CCS News

Tech Note: New Message based SPI functions in the CCS C Compiler!

Thursday 29 August, 2019

The CCS C Compiler has added three new functions to the #use spi() library when setup as an SPI master for transferring data to and from a SPI slave device. The functions are the spi_transfer(), spi_transfer_out() and spi_transfer_in() functions. These functions are available for all devices when setup as a SPI master using the hardware SSPx or SPIx peripherals or when doing a software SPI. One advantage of using the spi_transfer() functions is that all the steps for reading or writing data to and from an SPI slave device can be done in a single call to one of the functions instead of multiple calls to the spi_xfer() function. Additionally, if an enable pin is specified in #use spi(), it will set the enable pin to the active state at the start of the transfer, transfer all the bytes, and then set the enable pin to the inactive state at the end of the transfer, instead of toggling the enable pin between the active and inactive states between each call to spi_xfer().

The spi_transfer_out() function transfers data to a slave device and is used as follows:

spi_transfer_out(wData, Count);

The above will cause the master to set the enable pin to the active state, if the enable pin was specified. It will then write count bytes to the slave, and set the enable pin to the inactive state, if enable pin was specified.

The spi_transfer_in() function transfers data from a slave device and is used as follows:

spi_transfer_in(rData, Count);

The above will cause the master to set the enable pin to the active state, if enable pin was specified, read count bytes from slave, and set the enable pin to the inactive state, if enable pin was specified. When using the spi_transfer_in() function it will output a low on the SPI DO pin while the data is being clocked in from the slave device.

The spi_transfer() function simultaneously transfers data to and from a slave device and is used as follows:

spi_transfer(wData, rData, Count);

The above will cause the master to set the enable pin to the active state, if enable pin was specified, write and read count bytes to and from the slave, and set the enable pin to the inactive state, if enable pin was specified.

See 9356.c in the Drivers folder where the CCS C compiler was installed for an example showing how to use the spi_transfer() function. The 9356.c driver is for a Microchip 93C56 Serial EEPROM with 2K bits of memory.

Additionally for devices that have a Direct Memory Access (DMA) peripheral, #use spi() has been updated so that DMA can be used with the spi_transfer() functions when using the hardware SSPx or SPIx peripheral to transfer data to and from SSPx or SPIx peripheral. Using the option USE_DMA in #use spi() will enable using DMA for the spi_transfer() functions, and the options DMA_RX_CHANNEL and DMA_TX_CHANNEL can be used in #use spi() to set the DMA RX and TX channels to something other then the default channels. By default the library uses DMA channel 0 to receive data and DMA channel 1 to transmit data in the PCD compiler. In the PCH compiler, the library uses DMA channel 1 to receive data and DMA channel 2 to transmit data.

When DMA is used, the spi_transfer() functions will not stop code execution while waiting for the spi_transfer() functions to finish the transfer. This is an advantage because it allows the PIC to execute other code while the DMA and SPI peripherals are transferring data to and from the SPI slave. To determine when the SPI transfer has been completed, the function spi_transfer_done() was add to the #use spi() library. Additionally the spi_transfer_clear() was also added the the #use spi() library to clear the interrupt flag used to determine when the transfer is complete. These functions check and clear the interrupt flag for the DMA channel used for receiving from the SPI peripheral. The PCD compiler uses the DMAx interrupt flag and the PCH compiler uses the DMAxDCNT interrupt flag.

When using the USE_DMA option the enable=PIN_xx option is not supported, by the #use spi() library, except for device that have a hardware SSxOUT pin and the device is set up to use the hardware enable pin. In that case, the spi_transfer() function will control the enable pin when the USE_DMA option is used. A PIC18F47K42 is an example of the PIC® that has a hardware controllable enable pin for the SPI peripheral.


Like us on Facebook. Follow us on Twitter.

About CCS:

CCS is a leading worldwide supplier of embedded software development tools that enable companies to develop premium products based on Microchip PIC® MCU and dsPIC® DSC devices. Complete proven tool chains from CCS include a code optimizing C compiler, application specific hardware platforms and software development kits. CCS' products accelerate development of energy saving industrial automation, wireless and wired communication, automotive, medical device and consumer product applications. Established in 1992, CCS is a Microchip Premier 3rd Party Partner. For more information, please visit http://www.ccsinfo.com.

PIC® MCU, MPLAB® IDE, MPLAB® ICD2, MPLAB® ICD3 and dsPIC® are registered trademarks of Microchip Technology Inc. in the U.S. and other countries.