void SystemPower_Config(void) { /* Enable Ultra low power mode */ HAL_PWREx_EnableUltraLowPower(); /* Enable the fast wake up from Ultra low power mode */ HAL_PWREx_EnableFastWakeUp(); /* Select HSI as system clock source after Wake Up from Stop mode */ __HAL_RCC_WAKEUPSTOP_CLK_CONFIG(RCC_StopWakeUpClock_HSI); /* Configure RTC */ 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 */ } }
/** * @brief System Power Configuration * The system Power is configured as follow : * + System Running at MSI (~32KHz) * + Flash 0 wait state * + Voltage Range 2 * + Code running from Internal FLASH * + Wakeup using EXTI Line (Key Button PC.13) * @param None * @retval None */ static void SystemPower_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; /* Enable Ultra low power mode */ HAL_PWREx_EnableUltraLowPower(); /* Enable the fast wake up from Ultra low power mode */ HAL_PWREx_EnableFastWakeUp(); /* 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_GPIOH_CLK_ENABLE(); /* Configure all GPIO port pins in Analog Input mode (floating input trigger OFF) */ GPIO_InitStructure.Pin = GPIO_PIN_All; GPIO_InitStructure.Mode = GPIO_MODE_ANALOG; GPIO_InitStructure.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOA, &GPIO_InitStructure); HAL_GPIO_Init(GPIOB, &GPIO_InitStructure); HAL_GPIO_Init(GPIOC, &GPIO_InitStructure); HAL_GPIO_Init(GPIOD, &GPIO_InitStructure); HAL_GPIO_Init(GPIOH, &GPIO_InitStructure); /* 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_GPIOH_CLK_DISABLE(); }
/** * @brief System Power Configuration * The system Power is configured as follow : * + Regulator in LP mode * + VREFINT OFF, with fast wakeup enabled * + HSI as SysClk after Wake Up * + No IWDG * + Automatic Wakeup using RTC clocked by LSI (after ~4s) * @param None * @retval None */ static void SystemPower_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; /* Enable Ultra low power mode */ HAL_PWREx_EnableUltraLowPower(); /* Enable the fast wake up from Ultra low power mode */ HAL_PWREx_EnableFastWakeUp(); /* Select HSI as system clock source after Wake Up from Stop mode */ __HAL_RCC_WAKEUPSTOP_CLK_CONFIG(RCC_StopWakeUpClock_HSI); /* Enable GPIOs clock */ __GPIOA_CLK_ENABLE(); __GPIOB_CLK_ENABLE(); __GPIOC_CLK_ENABLE(); __GPIOD_CLK_ENABLE(); __GPIOH_CLK_ENABLE(); /* Configure all GPIO port pins in Analog Input mode (floating input trigger OFF) */ GPIO_InitStructure.Pin = GPIO_PIN_All; GPIO_InitStructure.Mode = GPIO_MODE_ANALOG; GPIO_InitStructure.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOA, &GPIO_InitStructure); HAL_GPIO_Init(GPIOB, &GPIO_InitStructure); HAL_GPIO_Init(GPIOC, &GPIO_InitStructure); HAL_GPIO_Init(GPIOD, &GPIO_InitStructure); HAL_GPIO_Init(GPIOH, &GPIO_InitStructure); /* Disable GPIOs clock */ __GPIOA_CLK_DISABLE(); __GPIOB_CLK_DISABLE(); __GPIOC_CLK_DISABLE(); __GPIOD_CLK_DISABLE(); __GPIOH_CLK_DISABLE(); /* Configure RTC */ 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(); } }
/** * @brief System Power Configuration * The system Power is configured as follow : * + VREFINT OFF, with fast wakeup enabled * + No IWDG * + Wakeup using PWR_WAKEUP_PIN3 * @param None * @retval None */ static void SystemPower_Config(void) { /* Enable Power Control clock */ __HAL_RCC_PWR_CLK_ENABLE(); /* Enable Ultra low power mode */ HAL_PWREx_EnableUltraLowPower(); /* Enable the fast wake up from Ultra low power mode */ HAL_PWREx_EnableFastWakeUp(); }
static void RtcSetConfig( void ) { RTC_TimeTypeDef RTC_TimeStruct; RTC_DateTypeDef RTC_DateStruct; //RTC_AlarmTypeDef salarmstructure; /* Enable Ultra low power mode */ HAL_PWREx_EnableUltraLowPower(); /* Enable the fast wake up from Ultra low power mode */ HAL_PWREx_EnableFastWakeUp(); /* Select HSI as system clock source after Wake Up from Stop mode */ __HAL_RCC_WAKEUPSTOP_CLK_CONFIG(RCC_StopWakeUpClock_HSI); /*##-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 = RTC_SYNCH_PREDIV; RTCHandle.Init.OutPut = RTC_OUTPUT_DISABLE; RTCHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH; RTCHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN; HAL_RTC_Init(&RTCHandle); //Initiate data and time RTC_TimeStruct.TimeFormat = RTC_HOURFORMAT12_AM; RTC_TimeStruct.Hours = 0x00; RTC_TimeStruct.Minutes = 0x00; RTC_TimeStruct.Seconds = 0x00; RTC_TimeStruct.DayLightSaving = RTC_DAYLIGHTSAVING_NONE ; RTC_TimeStruct.StoreOperation = RTC_STOREOPERATION_RESET; HAL_RTC_SetTime(&RTCHandle,&RTC_TimeStruct,FORMAT_BIN); // RTC_TimeStructInit( &RTC_TimeStruct ); RTC_DateStruct.WeekDay = RTC_WEEKDAY_SATURDAY; RTC_DateStruct.Date = 0x01; RTC_DateStruct.Month = RTC_MONTH_JANUARY; RTC_DateStruct.Year = 0x01; HAL_RTC_SetDate(&RTCHandle,&RTC_DateStruct,FORMAT_BIN); }
/** * @brief System Power Configuration * The system Power is configured as follow : * + Regulator in LP mode * + VREFINT OFF, with fast wakeup enabled * + MSI as SysClk after Wake Up * + No IWDG * + Wakeup using EXTI Line (PA.12 (Arduino D2)) * @param None * @retval None */ static void SystemPower_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; /* Enable Power Control clock */ __HAL_RCC_PWR_CLK_ENABLE(); /* Enable Ultra low power mode */ HAL_PWREx_EnableUltraLowPower(); /* Enable the fast wake up from Ultra low power mode */ HAL_PWREx_EnableFastWakeUp(); /* Select MSI as system clock source after Wake Up from Stop mode */ __HAL_RCC_WAKEUPSTOP_CLK_CONFIG (RCC_STOP_WAKEUPCLOCK_MSI); /* Enable GPIOs clock */ __HAL_RCC_GPIOA_CLK_ENABLE(); __HAL_RCC_GPIOB_CLK_ENABLE(); __HAL_RCC_GPIOC_CLK_ENABLE(); __HAL_RCC_GPIOH_CLK_ENABLE(); /* Configure all GPIO port pins in Analog Input mode (floating input trigger OFF) */ /* Note: Debug using ST-Link is not possible during the execution of this */ /* example because communication between ST-link and the device */ /* under test is done through UART. All GPIO pins are disabled (set */ /* to analog input mode) including UART I/O pins. */ GPIO_InitStructure.Pin = GPIO_PIN_All; GPIO_InitStructure.Mode = GPIO_MODE_ANALOG; GPIO_InitStructure.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOA, &GPIO_InitStructure); HAL_GPIO_Init(GPIOB, &GPIO_InitStructure); HAL_GPIO_Init(GPIOC, &GPIO_InitStructure); HAL_GPIO_Init(GPIOH, &GPIO_InitStructure); /* Disable GPIOs clock */ __HAL_RCC_GPIOA_CLK_DISABLE(); __HAL_RCC_GPIOB_CLK_DISABLE(); __HAL_RCC_GPIOC_CLK_DISABLE(); __HAL_RCC_GPIOH_CLK_DISABLE(); }
void RtcEnterLowPowerStopMode( void ) { if( ( LowPowerDisableDuringTask == false ) && ( RtcTimerEventAllowsLowPower == true ) ) { BoardDeInitMcu( ); // Disable the Power Voltage Detector HAL_PWR_DisablePVD( ); SET_BIT( PWR->CR, PWR_CR_CWUF ); // Enable Ultra low power mode HAL_PWREx_EnableUltraLowPower( ); // Enable the fast wake up from Ultra low power mode HAL_PWREx_EnableFastWakeUp( ); // Enter Stop Mode HAL_PWR_EnterSTOPMode( PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI ); } }
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 System Power Configuration * The system Power is configured as follow : * + Regulator in LP mode * + VREFINT OFF, with fast wakeup enabled * + HSI as SysClk after Wake Up * + No IWDG * + Wakeup using EXTI Line (Key Button PC.13) * @param None * @retval None */ static void SystemPower_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; /* Enable Ultra low power mode */ HAL_PWREx_EnableUltraLowPower(); /* Enable the fast wake up from Ultra low power mode */ HAL_PWREx_EnableFastWakeUp(); /* Select HSI as system clock source after Wake Up from Stop mode */ __HAL_RCC_WAKEUPSTOP_CLK_CONFIG(RCC_StopWakeUpClock_HSI); /* Enable GPIOs clock */ __GPIOA_CLK_ENABLE(); __GPIOB_CLK_ENABLE(); __GPIOC_CLK_ENABLE(); __GPIOD_CLK_ENABLE(); __GPIOH_CLK_ENABLE(); /* Configure GPIO Pins: PA0,PA1,PA2,PA3,PA4,PA5,PA8,PA9,PA11,PA12,PA13,\ PA14,PA15 in Analog Input mode (floating input trigger OFF) */ /* PA6: SDO, PA7: SDI, PA10 :SDn are not put in analog mode */ GPIO_InitStructure.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 | \ GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_11 | \ GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15; GPIO_InitStructure.Mode = GPIO_MODE_ANALOG; GPIO_InitStructure.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOA, &GPIO_InitStructure); /* Configure GPIO Pins: PB0,PB1,PB2,PB4,PB5,PB7,PB8,PB9,PB10,PB11,PB12,PB13,\ PB14,PB15 in Analog Input mode (floating input trigger OFF) */ /*PB6: CSn, PB3: SCLK are not put in analog mode */ GPIO_InitStructure.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_4 | \ GPIO_PIN_5 | GPIO_PIN_7 | GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10 | \ GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15; GPIO_InitStructure.Mode = GPIO_MODE_ANALOG; GPIO_InitStructure.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOB, &GPIO_InitStructure); /* Configure GPIO Pins: PC0,PC1,PC2,PC3,PC4,PC5,PC6,PC8,PC9,PC10,PC11,PC12,\ PC13,PC14,PC15 in Analog Input mode (floating input trigger OFF) */ /*PC7: GPIO3, PC13: Push Button are not put in analog mode */ GPIO_InitStructure.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 | \ GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_8 | GPIO_PIN_9 | \ GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_14 | GPIO_PIN_15; GPIO_InitStructure.Mode = GPIO_MODE_ANALOG; GPIO_InitStructure.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOC, &GPIO_InitStructure); /* Configure all GPIO port pins in Analog Input mode (floating input trigger OFF) */ GPIO_InitStructure.Pin = GPIO_PIN_All; GPIO_InitStructure.Mode = GPIO_MODE_ANALOG; GPIO_InitStructure.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOD, &GPIO_InitStructure); HAL_GPIO_Init(GPIOH, &GPIO_InitStructure); /* Disable GPIOs clock */ __GPIOA_CLK_DISABLE(); __GPIOB_CLK_DISABLE(); __GPIOC_CLK_DISABLE(); __GPIOD_CLK_DISABLE(); __GPIOH_CLK_DISABLE(); }