Initial commit of firmware

This commit is contained in:
2025-04-12 13:30:57 +01:00
commit 264a3462e0
374 changed files with 332649 additions and 0 deletions

View File

@@ -0,0 +1,144 @@
/***************************************************************************//**
* @file
* @brief HFXO Manager API definition.
*******************************************************************************
* # License
* <b>Copyright 2019 Silicon Laboratories Inc. www.silabs.com</b>
*******************************************************************************
*
* SPDX-License-Identifier: Zlib
*
* The licensor of this software is Silicon Laboratories Inc.
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*
******************************************************************************/
/***************************************************************************//**
* @addtogroup hfxo_manager HFXO Manager
* @brief HFXO Manager
* @details
* ## Overview
*
* HFXO Manager is a platform service module intended to manage the High
* Frequency Crystal Oscillator (HFXO) module and offer related functionalities
* and services.
* For the moment, this module is only supported on Silicon Labs series 2
* devices.
* Among others, it handles the HFXO startup failures. This is to support
* sleepy crystals (crystals where the ESR value could change unexpectedly up
* to 5 times its value during the startup). In case of a failure during the
* HFXO startup, the HFXO Manager will retry the startup process with more
* aggressive settings (sleepy crystal settings) to try waking up the crystal
* from its sleepy state so that the ESR value can fall back to normal values.
* Once the crystal is out of its sleepy state, the module will put back the
* normal settings ensuring the right oscillation frequency. This feature can
* be enabled/disabled via the configuration define
* SL_HFXO_MANAGER_SLEEPY_CRYSTAL_SUPPORT.
* The module catches startup failures through interrupts using the HFXO
* interrupt handler. If your application also needs the HFXO interrupt
* handler, the configuration define SL_HFXO_MANAGER_CUSTOM_HFXO_IRQ_HANDLER
* can be used to remove the HFXO interrupt handler definition from the HFXO
* Manager so that it can be defined in your application. In that case, your
* definition of the HFXO Interrupt Handler will need to call the
* sl_hfxo_manager_irq_handler() function so that HFXO Manager can continue
* to work properly.
* The HFXO Manager is also required by the Power Manager module for some
* internal features and therefore becomes mandatory every time the Power
* Manager is present.
*
*
* ## Initialization
*
* Two functions are required to initialize the module.
* sl_hfxo_manager_init_hardware() is to initialize the HFXO interrupts and
* must therefore be called before any other HFXO initialization functions like
* the emlib CMU_HFXOInit() or device_init function sl_device_init_hfxo().
* The second initialization function sl_hfxo_manager_init() is required for
* internal use and needs to be called before going to sleep.
*
* @{
******************************************************************************/
#ifndef SL_HFXO_MANAGER_H
#define SL_HFXO_MANAGER_H
#include "sl_status.h"
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
/// @brief Sleepy Crystal settings
typedef struct sl_hfxo_manager_sleepy_xtal_settings {
uint32_t ana_ctune; ///< Tuning Capacitance values for XI and XO during startup intermediate and steady stages
uint32_t core_bias_current; ///< Core Bias current value during all stages
} sl_hfxo_manager_sleepy_xtal_settings_t;
/***************************************************************************//**
* HFXO Manager module hardware specific initialization.
******************************************************************************/
void sl_hfxo_manager_init_hardware(void);
/***************************************************************************//**
* Initialize HFXO Manager module.
*
* @return Status Code.
******************************************************************************/
sl_status_t sl_hfxo_manager_init(void);
/***************************************************************************//**
* Updates Sleepy Crystal settings.
*
* @param settings Pointer to settings structure
*
* @return Status Code.
*
* @note Those settings are temporarily used to force oscillation on sleepy
* crystal.
* Default values should be enough to wake-up sleepy crystals. Otherwise,
* this function can be used.
******************************************************************************/
sl_status_t sl_hfxo_manager_update_sleepy_xtal_settings(const sl_hfxo_manager_sleepy_xtal_settings_t *settings);
/***************************************************************************//**
* When this callback function is called, it means that HFXO failed twice in
* a row to start with normal configurations. This may mean that there is a
* bad crystal. When getting this callback, HFXO is running but its properties
* (frequency, precision) are not guaranteed. This should be considered as an
* error situation.
******************************************************************************/
void sl_hfxo_manager_notify_consecutive_failed_startups(void);
/***************************************************************************//**
* HFXO Manager HFXO interrupt handler.
*
* @note This function must be called by the HFXO interrupt handler in order
* to support the HFXO Manager module.
* This function handles the HFXO_IF_RDY, HFXO_IF_DNSERR and
* HFXO_XTALCTRL_SKIPCOREBIASOPT interrupt flags.
******************************************************************************/
void sl_hfxo_manager_irq_handler(void);
/** @} (end addtogroup hfxo_manager) */
#ifdef __cplusplus
}
#endif
#endif // SL_HFXO_MANAGER_H

View File

