I have been thinking about my design for a while but couldn't figure out a better way to deal with buffer overrun in multiple interrupts.
A dsPIC33EP chip is connected to a TFT display, a UART sensor, a micro-SD card and a UART camera. The baudrate of the UART sensor is 921600 and the camera (50k per pic) is 57600. The SD card sector write (512 bytes) time is a little bit less than 2ms. The screen (EVE FT800) takes 20ms to update.
The goal is to respond to the UART sensor (at least every 30ms) and update screen accordingly (update labels) and at the same time write pictures into the SD card.
To break down the task, I have successfully saved multiple pictures to the SD card using interrupts when only camera and SD card are running.
The interrupt routine:
volatile unsigned char buff[512];
volatile unsigned int ptr=0;
volatile unsigned char EOF=0;
volatile unsigned char buffReady=0;
void ISR(){
static unsigned char temp;
temp=UART_Read();
buff[ptr++]=temp;
if(ptr==512){ptr=0;buffReady=1;}
if(...){EOF=1;}
}
While the program is stuck in a loop to check EOF flag
while(1){
if(buffReady){SD_Write_Sector(buff);buffReady=0;}//write 512 bytes of image to SD card
if(EOF){break;}//jump out if the image has been finished
}
My concern is, how do I incorporate the routines to update the screen while receiving data from the UART sensor? It looks like the buffReady flag must be checked constantly. Any delay between each check may lose couple bytes since they are overwritten with new bytes.
Since the picture size is around 50K and the baudrate is 57600, there will be six bytes coming from the camera every millisecond. If the screen introduces a 20ms delay in between, the picture will sure be corrupted.
I thought about using a larger buffer to store pictures but larger buffer needs more time to be written to the SD card.
How should I arrange update screen and write image to SD card?