Skip to content

Commit bdb41c0

Browse files
HoZHelcfriedt
authored andcommitted
drivers: timer: Enable STM32WB0_RADIO_TIMER Kconfig parameter
Use radio timer as the system timer when Bluetooth is used. Modify CMakeLists.txt to compile radio timer driver when STM32WB0_RADIO_TIMER is enabled. Remove the common parts from hci_stm32wb0.c that are present in the radio timer driver. Set and retrieve the appropriate value for SYS_CLOCK_TICKS_PER_SEC and SYS_CLOCK_HW_CYCLES_PER_SEC respectively. Define radio_timer node and its properties. Enable radio_timer node in nucleo_wb0x boards. Signed-off-by: Ali Hozhabri <[email protected]>
1 parent 84f7098 commit bdb41c0

File tree

12 files changed

+86
-67
lines changed

12 files changed

+86
-67
lines changed

boards/st/nucleo_wb05kz/nucleo_wb05kz.dts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@
133133
status = "okay";
134134
};
135135

136+
&radio_timer {
137+
status = "okay";
138+
};
139+
136140
&usart1 {
137141
pinctrl-0 = <&usart1_tx_pa1 &usart1_rx_pb0>;
138142
pinctrl-names = "default";

boards/st/nucleo_wb07cc/nucleo_wb07cc.dts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@
132132
status = "okay";
133133
};
134134

135+
&radio_timer {
136+
status = "okay";
137+
};
138+
135139
&usart1 {
136140
pinctrl-0 = <&usart1_tx_pa9 &usart1_rx_pa8>;
137141
pinctrl-names = "default";

boards/st/nucleo_wb09ke/nucleo_wb09ke.dts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@
133133
status = "okay";
134134
};
135135

136+
&radio_timer {
137+
status = "okay";
138+
};
139+
136140
&usart1 {
137141
pinctrl-0 = <&usart1_tx_pa1 &usart1_rx_pb0>;
138142
pinctrl-names = "default";

drivers/bluetooth/hci/hci_stm32wb0.c

Lines changed: 10 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,8 @@ LOG_MODULE_REGISTER(bt_driver);
3030

3131
#define DT_DRV_COMPAT st_hci_stm32wb0
3232

33-
/* Max HS startup time expressed in system time (1953 us / 2.4414 us) */
34-
#define MAX_HS_STARTUP_TIME 320
35-
#define BLE_WKUP_PRIO 0
36-
#define BLE_WKUP_FLAGS 0
3733
#define BLE_TX_RX_PRIO 0
3834
#define BLE_TX_RX_FLAGS 0
39-
#define CPU_WKUP_PRIO 1
40-
#define CPU_WKUP_FLAGS 0
41-
#define BLE_ERROR_PRIO 3
42-
#define BLE_ERROR_FLAGS 0
4335
#define BLE_RXTX_SEQ_PRIO 3
4436
#define BLE_RXTX_SEQ_FLAGS 0
4537
#define PKA_PRIO 2
@@ -58,7 +50,7 @@ LOG_MODULE_REGISTER(bt_driver);
5850

5951
static uint32_t __noinit dyn_alloc_a[BLE_DYN_ALLOC_SIZE >> 2];
6052
static uint8_t buffer_out_mem[MAX_EVENT_SIZE];
61-
static struct k_work_delayable hal_radio_timer_work, ble_stack_work;
53+
static struct k_work_delayable ble_stack_work;
6254

6355
static struct net_buf *get_rx(uint8_t *msg);
6456
static PKA_HandleTypeDef hpka;
@@ -77,6 +69,14 @@ int BLEPLAT_NvmGet(void)
7769
return 0;
7870
}
7971

72+
uint8_t BLEPLAT_SetRadioTimerValue(uint32_t Time, uint8_t EventType, uint8_t CalReq)
73+
{
74+
uint8_t retval;
75+
76+
retval = HAL_RADIO_TIMER_SetRadioTimerValue(Time, EventType, CalReq);
77+
return retval;
78+
}
79+
8080
static void blestack_process(struct k_work *work)
8181
{
8282
BLE_STACK_Tick();
@@ -85,11 +85,6 @@ static void blestack_process(struct k_work *work)
8585
}
8686
}
8787

88-
static void vtimer_process(struct k_work *work)
89-
{
90-
HAL_RADIO_TIMER_Tick();
91-
}
92-
9388
/* "If, since the last power-on or reset, the Host has ever issued a legacy
9489
* advertising command and then issues an extended advertising command, or
9590
* has ever issued an extended advertising command and then issues a legacy
@@ -210,28 +205,13 @@ void send_event(uint8_t *buffer_out, uint16_t buffer_out_length, int8_t overflow
210205

211206
void HAL_RADIO_TIMER_TxRxWakeUpCallback(void)
212207
{
213-
k_work_schedule(&hal_radio_timer_work, K_NO_WAIT);
214-
k_work_schedule(&ble_stack_work, K_NO_WAIT);
215-
}
216-
217-
void HAL_RADIO_TIMER_CpuWakeUpCallback(void)
218-
{
219-
k_work_schedule(&hal_radio_timer_work, K_NO_WAIT);
220208
k_work_schedule(&ble_stack_work, K_NO_WAIT);
221209
}
222210

223211
void HAL_RADIO_TxRxCallback(uint32_t flags)
224212
{
225213
BLE_STACK_RadioHandler(flags);
226214
k_work_schedule(&ble_stack_work, K_NO_WAIT);
227-
k_work_schedule(&hal_radio_timer_work, K_NO_WAIT);
228-
}
229-
230-
ISR_DIRECT_DECLARE(RADIO_TIMER_TXRX_WKUP_IRQHandler)
231-
{
232-
HAL_RADIO_TIMER_TXRX_WKUP_IRQHandler();
233-
ISR_DIRECT_PM(); /* PM done after servicing interrupt for best latency */
234-
return 1;
235215
}
236216

