STM32L4xx_HAL_Driver  1.14.0
stm32l4xx_hal.c
Go to the documentation of this file.
1 
35 /* Includes ------------------------------------------------------------------*/
36 #include "stm32l4xx_hal.h"
37 
47 #ifdef HAL_MODULE_ENABLED
48 
49 /* Private typedef -----------------------------------------------------------*/
50 /* Private define ------------------------------------------------------------*/
54 #define STM32L4XX_HAL_VERSION_MAIN (0x01U)
55 #define STM32L4XX_HAL_VERSION_SUB1 (0x0AU)
56 #define STM32L4XX_HAL_VERSION_SUB2 (0x00U)
57 #define STM32L4XX_HAL_VERSION_RC (0x00U)
58 #define STM32L4XX_HAL_VERSION ((STM32L4XX_HAL_VERSION_MAIN << 24U)\
59  |(STM32L4XX_HAL_VERSION_SUB1 << 16U)\
60  |(STM32L4XX_HAL_VERSION_SUB2 << 8U)\
61  |(STM32L4XX_HAL_VERSION_RC))
62 
63 #if defined(VREFBUF)
64 #define VREFBUF_TIMEOUT_VALUE 10U /* 10 ms (to be confirmed) */
65 #endif /* VREFBUF */
66 
67 /* ------------ SYSCFG registers bit address in the alias region ------------ */
68 #define SYSCFG_OFFSET (SYSCFG_BASE - PERIPH_BASE)
69 /* --- MEMRMP Register ---*/
70 /* Alias word address of FB_MODE bit */
71 #define MEMRMP_OFFSET SYSCFG_OFFSET
72 #define FB_MODE_BitNumber 8U
73 #define FB_MODE_BB (PERIPH_BB_BASE + (MEMRMP_OFFSET * 32U) + (FB_MODE_BitNumber * 4U))
74 
75 /* --- SCSR Register ---*/
76 /* Alias word address of SRAM2ER bit */
77 #define SCSR_OFFSET (SYSCFG_OFFSET + 0x18U)
78 #define BRER_BitNumber 0U
79 #define SCSR_SRAM2ER_BB (PERIPH_BB_BASE + (SCSR_OFFSET * 32U) + (BRER_BitNumber * 4U))
80 
81 /* Private macro -------------------------------------------------------------*/
82 /* Private variables ---------------------------------------------------------*/
83 /* Private function prototypes -----------------------------------------------*/
84 
85 /* Exported variables --------------------------------------------------------*/
86 
90 __IO uint32_t uwTick;
91 uint32_t uwTickPrio = (1UL << __NVIC_PRIO_BITS); /* Invalid priority */
92 uint32_t uwTickFreq = HAL_TICK_FREQ_DEFAULT; /* 1KHz */
97 /* Exported functions --------------------------------------------------------*/
98 
152 HAL_StatusTypeDef HAL_Init(void)
153 {
154  HAL_StatusTypeDef status = HAL_OK;
155 
156  /* Configure Flash prefetch, Instruction cache, Data cache */
157  /* Default configuration at reset is: */
158  /* - Prefetch disabled */
159  /* - Instruction cache enabled */
160  /* - Data cache enabled */
161 #if (INSTRUCTION_CACHE_ENABLE == 0)
162  __HAL_FLASH_INSTRUCTION_CACHE_DISABLE();
163 #endif /* INSTRUCTION_CACHE_ENABLE */
164 
165 #if (DATA_CACHE_ENABLE == 0)
166  __HAL_FLASH_DATA_CACHE_DISABLE();
167 #endif /* DATA_CACHE_ENABLE */
168 
169 #if (PREFETCH_ENABLE != 0)
170  __HAL_FLASH_PREFETCH_BUFFER_ENABLE();
171 #endif /* PREFETCH_ENABLE */
172 
173  /* Set Interrupt Group Priority */
174  HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);
175 
176  /* Use SysTick as time base source and configure 1ms tick (default clock after Reset is MSI) */
177  if (HAL_InitTick(TICK_INT_PRIORITY) != HAL_OK)
178  {
179  status = HAL_ERROR;
180  }
181  else
182  {
183  /* Init the low level hardware */
184  HAL_MspInit();
185  }
186 
187  /* Return function status */
188  return status;
189 }
190 
196 HAL_StatusTypeDef HAL_DeInit(void)
197 {
198  /* Reset of all peripherals */
199  __HAL_RCC_APB1_FORCE_RESET();
200  __HAL_RCC_APB1_RELEASE_RESET();
201 
202  __HAL_RCC_APB2_FORCE_RESET();
203  __HAL_RCC_APB2_RELEASE_RESET();
204 
205  __HAL_RCC_AHB1_FORCE_RESET();
206  __HAL_RCC_AHB1_RELEASE_RESET();
207 
208  __HAL_RCC_AHB2_FORCE_RESET();
209  __HAL_RCC_AHB2_RELEASE_RESET();
210 
211  __HAL_RCC_AHB3_FORCE_RESET();
212  __HAL_RCC_AHB3_RELEASE_RESET();
213 
214  /* De-Init the low level hardware */
215  HAL_MspDeInit();
216 
217  /* Return function status */
218  return HAL_OK;
219 }
220 
225 __weak void HAL_MspInit(void)
226 {
227  /* NOTE : This function should not be modified, when the callback is needed,
228  the HAL_MspInit could be implemented in the user file
229  */
230 }
231 
236 __weak void HAL_MspDeInit(void)
237 {
238  /* NOTE : This function should not be modified, when the callback is needed,
239  the HAL_MspDeInit could be implemented in the user file
240  */
241 }
242 
259 __weak HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
260 {
261  HAL_StatusTypeDef status = HAL_OK;
262 
263  if (uwTickFreq != 0U)
264  {
265  /*Configure the SysTick to have interrupt in 1ms time basis*/
266  if (HAL_SYSTICK_Config(SystemCoreClock / (1000U / uwTickFreq)) == 0U)
267  {
268  /* Configure the SysTick IRQ priority */
269  if (TickPriority < (1UL << __NVIC_PRIO_BITS))
270  {
271  HAL_NVIC_SetPriority(SysTick_IRQn, TickPriority, 0U);
272  uwTickPrio = TickPriority;
273  }
274  else
275  {
276  status = HAL_ERROR;
277  }
278  }
279  else
280  {
281  status = HAL_ERROR;
282  }
283  }
284  else
285  {
286  status = HAL_ERROR;
287  }
288 
289  /* Return function status */
290  return status;
291 }
292 
326 __weak void HAL_IncTick(void)
327 {
328  uwTick += uwTickFreq;
329 }
330 
337 __weak uint32_t HAL_GetTick(void)
338 {
339  return uwTick;
340 }
341 
346 uint32_t HAL_GetTickPrio(void)
347 {
348  return uwTickPrio;
349 }
350 
356 HAL_StatusTypeDef HAL_SetTickFreq(uint32_t Freq)
357 {
358  HAL_StatusTypeDef status = HAL_OK;
359  assert_param(IS_TICKFREQ(Freq));
360 
361  if (uwTickFreq != Freq)
362  {
363  /* Apply the new tick Freq */
364  status = HAL_InitTick(uwTickPrio);
365  if (status == HAL_OK)
366  {
367  uwTickFreq = Freq;
368  }
369  }
370 
371  return status;
372 }
373 
378 uint32_t HAL_GetTickFreq(void)
379 {
380  return uwTickFreq;
381 }
382 
394 __weak void HAL_Delay(uint32_t Delay)
395 {
396  uint32_t tickstart = HAL_GetTick();
397  uint32_t wait = Delay;
398 
399  /* Add a period to guaranty minimum wait */
400  if (wait < HAL_MAX_DELAY)
401  {
402  wait += (uint32_t)(uwTickFreq);
403  }
404 
405  while((HAL_GetTick() - tickstart) < wait)
406  {
407  }
408 }
409 
420 __weak void HAL_SuspendTick(void)
421 {
422  /* Disable SysTick Interrupt */
423  SysTick->CTRL &= ~SysTick_CTRL_TICKINT_Msk;
424 }
425 
436 __weak void HAL_ResumeTick(void)
437 {
438  /* Enable SysTick Interrupt */
439  SysTick->CTRL |= SysTick_CTRL_TICKINT_Msk;
440 }
441 
446 uint32_t HAL_GetHalVersion(void)
447 {
448  return STM32L4XX_HAL_VERSION;
449 }
450 
455 uint32_t HAL_GetREVID(void)
456 {
457  return((DBGMCU->IDCODE & DBGMCU_IDCODE_REV_ID) >> 16);
458 }
459 
464 uint32_t HAL_GetDEVID(void)
465 {
466  return(DBGMCU->IDCODE & DBGMCU_IDCODE_DEV_ID);
467 }
468 
473 uint32_t HAL_GetUIDw0(void)
474 {
475  return(READ_REG(*((uint32_t *)UID_BASE)));
476 }
477 
482 uint32_t HAL_GetUIDw1(void)
483 {
484  return(READ_REG(*((uint32_t *)(UID_BASE + 4U))));
485 }
486 
491 uint32_t HAL_GetUIDw2(void)
492 {
493  return(READ_REG(*((uint32_t *)(UID_BASE + 8U))));
494 }
495 
521 {
522  SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_SLEEP);
523 }
524 
530 {
531  CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_SLEEP);
532 }
533 
539 {
540  SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
541 }
542 
548 {
549  CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
550 }
551 
557 {
558  SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
559 }
560 
566 {
567  CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
568 }
569 
599 {
600  /* unlock the write protection of the SRAM2ER bit */
601  SYSCFG->SKR = 0xCA;
602  SYSCFG->SKR = 0x53;
603  /* Starts a hardware SRAM2 erase operation*/
604  *(__IO uint32_t *) SCSR_SRAM2ER_BB = 0x00000001UL;
605 }
606 
618 {
619  *(__IO uint32_t *)FB_MODE_BB = 0x00000001UL;
620 }
621 
633 {
634 
635  *(__IO uint32_t *)FB_MODE_BB = 0x00000000UL;
636 }
637 
638 #if defined(VREFBUF)
639 
649 void HAL_SYSCFG_VREFBUF_VoltageScalingConfig(uint32_t VoltageScaling)
650 {
651  /* Check the parameters */
652  assert_param(IS_SYSCFG_VREFBUF_VOLTAGE_SCALE(VoltageScaling));
653 
654  MODIFY_REG(VREFBUF->CSR, VREFBUF_CSR_VRS, VoltageScaling);
655 }
656 
666 {
667  /* Check the parameters */
668  assert_param(IS_SYSCFG_VREFBUF_HIGH_IMPEDANCE(Mode));
669 
670  MODIFY_REG(VREFBUF->CSR, VREFBUF_CSR_HIZ, Mode);
671 }
672 
677 void HAL_SYSCFG_VREFBUF_TrimmingConfig(uint32_t TrimmingValue)
678 {
679  /* Check the parameters */
680  assert_param(IS_SYSCFG_VREFBUF_TRIMMING(TrimmingValue));
681 
682  MODIFY_REG(VREFBUF->CCR, VREFBUF_CCR_TRIM, TrimmingValue);
683 }
684 
689 HAL_StatusTypeDef HAL_SYSCFG_EnableVREFBUF(void)
690 {
691  uint32_t tickstart;
692 
693  SET_BIT(VREFBUF->CSR, VREFBUF_CSR_ENVR);
694 
695  /* Get Start Tick*/
696  tickstart = HAL_GetTick();
697 
698  /* Wait for VRR bit */
699  while(READ_BIT(VREFBUF->CSR, VREFBUF_CSR_VRR) == 0U)
700  {
701  if((HAL_GetTick() - tickstart) > VREFBUF_TIMEOUT_VALUE)
702  {
703  return HAL_TIMEOUT;
704  }
705  }
706 
707  return HAL_OK;
708 }
709 
716 {
717  CLEAR_BIT(VREFBUF->CSR, VREFBUF_CSR_ENVR);
718 }
719 #endif /* VREFBUF */
720 
727 {
728  SET_BIT(SYSCFG->CFGR1, SYSCFG_CFGR1_BOOSTEN);
729 }
730 
737 {
738  CLEAR_BIT(SYSCFG->CFGR1, SYSCFG_CFGR1_BOOSTEN);
739 }
740 
749 #endif /* HAL_MODULE_ENABLED */
750 
758 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
uint32_t HAL_GetTickPrio(void)
This function returns a tick priority.
void HAL_SYSCFG_VREFBUF_VoltageScalingConfig(uint32_t VoltageScaling)
Configure the internal voltage reference buffer voltage scale.
void HAL_DBGMCU_DisableDBGSleepMode(void)
Disable the Debug Module during SLEEP mode.
void HAL_DBGMCU_DisableDBGStopMode(void)
Disable the Debug Module during STOP0/STOP1/STOP2 modes.
void HAL_SYSCFG_DisableMemorySwappingBank(void)
Disable the Internal FLASH Bank Swapping.
void HAL_DBGMCU_EnableDBGStopMode(void)
Enable the Debug Module during STOP0/STOP1/STOP2 modes.
void HAL_SuspendTick(void)
Suspend Tick increment.
void HAL_SYSCFG_EnableIOAnalogSwitchBooster(void)
Enable the I/O analog switch voltage booster.
void HAL_MspDeInit(void)
DeInitialize the MSP.
This file contains all the functions prototypes for the HAL module driver.
HAL_StatusTypeDef HAL_DeInit(void)
De-initialize common part of the HAL and stop the source of time base.
void HAL_DBGMCU_DisableDBGStandbyMode(void)
Disable the Debug Module during STANDBY mode.
void HAL_SYSCFG_VREFBUF_HighImpedanceConfig(uint32_t Mode)
Configure the internal voltage reference buffer high impedance mode.
void HAL_Delay(uint32_t Delay)
This function provides minimum delay (in milliseconds) based on variable incremented.
uint32_t HAL_SYSTICK_Config(uint32_t TicksNumb)
Initialize the System Timer with interrupt enabled and start the System Tick Timer (SysTick): Counter...
uint32_t HAL_GetTick(void)
Provide a tick value in millisecond.
CLEAR_BIT(hrtc->Instance->CR, RTC_CR_WUTE)
void HAL_DBGMCU_EnableDBGStandbyMode(void)
Enable the Debug Module during STANDBY mode.
HAL_StatusTypeDef HAL_SetTickFreq(uint32_t Freq)
Set new tick Freq.
void HAL_DBGMCU_EnableDBGSleepMode(void)
Enable the Debug Module during SLEEP mode.
uint32_t HAL_GetHalVersion(void)
Return the HAL revision.
return HAL_OK
void HAL_IncTick(void)
This function is called to increment a global variable "uwTick" used as application time base...
uint32_t HAL_GetUIDw2(void)
Return the third word of the unique device identifier (UID based on 96 bits)
uint32_t HAL_GetREVID(void)
Return the device revision identifier.
uint32_t HAL_GetDEVID(void)
Return the device identifier.
void HAL_MspInit(void)
Initialize the MSP.
void HAL_SYSCFG_DisableVREFBUF(void)
Disable the Internal Voltage Reference buffer (VREFBUF).
HAL_StatusTypeDef HAL_Init(void)
Configure the Flash prefetch, the Instruction and Data caches, the time base source, NVIC and any required global low level hardware by calling the HAL_MspInit() callback function to be optionally defined in user file stm32l4xx_hal_msp.c.
uint32_t HAL_GetUIDw0(void)
Return the first word of the unique device identifier (UID based on 96 bits)
__IO uint32_t uwTick
Definition: stm32l4xx_hal.c:90
HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
This function configures the source of the time base: The time source is configured to have 1ms time ...
void HAL_SYSCFG_VREFBUF_TrimmingConfig(uint32_t TrimmingValue)
Tune the Internal Voltage Reference buffer (VREFBUF).
uint32_t HAL_GetUIDw1(void)
Return the second word of the unique device identifier (UID based on 96 bits)
void HAL_NVIC_SetPriority(IRQn_Type IRQn, uint32_t PreemptPriority, uint32_t SubPriority)
Set the priority of an interrupt.
HAL_StatusTypeDef HAL_SYSCFG_EnableVREFBUF(void)
Enable the Internal Voltage Reference buffer (VREFBUF).
MODIFY_REG(hrtc->Instance->CR, RTC_CR_WUCKSEL,(uint32_t) WakeUpClock)
void HAL_SYSCFG_DisableIOAnalogSwitchBooster(void)
Disable the I/O analog switch voltage booster.
void HAL_NVIC_SetPriorityGrouping(uint32_t PriorityGroup)
Set the priority grouping field (pre-emption priority and subpriority) using the required unlock sequ...
void HAL_SYSCFG_SRAM2Erase(void)
Start a hardware SRAM2 erase operation.
uint32_t uwTickFreq
Definition: stm32l4xx_hal.c:92
void HAL_ResumeTick(void)
Resume Tick increment.
uint32_t uwTickPrio
Definition: stm32l4xx_hal.c:91
uint32_t HAL_GetTickFreq(void)
Return tick frequency.
assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock))
void HAL_SYSCFG_EnableMemorySwappingBank(void)
Enable the Internal FLASH Bank Swapping.