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 an 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_expect[BUF_LENGTH] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13
};
static uint8_t buffer_rx[BUF_LENGTH] = {0x00};
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);
}
Add to user application main()
.
uint8_t result = 0;
configure_spi_slave();
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.
Use Case
Code
Add the following to your user application main()
.
}
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
- Read data from SPI master.
- 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--) {
}
}
}