Learn how to use UART on NXP MCXA Microcontroller

In the last blog, I have shown how to use GPIO as input, both in interrupt and polling mode. In this blog, I will explain how to use UART on NXP MCXA Microcontroller.

Serial Port / UART is used in wide range of application. It is popularly used interface on any MCU based design. It can be used for interfacing with PC over RS232 or to establish a communication between two MCUs or interfacing with devices like LTE modem, GPS modem, etc.

Serial port or UART interface is also very widely used as debug port to print debug messages which helps if the device misbehaves, looking at the debug messages once can figure out where the problem is.

Learn how to use UART on NXP MCXA Microcontroller 1

NXP has provided SDK where UART example is given. You can import by going to New>Import SDK examples, Select the FRDM Board, and select the right example as shown below.

Learn how to use UART on NXP MCXA Microcontroller 2

So, let us try to see what are the steps involved in order to enable/configure UART and send and receive data over UART.

In this example we will see how we can enable LPUART0 on NXP MXCA Microcontroller.

First we need to enable clocks, release reset and enable Pin for UART. This can be done as shown below.

    /* Write to PORT0: Peripheral clock is enabled */
    CLOCK_EnableClock(kCLOCK_GatePORT0);
    /* PORT0 peripheral is released from reset */
    RESET_ReleasePeripheralReset(kPORT0_RST_SHIFT_RSTn);

    CLOCK_SetClockDiv(kCLOCK_DivLPUART0, 1u);
    CLOCK_AttachClk(kFRO12M_to_LPUART0);
    /* LPUART0 peripheral is released from reset */
    RESET_ReleasePeripheralReset(kLPUART0_RST_SHIFT_RSTn);

RXD, TXD Pin configuration done as shown below. Note the alternate function Enum used kPORT_MuxAlt2 for UART function selection.


const port_pin_config_t port0_2_pin51_config = {/* Internal pull-up resistor is enabled */
                                                    kPORT_PullUp,
                                                    /* Low internal pull resistor value is selected. */
                                                    kPORT_LowPullResistor,
                                                    /* Fast slew rate is configured */
                                                    kPORT_FastSlewRate,
                                                    /* Passive input filter is disabled */
                                                    kPORT_PassiveFilterDisable,
                                                    /* Open drain output is disabled */
                                                    kPORT_OpenDrainDisable,
                                                    /* Low drive strength is configured */
                                                    kPORT_LowDriveStrength,
                                                    /* Normal drive strength is configured */
                                                    kPORT_NormalDriveStrength,
                                                    /* Pin is configured as LPUART0_RXD */
                                                    kPORT_MuxAlt2,
                                                    /* Digital input enabled */
                                                    kPORT_InputBufferEnable,
                                                    /* Digital input is not inverted */
                                                    kPORT_InputNormal,
                                                    /* Pin Control Register fields [15:0] are not locked */
                                                    kPORT_UnlockRegister};
    /* PORT0_2 (pin 51) is configured as LPUART0_RXD */
    PORT_SetPinConfig(PORT0, 2U, &port0_2_pin51_config);

    const port_pin_config_t port0_3_pin52_config = {/* Internal pull-up resistor is enabled */
                                                    kPORT_PullUp,
                                                    /* Low internal pull resistor value is selected. */
                                                    kPORT_LowPullResistor,
                                                    /* Fast slew rate is configured */
                                                    kPORT_FastSlewRate,
                                                    /* Passive input filter is disabled */
                                                    kPORT_PassiveFilterDisable,
                                                    /* Open drain output is disabled */
                                                    kPORT_OpenDrainDisable,
                                                    /* Low drive strength is configured */
                                                    kPORT_LowDriveStrength,
                                                    /* Normal drive strength is configured */
                                                    kPORT_NormalDriveStrength,
                                                    /* Pin is configured as LPUART0_TXD */
                                                    kPORT_MuxAlt2,
                                                    /* Digital input enabled */
                                                    kPORT_InputBufferEnable,
                                                    /* Digital input is not inverted */
                                                    kPORT_InputNormal,
                                                    /* Pin Control Register fields [15:0] are not locked */
                                                    kPORT_UnlockRegister};
    /* PORT0_3 (pin 52) is configured as LPUART0_TXD */
    PORT_SetPinConfig(PORT0, 3U, &port0_3_pin52_config);

After IO configuration is done, System clock is also set by calling BOARD_InitBootClocks(); which intern calls BOARD_BootClockFRO96M(); to set MCU frequency to 96Mhz.

After that, UART Port configuration is done as shown below.

#define LPUART_CLK_FREQ 12000000U

lpuart_config_t config;

/*
     * config.baudRate_Bps = 115200U;
     * config.parityMode = kLPUART_ParityDisabled;
     * config.stopBitCount = kLPUART_OneStopBit;
     * config.txFifoWatermark = 0;
     * config.rxFifoWatermark = 0;
     * config.enableTx = false;
     * config.enableRx = false;
     */

    LPUART_GetDefaultConfig(&config);
    config.baudRate_Bps = 115200U;
    config.enableTx     = true;
    config.enableRx     = true;

    LPUART_Init(LPUART0, &config, LPUART_CLK_FREQ);

And, in order to enable receive data interrupt following functions are called.

/* Enable RX interrupt. */
    LPUART_EnableInterrupts(LPUART0, kLPUART_RxDataRegFullInterruptEnable);
    EnableIRQ(LPUART0_IRQn);

This completes the configuration which is done only one time at the time of power up.

Now, in order to send data over UART, you can use the following function.

// to send multiple bytes at a time
LPUART_WriteBlocking(LPUART0, "CAPUF-EMBEDDED\r\n", 16);

There is another function LPUART_WriteByte, which could be used but it doesn’t check if transmit buffer is empty or not so you need to check that eternally, so better to use LPUART_WriteBlocking function only.

To receive data over UART in interrupt mode, following handler need to be called. One can receive the data, either push it to a buffer for processing it later or enable a flag and then process it further in the while(1) loop.

void DEMO_LPUART_IRQHandler(void)
{
    uint8_t data;

    /* If new data arrived. */
    if ((kLPUART_RxDataRegFullFlag)&LPUART_GetStatusFlags(LPUART0))
    {
        data = LPUART_ReadByte(LPUART0);
        rxFlag = 1;
    }
    SDK_ISR_EXIT_BARRIER;
}


//Example show how rxFlag can be handled 
Main()
{

        while(1)
        {
          if(rxFlag ==1)
          {
              LPUART_WriteBlocking(LPUART0, "Data Received\r\n", 16);
              rxFlag =0;
          }
        }
}

This complete the UART interface on MCXA Microcontroller.

The example I created for testing UART0 basically transmits a power up message “Hello from LPUART0” and prints “Data Received” every time any byte is received on the UART0. See the serial terminal screen shot below.

Learn how to use UART on NXP MCXA Microcontroller 3

Short Video show UART communication on MCXA Microcontroller

This is how you use UART on MXCA Microcontroller, I hope it was easy to follow. My new blog will focus on I2C Master interface on MCXA Microcontroller. Stay tuned!


I am running an Embedded Design House, CAPUF Embedded Pvt. Ltd, located in Bangalore, India. At CAPUF, we help companies build embedded products with our hardware and firmware design services.

We also help in design optimizations for power consumption, cost, mass manufacturing, and performance. Additionally, we develop PCB testing jigs and provide cloud-based monitoring solutions.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.