MakerSpace Amiens · Microcontrollers

Communicating and structuring

Lecture 3: serial buses, game loop and state machine

Alban Petit

On the agenda

  1. Why buses
  2. UART, I2C, SPI
  3. Choosing your bus
  4. The game loop
  5. The state machine
2UniLaSalle Amiens

What you will be able to do

By the end of this lecture, you will be able to:

  • say why a serial bus exists, and tell UART, I2C and SPI apart
  • choose the right bus for a peripheral, and wire it correctly
  • write a game loop that never blocks
  • recognise a situation where a state machine is needed
  • implement it with an enum and a switch

The first two make the chip talk to the world; the last three keep your code readable.

Communication buses Finite state machine

3UniLaSalle Amiens

Quiz: what is left from Lecture 2

  1. Between 0.8 V and 2.5 V, what does an ESP32-S3 input read?
  2. Why does an unconnected input read anything at all?
  3. With INPUT_PULLUP, what is read when the button is pressed?
  4. Can an open-drain output impose a HIGH?
  5. Why does a single press sometimes count twice?
  6. Which pins to read the joystick while Wi-Fi is running?
  7. How do you run a 12 V motor from a pin?
4UniLaSalle Amiens

Quiz: answers

  Answer
1 Nothing reliable: it is the undefined zone
2 It floats: high impedance, nothing fixes its voltage
3 LOW: the logic is inverted
4 No: it pulls to 0 V, a pull resistor provides the HIGH
5 Contact bounce; filter it with millis()
6 The ADC1 pins (ADC2 is unusable with Wi-Fi)
7 A “logic level” MOSFET, and a flyback diode

High impedance, open-drain and millis() come back today, with I2C and the game loop.

GPIO and the digital world ADC and PWM

5UniLaSalle Amiens

Why buses

Making components talk

One wire per piece of information?

The course’s display (240 × 320) receives 76,800 pixels per frame, i.e. more than 150,000 bytes in 16-bit colour. A sensor returns a temperature on 16 bits. An SD card, blocks of 512 bytes.

Wiring one wire per bit would be impractical: no pins left, an unreadable board, and interference between tracks.

A serial bus sends the bits one after the other, over two to four wires. Width is traded for time, and the chip is fast enough for it not to show.

Communication buses

7UniLaSalle Amiens

Serial or parallel

Comparison of a parallel link, eight wires and a single shot, and a serial link, one wire and eight successive shots

In parallel, one wire per bit: fast, but bulky. In serial, a single data wire: the bits go one after the other.

Communication buses

8UniLaSalle Amiens
Question The two possible answers
How many parties? Point-to-point (two) or multipoint (several)
A shared clock? Synchronous (yes) or asynchronous (no)
Both directions at once? Full-duplex (yes) or half-duplex (taking turns)
How is the recipient chosen? An address in the message, or a select wire

These four answers are enough to describe UART, I2C and SPI.

Communication buses

9UniLaSalle Amiens

Three buses, three trade-offs

  • UART: the simplest. Two wires, a single party, no shared clock
  • I2C: two wires, several components told apart by an address
  • SPI: four wires, a clock, the fastest of the three

None is better in absolute terms: each trades speed for wires, or wires for complexity. The choice is made by the need.

Communication buses

10UniLaSalle Amiens

UART

Two wires, point-to-point

The simplest of the three

UART wiring: each device's TX connected to the other's RX, and a common ground

Point-to-point, full-duplex, asynchronous: without a clock, the speed (the baud rate, often 115,200 baud) is set in advance on both sides.

Communication buses

12UniLaSalle Amiens
  Crossing is mandatory

One device’s TX connects to the other’s RX, and vice versa. TX-to-TX wiring transmits nothing, and it is silent: no error message will warn you, you will just see a module that “does not answer”.

Communication buses

13UniLaSalle Amiens

Anatomy of a frame

Timing diagram of an 8N1 UART frame for the letter A: idle at 1, start bit at 0, eight data bits from least to most significant, stop bit at 1

The start gives the signal, the receiver counts the bits at the agreed speed. 8 bits, no parity, 1 stop: 8N1. Different format or speed: gibberish.

Communication buses

14UniLaSalle Amiens

A frame, for real

Oscilloscope screenshot showing a serial frame, with the cursors measuring the duration of one bit

The cursors measure 103.6 µs per bit, i.e. 9600 baud. Screenshot: Haji akhundov, via Wikimedia Commons, under CC BY-SA 3.0.

Communication buses

