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.
 
 
 
 
 
 

427 lines
15 KiB

  1. /**
  2. ******************************************************************************
  3. * @file system_stm32l1xx.c
  4. * @author MCD Application Team
  5. * @brief CMSIS Cortex-M3 Device Peripheral Access Layer System Source File.
  6. *
  7. * This file provides two functions and one global variable to be called from
  8. * user application:
  9. * - SystemInit(): This function is called at startup just after reset and
  10. * before branch to main program. This call is made inside
  11. * the "startup_stm32l1xx.s" file.
  12. *
  13. * - SystemCoreClock variable: Contains the core clock (HCLK), it can be used
  14. * by the user application to setup the SysTick
  15. * timer or configure other parameters.
  16. *
  17. * - SystemCoreClockUpdate(): Updates the variable SystemCoreClock and must
  18. * be called whenever the core clock is changed
  19. * during program execution.
  20. *
  21. ******************************************************************************
  22. * @attention
  23. *
  24. * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
  25. * All rights reserved.</center></h2>
  26. *
  27. * This software component is licensed by ST under BSD 3-Clause license,
  28. * the "License"; You may not use this file except in compliance with the
  29. * License. You may obtain a copy of the License at:
  30. * opensource.org/licenses/BSD-3-Clause
  31. *
  32. ******************************************************************************
  33. */
  34. /** @addtogroup CMSIS
  35. * @{
  36. */
  37. /** @addtogroup stm32l1xx_system
  38. * @{
  39. */
  40. /** @addtogroup STM32L1xx_System_Private_Includes
  41. * @{
  42. */
  43. #include "stm32l1xx.h"
  44. /**
  45. * @}
  46. */
  47. /** @addtogroup STM32L1xx_System_Private_TypesDefinitions
  48. * @{
  49. */
  50. /**
  51. * @}
  52. */
  53. /** @addtogroup STM32L1xx_System_Private_Defines
  54. * @{
  55. */
  56. #if !defined (HSE_VALUE)
  57. #define HSE_VALUE ((uint32_t)8000000U) /*!< Default value of the External oscillator in Hz.
  58. This value can be provided and adapted by the user application. */
  59. #endif /* HSE_VALUE */
  60. #if !defined (HSI_VALUE)
  61. #define HSI_VALUE ((uint32_t)8000000U) /*!< Default value of the Internal oscillator in Hz.
  62. This value can be provided and adapted by the user application. */
  63. #endif /* HSI_VALUE */
  64. /*!< Uncomment the following line if you need to use external SRAM mounted
  65. on STM32L152D_EVAL board as data memory */
  66. /* #define DATA_IN_ExtSRAM */
  67. /*!< Uncomment the following line if you need to relocate your vector Table in
  68. Internal SRAM. */
  69. /* #define VECT_TAB_SRAM */
  70. #define VECT_TAB_OFFSET 0x00U /*!< Vector Table base offset field.
  71. This value must be a multiple of 0x200. */
  72. /**
  73. * @}
  74. */
  75. /** @addtogroup STM32L1xx_System_Private_Macros
  76. * @{
  77. */
  78. /**
  79. * @}
  80. */
  81. /** @addtogroup STM32L1xx_System_Private_Variables
  82. * @{
  83. */
  84. /* This variable is updated in three ways:
  85. 1) by calling CMSIS function SystemCoreClockUpdate()
  86. 2) by calling HAL API function HAL_RCC_GetHCLKFreq()
  87. 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
  88. Note: If you use this function to configure the system clock; then there
  89. is no need to call the 2 first functions listed above, since SystemCoreClock
  90. variable is updated automatically.
  91. */
  92. uint32_t SystemCoreClock = 2097000U;
  93. const uint8_t PLLMulTable[9] = {3U, 4U, 6U, 8U, 12U, 16U, 24U, 32U, 48U};
  94. const uint8_t AHBPrescTable[16] = {0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 1U, 2U, 3U, 4U, 6U, 7U, 8U, 9U};
  95. const uint8_t APBPrescTable[8] = {0U, 0U, 0U, 0U, 1U, 2U, 3U, 4U};
  96. /**
  97. * @}
  98. */
  99. /** @addtogroup STM32L1xx_System_Private_FunctionPrototypes
  100. * @{
  101. */
  102. #if defined (STM32L151xD) || defined (STM32L152xD) || defined (STM32L162xD)
  103. #ifdef DATA_IN_ExtSRAM
  104. static void SystemInit_ExtMemCtl(void);
  105. #endif /* DATA_IN_ExtSRAM */
  106. #endif /* STM32L151xD || STM32L152xD || STM32L162xD */
  107. /**
  108. * @}
  109. */
  110. /** @addtogroup STM32L1xx_System_Private_Functions
  111. * @{
  112. */
  113. /**
  114. * @brief Setup the microcontroller system.
  115. * Initialize the Embedded Flash Interface, the PLL and update the
  116. * SystemCoreClock variable.
  117. * @param None
  118. * @retval None
  119. */
  120. void SystemInit (void)
  121. {
  122. /*!< Set MSION bit */
  123. RCC->CR |= (uint32_t)0x00000100;
  124. /*!< Reset SW[1:0], HPRE[3:0], PPRE1[2:0], PPRE2[2:0], MCOSEL[2:0] and MCOPRE[2:0] bits */
  125. RCC->CFGR &= (uint32_t)0x88FFC00C;
  126. /*!< Reset HSION, HSEON, CSSON and PLLON bits */
  127. RCC->CR &= (uint32_t)0xEEFEFFFE;
  128. /*!< Reset HSEBYP bit */
  129. RCC->CR &= (uint32_t)0xFFFBFFFF;
  130. /*!< Reset PLLSRC, PLLMUL[3:0] and PLLDIV[1:0] bits */
  131. RCC->CFGR &= (uint32_t)0xFF02FFFF;
  132. /*!< Disable all interrupts */
  133. RCC->CIR = 0x00000000;
  134. #ifdef DATA_IN_ExtSRAM
  135. SystemInit_ExtMemCtl();
  136. #endif /* DATA_IN_ExtSRAM */
  137. #ifdef VECT_TAB_SRAM
  138. SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM. */
  139. #else
  140. SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH. */
  141. #endif
  142. }
  143. /**
  144. * @brief Update SystemCoreClock according to Clock Register Values
  145. * The SystemCoreClock variable contains the core clock (HCLK), it can
  146. * be used by the user application to setup the SysTick timer or configure
  147. * other parameters.
  148. *
  149. * @note Each time the core clock (HCLK) changes, this function must be called
  150. * to update SystemCoreClock variable value. Otherwise, any configuration
  151. * based on this variable will be incorrect.
  152. *
  153. * @note - The system frequency computed by this function is not the real
  154. * frequency in the chip. It is calculated based on the predefined
  155. * constant and the selected clock source:
  156. *
  157. * - If SYSCLK source is MSI, SystemCoreClock will contain the MSI
  158. * value as defined by the MSI range.
  159. *
  160. * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(*)
  161. *
  162. * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(**)
  163. *
  164. * - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(**)
  165. * or HSI_VALUE(*) multiplied/divided by the PLL factors.
  166. *
  167. * (*) HSI_VALUE is a constant defined in stm32l1xx.h file (default value
  168. * 16 MHz) but the real value may vary depending on the variations
  169. * in voltage and temperature.
  170. *
  171. * (**) HSE_VALUE is a constant defined in stm32l1xx.h file (default value
  172. * 8 MHz), user has to ensure that HSE_VALUE is same as the real
  173. * frequency of the crystal used. Otherwise, this function may
  174. * have wrong result.
  175. *
  176. * - The result of this function could be not correct when using fractional
  177. * value for HSE crystal.
  178. * @param None
  179. * @retval None
  180. */
  181. void SystemCoreClockUpdate (void)
  182. {
  183. uint32_t tmp = 0, pllmul = 0, plldiv = 0, pllsource = 0, msirange = 0;
  184. /* Get SYSCLK source -------------------------------------------------------*/
  185. tmp = RCC->CFGR & RCC_CFGR_SWS;
  186. switch (tmp)
  187. {
  188. case 0x00: /* MSI used as system clock */
  189. msirange = (RCC->ICSCR & RCC_ICSCR_MSIRANGE) >> 13;
  190. SystemCoreClock = (32768 * (1 << (msirange + 1)));
  191. break;
  192. case 0x04: /* HSI used as system clock */
  193. SystemCoreClock = HSI_VALUE;
  194. break;
  195. case 0x08: /* HSE used as system clock */
  196. SystemCoreClock = HSE_VALUE;
  197. break;
  198. case 0x0C: /* PLL used as system clock */
  199. /* Get PLL clock source and multiplication factor ----------------------*/
  200. pllmul = RCC->CFGR & RCC_CFGR_PLLMUL;
  201. plldiv = RCC->CFGR & RCC_CFGR_PLLDIV;
  202. pllmul = PLLMulTable[(pllmul >> 18)];
  203. plldiv = (plldiv >> 22) + 1;
  204. pllsource = RCC->CFGR & RCC_CFGR_PLLSRC;
  205. if (pllsource == 0x00)
  206. {
  207. /* HSI oscillator clock selected as PLL clock entry */
  208. SystemCoreClock = (((HSI_VALUE) * pllmul) / plldiv);
  209. }
  210. else
  211. {
  212. /* HSE selected as PLL clock entry */
  213. SystemCoreClock = (((HSE_VALUE) * pllmul) / plldiv);
  214. }
  215. break;
  216. default: /* MSI used as system clock */
  217. msirange = (RCC->ICSCR & RCC_ICSCR_MSIRANGE) >> 13;
  218. SystemCoreClock = (32768 * (1 << (msirange + 1)));
  219. break;
  220. }
  221. /* Compute HCLK clock frequency --------------------------------------------*/
  222. /* Get HCLK prescaler */
  223. tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)];
  224. /* HCLK clock frequency */
  225. SystemCoreClock >>= tmp;
  226. }
  227. #if defined (STM32L151xD) || defined (STM32L152xD) || defined (STM32L162xD)
  228. #ifdef DATA_IN_ExtSRAM
  229. /**
  230. * @brief Setup the external memory controller.
  231. * Called in SystemInit() function before jump to main.
  232. * This function configures the external SRAM mounted on STM32L152D_EVAL board
  233. * This SRAM will be used as program data memory (including heap and stack).
  234. * @param None
  235. * @retval None
  236. */
  237. void SystemInit_ExtMemCtl(void)
  238. {
  239. __IO uint32_t tmpreg = 0;
  240. /* Flash 1 wait state */
  241. FLASH->ACR |= FLASH_ACR_LATENCY;
  242. /* Power enable */
  243. RCC->APB1ENR |= RCC_APB1ENR_PWREN;
  244. /* Delay after an RCC peripheral clock enabling */
  245. tmpreg = READ_BIT(RCC->APB1ENR, RCC_APB1ENR_PWREN);
  246. /* Select the Voltage Range 1 (1.8 V) */
  247. PWR->CR = PWR_CR_VOS_0;
  248. /* Wait Until the Voltage Regulator is ready */
  249. while((PWR->CSR & PWR_CSR_VOSF) != RESET)
  250. {
  251. }
  252. /*-- GPIOs Configuration -----------------------------------------------------*/
  253. /*
  254. +-------------------+--------------------+------------------+------------------+
  255. + SRAM pins assignment +
  256. +-------------------+--------------------+------------------+------------------+
  257. | PD0 <-> FSMC_D2 | PE0 <-> FSMC_NBL0 | PF0 <-> FSMC_A0 | PG0 <-> FSMC_A10 |
  258. | PD1 <-> FSMC_D3 | PE1 <-> FSMC_NBL1 | PF1 <-> FSMC_A1 | PG1 <-> FSMC_A11 |
  259. | PD4 <-> FSMC_NOE | PE7 <-> FSMC_D4 | PF2 <-> FSMC_A2 | PG2 <-> FSMC_A12 |
  260. | PD5 <-> FSMC_NWE | PE8 <-> FSMC_D5 | PF3 <-> FSMC_A3 | PG3 <-> FSMC_A13 |
  261. | PD8 <-> FSMC_D13 | PE9 <-> FSMC_D6 | PF4 <-> FSMC_A4 | PG4 <-> FSMC_A14 |
  262. | PD9 <-> FSMC_D14 | PE10 <-> FSMC_D7 | PF5 <-> FSMC_A5 | PG5 <-> FSMC_A15 |
  263. | PD10 <-> FSMC_D15 | PE11 <-> FSMC_D8 | PF12 <-> FSMC_A6 | PG10<-> FSMC_NE2 |
  264. | PD11 <-> FSMC_A16 | PE12 <-> FSMC_D9 | PF13 <-> FSMC_A7 |------------------+
  265. | PD12 <-> FSMC_A17 | PE13 <-> FSMC_D10 | PF14 <-> FSMC_A8 |
  266. | PD13 <-> FSMC_A18 | PE14 <-> FSMC_D11 | PF15 <-> FSMC_A9 |
  267. | PD14 <-> FSMC_D0 | PE15 <-> FSMC_D12 |------------------+
  268. | PD15 <-> FSMC_D1 |--------------------+
  269. +-------------------+
  270. */
  271. /* Enable GPIOD, GPIOE, GPIOF and GPIOG interface clock */
  272. RCC->AHBENR = 0x000080D8;
  273. /* Delay after an RCC peripheral clock enabling */
  274. tmpreg = READ_BIT(RCC->AHBENR, RCC_AHBENR_GPIODEN);
  275. /* Connect PDx pins to FSMC Alternate function */
  276. GPIOD->AFR[0] = 0x00CC00CC;
  277. GPIOD->AFR[1] = 0xCCCCCCCC;
  278. /* Configure PDx pins in Alternate function mode */
  279. GPIOD->MODER = 0xAAAA0A0A;
  280. /* Configure PDx pins speed to 40 MHz */
  281. GPIOD->OSPEEDR = 0xFFFF0F0F;
  282. /* Configure PDx pins Output type to push-pull */
  283. GPIOD->OTYPER = 0x00000000;
  284. /* No pull-up, pull-down for PDx pins */
  285. GPIOD->PUPDR = 0x00000000;
  286. /* Connect PEx pins to FSMC Alternate function */
  287. GPIOE->AFR[0] = 0xC00000CC;
  288. GPIOE->AFR[1] = 0xCCCCCCCC;
  289. /* Configure PEx pins in Alternate function mode */
  290. GPIOE->MODER = 0xAAAA800A;
  291. /* Configure PEx pins speed to 40 MHz */
  292. GPIOE->OSPEEDR = 0xFFFFC00F;
  293. /* Configure PEx pins Output type to push-pull */
  294. GPIOE->OTYPER = 0x00000000;
  295. /* No pull-up, pull-down for PEx pins */
  296. GPIOE->PUPDR = 0x00000000;
  297. /* Connect PFx pins to FSMC Alternate function */
  298. GPIOF->AFR[0] = 0x00CCCCCC;
  299. GPIOF->AFR[1] = 0xCCCC0000;
  300. /* Configure PFx pins in Alternate function mode */
  301. GPIOF->MODER = 0xAA000AAA;
  302. /* Configure PFx pins speed to 40 MHz */
  303. GPIOF->OSPEEDR = 0xFF000FFF;
  304. /* Configure PFx pins Output type to push-pull */
  305. GPIOF->OTYPER = 0x00000000;
  306. /* No pull-up, pull-down for PFx pins */
  307. GPIOF->PUPDR = 0x00000000;
  308. /* Connect PGx pins to FSMC Alternate function */
  309. GPIOG->AFR[0] = 0x00CCCCCC;
  310. GPIOG->AFR[1] = 0x00000C00;
  311. /* Configure PGx pins in Alternate function mode */
  312. GPIOG->MODER = 0x00200AAA;
  313. /* Configure PGx pins speed to 40 MHz */
  314. GPIOG->OSPEEDR = 0x00300FFF;
  315. /* Configure PGx pins Output type to push-pull */
  316. GPIOG->OTYPER = 0x00000000;
  317. /* No pull-up, pull-down for PGx pins */
  318. GPIOG->PUPDR = 0x00000000;
  319. /*-- FSMC Configuration ------------------------------------------------------*/
  320. /* Enable the FSMC interface clock */
  321. RCC->AHBENR = 0x400080D8;
  322. /* Delay after an RCC peripheral clock enabling */
  323. tmpreg = READ_BIT(RCC->AHBENR, RCC_AHBENR_FSMCEN);
  324. (void)(tmpreg);
  325. /* Configure and enable Bank1_SRAM3 */
  326. FSMC_Bank1->BTCR[4] = 0x00001011;
  327. FSMC_Bank1->BTCR[5] = 0x00000300;
  328. FSMC_Bank1E->BWTR[4] = 0x0FFFFFFF;
  329. /*
  330. Bank1_SRAM3 is configured as follow:
  331. p.FSMC_AddressSetupTime = 0;
  332. p.FSMC_AddressHoldTime = 0;
  333. p.FSMC_DataSetupTime = 3;
  334. p.FSMC_BusTurnAroundDuration = 0;
  335. p.FSMC_CLKDivision = 0;
  336. p.FSMC_DataLatency = 0;
  337. p.FSMC_AccessMode = FSMC_AccessMode_A;
  338. FSMC_NORSRAMInitStructure.FSMC_Bank = FSMC_Bank1_NORSRAM3;
  339. FSMC_NORSRAMInitStructure.FSMC_DataAddressMux = FSMC_DataAddressMux_Disable;
  340. FSMC_NORSRAMInitStructure.FSMC_MemoryType = FSMC_MemoryType_SRAM;
  341. FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_16b;
  342. FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode = FSMC_BurstAccessMode_Disable;
  343. FSMC_NORSRAMInitStructure.FSMC_AsynchronousWait = FSMC_AsynchronousWait_Disable;
  344. FSMC_NORSRAMInitStructure.FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low;
  345. FSMC_NORSRAMInitStructure.FSMC_WrapMode = FSMC_WrapMode_Disable;
  346. FSMC_NORSRAMInitStructure.FSMC_WaitSignalActive = FSMC_WaitSignalActive_BeforeWaitState;
  347. FSMC_NORSRAMInitStructure.FSMC_WriteOperation = FSMC_WriteOperation_Enable;
  348. FSMC_NORSRAMInitStructure.FSMC_WaitSignal = FSMC_WaitSignal_Disable;
  349. FSMC_NORSRAMInitStructure.FSMC_ExtendedMode = FSMC_ExtendedMode_Disable;
  350. FSMC_NORSRAMInitStructure.FSMC_WriteBurst = FSMC_WriteBurst_Disable;
  351. FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &p;
  352. FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct = &p;
  353. FSMC_NORSRAMInit(&FSMC_NORSRAMInitStructure);
  354. FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM3, ENABLE);
  355. */
  356. }
  357. #endif /* DATA_IN_ExtSRAM */
  358. #endif /* STM32L151xD || STM32L152xD || STM32L162xD */
  359. /**
  360. * @}
  361. */
  362. /**
  363. * @}
  364. */
  365. /**
  366. * @}
  367. */
  368. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/