A low cost DIY sound pressure level sensor for enabling environmental noise awareness. https://lukasschwarz.org/noise-sensor
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.
 
 
 
 

291 lines
11 KiB

  1. /**
  2. ******************************************************************************
  3. * @file system_stm32g0xx.c
  4. * @author MCD Application Team
  5. * @brief CMSIS Cortex-M0+ 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_stm32g0xx.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. * After each device reset the HSI (8 MHz then 16 MHz) is used as system clock source.
  22. * Then SystemInit() function is called, in "startup_stm32g0xx.s" file, to
  23. * configure the system clock before to branch to main program.
  24. *
  25. * This file configures the system clock as follows:
  26. *=============================================================================
  27. *-----------------------------------------------------------------------------
  28. * System Clock source | HSI
  29. *-----------------------------------------------------------------------------
  30. * SYSCLK(Hz) | 16000000
  31. *-----------------------------------------------------------------------------
  32. * HCLK(Hz) | 16000000
  33. *-----------------------------------------------------------------------------
  34. * AHB Prescaler | 1
  35. *-----------------------------------------------------------------------------
  36. * APB Prescaler | 1
  37. *-----------------------------------------------------------------------------
  38. * HSI Division factor | 1
  39. *-----------------------------------------------------------------------------
  40. * PLL_M | 1
  41. *-----------------------------------------------------------------------------
  42. * PLL_N | 8
  43. *-----------------------------------------------------------------------------
  44. * PLL_P | 7
  45. *-----------------------------------------------------------------------------
  46. * PLL_Q | 2
  47. *-----------------------------------------------------------------------------
  48. * PLL_R | 2
  49. *-----------------------------------------------------------------------------
  50. * Require 48MHz for RNG | Disabled
  51. *-----------------------------------------------------------------------------
  52. *=============================================================================
  53. ******************************************************************************
  54. * @attention
  55. *
  56. * <h2><center>&copy; Copyright (c) 2018 STMicroelectronics.
  57. * All rights reserved.</center></h2>
  58. *
  59. * This software component is licensed by ST under BSD 3-Clause license,
  60. * the "License"; You may not use this file except in compliance with the
  61. * License. You may obtain a copy of the License at:
  62. * opensource.org/licenses/BSD-3-Clause
  63. *
  64. ******************************************************************************
  65. */
  66. /** @addtogroup CMSIS
  67. * @{
  68. */
  69. /** @addtogroup stm32g0xx_system
  70. * @{
  71. */
  72. /** @addtogroup STM32G0xx_System_Private_Includes
  73. * @{
  74. */
  75. #include "stm32g0xx.h"
  76. #if !defined (HSE_VALUE)
  77. #define HSE_VALUE (8000000UL) /*!< Value of the External oscillator in Hz */
  78. #endif /* HSE_VALUE */
  79. #if !defined (HSI_VALUE)
  80. #define HSI_VALUE (16000000UL) /*!< Value of the Internal oscillator in Hz*/
  81. #endif /* HSI_VALUE */
  82. #if !defined (LSI_VALUE)
  83. #define LSI_VALUE (32000UL) /*!< Value of LSI in Hz*/
  84. #endif /* LSI_VALUE */
  85. #if !defined (LSE_VALUE)
  86. #define LSE_VALUE (32768UL) /*!< Value of LSE in Hz*/
  87. #endif /* LSE_VALUE */
  88. /**
  89. * @}
  90. */
  91. /** @addtogroup STM32G0xx_System_Private_TypesDefinitions
  92. * @{
  93. */
  94. /**
  95. * @}
  96. */
  97. /** @addtogroup STM32G0xx_System_Private_Defines
  98. * @{
  99. */
  100. /************************* Miscellaneous Configuration ************************/
  101. /*!< Uncomment the following line if you need to relocate your vector Table in
  102. Internal SRAM. */
  103. /* #define VECT_TAB_SRAM */
  104. #define VECT_TAB_OFFSET 0x0U /*!< Vector Table base offset field.
  105. This value must be a multiple of 0x100. */
  106. /******************************************************************************/
  107. /**
  108. * @}
  109. */
  110. /** @addtogroup STM32G0xx_System_Private_Macros
  111. * @{
  112. */
  113. /**
  114. * @}
  115. */
  116. /** @addtogroup STM32G0xx_System_Private_Variables
  117. * @{
  118. */
  119. /* The SystemCoreClock variable is updated in three ways:
  120. 1) by calling CMSIS function SystemCoreClockUpdate()
  121. 2) by calling HAL API function HAL_RCC_GetHCLKFreq()
  122. 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
  123. Note: If you use this function to configure the system clock; then there
  124. is no need to call the 2 first functions listed above, since SystemCoreClock
  125. variable is updated automatically.
  126. */
  127. uint32_t SystemCoreClock = 16000000UL;
  128. const uint32_t AHBPrescTable[16UL] = {0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 1UL, 2UL, 3UL, 4UL, 6UL, 7UL, 8UL, 9UL};
  129. const uint32_t APBPrescTable[8UL] = {0UL, 0UL, 0UL, 0UL, 1UL, 2UL, 3UL, 4UL};
  130. /**
  131. * @}
  132. */
  133. /** @addtogroup STM32G0xx_System_Private_FunctionPrototypes
  134. * @{
  135. */
  136. /**
  137. * @}
  138. */
  139. /** @addtogroup STM32G0xx_System_Private_Functions
  140. * @{
  141. */
  142. /**
  143. * @brief Setup the microcontroller system.
  144. * @param None
  145. * @retval None
  146. */
  147. void SystemInit(void)
  148. {
  149. /* Configure the Vector Table location add offset address ------------------*/
  150. #ifdef VECT_TAB_SRAM
  151. SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */
  152. #else
  153. SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */
  154. #endif
  155. }
  156. /**
  157. * @brief Update SystemCoreClock variable according to Clock Register Values.
  158. * The SystemCoreClock variable contains the core clock (HCLK), it can
  159. * be used by the user application to setup the SysTick timer or configure
  160. * other parameters.
  161. *
  162. * @note Each time the core clock (HCLK) changes, this function must be called
  163. * to update SystemCoreClock variable value. Otherwise, any configuration
  164. * based on this variable will be incorrect.
  165. *
  166. * @note - The system frequency computed by this function is not the real
  167. * frequency in the chip. It is calculated based on the predefined
  168. * constant and the selected clock source:
  169. *
  170. * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(**) / HSI division factor
  171. *
  172. * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(***)
  173. *
  174. * - If SYSCLK source is LSI, SystemCoreClock will contain the LSI_VALUE
  175. *
  176. * - If SYSCLK source is LSE, SystemCoreClock will contain the LSE_VALUE
  177. *
  178. * - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(***)
  179. * or HSI_VALUE(*) multiplied/divided by the PLL factors.
  180. *
  181. * (**) HSI_VALUE is a constant defined in stm32g0xx_hal_conf.h file (default value
  182. * 16 MHz) but the real value may vary depending on the variations
  183. * in voltage and temperature.
  184. *
  185. * (***) HSE_VALUE is a constant defined in stm32g0xx_hal_conf.h file (default value
  186. * 8 MHz), user has to ensure that HSE_VALUE is same as the real
  187. * frequency of the crystal used. Otherwise, this function may
  188. * have wrong result.
  189. *
  190. * - The result of this function could be not correct when using fractional
  191. * value for HSE crystal.
  192. *
  193. * @param None
  194. * @retval None
  195. */
  196. //void SystemCoreClockUpdate(void)
  197. //{
  198. // uint32_t tmp;
  199. // uint32_t pllvco;
  200. // uint32_t pllr;
  201. // uint32_t pllsource;
  202. // uint32_t pllm;
  203. // uint32_t hsidiv;
  204. //
  205. // /* Get SYSCLK source -------------------------------------------------------*/
  206. // switch (RCC->CFGR & RCC_CFGR_SWS)
  207. // {
  208. // case RCC_CFGR_SWS_HSE: /* HSE used as system clock */ RCC_
  209. // SystemCoreClock = HSE_VALUE;
  210. // break;
  211. //
  212. // case RCC_CFGR_SWS_LSI: /* LSI used as system clock */
  213. // SystemCoreClock = LSI_VALUE;
  214. // break;
  215. //
  216. // case RCC_CFGR_SWS_LSE: /* LSE used as system clock */
  217. // SystemCoreClock = LSE_VALUE;
  218. // break;
  219. //
  220. // case RCC_CFGR_SWS_PLL: /* PLL used as system clock */
  221. // /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLLM) * PLLN
  222. // SYSCLK = PLL_VCO / PLLR
  223. // */
  224. // pllsource = (RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC);
  225. // pllm = ((RCC->PLLCFGR & RCC_PLLCFGR_PLLM) >> RCC_PLLCFGR_PLLM_Pos) + 1UL;
  226. //
  227. // if(pllsource == 0x03UL) /* HSE used as PLL clock source */
  228. // {
  229. // pllvco = (HSE_VALUE / pllm);
  230. // }
  231. // else /* HSI used as PLL clock source */
  232. // {
  233. // pllvco = (HSI_VALUE / pllm);
  234. // }
  235. // pllvco = pllvco * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos);
  236. // pllr = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLR) >> RCC_PLLCFGR_PLLR_Pos) + 1UL);
  237. //
  238. // SystemCoreClock = pllvco/pllr;
  239. // break;
  240. //
  241. // case RCC_CFGR_SWS_HSI: /* HSI used as system clock */
  242. // default: /* HSI used as system clock */
  243. // hsidiv = (1UL << ((READ_BIT(RCC->CR, RCC_CR_HSIDIV))>> RCC_CR_HSIDIV_Pos));
  244. // SystemCoreClock = (HSI_VALUE/hsidiv);
  245. // break;
  246. // }
  247. // /* Compute HCLK clock frequency --------------------------------------------*/
  248. // /* Get HCLK prescaler */
  249. // tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> RCC_CFGR_HPRE_Pos)];
  250. // /* HCLK clock frequency */
  251. // SystemCoreClock >>= tmp;
  252. //}
  253. /**
  254. * @}
  255. */
  256. /**
  257. * @}
  258. */
  259. /**
  260. * @}
  261. */
  262. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/