Search topics...
C / C++ ConceptsFunction Pointers and Callbacksfoundational

What is a callback and how is it used in embedded systems?

0 upvotes
Practice with AISoon

A callback is a function passed by pointer to be called later when an event occurs. In embedded systems, callbacks are the primary mechanism for decoupling drivers from application logic:

c
typedef void (*uart_rx_cb_t)(uint8_t byte);
void uart_register_rx_callback(uart_rx_cb_t cb);

The UART driver calls cb(received_byte) whenever a byte arrives. The application registers its handler without the driver needing to know what it does. Common uses: timer expiration, DMA complete, protocol parsers, state machine transitions.

Safety consideration: always null-check before calling (if (cb) cb(data);) and place constant dispatch tables in flash using const to prevent corruption.

Source: C / C++ Concepts Q&A