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 18, 2016 at 15:44

return dat[1];

// Surely not in [1], but rather [0] ??
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 18, 2016 at 23:00

so

return dat[0]; ? on the top :

BYTE xchg_spi ( /* Returns received data */
BYTE dat[0] 
 as well ?

Posted on January 19, 2016 at 02:28

No, that's not the syntax for parameters, there you are defining the size of the vector, the first element is at index 0.

For a parameter passed as BYTE vector[3], the three elements inside would be vector[0], vector[1] and vector[2]. This is just as it would be any type of array, review C language texts.

For a parameter passed as BYTE dat, you could passed it down as a pointer/address using &dat

Your AVR example seems to send data, and then return the received data, your HAL code doesn't seem to do that. Can't really help you with the HAL stuff, you should perhaps review some SPI FLASH examples

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 19, 2016 at 03:11

antonius
Senior
Posted on January 19, 2016 at 09:58

do you know where I can find SPI implementation with FATFs for STM32 ?

I can't find for STM32F107, FatFS and SPI.....??

Or I rewrite the code from AVR to STM32, since it's working on AVR, only I don't know yet on how to implement them on STM32F107....

I tried with STM32CubeMX....but trying adapting with HAL API...

Thanks

antonius
Senior
Posted on January 19, 2016 at 10:02

Do you mean like this?

STM32 :

/* Exchange a byte */ static BYTE xchg_spi ( /* Returns received data */ BYTE dat[] /* Data to be sent */ ) { while(HAL_SPI_GetState(&hspi2) != HAL_SPI_STATE_READY); HAL_SPI_Transmit(&hspi2,dat,1,100); return dat[0]; } This one is compileable, but does it do the same with the function from AVR ? How can I read content of SPI_DR on STM32CubeMX, any ideas ? Suppose to be simple like AVR... Thanks
Posted on January 19, 2016 at 18:04

hspi2.Instance->DR

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on January 19, 2016 at 18:08

Won't the more coherent method be

STM32 :
/* Exchange a byte */
static
BYTE xchg_spi ( /* Returns received data */
BYTE dat /* Data to be sent */
)
{
// TODO: Send dat here
while(HAL_SPI_GetState(&hspi2) != HAL_SPI_STATE_READY);
HAL_SPI_Receive(&hspi2,&dat,1,100); // passed as an address, 1 byte in length
// TODO: Wait for receive to actually occur
return dat;
}

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 19, 2016 at 23:34

So do you reckon the function is not trasmitting data but receiving ?

Because of I read this, please have a look.. Thanks

/*-----------------------------------------------------------------------*/
/* Send a command packet to MMC */
/*-----------------------------------------------------------------------*/
static
BYTE send_cmd ( /* Returns R1 resp (bit7==1:Send failed) */
BYTE cmd, /* Command index */
DWORD arg /* Argument */
)
{
BYTE n, res;
if (cmd & 0x80) { /* ACMD<
n
> is the command sequense of CMD55-CMD<
n
> */
cmd &= 0x7F;
res = send_cmd(CMD55, 0);
if (res > 1) return res;
}
/* Select the card and wait for ready except to stop multiple block read */
if (cmd != CMD12) {
deselect();
if (!select()) return 0xFF;
}
/* Send command packet */
xchg_spi(0x40 | cmd); /* Start + Command index */
xchg_spi((BYTE)(arg >> 24)); /* Argument[.24] */
xchg_spi((BYTE)(arg >> 16)); /* Argument[.16] */
xchg_spi((BYTE)(arg >> 8)); /* Argument[.8] */
xchg_spi((BYTE)arg); /* Argument[7..0] */
n = 0x01; /* Dummy CRC + Stop */
if (cmd == CMD0) n = 0x95; /* Valid CRC for CMD0(0) + Stop */
if (cmd == CMD8) n = 0x87; /* Valid CRC for CMD8(0x1AA) Stop */
xchg_spi(n);
/* Receive command response */
if (cmd == CMD12) xchg_spi(0xFF); /* Skip a stuff byte when stop reading */
n = 10; /* Wait for a valid response in timeout of 10 attempts */
do
res = xchg_spi(0xFF);
while ((res & 0x80) && --n);
return res; /* Return with the response value */
}