1 /* USER CODE BEGIN Header */
2 /**
3   ******************************************************************************
4   * @file    scm.h
5   * @author  MCD Application Team
6   * @brief   Header for scm.c module
7   ******************************************************************************
8   * @attention
9   *
10   * Copyright (c) 2024 STMicroelectronics.
11   * All rights reserved.
12   *
13   * This software is licensed under terms that can be found in the LICENSE file
14   * in the root directory of this software component.
15   * If no LICENSE file comes with this software, it is provided AS-IS.
16   *
17   ******************************************************************************
18   */
19 /* USER CODE END Header */
20 
21 /* Define to prevent recursive inclusion -------------------------------------*/
22 #ifndef SCM_H
23 #define SCM_H
24 
25 /* Includes ------------------------------------------------------------------*/
26 #include "main.h"
27 
28 #if (CFG_SCM_SUPPORTED == 1)
29 #include "stm32wbaxx_hal.h"
30 #include "stm32wbaxx_ll_pwr.h"
31 #include "stm32wbaxx_ll_rcc.h"
32 #include "stm32wbaxx_ll_tim.h"
33 
34 /* Exported types ------------------------------------------------------------*/
35 typedef enum {
36   NO_CLOCK_CONFIG = 0,
37   HSE_16MHZ,
38   HSE_32MHZ,
39   SYS_PLL,
40 } scm_clockconfig_t;
41 
42 typedef enum {
43   LP,
44   RUN,
45   HSE16,
46   HSE32,
47   PLL,
48 } scm_ws_lp_t;
49 
50 typedef enum {
51   HSEPRE_DISABLE = 0,
52   HSEPRE_ENABLE
53 } scm_hse_hsepre_t;
54 
55 typedef enum {
56   SCM_USER_APP,
57   SCM_USER_LL_FW,
58   SCM_USER_LL_HW_RCO_CLBR,
59   /* USER CODE BEGIN SCM_USER */
60 
61   /* USER CODE END SCM_USER */
62   TOTAL_CLIENT_NUM, /* To be at the end of the enum */
63 } scm_user_id_t;
64 
65 typedef enum {
66   NO_PLL,
67   PLL_INTEGER_MODE,
68   PLL_FRACTIONAL_MODE,
69 } scm_pll_mode_t;
70 
71 typedef enum {
72   SCM_RADIO_NOT_ACTIVE = 0,
73   SCM_RADIO_ACTIVE,
74 } scm_radio_state_t;
75 
76 typedef struct {
77   uint8_t are_pll_params_initialized;
78   scm_pll_mode_t pll_mode;
79   uint32_t PLLM;
80   uint32_t PLLN;
81   uint32_t PLLP;
82   uint32_t PLLQ;
83   uint32_t PLLR;
84   uint32_t PLLFractional;
85   uint32_t AHB5_PLL1_CLKDivider;
86 } scm_pll_config_t;
87 
88 typedef struct{
89   scm_clockconfig_t targeted_clock_freq;
90   uint32_t flash_ws_cfg;
91   uint32_t sram_ws_cfg;
92   scm_pll_config_t pll;
93 } scm_system_clock_t;
94 
95 /* Exported constants --------------------------------------------------------*/
96 /* Exported variables --------------------------------------------------------*/
97 
98 /* Exported macro ------------------------------------------------------------*/
99 /* Exported functions prototypes ---------------------------------------------*/
100 
101 /**
102   * @brief  System Clock Manager init code
103   * @param  None
104   * @retval None
105   */
106 void scm_init(void);
107 
108 /**
109   * @brief  Setup the system clock source in usable configuration for Connectivity use cases.
110   *         Called at startup or out of low power modes.
111   * @param  None
112   * @retval None
113   */
114 void scm_setup(void);
115 
116 /**
117   * @brief  Configure the PLL mode and parameters before PLL selection as system clock.
118   * @param  p_pll_config PLL coniguration to apply
119   * @retval None
120   * @note   scm_pll_setconfig to be called before PLL activation (PLL set as system core clock)
121   */
122 void scm_pll_setconfig(const scm_pll_config_t *p_pll_config);
123 
124 /**
125   * @brief  Restore system clock configuration when moving out of standby.
126   * @param  None
127   * @retval None
128   */
129 void scm_standbyexit(void);
130 
131 /**
132   * @brief  Return the state of the Radio.
133   * @param  None
134   * @retval radio_state
135   */
136 scm_radio_state_t isRadioActive(void);
137 
138 /**
139   * @brief  Configure the PLL for switching fractional parameters on the fly.
140   * @param  pll_frac Up to date fractional configuration.
141   * @retval None
142   * @note   A PLL update is requested only when the system clock is
143   *         running on the PLL with a different configuration that the
144   *         one required.
145   */
146 void scm_pll_fractional_update(uint32_t pll_frac);
147 
148 /**
149   * @brief  Set the HSE clock to the requested frequency.
150   * @param  user_id This parameter can be one of the following:
151   *         @arg SCM_USER_APP
152   *         @arg SCM_USER_LL_FW
153   * @param  sysclockconfig This parameter can be one of the following:
154   *         @arg HSE_16MHZ
155   *         @arg HSE_32MHZ
156   *         @arg SYS_PLL
157   * @retval None
158   */
159 void scm_setsystemclock (scm_user_id_t user_id, scm_clockconfig_t sysclockconfig);
160 
161 /**
162   * @brief  Called each time the PLL is ready
163   * @param  None
164   * @retval None
165   * @note   This function is defined as weak in SCM module.
166   *         Can be overridden by user.
167   */
168 void scm_pllready(void);
169 
170 /**
171   * @brief  Configure the Flash and SRAMs wait cycle (when required for system clock source change)
172   * @param  ws_lp_config: This parameter can be one of the following:
173   *         @arg LP
174   *         @arg RUN
175   *         @arg HSE16
176   *         @arg HSE32
177   *         @arg PLL
178   * @retval None
179   */
180 void scm_setwaitstates(const scm_ws_lp_t ws_lp_config);
181 
182 /**
183   * @brief  Notify the state of the Radio
184   * @param  radio_state: This parameter can be one of the following:
185   *         @arg SCM_RADIO_ACTIVE
186   *         @arg SCM_RADIO_NOT_ACTIVE
187   * @retval None
188   */
189 void scm_notifyradiostate(const scm_radio_state_t radio_state);
190 
191 /**
192   * @brief  SCM HSERDY interrupt handler.
193   *         Switch system clock on HSE.
194   * @param  None
195   * @retval None
196   */
197 void scm_hserdy_isr(void);
198 
199 /**
200   * @brief  SCM PLLRDY interrupt handler.
201   *         Switch system clock on PLL.
202   * @param  None
203   * @retval None
204   */
205 void scm_pllrdy_isr(void);
206 
207 /* SCM HSE BEGIN */
208 /**
209  * @brief Getter for SW HSERDY flag
210  */
211 uint8_t SCM_HSE_Get_SW_HSERDY(void);
212 
213 /**
214  * @brief Setter for SW HSERDY flag
215  */
216 void SCM_HSE_Set_SW_HSERDY(void);
217 
218 /**
219  * @brief Clean of SW HSERDY flag
220  */
221 void SCM_HSE_Clear_SW_HSERDY(void);
222 
223 /**
224  * @brief Polling function to wait until HSE is ready
225  */
226 void SCM_HSE_WaitUntilReady(void);
227 
228 /**
229  * @brief Start the HSE stabilization timer
230  */
231 void SCM_HSE_StartStabilizationTimer(void);
232 
233 /**
234  * @brief Stop the HSE stabilization timer
235  */
236 void SCM_HSE_StopStabilizationTimer(void);
237 
238 /**
239  * @brief HSE stabilization timer interrupt handler
240  */
241 void SCM_HSE_SW_HSERDY_isr(void);
242 /* SCM HSE END */
243 
244 /* Exported functions - To be implemented by the user ------------------------- */
245 
246 /**
247   * @brief  SCM HSI clock enable
248   * @details A weak version is implemented in the module sources.
249   * @details It can be overridden by user.
250   * @param  None
251   * @retval None
252   */
253 extern void SCM_HSI_CLK_ON(void);
254 
255 /**
256   * @brief  SCM HSI clock may be disabled when this function is called
257   * @details A weak version is implemented in the module sources.
258   * @details It can be overridden by user.
259   * @param  None
260   * @retval None
261   */
262 extern void SCM_HSI_CLK_OFF(void);
263 
264 /* SCM HSE BEGIN */
265 /**
266  * @brief Entry hook for HSI switch
267  */
268 extern void SCM_HSI_SwithSystemClock_Entry(void);
269 
270 /**
271  * @brief Exit hook for HSI switch
272  */
273 extern void SCM_HSI_SwithSystemClock_Exit(void);
274 /* SCM HSE END */
275 #else /* CFG_SCM_SUPPORTED */
276 
277 /* Unused empty functions */
278 void scm_hserdy_isr(void);
279 void scm_pllrdy_isr(void);
280 
281 #endif /* CFG_SCM_SUPPORTED */
282 
283 #endif /* SCM_H */
284