firmware-nucleo/Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal.c (20257B)
1 /** 2 ****************************************************************************** 3 * @file stm32f1xx_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) 2016 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 "stm32f1xx_hal.h" 37 38 /** @addtogroup STM32F1xx_HAL_Driver 39 * @{ 40 */ 41 42 /** @defgroup HAL 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 STM32F1xx HAL Driver version number 57 */ 58 #define __STM32F1xx_HAL_VERSION_MAIN (0x01U) /*!< [31:24] main version */ 59 #define __STM32F1xx_HAL_VERSION_SUB1 (0x01U) /*!< [23:16] sub1 version */ 60 #define __STM32F1xx_HAL_VERSION_SUB2 (0x0AU) /*!< [15:8] sub2 version */ 61 #define __STM32F1xx_HAL_VERSION_RC (0x00U) /*!< [7:0] release candidate */ 62 #define __STM32F1xx_HAL_VERSION ((__STM32F1xx_HAL_VERSION_MAIN << 24)\ 63 |(__STM32F1xx_HAL_VERSION_SUB1 << 16)\ 64 |(__STM32F1xx_HAL_VERSION_SUB2 << 8 )\ 65 |(__STM32F1xx_HAL_VERSION_RC)) 66 67 #define IDCODE_DEVID_MASK 0x00000FFFU 68 69 /** 70 * @} 71 */ 72 73 /* Private macro -------------------------------------------------------------*/ 74 /* Private variables ---------------------------------------------------------*/ 75 76 /** @defgroup HAL_Private_Variables HAL Private Variables 77 * @{ 78 */ 79 __IO uint32_t uwTick; 80 uint32_t uwTickPrio = (1UL << __NVIC_PRIO_BITS); /* Invalid PRIO */ 81 HAL_TickFreqTypeDef uwTickFreq = HAL_TICK_FREQ_DEFAULT; /* 1KHz */ 82 /** 83 * @} 84 */ 85 /* Private function prototypes -----------------------------------------------*/ 86 /* Exported functions ---------------------------------------------------------*/ 87 88 /** @defgroup HAL_Exported_Functions HAL Exported Functions 89 * @{ 90 */ 91 92 /** @defgroup HAL_Exported_Functions_Group1 Initialization and de-initialization Functions 93 * @brief Initialization and de-initialization functions 94 * 95 @verbatim 96 =============================================================================== 97 ##### Initialization and de-initialization functions ##### 98 =============================================================================== 99 [..] This section provides functions allowing to: 100 (+) Initializes the Flash interface, the NVIC allocation and initial clock 101 configuration. It initializes the systick also when timeout is needed 102 and the backup domain when enabled. 103 (+) de-Initializes common part of the HAL. 104 (+) Configure The time base source to have 1ms time base with a dedicated 105 Tick interrupt priority. 106 (++) SysTick timer is used by default as source of time base, but user 107 can eventually implement his proper time base source (a general purpose 108 timer for example or other time source), keeping in mind that Time base 109 duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and 110 handled in milliseconds basis. 111 (++) Time base configuration function (HAL_InitTick ()) is called automatically 112 at the beginning of the program after reset by HAL_Init() or at any time 113 when clock is configured, by HAL_RCC_ClockConfig(). 114 (++) Source of time base is configured to generate interrupts at regular 115 time intervals. Care must be taken if HAL_Delay() is called from a 116 peripheral ISR process, the Tick interrupt line must have higher priority 117 (numerically lower) than the peripheral interrupt. Otherwise the caller 118 ISR process will be blocked. 119 (++) functions affecting time base configurations are declared as __weak 120 to make override possible in case of other implementations in user file. 121 @endverbatim 122 * @{ 123 */ 124 125 /** 126 * @brief This function is used to initialize the HAL Library; it must be the first 127 * instruction to be executed in the main program (before to call any other 128 * HAL function), it performs the following: 129 * Configure the Flash prefetch. 130 * Configures the SysTick to generate an interrupt each 1 millisecond, 131 * which is clocked by the HSI (at this stage, the clock is not yet 132 * configured and thus the system is running from the internal HSI at 16 MHz). 133 * Set NVIC Group Priority to 4. 134 * Calls the HAL_MspInit() callback function defined in user file 135 * "stm32f1xx_hal_msp.c" to do the global low level hardware initialization 136 * 137 * @note SysTick is used as time base for the HAL_Delay() function, the application 138 * need to ensure that the SysTick time base is always set to 1 millisecond 139 * to have correct HAL operation. 140 * @retval HAL status 141 */ 142 HAL_StatusTypeDef HAL_Init(void) 143 { 144 /* Configure Flash prefetch */ 145 #if (PREFETCH_ENABLE != 0) 146 #if defined(STM32F101x6) || defined(STM32F101xB) || defined(STM32F101xE) || defined(STM32F101xG) || \ 147 defined(STM32F102x6) || defined(STM32F102xB) || \ 148 defined(STM32F103x6) || defined(STM32F103xB) || defined(STM32F103xE) || defined(STM32F103xG) || \ 149 defined(STM32F105xC) || defined(STM32F107xC) 150 151 /* Prefetch buffer is not available on value line devices */ 152 __HAL_FLASH_PREFETCH_BUFFER_ENABLE(); 153 #endif 154 #endif /* PREFETCH_ENABLE */ 155 156 /* Set Interrupt Group Priority */ 157 HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4); 158 159 /* Use systick as time base source and configure 1ms tick (default clock after Reset is HSI) */ 160 HAL_InitTick(TICK_INT_PRIORITY); 161 162 /* Init the low level hardware */ 163 HAL_MspInit(); 164 165 /* Return function status */ 166 return HAL_OK; 167 } 168 169 /** 170 * @brief This function de-Initializes common part of the HAL and stops the systick. 171 * of time base. 172 * @note This function is optional. 173 * @retval HAL status 174 */ 175 HAL_StatusTypeDef HAL_DeInit(void) 176 { 177 /* Reset of all peripherals */ 178 __HAL_RCC_APB1_FORCE_RESET(); 179 __HAL_RCC_APB1_RELEASE_RESET(); 180 181 __HAL_RCC_APB2_FORCE_RESET(); 182 __HAL_RCC_APB2_RELEASE_RESET(); 183 184 #if defined(STM32F105xC) || defined(STM32F107xC) 185 __HAL_RCC_AHB_FORCE_RESET(); 186 __HAL_RCC_AHB_RELEASE_RESET(); 187 #endif 188 189 /* De-Init the low level hardware */ 190 HAL_MspDeInit(); 191 192 /* Return function status */ 193 return HAL_OK; 194 } 195 196 /** 197 * @brief Initialize the MSP. 198 * @retval None 199 */ 200 __weak void HAL_MspInit(void) 201 { 202 /* NOTE : This function should not be modified, when the callback is needed, 203 the HAL_MspInit could be implemented in the user file 204 */ 205 } 206 207 /** 208 * @brief DeInitializes the MSP. 209 * @retval None 210 */ 211 __weak void HAL_MspDeInit(void) 212 { 213 /* NOTE : This function should not be modified, when the callback is needed, 214 the HAL_MspDeInit could be implemented in the user file 215 */ 216 } 217 218 /** 219 * @brief This function configures the source of the time base. 220 * The time source is configured to have 1ms time base with a dedicated 221 * Tick interrupt priority. 222 * @note This function is called automatically at the beginning of program after 223 * reset by HAL_Init() or at any time when clock is reconfigured by HAL_RCC_ClockConfig(). 224 * @note In the default implementation, SysTick timer is the source of time base. 225 * It is used to generate interrupts at regular time intervals. 226 * Care must be taken if HAL_Delay() is called from a peripheral ISR process, 227 * The SysTick interrupt must have higher priority (numerically lower) 228 * than the peripheral interrupt. Otherwise the caller ISR process will be blocked. 229 * The function is declared as __weak to be overwritten in case of other 230 * implementation in user file. 231 * @param TickPriority Tick interrupt priority. 232 * @retval HAL status 233 */ 234 __weak HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority) 235 { 236 /* Configure the SysTick to have interrupt in 1ms time basis*/ 237 if (HAL_SYSTICK_Config(SystemCoreClock / (1000U / uwTickFreq)) > 0U) 238 { 239 return HAL_ERROR; 240 } 241 242 /* Configure the SysTick IRQ priority */ 243 if (TickPriority < (1UL << __NVIC_PRIO_BITS)) 244 { 245 HAL_NVIC_SetPriority(SysTick_IRQn, TickPriority, 0U); 246 uwTickPrio = TickPriority; 247 } 248 else 249 { 250 return HAL_ERROR; 251 } 252 253 /* Return function status */ 254 return HAL_OK; 255 } 256 257 /** 258 * @} 259 */ 260 261 /** @defgroup HAL_Exported_Functions_Group2 HAL Control functions 262 * @brief HAL Control functions 263 * 264 @verbatim 265 =============================================================================== 266 ##### HAL Control functions ##### 267 =============================================================================== 268 [..] This section provides functions allowing to: 269 (+) Provide a tick value in millisecond 270 (+) Provide a blocking delay in millisecond 271 (+) Suspend the time base source interrupt 272 (+) Resume the time base source interrupt 273 (+) Get the HAL API driver version 274 (+) Get the device identifier 275 (+) Get the device revision identifier 276 (+) Enable/Disable Debug module during SLEEP mode 277 (+) Enable/Disable Debug module during STOP mode 278 (+) Enable/Disable Debug module during STANDBY mode 279 280 @endverbatim 281 * @{ 282 */ 283 284 /** 285 * @brief This function is called to increment a global variable "uwTick" 286 * used as application time base. 287 * @note In the default implementation, this variable is incremented each 1ms 288 * in SysTick ISR. 289 * @note This function is declared as __weak to be overwritten in case of other 290 * implementations in user file. 291 * @retval None 292 */ 293 __weak void HAL_IncTick(void) 294 { 295 uwTick += uwTickFreq; 296 } 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 /** 310 * @brief This function returns a tick priority. 311 * @retval tick priority 312 */ 313 uint32_t HAL_GetTickPrio(void) 314 { 315 return uwTickPrio; 316 } 317 318 /** 319 * @brief Set new tick Freq. 320 * @retval status 321 */ 322 HAL_StatusTypeDef HAL_SetTickFreq(HAL_TickFreqTypeDef Freq) 323 { 324 HAL_StatusTypeDef status = HAL_OK; 325 HAL_TickFreqTypeDef prevTickFreq; 326 327 assert_param(IS_TICKFREQ(Freq)); 328 329 if (uwTickFreq != Freq) 330 { 331 /* Back up uwTickFreq frequency */ 332 prevTickFreq = uwTickFreq; 333 334 /* Update uwTickFreq global variable used by HAL_InitTick() */ 335 uwTickFreq = Freq; 336 337 /* Apply the new tick Freq */ 338 status = HAL_InitTick(uwTickPrio); 339 340 if (status != HAL_OK) 341 { 342 /* Restore previous tick frequency */ 343 uwTickFreq = prevTickFreq; 344 } 345 } 346 347 return status; 348 } 349 350 /** 351 * @brief Return tick frequency. 352 * @retval Tick frequency. 353 * Value of @ref HAL_TickFreqTypeDef. 354 */ 355 HAL_TickFreqTypeDef HAL_GetTickFreq(void) 356 { 357 return uwTickFreq; 358 } 359 360 /** 361 * @brief This function provides minimum delay (in milliseconds) based 362 * on variable incremented. 363 * @note In the default implementation , SysTick timer is the source of time base. 364 * It is used to generate interrupts at regular time intervals where uwTick 365 * is incremented. 366 * @note This function is declared as __weak to be overwritten in case of other 367 * implementations in user file. 368 * @param Delay specifies the delay time length, in milliseconds. 369 * @retval None 370 */ 371 __weak void HAL_Delay(uint32_t Delay) 372 { 373 uint32_t tickstart = HAL_GetTick(); 374 uint32_t wait = Delay; 375 376 /* Add a freq to guarantee minimum wait */ 377 if (wait < HAL_MAX_DELAY) 378 { 379 wait += (uint32_t)(uwTickFreq); 380 } 381 382 while ((HAL_GetTick() - tickstart) < wait) 383 { 384 } 385 } 386 387 /** 388 * @brief Suspend Tick increment. 389 * @note In the default implementation , SysTick timer is the source of time base. It is 390 * used to generate interrupts at regular time intervals. Once HAL_SuspendTick() 391 * is called, the SysTick interrupt will be disabled and so Tick increment 392 * is suspended. 393 * @note This function is declared as __weak to be overwritten in case of other 394 * implementations in user file. 395 * @retval None 396 */ 397 __weak void HAL_SuspendTick(void) 398 { 399 /* Disable SysTick Interrupt */ 400 CLEAR_BIT(SysTick->CTRL, SysTick_CTRL_TICKINT_Msk); 401 } 402 403 /** 404 * @brief Resume Tick increment. 405 * @note In the default implementation , SysTick timer is the source of time base. It is 406 * used to generate interrupts at regular time intervals. Once HAL_ResumeTick() 407 * is called, the SysTick interrupt will be enabled and so Tick increment 408 * is resumed. 409 * @note This function is declared as __weak to be overwritten in case of other 410 * implementations in user file. 411 * @retval None 412 */ 413 __weak void HAL_ResumeTick(void) 414 { 415 /* Enable SysTick Interrupt */ 416 SET_BIT(SysTick->CTRL, SysTick_CTRL_TICKINT_Msk); 417 } 418 419 /** 420 * @brief Returns the HAL revision 421 * @retval version 0xXYZR (8bits for each decimal, R for RC) 422 */ 423 uint32_t HAL_GetHalVersion(void) 424 { 425 return __STM32F1xx_HAL_VERSION; 426 } 427 428 /** 429 * @brief Returns the device revision identifier. 430 * Note: On devices STM32F10xx8 and STM32F10xxB, 431 * STM32F101xC/D/E and STM32F103xC/D/E, 432 * STM32F101xF/G and STM32F103xF/G 433 * STM32F10xx4 and STM32F10xx6 434 * Debug registers DBGMCU_IDCODE and DBGMCU_CR are accessible only in 435 * debug mode (not accessible by the user software in normal mode). 436 * Refer to errata sheet of these devices for more details. 437 * @retval Device revision identifier 438 */ 439 uint32_t HAL_GetREVID(void) 440 { 441 return ((DBGMCU->IDCODE) >> DBGMCU_IDCODE_REV_ID_Pos); 442 } 443 444 /** 445 * @brief Returns the device identifier. 446 * Note: On devices STM32F10xx8 and STM32F10xxB, 447 * STM32F101xC/D/E and STM32F103xC/D/E, 448 * STM32F101xF/G and STM32F103xF/G 449 * STM32F10xx4 and STM32F10xx6 450 * Debug registers DBGMCU_IDCODE and DBGMCU_CR are accessible only in 451 * debug mode (not accessible by the user software in normal mode). 452 * Refer to errata sheet of these devices for more details. 453 * @retval Device identifier 454 */ 455 uint32_t HAL_GetDEVID(void) 456 { 457 return ((DBGMCU->IDCODE) & IDCODE_DEVID_MASK); 458 } 459 460 /** 461 * @brief Returns first word of the unique device identifier (UID based on 96 bits) 462 * @retval Device identifier 463 */ 464 uint32_t HAL_GetUIDw0(void) 465 { 466 return(READ_REG(*((uint32_t *)UID_BASE))); 467 } 468 469 /** 470 * @brief Returns second word of the unique device identifier (UID based on 96 bits) 471 * @retval Device identifier 472 */ 473 uint32_t HAL_GetUIDw1(void) 474 { 475 return(READ_REG(*((uint32_t *)(UID_BASE + 4U)))); 476 } 477 478 /** 479 * @brief Returns third word of the unique device identifier (UID based on 96 bits) 480 * @retval Device identifier 481 */ 482 uint32_t HAL_GetUIDw2(void) 483 { 484 return(READ_REG(*((uint32_t *)(UID_BASE + 8U)))); 485 } 486 487 /** 488 * @brief Enable the Debug Module during SLEEP mode 489 * @retval None 490 */ 491 void HAL_DBGMCU_EnableDBGSleepMode(void) 492 { 493 SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_SLEEP); 494 } 495 496 /** 497 * @brief Disable the Debug Module during SLEEP mode 498 * Note: On devices STM32F10xx8 and STM32F10xxB, 499 * STM32F101xC/D/E and STM32F103xC/D/E, 500 * STM32F101xF/G and STM32F103xF/G 501 * STM32F10xx4 and STM32F10xx6 502 * Debug registers DBGMCU_IDCODE and DBGMCU_CR are accessible only in 503 * debug mode (not accessible by the user software in normal mode). 504 * Refer to errata sheet of these devices for more details. 505 * @retval None 506 */ 507 void HAL_DBGMCU_DisableDBGSleepMode(void) 508 { 509 CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_SLEEP); 510 } 511 512 /** 513 * @brief Enable the Debug Module during STOP mode 514 * Note: On devices STM32F10xx8 and STM32F10xxB, 515 * STM32F101xC/D/E and STM32F103xC/D/E, 516 * STM32F101xF/G and STM32F103xF/G 517 * STM32F10xx4 and STM32F10xx6 518 * Debug registers DBGMCU_IDCODE and DBGMCU_CR are accessible only in 519 * debug mode (not accessible by the user software in normal mode). 520 * Refer to errata sheet of these devices for more details. 521 * Note: On all STM32F1 devices: 522 * If the system tick timer interrupt is enabled during the Stop mode 523 * debug (DBG_STOP bit set in the DBGMCU_CR register ), it will wakeup 524 * the system from Stop mode. 525 * Workaround: To debug the Stop mode, disable the system tick timer 526 * interrupt. 527 * Refer to errata sheet of these devices for more details. 528 * Note: On all STM32F1 devices: 529 * If the system tick timer interrupt is enabled during the Stop mode 530 * debug (DBG_STOP bit set in the DBGMCU_CR register ), it will wakeup 531 * the system from Stop mode. 532 * Workaround: To debug the Stop mode, disable the system tick timer 533 * interrupt. 534 * Refer to errata sheet of these devices for more details. 535 * @retval None 536 */ 537 void HAL_DBGMCU_EnableDBGStopMode(void) 538 { 539 SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP); 540 } 541 542 /** 543 * @brief Disable the Debug Module during STOP mode 544 * Note: On devices STM32F10xx8 and STM32F10xxB, 545 * STM32F101xC/D/E and STM32F103xC/D/E, 546 * STM32F101xF/G and STM32F103xF/G 547 * STM32F10xx4 and STM32F10xx6 548 * Debug registers DBGMCU_IDCODE and DBGMCU_CR are accessible only in 549 * debug mode (not accessible by the user software in normal mode). 550 * Refer to errata sheet of these devices for more details. 551 * @retval None 552 */ 553 void HAL_DBGMCU_DisableDBGStopMode(void) 554 { 555 CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP); 556 } 557 558 /** 559 * @brief Enable the Debug Module during STANDBY mode 560 * Note: On devices STM32F10xx8 and STM32F10xxB, 561 * STM32F101xC/D/E and STM32F103xC/D/E, 562 * STM32F101xF/G and STM32F103xF/G 563 * STM32F10xx4 and STM32F10xx6 564 * Debug registers DBGMCU_IDCODE and DBGMCU_CR are accessible only in 565 * debug mode (not accessible by the user software in normal mode). 566 * Refer to errata sheet of these devices for more details. 567 * @retval None 568 */ 569 void HAL_DBGMCU_EnableDBGStandbyMode(void) 570 { 571 SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY); 572 } 573 574 /** 575 * @brief Disable the Debug Module during STANDBY mode 576 * Note: On devices STM32F10xx8 and STM32F10xxB, 577 * STM32F101xC/D/E and STM32F103xC/D/E, 578 * STM32F101xF/G and STM32F103xF/G 579 * STM32F10xx4 and STM32F10xx6 580 * Debug registers DBGMCU_IDCODE and DBGMCU_CR are accessible only in 581 * debug mode (not accessible by the user software in normal mode). 582 * Refer to errata sheet of these devices for more details. 583 * @retval None 584 */ 585 void HAL_DBGMCU_DisableDBGStandbyMode(void) 586 { 587 CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY); 588 } 589 590 /** 591 * @} 592 */ 593 594 /** 595 * @} 596 */ 597 598 #endif /* HAL_MODULE_ENABLED */ 599 /** 600 * @} 601 */ 602 603 /** 604 * @} 605 */ 606 607
