cancel
Showing results for 
Search instead for 
Did you mean: 

STM32CubeMx HAL_SPI_Receive ?

antonius
Senior
Posted on October 06, 2015 at 23:59

Guys,

I want to send a command and then retrieve the output on MOSI line ... Is the right way doing it ?

int resultvalue = 0;
uint8_t data7[2] = { VS_READ_COMMAND, addressbyte };
HAL_SPI_Transmit(&hspi1,data7,2,1000);
then :
uint8_t data8[1] = {0x00}; <=== is it declared properly ?
resultvalue = HAL_SPI_Receive(&hspi1,data8,1,1000);
resultvalue <<= 8;
resultvalue += HAL_SPI_Receive(&hspi1,data8,1,1000);
return resultvalue;

Thanks a lot
21 REPLIES 21
caco3
Associate II
Posted on October 07, 2015 at 12:00

HAL_SPI_Receive()

will return a status, but not the received data! Try this:

HAL_SPI_Receive(&hspi1,data8,1,1000);
resultvalue = data8[0];
resultvalue <<= 8;
HAL_SPI_Receive(&hspi1,data8,1,1000);
resultvalue += data8[0];

antonius
Senior
Posted on October 09, 2015 at 11:07

So it will be:

unsigned char Mp3ReadRegister( unsigned char addressbyte)
{
int resultvalue = 0;
Mp3DeselectData();
Mp3SelectControl(); //XCS = 0
uint8_t data7[2] = { VS_READ_COMMAND, addressbyte };
HAL_SPI_Transmit(&hspi1,data7,2,1000);
uint8_t data8[1] = {0x00};
data8[0] = 0;
HAL_SPI_Receive(&hspi1,data8,1,1000);
resultvalue = data8[0];
resultvalue <<= 8;
HAL_SPI_Receive(&hspi1,data8,1,1000);
resultvalue += data8[0];
Mp3DeselectControl(); 
return resultvalue; //??16??????
}

Please correct me if I'm wrong, thanks
antonius
Senior
Posted on October 09, 2015 at 16:35

How can I receive data from SPI1 on STM32CubeMX  then ??????

Posted on October 11, 2015 at 20:11

Please correct me if I'm wrong

Not sure the world always works like that, you have to take some responsibility to think, test and evaluate why things aren't working.

Do you think casting a 32-bit integer into an 8-bit unsigned char is an effective way to pass back the value you have just read?

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 October 16, 2015 at 23:20

Hi clive, thanks for correcting me....

Which one is 32 bits ones ?....

Let me find out....

So 32bits to 32bits.....and the container size = the sender size

I'm not focusing on it....I'll be slowly reading it.....

Thanks again..

antonius
Senior
Posted on October 16, 2015 at 23:25

Like this : it's char ( 8 bit )...., please correct me if I have mistake, thanks

unsigned char Mp3ReadRegister( unsigned char addressbyte)
{
unsigned char resultvalue=0;
Mp3DeselectData();
Mp3SelectControl(); //XCS = 0
uint8_t data7[2] = { VS_READ_COMMAND, addressbyte };
HAL_SPI_Transmit(&hspi1,data7,2,1000);
uint8_t data8[1] = {0x00};
data8[0] = 0;
HAL_SPI_Receive(&hspi1,data8,1,1000);
resultvalue = data8[0];
resultvalue <<= 8;
HAL_SPI_Receive(&hspi1,data8,1,1000);
resultvalue += data8[0];
Mp3DeselectControl(); 
return resultvalue; 
}

Posted on October 17, 2015 at 01:52

No, you really need to study up on C types and how numbers are represented.

Look, you can't use a 8-bit variable to hold some thing that's going to take at least 16-bits to represent. If you shift that value from an 8-bit variable by 8 to the left the data is going to fall off the end, and disappear.

uint16_t Mp3ReadRegister( unsigned char addressbyte)
{
uint16_t resultvalue=0;
Mp3DeselectData();
Mp3SelectControl(); //XCS = 0
uint8_t data7[2] = { VS_READ_COMMAND, addressbyte };
HAL_SPI_Transmit(&hspi1,data7,2,1000);
uint8_t data8[1] = {0x00};
HAL_SPI_Receive(&hspi1,data8,1,1000);
resultvalue = (uint16_t)data8[0] << 8;
HAL_SPI_Receive(&hspi1,data8,1,1000);
resultvalue += (uint16_t)data8[0];
Mp3DeselectControl(); 
return resultvalue; 
}

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 October 17, 2015 at 15:00

How do you reckon if I made like this,

I worked when I compiled

unsigned int Mp3ReadRegister( unsigned char addressbyte)
{
unsigned int resultvalue=0;
Mp3DeselectData();
Mp3SelectControl(); //XCS = 0
uint8_t data7[2] = { VS_READ_COMMAND, addressbyte };
HAL_SPI_Transmit(&hspi1,data7,2,1000);
uint8_t data8[1] = {0x00};
data8[0] = 0;
HAL_SPI_Receive(&hspi1,data8,1,1000);
resultvalue = data8[0];
resultvalue <<= 8;
HAL_SPI_Receive(&hspi1,data8,1,1000);
resultvalue += data8[0];
Mp3DeselectControl(); 
return resultvalue; //??16??????
}

it,
antonius
Senior
Posted on October 17, 2015 at 15:10

How about transmiting ?

Am I correct replacing SPIPutchar ?

void Mp3WriteRegister(unsigned char addressbyte, unsigned char highbyte, unsigned char lowbyte)
{
Mp3DeselectData();
Mp3SelectControl(); //XCS = 0
uint8_t data6[4] = { 0x02, addressbyte, highbyte, lowbyte };
//uint8_t data6[4] = { VS_WRITE_COMMAND, addressbyte, highbyte, lowbyte };
HAL_SPI_Transmit(&hspi1,data6,4,1000);
//HAL_SPI_Transmit_DMA(&hspi1,data6,4); 
/*
SPIPutChar(VS_WRITE_COMMAND); //????????
SPIPutChar(addressbyte); //????????
SPIPutChar(highbyte); //????????8?
SPIPutChar(lowbyte); //????????8?
*/
Mp3DeselectControl();
}

Thanks a lot