In this use case, the SPI on extension header 1 of the Xplained Pro board will configured with the following settings:
- Slave mode enabled
- Preloading of shift register enabled
- MSB of the data is transmitted first
- Transfer mode 0
- SPI MUX Setting E (see Slave Mode Settings)
- 8-bit character size
- Not enabled in sleep mode
- GLCK generator 0
Setup
Prerequisites
The device must be connected to a SPI master, which must read from the device.
Code
The following must be added to the user application source file, outside any functions.
A sample buffer to send via SPI.
static uint8_t buffer_rx[BUF_LENGTH] = {0x00,};
static uint8_t buffer_expect[BUF_LENGTH] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13
};
Number of entries in the sample buffer.
A globally available software device instance struct to store the SPI driver state while it is in use.
A function for configuring the SPI.
void configure_spi_slave(void)
{
config_spi_slave.mode_specific.slave.preload_enable = true;
config_spi_slave.mux_setting = CONF_SLAVE_MUX_SETTING;
config_spi_slave.pinmux_pad0 = CONF_SLAVE_PINMUX_PAD0;
config_spi_slave.pinmux_pad1 = CONF_SLAVE_PINMUX_PAD1;
config_spi_slave.pinmux_pad2 = CONF_SLAVE_PINMUX_PAD2;
config_spi_slave.pinmux_pad3 = CONF_SLAVE_PINMUX_PAD3;
spi_init(&spi_slave_instance, CONF_SLAVE_SPI_MODULE, &config_spi_slave);
}
A function for configuring the callback functionality of the SPI.
void configure_spi_slave_callbacks(void)
{
}
A global variable that can flag to the application that the buffer has been transferred.
volatile bool transfer_complete_spi_slave = false;
Callback function.
static void spi_slave_callback(
struct spi_module *
const module)
{
transfer_complete_spi_slave = true;
}
Add to user application main()
.
uint8_t result = 0;
configure_spi_slave();
configure_spi_slave_callbacks();
Workflow
- Initialize system.
- Set-up the SPI.
- Create configuration struct.
- Get default configuration to edit.
- Set the SPI in slave mode.
- Enable preloading of shift register.
config_spi_slave.mode_specific.slave.preload_enable = true;
- Set frame format to SPI frame.
- Set MUX setting E.
config_spi_slave.mux_setting = CONF_SLAVE_MUX_SETTING;
- Set pinmux for pad 0 (data in MOSI).
- Set pinmux for pad 1 (slave select).
- Set pinmux for pad 2 (data out MISO).
- Set pinmux for pad 3 (SCK).
- Initialize SPI module with configuration.
spi_init(&spi_slave_instance, CONF_SLAVE_SPI_MODULE, &config_spi_slave);
- Enable SPI module.
- Setup of the callback functionality.
configure_spi_slave_callbacks();
- Register callback function for buffer transmitted.
- Enable callback for buffer transmitted.
Use Case
Code
Add the following to your user application main()
.
while(!transfer_complete_spi_slave) {
}
for (uint8_t i = 0; i < BUF_LENGTH; i++) {
if(buffer_rx[i] != buffer_expect[i]) {
result++;
}
}
while (true) {
if (result) {
volatile uint32_t delay = 30000;
while(delay--) {
}
} else {
volatile uint32_t delay = 600000;
while(delay--) {
}
}
}
Workflow
- Initiate a read buffer job.
- Wait for the transfer to be complete.
while(!transfer_complete_spi_slave) {
}
- Compare the received data with the transmitted data from SPI master.
for (uint8_t i = 0; i < BUF_LENGTH; i++) {
if(buffer_rx[i] != buffer_expect[i]) {
result++;
}
}
- Infinite loop. If the data is matched, LED0 will flash slowly. Otherwise, LED will flash quickly.
while (true) {
if (result) {
volatile uint32_t delay = 30000;
while(delay--) {
}
} else {
volatile uint32_t delay = 600000;
while(delay--) {
}
}
}
Callback
When the buffer is successfully transmitted from the master, the callback function will be called.
Workflow
- Let the application know that the buffer is transmitted by setting the global variable to true.
transfer_complete_spi_slave = true;