ajellisuk wrote on Wednesday, May 23, 2018:
Hello
I have a project where I am collecting packets of data using an STM32F745 and then sending them via an SPI port to a master device (the mcu is an spi slave). When I receive the packets of data I place them into a buffer held within a structure, and place the structure in a queue. My SPI ISR retrieves the structures from the queue and copies them into a dma buffer ready for sending to the master device. The problem I have is that when I retrive the structure from the queue, the data appears to be offset by 8 bytes. I have looked at this page https://www.freertos.org/a00118.html for an example of how to queue a structure, but am still having problems.
My structure is:
#define INTERNAL_BUFF_SIZE 248
typedef struct tSPIPacket
{
uint8_t flags;
uint8_t checksum;
uint8_t framecount;
uint8_t frametime;
uint64_t timestamp;
uint8_t size; //size of structure, not amount of data
uint8_t packetid; //an id for the entire packet (large data split across transfers have same id)
uint8_t xferid; //id for transfer in packet (0=first transfer for packet, 1=second etc)
uint8_t xfersize; //number of data bytes in this transfer
uint32_t totaldatasize; //total amount of data in the packet
uint32_t datatype; //fourcc,
uint8_t data[INTERNAL_BUFF_SIZE];
} SPI_Packet;
extern SPI_Packet Spi_Buffer;
extern SPI_Packet *Spi_Buffer2;
Then the code for creating the queue, and placing data onto the queue is:
spi_queue = xQueueCreate( 100, sizeof( SPI_Packet *) );
memcpy(Spi_Buffer.data[0], <incoming data>, INTERNAL_BUFF_SIZE);
// Fill out the packet header
Spi_Buffer.flags = 0;
Spi_Buffer.checksum = 0;
Spi_Buffer.framecount = 1;
Spi_Buffer.frametime = 1;
Spi_Buffer.timestamp = 1;
Spi_Buffer.size = sizeof(SPI_Packet);
Spi_Buffer.packetid = 0;
Spi_Buffer.xferid = 0;
Spi_Buffer.xfersize = INTERNAL_BUFF_SIZE;
Spi_Buffer.totaldatasize = INTERNAL_BUFF_SIZE + sizeof(SPI_Packet);
Spi_Buffer.datatype = FOURCC;
&Spi_Buffer, ( TickType_t ) 0 );
SPI_Packet *spitmp=&Spi_Buffer;
xQueueSend( spi_queue, ( void * ) spitmp, ( TickType_t ) 0 );
And the code for retriving the structures and placing them into the dma buffer is:
// Arm the SPI and DMA to Tx bytes from aSPITxBuffer
if ( xQueueReceiveFromISR( spi_queue, ( void * ) &(Spi_Buffer2), & xTaskWokenByReceive) )
{
memset ( aSPITxBuffer, 0, 256 ); // ensure tx buffer is clear
memcpy(&aSPITxBuffer[1], &Spi_Buffer2, 256);
HAL_SPI_TransmitReceive_DMA(&hspi5, (uint8_t*)&aSPITxBuffer[0], (uint8_t *)&aSPIRxBuffer[0], 256);
return; // Return from he as we don't want to be re armed for receive only
}
if ( xTaskWokenByReceive != pdFALSE )
{
/* We should switch context so the ISR returns to a different task.
NOTE: How this is done depends on the port you are using. Check
the documentation and examples for your port. */
HAL_SPI_Receive_DMA(&hspi5, (uint8_t *)aSPIRxBuffer, 7);
taskYIELD ();
}
I am using the intermediate buffer aSPITxBuffer to make this issue easier to debug.
Can someone please suggest why I’m getting the 8 byte offset when I retrieve Spi_Buffer2 from the queue please?
Thanks in advance
Andrew