Skip to content

Commit

Permalink
lib/stm32wba/hci Moving HAL based funcs into stm32wba hci part
Browse files Browse the repository at this point in the history
Moving HAL based funcs from zephyr to hal stm32wba hci part.
In this way we separate zephyr based adaptation from pure
HAL code.

Signed-off-by: Alessandro Manganaro <alessandro.manganaro@st.com>
  • Loading branch information
asm5878 committed Oct 23, 2024
1 parent dc6c138 commit 16ef0ae
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ if(CONFIG_HAS_STM32LIB)
zephyr_sources(stm32wba/hci/power_table.c)
zephyr_sources(stm32wba/hci/scm.c)
zephyr_sources(stm32wba/hci/log_module.c)
zephyr_sources(stm32wba/hci/linklayer_plat.c)
zephyr_sources(stm32wba/hci/ll_sys_if.c)
if(CONFIG_FLASH)
zephyr_sources(stm32wba/hci/flash_manager.c)
zephyr_sources(stm32wba/hci/flash_driver.c)
Expand Down
2 changes: 2 additions & 0 deletions lib/stm32wba/hci/README
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ Patch List:
app_conf.h
scm.c
ll_intf_cmn.h
ll_sys_if.c
linklayer_plat.c

* Changes from official delivery:
- dos2unix applied
Expand Down
78 changes: 78 additions & 0 deletions lib/stm32wba/hci/linklayer_plat.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright (c) 2023 STMicroelectronics
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <zephyr/logging/log.h>

#include "scm.h"

#define LOG_LEVEL CONFIG_SOC_LOG_LEVEL
LOG_MODULE_REGISTER(linklayer_plat);

/* Radio bus clock control variables */
uint8_t AHB5_SwitchedOff;
uint32_t radio_sleep_timer_val;

void LINKLAYER_PLAT_ClockInit(void)
{
AHB5_SwitchedOff = 0;
radio_sleep_timer_val = 0;

LL_PWR_EnableBkUpAccess();

/* Select LSE as Sleep CLK */
__HAL_RCC_RADIOSLPTIM_CONFIG(RCC_RADIOSTCLKSOURCE_LSE);

LL_PWR_DisableBkUpAccess();

/* Enable AHB5ENR peripheral clock (bus CLK) */
__HAL_RCC_RADIO_CLK_ENABLE();
}

void LINKLAYER_PLAT_WaitHclkRdy(void)
{
while (HAL_RCCEx_GetRadioBusClockReadiness() != RCC_RADIO_BUS_CLOCK_READY) {
}
}

void LINKLAYER_PLAT_AclkCtrl(uint8_t enable)
{
LOG_DBG("enable: %d", enable);
if (enable) {
/* Enable RADIO baseband clock (active CLK) */
HAL_RCCEx_EnableRadioBBClock();

/* Polling on HSE32 activation */
while (LL_RCC_HSE_IsReady() == 0) {
}
} else {
/* Disable RADIO baseband clock (active CLK) */
HAL_RCCEx_DisableRadioBBClock();
}
}

void LINKLAYER_PLAT_NotifyWFIEnter(void)
{
/* Check if Radio state will allow the AHB5 clock to be cut */

/* AHB5 clock will be cut in the following cases:
* - 2.4GHz radio is not in ACTIVE mode (in SLEEP or DEEPSLEEP mode).
* - RADIOSMEN and STRADIOCLKON bits are at 0.
*/
if ((LL_PWR_GetRadioMode() != LL_PWR_RADIO_ACTIVE_MODE) ||
((__HAL_RCC_RADIO_IS_CLK_SLEEP_ENABLED() == 0) &&
(LL_RCC_RADIO_IsEnabledSleepTimerClock() == 0))) {
AHB5_SwitchedOff = 1;
}
}

void LINKLAYER_PLAT_NotifyWFIExit(void)
{
/* Check if AHB5 clock has been turned of and needs resynchronisation */
if (AHB5_SwitchedOff) {
/* Read sleep register as earlier as possible */
radio_sleep_timer_val = ll_intf_cmn_get_slptmr_value();
}
}
102 changes: 102 additions & 0 deletions lib/stm32wba/hci/ll_sys_if.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* Copyright (c) 2023 STMicroelectronics
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <zephyr/logging/log.h>
#define LOG_LEVEL CONFIG_SOC_LOG_LEVEL
LOG_MODULE_REGISTER(ll_sys_if);

#include "ll_intf.h"
#include "ll_intf_cmn.h"
#include "utilities_common.h"

static void ll_sys_sleep_clock_source_selection(void);

void ll_sys_reset(void);

void ll_sys_config_params(void)
{
ll_intf_config_ll_ctx_params(USE_RADIO_LOW_ISR, NEXT_EVENT_SCHEDULING_FROM_ISR);
}

