C12 · Lesson 1 · The board, its pins and its datasheet
A microcontroller, and the document that describes it
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.
- 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
250to100and run it again: the chase speeds up. Change the pin list to light only P0 and P2.
Predict
The board's clock runs at 10 MHz, and its processor finishes one instruction per clock cycle. How long does one instruction take?
100 ns: one cycle of a 10 MHz clock is 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.
Name it
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.
Worked 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?
Show the solution
- Current through one LED. The resistor drops what the LED doesn't:
- Compare with one pin. That's within one pin's 8 mA, so each LED gets its own pin.
- Four LEDs on four pins. Their total is well inside the 50 mA package limit:
- 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.
Worked example
What's the address of the GPIO peripheral's IRQ_FLAGS register, at offset 0x24?
Show the solution
- Add the offset to the base address.
- 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.
Worked 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?
Show the solution
- One instruction. At 10 MHz one cycle is
- One pass of the loop. which is 2 million passes a second.
- 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
An LED with forward voltage 2 V hangs from a 3.3 V pin through 220 Ω. What current does it draw?
Write units the way you normally would: 1.5 mA, 1500 µA and 0.0015 A are the same answer.
Check
Choose the series resistor for an LED with 2 V forward voltage to carry 2 mA from a 3.3 V pin.
Write units the way you normally would: 1.5 Ω, 1500 mΩ and 0.0015 kΩ are the same answer.
Check
How many clock cycles of the 10 MHz board pass in 25 µs?
Check
A register sits at offset 08 from a peripheral's base, 40000000. What's its address, in hex?
Check
Pin P5 is bit 5 in the GPIO registers. What mask, in hex, selects only that pin?
Check
8 LEDs each draw 3 mA from their own pins. How much current does the chip supply to them in total?
Write units the way you normally would: 1.5 mA, 1500 µA and 0.0015 A are the same answer.
Check
A program needs to send data to a serial terminal. Which pins does it use?
Check
An LED is wired to P2. Write a MicroPython program that turns it on.
MicroPython on the course's board: machine (Pin, PWM, ADC, DAC, UART, I2C, SPI, Timer, WDT) and time. Each call into the hardware takes about 40 µs. Tab indents; press Esc and then Tab to move on.
Check
An LED with 2.1 V forward voltage is driven from a 3.3 V pin through 100 Ω; a pin may source 12 mA.
(a)
What current flows?
Write units the way you normally would: 1.5 mA, 1500 µA and 0.0015 A are the same answer.
(b)
How many such LEDs can one pin drive?
Check
On the 10 MHz board, consider an interval of 2.5 µs and a loop of 4 one-cycle instructions.
(a)
How many cycles is the interval?
(b)
How many whole passes of the loop fit in it?
Explain why
Why does every peripheral on a microcontroller appear as addresses in memory?
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
Keep practicing
12 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.