Skip to content

Commit 0a24130

Browse files
committed
[all] Refactor project
1 parent b1db763 commit 0a24130

219 files changed

Lines changed: 5557 additions & 6876 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.claude/skills/port-stm32-platform/SKILL.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ For each device type, decide **reuse existing driver (with alias)** OR **write a
5151

5252
The clock driver is almost always new per family. GPIO, I2C, and IWDG/WWDG are almost always reusable. UART, flash, RNG, DMA require careful diffing.
5353

54+
**Clock is the architectural exception.** Unlike every other driver type, the clock subsystem has no `whal_ClockDriver` vtable and no generic `whal_Clock_Init`/`Enable`/`Disable` API. Chip clock drivers expose imperative `Enable*`/`Disable*`/`Set*` helpers (e.g. `whal_<Platform>_Rcc_EnableOsc`, `EnablePll`, `SetSysClock`, `EnablePeriphClk`) and boards call them directly. See `docs/writing_a_driver.md` "Clock" section. The platform header does NOT define a `WHAL_<PLATFORM>_RCC_DRIVER` macro, and the chip's `g_whalClock` global has only `.regmap = { ... }` — no `.driver`, no `.cfg`. Reference: `wolfHAL/clock/stm32wb_rcc.h` and `boards/stm32wb55xx_nucleo/board.c`.
55+
5456
## Phase 2 — Platform header
5557

5658
Create `wolfHAL/platform/st/<chip>.h` (e.g., `stm32g431cb.h`). Reference: `stm32wba55cg.h` (GPDMA chip) or `stm32wb55xx.h` (DMA+DMAMUX chip).
@@ -88,9 +90,9 @@ Skeleton:
8890
1. Read `docs/writing_a_driver.md` for the driver pattern.
8991
2. Copy the closest existing driver as a starting point.
9092
3. Update register offsets, bit positions, and sequences per the TRM. **Cross-check register-map diagrams against the textual bit descriptions** — they sometimes disagree, and the textual description is authoritative.
91-
4. Match the existing naming: `whal_<Platform><Type>_<Func>` for functions, `whal_<Platform><Type>_Driver` for the vtable, `whal_<Platform><Type>_Cfg` for the config struct.
93+
4. Match the existing naming: `whal_<Platform><Type>_<Func>` for functions, `whal_<Platform><Type>_Driver` for the vtable, `whal_<Platform><Type>_Cfg` for the config struct. **Exception: the clock driver has no vtable and no `_Driver` symbol** — its public API is the imperative `Enable*`/`Disable*`/`Set*` helpers. See `docs/writing_a_driver.md` "Clock".
9294
5. Place files at `wolfHAL/<type>/<platform>_<type>.h` and `src/<type>/<platform>_<type>.c`.
93-
6. Do not add cross-driver calls from inside a driver — clock enables, power sequencing, flash wait states, pin muxing are the board's responsibility.
95+
6. Do not add cross-driver calls from inside a driver — clock enables, power sequencing, flash wait states, pin muxing are the board's responsibility. Boards toggle peripheral clocks via the chip clock driver's `whal_<Platform>_Rcc_EnablePeriphClk(&g_whalClock, &gateDescriptor)` (or chip-equivalent name).
9496
9597
### For each device type marked "reuse existing driver" — create an alias header + stub .c
9698
@@ -186,13 +188,13 @@ Create `boards/<board_name>/` with:
186188
Exports `extern whal_<Type>` instances and declares `Board_Init`/`Board_Deinit`/`Board_WaitMs`. Follow `docs/adding_a_board.md`.
187189

