source: trunk/firmware_cube/Drivers/STM32C0xx_HAL_Driver/Src/stm32c0xx_hal.c

Last change on this file was 7, checked in by f.jahn, 5 months ago
File size: 17.7 KB
Line 
1/**
2  ******************************************************************************
3  * @file    stm32c0xx_hal.c
4  * @author  MCD Application Team
5  * @brief   HAL module driver.
6  *          This is the common part of the HAL initialization
7  *
8  ******************************************************************************
9  * @attention
10  *
11  * Copyright (c) 2022 STMicroelectronics.
12  * All rights reserved.
13  *
14  * This software is licensed under terms that can be found in the LICENSE file
15  * in the root directory of this software component.
16  * If no LICENSE file comes with this software, it is provided AS-IS.
17  *
18  ******************************************************************************
19  @verbatim
20  ==============================================================================
21                     ##### How to use this driver #####
22  ==============================================================================
23    [..]
24    The common HAL driver contains a set of generic and common APIs that can be
25    used by the PPP peripheral drivers and the user to start using the HAL.
26    [..]
27    The HAL contains two APIs' categories:
28         (+) Common HAL APIs
29         (+) Services HAL APIs
30
31  @endverbatim
32  ******************************************************************************
33  */
34
35/* Includes ------------------------------------------------------------------*/
36#include "stm32c0xx_hal.h"
37
38/** @addtogroup STM32C0xx_HAL_Driver
39  * @{
40  */
41
42/** @addtogroup HAL
43  * @brief HAL module driver
44  * @{
45  */
46
47#ifdef HAL_MODULE_ENABLED
48
49/* Private typedef -----------------------------------------------------------*/
50/* Private define ------------------------------------------------------------*/
51
52/** @defgroup HAL_Private_Constants HAL Private Constants
53  * @{
54  */
55/**
56  * @brief STM32C0xx HAL Driver version number
57   */
58#define __STM32C0xx_HAL_VERSION_MAIN   (0x01U) /*!< [31:24] main version */
59#define __STM32C0xx_HAL_VERSION_SUB1   (0x03U) /*!< [23:16] sub1 version */
60#define __STM32C0xx_HAL_VERSION_SUB2   (0x00U) /*!< [15:8]  sub2 version */
61#define __STM32C0xx_HAL_VERSION_RC     (0x00U) /*!< [7:0]  release candidate */
62#define __STM32C0xx_HAL_VERSION         ((__STM32C0xx_HAL_VERSION_MAIN << 24U)\
63                                         |(__STM32C0xx_HAL_VERSION_SUB1 << 16U)\
64                                         |(__STM32C0xx_HAL_VERSION_SUB2 << 8U )\
65                                         |(__STM32C0xx_HAL_VERSION_RC))
66
67/**
68  * @}
69  */
70
71/* Private macro -------------------------------------------------------------*/
72/* Exported variables ---------------------------------------------------------*/
73/** @defgroup HAL_Exported_Variables HAL Exported Variables
74  * @{
75  */
76__IO uint32_t uwTick;
77uint32_t uwTickPrio = (1UL << __NVIC_PRIO_BITS); /* Invalid PRIO */
78HAL_TickFreqTypeDef uwTickFreq = HAL_TICK_FREQ_DEFAULT;  /* 1KHz */
79/**
80  * @}
81  */
82
83/* Private function prototypes -----------------------------------------------*/
84/* Exported functions --------------------------------------------------------*/
85
86/** @addtogroup HAL_Exported_Functions
87  * @{
88  */
89
90/** @addtogroup HAL_Exported_Functions_Group1
91  *  @brief    HAL Initialization and Configuration functions
92  *
93@verbatim
94 ===============================================================================
95           ##### HAL Initialization and Configuration functions #####
96 ===============================================================================
97    [..]  This section provides functions allowing to:
98      (+) Initialize the Flash interface the NVIC allocation and initial time base
99          clock configuration.
100      (+) De-initialize common part of the HAL.
101      (+) Configure the time base source to have 1ms time base with a dedicated
102          Tick interrupt priority.
103        (++) SysTick timer is used by default as source of time base, but user
104             can eventually implement his proper time base source (a general purpose
105             timer for example or other time source), keeping in mind that Time base
106             duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and
107             handled in milliseconds basis.
108        (++) Time base configuration function (HAL_InitTick ()) is called automatically
109             at the beginning of the program after reset by HAL_Init() or at any time
110             when clock is configured, by HAL_RCC_ClockConfig().
111        (++) Source of time base is configured  to generate interrupts at regular
112             time intervals. Care must be taken if HAL_Delay() is called from a
113             peripheral ISR process, the Tick interrupt line must have higher priority
114            (numerically lower) than the peripheral interrupt. Otherwise the caller
115            ISR process will be blocked.
116       (++) functions affecting time base configurations are declared as __weak
117             to make  override possible  in case of other  implementations in user file.
118@endverbatim
119  * @{
120  */
121
122/**
123  * @brief  Configure the Flash prefetch and the Instruction cache,
124  *         the time base source, NVIC and any required global low level hardware
125  *         by calling the HAL_MspInit() callback function to be optionally defined in user file
126  *         stm32c0xx_hal_msp.c.
127  *
128  * @note   HAL_Init() function is called at the beginning of program after reset and before
129  *         the clock configuration.
130  *
131  * @note   In the default implementation the System Timer (Systick) is used as source of time base.
132  *         The Systick configuration is based on HSI clock, as HSI is the clock
133  *         used after a system Reset.
134  *         Once done, time base tick starts incrementing: the tick variable counter is incremented
135  *         each 1ms in the SysTick_Handler() interrupt handler.
136  *
137  * @retval HAL status
138  */
139HAL_StatusTypeDef HAL_Init(void)
140{
141  HAL_StatusTypeDef  status = HAL_OK;
142
143  /* Configure Flash prefetch, Instruction cache             */
144  /* Default configuration at reset is:                      */
145  /* - Prefetch disabled                                     */
146  /* - Instruction cache enabled                             */
147
148#if (INSTRUCTION_CACHE_ENABLE == 0U)
149  __HAL_FLASH_INSTRUCTION_CACHE_DISABLE();
150#endif /* INSTRUCTION_CACHE_ENABLE */
151
152#if (PREFETCH_ENABLE != 0U)
153  __HAL_FLASH_PREFETCH_BUFFER_ENABLE();
154#endif /* PREFETCH_ENABLE */
155
156  /* Use SysTick as time base source and configure 1ms tick (default clock after Reset is HSI) */
157  if (HAL_InitTick(TICK_INT_PRIORITY) != HAL_OK)
158  {
159    status = HAL_ERROR;
160  }
161  else
162  {
163    /* Init the low level hardware */
164    HAL_MspInit();
165  }
166
167  /* Return function status */
168  return status;
169}
170
171/**
172  * @brief  This function de-Initializes common part of the HAL and stops the source of time base.
173  * @note   This function is optional.
174  * @retval HAL status
175  */
176HAL_StatusTypeDef HAL_DeInit(void)
177{
178  /* Reset of all peripherals */
179  __HAL_RCC_APB1_GRP1_FORCE_RESET();
180  __HAL_RCC_APB1_GRP1_RELEASE_RESET();
181
182  __HAL_RCC_APB1_GRP2_FORCE_RESET();
183  __HAL_RCC_APB1_GRP2_RELEASE_RESET();
184
185  __HAL_RCC_AHB_FORCE_RESET();
186  __HAL_RCC_AHB_RELEASE_RESET();
187
188  __HAL_RCC_IOP_FORCE_RESET();
189  __HAL_RCC_IOP_RELEASE_RESET();
190
191  /* De-Init the low level hardware */
192  HAL_MspDeInit();
193
194  /* Return function status */
195  return HAL_OK;
196}
197
198/**
199  * @brief  Initialize the MSP.
200  * @retval None
201  */
202__weak void HAL_MspInit(void)
203{
204  /* NOTE : This function should not be modified, when the callback is needed,
205            the HAL_MspInit could be implemented in the user file
206   */
207}
208
209/**
210  * @brief  DeInitializes the MSP.
211  * @retval None
212  */
213__weak void HAL_MspDeInit(void)
214{
215  /* NOTE : This function should not be modified, when the callback is needed,
216            the HAL_MspDeInit could be implemented in the user file
217   */
218}
219
220/**
221  * @brief This function configures the source of the time base:
222  *        The time source is configured  to have 1ms time base with a dedicated
223  *        Tick interrupt priority.
224  * @note This function is called  automatically at the beginning of program after
225  *       reset by HAL_Init() or at any time when clock is reconfigured  by HAL_RCC_ClockConfig().
226  * @note In the default implementation, SysTick timer is the source of time base.
227  *       It is used to generate interrupts at regular time intervals.
228  *       Care must be taken if HAL_Delay() is called from a peripheral ISR process,
229  *       The SysTick interrupt must have higher priority (numerically lower)
230  *       than the peripheral interrupt. Otherwise the caller ISR process will be blocked.
231  *       The function is declared as __weak  to be overwritten  in case of other
232  *       implementation  in user file.
233  * @param TickPriority Tick interrupt priority.
234  * @retval HAL status
235  */
236__weak HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
237{
238  HAL_StatusTypeDef  status = HAL_OK;
239
240  if ((uint32_t)uwTickFreq != 0UL)
241  {
242    /*Configure the SysTick to have interrupt in 1ms time basis*/
243    if (HAL_SYSTICK_Config(SystemCoreClock / (1000UL / (uint32_t)uwTickFreq)) == 0U)
244    {
245      /* Configure the SysTick IRQ priority */
246      if (TickPriority < (1UL << __NVIC_PRIO_BITS))
247      {
248        HAL_NVIC_SetPriority(SysTick_IRQn, TickPriority, 0U);
249        uwTickPrio = TickPriority;
250      }
251      else
252      {
253        status = HAL_ERROR;
254      }
255    }
256    else
257    {
258      status = HAL_ERROR;
259    }
260  }
261  else
262  {
263    status = HAL_ERROR;
264  }
265
266  /* Return function status */
267  return status;
268}
269
270/**
271  * @}
272  */
273
274/** @addtogroup HAL_Exported_Functions_Group2
275  *  @brief    HAL Control functions
276  *
277@verbatim
278 ===============================================================================
279                      ##### HAL Control functions #####
280 ===============================================================================
281    [..]  This section provides functions allowing to:
282      (+) Provide a tick value in millisecond
283      (+) Provide a blocking delay in millisecond
284      (+) Suspend the time base source interrupt
285      (+) Resume the time base source interrupt
286      (+) Get the HAL API driver version
287      (+) Get the device identifier
288      (+) Get the device revision identifier
289
290@endverbatim
291  * @{
292  */
293
294/**
295  * @brief This function is called to increment  a global variable "uwTick"
296  *        used as application time base.
297  * @note In the default implementation, this variable is incremented each 1ms
298  *       in SysTick ISR.
299  * @note This function is declared as __weak to be overwritten in case of other
300  *      implementations in user file.
301  * @retval None
302  */
303__weak void HAL_IncTick(void)
304{
305  uwTick += (uint32_t)uwTickFreq;
306}
307
308/**
309  * @brief Provides a tick value in millisecond.
310  * @note This function is declared as __weak to be overwritten in case of other
311  *       implementations in user file.
312  * @retval tick value
313  */
314__weak uint32_t HAL_GetTick(void)
315{
316  return uwTick;
317}
318
319/**
320  * @brief This function returns a tick priority.
321  * @retval tick priority
322  */
323uint32_t HAL_GetTickPrio(void)
324{
325  return uwTickPrio;
326}
327
328/**
329  * @brief Set new tick Freq.
330  * @retval status
331  */
332HAL_StatusTypeDef HAL_SetTickFreq(HAL_TickFreqTypeDef Freq)
333{
334  HAL_StatusTypeDef status  = HAL_OK;
335
336  assert_param(IS_TICKFREQ(Freq));
337
338  if (uwTickFreq != Freq)
339  {
340    /* Apply the new tick Freq  */
341    status = HAL_InitTick(uwTickPrio);
342
343    if (status == HAL_OK)
344    {
345      uwTickFreq = Freq;
346    }
347  }
348
349  return status;
350}
351
352/**
353  * @brief return tick frequency.
354  * @retval Tick frequency.
355  *         Value of @ref HAL_TickFreqTypeDef.
356  */
357HAL_TickFreqTypeDef HAL_GetTickFreq(void)
358{
359  return uwTickFreq;
360}
361
362/**
363  * @brief This function provides minimum delay (in milliseconds) based
364  *        on variable incremented.
365  * @note In the default implementation , SysTick timer is the source of time base.
366  *       It is used to generate interrupts at regular time intervals where uwTick
367  *       is incremented.
368  * @note This function is declared as __weak to be overwritten in case of other
369  *       implementations in user file.
370  * @param Delay  specifies the delay time length, in milliseconds.
371  * @retval None
372  */
373__weak void HAL_Delay(uint32_t Delay)
374{
375  uint32_t tickstart = HAL_GetTick();
376  uint32_t wait = Delay;
377
378  /* Add a freq to guarantee minimum wait */
379  if (wait < HAL_MAX_DELAY)
380  {
381    wait += (uint32_t)(uwTickFreq);
382  }
383
384  while ((HAL_GetTick() - tickstart) < wait)
385  {
386  }
387}
388
389/**
390  * @brief Suspend 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_SuspendTick()
393  *       is called, the SysTick interrupt will be disabled and so Tick increment
394  *       is suspended.
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_SuspendTick(void)
400{
401  /* Disable SysTick Interrupt */
402  CLEAR_BIT(SysTick->CTRL, SysTick_CTRL_TICKINT_Msk);
403}
404
405/**
406  * @brief Resume Tick increment.
407  * @note In the default implementation , SysTick timer is the source of time base. It is
408  *       used to generate interrupts at regular time intervals. Once HAL_ResumeTick()
409  *       is called, the SysTick interrupt will be enabled and so Tick increment
410  *       is resumed.
411  * @note This function is declared as __weak to be overwritten in case of other
412  *       implementations in user file.
413  * @retval None
414  */
415__weak void HAL_ResumeTick(void)
416{
417  /* Enable SysTick Interrupt */
418  SET_BIT(SysTick->CTRL, SysTick_CTRL_TICKINT_Msk);
419}
420
421/**
422  * @brief  Returns the HAL revision
423  * @retval version : 0xXYZR (8bits for each decimal, R for RC)
424  */
425uint32_t HAL_GetHalVersion(void)
426{
427  return __STM32C0xx_HAL_VERSION;
428}
429
430/**
431  * @brief  Returns the device revision identifier.
432  * @retval Device revision identifier
433  */
434uint32_t HAL_GetREVID(void)
435{
436  return ((DBG->IDCODE & DBG_IDCODE_REV_ID) >> 16U);
437}
438
439/**
440  * @brief  Returns the device identifier.
441  * @retval Device identifier
442  */
443uint32_t HAL_GetDEVID(void)
444{
445  return ((DBG->IDCODE) & DBG_IDCODE_DEV_ID);
446}
447
448/**
449  * @brief  Returns first word of the unique device identifier (UID based on 96 bits)
450  * @retval Device identifier
451  */
452uint32_t HAL_GetUIDw0(void)
453{
454  return (READ_REG(*((uint32_t *)UID_BASE)));
455}
456
457/**
458  * @brief  Returns second word of the unique device identifier (UID based on 96 bits)
459  * @retval Device identifier
460  */
461uint32_t HAL_GetUIDw1(void)
462{
463  return (READ_REG(*((uint32_t *)(UID_BASE + 4U))));
464}
465
466/**
467  * @brief  Returns third word of the unique device identifier (UID based on 96 bits)
468  * @retval Device identifier
469  */
470uint32_t HAL_GetUIDw2(void)
471{
472  return (READ_REG(*((uint32_t *)(UID_BASE + 8U))));
473}
474
475/**
476  * @}
477  */
478
479/** @addtogroup HAL_Exported_Functions_Group3
480  *  @brief    HAL Debug functions
481  *
482@verbatim
483 ===============================================================================
484                      ##### HAL Debug functions #####
485 ===============================================================================
486    [..]  This section provides functions allowing to:
487      (+) Enable/Disable Debug module during STOP mode
488      (+) Enable/Disable Debug module during STANDBY mode
489
490@endverbatim
491  * @{
492  */
493
494/**
495  * @brief  Enable the Debug Module during STOP mode
496  * @retval None
497  */
498void HAL_DBGMCU_EnableDBGStopMode(void)
499{
500  SET_BIT(DBG->CR, DBG_CR_DBG_STOP);
501}
502
503/**
504  * @brief  Disable the Debug Module during STOP mode
505  * @retval None
506  */
507void HAL_DBGMCU_DisableDBGStopMode(void)
508{
509  CLEAR_BIT(DBG->CR, DBG_CR_DBG_STOP);
510}
511
512/**
513  * @brief  Enable the Debug Module during STANDBY mode
514  * @retval None
515  */
516void HAL_DBGMCU_EnableDBGStandbyMode(void)
517{
518  SET_BIT(DBG->CR, DBG_CR_DBG_STANDBY);
519}
520
521/**
522  * @brief  Disable the Debug Module during STANDBY mode
523  * @retval None
524  */
525void HAL_DBGMCU_DisableDBGStandbyMode(void)
526{
527  CLEAR_BIT(DBG->CR, DBG_CR_DBG_STANDBY);
528}
529
530/**
531  * @}
532  */
533
534/** @addtogroup HAL_Exported_Functions_Group4
535  *  @brief    SYSCFG configuration functions
536  *
537@verbatim
538 ===============================================================================
539                      ##### HAL SYSCFG configuration functions #####
540 ===============================================================================
541    [..]  This section provides functions allowing to:
542      (+) Enable/Disable Pin remap
543@endverbatim
544  * @{
545  */
546/**
547  * @brief  Enable the remap on PA11_PA12
548  * @param  PinRemap specifies which pins have to be remapped
549  *         This parameter can be any combination of the following values:
550  *         @arg @ref SYSCFG_REMAP_PA11
551  *         @arg @ref SYSCFG_REMAP_PA12
552  * @retval None
553  */
554void HAL_SYSCFG_EnableRemap(uint32_t PinRemap)
555{
556  /* Check the parameter */
557  assert_param(IS_HAL_REMAP_PIN(PinRemap));
558  SET_BIT(SYSCFG->CFGR1, PinRemap);
559}
560
561/**
562  * @brief  Disable the remap on PA11_PA12
563  * @param  PinRemap specifies which pins will behave normally
564  *         This parameter can be any combination of the following values:
565  *         @arg @ref SYSCFG_REMAP_PA11
566  *         @arg @ref SYSCFG_REMAP_PA12
567  * @retval None
568  */
569void HAL_SYSCFG_DisableRemap(uint32_t PinRemap)
570{
571  /* Check the parameter */
572  assert_param(IS_HAL_REMAP_PIN(PinRemap));
573  CLEAR_BIT(SYSCFG->CFGR1, PinRemap);
574}
575/**
576  * @brief  Set Pin Binding
577  * @param  pin_binding specifies which pin will bind a specific GPIO
578  *         for each die package
579  *         This parameter can be a value of @ref HAL_BIND_CFG
580  * @retval None
581  */
582void HAL_SYSCFG_SetPinBinding(uint32_t pin_binding)
583{
584  /* Check the parameter */
585  assert_param(IS_HAL_SYSCFG_PINBINDING(pin_binding));
586  LL_SYSCFG_ConfigPinMux(pin_binding);
587}
588
589/**
590  * @brief return Pin Binding configuration
591  * @param  pin_binding_source
592  *         This parameter can be a value of @ref HAL_BIND_SCOURCE
593  * @retval PinMux configuration
594  */
595uint32_t HAL_SYSCFG_GetPinBinding(uint32_t pin_binding_source)
596{
597  return LL_SYSCFG_GetConfigPinMux(pin_binding_source);
598}
599
600/**
601  * @}
602  */
603
604/**
605  * @}
606  */
607
608#endif /* HAL_MODULE_ENABLED */
609/**
610  * @}
611  */
612
613/**
614  * @}
615  */
Note: See TracBrowser for help on using the repository browser.