15UniLaSalle Amiens

Where you already use it

Serial.begin(115200);                 // the serial monitor port
Serial.printf("x=%d y=%d\n", x, y);

The serial monitor is handled like a UART. UART is also the bus of GPS, Bluetooth or GSM modules, and the simplest way of making two microcontrollers talk.

On many boards, a USB-serial chip (CP2102, CH340) translates UART into USB and requires a driver on Windows. The XIAO has none: Serial goes directly through the ESP32-S3’s native USB, and UART0 stays free on D6/D7.

The serial port in VSCode Checking the toolchain

16UniLaSalle Amiens

I2C

Two wires, several components

One bus, addresses

I2C bus: one master and three slaves, each with its address, connected to two shared wires SDA and SCL, with two pull-up resistors to 3.3 V

Multipoint, synchronous (SCL), half-duplex. Each slave has a 7-bit address; the master writes it at the head of each message.

Communication buses

18UniLaSalle Amiens

An I2C exchange

I2C timing diagram: START, seven address bits, R/W bit, ACK, eight data bits, ACK, STOP, with one SCL clock pulse per bit

Start, address + R/W, slave ACK, data (one ACK per byte), stop. No ACK after the address: nobody answers at that address.

Communication buses

19UniLaSalle Amiens

Pull-ups are not optional

SDA and SCL are open-drain, as in Lecture 2: components can only pull the line LOW, never HIGH. This is precisely what allows several of them to share the wire without a short circuit.

Two pull-up resistors (~4.7 kΩ) to 3.3 V are therefore needed. Without them, the line never rises and nothing communicates.

Most commercial modules already include them. If you chain several, a single pair is enough for the whole bus: no need to stack them. ↓ for the details.

Communication buses GPIO and the digital world

20UniLaSalle Amiens

Sharing a wire without a short circuit

Two open-drain components on the same line, with a pull-up resistor to 3.3 V: one releases the line, the other pulls it to 0

Anyone can pull the line to 0 or release it, never impose a 1: two components cannot contradict each other.

GPIO and the digital world

20.1UniLaSalle Amiens

Who is there? The I2C scanner

You do not always know the address. Sweep them all:

for (byte addr = 1; addr < 127; addr++) {
  Wire.beginTransmission(addr);
  if (Wire.endTransmission() == 0) {
    Serial.printf("Found: 0x%02X\n", addr);
  }
}

Missing from the scan? The problem is the wiring or the pull-ups.

Communication buses

21UniLaSalle Amiens

Standard speeds

Mode Speed
Standard 100 kHz
Fast 400 kHz
Fast+ 1 MHz
High Speed 3.4 MHz

More than enough for sensors or a small monochrome OLED display, over one or two metres at most. Far too slow for a colour TFT display: at 400 kHz, a 240 × 320 pixel frame would take more than three seconds.

Communication buses

22UniLaSalle Amiens

SPI

Four wires, full speed

The display’s bus

Wire Role
MOSI Data from master to slave
MISO Data from slave to master
SCK Clock, generated by the master
CS Chip Select: one wire per slave

Synchronous transmission (the clock paces each bit, so no baud rate to agree on) and full-duplex (both directions at once).

Communication buses

24UniLaSalle Amiens

One master, several slaves

SPI bus: one master and three slaves sharing SCK, MOSI and MISO, each connected to the master by its own CS wire

No address: the CS at 0 designates the slave. The others leave MISO in high impedance. From a few MHz up to 80 MHz on the ESP32-S3.

Communication buses

25UniLaSalle Amiens

An SPI exchange

SPI timing diagram: CS goes to 0, eight SCK clock pulses, one bit sent on MOSI and one bit received on MISO at each pulse, then CS goes back up

At each clock pulse, one bit leaves and one bit comes back: full-duplex. No address, no acknowledgement: that is what makes it so fast.

Communication buses

26UniLaSalle Amiens

The display wiring

Signal Pin Role
SCK D9 (GPIO8) SPI clock
MOSI D10 (GPIO9) Data from CPU to display
DC D8 (GPIO7) Command / data switch
RESET D7 (GPIO44) Display hardware reset
CS GND Display always selected
LED 3V3 Backlight, always on

MISO is not wired (write only) and CS is at GND: a single slave, always selected.

ST7789 SPI TFT display

27UniLaSalle Amiens

The display wiring, wire by wire

VCC and LED to 3V3, GND and CS to GND, RESET to D7, DC to D8, SDI to D10, SCK to D9

CS is connected to GND: a single slave, always selected.

