hal_lld.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  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 STM32L1xx/hal_lld.h
  15. * @brief STM32L1xx 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_HSECLK.
  20. * - STM32_HSE_BYPASS (optionally).
  21. * .
  22. * One of the following macros must also be defined:
  23. * - STM32L100xB, STM32L100xBA, STM32L100xC.
  24. * - STM32L151xB, STM32L151xBA, STM32L151xC, STM32L151xCA,
  25. * STM32L151xD, STM32L151xDX, STM32L151xE.
  26. * - STM32L152xB, STM32L152xBA, STM32L152xC, STM32L152xCA,
  27. * STM32L152xD, STM32L152xDX, STM32L152xE.
  28. * - STM32L162xC, STM32L162xCA, STM32L162xD, STM32L162xDX,
  29. * STM32L162xE.
  30. * .
  31. *
  32. * @addtogroup HAL
  33. * @{
  34. */
  35. #ifndef HAL_LLD_H
  36. #define HAL_LLD_H
  37. #include "stm32_registry.h"
  38. /*===========================================================================*/
  39. /* Driver constants. */
  40. /*===========================================================================*/
  41. /**
  42. * @name Platform identification
  43. * @{
  44. */
  45. #if defined(STM32L100xB) || defined(STM32L151xB) || \
  46. defined(STM32L152xB) || defined(__DOXYGEN__)
  47. #define PLATFORM_NAME "STM32L1xx Ultra Low Power Medium Density"
  48. #elif defined(STM32L100xBA) || defined(STM32L100xC) || \
  49. defined(STM32L151xBA) || defined(STM32L151xC) || \
  50. defined(STM32L151xCA) || defined(STM32L152xBA) || \
  51. defined(STM32L152xC) || defined(STM32L152xCA) || \
  52. defined(STM32L162xC) || defined(STM32L162xCA)
  53. #define PLATFORM_NAME "STM32L1xx Ultra Low Power Medium Density Plus"
  54. #elif defined(STM32L151xD) || defined(STM32L151xDX) || \
  55. defined(STM32L151xE) || defined(STM32L152xD) || \
  56. defined(STM32L152xDX) || defined(STM32L152xE) || \
  57. defined(STM32L162xD) || defined(STM32L162xDX) || \
  58. defined(STM32L162xE)
  59. #define PLATFORM_NAME "STM32L1xx Ultra Low Power High Density"
  60. #else
  61. #error "STM32L1xx device not specified"
  62. #endif
  63. /**
  64. * @brief Sub-family identifier.
  65. */
  66. #if !defined(STM32L1XX) || defined(__DOXYGEN__)
  67. #define STM32L1XX
  68. #endif
  69. /** @} */
  70. /**
  71. * @name Internal clock sources
  72. * @{
  73. */
  74. #define STM32_HSICLK 16000000 /**< High speed internal clock. */
  75. #define STM32_LSICLK 38000 /**< Low speed internal clock. */
  76. /** @} */
  77. /**
  78. * @name PWR_CR register bits definitions
  79. * @{
  80. */
  81. #define STM32_VOS_MASK (3 << 11) /**< Core voltage mask. */
  82. #define STM32_VOS_1P8 (1 << 11) /**< Core voltage 1.8 Volts. */
  83. #define STM32_VOS_1P5 (2 << 11) /**< Core voltage 1.5 Volts. */
  84. #define STM32_VOS_1P2 (3 << 11) /**< Core voltage 1.2 Volts. */
  85. #define STM32_PLS_MASK (7 << 5) /**< PLS bits mask. */
  86. #define STM32_PLS_LEV0 (0 << 5) /**< PVD level 0. */
  87. #define STM32_PLS_LEV1 (1 << 5) /**< PVD level 1. */
  88. #define STM32_PLS_LEV2 (2 << 5) /**< PVD level 2. */
  89. #define STM32_PLS_LEV3 (3 << 5) /**< PVD level 3. */
  90. #define STM32_PLS_LEV4 (4 << 5) /**< PVD level 4. */
  91. #define STM32_PLS_LEV5 (5 << 5) /**< PVD level 5. */
  92. #define STM32_PLS_LEV6 (6 << 5) /**< PVD level 6. */
  93. #define STM32_PLS_LEV7 (7 << 5) /**< PVD level 7. */
  94. /** @} */
  95. /**
  96. * @name RCC_CR register bits definitions
  97. * @{
  98. */
  99. #define STM32_RTCPRE_MASK (3 << 29) /**< RTCPRE mask. */
  100. #define STM32_RTCPRE_DIV2 (0 << 29) /**< HSE divided by 2. */
  101. #define STM32_RTCPRE_DIV4 (1 << 29) /**< HSE divided by 4. */
  102. #define STM32_RTCPRE_DIV8 (2 << 29) /**< HSE divided by 2. */
  103. #define STM32_RTCPRE_DIV16 (3 << 29) /**< HSE divided by 16. */
  104. /** @} */
  105. /**
  106. * @name RCC_CFGR register bits definitions
  107. * @{
  108. */
  109. #define STM32_SW_MSI (0 << 0) /**< SYSCLK source is MSI. */
  110. #define STM32_SW_HSI (1 << 0) /**< SYSCLK source is HSI. */
  111. #define STM32_SW_HSE (2 << 0) /**< SYSCLK source is HSE. */
  112. #define STM32_SW_PLL (3 << 0) /**< SYSCLK source is PLL. */
  113. #define STM32_HPRE_DIV1 (0 << 4) /**< SYSCLK divided by 1. */
  114. #define STM32_HPRE_DIV2 (8 << 4) /**< SYSCLK divided by 2. */
  115. #define STM32_HPRE_DIV4 (9 << 4) /**< SYSCLK divided by 4. */
  116. #define STM32_HPRE_DIV8 (10 << 4) /**< SYSCLK divided by 8. */
  117. #define STM32_HPRE_DIV16 (11 << 4) /**< SYSCLK divided by 16. */
  118. #define STM32_HPRE_DIV64 (12 << 4) /**< SYSCLK divided by 64. */
  119. #define STM32_HPRE_DIV128 (13 << 4) /**< SYSCLK divided by 128. */
  120. #define STM32_HPRE_DIV256 (14 << 4) /**< SYSCLK divided by 256. */
  121. #define STM32_HPRE_DIV512 (15 << 4) /**< SYSCLK divided by 512. */
  122. #define STM32_PPRE1_DIV1 (0 << 8) /**< HCLK divided by 1. */
  123. #define STM32_PPRE1_DIV2 (4 << 8) /**< HCLK divided by 2. */
  124. #define STM32_PPRE1_DIV4 (5 << 8) /**< HCLK divided by 4. */
  125. #define STM32_PPRE1_DIV8 (6 << 8) /**< HCLK divided by 8. */
  126. #define STM32_PPRE1_DIV16 (7 << 8) /**< HCLK divided by 16. */
  127. #define STM32_PPRE2_DIV1 (0 << 11) /**< HCLK divided by 1. */
  128. #define STM32_PPRE2_DIV2 (4 << 11) /**< HCLK divided by 2. */
  129. #define STM32_PPRE2_DIV4 (5 << 11) /**< HCLK divided by 4. */
  130. #define STM32_PPRE2_DIV8 (6 << 11) /**< HCLK divided by 8. */
  131. #define STM32_PPRE2_DIV16 (7 << 11) /**< HCLK divided by 16. */
  132. #define STM32_PLLSRC_HSI (0 << 16) /**< PLL clock source is HSI. */
  133. #define STM32_PLLSRC_HSE (1 << 16) /**< PLL clock source is HSE. */
  134. #define STM32_MCOSEL_NOCLOCK (0 << 24) /**< No clock on MCO pin. */
  135. #define STM32_MCOSEL_SYSCLK (1 << 24) /**< SYSCLK on MCO pin. */
  136. #define STM32_MCOSEL_HSI (2 << 24) /**< HSI clock on MCO pin. */
  137. #define STM32_MCOSEL_MSI (3 << 24) /**< MSI clock on MCO pin. */
  138. #define STM32_MCOSEL_HSE (4 << 24) /**< HSE clock on MCO pin. */
  139. #define STM32_MCOSEL_PLL (5 << 24) /**< PLL clock on MCO pin. */
  140. #define STM32_MCOSEL_LSI (6 << 24) /**< LSI clock on MCO pin. */
  141. #define STM32_MCOSEL_LSE (7 << 24) /**< LSE clock on MCO pin. */
  142. #define STM32_MCOPRE_DIV1 (0 << 28) /**< MCO divided by 1. */
  143. #define STM32_MCOPRE_DIV2 (1 << 28) /**< MCO divided by 2. */
  144. #define STM32_MCOPRE_DIV4 (2 << 28) /**< MCO divided by 4. */
  145. #define STM32_MCOPRE_DIV8 (3 << 28) /**< MCO divided by 8. */
  146. #define STM32_MCOPRE_DIV16 (4 << 28) /**< MCO divided by 16. */
  147. /** @} */
  148. /**
  149. * @name RCC_ICSCR register bits definitions
  150. * @{
  151. */
  152. #define STM32_MSIRANGE_MASK (7 << 13) /**< MSIRANGE field mask. */
  153. #define STM32_MSIRANGE_64K (0 << 13) /**< 64kHz nominal. */
  154. #define STM32_MSIRANGE_128K (1 << 13) /**< 128kHz nominal. */
  155. #define STM32_MSIRANGE_256K (2 << 13) /**< 256kHz nominal. */
  156. #define STM32_MSIRANGE_512K (3 << 13) /**< 512kHz nominal. */
  157. #define STM32_MSIRANGE_1M (4 << 13) /**< 1MHz nominal. */
  158. #define STM32_MSIRANGE_2M (5 << 13) /**< 2MHz nominal. */
  159. #define STM32_MSIRANGE_4M (6 << 13) /**< 4MHz nominal */
  160. /** @} */
  161. /**
  162. * @name RCC_CSR register bits definitions
  163. * @{
  164. */
  165. #define STM32_RTCSEL_MASK (3 << 16) /**< RTC source mask. */
  166. #define STM32_RTCSEL_NOCLOCK (0 << 16) /**< No RTC source. */
  167. #define STM32_RTCSEL_LSE (1 << 16) /**< RTC source is LSE. */
  168. #define STM32_RTCSEL_LSI (2 << 16) /**< RTC source is LSI. */
  169. #define STM32_RTCSEL_HSEDIV (3 << 16) /**< RTC source is HSE divided. */
  170. /** @} */
  171. /*===========================================================================*/
  172. /* Driver pre-compile time settings. */
  173. /*===========================================================================*/
  174. /**
  175. * @name Configuration options
  176. * @{
  177. */
  178. /**
  179. * @brief Disables the PWR/RCC initialization in the HAL.
  180. */
  181. #if !defined(STM32_NO_INIT) || defined(__DOXYGEN__)
  182. #define STM32_NO_INIT FALSE
  183. #endif
  184. /**
  185. * @brief Core voltage selection.
  186. * @note This setting affects all the performance and clock related
  187. * settings, the maximum performance is only obtainable selecting
  188. * the maximum voltage.
  189. */
  190. #if !defined(STM32_VOS) || defined(__DOXYGEN__)
  191. #define STM32_VOS STM32_VOS_1P8
  192. #endif
  193. /**
  194. * @brief Enables or disables the programmable voltage detector.
  195. */
  196. #if !defined(STM32_PVD_ENABLE) || defined(__DOXYGEN__)
  197. #define STM32_PVD_ENABLE FALSE
  198. #endif
  199. /**
  200. * @brief Sets voltage level for programmable voltage detector.
  201. */
  202. #if !defined(STM32_PLS) || defined(__DOXYGEN__)
  203. #define STM32_PLS STM32_PLS_LEV0
  204. #endif
  205. /**
  206. * @brief Enables or disables the HSI clock source.
  207. */
  208. #if !defined(STM32_HSI_ENABLED) || defined(__DOXYGEN__)
  209. #define STM32_HSI_ENABLED TRUE
  210. #endif
  211. /**
  212. * @brief Enables or disables the LSI clock source.
  213. */
  214. #if !defined(STM32_LSI_ENABLED) || defined(__DOXYGEN__)
  215. #define STM32_LSI_ENABLED TRUE
  216. #endif
  217. /**
  218. * @brief Enables or disables the HSE clock source.
  219. */
  220. #if !defined(STM32_HSE_ENABLED) || defined(__DOXYGEN__)
  221. #define STM32_HSE_ENABLED FALSE
  222. #endif
  223. /**
  224. * @brief Enables or disables the LSE clock source.
  225. */
  226. #if !defined(STM32_LSE_ENABLED) || defined(__DOXYGEN__)
  227. #define STM32_LSE_ENABLED FALSE
  228. #endif
  229. /**
  230. * @brief ADC clock setting.
  231. */
  232. #if !defined(STM32_ADC_CLOCK_ENABLED) || defined(__DOXYGEN__)
  233. #define STM32_ADC_CLOCK_ENABLED TRUE
  234. #endif
  235. /**
  236. * @brief USB clock setting.
  237. */
  238. #if !defined(STM32_USB_CLOCK_ENABLED) || defined(__DOXYGEN__)
  239. #define STM32_USB_CLOCK_ENABLED TRUE
  240. #endif
  241. /**
  242. * @brief MSI frequency setting.
  243. */
  244. #if !defined(STM32_MSIRANGE) || defined(__DOXYGEN__)
  245. #define STM32_MSIRANGE STM32_MSIRANGE_2M
  246. #endif
  247. /**
  248. * @brief Main clock source selection.
  249. * @note If the selected clock source is not the PLL then the PLL is not
  250. * initialized and started.
  251. * @note The default value is calculated for a 32MHz system clock from
  252. * the internal 16MHz HSI clock.
  253. */
  254. #if !defined(STM32_SW) || defined(__DOXYGEN__)
  255. #define STM32_SW STM32_SW_PLL
  256. #endif
  257. /**
  258. * @brief Clock source for the PLL.
  259. * @note This setting has only effect if the PLL is selected as the
  260. * system clock source.
  261. * @note The default value is calculated for a 32MHz system clock from
  262. * the internal 16MHz HSI clock.
  263. */
  264. #if !defined(STM32_PLLSRC) || defined(__DOXYGEN__)
  265. #define STM32_PLLSRC STM32_PLLSRC_HSI
  266. #endif
  267. /**
  268. * @brief PLL multiplier value.
  269. * @note The allowed values are 3, 4, 6, 8, 12, 16, 32, 48.
  270. * @note The default value is calculated for a 32MHz system clock from
  271. * the internal 16MHz HSI clock.
  272. */
  273. #if !defined(STM32_PLLMUL_VALUE) || defined(__DOXYGEN__)
  274. #define STM32_PLLMUL_VALUE 6
  275. #endif
  276. /**
  277. * @brief PLL divider value.
  278. * @note The allowed values are 2, 3, 4.
  279. * @note The default value is calculated for a 32MHz system clock from
  280. * the internal 16MHz HSI clock.
  281. */
  282. #if !defined(STM32_PLLDIV_VALUE) || defined(__DOXYGEN__)
  283. #define STM32_PLLDIV_VALUE 3
  284. #endif
  285. /**
  286. * @brief AHB prescaler value.
  287. * @note The default value is calculated for a 32MHz system clock from
  288. * the internal 16MHz HSI clock.
  289. */
  290. #if !defined(STM32_HPRE) || defined(__DOXYGEN__)
  291. #define STM32_HPRE STM32_HPRE_DIV1
  292. #endif
  293. /**
  294. * @brief APB1 prescaler value.
  295. */
  296. #if !defined(STM32_PPRE1) || defined(__DOXYGEN__)
  297. #define STM32_PPRE1 STM32_PPRE1_DIV1
  298. #endif
  299. /**
  300. * @brief APB2 prescaler value.
  301. */
  302. #if !defined(STM32_PPRE2) || defined(__DOXYGEN__)
  303. #define STM32_PPRE2 STM32_PPRE2_DIV1
  304. #endif
  305. /**
  306. * @brief MCO clock source.
  307. */
  308. #if !defined(STM32_MCOSEL) || defined(__DOXYGEN__)
  309. #define STM32_MCOSEL STM32_MCOSEL_NOCLOCK
  310. #endif
  311. /**
  312. * @brief MCO divider setting.
  313. */
  314. #if !defined(STM32_MCOPRE) || defined(__DOXYGEN__)
  315. #define STM32_MCOPRE STM32_MCOPRE_DIV1
  316. #endif
  317. /**
  318. * @brief RTC/LCD clock source.
  319. */
  320. #if !defined(STM32_RTCSEL) || defined(__DOXYGEN__)
  321. #define STM32_RTCSEL STM32_RTCSEL_LSE
  322. #endif
  323. /**
  324. * @brief HSE divider toward RTC setting.
  325. */
  326. #if !defined(STM32_RTCPRE) || defined(__DOXYGEN__)
  327. #define STM32_RTCPRE STM32_RTCPRE_DIV2
  328. #endif
  329. /** @} */
  330. /*===========================================================================*/
  331. /* Derived constants and error checks. */
  332. /*===========================================================================*/
  333. /*
  334. * Configuration-related checks.
  335. */
  336. #if !defined(STM32L1xx_MCUCONF)
  337. #error "Using a wrong mcuconf.h file, STM32L1xx_MCUCONF not defined"
  338. #endif
  339. /* Voltage related limits.*/
  340. #if (STM32_VOS == STM32_VOS_1P8) || defined(__DOXYGEN__)
  341. /**
  342. * @brief Maximum HSE clock frequency at current voltage setting.
  343. */
  344. #define STM32_HSECLK_MAX 32000000
  345. /**
  346. * @brief Maximum SYSCLK clock frequency at current voltage setting.
  347. */
  348. #define STM32_SYSCLK_MAX 32000000
  349. /**
  350. * @brief Maximum VCO clock frequency at current voltage setting.
  351. */
  352. #define STM32_PLLVCO_MAX 96000000
  353. /**
  354. * @brief Minimum VCO clock frequency at current voltage setting.
  355. */
  356. #define STM32_PLLVCO_MIN 6000000
  357. /**
  358. * @brief Maximum APB1 clock frequency.
  359. */
  360. #define STM32_PCLK1_MAX 32000000
  361. /**
  362. * @brief Maximum APB2 clock frequency.
  363. */
  364. #define STM32_PCLK2_MAX 32000000
  365. /**
  366. * @brief Maximum frequency not requiring a wait state for flash accesses.
  367. */
  368. #define STM32_0WS_THRESHOLD 16000000
  369. /**
  370. * @brief HSI availability at current voltage settings.
  371. */
  372. #define STM32_HSI_AVAILABLE TRUE
  373. #elif STM32_VOS == STM32_VOS_1P5
  374. #define STM32_HSECLK_MAX 16000000
  375. #define STM32_SYSCLK_MAX 16000000
  376. #define STM32_PLLVCO_MAX 48000000
  377. #define STM32_PLLVCO_MIN 6000000
  378. #define STM32_PCLK1_MAX 16000000
  379. #define STM32_PCLK2_MAX 16000000
  380. #define STM32_0WS_THRESHOLD 8000000
  381. #define STM32_HSI_AVAILABLE TRUE
  382. #elif STM32_VOS == STM32_VOS_1P2
  383. #define STM32_HSECLK_MAX 4000000
  384. #define STM32_SYSCLK_MAX 4000000
  385. #define STM32_PLLVCO_MAX 24000000
  386. #define STM32_PLLVCO_MIN 6000000
  387. #define STM32_PCLK1_MAX 4000000
  388. #define STM32_PCLK2_MAX 4000000
  389. #define STM32_0WS_THRESHOLD 2000000
  390. #define STM32_HSI_AVAILABLE FALSE
  391. #else
  392. #error "invalid STM32_VOS value specified"
  393. #endif
  394. /* HSI related checks.*/
  395. #if STM32_HSI_ENABLED
  396. #if !STM32_HSI_AVAILABLE
  397. #error "impossible to activate HSI under the current voltage settings"
  398. #endif
  399. #else /* !STM32_HSI_ENABLED */
  400. #if STM32_ADC_CLOCK_ENABLED || \
  401. (STM32_SW == STM32_SW_HSI) || \
  402. ((STM32_SW == STM32_SW_PLL) && \
  403. (STM32_PLLSRC == STM32_PLLSRC_HSI)) || \
  404. (STM32_MCOSEL == STM32_MCOSEL_HSI) || \
  405. ((STM32_MCOSEL == STM32_MCOSEL_PLL) && \
  406. (STM32_PLLSRC == STM32_PLLSRC_HSI))
  407. #error "required HSI clock is not enabled"
  408. #endif
  409. #endif /* !STM32_HSI_ENABLED */
  410. /* HSE related checks.*/
  411. #if STM32_HSE_ENABLED
  412. #if STM32_HSECLK == 0
  413. #error "impossible to activate HSE"
  414. #endif
  415. #if (STM32_HSECLK < 1000000) || (STM32_HSECLK > STM32_HSECLK_MAX)
  416. #error "STM32_HSECLK outside acceptable range (1MHz...STM32_HSECLK_MAX)"
  417. #endif
  418. #else /* !STM32_HSE_ENABLED */
  419. #if (STM32_SW == STM32_SW_HSE) || \
  420. ((STM32_SW == STM32_SW_PLL) && \
  421. (STM32_PLLSRC == STM32_PLLSRC_HSE)) || \
  422. (STM32_MCOSEL == STM32_MCOSEL_HSE) || \
  423. ((STM32_MCOSEL == STM32_MCOSEL_PLL) && \
  424. (STM32_PLLSRC == STM32_PLLSRC_HSE)) || \
  425. (STM32_RTCSEL == STM32_RTCSEL_HSEDIV)
  426. #error "required HSE clock is not enabled"
  427. #endif
  428. #endif /* !STM32_HSE_ENABLED */
  429. /* LSI related checks.*/
  430. #if STM32_LSI_ENABLED
  431. #else /* !STM32_LSI_ENABLED */
  432. #if STM32_MCOSEL == STM32_MCOSEL_LSI
  433. #error "LSI not enabled, required by STM32_MCOSEL"
  434. #endif
  435. #if STM32_RTCSEL == STM32_RTCSEL_LSI
  436. #error "LSI not enabled, required by STM32_RTCSEL"
  437. #endif
  438. #endif /* !STM32_LSI_ENABLED */
  439. /* LSE related checks.*/
  440. #if STM32_LSE_ENABLED
  441. #if (STM32_LSECLK == 0)
  442. #error "impossible to activate LSE"
  443. #endif
  444. #if (STM32_LSECLK < 1000) || (STM32_LSECLK > 1000000)
  445. #error "STM32_LSECLK outside acceptable range (1...1000kHz)"
  446. #endif
  447. #else /* !STM32_LSE_ENABLED */
  448. #if STM32_MCOSEL == STM32_MCOSEL_LSE
  449. #error "LSE not enabled, required by STM32_MCOSEL"
  450. #endif
  451. #if STM32_RTCSEL == STM32_RTCSEL_LSE
  452. #error "LSE not enabled, required by STM32_RTCSEL"
  453. #endif
  454. #endif /* !STM32_LSE_ENABLED */
  455. /* PLL related checks.*/
  456. #if STM32_USB_CLOCK_ENABLED || \
  457. (STM32_SW == STM32_SW_PLL) || \
  458. (STM32_MCOSEL == STM32_MCOSEL_PLL) || \
  459. defined(__DOXYGEN__)
  460. /**
  461. * @brief PLL activation flag.
  462. */
  463. #define STM32_ACTIVATE_PLL TRUE
  464. #else
  465. #define STM32_ACTIVATE_PLL FALSE
  466. #endif
  467. /**
  468. * @brief PLLMUL field.
  469. */
  470. #if (STM32_PLLMUL_VALUE == 3) || defined(__DOXYGEN__)
  471. #define STM32_PLLMUL (0 << 18)
  472. #elif STM32_PLLMUL_VALUE == 4
  473. #define STM32_PLLMUL (1 << 18)
  474. #elif STM32_PLLMUL_VALUE == 6
  475. #define STM32_PLLMUL (2 << 18)
  476. #elif STM32_PLLMUL_VALUE == 8
  477. #define STM32_PLLMUL (3 << 18)
  478. #elif STM32_PLLMUL_VALUE == 12
  479. #define STM32_PLLMUL (4 << 18)
  480. #elif STM32_PLLMUL_VALUE == 16
  481. #define STM32_PLLMUL (5 << 18)
  482. #elif STM32_PLLMUL_VALUE == 24
  483. #define STM32_PLLMUL (6 << 18)
  484. #elif STM32_PLLMUL_VALUE == 32
  485. #define STM32_PLLMUL (7 << 18)
  486. #elif STM32_PLLMUL_VALUE == 48
  487. #define STM32_PLLMUL (8 << 18)
  488. #else
  489. #error "invalid STM32_PLLMUL_VALUE value specified"
  490. #endif
  491. /**
  492. * @brief PLLDIV field.
  493. */
  494. #if (STM32_PLLDIV_VALUE == 2) || defined(__DOXYGEN__)
  495. #define STM32_PLLDIV (1 << 22)
  496. #elif STM32_PLLDIV_VALUE == 3
  497. #define STM32_PLLDIV (2 << 22)
  498. #elif STM32_PLLDIV_VALUE == 4
  499. #define STM32_PLLDIV (3 << 22)
  500. #else
  501. #error "invalid STM32_PLLDIV_VALUE value specified"
  502. #endif
  503. /**
  504. * @brief PLL input clock frequency.
  505. */
  506. #if (STM32_PLLSRC == STM32_PLLSRC_HSE) || defined(__DOXYGEN__)
  507. #define STM32_PLLCLKIN STM32_HSECLK
  508. #elif STM32_PLLSRC == STM32_PLLSRC_HSI
  509. #define STM32_PLLCLKIN STM32_HSICLK
  510. #else
  511. #error "invalid STM32_PLLSRC value specified"
  512. #endif
  513. /* PLL input frequency range check.*/
  514. #if (STM32_PLLCLKIN < 2000000) || (STM32_PLLCLKIN > 24000000)
  515. #error "STM32_PLLCLKIN outside acceptable range (2...24MHz)"
  516. #endif
  517. /**
  518. * @brief PLL VCO frequency.
  519. */
  520. #define STM32_PLLVCO (STM32_PLLCLKIN * STM32_PLLMUL_VALUE)
  521. /* PLL output frequency range check.*/
  522. #if (STM32_PLLVCO < STM32_PLLVCO_MIN) || (STM32_PLLVCO > STM32_PLLVCO_MAX)
  523. #error "STM32_PLLVCO outside acceptable range (STM32_PLLVCO_MIN...STM32_PLLVCO_MAX)"
  524. #endif
  525. /**
  526. * @brief PLL output clock frequency.
  527. */
  528. #define STM32_PLLCLKOUT (STM32_PLLVCO / STM32_PLLDIV_VALUE)
  529. /* PLL output frequency range check.*/
  530. #if (STM32_PLLCLKOUT < 2000000) || (STM32_PLLCLKOUT > 32000000)
  531. #error "STM32_PLLCLKOUT outside acceptable range (2...32MHz)"
  532. #endif
  533. /**
  534. * @brief MSI frequency.
  535. */
  536. #if STM32_MSIRANGE == STM32_MSIRANGE_64K
  537. #define STM32_MSICLK 65500
  538. #elif STM32_MSIRANGE == STM32_MSIRANGE_128K
  539. #define STM32_MSICLK 131000
  540. #elif STM32_MSIRANGE == STM32_MSIRANGE_256K
  541. #define STM32_MSICLK 262000
  542. #elif STM32_MSIRANGE == STM32_MSIRANGE_512K
  543. #define STM32_MSICLK 524000
  544. #elif STM32_MSIRANGE == STM32_MSIRANGE_1M
  545. #define STM32_MSICLK 1050000
  546. #elif STM32_MSIRANGE == STM32_MSIRANGE_2M
  547. #define STM32_MSICLK 2100000
  548. #elif STM32_MSIRANGE == STM32_MSIRANGE_4M
  549. #define STM32_MSICLK 4200000
  550. #else
  551. #error "invalid STM32_MSIRANGE value specified"
  552. #endif
  553. /**
  554. * @brief System clock source.
  555. */
  556. #if STM32_NO_INIT || defined(__DOXYGEN__)
  557. #define STM32_SYSCLK 2100000
  558. #elif (STM32_SW == STM32_SW_MSI)
  559. #define STM32_SYSCLK STM32_MSICLK
  560. #elif (STM32_SW == STM32_SW_HSI)
  561. #define STM32_SYSCLK STM32_HSICLK
  562. #elif (STM32_SW == STM32_SW_HSE)
  563. #define STM32_SYSCLK STM32_HSECLK
  564. #elif (STM32_SW == STM32_SW_PLL)
  565. #define STM32_SYSCLK STM32_PLLCLKOUT
  566. #else
  567. #error "invalid STM32_SW value specified"
  568. #endif
  569. /* Check on the system clock.*/
  570. #if STM32_SYSCLK > STM32_SYSCLK_MAX
  571. #error "STM32_SYSCLK above maximum rated frequency (STM32_SYSCLK_MAX)"
  572. #endif
  573. /**
  574. * @brief AHB frequency.
  575. */
  576. #if (STM32_HPRE == STM32_HPRE_DIV1) || defined(__DOXYGEN__)
  577. #define STM32_HCLK (STM32_SYSCLK / 1)
  578. #elif STM32_HPRE == STM32_HPRE_DIV2
  579. #define STM32_HCLK (STM32_SYSCLK / 2)
  580. #elif STM32_HPRE == STM32_HPRE_DIV4
  581. #define STM32_HCLK (STM32_SYSCLK / 4)
  582. #elif STM32_HPRE == STM32_HPRE_DIV8
  583. #define STM32_HCLK (STM32_SYSCLK / 8)
  584. #elif STM32_HPRE == STM32_HPRE_DIV16
  585. #define STM32_HCLK (STM32_SYSCLK / 16)
  586. #elif STM32_HPRE == STM32_HPRE_DIV64
  587. #define STM32_HCLK (STM32_SYSCLK / 64)
  588. #elif STM32_HPRE == STM32_HPRE_DIV128
  589. #define STM32_HCLK (STM32_SYSCLK / 128)
  590. #elif STM32_HPRE == STM32_HPRE_DIV256
  591. #define STM32_HCLK (STM32_SYSCLK / 256)
  592. #elif STM32_HPRE == STM32_HPRE_DIV512
  593. #define STM32_HCLK (STM32_SYSCLK / 512)
  594. #else
  595. #error "invalid STM32_HPRE value specified"
  596. #endif
  597. /* AHB frequency check.*/
  598. #if STM32_HCLK > STM32_SYSCLK_MAX
  599. #error "STM32_HCLK exceeding maximum frequency (STM32_SYSCLK_MAX)"
  600. #endif
  601. /**
  602. * @brief APB1 frequency.
  603. */
  604. #if (STM32_PPRE1 == STM32_PPRE1_DIV1) || defined(__DOXYGEN__)
  605. #define STM32_PCLK1 (STM32_HCLK / 1)
  606. #elif STM32_PPRE1 == STM32_PPRE1_DIV2
  607. #define STM32_PCLK1 (STM32_HCLK / 2)
  608. #elif STM32_PPRE1 == STM32_PPRE1_DIV4
  609. #define STM32_PCLK1 (STM32_HCLK / 4)
  610. #elif STM32_PPRE1 == STM32_PPRE1_DIV8
  611. #define STM32_PCLK1 (STM32_HCLK / 8)
  612. #elif STM32_PPRE1 == STM32_PPRE1_DIV16
  613. #define STM32_PCLK1 (STM32_HCLK / 16)
  614. #else
  615. #error "invalid STM32_PPRE1 value specified"
  616. #endif
  617. /* APB1 frequency check.*/
  618. #if STM32_PCLK1 > STM32_PCLK1_MAX
  619. #error "STM32_PCLK1 exceeding maximum frequency (STM32_PCLK1_MAX)"
  620. #endif
  621. /**
  622. * @brief APB2 frequency.
  623. */
  624. #if (STM32_PPRE2 == STM32_PPRE2_DIV1) || defined(__DOXYGEN__)
  625. #define STM32_PCLK2 (STM32_HCLK / 1)
  626. #elif STM32_PPRE2 == STM32_PPRE2_DIV2
  627. #define STM32_PCLK2 (STM32_HCLK / 2)
  628. #elif STM32_PPRE2 == STM32_PPRE2_DIV4
  629. #define STM32_PCLK2 (STM32_HCLK / 4)
  630. #elif STM32_PPRE2 == STM32_PPRE2_DIV8
  631. #define STM32_PCLK2 (STM32_HCLK / 8)
  632. #elif STM32_PPRE2 == STM32_PPRE2_DIV16
  633. #define STM32_PCLK2 (STM32_HCLK / 16)
  634. #else
  635. #error "invalid STM32_PPRE2 value specified"
  636. #endif
  637. /* APB2 frequency check.*/
  638. #if STM32_PCLK2 > STM32_PCLK2_MAX
  639. #error "STM32_PCLK2 exceeding maximum frequency (STM32_PCLK2_MAX)"
  640. #endif
  641. /**
  642. * @brief MCO clock before divider.
  643. */
  644. #if (STM32_MCOSEL == STM32_MCOSEL_NOCLOCK) || defined(__DOXYGEN__)
  645. #define STM32_MCODIVCLK 0
  646. #elif STM32_MCOSEL == STM32_MCOSEL_HSI
  647. #define STM32_MCODIVCLK STM32_HSICLK
  648. #elif STM32_MCOSEL == STM32_MCOSEL_MSI
  649. #define STM32_MCODIVCLK STM32_MSICLK
  650. #elif STM32_MCOSEL == STM32_MCOSEL_HSE
  651. #define STM32_MCODIVCLK STM32_HSECLK
  652. #elif STM32_MCOSEL == STM32_MCOSEL_PLL
  653. #define STM32_MCODIVCLK STM32_PLLCLKOUT
  654. #elif STM32_MCOSEL == STM32_MCOSEL_LSI
  655. #define STM32_MCODIVCLK STM32_LSICLK
  656. #elif STM32_MCOSEL == STM32_MCOSEL_LSE
  657. #define STM32_MCODIVCLK STM32_LSECLK
  658. #else
  659. #error "invalid STM32_MCOSEL value specified"
  660. #endif
  661. /**
  662. * @brief MCO output pin clock.
  663. */
  664. #if (STM32_MCOPRE == STM32_MCOPRE_DIV1) || defined(__DOXYGEN__)
  665. #define STM32_MCOCLK STM32_MCODIVCLK
  666. #elif STM32_MCOPRE == STM32_MCOPRE_DIV2
  667. #define STM32_MCOCLK (STM32_MCODIVCLK / 2)
  668. #elif STM32_MCOPRE == STM32_MCOPRE_DIV4
  669. #define STM32_MCOCLK (STM32_MCODIVCLK / 4)
  670. #elif STM32_MCOPRE == STM32_MCOPRE_DIV8
  671. #define STM32_MCOCLK (STM32_MCODIVCLK / 8)
  672. #elif STM32_MCOPRE == STM32_MCOPRE_DIV16
  673. #define STM32_MCOCLK (STM32_MCODIVCLK / 16)
  674. #else
  675. #error "invalid STM32_MCOPRE value specified"
  676. #endif
  677. /**
  678. * @brief HSE divider toward RTC clock.
  679. */
  680. #if (STM32_RTCPRE == STM32_RTCPRE_DIV2) || defined(__DOXYGEN__)
  681. #define STM32_HSEDIVCLK (STM32_HSECLK / 2)
  682. #elif (STM32_RTCPRE == STM32_RTCPRE_DIV4) || defined(__DOXYGEN__)
  683. #define STM32_HSEDIVCLK (STM32_HSECLK / 4)
  684. #elif (STM32_RTCPRE == STM32_RTCPRE_DIV8) || defined(__DOXYGEN__)
  685. #define STM32_HSEDIVCLK (STM32_HSECLK / 8)
  686. #elif (STM32_RTCPRE == STM32_RTCPRE_DIV16) || defined(__DOXYGEN__)
  687. #define STM32_HSEDIVCLK (STM32_HSECLK / 16)
  688. #else
  689. #error "invalid STM32_RTCPRE value specified"
  690. #endif
  691. /**
  692. * @brief RTC/LCD clock.
  693. */
  694. #if (STM32_RTCSEL == STM32_RTCSEL_NOCLOCK) || defined(__DOXYGEN__)
  695. #define STM32_RTCCLK 0
  696. #elif STM32_RTCSEL == STM32_RTCSEL_LSE
  697. #define STM32_RTCCLK STM32_LSECLK
  698. #elif STM32_RTCSEL == STM32_RTCSEL_LSI
  699. #define STM32_RTCCLK STM32_LSICLK
  700. #elif STM32_RTCSEL == STM32_RTCSEL_HSEDIV
  701. #define STM32_RTCCLK STM32_HSEDIVCLK
  702. #else
  703. #error "invalid STM32_RTCSEL value specified"
  704. #endif
  705. /**
  706. * @brief USB frequency.
  707. */
  708. #define STM32_USBCLK (STM32_PLLVCO / 2)
  709. /**
  710. * @brief Timers 2, 3, 4, 6, 7 clock.
  711. */
  712. #if (STM32_PPRE1 == STM32_PPRE1_DIV1) || defined(__DOXYGEN__)
  713. #define STM32_TIMCLK1 (STM32_PCLK1 * 1)
  714. #else
  715. #define STM32_TIMCLK1 (STM32_PCLK1 * 2)
  716. #endif
  717. /**
  718. * @brief Timers 9, 10, 11 clock.
  719. */
  720. #if (STM32_PPRE2 == STM32_PPRE2_DIV1) || defined(__DOXYGEN__)
  721. #define STM32_TIMCLK2 (STM32_PCLK2 * 1)
  722. #else
  723. #define STM32_TIMCLK2 (STM32_PCLK2 * 2)
  724. #endif
  725. /**
  726. * @brief Flash settings.
  727. */
  728. #if (STM32_HCLK <= STM32_0WS_THRESHOLD) || defined(__DOXYGEN__)
  729. #define STM32_FLASHBITS1 0x00000000
  730. #else
  731. #define STM32_FLASHBITS1 0x00000004
  732. #define STM32_FLASHBITS2 0x00000007
  733. #endif
  734. /*===========================================================================*/
  735. /* Driver data structures and types. */
  736. /*===========================================================================*/
  737. /*===========================================================================*/
  738. /* Driver macros. */
  739. /*===========================================================================*/
  740. /*===========================================================================*/
  741. /* External declarations. */
  742. /*===========================================================================*/
  743. /* Various helpers.*/
  744. #include "nvic.h"
  745. #include "cache.h"
  746. #include "mpu_v7m.h"
  747. #include "stm32_isr.h"
  748. #include "stm32_dma.h"
  749. #include "stm32_rcc.h"
  750. #ifdef __cplusplus
  751. extern "C" {
  752. #endif
  753. void hal_lld_init(void);
  754. void stm32_clock_init(void);
  755. #ifdef __cplusplus
  756. }
  757. #endif
  758. #endif /* HAL_LLD_H */
  759. /** @} */