@@ -0,0 +1,102 @@
/***************************************************************************//**
* @file
* @brief HFXO Manager API definition.
*******************************************************************************
* # License
* <b>Copyright 2019 Silicon Laboratories Inc. www.silabs.com</b>
*******************************************************************************
*
* SPDX-License-Identifier: Zlib
*
* The licensor of this software is Silicon Laboratories Inc.
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*
******************************************************************************/
#ifndef SLI_HFXO_MANAGER_H
#define SLI_HFXO_MANAGER_H
#include <stdbool.h>
#if defined(SL_COMPONENT_CATALOG_PRESENT)
#include "sl_component_catalog.h"
#endif
#if defined(SL_CATALOG_SLEEPTIMER_PRESENT) && defined(SYSRTC_PRESENT)
#include "sli_sleeptimer.h"
#if (SL_SLEEPTIMER_PERIPHERAL == SL_SLEEPTIMER_PERIPHERAL_SYSRTC)
#define HFXO_MANAGER_SLEEPTIMER_SYSRTC_INTEGRATION_ON
#endif
#endif
#ifdef __cplusplus
extern "C" {
#endif
/***************************************************************************//**
* HFXO Manager module hardware specific initialization.
******************************************************************************/
void sli_hfxo_manager_init_hardware(void);
/***************************************************************************//**
* Function to call just before starting HFXO, to save current tick count.
******************************************************************************/
void sli_hfxo_manager_begin_startup_measurement(void);
#if defined(HFXO_MANAGER_SLEEPTIMER_SYSRTC_INTEGRATION_ON)
/***************************************************************************//**
* Function to call when a SYSRTC compare match event produces a PRS signal to
start HFXO.
******************************************************************************/
void sli_hfxo_manager_retrieve_begining_startup_measurement(void);
#endif
/***************************************************************************//**
* Function to call just after HFXO becomes ready, to save current tick count
* and calculate HFXO startup time.
******************************************************************************/
void sli_hfxo_manager_end_startup_measurement(void);
/***************************************************************************//**
* Retrieves HFXO startup time average value.
******************************************************************************/
uint32_t sli_hfxo_manager_get_startup_time(void);
/***************************************************************************//**
* Retrieves HFXO startup time latest value.
******************************************************************************/
uint32_t sli_hfxo_manager_get_latest_startup_time(void);
/***************************************************************************//**
* Checks if HFXO is ready and, if needed, waits for it to be.
*
* @param wait True, to wait for HFXO to be ready.
* False, otherwise.
*
* @return True, if HFXO is ready.
* False, otherwise.
*
* @note This will also make sure we are not in the process of restarting HFXO
* with different settings.
******************************************************************************/
bool sli_hfxo_manager_is_hfxo_ready(bool wait);
#ifdef __cplusplus
}
#endif
#endif /* SLI_HFXO_MANAGER_H */

View File

