stm32-stopmode-main.c

by Marwen Maghrebi
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include<stdio.h>
#include<string.h>
/* USER CODE END Includes */

/* Private variables ---------------------------------------------------------*/
ADC_HandleTypeDef hadc1;

RTC_HandleTypeDef hrtc;

UART_HandleTypeDef huart1;

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART1_UART_Init(void);
static void MX_RTC_Init(void);
static void MX_ADC1_Init(void);
/* USER CODE BEGIN PFP */
static void performADCMeasurement(void);
static void sendUARTMessage(char *message);
static void reconfigureRTCAlarm(RTC_HandleTypeDef *hrtc, uint32_t secondsToAdd);

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
{

    sendUARTMessage("   RTC Alarm Triggered: LED ON\r\n");

    // Reinitialize peripherals after waking up
    MX_ADC1_Init(); // Reinitialize ADC
    
    // Perform ADC measurement and transmit the result over UART
    performADCMeasurement();

    // Reconfigure the alarm to trigger after 20 seconds
    reconfigureRTCAlarm(hrtc, 20);
}

void Enter_Stop_Mode(void)
{
    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_2,0);
    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1,1);

    sendUARTMessage("0-Entering Stop Mode\r\n");

    // Disable ADC power-consuming peripherals  before entering Stop Mode to maximize power savings
    HAL_ADC_DeInit(&hadc1); 
 
    // Suspend SysTick interrupt to prevent it from waking up the MCU
    HAL_SuspendTick();

    // Enter Stop Mode with low power regulator on
    HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);

    // Resume SysTick interrupt and reconfigure system clock after waking up
    HAL_ResumeTick();
    SystemClock_Config();

    sendUARTMessage("1-Woke up from Stop Mode\r\n");
    sendUARTMessage("2-System Clock Reconfigured\r\n");
    sendUARTMessage("-------------------------------\r\n");
    // Toggle an LED
    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1,0);
    for (int i = 0; i < 20; i++) {

            HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_2);
            HAL_Delay(100);
        }
    HAL_Delay(500);

}


void sendUARTMessage(char *message)
{
    HAL_UART_Transmit(&huart1, (uint8_t*)message, strlen(message), HAL_MAX_DELAY);
}

void performADCMeasurement(void)
{
    // Start ADC conversion
    HAL_ADC_Start(&hadc1);

    // Wait for ADC conversion to complete
    if (HAL_ADC_PollForConversion(&hadc1, HAL_MAX_DELAY) == HAL_OK)
    {
        // Get the ADC value
        uint32_t adcValue = HAL_ADC_GetValue(&hadc1);

        // Convert the ADC value to a string and send it over UART
        char adcMsg[50];
        snprintf(adcMsg, sizeof(adcMsg), "   ADC Value: %lu\r\n", adcValue);
        sendUARTMessage("   **********************************\n\r");
        sendUARTMessage(adcMsg);
        sendUARTMessage("   **********************************\n\r");
    }
    // Stop ADC conversion
    HAL_ADC_Stop(&hadc1);
}

void reconfigureRTCAlarm(RTC_HandleTypeDef *hrtc, uint32_t secondsToAdd)
{
    RTC_TimeTypeDef time = {0};
    RTC_DateTypeDef date = {0};

    HAL_RTC_GetTime(hrtc, &time, RTC_FORMAT_BIN);
    HAL_RTC_GetDate(hrtc, &date, RTC_FORMAT_BIN);

    time.Seconds += secondsToAdd;
    if (time.Seconds >= 60)
    {
        time.Minutes++;
        time.Seconds -= 60;
    }
    if (time.Minutes >= 60)
    {
        time.Hours++;
        time.Minutes -= 60;
    }
    if (time.Hours >= 24)
    {
        time.Hours = 0;
    }

    RTC_AlarmTypeDef sAlarm = {0};
    sAlarm.Alarm = RTC_ALARM_A;
    sAlarm.AlarmTime.Hours   = time.Hours;
    sAlarm.AlarmTime.Minutes = time.Minutes;
    sAlarm.AlarmTime.Seconds = time.Seconds;

    if (HAL_RTC_SetAlarm_IT(hrtc, &sAlarm, RTC_FORMAT_BIN) != HAL_OK)
    {
        sendUARTMessage("Error: Setting RTC Alarm Failed\r\n");
        Error_Handler();
    }

    sendUARTMessage("   RTC Alarm Reconfigured\r\n");
}

/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* 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_USART1_UART_Init();
  MX_RTC_Init();
  MX_ADC1_Init();
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */
    /* USER CODE BEGIN 3 */
      Enter_Stop_Mode();
  }
  /* USER CODE END 3 */
}
Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?
-
00:00
00:00
Update Required Flash plugin
-
00:00
00:00