188190
### `board.c`
189-
Define each `whal_<Type>` global with its platform macro + board-specific config (pins, baud rates, timeout, DMA channel assignments). Implement `Board_Init` in dependency order: PWR → Clock → peripheral clock enables → GPIO → UART → Timer → the rest. Keep the watchdog out of `Board_Init` (the app starts it when ready to refresh) per `docs/adding_a_board.md`. Guard DMA-specific setup under `#ifdef BOARD_DMA`, matching `boards/stm32wba55cg_nucleo/board.c`.
191+
Define each `whal_<Type>` global with its platform macro + board-specific config (pins, baud rates, timeout, DMA channel assignments). The `whal_Clock` global is just `.regmap = { ... }` (no `.driver`, no `.cfg`) since the clock subsystem has no vtable. Implement `Board_Init` in dependency order: PWR → bring up clock tree imperatively (oscillator on, PLL configure+enable, sysclk switch) → enable peripheral clocks → GPIO → UART → Timer → the rest. Keep the watchdog out of `Board_Init` (the app starts it when ready to refresh) per `docs/adding_a_board.md`. Guard DMA-specific setup under `#ifdef BOARD_DMA`, matching `boards/stm32wba55cg_nucleo/board.c`.
190192

191193
### GPIO pin conflict check
192194
After writing the `pinCfg` array in `board.c`, scan every entry pair and verify no two entries share the same physical port+pin. This is a common mistake when a pin serves double duty (e.g., PA5 used as both an LED and SPI1_SCK). If a conflict is found, consult the chip's alternate-function table in the datasheet and remap the conflicting peripheral to an alternate pin on a different port.
193195

194-
### `Makefile.inc`
195-
Model on `boards/stm32wba55cg_nucleo/Makefile.inc`:
196+
### `board.mk`
197+
Model on `boards/stm32wba55cg_nucleo/board.mk`:
196198
- `PLATFORM = <platform>` — matches the prefix used in `src/*/<platform>_*.c`
197199
- `TESTS ?= clock gpio flash timer rng crypto uart spi i2c irq` — trim to what the board supports
198200
- `CFLAGS``-mcpu=cortex-m4`/`cortex-m33` to match the core, `-DPLATFORM_<UPPER>` for platform ifdefs

boards/README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,20 @@ build configuration.
2929

3030
Each board directory contains:
3131

32-
- **`Makefile.inc`** - Build configuration: toolchain, CPU flags, platform
32+
- **`board.mk`** - Build configuration: toolchain, CPU flags, platform
3333
drivers, and linker script. Included by application Makefiles via
34-
`include $(BOARD_DIR)/Makefile.inc`.
34+
`include $(BOARD_DIR)/board.mk`.
3535
- **`board.h`** - Board-level declarations: global peripheral instances,
3636
pin definitions, and `Board_Init()`/`Board_Deinit()` prototypes.
3737
- **`board.c`** - Peripheral instantiation and `Board_Init()` implementation
38-
(supply, clock, GPIO, UART, flash, timer).
38+
(power, clock, GPIO, UART, flash, timer).
3939
- **`linker.ld`** - Linker script defining memory regions (flash, RAM).
4040
- Any additional board-specific source files (e.g. interrupt vector table,
4141
architecture-specific startup code).
4242

43-
## Makefile.inc Convention
43+
## board.mk Convention
4444

45-
Board `Makefile.inc` files use a self-referencing pattern so that they can be
45+
Board `board.mk` files use a self-referencing pattern so that they can be
4646
included from any directory:
4747

