Implement a secure ICS protocol targeting LoRa Node151 microcontroller for controlling irrigation.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

607 lines
20 KiB

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