/** * @brief This function configures the system to enter Standby mode with RTC * clocked by LSE or LSI for current consumption measurement purpose. * STANDBY Mode with RTC clocked by LSE/LSI * ======================================== * - RTC Clocked by LSE/LSI * - IWDG OFF * - Backup SRAM OFF * - Automatic Wakeup using RTC clocked by LSE/LSI (after ~20s) * @param None * @retval None */ void StandbyRTCMode_Measure(void) { RTCHandle.Instance = RTC; /* Configure RTC prescaler and RTC data registers 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.Init.HourFormat = RTC_HOURFORMAT_24; RTCHandle.Init.AsynchPrediv = RTC_ASYNCH_PREDIV; RTCHandle.Init.SynchPrediv = RTC_SYNCH_PREDIV; 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(); } /*## Configure the Wake up timer ###########################################*/ /* RTC Wakeup Interrupt Generation: Wakeup Time Base = (RTC_WAKEUPCLOCK_RTCCLK_DIV /(LSI)) Wakeup Time = Wakeup Time Base * WakeUpCounter = (RTC_WAKEUPCLOCK_RTCCLK_DIV /(LSI)) * WakeUpCounter ==> WakeUpCounter = Wakeup Time / Wakeup Time Base To configure the wake up timer to 20s the WakeUpCounter is set to 0xA017: RTC_WAKEUPCLOCK_RTCCLK_DIV = RTCCLK_Div16 = 16 Wakeup Time Base = 16 /(~32.768KHz) = ~0,488 ms Wakeup Time = ~20s = 0,488ms * WakeUpCounter ==> WakeUpCounter = ~20s/0,488ms = 40983 = 0xA017 */ /* Disable Wake-up timer */ if(HAL_RTCEx_DeactivateWakeUpTimer(&RTCHandle) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /*## Disable all used wakeup sources: Wake up Timer ########################*/ HAL_RTCEx_DeactivateWakeUpTimer(&RTCHandle); /*## Clear all related wakeup flags ########################################*/ /* Clear PWR wake up Flag */ __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU); /* Clear RTC Wake Up timer Flag */ __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(&RTCHandle, RTC_FLAG_WUTF); /*## Setting the Wake up time ##############################################*/ HAL_RTCEx_SetWakeUpTimer_IT(&RTCHandle, 0xA017, RTC_WAKEUPCLOCK_RTCCLK_DIV16); /*## Enter the Standby mode ################################################*/ /* Request to enter STANDBY mode */ HAL_PWR_EnterSTANDBYMode(); }
static void RtcStartWakeUpAlarm( uint32_t timeoutValue ) { RtcCalendar_t now; RtcCalendar_t alarmTimer; RTC_AlarmTypeDef alarmStructure; HAL_RTC_DeactivateAlarm( &RtcHandle, RTC_ALARM_A ); HAL_RTCEx_DeactivateWakeUpTimer( &RtcHandle ); // Load the RTC calendar now = RtcGetCalendar( ); // Save the calendar into RtcCalendarContext to be able to calculate the elapsed time RtcCalendarContext = now; // timeoutValue is in ms alarmTimer = RtcComputeTimerTimeToAlarmTick( timeoutValue, now ); alarmStructure.Alarm = RTC_ALARM_A; alarmStructure.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_DATE; alarmStructure.AlarmMask = RTC_ALARMMASK_NONE; alarmStructure.AlarmTime.TimeFormat = RTC_HOURFORMAT12_AM; alarmStructure.AlarmTime.Seconds = alarmTimer.CalendarTime.Seconds; alarmStructure.AlarmTime.Minutes = alarmTimer.CalendarTime.Minutes; alarmStructure.AlarmTime.Hours = alarmTimer.CalendarTime.Hours; alarmStructure.AlarmDateWeekDay = alarmTimer.CalendarDate.Date; if( HAL_RTC_SetAlarm_IT( &RtcHandle, &alarmStructure, RTC_FORMAT_BIN ) != HAL_OK ) { assert_param( FAIL ); } }
/*config wake up timer*/ void rtc_wake_up_timer_config(uint32_t time) { /*## Configure the Wake up timer ###########################################*/ /* RTC Wakeup Interrupt Generation: Wakeup Time Base = (RTC_WAKEUPCLOCK_RTCCLK_DIV /(LSI)) Wakeup Time = Wakeup Time Base * WakeUpCounter = (RTC_WAKEUPCLOCK_RTCCLK_DIV /(LSI)) * WakeUpCounter ==> WakeUpCounter = Wakeup Time / Wakeup Time Base To configure the wake up timer to 20s the WakeUpCounter is set to 0xA017: RTC_WAKEUPCLOCK_RTCCLK_DIV = RTCCLK_Div16 = 16 Wakeup Time Base = 16 /(~32.768KHz) = ~0,488 ms Wakeup Time = ~20s = 0,488ms * WakeUpCounter ==> WakeUpCounter = ~20s/0,488ms = 40983 = 0xA017 */ /* Disable Wake-up timer */ time = (time*1000)/488; HAL_RTCEx_DeactivateWakeUpTimer(&RTCHandle); /*## Clear all related wakeup flags ########################################*/ /* Clear PWR wake up Flag */ __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU); /* Clear RTC Wake Up timer Flag */ __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(&RTCHandle, RTC_FLAG_WUTF); /*## Setting the Wake up time ##############################################*/ HAL_RTCEx_SetWakeUpTimer_IT(&RTCHandle, time-1, RTC_WAKEUPCLOCK_RTCCLK_DIV16); }
//----------------------------------------------------------- void rtc_init() { HAL_RTCEx_DeactivateWakeUpTimer(&hrtc); HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR2, 0xaaaa); osMutexDef(rtc_mutex); rtc_mutex = osMutexCreate(osMutex(rtc_mutex)); }
//------------------------------------------------------------ //wake up control void rtc_wakeup(uint8_t flag) { if(flag==0) HAL_RTCEx_DeactivateWakeUpTimer(&hrtc); else HAL_RTCEx_SetWakeUpTimer_IT(&hrtc, 10240, RTC_WAKEUPCLOCK_RTCCLK_DIV16); }
/** * @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(); /* Configure LED2 to handle error handler */ BSP_LED_Init(LED2); /* Configure the system clock @ 32 Mhz */ SystemClock_Config(); /* Configure the system Power */ SystemPower_Config(); while (1) { /* Insert 5 seconds delay */ HAL_Delay(5000); /* Disable Wakeup Counter */ HAL_RTCEx_DeactivateWakeUpTimer(&RTCHandle); /*## Setting the Wake up time ############################################*/ /* RTC Wakeup Interrupt Generation: Wakeup Time Base = (RTC_WAKEUPCLOCK_RTCCLK_DIV /(LSE or LSI)) Wakeup Time = Wakeup Time Base * WakeUpCounter = (RTC_WAKEUPCLOCK_RTCCLK_DIV /(LSE or LSI)) * WakeUpCounter ==> WakeUpCounter = Wakeup Time / Wakeup Time Base To configure the wake up timer to 4s the WakeUpCounter is set to 0x1FFF: RTC_WAKEUPCLOCK_RTCCLK_DIV = RTCCLK_Div16 = 16 Wakeup Time Base = 16 /(~39.000KHz) = ~0,410 ms Wakeup Time = ~4s = 0,410ms * WakeUpCounter ==> WakeUpCounter = ~4s/0,410ms = 9750 = 0x2616 */ HAL_RTCEx_SetWakeUpTimer_IT(&RTCHandle, 0x2616, RTC_WAKEUPCLOCK_RTCCLK_DIV16); /* Enter Stop Mode */ HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI); /* Configures system clock after wake-up from STOP: enable HSI, PLL and select PLL as system clock source (HSI and PLL are disabled automatically in STOP mode) */ SystemClockConfig_STOP(); } }
/** * @brief This function configures the system to enter Stop mode with RTC * clocked by LSE or LSI for current consumption measurement purpose. * STOP Mode with RTC clocked by LSE/LSI * ===================================== * - RTC Clocked by LSE or LSI * - Regulator in LP mode * - HSI, HSE OFF and LSI OFF if not used as RTC Clock source * - No IWDG * - FLASH in deep power down mode * - Automatic Wakeup using RTC clocked by LSE/LSI (~20s) * @param None * @retval None */ void StopMode_Measure(void) { #ifdef STOP_IO GPIO_InitTypeDef GPIO_InitStruct; /* Configure all GPIO as analog to reduce current consumption on non used IOs */ /* Enable GPIOs clock */ __HAL_RCC_GPIOA_CLK_ENABLE(); __HAL_RCC_GPIOB_CLK_ENABLE(); __HAL_RCC_GPIOC_CLK_ENABLE(); __HAL_RCC_GPIOH_CLK_ENABLE(); GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Pin = GPIO_PIN_All; HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); HAL_GPIO_Init(GPIOH, &GPIO_InitStruct); HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); /* Disable GPIOs clock */ __HAL_RCC_GPIOA_CLK_DISABLE(); __HAL_RCC_GPIOB_CLK_DISABLE(); __HAL_RCC_GPIOC_CLK_DISABLE(); __HAL_RCC_GPIOH_CLK_DISABLE(); #endif /* FLASH Deep Power Down Mode enabled */ HAL_PWREx_EnableFlashPowerDown(); /*## Enter Stop Mode #######################################################*/ HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFE); /* Configures system clock after wake-up from STOP: enable HSE, PLL and select PLL as system clock source (HSE and PLL are disabled in STOP mode) */ SYSCLKConfig_STOP(); /* Disable Wake-up timer */ if(HAL_RTCEx_DeactivateWakeUpTimer(&RTCHandle) != HAL_OK) { /* Initialization Error */ Error_Handler(); } }
/** * @brief This function configures the system to enter Stop mode with RTC * clocked by LSE or LSI for current consumption measurement purpose. * STOP Mode with RTC clocked by LSE/LSI * ===================================== * - RTC Clocked by LSE or LSI * - Regulator in LP mode * - HSI, HSE OFF and LSI OFF if not used as RTC Clock source * - No IWDG * - FLASH in deep power down mode * - Automatic Wakeup using RTC clocked by LSE/LSI (~20s) * @param None * @retval None */ void StopMode_Measure(void) { GPIO_InitTypeDef GPIO_InitStruct; /* Configure all GPIO as analog to reduce current consumption on non used IOs */ /* Enable GPIOs clock */ __HAL_RCC_GPIOA_CLK_ENABLE(); __HAL_RCC_GPIOB_CLK_ENABLE(); __HAL_RCC_GPIOC_CLK_ENABLE(); __HAL_RCC_GPIOD_CLK_ENABLE(); __HAL_RCC_GPIOE_CLK_ENABLE(); __HAL_RCC_GPIOF_CLK_ENABLE(); __HAL_RCC_GPIOG_CLK_ENABLE(); __HAL_RCC_GPIOH_CLK_ENABLE(); __HAL_RCC_GPIOI_CLK_ENABLE(); GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Pin = GPIO_PIN_All; HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); HAL_GPIO_Init(GPIOD, &GPIO_InitStruct); HAL_GPIO_Init(GPIOE, &GPIO_InitStruct); HAL_GPIO_Init(GPIOF, &GPIO_InitStruct); HAL_GPIO_Init(GPIOG, &GPIO_InitStruct); HAL_GPIO_Init(GPIOH, &GPIO_InitStruct); HAL_GPIO_Init(GPIOI, &GPIO_InitStruct); HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); /* Disable GPIOs clock */ __HAL_RCC_GPIOA_CLK_DISABLE(); __HAL_RCC_GPIOB_CLK_DISABLE(); __HAL_RCC_GPIOC_CLK_DISABLE(); __HAL_RCC_GPIOD_CLK_DISABLE(); __HAL_RCC_GPIOE_CLK_DISABLE(); __HAL_RCC_GPIOF_CLK_DISABLE(); __HAL_RCC_GPIOG_CLK_DISABLE(); __HAL_RCC_GPIOH_CLK_DISABLE(); __HAL_RCC_GPIOI_CLK_DISABLE(); RTCHandle.Instance = RTC; /* Configure RTC prescaler and RTC data registers 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.Init.HourFormat = RTC_HOURFORMAT_24; RTCHandle.Init.AsynchPrediv = RTC_ASYNCH_PREDIV; RTCHandle.Init.SynchPrediv = RTC_SYNCH_PREDIV; 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(); } /*## Configure the Wake up timer ###########################################*/ /* RTC Wakeup Interrupt Generation: Wakeup Time Base = (RTC_WAKEUPCLOCK_RTCCLK_DIV /(LSI)) Wakeup Time = Wakeup Time Base * WakeUpCounter = (RTC_WAKEUPCLOCK_RTCCLK_DIV /(LSI)) * WakeUpCounter ==> WakeUpCounter = Wakeup Time / Wakeup Time Base To configure the wake up timer to 20s the WakeUpCounter is set to 0xA017: RTC_WAKEUPCLOCK_RTCCLK_DIV = RTCCLK_Div16 = 16 Wakeup Time Base = 16 /(~32.768KHz) = ~0,488 ms Wakeup Time = ~20s = 0,488ms * WakeUpCounter ==> WakeUpCounter = ~20s/0,488ms = 40983 = 0xA017 */ HAL_RTCEx_SetWakeUpTimer_IT(&RTCHandle, 0xA017, RTC_WAKEUPCLOCK_RTCCLK_DIV16); /* FLASH Deep Power Down Mode enabled */ HAL_PWREx_EnableFlashPowerDown(); /* Enter Stop Mode */ HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI); /* Configures system clock after wake-up from STOP: enable HSE, PLL and select PLL as system clock source (HSE and PLL are disabled in STOP mode) */ SYSCLKConfig_STOP(); /* Disable Wake-up timer */ if(HAL_RTCEx_DeactivateWakeUpTimer(&RTCHandle) != HAL_OK) { /* Initialization 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 LED1, LED2, LED3 and LED4 */ BSP_LED_Init(LED1); BSP_LED_Init(LED2); BSP_LED_Init(LED3); BSP_LED_Init(LED4); /* Configure the system clock to 168 MHz */ SystemClock_Config(); /* Configure Key Button (EXTI_Line15) will be used to wakeup the system from STOP mode */ BSP_PB_Init(BUTTON_KEY, BUTTON_MODE_EXTI); /*## Configure the RTC peripheral #######################################*/ RTCHandle.Instance = RTC; /* Configure RTC prescaler and RTC data registers 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.Init.HourFormat = RTC_HOURFORMAT_24; RTCHandle.Init.AsynchPrediv = RTC_ASYNCH_PREDIV; RTCHandle.Init.SynchPrediv = RTC_SYNCH_PREDIV; 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) { /* Insert 5 second delay */ HAL_Delay(5000); /*## Configure the Wake up timer ###########################################*/ /* RTC Wakeup Interrupt Generation: Wakeup Time Base = (RTC_WAKEUPCLOCK_RTCCLK_DIV /(LSE or LSI)) Wakeup Time = Wakeup Time Base * WakeUpCounter = (RTC_WAKEUPCLOCK_RTCCLK_DIV /(LSE or LSI)) * WakeUpCounter ==> WakeUpCounter = Wakeup Time / Wakeup Time Base To configure the wake up timer to 4s the WakeUpCounter is set to 0x1FFF: RTC_WAKEUPCLOCK_RTCCLK_DIV = RTCCLK_Div16 = 16 Wakeup Time Base = 16 /(~32.768KHz) = ~0,488 ms Wakeup Time = ~4s = 0,488ms * WakeUpCounter ==> WakeUpCounter = ~4s/0,488ms = 8191 = 0x1FFF */ HAL_RTCEx_SetWakeUpTimer_IT(&RTCHandle, 0x1FFF, RTC_WAKEUPCLOCK_RTCCLK_DIV16); /* Turn OFF LED's */ BSP_LED_Off(LED1); BSP_LED_Off(LED2); BSP_LED_Off(LED3); BSP_LED_Off(LED4); /* Enter Stop Mode */ HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI); /* Disable Wakeup Counter */ HAL_RTCEx_DeactivateWakeUpTimer(&RTCHandle); /* Configures system clock after wake-up from STOP: enable HSE, PLL and select PLL as system clock source (HSE and PLL are disabled in STOP mode) */ SYSCLKConfig_STOP(); } }
/** * @brief This function configures the system to enter Stop mode with RTC * clocked by LSE or LSI for current consumption measurement purpose. * STOP Mode with RTC clocked by LSE/LSI * ===================================== * - RTC Clocked by LSE or LSI * - Regulator in LP mode * - HSI, HSE OFF and LSI OFF if not used as RTC Clock source * - No IWDG * - Automatic Wakeup using RTC clocked by LSE/LSI (~20s) * @param None * @retval None */ void StopRTCMode_Measure(void) { GPIO_InitTypeDef GPIO_InitStruct; /* Configure all GPIO as analog to reduce current consumption on non used IOs */ /* Enable GPIOs clock */ /* Warning : Reconfiguring all GPIO will close the connexion with the debugger */ __HAL_RCC_GPIOA_CLK_ENABLE(); __HAL_RCC_GPIOB_CLK_ENABLE(); __HAL_RCC_GPIOC_CLK_ENABLE(); __HAL_RCC_GPIOD_CLK_ENABLE(); __HAL_RCC_GPIOF_CLK_ENABLE(); GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Pin = GPIO_PIN_All; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); HAL_GPIO_Init(GPIOD, &GPIO_InitStruct); HAL_GPIO_Init(GPIOF, &GPIO_InitStruct); /* Disable GPIOs clock */ __HAL_RCC_GPIOA_CLK_DISABLE(); __HAL_RCC_GPIOB_CLK_DISABLE(); __HAL_RCC_GPIOC_CLK_DISABLE(); __HAL_RCC_GPIOD_CLK_DISABLE(); __HAL_RCC_GPIOF_CLK_DISABLE(); RTCHandle.Instance = RTC; /* Configure RTC prescaler and RTC data registers as follows: - 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.Init.HourFormat = RTC_HOURFORMAT_24; RTCHandle.Init.AsynchPrediv = RTC_ASYNCH_PREDIV; RTCHandle.Init.SynchPrediv = RTC_SYNCH_PREDIV; 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(); } /*## Configure the Wake up timer ###########################################*/ /* RTC Wakeup Interrupt Generation: Wakeup Time Base = (RTC_WAKEUPCLOCK_RTCCLK_DIV /(LSE or LSI)) Wakeup Time = Wakeup Time Base * WakeUpCounter = (RTC_WAKEUPCLOCK_RTCCLK_DIV /(LSE or LSI)) * WakeUpCounter ==> WakeUpCounter = Wakeup Time / Wakeup Time Base To configure the wake up timer to 20s the WakeUpCounter is set to 0xA017: RTC_WAKEUPCLOCK_RTCCLK_DIV = RTCCLK_Div16 = 16 Wakeup Time Base = 16 /(~32.768KHz) = ~0,488 ms Wakeup Time = ~20s = 0,488ms * WakeUpCounter ==> WakeUpCounter = ~20s/0,488ms = 40983 = 0xA017 */ /* Disable Wake-up timer */ HAL_RTCEx_DeactivateWakeUpTimer(&RTCHandle); HAL_RTCEx_SetWakeUpTimer_IT(&RTCHandle, 0xA017, RTC_WAKEUPCLOCK_RTCCLK_DIV16); /* Configure User push-button as external interrupt generator */ BSP_PB_Init(BUTTON_USER, BUTTON_MODE_EXTI); /* Enter Stop Mode */ HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI); /* Configures system clock after wake-up from STOP: enable HSI and PLL with HSI as source*/ SystemClock_Config(); /* Disable Wake-up timer */ HAL_RTCEx_DeactivateWakeUpTimer(&RTCHandle); }
int main(void) { /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ /* MCU Configuration----------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* USER CODE BEGIN Init */ /* USER CODE END Init */ /* Configure the system clock */ SystemClock_Config(); /* USER CODE BEGIN SysInit */ /* USER CODE END SysInit */ /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_ADC_Init(); MX_I2C1_Init(); MX_RTC_Init(); MX_USART1_UART_Init(); MX_SPI2_Init(); /* Initialize interrupts */ MX_NVIC_Init(); /* USER CODE BEGIN 2 */ HAL_ADCEx_Calibration_Start(&hadc, ADC_SINGLE_ENDED); /* Enable Ultra low power mode */ HAL_PWREx_EnableUltraLowPower(); __HAL_RCC_WAKEUPSTOP_CLK_CONFIG(RCC_STOP_WAKEUPCLOCK_HSI); /* Buffer used for transmission on USART1 */ char tx1_buffer[120]; RFM95_init(&hspi2); uint8_t payload_buff[14]; PAYLOAD_Garden payload_garden; payload_garden.MessageType = 50; payload_garden.MessageId = 0; // Start in sensing mode. state = MAIN_STATE_SENSE; /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ /* Do some work */ if (state == MAIN_STATE_SENSE) { HAL_ADC_Start(&hadc); // junk readings payload_garden.Temperature = TEMPERATURE_external(); payload_garden.CpuTemperature = TEMPERATURE_cpu(); HAL_GPIO_WritePin(GPIOA, LED_Pin, GPIO_PIN_SET); payload_garden.MessageId++; payload_garden.VCC = BATTERY_vcc(); payload_garden.ChargeMv = BATTERY_ChargeMv(); payload_garden.ChargeMa = BATTERY_ChargeMa(); /* Get the light reading while the adc gets ready */ payload_garden.Light = LIGHT_lux(); payload_garden.Temperature = TEMPERATURE_external(); payload_garden.CpuTemperature = TEMPERATURE_cpu(); HAL_ADC_Stop(&hadc); sprintf(tx1_buffer, "id:%d, vcc:%d, mv:%d, ma:%d, C:%d, cpuC:%d, lux:%d\n", payload_garden.MessageId, payload_garden.VCC, payload_garden.ChargeMv, payload_garden.ChargeMa, payload_garden.Temperature, payload_garden.CpuTemperature, payload_garden.Light); HAL_UART_Transmit(&huart1, (uint8_t*) tx1_buffer, strlen(tx1_buffer), 1000); PAYLOAD_Garden_serialize(payload_garden, payload_buff); RFM95_send(&hspi2, payload_buff, 14); state = MAIN_STATE_TX; } /* Do nothing while the transmission is in progress */ else if (state == MAIN_STATE_TX) { if (dio0_action == 1) { RFM95_setMode(&hspi2, RFM95_MODE_SLEEP); state = MAIN_STATE_SLEEP; } //TMP while interrupts are investigated //HAL_Delay(30); //state = MAIN_STATE_SLEEP; } /* Now that all the work is done, sleep until its time to do it all again */ else if (state == MAIN_STATE_SLEEP) { HAL_GPIO_WritePin(GPIOA, LED_Pin, GPIO_PIN_RESET); //TMP while RFM is diabled //HAL_Delay(1000); /* Turn off the pin interrupts */ HAL_NVIC_DisableIRQ(EXTI4_15_IRQn); HAL_SuspendTick(); /* Enter Stop Mode */ HAL_RTCEx_SetWakeUpTimer_IT(&hrtc, 60, RTC_WAKEUPCLOCK_CK_SPRE_16BITS); HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI); HAL_RTCEx_DeactivateWakeUpTimer(&hrtc); HAL_ResumeTick(); /* Turn on the pin interrupts */ HAL_NVIC_EnableIRQ(EXTI4_15_IRQn); state = MAIN_STATE_SENSE; } } /* USER CODE END 3 */ }
/** * @brief Main program * @param None * @retval None */ int main(void) { /* Enable the CPU Cache */ CPU_CACHE_Enable(); /* STM32F7xx HAL library initialization: - Configure the Flash ART accelerator on ITCM interface - 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 216 MHz */ SystemClock_Config(); /* Since MFX is used, LED init is done after clock config */ /* Configure LED1, LED2, LED3 and LED4 */ BSP_LED_Init(LED1); BSP_LED_Init(LED2); BSP_LED_Init(LED3); BSP_LED_Init(LED4); /* Tamper push-button (EXTI15_10) will be used to wakeup the system from STOP mode */ BSP_PB_Init(BUTTON_TAMPER, BUTTON_MODE_EXTI); /*## Configure the RTC peripheral #######################################*/ /* Enable Power Clock */ __HAL_RCC_PWR_CLK_ENABLE(); /* Allow Access to RTC Backup domaine */ HAL_PWR_EnableBkUpAccess(); RTCHandle.Instance = RTC; /* Configure RTC prescaler and RTC data registers as follows: - 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.Init.HourFormat = RTC_HOURFORMAT_24; RTCHandle.Init.AsynchPrediv = RTC_ASYNCH_PREDIV; RTCHandle.Init.SynchPrediv = RTC_SYNCH_PREDIV; 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(); } while (1) { /* Turn On LED4 */ BSP_LED_On(LED4); /* Insert 5 second delay */ HAL_Delay(5000); /*## Configure the Wake up timer ###########################################*/ /* RTC Wakeup Interrupt Generation: Wakeup Time Base = (RTC_WAKEUPCLOCK_RTCCLK_DIV /(LSI)) Wakeup Time = Wakeup Time Base * WakeUpCounter = (RTC_WAKEUPCLOCK_RTCCLK_DIV /(LSI)) * WakeUpCounter ==> WakeUpCounter = Wakeup Time / Wakeup Time Base To configure the wake up timer to 4s the WakeUpCounter is set to 0x242B: RTC_WAKEUPCLOCK_RTCCLK_DIV = RTCCLK_Div16 = 16 Wakeup Time Base = 16 /(~37KHz) = ~0,432 ms Wakeup Time = ~4s = 0,432ms * WakeUpCounter ==> WakeUpCounter = ~4s/0,432ms = 9259 = 0x242B */ HAL_RTCEx_SetWakeUpTimer_IT(&RTCHandle, 0x242B, RTC_WAKEUPCLOCK_RTCCLK_DIV16); /* Turn OFF LED's */ BSP_LED_Off(LED1); BSP_LED_Off(LED2); BSP_LED_Off(LED4); #ifdef UNDERDRIVE_MODE __HAL_PWR_UNDERDRIVE_ENABLE(); #endif /* UNDERDRIVE_MODE */ /* Enter Stop Mode */ HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI); /* Disable Wakeup Counter */ HAL_RTCEx_DeactivateWakeUpTimer(&RTCHandle); /* Configures system clock after wake-up from STOP: enable HSE, PLL and select PLL as system clock source (HSE and PLL are disabled in STOP mode) */ SYSCLKConfig_STOP(); } }
void rtc_deactivate_wake_up_timer(void) { HAL_RTCEx_DeactivateWakeUpTimer(&RtcHandle); }
int main(void) { /* USER CODE BEGIN 1 */ // AxesRaw_t data; FRESULT res; /* FatFs function common result code */ uint32_t byteswritten, bytesread; /* File write/read counts */ char rtext[256]; /* File read buffer */ // HALL_SENS_PWR_ON; // CLAMP_SENS_PWR_ON; // 1. SPI1 is for ACC /* 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(); MX_ADC_Init(); MX_SDIO_SD_Init(); MX_SPI1_Init(); MX_SPI2_Init(); MX_USART1_UART_Init(); MX_USART2_UART_Init(); MX_FATFS_Init(); MX_RTC_Init(); /* USER CODE BEGIN 2 */ HAL_RTCEx_DeactivateWakeUpTimer(&hrtc); HAL_RTCEx_SetWakeUpTimer_IT(&hrtc, 1, RTC_WAKEUPCLOCK_CK_SPRE_16BITS); EXTI0_IRQHandler_Config(); // EXTI13_IRQHandler_Config(); // HAL_DBGMCU_EnableDBGStopMode(); //HALL_SENS_PWR_ON; //CLAMP_SENS_PWR_OFF; // init gpio before! UC_2_8V; printf("FW start\r\n"); LIS3DH_PreInit(); LIS3DH_SetMode(LIS3DH_NORMAL); //reg1 LIS3DH_SetODR(LIS3DH_ODR_400Hz); //reg1 LIS3DH_SetAxis(LIS3DH_X_ENABLE | LIS3DH_Y_ENABLE | LIS3DH_Z_ENABLE ); //reg1 //Direct IRQ from watemark and overrun to 1st pin. reg3 LIS3DH_SetInt1Pin(LIS3DH_CLICK_ON_PIN_INT1_DISABLE | LIS3DH_I1_INT1_ON_PIN_INT1_DISABLE | LIS3DH_I1_INT2_ON_PIN_INT1_DISABLE | LIS3DH_I1_DRDY1_ON_INT1_DISABLE | LIS3DH_I1_DRDY2_ON_INT1_DISABLE | LIS3DH_WTM_ON_INT1_ENABLE | LIS3DH_INT1_OVERRUN_DISABLE); //REG4 LIS3DH_SetBDU(MEMS_ENABLE); LIS3DH_SetFullScale(LIS3DH_FULLSCALE_2); //reg4 //REG5 LIS3DH_FIFOModeEnable(LIS3DH_FIFO_STREAM_MODE); //Enable store into FIFO reg5 LIS3DH_Int1LatchEnable(MEMS_ENABLE); //LIS3DH_FIFO_CTRL_REG LIS3DH_SetTriggerInt(LIS3DH_TRIG_INT1); LIS3DH_SetWaterMark(15); // watermark for irq generation from fifo LIS3DH_FIFO_CTRL_REG LIS3DH_ReadFIFO(); // clean FIFO and reset IRQ while (1) { //LIS3DH_GetInt1Src(&resp); //printf("INT1SRC %i ", resp); /*LIS3DH_GetFifoSourceBit(LIS3DH_FIFO_SRC_WTM, &resp); printf("WTM %i ", resp); LIS3DH_GetReg3Bit(LIS3DH_I1_WTM, &resp); printf("WMBIT %i ", resp); LIS3DH_GetFifoSourceFSS(&resp); printf("FIFO %i\r\n", resp); //LIS3DH_GetIntCounter(&resp); //printf("Interrupts counter=%i\r\n", resp); if (resp > 26) { //rep: //LIS3DH_GetFifoSourceFSS(&resp); //printf("> %i recs in FIFO (while)\r\n", resp); //HAL_Delay(15); //LIS3DH_GetInt1Src(&resp); /* rep: if(LIS3DH_GetAccAxesRaw(&data)==1){ LIS3DH_GetFifoSourceBit(LIS3DH_FIFO_SRC_WTM, &resp); //printf("X=%6d Y=%6d Z=%6d \r\n", data.AXIS_X, data.AXIS_Y, data.AXIS_Z); printf(" READ WTM%i\r\n", resp); } else { printf("ER\r\n"); } LIS3DH_GetFifoSourceFSS(&resp); if (resp > 0) goto rep; LIS3DH_ResetInt1Latch(); LIS3DH_FIFOModeEnable(LIS3DH_FIFO_STREAM_MODE);*/ LIS3DH_ReadFIFO(); HAL_Delay(2000); } //ENABLE ALL IRQs //LIS3DH_SetInt1Pin(LIS3DH_CLICK_ON_PIN_INT1_DISABLE | LIS3DH_I1_INT1_ON_PIN_INT1_ENABLE | LIS3DH_I1_INT2_ON_PIN_INT1_ENABLE | LIS3DH_I1_DRDY1_ON_INT1_ENABLE | LIS3DH_I1_DRDY2_ON_INT1_ENABLE | LIS3DH_WTM_ON_INT1_ENABLE | LIS3DH_INT1_OVERRUN_ENABLE); //LIS3DH_SetInt2Pin(LIS3DH_CLICK_ON_PIN_INT2_DISABLE | LIS3DH_I2_INT1_ON_PIN_INT2_ENABLE | LIS3DH_I2_INT2_ON_PIN_INT2_ENABLE | LIS3DH_I2_BOOT_ON_INT2_ENABLE | LIS3DH_INT_ACTIVE_HIGH); // HAL_Delay(2000); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ // if(LIS3DH_GetAccAxesRaw(&data)==1){ // printf("X=%6d Y=%6d Z=%6d \r\n", data.AXIS_X, data.AXIS_Y, data.AXIS_Z); // } else { // printf("ER\r\n"); // } HAL_Delay(50); // lowest_power(); //go to stop. Wakeup on RTC wakeup or 1st or 2d PIN wakeup } //SDIO FAT PART. //ENABLE_2_5V; //DC-DC enable SD_PWR_ON; //Power to SD card //INIT SD and CARD after because no power to sd HAL_Delay(50); MX_SDIO_SD_Init(); MX_FATFS_Init(); /*##-1- FatFS: Link the SD disk I/O driver ##########*/ if(FATFS_LinkDriver(&SD_Driver, SDPath) == 0){ /* success: set the orange LED on */ //HAL_GPIO_WritePin(GPIOG, GPIO_PIN_7, GPIO_PIN_RESET); /*##-2- Register the file system object to the FatFs module NB! mout right now! ###*/ res = f_mount(&SDFatFs, (TCHAR const*)SD_Path, 1) ; if(res != FR_OK){ /* FatFs Initialization Error : set the red LED on */ printf ("Problem fmount\r\n"); while(1); } else { /*##-3- Create a FAT file system (format) on the logical drive#*/ /* WARNING: Formatting the uSD card will delete all content on the device */ res = f_mkfs((TCHAR const*)SD_Path, 0, 0); if(res != FR_OK){ /* FatFs Format Error : set the red LED on */ printf ("Problem f_mkfs\r\n"); while(1); } else { /*##-4- Create & Open a new text file object with write access#*/ if(f_open(&MyFile, "Hello.txt", FA_CREATE_ALWAYS | FA_WRITE) != FR_OK){ /* 'Hello.txt' file Open for write Error : set the red LED on */ printf ("Problem f_open\r\n"); while(1); } else { /*##-5- Write data to the text file ####################*/ res = f_write(&MyFile, wtext, sizeof(wtext), (void*)&byteswritten); if((byteswritten == 0) || (res != FR_OK)){ /* 'Hello.txt' file Write or EOF Error : set the red LED on */ printf ("Problem f_write\r\n"); while(1); } else { /*##-6- Successful open/write : set the blue LED on */ // HAL_GPIO_WritePin(GPIOG, GPIO_PIN_12, GPIO_PIN_RESET); f_close(&MyFile); /*##-7- Open the text file object with read access #*/ if(f_open(&MyFile, "Hello.txt", FA_READ) != FR_OK){ /* 'Hello.txt' file Open for read Error : set the red LED on */ //HAL_GPIO_WritePin(GPIOG, GPIO_PIN_10, GPIO_PIN_RESET); printf ("Problem f_open\r\n"); while(1); } else { /*##-8- Read data from the text file #########*/ res = f_read(&MyFile, rtext, sizeof(wtext), &bytesread); // if((strcmp(rtext,wtext)!=0)|| (res != FR_OK)){ // /* 'Hello.txt' file Read or EOF Error : set the red LED on */ // printf ("Problem f_read\r\n"); // while(1); // } else { // printf ("FAT operation done OK!\r\n"); /* Successful read : set the green LED On */ //HAL_GPIO_WritePin(GPIOG, GPIO_PIN_6, GPIO_PIN_RESET); /*##-9- Close the open text file ################*/ f_close(&MyFile); } } } } } } /*##-10- Unlink the micro SD disk I/O driver #########*/ FATFS_UnLinkDriver(SD_Path); /* USER CODE END 3 */ }