@@ -0,0 +1,235 @@
/***************************************************************************//**
* @file
* @brief HFXO Manager API implementation.
*******************************************************************************
* # License
* <b>Copyright 2019 Silicon Laboratories Inc. www.silabs.com</b>
*******************************************************************************
*
* SPDX-License-Identifier: Zlib
*
* The licensor of this software is Silicon Laboratories Inc.
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*
******************************************************************************/
#include "em_device.h"
#include "sl_hfxo_manager.h"
#include "sli_hfxo_manager.h"
#include "sli_hfxo_manager_internal.h"
#include "sl_sleeptimer.h"
#include "sl_assert.h"
#include "sl_status.h"
#include <stdbool.h>
/*******************************************************************************
********************************* DEFINES *********************************
******************************************************************************/
// Table size of HFXO wake-up time measurement
#define HFXO_STARTUP_TIME_TABLE_SIZE 10
// Default time value in microseconds required to wake-up the hfxo oscillator.
#define HFXO_STARTUP_TIME_DEFAULT_VALUE_US (600u)
/*******************************************************************************
***************************** DATA TYPES **********************************
******************************************************************************/
/*******************************************************************************
*************************** LOCAL VARIABLES ********************************
******************************************************************************/
// Time in ticks required for HFXO start-up after wake-up from sleep.
static volatile uint32_t hfxo_startup_time_tick = 0;
static volatile uint32_t hfxo_last_startup_time = 0;
static uint32_t hfxo_startup_time_table[HFXO_STARTUP_TIME_TABLE_SIZE];
static uint8_t hfxo_startup_time_table_index = 0;
static uint32_t hfxo_startup_time_sum_average = 0;
static volatile uint32_t hfxo_startup_time_tc_initial = 0;
static volatile bool hfxo_measurement_on = false;
/*******************************************************************************
************************** GLOBAL FUNCTIONS *******************************
******************************************************************************/
/***************************************************************************//**
* HFXO Manager module hardware specific initialization.
******************************************************************************/
void sl_hfxo_manager_init_hardware(void)
{
sli_hfxo_manager_init_hardware();
}
/***************************************************************************//**
* Initialize HFXO Manager module.
******************************************************************************/
sl_status_t sl_hfxo_manager_init(void)
{
sl_status_t status;
// Initialize Sleeptimer module in case not already done.
status = sl_sleeptimer_init();
if (status != SL_STATUS_OK) {
return status;
}
#if defined(HFXO_MANAGER_SLEEPTIMER_SYSRTC_INTEGRATION_ON)
// Additional Sleeptimer HW configuration if SYSRTC is used
sli_sleeptimer_hal_hfxo_manager_integration_init();
#endif
// Set HFXO startup time to conservative default value
hfxo_startup_time_tick = (((HFXO_STARTUP_TIME_DEFAULT_VALUE_US * sl_sleeptimer_get_timer_frequency()) + (1000000 - 1)) / 1000000);
for (uint8_t i = 0; i < HFXO_STARTUP_TIME_TABLE_SIZE; i++) {
hfxo_startup_time_table[i] = hfxo_startup_time_tick;
hfxo_startup_time_sum_average += hfxo_startup_time_tick;
}
return SL_STATUS_OK;
}
/***************************************************************************//**
* Updates Sleepy Crystal settings.
*
* @param settings Pointer to settings structure
*
* @return Status Code.
*
* @note Those settings are temporarily used to force oscillation on sleepy
* crystal.
* Default values should be enough to wake-up sleepy crystals. Otherwise,
* this function can be used.
******************************************************************************/
sl_status_t sl_hfxo_manager_update_sleepy_xtal_settings(const sl_hfxo_manager_sleepy_xtal_settings_t *settings)
{
return sli_hfxo_manager_update_sleepy_xtal_settings_hardware(settings);
}
/***************************************************************************//**
* When this callback function is called, it means that HFXO failed twice in
* a row to start with normal configurations. This may mean that there is a
* bad crystal. When getting this callback, HFXO is running but its properties
* (frequency, precision) are not guaranteed. This should be considered as an
* error situation.
******************************************************************************/
__WEAK void sl_hfxo_manager_notify_consecutive_failed_startups(void)
{
EFM_ASSERT(false);
}
/*******************************************************************************
********************** GLOBAL INTERNAL FUNCTIONS **************************
******************************************************************************/
/***************************************************************************//**
* Function to call just before starting HFXO, to save current tick count.
******************************************************************************/
void sli_hfxo_manager_begin_startup_measurement(void)
{
hfxo_measurement_on = true;
hfxo_startup_time_tc_initial = sl_sleeptimer_get_tick_count();
}
#if defined(HFXO_MANAGER_SLEEPTIMER_SYSRTC_INTEGRATION_ON)
/***************************************************************************//**
* Function to retrieve the capture channel value that was saved when
* HFXO became enabled.
*
* @note SYSRTC Capture channel is used to save when HFXO becomes enabled.
* The HFXO startup measurement will only be done based on the capture
* channel if the capture value is valid.
******************************************************************************/
void sli_hfxo_manager_retrieve_begining_startup_measurement(void)
{
// ULFRCO does not have the precision to measure the HFXO startup time.
// So just return if ULFRCO is used as source oscillator.
if (sl_sleeptimer_get_timer_frequency() <= SystemULFRCOClockGet()) {
return;
}
uint32_t startup_time = sli_sleeptimer_get_capture();
if (startup_time != 0) {
hfxo_startup_time_tc_initial = startup_time;
hfxo_measurement_on = true;
}
}
#endif
/***************************************************************************//**
* Function to call just after HFXO becomes ready, to save current tick count
* and calculate HFXO startup time.
******************************************************************************/
void sli_hfxo_manager_end_startup_measurement(void)
{
uint32_t default_startup_ticks;
if (hfxo_measurement_on == false) {
return;
}
hfxo_last_startup_time = sl_sleeptimer_get_tick_count() - hfxo_startup_time_tc_initial;
// With low precision clock, the HFXO startup time measure could be zero.
// In that case, ensure it's a least 1 tick.
hfxo_last_startup_time = (hfxo_last_startup_time == 0) ? 1 : hfxo_last_startup_time;
// Skip measurement if value is out of bound
default_startup_ticks = (((HFXO_STARTUP_TIME_DEFAULT_VALUE_US * sl_sleeptimer_get_timer_frequency()) + (1000000 - 1)) / 1000000);
EFM_ASSERT(hfxo_last_startup_time <= default_startup_ticks);
if (hfxo_last_startup_time > default_startup_ticks) {
hfxo_measurement_on = false;
return;
}
// Calculate average for HFXO restore time
hfxo_startup_time_sum_average -= (int32_t)hfxo_startup_time_table[hfxo_startup_time_table_index] - (int32_t)hfxo_last_startup_time;
hfxo_startup_time_table[hfxo_startup_time_table_index] = hfxo_last_startup_time;
hfxo_startup_time_tick = ((hfxo_startup_time_sum_average + (HFXO_STARTUP_TIME_TABLE_SIZE - 1) ) / HFXO_STARTUP_TIME_TABLE_SIZE);
// Update index of wakeup time table
hfxo_startup_time_table_index++;
hfxo_startup_time_table_index %= HFXO_STARTUP_TIME_TABLE_SIZE;
hfxo_measurement_on = false;
}
/***************************************************************************//**
* Retrieves HFXO startup time average value.
*
* @return HFXO startup time average value.
******************************************************************************/
uint32_t sli_hfxo_manager_get_startup_time(void)
{
return hfxo_startup_time_tick;
}
/***************************************************************************//**
* Retrieves HFXO startup time latest value.
*
* @return HFXO startup time latest value.
******************************************************************************/
uint32_t sli_hfxo_manager_get_latest_startup_time(void)
{
return hfxo_last_startup_time;
}

