hal_lld.h 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214
  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 STM32L0xx/hal_lld.h
  15. * @brief STM32L0xx 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. * - STM32L011xx, STM32L031xx,
  26. * STM32L051xx, STM32L052xx, STM32L053xx,
  27. * STM32L061xx, STM32L062xx, STM32L063xx,
  28. * STM32L071xx, STM32L072xx, STM32L073xx for ultra-low-power MCUs.
  29. * .
  30. *
  31. * @addtogroup HAL
  32. * @{
  33. */
  34. #ifndef HAL_LLD_H
  35. #define HAL_LLD_H
  36. /*
  37. * Registry definitions.
  38. */
  39. #include "stm32_registry.h"
  40. /*===========================================================================*/
  41. /* Driver constants. */
  42. /*===========================================================================*/
  43. /**
  44. * @name Platform identification macros
  45. * @{
  46. */
  47. #if defined(STM32L011xx) || defined(__DOXYGEN__)
  48. #define PLATFORM_NAME "STM32L011xx ultra-low-power MCU"
  49. #elif defined(STM32L031xx)
  50. #define PLATFORM_NAME "STM32L031xx ultra-low-power MCU"
  51. #elif defined(STM32L051xx)
  52. #define PLATFORM_NAME "STM32L051xx ultra-low-power MCU"
  53. #elif defined(STM32L052xx)
  54. #define PLATFORM_NAME "STM32L052xx ultra-low-power MCU"
  55. #elif defined(STM32L053xx)
  56. #define PLATFORM_NAME "STM32L053xx ultra-low-power MCU"
  57. #elif defined(STM32L061xx)
  58. #define PLATFORM_NAME "STM32L061xx ultra-low-power MCU"
  59. #elif defined(STM32L062xx)
  60. #define PLATFORM_NAME "STM32L062xx ultra-low-power MCU"
  61. #elif defined(STM32L063xx)
  62. #define PLATFORM_NAME "STM32L063xx ultra-low-power MCU"
  63. #elif defined(STM32L071xx)
  64. #define PLATFORM_NAME "STM32L071xx ultra-low-power MCU"
  65. #elif defined(STM32L072xx)
  66. #define PLATFORM_NAME "STM32L073xx ultra-low-power MCU"
  67. #elif defined(STM32L073xx)
  68. #define PLATFORM_NAME "STM32L073xx ultra-low-power MCU"
  69. #else
  70. #error "STM32L0xx device not specified"
  71. #endif
  72. /** @} */
  73. /**
  74. * @name Sub-family identifier
  75. */
  76. #if !defined(STM32L0XX) || defined(__DOXYGEN__)
  77. #define STM32L0XX
  78. #endif
  79. /** @} */
  80. /**
  81. * @name Internal clock sources
  82. * @{
  83. */
  84. #define STM32_HSI16CLK 16000000 /**< 16MHz internal clock. */
  85. #define STM32_HSI48CLK 48000000 /**< 48MHz internal clock. */
  86. #define STM32_LSICLK 37000 /**< Low speed internal clock. */
  87. /** @} */
  88. /**
  89. * @name PWR_CR register bits definitions
  90. * @{
  91. */
  92. #define STM32_PLS_MASK (7 << 5) /**< PLS field mask. */
  93. #define STM32_PLS_LEV0 (0 << 5) /**< PVD level 0. */
  94. #define STM32_PLS_LEV1 (1 << 5) /**< PVD level 1. */
  95. #define STM32_PLS_LEV2 (2 << 5) /**< PVD level 2. */
  96. #define STM32_PLS_LEV3 (3 << 5) /**< PVD level 3. */
  97. #define STM32_PLS_LEV4 (4 << 5) /**< PVD level 4. */
  98. #define STM32_PLS_LEV5 (5 << 5) /**< PVD level 5. */
  99. #define STM32_PLS_LEV6 (6 << 5) /**< PVD level 6. */
  100. #define STM32_PLS_EXT (7 << 5) /**< PVD level 7. */
  101. #define STM32_VOS_MASK (3 << 11) /**< VOS field mask. */
  102. #define STM32_VOS_1P8 (1 << 11) /**< VOS level 1.8 volts. */
  103. #define STM32_VOS_1P5 (2 << 11) /**< VOS level 1.5 volts. */
  104. #define STM32_VOS_1P2 (3 << 11) /**< VOS level 1.2 volts. */
  105. /** @} */
  106. /**
  107. * @name RCC_CR register bits definitions
  108. * @{
  109. */
  110. #define STM32_RTCPRE_MASK (3 << 20) /**< RTCPRE mask. */
  111. #define STM32_RTCPRE_DIV2 (0 << 20) /**< HSE divided by 2. */
  112. #define STM32_RTCPRE_DIV4 (1 << 20) /**< HSE divided by 4. */
  113. #define STM32_RTCPRE_DIV8 (2 << 20) /**< HSE divided by 2. */
  114. #define STM32_RTCPRE_DIV16 (3 << 20) /**< HSE divided by 16. */
  115. /** @} */
  116. /**
  117. * @name RCC_CFGR register bits definitions
  118. * @{
  119. */
  120. #define STM32_SW_MASK (3 << 0) /**< SW field mask. */
  121. #define STM32_SW_MSI (0 << 0) /**< SYSCLK source is MSI. */
  122. #define STM32_SW_HSI16 (1 << 0) /**< SYSCLK source is HSI16 */
  123. #define STM32_SW_HSE (2 << 0) /**< SYSCLK source is HSE. */
  124. #define STM32_SW_PLL (3 << 0) /**< SYSCLK source is PLL. */
  125. #define STM32_HPRE_MASK (15 << 4) /**< HPRE field mask. */
  126. #define STM32_HPRE_DIV1 (0 << 4) /**< SYSCLK divided by 1. */
  127. #define STM32_HPRE_DIV2 (8 << 4) /**< SYSCLK divided by 2. */
  128. #define STM32_HPRE_DIV4 (9 << 4) /**< SYSCLK divided by 4. */
  129. #define STM32_HPRE_DIV8 (10 << 4) /**< SYSCLK divided by 8. */
  130. #define STM32_HPRE_DIV16 (11 << 4) /**< SYSCLK divided by 16. */
  131. #define STM32_HPRE_DIV64 (12 << 4) /**< SYSCLK divided by 64. */
  132. #define STM32_HPRE_DIV128 (13 << 4) /**< SYSCLK divided by 128. */
  133. #define STM32_HPRE_DIV256 (14 << 4) /**< SYSCLK divided by 256. */
  134. #define STM32_HPRE_DIV512 (15 << 4) /**< SYSCLK divided by 512. */
  135. #define STM32_PPRE1_MASK (7 << 8) /**< PPRE2 field mask. */
  136. #define STM32_PPRE1_DIV1 (0 << 8) /**< HCLK divided by 1. */
  137. #define STM32_PPRE1_DIV2 (4 << 8) /**< HCLK divided by 2. */
  138. #define STM32_PPRE1_DIV4 (5 << 8) /**< HCLK divided by 4. */
  139. #define STM32_PPRE1_DIV8 (6 << 8) /**< HCLK divided by 8. */
  140. #define STM32_PPRE1_DIV16 (7 << 8) /**< HCLK divided by 16. */
  141. #define STM32_PPRE2_MASK (7 << 11) /**< PPRE2 field mask. */
  142. #define STM32_PPRE2_DIV1 (0 << 11) /**< HCLK divided by 1. */
  143. #define STM32_PPRE2_DIV2 (4 << 11) /**< HCLK divided by 2. */
  144. #define STM32_PPRE2_DIV4 (5 << 11) /**< HCLK divided by 4. */
  145. #define STM32_PPRE2_DIV8 (6 << 11) /**< HCLK divided by 8. */
  146. #define STM32_PPRE2_DIV16 (7 << 11) /**< HCLK divided by 16. */
  147. #define STM32_STOPWUCK_MASK (1 << 15) /**< PLLDIV field mask. */
  148. #define STM32_STOPWUCK_MSI (0 << 15) /**< MSI is wakeup clock. */
  149. #define STM32_STOPWUCK_HSI16 (1 << 15) /**< HSI16 is wakeup clock. */
  150. #define STM32_PLLSRC_MASK (1 << 16) /**< PLLSRC field mask. */
  151. #define STM32_PLLSRC_HSI16 (0 << 16) /**< PLL clock source is HSI16. */
  152. #define STM32_PLLSRC_HSE (1 << 16) /**< PLL clock source is HSE. */
  153. #define STM32_PLLMUL_MASK (15 << 18) /**< PLLMUL field mask. */
  154. #define STM32_PLLMUL_MUL3 (0 << 18) /**< PLL multiplier is 3. */
  155. #define STM32_PLLMUL_MUL4 (1 << 18) /**< PLL multiplier is 4. */
  156. #define STM32_PLLMUL_MUL6 (2 << 18) /**< PLL multiplier is 6. */
  157. #define STM32_PLLMUL_MUL8 (3 << 18) /**< PLL multiplier is 8. */
  158. #define STM32_PLLMUL_MUL12 (4 << 18) /**< PLL multiplier is 12. */
  159. #define STM32_PLLMUL_MUL16 (5 << 18) /**< PLL multiplier is 16. */
  160. #define STM32_PLLMUL_MUL24 (6 << 18) /**< PLL multiplier is 24. */
  161. #define STM32_PLLMUL_MUL32 (7 << 18) /**< PLL multiplier is 32. */
  162. #define STM32_PLLMUL_MUL48 (8 << 18) /**< PLL multiplier is 48. */
  163. #define STM32_PLLDIV_MASK (3 << 22) /**< PLLDIV field mask. */
  164. #define STM32_PLLDIV_DIV2 (1 << 22) /**< PLL divided by 2. */
  165. #define STM32_PLLDIV_DIV3 (2 << 22) /**< PLL divided by 3. */
  166. #define STM32_PLLDIV_DIV4 (3 << 22) /**< PLL divided by 4. */
  167. #define STM32_MCOSEL_MASK (15 << 24) /**< MCOSEL field mask. */
  168. #define STM32_MCOSEL_NOCLOCK (0 << 24) /**< No clock on MCO pin. */
  169. #define STM32_MCOSEL_SYSCLK (1 << 24) /**< SYSCLK on MCO pin. */
  170. #define STM32_MCOSEL_HSI16 (2 << 24) /**< HSI16 clock on MCO pin. */
  171. #define STM32_MCOSEL_MSI (3 << 24) /**< MSI clock on MCO pin. */
  172. #define STM32_MCOSEL_HSE (4 << 24) /**< HSE clock on MCO pin. */
  173. #define STM32_MCOSEL_PLL (5 << 24) /**< PLL clock on MCO pin. */
  174. #define STM32_MCOSEL_LSI (6 << 24) /**< LSI clock on MCO pin. */
  175. #define STM32_MCOSEL_LSE (7 << 24) /**< LSE clock on MCO pin. */
  176. #define STM32_MCOSEL_HSI48 (8 << 24) /**< HSI48 clock on MCO pin. */
  177. #define STM32_MCOPRE_MASK (7 << 28) /**< MCOPRE field mask. */
  178. #define STM32_MCOPRE_DIV1 (0 << 28) /**< MCO is divided by 1. */
  179. #define STM32_MCOPRE_DIV2 (1 << 28) /**< MCO is divided by 1. */
  180. #define STM32_MCOPRE_DIV4 (2 << 28) /**< MCO is divided by 1. */
  181. #define STM32_MCOPRE_DIV8 (3 << 28) /**< MCO is divided by 1. */
  182. #define STM32_MCOPRE_DIV16 (4 << 28) /**< MCO is divided by 1. */
  183. /** @} */
  184. /**
  185. * @name RCC_ICSCR register bits definitions
  186. * @{
  187. */
  188. #define STM32_MSIRANGE_MASK (7 << 13) /**< MSIRANGE field mask. */
  189. #define STM32_MSIRANGE_64K (0 << 13) /**< 64kHz nominal. */
  190. #define STM32_MSIRANGE_128K (1 << 13) /**< 128kHz nominal. */
  191. #define STM32_MSIRANGE_256K (2 << 13) /**< 256kHz nominal. */
  192. #define STM32_MSIRANGE_512K (3 << 13) /**< 512kHz nominal. */
  193. #define STM32_MSIRANGE_1M (4 << 13) /**< 1MHz nominal. */
  194. #define STM32_MSIRANGE_2M (5 << 13) /**< 2MHz nominal. */
  195. #define STM32_MSIRANGE_4M (6 << 13) /**< 4MHz nominal */
  196. /** @} */
  197. /**
  198. * @name RCC_CSR register bits definitions
  199. * @{
  200. */
  201. #define STM32_RTCSEL_MASK (3 << 16) /**< RTC source mask. */
  202. #define STM32_RTCSEL_NOCLOCK (0 << 16) /**< No RTC source. */
  203. #define STM32_RTCSEL_LSE (1 << 16) /**< RTC source is LSE. */
  204. #define STM32_RTCSEL_LSI (2 << 16) /**< RTC source is LSI. */
  205. #define STM32_RTCSEL_HSEDIV (3 << 16) /**< RTC source is HSE divided. */
  206. /** @} */
  207. /**
  208. * @name RCC_CCIPR register bits definitions
  209. * @{
  210. */
  211. #define STM32_USART1SEL_MASK (3 << 0) /**< USART1 clock source mask. */
  212. #define STM32_USART1SEL_APB (0 << 0) /**< USART1 clock is APB. */
  213. #define STM32_USART1SEL_SYSCLK (1 << 0) /**< USART1 clock is SYSCLK. */
  214. #define STM32_USART1SEL_HSI16 (2 << 0) /**< USART1 clock is HSI16. */
  215. #define STM32_USART1SEL_LSE (3 << 0) /**< USART1 clock is LSE. */
  216. #define STM32_USART2SEL_MASK (3 << 2) /**< USART2 clock source mask. */
  217. #define STM32_USART2SEL_APB (0 << 2) /**< USART2 clock is APB. */
  218. #define STM32_USART2SEL_SYSCLK (1 << 2) /**< USART2 clock is SYSCLK. */
  219. #define STM32_USART2SEL_HSI16 (2 << 2) /**< USART2 clock is HSI16. */
  220. #define STM32_USART2SEL_LSE (3 << 2) /**< USART2 clock is LSE. */
  221. #define STM32_LPUART1SEL_MASK (3 << 10) /**< LPUART1 clock source mask. */
  222. #define STM32_LPUART1SEL_APB (0 << 10) /**< LPUART1 clock is APB. */
  223. #define STM32_LPUART1SEL_SYSCLK (1 << 10) /**< LPUART1 clock is SYSCLK. */
  224. #define STM32_LPUART1SEL_HSI16 (2 << 10) /**< LPUART1 clock is HSI16. */
  225. #define STM32_LPUART1SEL_LSE (3 << 10) /**< LPUART1 clock is LSE. */
  226. #define STM32_I2C1SEL_MASK (3 << 12) /**< I2C1 clock source mask. */
  227. #define STM32_I2C1SEL_APB (0 << 12) /**< I2C1 clock is APB. */
  228. #define STM32_I2C1SEL_SYSCLK (1 << 12) /**< I2C1 clock is SYSCLK. */
  229. #define STM32_I2C1SEL_HSI16 (2 << 12) /**< I2C1 clock is HSI16. */
  230. #define STM32_I2C3SEL_MASK (3 << 16) /**< I2C3 clock source mask. */
  231. #define STM32_I2C3SEL_APB (0 << 16) /**< I2C3 clock is APB. */
  232. #define STM32_I2C3SEL_SYSCLK (1 << 16) /**< I2C3 clock is SYSCLK. */
  233. #define STM32_I2C3SEL_HSI16 (2 << 16) /**< I2C3 clock is HSI16. */
  234. #define STM32_LPTIM1SEL_MASK (3 << 18) /**< LPTIM1 clock source mask. */
  235. #define STM32_LPTIM1SEL_APB (0 << 18) /**< LPTIM1 clock is APB. */
  236. #define STM32_LPTIM1SEL_LSI (1 << 18) /**< LPTIM1 clock is LSI. */
  237. #define STM32_LPTIM1SEL_HSI16 (2 << 18) /**< LPTIM1 clock is HSI16. */
  238. #define STM32_LPTIM1SEL_LSE (3 << 18) /**< LPTIM1 clock is LSE. */
  239. #define STM32_HSI48SEL_MASK (1 << 26) /**< HSI48SEL clock source mask.*/
  240. #define STM32_HSI48SEL_USBPLL (0 << 26) /**< USB48 clock is PLL/2. */
  241. #define STM32_HSI48SEL_HSI48 (1 << 26) /**< USB48 clock is HSI48. */
  242. /** @} */
  243. /**
  244. * @name SYSCFG_CFGR3_ register bits definitions
  245. * @{
  246. */
  247. #define STM32_VREFINT_EN (1 << 0) /**< VREFINT enable switch. */
  248. /** @} */
  249. /*===========================================================================*/
  250. /* Driver pre-compile time settings. */
  251. /*===========================================================================*/
  252. /**
  253. * @name Configuration options
  254. * @{
  255. */
  256. /**
  257. * @brief Disables the PWR/RCC initialization in the HAL.
  258. */
  259. #if !defined(STM32_NO_INIT) || defined(__DOXYGEN__)
  260. #define STM32_NO_INIT FALSE
  261. #endif
  262. /**
  263. * @brief Core voltage selection.
  264. * @note This setting affects all the performance and clock related
  265. * settings, the maximum performance is only obtainable selecting
  266. * the maximum voltage.
  267. */
  268. #if !defined(STM32_VOS) || defined(__DOXYGEN__)
  269. #define STM32_VOS STM32_VOS_1P8
  270. #endif
  271. /**
  272. * @brief Enables or disables the programmable voltage detector.
  273. */
  274. #if !defined(STM32_PVD_ENABLE) || defined(__DOXYGEN__)
  275. #define STM32_PVD_ENABLE FALSE
  276. #endif
  277. /**
  278. * @brief Sets voltage level for programmable voltage detector.
  279. */
  280. #if !defined(STM32_PLS) || defined(__DOXYGEN__)
  281. #define STM32_PLS STM32_PLS_LEV0
  282. #endif
  283. /**
  284. * @brief Enables or disables the HSI16 clock source.
  285. */
  286. #if !defined(STM32_HSI16_ENABLED) || defined(__DOXYGEN__)
  287. #define STM32_HSI16_ENABLED TRUE
  288. #endif
  289. /**
  290. * @brief Enables or disables the HSI16 clock divider.
  291. */
  292. #if !defined(STM32_HSI16_DIVIDER_ENABLED) || defined(__DOXYGEN__)
  293. #define STM32_HSI16_DIVIDER_ENABLED FALSE
  294. #endif
  295. /**
  296. * @brief Enables or disables the LSI clock source.
  297. */
  298. #if !defined(STM32_LSI_ENABLED) || defined(__DOXYGEN__)
  299. #define STM32_LSI_ENABLED TRUE
  300. #endif
  301. /**
  302. * @brief Enables or disables the HSE clock source.
  303. */
  304. #if !defined(STM32_HSE_ENABLED) || defined(__DOXYGEN__)
  305. #define STM32_HSE_ENABLED FALSE
  306. #endif
  307. /**
  308. * @brief Enables or disables the LSE clock source.
  309. */
  310. #if !defined(STM32_LSE_ENABLED) || defined(__DOXYGEN__)
  311. #define STM32_LSE_ENABLED FALSE
  312. #endif
  313. /**
  314. * @brief ADC clock setting.
  315. */
  316. #if !defined(STM32_ADC_CLOCK_ENABLED) || defined(__DOXYGEN__)
  317. #define STM32_ADC_CLOCK_ENABLED TRUE
  318. #endif
  319. /**
  320. * @brief USB clock setting.
  321. */
  322. #if !defined(STM32_USB_CLOCK_ENABLED) || defined(__DOXYGEN__)
  323. #define STM32_USB_CLOCK_ENABLED TRUE
  324. #endif
  325. /**
  326. * @brief MSI frequency setting.
  327. */
  328. #if !defined(STM32_MSIRANGE) || defined(__DOXYGEN__)
  329. #define STM32_MSIRANGE STM32_MSIRANGE_2M
  330. #endif
  331. /**
  332. * @brief Main clock source selection.
  333. * @note If the selected clock source is not the PLL then the PLL is not
  334. * initialized and started.
  335. * @note The default value is calculated for a 32MHz system clock from
  336. * the internal 16MHz HSI clock.
  337. */
  338. #if !defined(STM32_SW) || defined(__DOXYGEN__)
  339. #define STM32_SW STM32_SW_PLL
  340. #endif
  341. /**
  342. * @brief Clock source for the PLL.
  343. * @note This setting has only effect if the PLL is selected as the
  344. * system clock source.
  345. * @note The default value is calculated for a 32MHz system clock from
  346. * the internal 16MHz HSI clock.
  347. */
  348. #if !defined(STM32_PLLSRC) || defined(__DOXYGEN__)
  349. #define STM32_PLLSRC STM32_PLLSRC_HSI16
  350. #endif
  351. /**
  352. * @brief PLL multiplier value.
  353. * @note The allowed values are 3, 4, 6, 8, 12, 16, 32, 48.
  354. * @note The default value is calculated for a 32MHz system clock from
  355. * the internal 16MHz HSI clock.
  356. */
  357. #if !defined(STM32_PLLMUL_VALUE) || defined(__DOXYGEN__)
  358. #define STM32_PLLMUL_VALUE 4
  359. #endif
  360. /**
  361. * @brief PLL divider value.
  362. * @note The allowed values are 2, 3, 4.
  363. * @note The default value is calculated for a 32MHz system clock from
  364. * the internal 16MHz HSI clock.
  365. */
  366. #if !defined(STM32_PLLDIV_VALUE) || defined(__DOXYGEN__)
  367. #define STM32_PLLDIV_VALUE 2
  368. #endif
  369. /**
  370. * @brief AHB prescaler value.
  371. * @note The default value is calculated for a 32MHz system clock from
  372. * the internal 16MHz HSI clock.
  373. */
  374. #if !defined(STM32_HPRE) || defined(__DOXYGEN__)
  375. #define STM32_HPRE STM32_HPRE_DIV1
  376. #endif
  377. /**
  378. * @brief APB1 prescaler value.
  379. */
  380. #if !defined(STM32_PPRE1) || defined(__DOXYGEN__)
  381. #define STM32_PPRE1 STM32_PPRE1_DIV1
  382. #endif
  383. /**
  384. * @brief APB2 prescaler value.
  385. */
  386. #if !defined(STM32_PPRE2) || defined(__DOXYGEN__)
  387. #define STM32_PPRE2 STM32_PPRE2_DIV1
  388. #endif
  389. /**
  390. * @brief MCO clock source.
  391. */
  392. #if !defined(STM32_MCOSEL) || defined(__DOXYGEN__)
  393. #define STM32_MCOSEL STM32_MCOSEL_NOCLOCK
  394. #endif
  395. /**
  396. * @brief MCO divider setting.
  397. */
  398. #if !defined(STM32_MCOPRE) || defined(__DOXYGEN__)
  399. #define STM32_MCOPRE STM32_MCOPRE_DIV1
  400. #endif
  401. /**
  402. * @brief RTC/LCD clock source.
  403. */
  404. #if !defined(STM32_RTCSEL) || defined(__DOXYGEN__)
  405. #define STM32_RTCSEL STM32_RTCSEL_LSI
  406. #endif
  407. /**
  408. * @brief HSE divider toward RTC setting.
  409. */
  410. #if !defined(STM32_RTCPRE) || defined(__DOXYGEN__)
  411. #define STM32_RTCPRE STM32_RTCPRE_DIV2
  412. #endif
  413. /**
  414. * @brief USART1 clock source.
  415. */
  416. #if !defined(STM32_USART1SEL) || defined(__DOXYGEN__)
  417. #define STM32_USART1SEL STM32_USART1SEL_APB
  418. #endif
  419. /**
  420. * @brief USART2 clock source.
  421. */
  422. #if !defined(STM32_USART2SEL) || defined(__DOXYGEN__)
  423. #define STM32_USART2SEL STM32_USART2SEL_APB
  424. #endif
  425. /**
  426. * @brief LPUART1 clock source.
  427. */
  428. #if !defined(STM32_LPUART1SEL) || defined(__DOXYGEN__)
  429. #define STM32_LPUART1SEL STM32_LPUART1SEL_APB
  430. #endif
  431. /**
  432. * @brief I2C clock source.
  433. */
  434. #if !defined(STM32_I2C1SEL) || defined(__DOXYGEN__)
  435. #define STM32_I2C1SEL STM32_I2C1SEL_APB
  436. #endif
  437. /**
  438. * @brief LPTIM1 clock source.
  439. */
  440. #if !defined(STM32_LPTIM1SEL) || defined(__DOXYGEN__)
  441. #define STM32_LPTIM1SEL STM32_LPTIM1SEL_APB
  442. #endif
  443. /**
  444. * @bief USB/RNG clock source.
  445. */
  446. #if !defined(STM32_HSI48SEL) || defined(__DOXYGEN__)
  447. #define STM32_HSI48SEL STM32_HSI48SEL_USBPLL
  448. #endif
  449. /** @} */
  450. /*===========================================================================*/
  451. /* Derived constants and error checks. */
  452. /*===========================================================================*/
  453. /*
  454. * Configuration-related checks.
  455. */
  456. #if !defined(STM32L0xx_MCUCONF)
  457. #error "Using a wrong mcuconf.h file, STM32L0xx_MCUCONF not defined"
  458. #endif
  459. /*
  460. * Board files sanity checks.
  461. */
  462. #if !defined(STM32_LSECLK)
  463. #error "STM32_LSECLK not defined in board.h"
  464. #endif
  465. #if !defined(STM32_LSEDRV)
  466. #error "STM32_LSEDRV not defined in board.h"
  467. #endif
  468. #if !defined(STM32_HSECLK)
  469. #error "STM32_HSECLK not defined in board.h"
  470. #endif
  471. /* Voltage related limits.*/
  472. #if (STM32_VOS == STM32_VOS_1P8) || defined(__DOXYGEN__)
  473. /**
  474. * @name Absolute Maximum Ratings
  475. * @{
  476. */
  477. /**
  478. * @brief Maximum SYSCLK clock frequency at current voltage setting.
  479. */
  480. #define STM32_SYSCLK_MAX 32000000
  481. /**
  482. * @brief Maximum HSE clock frequency at current voltage setting.
  483. */
  484. #define STM32_HSECLK_MAX 32000000
  485. /**
  486. * @brief Minimum HSE clock frequency.
  487. */
  488. #define STM32_HSECLK_MIN 1000000
  489. /**
  490. * @brief Maximum LSE clock frequency.
  491. */
  492. #define STM32_LSECLK_MAX 1000000
  493. /**
  494. * @brief Minimum LSE clock frequency.
  495. */
  496. #define STM32_LSECLK_MIN 1000
  497. /**
  498. * @brief Maximum PLL input frequency.
  499. */
  500. #define STM32_PLLIN_MAX 24000000
  501. /**
  502. * @brief Maximum PLL input frequency.
  503. */
  504. #define STM32_PLLIN_MIN 2000000
  505. /**
  506. * @brief Maximum VCO clock frequency at current voltage setting.
  507. */
  508. #define STM32_PLLVCO_MAX 96000000
  509. /**
  510. * @brief Minimum VCO clock frequency at current voltage setting.
  511. */
  512. #define STM32_PLLVCO_MIN 6000000
  513. /**
  514. * @brief Maximum PLL output frequency.
  515. */
  516. #define STM32_PLLOUT_MAX 32000000
  517. /**
  518. * @brief Maximum PLL output frequency.
  519. */
  520. #define STM32_PLLOUT_MIN 2000000
  521. /**
  522. * @brief Maximum APB1 clock frequency.
  523. */
  524. #define STM32_PCLK1_MAX 32000000
  525. /**
  526. * @brief Maximum APB2 clock frequency.
  527. */
  528. #define STM32_PCLK2_MAX 32000000
  529. /**
  530. * @brief Maximum frequency not requiring a wait state for flash accesses.
  531. */
  532. #define STM32_0WS_THRESHOLD 16000000
  533. /**
  534. * @brief HSI availability at current voltage settings.
  535. */
  536. #define STM32_HSI_AVAILABLE TRUE
  537. /** @} */
  538. #elif STM32_VOS == STM32_VOS_1P5
  539. #define STM32_SYSCLK_MAX 16000000
  540. #define STM32_HSECLK_MAX 16000000
  541. #define STM32_HSECLK_MIN 1000000
  542. #define STM32_LSECLK_MAX 1000000
  543. #define STM32_LSECLK_MIN 1000
  544. #define STM32_PLLIN_MAX 16000000
  545. #define STM32_PLLIN_MIN 2000000
  546. #define STM32_PLLVCO_MAX 48000000
  547. #define STM32_PLLVCO_MIN 6000000
  548. #define STM32_PLLOUT_MAX 16000000
  549. #define STM32_PLLOUT_MIN 2000000
  550. #define STM32_PCLK1_MAX 16000000
  551. #define STM32_PCLK2_MAX 16000000
  552. #define STM32_0WS_THRESHOLD 8000000
  553. #define STM32_HSI_AVAILABLE TRUE
  554. #elif STM32_VOS == STM32_VOS_1P2
  555. #define STM32_SYSCLK_MAX 4000000
  556. #define STM32_HSECLK_MAX 8000000
  557. #define STM32_HSECLK_MIN 1000000
  558. #define STM32_LSECLK_MAX 1000000
  559. #define STM32_LSECLK_MIN 1000
  560. #define STM32_PLLIN_MAX 8000000
  561. #define STM32_PLLIN_MIN 2000000
  562. #define STM32_PLLVCO_MAX 24000000
  563. #define STM32_PLLVCO_MIN 6000000
  564. #define STM32_PLLOUT_MAX 4000000
  565. #define STM32_PLLOUT_MIN 2000000
  566. #define STM32_PCLK1_MAX 4000000
  567. #define STM32_PCLK2_MAX 4000000
  568. #define STM32_0WS_THRESHOLD 4000000
  569. #define STM32_HSI_AVAILABLE FALSE
  570. #else
  571. #error "invalid STM32_VOS value specified"
  572. #endif
  573. /* HSI related checks.*/
  574. #if STM32_HSI16_ENABLED
  575. #if !STM32_HSI_AVAILABLE
  576. #error "impossible to activate HSI under the current voltage settings"
  577. #endif
  578. #else /* !STM32_HSI16_ENABLED */
  579. #if STM32_ADC_CLOCK_ENABLED
  580. #error "HSI16 not enabled, required by STM32_ADC_CLOCK_ENABLED"
  581. #endif
  582. #if (STM32_SW == STM32_SW_HSI16)
  583. #error "HSI16 not enabled, required by STM32_SW"
  584. #endif
  585. #if ((STM32_SW == STM32_SW_PLL) && (STM32_PLLSRC == STM32_PLLSRC_HSI16))
  586. #error "HSI16 not enabled, required by STM32_PLLSRC"
  587. #endif
  588. #if (STM32_MCOSEL == STM32_MCOSEL_HSI16)
  589. #error "HSI16 not enabled, required by STM32_MCOSEL"
  590. #endif
  591. #if ((STM32_MCOSEL == STM32_MCOSEL_PLL) && (STM32_PLLSRC == STM32_PLLSRC_HSI16))
  592. #error "HSI16 not enabled, required by STM32_PLLSRC"
  593. #endif
  594. #endif /* !STM32_HSI16_ENABLED */
  595. /*
  596. * @brief Divided HSI16 clock.
  597. */
  598. #if STM32_HSI16_DIVIDER_ENABLED || defined(__DOXYGEN__)
  599. #define STM32_HSI16DIVCLK (STM32_HSI16CLK / 4)
  600. #else
  601. #define STM32_HSI16DIVCLK STM32_HSI16CLK
  602. #endif
  603. /* HSE related checks.*/
  604. #if STM32_HSE_ENABLED
  605. #if STM32_HSECLK == 0
  606. #error "impossible to activate HSE, frequency is zero"
  607. #endif
  608. #if (STM32_HSECLK < STM32_HSECLK_MIN) || (STM32_HSECLK > STM32_HSECLK_MAX)
  609. #error "STM32_HSECLK outside acceptable range (STM32_HSECLK_MIN...STM32_HSECLK_MAX)"
  610. #endif
  611. #else /* !STM32_HSE_ENABLED */
  612. #if (STM32_SW == STM32_SW_HSE)
  613. #error "HSE not enabled, required by STM32_SW"
  614. #endif
  615. #if ((STM32_SW == STM32_SW_PLL) && (STM32_PLLSRC == STM32_PLLSRC_HSE))
  616. #error "HSE not enabled, required by STM32_PLLSRC"
  617. #endif
  618. #if (STM32_MCOSEL == STM32_MCOSEL_HSE)
  619. #error "HSE not enabled, required by STM32_MCOSEL"
  620. #endif
  621. #if ((STM32_MCOSEL == STM32_MCOSEL_PLL) && (STM32_PLLSRC == STM32_PLLSRC_HSE))
  622. #error "HSE not enabled, required by STM32_PLLSRC"
  623. #endif
  624. #if (STM32_RTCSEL == STM32_RTCSEL_HSEDIV)
  625. #error "HSE not enabled, required by STM32_RTCSEL"
  626. #endif
  627. #endif /* !STM32_HSE_ENABLED */
  628. /* LSI related checks.*/
  629. #if STM32_LSI_ENABLED
  630. #else /* !STM32_LSI_ENABLED */
  631. #if STM32_MCOSEL == STM32_MCOSEL_LSI
  632. #error "LSI not enabled, required by STM32_MCOSEL"
  633. #endif
  634. #if STM32_RTCSEL == STM32_RTCSEL_LSI
  635. #error "LSI not enabled, required by STM32_RTCSEL"
  636. #endif
  637. #endif /* !STM32_LSI_ENABLED */
  638. /* LSE related checks.*/
  639. #if STM32_LSE_ENABLED
  640. #if (STM32_LSECLK == 0)
  641. #error "impossible to activate LSE, frequency is zero"
  642. #endif
  643. #if (STM32_LSECLK < STM32_LSECLK_MIN) || (STM32_LSECLK > STM32_LSECLK_MAX)
  644. #error "STM32_LSECLK outside acceptable range (STM32_LSECLK_MIN...STM32_LSECLK_MAX)"
  645. #endif
  646. #else /* !STM32_LSE_ENABLED */
  647. #if STM32_MCOSEL == STM32_MCOSEL_LSE
  648. #error "LSE not enabled, required by STM32_MCOSEL"
  649. #endif
  650. #if STM32_RTCSEL == STM32_RTCSEL_LSE
  651. #error "LSE not enabled, required by STM32_RTCSEL"
  652. #endif
  653. #endif /* !STM32_LSE_ENABLED */
  654. /* PLL related checks.*/
  655. #if (STM32_SW == STM32_SW_PLL) || (STM32_MCOSEL == STM32_MCOSEL_PLL) || \
  656. (STM32_USB_CLOCK_ENABLED && (STM32_HSI48SEL == STM32_HSI48SEL_USBPLL)) || \
  657. defined(__DOXYGEN__)
  658. /**
  659. * @brief PLL activation flag.
  660. */
  661. #define STM32_ACTIVATE_PLL TRUE
  662. #else
  663. #define STM32_ACTIVATE_PLL FALSE
  664. #endif
  665. /* HSI48 related checks.*/
  666. #if (STM32_USB_CLOCK_ENABLED && (STM32_HSI48SEL == STM32_HSI48SEL_HSI48)) || \
  667. defined(__DOXYGEN__)
  668. /**
  669. * @brief HSI48 activation flag.
  670. */
  671. #define STM32_ACTIVATE_HSI48 TRUE
  672. #else
  673. #define STM32_ACTIVATE_HSI48 FALSE
  674. #endif
  675. /**
  676. * @brief PLLMUL field.
  677. */
  678. #if (STM32_PLLMUL_VALUE == 3) || defined(__DOXYGEN__)
  679. #define STM32_PLLMUL STM32_PLLMUL_MUL3
  680. #elif STM32_PLLMUL_VALUE == 4
  681. #define STM32_PLLMUL STM32_PLLMUL_MUL4
  682. #elif STM32_PLLMUL_VALUE == 6
  683. #define STM32_PLLMUL STM32_PLLMUL_MUL6
  684. #elif STM32_PLLMUL_VALUE == 8
  685. #define STM32_PLLMUL STM32_PLLMUL_MUL8
  686. #elif STM32_PLLMUL_VALUE == 12
  687. #define STM32_PLLMUL STM32_PLLMUL_MUL12
  688. #elif STM32_PLLMUL_VALUE == 16
  689. #define STM32_PLLMUL STM32_PLLMUL_MUL16
  690. #elif STM32_PLLMUL_VALUE == 24
  691. #define STM32_PLLMUL STM32_PLLMUL_MUL24
  692. #elif STM32_PLLMUL_VALUE == 32
  693. #define STM32_PLLMUL STM32_PLLMUL_MUL32
  694. #elif STM32_PLLMUL_VALUE == 48
  695. #define STM32_PLLMUL STM32_PLLMUL_MUL48
  696. #else
  697. #error "invalid STM32_PLLMUL_VALUE value specified"
  698. #endif
  699. /**
  700. * @brief PLLDIV field.
  701. */
  702. #if (STM32_PLLDIV_VALUE == 2) || defined(__DOXYGEN__)
  703. #define STM32_PLLDIV STM32_PLLDIV_DIV2
  704. #elif STM32_PLLDIV_VALUE == 3
  705. #define STM32_PLLDIV STM32_PLLDIV_DIV3
  706. #elif STM32_PLLDIV_VALUE == 4
  707. #define STM32_PLLDIV STM32_PLLDIV_DIV4
  708. #else
  709. #error "invalid STM32_PLLDIV_VALUE value specified"
  710. #endif
  711. /**
  712. * @brief PLL input clock frequency.
  713. */
  714. #if (STM32_PLLSRC == STM32_PLLSRC_HSE) || defined(__DOXYGEN__)
  715. #define STM32_PLLCLKIN STM32_HSECLK
  716. #elif STM32_PLLSRC == STM32_PLLSRC_HSI16
  717. #define STM32_PLLCLKIN STM32_HSI16DIVCLK
  718. #else
  719. #error "invalid STM32_PLLSRC value specified"
  720. #endif
  721. /* PLL input frequency range check.*/
  722. #if (STM32_PLLCLKIN < STM32_PLLIN_MIN) || (STM32_PLLCLKIN > STM32_PLLIN_MAX)
  723. #error "STM32_PLLCLKIN outside acceptable range (STM32_PLLIN_MIN...STM32_PLLIN_MAX)"
  724. #endif
  725. /**
  726. * @brief PLL VCO frequency.
  727. */
  728. #define STM32_PLLVCO (STM32_PLLCLKIN * STM32_PLLMUL_VALUE)
  729. /* PLL output frequency range check.*/
  730. #if (STM32_PLLVCO < STM32_PLLVCO_MIN) || (STM32_PLLVCO > STM32_PLLVCO_MAX)
  731. #error "STM32_PLLVCO outside acceptable range (STM32_PLLVCO_MIN...STM32_PLLVCO_MAX)"
  732. #endif
  733. /**
  734. * @brief PLL output clock frequency.
  735. */
  736. #define STM32_PLLCLKOUT (STM32_PLLVCO / STM32_PLLDIV_VALUE)
  737. /* PLL output frequency range check.*/
  738. #if (STM32_PLLCLKOUT < STM32_PLLOUT_MIN) || (STM32_PLLCLKOUT > STM32_PLLOUT_MAX)
  739. #error "STM32_PLLCLKOUT outside acceptable range (STM32_PLLOUT_MIN...STM32_PLLOUT_MAX)"
  740. #endif
  741. /**
  742. * @brief MSI frequency.
  743. * @note Values are taken from the STM8Lxx datasheet.
  744. */
  745. #if STM32_MSIRANGE == STM32_MSIRANGE_64K
  746. #define STM32_MSICLK 65500
  747. #elif STM32_MSIRANGE == STM32_MSIRANGE_128K
  748. #define STM32_MSICLK 131000
  749. #elif STM32_MSIRANGE == STM32_MSIRANGE_256K
  750. #define STM32_MSICLK 262000
  751. #elif STM32_MSIRANGE == STM32_MSIRANGE_512K
  752. #define STM32_MSICLK 524000
  753. #elif STM32_MSIRANGE == STM32_MSIRANGE_1M
  754. #define STM32_MSICLK 1050000
  755. #elif STM32_MSIRANGE == STM32_MSIRANGE_2M
  756. #define STM32_MSICLK 2100000
  757. #elif STM32_MSIRANGE == STM32_MSIRANGE_4M
  758. #define STM32_MSICLK 4200000
  759. #else
  760. #error "invalid STM32_MSIRANGE value specified"
  761. #endif
  762. /**
  763. * @brief System clock source.
  764. */
  765. #if STM32_NO_INIT || defined(__DOXYGEN__)
  766. #define STM32_SYSCLK 2100000
  767. #elif (STM32_SW == STM32_SW_MSI)
  768. #define STM32_SYSCLK STM32_MSICLK
  769. #elif (STM32_SW == STM32_SW_HSI16)
  770. #define STM32_SYSCLK STM32_HSI16DIVCLK
  771. #elif (STM32_SW == STM32_SW_HSE)
  772. #define STM32_SYSCLK STM32_HSECLK
  773. #elif (STM32_SW == STM32_SW_PLL)
  774. #define STM32_SYSCLK STM32_PLLCLKOUT
  775. #else
  776. #error "invalid STM32_SW value specified"
  777. #endif
  778. /* Check on the system clock.*/
  779. #if STM32_SYSCLK > STM32_SYSCLK_MAX
  780. #error "STM32_SYSCLK above maximum rated frequency (STM32_SYSCLK_MAX)"
  781. #endif
  782. /**
  783. * @brief AHB frequency.
  784. */
  785. #if (STM32_HPRE == STM32_HPRE_DIV1) || defined(__DOXYGEN__)
  786. #define STM32_HCLK (STM32_SYSCLK / 1)
  787. #elif STM32_HPRE == STM32_HPRE_DIV2
  788. #define STM32_HCLK (STM32_SYSCLK / 2)
  789. #elif STM32_HPRE == STM32_HPRE_DIV4
  790. #define STM32_HCLK (STM32_SYSCLK / 4)
  791. #elif STM32_HPRE == STM32_HPRE_DIV8
  792. #define STM32_HCLK (STM32_SYSCLK / 8)
  793. #elif STM32_HPRE == STM32_HPRE_DIV16
  794. #define STM32_HCLK (STM32_SYSCLK / 16)
  795. #elif STM32_HPRE == STM32_HPRE_DIV64
  796. #define STM32_HCLK (STM32_SYSCLK / 64)
  797. #elif STM32_HPRE == STM32_HPRE_DIV128
  798. #define STM32_HCLK (STM32_SYSCLK / 128)
  799. #elif STM32_HPRE == STM32_HPRE_DIV256
  800. #define STM32_HCLK (STM32_SYSCLK / 256)
  801. #elif STM32_HPRE == STM32_HPRE_DIV512
  802. #define STM32_HCLK (STM32_SYSCLK / 512)
  803. #else
  804. #error "invalid STM32_HPRE value specified"
  805. #endif
  806. /* AHB frequency check.*/
  807. #if STM32_HCLK > STM32_SYSCLK_MAX
  808. #error "STM32_HCLK exceeding maximum frequency (STM32_SYSCLK_MAX)"
  809. #endif
  810. /**
  811. * @brief APB1 frequency.
  812. */
  813. #if (STM32_PPRE1 == STM32_PPRE1_DIV1) || defined(__DOXYGEN__)
  814. #define STM32_PCLK1 (STM32_HCLK / 1)
  815. #elif STM32_PPRE1 == STM32_PPRE1_DIV2
  816. #define STM32_PCLK1 (STM32_HCLK / 2)
  817. #elif STM32_PPRE1 == STM32_PPRE1_DIV4
  818. #define STM32_PCLK1 (STM32_HCLK / 4)
  819. #elif STM32_PPRE1 == STM32_PPRE1_DIV8
  820. #define STM32_PCLK1 (STM32_HCLK / 8)
  821. #elif STM32_PPRE1 == STM32_PPRE1_DIV16
  822. #define STM32_PCLK1 (STM32_HCLK / 16)
  823. #else
  824. #error "invalid STM32_PPRE1 value specified"
  825. #endif
  826. /* APB1 frequency check.*/
  827. #if STM32_PCLK1 > STM32_PCLK1_MAX
  828. #error "STM32_PCLK1 exceeding maximum frequency (STM32_PCLK1_MAX)"
  829. #endif
  830. /**
  831. * @brief APB2 frequency.
  832. */
  833. #if (STM32_PPRE2 == STM32_PPRE2_DIV1) || defined(__DOXYGEN__)
  834. #define STM32_PCLK2 (STM32_HCLK / 1)
  835. #elif STM32_PPRE2 == STM32_PPRE2_DIV2
  836. #define STM32_PCLK2 (STM32_HCLK / 2)
  837. #elif STM32_PPRE2 == STM32_PPRE2_DIV4
  838. #define STM32_PCLK2 (STM32_HCLK / 4)
  839. #elif STM32_PPRE2 == STM32_PPRE2_DIV8
  840. #define STM32_PCLK2 (STM32_HCLK / 8)
  841. #elif STM32_PPRE2 == STM32_PPRE2_DIV16
  842. #define STM32_PCLK2 (STM32_HCLK / 16)
  843. #else
  844. #error "invalid STM32_PPRE2 value specified"
  845. #endif
  846. /* APB2 frequency check.*/
  847. #if STM32_PCLK2 > STM32_PCLK2_MAX
  848. #error "STM32_PCLK2 exceeding maximum frequency (STM32_PCLK2_MAX)"
  849. #endif
  850. /**
  851. * @brief MCO selector clock.
  852. */
  853. #if (STM32_MCOSEL == STM32_MCOSEL_NOCLOCK) || defined(__DOXYGEN__)
  854. #define STM32_MCODIVCLK 0
  855. #elif STM32_MCOSEL == STM32_MCOSEL_SYSCLK
  856. #define STM32_MCODIVCLK STM32_SYSCLK
  857. #elif STM32_MCOSEL == STM32_MCOSEL_HSI16
  858. #define STM32_MCODIVCLK STM32_HSI16DIVCLK
  859. #elif STM32_MCOSEL == STM32_MCOSEL_MSI
  860. #define STM32_MCODIVCLK STM32_MSICLK
  861. #elif STM32_MCOSEL == STM32_MCOSEL_HSE
  862. #define STM32_MCODIVCLK STM32_HSECLK
  863. #elif STM32_MCOSEL == STM32_MCOSEL_PLL
  864. #define STM32_MCODIVCLK STM32_PLLCLKOUT
  865. #elif STM32_MCOSEL == STM32_MCOSEL_LSI
  866. #define STM32_MCODIVCLK STM32_LSICLK
  867. #elif STM32_MCOSEL == STM32_MCOSEL_LSE
  868. #define STM32_MCODIVCLK STM32_LSECLK
  869. #elif STM32_MCOSEL == STM32_MCOSEL_HSI48
  870. #define STM32_MCODIVCLK STM32_HSI48CLK
  871. #else
  872. #error "invalid STM32_MCOSEL value specified"
  873. #endif
  874. /**
  875. * @brief MCO output pin clock.
  876. */
  877. #if (STM32_MCOPRE == STM32_MCOPRE_DIV1) || defined(__DOXYGEN__)
  878. #define STM32_MCOCLK STM32_MCODIVCLK
  879. #elif STM32_MCOPRE == STM32_MCOPRE_DIV2
  880. #define STM32_MCOCLK (STM32_MCODIVCLK / 2)
  881. #elif STM32_MCOPRE == STM32_MCOPRE_DIV4
  882. #define STM32_MCOCLK (STM32_MCODIVCLK / 4)
  883. #elif STM32_MCOPRE == STM32_MCOPRE_DIV8
  884. #define STM32_MCOCLK (STM32_MCODIVCLK / 8)
  885. #elif STM32_MCOPRE == STM32_MCOPRE_DIV16
  886. #define STM32_MCOCLK (STM32_MCODIVCLK / 16)
  887. #else
  888. #error "invalid STM32_MCOPRE value specified"
  889. #endif
  890. /**
  891. * @brief HSE divider toward RTC clock.
  892. */
  893. #if (STM32_RTCPRE == STM32_RTCPRE_DIV2) || defined(__DOXYGEN__)
  894. #define STM32_HSEDIVCLK (STM32_HSECLK / 2)
  895. #elif (STM32_RTCPRE == STM32_RTCPRE_DIV4) || defined(__DOXYGEN__)
  896. #define STM32_HSEDIVCLK (STM32_HSECLK / 4)
  897. #elif (STM32_RTCPRE == STM32_RTCPRE_DIV8) || defined(__DOXYGEN__)
  898. #define STM32_HSEDIVCLK (STM32_HSECLK / 8)
  899. #elif (STM32_RTCPRE == STM32_RTCPRE_DIV16) || defined(__DOXYGEN__)
  900. #define STM32_HSEDIVCLK (STM32_HSECLK / 16)
  901. #else
  902. #error "invalid STM32_RTCPRE value specified"
  903. #endif
  904. /**
  905. * @brief RTC/LCD clock.
  906. */
  907. #if (STM32_RTCSEL == STM32_RTCSEL_NOCLOCK) || defined(__DOXYGEN__)
  908. #define STM32_RTCCLK 0
  909. #elif STM32_RTCSEL == STM32_RTCSEL_LSE
  910. #define STM32_RTCCLK STM32_LSECLK
  911. #elif STM32_RTCSEL == STM32_RTCSEL_LSI
  912. #define STM32_RTCCLK STM32_LSICLK
  913. #elif STM32_RTCSEL == STM32_RTCSEL_HSEDIV
  914. #define STM32_RTCCLK STM32_HSEDIVCLK
  915. #else
  916. #error "invalid STM32_RTCSEL value specified"
  917. #endif
  918. /**
  919. * @brief USART1 frequency.
  920. */
  921. #if (STM32_USART1SEL == STM32_USART1SEL_APB) || defined(__DOXYGEN__)
  922. #define STM32_USART1CLK STM32_PCLK2
  923. #elif STM32_USART1SEL == STM32_USART1SEL_SYSCLK
  924. #define STM32_USART1CLK STM32_SYSCLK
  925. #elif STM32_USART1SEL == STM32_USART1SEL_HSI16
  926. #define STM32_USART1CLK STM32_HSI16DIVCLK
  927. #elif STM32_USART1SEL == STM32_USART1SEL_LSE
  928. #define STM32_USART1CLK STM32_LSECLK
  929. #else
  930. #error "invalid source selected for USART1 clock"
  931. #endif
  932. /**
  933. * @brief USART2 frequency.
  934. */
  935. #if (STM32_USART2SEL == STM32_USART2SEL_APB) || defined(__DOXYGEN__)
  936. #define STM32_USART2CLK STM32_PCLK1
  937. #elif STM32_USART2SEL == STM32_USART2SEL_SYSCLK
  938. #define STM32_USART2CLK STM32_SYSCLK
  939. #elif STM32_USART2SEL == STM32_USART2SEL_HSI16
  940. #define STM32_USART2CLK STM32_HSI16DIVCLK
  941. #elif STM32_USART2SEL == STM32_USART2SEL_LSE
  942. #define STM32_USART2CLK STM32_LSECLK
  943. #else
  944. #error "invalid source selected for USART2 clock"
  945. #endif
  946. /**
  947. * @brief USART4 frequency.
  948. */
  949. #define STM32_UART4CLK STM32_PCLK1
  950. /**
  951. * @brief USART5 frequency.
  952. */
  953. #define STM32_UART5CLK STM32_PCLK1
  954. /**
  955. * @brief LPUART1 frequency.
  956. */
  957. #if (STM32_LPUART1SEL == STM32_LPUART1SEL_APB) || defined(__DOXYGEN__)
  958. #define STM32_LPUART1CLK STM32_PCLK1
  959. #elif STM32_LPUART1SEL == STM32_LPUART1SEL_SYSCLK
  960. #define STM32_LPUART1CLK STM32_SYSCLK
  961. #elif STM32_LPUART1SEL == STM32_LPUART1SEL_HSI16
  962. #define STM32_LPUART1CLK STM32_HSI16DIVCLK
  963. #elif STM32_LPUART1SEL == STM32_LPUART1SEL_LSE
  964. #define STM32_LPUART1CLK STM32_LSECLK
  965. #else
  966. #error "invalid source selected for LPUART1 clock"
  967. #endif
  968. /**
  969. * @brief I2C1 frequency.
  970. */
  971. #if (STM32_I2C1SEL == STM32_I2C1SEL_APB) || defined(__DOXYGEN__)
  972. #define STM32_I2C1CLK STM32_PCLK1
  973. #elif STM32_I2C1SEL == STM32_I2C1SEL_SYSCLK
  974. #define STM32_I2C1CLK STM32_SYSCLK
  975. #elif STM32_I2C1SEL == STM32_I2C1SEL_HSI16
  976. #define STM32_I2C1CLK STM32_HSI16DIVCLK
  977. #else
  978. #error "invalid source selected for I2C1 clock"
  979. #endif
  980. /**
  981. * @brief LPTIM1 frequency.
  982. */
  983. #if (STM32_LPTIM1SEL == STM32_LPTIM1SEL_APB) || defined(__DOXYGEN__)
  984. #define STM32_LPTIM1CLK STM32_PCLK1
  985. #elif STM32_LPTIM1SEL == STM32_LPTIM1SEL_LSI
  986. #define STM32_LPTIM1CLK STM32_LSICLK
  987. #elif STM32_LPTIM1SEL == STM32_LPTIM1SEL_HSI16
  988. #define STM32_LPTIM1CLK STM32_HSI16DIVCLK
  989. #elif STM32_LPTIM1SEL == STM32_LPTIM1SEL_LSE
  990. #define STM32_LPTIM1CLK STM32_LSECLK
  991. #else
  992. #error "invalid source selected for LPTIM1 clock"
  993. #endif
  994. /**
  995. * @brief USB/RNG frequency.
  996. */
  997. #if (STM32_HSI48SEL == STM32_HSI48SEL_HSI48) || defined(__DOXYGEN__)
  998. #define STM32_USBCLK STM32_HSI48CLK
  999. #elif STM32_HSI48SEL == STM32_HSI48SEL_USBPLL
  1000. #define STM32_USBCLK (STM32_PLLVCO / 2)
  1001. #else
  1002. #error "invalid STM32_HSI48SEL value specified"
  1003. #endif
  1004. /**
  1005. * @brief Timers LPTIM1, TIM2, TIM6 clock.
  1006. */
  1007. #if (STM32_PPRE1 == STM32_PPRE1_DIV1) || defined(__DOXYGEN__)
  1008. #define STM32_TIMCLK1 (STM32_PCLK1 * 1)
  1009. #else
  1010. #define STM32_TIMCLK1 (STM32_PCLK1 * 2)
  1011. #endif
  1012. /**
  1013. * @brief Timers TIM21, TIM22 clock.
  1014. */
  1015. #if (STM32_PPRE2 == STM32_PPRE2_DIV1) || defined(__DOXYGEN__)
  1016. #define STM32_TIMCLK2 (STM32_PCLK2 * 1)
  1017. #else
  1018. #define STM32_TIMCLK2 (STM32_PCLK2 * 2)
  1019. #endif
  1020. /**
  1021. * @brief Flash settings.
  1022. */
  1023. #if (STM32_HCLK <= STM32_0WS_THRESHOLD) || defined(__DOXYGEN__)
  1024. #define STM32_FLASHBITS 0
  1025. #else
  1026. #define STM32_FLASHBITS (FLASH_ACR_PRE_READ | \
  1027. FLASH_ACR_PRFTEN | \
  1028. FLASH_ACR_LATENCY)
  1029. #endif
  1030. /*===========================================================================*/
  1031. /* Driver data structures and types. */
  1032. /*===========================================================================*/
  1033. /*===========================================================================*/
  1034. /* Driver macros. */
  1035. /*===========================================================================*/
  1036. /*===========================================================================*/
  1037. /* External declarations. */
  1038. /*===========================================================================*/
  1039. /* Various helpers.*/
  1040. #include "nvic.h"
  1041. #include "cache.h"
  1042. #include "stm32_isr.h"
  1043. #include "stm32_dma.h"
  1044. #include "stm32_exti.h"
  1045. #include "stm32_rcc.h"
  1046. #ifdef __cplusplus
  1047. extern "C" {
  1048. #endif
  1049. void hal_lld_init(void);
  1050. void stm32_clock_init(void);
  1051. #ifdef __cplusplus
  1052. }
  1053. #endif
  1054. #endif /* HAL_LLD_H */
  1055. /** @} */