Skip to content

Commit 8d98dfe

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 8d98dfe

File tree

5 files changed

+429
-0
lines changed

5 files changed

+429
-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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ Description:
7474
Projects/NUCLEO-WBA55CG/Applications/BLE/BLE_HeartRate/STM32_WPAN/Target/power_table.c
7575
Projects/NUCLEO-WBA55CG/Applications/BLE/BLE_HeartRate/STM32_WPAN/Target/bpka.c
7676
Projects/NUCLEO-WBA55CG/Applications/BLE/BLE_HeartRate/STM32_WPAN/Target/bpka.h
77+
Projects/NUCLEO-WBA55CG/Applications/BLE/BLE_HeartRate/STM32_WPAN/Target/linklayer_plat.c
78+
Projects/NUCLEO-WBA55CG/Applications/BLE/BLE_HeartRate/STM32_WPAN/Target/ll_sys_if.c
7779
Utilities/trace/adv_trace/stm32_adv_trace.h
7880
Utilities/misc/stm32_mem.h
7981
Utilities/tim_serv/stm32_timer.h
@@ -117,7 +119,10 @@ Patch List:
117119
app_conf.h
118120
scm.c
119121
ll_intf_cmn.h
122+
ll_sys_if.c
123+
linklayer_plat.c
120124

121125
* Changes from official delivery:
122126
- dos2unix applied
123127
- trailing white spaces removed
128+
- #ifndef __ZEPHYR__ applied to remove Cube specific implementation

lib/stm32wba/hci/linklayer_plat.c

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

0 commit comments

Comments
 (0)