2015-10-06 02:59 PM
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
2015-10-07 03:00 AM
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];
2015-10-09 02:07 AM
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
2015-10-09 07:35 AM
How can I receive data from SPI1 on STM32CubeMX then ??????
2015-10-11 11:11 AM
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?2015-10-16 02:20 PM
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..2015-10-16 02:25 PM
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;
}
2015-10-16 04:52 PM
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;
}
2015-10-17 06:00 AM
How do you reckon if I made like this,
I worked when I compiledunsigned 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,
2015-10-17 06:10 AM
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