#if (CFG_RADIO_LSE_SLEEP_TIMER_CUSTOM_SCA_RANGE == 0)
uint8_t ll_sys_BLE_sleep_clock_accuracy_selection(void)
{
uint8_t BLE_sleep_clock_accuracy = 0;
uint32_t RevID = LL_DBGMCU_GetRevisionID();
uint32_t linklayer_slp_clk_src = LL_RCC_RADIO_GetSleepTimerClockSource();

if (linklayer_slp_clk_src == LL_RCC_RADIOSLEEPSOURCE_LSE) {
/* LSE selected as Link Layer sleep clock source. */
/* Sleep clock accuracy is different regarding the WBA device ID and revision */
#if defined(STM32WBA52xx) || defined(STM32WBA54xx) || defined(STM32WBA55xx)
if (RevID == REV_ID_A) {
BLE_sleep_clock_accuracy = STM32WBA5x_REV_ID_A_SCA_RANGE;
} else if (RevID == REV_ID_B) {
BLE_sleep_clock_accuracy = STM32WBA5x_REV_ID_B_SCA_RANGE;
} else {
/* Revision ID not supported, default value of 500ppm applied */
BLE_sleep_clock_accuracy = STM32WBA5x_DEFAULT_SCA_RANGE;
}
#else
UNUSED(RevID);
#endif
/* defined(STM32WBA52xx) || defined(STM32WBA54xx) || defined(STM32WBA55xx) */
} else {
/* LSE is not the Link Layer sleep clock source, */
/* sleep clock accuracy default value is 500 ppm */
BLE_sleep_clock_accuracy = STM32WBA5x_DEFAULT_SCA_RANGE;
}

return BLE_sleep_clock_accuracy;
}
#endif /* CFG_RADIO_LSE_SLEEP_TIMER_CUSTOM_SCA_RANGE */

void ll_sys_sleep_clock_source_selection(void)
{
uint16_t freq_value = 0;
uint32_t linklayer_slp_clk_src = LL_RCC_RADIOSLEEPSOURCE_NONE;

linklayer_slp_clk_src = LL_RCC_RADIO_GetSleepTimerClockSource();
switch (linklayer_slp_clk_src) {
case LL_RCC_RADIOSLEEPSOURCE_LSE:
linklayer_slp_clk_src = RTC_SLPTMR;
break;

case LL_RCC_RADIOSLEEPSOURCE_LSI:
linklayer_slp_clk_src = RCO_SLPTMR;
break;

case LL_RCC_RADIOSLEEPSOURCE_HSE_DIV1000:
linklayer_slp_clk_src = CRYSTAL_OSCILLATOR_SLPTMR;
break;

case LL_RCC_RADIOSLEEPSOURCE_NONE:
/* No Link Layer sleep clock source selected */
assert_param(0);
break;
}
ll_intf_cmn_le_select_slp_clk_src((uint8_t)linklayer_slp_clk_src, &freq_value);
}

void ll_sys_reset(void)
{
#if (CFG_RADIO_LSE_SLEEP_TIMER_CUSTOM_SCA_RANGE == 0)
uint8_t bsca = 0;
#endif /* CFG_RADIO_LSE_SLEEP_TIMER_CUSTOM_SCA_RANGE */

/* Apply the selected link layer sleep timer source */
ll_sys_sleep_clock_source_selection();

/* Configure the link layer sleep clock accuracy if different from the default one */
#if (CFG_RADIO_LSE_SLEEP_TIMER_CUSTOM_SCA_RANGE != 0)
ll_intf_le_set_sleep_clock_accuracy(CFG_RADIO_LSE_SLEEP_TIMER_CUSTOM_SCA_RANGE);
#else
bsca = ll_sys_BLE_sleep_clock_accuracy_selection();
if (bsca != STM32WBA5x_DEFAULT_SCA_RANGE) {
ll_intf_le_set_sleep_clock_accuracy(bsca);
}
#endif /* CFG_RADIO_LSE_SLEEP_TIMER_CUSTOM_SCA_RANGE */
}
2 changes: 2 additions & 0 deletions scripts/ble_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@
ble_heartrate_app_path + "/STM32_WPAN/Target/power_table.c",
ble_heartrate_app_path + "/STM32_WPAN/Target/bpka.c",
ble_heartrate_app_path + "/STM32_WPAN/Target/bpka.h",
ble_heartrate_app_path + "/STM32_WPAN/Target/linklayer_plat.c",
ble_heartrate_app_path + "/STM32_WPAN/Target/ll_sys_if.c",
"Utilities/trace/adv_trace/stm32_adv_trace.h",
"Utilities/misc/stm32_mem.h",
"Utilities/tim_serv/stm32_timer.h",
Expand Down

0 comments on commit 16ef0ae

Please sign in to comment.