237217
ISR_DIRECT_DECLARE(RADIO_TXRX_IRQHandler)
@@ -248,56 +228,24 @@ ISR_DIRECT_DECLARE(RADIO_TXRX_SEQ_IRQHandler)
248228
return 1;
249229
}
250230

251-
ISR_DIRECT_DECLARE(RADIO_TIMER_CPU_WKUP_IRQHandler)
252-
{
253-
HAL_RADIO_TIMER_TimeoutCallback();
254-
HAL_RADIO_TIMER_CpuWakeUpCallback();
255-
ISR_DIRECT_PM(); /* PM done after servicing interrupt for best latency */
256-
return 1;
257-
}
258-
259-
ISR_DIRECT_DECLARE(RADIO_TIMER_ERROR_IRQHandler)
260-
{
261-
volatile uint32_t debug_cmd;
262-
263-
BLUE->DEBUGCMDREG |= 1;
264-
/* If the device is configured with CLK_SYS = 64MHz
265-
* and BLE clock = 16MHz, a register read is necessary
266-
* to ensure interrupt register is properly cleared
267-
* due to AHB down converter latency
268-
*/
269-
debug_cmd = BLUE->DEBUGCMDREG;
270-
LOG_ERR("Timer error");
271-
ISR_DIRECT_PM(); /* PM done after servicing interrupt for best latency */
272-
return 1;
273-
}
274-
275231
/* Function called from PKA_IRQHandler() context. */
276232
void PKAMGR_IRQCallback(void)
277233
{
278234
k_work_schedule(&ble_stack_work, K_NO_WAIT);
279235
}
280236

281-
static int _PKA_IRQHandler(void *args)
237+
static void _PKA_IRQHandler(void *args)
282238
{
283239
ARG_UNUSED(args);
284240

285241
HAL_PKA_IRQHandler(&hpka);
286-
ISR_DIRECT_PM(); /* PM done after servicing interrupt for best latency */
287-
return 1;
288242
}
289243

290244
static void ble_isr_installer(void)
291245
{
292-
IRQ_DIRECT_CONNECT(RADIO_TIMER_TXRX_WKUP_IRQn, BLE_WKUP_PRIO,
293-
RADIO_TIMER_TXRX_WKUP_IRQHandler, BLE_WKUP_FLAGS);
294246
IRQ_DIRECT_CONNECT(RADIO_TXRX_IRQn, BLE_TX_RX_PRIO, RADIO_TXRX_IRQHandler, BLE_TX_RX_FLAGS);
295-
IRQ_DIRECT_CONNECT(RADIO_TIMER_CPU_WKUP_IRQn, CPU_WKUP_PRIO,
296-
RADIO_TIMER_CPU_WKUP_IRQHandler, CPU_WKUP_FLAGS);
297247
IRQ_DIRECT_CONNECT(RADIO_TXRX_SEQ_IRQn, BLE_RXTX_SEQ_PRIO, RADIO_TXRX_SEQ_IRQHandler,
298248
BLE_RXTX_SEQ_FLAGS);
299-
IRQ_DIRECT_CONNECT(RADIO_TIMER_ERROR_IRQn, BLE_ERROR_PRIO, RADIO_TIMER_ERROR_IRQHandler,
300-
BLE_ERROR_FLAGS);
301249
IRQ_CONNECT(PKA_IRQn, PKA_PRIO, _PKA_IRQHandler, NULL, PKA_FLAGS);
302250
}
303251

