Skip to main content
antonius
Associate III
October 6, 2015
Question

STM32CubeMx HAL_SPI_Receive ?

  • October 6, 2015
  • 21 replies
  • 2880 views
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
    This topic has been closed for replies.

    21 replies

    caco3
    Associate III
    October 7, 2015
    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
    antoniusAuthor
    Associate III
    October 9, 2015
    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
    antoniusAuthor
    Associate III
    October 9, 2015
    Posted on October 09, 2015 at 16:35

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

    Tesla DeLorean
    Guru
    October 11, 2015
    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 (See Profile) Up vote any posts that you find helpful, it shows what's working..
    antonius
    antoniusAuthor
    Associate III
    October 16, 2015
    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
    antoniusAuthor
    Associate III
    October 16, 2015
    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; 
    }

    Tesla DeLorean
    Guru
    October 16, 2015
    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 (See Profile) Up vote any posts that you find helpful, it shows what's working..
    antonius
    antoniusAuthor
    Associate III
    October 17, 2015
    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
    antoniusAuthor
    Associate III
    October 17, 2015
    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
    Tesla DeLorean
    Guru
    October 17, 2015
    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 (See Profile) Up vote any posts that you find helpful, it shows what's working..