hal_lld.h 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005
  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 STM32F0xx/hal_lld.h
  15. * @brief STM32F0xx 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. * - STM32F030x4, STM32F030x6, STM32F030x8, STM32F030xC,
  26. * STM32F070x6, STM32F070xB for Value Line devices.
  27. * - STM32F031x6, STM32F051x8, STM32F071xB, STM32F091xC
  28. * for Access Line devices.
  29. * - STM32F042x6, STM32F072xB for USB Line devices.
  30. * - STM32F038xx, STM32F048xx, STM32F058xx, STM32F078xx,
  31. * STM32F098xx for Low Voltage Line devices.
  32. * .
  33. *
  34. * @addtogroup HAL
  35. * @{
  36. */
  37. #ifndef HAL_LLD_H
  38. #define HAL_LLD_H
  39. /*
  40. * Registry definitions.
  41. */
  42. #include "stm32_registry.h"
  43. /*===========================================================================*/
  44. /* Driver constants. */
  45. /*===========================================================================*/
  46. /**
  47. * @name Platform identification macros
  48. * @{
  49. */
  50. #if defined(STM32F030x4) || defined(__DOXYGEN__)
  51. #define PLATFORM_NAME "STM32F030x4 Entry Level Value Line devices"
  52. #elif defined(STM32F030x6)
  53. #define PLATFORM_NAME "STM32F030x6 Entry Level Value Line devices"
  54. #elif defined(STM32F030x8)
  55. #define PLATFORM_NAME "STM32F030x8 Entry Level Value Line devices"
  56. #elif defined(STM32F030xC)
  57. #define PLATFORM_NAME "STM32F030xC Entry Level Value Line devices"
  58. #elif defined(STM32F070x6)
  59. #define PLATFORM_NAME "STM32F070x6 Entry Level Value Line devices"
  60. #elif defined(STM32F070xB)
  61. #define PLATFORM_NAME "STM32F070xB Entry Level Value Line devices"
  62. #elif defined(STM32F031x6)
  63. #define PLATFORM_NAME "STM32F031x6 Entry Level Access Line devices"
  64. #elif defined(STM32F051x8)
  65. #define PLATFORM_NAME "STM32F051x8 Entry Level Access Line devices"
  66. #elif defined(STM32F071xB)
  67. #define PLATFORM_NAME "STM32F071xB Entry Level Access Line devices"
  68. #elif defined(STM32F091xC)
  69. #define PLATFORM_NAME "STM32F091xC Entry Level Access Line devices"
  70. #elif defined(STM32F042x6)
  71. #define PLATFORM_NAME "STM32F042x6 Entry Level USB Line devices"
  72. #elif defined(STM32F072xB)
  73. #define PLATFORM_NAME "STM32F072xB Entry Level USB Line devices"
  74. #elif defined(STM32F038xx)
  75. #define PLATFORM_NAME "STM32F038xx Entry Level Low Voltage Line devices"
  76. #elif defined(STM32F048xx)
  77. #define PLATFORM_NAME "STM32F048xx Entry Level Low Voltage Line devices"
  78. #elif defined(STM32F058xx)
  79. #define PLATFORM_NAME "STM32F058xx Entry Level Low Voltage Line devices"
  80. #elif defined(STM32F078xx)
  81. #define PLATFORM_NAME "STM32F078xx Entry Level Low Voltage Line devices"
  82. #elif defined(STM32F098xx)
  83. #define PLATFORM_NAME "STM32F098xx Entry Level Low Voltage Line devices"
  84. #else
  85. #error "STM32F0xx device unsupported or not specified"
  86. #endif
  87. /** @} */
  88. /**
  89. * @name Absolute Maximum Ratings
  90. * @{
  91. */
  92. /**
  93. * @brief Maximum system clock frequency.
  94. */
  95. #define STM32_SYSCLK_MAX 48000000
  96. /**
  97. * @brief Maximum HSE clock frequency.
  98. */
  99. #define STM32_HSECLK_MAX 32000000
  100. /**
  101. * @brief Minimum HSE clock frequency.
  102. */
  103. #define STM32_HSECLK_MIN 1000000
  104. /**
  105. * @brief Maximum LSE clock frequency.
  106. */
  107. #define STM32_LSECLK_MAX 1000000
  108. /**
  109. * @brief Minimum LSE clock frequency.
  110. */
  111. #define STM32_LSECLK_MIN 32768
  112. /**
  113. * @brief Maximum PLLs input clock frequency.
  114. */
  115. #define STM32_PLLIN_MAX 25000000
  116. /**
  117. * @brief Minimum PLLs input clock frequency.
  118. */
  119. #define STM32_PLLIN_MIN 1000000
  120. /**
  121. * @brief Maximum PLL output clock frequency.
  122. */
  123. #define STM32_PLLOUT_MAX 48000000
  124. /**
  125. * @brief Minimum PLL output clock frequency.
  126. */
  127. #define STM32_PLLOUT_MIN 16000000
  128. /**
  129. * @brief Maximum APB clock frequency.
  130. */
  131. #define STM32_PCLK_MAX 48000000
  132. /** @} */
  133. /**
  134. * @name Internal clock sources
  135. * @{
  136. */
  137. #define STM32_HSICLK 8000000 /**< High speed internal clock. */
  138. #define STM32_HSI14CLK 14000000 /**< 14MHz speed internal clock.*/
  139. #define STM32_HSI48CLK 48000000 /**< 48MHz speed internal clock.*/
  140. #define STM32_LSICLK 40000 /**< Low speed internal clock. */
  141. /** @} */
  142. /**
  143. * @name PWR_CR register bits definitions
  144. * @{
  145. */
  146. #define STM32_PLS_MASK (7 << 5) /**< PLS bits mask. */
  147. #define STM32_PLS_LEV0 (0 << 5) /**< PVD level 0. */
  148. #define STM32_PLS_LEV1 (1 << 5) /**< PVD level 1. */
  149. #define STM32_PLS_LEV2 (2 << 5) /**< PVD level 2. */
  150. #define STM32_PLS_LEV3 (3 << 5) /**< PVD level 3. */
  151. #define STM32_PLS_LEV4 (4 << 5) /**< PVD level 4. */
  152. #define STM32_PLS_LEV5 (5 << 5) /**< PVD level 5. */
  153. #define STM32_PLS_LEV6 (6 << 5) /**< PVD level 6. */
  154. #define STM32_PLS_LEV7 (7 << 5) /**< PVD level 7. */
  155. /** @} */
  156. /**
  157. * @name RCC_CFGR register bits definitions
  158. * @{
  159. */
  160. #define STM32_SW_HSI (0 << 0) /**< SYSCLK source is HSI. */
  161. #define STM32_SW_HSE (1 << 0) /**< SYSCLK source is HSE. */
  162. #define STM32_SW_PLL (2 << 0) /**< SYSCLK source is PLL. */
  163. #define STM32_SW_HSI48 (3 << 0) /**< SYSCLK source is HSI48. */
  164. #define STM32_HPRE_DIV1 (0 << 4) /**< SYSCLK divided by 1. */
  165. #define STM32_HPRE_DIV2 (8 << 4) /**< SYSCLK divided by 2. */
  166. #define STM32_HPRE_DIV4 (9 << 4) /**< SYSCLK divided by 4. */
  167. #define STM32_HPRE_DIV8 (10 << 4) /**< SYSCLK divided by 8. */
  168. #define STM32_HPRE_DIV16 (11 << 4) /**< SYSCLK divided by 16. */
  169. #define STM32_HPRE_DIV64 (12 << 4) /**< SYSCLK divided by 64. */
  170. #define STM32_HPRE_DIV128 (13 << 4) /**< SYSCLK divided by 128. */
  171. #define STM32_HPRE_DIV256 (14 << 4) /**< SYSCLK divided by 256. */
  172. #define STM32_HPRE_DIV512 (15 << 4) /**< SYSCLK divided by 512. */
  173. #define STM32_PPRE_DIV1 (0 << 8) /**< HCLK divided by 1. */
  174. #define STM32_PPRE_DIV2 (4 << 8) /**< HCLK divided by 2. */
  175. #define STM32_PPRE_DIV4 (5 << 8) /**< HCLK divided by 4. */
  176. #define STM32_PPRE_DIV8 (6 << 8) /**< HCLK divided by 8. */
  177. #define STM32_PPRE_DIV16 (7 << 8) /**< HCLK divided by 16. */
  178. #define STM32_PLLSRC_HSI_DIV2 (0 << 15) /**< PLL clock source is HSI/2. */
  179. #define STM32_PLLSRC_HSI (1 << 15) /**< PLL clock source is HSI */
  180. #define STM32_PLLSRC_HSE (2 << 15) /**< PLL clock source is HSE. */
  181. #define STM32_PLLSRC_HSI48 (3 << 15) /**< PLL clock source is HSI48. */
  182. #define STM32_MCOSEL_NOCLOCK (0 << 24) /**< No clock on MCO pin. */
  183. #define STM32_MCOSEL_HSI14 (1 << 24) /**< HSI14 clock on MCO pin. */
  184. #define STM32_MCOSEL_LSI (2 << 24) /**< LSI clock on MCO pin. */
  185. #define STM32_MCOSEL_LSE (3 << 24) /**< LSE clock on MCO pin. */
  186. #define STM32_MCOSEL_SYSCLK (4 << 24) /**< SYSCLK on MCO pin. */
  187. #define STM32_MCOSEL_HSI (5 << 24) /**< HSI clock on MCO pin. */
  188. #define STM32_MCOSEL_HSE (6 << 24) /**< HSE clock on MCO pin. */
  189. #define STM32_MCOSEL_PLLDIV2 (7 << 24) /**< PLL/2 clock on MCO pin. */
  190. #define STM32_MCOSEL_HSI48 (8 << 24) /**< HSI48 clock on MCO pin. */
  191. #define STM32_MCOPRE_DIV1 (0 << 28) /**< MCO divided by 1. */
  192. #define STM32_MCOPRE_DIV2 (1 << 28) /**< MCO divided by 2. */
  193. #define STM32_MCOPRE_DIV4 (2 << 28) /**< MCO divided by 4. */
  194. #define STM32_MCOPRE_DIV8 (3 << 28) /**< MCO divided by 8. */
  195. #define STM32_MCOPRE_DIV16 (4 << 28) /**< MCO divided by 16. */
  196. #define STM32_MCOPRE_DIV32 (5 << 28) /**< MCO divided by 32. */
  197. #define STM32_MCOPRE_DIV64 (6 << 28) /**< MCO divided by 64. */
  198. #define STM32_MCOPRE_DIV128 (7 << 28) /**< MCO divided by 128. */
  199. #define STM32_PLLNODIV_MASK (1 << 31) /**< MCO PLL divider mask. */
  200. #define STM32_PLLNODIV_DIV2 (0 << 31) /**< MCO PLL is divided by two. */
  201. #define STM32_PLLNODIV_DIV1 (1 << 31) /**< MCO PLL is divided by one. */
  202. /** @} */
  203. /**
  204. * @name RCC_CFGR2 register bits definitions
  205. * @{
  206. */
  207. #define STM32_PRE_DIV1 (0 << 0) /**< PLLSRC divided by 1. */
  208. #define STM32_PRE_DIV2 (1 << 0) /**< SYSCLK divided by 2. */
  209. #define STM32_PRE_DIV3 (2 << 0) /**< SYSCLK divided by 3. */
  210. #define STM32_PRE_DIV4 (3 << 0) /**< PLLSRC divided by 4. */
  211. #define STM32_PRE_DIV5 (4 << 0) /**< SYSCLK divided by 5. */
  212. #define STM32_PRE_DIV6 (5 << 0) /**< SYSCLK divided by 6. */
  213. #define STM32_PRE_DIV7 (6 << 0) /**< PLLSRC divided by 7. */
  214. #define STM32_PRE_DIV8 (7 << 0) /**< SYSCLK divided by 8. */
  215. #define STM32_PRE_DIV9 (8 << 0) /**< SYSCLK divided by 9. */
  216. #define STM32_PRE_DIV10 (9 << 0) /**< PLLSRC divided by 10. */
  217. #define STM32_PRE_DIV11 (10 << 0) /**< SYSCLK divided by 11. */
  218. #define STM32_PRE_DIV12 (11 << 0) /**< SYSCLK divided by 12. */
  219. #define STM32_PRE_DIV13 (12 << 0) /**< PLLSRC divided by 13. */
  220. #define STM32_PRE_DIV14 (13 << 0) /**< SYSCLK divided by 14. */
  221. #define STM32_PRE_DIV15 (14 << 0) /**< SYSCLK divided by 15. */
  222. #define STM32_PRE_DIV16 (15 << 0) /**< PLLSRC divided by 16. */
  223. /** @} */
  224. /**
  225. * @name RCC_CFGR3 register bits definitions
  226. * @{
  227. */
  228. #define STM32_USART1SW_MASK (3 << 0) /**< USART1 clock source mask. */
  229. #define STM32_USART1SW_PCLK (0 << 0) /**< USART1 clock is PCLK. */
  230. #define STM32_USART1SW_SYSCLK (1 << 0) /**< USART1 clock is SYSCLK. */
  231. #define STM32_USART1SW_LSE (2 << 0) /**< USART1 clock is LSE. */
  232. #define STM32_USART1SW_HSI (3 << 0) /**< USART1 clock is HSI. */
  233. #define STM32_I2C1SW_MASK (1 << 4) /**< I2C clock source mask. */
  234. #define STM32_I2C1SW_HSI (0 << 4) /**< I2C clock is HSI. */
  235. #define STM32_I2C1SW_SYSCLK (1 << 4) /**< I2C clock is SYSCLK. */
  236. #define STM32_CECSW_MASK (1 << 6) /**< CEC clock source mask. */
  237. #define STM32_CECSW_HSI (0 << 6) /**< CEC clock is HSI/244. */
  238. #define STM32_CECSW_LSE (1 << 6) /**< CEC clock is LSE. */
  239. #define STM32_CECSW_OFF 0xFFFFFFFF /**< CEC clock is not required. */
  240. #define STM32_USBSW_MASK (1 << 7) /**< USB clock source mask. */
  241. #define STM32_USBSW_HSI48 (0 << 7) /**< USB clock is HSI48. */
  242. #define STM32_USBSW_PCLK (1 << 7) /**< USB clock is PCLK. */
  243. /** @} */
  244. /**
  245. * @name RCC_BDCR register bits definitions
  246. * @{
  247. */
  248. #define STM32_RTCSEL_MASK (3 << 8) /**< RTC clock source mask. */
  249. #define STM32_RTCSEL_NOCLOCK (0 << 8) /**< No clock. */
  250. #define STM32_RTCSEL_LSE (1 << 8) /**< LSE used as RTC clock. */
  251. #define STM32_RTCSEL_LSI (2 << 8) /**< LSI used as RTC clock. */
  252. #define STM32_RTCSEL_HSEDIV (3 << 8) /**< HSE divided by 32 used as
  253. RTC clock. */
  254. /** @} */
  255. /*===========================================================================*/
  256. /* Driver pre-compile time settings. */
  257. /*===========================================================================*/
  258. /**
  259. * @name Configuration options
  260. * @{
  261. */
  262. /**
  263. * @brief Disables the PWR/RCC initialization in the HAL.
  264. */
  265. #if !defined(STM32_NO_INIT) || defined(__DOXYGEN__)
  266. #define STM32_NO_INIT FALSE
  267. #endif
  268. /**
  269. * @brief Enables or disables the programmable voltage detector.
  270. */
  271. #if !defined(STM32_PVD_ENABLE) || defined(__DOXYGEN__)
  272. #define STM32_PVD_ENABLE FALSE
  273. #endif
  274. /**
  275. * @brief Sets voltage level for programmable voltage detector.
  276. */
  277. #if !defined(STM32_PLS) || defined(__DOXYGEN__)
  278. #define STM32_PLS STM32_PLS_LEV0
  279. #endif
  280. /**
  281. * @brief Enables or disables the HSI clock source.
  282. */
  283. #if !defined(STM32_HSI_ENABLED) || defined(__DOXYGEN__)
  284. #define STM32_HSI_ENABLED TRUE
  285. #endif
  286. /**
  287. * @brief Enables or disables the HSI14 clock source.
  288. */
  289. #if !defined(STM32_HSI14_ENABLED) || defined(__DOXYGEN__)
  290. #define STM32_HSI14_ENABLED TRUE
  291. #endif
  292. /**
  293. * @brief Enables or disables the HSI48 clock source.
  294. */
  295. #if !defined(STM32_HSI48_ENABLED) || defined(__DOXYGEN__)
  296. #define STM32_HSI48_ENABLED FALSE
  297. #endif
  298. /**
  299. * @brief Enables or disables the LSI clock source.
  300. */
  301. #if !defined(STM32_LSI_ENABLED) || defined(__DOXYGEN__)
  302. #define STM32_LSI_ENABLED FALSE
  303. #endif
  304. /**
  305. * @brief Enables or disables the HSE clock source.
  306. */
  307. #if !defined(STM32_HSE_ENABLED) || defined(__DOXYGEN__)
  308. #define STM32_HSE_ENABLED TRUE
  309. #endif
  310. /**
  311. * @brief Enables or disables the LSE clock source.
  312. */
  313. #if !defined(STM32_LSE_ENABLED) || defined(__DOXYGEN__)
  314. #define STM32_LSE_ENABLED FALSE
  315. #endif
  316. /**
  317. * @brief Main clock source selection.
  318. * @note If the selected clock source is not the PLL then the PLL is not
  319. * initialized and started.
  320. * @note The default value is calculated for a 48MHz system clock from
  321. * a 8MHz crystal using the PLL.
  322. */
  323. #if !defined(STM32_SW) || defined(__DOXYGEN__)
  324. #define STM32_SW STM32_SW_PLL
  325. #endif
  326. /**
  327. * @brief Clock source for the PLL.
  328. * @note This setting has only effect if the PLL is selected as the
  329. * system clock source.
  330. * @note The default value is calculated for a 48MHz system clock from
  331. * a 8MHz crystal using the PLL.
  332. */
  333. #if !defined(STM32_PLLSRC) || defined(__DOXYGEN__)
  334. #define STM32_PLLSRC STM32_PLLSRC_HSE
  335. #endif
  336. /**
  337. * @brief Crystal PLL pre-divider.
  338. * @note This setting has only effect if the PLL is selected as the
  339. * system clock source.
  340. * @note The default value is calculated for a 72MHz system clock from
  341. * a 8MHz crystal using the PLL.
  342. */
  343. #if !defined(STM32_PREDIV_VALUE) || defined(__DOXYGEN__)
  344. #define STM32_PREDIV_VALUE 1
  345. #endif
  346. /**
  347. * @brief PLL multiplier value.
  348. * @note The allowed range is 2...16.
  349. * @note The default value is calculated for a 48MHz system clock from
  350. * a 8MHz crystal using the PLL.
  351. */
  352. #if !defined(STM32_PLLMUL_VALUE) || defined(__DOXYGEN__)
  353. #define STM32_PLLMUL_VALUE 6
  354. #endif
  355. /**
  356. * @brief AHB prescaler value.
  357. * @note The default value is calculated for a 48MHz system clock from
  358. * a 8MHz crystal using the PLL.
  359. */
  360. #if !defined(STM32_HPRE) || defined(__DOXYGEN__)
  361. #define STM32_HPRE STM32_HPRE_DIV1
  362. #endif
  363. /**
  364. * @brief APB1 prescaler value.
  365. */
  366. #if !defined(STM32_PPRE) || defined(__DOXYGEN__)
  367. #define STM32_PPRE STM32_PPRE_DIV1
  368. #endif
  369. /**
  370. * @brief MCO pin setting.
  371. */
  372. #if !defined(STM32_MCOSEL) || defined(__DOXYGEN__)
  373. #define STM32_MCOSEL STM32_MCOSEL_NOCLOCK
  374. #endif
  375. /**
  376. * @brief MCO divider setting.
  377. */
  378. #if !defined(STM32_MCOPRE) || defined(__DOXYGEN__)
  379. #define STM32_MCOPRE STM32_MCOPRE_DIV1
  380. #endif
  381. /**
  382. * @brief MCO PLL divider setting.
  383. */
  384. #if !defined(STM32_PLLNODIV) || defined(__DOXYGEN__)
  385. #define STM32_PLLNODIV STM32_PLLNODIV_DIV2
  386. #endif
  387. /**
  388. * @brief USB Clock source.
  389. */
  390. #if !defined(STM32_USBSW) || defined(__DOXYGEN__)
  391. #define STM32_USBSW STM32_USBSW_HSI48
  392. #endif
  393. /**
  394. * @brief CEC clock source.
  395. */
  396. #if !defined(STM32_CECSW) || defined(__DOXYGEN__)
  397. #define STM32_CECSW STM32_CECSW_HSI
  398. #endif
  399. /**
  400. * @brief I2C1 clock source.
  401. */
  402. #if !defined(STM32_I2C1SW) || defined(__DOXYGEN__)
  403. #define STM32_I2C1SW STM32_I2C1SW_HSI
  404. #endif
  405. /**
  406. * @brief USART1 clock source.
  407. */
  408. #if !defined(STM32_USART1SW) || defined(__DOXYGEN__)
  409. #define STM32_USART1SW STM32_USART1SW_PCLK
  410. #endif
  411. /**
  412. * @brief RTC clock source.
  413. */
  414. #if !defined(STM32_RTCSEL) || defined(__DOXYGEN__)
  415. #define STM32_RTCSEL STM32_RTCSEL_LSI
  416. #endif
  417. /** @} */
  418. /*===========================================================================*/
  419. /* Derived constants and error checks. */
  420. /*===========================================================================*/
  421. /*
  422. * Configuration-related checks.
  423. */
  424. #if !defined(STM32F0xx_MCUCONF)
  425. #error "Using a wrong mcuconf.h file, STM32F0xx_MCUCONF not defined"
  426. #endif
  427. /*
  428. * HSI related checks.
  429. */
  430. #if STM32_HSI_ENABLED
  431. #if (STM32_SW == STM32_SW_PLL) && \
  432. (STM32_PLLSRC == STM32_PLLSRC_HSI) && !STM32_HAS_HSI_PREDIV
  433. #error "STM32_PLLSRC_HSI not available on this platform. Select STM32_PLLSRC_HSI_DIV2 instead."
  434. #endif
  435. #else /* !STM32_HSI_ENABLED */
  436. #if STM32_SW == STM32_SW_HSI
  437. #error "HSI not enabled, required by STM32_SW"
  438. #endif
  439. #if STM32_CECSW == STM32_CECSW_HSI
  440. #error "HSI not enabled, required by STM32_CECSW"
  441. #endif
  442. #if STM32_I2C1SW == STM32_I2C1SW_HSI
  443. #error "HSI not enabled, required by STM32_I2C1SW"
  444. #endif
  445. #if STM32_USART1SW == STM32_USART1SW_HSI
  446. #error "HSI not enabled, required by STM32_USART1SW"
  447. #endif
  448. #if (STM32_SW == STM32_SW_PLL) && \
  449. (STM32_PLLSRC == STM32_PLLSRC_HSI_DIV2) || \
  450. (STM32_PLLSRC == STM32_PLLSRC_HSI)
  451. #error "HSI not enabled, required by STM32_SW and STM32_PLLSRC"
  452. #endif
  453. #if (STM32_MCOSEL == STM32_MCOSEL_HSI) || \
  454. ((STM32_MCOSEL == STM32_MCOSEL_PLLDIV2) && \
  455. ((STM32_PLLSRC == STM32_PLLSRC_HSI_DIV2) || \
  456. (STM32_PLLSRC == STM32_PLLSRC_HSI)))
  457. #error "HSI not enabled, required by STM32_MCOSEL"
  458. #endif
  459. #endif /* !STM32_HSI_ENABLED */
  460. /*
  461. * HSI14 related checks.
  462. */
  463. #if STM32_HSI14_ENABLED
  464. #else /* !STM32_HSI14_ENABLED */
  465. #if STM32_MCOSEL == STM32_MCOSEL_HSI14
  466. #error "HSI14 not enabled, required by STM32_MCOSEL"
  467. #endif
  468. #endif /* !STM32_HSI14_ENABLED */
  469. /*
  470. * HSI48 related checks.
  471. */
  472. #if STM32_HSI48_ENABLED
  473. #if !STM32_HAS_HSI48
  474. #error "HSI48 not available on this platform"
  475. #endif
  476. #else /* !STM32_HSI48_ENABLED */
  477. #if STM32_SW == STM32_SW_HSI48
  478. #error "HSI48 not enabled, required by STM32_SW"
  479. #endif
  480. #if (STM32_MCOSEL == STM32_MCOSEL_HSI48) || \
  481. ((STM32_MCOSEL == STM32_MCOSEL_PLLDIV2) && \
  482. ((STM32_PLLSRC == STM32_PLLSRC_HSI_DIV2) || \
  483. (STM32_PLLSRC == STM32_PLLSRC_HSI48)))
  484. #error "HSI48 not enabled, required by STM32_MCOSEL"
  485. #endif
  486. #if (STM32_SW == STM32_SW_PLL) && (STM32_PLLSRC == STM32_PLLSRC_HSI48)
  487. #error "HSI48 not enabled, required by STM32_SW and STM32_PLLSRC"
  488. #endif
  489. #endif /* !STM32_HSI48_ENABLED */
  490. /*
  491. * HSE related checks.
  492. */
  493. #if STM32_HSE_ENABLED
  494. #if STM32_HSECLK == 0
  495. #error "HSE frequency not defined"
  496. #elif (STM32_HSECLK < STM32_HSECLK_MIN) || (STM32_HSECLK > STM32_HSECLK_MAX)
  497. #error "STM32_HSECLK outside acceptable range (STM32_HSECLK_MIN...STM32_HSECLK_MAX)"
  498. #endif
  499. #else /* !STM32_HSE_ENABLED */
  500. #if STM32_SW == STM32_SW_HSE
  501. #error "HSE not enabled, required by STM32_SW"
  502. #endif
  503. #if (STM32_SW == STM32_SW_PLL) && (STM32_PLLSRC == STM32_PLLSRC_HSE)
  504. #error "HSE not enabled, required by STM32_SW and STM32_PLLSRC"
  505. #endif
  506. #if (STM32_MCOSEL == STM32_MCOSEL_HSE) || \
  507. ((STM32_MCOSEL == STM32_MCOSEL_PLLDIV2) && \
  508. (STM32_PLLSRC == STM32_PLLSRC_HSE))
  509. #error "HSE not enabled, required by STM32_MCOSEL"
  510. #endif
  511. #if STM32_RTCSEL == STM32_RTCSEL_HSEDIV
  512. #error "HSE not enabled, required by STM32_RTCSEL"
  513. #endif
  514. #endif /* !STM32_HSE_ENABLED */
  515. /*
  516. * LSI related checks.
  517. */
  518. #if STM32_LSI_ENABLED
  519. #else /* !STM32_LSI_ENABLED */
  520. #if STM32_RTCSEL == STM32_RTCSEL_LSI
  521. #error "LSI not enabled, required by STM32_RTCSEL"
  522. #endif
  523. #endif /* !STM32_LSI_ENABLED */
  524. /*
  525. * LSE related checks.
  526. */
  527. #if STM32_LSE_ENABLED
  528. #if (STM32_LSECLK == 0)
  529. #error "LSE frequency not defined"
  530. #endif
  531. #if (STM32_LSECLK < STM32_LSECLK_MIN) || (STM32_LSECLK > STM32_LSECLK_MAX)
  532. #error "STM32_LSECLK outside acceptable range (STM32_LSECLK_MIN...STM32_LSECLK_MAX)"
  533. #endif
  534. #if !defined(STM32_LSEDRV)
  535. #error "STM32_LSEDRV not defined"
  536. #endif
  537. #if (STM32_LSEDRV >> 3) > 3
  538. #error "STM32_LSEDRV outside acceptable range ((0<<3)...(3<<3))"
  539. #endif
  540. #else /* !STM32_LSE_ENABLED */
  541. #if STM32_CECSW == STM32_CECSW_LSE
  542. #error "LSE not enabled, required by STM32_CECSW"
  543. #endif
  544. #if STM32_USART1SW == STM32_USART1SW_LSE
  545. #error "LSE not enabled, required by STM32_USART1SW"
  546. #endif
  547. #if STM32_RTCSEL == STM32_RTCSEL_LSE
  548. #error "LSE not enabled, required by STM32_RTCSEL"
  549. #endif
  550. #endif /* !STM32_LSE_ENABLED */
  551. /* PLL activation conditions.*/
  552. #if (STM32_SW == STM32_SW_PLL) || \
  553. (STM32_USBSW == STM32_USBSW_PCLK) || \
  554. (STM32_MCOSEL == STM32_MCOSEL_PLLDIV2) || \
  555. defined(__DOXYGEN__)
  556. /**
  557. * @brief PLL activation flag.
  558. */
  559. #define STM32_ACTIVATE_PLL TRUE
  560. #else
  561. #define STM32_ACTIVATE_PLL FALSE
  562. #endif
  563. /* HSE, HSI prescaler setting check.*/
  564. #if ((STM32_PREDIV_VALUE >= 1) || (STM32_PREDIV_VALUE <= 16))
  565. #define STM32_PREDIV ((STM32_PREDIV_VALUE - 1) << 0)
  566. #else
  567. #error "invalid STM32_PREDIV value specified"
  568. #endif
  569. /**
  570. * @brief PLLMUL field.
  571. */
  572. #if ((STM32_PLLMUL_VALUE >= 2) && (STM32_PLLMUL_VALUE <= 16)) || \
  573. defined(__DOXYGEN__)
  574. #define STM32_PLLMUL ((STM32_PLLMUL_VALUE - 2) << 18)
  575. #else
  576. #error "invalid STM32_PLLMUL_VALUE value specified"
  577. #endif
  578. /**
  579. * @brief PLL input clock frequency.
  580. */
  581. #if (STM32_PLLSRC == STM32_PLLSRC_HSE) || defined(__DOXYGEN__)
  582. #define STM32_PLLCLKIN (STM32_HSECLK / STM32_PREDIV_VALUE)
  583. #elif STM32_PLLSRC == STM32_PLLSRC_HSI_DIV2
  584. #define STM32_PLLCLKIN (STM32_HSICLK / 2)
  585. #elif STM32_PLLSRC == STM32_PLLSRC_HSI
  586. #define STM32_PLLCLKIN (STM32_HSICLK / STM32_PREDIV_VALUE)
  587. #elif STM32_PLLSRC == STM32_PLLSRC_HSI48
  588. #define STM32_PLLCLKIN (STM32_HSI48CLK / STM32_PREDIV_VALUE)
  589. #else
  590. #error "invalid STM32_PLLSRC value specified"
  591. #endif
  592. /* PLL input frequency range check.*/
  593. #if (STM32_PLLCLKIN < STM32_PLLIN_MIN) || (STM32_PLLCLKIN > STM32_PLLIN_MAX)
  594. #error "STM32_PLLCLKIN outside acceptable range (STM32_PLLIN_MIN...STM32_PLLIN_MAX)"
  595. #endif
  596. /**
  597. * @brief PLL output clock frequency.
  598. */
  599. #define STM32_PLLCLKOUT (STM32_PLLCLKIN * STM32_PLLMUL_VALUE)
  600. /* PLL output frequency range check.*/
  601. #if (STM32_PLLCLKOUT < STM32_PLLOUT_MIN) || (STM32_PLLCLKOUT > STM32_PLLOUT_MAX)
  602. #error "STM32_PLLCLKOUT outside acceptable range (STM32_PLLOUT_MIN...STM32_PLLOUT_MAX)"
  603. #endif
  604. /**
  605. * @brief System clock source.
  606. */
  607. #if (STM32_SW == STM32_SW_PLL) || defined(__DOXYGEN__)
  608. #define STM32_SYSCLK STM32_PLLCLKOUT
  609. #elif (STM32_SW == STM32_SW_HSI)
  610. #define STM32_SYSCLK STM32_HSICLK
  611. #elif (STM32_SW == STM32_SW_HSI48)
  612. #define STM32_SYSCLK STM32_HSI48CLK
  613. #elif (STM32_SW == STM32_SW_HSE)
  614. #define STM32_SYSCLK STM32_HSECLK
  615. #else
  616. #error "invalid STM32_SW value specified"
  617. #endif
  618. /* Check on the system clock.*/
  619. #if STM32_SYSCLK > STM32_SYSCLK_MAX
  620. #error "STM32_SYSCLK above maximum rated frequency (STM32_SYSCLK_MAX)"
  621. #endif
  622. /**
  623. * @brief AHB frequency.
  624. */
  625. #if (STM32_HPRE == STM32_HPRE_DIV1) || defined(__DOXYGEN__)
  626. #define STM32_HCLK (STM32_SYSCLK / 1)
  627. #elif STM32_HPRE == STM32_HPRE_DIV2
  628. #define STM32_HCLK (STM32_SYSCLK / 2)
  629. #elif STM32_HPRE == STM32_HPRE_DIV4
  630. #define STM32_HCLK (STM32_SYSCLK / 4)
  631. #elif STM32_HPRE == STM32_HPRE_DIV8
  632. #define STM32_HCLK (STM32_SYSCLK / 8)
  633. #elif STM32_HPRE == STM32_HPRE_DIV16
  634. #define STM32_HCLK (STM32_SYSCLK / 16)
  635. #elif STM32_HPRE == STM32_HPRE_DIV64
  636. #define STM32_HCLK (STM32_SYSCLK / 64)
  637. #elif STM32_HPRE == STM32_HPRE_DIV128
  638. #define STM32_HCLK (STM32_SYSCLK / 128)
  639. #elif STM32_HPRE == STM32_HPRE_DIV256
  640. #define STM32_HCLK (STM32_SYSCLK / 256)
  641. #elif STM32_HPRE == STM32_HPRE_DIV512
  642. #define STM32_HCLK (STM32_SYSCLK / 512)
  643. #else
  644. #error "invalid STM32_HPRE value specified"
  645. #endif
  646. /* AHB frequency check.*/
  647. #if STM32_HCLK > STM32_SYSCLK_MAX
  648. #error "STM32_HCLK exceeding maximum frequency (STM32_SYSCLK_MAX)"
  649. #endif
  650. /**
  651. * @brief APB frequency.
  652. */
  653. #if (STM32_PPRE == STM32_PPRE_DIV1) || defined(__DOXYGEN__)
  654. #define STM32_PCLK (STM32_HCLK / 1)
  655. #elif STM32_PPRE == STM32_PPRE_DIV2
  656. #define STM32_PCLK (STM32_HCLK / 2)
  657. #elif STM32_PPRE == STM32_PPRE_DIV4
  658. #define STM32_PCLK (STM32_HCLK / 4)
  659. #elif STM32_PPRE == STM32_PPRE_DIV8
  660. #define STM32_PCLK (STM32_HCLK / 8)
  661. #elif STM32_PPRE == STM32_PPRE_DIV16
  662. #define STM32_PCLK (STM32_HCLK / 16)
  663. #else
  664. #error "invalid STM32_PPRE value specified"
  665. #endif
  666. /* APB frequency check.*/
  667. #if STM32_PCLK > STM32_PCLK_MAX
  668. #error "STM32_PCLK exceeding maximum frequency (STM32_PCLK_MAX)"
  669. #endif
  670. /* STM32_PLLNODIV check.*/
  671. #if (STM32_PLLNODIV != STM32_PLLNODIV_DIV2) && \
  672. (STM32_PLLNODIV != STM32_PLLNODIV_DIV1)
  673. #error "invalid STM32_PLLNODIV value specified"
  674. #endif
  675. /**
  676. * @brief MCO clock before divider.
  677. */
  678. #if (STM32_MCOSEL == STM32_MCOSEL_NOCLOCK) || defined(__DOXYGEN__)
  679. #define STM32_MCODIVCLK 0
  680. #elif STM32_MCOSEL == STM32_MCOSEL_HSI14
  681. #define STM32_MCODIVCLK STM32_HSI14CLK
  682. #elif STM32_MCOSEL == STM32_MCOSEL_LSI
  683. #define STM32_MCODIVCLK STM32_LSICLK
  684. #elif STM32_MCOSEL == STM32_MCOSEL_LSE
  685. #define STM32_MCODIVCLK STM32_LSECLK
  686. #elif STM32_MCOSEL == STM32_MCOSEL_SYSCLK
  687. #define STM32_MCODIVCLK STM32_SYSCLK
  688. #elif STM32_MCOSEL == STM32_MCOSEL_HSI
  689. #define STM32_MCODIVCLK STM32_HSICLK
  690. #elif STM32_MCOSEL == STM32_MCOSEL_HSE
  691. #define STM32_MCODIVCLK STM32_HSECLK
  692. #elif STM32_MCOSEL == STM32_MCOSEL_PLLDIV2
  693. #if STM32_PLLNODIV == STM32_PLLNODIV_DIV2
  694. #define STM32_MCODIVCLK (STM32_PLLCLKOUT / 2)
  695. #else
  696. #define STM32_MCODIVCLK (STM32_PLLCLKOUT / 1)
  697. #endif
  698. #elif STM32_MCOSEL == STM32_MCOSEL_HSI48
  699. #define STM32_MCODIVCLK STM32_HSI48CLK
  700. #else
  701. #error "invalid STM32_MCOSEL value specified"
  702. #endif
  703. /**
  704. * @brief MCO output pin clock.
  705. */
  706. #if (STM32_MCOPRE == STM32_MCOPRE_DIV1) || defined(__DOXYGEN__)
  707. #define STM32_MCOCLK STM32_MCODIVCLK
  708. #elif (STM32_MCOPRE == STM32_MCOPRE_DIV2) && STM32_HAS_MCO_PREDIV
  709. #define STM32_MCOCLK (STM32_MCODIVCLK / 2)
  710. #elif (STM32_MCOPRE == STM32_MCOPRE_DIV4) && STM32_HAS_MCO_PREDIV
  711. #define STM32_MCOCLK (STM32_MCODIVCLK / 4)
  712. #elif (STM32_MCOPRE == STM32_MCOPRE_DIV8) && STM32_HAS_MCO_PREDIV
  713. #define STM32_MCOCLK (STM32_MCODIVCLK / 8)
  714. #elif (STM32_MCOPRE == STM32_MCOPRE_DIV16) && STM32_HAS_MCO_PREDIV
  715. #define STM32_MCOCLK (STM32_MCODIVCLK / 16)
  716. #elif !STM32_HAS_MCO_PREDIV
  717. #error "MCO_PREDIV not available on this platform. Select STM32_MCODIVCLK."
  718. #else
  719. #error "invalid STM32_MCOPRE value specified"
  720. #endif
  721. /**
  722. * @brief RTC clock.
  723. */
  724. #if (STM32_RTCSEL == STM32_RTCSEL_LSE) || defined(__DOXYGEN__)
  725. #define STM32_RTCCLK STM32_LSECLK
  726. #elif STM32_RTCSEL == STM32_RTCSEL_LSI
  727. #define STM32_RTCCLK STM32_LSICLK
  728. #elif STM32_RTCSEL == STM32_RTCSEL_HSEDIV
  729. #define STM32_RTCCLK (STM32_HSECLK / 32)
  730. #elif STM32_RTCSEL == STM32_RTCSEL_NOCLOCK
  731. #define STM32_RTCCLK 0
  732. #else
  733. #error "invalid source selected for RTC clock"
  734. #endif
  735. /**
  736. * @brief USB frequency.
  737. */
  738. #if (STM32_USBSW == STM32_USBSW_HSI48) || defined(__DOXYGEN__)
  739. #define STM32_USBCLK STM32_HSI48CLK
  740. #elif STM32_USBSW == STM32_USBSW_PCLK
  741. #define STM32_USBCLK STM32_PLLCLKOUT
  742. #else
  743. #error "invalid source selected for USB clock"
  744. #endif
  745. /**
  746. * @brief CEC frequency.
  747. */
  748. #if (STM32_CECSW == STM32_CECSW_HSI) || defined(__DOXYGEN__)
  749. #define STM32_CECCLK STM32_HSICLK
  750. #elif STM32_CECSW == STM32_CECSW_LSE
  751. #define STM32_CECCLK STM32_LSECLK
  752. #elif STM32_CECSW == STM32_CECSW_OFF
  753. #define STM32_CECCLK 0
  754. #else
  755. #error "invalid source selected for CEC clock"
  756. #endif
  757. /**
  758. * @brief I2C1 frequency.
  759. */
  760. #if (STM32_I2C1SW == STM32_I2C1SW_HSI) || defined(__DOXYGEN__)
  761. #define STM32_I2C1CLK STM32_HSICLK
  762. #elif STM32_I2C1SW == STM32_I2C1SW_SYSCLK
  763. #define STM32_I2C1CLK STM32_SYSCLK
  764. #else
  765. #error "invalid source selected for I2C1 clock"
  766. #endif
  767. /**
  768. * @brief USART1 frequency.
  769. */
  770. #if (STM32_USART1SW == STM32_USART1SW_PCLK) || defined(__DOXYGEN__)
  771. #define STM32_USART1CLK STM32_PCLK
  772. #elif STM32_USART1SW == STM32_USART1SW_SYSCLK
  773. #define STM32_USART1CLK STM32_SYSCLK
  774. #elif STM32_USART1SW == STM32_USART1SW_LSE
  775. #define STM32_USART1CLK STM32_LSECLK
  776. #elif STM32_USART1SW == STM32_USART1SW_HSI
  777. #define STM32_USART1CLK STM32_HSICLK
  778. #else
  779. #error "invalid source selected for USART1 clock"
  780. #endif
  781. /**
  782. * @brief USART2 frequency.
  783. */
  784. #define STM32_USART2CLK STM32_PCLK
  785. /**
  786. * @brief USART3 frequency.
  787. */
  788. #define STM32_USART3CLK STM32_PCLK
  789. /**
  790. * @brief USART4 frequency.
  791. */
  792. #define STM32_UART4CLK STM32_PCLK
  793. /**
  794. * @brief USART5 frequency.
  795. */
  796. #define STM32_UART5CLK STM32_PCLK
  797. /**
  798. * @brief USART6 frequency.
  799. */
  800. #define STM32_USART6CLK STM32_PCLK
  801. /**
  802. * @brief USART7 frequency.
  803. */
  804. #define STM32_UART7CLK STM32_PCLK
  805. /**
  806. * @brief USART8 frequency.
  807. */
  808. #define STM32_UART8CLK STM32_PCLK
  809. /**
  810. * @brief Timers clock.
  811. */
  812. #if (STM32_PPRE == STM32_PPRE_DIV1) || defined(__DOXYGEN__)
  813. #define STM32_TIMCLK1 (STM32_PCLK * 1)
  814. #define STM32_TIMCLK2 (STM32_PCLK * 1)
  815. #else
  816. #define STM32_TIMCLK1 (STM32_PCLK * 2)
  817. #define STM32_TIMCLK2 (STM32_PCLK * 2)
  818. #endif
  819. /**
  820. * @brief Flash settings.
  821. */
  822. #if (STM32_HCLK <= 24000000) || defined(__DOXYGEN__)
  823. #define STM32_FLASHBITS 0x00000010
  824. #else
  825. #define STM32_FLASHBITS 0x00000011
  826. #endif
  827. /*
  828. * For compatibility with driver assuming a specific PPRE clock.
  829. */
  830. #define STM32_PCLK1 STM32_PCLK
  831. #define STM32_PCLK2 STM32_PCLK
  832. /*===========================================================================*/
  833. /* Driver data structures and types. */
  834. /*===========================================================================*/
  835. /*===========================================================================*/
  836. /* Driver macros. */
  837. /*===========================================================================*/
  838. /*===========================================================================*/
  839. /* External declarations. */
  840. /*===========================================================================*/
  841. /* Various helpers.*/
  842. #include "nvic.h"
  843. #include "cache.h"
  844. #include "stm32_isr.h"
  845. #include "stm32_dma.h"
  846. #include "stm32_exti.h"
  847. #include "stm32_rcc.h"
  848. #ifdef __cplusplus
  849. extern "C" {
  850. #endif
  851. void hal_lld_init(void);
  852. void stm32_clock_init(void);
  853. #ifdef __cplusplus
  854. }
  855. #endif
  856. #endif /* HAL_LLD_H */
  857. /** @} */