Skip to content

Commit 6b528ff

Browse files
committed
lib/stm32wba/hci Moving HAL based funcs into stm32wba hci part
Moving HAL based funcs from zephyr to hal stm32wba hci part. In this way we separate zephyr based adaptation code from pure HAL code. Signed-off-by: Alessandro Manganaro <[email protected]>
1 parent dc6c138 commit 6b528ff

File tree

5 files changed

+423
-0
lines changed

5 files changed

+423
-0
lines changed

lib/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ if(CONFIG_HAS_STM32LIB)
3939
zephyr_sources(stm32wba/hci/power_table.c)
4040
zephyr_sources(stm32wba/hci/scm.c)
4141
zephyr_sources(stm32wba/hci/log_module.c)
42+
zephyr_sources(stm32wba/hci/linklayer_plat.c)
43+
zephyr_sources(stm32wba/hci/ll_sys_if.c)
4244
if(CONFIG_FLASH)
4345
zephyr_sources(stm32wba/hci/flash_manager.c)
4446
zephyr_sources(stm32wba/hci/flash_driver.c)

lib/stm32wba/hci/README

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Description:
3333
Middlewares/ST/STM32_WPAN/link_layer/ll_cmd_lib/inc/os_wrapper.h
3434
Middlewares/ST/STM32_WPAN/link_layer/ll_cmd_lib/inc/power_table.h
3535
Middlewares/ST/STM32_WPAN/link_layer/ll_cmd_lib/inc/pta.h
36+
Middlewares/ST/STM32_WPAN/link_layer/ll_cmd_lib/inc/ll_intf_cmn.h
3637
Middlewares/ST/STM32_WPAN/link_layer/ll_cmd_lib/config/ble_full/ll_fw_config.h
3738
Middlewares/ST/STM32_WPAN/link_layer/ll_sys/inc/linklayer_plat.h
3839
Middlewares/ST/STM32_WPAN/link_layer/ll_sys/inc/ll_sys.h
@@ -74,6 +75,8 @@ Description:
7475
Projects/NUCLEO-WBA55CG/Applications/BLE/BLE_HeartRate/STM32_WPAN/Target/power_table.c
7576
Projects/NUCLEO-WBA55CG/Applications/BLE/BLE_HeartRate/STM32_WPAN/Target/bpka.c
7677
Projects/NUCLEO-WBA55CG/Applications/BLE/BLE_HeartRate/STM32_WPAN/Target/bpka.h
78+
Projects/NUCLEO-WBA55CG/Applications/BLE/BLE_HeartRate/STM32_WPAN/Target/linklayer_plat.c
79+
Projects/NUCLEO-WBA55CG/Applications/BLE/BLE_HeartRate/STM32_WPAN/Target/ll_sys_if.c
7780
Utilities/trace/adv_trace/stm32_adv_trace.h
7881
Utilities/misc/stm32_mem.h
7982
Utilities/tim_serv/stm32_timer.h
@@ -117,6 +120,12 @@ Patch List:
117120
app_conf.h
118121
scm.c
119122
ll_intf_cmn.h
123+
ll_sys_if.c
124+
linklayer_plat.c
125+
126+
* #ifndef __ZEPHYR__ applied to remove Cube specific implementation
127+
Impacted files: ll_sys_if.c
128+
linklayer_plat.c
120129

121130
* Changes from official delivery:
122131
- dos2unix applied

lib/stm32wba/hci/linklayer_plat.c

