title: Data Not Being Stored in Receive Data Register, (UART RXNE Flag not setting)
body: <p>I have been programming the stm32l412kb nucleo board, attempting to achieve basic UART communication. Transmission from the board works great but the board is not appearing to receive any data. </p>
  
  <p>For the software side, I have tried using standard HAL code in a few ways different, in both interrupt and non-interrupt mode. I have tied a more basic approach (shown below). From debugging line by line I have found that the Receive Data register (RDR) is not filling (and consequently the flag which sets when there is data there is not setting). This has been the error in each case.</p>
  
  <p>This aim of this code is to send back the character entered.</p>
  
  <pre><code>#include "stm32l4xx.h"
  
  int main(void)
  {
  
    /* USER CODE BEGIN 1 */
  
      /*The Usart2 peripheral needs its clock to be enabled.*/
      RCC-&gt;APB1ENR1 |= RCC_APB1ENR1_USART2EN;
      RCC-&gt;AHB2ENR |= RCC_AHB2ENR_GPIOAEN;
      /*The 72 MHz APB1 bus clock with a 9600baud rate gives a baud rate for the register of 0x1D4C*/
      USART2-&gt;BRR = 0x1D4C;
      /*For USART2 we need to enable the overall UART (U) driver, the transmission lines(T) and the reading lines(R). UART Enable is last.*/
      USART2-&gt;CR1 |= USART_CR1_RE | USART_CR1_TE | USART_CR1_UE;
  
  /*Setting transmission pin*/
      GPIOA-&gt;MODER |= GPIO_MODE_AF_PP;
      GPIOA-&gt;OSPEEDR |= GPIO_SPEED_FREQ_HIGH;
  
    /* USER CODE END 1 */
  
    /* Infinite loop */
    /* USER CODE BEGIN WHILE */
    while (1)
    {
        if (USART2-&gt;ISR &amp; USART_ISR_RXNE) //if RX is not empty
          {
           char temp = USART2-&gt;RDR; //fetch the data received
           USART2-&gt;TDR = temp;  //send it back out
           while (!(USART2-&gt;ISR &amp; USART_ISR_TC)); //wait for TX to be complete
          }
    }
  
        return 0;
  }
  </code></pre>
  
  <p>To send the data I have used RealTerm Serial Capture and have tried also the stm32cubeIDE console. One possibility of the source of the problem is that the DataSheet says </p>
  
  <p>"In the USART, the start bit is detected when a specific sequence of samples is recognized. This sequence is: 1 1 1 0 X 0 X 0 X 0 0 0 0."</p>
  
  <p>I have not coded any way of leading my data with this, however, from all the examples I have seen from a couple of books as well as videos, they did not need to think about this and it worked perfectly. Could it be a hardware problem? Is there something I'm not initialising? I have even tried different cables. </p>
  
  <p>Many thanks in advance for any help, </p>
  
  <p>Harry</p>
  
  <p>/*********************************UPDATE**************************************/</p>
  
  <p>First and foremost, thank you very much for the help, I now understand basics such as how to use the datasheet to configure the registers. It is much appreciated. I have updated my code but still the problem remains.</p>
  
  <p>So I have updated my configuration as so:</p>
  
  <pre><code>/*Configuring GPIO Pins*/
  /*Clearing whatever is held in the mode registers for pins 2 and 3 (Inverting with their masks.)*/
  GPIOA -&gt; MODER &amp;= ~(GPIO_MODER_MODE2_Msk | GPIO_MODER_MODE3_Msk);
  /*The 2 bits 10 are being shifted to the position which configures Mode of pin 2 and also for pin 3 in the mode register.
   *(10 is alterntive function mode).*/
  GPIOA -&gt; MODER |= (0b10 &lt;&lt; GPIO_MODER_MODE2_Pos) | (0b10 &lt;&lt; GPIO_MODER_MODE3_Pos);
  /*Clearing whatever is held in the output speed registers for pins 2 and 3*/
  GPIOA -&gt; OSPEEDR &amp;= ~(GPIO_OSPEEDR_OSPEED2_Msk | GPIO_OSPEEDR_OSPEED3_Msk);
  /*Setting the speed of pins 2 and 3 to be very high(11)*/
  GPIOA -&gt; OSPEEDR |= (0b11 &lt;&lt; GPIO_OSPEEDR_OSPEED2_Pos) | (0b11 &lt;&lt; GPIO_OSPEEDR_OSPEED3_Pos);
  /*Clearing whatever is held in the alternative function registers for pins 2 and 3.*/
  GPIOA -&gt; AFR[0] &amp;= ~(GPIO_AFRL_AFSEL2_Msk | GPIO_AFRL_AFSEL3_Msk);
  /*Setting the pins 2 and 3 to their alternative functions(TX and RX)*/
  GPIOA -&gt; AFR[0] |= (7 &lt;&lt; GPIO_AFRL_AFSEL2_Pos) | (7 &lt;&lt; GPIO_AFRL_AFSEL3_Pos);
  
  /*Clock Configuration*/
  /*Enabling the USART2 peripheral clock.*/
  RCC-&gt;APB1ENR1 &amp;= ~(RCC_APB1ENR1_USART2EN_Msk);
  RCC-&gt;APB1ENR1 |= (0b1 &lt;&lt; RCC_APB1ENR1_USART2EN_Pos);
  /*Enabling the GPIOA port peripheral clock*/
  RCC-&gt;AHB2ENR &amp;= ~(RCC_AHB2ENR_GPIOAEN_Msk);
  RCC-&gt;AHB2ENR |= (0b1 &lt;&lt; RCC_AHB2ENR_GPIOAEN_Pos);
  
  /*USART Configuartion*/
  /*The 72 MHz APB1 bus clock with a 9600baud rate gives a baud rate for the register of 0x1D4C*/
  USART2-&gt;BRR = 0x1D4C;
  /*For USART2 we need to enable the overall UART (U) driver, the transmission lines(T) and the reading lines(R). UART Enable is last.*/
  USART2-&gt;CR1 &amp;= ~(USART_CR1_RE_Msk | USART_CR1_TE_Msk | USART_CR1_UE_Msk);
  USART2-&gt;CR1 |= USART_CR1_RE | USART_CR1_TE | USART_CR1_UE;
  </code></pre>
  
  <p>Which has greatly developed my understanding of how to properly configure the device. However, I'm still having a problem with the overall aim of the code to bounce back a character, as the data is still not being read by the MCU. I will pursue on and update if it's successful. I'm thankful for any further suggestions. </p>