C12 Embedded systems in MicroPython and C · Practice
Problem set 1: the board, blinking, bits and memory
Problems dealt one at a time, with new numbers every time. The kinds you miss come back more often, and you can keep going as long as you like.
Check
An LED with a 2.1 V forward voltage must carry 2 mA from a 3.3 V pin.
(a)
What series resistor does it need?
Write units the way you normally would: 1.5 Ω, 1500 mΩ and 0.0015 kΩ are the same answer.
(b)
Each on its own pin, how many such LEDs fit within the 50 mA package limit?
Check
A delay must last 400 µs, built from a loop of 4 cycles per pass.
(a)
How many clock cycles is that?
(b)
About how many passes does the loop need?
Check
A peripheral starts at 40002000; a register sits at offset 4 (decimal).
(a)
What's the register's address, in hex?
(b)
Counting from 0x4000_0000 in 4 KiB blocks, which block number is the peripheral in?
Check
A program drives P2 and P5.
(a)
What mask selects both pins, in hex?
(b)
What's ~mask & 0xFFFF, the mask of every other pin, in hex?
Check
A C loop toggles a pin once per pass, 4 cycles per pass; a MicroPython loop toggles it with 2 hardware calls per pass.
(a)
What frequency does the C loop make?
Write units the way you normally would: 1.5 kHz, 1500 Hz and 0.0015 MHz are the same answer.
(b)
And the MicroPython loop?
Check
An LED blinks at 4 Hz with a 10% duty cycle.
(a)
How long is it on each period?
Write units the way you normally would: 1.5 ms, 1500 µs and 0.0015 s are the same answer.
(b)
And off?
Check
A fixed-sleep loop sleeps 250 ms and makes 3 hardware calls (40 µs each) per pass.
(a)
How long is each pass?
Write units the way you normally would: 1.5 ms, 1500 µs and 0.0015 s are the same answer.
(b)
Compared with an exact 250 ms grid, how far behind is it after an hour of passes?
Check
A uint8_t holds 230; 90 is added to it.
(a)
What does it hold?
(b)
What would a uint16_t hold?
Check
A register holds C3F0.
(a)
What's the 4-bit field at bit 4, in hex?
(b)
What's the register after writing 9 into that field, in hex?
Check
A field is 10 bits wide.
(a)
What's the largest value it holds?
(b)
What's that value times 1?
Check
A table holds 50 values of 2 bytes each, and the board has 60 KiB of RAM.
(a)
How many bytes does it take?
(b)
What percentage of the RAM is that?
Check
A C register write takes 3 instructions; a MicroPython one takes 40 µs. A task makes 20 thousand writes.
(a)
How long does it take in C?
Write units the way you normally would: 1.5 ms, 1500 µs and 0.0015 s are the same answer.
(b)
And in MicroPython?
Check
A chip has 16 KB of RAM. The program has 1 KB of .data and 6 KB of .bss; its deepest call chain is 20 frames of 48 bytes, plus one 64-byte interrupt frame.
(a)
How much stack does it need?
(b)
How much RAM is left over?
Check
A lookup table of 512 entries of 2 bytes is declared without const.
(a)
How much RAM does it waste?
(b)
How much memory does it occupy in all, counting its copy in flash?
Check
A recursive function puts a 64-byte buffer on the stack and recurses 8 levels deep; each frame also holds 16 bytes of saved registers.
(a)
How much stack does the recursion take?
(b)
How much if the buffer is made static?
A kind of problem counts as solid once you've got it right on the first try twice in a row. Back to the lesson · Practice every topic in C12