# CubeMX settings: the pump controller

ARGYNIX Skills (worldskills.blog), lesson "Firmware that does not hang".
Our own settings sheet for the pump controller: a state machine with a debounced button
and level switch, the independent watchdog (IWDG), and the run limit saved in flash.
Board: NUCLEO-F103RB. The code to add is in `pump_controller_user_code.c`.

Labels are as STM32CubeMX 6 shows them. If one reads a little differently in your version,
look for the same word nearby. Part 5 tells you whether every setting really reached the
code.

## 1. New project, clock and debug

As in the timers lesson (`timers_pwm_cubemx.md`, parts 1 to 3): MCU Selector
**STM32F103RBTx**, SYS Debug **Serial Wire**, Timebase Source **SysTick**, RCC High Speed
Clock **BYPASS Clock Source**, 72 MHz with APB1 at 36 MHz. No ADC this time.

Keep the Timebase Source on SysTick. The program defines its own
`HAL_TIM_PeriodElapsedCallback` for TIM3. With a TIM as the timebase, CubeMX writes one
too, and the two clash.

## 2. Pins

| Pin | Mode | Settings (System Core > GPIO) | What it is |
|---|---|---|---|
| PC13 | GPIO_Input | No pull-up and no pull-down | B1, the blue button. The board has its own pull-up: LOW when pressed. |
| PA10 | GPIO_Input | Pull-up | The level switch, to GND. Closed (LOW) = tank full. |
| PA8 | GPIO_Output | Output level Low | The pump: an LED with a 1 kOhm resistor to GND, or a relay driver |
| PA5 | GPIO_Output | Output level Low | LD2, the status LED |

Low at start-up matters: the pump must be off from the first microsecond, before the
program has decided anything.

## 3. Peripherals

| Where | Setting | Value | Why |
|---|---|---|---|
| Timers > TIM3 | Clock Source | Internal Clock | |
| TIM3 > Parameter Settings | Prescaler (PSC) | 71 | 72 MHz / 72 = 1 MHz |
| | Counter Period (ARR) | 999 | 1 MHz / 1000 = 1 kHz: an interrupt every 1 ms |
| TIM3 > NVIC Settings | TIM3 global interrupt | Enabled (ticked) | The debounce runs in its callback |
| System Core > IWDG | Activated | ticked | |
| IWDG > Parameter Settings | IWDG counter clock prescaler | 8 | |
| | IWDG down-counter reload value | 1249 | 8 x (1249 + 1) / 40 kHz = 250 ms with a typical LSI. The LSI is only promised between 30 and 60 kHz, so the real time is 167 to 333 ms. |
| Connectivity > USART2 | Mode | Asynchronous, 115200 Bits/s, 8 Bits, None, 1 | The state log on the ST-LINK's virtual COM port. Sending only, so no interrupt. |

How to choose the watchdog time: above the longest pass of the main loop, and short enough
to stop a hang quickly. Here the longest pass is a flash save: a page erase takes 20 to
40 ms. The shortest real timeout, 167 ms, is four times that.

## 4. Generate and open

Project Manager: Toolchain / IDE **STM32CubeIDE**, then **GENERATE CODE**. In
STM32CubeIDE: **File > STM32 Project Create/Import > Import STM32 Project** the first
time, **F5** (Refresh) after each new generate. Copy the blocks of
`pump_controller_user_code.c` into `Core/Src/main.c`, each between its own
`USER CODE BEGIN` and `USER CODE END` comments.

## 5. Check the generated code

- `MX_IWDG_Init()`: `hiwdg.Init.Prescaler = IWDG_PRESCALER_8;`, `hiwdg.Init.Reload = 1249;`.
  `HAL_IWDG_Init()` starts the watchdog, so from that line on it runs: the program must
  reach `HAL_IWDG_Refresh()` in time from then on.
- `MX_TIM3_Init()`: `htim3.Init.Prescaler = 71;`, `htim3.Init.Period = 999;`.
- `stm32f1xx_hal_msp.c`: `HAL_NVIC_EnableIRQ(TIM3_IRQn);`.
- `stm32f1xx_it.c`: `TIM3_IRQHandler()` calls `HAL_TIM_IRQHandler(&htim3);`.
- `MX_GPIO_Init()`: `GPIO_PULLUP` for PA10, `GPIO_NOPULL` for PC13, and PA8 and PA5 written
  LOW before they become outputs.

