OSStatus PlatformWDGInitialize( uint32_t timeout_ms ) { plat_log_trace(); OSStatus err = kNoErr; // PLATFORM_TO_DO /* Get the LSI frequency: TIM5 is used to measure the LSI frequency */ LsiFreq = GetLSIFrequency(); IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable); /* IWDG counter clock: LSI/32 */ IWDG_SetPrescaler(IWDG_Prescaler_128); /* Set counter reload value to obtain 250ms IWDG TimeOut. Counter Reload Value = 250ms/IWDG counter clock period = 250ms / (LSI/32) = 0.25s / (LsiFreq/32) = LsiFreq/(32 * 4) = LsiFreq/128 */ IWDG_SetReload((uint16_t)(LsiFreq*timeout_ms/128000)); /* Reload IWDG counter */ IWDG_ReloadCounter(); /* Enable IWDG (the LSI oscillator will be enabled by hardware) */ IWDG_Enable(); return err; }
/** * Initializes the boards watchdog timer. If the behavior of the board is changed, this method may need to be * modified if the main control loop is changed. * * @param none * @retval none */ void Watchdog_Init(void) { /* Get the LSI frequency from TIM5 */ LsiFreq = GetLSIFrequency(); printf("LSI Frequency: %" PRIu32 "\n", LsiFreq); /* Enable write access to IWDG_PR and IWDG_RLR registers */ IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable); /* IWDG counter clock: LSI/32 */ IWDG_SetPrescaler(IWDG_Prescaler_64); /* Set counter reload value to obtain 1s IWDG TimeOut. IWDG counter clock Frequency = LsiFreq/32 Counter Reload Value = 250ms/IWDG counter clock period = 0.25s / (32/LsiFreq) = LsiFreq/(32 * 4) = LsiFreq/128 */ IWDG_SetReload(LsiFreq/16); /* Reload IWDG counter */ IWDG_ReloadCounter(); /* Enable IWDG (the LSI oscillator will be enabled by hardware) */ IWDG_Enable(); }
/** * @brief Main program. * @param None * @retval None */ int main(void) { /* STM32L0xx HAL library initialization: - Configure the Flash prefetch, Flash preread and Buffer caches - Systick timer is configured by default as source of time base, but user can eventually implement his proper time base source (a general purpose timer for example or other time source), keeping in mind that Time base duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and handled in milliseconds basis. - Low Level Initialization */ HAL_Init(); /******************************************************************************* * Common Configuration Routines * *******************************************************************************/ /* Configure LED2 */ BSP_LED_Init(LED2); /* Configure the system clock to 32 Mhz */ SystemClock_Config(); /* Configure Button Key */ BSP_PB_Init(BUTTON_KEY, BUTTON_MODE_GPIO); /* RTC Configuration -------------------------------------------------------*/ RTC_Config(); /* Wait Until KEY BUTTON is pressed */ while(BSP_PB_GetState(BUTTON_KEY) != RESET) { } while(BSP_PB_GetState(BUTTON_KEY) != SET) { } /* Get the LSI frequency: TIM21 is used to measure the LSI frequency */ uwLsiFreq = GetLSIFrequency(); /* Update the Calendar Configuration with the LSI exact value */ RtcHandle.Init.HourFormat = RTC_HOURFORMAT_24; RtcHandle.Init.AsynchPrediv = 0x7F; RtcHandle.Init.SynchPrediv = (uwLsiFreq/128) - 1; RtcHandle.Init.OutPut = RTC_OUTPUT_DISABLE; RtcHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH; RtcHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN; if(HAL_RTC_Init(&RtcHandle) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /* Infinite loop */ while (1) { } }
/* ********************************************************************************************************* * 函 数 名: bsp_InitIwdg * 功能说明: 独立看门狗时间配置函数 * 形 参:IWDGTime: 0 ---- 0x0FFF * 独立看门狗时间设置,单位为ms,IWDGTime = 1000 大约就是一秒的 * 时间,这里没有结合TIM5测得实际LSI频率,只是为了操作方便取了 * 一个估计值超过IWDGTime不进行喂狗的话系统将会复位。 * LSI = 34000左右 * 返 回 值: 无 ********************************************************************************************************* */ void bsp_iwdg_init(unsigned int IWDGTime) { /* 检测系统是否从独立看门狗复位中恢复 */ if (RCC_GetFlagStatus(RCC_FLAG_IWDGRST) != RESET) { /* 清除复位标志 */ RCC_ClearFlag(); } else { /* 标志没有设置 */ } #if 1 /* 通过TIM5输入捕获得到LSI频率 */ LsiFreq = GetLSIFrequency(); #else /* 使能LSI */ RCC_LSICmd(ENABLE); /* 等待直到LSI就绪 */ while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET) {} /* */ LsiFreq = 32000; #endif /* 写入0x5555表示允许访问IWDG_PR 和IWDG_RLR寄存器 */ IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable); /* LSI/32 分频*/ IWDG_SetPrescaler(IWDG_Prescaler_256); /*特别注意,由于这里_ulIWDGTime的最小单位是ms, 所以这里重装计数的 计数时 需要除以1000 Counter Reload Value = (IWDGTime / 1000) /(1 / IWDG counter clock period) = (IWDGTime / 1000) / (32/LSI) = (IWDGTime / 1000) / (32/LsiFreq) = LsiFreq * IWDGTime / 32000 实际测试LsiFreq = 34000,所以这里取1的时候 大概就是1ms */ IWDG_SetReload(IWDGTime); /* 重载IWDG计数 */ IWDG_ReloadCounter(); /* 使能 IWDG (LSI oscillator 由硬件使能) */ IWDG_Enable(); }
/** * @brief Main program * @param None * @retval None */ int main(void) { /* STM32F107xC HAL library initialization: - Configure the Flash prefetch - Systick timer is configured by default as source of time base, but user can eventually implement his proper time base source (a general purpose timer for example or other time source), keeping in mind that Time base duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and handled in milliseconds basis. - Set NVIC Group Priority to 4 - Low Level Initialization */ HAL_Init(); /* Configure the system clock to 72 MHz */ SystemClock_Config(); /* Configure LED1 */ BSP_LED_Init(LED1); BSP_LED_Init(LED_RED); /* Configure Button Key */ BSP_PB_Init(BUTTON_KEY, BUTTON_MODE_GPIO); /* RTC Configuration -------------------------------------------------------*/ RTC_Config(); /* Wait Until KEY BUTTON is pressed */ while(BSP_PB_GetState(BUTTON_KEY) != RESET) { } while(BSP_PB_GetState(BUTTON_KEY) != SET) { } /* Get the LSI frequency: TIM5 is used to measure the LSI frequency */ uwLsiFreq = GetLSIFrequency(); /* Update the Calendar Configuration with the LSI exact value */ RtcHandle.Init.AsynchPrediv = (uwLsiFreq - 1); if(HAL_RTC_Init(&RtcHandle) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /* Infinite loop */ while (1) { } }
/** * @brief Configure the current time and date. * @param None * @retval None */ void k_CalendarBkupInit(void) { RCC_OscInitTypeDef RCC_OscInitStruct; RCC_PeriphCLKInitTypeDef PeriphClkInitStruct; /*##-1- Configure LSE as RTC clock source ##################################*/ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; RCC_OscInitStruct.LSEState = RCC_LSE_OFF; RCC_OscInitStruct.LSIState = RCC_LSI_ON; if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { return; } PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC; PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI; if(HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) { return; } /*##-3- Get the LSI frequency: TIM5 is used to measure the LSI frequency ###*/ uwLsiFreq = GetLSIFrequency(); /*##-1- Configure the RTC peripheral #######################################*/ /* Configure RTC prescaler and RTC data registers */ /* RTC configured as follow: - Hour Format = Format 24 - Asynch Prediv = Value according to source clock - Synch Prediv = Value according to source clock - OutPut = Output Disable - OutPutPolarity = High Polarity - OutPutType = Open Drain */ RtcHandle.Instance = RTC; RtcHandle.Init.HourFormat = RTC_HOURFORMAT_24; RtcHandle.Init.AsynchPrediv = RTC_ASYNCH_PREDIV; RtcHandle.Init.SynchPrediv = (uwLsiFreq/128) - 1; RtcHandle.Init.OutPut = RTC_OUTPUT_DISABLE; RtcHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH; RtcHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN; if(HAL_RTC_Init(&RtcHandle) != HAL_OK) { } }
/** * @brief Main program * @param None * @retval None */ int main(void) { /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup files (startup_stm32f40_41xxx.s/startup_stm32f427_437xx.s) before to branch to application main. */ /* Initialize LEDs mounted on EVAL board */ STM_EVAL_LEDInit(LED1); STM_EVAL_LEDInit(LED2); /* Initialize Tamper Button mounted on EVAL board */ STM_EVAL_PBInit(BUTTON_TAMPER, BUTTON_MODE_GPIO); /* RTC Configuration */ RTC_Config(); /* Wait Until BUTTON TAMPER is pressed */ while(STM_EVAL_PBGetState(BUTTON_TAMPER) != RESET) { } /* Get the LSI frequency: TIM5 is used to measure the LSI frequency */ uwLsiFreq = GetLSIFrequency(); /* Turn on LED2 */ STM_EVAL_LEDOn(LED2); /* Calendar Configuration */ /* ck_spre(1Hz) = RTCCLK(LSI) /(AsynchPrediv + 1)*(SynchPrediv + 1)*/ RTC_InitStructure.RTC_AsynchPrediv = 0x7F; RTC_InitStructure.RTC_SynchPrediv = (uwLsiFreq/128) - 1; RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24; RTC_Init(&RTC_InitStructure); while (1) { } }
void RTC_to_default_state(void) { /* RTC Configuration -------------------------------------------------------*/ RTC_Config(); /*1 seconds delay*/ //DelayResolution100us(10000); /* Get the LSI frequency: TIM5 is used to measure the LSI frequency */ LsiFreq = GetLSIFrequency(); /* Turn on LED2 */ //STM_EVAL_LEDOn(LED2); /* Calendar Configuration */ //LsiFreq = 35307 RTC_InitStructure.RTC_AsynchPrediv = 0x7F; RTC_InitStructure.RTC_SynchPrediv = 275;//(LsiFreq/128) - 1; //276*128=35328 //160*220=35200 //130*271=35300 RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24; RTC_Init(&RTC_InitStructure); RTC_DateStructure.RTC_Month = RTC_Month_March; RTC_DateStructure.RTC_Date = 28; RTC_DateStructure.RTC_Year = 13; RTC_DateStructure.RTC_WeekDay = RTC_Weekday_Thursday; RTC_TimeStructure.RTC_H12 = RTC_H12_AM; RTC_TimeStructure.RTC_Hours = 16; RTC_TimeStructure.RTC_Minutes = 30; RTC_TimeStructure.RTC_Seconds = 00; //RTC_SetTime(RTC_Format_BIN, &RTC_TimeStructure); //RTC_SetDate(RTC_Format_BIN,&RTC_DateStructure); }
/** * @brief Main program. * @param None * @retval None */ int main(void) { /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32f30x.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f30x.c file */ /* Initialize LEDs mounted on STM32303C-EVAL board --------------------------*/ STM_EVAL_LEDInit(LED1); STM_EVAL_LEDInit(LED2); STM_EVAL_PBInit(BUTTON_KEY, BUTTON_MODE_GPIO); /* RTC Configuration -------------------------------------------------------*/ RTC_Config(); /* Wait Until KEY BUTTON is pressed */ while(STM_EVAL_PBGetState(BUTTON_KEY) != RESET) { } /* Get the LSI frequency: TIM16 is used to measure the LSI frequency */ LsiFreq = GetLSIFrequency(); /* Turn on LED2 */ STM_EVAL_LEDOn(LED2); /* Calendar Configuration */ RTC_InitStructure.RTC_AsynchPrediv = 88; RTC_InitStructure.RTC_SynchPrediv = (LsiFreq/89) - 1; RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24; RTC_Init(&RTC_InitStructure); /* Infinite loop */ while (1) { } }
OSStatus platform_watchdog_init( uint32_t timeout_ms ) { // PLATFORM_TO_DO #ifndef MICO_DISABLE_WATCHDOG OSStatus err = kNoErr; uint32_t reloadTick; timeout_ms += 1000; /* Get the LSI frequency: TIM5 is used to measure the LSI frequency */ LsiFreq = GetLSIFrequency(); /* Set counter reload value to obtain timeout_ms IWDG TimeOut. Counter Reload Value = timeout_s /IWDG counter clock period = timeout_ms * (LSI/256) / 1000 */ reloadTick = (LsiFreq*timeout_ms)/256000; require_action( reloadTick <= 0xFFF, exit, err = kParamErr ); IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable); /* IWDG counter clock: LSI/256 */ IWDG_SetPrescaler(IWDG_Prescaler_256); IWDG_SetReload(reloadTick); /* Reload IWDG counter */ IWDG_ReloadCounter(); /* Enable IWDG (the LSI oscillator will be enabled by hardware) */ IWDG_Enable(); exit: return err; #else UNUSED_PARAMETER( timeout_ms ); return kUnsupportedErr; #endif }
/** * @brief Main program. * @param None * @retval None */ int main(void) { /* STM32F4xx HAL library initialization: - Configure the Flash prefetch, instruction and Data caches - Systick timer is configured by default as source of time base, but user can eventually implement his proper time base source (a general purpose timer for example or other time source), keeping in mind that Time base duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and handled in milliseconds basis. - Set NVIC Group Priority to 4 - Low Level Initialization: global MSP (MCU Support Package) initialization */ HAL_Init(); /* Configure the system clock to 180 MHz */ SystemClock_Config(); /* Configure LED1, LED2, LED3 and Tamper push-button */ BSP_LED_Init(LED1); BSP_LED_Init(LED2); BSP_LED_Init(LED3); /* Configure Tamper push-button */ BSP_PB_Init(BUTTON_TAMPER, BUTTON_MODE_EXTI); /*##-1- Check if the system has resumed from IWDG reset ####################*/ if (__HAL_RCC_GET_FLAG(RCC_FLAG_IWDGRST) != RESET) { /* IWDGRST flag set: Turn LED1 on */ BSP_LED_On(LED1); /* Clear reset flags */ __HAL_RCC_CLEAR_RESET_FLAGS(); } else { /* IWDGRST flag is not set: Turn LED1 off */ BSP_LED_Off(LED1); } /*##-2- Get the LSI frequency: TIM5 is used to measure the LSI frequency ###*/ uwLsiFreq = GetLSIFrequency(); /*##-3- Configure the IWDG peripheral ######################################*/ /* Set counter reload value to obtain 250ms IWDG TimeOut. IWDG counter clock Frequency = LsiFreq / 32 Counter Reload Value = 250ms / IWDG counter clock period = 0.25s / (32/LsiFreq) = LsiFreq / (32 * 4) = LsiFreq / 128 */ IwdgHandle.Instance = IWDG; IwdgHandle.Init.Prescaler = IWDG_PRESCALER_32; IwdgHandle.Init.Reload = uwLsiFreq / 128; if (HAL_IWDG_Init(&IwdgHandle) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /*##-4- Start the IWDG #####################################################*/ if (HAL_IWDG_Start(&IwdgHandle) != HAL_OK) { Error_Handler(); } /* Infinite loop */ while (1) { /* Toggle LED2 */ BSP_LED_Toggle(LED2); /* Insert 240 ms delay */ HAL_Delay(240); /* Refresh IWDG: reload counter */ if (HAL_IWDG_Refresh(&IwdgHandle) != HAL_OK) { /* Refresh Error */ Error_Handler(); } } }
/** * @brief Main program * @param None * @retval None */ int main(void) { /* STM32F4xx HAL library initialization: - Configure the Flash prefetch, instruction and Data caches - Configure the Systick to generate an interrupt each 1 msec - Set NVIC Group Priority to 4 - Global MSP (MCU Support Package) initialization */ HAL_Init(); /* Configure the system clock to 168 MHz */ SystemClock_Config(); /* Configure LED1, LED2 and LED3 */ BSP_LED_Init(LED1); BSP_LED_Init(LED2); BSP_LED_Init(LED3); /* Configure the User Button in EXTI Mode */ BSP_PB_Init(BUTTON_TAMPER, BUTTON_MODE_EXTI); /*##-1- Check if the system has resumed from IWDG reset ####################*/ if(__HAL_RCC_GET_FLAG(RCC_FLAG_IWDGRST) != RESET) { /* IWDGRST flag set: Turn LED1 on */ BSP_LED_On(LED1); /* Clear reset flags */ __HAL_RCC_CLEAR_RESET_FLAGS(); } else { /* IWDGRST flag is not set: Turn LED1 off */ BSP_LED_Off(LED1); } /*##-2- Get the LSI frequency: TIM5 is used to measure the LSI frequency ###*/ uwLsiFreq = GetLSIFrequency(); /*##-3- Configure the IWDG peripheral ######################################*/ /* Set counter reload value to obtain 250ms IWDG Timeout. IWDG counter clock Frequency = LsiFreq / 32 Counter Reload Value = 250ms / IWDG counter clock period = 0.25s / (32/LsiFreq) = LsiFreq / (32 * 4) = LsiFreq / 128 */ IwdgHandle.Instance = IWDG; IwdgHandle.Init.Prescaler = IWDG_PRESCALER_32; IwdgHandle.Init.Reload = uwLsiFreq/128; if(HAL_IWDG_Init(&IwdgHandle) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /*##-4- Start the IWDG #####################################################*/ if(HAL_IWDG_Start(&IwdgHandle) != HAL_OK) { Error_Handler(); } /* Infinite loop */ while (1) { /* Toggle LED2 */ BSP_LED_Toggle(LED2); /* Insert 240 ms delay */ HAL_Delay(240); /* Refresh IWDG: reload counter */ if(HAL_IWDG_Refresh(&IwdgHandle) != HAL_OK) { /* Refresh Error */ Error_Handler(); } } }
/** * @brief Main program * @param None * @retval None */ int main(void) { /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32f2xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f2xx.c file */ /* Initialize LED1, LED2 and Key Button mounted on STM322xG-EVAL board */ STM_EVAL_LEDInit(LED1); STM_EVAL_LEDInit(LED2); STM_EVAL_PBInit(BUTTON_KEY, BUTTON_MODE_EXTI); /* Setup SysTick Timer for 1 msec interrupts */ if (SysTick_Config(SystemCoreClock / 1000)) { /* Capture error */ while (1); } /* Check if the system has resumed from IWDG reset */ if (RCC_GetFlagStatus(RCC_FLAG_IWDGRST) != RESET) { /* IWDGRST flag set */ /* Turn on LED1 */ STM_EVAL_LEDOn(LED1); /* Clear reset flags */ RCC_ClearFlag(); } else { /* IWDGRST flag is not set */ /* Turn off LED1 */ STM_EVAL_LEDOff(LED1); } /* Get the LSI frequency: TIM5 is used to measure the LSI frequency */ LsiFreq = GetLSIFrequency(); /* IWDG timeout equal to 250 ms (the timeout may varies due to LSI frequency dispersion) */ /* Enable write access to IWDG_PR and IWDG_RLR registers */ IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable); /* IWDG counter clock: LSI/32 */ IWDG_SetPrescaler(IWDG_Prescaler_32); /* Set counter reload value to obtain 250ms IWDG TimeOut. Counter Reload Value = 250ms/IWDG counter clock period = 250ms / (LSI/32) = 0.25s / (LsiFreq/32) = LsiFreq/(32 * 4) = LsiFreq/128 */ IWDG_SetReload(LsiFreq/128); /* Reload IWDG counter */ IWDG_ReloadCounter(); /* Enable IWDG (the LSI oscillator will be enabled by hardware) */ IWDG_Enable(); while (1) { /* Toggle LED2 */ STM_EVAL_LEDToggle(LED2); /* Insert 240 ms delay */ Delay(240); /* Reload IWDG counter */ IWDG_ReloadCounter(); } }
/** * @brief Main program * @param None * @retval None */ int main(void) { /* STM32L0xx HAL library initialization: - Configure the Flash prefetch, Flash preread and Buffer caches - Systick timer is configured by default as source of time base, but user can eventually implement his proper time base source (a general purpose timer for example or other time source), keeping in mind that Time base duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and handled in milliseconds basis. - Low Level Initialization */ HAL_Init(); /******************************************************************************* * Common Configuration Routines * *******************************************************************************/ /* Configure LED2 and Key Button */ BSP_LED_Init(LED2); BSP_PB_Init(BUTTON_KEY, BUTTON_MODE_EXTI); /* Configure the system clock to 32 Mhz */ SystemClock_Config(); /*##-1- Check if the system has resumed from IWDG reset ####################*/ if (__HAL_RCC_GET_FLAG(RCC_FLAG_IWDGRST) != RESET) { /* IWDGRST flag set: Turn LED2 on */ BSP_LED_On(LED2); /* Insert 4s delay */ HAL_Delay(4000); /* Clear reset flags */ __HAL_RCC_CLEAR_RESET_FLAGS(); } else { /* IWDGRST flag is not set: Turn LED2 off */ BSP_LED_Off(LED2); } /*##-2- Get the LSI frequency: TIM21 is used to measure the LSI frequency ###*/ uwLsiFreq = GetLSIFrequency(); /*##-3- Configure & Initialize the IWDG peripheral ######################################*/ /* Set counter reload value to obtain 250ms IWDG TimeOut. IWDG counter clock Frequency = LsiFreq/32 Counter Reload Value = 250ms/IWDG counter clock period = 0.25s / (32/LsiFreq) = LsiFreq/(32 * 4) = LsiFreq/128 */ IwdgHandle.Instance = IWDG; IwdgHandle.Init.Prescaler = IWDG_PRESCALER_32; IwdgHandle.Init.Reload = uwLsiFreq/128; IwdgHandle.Init.Window = IWDG_WINDOW_DISABLE; if(HAL_IWDG_Init(&IwdgHandle) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /*##-4- Start the IWDG #####################################################*/ if(HAL_IWDG_Start(&IwdgHandle) != HAL_OK) { Error_Handler(); } /* Infinite loop */ while (1) { /* Toggle LED2 */ BSP_LED_Toggle(LED2); /* Insert 240 ms delay */ HAL_Delay(240); /* Refresh IWDG: reload counter */ if(HAL_IWDG_Refresh(&IwdgHandle) != HAL_OK) { /* Refresh Error */ Error_Handler(); } } }
/** * @brief Main program. * @param None * @retval None */ int main(void) { GPIO_InitTypeDef GPIO_InitStruct; /* STM32F3xx HAL library initialization: - Configure the Flash prefetch - Configure the Systick to generate an interrupt each 1 msec - Set NVIC Group Priority to 4 - Low Level Initialization */ HAL_Init(); /* Configure the system clock to 64 MHz */ SystemClock_Config(); /* Configure LED3 */ BSP_LED_Init(LED3); /*##-1- Check if the system has resumed from IWDG reset ####################*/ if (__HAL_RCC_GET_FLAG(RCC_FLAG_IWDGRST) != RESET) { /* IWDGRST flag set: Turn LED3 on */ BSP_LED_On(LED3); /* Insert 4s delay */ HAL_Delay(4000); /* Clear reset flags */ __HAL_RCC_CLEAR_RESET_FLAGS(); } else { /* IWDGRST flag is not set: Turn LED3 off */ BSP_LED_Off(LED3); } /* Configure PA.12 (Arduino D2) as input with External interrupt */ GPIO_InitStruct.Pin = GPIO_PIN_12; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING; /* Enable GPIOA clock */ __HAL_RCC_GPIOA_CLK_ENABLE(); HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); /* Enable and set PA.12 (Arduino D2) EXTI Interrupt to the lowest priority */ NVIC_SetPriority((IRQn_Type)(EXTI15_10_IRQn), 0x03); HAL_NVIC_EnableIRQ((IRQn_Type)(EXTI15_10_IRQn)); /*##-2- Get the LSI frequency: TIM16 is used to measure the LSI frequency ###*/ uwLsiFreq = GetLSIFrequency(); /*##-3- Configure the IWDG peripheral ######################################*/ /* Set counter reload value to obtain 250ms IWDG TimeOut. IWDG counter clock Frequency = LsiFreq / 32 Counter Reload Value = 250ms / IWDG counter clock period = 0.25s / (32/LsiFreq) = LsiFreq / (32 * 4) = LsiFreq / 128 */ IwdgHandle.Instance = IWDG; IwdgHandle.Init.Prescaler = IWDG_PRESCALER_32; IwdgHandle.Init.Reload = uwLsiFreq / 128; IwdgHandle.Init.Window = IWDG_WINDOW_DISABLE; if (HAL_IWDG_Init(&IwdgHandle) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /*##-4- Start the IWDG #####################################################*/ if (HAL_IWDG_Start(&IwdgHandle) != HAL_OK) { Error_Handler(); } /* Infinite loop */ while (1) { /* Toggle LED3 */ BSP_LED_Toggle(LED3); /* Insert 240 ms delay */ HAL_Delay(240); /* Refresh IWDG: reload counter */ if (HAL_IWDG_Refresh(&IwdgHandle) != HAL_OK) { /* Refresh Error */ Error_Handler(); } } }
/** * @brief Configures the RTC clock source. * @param None * @retval None */ static void RTC_Config(void) { RTC_InitTypeDef RTC_InitStructure; RTC_TimeTypeDef RTC_TimeStructure; uint32_t LSIFreq = 0; /* Enable the PWR clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); /* Allow access to Backup Domain */ PWR_BackupAccessCmd(ENABLE); /* Clear Wakeup flag */ PWR_ClearFlag(PWR_FLAG_WU); /* Enable the LSI OSC */ RCC_LSICmd(ENABLE); /* Wait till LSI is ready */ while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET) {} /* Check if the StandBy flag is set */ if (PWR_GetFlagStatus(PWR_FLAG_SB) != RESET) { /* Clear StandBy flag */ PWR_ClearFlag(PWR_FLAG_SB); /* Check if the StandBy flag is cleared */ if (PWR_GetFlagStatus(PWR_FLAG_SB) != RESET) { while(1); } RTC_WaitForSynchro(); /* No need to configure the RTC as the RTC config(clock source, enable, prescaler,...) are kept after wake-up from STANDBY */ } else { /* RTC Configuration ******************************************************/ /* Reset Backup Domain */ RCC_BackupResetCmd(ENABLE); RCC_BackupResetCmd(DISABLE); /* Select the RTC Clock Source */ RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI); /* Enable the RTC Clock */ RCC_RTCCLKCmd(ENABLE); /* Wait for RTC APB registers synchronisation */ RTC_WaitForSynchro(); /* Get the LSI frequency: TIM14 is used to measure the LSI frequency */ LSIFreq = GetLSIFrequency(); RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24; RTC_InitStructure.RTC_AsynchPrediv = 99; RTC_InitStructure.RTC_SynchPrediv = (LSIFreq/100) - 1; RTC_Init(&RTC_InitStructure); /* Set the time to 01h 00mn 00s AM */ RTC_TimeStructure.RTC_H12 = RTC_H12_AM; RTC_TimeStructure.RTC_Hours = 0x01; RTC_TimeStructure.RTC_Minutes = 0x00; RTC_TimeStructure.RTC_Seconds = 0x00; RTC_SetTime(RTC_Format_BIN, &RTC_TimeStructure); } }
/** * @brief Main program * @param None * @retval None */ int main(void) { /* STM32F3xx HAL library initialization: - Configure the Flash prefetch - Systick timer is configured by default as source of time base, but user can eventually implement his proper time base source (a general purpose timer for example or other time source), keeping in mind that Time base duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and handled in milliseconds basis. - Set NVIC Group Priority to 4 - Low Level Initialization */ HAL_Init(); /******************************************************************************* * Common Configuration Routines * *******************************************************************************/ /* Configure the system clock to 72 Mhz */ SystemClock_Config(); /* Configure LED1, LED2, LED3 and Key Button mounted on STM32303C-EVAL board */ BSP_LED_Init(LED1); BSP_LED_Init(LED2); BSP_LED_Init(LED3); BSP_PB_Init(BUTTON_KEY, BUTTON_MODE_EXTI); SystemCoreClockUpdate(); /*##-1- Setup SysTick Timer for 1 msec interrupts ##########################*/ if (HAL_SYSTICK_Config(SystemCoreClock / 1000)) { /* Capture Error */ Error_Handler(); } /*##-2- Check if the system has resumed from IWDG reset ####################*/ if (__HAL_RCC_GET_FLAG(RCC_FLAG_IWDGRST) != RESET) { /* IWDGRST flag set: Turn LED1 on */ BSP_LED_On(LED1); /* Clear reset flags */ __HAL_RCC_CLEAR_RESET_FLAGS(); } else { /* IWDGRST flag is not set: Turn LED1 off */ BSP_LED_Off(LED1); } /*##-3- Get the LSI frequency */ #ifdef LSI_TIM_MEASURE /* using TIM14 to measure the LSI frequency ### */ uwLsiFreq = GetLSIFrequency(); #else /* or not */ uwLsiFreq = 42000; #endif /*##-4- Configure & Initialize the IWDG peripheral ######################################*/ /* Set counter reload value to obtain 250ms IWDG TimeOut. IWDG counter clock Frequency = LsiFreq/32 Counter Reload Value = 250ms/IWDG counter clock period = 0.25s / (32/LsiFreq) = LsiFreq/(32 * 4) = LsiFreq/128 */ IwdgHandle.Instance = IWDG; IwdgHandle.Init.Prescaler = IWDG_PRESCALER_32; IwdgHandle.Init.Reload = uwLsiFreq/128; IwdgHandle.Init.Window = IWDG_WINDOW_DISABLE; if(HAL_IWDG_Init(&IwdgHandle) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /*##-5- Start the IWDG #####################################################*/ if(HAL_IWDG_Start(&IwdgHandle) != HAL_OK) { Error_Handler(); } /* Infinite loop */ while (1) { /* Toggle LED2 */ BSP_LED_Toggle(LED2); /* Insert 240 ms delay */ HAL_Delay(240); /* turn on Reset LED1 */ BSP_LED_Off(LED1); /* Refresh IWDG: reload counter */ if(HAL_IWDG_Refresh(&IwdgHandle) != HAL_OK) { /* Refresh Error */ Error_Handler(); } } }