Skip to main content
Be Bitwise

Peripherals & Communication

Microcontroller Programming · Embedded Systems & Firmware track

5 min read

In this module

Talking to the Physical World

Microcontrollers reach into the physical world through peripherals, hardware blocks built into the MCU that talk to external sensors, actuators, displays, and other devices. Understanding these protocols at the signal level is the bread and butter of embedded programming, and a prerequisite for hardware security analysis.

GPIO: General Purpose Input/Output

GPIO pins are the humblest peripheral. Each pin gets configured as either an input (reading external voltage levels) or an output (driving the line high or low). LEDs, buttons, chip-select signals, generic digital interfaces — all GPIO.

Every GPIO pin has its own configuration registers covering mode (input/output/alternate function/analogue), output type (push-pull or open-drain), speed, and the internal pull-up/pull-down resistors.

ADC and DAC

ADC (Analogue-to-Digital Converter) turns an analogue voltage (0V up to Vref) into a digital number. A 12-bit ADC on a 3.3V system maps 0V to 0 and 3.3V to 4095. You'll meet ADCs in sensor reads (temperature, pressure, light), battery voltage measurement, and audio sampling.

Take a reading of 2048 from that 12-bit, 3.3 V ADC. The voltage is 2048 × 3.3 / 4095 ≈ 1.65 V, just over half scale. In binary it's a single set bit, bit 11, the top bit of the twelve:

211
210
29
28
27
26
25
24
23
22
21
20
Decimal2048
Hex0x800
Binary100000000000

Clear the leftmost bit and set the one next to it and you get 1024, about 0.83 V. Each bit is worth half the voltage of the bit to its left, and the lowest bit is worth about 0.8 mV, which is the finest step this ADC can resolve.

DAC (Digital-to-Analogue Converter) runs the opposite direction, converting a digital number back to an analogue voltage. Used for audio output, motor speed control (with filtering), and generating reference voltages.

Communication Protocols

MCUs communicate with external devices through standard serial protocols:

UART (Universal Asynchronous Receiver/Transmitter): the simplest serial protocol going. Two wires, TX and RX, carry data at an agreed baud rate (something like 115200 bps). No clock signal travels with the data, so both sides have to agree on timing in advance. You'll find UART on debug consoles, GPS modules, and Bluetooth modules.

text
UART frame: [START][D0][D1][D2][D3][D4][D5][D6][D7][STOP]
1 bit ---------- 8 data bits ---------- 1 bit

That's the common 8N1 setting: 8 data bits, no parity, 1 stop bit. Both ends can also agree on 7 or 9 data bits, an even or odd parity bit, or 2 stop bits.

SPI (Serial Peripheral Interface): synchronous, four wires — MOSI (data out), MISO (data in), SCK (clock), CS (chip select). The master drives the clock, which is how SPI hits speeds in the tens of megahertz. Flash chips, SD cards, displays, and high-speed sensors all live on SPI buses.

I2C (Inter-Integrated Circuit): also synchronous but down to just two wires, SDA (data) and SCL (clock). Multiple devices share the same bus, each addressed by a unique 7-bit ID. Slower than SPI but pin-thrifty. The natural home for EEPROMs, temperature sensors, accelerometers, and real-time clocks.

Protocol Security Implications

These protocols have no authentication or encryption. Any device on an I2C bus can read all traffic. SPI chip select can be spoofed. UART is plaintext. For security-critical applications, these protocols must be protected either by physical isolation (the bus is inside a tamper-resistant enclosure) or by adding application-level encryption and authentication on top of the raw protocol.

Timers and PWM

Hardware timers are counters that increment at a configurable rate. They are used for:

  • Delays: Count timer ticks for precise timing without busy-waiting
  • PWM (Pulse Width Modulation): Generate a square wave with controllable duty cycle. Used for LED dimming, motor speed control, and servo positioning
  • Input capture: Measure the timing of external events (like measuring a pulse width)
  • Watchdog: Reset the MCU if the software fails to periodically "pet" the watchdog timer (detecting hangs)
c
// Configure timer for 1 kHz PWM on channel 1
TIM3->PSC = 83; // Prescaler: 84 MHz / 84 = 1 MHz timer clock
TIM3->ARR = 999; // Auto-reload: 1 MHz / 1000 = 1 kHz PWM
TIM3->CCR1 = 500; // 50% duty cycle (500/1000)
TIM3->CCMR1 = (6 << 4) | TIM_CCMR1_OC1PE; // PWM mode 1, CCR1 preload on
TIM3->CCER = 1; // Enable channel 1 output
TIM3->CR1 = TIM_CR1_ARPE | TIM_CR1_CEN; // ARR preload on, start timer

DMA for Efficient Data Transfer

DMA (Direct Memory Access) controllers move data between peripherals and memory without involving the CPU at all. This becomes indispensable once throughput climbs:

  • ADC with DMA: Continuously sample analogue inputs and store results in a buffer
  • UART with DMA: Receive or transmit large data blocks without per-byte interrupts
  • SPI with DMA: High-speed data transfer to/from external flash or displays
c
// Configure DMA to transfer UART received data to a buffer.
// On the STM32F4, USART2_RX is wired to DMA1 stream 5, channel 4.
RCC->AHB1ENR |= RCC_AHB1ENR_DMA1EN; // Clock the DMA controller
DMA1_Stream5->CR &= ~DMA_SxCR_EN; // Stream must be off to configure
while (DMA1_Stream5->CR & DMA_SxCR_EN);
DMA1->HIFCR = DMA_HIFCR_CTCIF5 | DMA_HIFCR_CHTIF5 | DMA_HIFCR_CTEIF5 |
DMA_HIFCR_CDMEIF5 | DMA_HIFCR_CFEIF5; // Clear old flags
DMA1_Stream5->PAR = (uint32_t)&USART2->DR; // Source: UART data register
DMA1_Stream5->M0AR = (uint32_t)rx_buffer; // Destination: RAM buffer
DMA1_Stream5->NDTR = BUFFER_SIZE; // Number of bytes to transfer
DMA1_Stream5->CR = (4U << DMA_SxCR_CHSEL_Pos) | // Channel 4 = USART2_RX
DMA_SxCR_MINC | // Increment memory address
DMA_SxCR_TCIE | // Interrupt when done
DMA_SxCR_EN; // Enable DMA stream
USART2->CR3 |= USART_CR3_DMAR; // Let USART2 raise RX DMA requests
// Now UART data arrives in rx_buffer without CPU intervention
// CPU is free to do other work until DMA transfer complete interrupt

DMA is how resource-starved MCUs hit serious I/O throughput. Without it, the CPU babysits every byte and has nothing left for the actual computation.

The Peripheral Ecosystem

A modern MCU like the STM32F4 has dozens of peripherals: multiple UARTs, SPI buses, I2C buses, timers, ADCs, DACs, DMA controllers, USB, CAN bus, random number generators, cryptographic accelerators, and more. Mastering embedded programming means understanding how to configure and orchestrate these peripherals to build a complete system from hardware building blocks.