Skip to content

Commit 6d281a8

Browse files
committed
drivers: timer: Enable STM32_RADIO_TIMER Kconfig parameter for STM32WB0x
Use radio timer as the system timer when Bluetooth is used. Modify CMakeLists.txt to compile radio timer driver when STM32_RADIO_TIMER is enabled. Remove the common parts from hci_stm32wb0.c that are present in the radio timer driver. Set the appropriate value for SYS_CLOCK_HW_CYCLES_PER_SEC and SYS_CLOCK_TICKS_PER_SEC. Signed-off-by: Ali Hozhabri <[email protected]>
1 parent dc75210 commit 6d281a8

File tree

6 files changed

+38
-65
lines changed

6 files changed

+38
-65
lines changed

drivers/bluetooth/hci/hci_stm32wb0.c

Lines changed: 10 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,8 @@ LOG_MODULE_REGISTER(bt_driver);
3131
#define DT_DRV_COMPAT st_hci_stm32wb0
3232

3333
/* 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
3734
#define BLE_TX_RX_PRIO 0
3835
#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
4336
#define BLE_RXTX_SEQ_PRIO 3
4437
#define BLE_RXTX_SEQ_FLAGS 0
4538
#define PKA_PRIO 2
@@ -58,7 +51,7 @@ LOG_MODULE_REGISTER(bt_driver);
5851

5952
static uint32_t __noinit dyn_alloc_a[BLE_DYN_ALLOC_SIZE >> 2];
6053
static uint8_t buffer_out_mem[MAX_EVENT_SIZE];
61-
static struct k_work_delayable hal_radio_timer_work, ble_stack_work;
54+
static struct k_work_delayable ble_stack_work;
6255

6356
static struct net_buf *get_rx(uint8_t *msg);
6457
static PKA_HandleTypeDef hpka;
@@ -77,6 +70,14 @@ int BLEPLAT_NvmGet(void)
7770
return 0;
7871
}
7972

73+
uint8_t BLEPLAT_SetRadioTimerValue(uint32_t Time, uint8_t EventType, uint8_t CalReq)
74+
{
75+
uint8_t retval;
76+
77+
retval = HAL_RADIO_TIMER_SetRadioTimerValue(Time, EventType, CalReq);
78+
return retval;
79+
}
80+
8081
static void blestack_process(struct k_work *work)
8182
{
8283
BLE_STACK_Tick();
@@ -85,11 +86,6 @@ static void blestack_process(struct k_work *work)
8586
}
8687
}
8788

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

211207
void HAL_RADIO_TIMER_TxRxWakeUpCallback(void)
212208
{
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);
220209
k_work_schedule(&ble_stack_work, K_NO_WAIT);
221210
}
222211

223212
void HAL_RADIO_TxRxCallback(uint32_t flags)
224213
{
225214
BLE_STACK_RadioHandler(flags);
226215
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;
235216
}
236217

237218
ISR_DIRECT_DECLARE(RADIO_TXRX_IRQHandler)
@@ -248,56 +229,24 @@ ISR_DIRECT_DECLARE(RADIO_TXRX_SEQ_IRQHandler)
248229
return 1;
249230
}
250231

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-
275232
/* Function called from PKA_IRQHandler() context. */
276233
void PKAMGR_IRQCallback(void)
277234
{
278235
k_work_schedule(&ble_stack_work, K_NO_WAIT);
279236
}
280237

281-
static int _PKA_IRQHandler(void *args)
238+
static void _PKA_IRQHandler(void *args)
282239
{
283240
ARG_UNUSED(args);
284241

285242
HAL_PKA_IRQHandler(&hpka);
286-
ISR_DIRECT_PM(); /* PM done after servicing interrupt for best latency */
287-
return 1;
288243
}
289244

290245
static void ble_isr_installer(void)
291246
{
292-
IRQ_DIRECT_CONNECT(RADIO_TIMER_TXRX_WKUP_IRQn, BLE_WKUP_PRIO,
293-
RADIO_TIMER_TXRX_WKUP_IRQHandler, BLE_WKUP_FLAGS);
294247
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);
297248
IRQ_DIRECT_CONNECT(RADIO_TXRX_SEQ_IRQn, BLE_RXTX_SEQ_PRIO, RADIO_TXRX_SEQ_IRQHandler,
298249
BLE_RXTX_SEQ_FLAGS);
299-
IRQ_DIRECT_CONNECT(RADIO_TIMER_ERROR_IRQn, BLE_ERROR_PRIO, RADIO_TIMER_ERROR_IRQHandler,
300-
BLE_ERROR_FLAGS);
301250
IRQ_CONNECT(PKA_IRQn, PKA_PRIO, _PKA_IRQHandler, NULL, PKA_FLAGS);
302251
}
303252

