Blog Datasheets Home About me Clients My work Services Contact

G2Labs Grzegorz Grzęda

Exploring the UART communication on the nRF52

November 15, 2023

Exploring the UART Communication on the nRF52

The nRF52 is a powerful and versatile microcontroller that supports various communication protocols. In this blog post, we will be focusing on exploring UART (Universal Asynchronous Receiver-Transmitter) communication on the nRF52. UART is a simple and widely used communication protocol that allows two devices to send and receive data asynchronously.

Setting Up the nRF52 Development Environment

Before we dive into UART communication, let’s ensure that we have the necessary tools and libraries installed to work with the nRF52 microcontroller board.

  1. Install the nRF Command Line Tools: Visit the Nordic Semiconductor website and download the nRF Command Line Tools appropriate for your operating system. Follow the installation instructions provided by Nordic Semiconductor.

  2. Install the nRF5 SDK: Download the latest version of the nRF5 Software Development Kit (SDK) from the Nordic Semiconductor website. Extract the downloaded SDK package to a convenient location on your machine.

  3. Set up the development environment: Set the GNUARMEMB_TOOLCHAIN_PATH environment variable to the path of your ARM GCC toolchain. This will allow the nRF5 SDK to find the necessary tools during compilation.

  4. Configure the development kit: If you are using a specific development kit, such as the nRF52 DK, follow the instructions provided in the kit’s documentation to set up the hardware and drivers.

Once you have completed the above steps, you are ready to start exploring UART communication on the nRF52.

Initializing UART

In order to use UART on the nRF52, we need to initialize the UART peripheral and configure its settings. Let’s take a look at an example of how to initialize UART with default settings:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#include <stdio.h>
#include "nrf_uart.h"

int main(void)
{
    nrf_uart_baudrate_set(NRF_UART0, UART_BAUDRATE_115200);
    nrf_uart_configure(NRF_UART0, NRF_UART_PARITY_EXCLUDED, NRF_UART_HWFC_DISABLED);

    // Other initialization code...

    while (1)
    {
        // UART communication code...
    }
}

In the above code snippet, we include the necessary header files and then set the baud rate and configuration for NRF_UART0, which is the UART module provided by the nRF52. We set the baud rate to 115200 and configure the UART to exclude any parity bits and disable hardware flow control. You can change these settings according to your requirements.

Sending Data via UART

Now that we have initialized the UART module, let’s move on to sending data via UART. The nRF5 SDK provides functions to write data to the UART transmit buffer. Here’s an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <stdio.h>
#include "nrf_uart.h"

int main(void)
{
    // UART initialization code...

    while (1)
    {
        char message[] = "Hello UART!";
        uint8_t len = sizeof(message) - 1;

        nrf_uart_event_clear(NRF_UART0, NRF_UART_EVENT_TXDRDY);
        nrf_uart_tx_buffer_set(NRF_UART0, (uint8_t *)message, len);
        
        while (!nrf_uart_event_check(NRF_UART0, NRF_UART_EVENT_TXDRDY))
        {
            // Wait for transmission to complete...
        }

        // Other code...
    }
}

In the above code snippet, we first define a char array message with the data we want to send. We calculate the length of the message and clear the NRF_UART_EVENT_TXDRDY event to ensure we start with a fresh transmit buffer. We then set the transmit buffer with our message and wait until the transmission completes using a while loop. Once the NRF_UART_EVENT_TXDRDY event is set, we can proceed with other code execution.

Receiving Data via UART

Receiving data via UART on the nRF52 is similar to sending data. We need to configure the UART peripheral and then read from the receive buffer. Here’s an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <stdio.h>
#include "nrf_uart.h"

int main(void)
{
    // UART initialization code...

    while (1)
    {
        uint8_t buffer[256];
        uint8_t index = 0;

        if (nrf_uart_event_check(NRF_UART0, NRF_UART_EVENT_RXDRDY))
        {
            buffer[index++] = nrf_uart_rxd_get(NRF_UART0);
            
            nrf_uart_event_clear(NRF_UART0, NRF_UART_EVENT_RXDRDY);
        }

        // Process received data...

        // Other code...
    }
}

In the above code snippet, we define a buffer to store the received data and an index variable to keep track of the buffer position. We check if the NRF_UART_EVENT_RXDRDY event is set, indicating that new data has been received. If the event is set, we read the received byte and store it in the buffer. We then clear the NRF_UART_EVENT_RXDRDY event and proceed with further processing of the received data.

Conclusion

In this blog post, we explored UART communication on the nRF52 microcontroller using the Nordic Semiconductor nRF5 SDK. We learned how to initialize the UART peripheral, send data, and receive data. UART is a versatile communication protocol that can be used to interface the nRF52 with other devices, such as sensors, displays, and other microcontrollers. With the knowledge gained from this blog post, you are now ready to incorporate UART communication into your nRF52 projects. Happy coding!


➡️ Using I2C protocol with the nRF52


⬅️ Understanding the GPIOs on the nRF52


Go back to Posts.