C121 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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 250 to 100 and 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?

Name it

The datasheet, summarized

FeatureThis board
processorRISC-V RV32IM, one instruction per cycle
clock10 MHz (100 ns per cycle)
program memory4 KiB at 0x0000
RAM0x1000 to 0x10000 (60 KiB); the stack starts at the top
supply3.3 V; 2 mA running, 0.5 mA in sleep, 2 µA in deep sleep
pinsP0 to P15, 3.3 V logic, up to 8 mA each and 50 mA in total
peripheralsmemory-mapped from 0x4000_0000

Pins and their alternate functions

PinsUse
P0 to P3general-purpose input or output; PWM channels 0 to 3
P4, P5, P13 to P15general-purpose input or output (P13 is SPI chip select by convention)
P6, P7UART transmit and receive
P8, P9I²C data (SDA) and clock (SCL)
P10 to P12SPI 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.

BasePeripheral
0x4000_0000GPIO: OUT, IN, DIR, PULLUP, SET, CLR, TOGGLE, interrupt registers
0x4000_1000TIMER
0x4000_2000PWM, four channels
0x4000_3000ADC, 12 bits, 0 to 3.3 V, 10 µs per conversion
0x4000_4000DAC, 12 bits
0x4000_5000UART
0x4000_6000I²C
0x4000_7000SPI
0x4000_8000SYS: 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
  1. Current through one LED. The resistor drops what the LED doesn't: I=3.3−2.0330A=3.9mA
  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×3.94mA=15.8mA
  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.

Worked example

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

Show the solution
  1. Add the offset to the base address. 0x4000_0000+0x24=0x4000_0024
  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.

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
  1. One instruction. At 10 MHz one cycle is t=110MHz=100ns
  2. One pass of the loop. 5×100ns=500ns 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

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?

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.