hal_lld.h 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281
  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 STM32L4xx/hal_lld.h
  15. * @brief STM32L4xx 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. * - STM32L432xx, STM32L433xx, STM32L443xx.
  26. * - STM32L471xx, STM32L475xx, STM32L476xx, STM32L496xx.
  27. * - STM32L485xx, STM32L486xx, STM32L4A6xx.
  28. * .
  29. *
  30. * @addtogroup STM32L4xx_ISR
  31. * @{
  32. */
  33. #ifndef HAL_LLD_H
  34. #define HAL_LLD_H
  35. #include "stm32_registry.h"
  36. /*===========================================================================*/
  37. /* Driver constants. */
  38. /*===========================================================================*/
  39. /**
  40. * @name Platform identification
  41. * @{
  42. */
  43. #if defined(STM32L432xx) || defined(STM32L433xx) || defined(STM32L443xx) || \
  44. defined(STM32L471xx) || defined(STM32L475xx) || \
  45. defined(STM32L476xx) || defined(STM32L496xx) || defined(__DOXYGEN__)
  46. #define PLATFORM_NAME "STM32L4xx Ultra Low Power"
  47. #elif defined(STM32L485xx) || defined(STM32L486xx) || defined(STM32L4A6xx)
  48. #define PLATFORM_NAME "STM32L4xx Ultra Low Power with Crypto"
  49. #else
  50. #error "STM32L4xx device not specified"
  51. #endif
  52. /**
  53. * @brief Sub-family identifier.
  54. */
  55. #if !defined(STM32L4XX) || defined(__DOXYGEN__)
  56. #define STM32L4XX
  57. #endif
  58. /** @} */
  59. /**
  60. * @name Internal clock sources
  61. * @{
  62. */
  63. #define STM32_HSI16CLK 16000000 /**< 16MHz internal clock. */
  64. #define STM32_HSI48CLK 48000000 /**< 48MHz internal clock. */
  65. #define STM32_LSICLK 32000 /**< Low speed internal clock. */
  66. /** @} */
  67. /**
  68. * @name PWR_CR1 register bits definitions
  69. * @{
  70. */
  71. #define STM32_VOS_MASK (3 << 9) /**< Core voltage mask. */
  72. #define STM32_VOS_RANGE1 (1 << 9) /**< Core voltage 1.2 Volts. */
  73. #define STM32_VOS_RANGE2 (2 << 9) /**< Core voltage 1.0 Volts. */
  74. /** @} */
  75. /**
  76. * @name PWR_CR2 register bits definitions
  77. * @{
  78. */
  79. #define STM32_PLS_MASK (7 << 1) /**< PLS bits mask. */
  80. #define STM32_PLS_LEV0 (0 << 1) /**< PVD level 0. */
  81. #define STM32_PLS_LEV1 (1 << 1) /**< PVD level 1. */
  82. #define STM32_PLS_LEV2 (2 << 1) /**< PVD level 2. */
  83. #define STM32_PLS_LEV3 (3 << 1) /**< PVD level 3. */
  84. #define STM32_PLS_LEV4 (4 << 1) /**< PVD level 4. */
  85. #define STM32_PLS_LEV5 (5 << 1) /**< PVD level 5. */
  86. #define STM32_PLS_LEV6 (6 << 1) /**< PVD level 6. */
  87. #define STM32_PLS_EXT (7 << 1) /**< PVD level 7. */
  88. /** @} */
  89. /**
  90. * @name RCC_CR register bits definitions
  91. * @{
  92. */
  93. #define STM32_MSIRANGE_MASK (15 << 4) /**< MSIRANGE field mask. */
  94. #define STM32_MSIRANGE_100K (0 << 4) /**< 100kHz nominal. */
  95. #define STM32_MSIRANGE_200K (1 << 4) /**< 200kHz nominal. */
  96. #define STM32_MSIRANGE_400K (2 << 4) /**< 400kHz nominal. */
  97. #define STM32_MSIRANGE_800K (3 << 4) /**< 800kHz nominal. */
  98. #define STM32_MSIRANGE_1M (4 << 4) /**< 1MHz nominal. */
  99. #define STM32_MSIRANGE_2M (5 << 4) /**< 2MHz nominal. */
  100. #define STM32_MSIRANGE_4M (6 << 4) /**< 4MHz nominal. */
  101. #define STM32_MSIRANGE_8M (7 << 4) /**< 8MHz nominal. */
  102. #define STM32_MSIRANGE_16M (8 << 4) /**< 16MHz nominal. */
  103. #define STM32_MSIRANGE_24M (9 << 4) /**< 24MHz nominal. */
  104. #define STM32_MSIRANGE_32M (10 << 4) /**< 32MHz nominal. */
  105. #define STM32_MSIRANGE_48M (11 << 4) /**< 48MHz nominal. */
  106. /** @} */
  107. /**
  108. * @name RCC_CFGR register bits definitions
  109. * @{
  110. */
  111. #define STM32_SW_MASK (3 << 0) /**< SW field mask. */
  112. #define STM32_SW_MSI (0 << 0) /**< SYSCLK source is MSI. */
  113. #define STM32_SW_HSI16 (1 << 0) /**< SYSCLK source is HSI. */
  114. #define STM32_SW_HSE (2 << 0) /**< SYSCLK source is HSE. */
  115. #define STM32_SW_PLL (3 << 0) /**< SYSCLK source is PLL. */
  116. #define STM32_HPRE_MASK (15 << 4) /**< HPRE field mask. */
  117. #define STM32_HPRE_DIV1 (0 << 4) /**< SYSCLK divided by 1. */
  118. #define STM32_HPRE_DIV2 (8 << 4) /**< SYSCLK divided by 2. */
  119. #define STM32_HPRE_DIV4 (9 << 4) /**< SYSCLK divided by 4. */
  120. #define STM32_HPRE_DIV8 (10 << 4) /**< SYSCLK divided by 8. */
  121. #define STM32_HPRE_DIV16 (11 << 4) /**< SYSCLK divided by 16. */
  122. #define STM32_HPRE_DIV64 (12 << 4) /**< SYSCLK divided by 64. */
  123. #define STM32_HPRE_DIV128 (13 << 4) /**< SYSCLK divided by 128. */
  124. #define STM32_HPRE_DIV256 (14 << 4) /**< SYSCLK divided by 256. */
  125. #define STM32_HPRE_DIV512 (15 << 4) /**< SYSCLK divided by 512. */
  126. #define STM32_PPRE1_MASK (7 << 8) /**< PPRE1 field mask. */
  127. #define STM32_PPRE1_DIV1 (0 << 8) /**< HCLK divided by 1. */
  128. #define STM32_PPRE1_DIV2 (4 << 8) /**< HCLK divided by 2. */
  129. #define STM32_PPRE1_DIV4 (5 << 8) /**< HCLK divided by 4. */
  130. #define STM32_PPRE1_DIV8 (6 << 8) /**< HCLK divided by 8. */
  131. #define STM32_PPRE1_DIV16 (7 << 8) /**< HCLK divided by 16. */
  132. #define STM32_PPRE2_MASK (7 << 11) /**< PPRE2 field mask. */
  133. #define STM32_PPRE2_DIV1 (0 << 11) /**< HCLK divided by 1. */
  134. #define STM32_PPRE2_DIV2 (4 << 11) /**< HCLK divided by 2. */
  135. #define STM32_PPRE2_DIV4 (5 << 11) /**< HCLK divided by 4. */
  136. #define STM32_PPRE2_DIV8 (6 << 11) /**< HCLK divided by 8. */
  137. #define STM32_PPRE2_DIV16 (7 << 11) /**< HCLK divided by 16. */
  138. #define STM32_STOPWUCK_MASK (1 << 15) /**< STOPWUCK field mask. */
  139. #define STM32_STOPWUCK_MSI (0 << 15) /**< Wakeup clock is MSI. */
  140. #define STM32_STOPWUCK_HSI16 (1 << 15) /**< Wakeup clock is HSI16. */
  141. #define STM32_MCOSEL_MASK (15 << 24) /**< MCOSEL field mask. */
  142. #define STM32_MCOSEL_NOCLOCK (0 << 24) /**< No clock on MCO pin. */
  143. #define STM32_MCOSEL_SYSCLK (1 << 24) /**< SYSCLK on MCO pin. */
  144. #define STM32_MCOSEL_MSI (2 << 24) /**< MSI clock on MCO pin. */
  145. #define STM32_MCOSEL_HSI16 (3 << 24) /**< HSI16 clock on MCO pin. */
  146. #define STM32_MCOSEL_HSE (4 << 24) /**< HSE clock on MCO pin. */
  147. #define STM32_MCOSEL_PLL (5 << 24) /**< PLL clock on MCO pin. */
  148. #define STM32_MCOSEL_LSI (6 << 24) /**< LSI clock on MCO pin. */
  149. #define STM32_MCOSEL_LSE (7 << 24) /**< LSE clock on MCO pin. */
  150. #define STM32_MCOSEL_HSI48 (8 << 24) /**< HSI48 clock on MCO pin. */
  151. #define STM32_MCOPRE_MASK (7 << 28) /**< MCOPRE field mask. */
  152. #define STM32_MCOPRE_DIV1 (0 << 28) /**< MCO divided by 1. */
  153. #define STM32_MCOPRE_DIV2 (1 << 28) /**< MCO divided by 2. */
  154. #define STM32_MCOPRE_DIV4 (2 << 28) /**< MCO divided by 4. */
  155. #define STM32_MCOPRE_DIV8 (3 << 28) /**< MCO divided by 8. */
  156. #define STM32_MCOPRE_DIV16 (4 << 28) /**< MCO divided by 16. */
  157. /** @} */
  158. /**
  159. * @name RCC_PLLCFGR register bits definitions
  160. * @{
  161. */
  162. #define STM32_PLLSRC_MASK (3 << 0) /**< PLL clock source mask. */
  163. #define STM32_PLLSRC_NOCLOCK (0 << 0) /**< PLL clock source disabled. */
  164. #define STM32_PLLSRC_MSI (1 << 0) /**< PLL clock source is MSI. */
  165. #define STM32_PLLSRC_HSI16 (2 << 0) /**< PLL clock source is HSI16. */
  166. #define STM32_PLLSRC_HSE (3 << 0) /**< PLL clock source is HSE. */
  167. /** @} */
  168. /**
  169. * @name RCC_CCIPR register bits definitions
  170. * @{
  171. */
  172. #define STM32_USART1SEL_MASK (3 << 0) /**< USART1SEL mask. */
  173. #define STM32_USART1SEL_PCLK2 (0 << 0) /**< USART1 source is PCLK2. */
  174. #define STM32_USART1SEL_SYSCLK (1 << 0) /**< USART1 source is SYSCLK. */
  175. #define STM32_USART1SEL_HSI16 (2 << 0) /**< USART1 source is HSI16. */
  176. #define STM32_USART1SEL_LSE (3 << 0) /**< USART1 source is LSE. */
  177. #define STM32_USART2SEL_MASK (3 << 2) /**< USART2 mask. */
  178. #define STM32_USART2SEL_PCLK1 (0 << 2) /**< USART2 source is PCLK1. */
  179. #define STM32_USART2SEL_SYSCLK (1 << 2) /**< USART2 source is SYSCLK. */
  180. #define STM32_USART2SEL_HSI16 (2 << 2) /**< USART2 source is HSI16. */
  181. #define STM32_USART2SEL_LSE (3 << 2) /**< USART2 source is LSE. */
  182. #define STM32_USART3SEL_MASK (3 << 4) /**< USART3 mask. */
  183. #define STM32_USART3SEL_PCLK1 (0 << 4) /**< USART3 source is PCLK1. */
  184. #define STM32_USART3SEL_SYSCLK (1 << 4) /**< USART3 source is SYSCLK. */
  185. #define STM32_USART3SEL_HSI16 (2 << 4) /**< USART3 source is HSI16. */
  186. #define STM32_USART3SEL_LSE (3 << 4) /**< USART3 source is LSE. */
  187. #define STM32_UART4SEL_MASK (3 << 6) /**< UART4 mask. */
  188. #define STM32_UART4SEL_PCLK1 (0 << 6) /**< UART4 source is PCLK1. */
  189. #define STM32_UART4SEL_SYSCLK (1 << 6) /**< UART4 source is SYSCLK. */
  190. #define STM32_UART4SEL_HSI16 (2 << 6) /**< UART4 source is HSI16. */
  191. #define STM32_UART4SEL_LSE (3 << 6) /**< UART4 source is LSE. */
  192. #define STM32_UART5SEL_MASK (3 << 8) /**< UART5 mask. */
  193. #define STM32_UART5SEL_PCLK1 (0 << 8) /**< UART5 source is PCLK1. */
  194. #define STM32_UART5SEL_SYSCLK (1 << 8) /**< UART5 source is SYSCLK. */
  195. #define STM32_UART5SEL_HSI16 (2 << 8) /**< UART5 source is HSI16. */
  196. #define STM32_UART5SEL_LSE (3 << 8) /**< UART5 source is LSE. */
  197. #define STM32_LPUART1SEL_MASK (3 << 10) /**< LPUART1 mask. */
  198. #define STM32_LPUART1SEL_PCLK1 (0 << 10) /**< LPUART1 source is PCLK1. */
  199. #define STM32_LPUART1SEL_SYSCLK (1 << 10) /**< LPUART1 source is SYSCLK. */
  200. #define STM32_LPUART1SEL_HSI16 (2 << 10) /**< LPUART1 source is HSI16. */
  201. #define STM32_LPUART1SEL_LSE (3 << 10) /**< LPUART1 source is LSE. */
  202. #define STM32_I2C1SEL_MASK (3 << 12) /**< I2C1SEL mask. */
  203. #define STM32_I2C1SEL_PCLK1 (0 << 12) /**< I2C1 source is PCLK1. */
  204. #define STM32_I2C1SEL_SYSCLK (1 << 12) /**< I2C1 source is SYSCLK. */
  205. #define STM32_I2C1SEL_HSI16 (2 << 12) /**< I2C1 source is HSI16. */
  206. #define STM32_I2C2SEL_MASK (3 << 14) /**< I2C2SEL mask. */
  207. #define STM32_I2C2SEL_PCLK1 (0 << 14) /**< I2C2 source is PCLK1. */
  208. #define STM32_I2C2SEL_SYSCLK (1 << 14) /**< I2C2 source is SYSCLK. */
  209. #define STM32_I2C2SEL_HSI16 (2 << 14) /**< I2C2 source is HSI16. */
  210. #define STM32_I2C3SEL_MASK (3 << 16) /**< I2C3SEL mask. */
  211. #define STM32_I2C3SEL_PCLK1 (0 << 16) /**< I2C3 source is PCLK1. */
  212. #define STM32_I2C3SEL_SYSCLK (1 << 16) /**< I2C3 source is SYSCLK. */
  213. #define STM32_I2C3SEL_HSI16 (2 << 16) /**< I2C3 source is HSI16. */
  214. #define STM32_LPTIM1SEL_MASK (3 << 18) /**< LPTIM1SEL mask. */
  215. #define STM32_LPTIM1SEL_PCLK1 (0 << 18) /**< LPTIM1 source is PCLK1. */
  216. #define STM32_LPTIM1SEL_LSI (1 << 18) /**< LPTIM1 source is LSI. */
  217. #define STM32_LPTIM1SEL_HSI16 (2 << 18) /**< LPTIM1 source is HSI16. */
  218. #define STM32_LPTIM1SEL_LSE (3 << 18) /**< LPTIM1 source is LSE. */
  219. #define STM32_LPTIM2SEL_MASK (3 << 20) /**< LPTIM2SEL mask. */
  220. #define STM32_LPTIM2SEL_PCLK1 (0 << 20) /**< LPTIM2 source is PCLK1. */
  221. #define STM32_LPTIM2SEL_LSI (1 << 20) /**< LPTIM2 source is LSI. */
  222. #define STM32_LPTIM2SEL_HSI16 (2 << 20) /**< LPTIM2 source is HSI16. */
  223. #define STM32_LPTIM2SEL_LSE (3 << 20) /**< LPTIM2 source is LSE. */
  224. #define STM32_SAI1SEL_MASK (3 << 22) /**< SAI1SEL mask. */
  225. #define STM32_SAI1SEL_PLLSAI1 (0 << 22) /**< SAI1 source is PLLSAI1-P. */
  226. #define STM32_SAI1SEL_PLLSAI2 (1 << 22) /**< SAI1 source is PLLSAI2-P. */
  227. #define STM32_SAI1SEL_PLL (2 << 22) /**< SAI1 source is PLL-P. */
  228. #define STM32_SAI1SEL_EXTCLK (3 << 22) /**< SAI1 source is external. */
  229. #define STM32_SAI1SEL_OFF 0xFFFFFFFFU /**< SAI1 clock is not required.*/
  230. #define STM32_SAI2SEL_MASK (3 << 24) /**< SAI2SEL mask. */
  231. #define STM32_SAI2SEL_PLLSAI1 (0 << 24) /**< SAI2 source is PLLSAI1-P. */
  232. #define STM32_SAI2SEL_PLLSAI2 (1 << 24) /**< SAI2 source is PLLSAI2-P. */
  233. #define STM32_SAI2SEL_PLL (2 << 24) /**< SAI2 source is PLL-P. */
  234. #define STM32_SAI2SEL_EXTCLK (3 << 24) /**< SAI2 source is external. */
  235. #define STM32_SAI2SEL_OFF 0xFFFFFFFFU /**< SAI2 clock is not required.*/
  236. #define STM32_CLK48SEL_MASK (3 << 26) /**< CLK48SEL mask. */
  237. #if !STM32_CLOCK_HAS_HSI48
  238. #define STM32_CLK48SEL_NOCLK (0 << 26) /**< CLK48 disabled. */
  239. #else
  240. #define STM32_CLK48SEL_HSI48 (0 << 26) /**< CLK48 source is HSI48. */
  241. #endif
  242. #define STM32_CLK48SEL_PLLSAI1 (1 << 26) /**< CLK48 source is PLLSAI1-Q. */
  243. #define STM32_CLK48SEL_PLL (2 << 26) /**< CLK48 source is PLL-Q. */
  244. #define STM32_CLK48SEL_MSI (3 << 26) /**< CLK48 source is MSI. */
  245. #define STM32_ADCSEL_MASK (3 << 28) /**< ADCSEL mask. */
  246. #define STM32_ADCSEL_NOCLK (0 << 28) /**< ADC clock disabled. */
  247. #define STM32_ADCSEL_PLLSAI1 (1 << 28) /**< ADC source is PLLSAI1-R. */
  248. #define STM32_ADCSEL_PLLSAI2 (2 << 28) /**< ADC source is PLLSAI2-R. */
  249. #define STM32_ADCSEL_SYSCLK (3 << 28) /**< ADC source is SYSCLK. */
  250. #define STM32_SWPMI1SEL_MASK (1 << 30) /**< SWPMI1SEL mask. */
  251. #define STM32_SWPMI1SEL_PCLK1 (0 << 30) /**< SWPMI1 source is PCLK1. */
  252. #define STM32_SWPMI1SEL_HSI16 (1 << 30) /**< SWPMI1 source is HSI16. */
  253. #define STM32_DFSDMSEL_MASK (1 << 31) /**< DFSDMSEL mask. */
  254. #define STM32_DFSDMSEL_PCLK2 (0 << 31) /**< DFSDM source is PCLK2. */
  255. #define STM32_DFSDMSEL_SYSCLK (1 << 31) /**< DFSDM source is SYSCLK. */
  256. /** @} */
  257. /**
  258. * @name RCC_BDCR register bits definitions
  259. * @{
  260. */
  261. #define STM32_RTCSEL_MASK (3 << 8) /**< RTC source mask. */
  262. #define STM32_RTCSEL_NOCLOCK (0 << 8) /**< No RTC source. */
  263. #define STM32_RTCSEL_LSE (1 << 8) /**< RTC source is LSE. */
  264. #define STM32_RTCSEL_LSI (2 << 8) /**< RTC source is LSI. */
  265. #define STM32_RTCSEL_HSEDIV (3 << 8) /**< RTC source is HSE divided. */
  266. #define STM32_LSCOSEL_MASK (3 << 24) /**< LSCO pin clock source. */
  267. #define STM32_LSCOSEL_NOCLOCK (0 << 24) /**< No clock on LSCO pin. */
  268. #define STM32_LSCOSEL_LSI (1 << 24) /**< LSI on LSCO pin. */
  269. #define STM32_LSCOSEL_LSE (3 << 24) /**< LSE on LSCO pin. */
  270. /** @} */
  271. /**
  272. * @name RCC_CSR register bits definitions
  273. * @{
  274. */
  275. #define STM32_MSISRANGE_MASK (15 << 8) /**< MSISRANGE field mask. */
  276. #define STM32_MSISRANGE_1M (4 << 8) /**< 1MHz nominal. */
  277. #define STM32_MSISRANGE_2M (5 << 8) /**< 2MHz nominal. */
  278. #define STM32_MSISRANGE_4M (6 << 8) /**< 4MHz nominal. */
  279. #define STM32_MSISRANGE_8M (7 << 8) /**< 8MHz nominal. */
  280. /** @} */
  281. /*===========================================================================*/
  282. /* Driver pre-compile time settings. */
  283. /*===========================================================================*/
  284. /**
  285. * @name Configuration options
  286. * @{
  287. */
  288. /**
  289. * @brief Disables the PWR/RCC initialization in the HAL.
  290. */
  291. #if !defined(STM32_NO_INIT) || defined(__DOXYGEN__)
  292. #define STM32_NO_INIT FALSE
  293. #endif
  294. /**
  295. * @brief Core voltage selection.
  296. * @note This setting affects all the performance and clock related
  297. * settings, the maximum performance is only obtainable selecting
  298. * the maximum voltage.
  299. */
  300. #if !defined(STM32_VOS) || defined(__DOXYGEN__)
  301. #define STM32_VOS STM32_VOS_RANGE1
  302. #endif
  303. /**
  304. * @brief Enables or disables the programmable voltage detector.
  305. */
  306. #if !defined(STM32_PVD_ENABLE) || defined(__DOXYGEN__)
  307. #define STM32_PVD_ENABLE FALSE
  308. #endif
  309. /**
  310. * @brief Sets voltage level for programmable voltage detector.
  311. */
  312. #if !defined(STM32_PLS) || defined(__DOXYGEN__)
  313. #define STM32_PLS STM32_PLS_LEV0
  314. #endif
  315. /**
  316. * @brief Enables or disables the HSI16 clock source.
  317. */
  318. #if !defined(STM32_HSI16_ENABLED) || defined(__DOXYGEN__)
  319. #define STM32_HSI16_ENABLED FALSE
  320. #endif
  321. /**
  322. * @brief Enables or disables the HSI48 clock source.
  323. */
  324. #if !defined(STM32_HSI48_ENABLED) || defined(__DOXYGEN__)
  325. #define STM32_HSI48_ENABLED FALSE
  326. #endif
  327. /**
  328. * @brief Enables or disables the LSI clock source.
  329. */
  330. #if !defined(STM32_LSI_ENABLED) || defined(__DOXYGEN__)
  331. #define STM32_LSI_ENABLED TRUE
  332. #endif
  333. /**
  334. * @brief Enables or disables the HSE clock source.
  335. */
  336. #if !defined(STM32_HSE_ENABLED) || defined(__DOXYGEN__)
  337. #define STM32_HSE_ENABLED FALSE
  338. #endif
  339. /**
  340. * @brief Enables or disables the LSE clock source.
  341. */
  342. #if !defined(STM32_LSE_ENABLED) || defined(__DOXYGEN__)
  343. #define STM32_LSE_ENABLED FALSE
  344. #endif
  345. /**
  346. * @brief Enables or disables the MSI PLL on LSE clock source.
  347. */
  348. #if !defined(STM32_MSIPLL_ENABLED) || defined(__DOXYGEN__)
  349. #define STM32_MSIPLL_ENABLED FALSE
  350. #endif
  351. /**
  352. * @brief MSI frequency setting.
  353. */
  354. #if !defined(STM32_MSIRANGE) || defined(__DOXYGEN__)
  355. #define STM32_MSIRANGE STM32_MSIRANGE_4M
  356. #endif
  357. /**
  358. * @brief MSI frequency setting after standby.
  359. */
  360. #if !defined(STM32_MSISRANGE) || defined(__DOXYGEN__)
  361. #define STM32_MSISRANGE STM32_MSISRANGE_4M
  362. #endif
  363. /**
  364. * @brief Main clock source selection.
  365. * @note If the selected clock source is not the PLL then the PLL is not
  366. * initialized and started.
  367. * @note The default value is calculated for a 80MHz system clock from
  368. * the internal 4MHz MSI clock.
  369. */
  370. #if !defined(STM32_SW) || defined(__DOXYGEN__)
  371. #define STM32_SW STM32_SW_PLL
  372. #endif
  373. /**
  374. * @brief Clock source for the PLL.
  375. * @note This setting has only effect if the PLL is selected as the
  376. * system clock source.
  377. * @note The default value is calculated for a 80MHz system clock from
  378. * the internal 4MHz MSI clock.
  379. */
  380. #if !defined(STM32_PLLSRC) || defined(__DOXYGEN__)
  381. #define STM32_PLLSRC STM32_PLLSRC_MSI
  382. #endif
  383. /**
  384. * @brief PLLM divider value.
  385. * @note The allowed values are 1..8.
  386. * @note The default value is calculated for a 80MHz system clock from
  387. * the internal 4MHz MSI clock.
  388. */
  389. #if !defined(STM32_PLLM_VALUE) || defined(__DOXYGEN__)
  390. #define STM32_PLLM_VALUE 1
  391. #endif
  392. /**
  393. * @brief PLLN multiplier value.
  394. * @note The allowed values are 8..86.
  395. * @note The default value is calculated for a 80MHz system clock from
  396. * the internal 4MHz MSI clock.
  397. */
  398. #if !defined(STM32_PLLN_VALUE) || defined(__DOXYGEN__)
  399. #define STM32_PLLN_VALUE 80
  400. #endif
  401. /**
  402. * @brief PLLPDIV divider value or zero if disabled.
  403. * @note The allowed values are 0, 2..31.
  404. */
  405. #if !defined(STM32_PLLPDIV_VALUE) || defined(__DOXYGEN__)
  406. #define STM32_PLLPDIV_VALUE 0
  407. #endif
  408. /**
  409. * @brief PLLP divider value.
  410. * @note The allowed values are 7, 17.
  411. * @note The default value is calculated for a 80MHz system clock from
  412. * the internal 4MHz MSI clock.
  413. */
  414. #if !defined(STM32_PLLP_VALUE) || defined(__DOXYGEN__)
  415. #define STM32_PLLP_VALUE 7
  416. #endif
  417. /**
  418. * @brief PLLQ divider value.
  419. * @note The allowed values are 2, 4, 6, 8.
  420. * @note The default value is calculated for a 80MHz system clock from
  421. * the internal 4MHz MSI clock.
  422. */
  423. #if !defined(STM32_PLLQ_VALUE) || defined(__DOXYGEN__)
  424. #define STM32_PLLQ_VALUE 6
  425. #endif
  426. /**
  427. * @brief PLLR divider value.
  428. * @note The allowed values are 2, 4, 6, 8.
  429. * @note The default value is calculated for a 80MHz system clock from
  430. * the internal 4MHz MSI clock.
  431. */
  432. #if !defined(STM32_PLLR_VALUE) || defined(__DOXYGEN__)
  433. #define STM32_PLLR_VALUE 4
  434. #endif
  435. /**
  436. * @brief AHB prescaler value.
  437. * @note The default value is calculated for a 80MHz system clock from
  438. * the internal 4MHz MSI clock.
  439. */
  440. #if !defined(STM32_HPRE) || defined(__DOXYGEN__)
  441. #define STM32_HPRE STM32_HPRE_DIV1
  442. #endif
  443. /**
  444. * @brief APB1 prescaler value.
  445. */
  446. #if !defined(STM32_PPRE1) || defined(__DOXYGEN__)
  447. #define STM32_PPRE1 STM32_PPRE1_DIV1
  448. #endif
  449. /**
  450. * @brief APB2 prescaler value.
  451. */
  452. #if !defined(STM32_PPRE2) || defined(__DOXYGEN__)
  453. #define STM32_PPRE2 STM32_PPRE2_DIV1
  454. #endif
  455. /**
  456. * @brief STOPWUCK clock setting.
  457. */
  458. #if !defined(STM32_STOPWUCK) || defined(__DOXYGEN__)
  459. #define STM32_STOPWUCK STM32_STOPWUCK_MSI
  460. #endif
  461. /**
  462. * @brief MCO clock source.
  463. */
  464. #if !defined(STM32_MCOSEL) || defined(__DOXYGEN__)
  465. #define STM32_MCOSEL STM32_MCOSEL_NOCLOCK
  466. #endif
  467. /**
  468. * @brief MCO divider setting.
  469. */
  470. #if !defined(STM32_MCOPRE) || defined(__DOXYGEN__)
  471. #define STM32_MCOPRE STM32_MCOPRE_DIV1
  472. #endif
  473. /**
  474. * @brief LSCO clock source.
  475. */
  476. #if !defined(STM32_LSCOSEL) || defined(__DOXYGEN__)
  477. #define STM32_LSCOSEL STM32_LSCOSEL_NOCLOCK
  478. #endif
  479. /**
  480. * @brief PLLSAI1N multiplier value.
  481. * @note The allowed values are 8..86.
  482. */
  483. #if !defined(STM32_PLLSAI1N_VALUE) || defined(__DOXYGEN__)
  484. #define STM32_PLLSAI1N_VALUE 80
  485. #endif
  486. /**
  487. * @brief PLLSAI1PDIV divider value or zero if disabled.
  488. * @note The allowed values are 0, 2..31.
  489. */
  490. #if !defined(STM32_PLLSAI1PDIV_VALUE) || defined(__DOXYGEN__)
  491. #define STM32_PLLSAI1PDIV_VALUE 0
  492. #endif
  493. /**
  494. * @brief PLLSAI1P divider value.
  495. * @note The allowed values are 7, 17.
  496. */
  497. #if !defined(STM32_PLLSAI1P_VALUE) || defined(__DOXYGEN__)
  498. #define STM32_PLLSAI1P_VALUE 7
  499. #endif
  500. /**
  501. * @brief PLLSAI1Q divider value.
  502. * @note The allowed values are 2, 4, 6, 8.
  503. */
  504. #if !defined(STM32_PLLSAI1Q_VALUE) || defined(__DOXYGEN__)
  505. #define STM32_PLLSAI1Q_VALUE 6
  506. #endif
  507. /**
  508. * @brief PLLSAI1R divider value.
  509. * @note The allowed values are 2, 4, 6, 8.
  510. */
  511. #if !defined(STM32_PLLSAI1R_VALUE) || defined(__DOXYGEN__)
  512. #define STM32_PLLSAI1R_VALUE 4
  513. #endif
  514. /**
  515. * @brief PLLSAI2N multiplier value.
  516. * @note The allowed values are 8..86.
  517. */
  518. #if !defined(STM32_PLLSAI2N_VALUE) || defined(__DOXYGEN__)
  519. #define STM32_PLLSAI2N_VALUE 80
  520. #endif
  521. /**
  522. * @brief PLLSAI2PDIV divider value or zero if disabled.
  523. * @note The allowed values are 0, 2..31.
  524. */
  525. #if !defined(STM32_PLLSAI2PDIV_VALUE) || defined(__DOXYGEN__)
  526. #define STM32_PLLSAI2PDIV_VALUE 0
  527. #endif
  528. /**
  529. * @brief PLLSAI2P divider value.
  530. * @note The allowed values are 7, 17.
  531. */
  532. #if !defined(STM32_PLLSAI2P_VALUE) || defined(__DOXYGEN__)
  533. #define STM32_PLLSAI2P_VALUE 7
  534. #endif
  535. /**
  536. * @brief PLLSAI2R divider value.
  537. * @note The allowed values are 2, 4, 6, 8.
  538. */
  539. #if !defined(STM32_PLLSAI2R_VALUE) || defined(__DOXYGEN__)
  540. #define STM32_PLLSAI2R_VALUE 4
  541. #endif
  542. /**
  543. * @brief USART1 clock source.
  544. */
  545. #if !defined(STM32_USART1SEL) || defined(__DOXYGEN__)
  546. #define STM32_USART1SEL STM32_USART1SEL_SYSCLK
  547. #endif
  548. /**
  549. * @brief USART2 clock source.
  550. */
  551. #if !defined(STM32_USART2SEL) || defined(__DOXYGEN__)
  552. #define STM32_USART2SEL STM32_USART2SEL_SYSCLK
  553. #endif
  554. /**
  555. * @brief USART3 clock source.
  556. */
  557. #if !defined(STM32_USART3SEL) || defined(__DOXYGEN__)
  558. #define STM32_USART3SEL STM32_USART3SEL_SYSCLK
  559. #endif
  560. /**
  561. * @brief UART4 clock source.
  562. */
  563. #if !defined(STM32_UART4SEL) || defined(__DOXYGEN__)
  564. #define STM32_UART4SEL STM32_UART4SEL_SYSCLK
  565. #endif
  566. /**
  567. * @brief UART5 clock source.
  568. */
  569. #if !defined(STM32_UART5SEL) || defined(__DOXYGEN__)
  570. #define STM32_UART5SEL STM32_UART5SEL_SYSCLK
  571. #endif
  572. /**
  573. * @brief LPUART1 clock source.
  574. */
  575. #if !defined(STM32_LPUART1SEL) || defined(__DOXYGEN__)
  576. #define STM32_LPUART1SEL STM32_LPUART1SEL_SYSCLK
  577. #endif
  578. /**
  579. * @brief I2C1 clock source.
  580. */
  581. #if !defined(STM32_I2C1SEL) || defined(__DOXYGEN__)
  582. #define STM32_I2C1SEL STM32_I2C1SEL_SYSCLK
  583. #endif
  584. /**
  585. * @brief I2C2 clock source.
  586. */
  587. #if !defined(STM32_I2C2SEL) || defined(__DOXYGEN__)
  588. #define STM32_I2C2SEL STM32_I2C2SEL_SYSCLK
  589. #endif
  590. /**
  591. * @brief I2C3 clock source.
  592. */
  593. #if !defined(STM32_I2C3SEL) || defined(__DOXYGEN__)
  594. #define STM32_I2C3SEL STM32_I2C3SEL_SYSCLK
  595. #endif
  596. /**
  597. * @brief LPTIM1 clock source.
  598. */
  599. #if !defined(STM32_LPTIM1SEL) || defined(__DOXYGEN__)
  600. #define STM32_LPTIM1SEL STM32_LPTIM1SEL_PCLK1
  601. #endif
  602. /**
  603. * @brief LPTIM2 clock source.
  604. */
  605. #if !defined(STM32_LPTIM2SEL) || defined(__DOXYGEN__)
  606. #define STM32_LPTIM2SEL STM32_LPTIM2SEL_PCLK1
  607. #endif
  608. /**
  609. * @brief SAI1SEL value (SAI1 clock source).
  610. */
  611. #if !defined(STM32_SAI1SEL) || defined(__DOXYGEN__)
  612. #define STM32_SAI1SEL STM32_SAI1SEL_OFF
  613. #endif
  614. /**
  615. * @brief SAI2SEL value (SAI2 clock source).
  616. */
  617. #if !defined(STM32_SAI2SEL) || defined(__DOXYGEN__)
  618. #define STM32_SAI2SEL STM32_SAI2SEL_OFF
  619. #endif
  620. /**
  621. * @brief CLK48SEL value (48MHz clock source).
  622. */
  623. #if !defined(STM32_CLK48SEL) || defined(__DOXYGEN__)
  624. #define STM32_CLK48SEL STM32_CLK48SEL_PLL
  625. #endif
  626. /**
  627. * @brief ADCSEL value (ADCs clock source).
  628. */
  629. #if !defined(STM32_ADCSEL) || defined(__DOXYGEN__)
  630. #define STM32_ADCSEL STM32_ADCSEL_SYSCLK
  631. #endif
  632. /**
  633. * @brief SWPMI1SEL value (SWPMI clock source).
  634. */
  635. #if !defined(STM32_SWPMI1SEL) || defined(__DOXYGEN__)
  636. #define STM32_SWPMI1SEL STM32_SWPMI1SEL_PCLK1
  637. #endif
  638. /**
  639. * @brief DFSDMSEL value (DFSDM clock source).
  640. */
  641. #if !defined(STM32_DFSDMSEL) || defined(__DOXYGEN__)
  642. #define STM32_DFSDMSEL STM32_DFSDMSEL_PCLK2
  643. #endif
  644. /**
  645. * @brief RTC/LCD clock source.
  646. */
  647. #if !defined(STM32_RTCSEL) || defined(__DOXYGEN__)
  648. #define STM32_RTCSEL STM32_RTCSEL_LSI
  649. #endif
  650. /** @} */
  651. /*===========================================================================*/
  652. /* Derived constants and error checks. */
  653. /*===========================================================================*/
  654. /*
  655. * Configuration-related checks.
  656. */
  657. #if !defined(STM32L4xx_MCUCONF)
  658. #error "Using a wrong mcuconf.h file, STM32L4xx_MCUCONF not defined"
  659. #endif
  660. /* Only some devices have strongly checked mcuconf.h files. Others will be
  661. added gradually.*/
  662. #if defined(STM32L432xx) && !defined(STM32L432_MCUCONF)
  663. #error "Using a wrong mcuconf.h file, STM32L432_MCUCONF not defined"
  664. #endif
  665. #if defined(STM32L433xx) && !defined(STM32L433_MCUCONF)
  666. #error "Using a wrong mcuconf.h file, STM32L433_MCUCONF not defined"
  667. #endif
  668. #if defined(STM32L476xx) && !defined(STM32L476_MCUCONF)
  669. #error "Using a wrong mcuconf.h file, STM32L476_MCUCONF not defined"
  670. #endif
  671. #if defined(STM32L486xx) && !defined(STM32L486_MCUCONF)
  672. #error "Using a wrong mcuconf.h file, STM32L486_MCUCONF not defined"
  673. #endif
  674. #if defined(STM32L496xx) && !defined(STM32L496_MCUCONF)
  675. #error "Using a wrong mcuconf.h file, STM32L496_MCUCONF not defined"
  676. #endif
  677. #if defined(STM32L4A6xx) && !defined(STM32L4A6_MCUCONF)
  678. #error "Using a wrong mcuconf.h file, STM32L4A6_MCUCONF not defined"
  679. #endif
  680. /*
  681. * Board files sanity checks.
  682. */
  683. #if !defined(STM32_LSECLK)
  684. #error "STM32_LSECLK not defined in board.h"
  685. #endif
  686. #if !defined(STM32_LSEDRV)
  687. #error "STM32_LSEDRV not defined in board.h"
  688. #endif
  689. #if !defined(STM32_HSECLK)
  690. #error "STM32_HSECLK not defined in board.h"
  691. #endif
  692. /* Voltage related limits.*/
  693. #if (STM32_VOS == STM32_VOS_RANGE1) || defined(__DOXYGEN__)
  694. /**
  695. * @name System Limits
  696. * @{
  697. */
  698. /**
  699. * @brief Maximum SYSCLK clock frequency at current voltage setting.
  700. */
  701. #define STM32_SYSCLK_MAX 80000000
  702. /**
  703. * @brief Maximum HSE clock frequency at current voltage setting.
  704. */
  705. #define STM32_HSECLK_MAX 48000000
  706. /**
  707. * @brief Maximum HSE clock frequency using an external source.
  708. */
  709. #define STM32_HSECLK_BYP_MAX 48000000
  710. /**
  711. * @brief Minimum HSE clock frequency.
  712. */
  713. #define STM32_HSECLK_MIN 4000000
  714. /**
  715. * @brief Minimum HSE clock frequency using an external source.
  716. */
  717. #define STM32_HSECLK_BYP_MIN 8000000
  718. /**
  719. * @brief Maximum LSE clock frequency.
  720. */
  721. #define STM32_LSECLK_MAX 32768
  722. /**
  723. * @brief Maximum LSE clock frequency.
  724. */
  725. #define STM32_LSECLK_BYP_MAX 1000000
  726. /**
  727. * @brief Minimum LSE clock frequency.
  728. */
  729. #define STM32_LSECLK_MIN 32768
  730. /**
  731. * @brief Minimum LSE clock frequency.
  732. */
  733. #define STM32_LSECLK_BYP_MIN 32768
  734. /**
  735. * @brief Maximum PLLs input clock frequency.
  736. */
  737. #define STM32_PLLIN_MAX 16000000
  738. /**
  739. * @brief Minimum PLLs input clock frequency.
  740. */
  741. #define STM32_PLLIN_MIN 4000000
  742. /**
  743. * @brief Maximum VCO clock frequency at current voltage setting.
  744. */
  745. #define STM32_PLLVCO_MAX 344000000
  746. /**
  747. * @brief Minimum VCO clock frequency at current voltage setting.
  748. */
  749. #define STM32_PLLVCO_MIN 64000000
  750. /**
  751. * @brief Maximum PLL-P output clock frequency.
  752. */
  753. #define STM32_PLLP_MAX 80000000
  754. /**
  755. * @brief Minimum PLL-P output clock frequency.
  756. */
  757. #define STM32_PLLP_MIN 2064500
  758. /**
  759. * @brief Maximum PLL-Q output clock frequency.
  760. */
  761. #define STM32_PLLQ_MAX 80000000
  762. /**
  763. * @brief Minimum PLL-Q output clock frequency.
  764. */
  765. #define STM32_PLLQ_MIN 8000000
  766. /**
  767. * @brief Maximum PLL-R output clock frequency.
  768. */
  769. #define STM32_PLLR_MAX 80000000
  770. /**
  771. * @brief Minimum PLL-R output clock frequency.
  772. */
  773. #define STM32_PLLR_MIN 8000000
  774. /**
  775. * @brief Maximum APB1 clock frequency.
  776. */
  777. #define STM32_PCLK1_MAX 80000000
  778. /**
  779. * @brief Maximum APB2 clock frequency.
  780. */
  781. #define STM32_PCLK2_MAX 80000000
  782. /**
  783. * @brief Maximum ADC clock frequency.
  784. */
  785. #define STM32_ADCCLK_MAX 80000000
  786. /** @} */
  787. /**
  788. * @name Flash Wait states
  789. * @{
  790. */
  791. #define STM32_0WS_THRESHOLD 16000000
  792. #define STM32_1WS_THRESHOLD 32000000
  793. #define STM32_2WS_THRESHOLD 48000000
  794. #define STM32_3WS_THRESHOLD 64000000
  795. /** @} */
  796. #elif STM32_VOS == STM32_VOS_RANGE2
  797. #define STM32_SYSCLK_MAX 26000000
  798. #define STM32_HSECLK_MAX 26000000
  799. #define STM32_HSECLK_BYP_MAX 26000000
  800. #define STM32_HSECLK_MIN 8000000
  801. #define STM32_HSECLK_BYP_MIN 8000000
  802. #define STM32_LSECLK_MAX 32768
  803. #define STM32_LSECLK_BYP_MAX 1000000
  804. #define STM32_LSECLK_MIN 32768
  805. #define STM32_LSECLK_BYP_MIN 32768
  806. #define STM32_PLLIN_MAX 16000000
  807. #define STM32_PLLIN_MIN 4000000
  808. #define STM32_PLLVCO_MAX 128000000
  809. #define STM32_PLLVCO_MIN 64000000
  810. #define STM32_PLLP_MAX 26000000
  811. #define STM32_PLLP_MIN 2064500
  812. #define STM32_PLLQ_MAX 26000000
  813. #define STM32_PLLQ_MIN 8000000
  814. #define STM32_PLLR_MAX 26000000
  815. #define STM32_PLLR_MIN 8000000
  816. #define STM32_PCLK1_MAX 26000000
  817. #define STM32_PCLK2_MAX 26000000
  818. #define STM32_ADCCLK_MAX 26000000
  819. #define STM32_0WS_THRESHOLD 6000000
  820. #define STM32_1WS_THRESHOLD 12000000
  821. #define STM32_2WS_THRESHOLD 18000000
  822. #define STM32_3WS_THRESHOLD 26000000
  823. #else
  824. #error "invalid STM32_VOS value specified"
  825. #endif
  826. /**
  827. * @brief MSI frequency.
  828. */
  829. #if STM32_MSIRANGE == STM32_MSIRANGE_100K
  830. #define STM32_MSICLK 100000
  831. #elif STM32_MSIRANGE == STM32_MSIRANGE_200K
  832. #define STM32_MSICLK 200000
  833. #elif STM32_MSIRANGE == STM32_MSIRANGE_400K
  834. #define STM32_MSICLK 400000
  835. #elif STM32_MSIRANGE == STM32_MSIRANGE_800K
  836. #define STM32_MSICLK 800000
  837. #elif STM32_MSIRANGE == STM32_MSIRANGE_1M
  838. #define STM32_MSICLK 1000000
  839. #elif STM32_MSIRANGE == STM32_MSIRANGE_2M
  840. #define STM32_MSICLK 2000000
  841. #elif STM32_MSIRANGE == STM32_MSIRANGE_4M
  842. #define STM32_MSICLK 4000000
  843. #elif STM32_MSIRANGE == STM32_MSIRANGE_8M
  844. #define STM32_MSICLK 8000000
  845. #elif STM32_MSIRANGE == STM32_MSIRANGE_16M
  846. #define STM32_MSICLK 16000000
  847. #elif STM32_MSIRANGE == STM32_MSIRANGE_24M
  848. #define STM32_MSICLK 24000000
  849. #elif STM32_MSIRANGE == STM32_MSIRANGE_32M
  850. #define STM32_MSICLK 32000000
  851. #elif STM32_MSIRANGE == STM32_MSIRANGE_48M
  852. #define STM32_MSICLK 48000000
  853. #else
  854. #error "invalid STM32_MSIRANGE value specified"
  855. #endif
  856. /**
  857. * @brief MSIS frequency.
  858. */
  859. #if STM32_MSISRANGE == STM32_MSISRANGE_1M
  860. #define STM32_MSISCLK 1000000
  861. #elif STM32_MSISRANGE == STM32_MSISRANGE_2M
  862. #define STM32_MSISCLK 2000000
  863. #elif STM32_MSISRANGE == STM32_MSISRANGE_4M
  864. #define STM32_MSISCLK 4000000
  865. #elif STM32_MSISRANGE == STM32_MSISRANGE_8M
  866. #define STM32_MSISCLK 8000000
  867. #else
  868. #error "invalid STM32_MSISRANGE value specified"
  869. #endif
  870. /*
  871. * HSI16 related checks.
  872. */
  873. #if STM32_HSI16_ENABLED
  874. #else /* !STM32_HSI16_ENABLED */
  875. #if STM32_SW == STM32_SW_HSI16
  876. #error "HSI16 not enabled, required by STM32_SW"
  877. #endif
  878. #if (STM32_SW == STM32_SW_PLL) && (STM32_PLLSRC == STM32_PLLSRC_HSI16)
  879. #error "HSI16 not enabled, required by STM32_SW and STM32_PLLSRC"
  880. #endif
  881. #if (STM32_MCOSEL == STM32_MCOSEL_HSI16) || \
  882. ((STM32_MCOSEL == STM32_MCOSEL_PLL) && \
  883. (STM32_PLLSRC == STM32_PLLSRC_HSI16))
  884. #error "HSI16 not enabled, required by STM32_MCOSEL"
  885. #endif
  886. #if ((STM32_SAI1SEL == STM32_SAI1SEL_PLLSAI1) || \
  887. (STM32_SAI1SEL == STM32_SAI1SEL_PLLSAI2)) && \
  888. (STM32_PLLSRC == STM32_PLLSRC_HSI16)
  889. #error "HSI16 not enabled, required by STM32_SAI1SEL"
  890. #endif
  891. #if ((STM32_SAI2SEL == STM32_SAI2SEL_PLLSAI1) || \
  892. (STM32_SAI2SEL == STM32_SAI2SEL_PLLSAI2)) && \
  893. (STM32_PLLSRC == STM32_PLLSRC_HSI16)
  894. #error "HSI16 not enabled, required by STM32_SAI2SEL"
  895. #endif
  896. #if (STM32_USART1SEL == STM32_USART1SEL_HSI16)
  897. #error "HSI16 not enabled, required by STM32_USART1SEL"
  898. #endif
  899. #if (STM32_USART2SEL == STM32_USART2SEL_HSI16)
  900. #error "HSI16 not enabled, required by STM32_USART2SEL"
  901. #endif
  902. #if (STM32_USART3SEL == STM32_USART3SEL_HSI16)
  903. #error "HSI16 not enabled, required by STM32_USART3SEL"
  904. #endif
  905. #if (STM32_UART4SEL == STM32_UART4SEL_HSI16)
  906. #error "HSI16 not enabled, required by STM32_UART4SEL"
  907. #endif
  908. #if (STM32_UART5SEL == STM32_UART5SEL_HSI16)
  909. #error "HSI16 not enabled, required by STM32_UART5SEL"
  910. #endif
  911. #if (STM32_LPUART1SEL == STM32_LPUART1SEL_HSI16)
  912. #error "HSI16 not enabled, required by STM32_LPUART1SEL"
  913. #endif
  914. #if (STM32_I2C1SEL == STM32_I2C1SEL_HSI16)
  915. #error "HSI16 not enabled, required by I2C1SEL"
  916. #endif
  917. #if (STM32_I2C2SEL == STM32_I2C2SEL_HSI16)
  918. #error "HSI16 not enabled, required by I2C2SEL"
  919. #endif
  920. #if (STM32_I2C3SEL == STM32_I2C3SEL_HSI16)
  921. #error "HSI16 not enabled, required by I2C3SEL"
  922. #endif
  923. #if (STM32_LPTIM1SEL == STM32_LPTIM1SEL_HSI16)
  924. #error "HSI16 not enabled, required by LPTIM1SEL"
  925. #endif
  926. #if (STM32_LPTIM2SEL == STM32_LPTIM2SEL_HSI16)
  927. #error "HSI16 not enabled, required by LPTIM2SEL"
  928. #endif
  929. #if (STM32_SWPMI1SEL == STM32_SWPMI1SEL_HSI16)
  930. #error "HSI16 not enabled, required by SWPMI1SEL"
  931. #endif
  932. #if (STM32_STOPWUCK == STM32_STOPWUCK_HSI16)
  933. #error "HSI16 not enabled, required by STM32_STOPWUCK"
  934. #endif
  935. #endif /* !STM32_HSI16_ENABLED */
  936. #if STM32_CLOCK_HAS_HSI48
  937. #if STM32_HSI48_ENABLED
  938. #else /* !STM32_HSI48_ENABLED */
  939. #if STM32_MCOSEL == STM32_MCOSEL_HSI48
  940. #error "HSI48 not enabled, required by STM32_MCOSEL"
  941. #endif
  942. #if STM32_CLK48SEL == STM32_CLK48SEL_HSI48
  943. #error "HSI48 not enabled, required by STM32_CLK48SEL"
  944. #endif
  945. #endif /* !STM32_HSI48_ENABLED */
  946. #endif /* STM32_CLOCK_HAS_HSI48 */
  947. /*
  948. * HSE related checks.
  949. */
  950. #if STM32_HSE_ENABLED
  951. #if STM32_HSECLK == 0
  952. #error "HSE frequency not defined"
  953. #else /* STM32_HSECLK != 0 */
  954. #if defined(STM32_HSE_BYPASS)
  955. #if (STM32_HSECLK < STM32_HSECLK_BYP_MIN) || (STM32_HSECLK > STM32_HSECLK_BYP_MAX)
  956. #error "STM32_HSECLK outside acceptable range (STM32_HSECLK_BYP_MIN...STM32_HSECLK_BYP_MAX)"
  957. #endif
  958. #else /* !defined(STM32_HSE_BYPASS) */
  959. #if (STM32_HSECLK < STM32_HSECLK_MIN) || (STM32_HSECLK > STM32_HSECLK_MAX)
  960. #error "STM32_HSECLK outside acceptable range (STM32_HSECLK_MIN...STM32_HSECLK_MAX)"
  961. #endif
  962. #endif /* !defined(STM32_HSE_BYPASS) */
  963. #endif /* STM32_HSECLK != 0 */
  964. #else /* !STM32_HSE_ENABLED */
  965. #if STM32_SW == STM32_SW_HSE
  966. #error "HSE not enabled, required by STM32_SW"
  967. #endif
  968. #if (STM32_SW == STM32_SW_PLL) && (STM32_PLLSRC == STM32_PLLSRC_HSE)
  969. #error "HSE not enabled, required by STM32_SW and STM32_PLLSRC"
  970. #endif
  971. #if (STM32_MCOSEL == STM32_MCOSEL_HSE) || \
  972. ((STM32_MCOSEL == STM32_MCOSEL_PLL) && \
  973. (STM32_PLLSRC == STM32_PLLSRC_HSE))
  974. #error "HSE not enabled, required by STM32_MCOSEL"
  975. #endif
  976. #if ((STM32_SAI1SEL == STM32_SAI1SEL_PLLSAI1) | \
  977. (STM32_SAI1SEL == STM32_SAI1SEL_PLLSAI2)) && \
  978. (STM32_PLLSRC == STM32_PLLSRC_HSE)
  979. #error "HSE not enabled, required by STM32_SAI1SEL"
  980. #endif
  981. #if ((STM32_SAI2SEL == STM32_SAI2SEL_PLLSAI1) | \
  982. (STM32_SAI2SEL == STM32_SAI2SEL_PLLSAI2)) && \
  983. (STM32_PLLSRC == STM32_PLLSRC_HSE)
  984. #error "HSE not enabled, required by STM32_SAI2SEL"
  985. #endif
  986. #if STM32_RTCSEL == STM32_RTCSEL_HSEDIV
  987. #error "HSE not enabled, required by STM32_RTCSEL"
  988. #endif
  989. #endif /* !STM32_HSE_ENABLED */
  990. /*
  991. * LSI related checks.
  992. */
  993. #if STM32_LSI_ENABLED
  994. #else /* !STM32_LSI_ENABLED */
  995. #if STM32_RTCSEL == STM32_RTCSEL_LSI
  996. #error "LSI not enabled, required by STM32_RTCSEL"
  997. #endif
  998. #if STM32_MCOSEL == STM32_MCOSEL_LSI
  999. #error "LSI not enabled, required by STM32_MCOSEL"
  1000. #endif
  1001. #if STM32_LSCOSEL == STM32_LSCOSEL_LSI
  1002. #error "LSI not enabled, required by STM32_LSCOSEL"
  1003. #endif
  1004. #endif /* !STM32_LSI_ENABLED */
  1005. /*
  1006. * LSE related checks.
  1007. */
  1008. #if STM32_LSE_ENABLED
  1009. #if (STM32_LSECLK == 0)
  1010. #error "LSE frequency not defined"
  1011. #endif
  1012. #if (STM32_LSECLK < STM32_LSECLK_MIN) || (STM32_LSECLK > STM32_LSECLK_MAX)
  1013. #error "STM32_LSECLK outside acceptable range (STM32_LSECLK_MIN...STM32_LSECLK_MAX)"
  1014. #endif
  1015. #else /* !STM32_LSE_ENABLED */
  1016. #if STM32_RTCSEL == STM32_RTCSEL_LSE
  1017. #error "LSE not enabled, required by STM32_RTCSEL"
  1018. #endif
  1019. #if STM32_MCOSEL == STM32_MCOSEL_LSE
  1020. #error "LSE not enabled, required by STM32_MCOSEL"
  1021. #endif
  1022. #if STM32_LSCOSEL == STM32_LSCOSEL_LSE
  1023. #error "LSE not enabled, required by STM32_LSCOSEL"
  1024. #endif
  1025. #if STM32_MSIPLL_ENABLED == TRUE
  1026. #error "LSE not enabled, required by STM32_MSIPLL_ENABLED"
  1027. #endif
  1028. #endif /* !STM32_LSE_ENABLED */
  1029. /*
  1030. * MSI related checks.
  1031. */
  1032. #if (STM32_MSIRANGE == STM32_MSIRANGE_48M) && !STM32_MSIPLL_ENABLED
  1033. #warning "STM32_MSIRANGE_48M should be used with STM32_MSIPLL_ENABLED"
  1034. #endif
  1035. /**
  1036. * @brief STM32_PLLM field.
  1037. */
  1038. #if ((STM32_PLLM_VALUE >= 1) && (STM32_PLLM_VALUE <= 8)) || \
  1039. defined(__DOXYGEN__)
  1040. #define STM32_PLLM ((STM32_PLLM_VALUE - 1) << 4)
  1041. #else
  1042. #error "invalid STM32_PLLM_VALUE value specified"
  1043. #endif
  1044. /**
  1045. * @brief PLLs input clock frequency.
  1046. */
  1047. #if (STM32_PLLSRC == STM32_PLLSRC_HSE) || defined(__DOXYGEN__)
  1048. #define STM32_PLLCLKIN (STM32_HSECLK / STM32_PLLM_VALUE)
  1049. #elif STM32_PLLSRC == STM32_PLLSRC_MSI
  1050. #define STM32_PLLCLKIN (STM32_MSICLK / STM32_PLLM_VALUE)
  1051. #elif STM32_PLLSRC == STM32_PLLSRC_HSI16
  1052. #define STM32_PLLCLKIN (STM32_HSI16CLK / STM32_PLLM_VALUE)
  1053. #elif STM32_PLLSRC == STM32_PLLSRC_NOCLOCK
  1054. #define STM32_PLLCLKIN 0
  1055. #else
  1056. #error "invalid STM32_PLLSRC value specified"
  1057. #endif
  1058. /*
  1059. * PLLs input frequency range check.
  1060. */
  1061. #if (STM32_PLLCLKIN != 0) && \
  1062. ((STM32_PLLCLKIN < STM32_PLLIN_MIN) || (STM32_PLLCLKIN > STM32_PLLIN_MAX))
  1063. #error "STM32_PLLCLKIN outside acceptable range (STM32_PLLIN_MIN...STM32_PLLIN_MAX)"
  1064. #endif
  1065. /*
  1066. * PLL enable check.
  1067. */
  1068. #if (STM32_HSI48_ENABLED && (STM32_CLK48SEL == STM32_CLK48SEL_PLL)) || \
  1069. (STM32_SW == STM32_SW_PLL) || \
  1070. (STM32_MCOSEL == STM32_MCOSEL_PLL) || \
  1071. (STM32_SAI1SEL == STM32_SAI1SEL_PLL) || \
  1072. (STM32_SAI2SEL == STM32_SAI2SEL_PLL) || \
  1073. defined(__DOXYGEN__)
  1074. #if STM32_PLLCLKIN == 0
  1075. #error "PLL activation required but no PLL clock selected"
  1076. #endif
  1077. /**
  1078. * @brief PLL activation flag.
  1079. */
  1080. #define STM32_ACTIVATE_PLL TRUE
  1081. #else
  1082. #define STM32_ACTIVATE_PLL FALSE
  1083. #endif
  1084. /**
  1085. * @brief STM32_PLLN field.
  1086. */
  1087. #if ((STM32_PLLN_VALUE >= 8) && (STM32_PLLN_VALUE <= 86)) || \
  1088. defined(__DOXYGEN__)
  1089. #define STM32_PLLN (STM32_PLLN_VALUE << 8)
  1090. #else
  1091. #error "invalid STM32_PLLN_VALUE value specified"
  1092. #endif
  1093. /**
  1094. * @brief STM32_PLLP field.
  1095. */
  1096. #if (STM32_PLLP_VALUE == 7) || defined(__DOXYGEN__)
  1097. #define STM32_PLLP (0 << 17)
  1098. #elif STM32_PLLP_VALUE == 17
  1099. #define STM32_PLLP (1 << 17)
  1100. #else
  1101. #error "invalid STM32_PLLP_VALUE value specified"
  1102. #endif
  1103. /**
  1104. * @brief STM32_PLLQ field.
  1105. */
  1106. #if (STM32_PLLQ_VALUE == 2) || defined(__DOXYGEN__)
  1107. #define STM32_PLLQ (0 << 21)
  1108. #elif STM32_PLLQ_VALUE == 4
  1109. #define STM32_PLLQ (1 << 21)
  1110. #elif STM32_PLLQ_VALUE == 6
  1111. #define STM32_PLLQ (2 << 21)
  1112. #elif STM32_PLLQ_VALUE == 8
  1113. #define STM32_PLLQ (3 << 21)
  1114. #else
  1115. #error "invalid STM32_PLLQ_VALUE value specified"
  1116. #endif
  1117. /**
  1118. * @brief STM32_PLLR field.
  1119. */
  1120. #if (STM32_PLLR_VALUE == 2) || defined(__DOXYGEN__)
  1121. #define STM32_PLLR (0 << 25)
  1122. #elif STM32_PLLR_VALUE == 4
  1123. #define STM32_PLLR (1 << 25)
  1124. #elif STM32_PLLR_VALUE == 6
  1125. #define STM32_PLLR (2 << 25)
  1126. #elif STM32_PLLR_VALUE == 8
  1127. #define STM32_PLLR (3 << 25)
  1128. #else
  1129. #error "invalid STM32_PLLR_VALUE value specified"
  1130. #endif
  1131. #if defined(STM32L496xx) || defined(STM32L4A6xx)
  1132. /**
  1133. * @brief STM32_PLLPDIV field. (Only for STM32L496xx/4A6xx)
  1134. */
  1135. #if (STM32_PLLPDIV_VALUE == 0) || \
  1136. ((STM32_PLLPDIV_VALUE >= 2) && (STM32_PLLPDIV_VALUE <= 31)) || \
  1137. defined(__DOXYGEN__)
  1138. #define STM32_PLLPDIV (STM32_PLLPDIV_VALUE << 27)
  1139. #else
  1140. #error "invalid STM32_PLLPDIV_VALUE value specified"
  1141. #endif
  1142. #endif
  1143. /**
  1144. * @brief STM32_PLLPEN field.
  1145. */
  1146. #if (STM32_SAI1SEL == STM32_SAI1SEL_PLL) || \
  1147. (STM32_SAI2SEL == STM32_SAI2SEL_PLL) || \
  1148. defined(__DOXYGEN__)
  1149. #define STM32_PLLPEN (1 << 16)
  1150. #else
  1151. #define STM32_PLLPEN (0 << 16)
  1152. #endif
  1153. /**
  1154. * @brief STM32_PLLQEN field.
  1155. */
  1156. #if (STM32_CLK48SEL == STM32_CLK48SEL_PLL) || defined(__DOXYGEN__)
  1157. #define STM32_PLLQEN (1 << 20)
  1158. #else
  1159. #define STM32_PLLQEN (0 << 20)
  1160. #endif
  1161. /**
  1162. * @brief STM32_PLLREN field.
  1163. */
  1164. #if (STM32_SW == STM32_SW_PLL) || \
  1165. (STM32_MCOSEL == STM32_MCOSEL_PLL) || \
  1166. defined(__DOXYGEN__)
  1167. #define STM32_PLLREN (1 << 24)
  1168. #else
  1169. #define STM32_PLLREN (0 << 24)
  1170. #endif
  1171. /**
  1172. * @brief PLL VCO frequency.
  1173. */
  1174. #define STM32_PLLVCO (STM32_PLLCLKIN * STM32_PLLN_VALUE)
  1175. /*
  1176. * PLL VCO frequency range check.
  1177. */
  1178. #if STM32_ACTIVATE_PLL && \
  1179. ((STM32_PLLVCO < STM32_PLLVCO_MIN) || (STM32_PLLVCO > STM32_PLLVCO_MAX))
  1180. #error "STM32_PLLVCO outside acceptable range (STM32_PLLVCO_MIN...STM32_PLLVCO_MAX)"
  1181. #endif
  1182. /**
  1183. * @brief PLL P output clock frequency.
  1184. */
  1185. #if (STM32_PLLPDIV_VALUE == 0) || defined(__DOXYGEN__)
  1186. #define STM32_PLL_P_CLKOUT (STM32_PLLVCO / STM32_PLLP_VALUE)
  1187. #else
  1188. #define STM32_PLL_P_CLKOUT (STM32_PLLVCO / STM32_PLLPDIV_VALUE)
  1189. #endif
  1190. /**
  1191. * @brief PLL Q output clock frequency.
  1192. */
  1193. #define STM32_PLL_Q_CLKOUT (STM32_PLLVCO / STM32_PLLQ_VALUE)
  1194. /**
  1195. * @brief PLL R output clock frequency.
  1196. */
  1197. #define STM32_PLL_R_CLKOUT (STM32_PLLVCO / STM32_PLLR_VALUE)
  1198. /*
  1199. * PLL-P output frequency range check.
  1200. */
  1201. #if STM32_ACTIVATE_PLL && \
  1202. ((STM32_PLL_P_CLKOUT < STM32_PLLP_MIN) || (STM32_PLL_P_CLKOUT > STM32_PLLP_MAX))
  1203. #error "STM32_PLL_P_CLKOUT outside acceptable range (STM32_PLLP_MIN...STM32_PLLP_MAX)"
  1204. #endif
  1205. /*
  1206. * PLL-Q output frequency range check.
  1207. */
  1208. #if STM32_ACTIVATE_PLL && \
  1209. ((STM32_PLL_Q_CLKOUT < STM32_PLLQ_MIN) || (STM32_PLL_Q_CLKOUT > STM32_PLLQ_MAX))
  1210. #error "STM32_PLL_Q_CLKOUT outside acceptable range (STM32_PLLQ_MIN...STM32_PLLQ_MAX)"
  1211. #endif
  1212. /*
  1213. * PLL-R output frequency range check.
  1214. */
  1215. #if STM32_ACTIVATE_PLL && \
  1216. ((STM32_PLL_R_CLKOUT < STM32_PLLR_MIN) || (STM32_PLL_R_CLKOUT > STM32_PLLR_MAX))
  1217. #error "STM32_PLL_R_CLKOUT outside acceptable range (STM32_PLLR_MIN...STM32_PLLR_MAX)"
  1218. #endif
  1219. /**
  1220. * @brief System clock source.
  1221. */
  1222. #if STM32_NO_INIT || defined(__DOXYGEN__)
  1223. #define STM32_SYSCLK STM32_MSICLK
  1224. #elif (STM32_SW == STM32_SW_MSI)
  1225. #define STM32_SYSCLK STM32_MSICLK
  1226. #elif (STM32_SW == STM32_SW_HSI16)
  1227. #define STM32_SYSCLK STM32_HSI16CLK
  1228. #elif (STM32_SW == STM32_SW_HSE)
  1229. #define STM32_SYSCLK STM32_HSECLK
  1230. #elif (STM32_SW == STM32_SW_PLL)
  1231. #define STM32_SYSCLK STM32_PLL_R_CLKOUT
  1232. #else
  1233. #error "invalid STM32_SW value specified"
  1234. #endif
  1235. /* Check on the system clock.*/
  1236. #if STM32_SYSCLK > STM32_SYSCLK_MAX
  1237. #error "STM32_SYSCLK above maximum rated frequency (STM32_SYSCLK_MAX)"
  1238. #endif
  1239. /**
  1240. * @brief AHB frequency.
  1241. */
  1242. #if (STM32_HPRE == STM32_HPRE_DIV1) || defined(__DOXYGEN__)
  1243. #define STM32_HCLK (STM32_SYSCLK / 1)
  1244. #elif STM32_HPRE == STM32_HPRE_DIV2
  1245. #define STM32_HCLK (STM32_SYSCLK / 2)
  1246. #elif STM32_HPRE == STM32_HPRE_DIV4
  1247. #define STM32_HCLK (STM32_SYSCLK / 4)
  1248. #elif STM32_HPRE == STM32_HPRE_DIV8
  1249. #define STM32_HCLK (STM32_SYSCLK / 8)
  1250. #elif STM32_HPRE == STM32_HPRE_DIV16
  1251. #define STM32_HCLK (STM32_SYSCLK / 16)
  1252. #elif STM32_HPRE == STM32_HPRE_DIV64
  1253. #define STM32_HCLK (STM32_SYSCLK / 64)
  1254. #elif STM32_HPRE == STM32_HPRE_DIV128
  1255. #define STM32_HCLK (STM32_SYSCLK / 128)
  1256. #elif STM32_HPRE == STM32_HPRE_DIV256
  1257. #define STM32_HCLK (STM32_SYSCLK / 256)
  1258. #elif STM32_HPRE == STM32_HPRE_DIV512
  1259. #define STM32_HCLK (STM32_SYSCLK / 512)
  1260. #else
  1261. #error "invalid STM32_HPRE value specified"
  1262. #endif
  1263. /*
  1264. * AHB frequency check.
  1265. */
  1266. #if STM32_HCLK > STM32_SYSCLK_MAX
  1267. #error "STM32_HCLK exceeding maximum frequency (STM32_SYSCLK_MAX)"
  1268. #endif
  1269. /**
  1270. * @brief APB1 frequency.
  1271. */
  1272. #if (STM32_PPRE1 == STM32_PPRE1_DIV1) || defined(__DOXYGEN__)
  1273. #define STM32_PCLK1 (STM32_HCLK / 1)
  1274. #elif STM32_PPRE1 == STM32_PPRE1_DIV2
  1275. #define STM32_PCLK1 (STM32_HCLK / 2)
  1276. #elif STM32_PPRE1 == STM32_PPRE1_DIV4
  1277. #define STM32_PCLK1 (STM32_HCLK / 4)
  1278. #elif STM32_PPRE1 == STM32_PPRE1_DIV8
  1279. #define STM32_PCLK1 (STM32_HCLK / 8)
  1280. #elif STM32_PPRE1 == STM32_PPRE1_DIV16
  1281. #define STM32_PCLK1 (STM32_HCLK / 16)
  1282. #else
  1283. #error "invalid STM32_PPRE1 value specified"
  1284. #endif
  1285. /*
  1286. * APB1 frequency check.
  1287. */
  1288. #if STM32_PCLK1 > STM32_PCLK1_MAX
  1289. #error "STM32_PCLK1 exceeding maximum frequency (STM32_PCLK1_MAX)"
  1290. #endif
  1291. /**
  1292. * @brief APB2 frequency.
  1293. */
  1294. #if (STM32_PPRE2 == STM32_PPRE2_DIV1) || defined(__DOXYGEN__)
  1295. #define STM32_PCLK2 (STM32_HCLK / 1)
  1296. #elif STM32_PPRE2 == STM32_PPRE2_DIV2
  1297. #define STM32_PCLK2 (STM32_HCLK / 2)
  1298. #elif STM32_PPRE2 == STM32_PPRE2_DIV4
  1299. #define STM32_PCLK2 (STM32_HCLK / 4)
  1300. #elif STM32_PPRE2 == STM32_PPRE2_DIV8
  1301. #define STM32_PCLK2 (STM32_HCLK / 8)
  1302. #elif STM32_PPRE2 == STM32_PPRE2_DIV16
  1303. #define STM32_PCLK2 (STM32_HCLK / 16)
  1304. #else
  1305. #error "invalid STM32_PPRE2 value specified"
  1306. #endif
  1307. /*
  1308. * APB2 frequency check.
  1309. */
  1310. #if STM32_PCLK2 > STM32_PCLK2_MAX
  1311. #error "STM32_PCLK2 exceeding maximum frequency (STM32_PCLK2_MAX)"
  1312. #endif
  1313. /*
  1314. * PLLSAI1 enable check.
  1315. */
  1316. #if (STM32_SAI1SEL == STM32_SAI1SEL_PLLSAI1) || \
  1317. (STM32_SAI2SEL == STM32_SAI2SEL_PLLSAI1) || \
  1318. (STM32_CLK48SEL == STM32_CLK48SEL_PLLSAI1) || \
  1319. (STM32_ADCSEL == STM32_ADCSEL_PLLSAI1) || \
  1320. defined(__DOXYGEN__)
  1321. #if STM32_PLLCLKIN == 0
  1322. #error "PLLSAI1 activation required but no PLL clock selected"
  1323. #endif
  1324. /**
  1325. * @brief PLLSAI1 activation flag.
  1326. */
  1327. #define STM32_ACTIVATE_PLLSAI1 TRUE
  1328. #else
  1329. #define STM32_ACTIVATE_PLLSAI1 FALSE
  1330. #endif
  1331. /**
  1332. * @brief STM32_PLLSAI1N field.
  1333. */
  1334. #if ((STM32_PLLSAI1N_VALUE >= 8) && (STM32_PLLSAI1N_VALUE <= 86)) || \
  1335. defined(__DOXYGEN__)
  1336. #define STM32_PLLSAI1N (STM32_PLLSAI1N_VALUE << 8)
  1337. #else
  1338. #error "invalid STM32_PLLSAI1N_VALUE value specified"
  1339. #endif
  1340. /**
  1341. * @brief STM32_PLLSAI1P field.
  1342. */
  1343. #if (STM32_PLLSAI1P_VALUE == 7) || defined(__DOXYGEN__)
  1344. #define STM32_PLLSAI1P (0 << 17)
  1345. #elif STM32_PLLSAI1P_VALUE == 17
  1346. #define STM32_PLLSAI1P (1 << 17)
  1347. #else
  1348. #error "invalid STM32_PLLSAI1P_VALUE value specified"
  1349. #endif
  1350. /**
  1351. * @brief STM32_PLLSAI1Q field.
  1352. */
  1353. #if (STM32_PLLSAI1Q_VALUE == 2) || defined(__DOXYGEN__)
  1354. #define STM32_PLLSAI1Q (0 << 21)
  1355. #elif STM32_PLLSAI1Q_VALUE == 4
  1356. #define STM32_PLLSAI1Q (1 << 21)
  1357. #elif STM32_PLLSAI1Q_VALUE == 6
  1358. #define STM32_PLLSAI1Q (2 << 21)
  1359. #elif STM32_PLLSAI1Q_VALUE == 8
  1360. #define STM32_PLLSAI1Q (3 << 21)
  1361. #else
  1362. #error "invalid STM32_PLLSAI1Q_VALUE value specified"
  1363. #endif
  1364. /**
  1365. * @brief STM32_PLLSAI1R field.
  1366. */
  1367. #if (STM32_PLLSAI1R_VALUE == 2) || defined(__DOXYGEN__)
  1368. #define STM32_PLLSAI1R (0 << 25)
  1369. #elif STM32_PLLSAI1R_VALUE == 4
  1370. #define STM32_PLLSAI1R (1 << 25)
  1371. #elif STM32_PLLSAI1R_VALUE == 6
  1372. #define STM32_PLLSAI1R (2 << 25)
  1373. #elif STM32_PLLSAI1R_VALUE == 8
  1374. #define STM32_PLLSAI1R (3 << 25)
  1375. #else
  1376. #error "invalid STM32_PLLSAI1R_VALUE value specified"
  1377. #endif
  1378. #if defined(STM32L496xx) || defined(STM32L4A6xx)
  1379. /**
  1380. * @brief STM32_PLLSAI1PDIV field. (Only for STM32L496xx/4A6xx)
  1381. */
  1382. #if ((STM32_PLLSAI1PDIV_VALUE != 1) && (STM32_PLLSAI1PDIV_VALUE <= 31)) || \
  1383. defined(__DOXYGEN__)
  1384. #define STM32_PLLSAI1PDIV (STM32_PLLSAI1PDIV_VALUE << 27)
  1385. #else
  1386. #error "invalid STM32_PLLSAI1PDIV_VALUE value specified"
  1387. #endif
  1388. #endif
  1389. /**
  1390. * @brief STM32_PLLSAI1PEN field.
  1391. */
  1392. #if (STM32_SAI1SEL == STM32_SAI1SEL_PLLSAI1) || \
  1393. (STM32_SAI2SEL == STM32_SAI2SEL_PLLSAI1) || \
  1394. defined(__DOXYGEN__)
  1395. #define STM32_PLLSAI1PEN (1 << 16)
  1396. #else
  1397. #define STM32_PLLSAI1PEN (0 << 16)
  1398. #endif
  1399. /**
  1400. * @brief STM32_PLLSAI1QEN field.
  1401. */
  1402. #if (STM32_CLK48SEL == STM32_CLK48SEL_PLLSAI1) || defined(__DOXYGEN__)
  1403. #define STM32_PLLSAI1QEN (1 << 20)
  1404. #else
  1405. #define STM32_PLLSAI1QEN (0 << 20)
  1406. #endif
  1407. /**
  1408. * @brief STM32_PLLSAI1REN field.
  1409. */
  1410. #if (STM32_ADCSEL == STM32_ADCSEL_PLLSAI1) || defined(__DOXYGEN__)
  1411. #define STM32_PLLSAI1REN (1 << 24)
  1412. #else
  1413. #define STM32_PLLSAI1REN (0 << 24)
  1414. #endif
  1415. /**
  1416. * @brief PLLSAI1 VCO frequency.
  1417. */
  1418. #define STM32_PLLSAI1VCO (STM32_PLLCLKIN * STM32_PLLSAI1N_VALUE)
  1419. /*
  1420. * PLLSAI1 VCO frequency range check.
  1421. */
  1422. #if STM32_ACTIVATE_PLLSAI1 && \
  1423. ((STM32_PLLSAI1VCO < STM32_PLLVCO_MIN) || (STM32_PLLSAI1VCO > STM32_PLLVCO_MAX))
  1424. #error "STM32_PLLSAI1VCO outside acceptable range (STM32_PLLVCO_MIN...STM32_PLLVCO_MAX)"
  1425. #endif
  1426. /**
  1427. * @brief PLLSAI1-P output clock frequency.
  1428. */
  1429. #if (STM32_PLLSAI1PDIV_VALUE == 0) || defined(__DOXYGEN__)
  1430. #define STM32_PLLSAI1_P_CLKOUT (STM32_PLLSAI1VCO / STM32_PLLSAI1P_VALUE)
  1431. #else
  1432. #define STM32_PLLSAI1_P_CLKOUT (STM32_PLLSAI1VCO / STM32_PLLSAI1PDIV_VALUE)
  1433. #endif
  1434. /**
  1435. * @brief PLLSAI1-Q output clock frequency.
  1436. */
  1437. #define STM32_PLLSAI1_Q_CLKOUT (STM32_PLLSAI1VCO / STM32_PLLSAI1Q_VALUE)
  1438. /**
  1439. * @brief PLLSAI1-R output clock frequency.
  1440. */
  1441. #define STM32_PLLSAI1_R_CLKOUT (STM32_PLLSAI1VCO / STM32_PLLSAI1R_VALUE)
  1442. /*
  1443. * PLLSAI1-P output frequency range check.
  1444. */
  1445. #if STM32_ACTIVATE_PLLSAI1 && \
  1446. ((STM32_PLLSAI1_P_CLKOUT < STM32_PLLP_MIN) || (STM32_PLLSAI1_P_CLKOUT > STM32_PLLP_MAX))
  1447. #error "STM32_PLLSAI1_P_CLKOUT outside acceptable range (STM32_PLLP_MIN...STM32_PLLP_MAX)"
  1448. #endif
  1449. /*
  1450. * PLLSAI1-Q output frequency range check.
  1451. */
  1452. #if STM32_ACTIVATE_PLLSAI1 && \
  1453. ((STM32_PLLSAI1_Q_CLKOUT < STM32_PLLQ_MIN) || (STM32_PLLSAI1_Q_CLKOUT > STM32_PLLQ_MAX))
  1454. #error "STM32_PLLSAI1_Q_CLKOUT outside acceptable range (STM32_PLLQ_MIN...STM32_PLLQ_MAX)"
  1455. #endif
  1456. /*
  1457. * PLLSAI1-R output frequency range check.
  1458. */
  1459. #if STM32_ACTIVATE_PLLSAI1 && \
  1460. ((STM32_PLLSAI1_R_CLKOUT < STM32_PLLR_MIN) || (STM32_PLLSAI1_R_CLKOUT > STM32_PLLR_MAX))
  1461. #error "STM32_PLLSAI1_R_CLKOUT outside acceptable range (STM32_PLLR_MIN...STM32_PLLR_MAX)"
  1462. #endif
  1463. /*
  1464. * PLLSAI2 enable check.
  1465. */
  1466. #if (STM32_SAI1SEL == STM32_SAI1SEL_PLLSAI2) || \
  1467. (STM32_SAI2SEL == STM32_SAI2SEL_PLLSAI2) || \
  1468. (STM32_ADCSEL == STM32_ADCSEL_PLLSAI2) || \
  1469. defined(__DOXYGEN__)
  1470. #if STM32_PLLCLKIN == 0
  1471. #error "PLLSAI2 activation required but no PLL clock selected"
  1472. #endif
  1473. /**
  1474. * @brief PLLSAI2 activation flag.
  1475. */
  1476. #define STM32_ACTIVATE_PLLSAI2 TRUE
  1477. #else
  1478. #define STM32_ACTIVATE_PLLSAI2 FALSE
  1479. #endif
  1480. /**
  1481. * @brief STM32_PLLSAI2N field.
  1482. */
  1483. #if ((STM32_PLLSAI2N_VALUE >= 8) && (STM32_PLLSAI2N_VALUE <= 86)) || \
  1484. defined(__DOXYGEN__)
  1485. #define STM32_PLLSAI2N (STM32_PLLSAI2N_VALUE << 8)
  1486. #else
  1487. #error "invalid STM32_PLLSAI2N_VALUE value specified"
  1488. #endif
  1489. /**
  1490. * @brief STM32_PLLSAI2P field.
  1491. */
  1492. #if (STM32_PLLSAI2P_VALUE == 7) || defined(__DOXYGEN__)
  1493. #define STM32_PLLSAI2P (0 << 17)
  1494. #elif STM32_PLLSAI2P_VALUE == 17
  1495. #define STM32_PLLSAI2P (1 << 17)
  1496. #else
  1497. #error "invalid STM32_PLLSAI2P_VALUE value specified"
  1498. #endif
  1499. /**
  1500. * @brief STM32_PLLSAI2R field.
  1501. */
  1502. #if (STM32_PLLSAI2R_VALUE == 2) || defined(__DOXYGEN__)
  1503. #define STM32_PLLSAI2R (0 << 25)
  1504. #elif STM32_PLLSAI2R_VALUE == 4
  1505. #define STM32_PLLSAI2R (1 << 25)
  1506. #elif STM32_PLLSAI2R_VALUE == 6
  1507. #define STM32_PLLSAI2R (2 << 25)
  1508. #elif STM32_PLLSAI2R_VALUE == 8
  1509. #define STM32_PLLSAI2R (3 << 25)
  1510. #else
  1511. #error "invalid STM32_PLLSAI2R_VALUE value specified"
  1512. #endif
  1513. #if defined(STM32L496xx) || defined(STM32L4A6xx)
  1514. /**
  1515. * @brief STM32_PLLSAI2PDIV field. (Only for STM32L496xx/4A6xx)
  1516. */
  1517. #if ((STM32_PLLSAI2PDIV_VALUE != 1) && (STM32_PLLSAI2PDIV_VALUE <= 31)) || \
  1518. defined(__DOXYGEN__)
  1519. #define STM32_PLLSAI2PDIV (STM32_PLLSAI2PDIV_VALUE << 27)
  1520. #else
  1521. #error "invalid STM32_PLLSAI2PDIV_VALUE value specified"
  1522. #endif
  1523. #endif
  1524. /**
  1525. * @brief STM32_PLLSAI2PEN field.
  1526. */
  1527. #if (STM32_SAI1SEL == STM32_SAI1SEL_PLLSAI2) || \
  1528. (STM32_SAI2SEL == STM32_SAI2SEL_PLLSAI2) || \
  1529. defined(__DOXYGEN__)
  1530. #define STM32_PLLSAI2PEN (1 << 16)
  1531. #else
  1532. #define STM32_PLLSAI2PEN (0 << 16)
  1533. #endif
  1534. /**
  1535. * @brief STM32_PLLSAI2REN field.
  1536. */
  1537. #if (STM32_ADCSEL == STM32_ADCSEL_PLLSAI2) || defined(__DOXYGEN__)
  1538. #define STM32_PLLSAI2REN (1 << 24)
  1539. #else
  1540. #define STM32_PLLSAI2REN (0 << 24)
  1541. #endif
  1542. /**
  1543. * @brief PLLSAI2 VCO frequency.
  1544. */
  1545. #define STM32_PLLSAI2VCO (STM32_PLLCLKIN * STM32_PLLSAI2N_VALUE)
  1546. /*
  1547. * PLLSAI2 VCO frequency range check.
  1548. */
  1549. #if STM32_ACTIVATE_PLLSAI2 && \
  1550. ((STM32_PLLSAI2VCO < STM32_PLLVCO_MIN) || (STM32_PLLSAI2VCO > STM32_PLLVCO_MAX))
  1551. #error "STM32_PLLSAI2VCO outside acceptable range (STM32_PLLVCO_MIN...STM32_PLLVCO_MAX)"
  1552. #endif
  1553. /**
  1554. * @brief PLLSAI2-P output clock frequency.
  1555. */
  1556. #if (STM32_PLLSAI2PDIV_VALUE == 0) || defined(__DOXYGEN__)
  1557. #define STM32_PLLSAI2_P_CLKOUT (STM32_PLLSAI2VCO / STM32_PLLSAI2P_VALUE)
  1558. #else
  1559. #define STM32_PLLSAI2_P_CLKOUT (STM32_PLLSAI2VCO / STM32_PLLSAI2PDIV_VALUE)
  1560. #endif
  1561. /**
  1562. * @brief PLLSAI2-R output clock frequency.
  1563. */
  1564. #define STM32_PLLSAI2_R_CLKOUT (STM32_PLLSAI2VCO / STM32_PLLSAI2R_VALUE)
  1565. /*
  1566. * PLLSAI2-P output frequency range check.
  1567. */
  1568. #if STM32_ACTIVATE_PLLSAI2 && \
  1569. ((STM32_PLLSAI2_P_CLKOUT < STM32_PLLP_MIN) || (STM32_PLLSAI2_P_CLKOUT > STM32_PLLP_MAX))
  1570. #error "STM32_PLLSAI2_P_CLKOUT outside acceptable range (STM32_PLLP_MIN...STM32_PLLP_MAX)"
  1571. #endif
  1572. /*
  1573. * PLLSAI2-R output frequency range check.
  1574. */
  1575. #if STM32_ACTIVATE_PLLSAI2 && \
  1576. ((STM32_PLLSAI2_R_CLKOUT < STM32_PLLR_MIN) || (STM32_PLLSAI2_R_CLKOUT > STM32_PLLR_MAX))
  1577. #error "STM32_PLLSAI2_R_CLKOUT outside acceptable range (STM32_PLLR_MIN...STM32_PLLR_MAX)"
  1578. #endif
  1579. /**
  1580. * @brief MCO divider clock frequency.
  1581. */
  1582. #if (STM32_MCOSEL == STM32_MCOSEL_NOCLOCK) || defined(__DOXYGEN__)
  1583. #define STM32_MCODIVCLK 0
  1584. #elif STM32_MCOSEL == STM32_MCOSEL_SYSCLK
  1585. #define STM32_MCODIVCLK STM32_SYSCLK
  1586. #elif STM32_MCOSEL == STM32_MCOSEL_MSI
  1587. #define STM32_MCODIVCLK STM32_MSICLK
  1588. #elif STM32_MCOSEL == STM32_MCOSEL_HSI16
  1589. #define STM32_MCODIVCLK STM32_HSI16CLK
  1590. #elif STM32_MCOSEL == STM32_MCOSEL_HSE
  1591. #define STM32_MCODIVCLK STM32_HSECLK
  1592. #elif STM32_MCOSEL == STM32_MCOSEL_PLL
  1593. #define STM32_MCODIVCLK STM32_PLL_P_CLKOUT
  1594. #elif STM32_MCOSEL == STM32_MCOSEL_LSI
  1595. #define STM32_MCODIVCLK STM32_LSICLK
  1596. #elif STM32_MCOSEL == STM32_MCOSEL_LSE
  1597. #define STM32_MCODIVCLK STM32_LSECLK
  1598. #elif STM32_MCOSEL == STM32_MCOSEL_HSI48
  1599. #define STM32_MCODIVCLK STM32_HSI48CLK
  1600. #else
  1601. #error "invalid STM32_MCOSEL value specified"
  1602. #endif
  1603. /**
  1604. * @brief MCO output pin clock frequency.
  1605. */
  1606. #if (STM32_MCOPRE == STM32_MCOPRE_DIV1) || defined(__DOXYGEN__)
  1607. #define STM32_MCOCLK STM32_MCODIVCLK
  1608. #elif STM32_MCOPRE == STM32_MCOPRE_DIV2
  1609. #define STM32_MCOCLK (STM32_MCODIVCLK / 2)
  1610. #elif STM32_MCOPRE == STM32_MCOPRE_DIV4
  1611. #define STM32_MCOCLK (STM32_MCODIVCLK / 4)
  1612. #elif STM32_MCOPRE == STM32_MCOPRE_DIV8
  1613. #define STM32_MCOCLK (STM32_MCODIVCLK / 8)
  1614. #elif STM32_MCOPRE == STM32_MCOPRE_DIV16
  1615. #define STM32_MCOCLK (STM32_MCODIVCLK / 16)
  1616. #else
  1617. #error "invalid STM32_MCOPRE value specified"
  1618. #endif
  1619. /**
  1620. * @brief RTC clock frequency.
  1621. */
  1622. #if (STM32_RTCSEL == STM32_RTCSEL_NOCLOCK) || defined(__DOXYGEN__)
  1623. #define STM32_RTCCLK 0
  1624. #elif STM32_RTCSEL == STM32_RTCSEL_LSE
  1625. #define STM32_RTCCLK STM32_LSECLK
  1626. #elif STM32_RTCSEL == STM32_RTCSEL_LSI
  1627. #define STM32_RTCCLK STM32_LSICLK
  1628. #elif STM32_RTCSEL == STM32_RTCSEL_HSEDIV
  1629. #define STM32_RTCCLK (STM32_HSECLK / 32)
  1630. #else
  1631. #error "invalid STM32_RTCSEL value specified"
  1632. #endif
  1633. /**
  1634. * @brief USART1 clock frequency.
  1635. */
  1636. #if (STM32_USART1SEL == STM32_USART1SEL_PCLK2) || defined(__DOXYGEN__)
  1637. #define STM32_USART1CLK STM32_PCLK2
  1638. #elif STM32_USART1SEL == STM32_USART1SEL_SYSCLK
  1639. #define STM32_USART1CLK STM32_SYSCLK
  1640. #elif STM32_USART1SEL == STM32_USART1SEL_HSI16
  1641. #define STM32_USART1CLK STM32_HSI16CLK
  1642. #elif STM32_USART1SEL == STM32_USART1SEL_LSE
  1643. #define STM32_USART1CLK STM32_LSECLK
  1644. #else
  1645. #error "invalid source selected for USART1 clock"
  1646. #endif
  1647. /**
  1648. * @brief USART2 clock frequency.
  1649. */
  1650. #if (STM32_USART2SEL == STM32_USART2SEL_PCLK1) || defined(__DOXYGEN__)
  1651. #define STM32_USART2CLK STM32_PCLK1
  1652. #elif STM32_USART2SEL == STM32_USART2SEL_SYSCLK
  1653. #define STM32_USART2CLK STM32_SYSCLK
  1654. #elif STM32_USART2SEL == STM32_USART2SEL_HSI16
  1655. #define STM32_USART2CLK STM32_HSI16CLK
  1656. #elif STM32_USART2SEL == STM32_USART2SEL_LSE
  1657. #define STM32_USART2CLK STM32_LSECLK
  1658. #else
  1659. #error "invalid source selected for USART2 clock"
  1660. #endif
  1661. /**
  1662. * @brief USART3 clock frequency.
  1663. */
  1664. #if (STM32_USART3SEL == STM32_USART3SEL_PCLK1) || defined(__DOXYGEN__)
  1665. #define STM32_USART3CLK STM32_PCLK1
  1666. #elif STM32_USART3SEL == STM32_USART3SEL_SYSCLK
  1667. #define STM32_USART3CLK STM32_SYSCLK
  1668. #elif STM32_USART3SEL == STM32_USART3SEL_HSI16
  1669. #define STM32_USART3CLK STM32_HSI16CLK
  1670. #elif STM32_USART3SEL == STM32_USART3SEL_LSE
  1671. #define STM32_USART3CLK STM32_LSECLK
  1672. #else
  1673. #error "invalid source selected for USART3 clock"
  1674. #endif
  1675. /**
  1676. * @brief UART4 clock frequency.
  1677. */
  1678. #if (STM32_UART4SEL == STM32_UART4SEL_PCLK1) || defined(__DOXYGEN__)
  1679. #define STM32_UART4CLK STM32_PCLK1
  1680. #elif STM32_UART4SEL == STM32_UART4SEL_SYSCLK
  1681. #define STM32_UART4CLK STM32_SYSCLK
  1682. #elif STM32_UART4SEL == STM32_UART4SEL_HSI16
  1683. #define STM32_UART4CLK STM32_HSI16CLK
  1684. #elif STM32_UART4SEL == STM32_UART4SEL_LSE
  1685. #define STM32_UART4CLK STM32_LSECLK
  1686. #else
  1687. #error "invalid source selected for UART4 clock"
  1688. #endif
  1689. /**
  1690. * @brief UART5 clock frequency.
  1691. */
  1692. #if (STM32_UART5SEL == STM32_UART5SEL_PCLK1) || defined(__DOXYGEN__)
  1693. #define STM32_UART5CLK STM32_PCLK1
  1694. #elif STM32_UART5SEL == STM32_UART5SEL_SYSCLK
  1695. #define STM32_UART5CLK STM32_SYSCLK
  1696. #elif STM32_UART5SEL == STM32_UART5SEL_HSI16
  1697. #define STM32_UART5CLK STM32_HSI16CLK
  1698. #elif STM32_UART5SEL == STM32_UART5SEL_LSE
  1699. #define STM32_UART5CLK STM32_LSECLK
  1700. #else
  1701. #error "invalid source selected for UART5 clock"
  1702. #endif
  1703. /**
  1704. * @brief LPUART1 clock frequency.
  1705. */
  1706. #if (STM32_LPUART1SEL == STM32_LPUART1SEL_PCLK1) || defined(__DOXYGEN__)
  1707. #define STM32_LPUART1CLK STM32_PCLK1
  1708. #elif STM32_LPUART1SEL == STM32_LPUART1SEL_SYSCLK
  1709. #define STM32_LPUART1CLK STM32_SYSCLK
  1710. #elif STM32_LPUART1SEL == STM32_LPUART1SEL_HSI16
  1711. #define STM32_LPUART1CLK STM32_HSI16CLK
  1712. #elif STM32_LPUART1SEL == STM32_LPUART1SEL_LSE
  1713. #define STM32_LPUART1CLK STM32_LSECLK
  1714. #else
  1715. #error "invalid source selected for LPUART1 clock"
  1716. #endif
  1717. /**
  1718. * @brief I2C1 clock frequency.
  1719. */
  1720. #if (STM32_I2C1SEL == STM32_I2C1SEL_PCLK1) || defined(__DOXYGEN__)
  1721. #define STM32_I2C1CLK STM32_PCLK1
  1722. #elif STM32_I2C1SEL == STM32_I2C1SEL_SYSCLK
  1723. #define STM32_I2C1CLK STM32_SYSCLK
  1724. #elif STM32_I2C1SEL == STM32_I2C1SEL_HSI16
  1725. #define STM32_I2C1CLK STM32_HSI16CLK
  1726. #else
  1727. #error "invalid source selected for I2C1 clock"
  1728. #endif
  1729. /**
  1730. * @brief I2C2 clock frequency.
  1731. */
  1732. #if (STM32_I2C2SEL == STM32_I2C2SEL_PCLK1) || defined(__DOXYGEN__)
  1733. #define STM32_I2C2CLK STM32_PCLK1
  1734. #elif STM32_I2C2SEL == STM32_I2C2SEL_SYSCLK
  1735. #define STM32_I2C2CLK STM32_SYSCLK
  1736. #elif STM32_I2C2SEL == STM32_I2C2SEL_HSI16
  1737. #define STM32_I2C2CLK STM32_HSI16CLK
  1738. #else
  1739. #error "invalid source selected for I2C2 clock"
  1740. #endif
  1741. /**
  1742. * @brief I2C3 clock frequency.
  1743. */
  1744. #if (STM32_I2C3SEL == STM32_I2C3SEL_PCLK1) || defined(__DOXYGEN__)
  1745. #define STM32_I2C3CLK STM32_PCLK1
  1746. #elif STM32_I2C3SEL == STM32_I2C3SEL_SYSCLK
  1747. #define STM32_I2C3CLK STM32_SYSCLK
  1748. #elif STM32_I2C3SEL == STM32_I2C3SEL_HSI16
  1749. #define STM32_I2C3CLK STM32_HSI16CLK
  1750. #else
  1751. #error "invalid source selected for I2C3 clock"
  1752. #endif
  1753. /**
  1754. * @brief LPTIM1 clock frequency.
  1755. */
  1756. #if (STM32_LPTIM1SEL == STM32_LPTIM1SEL_PCLK1) || defined(__DOXYGEN__)
  1757. #define STM32_LPTIM1CLK STM32_PCLK1
  1758. #elif STM32_LPTIM1SEL == STM32_LPTIM1SEL_LSI
  1759. #define STM32_LPTIM1CLK STM32_LSICLK
  1760. #elif STM32_LPTIM1SEL == STM32_LPTIM1SEL_HSI16
  1761. #define STM32_LPTIM1CLK STM32_HSI16CLK
  1762. #elif STM32_LPTIM1SEL == STM32_LPTIM1SEL_LSE
  1763. #define STM32_LPTIM1CLK STM32_LSECLK
  1764. #else
  1765. #error "invalid source selected for LPTIM1 clock"
  1766. #endif
  1767. /**
  1768. * @brief LPTIM2 clock frequency.
  1769. */
  1770. #if (STM32_LPTIM2SEL == STM32_LPTIM2SEL_PCLK1) || defined(__DOXYGEN__)
  1771. #define STM32_LPTIM2CLK STM32_PCLK1
  1772. #elif STM32_LPTIM2SEL == STM32_LPTIM2SEL_LSI
  1773. #define STM32_LPTIM2CLK STM32_LSICLK
  1774. #elif STM32_LPTIM2SEL == STM32_LPTIM2SEL_HSI16
  1775. #define STM32_LPTIM2CLK STM32_HSI16CLK
  1776. #elif STM32_LPTIM2SEL == STM32_LPTIM2SEL_LSE
  1777. #define STM32_LPTIM2CLK STM32_LSECLK
  1778. #else
  1779. #error "invalid source selected for LPTIM2 clock"
  1780. #endif
  1781. /**
  1782. * @brief 48MHz clock frequency.
  1783. */
  1784. #if !STM32_CLOCK_HAS_HSI48 || defined(__DOXYGEN__)
  1785. #if (STM32_CLK48SEL == STM32_CLK48SEL_NOCLK) || defined(__DOXYGEN__)
  1786. #define STM32_48CLK 0
  1787. #elif STM32_CLK48SEL == STM32_CLK48SEL_PLLSAI1
  1788. #define STM32_48CLK (STM32_PLLSAI1VCO / STM32_PLLSAI1Q_VALUE)
  1789. #elif STM32_CLK48SEL == STM32_CLK48SEL_PLL
  1790. #define STM32_48CLK (STM32_PLLVCO / STM32_PLLQ_VALUE)
  1791. #elif STM32_CLK48SEL == STM32_CLK48SEL_MSI
  1792. #define STM32_48CLK STM32_MSICLK
  1793. #else
  1794. #error "invalid source selected for 48CLK clock"
  1795. #endif
  1796. #else /* STM32_CLOCK_HAS_HSI48 */
  1797. #if (STM32_CLK48SEL == STM32_CLK48SEL_HSI48) || defined(__DOXYGEN__)
  1798. #define STM32_48CLK STM32_HSI48CLK
  1799. #elif STM32_CLK48SEL == STM32_CLK48SEL_PLLSAI1
  1800. #define STM32_48CLK (STM32_PLLSAI1VCO / STM32_PLLSAI1Q_VALUE)
  1801. #elif STM32_CLK48SEL == STM32_CLK48SEL_PLL
  1802. #define STM32_48CLK (STM32_PLLVCO / STM32_PLLQ_VALUE)
  1803. #elif STM32_CLK48SEL == STM32_CLK48SEL_MSI
  1804. #define STM32_48CLK STM32_MSICLK
  1805. #else
  1806. #error "invalid source selected for 48CLK clock"
  1807. #endif
  1808. #endif /* STM32_CLOCK_HAS_HSI48 */
  1809. /**
  1810. * @brief USB clock point.
  1811. */
  1812. #define STM32_USBCLK STM32_48CLK
  1813. /**
  1814. * @brief RNG clock point.
  1815. */
  1816. #define STM32_RNGCLK STM32_48CLK
  1817. /**
  1818. * @brief ADC clock frequency.
  1819. */
  1820. #if (STM32_ADCSEL == STM32_ADCSEL_NOCLK) || defined(__DOXYGEN__)
  1821. #define STM32_ADCCLK 0
  1822. #elif STM32_ADCSEL == STM32_ADCSEL_PLLSAI1
  1823. #define STM32_ADCCLK STM32_PLLSAI1_R_CLKOUT
  1824. #elif STM32_ADCSEL == STM32_ADCSEL_PLLSAI2
  1825. #define STM32_ADCCLK STM32_PLLSAI2_R_CLKOUT
  1826. #elif STM32_ADCSEL == STM32_ADCSEL_SYSCLK
  1827. #define STM32_ADCCLK STM32_SYSCLK
  1828. #else
  1829. #error "invalid source selected for ADC clock"
  1830. #endif
  1831. /**
  1832. * @brief SWPMI1 clock frequency.
  1833. */
  1834. #if (STM32_SWPMI1SEL == STM32_SWPMI1SEL_PCLK1) || defined(__DOXYGEN__)
  1835. #define STM32_SWPMI1CLK STM32_PCLK1
  1836. #elif STM32_SWPMI1SEL == STM32_SWPMI1SEL_HSI16
  1837. #define STM32_SWPMI1CLK STM32_HSI16CLK
  1838. #else
  1839. #error "invalid source selected for SWPMI1 clock"
  1840. #endif
  1841. /**
  1842. * @brief DFSDM clock frequency.
  1843. */
  1844. #if (STM32_DFSDMSEL == STM32_DFSDMSEL_PCLK2) || defined(__DOXYGEN__)
  1845. #define STM32_DFSDMCLK STM32_PCLK2
  1846. #elif STM32_DFSDMSEL == STM32_DFSDMSEL_SYSCLK
  1847. #define STM32_DFSDMCLK STM32_SYSCLK
  1848. #else
  1849. #error "invalid source selected for DFSDM clock"
  1850. #endif
  1851. /**
  1852. * @brief SDMMC frequency.
  1853. */
  1854. #define STM32_SDMMC1CLK STM32_48CLK
  1855. /**
  1856. * @brief Clock of timers connected to APB1
  1857. */
  1858. #if (STM32_PPRE1 == STM32_PPRE1_DIV1) || defined(__DOXYGEN__)
  1859. #define STM32_TIMCLK1 (STM32_PCLK1 * 1)
  1860. #else
  1861. #define STM32_TIMCLK1 (STM32_PCLK1 * 2)
  1862. #endif
  1863. /**
  1864. * @brief Clock of timers connected to APB2.
  1865. */
  1866. #if (STM32_PPRE2 == STM32_PPRE2_DIV1) || defined(__DOXYGEN__)
  1867. #define STM32_TIMCLK2 (STM32_PCLK2 * 1)
  1868. #else
  1869. #define STM32_TIMCLK2 (STM32_PCLK2 * 2)
  1870. #endif
  1871. /**
  1872. * @brief Flash settings.
  1873. */
  1874. #if (STM32_HCLK <= STM32_0WS_THRESHOLD) || defined(__DOXYGEN__)
  1875. #define STM32_FLASHBITS FLASH_ACR_LATENCY_0WS
  1876. #elif STM32_HCLK <= STM32_1WS_THRESHOLD
  1877. #define STM32_FLASHBITS FLASH_ACR_LATENCY_1WS
  1878. #elif STM32_HCLK <= STM32_2WS_THRESHOLD
  1879. #define STM32_FLASHBITS FLASH_ACR_LATENCY_2WS
  1880. #elif STM32_HCLK <= STM32_3WS_THRESHOLD
  1881. #define STM32_FLASHBITS FLASH_ACR_LATENCY_3WS
  1882. #else
  1883. #define STM32_FLASHBITS FLASH_ACR_LATENCY_4WS
  1884. #endif
  1885. /**
  1886. * @brief Flash settings for MSI.
  1887. */
  1888. #if (STM32_MSICLK <= STM32_0WS_THRESHOLD) || defined(__DOXYGEN__)
  1889. #define STM32_MSI_FLASHBITS FLASH_ACR_LATENCY_0WS
  1890. #elif STM32_MSICLK <= STM32_1WS_THRESHOLD
  1891. #define STM32_MSI_FLASHBITS FLASH_ACR_LATENCY_1WS
  1892. #elif STM32_MSICLK <= STM32_2WS_THRESHOLD
  1893. #define STM32_MSI_FLASHBITS FLASH_ACR_LATENCY_2WS
  1894. #elif STM32_MSICLK <= STM32_3WS_THRESHOLD
  1895. #define STM32_MSI_FLASHBITS FLASH_ACR_LATENCY_3WS
  1896. #else
  1897. #define STM32_MSI_FLASHBITS FLASH_ACR_LATENCY_4WS
  1898. #endif
  1899. /*===========================================================================*/
  1900. /* Driver data structures and types. */
  1901. /*===========================================================================*/
  1902. /*===========================================================================*/
  1903. /* Driver macros. */
  1904. /*===========================================================================*/
  1905. /*===========================================================================*/
  1906. /* External declarations. */
  1907. /*===========================================================================*/
  1908. /* Various helpers.*/
  1909. #include "nvic.h"
  1910. #include "cache.h"
  1911. #include "mpu_v7m.h"
  1912. #include "stm32_isr.h"
  1913. #include "stm32_dma.h"
  1914. #include "stm32_exti.h"
  1915. #include "stm32_rcc.h"
  1916. #ifdef __cplusplus
  1917. extern "C" {
  1918. #endif
  1919. void hal_lld_init(void);
  1920. void stm32_clock_init(void);
  1921. #ifdef __cplusplus
  1922. }
  1923. #endif
  1924. #endif /* HAL_LLD_H */
  1925. /** @} */