/** * @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 LED3 */ BSP_LED_Init(LED3); /* Disable Prefetch Buffer */ __HAL_FLASH_PREFETCH_BUFFER_DISABLE(); /* Configure the system clock @ 32 KHz */ SystemClock_Config(); /* Configure the system Power */ SystemPower_Config(); while (1) { /* Configure Key Button*/ BSP_PB_Init(BUTTON_KEY,BUTTON_MODE_GPIO); /* Wait Until Key button pressed */ while(BSP_PB_GetState(BUTTON_KEY) == RESET) { } /* Wait Until Key button pressed */ while(BSP_PB_GetState(BUTTON_KEY) != RESET) { } /* Key button (EXTI_Line13) will be used to wakeup the system from SLEEP mode */ BSP_PB_Init(BUTTON_KEY, BUTTON_MODE_EXTI); /* Enable the power down mode during Sleep mode */ __HAL_FLASH_SLEEP_POWERDOWN_ENABLE(); /*Suspend Tick increment to prevent wakeup by Systick interrupt. Otherwise the Systick interrupt will wake up the device within 1ms (HAL time base)*/ HAL_SuspendTick(); /* Enter Sleep Mode , wake up is done once Key push button is pressed */ HAL_PWR_EnterSLEEPMode(PWR_LOWPOWERREGULATOR_ON, PWR_SLEEPENTRY_WFI); /* Resume Tick interrupt if disabled prior to sleep mode entry*/ HAL_ResumeTick(); } }
void DEVICE_Handler(void) { char buf[10]; static uint8_t _start = 1; // device.door->NEW = 1; while (1) { if (sleep) { // HAL_PCD_DevDisconnect(&hpcd_USB_FS); /* отключить SysTick */ HAL_SuspendTick(); /* Enters Stop mode */ HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI); /* выход из STOPMode и инициализация USB */ /* включить SysTick */ HAL_ResumeTick(); /* Configure the system clock */ SystemClock_Config(); // HAL_PCD_DevConnect(&hpcd_USB_FS); } else { // HAL_Delay(200); // printf("test\n"); // HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_15); } } }
void goSleep(void) { setSensorDevPwr(DEV_PWR_OFF); setSysWakeUpLed(LED_OFF); setSysStsLed(LED_OFF); setTestPassLed(LED_OFF); setTestFailLed(LED_OFF); int adcVolBat = 0; do { HAL_SuspendTick(); HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFE); HAL_ResumeTick(); adcVolBat = readAdcChannel(ADC_CHANNEL_7); if (adcVolBat < (0x0400)) { setSysStsLed(LED_ON); HAL_Delay(10); setSysStsLed(LED_OFF); HAL_Delay(200); setSysStsLed(LED_ON); HAL_Delay(10); setSysStsLed(LED_OFF); } } while (adcVolBat < (0x0400)); }
/** * @brief This routine puts the MCU in sleep mode * @param None * @retval None */ void MCU_Enter_SleepMode(void) { /*Suspend Tick increment to prevent wakeup by Systick interrupt. Otherwise the Systick interrupt will wake up the device within 1ms (HAL time base)*/ HAL_SuspendTick(); HAL_PWR_EnterSLEEPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI); /* Infinite loop */ }
void SleepMode(void) { GPIO_InitTypeDef GPIO_InitStruct; /* Disable all GPIOs to reduce power */ MX_GPIO_Deinit(); /* Configure User push-button as external interrupt generator */ __HAL_RCC_GPIOC_CLK_ENABLE(); GPIO_InitStruct.Pin = B1_Pin; GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct); HAL_UART_DeInit(&huart2); /* Suspend Tick increment to prevent wakeup by Systick interrupt. Otherwise the Systick interrupt will wake up the device within 1ms (HAL time base) */ HAL_SuspendTick(); __HAL_RCC_PWR_CLK_ENABLE(); /* Request to enter SLEEP mode */ HAL_PWR_EnterSLEEPMode(0, PWR_SLEEPENTRY_WFI); /* Resume Tick interrupt if disabled prior to sleep mode entry*/ HAL_ResumeTick(); /* Reinitialize GPIOs */ MX_GPIO_Init(); /* Reinitialize UART2 */ MX_USART2_UART_Init(); }
void __initialize_hardware(void) { // Call the CSMSIS system initialisation routine. SystemInit(); #if defined (__VFP_FP__) && !defined (__SOFTFP__) // Enable the Cortex-M4 FPU only when -mfloat-abi=hard. // Code taken from Section 7.1, Cortex-M4 TRM (DDI0439C) // Set bits 20-23 to enable CP10 and CP11 coprocessor SCB->CPACR |= (0xF << 20); #endif // (__VFP_FP__) && !(__SOFTFP__) // Initialise the HAL Library; it must be the first // instruction to be executed in the main program. HAL_Init(); // Warning: The HAL always initialises the system timer. // For this to work, the default SysTick_Handler must not hang // (see system/src/cortexm/exception_handlers.c) // Unless explicitly enabled by the application, we prefer // to keep the timer interrupts off. HAL_SuspendTick(); // Enable HSE Oscillator and activate PLL with HSE as source configure_system_clock(); }
/** * @brief Low power sleep mode * NOTE: This is Low power sleep mode with wakeup button(PA0) */ void BSP_Sleepmode_PB(void) { /* suspand systick */ HAL_SuspendTick(); HAL_PWR_EnterSLEEPMode(PWR_LOWPOWERREGULATOR_ON,PWR_SLEEPENTRY_WFI); /* Resume systick */ HAL_ResumeTick(); }
void sleep(void) { // Stop HAL systick HAL_SuspendTick(); // Request to enter SLEEP mode HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI); // Restart HAL systick HAL_ResumeTick(); }
/** * @brief This function configures the system to enter Sleep mode for * current consumption measurement purpose. * Sleep Mode * ========== * - System Running at PLL (168MHz) * - Flash 5 wait state * - Instruction and Data caches ON * - Prefetch ON * - Code running from Internal FLASH * - All peripherals disabled. * - Wakeup using EXTI Line (Key Button) * @param None * @retval None */ void SleepMode_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_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(GPIOD, &GPIO_InitStruct); HAL_GPIO_Init(GPIOE, &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_GPIOD_CLK_DISABLE(); __HAL_RCC_GPIOE_CLK_DISABLE(); __HAL_RCC_GPIOH_CLK_DISABLE(); /* Configure Key Button */ BSP_PB_Init(BUTTON_KEY, BUTTON_MODE_EXTI); /* Suspend Tick increment to prevent wakeup by Systick interrupt. Otherwise the Systick interrupt will wake up the device within 1ms (HAL time base) */ HAL_SuspendTick(); /* Request to enter SLEEP mode */ HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI); /* Resume Tick interrupt if disabled prior to sleep mode entry */ HAL_ResumeTick(); /* Initialize LED6 */ BSP_LED_Init(LED6); /* Turn LED6 On */ BSP_LED_On(LED6); /* Inserted Delay */ HAL_Delay(200); }
void MCU_Enter_StopMode(void) { HAL_SuspendTick(); __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU); while((PWR->CSR & (uint32_t) 0x00000001)!=0);//attesa che il WUF si azzeri (via HW) HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFE); /* Infinite loop */ HAL_ResumeTick(); }
/** * @brief This function is executed in case of error occurrence. * @param None * @retval None */ void Error_Handler(void) { /* Suspend tick */ HAL_SuspendTick(); /* Turn LED3 on */ BSP_LED_On(LED3); while (1) { } }
void deepsleep(void) { // Stop HAL systick HAL_SuspendTick(); // Request to enter STOP mode with regulator in low power mode HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI); // After wake-up from STOP reconfigure the PLL SetSysClock(); // Restart HAL systick HAL_ResumeTick(); }
/** * @brief This function configures the system to enter Sleep mode for * current consumption measurement purpose. * Sleep Mode * ========== * - System Running at PLL (72 MHz) * - Flash 2 wait state * - Prefetch ON * - Code running from Internal FLASH * - All peripherals disabled. * - Wakeup using EXTI Line (User push-button PA.00 pin) * @param None * @retval None */ void SleepMode_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 */ __GPIOA_CLK_ENABLE(); __GPIOB_CLK_ENABLE(); __GPIOC_CLK_ENABLE(); __GPIOD_CLK_ENABLE(); __GPIOE_CLK_ENABLE(); __GPIOF_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(GPIOA, &GPIO_InitStruct); HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); HAL_GPIO_Init(GPIOD, &GPIO_InitStruct); HAL_GPIO_Init(GPIOE, &GPIO_InitStruct); HAL_GPIO_Init(GPIOF, &GPIO_InitStruct); /* Disable GPIOs clock */ __GPIOA_CLK_DISABLE(); __GPIOB_CLK_DISABLE(); __GPIOC_CLK_DISABLE(); __GPIOD_CLK_DISABLE(); __GPIOE_CLK_DISABLE(); __GPIOF_CLK_DISABLE(); /* Configure User push-button as external interrupt generator */ BSP_PB_Init(BUTTON_USER, BUTTON_MODE_EXTI); /*Suspend Tick increment to prevent wakeup by Systick interrupt. Otherwise the Systick interrupt will wake up the device within 1ms (HAL time base)*/ HAL_SuspendTick(); /* Request to enter SLEEP mode */ HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI); /* Resume Tick interrupt if disabled prior to sleep mode entry*/ HAL_ResumeTick(); /* Initialize led */ BSP_LED_Init(LED3); }
/** * @brief Low power sleep mode * NOTE: This is Low power sleep mode, system can be wake up using any interrupt */ void BSP_SleepMode(void) { /* suspand systick */ HAL_SuspendTick(); /* * Enters Sleep mode * PWR_LOWPOWERREGULATOR_ON: SLEEP mode with low power regulator ON * PWR_SLEEPENTRY_WFI: enter SLEEP mode with WFI instruction */ HAL_PWR_EnterSLEEPMode(PWR_LOWPOWERREGULATOR_ON,PWR_SLEEPENTRY_WFI); /* Resume systick */ HAL_ResumeTick(); }
void SleepMode(void) { //SystemClock_Config_LOW(); /* 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_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(); */ /*Suspend Tick increment to prevent wakeup by Systick interrupt. Otherwise the Systick interrupt will wake up the device within 1ms (HAL time base)*/ HAL_SuspendTick(); /* Request to enter SLEEP mode */ HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI); /* Resume Tick interrupt if disabled prior to sleep mode entry*/ HAL_ResumeTick(); // SystemClock_Config_8MHz(); /* Inserted Delay */ HAL_Delay(200); }
/** * @brief Main program * @param None * @retval None */ int main(void) { /* 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 */ SystemClock_Config(); /* Configure LED1 */ BSP_LED_Init(LED1); /* Disable Prefetch Buffer */ __HAL_FLASH_PREFETCH_BUFFER_DISABLE(); while (1) { /* Insert 5 seconds delay */ HAL_Delay(5000); /* Configure the system Power */ SystemPower_Config(); /* User push-button (line 15_10) will be used to wakeup the system from SLEEP mode */ BSP_PB_Init(BUTTON_USER, BUTTON_MODE_EXTI); /*Suspend Tick increment to prevent wakeup by Systick interrupt. Otherwise the Systick interrupt will wake up the device within 1ms (HAL time base)*/ HAL_SuspendTick(); /* Enter Sleep Mode , wake up is done once User push-button is pressed */ HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI); /* Resume Tick interrupt if disabled prior to SLEEP mode entry */ HAL_ResumeTick(); /* Re-configure LED1 */ BSP_LED_Init(LED1); } }
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(); /* Configure the system clock */ SystemClock_Config(); /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_I2C1_Init(); MX_USART2_UART_Init(); /* USER CODE BEGIN 2 */ HAL_DBGMCU_EnableDBGSleepMode(); HAL_SuspendTick(); // we don't want systick to wake us up every 1ms main_app(); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ HAL_PWR_EnterSTOPMode(PWR_MAINREGULATOR_ON, PWR_STOPENTRY_WFI); } /* USER CODE END 3 */ }
/** * @brief EXTI line detection callback. * @param GPIO_Pin: Specifies the pins connected EXTI line * @retval None */ void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) { if(GPIO_Pin == KEY_BUTTON_PIN) { if (uwIncrementState == 0) { /* Suspend tick increment */ HAL_SuspendTick(); /* Change the Push button state */ uwIncrementState = 1; } else { /* Resume tick increment */ HAL_ResumeTick(); /* Change the Push button state */ uwIncrementState = 0; } } }
void __initialize_hardware(void) { // Enable HSE Oscillator and activate PLL with HSE as source configure_system_clock(); // Call the CSMSIS system clock routine to store the clock frequency // in the SystemCoreClock global RAM location. SystemCoreClockUpdate(); // Initialise the HAL Library; it must be the first // instruction to be executed in the main program. HAL_Init(); // Warning: The HAL always initialises the system timer. // For this to work, the default SysTick_Handler must not hang // (see system/src/cortexm/exception_handlers.c) // Unless explicitly enabled by the application, we prefer // to keep the timer interrupts off. HAL_SuspendTick(); }
void deepsleep(void) { // Stop HAL systick HAL_SuspendTick(); // Request to enter STOP mode with regulator in low power mode #if TARGET_STM32L4 HAL_PWREx_EnterSTOP2Mode(PWR_STOPENTRY_WFI); #else /* TARGET_STM32L4 */ HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI); #endif /* TARGET_STM32L4 */ // Restart HAL systick HAL_ResumeTick(); // After wake-up from STOP reconfigure the PLL SetSysClock(); #if DEVICE_LOWPOWERTIMER rtc_synchronize(); #endif }
/** * @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 LED3 */ BSP_LED_Init(LED3); /* Configure the system clock to 2 Mhz */ SystemClock_Config(); /* Configure and enable the DMA Stream for Memory to Memory transfer */ DMA_Config(); /* Suspend Tick increment to prevent wakeup by Systick interrupt. */ /* Otherwise the Systick interrupt will wake up the device within 1ms */ /* (HAL time base). */ HAL_SuspendTick(); /* Enter sleep mode and wait for DMA interrupt */ HAL_PWR_EnterSLEEPMode(PWR_LOWPOWERREGULATOR_ON, PWR_SLEEPENTRY_WFI); /* Resume Tick interrupt if disabled prior to SLEEP mode entry */ HAL_ResumeTick(); /* Infinite loop */ while (1) { } }
/** * @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(); /* Configure the system clock to 72 MHz */ SystemClock_Config(); /* Configure LEDs */ BSP_LED_Init(LED1); BSP_LED_Init(LED2); BSP_LED_Init(LED4); /*##-1- Configure the TSC peripheral #######################################*/ TscHandle.Instance = TSCx; TscHandle.Init.AcquisitionMode = TSC_ACQ_MODE_NORMAL; TscHandle.Init.CTPulseHighLength = TSC_CTPH_1CYCLE; TscHandle.Init.CTPulseLowLength = TSC_CTPL_1CYCLE; TscHandle.Init.IODefaultMode = TSC_IODEF_OUT_PP_LOW; TscHandle.Init.MaxCountInterrupt = ENABLE; TscHandle.Init.MaxCountValue = TSC_MCV_16383; TscHandle.Init.PulseGeneratorPrescaler = TSC_PG_PRESC_DIV64; TscHandle.Init.SpreadSpectrum = DISABLE; TscHandle.Init.SpreadSpectrumDeviation = 127; TscHandle.Init.SpreadSpectrumPrescaler = TSC_SS_PRESC_DIV1; TscHandle.Init.SynchroPinPolarity = TSC_SYNC_POL_FALL; TscHandle.Init.ChannelIOs = 0; /* Not needed yet. Will be set with HAL_TSC_IOConfig() */ TscHandle.Init.SamplingIOs = 0; /* Not needed yet. Will be set with HAL_TSC_IOConfig() */ TscHandle.Init.ShieldIOs = 0; /* Not needed yet. Will be set with HAL_TSC_IOConfig() */ if (HAL_TSC_Init(&TscHandle) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /*##-2- Configure the touch-sensing IOs ####################################*/ IoConfig.ChannelIOs = TSC_GROUP8_IO2; /* Start with the first channel */ IoConfig.SamplingIOs = (TSC_GROUP8_IO1 | TSC_GROUP8_IO1 | TSC_GROUP6_IO1); IoConfig.ShieldIOs = TSC_GROUP6_IO2; if (HAL_TSC_IOConfig(&TscHandle, &IoConfig) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /*##-3- Discharge the touch-sensing IOs ####################################*/ HAL_TSC_IODischarge(&TscHandle, ENABLE); HAL_Delay(1); /* 1 ms is more than enough to discharge all capacitors */ /*##-4- Start the acquisition process ######################################*/ if (HAL_TSC_Start_IT(&TscHandle) != HAL_OK) { /* Acquisition Error */ Error_Handler(); } /* Infinite loop */ while (1) { /*Suspend Tick increment to prevent wakeup by Systick interrupt. Otherwise the Systick interrupt will wake up the device within 1ms (HAL time base)*/ HAL_SuspendTick(); /* The acquisition process is now performed in the HAL_TSC_ConvCpltCallback() function */ HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI); /* Resume Tick interrupt if disabled prior to sleep mode entry*/ HAL_ResumeTick(); } }
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 */ }