Example Of Non Irq Function Fills Buffers and IRQ Function Empties Buffers
This implemenation is based on the function sending the data being an irq which sends bits of the data irq calls and the function filling the data buffers being non irq and ensuring the operation is irq safe.
#define FILE_DATA_NUM_OF_BUFFERS 4
#define FILE_DATA_BUFFER_LENGTH 512
int file_data_next_buffer_to_fill = 0;
volatile int file_data_sending_buffer = 0;
volatile int file_data_sending_buffer_byte = 0;
BYTE file_data_buffers[FILE_DATA_NUM_OF_BUFFERS][FILE_DATA_BUFFER_LENGTH];
//----- START THE SENDING PROCESS -----
file_data_next_buffer_to_fill = 0;
file_data_sending_buffer = FILE_DATA_NUM_OF_BUFFERS - 1;
file_data_sending_buffer_byte = FILE_DATA_BUFFER_LENGTH;
//----- THE DATA COLLECTION FUNCTION FILLING THE BUFFER -----
//It wants to fill the next_buffer_to_fill with new data and on to ultimately meet file_data_sending_buffer
DISABLE_INT;
if (file_data_next_buffer_to_fill != file_data_sending_buffer)
{
//----- THERE IS AT LEAST 1 BUFFER WAITING TO BE FILLED -----
p_buffer = &file_data_buffers[file_data_next_buffer_to_fill][0];
file_data_next_buffer_to_fill++;
if (file_data_next_buffer_to_fill >= FILE_DATA_NUM_OF_BUFFERS)
file_data_next_buffer_to_fill = 0;
}
ENABLE_INT;
//----- THE IRQ SENDING THE DATA -----
//It wants to move the sending_buffer on to ultimately meet the next_buffer_to_fill
while (#)
{
if (file_data_sending_buffer_byte >= FILE_DATA_BUFFER_LENGTH)
{
file_data_sending_buffer_byte = 0;
file_data_sending_buffer++;
if (file_data_sending_buffer >= FILE_DATA_NUM_OF_BUFFERS)
file_data_sending_buffer = 0;
if(file_data_sending_buffer == file_data_next_buffer_to_fill)
{
//Disable the irq as there's no data available to send yet
//...
break;
}
}
//Get next block of bytes and send them
p_buffer = &file_data_buffers[file_data_sending_buffer][file_data_sending_buffer_byte];
//...
file_data_sending_buffer_byte += 32; //<<<How ever many bytes just got sent
}
Example With Buffer Overwrite If Data Not Sent In Time
This implemenation is based on the function sending the data being an irq which copies all of the data to send in a single irq call and the function filling the data buffers being non irq and ensuring the operation is irq safe.
If the sending function isn't called in time oldest data is overwritten with new data.
#define LIVE_READINGS_NUM_OF_BUFFERS 6
#define LIVE_READINGS_BUFFER_LENGTH 100
int live_readings_next_buffer = 0; //The next buffer to load with data
volatile int live_readings_last_buffer = 0; //The last (oldest) buffer waiting to be sent
BYTE live_reading_buffers[LIVE_READINGS_NUM_OF_BUFFERS][LIVE_READINGS_BUFFER_LENGTH];
//----- THE IRQ SENDING THE DATA -----
//It wants to move the last_buffer on to ultimately meet the next_buffer
if (live_readings_next_buffer == live_readings_last_buffer)
{
//Nothing to send
}
else
{
//Send last_buffer and then increment last_buffer
//Send it (or copy it before leaving the irq)
p_buffer = &live_reading_buffers[live_readings_last_buffer][0];
//Read the buffer data here...
live_readings_last_buffer++;
if (live_readings_last_buffer >= LIVE_READINGS_NUM_OF_BUFFERS)
live_readings_last_buffer = 0;
}
//----- THE DATA COLLECTION FUNCTION FILLING THE BUFFER -----
//It wants to fill the buffer with new data, discarding the oldest data if necessary
//It fills next_buffer and then increments next_buffer
//If there is data waiting to be sent it does the same, but increments last_buffer if it = next_buffer (there will always be 1 unused buffer when data is waiting)
DISABLE_INT;
p_buffer = &live_reading_buffers[live_readings_next_buffer][0];
//Load the buffer here...
live_readings_next_buffer++;
if (live_readings_next_buffer >= LIVE_READINGS_NUM_OF_BUFFERS)
live_readings_next_buffer = 0;
if (live_readings_next_buffer == live_readings_last_buffer)
{
//We are overwriting the last buffer as its not been sent in time
live_readings_last_buffer++;
if (live_readings_last_buffer >= LIVE_READINGS_NUM_OF_BUFFERS)
live_readings_last_buffer = 0;
}
ENABLE_INT;
USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through resources like this. We hope you find it helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support here. If you need help with a problem please use one of the many online forums.