I have a problem with SPI peripheral, strictly speaking, with the board configured as a SPI slave, which sends a data in response to incoming message from other board configured as a master.
Slave (STM32F407VG-Discovery):
- SPI initialization function:
void spi2_peripheral_config_init(void)
{
LL_SPI_InitTypeDef spi2_peripheral_config = {
.TransferDirection = LL_SPI_FULL_DUPLEX,
.Mode = LL_SPI_MODE_SLAVE,
.DataWidth = LL_SPI_DATAWIDTH_8BIT,
.ClockPolarity = LL_SPI_POLARITY_LOW,
.ClockPhase = LL_SPI_PHASE_1EDGE,
.NSS = LL_SPI_NSS_HARD_INPUT,
.BaudRate = LL_SPI_BAUDRATEPRESCALER_DIV2,
.BitOrder = LL_SPI_MSB_FIRST,
.CRCCalculation = LL_SPI_CRCCALCULATION_DISABLE,
.CRCPoly = 0x00
};
LL_SPI_Init(SPI2, &spi2_peripheral_config);
LL_SPI_Enable(SPI2);
}
- SPI send data function:
void spi2_send_data(uint8_t *tx_buffer, uint32_t bytes_to_send)
{
for (uint32_t index = 0; index < bytes_to_send; index++) {
while (LL_SPI_IsActiveFlag_TXE(SPI2) == 0) {
;
}
LL_SPI_TransmitData8(SPI2, tx_buffer[index]);
}
}
- SPI receive data function:
void spi2_receive_data(uint8_t *rx_buffer, uint32_t bytes_to_receive)
{
for (uint32_t index = 0; index < bytes_to_receive; index++) {
while (LL_SPI_IsActiveFlag_RXNE(SPI2) == 0) {
;
}
rx_buffer[index] = LL_SPI_ReceiveData8(SPI2);
}
}
Inside the main application, I have the following code, which waits for the incoming data from master:
char received_message[17] = {0};
spi2_receive_data((uint8_t *)received_message, 17);
while (strcmp(received_message, "Hello STM32F407!") != 0) {
;
}
status_led_turn_on();
uint8_t response_code = 0x55;
while (1) {
spi2_send_data(&response_code, 1);
}
This code works correctly, but if I would like to send out some data, i.e. Slave->Master, MISO line gets "weird", it is not able to send any data and if it is, it is total garbage. Below I attach pictures from USB logic analyzer:
I know that to get data from slave, the Master must provide the clock signal, which means that Master must send some dummy data to read the data from Slave.
But when I send one dummy byte from Master to Slave, MISO is not able to provide any feedback.
What can cause such a problem?