---
title: The board, its pins and its datasheet
heading: A microcontroller, and the document that describes it
eyebrow: C12 · Lesson 1 · The board, its pins and its datasheet
xp: 40
---

::: hook
A microcontroller is a whole computer on one chip: a processor, memory, and a set of **peripherals** (pin drivers, timers, converters, serial ports) that connect it to the world. What it can do, and how a program makes it do it, is written down in one document, the **datasheet**: the clock speed, which pin does what, how much current a pin can source, and above all the **register map**, the addresses where a program reads and writes to control each peripheral. Every embedded program, in any language, ends up as reads and writes at those addresses. This course's board is a 10 MHz RISC-V microcontroller (the processor from C8) with sixteen pins and seven peripherals; this lesson is its datasheet.
:::

::: play
- The program lights the LEDs on P0 to P3 one after another, a quarter second each, in MicroPython. Press Run, then watch the replay: the board's LEDs and the timeline below follow the same moments.
- Drag along the timeline to look at any instant. Each lane is one pin, high or low.
- Change `250` to `100` and run it again: the chase speeds up. Change the pin list to light only P0 and P2.
:::
:::

```sim board
lang: micropython
attach: led P0 | red
attach: led P1 | orange
attach: led P2 | yellow
attach: led P3 | green
scope: P0, P1, P2, P3
run: 2 s
code:
from machine import Pin
import time

leds = [Pin(n, Pin.OUT) for n in (0, 1, 2, 3)]

while True:
    for led in leds:
        led.on()
        time.sleep_ms(250)
        led.off()
```

::: predict
## The board's clock runs at 10 MHz, and its processor finishes one instruction per clock cycle. How long does one instruction take?

- [ ] 10 ns
- [x] 100 ns
- [ ] 1 µs
- [ ] 10 µs

::: reveal
100 ns: one cycle of a 10 MHz clock is $1/(10 \times 10^6) = 10^{-7}$ s. A million instructions take a tenth of a second, which is quick for blinking an LED and slow for processing audio sample by sample. Knowing the time per instruction is how embedded programmers estimate whether code is fast enough.
:::
:::

::: nameit
## The datasheet, summarized

| Feature | This board |
|---|---|
| processor | RISC-V RV32IM, one instruction per cycle |
| clock | 10 MHz (100 ns per cycle) |
| program memory | 4 KiB at 0x0000 |
| RAM | 0x1000 to 0x10000 (60 KiB); the stack starts at the top |
| supply | 3.3 V; 2 mA running, 0.5 mA in sleep, 2 µA in deep sleep |
| pins | P0 to P15, 3.3 V logic, up to 8 mA each and 50 mA in total |
| peripherals | memory-mapped from 0x4000_0000 |

## Pins and their alternate functions

| Pins | Use |
|---|---|
| P0 to P3 | general-purpose input or output; PWM channels 0 to 3 |
| P4, P5, P13 to P15 | general-purpose input or output (P13 is SPI chip select by convention) |
| P6, P7 | UART transmit and receive |
| P8, P9 | I²C data (SDA) and clock (SCL) |
| P10 to P12 | SPI clock, data out, data in |

## The register map

Each peripheral is a block of 32-bit registers; a program controls it by writing and reading those addresses.

| Base | Peripheral |
|---|---|
| 0x4000_0000 | GPIO: OUT, IN, DIR, PULLUP, SET, CLR, TOGGLE, interrupt registers |
| 0x4000_1000 | TIMER |
| 0x4000_2000 | PWM, four channels |
| 0x4000_3000 | ADC, 12 bits, 0 to 3.3 V, 10 µs per conversion |
| 0x4000_4000 | DAC, 12 bits |
| 0x4000_5000 | UART |
| 0x4000_6000 | I²C |
| 0x4000_7000 | SPI |
| 0x4000_8000 | SYS: cycle counter, sleep, watchdog |

A register's address is its peripheral's base plus its offset: GPIO's IN register, at offset 0x04, is at 0x4000_0004. In C, `#include "board.h"` names every register; in MicroPython, the `machine` module wraps them in objects like `Pin`.

## Two ways to program it

**MicroPython** runs an interpreter on the chip: quick to write and change, but each call into the hardware costs about 40 µs here. **C** is compiled to the processor's own instructions: more to write, and about a hundred times faster at the hardware. This course writes most labs both ways.
:::