The modules of the kit

27.1UniLaSalle Amiens

In practice, you will not write the SPI

lib_deps =
    adafruit/Adafruit ST7735 and ST7789 Library@^1.11.0
Adafruit_ST7789 tft(&SPI, -1, D8, D7);  // CS (none), DC, RESET
SPI.begin(D9, -1, D10, -1);             // SCK, MISO, MOSI, CS
tft.init(240, 320, SPI_MODE3);
tft.fillScreen(ST77XX_BLACK);
  The classic sticking point

Wrong pins give a black screen, not a compilation error. If the screen stays off while everything compiles, first check the pins passed to SPI.begin() (SCK on D9) and SPI_MODE3, mandatory when CS is connected to GND.

The modules of the kit

28UniLaSalle Amiens

Choosing your bus

The dashboard

Criterion UART I2C SPI
Wires (excluding ground) 2 2 3 + 1 CS per slave
Link point-to-point multipoint multipoint
Clock asynchronous synchronous synchronous
Direction full-duplex half-duplex full-duplex
Recipient none to choose 7-bit address CS wire
Acknowledgement no yes (ACK) no
Speed ~1 Mbit/s 0.1 to 3.4 Mbit/s up to 80 Mbit/s
Typical use debugging, modules sensors, OLED displays, SD card

Communication buses

30UniLaSalle Amiens

The decision tree

flowchart LR
  B{"Need<br/>speed?"} -->|Yes| C["SPI<br/>display, SD card"]
  B -->|No| D{"Several<br/>components?"}
  D -->|Yes| E["I2C<br/>sensors, OLED"]
  D -->|No| F["UART<br/>debug, GPS, BT"]

Our project: a serial port for the monitor, SPI for the display. ↓ for the page.

Communication buses

31UniLaSalle Amiens

The concept online

31.1UniLaSalle Amiens

The game loop

Never block

Read, compute, display


void loop() {
  static uint32_t lastFrameMs = 0;
  uint32_t nowMs = millis();
  if (nowMs - lastFrameMs < FRAME_TIME_MS) {   // 16 ms
    return;                                    // not time yet
  }
  lastFrameMs = nowMs;

  float joystickX   = normalizeJoystick(analogRead(PIN_JOYSTICK_X), joystickCenterX);
  bool startPressed = justPressed(selectButton);          // 1. read
  gameUpdate(game, joystickX, startPressed, nowMs);       // 2. compute
  rendererDraw(game);                                     // 3. display
}

This framework will not change any more: the Pong of Lab 4 and then the project’s Snake will enrich gameUpdate() and rendererDraw().

Pong on the console

33UniLaSalle Amiens

The game loop over time

One frame every 16 ms: read, compute, display, then a wait handed back to loop

The remaining time is never lost in a delay().

From hardware to software

33.1UniLaSalle Amiens

Why 16 ms

16 ms

per frame, i.e. 60 frames per second. Below that, the eye sees stutter.

4 M

CPU cycles available in those 16 ms, at 240 MHz. Enough to do a lot.

If a frame takes more than 16 ms to compute, the game slows down instead of skipping frames. First suspect when things are slow: a forgotten delay(), or a full redraw of the screen instead of only the modified areas.

Pong on the console

34UniLaSalle Amiens

The state machine

Getting out of the spaghetti

Code that gets tangled

A game has several stages in its life: a welcome menu, a game in progress, a game over screen. Handled by hand, they give this:

bool inGame = false;
bool gameOver = false;
bool menuActive = true;
// and it gets worse with every new phase

What does inGame && gameOver mean? Nothing. And yet nothing prevents it.

Finite state machine

36UniLaSalle Amiens

An FSM is three things

  • a finite set of states, transitions, a single active state
stateDiagram-v2
  [*] --> MENU
  MENU --> PLAYING : START button
  PLAYING --> GAME_OVER : lives == 0
  GAME_OVER --> MENU : RETRY button

Finite state machine

37UniLaSalle Amiens

The same thing, as a table

State \ Event START lives == 0 RETRY
MENU PLAYING - -
PLAYING - GAME_OVER -
GAME_OVER - - MENU

A “-“ cell means “event ignored in this state”. The table translates line by line into a switch/case, and reveals the forgotten cases.

Drawing it before coding takes five minutes, and saves two hours.

Finite state machine

38UniLaSalle Amiens

enum and switch

enum GameState { MENU, PLAYING, GAME_OVER };
GameState state = MENU;                   // the current state, only one

