/**
******************************************************************************
* @file UART/UART_WakeUpFromStop/Src/main.c
* @author MCD Application Team
* @brief This sample code shows how to use UART HAL API to wake up
* the MCU from STOP mode
* Two boards are used, one which enters STOP mode and the second
* one which sends the wake-up stimuli.
******************************************************************************
* @attention
*
*
© Copyright (c) 2016 STMicroelectronics.
* All rights reserved.
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include
#include
#include
#include
#include
/** @addtogroup STM32L0xx_HAL_Examples
* @{
*/
/** @addtogroup UART_WakeUpFromStop
* @{
*/
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* to enable for the board entering STOP mode,
to disable for the board sending wake-up stimuli */
#define BOARD_IN_STOP_MODE
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* UART handler declaration */
UART_HandleTypeDef UartHandle;
UART_WakeUpTypeDef WakeUpSelection;
/* Buffer used for reception */
uint8_t aRxBuffer[2];
/* Private function prototypes -----------------------------------------------*/
void ManageReception(void);
const uint8_t* random_frame_status();
static void Error_Handler(void);
static void SystemClock_Config_GEN(void);
static void MX_GPIO_Init();
/**
* @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 the system clock to 32 MHz */
SystemClock_Config_GEN();
/* Configure LED3 */
BSP_LED_Init(LED3);
MX_GPIO_Init();
/* Select HSI as wakeup clock from stop mode */
__HAL_RCC_WAKEUPSTOP_CLK_CONFIG(RCC_STOP_WAKEUPCLOCK_HSI);
/* HSI must be UART clock source to be able to wake up the MCU */
__HAL_RCC_USART2_CONFIG(RCC_USART2CLKSOURCE_HSI);
/*##-1- Configure the UART peripheral ######################################*/
/* Put the USART peripheral in the Asynchronous mode (UART Mode) */
/* UART configured as follows:
- Word Length = 8 Bits
- Stop Bit = One Stop bit
- Parity = None
- BaudRate = 19200 baud
- Hardware flow control disabled (RTS and CTS signals) */
UartHandle.Instance = USARTx;
HAL_UART_DeInit(&UartHandle);
UartHandle.Init.BaudRate = 19200;
UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
UartHandle.Init.StopBits = UART_STOPBITS_1;
UartHandle.Init.Parity = UART_PARITY_NONE;
UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
UartHandle.Init.Mode = UART_MODE_TX_RX;
UartHandle.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if(HAL_UART_Init(&UartHandle) != HAL_OK)
{
Error_Handler();
}
BSP_LED_On(LED3);
HAL_Delay(5000);
while( (__HAL_UART_GET_FLAG(&UartHandle, USART_ISR_BUSY) == SET) ){};
/* make sure that UART is ready to receive
* (test carried out again later in HAL_UARTEx_StopModeWakeUpSourceConfig) */
while(__HAL_UART_GET_FLAG(&UartHandle, USART_ISR_REACK) == RESET){};
while (1)
{
if( (__HAL_UART_GET_FLAG(&UartHandle, UART_FLAG_RXNE) != SET) )
{
/* set the wake-up event:
* specify wake-up on RXNE flag */
WakeUpSelection.WakeUpEvent = UART_WAKEUP_ON_READDATA_NONEMPTY;
if (HAL_UARTEx_StopModeWakeUpSourceConfig(&UartHandle, WakeUpSelection)!= HAL_OK)
{
Error_Handler();
}
/* Enable the UART Wake UP from stop mode Interrupt */
__HAL_UART_ENABLE_IT(&UartHandle, UART_IT_WUF);
/* about to enter stop mode: switch off LED */
BSP_LED_Off(LED3);
/* enable MCU wake-up by UART */
HAL_UARTEx_EnableStopMode(&UartHandle);
/* enter stop mode */
HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);
/* ... STOP mode ... */
/* at that point, MCU has been awoken: the LED has been turned back on */
/* Wake Up based on RXNE flag successful */
HAL_UARTEx_DisableStopMode(&UartHandle);
BSP_LED_On(LED3);
}
ManageReception();
}
}
void ManageReception(void)
{
uint8_t dataToSend[2];
do {
if( HAL_UART_Receive(&UartHandle, (uint8_t *)&aRxBuffer[0], 1, 2) == HAL_OK )
{
memcpy(&dataToSend[0],&aRxBuffer[0] , 1);
if (dataToSend[0] == 0x55)
{
if(HAL_UART_Receive(&UartHandle, (uint8_t *)&aRxBuffer[1], 1, 2) == HAL_OK)
{
memcpy(&dataToSend[1],&aRxBuffer[1] , 1);
switch(dataToSend[1])
{
case 0xCA:
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);
HAL_Delay(1);
HAL_UART_Transmit(&UartHandle, (uint8_t[]){0x5B, 0xCA}, 2, 2);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);
break;
case 0xCB:
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);
HAL_Delay(1);
HAL_UART_Transmit(&UartHandle, (uint8_t[]){0x5B, 0xCB}, 2, 2);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);
break;
case 0x10:
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);
HAL_Delay(1);
HAL_UART_Transmit(&UartHandle, (uint8_t[]){0x5B, 0x10}, 2, 2);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);
break;
case 0x11:
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);
HAL_Delay(1);
HAL_UART_Transmit(&UartHandle, (uint8_t[]){0x5B, 0x11}, 2, 2);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);
break;
case 0xFF:
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);
HAL_Delay(1);
HAL_UART_Transmit(&UartHandle, (uint8_t[]){0x5B, 0xFF}, 2, 2); // rectification
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);
break;
case 0x20:
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);
HAL_Delay(1);
HAL_UART_Transmit(&UartHandle, (uint8_t[]){0x5B, 0x24}, 2, 2);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);
break;
// Ajoutez d'autres cas si nécessaire
}
}
}
}
else
{
//nothing to do
}
}while ((__HAL_UART_GET_FLAG(&UartHandle, USART_ISR_BUSY) == SET) || (__HAL_UART_GET_FLAG(&UartHandle, USART_ISR_REACK) == RESET));
}
/**
* @brief UART error callbacks
* @param UartHandle: UART handle
* @note This example shows a simple way to report transfer error, and you can
* add your own implementation.
* @retval None
*/
void HAL_UART_ErrorCallback(UART_HandleTypeDef *UartHandle)
{
Error_Handler();
}
/**
* @brief This function is executed in case of error occurrence.
* @param None
* @retval None
*/
static void Error_Handler(void)
{
{
/* In case of error, LED3 transmits a sequence of three dots, three dashes, three dots */
BSP_LED_On(LED3);
HAL_Delay(300);
BSP_LED_Off(LED3);
HAL_Delay(300);
BSP_LED_On(LED3);
}
}
void SystemClock_Config_GEN(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
/** Configure the main internal regulator output voltage
*/
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLLMUL_4;
RCC_OscInitStruct.PLL.PLLDIV = RCC_PLLDIV_2;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
{
Error_Handler();
}
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART2;
PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_HSI;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
{
Error_Handler();
}
}
// Fonction d'initialisation des GPIO pour PA3
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOA_CLK_ENABLE();
/* Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);
/* Configure GPIO pin : PA5 (connected to LED) */
GPIO_InitStruct.Pin = GPIO_PIN_4;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
/**
* @}
*/
/**
* @}
*/
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/