## 6. Debug and test

- **Live Expressions**: `state`, `settings`, `btn` and `lvl`. Hold B1: `btn.count` reads
  20 and `btn.stable` 1. Let go: both go back to 0. The 20 ms climb is too fast for Live
  Expressions to show; to watch it, put a breakpoint in `debounce_step` and step.
- The watchdog and breakpoints: the IWDG keeps counting while a breakpoint holds the
  core, unless something freezes it. The debug configuration's Debugger tab has a setting,
  Suspend watchdog counters while halted, and the download also calls
  `__HAL_DBGMCU_FREEZE_IWDG()` in USER CODE SysInit, so it does not depend on that
  setting. To see why it matters, once: set that setting to Disable, comment out the
  freeze, stop on a breakpoint for a second, and watch the board reset under you.
- The watchdog test: set `TEST_WATCHDOG` to 1, build, and start it with
  **Run > Run As > STM32 C/C++ Application** (no debugger holding it). Start the pump and
  hold B1 for 2 s. The program hangs on purpose, and within about a quarter of a second
  the terminal shows a new start in FAULT, with the pump off.
- The saved setting: a long press in IDLE moves the run limit on. Press the black reset
  button: the limit is still there. In a debug session, the **Memory** view at
  0x0801FC00 shows the three saved half-words: the version, the limit and the check.
- **Build Analyzer**: the program must end well below 0x0801FC00, the settings page.

## Sources

Checked on 2026-11-25. st.com did not answer from our network that day, so we read ST's
documents in the copies named here and the HAL in ST's own source code.

- STM32F1 HAL driver V1.1.10 (22 July 2024), ST's source on GitHub: `HAL_IWDG_Init`
  starts the watchdog, `HAL_IWDG_Refresh`, `__HAL_DBGMCU_FREEZE_IWDG`, `__HAL_RCC_GET_FLAG`
  with `RCC_FLAG_IWDGRST`, `__HAL_RCC_CLEAR_RESET_FLAGS`, `HAL_FLASHEx_Erase`,
  `HAL_FLASH_Program`, `HAL_TIM_Base_Start_IT`, `HAL_TIM_PeriodElapsedCallback`.
  https://github.com/STMicroelectronics/stm32f1xx-hal-driver
- STM32F103x8/xB datasheet (we read Doc ID 13587 Rev 14): LSI 30 to 60 kHz, 40 kHz
  typical; flash page erase 20 to 40 ms; flash endurance 10 000 cycles.
  https://www.st.com/resource/en/datasheet/stm32f103c8.pdf
- RM0008, STM32F10x reference manual: the IWDG timeout, the reset flags in RCC_CSR.
  https://www.st.com/resource/en/reference_manual/rm0008-stm32f101xx-stm32f102xx-stm32f103xx-stm32f105xx-and-stm32f107xx-advanced-armbased-32bit-mcus-stmicroelectronics.pdf
- STM32 Nucleo-64 boards user manual, UM1724 (we read Rev 14): B1 on PC13, LD2 on PA5.
  https://www.st.com/resource/en/user_manual/dm00105823-stm32-nucleo64-boards-mb1136-stmicroelectronics.pdf
- STM32CubeIDE user guide, UM2609 Rev 18, June 2026 (installed with STM32CubeIDE 2.2): Run
  As, Live Expressions, the Memory view, the Build Analyzer, Suspend watchdog counters
  while halted.
  https://www.st.com/resource/en/user_manual/um2609-stm32cubeide-user-guide-stmicroelectronics.pdf
- STM32CubeMX user manual, UM1718 Rev 50, February 2026 (the copy installed with
  STM32CubeMX): the user sections, Activated for the IWDG, NVIC Settings.
  https://www.st.com/resource/en/user_manual/um1718-stm32cubemx-for-stm32-configuration-and-initialization-c-code-generation-stmicroelectronics.pdf
- STM32CubeF1, ST's EEPROM emulation example for the NUCLEO-F103RB
  (Projects/STM32F103RB-Nucleo/Applications/EEPROM/EEPROM_Emulation), which points to ST's
  application note AN2594: many saves in one page before it is erased.
  https://github.com/STMicroelectronics/STM32CubeF1
