2016-01-18 06:30 AM
Guys,
Which API should I use for reading the value of SPI data register (SPI_DR) ? How can I differentiate between data from SPI1 and SPI2 ? I tried to port from AVR to STM32CubeMX...please have a look and correct me if I'm wrong :STM32 :
/* Exchange a byte */
static
BYTE xchg_spi ( /* Returns received data */
BYTE dat[1] /* Data to be sent */
)
{
while(HAL_SPI_GetState(&hspi2) != HAL_SPI_STATE_READY);
HAL_SPI_Receive(&hspi2,dat,1,100);
return dat[1];
}
AVR :
/* Exchange a byte */
static
BYTE xchg_spi ( /* Returns received data */
BYTE dat /* Data to be sent */
)
{
SPDR = dat;
loop_until_bit_is_set(SPSR, SPIF);
return SPDR;
}
Thanks
2016-01-19 03:44 PM
With my limited understanding of the HAL/Cube software, I don't think the function you created transmits the data passed to it.
Would this be more appropriate?HAL_SPI_TransmitReceive()2016-01-20 01:41 AM
From my understanding the function is sending a command then receive a result from that command,
Correct me if I'm wrong ? Thanks2016-01-20 01:42 AM
From my understanding the function is sending a command then receive a result from that command,
Correct me if I'm wrong ? Thanks2016-01-20 05:40 AM
Do you reckon like this one ?
/* Exchange a byte */ static BYTE xchg_spi ( /* Returns received data */ BYTE dat /* Data to be sent */ ) { uint8_t result = 0; while(HAL_SPI_GetState(&hspi2) != HAL_SPI_STATE_READY); HAL_SPI_TransmitReceive(&hspi2,&dat,&result,1,1000); return result; }