cwalter-at / freemodbus

BSD licensed MODBUS RTU/ASCII and TCP slave
768 stars 393 forks source link

BARE demo portevent.c variables have a lack of volatile qualifier. No event received from a bus with optimization enabled. #38

Open Nikolay-Po opened 1 year ago

Nikolay-Po commented 1 year ago

Demo portevent.c variables are optimized-out by current compilers. No events received from a bus. Here, demo/BARE/port/portevent.c:

/* ----------------------- Variables ----------------------------------------*/
static eMBEventType eQueuedEvent;
static BOOL     xEventInQueue;

Both variables MUST be declared with volatile qualifier:

/* ----------------------- Variables ----------------------------------------*/
static volatile eMBEventType eQueuedEvent;
static volatile BOOL     xEventInQueue;

This is because both variables are changing in an interrupts, unexpectedly for the compiler. When I compiled my RTU port using BARE demo template, I discovered it works without an optimization and doesn't with high level. The single failure point was portevent.c with variable definition. It is obvious that these variables need to be volatile to be processed correctly in MODBUS poll function.