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
Posted on October 17, 2015 at 16:02

Like I said earlier, I think that's a determination you have to make for yourself. The types are at least consistent, and won't lose precision.

The compiler can tell you if the syntax is correct, not if it's functionally correct. I'm not using the HAL or this VS chip, how they interact is something you'll need to experiment with, and review the documentation for. For transmitting, there should be several ways to do it, you might want to check the signals with a logic analyzer to understand if one method has tighter timing, with shorter inter-symbol delays. From: h.rick Posted: Saturday, October 17, 2015 3:10 PM Subject: STM32CubeMx HAL_SPI_Receive ?

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
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 18, 2015 at 14:53

Please confirm, is this statement similar ?
#define MP3_XCS ( 1 << 
4
)
#define Mp3SelectControl() { GPIOA->ODR &= ~MP3_XCS; }
equal to :
#define Mp3SelectControl() { HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4 , 0 ); }
Thanks

Posted on October 18, 2015 at 21:39

With my limited perspective on HAL they would appear to have similar effect.

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 25, 2015 at 06:41

Ok, thanks clive,

I have this function :

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

How can I print

resultvalue

into UART ? Is it possible with printf(resultvalue) ? Would it be like this : ?

while(Mp3ReadRegister(0x03) != CLOCK_REG)//�?´Ê±ÖӼĴæÆ÷ //set PLL register 
//while(VS1003B_ReadCMD(0x03) != CLOCK_REG)//�?´Ê±ÖӼĴæÆ÷ //set PLL register
{
//check content or register 0x03
uint16_t ReadRegister;
ReadRegister = Mp3ReadRegister(0x03);
sprintf(str, ''%d'', ReadRegister); // As a value
HAL_UART_Transmit(&huart1, str, 100, 1000); // As a pointer, with a length

Posted on October 25, 2015 at 19:02

uint16_t ReadRegister;
ReadRegister = Mp3ReadRegister(0x03);
sprintf(str, ''%d

'', ReadRegister); // As a value, should also return a length of the output
HAL_UART_Transmit(&huart1, str, strlen(str), 1000); // As a pointer, with a length

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 27, 2015 at 13:34

I get the output like this in my serial....

Anything I must fix ?

REGISTER 0x03 =

 

.......&#1;&#2;&#3;&#4;&#1;&#2;&#3;&#4;&#6;&#7;&#8;    Ã…’'.&#8;... <...X$.&#8;È'.&#8;<.. ¤&#4;..h$.&#8;.........¢J&#4;........&#1;&#2;&#3;&#4;&#6;&#7;&#8;    2192

thanks

antonius
Senior
Posted on October 27, 2015 at 13:39

code :

//Check the content of register and transmit via UART
char str[32]; 
uint16_t ReadRegister;
ReadRegister = Mp3ReadRegister(0x00);
sprintf(str, ''%d

'', ReadRegister); // As a value, should also return a length of the output
HAL_UART_Transmit(&huart1, ''REGISTER 0x03 = 

 

'', 100, 1000); // As a pointer, with a length
HAL_UART_Transmit(&huart1, str, strlen(str), 1000); // As a pointer, with a length

Posted on October 27, 2015 at 16:48

You are making my head hurt.

HAL_UART_Transmit(&huart1, ''REGISTER 0x03 = 

 

'', 100, 1000); // As a pointer, with a length

There are NOT 100 characters there, that's why it prints ''REGISTER 0x03 ...'' and then lots of other junk. Constrain the length to that of the actual string you want to send. Or use subroutines. You really need to learn to code. void Transmit_String(char *s) { HAL_UART_Transmit(&huart1, s, strlen(s), 1000); // As a pointer, with a length } ... Transmit_String(''REGISTER 0x03 = ''); Transmit_String(str);
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 27, 2015 at 22:44

 HAL_UART_Transmit(&huart1, ''REGISTER 0x03 = \r\n \r\n'', 22, 1000); // As a pointer, with a length

?

antonius
Senior
Posted on October 27, 2015 at 22:54

Thanks for the suggestion,

Mp3WriteRegister(0x03,0x88,0x10);
HAL_Delay(1000);
//Check the content of register and transmit via UART
char str[32]; 
uint16_t ReadRegister;
ReadRegister = Mp3ReadRegister(0x03);
sprintf(str, ''%d

'', ReadRegister); // As a value, should also return a length of the output
HAL_UART_Transmit(&huart1, ''REGISTER 0x03 ='', 15, 1000); // As a pointer, with a length
HAL_UART_Transmit(&huart1, str, strlen(str), 1000); // As a pointer, with a length

The output :

REGISTER 0x03 =34832

Is it matter of ASCII and hex format ?

void Mp3WriteRegister(uint8_t addressbyte, uint8_t highbyte, uint8_t lowbyte)
{
Mp3DeselectData();
Mp3SelectControl(); //XCS = 0
uint8_t data6[4] = { VS_WRITE_COMMAND, addressbyte, highbyte, lowbyte };
HAL_SPI_Transmit(&hspi1,data6,4,1000);
Mp3DeselectControl();
}
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; 
}