Implementing an SPI slave mechanism with STM32 using cube and HAL
Hello all,
I'm trying to implement an SPI slave using stm32 u-controler. the master is as an FPGA.
so i want to be able to read and write different registers in the micro.
the FPGA generates a repeating read command in the next structure (there is also some i2c in the system, that is why the msg is a bit similar to i2c): read\write command, register, B0,B1,B2,B3,B4.
the slave will be ready for spi communication and when he get a msg on the line it would receive the initial 2 bytes to understand weather this is read or write, and when they are completed it would immediately continue to receive another 5 bytes (if write msg) or will send to master the data of the register (if read msg). slave micro runs in 32Mhz, and SPI CLK sent by master is 2Mhz. so i have high confidence that the slave would be able to decide what would be the next action in time to receive\transmit the next 5 bytes.
The problem is that the slave respond to the read command only at the following read command and not immediately...
could any one assist me?
attaching the relevant code:
in my main:
HAL_SPI_Receive_IT(s, (uint8_t *)&RX_buf, 2);
once completed is used the HAL_SPI_RxCpltCallback to continue (sorry for the partly code here, tried to spare the small details):
if (read)
HAL_SPI_Transmit(SPI_PROC,(uint8_t *)&TX_buf,5,1000);
// enabling SPI bus for the next transmition
HAL_SPI_Receive_IT(SPI_PROC, (uint8_t *)&RX_buf, 2);
if (write)
HAL_SPI_Transmit(SPI_PROC,(uint8_t *)&RX_buf,5,1000);
// enabling SPI bus for the next transmition
HAL_SPI_Receive_IT(SPI_PROC, (uint8_t *)&RX_buf, 2);
had also some troubles with the call back not being entered sometimes by i made some work to bypass it so that i could focus on the read problem...
any help would be appreciated
thanks
Edo
