How do I get data out of an ISR safely?
I've been making decent progress on learning how to use C/C++ to replace an existing C# embedded application. I've cut a few corners along the way and I'd like to see if I can clean up the worst ones. Right now I have an ISR getting pressure data from a UART instrument. My app is a pretty classic state machine and what ever state object is active will need that pressure data. Right now, the UART Callback ISR for the pressure instrument just writes the data to my StateVariable struct in main:
struct StateVariables
{
double pressure;
string pressureTimestamp;
};
static StateVariables SV;void usart6RXIdleCallback(UART_HandleTypeDef *huart)
{
const uint8_t engrMsgSize = 64;
char engrMsg[engrMsgSize] = "";
SV.pressureTimestamp = mcuRTC_SBE41.getCPFTimestamp();
SV.pressure = parsePressureMsg(usart6RXBuffer, sizeof(usart6RXBuffer));
Any object that needs the latest pressure value just gets it from the SV struct and can check the time stamp to make sure it isn't a stale value. I understand my SV struct is really just a global variable and globals are not generally a good thing. Although in this case, the only thing that will ever write to the SV.pressure and SV.pressureTimestamp is the USART Callback. Regardless, a better solution would probably be to have the ISR publish the new pressure value to a list of subscribers or stuff it in a list of FIFOs but that seems like a lot of overhead to handle in an ISR that I'm trying to keep as simple as possible. This seems like a standard thing to do in an embedded system and I'd be very interested to learn how real programmers handle this.
Thanks - Gene
