<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>UART Sample Code for Receiving Transmission</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/108399/uart-sample-code-for-receiving-transmission</link><description>Hello, I currently have UART code that can transmit a character. However, I am wondering if there&amp;#39;s any sample code for receiving a UART transmission? Thank you! 
 
 
 I also noticed that my UART code for transmission only transmits 1 byte and not the</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Tue, 20 Feb 2024 01:20:31 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/108399/uart-sample-code-for-receiving-transmission" /><item><title>RE: UART Sample Code for Receiving Transmission</title><link>https://devzone.nordicsemi.com/thread/469663?ContentTypeID=1</link><pubDate>Tue, 20 Feb 2024 01:20:31 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:041576cb-2cbd-4fca-b587-8cf6c313fb19</guid><dc:creator>hmolesworth</dc:creator><description>&lt;p&gt;Try this; I wrote this code to test different baud rates but it does what you are looking for I think, just choose a single baud rate; note you can use the Tx pin as the Rx pin for loopback testing without another device such as a PC (I set Loopback mode in this snippet):&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;static void SetupUART(const uint32_t BaudRate);
static void StartUART_Transmit(const uint8_t * const pMsg, const uint16_t MsgLength);
static void DisableUART(void);
static uint16_t SetupUART_Receive(uint8_t * const pMsg, const uint16_t MaxMsgLength);
static uint16_t WaitUART_Receive(void);

static NRF_UARTE_Type *pUART = NRF_UARTE0;

#define PIN_UART_TX         PIN_FEATHER_TXD
#define PIN_UART_RX         PIN_FEATHER_RXD
#define MAX_TX_PACKET_SIZE  256 //2048

static uint8_t TxMsg[MAX_TX_PACKET_SIZE] = &amp;quot;Baudrate generator for the UART is the top 20 bits of a 32-bit field; add in rounding 0.5 ls bit&amp;quot;;
#define TX_PACKET_SIZE sizeof(TxMsg)
#define RX_PACKET_SIZE (TX_PACKET_SIZE - 1)
static uint8_t RxMsg[RX_PACKET_SIZE] = &amp;quot;&amp;quot;;
uint32_t RequiredBaudRate = 1000000UL;
uint32_t MaxOkdBaudRate = 0UL;
uint32_t CalculatedRegisterValue;
uint32_t RegisterValueAtMaxBaud = 0;
uint64_t SystemClock = 16000000ULL;    // Typically 16MHz
uint64_t MagicScaler = 32;             // Preserves bits on divisions, shift 32 bits

void UART_BaudTest(void)
{
   // Enable crystal osc for 3rd party serial devices, don&amp;#39;t care for Loopback testing
   //for (RequiredBaudRate = 1000000; RequiredBaudRate &amp;lt; 2000000; )
   {
      //  Baudrate generator for the UART is the top 20 bits of a 32-bit field; add in rounding 0.5 ls bit
      CalculatedRegisterValue = (uint32_t)(((((uint64_t)RequiredBaudRate &amp;lt;&amp;lt; MagicScaler) + (SystemClock &amp;gt;&amp;gt; 1)) / SystemClock) + 0x800) &amp;amp; 0xFFFFF000;
      // Setup UART from scratch for each baud rate test
      SetupUART(CalculatedRegisterValue); // NRF_UART_BAUDRATE_230400);
      SetupUART_Receive(RxMsg, sizeof(RxMsg));
      StartUART_Transmit(TxMsg, TX_PACKET_SIZE);
      WaitUART_Receive();
      DisableUART();
      if ((memcmp(TxMsg, RxMsg, RX_PACKET_SIZE) == 0) &amp;amp;&amp;amp; (pUART-&amp;gt;EVENTS_ERROR == 0) &amp;amp;&amp;amp; (pUART-&amp;gt;ERRORSRC == 0x0000))
      {
         MaxOkdBaudRate = RequiredBaudRate;
         RegisterValueAtMaxBaud = CalculatedRegisterValue;
         //break;
      }
      RequiredBaudRate += 20;
   }
   while (1)
      ;
}

