← TATVON
PUBLIC FLAGSHIP LESSONAutomotive Embedded C · volatile
SELECTED MODULE

Embedded C

MCU, memory, interrupts and production safety

MODULE PROGRESS01 / 20
CHAPTER 02

Hardware-facing C

Write code that safely communicates with peripherals whose state changes independently of normal program execution.

AUTOMOTIVE EMBEDDED C50 min · Concept + ECU example + failure + evidence

volatile exact semantics

CASE 05 · CAN RECEIVE PATH

The frame arrived.
The task never saw it.

At -O0 everything works. In the optimized release, the 10 ms communication task occasionally misses the flag written by the CAN Rx ISR.

CANoe traceFrame received ✓
ISR breakpointISR executed ✓
10 ms taskFlag not seen ✕
TC3xx ECUCAN RxISR
value = 1
10 ms TASKwhile(flag == 0)stuck
01 · THE IDEA

What does volatile mean?

The value may change outside the current code flow. The compiler must perform the required access to the real object instead of trusting an older value kept in a CPU register.

volatile controls compiler access.It does not create a lock, atomicity or multicore synchronization.
02 · WHERE AUTOMOTIVE SOFTWARE USES IT

Only where the value changes asynchronously.

ISR ↔ TASKCommunication flag

CAN, LIN or timer ISR updates a status read by a cyclic task.

MCU HARDWAREPeripheral status

Controller hardware changes a bit while software polls its fixed register.

DMA ENGINECompletion state

DMA updates a descriptor independently of normal CPU execution.

DO NOT ASSUMENot synchronization

Use OS resources, atomics or queues when the real problem is a race or event loss.

03 · CORRECT AUTOMOTIVE PATTERN

One shared object. Two execution contexts.

The object is zero-initialized in .bss RAM. The qualifier changes access rules—not its storage area.

static volatile uint8_t CanRxPending; /* .bss RAM */

void Can_RxIsr(void)
{
  CanRxPending = 1u;
}

void Com_10msTask(void)
{
  if (CanRxPending != 0u)
  {
    CanRxPending = 0u;
    ProcessCanFrame();
  }
}
AUTOMOTIVE RUNTIME LAB

CAN Rx ISR → 10 ms communication task

Compare the generated runtime behaviour with and without volatile.

CPU REGISTERR0 = 0Required reload/store
.bss RAM · CanRxPending0Normal RAM section—not a special volatile area
ASYNC SOURCE10 ms TaskValue changes outside current task flow
01 · Task reads 0

10 ms task loads CanRxPending from .bss RAM.

05 · ENGINEERING DECISION

Useful—but not free.

What it solves
  • Required reads and writes remain observable.
  • Hardware polling matches the intended runtime model.
  • Assembly retains the expected load/store evidence.
What it cannot solve
  • Atomicity, races, lost events or ordering.
  • Multicore cache coherency.
  • Poor architecture hidden by excessive qualifiers.
06 · ROOT-CAUSE INVESTIGATION

Debug the evidence—not the keyword.

Symptom: CAN frame is present, ISR runs, but the optimized task does not process it.

  1. Reproduce exactly

    Replay the same CAN stimulus and preserve timestamps.

  2. Prove the producer

    Confirm ISR entry and RAM write at the flag address.

  3. Find the first mismatch

    RAM becomes 1, but task control flow behaves as 0.

  4. Compare assembly

    Check -O0 versus release: repeated load or one cached read?

  5. Select the correct fix

    Use volatile for visibility; counter, queue, atomic or OS resource for stronger guarantees.

  6. Stress the correction

    Repeat under CAN burst, timing variation and optimized build.

07 · CODING DECISION LAB

Two CAN frames arrive before the 10 ms task runs.

A Boolean flag can represent only “pending” or “not pending.” Which design preserves both events?

PROFESSIONAL VERIFICATION

Do not stop at “it works”

Observe

Inputs, addresses, state, timing and generated instructions.

Compare

Expected contract against the first different runtime boundary.

Prove

Repeat the scenario and demonstrate the correction under realistic conditions.

KNOWLEDGE CHECK

What is the primary guarantee of volatile?

YOUR LEARNING RECORDSave this lesson across devices
TATVON AI MENTOR

Think through volatile exact semantics

Ask for an explanation, an automotive example or a question that tests your understanding.

Checking mentor availability…