TMS320F28335 Library  1.0
Documentation
SPI

The TMS320F28335 provides one Serial Peripheral Interface (SPI) module. The device can operate in SPI-Master or in SPI-Slave mode. For the configuration of the SPI-module, user needs to create an instance of SPIInterface and assign the GPIOs of SPI-module to it (see table below).

User can also configure the length in bit to be sent and received by one message via SPI. The max. length is 16 bit.

To address different slaves, user has to configure different GPIOs as outputs and connect these pins to slave-select pins of slave. When starting communication with a slave, the respective GPIO output signal for this slave needs to be set to low. After communication, the respective GPIO output signal for this slave needs to be set to high again. The Slave-Select pin of SPI-Master is not connected in this case.

When communicating to a slave, master sends data via SIMO to the slave and equal amount of databits is sent back from slave to master via SOMI.

For data communication, there are 3 possibilities:

1) Master sends valid data to slave and gets back valid data from slave (valid communication in both ways)
2) Master sends dummy data to slave and gets back valid data from slave (valid communication from slave to master)
3) Master sends valid data to slave and gets back dummy data from slave (valid communication from master to slave)

--> Master only gets back data from slave (valid or not), when Master sends data to slave (valid or not)


Pin
Slave In Master Out (SIMO) GPIO16
Slave Out Master In (SOMI) GPIO17
Clock GPIO18
Slave-Select GPIO19

Code example

Configuration as SPI-Master without interrupt

#include "global_defines.h" //Include global_defines.h
#include "system_controls/system_controls.h" //Include module of system controls
#include "spi/spi.h" //Include module of SPI
SPIInterface SPI_A = {GPIO16, GPIO17, GPIO18, GPIO19, 16}; //Instantiate SPIInterface and assign GPIO16, GPIO17, GPIO18 and GPIO19 to SPI_A with a wordlength of 16 Bit
void main(void){
int spi_sdata = 0xF6F6;
int spi_rdata = 0;
int result = 0; //Optional: can be used to check return values of functions
setSystemControls(); //Call setSystemControls() function
result = init_SPIMasterMode(SPI_A, 1500000); //Init SPI_A with a baudrate of 1,5 MHz. Return result of operation
while(1){ //Endless loop
SPI_send(spi_sdata); //SPI send data to TX-FIFO
SPI_read(&spi_rdata); //SPI read data from RX-FIFO
}
}

Configuration as SPI-Master with interrupt

#include "global_defines.h" //Include global_defines.h
#include "system_controls/system_controls.h" //Include module of system controls
#include "spi/spi.h" //Include module of SPI
SPIInterface SPI_A = {GPIO16, GPIO17, GPIO18, GPIO19, 16}; //Instantiate SPIInterface and assign GPIO16, GPIO17, GPIO18 and GPIO19 to SPI_A with a wordlength of 16 Bit
__interrupt void spiTxFIFOISR(void); //Declare ISR for Tx
__interrupt void spiRxFIFOISR(void); //Declare ISR for Rx
Uint16 spi_sdata; //Send data
Uint16 spi_rdata; //Receive data
void main(void){
int result = 0; //Optional: can be used to check return values of functions
spi_rdata = 0;
spi_sdata = 0x1234; //Setup data for slave
setSystemControls(); //Call setSystemControls() function
result = init_SPIMasterMode(SPI_A, 1500000); //Init SPI_A with a baudrate of 1,5 MHz. Return result of operation
SPI_setTxInterrupt(&spiTxFIFOISR); //Assign Tx-ISR to SPI-module. Tx-ISR will be called when Tx-FIFO is empty. No return value
SPI_setRxInterrupt(&spiRxFIFOISR); //Assign Rx-ISR to SPI-module. Rx-ISR will be called when there are one or more words (word = 16 Bit) in Rx-FIFO. No return value
while(1){
//Endless loop
}
}
__interrupt void spiTxFIFOISR(void){
SPI_send(spi_sdata); //SPI send data to TX-FIFO - data will be sent to slave
}
__interrupt void spiRxFIFOISR(void){
SPI_read(&spi_rdata); //SPI read data from RX-FIFO
}

Configuration as SPI-Slave without interrupt

