11 Review questions and further exercises
11.1 Review questions
Q1
Give two reasons why you must not read the accelerometer over I²C inside the button’s interrupt handler (ISR).
- I²C communication takes time, and during it other interrupts and all tasks (including BLE) stop
- The I²C library itself may use interrupts or waits, and calling it from inside an ISR can cause deadlocks or hangs
In the ISR only post an event, and read the sensor in a handler. (Chapters 5 and 8)
Q2
Why does event_post() use xQueueSendToBackFromISR inside an ISR and xQueueSendToBack elsewhere?
Because the normal FreeRTOS API may make the caller wait (block) when the queue is full, and you cannot wait inside an ISR. From an ISR, use the FromISR version, which does not wait and instead returns “whether a higher-priority task was woken”. The executing context is determined from the IPSR register so that callers can use it without thinking about this. (Chapter 3)
Q3
What problems can occur if you put const uint8_t *data (a pointer to the buffer received over BLE) directly into the event queue?
By the time it is taken out of the queue and used in a handler, the BLE library may have reused that buffer for the next reception or freed it. As a result you read the contents of a different command, or invalid memory. Copy the needed bytes into the event payload and post that. (Chapters 3 and 10)
Q4
What happens if you call delay(500) inside a handler during measurement? Explain in light of the 100 ms timer and the queue length (32).
For 500 ms the main loop cannot take out the next event. Meanwhile the 100 ms timer posts 5 events and the 1-second timer 0–1, so 5–6 accumulate in the queue. It does not overflow immediately, but
- responses to BLE commands are delayed by up to 500 ms
- the every-100 ms samples are read together (almost simultaneously) after 500 ms, so the sampling interval is no longer uniform
- if this handler is called frequently, consumption cannot keep up with production, and eventually the queue overflows and events are discarded (
droppedincreases)
(Chapters 3 and 10)
Q5
What does a nullptr cell in the state transition table mean? Also describe the difference from making it a “do-nothing function” instead of nullptr.
nullptr expresses “in this state, ignore this event”, and dispatch() does nothing and returns the current state. A “do-nothing function” behaves the same, but with nullptr you can tell it is “ignore” just by looking at the table. On the other hand, if you “want to log that the event was ignored” or “want to return an error response to ignored commands”, a design with a common “reject” handler is also possible. (Chapter 4)
11.2 Further exercises
11.2.1 Exercise A: saving data to flash
Save the per-second aggregates to the XIAO nRF52840’s QSPI flash (2 MB).
- At measurement start, erase the destination region. Erasing takes time, so don’t erase it all at once in a handler; create an “erasing” state and erase one block per timer event
- Allow the saved data to be read out one record at a time via a BLE command
11.2.2 Exercise B: power saving when idle
- If the device remains idle with BLE disconnected for 5 minutes, power off the IMU and stop the 100 ms timer
- Restore when the button is pressed
- If it continues for another 30 minutes, enter System OFF (wake with the button)
Add “IDLE” and “SLEEP” states to the state transition table, and first design on which events the transitions happen in the table before implementing.
11.2.3 Exercise C: unit tests on the PC
Build an environment that tests the behaviour of dispatch() with pio test -e native. Replace the drivers/ functions (imu_read_magnitude_mg(), ble_send() etc.) with test fakes. Try making every cell of the state transition table a test case.