Tuesday, September 23, 2014

PIC32 UART Transmission using DMA

Direct Memory Access  is a feature of computerized systems that allows certain hardware subsystems to access main system memory independently of the central processing unit (CPU).

You can read more about general DMA write-up here.


In my previous blog, i implemented PIC32 UART transmission using ring buffer and TXIF interrupt. Drawback of this approach is UART transmission still uses some processing time to transmit the data which is in the buffer. 


To overcome this we can use DMA controller of PIC32 which will take care of the data transmission over UART without disturbing the CPU.


PIC32 DMA Block:

PIC32 DMA Block
PIC32MX795F512L has 
  • 8 dedicated DMA channels for USB OTG, Ethernet, and CAN.
  • Channel General Hardware DMA Controller.


How DMA Works?

PIC32 DMA Source to Destination Transfer Diagram

DMA controller to work needs source address where data is parked waiting to be transmitted and destination address to where data to be transmitted. In this case, vUARTMTXFIFO is source location where data is parked and TXREG of UART module is the destination. 

UART is byte level serial protocol, i.e. it transmits one byte data serially at any given point of time. However, source buffer vUARTMTXFIFO is of 750 bytes in length.

 So, we will use TXIF interrupt of the UART module to trigger the DMA controller on one byte data is transmitted. When UART module generates this interrupt, DMA will increase its source address pointer and will copy the next byte to TXREG. This will continue till all the bytes are transmitted.


All the above process of transmitting data using DMA occurs without interrupting CPU. Once all the data is transmitted, DMA will generate interrupt to indicate CPU that it has finished its job.


Download the source code here.



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