/*utils.c * @Created on: 22-Jan-2024 * @Author: Vecmocon Technology */ #include #include "../Generated Codes/ti_msp_config.h" static volatile uint32_t g_vrefInitFlag = 0; volatile int g_i32TickCnt; uint8_t g_systickInitFlag_u8 = 0; static const DL_VREF_Config g_dlVrefConfig = { .vrefEnable = DL_VREF_ENABLE_DISABLE, .bufConfig = DL_VREF_BUFCONFIG_OUTPUT_2_5V, .shModeEnable = DL_VREF_SHMODE_DISABLE, .holdCycleCount = DL_VREF_HOLD_MIN, .shCycleCount = DL_VREF_SH_MIN, }; static const DL_VREF_ClockConfig g_dlVrefClockConfig = { .clockSel = DL_VREF_CLOCK_LFCLK, .divideRatio = DL_VREF_CLOCK_DIVIDE_1, }; /** * @brief Initializes the SysTick timer. * * Configures the SysTick timer with the specified period and sets the interrupt priority. * * @param[in] eTick The period for the SysTick timer. * * @return IVEC_CORE_STATUS_SUCCESS if initialization is successful. */ IVEC_CoreStatus_e xMCAL_systickInit(IVEC_SystickPeriod_e eTick) { SysTick_Config(eTick); NVIC_SetPriority(SysTick_IRQn, 0); g_systickInitFlag_u8 = 1; return IVEC_CORE_STATUS_SUCCESS; } static const DL_SYSCTL_SYSPLLConfig g_dlSysPllConfig = { .inputFreq = DL_SYSCTL_SYSPLL_INPUT_FREQ_16_32_MHZ, .rDivClk2x = 1, .rDivClk1 = 0, .rDivClk0 = 0, .enableCLK2x = DL_SYSCTL_SYSPLL_CLK2X_DISABLE, .enableCLK1 = DL_SYSCTL_SYSPLL_CLK1_DISABLE, .enableCLK0 = DL_SYSCTL_SYSPLL_CLK0_ENABLE, .sysPLLMCLK = DL_SYSCTL_SYSPLL_MCLK_CLK0, .sysPLLRef = DL_SYSCTL_SYSPLL_REF_HFCLK, .qDiv = 5, .pDiv = DL_SYSCTL_SYSPLL_PDIV_1, }; /** * @brief Initializes the system control settings, including clock source and low power mode. * * Configures the power policy, BOR threshold, and flash wait state based on the specified * low power mode and clock source. It also sets up system PLL and HFCLK parameters accordingly. * * @param[in] u8ClkSrc The clock source to use (e.g., HFXT or SYSOSC). * @param[in] u8LowPowerMode The low power mode to configure (e.g., STANDBY0 or SLEEP0). * * @return IVEC_CORE_STATUS_SUCCESS if initialization is successful, * IVEC_CORE_STATUS_INIT_FAIL if invalid parameters are provided. */ IVEC_CoreStatus_e xMCAL_sysctlInit(uint8_t u8ClkSrc, uint8_t u8LowPowerMode) { if ((u8LowPowerMode != IVEC_STANDBY0) && (u8LowPowerMode != IVEC_SLEEP0)) return IVEC_CORE_STATUS_INIT_FAIL; if ((u8ClkSrc != IVEC_HFXT) && (u8ClkSrc != IVEC_SYSOSC)) return IVEC_CORE_STATUS_INIT_FAIL; if (u8LowPowerMode == IVEC_STANDBY0) { DL_SYSCTL_setPowerPolicySTANDBY0(); DL_SYSCTL_setBORThreshold(DL_SYSCTL_BOR_THRESHOLD_LEVEL_0); DL_SYSCTL_setFlashWaitState(DL_SYSCTL_FLASH_WAIT_STATE_2); } else if (u8LowPowerMode == IVEC_SLEEP0) { DL_SYSCTL_setBORThreshold(DL_SYSCTL_BOR_THRESHOLD_LEVEL_0); DL_SYSCTL_setFlashWaitState(DL_SYSCTL_FLASH_WAIT_STATE_2); } if (u8ClkSrc == IVEC_HFXT) { DL_SYSCTL_setBORThreshold(DL_SYSCTL_BOR_THRESHOLD_LEVEL_0); DL_SYSCTL_setFlashWaitState(DL_SYSCTL_FLASH_WAIT_STATE_2); DL_SYSCTL_setSYSOSCFreq(DL_SYSCTL_SYSOSC_FREQ_BASE); DL_SYSCTL_disableHFXT(); DL_SYSCTL_disableSYSPLL(); DL_SYSCTL_setHFCLKSourceHFXTParams(DL_SYSCTL_HFXT_RANGE_16_32_MHZ, 10, true); DL_SYSCTL_configSYSPLL((DL_SYSCTL_SYSPLLConfig *)&g_dlSysPllConfig); DL_SYSCTL_setULPCLKDivider(DL_SYSCTL_ULPCLK_DIV_2); DL_SYSCTL_setMCLKSource(SYSOSC, HSCLK, DL_SYSCTL_HSCLK_SOURCE_SYSPLL); } else if (u8ClkSrc == IVEC_SYSOSC) { DL_SYSCTL_setSYSOSCFreq(DL_SYSCTL_SYSOSC_FREQ_BASE); DL_SYSCTL_disableHFXT(); DL_SYSCTL_disableSYSPLL(); DL_SYSCTL_setHFCLKSourceHFXTParams(DL_SYSCTL_HFXT_RANGE_16_32_MHZ, 50, true); DL_SYSCTL_configSYSPLL((DL_SYSCTL_SYSPLLConfig *)&g_dlSysPllConfig); DL_SYSCTL_setHFCLKDividerForMFPCLK(DL_SYSCTL_HFCLK_MFPCLK_DIVIDER_6); DL_SYSCTL_enableMFCLK(); DL_SYSCTL_enableMFPCLK(); DL_SYSCTL_setMFPCLKSource(DL_SYSCTL_MFPCLK_SOURCE_SYSOSC); } return IVEC_CORE_STATUS_SUCCESS; } /** * @brief SysTick interrupt handler. * * Increments the global tick counter (`g_i32TickCnt`) each time the SysTick interrupt occurs. */ void SysTick_Handler(void) { g_i32TickCnt++; } /** * @brief Retrieves the current tick count. * * @return The current tick count stored in `g_i32TickCnt`. */ int32_t i32MCAL_getTicks() { return g_i32TickCnt; } /** * @brief Delays execution for a specified number of milliseconds based on tick count. * * Waits until the number of ticks has passed corresponding to the input delay (in milliseconds). * * @param[in] i32DelayMs The delay in milliseconds to wait. */ void vMCAL_delayTicks(int32_t i32DelayMs) { int32_t l_i32CurrTick = i32MCAL_getTicks(); while ((i32MCAL_getTicks() - l_i32CurrTick) < i32DelayMs) { /* Wait */ } } /** * @brief Initializes the MCU by configuring power and GPIO settings. * * Calls the necessary functions to initialize power and GPIO configurations for the MCU. */ void vMCAL_mcuInit(void) { SYSCFG_DL_initPower(); vMCAL_WatchdogEnablePower(); xMCAL_WatchdogInit(MCAL_WDT_4_SEC_TIMER); // Timer Inputs can be : 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 16 , 24 , 32 , 40 , 48 , 56 , 64 in seconds SYSCFG_DL_GPIO_init(); } /** * @brief Delays execution for a specified number of microseconds. * * Uses a cycle-based delay function to wait for the specified duration in microseconds. * * @param[in] u32Us The delay duration in microseconds. */ void vMCAL_delayUs(uint32_t u32Us) { delay_cycles(32 * u32Us); } /** * @brief Initializes the voltage reference (VREF) system. * * Configures the clock and reference settings for the VREF system. If already initialized, * it returns a failure status. * * @return IVEC_MCAL_STATUS_SUCCESS if initialization is successful, * IVEC_MCAL_STATUS_INIT_FAIL if VREF is already initialized. */ IVEC_McalStatus_e xMCAL_vrefInit(void) { if (g_vrefInitFlag == 0) { DL_VREF_setClockConfig(VREF, (DL_VREF_ClockConfig *)&g_dlVrefClockConfig); DL_VREF_configReference(VREF, (DL_VREF_Config *)&g_dlVrefConfig); delay_cycles(320); g_vrefInitFlag = 1; return IVEC_MCAL_STATUS_SUCCESS; } else { return IVEC_MCAL_STATUS_INIT_FAIL; } } /** * @brief Performs a soft reset on the MCU. * * Resets the MCU's CPU by calling the appropriate system control function. */ void vMCAL_softReset(void) { DL_SYSCTL_resetDevice(DL_SYSCTL_RESET_CPU); } void vMCAL_WDG_Refresh(void) { xMCAL_WatchdogReset(); } void vMCAL_WatchdogDisablePower(void) { DL_WWDT_disablePower(WATCHDOG_TIMER); } void vMCAL_WatchdogEnablePower(void) { DL_WWDT_enablePower(WATCHDOG_TIMER); }