/** * @brief Changes countdown time the watchdog timer will be energized - new_reload*(1/(37Khz/new_precaler)) = time to watchdog * @param new_precaler: must be a valid prescaler value such as IWDG_PRESCALER_4 - IWDG_PRESCALER_256 * @param new_reload: must be a valid reloas value in the range of 0-4095. * @retval EPS_soft_error_status. */ EPS_soft_error_status IWDG_change_reset_time(IWDG_HandleTypeDef *new_hiwdg, uint32_t new_precaler, uint32_t new_reload) { error_status = EPS_SOFT_ERROR_IWDG_CHANGE; /*Refresh previous seting IWDG*/ HAL_IWDG_Refresh(new_hiwdg); new_hiwdg->Instance = IWDG; new_hiwdg->Init.Prescaler = new_precaler; new_hiwdg->Init.Reload = new_reload;/*1900 *(1/(37Khz/Prescaler)) = time to watchdog =205.409msec*/ HAL_IWDG_Init(new_hiwdg); /*Start Counting*/ HAL_IWDG_Start(new_hiwdg); return EPS_SOFT_ERROR_IWDG_CHANGE_COMPLETE; }
int main(void) { /* USER CODE BEGIN 1 */ trace_printf("Hello\n"); /* USER CODE END 1 */ /* MCU Configuration----------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* Configure the system clock */ SystemClock_Config(); /* Initialize all configured peripherals */ MX_GPIO_Init(); /* USER CODE BEGIN 2 */ BSP_PB_Init(BUTTON_KEY,BUTTON_MODE_EXTI); // initialize on board switch BSP_LED_Init(LED5); // initialize on baord LED 4 and LED 5 BSP_LED_Init(LED4); /* Check if the system has resumed from WWDG reset */ if(__HAL_RCC_GET_FLAG(RCC_FLAG_IWDGRST) != RESET) { /* WWDGRST flag set: Turn LED1 on */ BSP_LED_On(LED5); /* Clear reset flags */ __HAL_RCC_CLEAR_RESET_FLAGS(); } else { /* WWDGRST flag is not set: Turn LED1 off */ BSP_LED_Off(LED5); } /* Initialize and start IWDG */ BSP_IWDG_Init(2000); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ BSP_LED_Toggle(LED4); HAL_Delay(1850); /* Refresh the IWDG reload count value */ HAL_IWDG_Refresh(&hiwdg_bsp); } /* USER CODE END 3 */ }
/* StartDefaultTask function */ void StartDefaultTask(void const * argument) { /* USER CODE BEGIN StartDefaultTask */ /* Infinite loop */ for(;;) { HAL_GPIO_TogglePin(Led_On_Board_GPIO_Port,Led_On_Board_Pin); osDelay(100); HAL_IWDG_Refresh(&hiwdg); } /* USER CODE END StartDefaultTask */ }
PROCESS_THREAD(network_process, ev, data) { PROCESS_BEGIN(); while(1) { PROCESS_PAUSE(); uip_len = enc28j60_read(uip_buf, UIP_BUFSIZE); if (uip_len > 0) { HAL_IWDG_Refresh(&hiwdg); struct uip_eth_hdr *header = ((struct uip_eth_hdr *)&uip_buf[0]); uint16_t packetType = header->type; #ifdef ENC28J60_DEBUG printf("?receivePacket: len: %d, dest: %02x:%02x:%02x:%02x:%02x:%02x, src: %02x:%02x:%02x:%02x:%02x:%02x, type: %d\n", uip_len, header->dest.addr[0], header->dest.addr[1], header->dest.addr[2], header->dest.addr[3], header->dest.addr[4], header->dest.addr[5], header->src.addr[0], header->src.addr[1], header->src.addr[2], header->src.addr[3], header->src.addr[4], header->src.addr[5], packetType ); for (int i = 0; i < uip_len; i++) { printf("%02x ", uip_buf[i]); } printf("\n"); #endif if (packetType == UIP_HTONS(UIP_ETHTYPE_IP)) { #ifdef ENC28J60_DEBUG printf("?readPacket type IP\n"); #endif uip_arp_ipin(); uip_input(); if (uip_len > 0) { uip_arp_out(); enc28j60_send(uip_buf, uip_len + sizeof(struct uip_eth_hdr)); } } else if (packetType == UIP_HTONS(UIP_ETHTYPE_ARP)) { #ifdef ENC28J60_DEBUG printf("?readPacket type ARP\n"); #endif uip_arp_arpin(); if (uip_len > 0) { enc28j60_send(uip_buf, uip_len + sizeof(struct uip_eth_hdr)); } } } } PROCESS_END(); }
/* When enter is pressed, the command is excecuted here */ void executeSerialCommand(uint8_t string[]) { float numArg = 0; uint8_t tempBool = 0; /* Big help screen */ if (compareCommand(string, CMD_HELP, &numArg)) { print("\r\n Format is:"); print("\r\n command:number"); print("\r\n number must be between:"); print("\r\n -1000 and 1000 for (permilli) commands"); print("\r\n 1 or 0 for (boolean) commands\n"); /* Because serial can be time consuming */ #ifdef WATCHDOG_EN HAL_IWDG_Refresh(&hiwdg); #endif print("\r\n defaults - Sets defaults (no args)"); print("\r\n print config - Prints out the current config (no args)"); print("\r\n power bias - Sets front to rear power bias (permilli)"); print("\r\n front slip - Sets front diff slip (permilli)"); /* Because serial can be time consuming */ #ifdef WATCHDOG_EN HAL_IWDG_Refresh(&hiwdg); #endif print("\r\n rear slip - Sets rear diff slip (permilli)"); print("\r\n gyro gain - Sets steering correction gain (permilli)"); print("\r\n steering trim - Trims the steering (permilli)"); /* Because serial can be time consuming */ #ifdef WATCHDOG_EN HAL_IWDG_Refresh(&hiwdg); #endif print("\r\n invert steering - Inverts the steering (boolean)"); print("\r\n wheel feedback - Enables the wheel speed feedback (boolean)\n"); } /* Prints current config */ if (compareCommand(string, CMD_PRINT_CONFIG, &numArg)) { uint8_t intString[10]; print("\r\n Power bias: "); itoa(ControllerConfig.powerBias, &intString, 10); print(&intString); print("\r\n Front slip: "); itoa(ControllerConfig.frontSlip, &intString, 10); print(&intString); print("\r\n Rear slip: "); itoa(ControllerConfig.rearSlip, &intString, 10); print(&intString); print("\r\n Gyro gain: "); itoa(ControllerConfig.gyroGain, &intString, 10); print(&intString); print("\r\n Steering trim: "); itoa(ControllerConfig.steeringTrim, &intString, 10); print(&intString); print("\r\n Invert steering: "); itoa(ControllerConfig.invertSteering, &intString, 10); print(&intString); print("\r\n Wheel speed feedback: "); itoa(ControllerConfig.enableWheelSpeedFeedback, &intString, 10); print(&intString); print("\r\n"); } /* Setting power bias etc */ if (compareCommand(string, CMD_SET_POWERBIAS, &numArg)) { print("\r\n Power bias "); setControllerConfig(powerBias, (int32_t)numArg); } if (compareCommand(string, CMD_SET_FRONTSLIP, &numArg)) { print("\r\n Front slip "); setControllerConfig(frontSlip, (int32_t)numArg); } if (compareCommand(string, CMD_SET_REARSLIP, &numArg)) { setControllerConfig(rearSlip, (int32_t)numArg); print("\r\n Rear slip "); } if (compareCommand(string, CMD_SET_GYROGAIN, &numArg)) { setControllerConfig(gyroGain, (int32_t)numArg); print("\r\n Gyro gain "); } if (compareCommand(string, CMD_SET_STEERINGTRIM, &numArg)) { setControllerConfig(steeringTrim, (int32_t)numArg); print("\r\n Steering trim "); } if (compareCommand(string, CMD_INVERT_STEERING, &numArg)) { setControllerConfig(invertSteering, (int32_t)numArg); print("\r\n Steering invert "); } if (compareCommand(string, CMD_WHEELFEEDBACK, &numArg)) { setControllerConfig(enableWheelSpeedFeedback, (int32_t)numArg); print("\r\n Wheel speed feedback "); } /* Restores default settings */ if (compareCommand(string, CMD_DEFAULTS, &numArg)) { /* Set defaults */ setControllerDefaults(); } /* Reboots CPU */ if (compareCommand(string, CMD_REBOOT, &numArg)) { /* Flag for reset */ ResetPendingFlag = 1; } print("\r\nBrinir> "); }
/** * @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(); } } }
void EPS_soft_error_handling(EPS_soft_error_status eps_error_status){ switch (eps_error_status) { case EPS_SOFT_ERROR_OK: /*Refresh watchdog. */ HAL_IWDG_Refresh(&hiwdg); break; case EPS_SOFT_ERROR_UNRESOLVED: //report and soft reset break; case EPS_SOFT_ERROR_UMBILICAL_UNPREDICTED: //report and soft reset break; case EPS_SOFT_ERROR_BOOTSEQ_POWEROFF_ALL_RAILS: //report and soft reset break; case EPS_SOFT_ERROR_BOOTSEQ_UMBILICAL_CHECK: //report and soft reset break; case EPS_SOFT_ERROR_BOOTSEQ_DEPLOYMENT_STAGE: //report and soft reset break; case EPS_SOFT_ERROR_OBC_COMM_INIT: //report and soft reset break; case EPS_SOFT_ERROR_DEBUG_UART_INIT: //report and soft reset break; case EPS_SOFT_ERROR_DEBUG_UART_FLUSH: //report and soft reset break; case EPS_SOFT_ERROR_LOAD_SAFETY_LIMITS: //report and soft reset break; case EPS_SOFT_ERROR_BOOTSEQ_POWERON_ALL_RAILS: //report and soft reset break; case EPS_SOFT_ERROR_STATE_INIT: //report and soft reset break; case EPS_SOFT_ERROR_STATE_UPDATE: //report and soft reset break; case EPS_SOFT_ERROR_KICK_TIMER: //report and soft reset break; case EPS_SOFT_ERROR_POWER_MODULE_INIT_ALL: //report and soft reset break; case EPS_SOFT_ERROR_POWER_MODULE_INIT_TOP: //report and soft reset break; case EPS_SOFT_ERROR_POWER_MODULE_INIT_BOTTOM: //report and soft reset break; case EPS_SOFT_ERROR_POWER_MODULE_INIT_LEFT: //report and soft reset break; case EPS_SOFT_ERROR_POWER_MODULE_INIT_RIGHT: //report and soft reset break; case EPS_SOFT_ERROR_MPPT_UPDATE_TOP: //report and soft reset break; case EPS_SOFT_ERROR_MPPT_UPDATE_BOTTOM: //report and soft reset break; case EPS_SOFT_ERROR_MPPT_UPDATE_LEFT: //report and soft reset break; case EPS_SOFT_ERROR_MPPT_UPDATE_RIGHT: //report and soft reset break; case EPS_SOFT_ERROR_OBC_COMM_SERVICE: //report and soft reset break; case EPS_SOFT_ERROR_SAFETY_CHECK: //report and soft reset break; case EPS_SOFT_ERROR_SAFETY_CHECK_BATTERY_VOLTAGE_UNPREDICTED: //report and soft reset break; case EPS_SOFT_ERROR_SAFETY_CHECK_BATTERY_TEMPERATURE_UNPREDICTED: //report and soft reset break; case EPS_SOFT_ERROR_BOOTSEQ_DEPLOYMENT_LEFT: //report and soft reset break; case EPS_SOFT_ERROR_BOOTSEQ_DEPLOYMENT_RIGHT: //report and soft reset break; case EPS_SOFT_ERROR_BOOTSEQ_DEPLOYMENT_TOP: //report and soft reset break; case EPS_SOFT_ERROR_BOOTSEQ_DEPLOYMENT_BOTTOM: //report and soft reset break; case EPS_SOFT_ERROR_BOOTSEQ_DEPLOYMENT_ANT1: //report and soft reset break; case EPS_SOFT_ERROR_BOOTSEQ_DEPLOYMENT_KEY_A: //report and soft reset break; case EPS_SOFT_ERROR_BOOTSEQ_DEPLOYMENT_KEY_B: //report and soft reset break; case EPS_SOFT_ERROR_BOOTSEQ_DEPLOYMENT_KEY_C: //report and soft reset break; case EPS_SOFT_ERROR_BOOTSEQ_DEPLOYMENT_KEY_D: //report and soft reset break; case EPS_SOFT_ERROR_BOOTSEQ_DEPLOYMENT_KEY_E: //report and soft reset break; case EPS_SOFT_ERROR_BOOTSEQ_DEPLOYMENT_KEY_F: //report and soft reset break; case EPS_SOFT_ERROR_BOOTSEQ_DEPLOYMENT_KEY_G: //report and soft reset break; case EPS_SOFT_ERROR_UPDATE_STATE_ADC: //report and soft reset break; case EPS_SOFT_ERROR_UPDATE_STATE_ADC_DMA: //report and soft reset break; case EPS_SOFT_ERROR_UPDATE_STATE_ADC_FILTER: //report and soft reset break; case EPS_SOFT_ERROR_UPDATE_STATE_ADC_CPU_TEMP: //report and soft reset break; case EPS_SOFT_ERROR_STATE_UPDATE_BATTERYPACK_TEMP: //report and soft reset break; case EPS_SOFT_ERROR_LAST_VALUE: //report and soft reset break; // default : // //YOU SHOULDNT BE HERE! // //ERROR HANDLING } }
/** * @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 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(); } } }
/** * @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 LED3 and Key Button */ BSP_LED_Init(LED3); BSP_PB_Init(BUTTON_KEY, BUTTON_MODE_EXTI); /* Configure the system clock to 2 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 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); } /*##-2- Configure & Initialize the IWDG peripheral ######################################*/ /* Set counter reload value to obtain 762ms IWDG TimeOut. Counter Reload Value = LsiFreq*Timeout(s)/prescaler = 37000*762ms/(16*1000) = 1762 (approx) */ IwdgHandle.Instance = IWDG; IwdgHandle.Init.Prescaler = IWDG_PRESCALER_16; IwdgHandle.Init.Reload = 1762; /* 762ms */ IwdgHandle.Init.Window = 1000; /* 400 ms */ /* when window option is enabled, we enable the IWDG immediately */ /* by writing 0xCCCC to the KEY register */ /*##-3- Start the IWDG #####################################################*/ if(HAL_IWDG_Start(&IwdgHandle) != HAL_OK) { Error_Handler(); } if(HAL_IWDG_Init(&IwdgHandle) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /* Infinite loop */ while (1) { /* Toggle LED3 */ BSP_LED_Toggle(LED3); /* Insert 450 ms delay, which should bring downcounter inside the window */ HAL_Delay(450); /* 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) { /* 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(); } } }