In my embedded project, I have one process that creates values that should be transmitted, and another that processes them. I made the below code to allow the creation process to write up to 2 packets ahead or the processor, so the processor (which operates in real-time based on the data) doesn't ever run out of data.
Here is the relevant buffering code:
The writing process is called with:
Problem: ReadValuesFromTransmitBuffer() always returns an array of zeros, rather than the data I wrote to the buffer.
I added the ValuesAllZero function as a debug to see where the values get lost. The seem to be intact when passed in, and intact when written, but zero when they are read from the buffer.
C isn't my native turf, and this is my first time I've attempted to use malloc or memcpy in more than a decade, so my mistake is probably basic.
Here is the relevant buffering code:
Code:
//Header file (relevant parts)
#define NumChannels (uint8_t)9
static uint8_t ChannelValues[NumChannels];
static uint8_t TransmitBuffer[3][NumChannels];
volatile uint8_t CurrentReadBuffer;
volatile uint8_t CurrentWriteBuffer;
//Source file (relevant parts)
uint8_t NextValue(uint8_t currentValue) {
uint8_t next = currentValue+1;
if (next > 2)
next = 0;
return next;
}
int CanRead() {
return CurrentReadBuffer != CurrentWriteBuffer;
}
int ValuesAllZero(uint8_t * values)
{
int ret = 1;
int i;
for (i=0;ret && i<NumChannels;i++)
ret &= (*(values+i)== 0);
return ret;
}
void WriteValuesToTransmitBuffer(uint8_t * values) {
if (ValuesAllZero(values))
_NOP();
//Wait until the values are copied before incrementing the write buffer
// to prevent concurrency issues.
uint8_t writingBuffer = NextValue(CurrentWriteBuffer);
if (writingBuffer == CurrentReadBuffer)
//If buffer is full, overwrite previously buffered data, as it is now stale.
writingBuffer = CurrentWriteBuffer;
// TransmitBuffer[writingBuffer] = values;
memcpy(TransmitBuffer[writingBuffer], values,
NumChannels * sizeof(uint8_t));
if (ValuesAllZero(TransmitBuffer[writingBuffer]))
_NOP();
CurrentWriteBuffer = writingBuffer;
}
uint8_t * ReadValuesFromTransmitBuffer() {
if (CanRead()) {
uint8_t * ret = malloc(NumChannels);
if (NULL == ret) return NULL;
// ret = TransmitBuffer[CurrentReadBuffer];
memcpy(ret, TransmitBuffer[CurrentReadBuffer], NumChannels * sizeof(uint8_t));
if (ValuesAllZero(ret))
_NOP();//A breakpoint here gets hit, showing I've lost my data.
CurrentReadBuffer = NextValue(CurrentReadBuffer);
return ret;
}
return NULL;
}
Code:
//Add readings to Buffer
ChannelValues[0] = controlsReport.X + 128;
ChannelValues[1] = controlsReport.Y + 128;
ChannelValues[2] = controlsReport.Collective + 128;
ChannelValues[3] = controlsReport.Throttle + 128;
ChannelValues[4] = controlsReport.AntiTorque + 128;
ChannelValues[5] = controlsReport.Collective + 128;
ChannelValues[6] = 129; //Future use
ChannelValues[7] = 129; //Future use
ChannelValues[8] = 129; //Future use
if (ValuesAllZero(ChannelValues))
_NOP();
WriteValuesToTransmitBuffer(ChannelValues);
Problem: ReadValuesFromTransmitBuffer() always returns an array of zeros, rather than the data I wrote to the buffer.
I added the ValuesAllZero function as a debug to see where the values get lost. The seem to be intact when passed in, and intact when written, but zero when they are read from the buffer.
C isn't my native turf, and this is my first time I've attempted to use malloc or memcpy in more than a decade, so my mistake is probably basic.