cancel
Showing results for 
Search instead for 
Did you mean: 

SPI data register (SPI_DR) on STM32CubeMX ?

antonius
Senior
Posted on January 18, 2016 at 15:30

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
13 REPLIES 13
Posted on January 20, 2016 at 00:44

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()

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
antonius
Senior
Posted on January 20, 2016 at 10:41

From my understanding the function is sending a command then receive a result from that command,

Correct me if I'm wrong ?

Thanks

antonius
Senior
Posted on January 20, 2016 at 10:42

From my understanding the function is sending a command then receive a result from that command,

Correct me if I'm wrong ?

Thanks

antonius
Senior
Posted on January 20, 2016 at 14:40

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; }