Hi guys
I find myself quite often need to buffer a packet of data from uart, then the main application will need to process them. Here is how I usually do it.
Is there better way to do this?
I find myself quite often need to buffer a packet of data from uart, then the main application will need to process them. Here is how I usually do it.
Code:
typedef struct{
uint8_t data[MAX_SIZE];
uint8_t pointer; // pointing to next empty location in a buffer
uint8_t isReady : 1; // set if the buffer is ready to be processed
uint8_t isBeingWriten : 1; // set if the buffer is being written to, usually in a interrupt.
// maybe other flags
}my_struct_t;
volatile my_struct_t my_struct[BUFFER_LEVEL];
// return the index of an empty buffer, return 0xFF if no empty bubfer
uint8_t findEmptyBufferIndex(void){
// disable uart interrupt
// do stuff
// enable uart interrupt
}
// return the index of a buffer that is ready to be processed by the main application., return 0xFF if there is no buffer to be processed
uint8_t findReadyBufferIndex(void){
// disable uart interrupt
// do stuff
// enable uart interrupt
}
Is there better way to do this?