hal_lld.h 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204
  1. /*
  2. ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. /**
  14. * @file STM32F3xx/hal_lld.h
  15. * @brief STM32F3xx HAL subsystem low level driver header.
  16. * @pre This module requires the following macros to be defined in the
  17. * @p board.h file:
  18. * - STM32_LSECLK.
  19. * - STM32_LSEDRV.
  20. * - STM32_LSE_BYPASS (optionally).
  21. * - STM32_HSECLK.
  22. * - STM32_HSE_BYPASS (optionally).
  23. * .
  24. * One of the following macros must also be defined:
  25. * - STM32F301x8 for Analog & DSP devices.
  26. * - STM32F302x8 for Analog & DSP devices.
  27. * - STM32F302xC for Analog & DSP devices.
  28. * - STM32F302xE for Analog & DSP devices.
  29. * - STM32F303x8 for Analog & DSP devices.
  30. * - STM32F303xC for Analog & DSP devices.
  31. * - STM32F303xE for Analog & DSP devices.
  32. * - STM32F318xx for Analog & DSP devices.
  33. * - STM32F328xx for Analog & DSP devices.
  34. * - STM32F334x8 for Analog & DSP devices.
  35. * - STM32F358xx for Analog & DSP devices.
  36. * - STM32F398xx for Analog & DSP devices.
  37. * .
  38. *
  39. * @addtogroup HAL
  40. * @{
  41. */
  42. #ifndef HAL_LLD_H
  43. #define HAL_LLD_H
  44. #include "stm32_registry.h"
  45. /*===========================================================================*/
  46. /* Driver constants. */
  47. /*===========================================================================*/
  48. /**
  49. * @name Platform identification macros
  50. * @{
  51. */
  52. #if defined(STM32F301x8) || defined(__DOXYGEN__)
  53. #define PLATFORM_NAME "STM32F301x8 Analog & DSP"
  54. #elif defined(STM32F302x8)
  55. #define PLATFORM_NAME "STM32F302x8 Analog & DSP"
  56. #elif defined(STM32F302xC)
  57. #define PLATFORM_NAME "STM32F302xC Analog & DSP"
  58. #elif defined(STM32F302xE)
  59. #define PLATFORM_NAME "STM32F302xE Analog & DSP"
  60. #elif defined(STM32F303x8)
  61. #define PLATFORM_NAME "STM32F303x8 Analog & DSP"
  62. #elif defined(STM32F303xC)
  63. #define PLATFORM_NAME "STM32F303xC Analog & DSP"
  64. #elif defined(STM32F303xE)
  65. #define PLATFORM_NAME "STM32F303xE Analog & DSP"
  66. #elif defined(STM32F318xx)
  67. #define PLATFORM_NAME "STM32F318xx Analog & DSP"
  68. #elif defined(STM32F328xx)
  69. #define PLATFORM_NAME "STM32F328xx Analog & DSP"
  70. #elif defined(STM32F334x8)
  71. #define PLATFORM_NAME "STM32F334x8 Analog & DSP"
  72. #elif defined(STM32F358xx)
  73. #define PLATFORM_NAME "STM32F358xx Analog & DSP"
  74. #elif defined(STM32F398xx)
  75. #define PLATFORM_NAME "STM32F398xx Analog & DSP"
  76. #else
  77. #error "STM32F3xx device not specified"
  78. #endif
  79. /** @} */
  80. /**
  81. * @name Absolute Maximum Ratings
  82. * @{
  83. */
  84. /**
  85. * @brief Maximum system clock frequency.
  86. */
  87. #define STM32_SYSCLK_MAX 72000000
  88. /**
  89. * @brief Maximum HSE clock frequency.
  90. */
  91. #define STM32_HSECLK_MAX 32000000
  92. /**
  93. * @brief Minimum HSE clock frequency.
  94. */
  95. #define STM32_HSECLK_MIN 1000000
  96. /**
  97. * @brief Maximum LSE clock frequency.
  98. */
  99. #define STM32_LSECLK_MAX 1000000
  100. /**
  101. * @brief Minimum LSE clock frequency.
  102. */
  103. #define STM32_LSECLK_MIN 32768
  104. /**
  105. * @brief Maximum PLLs input clock frequency.
  106. */
  107. #define STM32_PLLIN_MAX 24000000
  108. /**
  109. * @brief Minimum PLLs input clock frequency.
  110. */
  111. #define STM32_PLLIN_MIN 1000000
  112. /**
  113. * @brief Maximum PLL output clock frequency.
  114. */
  115. #define STM32_PLLOUT_MAX 72000000
  116. /**
  117. * @brief Minimum PLL output clock frequency.
  118. */
  119. #define STM32_PLLOUT_MIN 16000000
  120. /**
  121. * @brief Maximum APB1 clock frequency.
  122. */
  123. #define STM32_PCLK1_MAX 36000000
  124. /**
  125. * @brief Maximum APB2 clock frequency.
  126. */
  127. #define STM32_PCLK2_MAX 72000000
  128. /**
  129. * @brief Maximum ADC clock frequency.
  130. */
  131. #define STM32_ADCCLK_MAX 72000000
  132. /** @} */
  133. /**
  134. * @name Internal clock sources
  135. * @{
  136. */
  137. #define STM32_HSICLK 8000000 /**< High speed internal clock. */
  138. #define STM32_LSICLK 40000 /**< Low speed internal clock. */
  139. /** @} */
  140. /**
  141. * @name PWR_CR register bits definitions
  142. * @{
  143. */
  144. #define STM32_PLS_MASK (7 << 5) /**< PLS bits mask. */
  145. #define STM32_PLS_LEV0 (0 << 5) /**< PVD level 0. */
  146. #define STM32_PLS_LEV1 (1 << 5) /**< PVD level 1. */
  147. #define STM32_PLS_LEV2 (2 << 5) /**< PVD level 2. */
  148. #define STM32_PLS_LEV3 (3 << 5) /**< PVD level 3. */
  149. #define STM32_PLS_LEV4 (4 << 5) /**< PVD level 4. */
  150. #define STM32_PLS_LEV5 (5 << 5) /**< PVD level 5. */
  151. #define STM32_PLS_LEV6 (6 << 5) /**< PVD level 6. */
  152. #define STM32_PLS_LEV7 (7 << 5) /**< PVD level 7. */
  153. /** @} */
  154. /**
  155. * @name RCC_CFGR register bits definitions
  156. * @{
  157. */
  158. #define STM32_SW_HSI (0 << 0) /**< SYSCLK source is HSI. */
  159. #define STM32_SW_HSE (1 << 0) /**< SYSCLK source is HSE. */
  160. #define STM32_SW_PLL (2 << 0) /**< SYSCLK source is PLL. */
  161. #define STM32_HPRE_DIV1 (0 << 4) /**< SYSCLK divided by 1. */
  162. #define STM32_HPRE_DIV2 (8 << 4) /**< SYSCLK divided by 2. */
  163. #define STM32_HPRE_DIV4 (9 << 4) /**< SYSCLK divided by 4. */
  164. #define STM32_HPRE_DIV8 (10 << 4) /**< SYSCLK divided by 8. */
  165. #define STM32_HPRE_DIV16 (11 << 4) /**< SYSCLK divided by 16. */
  166. #define STM32_HPRE_DIV64 (12 << 4) /**< SYSCLK divided by 64. */
  167. #define STM32_HPRE_DIV128 (13 << 4) /**< SYSCLK divided by 128. */
  168. #define STM32_HPRE_DIV256 (14 << 4) /**< SYSCLK divided by 256. */
  169. #define STM32_HPRE_DIV512 (15 << 4) /**< SYSCLK divided by 512. */
  170. #define STM32_PPRE1_DIV1 (0 << 8) /**< HCLK divided by 1. */
  171. #define STM32_PPRE1_DIV2 (4 << 8) /**< HCLK divided by 2. */
  172. #define STM32_PPRE1_DIV4 (5 << 8) /**< HCLK divided by 4. */
  173. #define STM32_PPRE1_DIV8 (6 << 8) /**< HCLK divided by 8. */
  174. #define STM32_PPRE1_DIV16 (7 << 8) /**< HCLK divided by 16. */
  175. #define STM32_PPRE2_DIV1 (0 << 11) /**< HCLK divided by 1. */
  176. #define STM32_PPRE2_DIV2 (4 << 11) /**< HCLK divided by 2. */
  177. #define STM32_PPRE2_DIV4 (5 << 11) /**< HCLK divided by 4. */
  178. #define STM32_PPRE2_DIV8 (6 << 11) /**< HCLK divided by 8. */
  179. #define STM32_PPRE2_DIV16 (7 << 11) /**< HCLK divided by 16. */
  180. #define STM32_PLLSRC_HSI (0 << 16) /**< PLL clock source is HSI/2. */
  181. #define STM32_PLLSRC_HSE (1 << 16) /**< PLL clock source is
  182. HSE/PREDIV. */
  183. #define STM32_USBPRE_DIV1P5 (0 << 22) /**< USB clock is PLLCLK/1.5. */
  184. #define STM32_USBPRE_DIV1 (1 << 22) /**< USB clock is PLLCLK/1. */
  185. #define STM32_MCOSEL_NOCLOCK (0 << 24) /**< No clock on MCO pin. */
  186. #define STM32_MCOSEL_LSI (2 << 24) /**< LSI clock on MCO pin. */
  187. #define STM32_MCOSEL_LSE (3 << 24) /**< LSE clock on MCO pin. */
  188. #define STM32_MCOSEL_SYSCLK (4 << 24) /**< SYSCLK on MCO pin. */
  189. #define STM32_MCOSEL_HSI (5 << 24) /**< HSI clock on MCO pin. */
  190. #define STM32_MCOSEL_HSE (6 << 24) /**< HSE clock on MCO pin. */
  191. #define STM32_MCOSEL_PLLDIV2 (7 << 24) /**< PLL/2 clock on MCO pin. */
  192. /** @} */
  193. /**
  194. * @name RCC_BDCR register bits definitions
  195. * @{
  196. */
  197. #define STM32_RTCSEL_MASK (3 << 8) /**< RTC clock source mask. */
  198. #define STM32_RTCSEL_NOCLOCK (0 << 8) /**< No clock. */
  199. #define STM32_RTCSEL_LSE (1 << 8) /**< LSE used as RTC clock. */
  200. #define STM32_RTCSEL_LSI (2 << 8) /**< LSI used as RTC clock. */
  201. #define STM32_RTCSEL_HSEDIV (3 << 8) /**< HSE divided by 32 used as
  202. RTC clock. */
  203. /** @} */
  204. /**
  205. * @name RCC_CFGR2 register bits definitions
  206. * @{
  207. */
  208. #define STM32_PREDIV_MASK (15 << 0) /**< PREDIV divisor mask. */
  209. #define STM32_ADC12PRES_MASK (31 << 4) /**< ADC12 clock source mask. */
  210. #define STM32_ADC12PRES_NOCLOCK (0 << 4) /**< ADC12 clock is disabled. */
  211. #define STM32_ADC12PRES_DIV1 (16 << 4) /**< ADC12 clock is PLL/1. */
  212. #define STM32_ADC12PRES_DIV2 (17 << 4) /**< ADC12 clock is PLL/2. */
  213. #define STM32_ADC12PRES_DIV4 (18 << 4) /**< ADC12 clock is PLL/4. */
  214. #define STM32_ADC12PRES_DIV6 (19 << 4) /**< ADC12 clock is PLL/6. */
  215. #define STM32_ADC12PRES_DIV8 (20 << 4) /**< ADC12 clock is PLL/8. */
  216. #define STM32_ADC12PRES_DIV10 (21 << 4) /**< ADC12 clock is PLL/10. */
  217. #define STM32_ADC12PRES_DIV12 (22 << 4) /**< ADC12 clock is PLL/12. */
  218. #define STM32_ADC12PRES_DIV16 (23 << 4) /**< ADC12 clock is PLL/16. */
  219. #define STM32_ADC12PRES_DIV32 (24 << 4) /**< ADC12 clock is PLL/32. */
  220. #define STM32_ADC12PRES_DIV64 (25 << 4) /**< ADC12 clock is PLL/64. */
  221. #define STM32_ADC12PRES_DIV128 (26 << 4) /**< ADC12 clock is PLL/128. */
  222. #define STM32_ADC12PRES_DIV256 (27 << 4) /**< ADC12 clock is PLL/256. */
  223. #define STM32_ADC34PRES_MASK (31 << 9) /**< ADC34 clock source mask. */
  224. #define STM32_ADC34PRES_NOCLOCK (0 << 9) /**< ADC34 clock is disabled. */
  225. #define STM32_ADC34PRES_DIV1 (16 << 9) /**< ADC34 clock is PLL/1. */
  226. #define STM32_ADC34PRES_DIV2 (17 << 9) /**< ADC34 clock is PLL/2. */
  227. #define STM32_ADC34PRES_DIV4 (18 << 9) /**< ADC34 clock is PLL/4. */
  228. #define STM32_ADC34PRES_DIV6 (19 << 9) /**< ADC34 clock is PLL/6. */
  229. #define STM32_ADC34PRES_DIV8 (20 << 9) /**< ADC34 clock is PLL/8. */
  230. #define STM32_ADC34PRES_DIV10 (21 << 9) /**< ADC34 clock is PLL/10. */
  231. #define STM32_ADC34PRES_DIV12 (22 << 9) /**< ADC34 clock is PLL/12. */
  232. #define STM32_ADC34PRES_DIV16 (23 << 9) /**< ADC34 clock is PLL/16. */
  233. #define STM32_ADC34PRES_DIV32 (24 << 9) /**< ADC34 clock is PLL/32. */
  234. #define STM32_ADC34PRES_DIV64 (25 << 9) /**< ADC34 clock is PLL/64. */
  235. #define STM32_ADC34PRES_DIV128 (26 << 9) /**< ADC34 clock is PLL/128. */
  236. #define STM32_ADC34PRES_DIV256 (27 << 9) /**< ADC34 clock is PLL/256. */
  237. /** @} */
  238. /**
  239. * @name RCC_CFGR3 register bits definitions
  240. * @{
  241. */
  242. #define STM32_USART1SW_MASK (3 << 0) /**< USART1 clock source mask. */
  243. #define STM32_USART1SW_PCLK (0 << 0) /**< USART1 clock is PCLK. */
  244. #define STM32_USART1SW_SYSCLK (1 << 0) /**< USART1 clock is SYSCLK. */
  245. #define STM32_USART1SW_LSE (2 << 0) /**< USART1 clock is LSE. */
  246. #define STM32_USART1SW_HSI (3 << 0) /**< USART1 clock is HSI. */
  247. #define STM32_I2C1SW_MASK (1 << 4) /**< I2C1 clock source mask. */
  248. #define STM32_I2C1SW_HSI (0 << 4) /**< I2C1 clock is HSI. */
  249. #define STM32_I2C1SW_SYSCLK (1 << 4) /**< I2C1 clock is SYSCLK. */
  250. #define STM32_I2C2SW_MASK (1 << 5) /**< I2C2 clock source mask. */
  251. #define STM32_I2C2SW_HSI (0 << 5) /**< I2C2 clock is HSI. */
  252. #define STM32_I2C2SW_SYSCLK (1 << 5) /**< I2C2 clock is SYSCLK. */
  253. #define STM32_TIM1SW_MASK (1 << 8) /**< TIM1 clock source mask. */
  254. #define STM32_TIM1SW_PCLK2 (0 << 8) /**< TIM1 clock is PCLK2. */
  255. #define STM32_TIM1SW_PLLX2 (1 << 8) /**< TIM1 clock is PLL*2. */
  256. #define STM32_TIM8SW_MASK (1 << 9) /**< TIM8 clock source mask. */
  257. #define STM32_TIM8SW_PCLK2 (0 << 9) /**< TIM8 clock is PCLK2. */
  258. #define STM32_TIM8SW_PLLX2 (1 << 9) /**< TIM8 clock is PLL*2. */
  259. #define STM32_HRTIM1SW_MASK (1 << 12) /**< HRTIM1 clock source mask. */
  260. #define STM32_HRTIM1SW_PCLK2 (0 << 12) /**< HRTIM1 clock is PCLK2. */
  261. #define STM32_HRTIM1SW_PLLX2 (1 << 12) /**< HRTIM1 clock is PLL*2. */
  262. #define STM32_USART2SW_MASK (3 << 16) /**< USART2 clock source mask. */
  263. #define STM32_USART2SW_PCLK (0 << 16) /**< USART2 clock is PCLK. */
  264. #define STM32_USART2SW_SYSCLK (1 << 16) /**< USART2 clock is SYSCLK. */
  265. #define STM32_USART2SW_LSE (2 << 16) /**< USART2 clock is LSE. */
  266. #define STM32_USART2SW_HSI (3 << 16) /**< USART2 clock is HSI. */
  267. #define STM32_USART3SW_MASK (3 << 18) /**< USART3 clock source mask. */
  268. #define STM32_USART3SW_PCLK (0 << 18) /**< USART3 clock is PCLK. */
  269. #define STM32_USART3SW_SYSCLK (1 << 18) /**< USART3 clock is SYSCLK. */
  270. #define STM32_USART3SW_LSE (2 << 18) /**< USART3 clock is LSE. */
  271. #define STM32_USART3SW_HSI (3 << 18) /**< USART3 clock is HSI. */
  272. #define STM32_UART4SW_MASK (3 << 20) /**< USART4 clock source mask. */
  273. #define STM32_UART4SW_PCLK (0 << 20) /**< USART4 clock is PCLK. */
  274. #define STM32_UART4SW_SYSCLK (1 << 20) /**< USART4 clock is SYSCLK. */
  275. #define STM32_UART4SW_LSE (2 << 20) /**< USART4 clock is LSE. */
  276. #define STM32_UART4SW_HSI (3 << 20) /**< USART4 clock is HSI. */
  277. #define STM32_UART5SW_MASK (3 << 22) /**< USART5 clock source mask. */
  278. #define STM32_UART5SW_PCLK (0 << 22) /**< USART5 clock is PCLK. */
  279. #define STM32_UART5SW_SYSCLK (1 << 22) /**< USART5 clock is SYSCLK. */
  280. #define STM32_UART5SW_LSE (2 << 22) /**< USART5 clock is LSE. */
  281. #define STM32_UART5SW_HSI (3 << 22) /**< USART5 clock is HSI. */
  282. /** @} */
  283. /*===========================================================================*/
  284. /* Driver pre-compile time settings. */
  285. /*===========================================================================*/
  286. /**
  287. * @name Configuration options
  288. * @{
  289. */
  290. /**
  291. * @brief Disables the PWR/RCC initialization in the HAL.
  292. */
  293. #if !defined(STM32_NO_INIT) || defined(__DOXYGEN__)
  294. #define STM32_NO_INIT FALSE
  295. #endif
  296. /**
  297. * @brief Enables or disables the programmable voltage detector.
  298. */
  299. #if !defined(STM32_PVD_ENABLE) || defined(__DOXYGEN__)
  300. #define STM32_PVD_ENABLE FALSE
  301. #endif
  302. /**
  303. * @brief Sets voltage level for programmable voltage detector.
  304. */
  305. #if !defined(STM32_PLS) || defined(__DOXYGEN__)
  306. #define STM32_PLS STM32_PLS_LEV0
  307. #endif
  308. /**
  309. * @brief Enables or disables the HSI clock source.
  310. */
  311. #if !defined(STM32_HSI_ENABLED) || defined(__DOXYGEN__)
  312. #define STM32_HSI_ENABLED TRUE
  313. #endif
  314. /**
  315. * @brief Enables or disables the LSI clock source.
  316. */
  317. #if !defined(STM32_LSI_ENABLED) || defined(__DOXYGEN__)
  318. #define STM32_LSI_ENABLED TRUE
  319. #endif
  320. /**
  321. * @brief Enables or disables the HSE clock source.
  322. */
  323. #if !defined(STM32_HSE_ENABLED) || defined(__DOXYGEN__)
  324. #define STM32_HSE_ENABLED TRUE
  325. #endif
  326. /**
  327. * @brief Enables or disables the LSE clock source.
  328. */
  329. #if !defined(STM32_LSE_ENABLED) || defined(__DOXYGEN__)
  330. #define STM32_LSE_ENABLED FALSE
  331. #endif
  332. /**
  333. * @brief Main clock source selection.
  334. * @note If the selected clock source is not the PLL then the PLL is not
  335. * initialized and started.
  336. * @note The default value is calculated for a 72MHz system clock from
  337. * a 8MHz crystal using the PLL.
  338. */
  339. #if !defined(STM32_SW) || defined(__DOXYGEN__)
  340. #define STM32_SW STM32_SW_PLL
  341. #endif
  342. /**
  343. * @brief Clock source for the PLL.
  344. * @note This setting has only effect if the PLL is selected as the
  345. * system clock source.
  346. * @note The default value is calculated for a 72MHz system clock from
  347. * a 8MHz crystal using the PLL.
  348. */
  349. #if !defined(STM32_PLLSRC) || defined(__DOXYGEN__)
  350. #define STM32_PLLSRC STM32_PLLSRC_HSE
  351. #endif
  352. /**
  353. * @brief Crystal PLL pre-divider.
  354. * @note This setting has only effect if the PLL is selected as the
  355. * system clock source.
  356. * @note The default value is calculated for a 72MHz system clock from
  357. * a 8MHz crystal using the PLL.
  358. */
  359. #if !defined(STM32_PREDIV_VALUE) || defined(__DOXYGEN__)
  360. #define STM32_PREDIV_VALUE 1
  361. #endif
  362. /**
  363. * @brief PLL multiplier value.
  364. * @note The allowed range is 2...16.
  365. * @note The default value is calculated for a 72MHz system clock from
  366. * a 8MHz crystal using the PLL.
  367. */
  368. #if !defined(STM32_PLLMUL_VALUE) || defined(__DOXYGEN__)
  369. #define STM32_PLLMUL_VALUE 9
  370. #endif
  371. /**
  372. * @brief AHB prescaler value.
  373. * @note The default value is calculated for a 72MHz system clock from
  374. * a 8MHz crystal using the PLL.
  375. */
  376. #if !defined(STM32_HPRE) || defined(__DOXYGEN__)
  377. #define STM32_HPRE STM32_HPRE_DIV1
  378. #endif
  379. /**
  380. * @brief APB1 prescaler value.
  381. */
  382. #if !defined(STM32_PPRE1) || defined(__DOXYGEN__)
  383. #define STM32_PPRE1 STM32_PPRE1_DIV2
  384. #endif
  385. /**
  386. * @brief APB2 prescaler value.
  387. */
  388. #if !defined(STM32_PPRE2) || defined(__DOXYGEN__)
  389. #define STM32_PPRE2 STM32_PPRE2_DIV2
  390. #endif
  391. /**
  392. * @brief MCO pin setting.
  393. */
  394. #if !defined(STM32_MCOSEL) || defined(__DOXYGEN__)
  395. #define STM32_MCOSEL STM32_MCOSEL_NOCLOCK
  396. #endif
  397. /**
  398. * @brief ADC12 prescaler value.
  399. */
  400. #if !defined(STM32_ADC12PRES) || defined(__DOXYGEN__)
  401. #define STM32_ADC12PRES STM32_ADC12PRES_DIV1
  402. #endif
  403. /**
  404. * @brief ADC34 prescaler value.
  405. */
  406. #if !defined(STM32_ADC34PRES) || defined(__DOXYGEN__)
  407. #define STM32_ADC34PRES STM32_ADC34PRES_DIV1
  408. #endif
  409. /**
  410. * @brief USART1 clock source.
  411. */
  412. #if !defined(STM32_USART1SW) || defined(__DOXYGEN__)
  413. #define STM32_USART1SW STM32_USART1SW_PCLK
  414. #endif
  415. /**
  416. * @brief USART2 clock source.
  417. */
  418. #if !defined(STM32_USART2SW) || defined(__DOXYGEN__)
  419. #define STM32_USART2SW STM32_USART2SW_PCLK
  420. #endif
  421. /**
  422. * @brief USART3 clock source.
  423. */
  424. #if !defined(STM32_USART3SW) || defined(__DOXYGEN__)
  425. #define STM32_USART3SW STM32_USART3SW_PCLK
  426. #endif
  427. /**
  428. * @brief UART4 clock source.
  429. */
  430. #if !defined(STM32_UART4SW) || defined(__DOXYGEN__)
  431. #define STM32_UART4SW STM32_UART4SW_PCLK
  432. #endif
  433. /**
  434. * @brief UART5 clock source.
  435. */
  436. #if !defined(STM32_UART5SW) || defined(__DOXYGEN__)
  437. #define STM32_UART5SW STM32_UART5SW_PCLK
  438. #endif
  439. /**
  440. * @brief I2C1 clock source.
  441. */
  442. #if !defined(STM32_I2C1SW) || defined(__DOXYGEN__)
  443. #define STM32_I2C1SW STM32_I2C1SW_SYSCLK
  444. #endif
  445. /**
  446. * @brief I2C2 clock source.
  447. */
  448. #if !defined(STM32_I2C2SW) || defined(__DOXYGEN__)
  449. #define STM32_I2C2SW STM32_I2C2SW_SYSCLK
  450. #endif
  451. /**
  452. * @brief TIM1 clock source.
  453. */
  454. #if !defined(STM32_TIM1SW) || defined(__DOXYGEN__)
  455. #define STM32_TIM1SW STM32_TIM1SW_PCLK2
  456. #endif
  457. /**
  458. * @brief TIM8 clock source.
  459. */
  460. #if !defined(STM32_TIM8SW) || defined(__DOXYGEN__)
  461. #define STM32_TIM8SW STM32_TIM8SW_PCLK2
  462. #endif
  463. /**
  464. * @brief HRTIM1 clock source.
  465. */
  466. #if !defined(STM32_HRTIM1SW) || defined(__DOXYGEN__)
  467. #define STM32_HRTIM1SW STM32_HRTIM1SW_PCLK2
  468. #endif
  469. /**
  470. * @brief RTC clock source.
  471. */
  472. #if !defined(STM32_RTCSEL) || defined(__DOXYGEN__)
  473. #define STM32_RTCSEL STM32_RTCSEL_LSI
  474. #endif
  475. /**
  476. * @brief USB clock setting.
  477. */
  478. #if !defined(STM32_USB_CLOCK_REQUIRED) || defined(__DOXYGEN__)
  479. #define STM32_USB_CLOCK_REQUIRED TRUE
  480. #endif
  481. /**
  482. * @brief USB prescaler initialization.
  483. */
  484. #if !defined(STM32_USBPRE) || defined(__DOXYGEN__)
  485. #define STM32_USBPRE STM32_USBPRE_DIV1P5
  486. #endif
  487. /** @} */
  488. /*===========================================================================*/
  489. /* Derived constants and error checks. */
  490. /*===========================================================================*/
  491. /*
  492. * Configuration-related checks.
  493. */
  494. #if !defined(STM32F3xx_MCUCONF)
  495. #error "Using a wrong mcuconf.h file, STM32F3xx_MCUCONF not defined"
  496. #endif
  497. /* Only some devices have strongly checked mcuconf.h files. Others will be
  498. added gradually.*/
  499. #if (defined(STM32F303xC) || defined(STM32F303xE)) && \
  500. !defined(STM32F303_MCUCONF)
  501. #error "Using a wrong mcuconf.h file, STM32F303_MCUCONF not defined"
  502. #endif
  503. /*
  504. * HSI related checks.
  505. */
  506. #if STM32_HSI_ENABLED
  507. #else /* !STM32_HSI_ENABLED */
  508. #if STM32_SW == STM32_SW_HSI
  509. #error "HSI not enabled, required by STM32_SW"
  510. #endif
  511. #if STM32_USART1SW == STM32_USART1SW_HSI
  512. #error "HSI not enabled, required by STM32_USART1SW"
  513. #endif
  514. #if STM32_USART2SW == STM32_USART2SW_HSI
  515. #error "HSI not enabled, required by STM32_USART2SW"
  516. #endif
  517. #if STM32_USART3SW == STM32_USART3SW_HSI
  518. #error "HSI not enabled, required by STM32_USART3SW"
  519. #endif
  520. #if STM32_UART4SW == STM32_UART4SW_HSI
  521. #error "HSI not enabled, required by STM32_UART4SW"
  522. #endif
  523. #if STM32_UART5SW == STM32_UART5SW_HSI
  524. #error "HSI not enabled, required by STM32_UART5SW"
  525. #endif
  526. #if STM32_I2C1SW == STM32_I2C1SW_HSI
  527. #error "HSI not enabled, required by STM32_I2C1SW"
  528. #endif
  529. #if STM32_I2C2SW == STM32_I2C2SW_HSI
  530. #error "HSI not enabled, required by STM32_I2C2SW"
  531. #endif
  532. #if (STM32_SW == STM32_SW_PLL) && (STM32_PLLSRC == STM32_PLLSRC_HSI)
  533. #error "HSI not enabled, required by STM32_SW and STM32_PLLSRC"
  534. #endif
  535. #if (STM32_MCOSEL == STM32_MCOSEL_HSI) || \
  536. ((STM32_MCOSEL == STM32_MCOSEL_PLLDIV2) && \
  537. (STM32_PLLSRC == STM32_PLLSRC_HSI))
  538. #error "HSI not enabled, required by STM32_MCOSEL"
  539. #endif
  540. #endif /* !STM32_HSI_ENABLED */
  541. /*
  542. * HSE related checks.
  543. */
  544. #if STM32_HSE_ENABLED
  545. #if STM32_HSECLK == 0
  546. #error "HSE frequency not defined"
  547. #elif (STM32_HSECLK < STM32_HSECLK_MIN) || (STM32_HSECLK > STM32_HSECLK_MAX)
  548. #error "STM32_HSECLK outside acceptable range (STM32_HSECLK_MIN...STM32_HSECLK_MAX)"
  549. #endif
  550. #else /* !STM32_HSE_ENABLED */
  551. #if STM32_SW == STM32_SW_HSE
  552. #error "HSE not enabled, required by STM32_SW"
  553. #endif
  554. #if (STM32_SW == STM32_SW_PLL) && (STM32_PLLSRC == STM32_PLLSRC_HSE)
  555. #error "HSE not enabled, required by STM32_SW and STM32_PLLSRC"
  556. #endif
  557. #if (STM32_MCOSEL == STM32_MCOSEL_HSE) || \
  558. ((STM32_MCOSEL == STM32_MCOSEL_PLLDIV2) && \
  559. (STM32_PLLSRC == STM32_PLLSRC_HSE))
  560. #error "HSE not enabled, required by STM32_MCOSEL"
  561. #endif
  562. #if STM32_RTCSEL == STM32_RTCSEL_HSEDIV
  563. #error "HSE not enabled, required by STM32_RTCSEL"
  564. #endif
  565. #endif /* !STM32_HSE_ENABLED */
  566. /*
  567. * LSI related checks.
  568. */
  569. #if STM32_LSI_ENABLED
  570. #else /* !STM32_LSI_ENABLED */
  571. #if STM32_RTCSEL == STM32_RTCSEL_LSI
  572. #error "LSI not enabled, required by STM32_RTCSEL"
  573. #endif
  574. #endif /* !STM32_LSI_ENABLED */
  575. /*
  576. * LSE related checks.
  577. */
  578. #if STM32_LSE_ENABLED
  579. #if !defined(STM32_LSECLK) || (STM32_LSECLK == 0)
  580. #error "STM32_LSECLK not defined"
  581. #endif
  582. #if (STM32_LSECLK < STM32_LSECLK_MIN) || (STM32_LSECLK > STM32_LSECLK_MAX)
  583. #error "STM32_LSECLK outside acceptable range (STM32_LSECLK_MIN...STM32_LSECLK_MAX)"
  584. #endif
  585. #if !defined(STM32_LSEDRV)
  586. #error "STM32_LSEDRV not defined"
  587. #endif
  588. #if (STM32_LSEDRV >> 3) > 3
  589. #error "STM32_LSEDRV outside acceptable range ((0<<3)...(3<<3))"
  590. #endif
  591. #if STM32_USART1SW == STM32_USART1SW_LSE
  592. #error "LSE not enabled, required by STM32_USART1SW"
  593. #endif
  594. #if STM32_USART2SW == STM32_USART2SW_LSE
  595. #error "LSE not enabled, required by STM32_USART2SW"
  596. #endif
  597. #if STM32_USART3SW == STM32_USART3SW_LSE
  598. #error "LSE not enabled, required by STM32_USART3SW"
  599. #endif
  600. #if STM32_UART4SW == STM32_UART4SW_LSE
  601. #error "LSE not enabled, required by STM32_UART4SW"
  602. #endif
  603. #if STM32_UART5SW == STM32_UART5SW_LSE
  604. #error "LSE not enabled, required by STM32_UART5SW"
  605. #endif
  606. #else /* !STM32_LSE_ENABLED */
  607. #if STM32_RTCSEL == STM32_RTCSEL_LSE
  608. #error "LSE not enabled, required by STM32_RTCSEL"
  609. #endif
  610. #endif /* !STM32_LSE_ENABLED */
  611. /* PLL activation conditions.*/
  612. #if (STM32_SW == STM32_SW_PLL) || \
  613. (STM32_MCOSEL == STM32_MCOSEL_PLLDIV2) || \
  614. (STM32_TIM1SW == STM32_TIM1SW_PLLX2) || \
  615. (STM32_TIM8SW == STM32_TIM8SW_PLLX2) || \
  616. (STM32_ADC12PRES != STM32_ADC12PRES_NOCLOCK) || \
  617. (STM32_ADC34PRES != STM32_ADC34PRES_NOCLOCK) || \
  618. STM32_USB_CLOCK_REQUIRED || \
  619. defined(__DOXYGEN__)
  620. /**
  621. * @brief PLL activation flag.
  622. */
  623. #define STM32_ACTIVATE_PLL TRUE
  624. #else
  625. #define STM32_ACTIVATE_PLL FALSE
  626. #endif
  627. /* HSE prescaler setting check.*/
  628. #if ((STM32_PREDIV_VALUE >= 1) || (STM32_PREDIV_VALUE <= 16))
  629. #define STM32_PREDIV ((STM32_PREDIV_VALUE - 1) << 0)
  630. #else
  631. #error "invalid STM32_PREDIV value specified"
  632. #endif
  633. /**
  634. * @brief PLLMUL field.
  635. */
  636. #if ((STM32_PLLMUL_VALUE >= 2) && (STM32_PLLMUL_VALUE <= 16)) || \
  637. defined(__DOXYGEN__)
  638. #define STM32_PLLMUL ((STM32_PLLMUL_VALUE - 2) << 18)
  639. #else
  640. #error "invalid STM32_PLLMUL_VALUE value specified"
  641. #endif
  642. /**
  643. * @brief PLL input clock frequency.
  644. */
  645. #if (STM32_PLLSRC == STM32_PLLSRC_HSE) || defined(__DOXYGEN__)
  646. #define STM32_PLLCLKIN (STM32_HSECLK / STM32_PREDIV_VALUE)
  647. #elif STM32_PLLSRC == STM32_PLLSRC_HSI
  648. #define STM32_PLLCLKIN (STM32_HSICLK / 2)
  649. #else
  650. #error "invalid STM32_PLLSRC value specified"
  651. #endif
  652. /* PLL input frequency range check.*/
  653. #if (STM32_PLLCLKIN < STM32_PLLIN_MIN) || (STM32_PLLCLKIN > STM32_PLLIN_MAX)
  654. #error "STM32_PLLCLKIN outside acceptable range (STM32_PLLIN_MIN...STM32_PLLIN_MAX)"
  655. #endif
  656. /**
  657. * @brief PLL output clock frequency.
  658. */
  659. #define STM32_PLLCLKOUT (STM32_PLLCLKIN * STM32_PLLMUL_VALUE)
  660. /* PLL output frequency range check.*/
  661. #if (STM32_PLLCLKOUT < STM32_PLLOUT_MIN) || (STM32_PLLCLKOUT > STM32_PLLOUT_MAX)
  662. #error "STM32_PLLCLKOUT outside acceptable range (STM32_PLLOUT_MIN...STM32_PLLOUT_MAX)"
  663. #endif
  664. /**
  665. * @brief System clock source.
  666. */
  667. #if (STM32_SW == STM32_SW_PLL) || defined(__DOXYGEN__)
  668. #define STM32_SYSCLK STM32_PLLCLKOUT
  669. #elif (STM32_SW == STM32_SW_HSI)
  670. #define STM32_SYSCLK STM32_HSICLK
  671. #elif (STM32_SW == STM32_SW_HSE)
  672. #define STM32_SYSCLK STM32_HSECLK
  673. #else
  674. #error "invalid STM32_SW value specified"
  675. #endif
  676. /* Check on the system clock.*/
  677. #if STM32_SYSCLK > STM32_SYSCLK_MAX
  678. #error "STM32_SYSCLK above maximum rated frequency (STM32_SYSCLK_MAX)"
  679. #endif
  680. /**
  681. * @brief AHB frequency.
  682. */
  683. #if (STM32_HPRE == STM32_HPRE_DIV1) || defined(__DOXYGEN__)
  684. #define STM32_HCLK (STM32_SYSCLK / 1)
  685. #elif STM32_HPRE == STM32_HPRE_DIV2
  686. #define STM32_HCLK (STM32_SYSCLK / 2)
  687. #elif STM32_HPRE == STM32_HPRE_DIV4
  688. #define STM32_HCLK (STM32_SYSCLK / 4)
  689. #elif STM32_HPRE == STM32_HPRE_DIV8
  690. #define STM32_HCLK (STM32_SYSCLK / 8)
  691. #elif STM32_HPRE == STM32_HPRE_DIV16
  692. #define STM32_HCLK (STM32_SYSCLK / 16)
  693. #elif STM32_HPRE == STM32_HPRE_DIV64
  694. #define STM32_HCLK (STM32_SYSCLK / 64)
  695. #elif STM32_HPRE == STM32_HPRE_DIV128
  696. #define STM32_HCLK (STM32_SYSCLK / 128)
  697. #elif STM32_HPRE == STM32_HPRE_DIV256
  698. #define STM32_HCLK (STM32_SYSCLK / 256)
  699. #elif STM32_HPRE == STM32_HPRE_DIV512
  700. #define STM32_HCLK (STM32_SYSCLK / 512)
  701. #else
  702. #error "invalid STM32_HPRE value specified"
  703. #endif
  704. /* AHB frequency check.*/
  705. #if STM32_HCLK > STM32_SYSCLK_MAX
  706. #error "STM32_HCLK exceeding maximum frequency (STM32_SYSCLK_MAX)"
  707. #endif
  708. /**
  709. * @brief APB1 frequency.
  710. */
  711. #if (STM32_PPRE1 == STM32_PPRE1_DIV1) || defined(__DOXYGEN__)
  712. #define STM32_PCLK1 (STM32_HCLK / 1)
  713. #elif STM32_PPRE1 == STM32_PPRE1_DIV2
  714. #define STM32_PCLK1 (STM32_HCLK / 2)
  715. #elif STM32_PPRE1 == STM32_PPRE1_DIV4
  716. #define STM32_PCLK1 (STM32_HCLK / 4)
  717. #elif STM32_PPRE1 == STM32_PPRE1_DIV8
  718. #define STM32_PCLK1 (STM32_HCLK / 8)
  719. #elif STM32_PPRE1 == STM32_PPRE1_DIV16
  720. #define STM32_PCLK1 (STM32_HCLK / 16)
  721. #else
  722. #error "invalid STM32_PPRE1 value specified"
  723. #endif
  724. /* APB1 frequency check.*/
  725. #if STM32_PCLK1 > STM32_PCLK1_MAX
  726. #error "STM32_PCLK1 exceeding maximum frequency (STM32_PCLK1_MAX)"
  727. #endif
  728. /**
  729. * @brief APB2 frequency.
  730. */
  731. #if (STM32_PPRE2 == STM32_PPRE2_DIV1) || defined(__DOXYGEN__)
  732. #define STM32_PCLK2 (STM32_HCLK / 1)
  733. #elif STM32_PPRE2 == STM32_PPRE2_DIV2
  734. #define STM32_PCLK2 (STM32_HCLK / 2)
  735. #elif STM32_PPRE2 == STM32_PPRE2_DIV4
  736. #define STM32_PCLK2 (STM32_HCLK / 4)
  737. #elif STM32_PPRE2 == STM32_PPRE2_DIV8
  738. #define STM32_PCLK2 (STM32_HCLK / 8)
  739. #elif STM32_PPRE2 == STM32_PPRE2_DIV16
  740. #define STM32_PCLK2 (STM32_HCLK / 16)
  741. #else
  742. #error "invalid STM32_PPRE2 value specified"
  743. #endif
  744. /* APB2 frequency check.*/
  745. #if STM32_PCLK2 > STM32_PCLK2_MAX
  746. #error "STM32_PCLK2 exceeding maximum frequency (STM32_PCLK2_MAX)"
  747. #endif
  748. /**
  749. * @brief RTC clock.
  750. */
  751. #if (STM32_RTCSEL == STM32_RTCSEL_LSE) || defined(__DOXYGEN__)
  752. #define STM32_RTCCLK STM32_LSECLK
  753. #elif STM32_RTCSEL == STM32_RTCSEL_LSI
  754. #define STM32_RTCCLK STM32_LSICLK
  755. #elif STM32_RTCSEL == STM32_RTCSEL_HSEDIV
  756. #define STM32_RTCCLK (STM32_HSECLK / 32)
  757. #elif STM32_RTCSEL == STM32_RTCSEL_NOCLOCK
  758. #define STM32_RTCCLK 0
  759. #else
  760. #error "invalid source selected for RTC clock"
  761. #endif
  762. /**
  763. * @brief ADC12 frequency.
  764. */
  765. #if (STM32_ADC12PRES == STM32_ADC12PRES_NOCLOCK) || defined(__DOXYGEN__)
  766. #define STM32_ADC12CLK 0
  767. #elif STM32_ADC12PRES == STM32_ADC12PRES_DIV1
  768. #define STM32_ADC12CLK (STM32_PLLCLKOUT / 1)
  769. #elif STM32_ADC12PRES == STM32_ADC12PRES_DIV2
  770. #define STM32_ADC12CLK (STM32_PLLCLKOUT / 2)
  771. #elif STM32_ADC12PRES == STM32_ADC12PRES_DIV4
  772. #define STM32_ADC12CLK (STM32_PLLCLKOUT / 4)
  773. #elif STM32_ADC12PRES == STM32_ADC12PRES_DIV6
  774. #define STM32_ADC12CLK (STM32_PLLCLKOUT / 6)
  775. #elif STM32_ADC12PRES == STM32_ADC12PRES_DIV8
  776. #define STM32_ADC12CLK (STM32_PLLCLKOUT / 8)
  777. #elif STM32_ADC12PRES == STM32_ADC12PRES_DIV10
  778. #define STM32_ADC12CLK (STM32_PLLCLKOUT / 10)
  779. #elif STM32_ADC12PRES == STM32_ADC12PRES_DIV12
  780. #define STM32_ADC12CLK (STM32_PLLCLKOUT / 12)
  781. #elif STM32_ADC12PRES == STM32_ADC12PRES_DIV16
  782. #define STM32_ADC12CLK (STM32_PLLCLKOUT / 16)
  783. #elif STM32_ADC12PRES == STM32_ADC12PRES_DIV32
  784. #define STM32_ADC12CLK (STM32_PLLCLKOUT / 32)
  785. #elif STM32_ADC12PRES == STM32_ADC12PRES_DIV64
  786. #define STM32_ADC12CLK (STM32_PLLCLKOUT / 64)
  787. #elif STM32_ADC12PRES == STM32_ADC12PRES_DIV128
  788. #define STM32_ADC12CLK (STM32_PLLCLKOUT / 128)
  789. #elif STM32_ADC12PRES == STM32_ADC12PRES_DIV256
  790. #define STM32_ADC12CLK (STM32_PLLCLKOUT / 256)
  791. #else
  792. #error "invalid STM32_ADC12PRES value specified"
  793. #endif
  794. /**
  795. * @brief ADC34 frequency.
  796. */
  797. #if (STM32_ADC34PRES == STM32_ADC34PRES_NOCLOCK) || defined(__DOXYGEN__)
  798. #define STM32_ADC34CLK 0
  799. #elif STM32_ADC34PRES == STM32_ADC34PRES_DIV1
  800. #define STM32_ADC34CLK (STM32_PLLCLKOUT / 1)
  801. #elif STM32_ADC34PRES == STM32_ADC34PRES_DIV2
  802. #define STM32_ADC34CLK (STM32_PLLCLKOUT / 2)
  803. #elif STM32_ADC34PRES == STM32_ADC34PRES_DIV4
  804. #define STM32_ADC34CLK (STM32_PLLCLKOUT / 4)
  805. #elif STM32_ADC34PRES == STM32_ADC34PRES_DIV6
  806. #define STM32_ADC34CLK (STM32_PLLCLKOUT / 6)
  807. #elif STM32_ADC34PRES == STM32_ADC34PRES_DIV8
  808. #define STM32_ADC34CLK (STM32_PLLCLKOUT / 8)
  809. #elif STM32_ADC34PRES == STM32_ADC34PRES_DIV10
  810. #define STM32_ADC34CLK (STM32_PLLCLKOUT / 10)
  811. #elif STM32_ADC34PRES == STM32_ADC34PRES_DIV12
  812. #define STM32_ADC34CLK (STM32_PLLCLKOUT / 12)
  813. #elif STM32_ADC34PRES == STM32_ADC34PRES_DIV16
  814. #define STM32_ADC34CLK (STM32_PLLCLKOUT / 16)
  815. #elif STM32_ADC34PRES == STM32_ADC34PRES_DIV32
  816. #define STM32_ADC34CLK (STM32_PLLCLKOUT / 32)
  817. #elif STM32_ADC34PRES == STM32_ADC34PRES_DIV64
  818. #define STM32_ADC34CLK (STM32_PLLCLKOUT / 64)
  819. #elif STM32_ADC34PRES == STM32_ADC34PRES_DIV128
  820. #define STM32_ADC34CLK (STM32_PLLCLKOUT / 128)
  821. #elif STM32_ADC34PRES == STM32_ADC34PRES_DIV256
  822. #define STM32_ADC34CLK (STM32_PLLCLKOUT / 256)
  823. #else
  824. #error "invalid STM32_ADC34PRES value specified"
  825. #endif
  826. /* ADC12 frequency check.*/
  827. #if STM32_ADC12CLK > STM32_ADCCLK_MAX
  828. #error "STM32_ADC12CLK exceeding maximum frequency (STM32_ADCCLK_MAX)"
  829. #endif
  830. /* ADC34 frequency check.*/
  831. #if STM32_ADC34CLK > STM32_ADCCLK_MAX
  832. #error "STM32_ADC34CLK exceeding maximum frequency (STM32_ADCCLK_MAX)"
  833. #endif
  834. /**
  835. * @brief I2C1 frequency.
  836. */
  837. #if STM32_I2C1SW == STM32_I2C1SW_HSI
  838. #define STM32_I2C1CLK STM32_HSICLK
  839. #elif STM32_I2C1SW == STM32_I2C1SW_SYSCLK
  840. #define STM32_I2C1CLK STM32_SYSCLK
  841. #else
  842. #error "invalid source selected for I2C1 clock"
  843. #endif
  844. /**
  845. * @brief I2C2 frequency.
  846. */
  847. #if STM32_I2C2SW == STM32_I2C2SW_HSI
  848. #define STM32_I2C2CLK STM32_HSICLK
  849. #elif STM32_I2C2SW == STM32_I2C2SW_SYSCLK
  850. #define STM32_I2C2CLK STM32_SYSCLK
  851. #else
  852. #error "invalid source selected for I2C2 clock"
  853. #endif
  854. /**
  855. * @brief USART1 frequency.
  856. */
  857. #if STM32_USART1SW == STM32_USART1SW_PCLK
  858. #define STM32_USART1CLK STM32_PCLK2
  859. #elif STM32_USART1SW == STM32_USART1SW_SYSCLK
  860. #define STM32_USART1CLK STM32_SYSCLK
  861. #elif STM32_USART1SW == STM32_USART1SW_LSE
  862. #define STM32_USART1CLK STM32_LSECLK
  863. #elif STM32_USART1SW == STM32_USART1SW_HSI
  864. #define STM32_USART1CLK STM32_HSICLK
  865. #else
  866. #error "invalid source selected for USART1 clock"
  867. #endif
  868. /**
  869. * @brief USART2 frequency.
  870. */
  871. #if STM32_USART2SW == STM32_USART2SW_PCLK
  872. #define STM32_USART2CLK STM32_PCLK1
  873. #elif STM32_USART2SW == STM32_USART2SW_SYSCLK
  874. #define STM32_USART2CLK STM32_SYSCLK
  875. #elif STM32_USART2SW == STM32_USART2SW_LSE
  876. #define STM32_USART2CLK STM32_LSECLK
  877. #elif STM32_USART2SW == STM32_USART2SW_HSI
  878. #define STM32_USART2CLK STM32_HSICLK
  879. #else
  880. #error "invalid source selected for USART2 clock"
  881. #endif
  882. /**
  883. * @brief USART3 frequency.
  884. */
  885. #if STM32_USART3SW == STM32_USART3SW_PCLK
  886. #define STM32_USART3CLK STM32_PCLK1
  887. #elif STM32_USART3SW == STM32_USART3SW_SYSCLK
  888. #define STM32_USART3CLK STM32_SYSCLK
  889. #elif STM32_USART3SW == STM32_USART3SW_LSE
  890. #define STM32_USART3CLK STM32_LSECLK
  891. #elif STM32_USART3SW == STM32_USART3SW_HSI
  892. #define STM32_USART3CLK STM32_HSICLK
  893. #else
  894. #error "invalid source selected for USART3 clock"
  895. #endif
  896. /**
  897. * @brief UART4 frequency.
  898. */
  899. #if STM32_UART4SW == STM32_UART4SW_PCLK
  900. #define STM32_UART4CLK STM32_PCLK1
  901. #elif STM32_UART4SW == STM32_UART4SW_SYSCLK
  902. #define STM32_UART4CLK STM32_SYSCLK
  903. #elif STM32_UART4SW == STM32_UART4SW_LSE
  904. #define STM32_UART4CLK STM32_LSECLK
  905. #elif STM32_UART4SW == STM32_UART4SW_HSI
  906. #define STM32_UART4CLK STM32_HSICLK
  907. #else
  908. #error "invalid source selected for UART4 clock"
  909. #endif
  910. /**
  911. * @brief UART5 frequency.
  912. */
  913. #if STM32_UART5SW == STM32_UART5SW_PCLK
  914. #define STM32_UART5CLK STM32_PCLK1
  915. #elif STM32_UART5SW == STM32_UART5SW_SYSCLK
  916. #define STM32_UART5CLK STM32_SYSCLK
  917. #elif STM32_UART5SW == STM32_UART5SW_LSE
  918. #define STM32_UART5CLK STM32_LSECLK
  919. #elif STM32_UART5SW == STM32_UART5SW_HSI
  920. #define STM32_UART5CLK STM32_HSICLK
  921. #else
  922. #error "invalid source selected for UART5 clock"
  923. #endif
  924. /**
  925. * @brief TIM1 frequency.
  926. */
  927. #if STM32_TIM1SW == STM32_TIM1SW_PCLK2
  928. #if STM32_PPRE2 == STM32_PPRE2_DIV1
  929. #define STM32_TIM1CLK STM32_PCLK2
  930. #else
  931. #define STM32_TIM1CLK (STM32_PCLK2 * 2)
  932. #endif
  933. #elif STM32_TIM1SW == STM32_TIM1SW_PLLX2
  934. #if (STM32_SW != STM32_SW_PLL) || \
  935. (STM32_HPRE != STM32_HPRE_DIV1) || \
  936. (STM32_PPRE2 != STM32_PPRE2_DIV1)
  937. #error "double clock mode cannot be activated for TIM1 under the current settings"
  938. #endif
  939. #define STM32_TIM1CLK (STM32_PLLCLKOUT * 2)
  940. #else
  941. #error "invalid source selected for TIM1 clock"
  942. #endif
  943. /**
  944. * @brief TIM8 frequency.
  945. */
  946. #if STM32_TIM8SW == STM32_TIM8SW_PCLK2
  947. #if STM32_PPRE2 == STM32_PPRE2_DIV1
  948. #define STM32_TIM8CLK STM32_PCLK2
  949. #else
  950. #define STM32_TIM8CLK (STM32_PCLK2 * 2)
  951. #endif
  952. #elif STM32_TIM8SW == STM32_TIM8SW_PLLX2
  953. #if (STM32_SW != STM32_SW_PLL) || \
  954. (STM32_HPRE != STM32_HPRE_DIV1) || \
  955. (STM32_PPRE2 != STM32_PPRE2_DIV1)
  956. #error "double clock mode cannot be activated for TIM8 under the current settings"
  957. #endif
  958. #define STM32_TIM8CLK (STM32_PLLCLKOUT * 2)
  959. #else
  960. #error "invalid source selected for TIM8 clock"
  961. #endif
  962. /**
  963. * @brief HRTIM1 frequency.
  964. */
  965. #if STM32_HRTIM1SW == STM32_HRTIM1SW_PCLK2
  966. #if STM32_PPRE2 == STM32_PPRE2_DIV1
  967. #define STM32_HRTIM1CLK STM32_PCLK2
  968. #else
  969. #define STM32_HRTIM1CLK (STM32_PCLK2 * 2)
  970. #endif
  971. #elif STM32_HRTIM1SW == STM32_HRTIM1SW_PLLX2
  972. #if (STM32_SW != STM32_SW_PLL) || \
  973. (STM32_HPRE != STM32_HPRE_DIV1) || \
  974. (STM32_PPRE2 != STM32_PPRE2_DIV1)
  975. #error "double clock mode cannot be activated for HRTIM1 under the current settings"
  976. #endif
  977. #define STM32_HRTIM1CLK (STM32_PLLCLKOUT * 2)
  978. #else
  979. #error "invalid source selected for HRTIM1 clock"
  980. #endif
  981. /**
  982. * @brief Timers 2, 3, 4, 6, 7 frequency.
  983. */
  984. #if (STM32_PPRE1 == STM32_PPRE1_DIV1) || defined(__DOXYGEN__)
  985. #define STM32_TIMCLK1 (STM32_PCLK1 * 1)
  986. #else
  987. #define STM32_TIMCLK1 (STM32_PCLK1 * 2)
  988. #endif
  989. /**
  990. * @brief Timers 1, 8, 15, 16, 17 frequency.
  991. */
  992. #if (STM32_PPRE2 == STM32_PPRE2_DIV1) || defined(__DOXYGEN__)
  993. #define STM32_TIMCLK2 (STM32_PCLK2 * 1)
  994. #else
  995. #define STM32_TIMCLK2 (STM32_PCLK2 * 2)
  996. #endif
  997. /**
  998. * @brief USB frequency.
  999. */
  1000. #if (STM32_USBPRE == STM32_USBPRE_DIV1P5) || defined(__DOXYGEN__)
  1001. #define STM32_USBCLK ((STM32_PLLCLKOUT * 2) / 3)
  1002. #elif (STM32_USBPRE == STM32_USBPRE_DIV1)
  1003. #define STM32_USBCLK STM32_PLLCLKOUT
  1004. #else
  1005. #error "invalid STM32_USBPRE value specified"
  1006. #endif
  1007. /**
  1008. * @brief Flash settings.
  1009. */
  1010. #if (STM32_HCLK <= 24000000) || defined(__DOXYGEN__)
  1011. #define STM32_FLASHBITS 0x00000010
  1012. #elif STM32_HCLK <= 48000000
  1013. #define STM32_FLASHBITS 0x00000011
  1014. #else
  1015. #define STM32_FLASHBITS 0x00000012
  1016. #endif
  1017. /*===========================================================================*/
  1018. /* Driver data structures and types. */
  1019. /*===========================================================================*/
  1020. /*===========================================================================*/
  1021. /* Driver macros. */
  1022. /*===========================================================================*/
  1023. /*===========================================================================*/
  1024. /* External declarations. */
  1025. /*===========================================================================*/
  1026. /* Various helpers.*/
  1027. #include "nvic.h"
  1028. #include "cache.h"
  1029. #include "mpu_v7m.h"
  1030. #include "stm32_isr.h"
  1031. #include "stm32_dma.h"
  1032. #include "stm32_exti.h"
  1033. #include "stm32_rcc.h"
  1034. #ifdef __cplusplus
  1035. extern "C" {
  1036. #endif
  1037. void hal_lld_init(void);
  1038. void stm32_clock_init(void);
  1039. #ifdef __cplusplus
  1040. }
  1041. #endif
  1042. #endif /* HAL_LLD_H */
  1043. /** @} */