4848
```makefile
@@ -52,11 +52,11 @@ _BOARD_DIR := $(patsubst %/,%,$(dir $(lastword $(MAKEFILE_LIST))))
5252
`_BOARD_DIR` points to the board's own directory, while the application
5353
Makefile sets `BOARD_DIR` which may point elsewhere (e.g. a private board
5454
overlay). This enables private repositories to extend a board by including
55-
the base `Makefile.inc` and adding additional sources.
55+
the base `board.mk` and adding additional sources.
5656

5757
### What `BOARD_SOURCE` includes
5858

59-
Board `Makefile.inc` populates `BOARD_SOURCE` with all of the sources
59+
Board `board.mk` populates `BOARD_SOURCE` with all of the sources
6060
required to build the wolfHAL tests and sample applications for that board:
6161

6262
- Board files: `board.c` and any additional board-specific source files
@@ -66,13 +66,13 @@ required to build the wolfHAL tests and sample applications for that board:
6666
`gpio.c`, `clock.c`, `uart.c`, and other files under `src/*.c`
6767

6868
In your own projects you may either reuse these defaults by including the
69-
board `Makefile.inc` as-is, or define your own `BOARD_SOURCE` in your
69+
board `board.mk` as-is, or define your own `BOARD_SOURCE` in your
7070
application Makefile to select a different set of modules.
7171

7272
## Adding a New Board
7373

7474
1. Create a new directory: `boards/<vendor>_<board>/`
75-
2. Add `Makefile.inc` following the `_BOARD_DIR` pattern above
75+
2. Add `board.mk` following the `_BOARD_DIR` pattern above
7676
3. Implement `board.h`, `board.c`, and `linker.ld`
7777
4. Set `PLATFORM`, `TESTS`, toolchain variables, `CFLAGS`, and `BOARD_SOURCE`
7878
5. Build with `make BOARD=<your_board>`

boards/pic32cz_curiosity_ultra/board.c

Lines changed: 47 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -6,50 +6,18 @@
66
#include <wolfHAL/platform/microchip/pic32cz.h>
77
#include "peripheral.h"
88

9-
/* Supply */
10-
static whal_Supply g_whalSupply = {
11-
.regmap = { WHAL_PIC32CZ_SUPPLY_REGMAP },
12-
/* .driver: direct API mapping */
9+
/* Power */
10+
static whal_Power g_whalPower = {
11+
.regmap = { WHAL_PIC32CZ_SUPC_REGMAP },
1312
};
1413

1514
/* Clock */
1615
whal_Clock g_whalClock = {
17-
.regmap = { WHAL_PIC32CZ_CLOCK_PLL_REGMAP },
18-
/* .driver: direct API mapping */
19-
20-
.cfg = &(whal_Pic32czClock_Cfg) {
21-
/* 300MHz clock */
22-
.oscCtrlCfg = &(whal_Pic32czClockPll_OscCtrlCfg) {
23-
.pllInst = WHAL_PIC32CZ_PLL0,
24-
.refSel = WHAL_PIC32CZ_REFSEL_DFLL48M,
25-
.bwSel = WHAL_PIC32CZ_BWSEL_10MHz_TO_20MHz,
26-
27-
.fbDiv = 225,
28-
.refDiv = 12,
29-
30-
.outCfgCount = 1,
31-
.outCfg = &(whal_Pic32czClockPll_OutCfg) {
32-
.postDivMask = WHAL_PIC32CZ_POSTDIV0_Msk,
33-
.postDivPos = WHAL_PIC32CZ_POSTDIV0_Pos,
34-
.outEnMask = WHAL_PIC32CZ_OUTEN0_Msk,
35-
.outEnPos = WHAL_PIC32CZ_OUTEN0_Pos,
36-
.postDiv = 3,
37-
},
38-
},
39-
.mclkCfg = &(whal_Pic32czClock_MclkCfg) {
40-
.div = 2,
41-
},
42-
.gclkCfgCount = 1,
43-
.gclkCfg = &(whal_Pic32czClock_GclkCfg) {
44-
.gen = 0,
45-
.genSrc = WHAL_PIC32CZ_GENSRC_PLL0_CLOCKOUT0,
46-
.genDiv = 1,
47-
},
48-
},
16+
.regmap = { WHAL_PIC32CZ_CLOCK_REGMAP },
4917
};
5018

5119
/* Peripheral clocks */
52-
static const whal_Pic32czClock_Clk g_peripheralClocks[] = {
20+
static const whal_Pic32cz_Clock_PeriphClk g_periphClks[] = {
5321
{ /* SERCOM 4 (UART) */
5422
.gclkPeriphChannel = 25,
5523
.gclkPeriphSrc = 0, /* GEN 0 */
@@ -58,16 +26,16 @@ static const whal_Pic32czClock_Clk g_peripheralClocks[] = {
5826
.mclkEnablePos = 3,
5927
},
6028
};
61-
#define CLOCK_COUNT (sizeof(g_peripheralClocks) / sizeof(g_peripheralClocks[0]))
29+
#define PERIPH_CLK_COUNT (sizeof(g_periphClks) / sizeof(g_periphClks[0]))
6230

6331
/* GPIO */
6432
whal_Gpio g_whalGpio = {
6533
.regmap = { WHAL_PIC32CZ_GPIO_REGMAP },
6634
/* .driver: direct API mapping */
6735

68-
.cfg = &(whal_Pic32czGpio_Cfg) {
36+
.cfg = &(whal_Pic32cz_Gpio_Cfg) {
6937
.pinCfgCount = 3,
70-
.pinCfg = (whal_Pic32czGpio_PinCfg[]) {
38+
.pinCfg = (whal_Pic32cz_Gpio_PinCfg[]) {
7139
{ /* LED */
7240
.port = 1,
7341
.pin = 21,
@@ -95,7 +63,7 @@ whal_Uart g_whalUart = {
9563
.regmap = { WHAL_PIC32CZ_SERCOM4_UART_REGMAP },
9664
/* .driver: direct API mapping */
9765

98-
.cfg = &(whal_Pic32czUart_Cfg) {
66+
.cfg = &(whal_Pic32cz_Uart_Cfg) {
9967
.baud = WHAL_PIC32CZ_UART_BAUD(115200, 300000000),
10068
.txPad = WHAL_PIC32CZ_UART_TXPO_PAD0,
10169
.rxPad = WHAL_PIC32CZ_UART_RXPO_PAD1,
@@ -157,26 +125,46 @@ whal_Error Board_Init(void)
157125
{
158126
whal_Error err;
159127

160-
err = whal_Supply_Init(&g_whalSupply);
161-
if (err) {
162-
return err;
163-
}
164-
165128
/* Enable PLL power supply before clock init */
166-
err = whal_Supply_Enable(&g_whalSupply,
167-
&(whal_Pic32czSupc_Supply){WHAL_PIC32CZ_SUPPLY_PLL});
168-
if (err) {
129+
err = whal_Pic32cz_Supc_EnableSupply(&g_whalPower,
130+
&(whal_Pic32cz_Supc_Supply){WHAL_PIC32CZ_SUPC_PLL});
131+
if (err) {
132+
return err;
133+
}
134+
135+
/* PLL0: DFLL48 / 12 * 225 / 3 = 300 MHz, then GCLK0 from PLL0 OUT0,
136+
* then MCLK CPU divider /2 = 150 MHz CPU. */
137+
err = whal_Pic32cz_Clock_EnablePll(&g_whalClock, &(whal_Pic32cz_Clock_PllCfg){
138+
.pllInst = WHAL_PIC32CZ_PLL0,
139+
.refSel = WHAL_PIC32CZ_REFSEL_DFLL48M,
140+
.bwSel = WHAL_PIC32CZ_BWSEL_10MHz_TO_20MHz,
141+
.fbDiv = 225,
142+
.refDiv = 12,
143+
.outCfgCount = 1,
144+
.outCfg = &(whal_Pic32cz_Clock_PllOutCfg){
145+
.postDivMask = WHAL_PIC32CZ_POSTDIV0_Msk,
146+
.postDivPos = WHAL_PIC32CZ_POSTDIV0_Pos,
147+
.outEnMask = WHAL_PIC32CZ_OUTEN0_Msk,
148+
.outEnPos = WHAL_PIC32CZ_OUTEN0_Pos,
149+
.postDiv = 3,
150+
},
151+
});
152+
if (err)
169153
return err;
170-
}
171-
172-
err = whal_Clock_Init(&g_whalClock);
173-
if (err) {
154+
err = whal_Pic32cz_Clock_SetMclkDiv(&g_whalClock, 2);
155+
if (err)
156+
return err;
157+
err = whal_Pic32cz_Clock_EnableGclkGen(&g_whalClock, &(whal_Pic32cz_Clock_GenCfg){
158+
.gen = 0,
159+
.genSrc = WHAL_PIC32CZ_GENSRC_PLL0_CLOCKOUT0,
160+
.genDiv = 1,
161+
});
162+
if (err)
174163
return err;
175-
}
176164

177165
/* Enable peripheral clocks */
178-
for (size_t i = 0; i < CLOCK_COUNT; i++) {
179-
err = whal_Clock_Enable(&g_whalClock, &g_peripheralClocks[i]);
166+
for (size_t i = 0; i < PERIPH_CLK_COUNT; i++) {
167+
err = whal_Pic32cz_Clock_EnablePeriphClk(&g_whalClock, &g_periphClks[i]);
180168
if (err)
181169
return err;
182170
}
@@ -249,21 +237,13 @@ whal_Error Board_Deinit(void)
249237
}
250238

251239
/* Disable peripheral clocks */
252-
for (size_t i = 0; i < CLOCK_COUNT; i++) {
253-
err = whal_Clock_Disable(&g_whalClock, &g_peripheralClocks[i]);
240+
for (size_t i = 0; i < PERIPH_CLK_COUNT; i++) {
241+
err = whal_Pic32cz_Clock_DisablePeriphClk(&g_whalClock, &g_periphClks[i]);
254242
if (err)
255243
return err;
256244
}
257245

258-
err = whal_Clock_Deinit(&g_whalClock);
259-
if (err) {
260-
return err;
261-
}
262-
263-
err = whal_Supply_Deinit(&g_whalSupply);
264-
if (err) {
265-
return err;
266-
}
246+
/* SUPC outputs are left as-is; no Deinit operation. */
267247

268248
return WHAL_SUCCESS;
269249
}

boards/pic32cz_curiosity_ultra/Makefile.inc renamed to boards/pic32cz_curiosity_ultra/board.mk

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
_BOARD_DIR := $(patsubst %/,%,$(dir $(lastword $(MAKEFILE_LIST))))
22

33
PLATFORM = pic32cz
4-
TESTS ?= clock gpio flash timer supply uart
4+
TESTS ?= clock gpio flash timer uart
55

66
GCC = $(GCC_PATH)arm-none-eabi-gcc
77
LD = $(GCC_PATH)arm-none-eabi-ld
@@ -10,10 +10,9 @@ OBJCOPY = $(GCC_PATH)arm-none-eabi-objcopy
1010
CFLAGS += -Wall -Werror $(INCLUDE) -g3 \
1111
-ffreestanding -nostdlib -mcpu=cortex-m7 \
1212
-DPLATFORM_PIC32CZ -MMD -MP \
13-
-DWHAL_CFG_GPIO_API_MAPPING_PIC32CZ \
14-
-DWHAL_CFG_CLOCK_API_MAPPING_PIC32CZ_PLL \
15-
-DWHAL_CFG_UART_API_MAPPING_PIC32CZ \
16-
-DWHAL_CFG_SUPPLY_API_MAPPING_PIC32CZ
13+
-DWHAL_CFG_PIC32CZ_GPIO_DIRECT_API_MAPPING \
14+
-DWHAL_CFG_PIC32CZ_CLOCK_DIRECT_API_MAPPING \
15+
-DWHAL_CFG_PIC32CZ_UART_DIRECT_API_MAPPING
1716
LDFLAGS = --omagic -static
1817

1918
LINKER_SCRIPT ?= $(_BOARD_DIR)/linker.ld
@@ -32,4 +31,4 @@ BOARD_SOURCE += $(wildcard $(WHAL_DIR)/src/*/pic32cz_*.c)
3231
BOARD_SOURCE += $(wildcard $(WHAL_DIR)/src/*/systick.c)
3332

3433
# Peripheral devices
35-
include $(WHAL_DIR)/boards/peripheral/Makefile.inc
34+
include $(WHAL_DIR)/boards/peripheral/board.mk

0 commit comments

Comments
 (0)