Buffer

A First-In First-Out (FIFO) buffer system is employed for commands to ensure that the PIC does not need to wait on the computer for additional commands and always has a supply of commands to execute. Two DMA modules are employed in the buffer system, as shown in Figure 4-1. The first block (the input DMA) copies data from the UART to the FIFO each time the UART reports receipt of data. The second block (the reader DMA) copies data from the FIFO to a dedicated memory location, as it is requested by the program.
Figure 1. Buffer Configuration

The UART is configured using MPLABĀ® Code Configurator (MCC) as a standard 8-bit transmission, with no parity bit and a baud rate of 115200. Communication to the Python program was handled using the standard UART1_Write() function generated by MCC, which transmits the byte passed to it over UART.

While a UART is used in this application, virtually any CIP-based communication protocol could be used to feed the FIFO. The only requirement is that data is transferred a byte at a time and that the peripheral can trigger the DMA on receipt of data. For example, CAN, I2C or reading from an SD card could all replace the UART in this application, with no changes to other program functionality.

Figure 2. Buffer Operation

Each transmission from the Python program sends enough data to fill half of the FIFO. This means that at any time, half of the FIFO can be safely read from by the reader DMA while the other half is written to by the input DMA. For example, if 10 instruction spots are allocated for the buffer, five instructions at a time will be transmitted by the Python program.

Following the logic shown in Figure 4-2, each time new data is requested the number of remaining instructions is decremented. Once there are no remaining instructions in that half of the FIFO, the reader DMA moves on to the other section and 0xAA is sent via UART to the Python program, requesting the next instructions. As the reader DMA continues to copy from the FIFO, the Python program sends the new instructions via DMA. The input DMA then copies this new data to the old section of the FIFO buffer.