@@ -449,7 +397,6 @@ static int bt_hci_stm32wb0_send(const struct device *dev, struct net_buf *buf)
449397
static int bt_hci_stm32wb0_open(const struct device *dev, bt_hci_recv_t recv)
450398
{
451399
struct hci_data *data = dev->data;
452-
RADIO_TIMER_InitTypeDef VTIMER_InitStruct = {MAX_HS_STARTUP_TIME, 0, 0};
453400
RADIO_HandleTypeDef hradio = {0};
454401
BLE_STACK_InitTypeDef BLE_STACK_InitParams = {
455402
.BLEStartRamAddress = (uint8_t *)dyn_alloc_a,
@@ -486,8 +433,6 @@ static int bt_hci_stm32wb0_open(const struct device *dev, bt_hci_recv_t recv)
486433
ble_isr_installer();
487434
hradio.Instance = RADIO;
488435
HAL_RADIO_Init(&hradio);
489-
HAL_RADIO_TIMER_Init(&VTIMER_InitStruct);
490-
491436
HW_AES_Init();
492437
hpka.Instance = PKA;
493438
HAL_PKA_Init(&hpka);
@@ -504,7 +449,6 @@ static int bt_hci_stm32wb0_open(const struct device *dev, bt_hci_recv_t recv)
504449
aci_adv_nwk_init();
505450

506451
data->recv = recv;
507-
k_work_init_delayable(&hal_radio_timer_work, vtimer_process);
508452
k_work_init_delayable(&ble_stack_work, blestack_process);
509453
k_work_schedule(&ble_stack_work, K_NO_WAIT);
510454

drivers/timer/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ zephyr_library_sources_ifdef(CONFIG_REALTEK_RTS5912_RTMR realtek_rts5912_rtmr.c)
4040
zephyr_library_sources_ifdef(CONFIG_SAM0_RTC_TIMER sam0_rtc_timer.c)
4141
zephyr_library_sources_ifdef(CONFIG_SILABS_SLEEPTIMER_TIMER silabs_sleeptimer_timer.c)
4242
zephyr_library_sources_ifdef(CONFIG_STM32_LPTIM_TIMER stm32_lptim_timer.c)
43+
zephyr_library_sources_ifdef(CONFIG_STM32WB0_RADIO_TIMER stm32wb0_radio_timer.c)
4344
zephyr_library_sources_ifdef(CONFIG_TI_DM_TIMER ti_dmtimer.c)
4445
zephyr_library_sources_ifdef(CONFIG_XLNX_PSTTC_TIMER xlnx_psttc_timer.c)
4546
zephyr_library_sources_ifdef(CONFIG_XTENSA_TIMER xtensa_sys_timer.c)

drivers/timer/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ source "drivers/timer/Kconfig.sam0_rtc"
9898
source "drivers/timer/Kconfig.silabs"
9999
source "drivers/timer/Kconfig.smartbond"
100100
source "drivers/timer/Kconfig.stm32_lptim"
101+
source "drivers/timer/Kconfig.stm32wb0_radio_timer"
101102
source "drivers/timer/Kconfig.ti_dm_timer"
102103
source "drivers/timer/Kconfig.xlnx_psttc"
103104
source "drivers/timer/Kconfig.xtensa"
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# STM32WB0x radio timer configuration options
2+
3+
# Copyright (c) 2025 STMicroelectronics
4+
# SPDX-License-Identifier: Apache-2.0
5+
6+
config STM32WB0_RADIO_TIMER
7+
bool "STM32 Radio Timer for WB0x"
8+
default y if BT
9+
depends on SOC_SERIES_STM32WB0X
10+
select USE_STM32_HAL_RADIO_TIMER
11+
select HAS_STM32LIB
12+
select TICKLESS_CAPABLE
13+
select SYSTEM_TIMER_HAS_DISABLE_SUPPORT

drivers/timer/stm32wb0_radio_timer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
LOG_MODULE_REGISTER(radio_timer_driver);
1919

2020
/* Max HS startup time expressed in system time (1953 us / 2.4414 us) */
21-
#define MAX_HS_STARTUP_TIME 320
21+
#define MAX_HS_STARTUP_TIME DT_PROP(DT_NODELABEL(radio_timer), max_hs_startup_time)
2222
#define BLE_WKUP_PRIO 0
2323
#define CPU_WKUP_PRIO 1
2424
#define RADIO_TIMER_ERROR_PRIO 3

dts/arm/st/wb0/stm32wb0.dtsi

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,13 @@
297297
compatible = "st,hci-stm32wb0";
298298
status = "disabled";
299299
};
300+
301+
radio_timer: radio_timer {
302+
compatible = "st,stm32wb0-radio-timer";
303+
compensated-timer-freq = <409600>;
304+
max-hs-startup-time = <320>;
305+
status = "disabled";
306+
};
300307
};
301308

302309
&nvic {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Copyright (c) 2025, STMicroelectronics
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
description: STM32WB0 Bluetooth LE Radio Timer
5+
6+
compatible: "st,stm32wb0-radio-timer"
7+
8+
properties:
9+
compensated-timer-freq:
10+
type: int
11+
required: true
12+
const: 409600
13+
description: |
14+
Timer frequency resulting from LSI or LSE frequency with interpolated value from HSE with
15+
a periodic temperature compensation algorithm.
16+
It is independent of hardware oscillator variations due to temperature.
17+
The timer resolution is equivalent to 625/256 μs (approximately 2.4414 μs).
18+
The value is expressed in hertz (Hz).
19+
20+
max-hs-startup-time:
21+
type: int
22+
required: true
23+
description: |
24+
High speed crystal startup time. This is used by hardware to anticipate wake-up event
25+
so that high speed crystal is ready at the desired time. This parameter depends on the board
26+
and the crystal used. It is expressed in the unit of 625/256 μs (approximately 2.4414 μs).
27+
For example, if the property is set to 320, it means approximately 781 μs are needed.

0 commit comments

Comments
 (0)