Getting 3 arrays data from SPI RxBuffer

Joined
Jan 12, 2023
Messages
5
Reaction score
0
Hi I m using STM32H743 Nucleo, SPI to transmit and receive data from a sensor, the sensor return 3 bytes of data, SensorRXBuff[0], SensorRXBuff[1], and SensorRXBuff[2], i m gona use these 3 byte for indivual computation, i enclosed the function below, could anyone advise if this function is correct? is the uint_16 in front of function correct? how can i call the 2nd byte i.e SensorRXBuff[1]? Anyone can advise ??

uint16_t Sensor(uint8_t *ch)
{
uint16_t SensorTxBuff[2] = {0x8, 0xAF};
HAL_GPIO_WritePin(..) // SPI CS low
if (HAL_SPI_TransmitReceive(&hspi1,(uint8_t *)&SensorTxBuff, (uint8_t *)&SensorRXBuff, 3, 100) != HAL_OK);
{
Error_Handler();
}
Hal_GPIO_WritePin;(...)// SPI CS hi..
return SensorRXBuff;
}
 
Joined
Jan 24, 2024
Messages
42
Reaction score
7
Hi I m using STM32H743 Nucleo, SPI to transmit and receive data from a sensor, the sensor return 3 bytes of data, SensorRXBuff[0], SensorRXBuff[1], and SensorRXBuff[2], i m gona use these 3 byte for indivual computation, i enclosed the function below, could anyone advise if this function is correct? is the uint_16 in front of function correct? how can i call the 2nd byte i.e SensorRXBuff[1]? Anyone can advise ??

uint16_t Sensor(uint8_t *ch)
{
uint16_t SensorTxBuff[2] = {0x8, 0xAF};
HAL_GPIO_WritePin(..) // SPI CS low
if (HAL_SPI_TransmitReceive(&hspi1,(uint8_t *)&SensorTxBuff, (uint8_t *)&SensorRXBuff, 3, 100) != HAL_OK);
{
Error_Handler();
}
Hal_GPIO_WritePin;(...)// SPI CS hi..
return SensorRXBuff;
}
There are a few issues with your code snippet:

1. Function return type: You've defined the function Sensor to return uint16_t, but it should return an array of uint8_t (since you are receiving 3 bytes of data from the sensor). So, the return type should be uint8_t*.

2. Typo in HAL_GPIO_WritePin: You've missed the pin and the action arguments for the function HAL_GPIO_WritePin after the semicolon.

3. Accessing the received data: To access individual bytes of the received data, you can index the SensorRXBuff array.

Here's the corrected version of your function:

C:
uint8_t* Sensor(void)
{
    uint8_t SensorTxBuff[2] = {0x8, 0xAF};
    HAL_GPIO_WritePin(/* specify pin and port for CS */, GPIO_PIN_RESET); // SPI CS low
    if (HAL_SPI_TransmitReceive(&hspi1, SensorTxBuff, SensorRXBuff, 3, 100) != HAL_OK)
    {
        Error_Handler();
    }
    HAL_GPIO_WritePin(/* specify pin and port for CS */, GPIO_PIN_SET); // SPI CS hi
    return SensorRXBuff;
}

To access the individual bytes received from the sensor, you can use array indexing. For example, to access the second byte (index 1) of SensorRXBuff, you can do SensorRXBuff[1].

Also, ensure that SensorRXBuff is declared and properly sized before using this function.
 
Joined
Jan 12, 2023
Messages
5
Reaction score
0
thanks for reply, i tried your suggestion but compiler report these error.. @ --> uint8_t* Sensor(void)

conflicting types for "Sensor"; have "uint32_(uint8_t*)" {aka 'long unsigned int(unsigned char*)'}
 
Joined
Jan 12, 2023
Messages
5
Reaction score
0
error shd be
"conflicting types for "Sensor"; have "uint8_(uint8_t*)" {aka 'long unsigned int(unsigned char*)'}"
please advise thx....
 
Joined
Jan 24, 2024
Messages
42
Reaction score
7
I see, the error is due to a conflicting declaration of the Sensor function elsewhere in your code. The compiler is seeing a different declaration or definition of Sensor with a different return type.

To resolve this issue, you should make sure that there is no other conflicting declaration or definition of the Sensor function elsewhere in your codebase. The conflicting declaration might be in a header file or another source file.

If you intended to return a pointer to an array of uint8_t, as suggested in my previous response, then you need to ensure that the return type and the declaration of the function match in all places where it's used.

Here's how you should declare the function in your code:

C:
uint8_t* Sensor(void);

And define it as follows:


C:
uint8_t* Sensor(void)
{
    // Function body
}

Ensure that you have only one definition or declaration of the Sensor function with the correct return type throughout your codebase. This should resolve the conflicting types error.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,794
Messages
2,569,641
Members
45,355
Latest member
SJLChristi

Latest Threads

Top