Skip to content

Commit 6b76beb

Browse files
committed
[i2c, sensor, stm32wb_i2c, bmi270, boards] Add I2C, sensor, and BMI270 IMU support for STM32WB
1 parent 6a1c600 commit 6b76beb

26 files changed

Lines changed: 2192 additions & 65 deletions

File tree

.github/workflows/ci.yml

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,26 @@ jobs:
4242
working-directory: tests
4343
run: CFLAGS="${{ matrix.extra_cflags }}" make BOARD=${{ matrix.board }}
4444

45-
peripheral-drivers:
45+
peripheral-tests:
4646
runs-on: ubuntu-latest
4747
strategy:
4848
matrix:
49-
source:
50-
- src/block/sdhc_spi.c
51-
- src/flash/spi_nor.c
49+
include:
50+
- board: stm32wb55xx_nucleo
51+
peripherals: bmi270
52+
tests: bmi270
53+
- board: stm32wb55xx_nucleo
54+
peripherals: spi_nor_w25q64
55+
tests: flash
56+
- board: stm32wb55xx_nucleo
57+
peripherals: sdhc_spi_sdcard32gb
58+
tests: block
5259
steps:
5360
- uses: actions/checkout@v4
5461

5562
- name: Install ARM toolchain
5663
run: sudo apt-get update && sudo apt-get install -y gcc-arm-none-eabi
5764

58-
- name: Compile ${{ matrix.source }}
59-
run: arm-none-eabi-gcc -c -I. -Wall -Werror ${{ matrix.source }} -o /dev/null
65+
- name: Build tests
66+
working-directory: tests
67+
run: make BOARD=${{ matrix.board }} PERIPHERALS="${{ matrix.peripherals }}" TESTS="${{ matrix.tests }}"

boards/peripheral/Makefile.inc

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,8 @@ _PERIPHERAL_DIR := $(patsubst %/,%,$(dir $(lastword $(MAKEFILE_LIST))))
22

33
BOARD_SOURCE += $(_PERIPHERAL_DIR)/peripheral.c
44

5-
ifdef PERIPHERAL_SDHC_SPI_SDCARD32GB
6-
CFLAGS += -DPERIPHERAL_SDHC_SPI_SDCARD32GB
7-
BOARD_SOURCE += $(_PERIPHERAL_DIR)/block/sdhc_spi_sdcard32gb.c
8-
BOARD_SOURCE += $(WHAL_DIR)/src/block/sdhc_spi.c
9-
endif
5+
uc = $(shell echo $(1) | tr a-z A-Z | tr - _)
106

11-
ifdef PERIPHERAL_SPI_NOR_W25Q64
12-
CFLAGS += -DPERIPHERAL_SPI_NOR_W25Q64
13-
BOARD_SOURCE += $(_PERIPHERAL_DIR)/flash/spi_nor_w25q64.c
14-
BOARD_SOURCE += $(WHAL_DIR)/src/flash/spi_nor.c
15-
endif
7+
$(foreach p,$(PERIPHERALS),$(eval CFLAGS += -DPERIPHERAL_$(call uc,$(p))))
8+
$(foreach p,$(PERIPHERALS),$(eval BOARD_SOURCE += $(shell find $(_PERIPHERAL_DIR) -name '$(p).c')))
9+
$(foreach p,$(PERIPHERALS),$(eval BOARD_SOURCE += $(shell find $(WHAL_DIR)/src -name '$(p).c')))

boards/peripheral/peripheral.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
#include "flash/spi_nor_w25q64.h"
99
#endif
1010

