Teaching pack · UAT 204
A 15-week teaching plan, 5 worksheets, a scoring rubric and a map from the course description to the lessons, for instructors to evaluate and adapt before teaching.
Coverage of the course description
The lessons fully teach 10 of 10 description topics. Each topic lists the modules and lesson sections that teach it, and incomplete topics say what is missing.
| Description topic | Modules | Where it is taught | Status |
|---|---|---|---|
| Microcontroller architecture | M1 | Module 1: Inside a microcontroller (CPU, bus, peripherals) and MCUs in drone work (Pixhawk 6X, Pico 2, ESP32-S3) | Covered |
| Memory | M1M2 | Module 1: Flash and SRAM in 'Inside a microcontroller' · Module 2: Memory and coding standards | Covered |
| Input/output ports | M2M3 | Module 2: Manipulating register bits · Module 3: GPIO (pull-ups and reading a button) | Covered |
| Analog-to-digital conversion | M1 | Module 1: The ADC, from voltage to numbers, and Example 1 measuring a 6S battery with a 12-bit ADC | Covered |
| PWM | M3 | Module 3: Timers and PWM, pulse widths for servos and ESCs | Covered |
| Timers | M3 | Module 3: Timers and PWM and Example 1 finding PSC and ARR | Covered |
| Interrupts | M2M3 | Module 2: volatile · Module 3: Polling and interrupts and Example 2 debouncing a button | Covered |
| Serial communications | M4 | Module 4: UART · I2C and SPI · CAN and DroneCAN · Detecting corruption with a checksum | Covered |
| Embedded programming | M2M5 | Module 2: Fixed-width data types, bit manipulation, volatile and BARR-C · Module 5: Tasks, priority and preemption and RTOSs in flight stacks | Covered |
| Interfacing sensors and actuators | M1M3M4 | Module 1: voltage divider into the ADC · Module 3: driving a servo with PWM · Module 4: an I2C sensor and a DroneCAN GPS | Covered |
Weekly teaching plan
| Week | Topic | Pre-class | Lecture | Lab | Evidence |
|---|---|---|---|---|---|
| 1 M1 | Inside a microcontroller | Read module 1 'Why this matters' and 'Inside a microcontroller' | CPU, Flash, SRAM, bus and peripherals; how an MCU differs from a general computer | Worksheet 1 part A: read the Pico 2 pinout, install the tools and write an LED blink program | Pin map marking GPIO, ADC, 3.3 V and ground; working blink program |
| 2 M1 | The ADC: from voltage to numbers | Read the ADC section; run Example 1 | 12-bit resolution, reference voltage and a divider for a 6S battery | Worksheet 1 part B: build the divider, feed it from a supply, read the ADC at three voltages and find a calibration factor | ADC readings against the multimeter, with calibration factor |
| 3 M1 | MCUs in drone work | Read 'MCUs in drone work'; take the module 1 quiz | The two processors in the Pixhawk 6X and the FMU/IO split | Worksheet 1 part C: use the PX4 documentation to map each Pixhawk 6X sensor and port to its processor | Worksheet 1 report and quiz result |
| 4 M2 | Fixed-width data types and overflow | Read module 2 'Fixed-width data types'; run Example 1 | uint8_t to int32_t and an 8-bit counter wrapping round | Worksheet 2 part A: predict then run Example 1; write a uint8_t counter on the Pico 2 showing wrap-around over serial | Predictions against results and the serial log |
| 5 M2 | Manipulating register bits | Read 'Manipulating register bits'; run Example 2 | Setting, clearing, toggling and reading bits with bitwise operators | Worksheet 2 part B: write C on the Pico 2 that sets and clears GPIO control bits with SDK functions, checked against hand calculation | Calculated versus read bit values |
| 6 M2 | volatile, memory and coding standards | Read 'volatile' and 'Memory and coding standards'; take the module 2 quiz | Why optimisation loses an interrupt flag, and the relevant BARR-C rules | Worksheet 2 part C: test a flag with and without volatile; review the group's code against three BARR-C rules | Worksheet 2 report and quiz result |
| 7 M3 | GPIO and reading a button | Read module 3 'GPIO' | Input and output pins, internal pull-ups and contact bounce | Worksheet 3 part A: wire a button with pull-up, read by polling and count presses without debouncing | Counted presses against actual presses |
| 8 M3 | Timers and PWM | Read 'Timers and PWM'; run Example 1 | PSC, ARR and CCR and servo/ESC pulse widths | Worksheet 3 part B: generate 1100, 1500 and 1900 µs pulses, measure them and drive a servo | Captures of the three pulses; PSC and ARR calculated for the Pico 2 |
| 9 M3 | Polling versus interrupts; midterm | Read 'Polling and interrupts'; run Example 2; review modules 1–3 and take the module 3 quiz | Midterm examination (modules 1–3) | Worksheet 3 part C: switch to an interrupt setting a volatile flag, debounce in the main loop and compare counts with polling | Worksheet 3 report and midterm score |
| 10 M4 | UART and real throughput | Read module 4 'UART'; run Example 1 | UART framing, start and stop bits, baud and real throughput | Worksheet 4 part A: set a wrong baud rate on one side, observe the data and explain | Garbled data sample with explanation |
| 11 M4 | I2C, SPI and reading a sensor | Read 'I2C and SPI' | Addresses, ACK, signal lines and when to use SPI | Worksheet 4 part B: connect an I2C sensor, scan addresses, read values and capture SDA/SCL with a logic analyzer | Bus capture labelled with start, address, ACK and data |
| 12 M4 | Checksums, CAN and DroneCAN | Read 'CAN and DroneCAN' and 'Detecting corruption with a checksum'; run Example 2; take the module 4 quiz | Frame integrity checks and using DroneCAN devices on ArduPilot | Worksheet 4 part C: send Example 2 frames over UART, write a receiver checking the checksum, and list the DroneCAN GPS parameters | Worksheet 4 report and quiz result |
| 13 M5 | Tasks, priority and preemption | Read module 5 'Tasks, priority and preemption'; run Example 1 | Rate-monotonic priorities and checking a task set meets its deadlines | Worksheet 5 part A: build a three-task FreeRTOS project (10, 100, 500 ms) and time it by toggling GPIO pins | Timing captures of each task |
| 14 M5 | RTOSs in flight stacks and from MCU to AI | Read 'RTOSs in flight stacks' and 'From MCU to AI'; take the module 5 quiz | RTOSs in PX4 and ArduPilot and the limits of MCUs for AI workloads | Worksheet 5 part B: add a heavy low-priority load, check sensor timing, enable the watchdog and simulate a hung task | Measured timing against Example 1 and a record of the reset |
| 15 M5 | Embedded mini-project | Prepare the mini-project code and report combining worksheets 1–5 | Course wrap-up and project criteria | Mini-project demo: read a sensor (ADC or I2C), send checksummed UART frames and drive a servo under FreeRTOS | Demo, code and mini-project report, scored with the rubric |
Worksheets and instructor guide
One worksheet per module. Students open it on the web, print it or download the .docx. The instructor guide under each worksheet gives the class timing, expected results, discussion questions and notes for students with different backgrounds.
1Worksheet 1 Getting to know the Pico 2 and its ADC
Lab · About 6 h of class time · CLO1
Objective: Identify MCU pins and parts, write a first program, calibrate voltage measurement with the ADC and relate it to a real flight controller
Equipment
- Raspberry Pi Pico 2 and USB cable
- Computer with MicroPython or the C/C++ SDK
- Breadboard, resistors and LEDs
- Adjustable supply
- Multimeter
- Example code at /downloads/uat-204/
Safety
- Never feed more than 3.3 V into an ADC pin; check the calculated value before powering
- Use a supply instead of a real battery, with the current limit set
- Unplug USB before rewiring
Steps
- Read the pinout and mark GPIO, ADC, 3.3 V and ground
- Install the tools as directed and write an LED blink program
- Build the divider and feed it from a supply no higher than the safe value
- Read the ADC at three voltages
- Compare with Example 1 and the multimeter; find a calibration factor
- Use the PX4 Pixhawk 6X page to map sensors and ports to processors
Deliverables
- Annotated pin map
- Blink and ADC code
- ADC against multimeter table with calibration factor
- Pixhawk 6X device-to-processor table
Checklist
- The blink and ADC programs run, and the code actually used is attached
- Table 2 has all 3 voltages, every value has a unit, and every ADC pin voltage is at or below 3.3 V
- The calibration factor calculation (multimeter value divided by the value computed from the code) is shown for at least one row
- Table 1 marks the 3.3 V, ground, ADC and LED GPIO pins matching the pinout
- Table 3 names both the main processor and the IO processor, with the interface of each device
Instructor guide
Class timing
- Objectives and safety briefing: the 3.3 V limit and setting the current limit30 min
- Read the pinout, install the tools and write the blink program90 min
- Build the divider, calculate the pin voltage before powering, read the ADC at three voltages90 min
- Compare with the multimeter and Example 1; find the calibration factor60 min
- Study the Pixhawk 6X page and fill in Table 360 min
- Wrap-up, questions and hand-in checklist30 min
Expected results
- Table 1: at least the 3.3 V pin, a ground pin, one ADC pin and the GPIO pin driving the LED (through a current-limiting resistor). Pin numbers must match the real Pico 2 pinout, not just the pin names.
- Table 2 (with R1 = 100 kΩ, R2 = 10 kΩ and a 3.3 V reference as in Example 1): ratio 0.0909, 1 LSB = 0.806 mV at the pin = 8.86 mV of input voltage. 12.0 V in gives 1.091 V at the pin and a code of about 1354; 9.0 V gives 0.818 V, about 1016; 5.0 V gives 0.455 V, about 564 (if the supply reaches 25.2, 22.2 and 19.8 V the codes are 2844, 2505 and 2234). The calibration factor should be between 0.97 and 1.03 with 1% resistors (about ±1.8% from the resistors); with 5% resistors it may be off by about ±9%. Example: multimeter 12.00 V, computed 12.18 V, factor 0.985.
- Table 3 (from Figure 2 of the lesson): the main processor (FMU), an STM32H753 Cortex-M7 at 480 MHz, connects to the IMU, barometer and compass over SPI and I2C, to GPS and telemetry over UART, and to DroneCAN over CAN; the IO processor, an STM32F100 at 24 MHz, handles PWM outputs and RC input. A good reason given: outputs are offloaded so the main processor can spend its time on state estimation and attitude control.
- A typical difference is 1 to 3% with 1% resistors. A strong answer computes the percentage from its own table and notes that an error that stays the same proportion at every voltage comes from the resistor ratio or the reference voltage, so a calibration factor fixes it, while readings that jump between samples come from noise and need averaging.
- 3.3 / 0.0909 = 36.3 V, but the supply must be set well below it because resistors have tolerance, the real reference may not be exactly 3.3 V, and a wiring mistake or a loose R2 puts the full voltage on the pin. That is why a current-limited supply is used and the pin voltage is calculated before every power-up.
- About 8.86 mV of battery voltage per step (0.806 mV at the pin). That is fine enough to warn of a low battery, because warning thresholds differ by hundreds of mV across the pack. The real limit is accuracy (resistors, reference, noise), not resolution, which is why calibration is needed.
- The main processor must estimate state and control attitude hundreds of times a second with many sensors. Moving PWM outputs and RC input to the IO processor reduces its load and separates output handling from computation. A strong answer cites the real devices in Table 3 rather than just saying it is faster.
Discussion questions
- When choosing an MCU for a drone's add-on sensor module, what must you check besides CPU speed?
- Why do SRAM variables vanish at power-off while the program in Flash stays, and what does that mean for the calibration factor found today?
Adapting to student background
- Upper secondary (M.6)
- Have them calculate the divider ratio, LSB and expected code by hand before wiring, then use the measurements to test the prediction; pair them with a peer confident on the breadboard and have the instructor check the circuit before power-up.
- Vocational certificate
- Let them wire from the diagram step by step and measure with the multimeter first, then use a fill-in table for the calculation (input voltage times 10/110 gives the pin voltage), with Example 1's code checking the numbers instead of long hand working.
- Diploma
- Extension: switch to another safe resistor pair, find the new calibration factor, and add 16-sample averaging to the program, comparing how steady the readings are before and after.
2Worksheet 2 Bits, counters and volatile on the board
Lab · About 6 h of class time · CLO2, CLO1
Objective: Use fixed-width types and bit manipulation correctly, understand the effect of volatile and review code against BARR-C
Equipment
- Pico 2 with the C/C++ SDK
- Computer with Python and a serial terminal
- Module 2 example code
- BARR-C coding standard
Safety
- Low-voltage bench work over USB; no flying
Steps
- Run Examples 1 and 2, predicting results for new starting values
- Write C that sets and clears GPIO control bits with SDK functions
- Compare bit values with hand calculation
- Write a uint8_t counter and show wrap-around over serial
- Test an interrupt flag with and without volatile, optimisation on; record behaviour
- Read three BARR-C rules on types and variables and review the group's code
Deliverables
- Predictions against results
- Bit code and counter log
- volatile experiment notes with explanation
- List of BARR-C fixes
Checklist
- Every row of Table 1 has a prediction written before running and the actual result
- The set and clear code changes only the target bit, never overwrites the whole register, and uses unsigned constants such as 1u << k
- The serial log showing the counter wrapping from 255 to 0 is attached
- The group's code uses fixed-width types from stdint.h, with a list of fixes against the three chosen BARR-C rules
- Table 3 states the optimisation level and explains the behaviour of both versions
Instructor guide
Class timing
- Review fixed-width types and bit operators; brief the task30 min
- Run Examples 1 and 2 with new values, predicting first60 min
- Write C to set and clear GPIO bits on the board and compare with hand calculation90 min
- The uint8_t counter over serial and the volatile experiment90 min
- Read three BARR-C rules and review the group's code60 min
- Wrap-up, questions and hand-in checklist30 min
Expected results
- Table 1 (if the instructor uses these values): Example 1 starting at 253, counting 8 times gives 254 255 0 1 2 3 4 5, difference 8; Example 2 starting at 0b11000110, set bit 4 gives 11010110, clearing bit 0 changes nothing (it is already 0), clearing bit 1 gives 11010100, toggling bit 7 gives 01010100 (0x54), bit 2 = 1 and bits 5..4 = 1; the board counter starting at 0 after 300 increments shows 44. Predictions may be wrong, but the last column must explain why.
- Table 2: the mask for GPIO pin k is 1u << k, e.g. GPIO 15 gives 0x00008000 = 32768. Setting the bit turns that pin's LED on, clearing it turns it off, and the other pins do not change. If SDK functions are used, state which register bit each function sets or clears.
- Table 3: with volatile, the main loop sees the flag every time the interrupt sets it (the LED or message responds every time). Without volatile and with optimisation on, it usually never responds or responds only once, because the compiler reads the flag once and reuses the remembered value. With optimisation off it may appear to work, which is why this bug is hard to find.
- It wraps from 255 to 0. The difference (2 − 250) as uint8_t is 8, which is correct because unsigned subtraction wraps modulo 256 in the same way, as long as fewer than 256 counts pass between the two readings. A strong answer links this to MCU timers that wrap periodically.
- The compiler sees that the main loop never writes the flag itself, so it reads it once and keeps it in a CPU register instead of reading memory again; the ISR's change is never seen. With optimisation off the compiler reads memory every time, so it seems to work. volatile forces a real read every time.
- Overwriting sets every bit to the new value, so other pins already set (such as another LED) switch off or change mode unintentionally. That is why bits are set with OR and a mask, and cleared with AND and the complement of the mask.
- No. Per Arm's guide, volatile is for peripheral registers and variables modified by ISRs; it is not a way to share data between threads or tasks. Use atomics or the RTOS's own synchronisation tools instead.
Discussion questions
- Why should embedded code avoid int when desktop code uses it everywhere?
- Safety-critical systems often forbid dynamic allocation after start-up. What problem does that prevent on an MCU with little SRAM?
- What does a bug that disappears when optimisation is off tell you about testing code before it flies?
Adapting to student background
- Upper secondary (M.6)
- Have them write each register value in binary on paper before running, explain wrap-around with number bases and modulo 256 from school maths, and give extra time for compiling and flashing the board.
- Vocational certificate
- Start from what the board shows (LEDs on and off, the serial counter), then explain the bits with an 8-box table where they shade the changed box; use Python to check answers instead of converting bases by hand.
- Diploma
- Extension: write functions that read and write a multi-bit field (such as bits 5..4) without touching other bits, and test them with three starting values.
3Worksheet 3 Driving a servo and reading a button with interrupts
Lab · About 6 h of class time · CLO2
Objective: Compare polling with interrupts, generate accurate PWM pulses with a timer and drive a servo safely
Equipment
- Pico 2 board
- Push button and breadboard
- Servo and a separate servo power supply
- Oscilloscope or logic analyzer
- Documentation for the servo used
Safety
- Power the servo from a separate supply, never the board's 3.3 V pin, with a common ground
- Clamp the servo and keep fingers clear of the moving horn
Steps
- Wire a button with the internal pull-up; poll it and count presses without debouncing
- Switch to an interrupt setting a volatile flag; debounce in the main loop
- Compare the counts from both methods
- Generate 1100, 1500 and 1900 µs pulses at the servo's documented frequency and measure them
- Connect the servo and command all three pulses
- Calculate PSC and ARR for the Pico 2 clock as in Example 1
- Repeat Example 3 with a different stall time and queue size; record missed, overflow and age
Deliverables
- Press counts for polling and interrupts
- Captures of the three pulses
- PSC and ARR calculation
- Source code
- Table of the repeated Example 3 results
Checklist
- Table 1 has at least 3 runs, each with the real number of presses
- Captures of all 3 pulses from the scope or logic analyzer are attached, with width and period readable
- Box 3 shows f_TIM/((PSC+1)(ARR+1)) with every value substituted and checks ARR fits the counter
- The ISR only sets a volatile flag; heavy work and printing are in the main loop
- The servo uses a separate supply with common ground, and the pulse frequency from its documentation is stated
Instructor guide
Class timing
- Objectives, servo safety and separate power wiring30 min
- Wire the button, count by polling, then switch to interrupt with debouncing90 min
- Calculate PSC and ARR, generate the three pulses and measure them90 min
- Connect the servo and command the three pulses45 min
- Repeat Example 3 with a new stall time and queue size60 min
- Wrap-up, questions and hand-in checklist45 min
Expected results
- Table 1: polling without debouncing usually counts as many or more presses than were made (some buttons several times more; in the lesson's Example 2 one press shows 5 falling edges) and can miss presses if the main loop is busy. Interrupt with debouncing must match the real count in every run; if it still overcounts, increase the debounce time.
- Table 2 (if the servo documentation gives 50 Hz): CCR = 1100, 1500, 1900, duty 5.50%, 7.50%, 9.50%, measured period 20.0 ms. Measured widths should match within ±5 µs (depending on the instrument's resolution). The horn sits at one side, centre and the other side. If the servo uses another frequency, recalculate ARR; CCR still equals the pulse width in µs.
- Box 3: the Pico 2 clock is 150 MHz and we count in 1 µs steps, so PSC = 150 − 1 = 149; a 20 ms period gives ARR = 20000 − 1 = 19999. Check: 150,000,000 / (150 × 20000) = 50.0 Hz, and ARR 19999 is below the 65,535 limit of a 16-bit counter, so it fits.
- Table 4 (suggested runs): main loop at 25, 95, 105 ms with an 8-slot queue gives missed = 7, overflow = 0, but the largest age is still 75 ms and 7 items remain queued; runs at 25, 55, 105 ms with a 4-slot queue give missed = 7, overflow = 4 and ages 15, 35, 75 ms; the lesson's case (25, 95, 105, 4 slots) gives missed = 7, overflow = 4, age 75 ms. Latest value gives age 5 ms in every case.
- The button contacts bounce briefly when pressed, so the signal goes up and down several times and fast polling sees several falling edges. 5 ms is right if the debounced count matches the real presses in every run; if it still overcounts, lengthen it; if it starts missing quick presses, it is too long. A strong answer quotes numbers from Table 1.
- They should differ by no more than about ±5 µs; if more, check PSC and the clock used in the calculation. With the timer counting in 1 µs steps each CCR count is 1 µs, so the pin stays high for exactly CCR microseconds, which keeps the code readable.
- A servo draws high current when it moves. Powered from the board's 3.3 V pin the supply sags until the MCU resets or misbehaves, and the board's regulator may be damaged. A common ground is needed because the control pulse must share the same reference.
- No. A bigger queue reduces overflow, but the delivered data is still 75 ms old and more items pile up; the fix is a faster main loop. A controller should use the latest value with a maximum age, while a logger uses a queue and records the overflow count.
Discussion questions
- When reading very short signals, such as pulses from a motor rpm sensor, why is polling riskier than an interrupt?
- If an ISR passes two fields (tick number and time) to the main loop, why is volatile alone not enough, and how is it fixed?
Adapting to student background
- Upper secondary (M.6)
- Have them calculate PSC, ARR and duty by hand before coding, then sketch the expected waveform to compare with the capture; have the instructor check the servo power wiring before power-up.
- Vocational certificate
- Start by wiring the button and servo to see results first, then use a fill-in table (clock divided by 1,000,000 minus 1 gives PSC) and check with Example 1's code instead of doing all the arithmetic by hand.
- Diploma
- Extension: write a program that sweeps the pulse slowly from 1100 to 1900 µs in 10 µs steps, clamps it to that range, and measure whether each step is visible on the instrument.
4Worksheet 4 Connecting a sensor and sending serial data
Lab · About 6 h of class time · CLO3
Objective: Read a sensor over I2C, analyse bus signals, send checksummed frames over UART and identify DroneCAN device settings
Equipment
- Pico 2 board
- I2C sensor such as the training board's barometer or IMU
- Logic analyzer
- USB-UART adapter
- Computer with Python
- ArduPilot DroneCAN documentation
Safety
- Low-voltage bench work; check the sensor's logic level matches 3.3 V before connecting
- Share ground between the board and the USB-UART adapter
Steps
- Connect the I2C sensor, scan the bus for addresses and read values
- Capture SDA and SCL; identify start, address, ACK and data
- Send Example 2 frames over UART to the computer
- Write a receiver that checks the checksum
- Set a wrong baud rate on one side; observe and explain
- Study ArduPilot's DroneCAN page and list the parameters for a DroneCAN GPS
Deliverables
- Sensor address and readings
- Annotated I2C capture
- Sender and receiver code with checksum results
- Wrong-baud explanation and DroneCAN parameter list
Checklist
- Table 1 gives the I2C address in hex matching the sensor datasheet, and readings have units
- The SDA and SCL capture is attached with start, address, ACK and data all marked
- The receiver checks the start byte, length and checksum, with results for both a good and a damaged frame
- Table 3 includes the wrong-baud case with an explanation of the cause
- A list of the DroneCAN parameters needed for a GPS is given, with values and the documentation page cited
Instructor guide
Class timing
- Objectives; check the sensor logic level and common ground30 min
- Connect the I2C sensor, scan the bus and read values75 min
- Capture SDA and SCL with the logic analyzer and label each part60 min
- Send frames over UART, write the checksum receiver and try a wrong baud rate105 min
- Study ArduPilot's DroneCAN page and list the parameters45 min
- Wrap-up, questions and hand-in checklist45 min
Expected results
- Table 1: the 7-bit address must match the datasheet of the training board's sensor (Figure 2 of the lesson gives 0x68, 0x76 and 0x1E as examples). If nothing is found, check the pull-ups, swapped SDA and SCL, and power. Readings must be plausible, e.g. a barometer gives room air pressure and an IMU at rest measures about 9.8 m/s² on the vertical axis.
- Table 2: start (SDA falls while SCL is high), the address byte with the read or write bit, ACK (SDA low on the 9th clock), data bytes, and stop. Reading 6 bytes takes about 830 µs at 100 kHz or 208 µs at 400 kHz by Example 1's estimate; accept measured times within about ±30%, because the estimate counts bits roughly and there are gaps between bytes.
- Table 3: the good frame aa 04 09 c4 01 2c e4 (7 bytes, values 2500 and 300) passes the checksum; a frame with one flipped bit (4th byte becomes d4) fails; mismatched baud (e.g. sending at 115200, receiving at 57600) gives garbled or missing bytes or framing errors, and nearly every frame fails the checksum.
- 7 bytes; 8N1 uses 10 bits per byte, so at 115200 baud a frame takes about 608 µs (about 1215 µs at 57600, 304 µs at 230400). Every 100 ms is 70 bytes per second, 0.61% of the 11,520 bytes per second at 115200 baud (1.22% at 57600), leaving plenty of room for other data.
- The receiver samples at the wrong moments because the bit lengths differ (by a factor of two for 115200 against 57600), so the bytes are meaningless. The checksum computed from them does not match the checksum byte, so the receiver discards the frame instead of using wrong numbers. A strong answer adds that the checksum does not tell you the cause; you still have to check the baud.
- Not always. XOR misses some damage, such as the same bit flipping in two bytes, which is why real protocols use CRCs. Even when it passes, the data may be too old after waiting in a buffer (lesson Example 3: age 30,296 µs is over the 20,000 µs limit, so it is discarded), or the field may be read with the wrong type, e.g. 0xFF9C is −100 signed but 65,436 unsigned.
- CAN uses a differential CANH and CANL pair, so it resists noise and supports long runs; every device shares one pair with terminating resistors at both ends, which cuts wiring, and DroneCAN devices report their own status. A good parameter list enables a CAN driver on the port, sets that driver's protocol to DroneCAN, sets the bit rate (CAN_P1_BITRATE, default 1,000,000) and sets the GPS type to DroneCAN, with names checked against the documentation for the firmware version used.
Discussion questions
- For an IMU read thousands of times a second and a compass read occasionally, would you use SPI or I2C for each, and why?
- UART data can arrive as half a frame or several frames together. How must the receiver be designed so it does not cut frames wrongly?
- What happens if two I2C devices on one bus have the same address, and how could it be solved?
Adapting to student background
- Upper secondary (M.6)
- Have them convert one frame's bytes to binary and compute the XOR by hand before writing the receiver, and explain the captured waveform to a peer before filling in the table; give extra practice wiring and using the logic analyzer.
- Vocational certificate
- Start by wiring and seeing data flow in the serial terminal, then use one capture the instructor has labelled as a model; start the receiver from Example 2's code and adapt it.
- Diploma
- Extension: add a sequence number to the frame and make the receiver count lost frames as in Example 3, testing it by deliberately skipping some frames on the sender.
5Worksheet 5 Tasks on FreeRTOS
Lab · About 6 h of class time · CLO2, CLO1
Objective: Build a multi-task RTOS system, assign rate-monotonic priorities, compare measured timing with calculation and test the watchdog
Equipment
- Pico 2 with an SDK supporting FreeRTOS
- Logic analyzer
- LED and the sensor from worksheet 4
- Computer with Python and the module 5 example code
Safety
- Low-voltage bench work; no flying
Steps
- Create a FreeRTOS project with three tasks: sensor every 10 ms, frame every 100 ms, LED every 500 ms
- Assign rate-monotonic priorities
- Measure each task's real timing by toggling GPIO pins with a logic analyzer
- Add a simulated heavy load in a low-priority task; check the sensor task stays on time
- Enter measured values into Example 1 and compare calculation with measurement
- Enable the watchdog, simulate a hung task and observe the reset
Deliverables
- FreeRTOS project code
- Task timing captures before and after the load
- Calculation against measurement table
- Watchdog test record
Checklist
- Table 1 has all 3 tasks and the load task, every value in ms
- Logic analyzer timing captures before and after the load are attached
- Priorities follow rate-monotonic order (shorter period, higher priority) and are stated in the code
- Tasks use RTOS delays, not busy-waiting, except the simulated load task
- Box 3 states the watchdog timeout, how the task was hung and the evidence of the reset
Instructor guide
Class timing
- Review tasks, priority, preemption and rate-monotonic order; brief the task30 min
- Build the three-task FreeRTOS project and set priorities90 min
- Measure real timing by toggling GPIO pins with the logic analyzer75 min
- Add the simulated load, measure again and enter values into Example 175 min
- Test the watchdog45 min
- Wrap-up, questions and hand-in checklist45 min
Expected results
- Table 1: measured periods should match 10, 100 and 500 ms within about one RTOS tick. Run times C depend on the group's code; values such as sensor 1.0 ms, frame 2.0 ms and LED 0.1 ms are reasonable. After the low-priority load is added, the sensor and frame tasks must stay on period, while the load task itself runs slower or gets less time.
- Table 2 (if measured C = 1.0, 2.0 and 0.1 ms): U = 1.0/10 + 2.0/100 + 0.1/500 = 0.1202, bound for 3 tasks 0.780, simulated missed = 0; adding a load of 300 ms every 1 s gives U = 0.4202, bound for 4 tasks 0.757, missed = 0; even a load of 870 ms per 1 s (U = 0.9902) misses nothing in the simulation. The board should agree: the sensor task never misses its period.
- Box 3: when one task hangs (for example an endless loop that never feeds the watchdog), the system resets after the watchdog timeout. Evidence: the LED restarts its power-on pattern or the start-up message appears again on serial. The time before the reset should be close to the timeout set.
- With the values above U is about 0.12, far below the 0.780 bound, so every task is guaranteed to meet its deadline, and the board should show no missed periods. If results differ, a strong answer finds the cause, such as C measured without interrupt time, or an RTOS tick coarser than the period needed.
- A preemptive scheduler lets a higher-priority task interrupt a lower one at once, so the load only gets leftover time. If the load had higher priority than the sensor task, the sensor task would wait until the load finished or blocked and would miss its 10 ms period, which in a flight controller means control degrades immediately.
- No. The bound is only a sufficient condition. In Example 1, adding AI at 6 ms every 20 ms gives U = 0.95, above the 0.743 bound, yet the simulation misses nothing because the periods divide into each other almost exactly; it needs a closer check by simulation or measurement. If U exceeds 1 (AI at 8 ms gives U = 1.05), deadlines are certainly missed.
- A watchdog resets the system when the program hangs and fails to check in on time, so the controller recovers instead of staying frozen in flight. If a hung task could still feed it (for example from a timer interrupt that keeps running), the watchdog would never notice, so it should be fed only when every important task has reported it completed its work.
Discussion questions
- A flight controller must read the IMU and write logs at the same time. How should their priorities be set, and what happens to the flight if logging stalls?
- Why should image object detection not run on the same MCU as control, even with TinyML?
Adapting to student background
- Upper secondary (M.6)
- Have them compute U and the bound by hand before running Example 1 and predict the measurements; pair them with a peer confident with the logic analyzer and flashing the board.
- Vocational certificate
- Start from an instructor-prepared one-task FreeRTOS project and add one task at a time, viewing the logic analyzer after each; use a fill-in table of C divided by T for each task, then add them up.
- Diploma
- Extension: raise the load step by step until the lowest-priority task starts missing its period, record U at that point, and compare it with Example 1's simulation.
Scoring rubric
For worksheets and the mini-project. Each criterion scores level (1–4) ÷ 4 × weight.
| Criterion | Weight | Excellent (4) | Good (3) | Fair (2) | Needs work (1) |
|---|---|---|---|---|---|
| Correct programs and peripheral configuration | 30% | Programs work in every case; ADC, timer and baud settings correct; types and volatile used properly | Nearly all correct with minor flaws | Partly working or some settings wrong | Does not work |
| Measurement and verification with instruments | 25% | Pulses, bus traffic and task timing confirmed with scope or analyzer and fully compared with calculations | All measured, partly compared | Incomplete or misread measurements | No measurement evidence |
| Code quality against embedded standards | 20% | Readable, meets the chosen BARR-C rules, no heavy work in interrupts | Readable with minor rule breaches | Hard to read or several breaches | Unstructured |
| Recording results and explaining system behaviour | 25% | Complete report explaining effects such as bounce, wrong baud and task contention correctly | Complete, mostly explained | Parts missing or explanations off target | No explanation |
Assessment map: CLOs and how they are assessed Draft
The CLOs are a web-team draft; the course owner sets the final ones (curriculum book, part 4). This table is built from the worksheet and module-quiz data, so it follows any change to the CLOs.