static void SetupUART(const uint32_t BaudRate)
{
   // Disable the UARTE
   pUART-&amp;gt;ENABLE = 0;
   // Configure UARTE with no flow control, no parity bit and selected baud rate
   pUART-&amp;gt;CONFIG = 0;
   pUART-&amp;gt;BAUDRATE = BaudRate;
   // Select TX and RX pin default disconnected mode to avoid sending break indication on disabling uart
   nrf_gpio_cfg(PIN_UART_RX, NRF_GPIO_PIN_DIR_INPUT,  NRF_GPIO_PIN_INPUT_CONNECT, NRF_GPIO_PIN_PULLUP, NRF_GPIO_PIN_S0S1, NRF_GPIO_PIN_NOSENSE);
   nrf_gpio_cfg(PIN_UART_TX, NRF_GPIO_PIN_DIR_OUTPUT, NRF_GPIO_PIN_INPUT_CONNECT, NRF_GPIO_PIN_PULLUP, NRF_GPIO_PIN_H0H1, NRF_GPIO_PIN_NOSENSE);
   // Select TX and RX pins; note for Loopback testing or 1-wire or RS485 a single pin can be used for both Rx and Tx
   pUART-&amp;gt;PSEL.TXD = PIN_UART_TX;
   pUART-&amp;gt;PSEL.RXD = PIN_UART_TX;  // For single pin loopback use PIN_UART_TX here, normal uart use PIN_UART_RX
   // Disable all interrupts and clear events
   pUART-&amp;gt;INTENCLR = 0xFFFFFFFFUL;
   pUART-&amp;gt;EVENTS_TXSTARTED = 0;
   pUART-&amp;gt;EVENTS_TXSTOPPED = 0;
   pUART-&amp;gt;EVENTS_RXSTARTED = 0;
   pUART-&amp;gt;EVENTS_ERROR = 0;
   pUART-&amp;gt;ERRORSRC = 0x000F;    // Write &amp;#39;1&amp;#39;s to clear any errors left set
   pUART-&amp;gt;EVENTS_ENDTX = 0;
   pUART-&amp;gt;EVENTS_ENDRX = 0;
   // Enable the UART, note 0x04 for UART and 0x08 for UARTE with DMA
   pUART-&amp;gt;ENABLE = 0x08;
}
STATIC_ASSERT (UARTE_ENABLE_ENABLE_Enabled == 0x08, &amp;quot;UARTE_ENABLE_ENABLE_Enabled == 0x08 Failed&amp;quot;);

static void StartUART_Transmit(const uint8_t * const pMsg, const uint16_t MsgLength)
{
   // Configure transmit buffer and start the transfer
   pUART-&amp;gt;TXD.MAXCNT = MsgLength;
   pUART-&amp;gt;TXD.PTR    = (uint32_t)pMsg;
   pUART-&amp;gt;TASKS_STARTTX = 1;
   __DSB();
   // Wait until the transfer start event is indicated
   while (pUART-&amp;gt;EVENTS_TXSTARTED == 0) ;
   // Wait until the transfer is complete
   while (pUART-&amp;gt;EVENTS_ENDTX == 0) ;
   // Stop the UART TX
   pUART-&amp;gt;TASKS_STOPTX = 1;
   __DSB();
   // Wait until we receive the stopped event
   while (pUART-&amp;gt;EVENTS_TXSTOPPED == 0) ;
}

static void DisableUART(void)
{
   // Disable the UARTE
   pUART-&amp;gt;ENABLE = 0;
   // De-Select TX and RX pins
   pUART-&amp;gt;PSEL.TXD = 0x80000000;
   pUART-&amp;gt;PSEL.RXD = 0x80000000;
}

static uint16_t SetupUART_Receive(uint8_t * const pMsg, const uint16_t MaxMsgLength)
{
   volatile uint32_t Timeout;
   // Clear receive buffer
   memcpy(pMsg, &amp;quot;-----------------&amp;quot;, MaxMsgLength);
   // Configure receive buffer and start reception
   pUART-&amp;gt;RXD.MAXCNT = MaxMsgLength;
   pUART-&amp;gt;RXD.PTR    = (uint32_t)pMsg;
   pUART-&amp;gt;EVENTS_ENDRX = 0;
   pUART-&amp;gt;TASKS_STARTRX = 1;
   __DSB();
   // Wait until the transfer start event is indicated
   while (pUART-&amp;gt;EVENTS_RXSTARTED == 0) ;
   return 0;
}

static uint16_t WaitUART_Receive(void)
{
   // Wait until the transfer is complete
   //while (pUART-&amp;gt;EVENTS_ENDRX == 0) ; //add timeout here also add LEDs blinking during tests
   // or just delay a bit ..
   nrf_delay_ms(1);
   // Stop the UART RX
   pUART-&amp;gt;TASKS_STOPRX = 1;
   __DSB();
   // Wait until we receive the stopped event
   //while (pUART-&amp;gt;EVENTS_ENDRX == 0) ;
   return 0;
}&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: UART Sample Code for Receiving Transmission</title><link>https://devzone.nordicsemi.com/thread/469642?ContentTypeID=1</link><pubDate>Mon, 19 Feb 2024 21:47:45 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:639c98bd-7b23-4b45-b452-03865ed9cc06</guid><dc:creator>CtrlAltDel</dc:creator><description>&lt;p&gt;Hello, it&amp;#39;s because I am hoping to get more experience with lower level register programming. It&amp;#39;d also potentially give me more control.&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: UART Sample Code for Receiving Transmission</title><link>https://devzone.nordicsemi.com/thread/469545?ContentTypeID=1</link><pubDate>Mon, 19 Feb 2024 13:48:08 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:02001a9c-e971-4498-9e81-fdf465fb34d9</guid><dc:creator>Jared</dc:creator><description>&lt;p&gt;Hi there,&lt;/p&gt;
&lt;p&gt;is there a specific reason for why you want to do this bare metal? There is several examples that shows how to receive data through UART but they use our drivers, which is what we always recommend.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;regards&lt;/p&gt;
&lt;p&gt;Jared&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>