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.
 
 
 
 
 
 

409 lines
14 KiB

  1. /**
  2. ******************************************************************************
  3. * @file system_stm32f1xx.c
  4. * @author MCD Application Team
  5. * @brief CMSIS Cortex-M3 Device Peripheral Access Layer System Source File.
  6. *
  7. * 1. This file provides two functions and one global variable to be called from
  8. * user application:
  9. * - SystemInit(): Setups the system clock (System clock source, PLL Multiplier
  10. * factors, AHB/APBx prescalers and Flash settings).
  11. * This function is called at startup just after reset and
  12. * before branch to main program. This call is made inside
  13. * the "startup_stm32f1xx_xx.s" file.
  14. *
  15. * - SystemCoreClock variable: Contains the core clock (HCLK), it can be used
  16. * by the user application to setup the SysTick
  17. * timer or configure other parameters.
  18. *
  19. * - SystemCoreClockUpdate(): Updates the variable SystemCoreClock and must
  20. * be called whenever the core clock is changed
  21. * during program execution.
  22. *
  23. * 2. After each device reset the HSI (8 MHz) is used as system clock source.
  24. * Then SystemInit() function is called, in "startup_stm32f1xx_xx.s" file, to
  25. * configure the system clock before to branch to main program.
  26. *
  27. * 4. The default value of HSE crystal is set to 8 MHz (or 25 MHz, depending on
  28. * the product used), refer to "HSE_VALUE".
  29. * When HSE is used as system clock source, directly or through PLL, and you
  30. * are using different crystal you have to adapt the HSE value to your own
  31. * configuration.
  32. *
  33. ******************************************************************************
  34. * @attention
  35. *
  36. * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
  37. * All rights reserved.</center></h2>
  38. *
  39. * This software component is licensed by ST under BSD 3-Clause license,
  40. * the "License"; You may not use this file except in compliance with the
  41. * License. You may obtain a copy of the License at:
  42. * opensource.org/licenses/BSD-3-Clause
  43. *
  44. ******************************************************************************
  45. */
  46. /** @addtogroup CMSIS
  47. * @{
  48. */
  49. /** @addtogroup stm32f1xx_system
  50. * @{
  51. */
  52. /** @addtogroup STM32F1xx_System_Private_Includes
  53. * @{
  54. */
  55. #include "stm32f1xx.h"
  56. /**
  57. * @}
  58. */
  59. /** @addtogroup STM32F1xx_System_Private_TypesDefinitions
  60. * @{
  61. */
  62. /**
  63. * @}
  64. */
  65. /** @addtogroup STM32F1xx_System_Private_Defines
  66. * @{
  67. */
  68. #if !defined (HSE_VALUE)
  69. #define HSE_VALUE 8000000U /*!< Default value of the External oscillator in Hz.
  70. This value can be provided and adapted by the user application. */
  71. #endif /* HSE_VALUE */
  72. #if !defined (HSI_VALUE)
  73. #define HSI_VALUE 8000000U /*!< Default value of the Internal oscillator in Hz.
  74. This value can be provided and adapted by the user application. */
  75. #endif /* HSI_VALUE */
  76. /*!< Uncomment the following line if you need to use external SRAM */
  77. #if defined(STM32F100xE) || defined(STM32F101xE) || defined(STM32F101xG) || defined(STM32F103xE) || defined(STM32F103xG)
  78. /* #define DATA_IN_ExtSRAM */
  79. #endif /* STM32F100xE || STM32F101xE || STM32F101xG || STM32F103xE || STM32F103xG */
  80. /* Note: Following vector table addresses must be defined in line with linker
  81. configuration. */
  82. /*!< Uncomment the following line if you need to relocate the vector table
  83. anywhere in Flash or Sram, else the vector table is kept at the automatic
  84. remap of boot address selected */
  85. /* #define USER_VECT_TAB_ADDRESS */
  86. #if defined(USER_VECT_TAB_ADDRESS)
  87. /*!< Uncomment the following line if you need to relocate your vector Table
  88. in Sram else user remap will be done in Flash. */
  89. /* #define VECT_TAB_SRAM */
  90. #if defined(VECT_TAB_SRAM)
  91. #define VECT_TAB_BASE_ADDRESS SRAM_BASE /*!< Vector Table base address field.
  92. This value must be a multiple of 0x200. */
  93. #define VECT_TAB_OFFSET 0x00000000U /*!< Vector Table base offset field.
  94. This value must be a multiple of 0x200. */
  95. #else
  96. #define VECT_TAB_BASE_ADDRESS FLASH_BASE /*!< Vector Table base address field.
  97. This value must be a multiple of 0x200. */
  98. #define VECT_TAB_OFFSET 0x00000000U /*!< Vector Table base offset field.
  99. This value must be a multiple of 0x200. */
  100. #endif /* VECT_TAB_SRAM */
  101. #endif /* USER_VECT_TAB_ADDRESS */
  102. /******************************************************************************/
  103. /**
  104. * @}
  105. */
  106. /** @addtogroup STM32F1xx_System_Private_Macros
  107. * @{
  108. */
  109. /**
  110. * @}
  111. */
  112. /** @addtogroup STM32F1xx_System_Private_Variables
  113. * @{
  114. */
  115. /* This variable is updated in three ways:
  116. 1) by calling CMSIS function SystemCoreClockUpdate()
  117. 2) by calling HAL API function HAL_RCC_GetHCLKFreq()
  118. 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
  119. Note: If you use this function to configure the system clock; then there
  120. is no need to call the 2 first functions listed above, since SystemCoreClock
  121. variable is updated automatically.
  122. */
  123. uint32_t SystemCoreClock = 16000000;
  124. const uint8_t AHBPrescTable[16U] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
  125. const uint8_t APBPrescTable[8U] = {0, 0, 0, 0, 1, 2, 3, 4};
  126. /**
  127. * @}
  128. */
  129. /** @addtogroup STM32F1xx_System_Private_FunctionPrototypes
  130. * @{
  131. */
  132. #if defined(STM32F100xE) || defined(STM32F101xE) || defined(STM32F101xG) || defined(STM32F103xE) || defined(STM32F103xG)
  133. #ifdef DATA_IN_ExtSRAM
  134. static void SystemInit_ExtMemCtl(void);
  135. #endif /* DATA_IN_ExtSRAM */
  136. #endif /* STM32F100xE || STM32F101xE || STM32F101xG || STM32F103xE || STM32F103xG */
  137. /**
  138. * @}
  139. */
  140. /** @addtogroup STM32F1xx_System_Private_Functions
  141. * @{
  142. */
  143. /**
  144. * @brief Setup the microcontroller system
  145. * Initialize the Embedded Flash Interface, the PLL and update the
  146. * SystemCoreClock variable.
  147. * @note This function should be used only after reset.
  148. * @param None
  149. * @retval None
  150. */
  151. void SystemInit (void)
  152. {
  153. #if defined(STM32F100xE) || defined(STM32F101xE) || defined(STM32F101xG) || defined(STM32F103xE) || defined(STM32F103xG)
  154. #ifdef DATA_IN_ExtSRAM
  155. SystemInit_ExtMemCtl();
  156. #endif /* DATA_IN_ExtSRAM */
  157. #endif
  158. /* Configure the Vector Table location -------------------------------------*/
  159. #if defined(USER_VECT_TAB_ADDRESS)
  160. SCB->VTOR = VECT_TAB_BASE_ADDRESS | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM. */
  161. #endif /* USER_VECT_TAB_ADDRESS */
  162. }
  163. /**
  164. * @brief Update SystemCoreClock variable according to Clock Register Values.
  165. * The SystemCoreClock variable contains the core clock (HCLK), it can
  166. * be used by the user application to setup the SysTick timer or configure
  167. * other parameters.
  168. *
  169. * @note Each time the core clock (HCLK) changes, this function must be called
  170. * to update SystemCoreClock variable value. Otherwise, any configuration
  171. * based on this variable will be incorrect.
  172. *
  173. * @note - The system frequency computed by this function is not the real
  174. * frequency in the chip. It is calculated based on the predefined
  175. * constant and the selected clock source:
  176. *
  177. * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(*)
  178. *
  179. * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(**)
  180. *
  181. * - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(**)
  182. * or HSI_VALUE(*) multiplied by the PLL factors.
  183. *
  184. * (*) HSI_VALUE is a constant defined in stm32f1xx.h file (default value
  185. * 8 MHz) but the real value may vary depending on the variations
  186. * in voltage and temperature.
  187. *
  188. * (**) HSE_VALUE is a constant defined in stm32f1xx.h file (default value
  189. * 8 MHz or 25 MHz, depending on the product used), user has to ensure
  190. * that HSE_VALUE is same as the real frequency of the crystal used.
  191. * Otherwise, this function may have wrong result.
  192. *
  193. * - The result of this function could be not correct when using fractional
  194. * value for HSE crystal.
  195. * @param None
  196. * @retval None
  197. */
  198. void SystemCoreClockUpdate (void)
  199. {
  200. uint32_t tmp = 0U, pllmull = 0U, pllsource = 0U;
  201. #if defined(STM32F105xC) || defined(STM32F107xC)
  202. uint32_t prediv1source = 0U, prediv1factor = 0U, prediv2factor = 0U, pll2mull = 0U;
  203. #endif /* STM32F105xC */
  204. #if defined(STM32F100xB) || defined(STM32F100xE)
  205. uint32_t prediv1factor = 0U;
  206. #endif /* STM32F100xB or STM32F100xE */
  207. /* Get SYSCLK source -------------------------------------------------------*/
  208. tmp = RCC->CFGR & RCC_CFGR_SWS;
  209. switch (tmp)
  210. {
  211. case 0x00U: /* HSI used as system clock */
  212. SystemCoreClock = HSI_VALUE;
  213. break;
  214. case 0x04U: /* HSE used as system clock */
  215. SystemCoreClock = HSE_VALUE;
  216. break;
  217. case 0x08U: /* PLL used as system clock */
  218. /* Get PLL clock source and multiplication factor ----------------------*/
  219. pllmull = RCC->CFGR & RCC_CFGR_PLLMULL;
  220. pllsource = RCC->CFGR & RCC_CFGR_PLLSRC;
  221. #if !defined(STM32F105xC) && !defined(STM32F107xC)
  222. pllmull = ( pllmull >> 18U) + 2U;
  223. if (pllsource == 0x00U)
  224. {
  225. /* HSI oscillator clock divided by 2 selected as PLL clock entry */
  226. SystemCoreClock = (HSI_VALUE >> 1U) * pllmull;
  227. }
  228. else
  229. {
  230. #if defined(STM32F100xB) || defined(STM32F100xE)
  231. prediv1factor = (RCC->CFGR2 & RCC_CFGR2_PREDIV1) + 1U;
  232. /* HSE oscillator clock selected as PREDIV1 clock entry */
  233. SystemCoreClock = (HSE_VALUE / prediv1factor) * pllmull;
  234. #else
  235. /* HSE selected as PLL clock entry */
  236. if ((RCC->CFGR & RCC_CFGR_PLLXTPRE) != (uint32_t)RESET)
  237. {/* HSE oscillator clock divided by 2 */
  238. SystemCoreClock = (HSE_VALUE >> 1U) * pllmull;
  239. }
  240. else
  241. {
  242. SystemCoreClock = HSE_VALUE * pllmull;
  243. }
  244. #endif
  245. }
  246. #else
  247. pllmull = pllmull >> 18U;
  248. if (pllmull != 0x0DU)
  249. {
  250. pllmull += 2U;
  251. }
  252. else
  253. { /* PLL multiplication factor = PLL input clock * 6.5 */
  254. pllmull = 13U / 2U;
  255. }
  256. if (pllsource == 0x00U)
  257. {
  258. /* HSI oscillator clock divided by 2 selected as PLL clock entry */
  259. SystemCoreClock = (HSI_VALUE >> 1U) * pllmull;
  260. }
  261. else
  262. {/* PREDIV1 selected as PLL clock entry */
  263. /* Get PREDIV1 clock source and division factor */
  264. prediv1source = RCC->CFGR2 & RCC_CFGR2_PREDIV1SRC;
  265. prediv1factor = (RCC->CFGR2 & RCC_CFGR2_PREDIV1) + 1U;
  266. if (prediv1source == 0U)
  267. {
  268. /* HSE oscillator clock selected as PREDIV1 clock entry */
  269. SystemCoreClock = (HSE_VALUE / prediv1factor) * pllmull;
  270. }
  271. else
  272. {/* PLL2 clock selected as PREDIV1 clock entry */
  273. /* Get PREDIV2 division factor and PLL2 multiplication factor */
  274. prediv2factor = ((RCC->CFGR2 & RCC_CFGR2_PREDIV2) >> 4U) + 1U;
  275. pll2mull = ((RCC->CFGR2 & RCC_CFGR2_PLL2MUL) >> 8U) + 2U;
  276. SystemCoreClock = (((HSE_VALUE / prediv2factor) * pll2mull) / prediv1factor) * pllmull;
  277. }
  278. }
  279. #endif /* STM32F105xC */
  280. break;
  281. default:
  282. SystemCoreClock = HSI_VALUE;
  283. break;
  284. }
  285. /* Compute HCLK clock frequency ----------------*/
  286. /* Get HCLK prescaler */
  287. tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4U)];
  288. /* HCLK clock frequency */
  289. SystemCoreClock >>= tmp;
  290. }
  291. #if defined(STM32F100xE) || defined(STM32F101xE) || defined(STM32F101xG) || defined(STM32F103xE) || defined(STM32F103xG)
  292. /**
  293. * @brief Setup the external memory controller. Called in startup_stm32f1xx.s
  294. * before jump to __main
  295. * @param None
  296. * @retval None
  297. */
  298. #ifdef DATA_IN_ExtSRAM
  299. /**
  300. * @brief Setup the external memory controller.
  301. * Called in startup_stm32f1xx_xx.s/.c before jump to main.
  302. * This function configures the external SRAM mounted on STM3210E-EVAL
  303. * board (STM32 High density devices). This SRAM will be used as program
  304. * data memory (including heap and stack).
  305. * @param None
  306. * @retval None
  307. */
  308. void SystemInit_ExtMemCtl(void)
  309. {
  310. __IO uint32_t tmpreg;
  311. /*!< FSMC Bank1 NOR/SRAM3 is used for the STM3210E-EVAL, if another Bank is
  312. required, then adjust the Register Addresses */
  313. /* Enable FSMC clock */
  314. RCC->AHBENR = 0x00000114U;
  315. /* Delay after an RCC peripheral clock enabling */
  316. tmpreg = READ_BIT(RCC->AHBENR, RCC_AHBENR_FSMCEN);
  317. /* Enable GPIOD, GPIOE, GPIOF and GPIOG clocks */
  318. RCC->APB2ENR = 0x000001E0U;
  319. /* Delay after an RCC peripheral clock enabling */
  320. tmpreg = READ_BIT(RCC->APB2ENR, RCC_APB2ENR_IOPDEN);
  321. (void)(tmpreg);
  322. /* --------------- SRAM Data lines, NOE and NWE configuration ---------------*/
  323. /*---------------- SRAM Address lines configuration -------------------------*/
  324. /*---------------- NOE and NWE configuration --------------------------------*/
  325. /*---------------- NE3 configuration ----------------------------------------*/
  326. /*---------------- NBL0, NBL1 configuration ---------------------------------*/
  327. GPIOD->CRL = 0x44BB44BBU;
  328. GPIOD->CRH = 0xBBBBBBBBU;
  329. GPIOE->CRL = 0xB44444BBU;
  330. GPIOE->CRH = 0xBBBBBBBBU;
  331. GPIOF->CRL = 0x44BBBBBBU;
  332. GPIOF->CRH = 0xBBBB4444U;
  333. GPIOG->CRL = 0x44BBBBBBU;
  334. GPIOG->CRH = 0x444B4B44U;
  335. /*---------------- FSMC Configuration ---------------------------------------*/
  336. /*---------------- Enable FSMC Bank1_SRAM Bank ------------------------------*/
  337. FSMC_Bank1->BTCR[4U] = 0x00001091U;
  338. FSMC_Bank1->BTCR[5U] = 0x00110212U;
  339. }
  340. #endif /* DATA_IN_ExtSRAM */
  341. #endif /* STM32F100xE || STM32F101xE || STM32F101xG || STM32F103xE || STM32F103xG */
  342. /**
  343. * @}
  344. */
  345. /**
  346. * @}
  347. */
  348. /**
  349. * @}
  350. */
  351. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/