stm32-Sleep-mode-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 define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
#define LED_Pin GPIO_PIN_1
#define LED_Pin2 GPIO_PIN_2
#define LED_GPIO_Port GPIOB
/* USER CODE END PD */

/* Private variables ---------------------------------------------------------*/
UART_HandleTypeDef huart1;

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART1_UART_Init(void);
/* USER CODE BEGIN PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
uint8_t Rx_data;
char * str;

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
    if (huart->Instance == USART1) {
        str = "WakeUP from SLEEP by UART\r\n";
        HAL_UART_Transmit(&huart1, (uint8_t*) str, strlen(str), HAL_MAX_DELAY);

        // Re-enable the UART receive interrupt
        HAL_UART_Receive_IT(&huart1, &Rx_data, 1);
        HAL_PWR_DisableSleepOnExit();
    }
}

/* USER CODE END 0 */

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();
  /* USER CODE BEGIN 2 */
  HAL_UART_Receive_IT(&huart1, &Rx_data, sizeof(Rx_data));  // Enable UART receive interrupt
  /* USER CODE END 2 */

  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */
    /* USER CODE BEGIN 3 */
    str = "Entering into SLEEP MODE in 5 seconds\r\n";
    HAL_UART_Transmit(&huart1, (uint8_t*) str, strlen(str), HAL_MAX_DELAY);
    HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, RESET);
    HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin2,RESET);
    HAL_Delay(5000);

    HAL_SuspendTick();
    HAL_PWR_EnableSleepOnExit();

    HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, SET);
    str = "SLEEP MODE is active \r\n";
    HAL_UART_Transmit(&huart1, (uint8_t*) str, strlen(str), HAL_MAX_DELAY);

    HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI);
    HAL_ResumeTick();

    str = "WakeUP from SLEEP\r\n";
    HAL_UART_Transmit(&huart1, (uint8_t*) str, strlen(str), HAL_MAX_DELAY);
    HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, RESET);
    for (int i = 0; i < 20; i++) {

        HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin2);
        HAL_Delay(100);
    }
  }
  /* 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