Lines changed: 272 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,272 @@
1+
/*
2+
* Copyright (c) 2023 STMicroelectronics
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include "scm.h"
8+
9+
/* 2.4GHz RADIO ISR callbacks */
10+
typedef void (*radio_isr_cb_t) (void);
11+
12+
extern const struct device *rng_dev;
13+
14+
/* Radio critical sections */
15+
volatile int32_t irq_counter;
16+
17+
/* Radio bus clock control variables */
18+
uint8_t AHB5_SwitchedOff;
19+
uint32_t radio_sleep_timer_val;
20+
21+
void LINKLAYER_PLAT_ClockInit(void)
22+
{
23+
AHB5_SwitchedOff = 0;
24+
radio_sleep_timer_val = 0;
25+
26+
LL_PWR_EnableBkUpAccess();
27+
28+
/* Select LSE as Sleep CLK */
29+
__HAL_RCC_RADIOSLPTIM_CONFIG(RCC_RADIOSTCLKSOURCE_LSE);
30+
31+
LL_PWR_DisableBkUpAccess();
32+
33+
/* Enable AHB5ENR peripheral clock (bus CLK) */
34+
__HAL_RCC_RADIO_CLK_ENABLE();
35+
}
36+
37+
#ifndef __ZEPHYR__
38+
void LINKLAYER_PLAT_DelayUs(uint32_t delay)
39+
{
40+
__IO register uint32_t Delay = delay * (SystemCoreClock / 1000000U);
41+
do
42+
{
43+
__NOP();
44+
}
45+
while (Delay --);
46+
}
47+
#endif
48+
49+
#ifndef __ZEPHYR__
50+
void LINKLAYER_PLAT_Assert(uint8_t condition)
51+
{
52+
assert_param(condition);
53+
}
54+
#endif
55+
56+
void LINKLAYER_PLAT_WaitHclkRdy(void)
57+
{
58+
while (HAL_RCCEx_GetRadioBusClockReadiness() != RCC_RADIO_BUS_CLOCK_READY) {
59+
}
60+
}
61+
62+
void LINKLAYER_PLAT_AclkCtrl(uint8_t enable)
63+
{
64+
if (enable) {
65+
/* Enable RADIO baseband clock (active CLK) */
66+
HAL_RCCEx_EnableRadioBBClock();
67+
68+
/* Polling on HSE32 activation */
69+
while (LL_RCC_HSE_IsReady() == 0) {
70+
}
71+
} else {
72+
/* Disable RADIO baseband clock (active CLK) */
73+
HAL_RCCEx_DisableRadioBBClock();
74+
}
75+
}
76+
77+
#ifndef __ZEPHYR__
78+
void LINKLAYER_PLAT_GetRNG(uint8_t *ptr_rnd, uint32_t len)
79+
{
80+
uint32_t nb_remaining_rng = len;
81+
uint32_t generated_rng;
82+
83+
/* Get the requested RNGs (4 bytes by 4bytes) */
84+
while (nb_remaining_rng >= 4)
85+
{
86+
generated_rng = 0;
87+
HW_RNG_Get(1, &generated_rng);
88+
memcpy((ptr_rnd+(len-nb_remaining_rng)), &generated_rng, 4);
89+
nb_remaining_rng -=4;
90+
}
91+
92+
/* Get the remaining number of RNGs */
93+
if (nb_remaining_rng>0) {
94+
generated_rng = 0;
95+
HW_RNG_Get(1, &generated_rng);
96+
memcpy((ptr_rnd+(len-nb_remaining_rng)), &generated_rng, nb_remaining_rng);
97+
}
98+
}
99+
#endif
100+
101+
#ifndef __ZEPHYR__
102+
void LINKLAYER_PLAT_SetupRadioIT(void (*intr_cb)())
103+
{
104+
radio_callback = intr_cb;
105+
HAL_NVIC_SetPriority((IRQn_Type) RADIO_INTR_NUM, RADIO_INTR_PRIO_HIGH, 0);
106+
HAL_NVIC_EnableIRQ((IRQn_Type) RADIO_INTR_NUM);
107+
}
108+
#endif
109+
110+
#ifndef __ZEPHYR__
111+
void LINKLAYER_PLAT_SetupSwLowIT(void (*intr_cb)())
112+
{
113+
low_isr_callback = intr_cb;
114+
115+
HAL_NVIC_SetPriority((IRQn_Type) RADIO_SW_LOW_INTR_NUM, RADIO_SW_LOW_INTR_PRIO, 0);
116+
HAL_NVIC_EnableIRQ((IRQn_Type) RADIO_SW_LOW_INTR_NUM);
117+
}
118+
#endif
119+
120+
#ifndef __ZEPHYR__
121+
void LINKLAYER_PLAT_TriggerSwLowIT(uint8_t priority)
122+
{
123+
uint8_t low_isr_priority = RADIO_INTR_PRIO_LOW;
124+
125+
if (NVIC_GetActive(RADIO_SW_LOW_INTR_NUM) == 0) {
126+
/* No nested SW low ISR, default behavior */
127+
128+
if (priority == 0) {
129+
low_isr_priority = RADIO_SW_LOW_INTR_PRIO;
130+
}
131+
132+
HAL_NVIC_SetPriority((IRQn_Type) RADIO_SW_LOW_INTR_NUM, low_isr_priority, 0);
133+
} else {
134+
/* Nested call detected */
135+
/* No change for SW radio low interrupt priority for the moment */
136+
if (priority != 0) {
137+
/* At the end of current SW radio low ISR, this pending SW low interrupt
138+
* will run with RADIO_INTR_PRIO_LOW priority
139+
**/
140+
radio_sw_low_isr_is_running_high_prio = 1;
141+
}
142+
}
143+
144+
HAL_NVIC_SetPendingIRQ((IRQn_Type) RADIO_SW_LOW_INTR_NUM);
145+
}
146+
#endif
147+
148+
void LINKLAYER_PLAT_EnableIRQ(void)
149+
{
150+
irq_counter = MAX(0, irq_counter - 1);
151+
152+
if (irq_counter == 0) {
153+
__enable_irq();
154+
}
155+
}
156+
157+
void LINKLAYER_PLAT_DisableIRQ(void)
158+
{
159+
__disable_irq();
160+
161+
irq_counter++;
162+
}
163+
164+
#ifndef __ZEPHYR__
165+
void LINKLAYER_PLAT_EnableSpecificIRQ(uint8_t isr_type)
166+
{
167+
if ((isr_type & LL_HIGH_ISR_ONLY) != 0) {
168+
prio_high_isr_counter--;
169+
if (prio_high_isr_counter == 0) {
170+
HAL_NVIC_EnableIRQ((IRQn_Type) RADIO_INTR_NUM);
171+
}
172+
}
173+
174+
if ((isr_type & LL_LOW_ISR_ONLY) != 0) {
175+
prio_low_isr_counter--;
176+
if (prio_low_isr_counter == 0) {
177+
HAL_NVIC_EnableIRQ((IRQn_Type) RADIO_SW_LOW_INTR_NUM);
178+
}
179+
}
180+
181+
if ((isr_type & SYS_LOW_ISR) != 0) {
182+
prio_sys_isr_counter--;
183+
if (prio_sys_isr_counter == 0) {
184+
__set_BASEPRI(local_basepri_value);
185+
}
186+
}
187+
}
188+
#endif
189+
190+
#ifndef __ZEPHYR__
191+
void LINKLAYER_PLAT_DisableSpecificIRQ(uint8_t isr_type)
192+
{
193+
194+
if ((isr_type & LL_HIGH_ISR_ONLY) != 0) {
195+
prio_high_isr_counter++;
196+
if (prio_high_isr_counter == 1) {
197+
HAL_NVIC_DisableIRQ(RADIO_INTR_NUM);
198+
}
199+
}
200+
201+
if ((isr_type & LL_LOW_ISR_ONLY) != 0) {
202+
prio_low_isr_counter++;
203+
if (prio_low_isr_counter == 1) {
204+
HAL_NVIC_DisableIRQ(RADIO_SW_LOW_INTR_NUM);
205+
}
206+
}
207+
208+
if ((isr_type & SYS_LOW_ISR) != 0) {
209+
prio_sys_isr_counter++;
210+
if (prio_sys_isr_counter == 1) {
211+
local_basepri_value = __get_BASEPRI();
212+
__set_BASEPRI_MAX(RADIO_INTR_PRIO_LOW_Z << 4);
213+
}
214+
}
215+
}
216+
#endif
217+
218+
void LINKLAYER_PLAT_EnableRadioIT(void)
219+
{
220+
NVIC_EnableIRQ(RADIO_INTR_NUM);
221+
}
222+
223+
void LINKLAYER_PLAT_DisableRadioIT(void)
224+
{
225+
NVIC_DisableIRQ(RADIO_INTR_NUM);
226+
}
227+
228+
#ifndef __ZEPHYR__
229+
void LINKLAYER_PLAT_StartRadioEvt(void)
230+
{
231+
__HAL_RCC_RADIO_CLK_SLEEP_ENABLE();
232+
233+
NVIC_SetPriority(RADIO_INTR_NUM, RADIO_INTR_PRIO_HIGH_Z);
234+
235+
scm_notifyradiostate(SCM_RADIO_ACTIVE);
236+
}
237+
#endif
238+
239+
#ifndef __ZEPHYR__
240+
void LINKLAYER_PLAT_StopRadioEvt(void)
241+
{
242+
__HAL_RCC_RADIO_CLK_SLEEP_DISABLE();
243+
244+
NVIC_SetPriority(RADIO_INTR_NUM, RADIO_INTR_PRIO_LOW_Z);
245+
246+
scm_notifyradiostate(SCM_RADIO_NOT_ACTIVE);
247+
}
248+
#endif
249+
250+
void LINKLAYER_PLAT_NotifyWFIEnter(void)
251+
{
252+
/* Check if Radio state will allow the AHB5 clock to be cut */
253+
254+
/* AHB5 clock will be cut in the following cases:
255+
* - 2.4GHz radio is not in ACTIVE mode (in SLEEP or DEEPSLEEP mode).
256+
* - RADIOSMEN and STRADIOCLKON bits are at 0.
257+
*/
258+
if ((LL_PWR_GetRadioMode() != LL_PWR_RADIO_ACTIVE_MODE) ||
259+
((__HAL_RCC_RADIO_IS_CLK_SLEEP_ENABLED() == 0) &&
260+
(LL_RCC_RADIO_IsEnabledSleepTimerClock() == 0))) {
261+
AHB5_SwitchedOff = 1;
262+
}
263+
}
264+
265+
void LINKLAYER_PLAT_NotifyWFIExit(void)
266+
{
267+
/* Check if AHB5 clock has been turned of and needs resynchronisation */
268+
if (AHB5_SwitchedOff) {
269+
/* Read sleep register as earlier as possible */
270+
radio_sleep_timer_val = ll_intf_cmn_get_slptmr_value();
271+
}
272+
}

0 commit comments

Comments
 (0)