void loop() {
  readInputs();
  switch (state) {
    // one case per state
  }
}

An enum names the states, a variable remembers the active one. The switch does the rest.

Finite state machine

39UniLaSalle Amiens

One case per state


case MENU:
  if (startPressed()) { drawGame(); state = PLAYING; }
  break;
case PLAYING:
  updateGame();              // and redraws what has moved
  if (playerLives == 0) { drawGameOver(); state = GAME_OVER; }
  break;
case GAME_OVER:
  if (retryPressed()) { drawMenu(); state = MENU; }
  break;

A full screen is drawn once, at the transition, never at every pass. And startPressed() is only true once per press (edge).

Finite state machine

40UniLaSalle Amiens
  Two meanings of the word "state"

In the same session, the word state refers to two things: the game data (ball position, score: these are variables) and the game phase (MENU, PLAYING, GAME_OVER: this is the FSM). Keep the distinction in mind so as not to mix them up.

Finite state machine

41UniLaSalle Amiens

Doing something only once

Resetting the score or clearing the screen must not be repeated 60 times per second. It is run at the moment of the transition, not in the case:

void goTo(GameState next) {
  state = next;
  stateEntry = millis();                      // time of entry
  if (next == PLAYING)   initGame();
  if (next == GAME_OVER) drawGameOver();
}

This is the entry action (onEnter). All the transitions go through here.

Finite state machine

42UniLaSalle Amiens

A state that switches on its own

The “GAME OVER” screen goes back to the menu after three seconds, without blocking anything:

case GAME_OVER:
  if (millis() - stateEntry > 3000) {
    goTo(MENU);
  }
  break;

FSM and millis() combine naturally: this is the timed transition. The time of entry into the state, stored by goTo(), serves as the reference.

Finite state machine From hardware to software

43UniLaSalle Amiens

The timed transition, as a diagram

stateDiagram-v2
  MENU --> PLAYING : press
  PLAYING --> GAME_OVER : no lives left
  GAME_OVER --> MENU : 3 s elapsed

The bottom arrow depends on no input: only on the time spent in the state.

Finite state machine

43.1UniLaSalle Amiens

The canonical example: the traffic light

stateDiagram-v2
  [*] --> GREEN
  GREEN --> AMBER : 5 s
  AMBER --> RED : 2 s
  RED --> GREEN : 5 s

Three states, purely timed transitions, no button.

Finite state machine

44UniLaSalle Amiens

The traffic light, in full

void loop() {
  unsigned long el = millis() - stateEntry;

  switch (state) {
    case GREEN: if (el > 5000) goTo(AMBER); break;
    case AMBER: if (el > 2000) goTo(RED);   break;
    case RED:   if (el > 5000) goTo(GREEN); break;
  }
}

No delay(): the program could also read a pedestrian button.

Finite state machine

45UniLaSalle Amiens

What the FSM gains you

Approach Readability Extensibility Typical bugs
Booleans low hard contradictory states
FSM enum high one more case none

In the project, this FSM will extend to the network states of the multiplayer Snake.

Finite state machine

46UniLaSalle Amiens

Activity: your Pong’s FSM

Draw your game’s state machine

  1. What are the states? (MENU, PLAYING, PAUSE?, GAME_OVER)
  2. Which events trigger each transition?
  3. What is the initial state?
  4. Fill in the table: are there any forgotten cells?

Compare with the neighbouring pair: the differences are instructive.

Finite state machine

47UniLaSalle Amiens

Key takeaways

  • A serial bus sends the bits one after the other
  • UART: 2 wires, TX/RX crossed, same baud rate on both sides
  • I2C: 2 wires, addressing, ~4.7 kΩ pull-ups mandatory
  • SPI: 4 wires, one CS per slave, the only fast one for a TFT
  • The game loop paces at 16 ms with millis(), never blocking
  • An FSM = states + transitions + a single active state
  • Entry action at the transition, never in the case

Communication buses Finite state machine

48UniLaSalle Amiens

And now: the labs, then the project

Session What you are going to do
Lab 1 Install, flash, read the serial port
Lab 2 Test the modules of the kit: GPIO, ADC, PWM, SPI
Lab 3 Design your board in KiCad
Lab 4 Solder the board, write a Pong on it: game loop and FSM
Project A multiplayer Snake, the FSM extended to the network

The three lectures are over: everything you have just seen is used from Lab 2 onwards.

Course syllabus The modules of the kit

49UniLaSalle Amiens
UniLaSalle Amiens, Demain commence ici