11+
#ifdef PERIPHERAL_BMI270
12+
#include "sensor/imu/bmi270.h"
13+
#endif
14+
1115
whal_PeripheralBlock_Cfg g_peripheralBlock[] = {
1216
#ifdef PERIPHERAL_SDHC_SPI_SDCARD32GB
1317
{
@@ -33,6 +37,16 @@ whal_PeripheralFlash_Cfg g_peripheralFlash[] = {
3337
{0}, /* sentinel */
3438
};
3539

40+
whal_PeripheralSensor_Cfg g_peripheralSensor[] = {
41+
#ifdef PERIPHERAL_BMI270
42+
{
43+
.name = "bmi270",
44+
.dev = &g_whalBmi270,
45+
},
46+
#endif
47+
{0}, /* sentinel */
48+
};
49+
3650
whal_Error Peripheral_Init(void)
3751
{
3852
whal_Error err;
@@ -49,13 +63,29 @@ whal_Error Peripheral_Init(void)
4963
return err;
5064
}
5165

66+
#if PERIPHERAL_SENSOR_COUNT > 0
67+
for (size_t i = 0; g_peripheralSensor[i].dev; i++) {
68+
err = whal_Sensor_Init(g_peripheralSensor[i].dev);
69+
if (err)
70+
return err;
71+
}
72+
#endif
73+
5274
return WHAL_SUCCESS;
5375
}
5476

5577
whal_Error Peripheral_Deinit(void)
5678
{
5779
whal_Error err;
5880

81+
#if PERIPHERAL_SENSOR_COUNT > 0
82+
for (size_t i = 0; g_peripheralSensor[i].dev; i++) {
83+
err = whal_Sensor_Deinit(g_peripheralSensor[i].dev);
84+
if (err)
85+
return err;
86+
}
87+
#endif
88+
5989
for (size_t i = 0; g_peripheralFlash[i].dev; i++) {
6090
err = whal_Flash_Deinit(g_peripheralFlash[i].dev);
6191
if (err)

boards/peripheral/peripheral.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <wolfHAL/wolfHAL.h>
55
#include <wolfHAL/block/block.h>
66
#include <wolfHAL/flash/flash.h>
7+
#include <wolfHAL/sensor/sensor.h>
78
#include <stddef.h>
89
#include <stdint.h>
910

@@ -24,9 +25,24 @@ typedef struct {
2425
size_t sectorSz; /* Sector (erase) size in bytes */
2526
} whal_PeripheralFlash_Cfg;
2627

28+
/* Peripheral sensor device test configuration */
29+
typedef struct {
30+
const char *name;
31+
whal_Sensor *dev;
32+
} whal_PeripheralSensor_Cfg;
33+
2734
extern whal_PeripheralBlock_Cfg g_peripheralBlock[];
2835
extern whal_PeripheralFlash_Cfg g_peripheralFlash[];
2936

37+
enum {
38+
#ifdef PERIPHERAL_BMI270
39+
PERIPHERAL_SENSOR_BMI270,
40+
#endif
41+
PERIPHERAL_SENSOR_COUNT,
42+
};
43+
44+
extern whal_PeripheralSensor_Cfg g_peripheralSensor[];
45+
3046
whal_Error Peripheral_Init(void);
3147
whal_Error Peripheral_Deinit(void);
3248

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include "bmi270.h"
2+
#include <wolfHAL/sensor/imu/bmi270.h>
3+
#include <wolfHAL/sensor/imu/bmi270_config_data.h>
4+
#include "board.h"
5+
6+
/*
7+
* Bosch BMI270 — 6-axis IMU (accelerometer + gyroscope)
8+
*
9+
* - I2C address: 0x68 (SDO low)
10+
* - Standard mode (100 kHz) or Fast mode (400 kHz)
11+
* - Requires 8192-byte config blob upload during init
12+
*/
13+
14+
whal_I2c_ComCfg g_bmi270ComCfg = {
15+
.freq = 400000, /* 400 kHz fast mode */
16+
.addr = WHAL_BMI270_ADDR_LOW,
17+
.addrSz = 7,
18+
};
19+
20+
whal_Sensor g_whalBmi270 = {
21+
.driver = &whal_Bmi270_Driver,
22+
.cfg = &(whal_Bmi270_Cfg) {
23+
.i2c = &g_whalI2c,
24+
.comCfg = &g_bmi270ComCfg,
25+
.configData = whal_bmi270_config_data,
26+
.configDataSz = WHAL_BMI270_CONFIG_DATA_SZ,
27+
.DelayMs = Board_WaitMs,
28+
},
29+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#ifndef BOARD_SENSOR_IMU_BMI270_H
2+
#define BOARD_SENSOR_IMU_BMI270_H
3+
4+
#include <wolfHAL/wolfHAL.h>
5+
#include <wolfHAL/sensor/sensor.h>
6+
#include <wolfHAL/sensor/imu/bmi270.h>
7+
8+
extern whal_Sensor g_whalBmi270;
9+
10+
#endif /* BOARD_SENSOR_IMU_BMI270_H */

boards/stm32wb55xx_nucleo/Makefile.inc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ BOARD_SOURCE += $(wildcard $(WHAL_DIR)/src/*/timer.c)
2727
BOARD_SOURCE += $(wildcard $(WHAL_DIR)/src/*/supply.c)
2828
BOARD_SOURCE += $(wildcard $(WHAL_DIR)/src/*/flash.c)
2929
BOARD_SOURCE += $(wildcard $(WHAL_DIR)/src/*/spi.c)
30+
BOARD_SOURCE += $(wildcard $(WHAL_DIR)/src/*/i2c.c)
31+
BOARD_SOURCE += $(wildcard $(WHAL_DIR)/src/*/sensor.c)
3032
BOARD_SOURCE += $(wildcard $(WHAL_DIR)/src/*/rng.c)
3133
BOARD_SOURCE += $(wildcard $(WHAL_DIR)/src/*/crypto.c)
3234
BOARD_SOURCE += $(wildcard $(WHAL_DIR)/src/*/block.c)

boards/stm32wb55xx_nucleo/board.c

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,10 @@
99

1010
/* SysTick timing */
1111
volatile uint32_t g_tick = 0;
12-
volatile uint8_t g_waiting = 0;
13-
volatile uint8_t g_tickOverflow = 0;
1412

15-
void SysTick_Handler()
13+
void SysTick_Handler(void)
1614
{
17-
uint32_t tickBefore = g_tick++;
18-
if (g_waiting) {
19-
if (tickBefore > g_tick)
20-
g_tickOverflow = 1;
21-
}
15+
g_tick++;
2216
}
2317

2418
uint32_t Board_GetTick(void)
@@ -64,6 +58,7 @@ static const whal_Stm32wbRcc_Clk g_clocks[] = {
6458
{WHAL_STM32WB55_SPI1_CLOCK},
6559
{WHAL_STM32WB55_RNG_CLOCK},
6660
{WHAL_STM32WB55_AES1_CLOCK},
61+
{WHAL_STM32WB55_I2C1_CLOCK},
6762
};
6863
#define CLOCK_COUNT (sizeof(g_clocks) / sizeof(g_clocks[0]))
6964

@@ -135,11 +130,39 @@ whal_Gpio g_whalGpio = {
135130
.speed = WHAL_STM32WB_GPIO_SPEED_FAST,
136131
.pull = WHAL_STM32WB_GPIO_PULL_UP,
137132
},
133+
[I2C_SCL_PIN] = { /* I2C1 SCL */
134+
.port = WHAL_STM32WB_GPIO_PORT_B,
135+
.pin = 8,
136+
.mode = WHAL_STM32WB_GPIO_MODE_ALTFN,
137+
.outType = WHAL_STM32WB_GPIO_OUTTYPE_OPENDRAIN,
138+
.speed = WHAL_STM32WB_GPIO_SPEED_FAST,
139+
.pull = WHAL_STM32WB_GPIO_PULL_UP,
140+
.altFn = 4,
141+
},
142+
[I2C_SDA_PIN] = { /* I2C1 SDA */
143+
.port = WHAL_STM32WB_GPIO_PORT_B,
144+
.pin = 9,
145+
.mode = WHAL_STM32WB_GPIO_MODE_ALTFN,
146+
.outType = WHAL_STM32WB_GPIO_OUTTYPE_OPENDRAIN,
147+
.speed = WHAL_STM32WB_GPIO_SPEED_FAST,
148+
.pull = WHAL_STM32WB_GPIO_PULL_UP,
149+
.altFn = 4,
150+
},
138151
},
139152
.pinCount = PIN_COUNT,
140153
},
141154
};
142155

156+
/* I2C */
157+
whal_I2c g_whalI2c = {
158+
WHAL_STM32WB55_I2C1_DEVICE,
159+
160+
.cfg = &(whal_Stm32wbI2c_Cfg) {
161+
.pclk = 64000000,
162+
.timeout = &g_whalTimeout,
163+
},
164+
};
165+
143166
/* SPI */
144167
whal_Spi g_whalSpi = {
145168
WHAL_STM32WB55_SPI1_DEVICE,
@@ -259,20 +282,7 @@ whal_Crypto g_whalCrypto = {
259282
void Board_WaitMs(size_t ms)
260283
{
261284
uint32_t startCount = g_tick;
262-
g_waiting = 1;
263-
while (1) {
264-
uint32_t currentCount = g_tick;
265-
if (g_tickOverflow) {
266-
if ((UINT32_MAX - startCount) + currentCount > ms) {
267-
break;
268-
}
269-
} else if (currentCount - startCount > ms) {
270-
break;
271-
}
272-
}
273-
274-
g_waiting = 0;
275-
g_tickOverflow = 0;
285+
while (g_tick - startCount < ms);
276286
}
277287

278288
whal_Error Board_Init(void)
@@ -347,6 +357,11 @@ whal_Error Board_Init(void)
347357
return err;
348358
}
349359

360+
err = whal_I2c_Init(&g_whalI2c);
361+
if (err) {
362+
return err;
363+
}
364+
350365
err = whal_Flash_Init(&g_whalFlash);
351366
if (err) {
352367
return err;
@@ -414,6 +429,11 @@ whal_Error Board_Deinit(void)
414429
return err;
415430
}
416431

432+
err = whal_I2c_Deinit(&g_whalI2c);
433+
if (err) {
434+
return err;
435+
}
436+
417437
err = whal_Spi_Deinit(&g_whalSpi);
418438
if (err) {
419439
return err;

boards/stm32wb55xx_nucleo/board.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ extern whal_Spi g_whalSpi;
1313
extern whal_Flash g_whalFlash;
1414
extern whal_Rng g_whalRng;
1515
extern whal_Crypto g_whalCrypto;
16+
extern whal_I2c g_whalI2c;
1617
extern whal_Irq g_whalIrq;
1718

1819
extern whal_Timeout g_whalTimeout;
@@ -26,6 +27,8 @@ enum {
2627
SPI_MISO_PIN,
2728
SPI_MOSI_PIN,
2829
SPI_CS_PIN,
30+
I2C_SCL_PIN,
31+
I2C_SDA_PIN,
2932
PIN_COUNT,
3033
};
3134

0 commit comments

Comments
 (0)