1 /*
2  * SPDX-FileCopyrightText: 2019-2025 SiFli Technologies(Nanjing) Co., Ltd
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef __BF0_CP_HAL_H
8 #define __BF0_CP_HAL_H
9 
10 #ifdef __cplusplus
11 extern "C" {
12 #endif
13 
14 #define MAKE_REG_VAL(val, mask, offset)        ((((uint32_t)(val)) << (offset)) & (mask))
15 #define GET_REG_VAL(reg, mask, offset)        ((((uint32_t)(reg)) & (mask)) >> (offset))
16 
17 //#define HAL_DEBUG_ENABLED
18 //#define TARMAC
19 
20 /* Includes ------------------------------------------------------------------*/
21 #ifdef SOC_BF0_HCPU
22 #include "bf0_hal_conf_hcpu.h"
23 #elif defined(SOC_BF0_LCPU)
24 #include "bf0_hal_conf_lcpu.h"
25 #elif defined(WIN32)
26 #define __IO volatile
27 #define __STATIC_INLINE                        static __inline
28 #define __get_PRIMASK() 0
29 #define __set_PRIMASK(mask) 0
30 #else
31 #error "Core not defined."
32 #endif
33 #include "bf0_hal_hlp.h"
34 
35 /** @addtogroup BF0_HAL_Driver Hardware Abstract Layer
36   * @brief Hardware Abstract Layer driver interface
37   * @{
38   */
39 
40 /** @addtogroup HAL_COMMON Hal Common functions
41  *  @{
42  *
43 */
44 
45 /** @addtogroup HAL_SYS_CFG System Configuration
46   * @ingroup HAL_COMMON
47   * @{
48   */
49 
50 /* Exported types ------------------------------------------------------------*/
51 /* Exported constants --------------------------------------------------------*/
52 /** @addtogroup SYSCFG_Exported_Constants SYSCFG Exported Constants
53   * @{
54   */
55 
56 /** @addtogroup SYSCFG_BootMode Boot Mode
57   * @{
58   */
59 #define SYSCFG_BOOT_NORMAL         0U       /*!< Normal boot */
60 #define SYSCFG_BOOT_UART           1U       /*!< Boot with UART loop for DFU */
61 /**
62   * @} SYSCFG_BootMode
63   */
64 
65 /** @defgroup SYSCFG_SWD SWD connect interface
66     @ingroup SYSCFG_Exported_Constants
67   * @{
68   */
69 #define SYSCFG_SWD_HCPU             0       /*!< SWD interface connect to HCPU */
70 #define SYSCFG_SWD_LCPU             1       /*!< SWD interface connect to LCPU */
71 #define SYSCFG_SWD_BCPU             2       /*!< SWD interface connect to BCPU */
72 
73 /**
74   * @} SYSCFG_SWD
75   */
76 
77 /**
78   * @} SYSCFG_Exported_Constants
79   */
80 
81 /** @defgroup SYSCFG_Exported_Macros SYSCFG Exported Macros
82     @ingroup HAL_SYS_CFG
83   * @{
84   */
85 
86 /** @brief  Get Current boot mode.
87   * @retval The boot mode as configured by user
88   *           @arg @ref SYSCFG_BOOT_NORMAL
89   *           @arg @ref SYSCFG_BOOT_UART
90   */
91 #define __HAL_SYSCFG_GET_BOOT_MODE()        (hwp_hpsys_cfg->BMR&HPSYS_CFG_BMR_BOOT_MODE_Msk)
92 
93 /** @brief  Set SWD interface.
94   * @param  COREID core id for SWD
95   *           @arg @ref SYSCFG_SWD_HCPU
96   *           @arg @ref SYSCFG_SWD_LCPU
97   *           @arg @ref SYSCFG_SWD_BCPU
98   */
99 #define __HAL_SYSCFG_SET_SWD(COREID)        (hwp_lpsys_rcc->SWCR=COREID)
100 
101 /** @brief  Set Security Key control.
102   */
103 #define __HAL_SYSCFG_SET_SECURITY()      (hwp_hpsys_cfg->SCR|=HPSYS_CFG_SCR_FKEY_MODE)
104 
105 /** @brief  Clear Security Key control.
106   */
107 
108 #define __HAL_SYSCFG_CLEAR_SECURITY()      (hwp_hpsys_cfg->SCR&=~HPSYS_CFG_SCR_FKEY_MODE)
109 
110 #define __HAL_SYSCFG_GET_SID()            (hwp_hpsys_cfg->IDR>>HPSYS_CFG_IDR_SID_Pos)           /*!< Get serial ID*/
111 #define __HAL_SYSCFG_GET_CID()            ((hwp_hpsys_cfg->IDR>>HPSYS_CFG_IDR_CID_Pos)&0xff)    /*!< Get Chip ID*/
112 #define __HAL_SYSCFG_GET_PID()            ((hwp_hpsys_cfg->IDR>>HPSYS_CFG_IDR_PID_Pos)&0xff)    /*!< Get Package ID*/
113 #ifndef SF32LB55X
114 #define __HAL_SYSCFG_GET_REVID()          (hwp_hpsys_cfg->IDR&0xff)                             /*!< Get Revision ID*/
115 #else
116 #define __HAL_SYSCFG_GET_REVID()          ((hwp_hpsys_cfg->USBCR>>HPSYS_CFG_USBCR_RSVD1_Pos)&0xff) /*!< Get Revision ID*/
117 #endif /* SF32LB55X */
118 
119 #ifdef SF32LB52X
120 #define __HAL_SYSCFG_CHECK_REVID()      HAL_ASSERT((__HAL_SYSCFG_GET_REVID() <= HAL_CHIP_REV_ID_A3) || \
121                                                    (__HAL_SYSCFG_GET_REVID() == HAL_CHIP_REV_ID_A4) || \
122                                                    (__HAL_SYSCFG_GET_REVID() == HAL_CHIP_REV_ID_B4))
123 #else
124 #define __HAL_SYSCFG_CHECK_REVID()
125 #endif /* SF32LB52X */
126 
127 #define __HAL_SYSCFG_Enable_USB()         (hwp_hpsys_cfg->USBCR|=HPSYS_CFG_USBCR_USB_EN)        /*!< Enable USB module*/
128 #define __HAL_SYSCFG_USB_DM_PD()         (hwp_hpsys_cfg->USBCR|=HPSYS_CFG_USBCR_DM_PD)        /*!< Pull Down USB DM pin, host only*/
129 
130 
131 #ifdef SF32LB55X
132 #define __HAL_SYSCFG_Enable_WDT_REBOOT(enable)
133 #else
134 #ifdef SOC_BF0_HCPU
135 #define __HAL_SYSCFG_Enable_WDT_REBOOT(enable)      \
136     {\
137         if (enable) \
138             (hwp_hpsys_cfg->SYSCR|=HPSYS_CFG_SYSCR_WDT1_REBOOT);       /*!< Enable Watchdog to reboot whole system*/ \
139         else  \
140             (hwp_hpsys_cfg->SYSCR&=~HPSYS_CFG_SYSCR_WDT1_REBOOT);      /*!< Watchdog only reboot HCPU subsystem*/  \
141     }
142 #else
143 #define __HAL_SYSCFG_Enable_WDT_REBOOT(enable)      \
144     {\
145         if (enable) \
146             (hwp_lpsys_cfg->SYSCR|=LPSYS_CFG_SYSCR_WDT2_REBOOT);       /*!< Enable Watchdog to reboot whole system*/ \
147         else  \
148             (hwp_lpsys_cfg->SYSCR&=~LPSYS_CFG_SYSCR_WDT2_REBOOT);      /*!< Watchdog only reboot LCPU subsystem*/  \
149     }
150 #endif
151 #endif
152 
153 #ifndef SF32LB55X
154 #ifdef SOC_BF0_HCPU
155 #define __HAL_SYSCFG_Enable_Assert_Trigger(enable)    \
156         { \
157             if (enable) \
158                 (hwp_hpsys_cfg->DBGR |= HPSYS_CFG_DBGR_LP2HP_NMIE);   /*!< Enable to receive LCPU trigger assert notify*/ \
159             else \
160                 (hwp_hpsys_cfg->DBGR &= ~HPSYS_CFG_DBGR_LP2HP_NMIE);  /*!< Not receive LCPU trigger assert notify*/  \
161         }
162 
163 #define __HAL_SYSCFG_Trigger_Assert()               (hwp_hpsys_cfg->DBGR |= HPSYS_CFG_DBGR_HP2LP_NMI)
164 #define __HAL_SYSCFG_Get_Trigger_Assert_Flag()      ((hwp_hpsys_cfg->DBGR & HPSYS_CFG_DBGR_LP2HP_NMIF) >> HPSYS_CFG_DBGR_LP2HP_NMIF_Pos)
165 #else // SOC_BF0_HCPU
166 #define __HAL_SYSCFG_Enable_Assert_Trigger(enable)    \
167         { \
168             if (enable) \
169                 (hwp_lpsys_cfg->DBGR |= LPSYS_CFG_DBGR_HP2LP_NMIE);   /*!< Enable to receive HCPU trigger assert notify*/ \
170             else \
171                 (hwp_lpsys_cfg->DBGR &= ~LPSYS_CFG_DBGR_HP2LP_NMIE);  /*!< Not receive HCPU trigger assert notify*/  \
172         }
173 
174 #define __HAL_SYSCFG_Trigger_Assert()               (hwp_lpsys_cfg->DBGR |= LPSYS_CFG_DBGR_LP2HP_NMI)
175 #define __HAL_SYSCFG_Get_Trigger_Assert_Flag()      ((hwp_lpsys_cfg->DBGR & LPSYS_CFG_DBGR_HP2LP_NMIF) >> LPSYS_CFG_DBGR_HP2LP_NMIF_Pos)
176 #endif // SOC_BF0_HCPU
177 #endif // SF32LB55X
178 
179 #ifndef  SF32LB55X
180 #define __HAL_SYSCFG_HPBG_EN()                (hwp_hpsys_cfg->CAU2_CR |= HPSYS_CFG_CAU2_CR_HPBG_EN)
181 #define __HAL_SYSCFG_HPBG_VDDPSW_EN()        (hwp_hpsys_cfg->CAU2_CR |= HPSYS_CFG_CAU2_CR_HPBG_VDDPSW_EN)
182 #endif  //#ifdef SF32LB55X
183 
184 #define SystemFixClock  48000000
185 
186 #ifdef ROM_ENABLED
187 #define __HAL_ROM_USED __weak
188 #else
189 #define __HAL_ROM_USED
190 #endif
191 
192 
193 #ifndef HAL_ALIGN
194 #define HAL_ALIGN(size, align)           (((size) + (align) - 1) & ~((align) - 1))
195 #endif /* HAL_ALIGN */
196 #ifndef HAL_ALIGN_DOWN
197 #define HAL_ALIGN_DOWN(size, align)      ((size) & ~((align) - 1))
198 #endif /* HAL_ALIGN_DOWN */
199 
200 #ifndef HAL_MIN
201 #define HAL_MIN(a, b) ((a) < (b) ? (a) : (b))
202 #endif /* HAL_MIN */
203 
204 #ifndef HAL_MAX
205 #define HAL_MAX(a, b) ((a) > (b) ? (a) : (b))
206 #endif /* HAL_MAX */
207 
208 #ifndef HAL_ABS
209 #define HAL_ABS(x) ((x) > 0 ? (x) : (-(x)))
210 #endif /* HAL_ABS */
211 
212 /** @brief  CHIP ID for A3.
213   */
214 #ifdef SF32LB55X
215 #define HAL_CHIP_REV_ID_A3 0x80
216 #elif defined(SF32LB52X)
217 #define HAL_CHIP_REV_ID_A3 0x03
218 /* actually it's the B3 revision id */
219 #define HAL_CHIP_REV_ID_A4 0x07
220 #define HAL_CHIP_REV_ID_B4 0x0F
221 #else
222 #define HAL_CHIP_REV_ID_A3 0xFF // Not defined
223 #define HAL_CHIP_REV_ID_A4 0xFF
224 #endif /* SF32LB55X */
225 
226 
227 #ifdef SF32LB52X
228 #ifdef SOC_BF0_HCPU
229 #define HAL_LXT_ENABLED() HAL_RTC_LXT_ENABLED()
230 #define HAL_LXT_DISABLED() (!HAL_RTC_LXT_ENABLED())
231 #else
232 #define HAL_LXT_ENABLED() HAL_GetLXTEnabled()
233 #define HAL_LXT_DISABLED() (!HAL_GetLXTEnabled())
234 #endif
235 #else
236 #define HAL_LXT_ENABLED() HAL_PMU_LXT_ENABLED()
237 #define HAL_LXT_DISABLED() HAL_PMU_LXT_DISABLED()
238 #endif
239 
240 
241 /**
242   * @} SYSCFG_Exported_Macros
243   */
244 
245 /**
246   * @} HAL_SYS_CFG
247   */
248 
249 /* Exported functions --------------------------------------------------------*/
250 
251 /** @defgroup HAL_Exported_Functions HAL Exported Functions
252   * @ingroup HAL_COMMON
253   * @{
254   */
255 
256 /** @addtogroup HAL_Exported_Functions_Group1
257   * @ingroup HAL_Exported_Functions
258   * @{
259   */
260 
261 /* Initialization and de-initialization functions  ******************************/
262 /**
263   * @brief  Configure the Flash prefetch, the Instruction and Data caches,
264   *         the time base source, NVIC and any required global low level hardware
265   *         by calling the HAL_PreInit() callback function to be optionally defined in user file
266   *
267   * @note   HAL_Init() function is called at the beginning of program after reset and before
268   *         the clock configuration.
269   *
270   * @note   In the default implementation the System Timer (Systick) is used as source of time base.
271   *         The Systick configuration is based on MSI clock, as MSI is the clock
272   *         used after a system Reset and the NVIC configuration is set to Priority group 4.
273   *         Once done, time base tick starts incrementing: the tick variable counter is incremented
274   *         each 1ms in the SysTick_Handler() interrupt handler.
275   *
276   * @retval HAL status
277   */
278 HAL_StatusTypeDef HAL_Init(void);
279 
280 /**
281   * @brief  Before HAL_Init is called, board specific initialization.
282   * @retval None
283   */
284 void HAL_PreInit(void);
285 
286 /**
287   * @brief  It's called after HAL_PreInit in HAL_Init
288   * @retval None
289   */
290 void HAL_PostMspInit(void);
291 
292 
293 /**
294   * @brief De-initialize common part of the HAL and stop the source of time base.
295   * @note This function is optional.
296   * @retval HAL status
297   */
298 HAL_StatusTypeDef HAL_DeInit(void);
299 
300 /**
301   * @brief  DeInitialize the MSP.
302   * @retval None
303   */
304 void HAL_MspDeInit(void);
305 
306 /**
307   * @brief This function configures the source of the time base:
308   *        The time source is configured to have 1ms time base with a dedicated
309   *        Tick interrupt priority.
310   * @note This function is called  automatically at the beginning of program after
311   *       reset by HAL_Init() or at any time when clock is reconfigured  by HAL_RCC_ClockConfig().
312   * @note In the default implementation, SysTick timer is the source of time base.
313   *       It is used to generate interrupts at regular time intervals.
314   *       Care must be taken if HAL_Delay() is called from a peripheral ISR process,
315   *       The SysTick interrupt must have higher priority (numerically lower)
316   *       than the peripheral interrupt. Otherwise the caller ISR process will be blocked.
317   *       The function is declared as __weak  to be overwritten  in case of other
318   *       implementation  in user file.
319   * @param TickPriority  Tick interrupt priority.
320   * @retval HAL status
321   */
322 HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority);
323 
324 /**
325   * @} HAL_Exported_Functions_Group1
326   */
327 
328 /** @addtogroup HAL_Exported_Functions_Group2 HAL Control functions
329   * @ingroup HAL_Exported_Functions
330   * @{
331   */
332 
333 /* Peripheral Control functions  ************************************************/
334 /**
335   * @brief This function is called to increment a global variable "uwTick"
336   *        used as application time base.
337   * @note In the default implementation, this variable is incremented each 1ms
338   *       in SysTick ISR.
339  * @note This function is declared as __weak to be overwritten in case of other
340   *      implementations in user file.
341   * @retval None
342   */
343 void HAL_IncTick(void);
344 
345 /**
346   * @brief This function provides minimum delay (in milliseconds) based
347   *        on variable incremented.
348   * @note In the default implementation , SysTick timer is the source of time base.
349   *       It is used to generate interrupts at regular time intervals where uwTick
350   *       is incremented.
351   * @note This function is declared as __weak to be overwritten in case of other
352   *       implementations in user file.
353   * @param Delay  specifies the delay time length, in milliseconds.
354   * @retval None
355   */
356 void HAL_Delay(uint32_t Delay);
357 void HAL_Delay_us(uint32_t us);
358 void HAL_Delay_us_(__IO uint32_t us);
359 
360 /**
361   * @brief Provide a tick value in millisecond.
362   * @note This function is declared as __weak to be overwritten in case of other
363   *       implementations in user file.
364   * @retval tick value
365   */
366 uint32_t HAL_GetTick(void);
367 
368 /**
369   * @brief Suspend Tick increment.
370   * @note In the default implementation , SysTick timer is the source of time base. It is
371   *       used to generate interrupts at regular time intervals. Once HAL_SuspendTick()
372   *       is called, the SysTick interrupt will be disabled and so Tick increment
373   *       is suspended.
374   * @note This function is declared as __weak to be overwritten in case of other
375   *       implementations in user file.
376   * @retval None
377   */
378 void HAL_SuspendTick(void);
379 
380 /**
381   * @brief Resume Tick increment.
382   * @note In the default implementation , SysTick timer is the source of time base. It is
383   *       used to generate interrupts at regular time intervals. Once HAL_ResumeTick()
384   *       is called, the SysTick interrupt will be enabled and so Tick increment
385   *       is resumed.
386   * @note This function is declared as __weak to be overwritten in case of other
387   *       implementations in user file.
388   * @retval None
389   */
390 void HAL_ResumeTick(void);
391 
392 /**
393   * @} HAL_Exported_Functions_Group2
394   */
395 
396 /**
397   * @} HAL_Exported_Functions
398   */
399 
400 /** @defgroup HAL_PORT Hal porting functions
401  *  @ingroup HAL_COMMON
402  *  @{
403  *
404 */
405 
406 #if defined(USE_FULL_ASSERT)||defined(_SIFLI_DOXYGEN_)
407 #define HAL_ASSERT(expr) ((expr) ? (void)0U : HAL_AssertFailed((char *)__FILE__, __LINE__))
408 
409 /* Exported functions ------------------------------------------------------- */
410 /**
411   * @brief  The assert failure printing.
412     If BSP_USING_FULL_ASSERT is defined, the asser_failed function need to be implemented.
413   * @param  file file name of source code for the failed assert
414   * @param  line code line number for the failed assert
415   * @retval None
416   */
417 void HAL_AssertFailed(char *file, uint32_t line);
418 #elif defined(USE_LOOP_ASSERT)
419 #define HAL_ASSERT(expr) if ((expr)==0) while (1) {;}
420 #else
421 #define HAL_ASSERT(expr) if ((expr)==0) while (1) {;}
422 #endif /* USE_FULL_ASSERT */
423 
424 /**
425   * @brief  Debug function to stop current thread execution.
426   * @retval None
427   */
428 #if defined(__CLANG_ARM) || defined(__GNUC__)
HAL_sw_breakpoint(void)429 __STATIC_INLINE void HAL_sw_breakpoint(void)
430 {
431     __ASM
432     (
433         "B . \n"
434     );
435 }
436 #else
HAL_sw_breakpoint(void)437 __STATIC_INLINE void HAL_sw_breakpoint(void)
438 {
439     volatile uint32_t dummy = 0;
440     while (0 == dummy);
441 }
442 #endif
443 
HAL_DisableInterrupt(void)444 __STATIC_INLINE uint32_t HAL_DisableInterrupt(void)
445 {
446     uint32_t mask;
447 
448     mask = __get_PRIMASK();
449     __set_PRIMASK(1);
450     return mask;
451 }
452 
HAL_EnableInterrupt(uint32_t mask)453 __STATIC_INLINE void HAL_EnableInterrupt(uint32_t mask)
454 {
455     __set_PRIMASK(mask);
456 }
457 
458 uint32_t HAL_GetLXTEnabled(void);
459 
460 
HAL_GetElapsedTick(uint32_t prev_tick,uint32_t curr_tick)461 __STATIC_INLINE uint32_t HAL_GetElapsedTick(uint32_t prev_tick, uint32_t curr_tick)
462 {
463     uint32_t elapsed_tick;
464 
465     if (curr_tick >= prev_tick)
466     {
467         elapsed_tick = curr_tick - prev_tick;
468     }
469     else
470     {
471         elapsed_tick = (UINT32_MAX - prev_tick) + 1 + curr_tick;
472     }
473 
474     return elapsed_tick;
475 }
476 
477 
478 /**
479   * @} HAL_PORT
480   */
481 
482 /**
483   * @} HAL_COMMON
484   */
485 
486 /**
487   * @} BF0_HAL_Driver
488   */
489 
490 #ifdef BSP_USING_NO_OS
491 #if defined(__CC_ARM) || defined(__CLANG_ARM)
492 #elif defined(__ICCARM__)
493 #define main __low_level_init
494 #elif defined(__GNUC__)
495 #define main entry
496 #endif
497 #else
498 #ifdef WIN32
499 #define main app_main
500 #endif
501 #endif
502 
503 /// @} file
504 
505 #ifdef __cplusplus
506 }
507 #endif
508 
509 #endif /* __BF0_CP_HAL_H */