iLLD_TC27xC  1.0
How to use the ASCLIN SPI Interface driver?
Collaboration diagram for How to use the ASCLIN SPI Interface driver?:

The SPI interface driver provides a default ASCLIN configuration for synchronous serial communication in 8 and 16 bit mode, and a set of data transfer routines.

Data transfers are buffered by the hardware based FIFOs. Incoming and outgoing data is transfered in background from/to the ASCLIN peripheral by interrupt service handlers, which are part of this driver as well. This allows a nonblocking communication without stalling the thread(s) from where data is sent and received.

The SPI interface driver works only as Master

In the following sections it will be described, how to integrate the driver into the application framework.

Preparation

Include Files

Include following header file into your C code:

Variables

Declare the Spi handle as global variable in your C code:

// used globally
static IfxAsclin_Spi spi;

In addition, declare global transmit and receive buffers for the data transfers:

#define SPI_BUFFER_SIZE 8
uint8 spiTxBuffer[SPI_BUFFER_SIZE];
uint8 spiRxBuffer[SPI_BUFFER_SIZE];

Interrupt Handler Installation

See also How to define Interrupts?

Define priorities for the Interrrupt handlers. This is normally done in the Ifx_IntPrioDef.h file:

// priorities are normally defined in Ifx_IntPrioDef.h
#define IFX_INTPRIO_ASCLIN1_TX 1
#define IFX_INTPRIO_ASCLIN1_RX 2
#define IFX_INTPRIO_ASCLIN1_ER 3

Add the interrupt service routines to your C code. They have to call the SPI interrupt handlers by passing the spi handle:

IFX_INTERRUPT(asclin1TxISR, 0, IFX_INTPRIO_ASCLIN1_TX)
{
}
IFX_INTERRUPT(asclin1RxISR, 0, IFX_INTPRIO_ASCLIN1_RX)
{
}
IFX_INTERRUPT(asclin1ErISR, 0, IFX_INTPRIO_ASCLIN1_ER)
{
}

Finally install the interrupt handlers in your initialisation function:

// install interrupt handlers
IfxCpu_Irq_installInterruptHandler(&asclin1TxISR, IFX_INTPRIO_ASCLIN1_TX);
IfxCpu_Irq_installInterruptHandler(&asclin1RxISR, IFX_INTPRIO_ASCLIN1_RX);
IfxCpu_Irq_installInterruptHandler(&asclin1ErISR, IFX_INTPRIO_ASCLIN1_ER);

Module Initialisation

The module initialisation can be done in the same function. Here an example:

// create module config
IfxAsclin_Spi_initModuleConfig(&spiConfig, &MODULE_ASCLIN1);
// set the desired baudrate
spiConfig.baudrate.prescaler = 1;
spiConfig.baudrate.baudrate = 1000000; // FDR values will be calculated in initModule
// ISR priorities and interrupt target
spiConfig.interrupt.txPriority = IFX_INTPRIO_ASCLIN1_TX; // see also \ref IfxLld_Asclin
spiConfig.interrupt.rxPriority = IFX_INTPRIO_ASCLIN1_RX; // see also \ref IfxLld_Asclin
spiConfig.interrupt.erPriority = IFX_INTPRIO_ASCLIN1_ER; // see also \ref IfxLld_Asclin
// pin configuration
const IfxAsclin_Spi_Pins pins = {
};
spiConfig.pins = &pins;
// initialize module
//IfxAsclin_Spi spi; // defined globally
IfxAsclin_Spi_initModule(&spi, &spiConfig);

The SPI is ready for use now!

Data Transfers

In following examples we assume, that following buffers are declared globally:

uint8 spiTxBuffer[SPI_BUFFER_SIZE] = { 1, 2, 3, 4, 5, 6, 7, 8 };
uint8 spiRxBuffer[SPI_BUFFER_SIZE] = { 0, 0, 0, 0, 0, 0, 0, 0 };

Sending and Receiving a data stream:

// wait until transfer of previous data stream is finished
// send/receive new stream
IfxAsclin_Spi_exchange(&spi, spiTxBuffer, spiRxBuffer, 8);

Send only, discard received data:

// wait until transfer of previous data stream is finished
// send new stream
IfxAsclin_Spi_exchange(&spi, &spiTxBuffer[i], NULL_PTR, 8);

Receive only, send all 1's

// wait until transfer of previous data stream is finished
// receive new stream
IfxAsclin_Spi_exchange(&spi, NULL_PTR, &spiRxBuffer[i], 8);