cancel
Showing results for 
Search instead for 
Did you mean: 

getting 3 arrays data from SPI RxBuffer

StanCosgrove
Associate II

Hi I m using 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]?

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

please advise...

7 REPLIES 7
tjaekel
Senior III

NO, it is NOT correct!

You define an uint16_t buffer for TX, but the SPI function sends all as a uint8_t buffer:
You will get the second byte as ZERO!

Your SensorTxBuff[2] has at the end as a uint8_t buffer used and sent by the SPI function this content:
    0x8, 0, 0xAF, 0   #16bit words as little endian, because it is uint16_t!

But you send this buffer with 3 bytes (!), so that SPI will send: 0x8, 0, 0xAF (where does the 3 comes from?)

The second byte in SensorRxBuff is SensorRxBuff[1] (all starts from index 0).

I guess your code is not a "real code": why do you use &SensorTxBuff? It should be just SensorTxBuff (because it is an array and already the address of the buffer), or you had to use &SensorTxBuff[0] (the address location of the first entry).

Try to understand buffers, their addresses, how to access elements of the array (via [INDEX]) and in particular what "type casting" is, what happens, what the endian is...
(you are asking about "basic coding rules", not about how to use SPI...   LOL)

Hi thx, the SPI transmit portion is working, i m able to receive 3 bytes from the sensor (ADC), the above code was modify to obtain the indiviual 3 received bytes, cos i need to compete the 2nd byte ie. SensorRXBuff[1]. 

could you advise what should the code be to churn out the 2nd receive byte from the SensorRXBuff ? thx.

void 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 high
	
}

 

Sure, sending 3 bytes via SPI in "full-duplex mode" gives you 3 bytes received.

As mentioned: the second byte received is SensorRxBuf[1].

uint8_t secondByteReceived;
secondByteReceived = SensorRxBuf[1];

You can also do:

uint16_t secondWordReceived;
secondWordReceived = SensorRxBuf[1];

But watch out here again: if you receive a byte via SPI, e.g. as 0x12 - the (16bit) word in secondWordReceived is 0x0012. When you dump the memory location (as bytes), you will see: 0x12, 0x00 (little endian).

Sorry, it does not look reasonable for me to define unit16_t with two values - which gives you 4 bytes and then to send just 3 bytes via SPI. OK, you get 3 bytes. And I am sure, you send 3 bytes, but the second one is a ZERO you send.

uint32_t Sensor(uint8_t *ch)
{
...
return SensorRXBuff; 
}

ok what should the function be? uint32_t and to return with SensorRXBuff ?

"I am lost":
SensorRxBuff is an address - not a value from the content of the buffer.

uint32_t Sensor(uint_t *ch)
{
//...
return (uint32_t)SensorRxBuff[1];
}

would make much more sense, right?

Do you know how to handle types, casts, elements of an array (buffer)...?

I tried your suggestions but compiler report error @ 

uint32_t Sensor(uint_t *ch)

conflicting types for "Sensor"; have "uint32_(uint8_t*)" {aka 'long unsigned int(unsigned char*)'}

return (uint32_t)SensorRxBuff[1];

syntax error...  pls advise thx...

I assume SensorRxBuff is not known in function Sensor. What is your entire function code?

Using *ch as the buffer - sure, it has to be something like this:

uint32_t Sensor(uint8_t *ch)
{
  //...
  return *(ch +1);
  //or, depending what you do with pointer ch:
  ch++;
  return *ch;
  //even tbis would return the 2nd element of the buffer ch:
  return *(++ch);
  //or this:
  return ch[1];
}