Catto / Topic Map · Digital Techniques II Level 4
Digital Techniques II · 120 h · Topic 4 of 6

Embedded systems hardware and software

A sensor that sleeps at 2 µA lasts more than two years on a CR2032 cell. If a peripheral is left on and the sleep current rises to 50 µA, the same cell lasts four months.

Microcontroller Boot code Interrupts Low power Watchdog

01A computer nobody sees

An embedded system is a computer dedicated to one task inside a product: the control of a washing machine, an airbag unit, a gas meter, a pacemaker. It does not look like a computer and is almost never reprogrammed, but it has a processor, memory, peripherals and software. What distinguishes it is the set of constraints: cost per unit, power consumption, size, guaranteed response times and years of operation without anyone restarting it.

The technician program presents the microcontroller families, their peripherals and the interrupts. Here we see how the complete system is designed.

02The hardware

BlockWhat has to be decided
ProcessorMicrocontroller, processor with an operating system, or FPGA, depending on the load and the cost
MemoryFlash for the program and SRAM for the data, with margin for updates
ClockInternal RC oscillator (cheap, ±1 %) or crystal (±20 ppm, needed for USB, CAN or a time-of-day clock)
PowerLinear or switching regulator, voltage supervision (brown-out) and power-up sequencing
Reset and supervisionReliable reset circuit and watchdog timer that restarts the system if the software hangs
Inputs and outputsProtection against discharges and overvoltages on everything that leaves the board
PlatformWhen it is the right choice
Microcontroller (Cortex-M, AVR, RISC-V)Control and data acquisition with guaranteed timing, microamp consumption in sleep
Application processor running LinuxGraphical interface, complex networking, gigabytes of memory; boot time of seconds
FPGA or SoC with FPGAParallel processing at speeds that software cannot reach

03The software

A microcontroller’s program starts without an operating system to prepare it. The boot code locates the vector table, initializes the stack, copies the initialized variables from flash to RAM, zeroes the rest and only then calls main. From there on there are three possible structures:

StructureHow it worksLimit
Main loopA while(1) that polls and services everything in orderThe response time is the duration of the complete loop
Foreground and backgroundInterrupts handle what is urgent and leave flags; the main loop does the restInterrupt routines have to be short and share data carefully
Real-time operating systemIndependent tasks with priorities and a schedulerMore memory and complexity; covered in the RTOS unit

On top of the hardware the code is organized in layers: drivers that know the registers, a hardware abstraction layer (HAL) that hides them, and the application, which should not know which microcontroller it runs on. Two details of the C language become critical: volatile on every variable modified by an interrupt or by the hardware, so that the compiler does not keep it in a register, and atomic access to the data shared between an interrupt and the main loop.

04Power consumption and battery

A battery-powered node spends almost all its time asleep: it wakes up, measures, sometimes transmits and goes back to sleep. The average current is the charge of each state divided by the period:

\[ \bar I = \frac{I_{act}\, t_{act} + I_{rad}\, t_{rad} + I_{dor}\, t_{dor}}{T} \] \[ t_{vida} = \frac{0.8\; C}{\bar I + I_{auto}} \] \( C \) is the battery capacity, of which it is wise to count only 80 %, and \( I_{auto} \) is the self-discharge expressed as a current. With short wake-ups, what decides the lifetime is the sleep current: 2 µA versus 50 µA is the difference between years and months.
Lab · how long a sensor’s battery lasts

A microcontroller that wakes up periodically, measures for 10 ms at 5 mA and every so often transmits by radio for 5 ms at 20 mA. The bar divides the charge consumed among the states; the chart below shows the shape of the current, with the time scale distorted so that the pulses can be seen.

05Development, debugging and reliability

The program is compiled on a computer for a different processor (cross-compilation) and is flashed and debugged through a JTAG or SWD interface, which allows you to halt the processor, step through the code and read memory without modifying the program. For timing problems, the oscilloscope and the logic analyzer are as necessary as the debugger: a pin that toggles on entering and leaving an interrupt measures its real duration.

A system that has to work for years unattended is designed to fail well: the watchdog restarts a hung program, voltage supervision prevents it from running with insufficient power, remote updates keep a backup image, and coding guidelines such as MISRA C restrict the language constructs most prone to errors.

06In the lab

Activity 1 · Measuring the real consumption

With a 10 Ω resistor in series with the supply of a microcontroller and an oscilloscope, measure the sleep current and the current during wake-ups. Calculate the lifetime of a CR2032 and compare it with the lab.

Activity 2 · Duration of an interrupt

Set a pin high on entering an interrupt routine and low on leaving it, and measure its duration and its variation with the oscilloscope.

Activity 3 · The watchdog

Enable the watchdog, deliberately cause an infinite loop and verify that the system restarts and records the cause of the reset.

07Common mistakes

  • Forgetting volatile. The compiler optimizes the read of a flag and the loop never sees the change the interrupt made.
  • Long interrupt routines. They delay all the others; the interrupt sets a flag and the loop does the processing.
  • Leaving a peripheral on during sleep. A converter or a floating pin can multiply the sleep current by twenty.
  • Disabling the watchdog for debugging and not enabling it again.

08Self-assessment

A sensor draws an average of 3 µA. How long does a 225 mAh CR2032 last, using 80 % of it?

\( 180\ \text{mAh} / 3\ \mu\text{A} = 60\,000 \) h, almost 7 years, not counting self-discharge.

What charge does a 10 ms wake-up at 5 mA consume?

50 µC, or 0.0139 µAh.

Why is a variable modified in an interrupt declared volatile?

So that the compiler reads it from memory every time, instead of assuming it has not changed since the last read.

When is a crystal needed instead of the internal RC oscillator?

When frequency accuracy is required: communications such as USB or CAN, a time-of-day clock or precise time measurements.

09Further reading

  • Elecia White. Making Embedded Systems. 2nd ed., O'Reilly, 2024. Embedded system design from software architecture to power consumption, with a lot of practical experience.
  • Joseph Yiu. The Definitive Guide to ARM Cortex-M3 and Cortex-M4 Processors. 3rd ed., Newnes, 2014. The processor in almost all current microcontrollers, with startup, interrupts and low-power modes.
  • Jack Ganssle. The Art of Designing Embedded Systems. 2nd ed., Newnes, 2008. How an embedded project is organized so that it works and can be maintained.
Development of the topic “Embedded systems hardware and software” of Digital Techniques II (Level 4), based on the curriculum of the UTN Electronic Engineering program, 2023 curriculum — Ordinance No. 1849 of the UTN Higher Council. Back to the Topic Map · catto.ar