@@ -450,7 +399,6 @@ static int bt_hci_stm32wb0_send(const struct device *dev, struct net_buf *buf)
450399
static int bt_hci_stm32wb0_open(const struct device *dev, bt_hci_recv_t recv)
451400
{
452401
struct hci_data *data = dev->data;
453-
RADIO_TIMER_InitTypeDef VTIMER_InitStruct = {MAX_HS_STARTUP_TIME, 0, 0};
454402
RADIO_HandleTypeDef hradio = {0};
455403
BLE_STACK_InitTypeDef BLE_STACK_InitParams = {
456404
.BLEStartRamAddress = (uint8_t *)dyn_alloc_a,
@@ -487,8 +435,6 @@ static int bt_hci_stm32wb0_open(const struct device *dev, bt_hci_recv_t recv)
487435
ble_isr_installer();
488436
hradio.Instance = RADIO;
489437
HAL_RADIO_Init(&hradio);
490-
HAL_RADIO_TIMER_Init(&VTIMER_InitStruct);
491-
492438
HW_AES_Init();
493439
hpka.Instance = PKA;
494440
HAL_PKA_Init(&hpka);
@@ -505,7 +451,6 @@ static int bt_hci_stm32wb0_open(const struct device *dev, bt_hci_recv_t recv)
505451
aci_adv_nwk_init();
506452

507453
data->recv = recv;
508-
k_work_init_delayable(&hal_radio_timer_work, vtimer_process);
509454
k_work_init_delayable(&ble_stack_work, blestack_process);
510455
k_work_schedule(&ble_stack_work, K_NO_WAIT);
511456

drivers/timer/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ zephyr_library_sources_ifdef(CONFIG_REALTEK_RTS5912_RTMR realtek_rts5912_rtmr.c)
4141
zephyr_library_sources_ifdef(CONFIG_SAM0_RTC_TIMER sam0_rtc_timer.c)
4242
zephyr_library_sources_ifdef(CONFIG_SILABS_SLEEPTIMER_TIMER silabs_sleeptimer_timer.c)
4343
zephyr_library_sources_ifdef(CONFIG_STM32_LPTIM_TIMER stm32_lptim_timer.c)
44+
zephyr_library_sources_ifdef(CONFIG_STM32_RADIO_TIMER stm32_radio_timer.c)
4445
zephyr_library_sources_ifdef(CONFIG_TI_DM_TIMER ti_dmtimer.c)
4546
zephyr_library_sources_ifdef(CONFIG_XLNX_PSTTC_TIMER xlnx_psttc_timer.c)
4647
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
@@ -99,6 +99,7 @@ source "drivers/timer/Kconfig.sam0_rtc"
9999
source "drivers/timer/Kconfig.silabs"
100100
source "drivers/timer/Kconfig.smartbond"
101101
source "drivers/timer/Kconfig.stm32_lptim"
102+
source "drivers/timer/Kconfig.stm32_radio"
102103
source "drivers/timer/Kconfig.ti_dm_timer"
103104
source "drivers/timer/Kconfig.xlnx_psttc"
104105
source "drivers/timer/Kconfig.xtensa"

drivers/timer/Kconfig.stm32_radio

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 STM32_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 TICKLESS_CAPABLE
12+
select HAS_STM32LIB
13+
select SYSTEM_TIMER_HAS_DISABLE_SUPPORT

soc/st/stm32/Kconfig.defconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ DT_FLASH_PARENT_IS_XSPI := $(dt_node_has_compat,$(DT_CHOSEN_FLASH_PARENT),$(DT_C
3535

3636
config SYS_CLOCK_HW_CYCLES_PER_SEC
3737
default "$(DT_STM32_RCC_CLOCK_FREQ)" if "$(dt_nodelabel_enabled,rcc)"
38+
depends on !STM32_RADIO_TIMER
3839

3940
config LOG_BACKEND_SWO_REF_FREQ_HZ
4041
default "$(DT_STM32_RCC_CLOCK_FREQ)" if "$(dt_nodelabel_enabled,rcc)"

soc/st/stm32/stm32wb0x/Kconfig.defconfig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,18 @@ if SOC_SERIES_STM32WB0X
88
config NUM_IRQS
99
default 32
1010

11+
config SYS_CLOCK_HW_CYCLES_PER_SEC
12+
default 409600
13+
depends on STM32_RADIO_TIMER
14+
15+
config SYS_CLOCK_TICKS_PER_SEC
16+
default 10240
17+
depends on STM32_RADIO_TIMER
18+
19+
config CORTEX_M_SYSTICK
20+
default n
21+
depends on STM32_RADIO_TIMER
22+
1123
if BT
1224

1325
config BT_AUTO_PHY_UPDATE

0 commit comments

Comments
 (0)