stm32h5xx_hal.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329
  1. /**
  2. **********************************************************************************************************************
  3. * @file stm32h5xx_hal.c
  4. * @author MCD Application Team
  5. * @brief HAL module driver.
  6. * This is the common part of the HAL initialization
  7. *
  8. @verbatim
  9. ======================================================================================================================
  10. ##### How to use this driver #####
  11. ======================================================================================================================
  12. [..]
  13. The common HAL driver contains a set of generic and common APIs that can be
  14. used by the PPP peripheral drivers and the user to start using the HAL.
  15. [..]
  16. The HAL contains two APIs' categories:
  17. (+) Common HAL APIs
  18. (+) Services HAL APIs
  19. @endverbatim
  20. **********************************************************************************************************************
  21. * @attention
  22. *
  23. * Copyright (c) 2023 STMicroelectronics.
  24. * All rights reserved.
  25. *
  26. * This software is licensed under terms that can be found in the LICENSE file
  27. * in the root directory of this software component.
  28. * If no LICENSE file comes with this software, it is provided AS-IS.
  29. *
  30. **********************************************************************************************************************
  31. */
  32. /* Includes ----------------------------------------------------------------------------------------------------------*/
  33. #include "stm32h5xx_hal.h"
  34. /** @addtogroup STM32H5xx_HAL_Driver
  35. * @{
  36. */
  37. /** @defgroup HAL HAL
  38. * @brief HAL module driver
  39. * @{
  40. */
  41. #ifdef HAL_MODULE_ENABLED
  42. /* Private typedef ---------------------------------------------------------------------------------------------------*/
  43. /* Private define ----------------------------------------------------------------------------------------------------*/
  44. /**
  45. * @brief STM32H5xx HAL Driver version number 1.5.0.RC1
  46. */
  47. #define __STM32H5XX_HAL_VERSION_MAIN (0x01U) /*!< [31:24] main version */
  48. #define __STM32H5XX_HAL_VERSION_SUB1 (0x05U) /*!< [23:16] sub1 version */
  49. #define __STM32H5XX_HAL_VERSION_SUB2 (0x00U) /*!< [15:8] sub2 version */
  50. #define __STM32H5XX_HAL_VERSION_RC (0x00U) /*!< [7:0] release candidate */
  51. #define __STM32H5XX_HAL_VERSION ((__STM32H5XX_HAL_VERSION_MAIN << 24U)\
  52. |(__STM32H5XX_HAL_VERSION_SUB1 << 16U)\
  53. |(__STM32H5XX_HAL_VERSION_SUB2 << 8U )\
  54. |(__STM32H5XX_HAL_VERSION_RC))
  55. #if defined(VREFBUF)
  56. #define VREFBUF_TIMEOUT_VALUE 10U /* 10 ms */
  57. #endif /* VREFBUF */
  58. /* Value used to increment hide protection level */
  59. #define SBS_HDPL_INCREMENT_VALUE (uint8_t)0x6A
  60. /* Value used to lock/unlock debug functionalities */
  61. #define SBS_DEBUG_LOCK_VALUE (uint8_t)0xC3
  62. #define SBS_DEBUG_UNLOCK_VALUE (uint8_t)0xB4
  63. /* Private macro -----------------------------------------------------------------------------------------------------*/
  64. /* Private variables -------------------------------------------------------------------------------------------------*/
  65. /* Exported variables ------------------------------------------------------------------------------------------------*/
  66. /** @defgroup HAL_Exported_Variables HAL Exported Variables
  67. * @{
  68. */
  69. __IO uint32_t uwTick;
  70. uint32_t uwTickPrio = (1UL << __NVIC_PRIO_BITS); /* Invalid PRIO */
  71. HAL_TickFreqTypeDef uwTickFreq = HAL_TICK_FREQ_DEFAULT; /* 1KHz */
  72. /**
  73. * @}
  74. */
  75. /* Private function prototypes ---------------------------------------------------------------------------------------*/
  76. /* Exported functions ------------------------------------------------------------------------------------------------*/
  77. /** @defgroup HAL_Exported_Functions HAL Exported Functions
  78. * @{
  79. */
  80. /** @defgroup HAL_Exported_Functions_Group1 Initialization and de-initialization Functions
  81. * @brief Initialization and de-initialization functions
  82. *
  83. @verbatim
  84. =======================================================================================================================
  85. ##### Initialization and de-initialization functions #####
  86. =======================================================================================================================
  87. [..] This section provides functions allowing to:
  88. (+) Initializes the Flash interface the NVIC allocation and initial clock
  89. configuration. It initializes the systick also when timeout is needed
  90. and the backup domain when enabled.
  91. (+) De-Initializes common part of the HAL.
  92. (+) Configure The time base source to have 1ms time base with a dedicated
  93. Tick interrupt priority.
  94. (++) SysTick timer is used by default as source of time base, but user
  95. can eventually implement his proper time base source (a general purpose
  96. timer for example or other time source), keeping in mind that Time base
  97. duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and
  98. handled in milliseconds basis.
  99. (++) Time base configuration function (HAL_InitTick ()) is called automatically
  100. at the beginning of the program after reset by HAL_Init() or at any time
  101. when clock is configured, by HAL_RCC_ClockConfig().
  102. (++) Source of time base is configured to generate interrupts at regular
  103. time intervals. Care must be taken if HAL_Delay() is called from a
  104. peripheral ISR process, the Tick interrupt line must have higher priority
  105. (numerically lower) than the peripheral interrupt. Otherwise the caller
  106. ISR process will be blocked.
  107. (++) functions affecting time base configurations are declared as __weak
  108. to make override possible in case of other implementations in user file.
  109. @endverbatim
  110. * @{
  111. */
  112. /**
  113. * @brief Configure the Flash prefetch, the time base source, NVIC and any required global low
  114. * level hardware by calling the HAL_MspInit() callback function to be optionally defined
  115. * in user file stm32h5xx_hal_msp.c.
  116. *
  117. * @note HAL_Init() function is called at the beginning of program after reset and before
  118. * the clock configuration.
  119. *
  120. * @note In the default implementation the System Timer (SysTick) is used as source of time base.
  121. * The SysTick configuration is based on HSI clock, as HSI is the clock
  122. * used after a system Reset and the NVIC configuration is set to Priority group 4.
  123. * Once done, time base tick starts incrementing: the tick variable counter is incremented
  124. * each 1ms in the SysTick_Handler() interrupt handler.
  125. *
  126. * @retval HAL status
  127. */
  128. HAL_StatusTypeDef HAL_Init(void)
  129. {
  130. /* Configure Flash prefetch */
  131. #if (PREFETCH_ENABLE != 0U)
  132. __HAL_FLASH_PREFETCH_BUFFER_ENABLE();
  133. #endif /* PREFETCH_ENABLE */
  134. /* Set Interrupt Group Priority */
  135. HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);
  136. /* Update the SystemCoreClock global variable */
  137. SystemCoreClock = HAL_RCC_GetSysClockFreq() >> AHBPrescTable[(RCC->CFGR2 & RCC_CFGR2_HPRE) >> RCC_CFGR2_HPRE_Pos];
  138. /* Select HCLK as SysTick clock source */
  139. HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  140. /* Use systick as time base source and configure 1ms tick (default clock after Reset is HSI) */
  141. if (HAL_InitTick(TICK_INT_PRIORITY) != HAL_OK)
  142. {
  143. return HAL_ERROR;
  144. }
  145. /* Init the low level hardware */
  146. HAL_MspInit();
  147. /* Return function status */
  148. return HAL_OK;
  149. }
  150. /**
  151. * @brief This function de-Initializes common part of the HAL and stops the systick.
  152. * This function is optional.
  153. * @retval HAL status
  154. */
  155. HAL_StatusTypeDef HAL_DeInit(void)
  156. {
  157. /* Reset of all peripherals */
  158. __HAL_RCC_APB1_FORCE_RESET();
  159. __HAL_RCC_APB1_RELEASE_RESET();
  160. __HAL_RCC_APB2_FORCE_RESET();
  161. __HAL_RCC_APB2_RELEASE_RESET();
  162. __HAL_RCC_APB3_FORCE_RESET();
  163. __HAL_RCC_APB3_RELEASE_RESET();
  164. __HAL_RCC_AHB1_FORCE_RESET();
  165. __HAL_RCC_AHB1_RELEASE_RESET();
  166. __HAL_RCC_AHB2_FORCE_RESET();
  167. __HAL_RCC_AHB2_RELEASE_RESET();
  168. #if defined(AHB4PERIPH_BASE)
  169. __HAL_RCC_AHB4_FORCE_RESET();
  170. __HAL_RCC_AHB4_RELEASE_RESET();
  171. #endif /* AHB4PERIPH_BASE */
  172. /* De-Init the low level hardware */
  173. HAL_MspDeInit();
  174. /* Return function status */
  175. return HAL_OK;
  176. }
  177. /**
  178. * @brief Initializes the MSP.
  179. * @retval None
  180. */
  181. __weak void HAL_MspInit(void)
  182. {
  183. /* NOTE : This function Should not be modified, when the callback is needed,
  184. the HAL_MspInit could be implemented in the user file
  185. */
  186. }
  187. /**
  188. * @brief DeInitializes the MSP.
  189. * @retval None
  190. */
  191. __weak void HAL_MspDeInit(void)
  192. {
  193. /* NOTE : This function Should not be modified, when the callback is needed,
  194. the HAL_MspDeInit could be implemented in the user file
  195. */
  196. }
  197. /**
  198. * @brief This function configures the source of the time base.
  199. * The time source is configured to have 1ms time base with a dedicated
  200. * Tick interrupt priority.
  201. * @note This function is called automatically at the beginning of program after
  202. * reset by HAL_Init() or at any time when clock is reconfigured by HAL_RCC_ClockConfig().
  203. * @note In the default implementation, SysTick timer is the source of time base.
  204. * It is used to generate interrupts at regular time intervals.
  205. * Care must be taken if HAL_Delay() is called from a peripheral ISR process,
  206. * The SysTick interrupt must have higher priority (numerically lower)
  207. * than the peripheral interrupt. Otherwise the caller ISR process will be blocked.
  208. * The function is declared as __weak to be overwritten in case of other
  209. * implementation in user file.
  210. * @param TickPriority: Tick interrupt priority.
  211. * @retval HAL status
  212. */
  213. __weak HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
  214. {
  215. uint32_t ticknumber = 0U;
  216. uint32_t systicksel;
  217. /* Check uwTickFreq for MisraC 2012 (even if uwTickFreq is a enum type that don't take the value zero)*/
  218. if ((uint32_t)uwTickFreq == 0UL)
  219. {
  220. return HAL_ERROR;
  221. }
  222. /* Check Clock source to calculate the tickNumber */
  223. if (READ_BIT(SysTick->CTRL, SysTick_CTRL_CLKSOURCE_Msk) == SysTick_CTRL_CLKSOURCE_Msk)
  224. {
  225. /* HCLK selected as SysTick clock source */
  226. ticknumber = SystemCoreClock / (1000UL / (uint32_t)uwTickFreq);
  227. }
  228. else
  229. {
  230. systicksel = HAL_SYSTICK_GetCLKSourceConfig();
  231. switch (systicksel)
  232. {
  233. /* HCLK_DIV8 selected as SysTick clock source */
  234. case SYSTICK_CLKSOURCE_HCLK_DIV8:
  235. /* Calculate tick value */
  236. ticknumber = (SystemCoreClock / (8000UL / (uint32_t)uwTickFreq));
  237. break;
  238. /* LSI selected as SysTick clock source */
  239. case SYSTICK_CLKSOURCE_LSI:
  240. /* Calculate tick value */
  241. ticknumber = (LSI_VALUE / (1000UL / (uint32_t)uwTickFreq));
  242. break;
  243. /* LSE selected as SysTick clock source */
  244. case SYSTICK_CLKSOURCE_LSE:
  245. /* Calculate tick value */
  246. ticknumber = (LSE_VALUE / (1000UL / (uint32_t)uwTickFreq));
  247. break;
  248. default:
  249. /* Nothing to do */
  250. break;
  251. }
  252. }
  253. /* Configure the SysTick to have interrupt in 1ms time basis*/
  254. if (HAL_SYSTICK_Config(ticknumber) > 0U)
  255. {
  256. return HAL_ERROR;
  257. }
  258. /* Configure the SysTick IRQ priority */
  259. HAL_NVIC_SetPriority(SysTick_IRQn, TickPriority, 0U);
  260. uwTickPrio = TickPriority;
  261. /* Return function status */
  262. return HAL_OK;
  263. }
  264. /**
  265. * @}
  266. */
  267. /** @defgroup HAL_Group2 HAL Control functions
  268. * @brief HAL Control functions
  269. *
  270. @verbatim
  271. =======================================================================================================================
  272. ##### HAL Control functions #####
  273. =======================================================================================================================
  274. [..] This section provides functions allowing to:
  275. (+) Provide a tick value in millisecond
  276. (+) Provide a blocking delay in millisecond
  277. (+) Suspend the time base source interrupt
  278. (+) Resume the time base source interrupt
  279. (+) Get the HAL API driver version
  280. (+) Get the device identifier
  281. (+) Get the device revision identifier
  282. @endverbatim
  283. * @{
  284. */
  285. /**
  286. * @brief This function is called to increment a global variable "uwTick"
  287. * used as application time base.
  288. * @note In the default implementation, this variable is incremented each 1ms
  289. * in SysTick ISR.
  290. * @note This function is declared as __weak to be overwritten in case of other
  291. * implementations in user file.
  292. * @retval None
  293. */
  294. __weak void HAL_IncTick(void)
  295. {
  296. uwTick += (uint32_t)uwTickFreq;
  297. }
  298. /**
  299. * @brief Provides a tick value in millisecond.
  300. * @note This function is declared as __weak to be overwritten in case of other
  301. * implementations in user file.
  302. * @retval tick value
  303. */
  304. __weak uint32_t HAL_GetTick(void)
  305. {
  306. return uwTick;
  307. }
  308. /**
  309. * @brief This function returns a tick priority.
  310. * @retval tick priority
  311. */
  312. uint32_t HAL_GetTickPrio(void)
  313. {
  314. return uwTickPrio;
  315. }
  316. /**
  317. * @brief Set new tick Freq.
  318. * @retval HAL status
  319. */
  320. HAL_StatusTypeDef HAL_SetTickFreq(HAL_TickFreqTypeDef Freq)
  321. {
  322. HAL_StatusTypeDef status = HAL_OK;
  323. HAL_TickFreqTypeDef prevTickFreq;
  324. assert_param(IS_TICKFREQ(Freq));
  325. if (uwTickFreq != Freq)
  326. {
  327. /* Back up uwTickFreq frequency */
  328. prevTickFreq = uwTickFreq;
  329. /* Update uwTickFreq global variable used by HAL_InitTick() */
  330. uwTickFreq = Freq;
  331. /* Apply the new tick Freq */
  332. status = HAL_InitTick(uwTickPrio);
  333. if (status != HAL_OK)
  334. {
  335. /* Restore previous tick frequency */
  336. uwTickFreq = prevTickFreq;
  337. }
  338. }
  339. return status;
  340. }
  341. /**
  342. * @brief Return tick frequency.
  343. * @retval Tick frequency.
  344. * Value of @ref HAL_TickFreqTypeDef.
  345. */
  346. HAL_TickFreqTypeDef HAL_GetTickFreq(void)
  347. {
  348. return uwTickFreq;
  349. }
  350. /**
  351. * @brief This function provides minimum delay (in milliseconds) based
  352. * on variable incremented.
  353. * @note In the default implementation , SysTick timer is the source of time base.
  354. * It is used to generate interrupts at regular time intervals where uwTick
  355. * is incremented.
  356. * @note This function is declared as __weak to be overwritten in case of other
  357. * implementations in user file.
  358. * @param Delay specifies the delay time length, in milliseconds.
  359. * @retval None
  360. */
  361. __weak void HAL_Delay(uint32_t Delay)
  362. {
  363. uint32_t tickstart = HAL_GetTick();
  364. uint32_t wait = Delay;
  365. /* Add a freq to guarantee minimum wait */
  366. if (wait < HAL_MAX_DELAY)
  367. {
  368. wait += (uint32_t)(uwTickFreq);
  369. }
  370. while ((HAL_GetTick() - tickstart) < wait)
  371. {
  372. }
  373. }
  374. /**
  375. * @brief Suspend Tick increment.
  376. * @note In the default implementation , SysTick timer is the source of time base. It is
  377. * used to generate interrupts at regular time intervals. Once HAL_SuspendTick()
  378. * is called, the SysTick interrupt will be disabled and so Tick increment
  379. * is suspended.
  380. * @note This function is declared as __weak to be overwritten in case of other
  381. * implementations in user file.
  382. * @retval None
  383. */
  384. __weak void HAL_SuspendTick(void)
  385. {
  386. /* Disable SysTick Interrupt */
  387. SysTick->CTRL &= ~SysTick_CTRL_TICKINT_Msk;
  388. }
  389. /**
  390. * @brief Resume Tick increment.
  391. * @note In the default implementation , SysTick timer is the source of time base. It is
  392. * used to generate interrupts at regular time intervals. Once HAL_ResumeTick()
  393. * is called, the SysTick interrupt will be enabled and so Tick increment
  394. * is resumed.
  395. * @note This function is declared as __weak to be overwritten in case of other
  396. * implementations in user file.
  397. * @retval None
  398. */
  399. __weak void HAL_ResumeTick(void)
  400. {
  401. /* Enable SysTick Interrupt */
  402. SysTick->CTRL |= SysTick_CTRL_TICKINT_Msk;
  403. }
  404. /**
  405. * @brief Returns the HAL revision
  406. * @retval version : 0xXYZR (8bits for each decimal, R for RC)
  407. */
  408. uint32_t HAL_GetHalVersion(void)
  409. {
  410. return __STM32H5XX_HAL_VERSION;
  411. }
  412. /**
  413. * @brief Returns the device revision identifier.
  414. * @retval Device revision identifier
  415. */
  416. uint32_t HAL_GetREVID(void)
  417. {
  418. return ((DBGMCU->IDCODE & DBGMCU_IDCODE_REV_ID) >> 16);
  419. }
  420. /**
  421. * @brief Returns the device identifier.
  422. * @retval Device identifier
  423. */
  424. uint32_t HAL_GetDEVID(void)
  425. {
  426. return (DBGMCU->IDCODE & DBGMCU_IDCODE_DEV_ID);
  427. }
  428. /**
  429. * @brief Return the first word of the unique device identifier (UID based on 96 bits)
  430. * @retval Device identifier
  431. */
  432. uint32_t HAL_GetUIDw0(void)
  433. {
  434. return (READ_REG(*((uint32_t *)UID_BASE)));
  435. }
  436. /**
  437. * @brief Return the second word of the unique device identifier (UID based on 96 bits)
  438. * @retval Device identifier
  439. */
  440. uint32_t HAL_GetUIDw1(void)
  441. {
  442. return (READ_REG(*((uint32_t *)(UID_BASE + 4U))));
  443. }
  444. /**
  445. * @brief Return the third word of the unique device identifier (UID based on 96 bits)
  446. * @retval Device identifier
  447. */
  448. uint32_t HAL_GetUIDw2(void)
  449. {
  450. return (READ_REG(*((uint32_t *)(UID_BASE + 8U))));
  451. }
  452. /**
  453. * @}
  454. */
  455. /** @defgroup HAL_Exported_Functions_Group3 HAL Debug functions
  456. * @brief HAL Debug functions
  457. *
  458. @verbatim
  459. =======================================================================================================================
  460. ##### HAL Debug functions #####
  461. =======================================================================================================================
  462. [..] This section provides functions allowing to:
  463. (+) Enable/Disable Debug module during STOP mode
  464. (+) Enable/Disable Debug module during STANDBY mode
  465. @endverbatim
  466. * @{
  467. */
  468. /**
  469. * @brief Enable the Debug Module during STOP mode.
  470. * @retval None
  471. */
  472. void HAL_DBGMCU_EnableDBGStopMode(void)
  473. {
  474. SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
  475. }
  476. /**
  477. * @brief Disable the Debug Module during STOP mode.
  478. * @retval None
  479. */
  480. void HAL_DBGMCU_DisableDBGStopMode(void)
  481. {
  482. CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
  483. }
  484. /**
  485. * @brief Enable the Debug Module during STANDBY mode.
  486. * @retval None
  487. */
  488. void HAL_DBGMCU_EnableDBGStandbyMode(void)
  489. {
  490. SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
  491. }
  492. /**
  493. * @brief Disable the Debug Module during STANDBY mode.
  494. * @retval None
  495. */
  496. void HAL_DBGMCU_DisableDBGStandbyMode(void)
  497. {
  498. CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
  499. }
  500. /**
  501. * @}
  502. */
  503. /** @defgroup HAL_Exported_Functions_Group4 HAL VREFBUF Control functions
  504. * @brief HAL VREFBUF Control functions
  505. *
  506. @verbatim
  507. =======================================================================================================================
  508. ##### HAL VREFBUF Control functions #####
  509. =======================================================================================================================
  510. [..] This section provides functions allowing to:
  511. (+) Configure the Voltage reference buffer
  512. (+) Enable/Disable the Voltage reference buffer
  513. @endverbatim
  514. * @{
  515. */
  516. #if defined(VREFBUF)
  517. /**
  518. * @brief Configure the internal voltage reference buffer voltage scale.
  519. * @param VoltageScaling: specifies the output voltage to achieve
  520. * This parameter can be one of the following values:
  521. * @arg VREFBUF_VOLTAGE_SCALE0: VREF_OUT1 around 2.5 V.
  522. * This requires VDDA equal to or higher than 2.8 V.
  523. * @arg VREFBUF_VOLTAGE_SCALE1: VREF_OUT2 around 2.048 V.
  524. * This requires VDDA equal to or higher than 2.4 V.
  525. * @arg VREFBUF_VOLTAGE_SCALE2: VREF_OUT3 around 1.8 V.
  526. * This requires VDDA equal to or higher than 2.1 V.
  527. * @arg VREFBUF_VOLTAGE_SCALE3: VREF_OUT4 around 1.5 V.
  528. * This requires VDDA equal to or higher than 1.8 V.
  529. * @retval None
  530. */
  531. void HAL_VREFBUF_VoltageScalingConfig(uint32_t VoltageScaling)
  532. {
  533. /* Check the parameters */
  534. assert_param(IS_VREFBUF_VOLTAGE_SCALE(VoltageScaling));
  535. MODIFY_REG(VREFBUF->CSR, VREFBUF_CSR_VRS, VoltageScaling);
  536. }
  537. /**
  538. * @brief Configure the internal voltage reference buffer high impedance mode.
  539. * @param Mode: specifies the high impedance mode
  540. * This parameter can be one of the following values:
  541. * @arg VREFBUF_HIGH_IMPEDANCE_DISABLE: VREF+ pin is internally connect to VREFINT output.
  542. * @arg VREFBUF_HIGH_IMPEDANCE_ENABLE: VREF+ pin is high impedance.
  543. * @retval None
  544. */
  545. void HAL_VREFBUF_HighImpedanceConfig(uint32_t Mode)
  546. {
  547. /* Check the parameters */
  548. assert_param(IS_VREFBUF_HIGH_IMPEDANCE(Mode));
  549. MODIFY_REG(VREFBUF->CSR, VREFBUF_CSR_HIZ, Mode);
  550. }
  551. /**
  552. * @brief Tune the Internal Voltage Reference buffer (VREFBUF).
  553. * @retval None
  554. */
  555. void HAL_VREFBUF_TrimmingConfig(uint32_t TrimmingValue)
  556. {
  557. /* Check the parameters */
  558. assert_param(IS_VREFBUF_TRIMMING(TrimmingValue));
  559. MODIFY_REG(VREFBUF->CCR, VREFBUF_CCR_TRIM, TrimmingValue);
  560. }
  561. /**
  562. * @brief Enable the Internal Voltage Reference buffer (VREFBUF).
  563. * @retval HAL_OK/HAL_TIMEOUT
  564. */
  565. HAL_StatusTypeDef HAL_EnableVREFBUF(void)
  566. {
  567. uint32_t tickstart;
  568. SET_BIT(VREFBUF->CSR, VREFBUF_CSR_ENVR);
  569. /* Get Start Tick*/
  570. tickstart = HAL_GetTick();
  571. /* Wait for VRR bit */
  572. while (READ_BIT(VREFBUF->CSR, VREFBUF_CSR_VRR) == 0UL)
  573. {
  574. if ((HAL_GetTick() - tickstart) > VREFBUF_TIMEOUT_VALUE)
  575. {
  576. return HAL_TIMEOUT;
  577. }
  578. }
  579. return HAL_OK;
  580. }
  581. /**
  582. * @brief Disable the Internal Voltage Reference buffer (VREFBUF).
  583. *
  584. * @retval None
  585. */
  586. void HAL_DisableVREFBUF(void)
  587. {
  588. CLEAR_BIT(VREFBUF->CSR, VREFBUF_CSR_ENVR);
  589. }
  590. #endif /* VREFBUF */
  591. /**
  592. * @}
  593. */
  594. /** @defgroup HAL_Exported_Functions_Group5 HAL SBS configuration functions
  595. * @brief HAL SBS configuration functions
  596. *
  597. @verbatim
  598. =======================================================================================================================
  599. ##### HAL SBS configuration functions #####
  600. =======================================================================================================================
  601. [..] This section provides functions allowing to:
  602. (+) Select the Ethernet PHY Interface
  603. (+) Enable/Disable the VDD I/Os Compensation Cell
  604. (+) Code selection/configuration for the VDD I/O Compensation cell
  605. (+) Get ready flag status of VDD I/Os Compensation cell
  606. (+) Get PMOS/NMOS compensation value of the I/Os supplied by VDD
  607. (+) Enable/Disable the NMI in case of double ECC error in FLASH Interface
  608. @endverbatim
  609. * @{
  610. */
  611. #if defined(SBS_PMCR_ETH_SEL_PHY)
  612. /**
  613. * @brief Ethernet PHY Interface Selection either MII or RMII
  614. * @param SBS_ETHInterface: Selects the Ethernet PHY interface
  615. * This parameter can be one of the following values:
  616. * @arg SBS_ETH_MII : Select the Media Independent Interface
  617. * @arg SBS_ETH_RMII: Select the Reduced Media Independent Interface
  618. * @retval None
  619. */
  620. void HAL_SBS_ETHInterfaceSelect(uint32_t SBS_ETHInterface)
  621. {
  622. /* Check the parameter */
  623. assert_param(IS_SBS_ETHERNET_CONFIG(SBS_ETHInterface));
  624. MODIFY_REG(SBS->PMCR, SBS_PMCR_ETH_SEL_PHY, (uint32_t)(SBS_ETHInterface));
  625. }
  626. #endif /* SBS_PMCR_ETH_SEL_PHY */
  627. /**
  628. * @brief Enables the VDD I/Os Compensation Cell.
  629. * @note The I/O compensation cell can be used only when the device supply
  630. * voltage ranges from 2.4 to 3.6 V.
  631. * @retval None
  632. */
  633. void HAL_SBS_EnableVddIO1CompensationCell(void)
  634. {
  635. SET_BIT(SBS->CCCSR, SBS_CCCSR_EN1) ;
  636. }
  637. /**
  638. * @brief Power-down the VDD I/Os Compensation Cell.
  639. * @note The I/O compensation cell can be used only when the device supply
  640. * voltage ranges from 2.4 to 3.6 V.
  641. * @retval None
  642. */
  643. void HAL_SBS_DisableVddIO1CompensationCell(void)
  644. {
  645. CLEAR_BIT(SBS->CCCSR, SBS_CCCSR_EN1);
  646. }
  647. /**
  648. * @brief Enables the VDDIO2 I/Os Compensation Cell.
  649. * @note The I/O compensation cell can be used only when the device supply
  650. * voltage ranges from 2.4 to 3.6 V.
  651. * @retval None
  652. */
  653. void HAL_SBS_EnableVddIO2CompensationCell(void)
  654. {
  655. SET_BIT(SBS->CCCSR, SBS_CCCSR_EN2) ;
  656. }
  657. /**
  658. * @brief Power-down the VDDIO2 I/Os Compensation Cell.
  659. * @note The I/O compensation cell can be used only when the device supply
  660. * voltage ranges from 2.4 to 3.6 V.
  661. * @retval None
  662. */
  663. void HAL_SBS_DisableVddIO2CompensationCell(void)
  664. {
  665. CLEAR_BIT(SBS->CCCSR, SBS_CCCSR_EN2);
  666. }
  667. /**
  668. * @brief Code selection for the VDD I/O Compensation cell
  669. * @param SBS_CompCode: Selects the code to be applied for the I/O compensation cell
  670. * This parameter can be one of the following values:
  671. * @arg SBS_VDD_CELL_CODE : Select Code from the cell (available in the SBS_CCVALR)
  672. * @arg SBS_VDD_REGISTER_CODE: Select Code from the SBS compensation cell code register (SBS_CCSWCR)
  673. * @retval None
  674. */
  675. void HAL_SBS_VDDCompensationCodeSelect(uint32_t SBS_CompCode)
  676. {
  677. /* Check the parameter */
  678. assert_param(IS_SBS_VDD_CODE_SELECT(SBS_CompCode));
  679. MODIFY_REG(SBS->CCCSR, SBS_CCCSR_CS1, (uint32_t)(SBS_CompCode));
  680. }
  681. /**
  682. * @brief Code selection for the VDDIO I/O Compensation cell
  683. * @param SBS_CompCode: Selects the code to be applied for the I/O compensation cell
  684. * This parameter can be one of the following values:
  685. * @arg SBS_VDDIO_CELL_CODE : Select Code from the cell (available in the SBS_CCVALR)
  686. * @arg SBS_VDDIO_REGISTER_CODE: Select Code from the SBS compensation cell code register (SBS_CCSWCR)
  687. * @retval None
  688. */
  689. void HAL_SBS_VDDIOCompensationCodeSelect(uint32_t SBS_CompCode)
  690. {
  691. /* Check the parameter */
  692. assert_param(IS_SBS_VDDIO_CODE_SELECT(SBS_CompCode));
  693. MODIFY_REG(SBS->CCCSR, SBS_CCCSR_CS2, (uint32_t)(SBS_CompCode));
  694. }
  695. /**
  696. * @brief VDDIO1 I/O Compensation cell get ready flag status
  697. * @retval State of bit (1 or 0).
  698. */
  699. uint32_t HAL_SBS_GetVddIO1CompensationCellReadyFlag(void)
  700. {
  701. return ((READ_BIT(SBS->CCCSR, SBS_CCCSR_RDY1) == SBS_CCCSR_RDY1) ? 1UL : 0UL);
  702. }
  703. /**
  704. * @brief VDDIO2 I/O Compensation cell get ready flag status
  705. * @retval State of bit (1 or 0).
  706. */
  707. uint32_t HAL_SBS_GetVddIO2CompensationCellReadyFlag(void)
  708. {
  709. return ((READ_BIT(SBS->CCCSR, SBS_CCCSR_RDY2) == SBS_CCCSR_RDY2) ? 1UL : 0UL);
  710. }
  711. /**
  712. * @brief Code configuration for the VDD I/O Compensation cell
  713. * @param SBS_PMOSCode: PMOS compensation code
  714. * This code is applied to the VDD I/O compensation cell when the CS1 bit of the
  715. * SBS_CCSR is set
  716. * @param SBS_NMOSCode: NMOS compensation code
  717. * This code is applied to the VDD I/O compensation cell when the CS1 bit of the
  718. * SBS_CCSR is set
  719. * @retval None
  720. */
  721. void HAL_SBS_VDDCompensationCodeConfig(uint32_t SBS_PMOSCode, uint32_t SBS_NMOSCode)
  722. {
  723. /* Check the parameter */
  724. assert_param(IS_SBS_CODE_CONFIG(SBS_PMOSCode));
  725. assert_param(IS_SBS_CODE_CONFIG(SBS_NMOSCode));
  726. MODIFY_REG(SBS->CCSWCR, SBS_CCSWCR_SW_ANSRC1 | SBS_CCSWCR_SW_APSRC1, (((uint32_t)(SBS_PMOSCode) << 4) | \
  727. (uint32_t)(SBS_NMOSCode)));
  728. }
  729. /**
  730. * @brief Code configuration for the VDDIO I/O Compensation cell
  731. * @param SBS_PMOSCode: PMOS compensation code
  732. * This code is applied to the VDDIO I/O compensation cell when the CS2 bit of the
  733. * SBS_CCSR is set
  734. * @param SBS_NMOSCode: NMOS compensation code
  735. * This code is applied to the VDDIO I/O compensation cell when the CS2 bit of the
  736. * SBS_CCSR is set
  737. * @retval None
  738. */
  739. void HAL_SBS_VDDIOCompensationCodeConfig(uint32_t SBS_PMOSCode, uint32_t SBS_NMOSCode)
  740. {
  741. /* Check the parameter */
  742. assert_param(IS_SBS_CODE_CONFIG(SBS_PMOSCode));
  743. assert_param(IS_SBS_CODE_CONFIG(SBS_NMOSCode));
  744. MODIFY_REG(SBS->CCSWCR, SBS_CCSWCR_SW_ANSRC2 | SBS_CCSWCR_SW_APSRC2, (((uint32_t)(SBS_PMOSCode) << 12) | \
  745. ((uint32_t)(SBS_NMOSCode) << 8)));
  746. }
  747. /**
  748. * @brief Get NMOS compensation value of the I/Os supplied by VDD
  749. * @retval None
  750. */
  751. uint32_t HAL_SBS_GetNMOSVddCompensationValue(void)
  752. {
  753. return (uint32_t)(READ_BIT(SBS->CCVALR, SBS_CCVALR_ANSRC1));
  754. }
  755. /**
  756. * @brief Get PMOS compensation value of the I/Os supplied by VDD
  757. * @retval None
  758. */
  759. uint32_t HAL_SBS_GetPMOSVddCompensationValue(void)
  760. {
  761. return (uint32_t)(READ_BIT(SBS->CCVALR, SBS_CCVALR_APSRC1) >> SBS_CCVALR_APSRC1_Pos);
  762. }
  763. /**
  764. * @brief Get NMOS compensation value of the I/Os supplied by VDDIO2
  765. * @retval None
  766. */
  767. uint32_t HAL_SBS_GetNMOSVddIO2CompensationValue(void)
  768. {
  769. return (uint32_t)(READ_BIT(SBS->CCVALR, SBS_CCVALR_ANSRC2) >> SBS_CCVALR_ANSRC2_Pos);
  770. }
  771. /**
  772. * @brief Get PMOS compensation value of the I/Os supplied by VDDIO2
  773. * @retval None
  774. */
  775. uint32_t HAL_SBS_GetPMOSVddIO2CompensationValue(void)
  776. {
  777. return (uint32_t)(READ_BIT(SBS->CCVALR, SBS_CCVALR_APSRC2) >> SBS_CCVALR_APSRC2_Pos);
  778. }
  779. /**
  780. * @brief Disable the NMI in case of double ECC error in FLASH Interface.
  781. *
  782. * @retval None
  783. */
  784. void HAL_SBS_FLASH_DisableECCNMI(void)
  785. {
  786. SET_BIT(SBS->ECCNMIR, SBS_ECCNMIR_ECCNMI_MASK_EN);
  787. }
  788. /**
  789. * @brief Enable the NMI in case of double ECC error in FLASH Interface.
  790. *
  791. * @retval None
  792. */
  793. void HAL_SBS_FLASH_EnableECCNMI(void)
  794. {
  795. CLEAR_BIT(SBS->ECCNMIR, SBS_ECCNMIR_ECCNMI_MASK_EN);
  796. }
  797. /**
  798. * @brief Check if the NMI is Enabled in case of double ECC error in FLASH Interface.
  799. *
  800. * @retval State of bit (1 or 0).
  801. */
  802. uint32_t HAL_SBS_FLASH_ECCNMI_IsDisabled(void)
  803. {
  804. return ((READ_BIT(SBS->ECCNMIR, SBS_ECCNMIR_ECCNMI_MASK_EN) == SBS_ECCNMIR_ECCNMI_MASK_EN) ? 1UL : 0UL);
  805. }
  806. /**
  807. * @}
  808. */
  809. /** @defgroup HAL_Exported_Functions_Group6 HAL SBS Boot control functions
  810. * @brief HAL SBS Boot functions
  811. *
  812. @verbatim
  813. =======================================================================================================================
  814. ##### HAL SBS Boot control functions #####
  815. =======================================================================================================================
  816. [..] This section provides functions allowing to:
  817. (+) Increment the HDPL value
  818. (+) Get the HDPL value
  819. @endverbatim
  820. * @{
  821. */
  822. /**
  823. * @brief Increment by 1 the HDPL value
  824. * @retval None
  825. */
  826. void HAL_SBS_IncrementHDPLValue(void)
  827. {
  828. MODIFY_REG(SBS->HDPLCR, SBS_HDPLCR_INCR_HDPL, SBS_HDPL_INCREMENT_VALUE);
  829. }
  830. /**
  831. * @brief Get the HDPL Value.
  832. *
  833. * @retval Returns the HDPL value
  834. * This return value can be one of the following values:
  835. * @arg SBS_HDPL_VALUE_0: HDPL0
  836. * @arg SBS_HDPL_VALUE_1: HDPL1
  837. * @arg SBS_HDPL_VALUE_2: HDPL2
  838. * @arg SBS_HDPL_VALUE_3: HDPL3
  839. */
  840. uint32_t HAL_SBS_GetHDPLValue(void)
  841. {
  842. return (uint32_t)(READ_BIT(SBS->HDPLSR, SBS_HDPLSR_HDPL));
  843. }
  844. /**
  845. * @}
  846. */
  847. /** @defgroup HAL_Exported_Functions_Group7 HAL SBS Hardware secure storage control functions
  848. * @brief HAL SBS Hardware secure storage functions
  849. *
  850. @verbatim
  851. =======================================================================================================================
  852. ##### HAL SBS Hardware secure storage control functions #####
  853. =======================================================================================================================
  854. [..] This section provides functions allowing to:
  855. (+) Select EPOCH security sent to SAES IP
  856. (+) Set/Get EPOCH security selection
  857. (+) Set/Get the OBK-HDPL Value
  858. @endverbatim
  859. * @{
  860. */
  861. #if defined(SBS_EPOCHSELCR_EPOCH_SEL)
  862. /**
  863. * @brief Select EPOCH security sent to SAES IP to encrypt/decrypt keys
  864. * @param Epoch_Selection: Select EPOCH security
  865. * This parameter can be one of the following values:
  866. * @arg SBS_EPOCH_SEL_SECURE : EPOCH secure selected.
  867. * @arg SBS_EPOCH_SEL_NONSECURE : EPOCH non secure selected.
  868. * @arg SBS_EPOCH_SEL_PUFCHECK : EPOCH all zeros for PUF integrity check.
  869. * @retval None
  870. */
  871. void HAL_SBS_EPOCHSelection(uint32_t Epoch_Selection)
  872. {
  873. /* Check the parameter */
  874. assert_param(IS_SBS_EPOCH_SELECTION(Epoch_Selection));
  875. MODIFY_REG(SBS->EPOCHSELCR, SBS_EPOCHSELCR_EPOCH_SEL, (uint32_t)(Epoch_Selection));
  876. }
  877. /**
  878. * @brief Get EPOCH security selection
  879. * @retval Returned value can be one of the following values:
  880. * @arg SBS_EPOCH_SEL_SECURE : EPOCH secure selected.
  881. * @arg SBS_EPOCH_SEL_NONSECURE : EPOCH non secure selected.
  882. * @arg SBS_EPOCH_SEL_PUFCHECK : EPOCH all zeros for PUF integrity check.
  883. */
  884. uint32_t HAL_SBS_GetEPOCHSelection(void)
  885. {
  886. return (uint32_t)(READ_BIT(SBS->EPOCHSELCR, SBS_EPOCHSELCR_EPOCH_SEL));
  887. }
  888. #endif /* SBS_EPOCHSELCR_EPOCH_SEL */
  889. #if defined(SBS_NEXTHDPLCR_NEXTHDPL)
  890. /**
  891. * @brief Set the OBK-HDPL Value.
  892. * @param OBKHDPL_Value Value of the increment to add to HDPL value to generate the OBK-HDPL.
  893. * This parameter can be one of the following values:
  894. * @arg SBS_OBKHDPL_INCR_0 : HDPL
  895. * @arg SBS_OBKHDPL_INCR_1 : HDPL + 1
  896. * @arg SBS_OBKHDPL_INCR_2 : HDPL + 2
  897. * @arg SBS_OBKHDPL_INCR_3 : HDPL + 3
  898. * @retval None
  899. */
  900. void HAL_SBS_SetOBKHDPL(uint32_t OBKHDPL_Value)
  901. {
  902. /* Check the parameter */
  903. assert_param(IS_SBS_OBKHDPL_SELECTION(OBKHDPL_Value));
  904. MODIFY_REG(SBS->NEXTHDPLCR, SBS_NEXTHDPLCR_NEXTHDPL, (uint32_t)(OBKHDPL_Value));
  905. }
  906. /**
  907. * @brief Get the OBK-HDPL Value.
  908. * @retval Returns the incremement to add to HDPL value to generate OBK-HDPL
  909. * This return value can be one of the following values:
  910. * @arg SBS_OBKHDPL_INCR_0: HDPL
  911. * @arg SBS_OBKHDPL_INCR_1: HDPL + 1
  912. * @arg SBS_OBKHDPL_INCR_2: HDPL + 2
  913. * @arg SBS_OBKHDPL_INCR_3: HDPL + 3
  914. */
  915. uint32_t HAL_SBS_GetOBKHDPL(void)
  916. {
  917. return (uint32_t)(READ_BIT(SBS->NEXTHDPLCR, SBS_NEXTHDPLCR_NEXTHDPL));
  918. }
  919. #endif /* SBS_NEXTHDPLCR_NEXTHDPL */
  920. /**
  921. * @}
  922. */
  923. /** @defgroup HAL_Exported_Functions_Group8 HAL SBS Debug control functions
  924. * @brief HAL SBS Debug functions
  925. *
  926. @verbatim
  927. =======================================================================================================================
  928. ##### SBS Debug control functions #####
  929. =======================================================================================================================
  930. [..] This section provides functions allowing to:
  931. (+) Open the device access port
  932. (+) Open the debug
  933. (+) Configure the authenticated debug HDPL
  934. (+) Get the current value of the hide protection level
  935. (+) Lock the access to the debug control register
  936. (+) Configure/Get the authenticated debug security access
  937. @endverbatim
  938. * @{
  939. */
  940. /**
  941. * @brief Open the device access port.
  942. * @note This function can be only used when device state is Closed.
  943. * @retval None
  944. */
  945. void HAL_SBS_OpenAccessPort(void)
  946. {
  947. MODIFY_REG(SBS->DBGCR, SBS_DBGCR_AP_UNLOCK, SBS_DEBUG_UNLOCK_VALUE);
  948. }
  949. /**
  950. * @brief Open the debug when the hide protection level is authorized.
  951. * @note This function can be only used when device state is Closed.
  952. * @retval None
  953. */
  954. void HAL_SBS_OpenDebug(void)
  955. {
  956. MODIFY_REG(SBS->DBGCR, SBS_DBGCR_DBG_UNLOCK, (SBS_DEBUG_UNLOCK_VALUE << SBS_DBGCR_DBG_UNLOCK_Pos));
  957. }
  958. /**
  959. * @brief Configure the authenticated debug hide protection level.
  960. * @note This function can be only used when device state is Closed.
  961. * @param Level Hide protection level where the authenticated debug opens
  962. * This value is one of @ref SBS_HDPL_Value (except SBS_HDPL_VALUE_0)
  963. * @retval HAL_OK if parameter is correct
  964. * HAL_ERROR otherwise
  965. */
  966. HAL_StatusTypeDef HAL_SBS_ConfigDebugLevel(uint32_t Level)
  967. {
  968. /* Check the parameter */
  969. assert_param(IS_SBS_HDPL(Level));
  970. if (Level != SBS_HDPL_VALUE_0)
  971. {
  972. MODIFY_REG(SBS->DBGCR, SBS_DBGCR_DBG_AUTH_HDPL, (Level << SBS_DBGCR_DBG_AUTH_HDPL_Pos));
  973. return HAL_OK;
  974. }
  975. else
  976. {
  977. return HAL_ERROR;
  978. }
  979. }
  980. /**
  981. * @brief Get the current value of the hide protection level.
  982. * @note This function can be only used when device state is Closed.
  983. * @retval Current hide protection level
  984. * This value is one of @ref SBS_HDPL_Value
  985. */
  986. uint32_t HAL_SBS_GetDebugLevel(void)
  987. {
  988. return ((SBS->DBGCR & SBS_DBGCR_DBG_AUTH_HDPL) >> SBS_DBGCR_DBG_AUTH_HDPL_Pos);
  989. }
  990. /**
  991. * @brief Lock the access to the debug control register.
  992. * @note This function can be only used when device state is Closed.
  993. * @note locking the current debug configuration is released only by a reset.
  994. * @retval None
  995. */
  996. void HAL_SBS_LockDebugConfig(void)
  997. {
  998. MODIFY_REG(SBS->DBGLOCKR, SBS_DBGLOCKR_DBGCFG_LOCK, SBS_DEBUG_LOCK_VALUE);
  999. }
  1000. #if defined(SBS_DBGCR_DBG_AUTH_SEC)
  1001. /**
  1002. * @brief Configure the authenticated debug security access.
  1003. * @param Control debug opening secure/non-secure or non-secure only
  1004. * This parameter can be one of the following values:
  1005. * @arg SBS_DEBUG_SEC_NSEC: debug opening for secure and non-secure.
  1006. * @arg SBS_DEBUG_NSEC: debug opening for non-secure only.
  1007. * @retval None
  1008. */
  1009. void HAL_SBS_ConfigDebugSecurity(uint32_t Security)
  1010. {
  1011. MODIFY_REG(SBS->DBGCR, SBS_DBGCR_DBG_AUTH_SEC, (Security << SBS_DBGCR_DBG_AUTH_SEC_Pos));
  1012. }
  1013. /**
  1014. * @brief Get the current value of the hide protection level.
  1015. * @note This function can be only used when device state is Closed.
  1016. * @retval Returned value can be one of the following values:
  1017. * @arg SBS_DEBUG_SEC_NSEC: debug opening for secure and non-secure.
  1018. * @arg SBS_DEBUG_NSEC: debug opening for non-secure only.
  1019. */
  1020. uint32_t HAL_SBS_GetDebugSecurity(void)
  1021. {
  1022. return ((SBS->DBGCR & SBS_DBGCR_DBG_AUTH_SEC) >> SBS_DBGCR_DBG_AUTH_SEC_Pos);
  1023. }
  1024. #endif /* SBS_DBGCR_DBG_AUTH_SEC */
  1025. /**
  1026. * @}
  1027. */
  1028. /** @defgroup HAL_Exported_Functions_Group9 HAL SBS lock management functions
  1029. * @brief SBS lock management functions.
  1030. *
  1031. @verbatim
  1032. =======================================================================================================================
  1033. ##### SBS lock functions #####
  1034. =======================================================================================================================
  1035. @endverbatim
  1036. * @{
  1037. */
  1038. /**
  1039. * @brief Lock the SBS item(s).
  1040. * @note Setting lock(s) depends on privilege mode in secure/non-secure code
  1041. * Lock(s) cleared only at system reset
  1042. * @param Item Item(s) to set lock on.
  1043. * This parameter can be a combination of @ref SBS_Lock_items
  1044. * @retval None
  1045. */
  1046. void HAL_SBS_Lock(uint32_t Item)
  1047. {
  1048. /* Check the parameters */
  1049. assert_param(IS_SBS_LOCK_ITEMS(Item));
  1050. /* Privilege secure/non-secure locks */
  1051. SBS->CNSLCKR = (0xFFFFU & Item); /* non-secure lock item in 16 lowest bits */
  1052. #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
  1053. /* Privilege secure only locks */
  1054. SBS->CSLCKR = ((0xFFFF0000U & Item) >> 16U); /* Secure-only lock item in 16 highest bits */
  1055. #endif /* __ARM_FEATURE_CMSE */
  1056. }
  1057. /**
  1058. * @brief Get the lock state of SBS items.
  1059. * @note Getting lock(s) depends on privilege mode in secure/non-secure code
  1060. * @param pItem pointer to return locked items
  1061. * the return value can be a combination of @ref SBS_Lock_items
  1062. * @retval HAL status
  1063. */
  1064. HAL_StatusTypeDef HAL_SBS_GetLock(uint32_t *pItem)
  1065. {
  1066. uint32_t tmp_lock;
  1067. /* Check null pointer */
  1068. if (pItem == NULL)
  1069. {
  1070. return HAL_ERROR;
  1071. }
  1072. /* Get the non-secure lock state */
  1073. tmp_lock = SBS->CNSLCKR;
  1074. /* Get the secure lock state in secure code */
  1075. #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
  1076. tmp_lock |= (SBS->CSLCKR << 16U);
  1077. #endif /* __ARM_FEATURE_CMSE */
  1078. /* Return overall lock status */
  1079. *pItem = tmp_lock;
  1080. return HAL_OK;
  1081. }
  1082. /**
  1083. * @}
  1084. */
  1085. /** @defgroup HAL_Exported_Functions_Group10 HAL SBS attributes management functions
  1086. * @brief SBS attributes management functions.
  1087. *
  1088. @verbatim
  1089. =======================================================================================================================
  1090. ##### SBS attributes functions #####
  1091. =======================================================================================================================
  1092. @endverbatim
  1093. * @{
  1094. */
  1095. #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
  1096. /**
  1097. * @brief Configure the SBS item attribute(s).
  1098. * @note Available attributes are to secure SBS items, so this function is
  1099. * only available in secure.
  1100. * SBS_FPU item attribute is only configurable through PRIVILEGE transaction.
  1101. * @param Item Item(s) to set attributes on.
  1102. * This parameter can be a one or a combination of @ref SBS_Attributes_items
  1103. * @param Attributes specifies the secure/non-secure attributes.
  1104. * @retval None
  1105. */
  1106. void HAL_SBS_ConfigAttributes(uint32_t Item, uint32_t Attributes)
  1107. {
  1108. uint32_t tmp;
  1109. /* Check the parameters */
  1110. assert_param(IS_SBS_ITEMS_ATTRIBUTES(Item));
  1111. assert_param(IS_SBS_ATTRIBUTES(Attributes));
  1112. tmp = SBS->SECCFGR;
  1113. /* Set or reset Item */
  1114. if ((Attributes & SBS_SEC) != 0x00U)
  1115. {
  1116. tmp |= Item;
  1117. }
  1118. else
  1119. {
  1120. tmp &= ~Item;
  1121. }
  1122. /* Set secure attributes */
  1123. SBS->SECCFGR = tmp;
  1124. }
  1125. /**
  1126. * @brief Get the attribute of a SBS items.
  1127. * @note Available attributes have read restrictions, so this function is
  1128. * only available in secure
  1129. * @param Item Single item to get secure/non-secure attribute from.
  1130. * @param pAttributes pointer to return the attribute.
  1131. * @retval HAL status
  1132. */
  1133. HAL_StatusTypeDef HAL_SBS_GetConfigAttributes(uint32_t Item, uint32_t *pAttributes)
  1134. {
  1135. /* Check null pointer */
  1136. if (pAttributes == NULL)
  1137. {
  1138. return HAL_ERROR;
  1139. }
  1140. /* Check the parameters */
  1141. assert_param(IS_SBS_ITEMS_ATTRIBUTES(Item));
  1142. /* Get the secure attribute state */
  1143. if ((SBS->SECCFGR & Item) != 0U)
  1144. {
  1145. *pAttributes = SBS_SEC;
  1146. }
  1147. else
  1148. {
  1149. *pAttributes = SBS_NSEC;
  1150. }
  1151. return HAL_OK;
  1152. }
  1153. #endif /* __ARM_FEATURE_CMSE */
  1154. /**
  1155. * @}
  1156. */
  1157. /**
  1158. * @}
  1159. */
  1160. #endif /* HAL_MODULE_ENABLED */
  1161. /**
  1162. * @}
  1163. */
  1164. /**
  1165. * @}
  1166. */