Saturday, September 6, 2014

PIC32 UART Transmission using Tx Interrupt & Ring Buffer

UART - Universal Asynchronous Receiver Transmitter is a very basic serial transmission protocol used widely in embedded space.
Even though UART is a a very basic peer-to-peer communication protocol, it will be the first protocol in any embedded developers carrier.

Let's not get into basics of UART, you can find the UART basics here and get on with the PIC32 UART coding.

PIC32MX795F512L has 8 level deep transmit FIFO.
PIC32MX795F512L UART Transmission FIFO Block

In general for UART blocking logic is implemented. During the entire string transmission the code waits there itself blocking other logic of the code.

To over come this we can have buffered UART transmission with the help of UART transmission interrupt.

In this approach i have used vUARTMTXFIFO buffer, 750 byte in length and MTXHeadPtrMTXTailPtr base pointers.

PutStrUART functions is used to copy the data to buffer with the help of shadow pointers MTXHeadPtrShadow and MTXTailPtrShadow.

Shadow pointers are used so that we don't disturb the base pointers if already transmission is under progress.

Once the data is buffered, UART TXIF interrupt is turned on, in this case it is U3TXIE.

PIC32 UART transmission interrupt will set when TXREG is empty and code execution jumps to interrupt vector.

In interrupt vector, MTXHeadPtr & MTXTailPtr pointer is compared and if both are not same next byte is copied to U3TXREGMTXTailPtr is incremented, code execution returns from interrupt.

If MTXHeadPtr & MTXTailPtr pointing to same location then UART transmission is terminated by clearing TXIE.

Advantage of this logic:
Super loop will not be blocked for whole string transmission.

Download the source code here


No comments: