diff --git a/CMakeLists.txt b/CMakeLists.txt index 19f5de5..c9a06b0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -54,6 +54,7 @@ set(TARGET_SOURCES "src/main.c" "src/pah8001_impl.c" "src/sensors_impl.c" + "src/util_systick.c" ) set(TARGET_C_DEFINES @@ -62,6 +63,8 @@ set(TARGET_C_DEFINES "MCUXPRESSO_SDK" "SERIAL_PORT_TYPE_UART=1" "__STARTUP_CLEAR_BSS" + "PRINTF_FLOAT_ENABLE=1" + "PRINTF_ADVANCED_ENABLE=1" ) set(TARGET_C_INCLUDES @@ -78,9 +81,10 @@ set(TARGET_C_INCLUDES # Shared libraries linked with application set(TARGET_LIBS + "imsensors" + "m" "power_cm4_hardabi" "pah8001" - "imsensors" ) # Shared library and linker script search paths @@ -93,9 +97,9 @@ set(TARGET_CFLAGS_HARDWARE "-mcpu=cortex-m4 -mthumb -mfloat-abi=hard -mfpu=fpv4- # Conditional flags # DEBUG -set(CMAKE_C_FLAGS_DEBUG "-DDEBUG -Og -g") -set(CMAKE_CXX_FLAGS_DEBUG "-DDEBUG -Og -g") -set(CMAKE_ASM_FLAGS_DEBUG "-DDEBUG -Og -g") +set(CMAKE_C_FLAGS_DEBUG "-DDEBUG -O0 -g") +set(CMAKE_CXX_FLAGS_DEBUG "-DDEBUG -O0 -g") +set(CMAKE_ASM_FLAGS_DEBUG "-DDEBUG -O0 -g") # RELEASE set(CMAKE_C_FLAGS_RELEASE "-DNDEBUG -O2 -flto") @@ -107,7 +111,7 @@ set(CMAKE_EXE_LINKER_FLAGS_RELEASE "-flto") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${TARGET_CFLAGS_HARDWARE} -Wall -fno-common -fno-builtin -ffreestanding -fdata-sections -ffunction-sections") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TARGET_CFLAGS_HARDWARE} -Wall -fno-common -fno-builtin -ffreestanding -fdata-sections -ffunction-sections") set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} ${CMAKE_C_FLAGS} -x assembler-with-cpp") -set(CMAKE_EXE_LINKER_FLAGS "-specs=nano.specs -specs=nosys.specs -Wl,--gc-sections -Wl,--print-memory-usage -lc -lm -lnosys ") +set(CMAKE_EXE_LINKER_FLAGS "-specs=nano.specs -specs=nosys.specs -lnosys -Wl,--gc-sections -Wl,--print-memory-usage -Wl,--no-warn-rwx-segments") add_subdirectory(lib/pixart_pah8001ei) diff --git a/LPC54102J512_M4F.mex b/LPC54102J512_M4F.mex index e6eebbb..d28ae47 100644 --- a/LPC54102J512_M4F.mex +++ b/LPC54102J512_M4F.mex @@ -1,5 +1,5 @@ - + LPC54102J512 LPC54102J512BD64 @@ -18,13 +18,17 @@ false - + - 11.0.1 + 12.0.0 + + + + @@ -45,60 +49,82 @@ true + + + true + + true + + + true + + + + + true + + + + + + + + + - + - 11.0.1 + 12.0.0 - + - + true - + true - + true - + true - + true - + true @@ -112,6 +138,63 @@ + false + + + + + + + + true + + + + + true + + + + + true + + + + + true + + + + + true + + + + + true + + + + + + + + + + + + + + + + + + + + + + + true @@ -135,25 +218,35 @@ 2.0.2 + + + true + + + + + 2.1.3 + + - 11.0.1 + 12.0.0 - + true - + true @@ -163,11 +256,18 @@ 0 + + + true + + - + + + @@ -175,7 +275,7 @@ - + @@ -183,6 +283,24 @@ + + + + + + + + + + + + + + + + + + diff --git a/board/board.c b/board/board.c index e390122..cf11bc3 100644 --- a/board/board.c +++ b/board/board.c @@ -25,8 +25,8 @@ const uint32_t ExtClockIn = BOARD_EXTCLKINRATE; status_t BOARD_InitDebugConsole(void) { status_t result; - /* attach 12 MHz clock to USART0 (debug console) */ - CLOCK_AttachClk(BOARD_DEBUG_UART_CLK_ATTACH); + /* Do not attach clock here, since the clocks are already configured by tools */ + // CLOCK_AttachClk(BOARD_DEBUG_UART_CLK_ATTACH); RESET_PeripheralReset(BOARD_DEBUG_UART_RST); result = DbgConsole_Init(BOARD_DEBUG_UART_INSTANCE, BOARD_DEBUG_UART_BAUDRATE, BOARD_DEBUG_UART_TYPE, BOARD_DEBUG_UART_CLK_FREQ); @@ -36,8 +36,8 @@ status_t BOARD_InitDebugConsole(void) status_t BOARD_InitDebugConsole_Core1(void) { status_t result; - /* attach 12 MHz clock to USART2 (debug console) */ - CLOCK_AttachClk(BOARD_DEBUG_UART_CLK_ATTACH_CORE1); + /* Do not attach clock here, since the clocks are already configured by tools */ + // CLOCK_AttachClk(BOARD_DEBUG_UART_CLK_ATTACH_CORE1); RESET_PeripheralReset(BOARD_DEBUG_UART_RST_CORE1); result = DbgConsole_Init(BOARD_DEBUG_UART_INSTANCE_CORE1, BOARD_DEBUG_UART_BAUDRATE_CORE1, BOARD_DEBUG_UART_TYPE_CORE1, BOARD_DEBUG_UART_CLK_FREQ_CORE1); diff --git a/board/clock_config.c b/board/clock_config.c index 9eb75da..6ac0ebd 100644 --- a/board/clock_config.c +++ b/board/clock_config.c @@ -19,11 +19,11 @@ /* clang-format off */ /* TEXT BELOW IS USED AS SETTING FOR TOOLS ************************************* !!GlobalInfo -product: Clocks v9.0 +product: Clocks v10.0 processor: LPC54102J512 package_id: LPC54102J512BD64 mcu_data: ksdk2_0 -processor_version: 11.0.1 +processor_version: 12.0.0 * BE CAREFUL MODIFYING THIS COMMENT - IT IS YAML SETTINGS FOR TOOLS **********/ /* clang-format on */ @@ -38,25 +38,22 @@ processor_version: 11.0.1 /******************************************************************************* * Variables ******************************************************************************/ -/* System clock frequency. */ -extern uint32_t SystemCoreClock; /******************************************************************************* ************************ BOARD_InitBootClocks function ************************ ******************************************************************************/ void BOARD_InitBootClocks(void) { - BOARD_BootClockRUN(); + BOARD_BootClockPLL150M(); } /******************************************************************************* - ********************** Configuration BOARD_BootClockRUN *********************** + ******************** Configuration BOARD_BootClockIRC12M ********************** ******************************************************************************/ /* clang-format off */ /* TEXT BELOW IS USED AS SETTING FOR TOOLS ************************************* !!Configuration -name: BOARD_BootClockRUN -called_from_default_init: true +name: BOARD_BootClockIRC12M outputs: - {id: ASYNCAPB_clock.outFreq, value: 12 MHz} - {id: FRG_clock.outFreq, value: 12 MHz} @@ -66,12 +63,12 @@ outputs: /* clang-format on */ /******************************************************************************* - * Variables for BOARD_BootClockRUN configuration + * Variables for BOARD_BootClockIRC12M configuration ******************************************************************************/ /******************************************************************************* - * Code for BOARD_BootClockRUN configuration + * Code for BOARD_BootClockIRC12M configuration ******************************************************************************/ -void BOARD_BootClockRUN(void) +void BOARD_BootClockIRC12M(void) { /*!< Set up the clock sources */ /*!< Set up IRC */ @@ -96,6 +93,82 @@ void BOARD_BootClockRUN(void) CLOCK_AttachClk(kIRC12M_to_MAIN_CLK); /*!< Switch MAIN_CLK to IRC12M */ /*!< Set SystemCoreClock variable. */ - SystemCoreClock = BOARD_BOOTCLOCKRUN_CORE_CLOCK; + SystemCoreClock = BOARD_BOOTCLOCKIRC12M_CORE_CLOCK; +} + +/******************************************************************************* + ******************** Configuration BOARD_BootClockPLL150M ********************* + ******************************************************************************/ +/* clang-format off */ +/* TEXT BELOW IS USED AS SETTING FOR TOOLS ************************************* +!!Configuration +name: BOARD_BootClockPLL150M +called_from_default_init: true +outputs: +- {id: ASYNCAPB_clock.outFreq, value: 50 MHz} +- {id: FRG_clock.outFreq, value: 50 MHz} +- {id: MAIN_clock.outFreq, value: 150 MHz} +- {id: SYSTICK_clock.outFreq, value: 10 MHz} +- {id: System_clock.outFreq, value: 150 MHz} +settings: +- {id: PLL_Mode, value: Normal} +- {id: ASYNC_SYSCON.ASYNCAPBCLKSELB.sel, value: SYSCON.pll_clk} +- {id: ASYNC_SYSCON.ASYNCCLKDIV.scale, value: '3'} +- {id: SYSCON.CLKOUTDIV.scale, value: '0', locked: true} +- {id: SYSCON.DIRECTO.sel, value: SYSCON.PLL} +- {id: SYSCON.MAINCLKSELB.sel, value: SYSCON.PLL_BYPASS} +- {id: SYSCON.M_MULT.scale, value: '50', locked: true} +- {id: SYSCON.N_DIV.scale, value: '4', locked: true} +- {id: SYSCON.PLL_BYPASS.sel, value: SYSCON.DIRECTO} +- {id: SYSCON.SYSTICKCLKDIV.scale, value: '15', locked: true} + * BE CAREFUL MODIFYING THIS COMMENT - IT IS YAML SETTINGS FOR TOOLS **********/ +/* clang-format on */ + +/******************************************************************************* + * Variables for BOARD_BootClockPLL150M configuration + ******************************************************************************/ +/******************************************************************************* + * Code for BOARD_BootClockPLL150M configuration + ******************************************************************************/ +void BOARD_BootClockPLL150M(void) +{ + /*!< Set up the clock sources */ + /*!< Set up IRC */ + POWER_DisablePD(kPDRUNCFG_PD_IRC_OSC); /*!< Ensure IRC OSC is on */ + POWER_DisablePD(kPDRUNCFG_PD_IRC); /*!< Ensure IRC is on */ + CLOCK_AttachClk(kIRC12M_to_MAIN_CLK); /*!< Switch to IRC 12MHz first to ensure we can change voltage without accidentally + being below the voltage for current speed */ + + /*!< Set up PLL */ + CLOCK_AttachClk(kIRC12M_to_SYS_PLL); /*!< Switch SYSPLLCLKSEL to IRC12M */ + POWER_DisablePD(kPDRUNCFG_PD_SYS_PLL); /*!< Ensure PLL is on */ + const pll_setup_t pllSetup = { + .syspllctrl = SYSCON_SYSPLLCTRL_BANDSEL_MASK | SYSCON_SYSPLLCTRL_SELI(52U) | SYSCON_SYSPLLCTRL_SELP(26U) | SYSCON_SYSPLLCTRL_DIRECTO_MASK, + .syspllndec = SYSCON_SYSPLLNDEC_NDEC(2U), + .syspllpdec = SYSCON_SYSPLLPDEC_PDEC(2U), + .syspllssctrl = {(SYSCON_SYSPLLSSCTRL0_MDEC(32597U) | SYSCON_SYSPLLSSCTRL0_SEL_EXT_MASK),0x0U}, + .pllRate = 150000000U, + .flags = PLL_SETUPFLAG_WAITLOCK + }; + CLOCK_SetPLLFreq(&pllSetup); /*!< Configure PLL to the desired values */ + + + POWER_SetVoltageForFreq(150000000U); /*!< Set voltage for the one of the fastest clock outputs: System clock output */ + CLOCK_SetFLASHAccessCyclesForFreq(150000000U); /*!< Set FLASH wait states for core */ + + /*!< Set up dividers */ + CLOCK_SetClkDiv(kCLOCK_DivSystickClk, 15U, false); /*!< Set SYSTICKCLKDIV divider to value 15 */ + CLOCK_SetClkDiv(kCLOCK_DivAhbClk, 1U, false); /*!< Set AHBCLKDIV divider to value 1 */ + SYSCON->ASYNCAPBCTRL = SYSCON_ASYNCAPBCTRL_ENABLE_MASK; /*!< Enable ASYNC APB subsystem */ + Clock_SetAsyncClkDiv(3U); /*!< Set ASYNCCLKDIV divider to value 3 */ + ASYNC_SYSCON->FRGCTRL = ((ASYNC_SYSCON->FRGCTRL & ~ASYNC_SYSCON_FRGCTRL_MULT_MASK) | ASYNC_SYSCON_FRGCTRL_MULT(0U)); /*!< Set FRG MULT to value 0 */ + ASYNC_SYSCON->ASYNCAPBCLKCTRL |= ASYNC_SYSCON_ASYNCAPBCLKCTRL_FRG0_MASK; /*!< Enable FRG clock */ + + /*!< Set up clock selectors - Attach clocks to the peripheries */ + CLOCK_AttachClk(kSYS_PLL_OUT_to_MAIN_CLK); /*!< Switch MAIN_CLK to SYS_PLL_OUT */ + CLOCK_AttachClk(kSYS_PLL_OUT_to_ASYNC_APB); /*!< Switch ASYNC_APB to SYS_PLL_OUT */ + + /*!< Set SystemCoreClock variable. */ + SystemCoreClock = BOARD_BOOTCLOCKPLL150M_CORE_CLOCK; } diff --git a/board/clock_config.h b/board/clock_config.h index 8212f14..2ab3ac7 100644 --- a/board/clock_config.h +++ b/board/clock_config.h @@ -33,16 +33,16 @@ void BOARD_InitBootClocks(void); #endif /* __cplusplus*/ /******************************************************************************* - ********************** Configuration BOARD_BootClockRUN *********************** + ******************** Configuration BOARD_BootClockIRC12M ********************** ******************************************************************************/ /******************************************************************************* - * Definitions for BOARD_BootClockRUN configuration + * Definitions for BOARD_BootClockIRC12M configuration ******************************************************************************/ -#define BOARD_BOOTCLOCKRUN_CORE_CLOCK 12000000U /*!< Core clock frequency: 12000000Hz */ +#define BOARD_BOOTCLOCKIRC12M_CORE_CLOCK 12000000U /*!< Core clock frequency: 12000000Hz */ /******************************************************************************* - * API for BOARD_BootClockRUN configuration + * API for BOARD_BootClockIRC12M configuration ******************************************************************************/ #if defined(__cplusplus) extern "C" { @@ -52,7 +52,33 @@ extern "C" { * @brief This function executes configuration of clocks. * */ -void BOARD_BootClockRUN(void); +void BOARD_BootClockIRC12M(void); + +#if defined(__cplusplus) +} +#endif /* __cplusplus*/ + +/******************************************************************************* + ******************** Configuration BOARD_BootClockPLL150M ********************* + ******************************************************************************/ +/******************************************************************************* + * Definitions for BOARD_BootClockPLL150M configuration + ******************************************************************************/ +#define BOARD_BOOTCLOCKPLL150M_CORE_CLOCK 150000000U /*!< Core clock frequency: 150000000Hz */ + + +/******************************************************************************* + * API for BOARD_BootClockPLL150M configuration + ******************************************************************************/ +#if defined(__cplusplus) +extern "C" { +#endif /* __cplusplus*/ + +/*! + * @brief This function executes configuration of clocks. + * + */ +void BOARD_BootClockPLL150M(void); #if defined(__cplusplus) } diff --git a/board/peripherals.c b/board/peripherals.c index 03401de..786b6ed 100644 --- a/board/peripherals.c +++ b/board/peripherals.c @@ -10,7 +10,7 @@ product: Peripherals v11.0 processor: LPC54102J512 package_id: LPC54102J512BD64 mcu_data: ksdk2_0 -processor_version: 11.0.1 +processor_version: 12.0.0 functionalGroups: - name: BOARD_InitPeripherals UUID: bf976c30-387a-4c02-956c-44a37954526e @@ -67,7 +67,8 @@ instance: - peripheral: 'NVIC' - config_sets: - nvic: - - interrupt_table: [] + - interrupt_table: + - 0: [] - interrupts: [] * BE CAREFUL MODIFYING THIS COMMENT - IT IS YAML SETTINGS FOR TOOLS **********/ /* clang-format on */ @@ -93,7 +94,7 @@ instance: - fsl_i2c: - i2c_mode: 'kI2C_Master' - clockSource: 'FunctionClock' - - clockSourceFreq: 'BOARD_BootClockRUN' + - clockSourceFreq: 'BOARD_BootClockPLL150M' - i2c_master_config: - enableMaster: 'true' - baudRate_Bps: '100000' @@ -111,6 +112,46 @@ static void I2C0_init(void) { I2C_MasterInit(I2C0_PERIPHERAL, &I2C0_config, I2C0_CLOCK_SOURCE); } +/*********************************************************************************************************************** + * PINT initialization code + **********************************************************************************************************************/ +/* clang-format off */ +/* TEXT BELOW IS USED AS SETTING FOR TOOLS ************************************* +instance: +- name: 'PINT' +- type: 'pint' +- mode: 'interrupt_mode' +- custom_name_enabled: 'false' +- type_id: 'pint_cf4a806bb2a6c1ffced58ae2ed7b43af' +- functional_group: 'BOARD_InitPeripherals' +- peripheral: 'PINT' +- config_sets: + - general: + - interrupt_array: + - 0: + - interrupt_id: 'INT_0' + - interrupt_selection: 'PINT.0' + - interrupt_type: 'kPINT_PinIntEnableFallEdge' + - callback_function: 'bmm_drdy_callback' + - enable_callback: 'true' + - interrupt: + - IRQn: 'PIN_INT0_IRQn' + - enable_priority: 'true' + - priority: '4' + * BE CAREFUL MODIFYING THIS COMMENT - IT IS YAML SETTINGS FOR TOOLS **********/ +/* clang-format on */ + +static void PINT_init(void) { + /* PINT initiation */ + PINT_Init(PINT_PERIPHERAL); + /* Interrupt vector PIN_INT0_IRQn priority settings in the NVIC. */ + NVIC_SetPriority(PINT_PINT_0_IRQN, PINT_PINT_0_IRQ_PRIORITY); + /* PINT PINT.0 configuration */ + PINT_PinInterruptConfig(PINT_PERIPHERAL, PINT_INT_0, kPINT_PinIntEnableFallEdge, bmm_drdy_callback); + /* Enable PINT PINT.0 callback */ + PINT_EnableCallbackByIndex(PINT_PERIPHERAL, kPINT_PinInt0); +} + /*********************************************************************************************************************** * Initialization functions **********************************************************************************************************************/ @@ -118,6 +159,7 @@ void BOARD_InitPeripherals(void) { /* Initialize components */ I2C0_init(); + PINT_init(); } /*********************************************************************************************************************** diff --git a/board/peripherals.h b/board/peripherals.h index 2a6ff44..48cc802 100644 --- a/board/peripherals.h +++ b/board/peripherals.h @@ -11,6 +11,7 @@ **********************************************************************************************************************/ #include "fsl_common.h" #include "fsl_i2c.h" +#include "fsl_pint.h" #if defined(__cplusplus) extern "C" { @@ -24,13 +25,28 @@ extern "C" { /* Definition of peripheral ID */ #define I2C0_PERIPHERAL ((I2C_Type *)I2C0) /* Definition of the clock source frequency */ -#define I2C0_CLOCK_SOURCE 12000000UL +#define I2C0_CLOCK_SOURCE 50000000UL +/* BOARD_InitPeripherals defines for PINT */ +/* Definition of peripheral ID */ +#define PINT_PERIPHERAL ((PINT_Type *) PINT_BASE) +/* PINT interrupt vector ID (number). */ +#define PINT_PINT_0_IRQN PIN_INT0_IRQn +/* PINT interrupt vector priority. */ +#define PINT_PINT_0_IRQ_PRIORITY 4 +/* Definition of PINT interrupt ID for interrupt 0 */ +#define PINT_INT_0 kPINT_PinInt0 /*********************************************************************************************************************** * Global variables **********************************************************************************************************************/ extern const i2c_master_config_t I2C0_config; +/*********************************************************************************************************************** + * Callback functions + **********************************************************************************************************************/ +/* INT_0 callback function for the PINT component */ +extern void bmm_drdy_callback(pint_pin_int_t pintr, uint32_t pmatch_status); + /*********************************************************************************************************************** * Initialization functions **********************************************************************************************************************/ diff --git a/board/pin_mux.c b/board/pin_mux.c index 1718c18..9f789f6 100644 --- a/board/pin_mux.c +++ b/board/pin_mux.c @@ -7,16 +7,21 @@ /* * TEXT BELOW IS USED AS SETTING FOR TOOLS ************************************* !!GlobalInfo -product: Pins v11.0 +product: Pins v12.0 processor: LPC54102J512 package_id: LPC54102J512BD64 mcu_data: ksdk2_0 -processor_version: 11.0.1 +processor_version: 12.0.0 +pin_labels: +- {pin_num: '28', pin_signal: PIO1_8/ADC_11/SPI1_MISO/CT32B1_MAT3/CT32B1_CAP3, label: LED_R, identifier: LED_R} +- {pin_num: '41', pin_signal: PIO0_7/U1_SCLK/SCT0_OUT0/CT32B0_MAT2/CT32B0_CAP2, label: BMM_DRDY, identifier: BMM_DRDY} * BE CAREFUL MODIFYING THIS COMMENT - IT IS YAML SETTINGS FOR TOOLS *********** */ /* clang-format on */ #include "fsl_common.h" +#include "fsl_gpio.h" +#include "fsl_inputmux.h" #include "pin_mux.h" /* FUNCTION ************************************************************************************************************ @@ -40,6 +45,8 @@ BOARD_InitPins: - {pin_num: '32', peripheral: USART0, signal: TXD, pin_signal: PIO0_1/U0_TXD/SPI0_SSEL1/CT32B0_CAP1/SCT0_OUT1} - {pin_num: '1', peripheral: I2C0, signal: SCL, pin_signal: PIO0_23/I2C0_SCL/CT32B0_CAP0} - {pin_num: '2', peripheral: I2C0, signal: SDA, pin_signal: PIO0_24/I2C0_SDA/CT32B0_CAP1/CT32B0_MAT0} + - {pin_num: '28', peripheral: GPIO, signal: 'PIO1, 8', pin_signal: PIO1_8/ADC_11/SPI1_MISO/CT32B1_MAT3/CT32B1_CAP3, direction: OUTPUT, gpio_init_state: 'true'} + - {pin_num: '41', peripheral: PINT, signal: 'PINT, 0', pin_signal: PIO0_7/U1_SCLK/SCT0_OUT0/CT32B0_MAT2/CT32B0_CAP2} * BE CAREFUL MODIFYING THIS COMMENT - IT IS YAML SETTINGS FOR TOOLS *********** */ /* clang-format on */ @@ -53,8 +60,21 @@ BOARD_InitPins: /* Function assigned for the Cortex-M4F */ void BOARD_InitPins(void) { + /* Enables the clock for the input muxes. 0 = Disable; 1 = Enable.: 0x01u */ + CLOCK_EnableClock(kCLOCK_InputMux); /* Enables the clock for the IOCON block. 0 = Disable; 1 = Enable.: 0x01u */ CLOCK_EnableClock(kCLOCK_Iocon); + /* Enables the clock for the GPIO1 module */ + CLOCK_EnableClock(kCLOCK_Gpio1); + + gpio_pin_config_t LED_R_config = { + .pinDirection = kGPIO_DigitalOutput, + .outputLogic = 1U + }; + /* Initialize GPIO functionality on pin PIO1_8 (pin 28) */ + GPIO_PinInit(BOARD_INITPINS_LED_R_GPIO, BOARD_INITPINS_LED_R_PORT, BOARD_INITPINS_LED_R_PIN, &LED_R_config); + /* PIO0_7 is selected for PINT input 0 */ + INPUTMUX_AttachSignal(INPUTMUX, 0U, kINPUTMUX_GpioPort0Pin7ToPintsel); IOCON->PIO[0][0] = ((IOCON->PIO[0][0] & /* Mask bits to zero which are setting */ @@ -103,6 +123,30 @@ void BOARD_InitPins(void) /* Select Analog/Digital mode. * : Digital mode. */ | IOCON_PIO_DIGIMODE(PIO024_DIGIMODE_DIGITAL)); + + IOCON->PIO[0][7] = ((IOCON->PIO[0][7] & + /* Mask bits to zero which are setting */ + (~(IOCON_PIO_FUNC_MASK | IOCON_PIO_DIGIMODE_MASK))) + + /* Selects pin function. + * : PORT07 (pin 41) is configured as PIO0_7. */ + | IOCON_PIO_FUNC(PIO07_FUNC_ALT0) + + /* Select Analog/Digital mode. + * : Digital mode. */ + | IOCON_PIO_DIGIMODE(PIO07_DIGIMODE_DIGITAL)); + + IOCON->PIO[1][8] = ((IOCON->PIO[1][8] & + /* Mask bits to zero which are setting */ + (~(IOCON_PIO_FUNC_MASK | IOCON_PIO_DIGIMODE_MASK))) + + /* Selects pin function. + * : PORT18 (pin 28) is configured as PIO1_8. */ + | IOCON_PIO_FUNC(PIO18_FUNC_ALT0) + + /* Select Analog/Digital mode. + * : Digital mode. */ + | IOCON_PIO_DIGIMODE(PIO18_DIGIMODE_DIGITAL)); } /*********************************************************************************************************************** * EOF diff --git a/board/pin_mux.h b/board/pin_mux.h index 13f637c..23be36f 100644 --- a/board/pin_mux.h +++ b/board/pin_mux.h @@ -33,6 +33,28 @@ void BOARD_InitBootPins(void); #define PIO023_FUNC_ALT1 0x01u /*!<@brief Selects pin function.: Alternative connection 1. */ #define PIO024_DIGIMODE_DIGITAL 0x01u /*!<@brief Select Analog/Digital mode.: Digital mode. */ #define PIO024_FUNC_ALT1 0x01u /*!<@brief Selects pin function.: Alternative connection 1. */ +#define PIO07_DIGIMODE_DIGITAL 0x01u /*!<@brief Select Analog/Digital mode.: Digital mode. */ +#define PIO07_FUNC_ALT0 0x00u /*!<@brief Selects pin function.: Alternative connection 0. */ +#define PIO18_DIGIMODE_DIGITAL 0x01u /*!<@brief Select Analog/Digital mode.: Digital mode. */ +#define PIO18_FUNC_ALT0 0x00u /*!<@brief Selects pin function.: Alternative connection 0. */ + +/*! @name PIO1_8 (number 28), LED_R + @{ */ + +/* Symbols to be used with GPIO driver */ +#define BOARD_INITPINS_LED_R_GPIO GPIO /*!<@brief GPIO peripheral base pointer */ +#define BOARD_INITPINS_LED_R_GPIO_PIN_MASK (1U << 8U) /*!<@brief GPIO pin mask */ +#define BOARD_INITPINS_LED_R_PORT 1U /*!<@brief PORT peripheral base pointer */ +#define BOARD_INITPINS_LED_R_PIN 8U /*!<@brief PORT pin number */ +#define BOARD_INITPINS_LED_R_PIN_MASK (1U << 8U) /*!<@brief PORT pin mask */ + /* @} */ + +/*! @name PIO0_7 (number 41), BMM_DRDY + @{ */ +#define BOARD_INITPINS_BMM_DRDY_PORT 0U /*!<@brief PORT peripheral base pointer */ +#define BOARD_INITPINS_BMM_DRDY_PIN 7U /*!<@brief PORT pin number */ +#define BOARD_INITPINS_BMM_DRDY_PIN_MASK (1U << 7U) /*!<@brief PORT pin mask */ + /* @} */ /*! * @brief Configures pin routing and optionally pin electrical features. diff --git a/include/util_systick.h b/include/util_systick.h new file mode 100644 index 0000000..5184c73 --- /dev/null +++ b/include/util_systick.h @@ -0,0 +1,11 @@ +#ifndef UTIL_SYSTICK_H +#define UTIL_SYSTICK_H + +#include + +void util_systick_init(void); +void util_systick_delay(uint64_t msec); +uint64_t util_systick_get(void); + + +#endif \ No newline at end of file diff --git a/src/main.c b/src/main.c index 710570c..1779aa7 100644 --- a/src/main.c +++ b/src/main.c @@ -9,6 +9,9 @@ /* PPG */ #include "pixart_pah8001ei.h" +/* Utils */ +#include "util_systick.h" + /* Sensors */ #include "imsensors/dht/dht_bme280.h" @@ -50,11 +53,13 @@ ims_bme280_t s_bmp = { int main(void) { BOARD_InitBootPins(); - BOARD_BootClockRUN(); + BOARD_InitBootClocks(); BOARD_InitBootPeripherals(); BOARD_InitDebugConsole(); + util_systick_init(); + PRINTF("Hello world!!\r\n"); pah8001_init(&s_pah); @@ -73,5 +78,11 @@ int main(void) { for (;;) { ims_bme280_measure_float(&s_bme, &r_bme_float); ims_bme280_measure_float(&s_bmp, &r_bmp_float); + + PRINTF("BME T: %3.2f, H:%3.02f, P: %5.02f\r\n", r_bme_float.temperature, r_bme_float.humidity, r_bme_float.pressure); + PRINTF("BMP T: %3.2f, H:%3.02f, P: %5.02f\r\n", r_bmp_float.temperature, r_bmp_float.humidity, r_bmp_float.pressure); + + util_systick_delay(500); + GPIO_PortToggle(BOARD_INITPINS_LED_R_GPIO, BOARD_INITPINS_LED_R_PORT, BOARD_INITPINS_LED_R_GPIO_PIN_MASK); } } \ No newline at end of file diff --git a/src/sensors_impl.c b/src/sensors_impl.c index 44df076..b42c672 100644 --- a/src/sensors_impl.c +++ b/src/sensors_impl.c @@ -2,43 +2,54 @@ /* SDK drivers */ #include "fsl_i2c.h" +#include "fsl_pint.h" + +/* Utils */ +#include "util_systick.h" ims_ret_t sensors_impl_i2c_xfer(void *pdev, uint16_t addr, ims_i2c_xfer_desc_t *xfer) { if (xfer->tx_size > 0) { if (I2C_MasterStart(I2C0, addr, kI2C_Write) != kStatus_Success) { - return IMS_FAIL; + goto xfer_fail; } if (I2C_MasterWriteBlocking(I2C0, xfer->tx_data, xfer->tx_size, kI2C_TransferNoStopFlag) != kStatus_Success) { - return IMS_FAIL; + goto xfer_fail; } } if (xfer->rx_size > 0) { if (xfer->tx_size > 0) { if (I2C_MasterRepeatedStart(I2C0, addr, kI2C_Read) != kStatus_Success) { - return IMS_FAIL; + goto xfer_fail; } } else { if (I2C_MasterStart(I2C0, addr, kI2C_Read) != kStatus_Success) { - return IMS_FAIL; + goto xfer_fail; } } if (I2C_MasterReadBlocking(I2C0, xfer->rx_data, xfer->rx_size, kI2C_TransferNoStopFlag) != kStatus_Success) { - return IMS_FAIL; + goto xfer_fail; } } if (I2C_MasterStop(I2C0) != kStatus_Success) { - return IMS_FAIL; + goto xfer_fail; } + return IMS_SUCCESS; + +xfer_fail: + /* Clean-up */ + I2C_MasterReset(I2C0); + return IMS_FAIL; +} + +ims_ret_t sensors_impl_delay(void *pdev, uint32_t msec) { + util_systick_delay(msec); + return IMS_SUCCESS; } -ims_ret_t sensors_impl_delay(void *pdev, uint32_t msec) { - SDK_DelayAtLeastUs(msec * 20); - - return IMS_SUCCESS; -} \ No newline at end of file +void bmm_drdy_callback(pint_pin_int_t pintr, uint32_t pmatch_status) {} \ No newline at end of file diff --git a/src/util_systick.c b/src/util_systick.c new file mode 100644 index 0000000..19ce442 --- /dev/null +++ b/src/util_systick.c @@ -0,0 +1,33 @@ +/* SDK drivers */ +#include "fsl_common.h" + +#include "util_systick.h" + +static volatile uint64_t s_tickcount; + +void util_systick_init(void) { + uint32_t tick_freq = CLOCK_GetFreq(kCLOCK_MainClk) / SYSCON->SYSTICKCLKDIV; + + SysTick->LOAD = (tick_freq / 1000) - 1U; + SysTick->VAL = 0U; + + NVIC_SetPriority(SysTick_IRQn, (1UL << __NVIC_PRIO_BITS) - 1UL); + + SysTick->CTRL = SysTick_CTRL_TICKINT_Msk | SysTick_CTRL_ENABLE_Msk; +} + +uint64_t util_systick_get(void) { + return s_tickcount; +} + +void util_systick_delay(uint64_t msec) { + uint64_t tick_end = s_tickcount + msec; + + while(tick_end > s_tickcount) { + __WFI(); + } +} + +void SysTick_Handler(void) { + s_tickcount++; +} \ No newline at end of file