I use the following type:
/* double_buffer.h */
typedef struct
{
uint8_t * active_buffer_p; //< Address of active buffer
uint8_t current_writing_index; //< Current writing index in active buffer
uint8_t buffer_1[BUFFER_SIZE]; //< First buffer
uint8_t buffer_2[BUFFER_SIZE]; //< Second buffer
} double_buffer_t;
#define DoubleBuffer_init(buffers) do { \
(buffers).active_buffer_p = (buffers).buffer_1; \
(buffers).current_writing_index = 0; \
} while(0)
In my code, I declare an array of double buffer, using the volatile keywoard (because the buffers can be updated/read asynchronously in interrupts and in functions):
static volatile double_buffer_t m_double_buffers[NB_DOUBLE_BUFFERS];
I then initialize those buffers individually:
DoubleBuffer_init(m_double_buffers[id]);
When I compile the software (gcc), I got the following warning:
error: assignment discards 'volatile' qualifier from pointer target type [-Werror=discarded-qualifiers]
28 | (buffers).active_buffer_p = (buffers).buffer_1; \
The reason why I have this warning is quite unclear to me, and I am not sure how to fix it.
Any help would be appreciated (I can update the question if something is not clear).
volatiledoes not mean "atomic". It's impossible to tell without specifics, but what you've posted opens to door to some nasty race conditions.volatilestructure also getvolatilequalified. The the pointer type ofdouble_buffer_t::active_buffer_pis missingvolatilequalifier.volatile. Every single time someone brings this up, we always get these irrelevant rants about "volatile and thread safety" in comments. Where are the threads? Who claimedvolatileis there to prevent data races? Nobody! So kindly delete those off-topic comments and check out this: Using volatile in embedded C development