[MCX A153] LPUART Introduction Guide (Japanese Blog) This article explains the basic concepts of LPUART ( Low Power Universal Asynchronous Receiver / Transmitter ) on the MCX A153 and how to use it with the MCUXpresso SDK, aimed at engineers who are new to NXP products. First, we will explain the hardware overview, and then, using the FRDM-MCXA153 board as a base, we will explain how to use the MCUXpresso SDK API with code snippets.
(Reading time: 20 minutes)
1. LPUART Module Overview
LPUART is a module for implementing asynchronous serial communication. Here, we will look at its key features in order.
Asynchronous communication (start bit/stop bit)
LPUART does not use a clock line; instead, it determines timing using start and stop bits included in the data. Because it only requires two wires, TX and RX, it is widely used for easily connecting different devices, such as communicating with a PC, connecting to sensors, and outputting debug logs.
Interrupt/DMA compatible
LPUART features a mechanism where hardware automatically assists with data transfer, rather than the CPU processing data byte by byte. Using interrupts, the CPU only processes data when an event occurs, and using DMA, data can be transferred to and from memory without CPU intervention. Because it's designed with DMA integration in mind, CPU load can be minimized even during high-speed communication.
Built-in FIFO (buffer)
LPUART has an internal buffer for sending and receiving data, also known as a FIFO. Because it can temporarily store data, it can absorb the difference between the communication speed and the processing speed of the CPU or DMA. When combined with DMA, it makes it easier to achieve uninterrupted, continuous communication.
Low power operation
Designed to operate with lower power consumption than a standard UART, it can sometimes continue operating even while in sleep mode. Because it works closely with clock control, it is particularly useful for battery-powered devices and IoT applications.
Supports multiple instances
The MCU has multiple LPUARTs, allowing for simultaneous communication of multiple devices. This enables you to separate roles for each instance, such as separating debugging and sensor communication.
2. Software Configuration (MCUXpresso SDK)
The MCUXpresso SDK provides three types of drivers for handling LPUART: polling, interrupt, and eDMA.
In practical applications, interrupt-based communication is sufficient for small-scale communications, but eDMA is used for high-speed, continuous communications. For applications where you don't want to burden the CPU, designing with eDMA in mind from the start will minimize future changes. Therefore, this article will proceed on the assumption that DMA is being used.
3. The role of FIFO and DMA
To use DMA correctly, it's essential to first understand how FIFOs work. LPUART has an internal FIFO (First-In First-Out buffer) that plays the role of temporarily holding transmitted data, temporarily storing received data, and absorbing the speed difference that occurs between LPUART and DMA.
In DMA integration, on the receiving side, data received by the LPUART is stored in the RX FIFO, and when a threshold is reached, the DMA transfers the data from the RX FIFO to memory. On the transmitting side, the DMA writes data from memory to the TX FIFO, and the LPUART sequentially retrieves and transmits the data from the TX FIFO. This mechanism allows for continuous data transfer on the hardware side without waiting for the DMA transfer to complete, enabling continuous communication without interrupting throughput.
The operation of DMA can be illustrated as follows:
When using LPUART with DMA, you need to configure the channel MUX to determine which eDMA channel the LPUART's TX/RX requests should be routed through.
In the MCX A153, there is no separate DMAMUX peripheral as seen in other families. Instead, each channel of the eDMA controller has a built-in channel MUX (CHn_MUX register) that selects which peripheral function's DMA request source it will accept. The SDK assigns request sources to each channel using the EDMA_SetChannelMux() API (the specific calling method is described in Chapter 4). The channel number is not fixed and can be freely switched depending on the application's use.
The diagram below illustrates how multiple DMA request sources on the peripheral function side are connected to the channel body via a channel MUX selector built into each eDMA channel.
In the diagram above, the solid lines show an example where LPUART0 TX is assigned to Channel 0 and LPUART0 RX to Channel 1 using EDMA_SetChannelMux . The dotted lines represent available connection options, and by rewriting the arguments when calling the API, it is possible to change other assignments, such as assigning LPUART0 TX to Channel 2.
4. Basic Flow of SDK Drivers
Next, we will demonstrate how to use the driver using the MCUXpresso SDK sample project "lpuart_edma_transfer" as an example.
Reference URL:
Hardware requirements — MCUXpresso SDK Documentation
mcuxsdk-examples/driver_examples/lpuart/edma_transfer/lpuart_edma_transfer.c
The following seven steps are the basic steps when using LPUART and DMA together:
Enable the clock.
Configure pin MUX
Initialize the LPUART.
Configure the eDMA channel MUX.
Generate a DMA handle
Start sending/receiving
The next process is controlled by the callback.
In the following sections, we will explain each step in detail, including code snippets.
4.1 Clock Port Settings
First, enable the clock on the LPUART module.
CLOCK_EnableClock(kCLOCK_Lpuart0);
Next, we configure the MUX pins used for TX and RX.
PORT_SetPinMux(PORTA, 1U, kPORT_MuxAlt2); // TX
PORT_SetPinMux(PORTA, 2U, kPORT_MuxAlt2); // RX
Aside from GND, the UART wiring consists of only two wires: transmit and receive. In the FRDM-MCXA153 circuit diagram, the TXD and RXD of LPUART0 are connected to the MCU-LINK's Virtual COM, and by connecting the MCU-Link to a PC via USB, debugging via UART communication becomes possible.
4.2 LPUART initialization
Initializing the LPUART involves first obtaining the default settings and then modifying the necessary items.
lpuart_config_t config;
LPUART_GetDefaultConfig(&config);
config.baudRate_Bps = 115200;
config.enableTx = true;
config.enableRx = true;
LPUART_Init(LPUART0, &config, CLOCK_GetFreq(kCLOCK_CoreSysClk));
The baud rate depends on the clock frequency supplied to the LPUART, so you need to pass the correct clock source frequency in CLOCK_GetFreq() . Enable TX and RX as needed.
4.3 eDMA Channel MUX Settings
Before generating the handle, you specify with EDMA_SetChannelMux which eDMA channel to route the LPUART TX/RX requests to. As explained in Chapter 3, the MCX A153 does not have an independent DMAMUX, and this API sets the request source in the CHn_MUX register for each channel.
/* LPUART0 TX を eDMA の任意チャネル(例: CH0)に割り当てる */
EDMA_SetChannelMux(DMA0, LPUART_TX_DMA_CHANNEL, kDma0RequestMuxLPUART0Tx);
/* LPUART0 RX を eDMA の任意チャネル(例: CH1)に割り当てる */
EDMA_SetChannelMux(DMA0, LPUART_RX_DMA_CHANNEL, kDma0RequestMuxLPUART0Rx);
The channel number specified here must match the channel number passed to txEdmaHandle / rxEdmaHandle when generating the subsequent DMA handle.
4.4 DMA Handle Generation
A handle is created to link LPUART and DMA, and a callback function is registered here to be called when communication is complete.
lpuart_edma_handle_t g_lpuartEdmaHandle;
LPUART_TransferCreateHandleEDMA(LPUART0, &g_lpuartEdmaHandle, lpuart_callback, userData, &txEdmaHandle, &rxEdmaHandle);
This handle associates the LPUART and DMA TX/RX channels, which are then used by subsequent transmit and receive APIs.
4.5 Transmission (DMA)
Sending is as simple as calling the API with the send buffer and size set to lpuart_transfer_t .
lpuart_transfer_t xfer;
xfer.data = txBuffer;
xfer.dataSize = txSize;
LPUART_TransferSendEDMA(LPUART0, &g_lpuartEdmaHandle, &xfer);
4.6 Receiving (DMA)
Similarly, receiving is also done by specifying the receive buffer and size when calling the function.
lpuart_transfer_t xfer;
xfer.data = rxBuffer;
xfer.dataSize = rxSize;
LPUART_TransferReceiveEDMA(LPUART0, &g_lpuartEdmaHandle, &xfer);
4.7 Callback Implementation
The application is notified of the completion of the communication via a callback function.
void lpuart_callback(LPUART_Type *base,
lpuart_edma_handle_t *handle,
status_t status,
void *userData)
{
if (status == kStatus_LPUART_TxIdle)
{
// 送信完了処理
}
else if (status == kStatus_LPUART_RxIdle)
{
// 受信完了処理
}
}
kStatus_LPUART_TxIdle kStatus_LPUART_RxIdle indicates completion of transmission via DMA, and indicates completion of reception via DMA. If you want to continue receiving continuously, you can call LPUART_TransferReceiveEDMA() again within this callback to continue receiving without interruption.
4.8 Key Design Points
When actually using LPUART and DMA in combination, there are several design considerations that you should keep in mind.
First, let's consider the transfer size . If the transfer size is too small, the number of interrupts will increase, reducing CPU efficiency. If it's too large, the latency until the transfer is complete will be high. You need to choose an appropriate value that matches the application's requirements while keeping the FIFO size in mind.
Next is the design of the receive buffer . If the receive length is predetermined, a fixed-length buffer is sufficient, but if you are continuously receiving data of an unspecified length, it is common to use a ring buffer.
Finally, there's the callback process . Callbacks not only notify the application that the transfer is complete, but they also trigger the reconfiguration of the next DMA transfer. Implementing this part is the most crucial for ensuring stable continuous communication.
5. Common problems
5.1 DMA is not working
Possible causes include the DMA clock not being enabled, the request source specified in EDMA_SetChannelMux not matching the LPUART's TX/RX, or the channel number passed to the LPUART driver not matching the channel number set in EDMA_SetChannelMux . The quickest solution is to first enable the clock and check that the channel MUX assignments match.
5.2 Data Loss
Typical issues include FIFO overruns and transfer size settings that don't match the actual data volume. Review the relationship between the FIFO threshold and the DMA transfer unit.
5.3 Failure due to high-speed communication
As the baud rate increases, factors such as FIFO underrun, insufficient buffer design, degraded waveform quality, and clock errors become apparent. It is necessary to isolate the cause from both the hardware-side signal quality and the software-side buffer supply capability.
6. Summary
The most important thing for effectively using LPUART in practice is to correctly understand the role of FIFO and design the data buffer in consideration of its interaction with DMA. By keeping these points in mind, you can achieve stable UART communication with low power consumption and low CPU load.
=========================
We are currently unable to respond to comments left in the "Comment" section of this post. We apologize for the inconvenience, but please refer to " Technical Questions to NXP - How to Contact Us (Japanese Blog) " when making inquiries. (If you are already an NXP distributor or have a relationship with NXP, you may contact your representative directly.) This article explains the basic concepts of LPUART (Low Power Universal Asynchronous Receiver/Transmitter) on the MCX A153 and how to use it with the MCUXpresso SDK, aimed at engineers who are new to NXP products. First, we will explain the hardware overview, and then, using the FRDM-MCXA153 board as a base, we will explain how to use the MCUXpresso SDK API with code snippets.
(Reading time: 20 minutes) MCUXpresso SDK MCX Japanese Blog
View full article