title: STM32F4 wake up from stop mode with UART
body: <p>I am using <strong>STM32F407VG</strong> MCU (<strong>STM32F4 Discovery board</strong>) and I want my MCU to wake-up from <strong>STOP</strong> mode when it receives a message from <strong>UART</strong>. I had to adjust some things manually because the STM32CubeMX is unable to set UART Rx pin as an GPIO interrupt pin when is configured for UART functionality. I am using <strong>DMA</strong> with UART. The function for reading UART is <strong>HAL_UART_Receive_DMA()</strong>.</p>
  <p>The wake-up from STOP mode when UART receives the msg works, but the problem is that a lot of bytes that are read are <strong>corrupted</strong>. I thought that <strong>only the first byte</strong> should be corrupted but not also the others. If I send another msg it is received fully and everything is OK, the problem is only with the first msg that is send and is used for wake-up the MCU. Any idea what the problem could be?</p>
  <p>I tested it in a way that whatever I send via UART to the MCU, it sends it back after wake-up. Here is the result (green - what I sent, white - what MCU received):</p>
  <p><a href="https://i.stack.imgur.com/34sYA.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/34sYA.png" alt="enter image description here" /></a></p>
  <p>It seems that somehow the data that is received by the MCU it depends where the <strong>space character</strong> is situated (except when space character is first).</p>
  <p>Here is some parts of my code:</p>
  <pre><code>void MX_USART3_UART_Init(void)
  {
  
    huart3.Instance = USART3;
    huart3.Init.BaudRate = 9600;
    huart3.Init.WordLength = UART_WORDLENGTH_8B;
    huart3.Init.StopBits = UART_STOPBITS_1;
    huart3.Init.Parity = UART_PARITY_NONE;
    huart3.Init.Mode = UART_MODE_TX_RX;
    huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
    huart3.Init.OverSampling = UART_OVERSAMPLING_16;
    if (HAL_UART_Init(&amp;huart3) != HAL_OK)
    {
      Error_Handler();
    }
  
  }
  
  //-----------------------------------
  
  // Setting the UART rx pin to trigger an interrupt on falling edge
  EXTI_ConfigTypeDef hExtiConfig;
  hExtiConfig.Line = EXTI_LINE_9; // PD9
  hExtiConfig.Mode = EXTI_MODE_INTERRUPT;
  hExtiConfig.Trigger = EXTI_TRIGGER_FALLING;
  hExtiConfig.GPIOSel = EXTI_GPIOD;   // PD9
  HAL_EXTI_SetConfigLine(&amp;g_hExtiHandle, &amp;hExtiConfig);
  HAL_EXTI_RegisterCallback(&amp;g_hExtiHandle, HAL_EXTI_COMMON_CB_ID, my_func);
  
  HAL_NVIC_SetPriority(EXTI9_5_IRQn, 0, 4);
  HAL_NVIC_EnableIRQ(EXTI9_5_IRQn);
  
  
  // Enter STOP mode
  HAL_PWREx_EnableFlashPowerDown();
  HAL_SuspendTick();
  HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);
  
  //-----------------------------------
  
  void EXTI9_5_IRQHandler(void)
  {
    HAL_ResumeTick();
    SystemClock_Config();
    MX_USART3_UART_Init();
  
    HAL_PWREx_DisableFlashPowerDown();
  
    HAL_EXTI_IRQHandler(&amp;g_hExtiHandle);
  }
  
  //-----------------------------------
  
  // Callback function after the wake-up
  void my_func(void)
  {
      HAL_GPIO_WritePin(LD4_GPIO_Port, LD4_Pin, GPIO_PIN_SET);
      HAL_EXTI_ClearPending(&amp;g_hExtiHandle, EXTI_TRIGGER_FALLING);
      HAL_EXTI_ClearConfigLine(&amp;g_hExtiHandle);   
  }
  </code></pre>