::: example
## An LED with a forward voltage of 2.0 V is driven from a 3.3 V pin through 330 Ω. What current flows, and can one pin drive four of them?

::: solution
1. **Current through one LED.** The resistor drops what the LED doesn't:
   $$\begin{aligned} I &= \frac{3.3 - 2.0}{330}\ \text{A} \\ &= 3.9\ \text{mA} \end{aligned}$$
2. **Compare with one pin.** That's within one pin's 8 mA, so each LED gets its own pin.
3. **Four LEDs on four pins.** Their total is well inside the 50 mA package limit:
   $$4 \times 3.94\ \text{mA} = 15.8\ \text{mA}$$
4. **How far it goes.** Twelve would be near the limit.

**Answer:** each LED draws 3.9 mA, within one pin's 8 mA, so each gets its own pin; four of them draw 15.8 mA, well inside the 50 mA package limit.
:::
:::

::: example
## What's the address of the GPIO peripheral's IRQ_FLAGS register, at offset 0x24?

::: solution
1. **Add the offset to the base address.**
   $$\begin{aligned} &\text{0x4000\_0000} + \text{0x24} \\ &= \text{0x4000\_0024} \end{aligned}$$
2. **Name it in C.** That's `GPIO_IRQ_FLAGS`; the header defines it as a pointer to that address.

**Answer:** IRQ_FLAGS is at 0x4000_0024.
:::
:::

::: example
## The board's processor runs at 10 MHz and most instructions take one cycle. How long is one instruction, and how fast can a 5-instruction loop repeat?

::: solution
1. **One instruction.** At 10 MHz one cycle is
   $$\begin{aligned} t &= \frac{1}{10\ \text{MHz}} \\ &= 100\ \text{ns} \end{aligned}$$
2. **One pass of the loop.**
   $$5 \times 100\ \text{ns} = 500\ \text{ns}$$
   which is 2 million passes a second.
3. **The budget.** That's the budget every timing calculation in this course starts from: a 1 ms interrupt period is 10,000 cycles.

**Answer:** one instruction takes 100 ns, and a 5-instruction loop repeats every 500 ns, 2 million times a second.
:::
:::

::: check
given: Vf = 1.8 | 2 | 2.2 | 3; R = 220 | 330 | 470
answer: (3.3 - Vf)/R*1000
unit: mA
tolerance: 1%

## An LED with forward voltage {Vf} V hangs from a 3.3 V pin through {R} Ω. What current does it draw?

::: right
Right. $(3.3 - V_f)/R$ = {answer} mA.
:::

::: misses
- [3.3/R*1000] Subtract the LED's forward voltage first.
- [any] Not yet. $(3.3 - V_f)/R$.
:::
:::

::: check
given: Vf = 1.8 | 2 | 2.2; I = 2 | 4 | 6
answer: (3.3 - Vf)/(I*1e-3)
unit: Ω
tolerance: 1%

## Choose the series resistor for an LED with {Vf} V forward voltage to carry {I} mA from a 3.3 V pin.

::: right
Right. $(3.3 - V_f)/I$ = {answer} Ω.
:::

::: misses
- [3.3/(I*1e-3)] The resistor only drops what the LED doesn't.
- [any] Not yet. $(3.3 - V_f)/I$.
:::
:::

::: check
given: t = 1 | 25 | 500 | 2500 | 100000
answer: t*10
tolerance: 0.5%

## How many clock cycles of the 10 MHz board pass in {t} µs?

::: right
Right. Ten cycles per microsecond: {answer} cycles.
:::

::: misses
- [t] Each microsecond is ten cycles at 10 MHz.
- [any] Not yet. Microseconds times 10.
:::
:::

::: check
given: off = 4 | 8 | 12 | 36; base = 0x40000000 | 0x40005000 | 0x40008000
base: 16
answer: base + off

## A register sits at offset {off:hex2} from a peripheral's base, {base:hex8}. What's its address, in hex?

::: right
Right. Base plus offset: {answer:hex8}.
:::

::: misses
- [any] Not yet. Add the offset to the base.
:::
:::

::: check
given: n = 3 | 5 | 9 | 13 | 15
base: 16
answer: 2^n

## Pin P{n} is bit {n} in the GPIO registers. What mask, in hex, selects only that pin?

