hal_lld.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 STM32F4xx/hal_lld.h
  15. * @brief STM32F4xx/STM32F2xx HAL subsystem low level driver header.
  16. *
  17. * @addtogroup HAL
  18. * @{
  19. */
  20. #ifndef HAL_LLD_H
  21. #define HAL_LLD_H
  22. #include "stm32_registry.h"
  23. #if defined(STM32F413xx)
  24. #include "hal_lld_type2.h"
  25. #else
  26. #include "hal_lld_type1.h"
  27. #endif
  28. /* Various helpers.*/
  29. #include "nvic.h"
  30. #include "cache.h"
  31. #include "mpu_v7m.h"
  32. #include "stm32_isr.h"
  33. #include "stm32_dma.h"
  34. #include "stm32_exti.h"
  35. #include "stm32_rcc.h"
  36. /*===========================================================================*/
  37. /* Driver constants. */
  38. /*===========================================================================*/
  39. /*===========================================================================*/
  40. /* Driver pre-compile time settings. */
  41. /*===========================================================================*/
  42. /*===========================================================================*/
  43. /* Derived constants and error checks. */
  44. /*===========================================================================*/
  45. /**
  46. * @brief MCO1 divider clock.
  47. */
  48. #if (STM32_MCO1SEL == STM32_MCO1SEL_HSI) || defined(__DOXYGEN__)
  49. #define STM32_MCO1DIVCLK STM32_HSICLK
  50. #elif STM32_MCO1SEL == STM32_MCO1SEL_LSE
  51. #define STM32_MCO1DIVCLK STM32_LSECLK
  52. #elif STM32_MCO1SEL == STM32_MCO1SEL_HSE
  53. #define STM32_MCO1DIVCLK STM32_HSECLK
  54. #elif STM32_MCO1SEL == STM32_MCO1SEL_PLL
  55. #define STM32_MCO1DIVCLK STM32_PLLCLKOUT
  56. #else
  57. #error "invalid STM32_MCO1SEL value specified"
  58. #endif
  59. /**
  60. * @brief MCO1 output pin clock.
  61. */
  62. #if (STM32_MCO1PRE == STM32_MCO1PRE_DIV1) || defined(__DOXYGEN__)
  63. #define STM32_MCO1CLK STM32_MCO1DIVCLK
  64. #elif STM32_MCO1PRE == STM32_MCO1PRE_DIV2
  65. #define STM32_MCO1CLK (STM32_MCO1DIVCLK / 2)
  66. #elif STM32_MCO1PRE == STM32_MCO1PRE_DIV3
  67. #define STM32_MCO1CLK (STM32_MCO1DIVCLK / 3)
  68. #elif STM32_MCO1PRE == STM32_MCO1PRE_DIV4
  69. #define STM32_MCO1CLK (STM32_MCO1DIVCLK / 4)
  70. #elif STM32_MCO1PRE == STM32_MCO1PRE_DIV5
  71. #define STM32_MCO1CLK (STM32_MCO1DIVCLK / 5)
  72. #else
  73. #error "invalid STM32_MCO1PRE value specified"
  74. #endif
  75. /**
  76. * @brief MCO2 divider clock.
  77. */
  78. #if (STM32_MCO2SEL == STM32_MCO2SEL_HSE) || defined(__DOXYGEN__)
  79. #define STM32_MCO2DIVCLK STM32_HSECLK
  80. #elif STM32_MCO2SEL == STM32_MCO2SEL_PLL
  81. #define STM32_MCO2DIVCLK STM32_PLLCLKOUT
  82. #elif STM32_MCO2SEL == STM32_MCO2SEL_SYSCLK
  83. #define STM32_MCO2DIVCLK STM32_SYSCLK
  84. #elif STM32_MCO2SEL == STM32_MCO2SEL_PLLI2S
  85. #define STM32_MCO2DIVCLK STM32_PLLI2S
  86. #else
  87. #error "invalid STM32_MCO2SEL value specified"
  88. #endif
  89. /**
  90. * @brief MCO2 output pin clock.
  91. */
  92. #if (STM32_MCO2PRE == STM32_MCO2PRE_DIV1) || defined(__DOXYGEN__)
  93. #define STM32_MCO2CLK STM32_MCO2DIVCLK
  94. #elif STM32_MCO2PRE == STM32_MCO2PRE_DIV2
  95. #define STM32_MCO2CLK (STM32_MCO2DIVCLK / 2)
  96. #elif STM32_MCO2PRE == STM32_MCO2PRE_DIV3
  97. #define STM32_MCO2CLK (STM32_MCO2DIVCLK / 3)
  98. #elif STM32_MCO2PRE == STM32_MCO2PRE_DIV4
  99. #define STM32_MCO2CLK (STM32_MCO2DIVCLK / 4)
  100. #elif STM32_MCO2PRE == STM32_MCO2PRE_DIV5
  101. #define STM32_MCO2CLK (STM32_MCO2DIVCLK / 5)
  102. #else
  103. #error "invalid STM32_MCO2PRE value specified"
  104. #endif
  105. /**
  106. * @brief RTC HSE divider setting.
  107. */
  108. #if ((STM32_RTCPRE_VALUE >= 2) && (STM32_RTCPRE_VALUE <= 31)) || \
  109. defined(__DOXYGEN__)
  110. #define STM32_RTCPRE (STM32_RTCPRE_VALUE << 16)
  111. #else
  112. #error "invalid STM32_RTCPRE value specified"
  113. #endif
  114. /**
  115. * @brief HSE divider toward RTC clock.
  116. */
  117. #if ((STM32_RTCPRE_VALUE >= 2) && (STM32_RTCPRE_VALUE <= 31)) || \
  118. defined(__DOXYGEN__)
  119. #define STM32_HSEDIVCLK (STM32_HSECLK / STM32_RTCPRE_VALUE)
  120. #else
  121. #error "invalid STM32_RTCPRE value specified"
  122. #endif
  123. /**
  124. * @brief RTC clock.
  125. */
  126. #if (STM32_RTCSEL == STM32_RTCSEL_NOCLOCK) || defined(__DOXYGEN__)
  127. #define STM32_RTCCLK 0
  128. #elif STM32_RTCSEL == STM32_RTCSEL_LSE
  129. #define STM32_RTCCLK STM32_LSECLK
  130. #elif STM32_RTCSEL == STM32_RTCSEL_LSI
  131. #define STM32_RTCCLK STM32_LSICLK
  132. #elif STM32_RTCSEL == STM32_RTCSEL_HSEDIV
  133. #define STM32_RTCCLK STM32_HSEDIVCLK
  134. #else
  135. #error "invalid STM32_RTCSEL value specified"
  136. #endif
  137. /**
  138. * @brief 48MHz frequency.
  139. */
  140. #if STM32_CLOCK48_REQUIRED || defined(__DOXYGEN__)
  141. #if STM32_HAS_RCC_CK48MSEL || defined(__DOXYGEN__)
  142. #if (STM32_CK48MSEL == STM32_CK48MSEL_PLL) || defined(__DOXYGEN__)
  143. #define STM32_PLL48CLK (STM32_PLLVCO / STM32_PLLQ_VALUE)
  144. #elif STM32_CK48MSEL == STM32_CK48MSEL_PLLALT
  145. #if STM32_RCC_CK48MSEL_USES_I2S
  146. #define STM32_PLL48CLK STM32_PLLI2S_Q_CLKOUT
  147. #else
  148. #define STM32_PLL48CLK STM32_PLLSAI_Q_CLKOUT
  149. #endif
  150. #else
  151. #error "invalid source selected for PLL48CLK clock"
  152. #endif
  153. #else /* !STM32_HAS_RCC_CK48MSEL */
  154. #define STM32_PLL48CLK (STM32_PLLVCO / STM32_PLLQ_VALUE)
  155. #endif /* !STM32_HAS_RCC_CK48MSEL */
  156. #else /* !STM32_CLOCK48_REQUIRED */
  157. #define STM32_PLL48CLK 0
  158. #endif /* STM32_CLOCK48_REQUIRED */
  159. #if !STM32_HAS_RCC_DCKCFGR || (STM32_TIMPRE == STM32_TIMPRE_PCLK) || \
  160. defined(__DOXYGEN__)
  161. /**
  162. * @brief Clock of timers connected to APB1
  163. * (Timers 2, 3, 4, 5, 6, 7, 12, 13, 14).
  164. */
  165. #if (STM32_PPRE1 == STM32_PPRE1_DIV1) || defined(__DOXYGEN__)
  166. #define STM32_TIMCLK1 (STM32_PCLK1 * 1)
  167. #else
  168. #define STM32_TIMCLK1 (STM32_PCLK1 * 2)
  169. #endif
  170. /**
  171. * @brief Clock of timers connected to APB2 (Timers 1, 8, 9, 10, 11).
  172. */
  173. #if (STM32_PPRE2 == STM32_PPRE2_DIV1) || defined(__DOXYGEN__)
  174. #define STM32_TIMCLK2 (STM32_PCLK2 * 1)
  175. #else
  176. #define STM32_TIMCLK2 (STM32_PCLK2 * 2)
  177. #endif
  178. #else /* STM32_HAS_RCC_DCKCFGR && (STM32_TIMPRE == STM32_TIMPRE_HCLK) */
  179. #if (STM32_PPRE1 == STM32_PPRE1_DIV1) || \
  180. (STM32_PPRE1 == STM32_PPRE1_DIV2) || \
  181. ((STM32_PPRE1 == STM32_PPRE1_DIV4) && \
  182. (STM32_TIMPRE_PRESCALE4 == TRUE)) || defined(__DOXYGEN__)
  183. #define STM32_TIMCLK1 STM32_HCLK
  184. #else
  185. #define STM32_TIMCLK1 (STM32_PCLK1 * 4)
  186. #endif
  187. #if (STM32_PPRE2 == STM32_PPRE2_DIV1) || \
  188. (STM32_PPRE2 == STM32_PPRE2_DIV2) || \
  189. ((STM32_PPRE2 == STM32_PPRE2_DIV4) && \
  190. (STM32_TIMPRE_PRESCALE4 == TRUE)) || defined(__DOXYGEN__)
  191. #define STM32_TIMCLK2 STM32_HCLK
  192. #else
  193. #define STM32_TIMCLK2 (STM32_PCLK2 * 4)
  194. #endif
  195. #endif /* STM32_HAS_RCC_DCKCFGR && (STM32_TIMPRE == STM32_TIMPRE_HCLK) */
  196. /**
  197. * @brief Flash settings.
  198. */
  199. #if (STM32_HCLK <= STM32_0WS_THRESHOLD) || defined(__DOXYGEN__)
  200. #define STM32_FLASHBITS 0x00000000
  201. #elif STM32_HCLK <= STM32_1WS_THRESHOLD
  202. #define STM32_FLASHBITS 0x00000001
  203. #elif STM32_HCLK <= STM32_2WS_THRESHOLD
  204. #define STM32_FLASHBITS 0x00000002
  205. #elif STM32_HCLK <= STM32_3WS_THRESHOLD
  206. #define STM32_FLASHBITS 0x00000003
  207. #elif STM32_HCLK <= STM32_4WS_THRESHOLD
  208. #define STM32_FLASHBITS 0x00000004
  209. #elif STM32_HCLK <= STM32_5WS_THRESHOLD
  210. #define STM32_FLASHBITS 0x00000005
  211. #elif STM32_HCLK <= STM32_6WS_THRESHOLD
  212. #define STM32_FLASHBITS 0x00000006
  213. #elif STM32_HCLK <= STM32_7WS_THRESHOLD
  214. #define STM32_FLASHBITS 0x00000007
  215. #elif STM32_HCLK <= STM32_8WS_THRESHOLD
  216. #define STM32_FLASHBITS 0x00000008
  217. #else
  218. #error "invalid frequency at specified VDD level"
  219. #endif
  220. /*===========================================================================*/
  221. /* Driver data structures and types. */
  222. /*===========================================================================*/
  223. /*===========================================================================*/
  224. /* Driver macros. */
  225. /*===========================================================================*/
  226. /*===========================================================================*/
  227. /* External declarations. */
  228. /*===========================================================================*/
  229. #endif /* HAL_LLD_H */
  230. /** @} */