I am writing some code for an MSP430 project and am having some trouble initializing a structure within a macro. The structure is a buffer structure:
struct Buffer{
unsigned int size;
unsigned int start;
unsigned int end;
unsigned int count;
unsigned char *data;
};
void buffer_init(struct Buffer *buffer, unsigned char *buffer_data, unsigned int size)
{
buffer->size = size;
buffer->start = 0;
buffer->end = 0;
buffer->count = 0;
buffer->data = buffer_data;
}
I have been having good luck with creating and initializing an array as follows (where the array is placed in a certain section in memory):
struct Buffer stream1_buffer;
#pragma DATA_SECTION(stream1_buffer_data, ".fram_vars")
static unsigned char stream1_buffer_data[512];
void init(void) {
buffer_init(&stream1_buffer,stream1_buffer_data,sizeof(stream1_buffer_data));
}
However, I need to now create an array of variable sized buffers that have a bit more information, such as channel number and sample rate. So I would like to be able to initialize a buffer in a macro so that I can easily create an array of a new structure called Stream that contains a buffer.
I have tried the following, but the test code does not provide the same results as it did when I initialized the buffer structure normally.
#define def_Buffer(name,length) \
static unsigned char name##_buffer_data[length]; \
static struct Buffer name##_buffer = { \
.size=length, \
.start=0, \
.end=0, \
.count=0, \
.data=(unsigned char *) name##_buffer_data \
}
Any suggestions on how to proceed? I'm not even 100% sure this is good coding practice. Thanks!
Aucun commentaire:
Enregistrer un commentaire