cchakri.aiLearn All courses
// COURSE · EMBEDDED SYSTEMS

Embedded C

Learn C for microcontrollers, memory-mapped peripherals, interrupts, communication buses, and reliable firmware.

01

C foundations for hardware

Syllabus

  1. 01Types, integer behavior, and bit operations
  2. 02Pointers, arrays, and memory
  3. 03Headers, compilation, and debugging

Detailed content

Types, integer behavior, and bit operations

Embedded hardware exposes registers and data fields at exact widths. Use fixed-width integer types where width matters, and understand signedness, overflow, shifts, masks, and integer promotion. Set or clear bits with explicit masks, and verify operations with tests or register-value inspection.

Pointers, arrays, and memory

Pointers hold addresses; arrays occupy contiguous elements, while a pointer is a separate value. Stack storage is limited and short-lived; static storage persists for the program; dynamic allocation may be unavailable or risky in firmware. Check bounds, ownership, and alignment before accessing memory-mapped or externally supplied data.

Headers, compilation, and debugging

A C program is preprocessed, compiled, assembled, and linked into a target image. Headers declare interfaces; source files define them. Cross-compilers produce code for a different processor than the host. Enable warnings, inspect linker maps, and use a debugger or hardware probe to examine registers and execution.

02

Microcontroller peripherals

Syllabus

  1. 01Memory-mapped I/O and GPIO
  2. 02Timers, counters, and PWM
  3. 03ADC, sensors, and calibration

Detailed content

Memory-mapped I/O and GPIO

Microcontroller peripherals are controlled through registers located at defined addresses. GPIO pins can be configured as inputs or outputs with electrical properties such as pull-ups. Read the datasheet and reference manual for register behavior. Use volatile for hardware registers so the compiler does not remove required accesses.

Timers, counters, and PWM

Timers count clock ticks and can generate periodic interrupts, capture input timing, or produce PWM signals. Derive prescalers and periods from the clock frequency and required timing. PWM controls average output power for motors or LEDs, but actual waveform quality depends on resolution and timer configuration.

ADC, sensors, and calibration

An ADC converts an analog voltage into a digital sample with finite resolution and noise. Choose the correct reference voltage and sampling rate. Sensor readings may need filtering and calibration against known values. Keep units explicit and detect out-of-range readings rather than letting bad measurements silently control outputs.

03

Interrupts and real-time behavior

Syllabus

  1. 01Interrupt service routines
  2. 02Scheduling and deadlines
  3. 03Concurrency and resource safety

Detailed content

Interrupt service routines

An interrupt pauses normal execution to handle a hardware event. Keep the handler short: capture essential data, clear the source, and signal a task for longer work. Avoid blocking calls and unsafe shared-state changes inside an interrupt. Account for interrupt priority and protect shared variables appropriately.

Scheduling and deadlines

A real-time system must produce results within known timing bounds. A cooperative loop is simple but can be delayed by long tasks; an RTOS schedules tasks by priority and readiness. Calculate worst-case execution and response time for critical work, and ensure higher-priority tasks cannot starve lower-priority tasks indefinitely.

Concurrency and resource safety

Tasks and interrupts may access the same data, creating races. Use brief critical sections, atomic operations, queues, or mutexes according to the execution context. Avoid holding a lock while performing slow I/O. Watch stack size and memory fragmentation, and define behavior when buffers or queues fill.

04

Communication and firmware reliability

Syllabus

  1. 01UART, SPI, and I²C
  2. 02CAN, USB, and networked devices
  3. 03Boot, updates, and field diagnostics

Detailed content

UART, SPI, and I²C

UART provides asynchronous serial communication using configured baud rates. SPI uses a clock and chip-select lines for fast full-duplex transfers; I²C shares clock and data wires with addressed devices. Confirm electrical levels, pull-ups, timing, and bus ownership. Use logic-analyzer traces to diagnose framing and protocol errors.

CAN, USB, and networked devices

CAN supports robust multi-node communication with arbitration and error detection, common in automotive and industrial systems. USB and network interfaces use richer protocols and state machines. Validate message identifiers, payload length, checksums, timeouts, and authorization before acting on received commands.

Boot, updates, and field diagnostics

A bootloader initializes hardware and can validate or install firmware images. Secure updates verify authenticity and integrity before replacing a working image. Include rollback when an update fails. Watchdogs recover from a stuck system; diagnostic logs should be compact, useful, and safe for the device’s memory and privacy requirements.