::: right
Right. $2^{\given{n}}$, written {answer:hex4}: a 1 in bit {n} and 0 everywhere else.
:::

::: misses
- [n] That's the bit number; the mask has a 1 in that position, $2^n$.
- [any] Not yet. Shift 1 left by {n}.
:::
:::

::: check
given: N = 4 | 8 | 12; I = 3 | 4 | 6
answer: N*I
unit: mA
tolerance: 0.5%

## {N} LEDs each draw {I} mA from their own pins. How much current does the chip supply to them in total?

::: right
Right. {answer} mA, which must stay under the 50 mA package limit.
:::

::: misses
- [any] Not yet. Add the currents.
:::
:::

::: check
kind: choice

## A program needs to send data to a serial terminal. Which pins does it use?

- [ ] P0 and P1
- [x] P6 (transmit) and P7 (receive)
- [ ] P8 and P9
- [ ] Any two pins it likes

::: misses
- [1] Those are general-purpose pins (and PWM channels).
- [3] P8 and P9 are the I²C bus.
- [4] The UART's pins are fixed on this chip, like most microcontrollers' alternate functions.
:::
:::

::: check
kind: micropython
given: n = 0 | 2 | 3 | 5
attach: led P{n}
run: 0.2
expect: level(P{n}) = 1

## An LED is wired to P{n}. Write a MicroPython program that turns it on.

```micropython starter
from machine import Pin

```

```micropython solution
from machine import Pin

led = Pin(n, Pin.OUT)
led.on()
```

::: right
Right. `Pin(n, Pin.OUT)` sets the pin's direction to output (a write to the GPIO's DIR register), and `on()` sets its level high (a write to SET).
:::
:::

::: check
kind: parts
given: Vf = 1.8 | 2.1 | 3.0; R = 100 | 220 | 470; m = 8 | 12

## An LED with {Vf} V forward voltage is driven from a 3.3 V pin through {R} Ω; a pin may source {m} mA.

::: part
answer: (3.3 - Vf)/R*1000
unit: mA
tolerance: 0.5%

### What current flows?

::: misses
- [3.3/R*1000] Subtract the LED's forward voltage first.
- [any] $(3.3 - V_f)/R$.
:::
:::

::: part
answer: floor(m/((3.3 - Vf)/R*1000))
tolerance: 0

### How many such LEDs can one pin drive?

::: misses
- [any] The pin's limit over one LED's current, rounded down.
:::
:::
:::

::: check
kind: parts
given: t = 0.1 | 2.5 | 125; k = 4 | 7

## On the 10 MHz board, consider an interval of {t} µs and a loop of {k} one-cycle instructions.

::: part
answer: t*10
tolerance: 0.5%

### How many cycles is the interval?

::: misses
- [any] 10 cycles per microsecond.
:::
:::

::: part
answer: floor(t*10/k)
tolerance: 0

### How many whole passes of the loop fit in it?

::: misses
- [any] Cycles over {k}, rounded down.
:::
:::
:::

::: explain
## Why does every peripheral on a microcontroller appear as addresses in memory?

::: model
Making each peripheral's control registers look like memory lets the processor use the instructions it already has, loads and stores, to control hardware: writing a word to the GPIO's OUT address drives the pins, and reading the ADC's DATA address returns a conversion. The chip's address decoder sends accesses in the peripheral region to the peripheral instead of to RAM. That keeps the processor simple, lets any language that can write to an address (C through pointers, MicroPython through its machine module) control the hardware, and makes the datasheet's register map the complete description of what software can do.
:::
:::

::: practice

::: check
given: Vf = 2 | 2.4; R = 150 | 680
answer: (3.3 - Vf)/R*1000
unit: mA
tolerance: 1%

## LED current: {Vf} V LED, {R} Ω, 3.3 V pin?

::: misses
- [any] $(3.3 - V_f)/R$.
:::
:::

::: check
given: t = 3 | 80 | 4000
answer: t*10
tolerance: 0.5%

## Cycles in {t} µs at 10 MHz?

::: misses
- [any] 10 per µs.
:::
:::

::: check
given: n = 2 | 7 | 11
base: 16
answer: 2^n

## Mask for P{n}, in hex?

::: misses
- [any] $2^n$.
:::
:::
:::
