Why is my buffer corrupted when for-loop is split up?
- February 16, 2022
- 3 replies
- 1828 views
Hello,
I have a project on a Nucleo board that samples the ADC and typecasts the result.
The typecasted value and the original value are sent via UART and virtual com to my PC.
When I put this code in a single for-loop, I can receive both the raw and converted values as can be seen by the overlapping first red and green graph in the attachment. I know they have some strange scale and offset that I think is caused by converting to bytes and sending the stream to my PC that reinterprets the bytes stream.
However, as soon as I split the for-loop in two, as seen in the code-snipped below, the scaled and offset data gets corrupted while, to my surprise, the raw ADC samples are still shown correctly. This can be seen in the second red and green graph where I think that the green line represents the scaled_and_offset_buffer samples. What puzzels me the most is that the red graph is not corrupted in the second case
What could cause this behavior?
Thanks in advance.
Ruud
Code snippet below. I included the complete project in the attachment.
...
typedef struct
{
uint8_t header[2];
uint8_t size;
int32_t adc_buffer_element;
int32_t scale_and_offset_buffer_element;
}UartFrame;
...
int Main_Update() //Continiously called from main while loop
{
while(!adc_transfer_complete_flags);
adc_transfer_complete_flags = 0;
/* Scale and Offset converted ADC samples */
for(uint32_t n=0; n<ADC_BUFFER_SIZE; n++)
{
scale_and_offset_buffer[n] = adc_buffer_ptr[n];
//scale_and_offset_buffer[n] <<= ADC_BIT_SHIFTS;
//scale_and_offset_buffer[n] += ADC_OFFSET;
/* works fine in a single for-loop */
//frame.adc_buffer_element = adc_buffer_ptr[n];
//frame.scale_and_offset_buffer_element = scale_and_offset_buffer[n];
//HAL_UART_Transmit(&hlpuart1, (uint8_t*) &frame, sizeof(frame), HAL_MAX_DELAY);
}
//arm_mult_q31(scale_and_offset_buffer, nco_cos_out, mult0_out, ADC_BUFFER_SIZE);
//arm_mult_q31(scale_and_offset_buffer, nco_sin_out, mult1_out, ADC_BUFFER_SIZE);
/* scale and offset buffer gets corrupted when for-loop is split */
for(uint32_t n=0; n<ADC_BUFFER_SIZE; n++)
{
frame.adc_buffer_element = adc_buffer_ptr[n];
frame.scale_and_offset_buffer_element = scale_and_offset_buffer[n];
HAL_UART_Transmit(&hlpuart1, (uint8_t*) &frame, sizeof(frame), HAL_MAX_DELAY);
}
return 0;
}
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc)
{
adc_transfer_complete_flags |= ADC_FULL_TRANSFER_COMPLETE;
adc_buffer_ptr = &adc_buffer[ADC_BUFFER_SIZE];
}
void HAL_ADC_ConvHalfCpltCallback(ADC_HandleTypeDef *hadc)
{
adc_transfer_complete_flags |= ADC_HALF_TRANSFER_COMPLETE;
adc_buffer_ptr = &adc_buffer[0];
}