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-18 06:44 AM
return dat[1];
// Surely not in [1], but rather [0] ??
2016-01-18 02:00 PM
so
return dat[0]; ? on the top :BYTE xchg_spi ( /* Returns received data */
BYTE dat[0]
as well ?
2016-01-18 05:28 PM
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 &datYour 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 examples2016-01-18 06:11 PM
2016-01-19 12:58 AM
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... Thanks2016-01-19 01:02 AM
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
2016-01-19 09:04 AM
hspi2.Instance->DR
2016-01-19 09:08 AM
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;
}
2016-01-19 02:34 PM
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 */
}