Projects / Data logger / Wrap up
Wrap up
Keep going
Pick one when you want more:
- Sample through an erase. Copy the vector table and
SysTick_Handlerinto RAM, point the core at the RAM table with theVTORregister, and readings keep coming while flash is busy. - A watchdog. Start the independent watchdog (IWDG) and feed it from the main loop. If the loop ever hangs, the chip resets itself and the log carries on.
- Real sleep.
wfiis the lightest sleep mode. Try Stop mode between readings, woken by the real-time clock, and measure the current with a multimeter on the Nucleo's IDD jumper. - Faster clock. Set up the PLL to run the F401 at 84 MHz. You will need to wait for the PLL to lock, set flash wait states first, and change
CPU_HZ. - A better dump. Print records as CSV and write a short Python script that reads the serial port and draws a graph.
- An external sensor. Read a sensor over I2C, such as a small digital thermometer, so you are measuring the room instead of the chip.
What this version leaves out, and what a real logger does
- We never change the clock, so the chip runs at 16 MHz from its internal oscillator, which drifts by about 1%. A real logger uses a crystal, and keeps time with the real-time clock, which runs from a battery when the main power is off.
- Our
t=restarts at every boot. A real logger stores the date and time in each record. - The internal sensor measures the chip, which runs warmer than the room, and ST describes it as better at spotting changes than at absolute readings. A real thermometer uses a separate sensor.
- We stall through flash erases and lose time. Real firmware runs time-critical code from RAM, or uses a chip with two flash banks so it can erase one and run from the other.
- Our log loses half its records at each sector switch. Real flash file systems spread erases over many sectors, track wear, and survive bad blocks.
- We never checked anything on a real board for this guide. Expect to find something we missed, and treat it as practice: the reference manual is the final word.
Check yourself
Answer each question in your head first, then open it to check.
What are the first two words of the vector table, and why does the second one end in an odd number?
The first is the starting value of the stack pointer, the top of RAM. The second is the address of the reset handler. A Cortex-M runs only Thumb instructions, and the lowest bit of a jump address set to 1 says "Thumb". An even address would make the chip fault on the first jump.Why does
Reset_Handlercopy.datafrom flash but only zero.bss?Globals with a starting value need that value, and RAM holds garbage at power-on, so the values are stored in flash and copied over. Globals without one start at zero by the rules of C, so there is nothing to copy and filling with zeros is enough.In Step 2.2, why did removing
volatilefromms_countturnticks_delayinto a loop that never ends?Withoutvolatile, the compiler is allowed to assume a variable changes only when the code it can see changes it. Nothing in the loop changesms_count, so it read the value once and made the loop either end at once or never end. It cannot see the interrupt.volatileforces a fresh read every time round.In Step 3.5, a 1000 ms stall at a 10 ms rate dropped exactly 36 readings. Where does 36 come from, and how big must the queue be to drop none?
In 1000 ms, 100 readings are taken. The queue holds 64 of them while the main loop is stuck, so 100 − 64 = 36 have nowhere to go. To drop none, it must hold at least 100, and in practice more, as a margin.Teach it back: explain to a teammate why
store_mountlooks for the last slot that is not empty, instead of the last slot that holds a valid record.A slot that holds half a record, cut off by a power failure, has had some of its bits programmed. Flash can only turn 1s into 0s, so that slot cannot be written again until the whole sector is erased. If the store treated the slot as free, its next write would mix with the old bits and fail. So anything not all 1s counts as used, and the store writes after it.
Sources
- ST: RM0368 reference manual for the STM32F401, for every register address and bit in this guide: RCC, GPIO, USART, ADC and the flash controller.
- ST: STM32F401RE datasheet, for the temperature sensor's calibration addresses and typical values, pin functions, and flash erase times and endurance.
- ST: Nucleo-64 user manual (UM1724), for the LED on PA5 and the ST-LINK serial port on PA2 and PA3.
- Arm: Cortex-M4 Devices Generic User Guide, for the vector table, SysTick, the NVIC and
wfi. - QEMU: STM32 boards, for what the netduinoplus2 machine emulates.
- GNU ld manual: linker scripts, for
MEMORY,SECTIONS,AT,KEEPandNOLOAD.