#include "global_defines.h" //Include global_defines.h
#include "system_controls/system_controls.h" //Include module of system controls
#include "spi/spi.h" //Include module of SPI
SPIInterface SPI_A = {GPIO16, GPIO17, GPIO18, GPIO19, 16}; //Instantiate SPIInterface and assign GPIO16, GPIO17, GPIO18 and GPIO19 to SPI_A with a wordlength of 16 Bit
void main(void){
int spi_sdata = 0xF6F6;
int spi_rdata = 0;
int result = 0; //Optional: can be used to check return values of functions
setSystemControls(); //Call setSystemControls() function
result = init_SPISlaveMode(SPI_A); //Init SPI_A. Return result of operation
while(1){ //Endless Loop
SPI_send(spi_sdata); //SPI send data to TX-FIFO. Data will only be shifted out when master sends data to slave
SPI_read(&spi_rdata); //SPI read data from RX-FIFO
}
}

Configuration as SPI-Slave with interrupt

#include "global_defines.h" //Include global_defines.h
#include "system_controls/system_controls.h" //Include module of system controls
#include "spi/spi.h" //Include module of SPI
SPIInterface SPI_A = {GPIO16, GPIO17, GPIO18, GPIO19, 16}; //Instantiate SPIInterface and assign GPIO16, GPIO17, GPIO18 and GPIO19 to SPI_A with a wordlength of 16 Bit
__interrupt void spiTxFIFOISR(void); //Declare ISR for Tx
__interrupt void spiRxFIFOISR(void); //Declare ISR for Rx
Uint16 spi_sdata; //Send data
Uint16 spi_rdata; //Receive data
void main(void){
int result = 0; //Optional: can be used to check return values of functions
spi_rdata = 0;
spi_sdata = 0x1234; //Setup data for slave
setSystemControls(); //Call setSystemControls() function
result = init_SPISlaveMode(SPI_A, 1500000); //Init SPI_A with a baudrate of 1,5 MHz. Return result of operation
SPI_setTxInterrupt(&spiTxFIFOISR); //Assign Tx-ISR to SPI-module. Tx-ISR will be called when Tx-FIFO is empty. No return value
SPI_setRxInterrupt(&spiRxFIFOISR); //Assign Rx-ISR to SPI-module. Rx-ISR will be called when there are one or more words (word = 16 Bit) in Rx-FIFO. No return value
while(1){
//Endless loop
}
}
__interrupt void spiTxFIFOISR(void){
SPI_send(spi_sdata); //SPI send data to TX-FIFO - data will only be shifted out when master sends data
}
__interrupt void spiRxFIFOISR(void){
SPI_read(&spi_rdata); //SPI read data from RX-FIFO
}

Documented C-Code of module:

SPI


Detailed description in Technical Reference Manual:

Technical_Reference_Manual

SPI_setTxInterrupt
void SPI_setTxInterrupt(void *spiTxFIFOISR)
Configuration of SPI Tx ISR.
Definition: spi.c:226
spi.h
Header file for SPI module.
init_SPISlaveMode
int16 init_SPISlaveMode(SPIInterface SPI_Int)
Initialization of SPI Interface for slave mode.
Definition: spi.c:127
SPI_clearRxInterruptFlag
void SPI_clearRxInterruptFlag(void)
Clear Interrupt Flag for receive operation.
Definition: spi.c:289
SPI_send
void SPI_send(Uint16 sdata)
Send data via SPI.
Definition: spi.c:258
init_SPIMasterMode
int16 init_SPIMasterMode(SPIInterface SPI_Int, Uint32 baudrate)
Initialization of SPI Interface for Master mode.
Definition: spi.c:22
global_defines.h
Header file for global defines.
setSystemControls
void setSystemControls(void)
Configuration of system control register.
Definition: system_controls.c:13
SPI_setRxInterrupt
void SPI_setRxInterrupt(void *spiRxFIFOISR)
Configuration of SPI Rx ISR.
Definition: spi.c:242
Uint16
unsigned int Uint16
16 Bit Variable: 0 .. 65.535
Definition: global_defines.h:21
SPI_clearTxInterruptFlag
void SPI_clearTxInterruptFlag()
Clear Interrupt Flag for transmit operation.
Definition: spi.c:298
SPI_read
void SPI_read(Uint16 *rdata)
Read Rx-data of SPI module.
Definition: spi.c:269
_SPIInterface
SPI Interface: Assign GPIOs to SPI module.
Definition: spi.h:12
system_controls.h
Header file for System Controls module.
SPI_interruptAck
void SPI_interruptAck()
Giving interrupts acknowledge to interrupt again.
Definition: spi.c:280