View File

@@ -0,0 +1,410 @@
/***************************************************************************//**
* @file
* @brief HFXO Manager HAL series 2 Devices.
*******************************************************************************
* # License
* <b>Copyright 2019 Silicon Laboratories Inc. www.silabs.com</b>
*******************************************************************************
*
* SPDX-License-Identifier: Zlib
*
* The licensor of this software is Silicon Laboratories Inc.
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*
******************************************************************************/
#include "em_device.h"
#if defined(_SILICON_LABS_32B_SERIES_2)
#include "sl_assert.h"
#include "sl_core.h"
#include "sli_hfxo_manager.h"
#include "sl_hfxo_manager.h"
#include "sl_hfxo_manager_config.h"
#include "sl_status.h"
#include <stdbool.h>
/*******************************************************************************
********************************* DEFINES *********************************
******************************************************************************/
#if (defined(SL_HFXO_MANAGER_SLEEPY_CRYSTAL_SUPPORT) \
&& (SL_HFXO_MANAGER_SLEEPY_CRYSTAL_SUPPORT == 1) \
&& defined(SL_CATALOG_POWER_MANAGER_DEEPSLEEP_BLOCKING_HFXO_RESTORE_PRESENT))
#error Component power_manager_deepsleep_blocking_hfxo_restore is not compatible with SL_HFXO_MANAGER_SLEEPY_CRYSTAL_SUPPORT configuration
#endif
// Defines for hidden field FORCERAWCLK in HFXO_CTRL register
#define _HFXO_MANAGER_CTRL_FORCERAWCLK_SHIFT 31
#define _HFXO_MANAGER_CTRL_FORCERAWCLK_MASK 0x80000000UL
#define HFXO_MANAGER_CTRL_FORCERAWCLK (0x1UL << _HFXO_MANAGER_CTRL_FORCERAWCLK_SHIFT)
// Defines for hidden PKDETCTRL register
#define HFXO_MANAGER_PKDETCTRL (*((volatile uint32_t *)(HFXO0_BASE + 0x34UL)))
#define _HFXO_MANAGER_PKDETCTRL_PKDETTHSTARTUPI_SHIFT 8
#define _HFXO_MANAGER_PKDETCTRL_PKDETTHSTARTUPI_MASK 0xF00UL
#define HFXO_MANAGER_PKDETCTRL_PKDETTHSTARTUPI_V105MV (0x00000000UL << _HFXO_MANAGER_PKDETCTRL_PKDETTHSTARTUPI_SHIFT)
#define HFXO_MANAGER_PKDETCTRL_PKDETTHSTARTUPI_V132MV (0x00000001UL << _HFXO_MANAGER_PKDETCTRL_PKDETTHSTARTUPI_SHIFT)
#define HFXO_MANAGER_PKDETCTRL_PKDETTHSTARTUPI_V157MV (0x00000002UL << _HFXO_MANAGER_PKDETCTRL_PKDETTHSTARTUPI_SHIFT)
#define HFXO_MANAGER_PKDETCTRL_PKDETTHSTARTUPI_V184MV (0x00000003UL << _HFXO_MANAGER_PKDETCTRL_PKDETTHSTARTUPI_SHIFT)
#define HFXO_MANAGER_PKDETCTRL_PKDETTHSTARTUPI_V210MV (0x00000004UL << _HFXO_MANAGER_PKDETCTRL_PKDETTHSTARTUPI_SHIFT)
#define HFXO_MANAGER_PKDETCTRL_PKDETTHSTARTUPI_V236MV (0x00000005UL << _HFXO_MANAGER_PKDETCTRL_PKDETTHSTARTUPI_SHIFT)
#define HFXO_MANAGER_PKDETCTRL_PKDETTHSTARTUPI_V262MV (0x00000006UL << _HFXO_MANAGER_PKDETCTRL_PKDETTHSTARTUPI_SHIFT)
#define HFXO_MANAGER_PKDETCTRL_PKDETTHSTARTUPI_V289MV (0x00000007UL << _HFXO_MANAGER_PKDETCTRL_PKDETTHSTARTUPI_SHIFT)
#define HFXO_MANAGER_PKDETCTRL_PKDETTHSTARTUPI_V315MV (0x00000008UL << _HFXO_MANAGER_PKDETCTRL_PKDETTHSTARTUPI_SHIFT)
#define HFXO_MANAGER_PKDETCTRL_PKDETTHSTARTUPI_V341MV (0x00000009UL << _HFXO_MANAGER_PKDETCTRL_PKDETTHSTARTUPI_SHIFT)
#define HFXO_MANAGER_PKDETCTRL_PKDETTHSTARTUPI_V367MV (0x0000000AUL << _HFXO_MANAGER_PKDETCTRL_PKDETTHSTARTUPI_SHIFT)
#define HFXO_MANAGER_PKDETCTRL_PKDETTHSTARTUPI_V394MV (0x0000000BUL << _HFXO_MANAGER_PKDETCTRL_PKDETTHSTARTUPI_SHIFT)
#define HFXO_MANAGER_PKDETCTRL_PKDETTHSTARTUPI_V420MV (0x0000000CUL << _HFXO_MANAGER_PKDETCTRL_PKDETTHSTARTUPI_SHIFT)
#define HFXO_MANAGER_PKDETCTRL_PKDETTHSTARTUPI_V446MV (0x0000000DUL << _HFXO_MANAGER_PKDETCTRL_PKDETTHSTARTUPI_SHIFT)
#define HFXO_MANAGER_PKDETCTRL_PKDETTHSTARTUPI_V472MV (0x0000000EUL << _HFXO_MANAGER_PKDETCTRL_PKDETTHSTARTUPI_SHIFT)
#define HFXO_MANAGER_PKDETCTRL_PKDETTHSTARTUPI_V499MV (0x0000000FUL << _HFXO_MANAGER_PKDETCTRL_PKDETTHSTARTUPI_SHIFT)
// IRQ Name depending on devices
#if defined(_SILICON_LABS_32B_SERIES_2_CONFIG_1)
#define HFXO_IRQ_NUMBER HFXO00_IRQn
#define HFXO_IRQ_HANDLER_FUNCTION HFXO00_IRQHandler
#else
#define HFXO_IRQ_NUMBER HFXO0_IRQn
#define HFXO_IRQ_HANDLER_FUNCTION HFXO0_IRQHandler
#endif
// Default values for the Sleepy Crystal settings
// Should be enough to guaranty HFXO startup
#define SLEEPY_XTAL_SETTING_DEFAULT_PKDETTHSTARTUPI HFXO_MANAGER_PKDETCTRL_PKDETTHSTARTUPI_V157MV
#define SLEEPY_XTAL_SETTING_DEFAULT_CTUNEANA 100u
#define SLEEPY_XTAL_SETTING_DEFAULT_COREBIAS 255u
/*******************************************************************************
*************************** LOCAL VARIABLES ********************************
******************************************************************************/
// Error flag to indicate if we failed the startup process
static volatile bool error_flag = false;
#if (SL_HFXO_MANAGER_SLEEPY_CRYSTAL_SUPPORT == 1)
// Error retry counter
static volatile uint8_t error_try_cnt = 0;
// Error State status
static volatile bool in_error_state = false;
// Variables to save normal settings
static uint32_t pkdettusstartupi_saved;
static uint32_t ctunexiana_saved;
static uint32_t ctunexoana_saved;
static uint32_t corebiasana_saved;
static uint32_t corebiasstartup_saved;
static uint32_t corebiasstartupi_saved;
// Variables for Sleepy Crystal settings
static uint32_t sleepy_xtal_settings_pkdettusstartupi = SLEEPY_XTAL_SETTING_DEFAULT_PKDETTHSTARTUPI; // Value already shifted
static uint32_t sleepy_xtal_settings_ctuneana = SLEEPY_XTAL_SETTING_DEFAULT_CTUNEANA;
static uint32_t sleepy_xtal_settings_corebias = SLEEPY_XTAL_SETTING_DEFAULT_COREBIAS;
#endif
/***************************************************************************//**
* HFXO ready notification callback for internal use with power manager
******************************************************************************/
__WEAK void sli_hfxo_manager_notify_ready_for_power_manager(void);
/***************************************************************************//**
* HFXO PRS ready notification callback for internal use with power manager
******************************************************************************/
__WEAK void sli_hfxo_notify_ready_for_power_manager_from_prs(void);
/***************************************************************************//**
* Hardware specific initialization.
******************************************************************************/
void sli_hfxo_manager_init_hardware(void)
{
// Increase HFXO Interrupt priority so that it won't be masked by BASEPRI
// and will preempt other interrupts.
NVIC_SetPriority(HFXO_IRQ_NUMBER, CORE_ATOMIC_BASE_PRIORITY_LEVEL - 1);
// Enable HFXO Interrupt if HFXO is used
#if _SILICON_LABS_32B_SERIES_2_CONFIG >= 2
CMU->CLKEN0_SET = CMU_CLKEN0_HFXO0;
#endif
HFXO0->IEN_CLR = HFXO_IEN_RDY | HFXO_IEN_DNSERR | HFXO_IEN_COREBIASOPTERR;
#if defined(HFXO_MANAGER_SLEEPTIMER_SYSRTC_INTEGRATION_ON)
HFXO0->IEN_CLR = HFXO_IEN_PRSRDY;
#endif
HFXO0->IF_CLR = HFXO_IF_RDY | HFXO_IF_DNSERR | HFXO_IEN_COREBIASOPTERR;
#if defined(HFXO_MANAGER_SLEEPTIMER_SYSRTC_INTEGRATION_ON)
HFXO0->IF_CLR = HFXO_IF_PRSRDY;
#endif
NVIC_ClearPendingIRQ(HFXO_IRQ_NUMBER);
NVIC_EnableIRQ(HFXO_IRQ_NUMBER);
HFXO0->IEN_SET = HFXO_IEN_RDY | HFXO_IEN_DNSERR | HFXO_IEN_COREBIASOPTERR;
#if defined(HFXO_MANAGER_SLEEPTIMER_SYSRTC_INTEGRATION_ON)
HFXO0->IEN_SET = HFXO_IEN_PRSRDY;
HFXO0->CTRL &= ~(_HFXO_CTRL_DISONDEMANDPRS_MASK & HFXO_CTRL_DISONDEMANDPRS_DEFAULT);
HFXO0->CTRL |= HFXO_CTRL_PRSSTATUSSEL1_ENS;
#endif
}
/***************************************************************************//**
* Updates sleepy crystal settings in specific hardware registers.
******************************************************************************/
sl_status_t sli_hfxo_manager_update_sleepy_xtal_settings_hardware(const sl_hfxo_manager_sleepy_xtal_settings_t *settings)
{
#if (SL_HFXO_MANAGER_SLEEPY_CRYSTAL_SUPPORT == 1)
EFM_ASSERT(settings->ana_ctune <= (_HFXO_XTALCTRL_CTUNEXIANA_MASK >> _HFXO_XTALCTRL_CTUNEXIANA_SHIFT));
EFM_ASSERT(settings->core_bias_current <= (_HFXO_XTALCTRL_COREBIASANA_MASK >> _HFXO_XTALCTRL_COREBIASANA_SHIFT));
sleepy_xtal_settings_ctuneana = settings->ana_ctune;
sleepy_xtal_settings_corebias = settings->core_bias_current;
return SL_STATUS_OK;
#else
(void)settings;
return SL_STATUS_NOT_AVAILABLE;
#endif
}
/***************************************************************************//**
* Checks if HFXO is ready and, if needed, waits for it to be.
*
* @note This will also make sure we are not in the process of restarting HFXO
* with different settings.
******************************************************************************/
bool sli_hfxo_manager_is_hfxo_ready(bool wait)
{
bool ready = false;
do {
#if defined(HFXO_MANAGER_SLEEPTIMER_SYSRTC_INTEGRATION_ON)
ready = (((HFXO0->STATUS & (HFXO_STATUS_RDY | HFXO_STATUS_PRSRDY)) != 0) && !error_flag) ? true : false;
#else
ready = (((HFXO0->STATUS & HFXO_STATUS_RDY) != 0) && !error_flag) ? true : false;
#endif
} while (!ready && wait);
return ready;
}
#if (SL_HFXO_MANAGER_CUSTOM_HFXO_IRQ_HANDLER == 0)
/*******************************************************************************
* HFXO interrupt handler.
*
* @note The HFXOx_IRQHandler provided by HFXO Manager will call
* @ref sl_hfxo_manager_irq_handler. Configure SL_HFXO_MANAGER_CUSTOM_HFXO_IRQ_HANDLER
* if the application wants to implement its own HFXOx_IRQHandler.
******************************************************************************/
void HFXO_IRQ_HANDLER_FUNCTION(void)
{
sl_hfxo_manager_irq_handler();
}
#endif
/*******************************************************************************
* HFXO Manager HFXO interrupt handler.
******************************************************************************/
void sl_hfxo_manager_irq_handler(void)
{
uint32_t irq_flag = HFXO0->IF;
#if (SL_HFXO_MANAGER_SLEEPY_CRYSTAL_SUPPORT == 1)
bool disondemand = (HFXO0->CTRL & _HFXO_CTRL_DISONDEMAND_MASK) ? true : false;
bool forceen = (HFXO0->CTRL & _HFXO_CTRL_FORCEEN_MASK) ? true : false;
#endif
#if defined(HFXO_MANAGER_SLEEPTIMER_SYSRTC_INTEGRATION_ON)
if (irq_flag & HFXO_IF_PRSRDY) {
// Clear PRS RDY flag and EM23ONDEMAND
HFXO0->IF_CLR = irq_flag & HFXO_IF_PRSRDY;
HFXO0->CTRL_CLR = HFXO_CTRL_EM23ONDEMAND;
// Only retrieve start of measurement if HFXO is not already ready.
if ((HFXO0->STATUS & HFXO_STATUS_RDY) == 0) {
sli_hfxo_manager_retrieve_begining_startup_measurement();
}
// Notify power manager HFXO is ready
sli_hfxo_notify_ready_for_power_manager_from_prs();
sli_hfxo_manager_notify_ready_for_power_manager();
// Update sleep on isr exit flag
sli_sleeptimer_update_sleep_on_isr_exit(true);
// Reset PRS signal through Sleeptimer
sli_sleeptimer_reset_prs_signal();
}
#endif
// RDY Interrupt Flag Handling
if (irq_flag & HFXO_IF_RDY) {
// Clear Ready flag
HFXO0->IF_CLR = irq_flag & HFXO_IF_RDY;
#if (SL_HFXO_MANAGER_SLEEPY_CRYSTAL_SUPPORT == 1)
if (error_flag) {
// Clear error flag, i.e. we successfully stated HFXO with the modified settings
error_flag = false;
// If it's the first time we succeed after an error, try back the normal settings
if (error_try_cnt <= 1) {
// Disable HFXO.
HFXO0->CTRL_CLR = HFXO_CTRL_FORCEEN;
HFXO0->CTRL_SET = HFXO_CTRL_DISONDEMAND;
while ((HFXO0->STATUS & HFXO_STATUS_ENS) != 0) {
}
// Put back normal settings
HFXO_MANAGER_PKDETCTRL = (HFXO_MANAGER_PKDETCTRL & ~_HFXO_MANAGER_PKDETCTRL_PKDETTHSTARTUPI_MASK) | pkdettusstartupi_saved;
HFXO0->XTALCTRL = (HFXO0->XTALCTRL & ~(_HFXO_XTALCTRL_CTUNEXIANA_MASK | _HFXO_XTALCTRL_CTUNEXOANA_MASK))
| ctunexiana_saved
| ctunexoana_saved;
HFXO0->XTALCFG = (HFXO0->XTALCFG & ~(_HFXO_XTALCFG_COREBIASSTARTUPI_MASK | _HFXO_XTALCFG_COREBIASSTARTUP_MASK))
| corebiasstartup_saved
| corebiasstartupi_saved;
HFXO0->XTALCTRL = (HFXO0->XTALCTRL & ~_HFXO_XTALCTRL_COREBIASANA_MASK) | corebiasana_saved;
// Put back FORCEEN and DISONDEMAND state
if (!disondemand) {
HFXO0->CTRL_CLR = HFXO_CTRL_DISONDEMAND;
} else {
HFXO0->CTRL_SET = HFXO_CTRL_DISONDEMAND;
}
if (forceen) {
HFXO0->CTRL_SET = HFXO_CTRL_FORCEEN;
} else {
HFXO0->CTRL_CLR = HFXO_CTRL_FORCEEN;
}
} else {
// Call notification function to tell users that sleepy crystal settings are kept
// This should only happen if you are in test condition or if you have a bad crystal.
sl_hfxo_manager_notify_consecutive_failed_startups();
in_error_state = true;
}
} else {
sli_hfxo_manager_end_startup_measurement();
sli_hfxo_manager_notify_ready_for_power_manager();
// Clear counter since we successfully started HFXO with normal settings
// or we are just keeping sleepy crystal settings indefinitely.
error_try_cnt = 0;
}
#else
sli_hfxo_manager_end_startup_measurement();
sli_hfxo_manager_notify_ready_for_power_manager();
#endif
}
// DNSERR Interrupt Flag Handling
if (irq_flag & HFXO_IF_DNSERR) {
// Clear error flag
HFXO0->IF_CLR = irq_flag & HFXO_IF_DNSERR;
#if (SL_HFXO_MANAGER_SLEEPY_CRYSTAL_SUPPORT == 1)
// We should not fail twice in a row
EFM_ASSERT(error_flag == false);
// Update global variables related to error.
error_flag = true;
error_try_cnt++;
// Save current settings
pkdettusstartupi_saved = (HFXO_MANAGER_PKDETCTRL & _HFXO_MANAGER_PKDETCTRL_PKDETTHSTARTUPI_MASK);
ctunexiana_saved = (HFXO0->XTALCTRL & _HFXO_XTALCTRL_CTUNEXIANA_MASK);
ctunexoana_saved = (HFXO0->XTALCTRL & _HFXO_XTALCTRL_CTUNEXOANA_MASK);
corebiasana_saved = (HFXO0->XTALCTRL & _HFXO_XTALCTRL_COREBIASANA_MASK);
corebiasstartup_saved = (HFXO0->XTALCFG & _HFXO_XTALCFG_COREBIASSTARTUP_MASK);
corebiasstartupi_saved = (HFXO0->XTALCFG & _HFXO_XTALCFG_COREBIASSTARTUPI_MASK);
// Disable HFXO.
HFXO0->CTRL_CLR = HFXO_CTRL_FORCEEN;
HFXO0->CTRL_SET = HFXO_CTRL_DISONDEMAND;
// Use FORCERAWCLK bit to exit error state when disabling
HFXO0->CTRL_SET = HFXO_MANAGER_CTRL_FORCERAWCLK;
while ((HFXO0->STATUS & _HFXO_STATUS_ENS_MASK) != 0U) {
}
HFXO0->CTRL_CLR = HFXO_MANAGER_CTRL_FORCERAWCLK;
// Change settings:
//Reduce Peak Detection Threshold for Startup Intermediate stage to 2 (V157MV)
HFXO_MANAGER_PKDETCTRL = (HFXO_MANAGER_PKDETCTRL & ~_HFXO_MANAGER_PKDETCTRL_PKDETTHSTARTUPI_MASK) | sleepy_xtal_settings_pkdettusstartupi;
// Reduce CTUNE values for steady stage
if (((ctunexiana_saved >> _HFXO_XTALCTRL_CTUNEXIANA_SHIFT) > 100)
|| ((ctunexoana_saved >> _HFXO_XTALCTRL_CTUNEXOANA_SHIFT) > 100)) {
HFXO0->XTALCTRL = (HFXO0->XTALCTRL & ~(_HFXO_XTALCTRL_CTUNEXIANA_MASK | _HFXO_XTALCTRL_CTUNEXOANA_MASK))
| (sleepy_xtal_settings_ctuneana << _HFXO_XTALCTRL_CTUNEXIANA_SHIFT)
| (sleepy_xtal_settings_ctuneana << _HFXO_XTALCTRL_CTUNEXOANA_SHIFT);
}
// Increase core bias current at all stages
HFXO0->XTALCFG = (HFXO0->XTALCFG & ~(_HFXO_XTALCFG_COREBIASSTARTUPI_MASK | _HFXO_XTALCFG_COREBIASSTARTUP_MASK))
| ((sleepy_xtal_settings_corebias >> 2) << _HFXO_XTALCFG_COREBIASSTARTUPI_SHIFT)
| ((sleepy_xtal_settings_corebias >> 2) << _HFXO_XTALCFG_COREBIASSTARTUP_SHIFT);
HFXO0->XTALCTRL = (HFXO0->XTALCTRL & ~_HFXO_XTALCTRL_COREBIASANA_MASK)
| (sleepy_xtal_settings_corebias << _HFXO_XTALCTRL_COREBIASANA_SHIFT);
// Put back FORCEEN and DISONDEMAND state
if (!disondemand) {
HFXO0->CTRL_CLR = HFXO_CTRL_DISONDEMAND;
} else {
HFXO0->CTRL_SET = HFXO_CTRL_DISONDEMAND;
}
if (forceen) {
HFXO0->CTRL_SET = HFXO_CTRL_FORCEEN;
} else {
HFXO0->CTRL_CLR = HFXO_CTRL_FORCEEN;
}
#endif
}
if (irq_flag & HFXO_IF_COREBIASOPTERR) {
// Clear Core Bias Optimization error flag
HFXO0->IF_CLR = irq_flag & HFXO_IF_COREBIASOPTERR;
#if (SL_HFXO_MANAGER_SLEEPY_CRYSTAL_SUPPORT == 1)
// In case the Core Bias Optimization fails during error handling,
// we disable it
if (in_error_state == true) {
// Disable HFXO.
HFXO0->CTRL_CLR = HFXO_CTRL_FORCEEN;
HFXO0->CTRL_SET = HFXO_CTRL_DISONDEMAND;
while ((HFXO0->STATUS & HFXO_STATUS_ENS) != 0) {
}
// Skip Core Bias Optimization in case of error
HFXO0->XTALCTRL_SET = HFXO_XTALCTRL_SKIPCOREBIASOPT;
// Put back FORCEEN and DISONDEMAND state
if (!disondemand) {
HFXO0->CTRL_CLR = HFXO_CTRL_DISONDEMAND;
} else {
HFXO0->CTRL_SET = HFXO_CTRL_DISONDEMAND;
}
if (forceen) {
HFXO0->CTRL_SET = HFXO_CTRL_FORCEEN;
} else {
HFXO0->CTRL_CLR = HFXO_CTRL_FORCEEN;
}
}
#endif
}
}
#endif // _SILICON_LABS_32B_SERIES_2

View File

@@ -0,0 +1,50 @@
/***************************************************************************//**
* @file
* @brief HFXO Manager Internal API definition.
*******************************************************************************
* # License
* <b>Copyright 2019 Silicon Laboratories Inc. www.silabs.com</b>
*******************************************************************************
*
* SPDX-License-Identifier: Zlib
*
* The licensor of this software is Silicon Laboratories Inc.
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*
******************************************************************************/
#ifndef SLI_HFXO_MANAGER_INTERNAL_H
#define SLI_HFXO_MANAGER_INTERNAL_H
#include "sl_hfxo_manager.h"
#include "sl_status.h"
#ifdef __cplusplus
extern "C" {
#endif
/***************************************************************************//**
* Updates sleepy crystal settings in specific hardware registers.
******************************************************************************/
sl_status_t sli_hfxo_manager_update_sleepy_xtal_settings_hardware(const sl_hfxo_manager_sleepy_xtal_settings_t *settings);
#ifdef __cplusplus
}
#endif
#endif /* SLI_HFXO_MANAGER_INTERNAL_H */