stm32timertran.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. /*
  2. * Copyright (C) 2013 by Uwe Bonnes (bon@elektron.ikp.physik.tu-darmstadt.de)
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the copyright holders nor the names of
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  26. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  27. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  29. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. * For additional information see http://www.ethernut.de/
  33. *
  34. */
  35. /*
  36. * $Id: stm32timertran.h 5638 2014-04-07 13:58:48Z u_bonnes $
  37. */
  38. /*!
  39. * \file include/arch/cm3/stm/stm32timertran.h
  40. * \brief Compile time STM32 timer translations.
  41. *
  42. * This header file determines the target specific timer entities,
  43. * by a simple configured timer identifier.
  44. *
  45. * Unlike most other header files, this one may be included several
  46. * times within a single source file, typically once for each configured
  47. * identifier.
  48. *
  49. * \code
  50. * #undef STM32TIMER_ID
  51. * #define STM32TIMER_ID STM32TIM_OWI0_TIMER_ID
  52. * #include <arch/cm3/stm/stm32timertran.h>
  53. * #define STM32_OWITIMER_BASE STM32TIMER_BASE
  54. * #define STM32_OWITIMER_SIG STM32TIMER_SIG
  55. * #define STM32_OWITIMER_CLK() STM32TIMER_CLK()
  56. * #define STM32_OWITIMER_RST() STM32TIMER_RST()
  57. * #define STM32_OWITIMER_PCLK STM32TIMER_PCLK
  58. * #define STM32_OWITIMER_NCH STM32TIMER_NCH
  59. * #if defined(MCU_STM32F1)
  60. * #define STM32_OWITIMER_REMAP_MASK STM32TIMER_REMAP_MASK
  61. * #define STM32_OWITIMER_REMAP_SHIFT STM32TIMER_REMAP_SHIFT
  62. * #else
  63. * #define STM32_OWITIMER_AF STM32TIMER_AF(STM32TIM_OWI_PORT, STM32TIM_OWI0_PIN)
  64. * #endif
  65. * #define STM32_OWITIMER_WIDTH STM32TIMER_WIDTH
  66. *
  67. * Provided entities
  68. * - STM32TIMER_BASE The base address of the timer
  69. * - STM32TIMER_SIG The signal for the timer.
  70. * FIXME: Multiple/Chained interrupts for TIM1, 9-17!
  71. * - STM32TIMER_CLK() Function to enable the clock for the timer.
  72. * - STM32TIMER_RST() Function to reset the timer.
  73. * - STM32TIMER_PCLK The Timer input clock CK_IN from the clock tree in Hertz.
  74. * - STM32TIMER_NCH Compare/Capture channels available for the timer.
  75. * - STM32TIMER_BDTR Timer has Break and dead-time register.
  76. * (STM32F1)
  77. * - STM32TIMER_REMAP_MASK Mask and shift value to remap the timer in AFIO_MAPR
  78. * - STM32TIMER_REMAP_SHIFT Mask and shift value to remap the timer in AFIO_MAPR
  79. * FIXME: The actual remap value must be given from the programmer.
  80. *(else)
  81. * - STM32TIMER_AF The alternate function index to connect timer to pin.
  82. * FIXME: Handle some F3 corner cases
  83. * - STM32TIMER_WIDTH The width of the timer in bits.
  84. */
  85. #include <cfg/arch.h>
  86. #include <arch/cm3/stm/stm32xxxx.h>
  87. /*
  88. * Remove any previously defined register names.
  89. */
  90. #undef STM32TIMER_BASE
  91. #undef STM32TIMER_SIG
  92. #undef STM32TIMER_CLK
  93. #undef STM32TIMER_RST
  94. #undef STM32TIMER_PCLK
  95. #undef STM32TIMER_NCH
  96. #undef STM32TIMER_BDTR
  97. #undef STM32TIMER_AF
  98. #undef STM32TIMER_REMAP_MASK
  99. #undef STM32TIMER_REMAP_SHIFT
  100. #undef STM32TIMER_WIDTH
  101. #define CM3BBPULSE(base, regstruct, reg, bit) do { \
  102. CM3BBSET(base, regstruct, reg, bit); \
  103. CM3BBCLR(base, regstruct, reg, bit); } while(0)
  104. /* What did the F3 designers smoke when they distributed the AF so random? */
  105. /* We leave the F1 remapping to the user */
  106. #if defined(RCC_APB2ENR_TIM1EN) && defined(RCC_APB2RSTR_TIM1RST) && (STM32TIMER_ID == 1)
  107. #define STM32TIMER_BASE TIM1_BASE
  108. #define STM32TIMER_SIG sig_TIM1
  109. #define STM32TIMER_CLK() CM3BBSET(RCC_BASE, RCC_TypeDef, APB2ENR, _BI32(RCC_APB2ENR_TIM1EN))
  110. #define STM32TIMER_RST() CM3BBPULSE(RCC_BASE, RCC_TypeDef, APB2RSTR, _BI32(RCC_APB2RSTR_TIM1RST))
  111. #define STM32TIMER_BDTR
  112. #if defined(MCU_STM32F3)
  113. #define STM32TIMER_NCH 6
  114. #else
  115. #define STM32TIMER_NCH 4
  116. #endif
  117. #if defined(MCU_STM32F3)
  118. /* RCC_CFGR3_TIM1SW may only be set with AHBdiv = ABP2DIV = 1, so timerclock is 2 * PCLK
  119. with RCC_CFGR3_TIM1SW or RCC_CFGR_PPRE2_2 set*/
  120. #define STM32TIMER_PCLK \
  121. (((CM3BBGET(RCC_BASE, RCC_TypeDef, CFGR3, _BI32(RCC_CFGR3_TIM1SW))) || \
  122. (CM3BBGET(RCC_BASE, RCC_TypeDef, CFGR, _BI32(RCC_CFGR_PPRE2_2))))? \
  123. (2 * NutClockGet(NUT_HWCLK_PCLK2)) : (NutClockGet(NUT_HWCLK_PCLK2)))
  124. #else
  125. #define STM32TIMER_PCLK \
  126. ((CM3BBGET(RCC_BASE, RCC_TypeDef, CFGR, _BI32(RCC_CFGR_PPRE2_2)))? \
  127. (2 * NutClockGet(NUT_HWCLK_PCLK2)) : (NutClockGet(NUT_HWCLK_PCLK2)))
  128. #endif
  129. #if defined(MCU_STM32F2) ||defined(MCU_STM32F4)
  130. #define STM32TIMER_AF(port, pin) 1
  131. #elif defined(MCU_STM32F3)
  132. #define STM32TIMER_AF(port, pin) (port == GPIOE_BASE)? 2 : \
  133. (((port == GPIOB_BASE) && (pin == 15))||((port == GPIOC_BASE) && (pin == 13)))? 4 : \
  134. (( port == GPIOA_BASE) && (pin == 15))? 9 : \
  135. (((port == GPIOA_BASE) && (pin == 11))||((port == GPIOB_BASE) && (pin == 8)))? 12 : 6
  136. #elif defined(MCU_STM32F1)
  137. #define STM32TIMER_REMAP_MASK AFIO_MAPR_TIM1_REMAP
  138. #define STM32TIMER_REMAP_SHIFT (_BI32(AFIO_MAPR_TIM1_REMAP_0))
  139. #endif
  140. #elif defined (RCC_APB1ENR_TIM2EN) && defined(RCC_APB1RSTR_TIM2RST) && (STM32TIMER_ID == 2)
  141. #define STM32TIMER_BASE TIM2_BASE
  142. #define STM32TIMER_SIG sig_TIM2
  143. #define STM32TIMER_CLK() CM3BBSET(RCC_BASE, RCC_TypeDef, APB1ENR, _BI32(RCC_APB1ENR_TIM2EN))
  144. #define STM32TIMER_RST() CM3BBPULSE(RCC_BASE, RCC_TypeDef, APB1RSTR, _BI32(RCC_APB1RSTR_TIM2RST))
  145. #define STM32TIMER_PCLK \
  146. ((CM3BBGET(RCC_BASE, RCC_TypeDef, CFGR, _BI32(RCC_CFGR_PPRE1_2)))? \
  147. (2 * NutClockGet(NUT_HWCLK_PCLK1)) : (NutClockGet(NUT_HWCLK_PCLK1)))
  148. #define STM32TIMER_NCH 4
  149. #if defined(MCU_STM32L1) || defined(MCU_STM32F2) ||defined(MCU_STM32F4)
  150. #define STM32TIMER_AF(port, pin) 1
  151. #elif defined(MCU_STM32F3)
  152. #define STM32TIMER_AF(port, pin) \
  153. (port == GPIOD_BASE) ? 2 : \
  154. (((port == GPIOA_BASE) && (pin == 9))||((port == GPIOA_BASE) && (pin == 10)))? 10 : 1
  155. #elif defined(MCU_STM32F1)
  156. #define STM32TIMER_REMAP_MASK AFIO_MAPR_TIM2_REMAP
  157. #define STM32TIMER_REMAP_SHIFT (_BI32(AFIO_MAPR_TIM2_REMAP_0))
  158. #endif
  159. #if defined(MCU_STM32F2) ||defined(MCU_STM32F3)||defined(MCU_STM32F4)
  160. #define STM32TIMER_WIDTH 32
  161. #endif
  162. #elif defined (RCC_APB1ENR_TIM3EN) && defined(RCC_APB1RSTR_TIM3RST) && (STM32TIMER_ID == 3)
  163. #define STM32TIMER_BASE TIM3_BASE
  164. #define STM32TIMER_SIG sig_TIM3
  165. #define STM32TIMER_CLK() CM3BBSET(RCC_BASE, RCC_TypeDef, APB1ENR, _BI32(RCC_APB1ENR_TIM3EN))
  166. #define STM32TIMER_RST() CM3BBPULSE(RCC_BASE, RCC_TypeDef, APB1RSTR, _BI32(RCC_APB1RSTR_TIM3RST))
  167. #define STM32TIMER_PCLK \
  168. ((CM3BBGET(RCC_BASE, RCC_TypeDef, CFGR, _BI32(RCC_CFGR_PPRE1_2)))? \
  169. (2 * NutClockGet(NUT_HWCLK_PCLK1)) : (NutClockGet(NUT_HWCLK_PCLK1)))
  170. #define STM32TIMER_NCH 4
  171. #if defined(MCU_STM32L1) || defined(MCU_STM32F2) ||defined(MCU_STM32F4)
  172. #define STM32TIMER_AF(port, pin) 2
  173. /* STM32F30: PA4/CH2/AF2, PA6/CH1/AF2,PA7/CH3/AF2,
  174. PB0/CH3/AF2, PB1/CH4/AF2, PB5/CH2/AF2,
  175. PB3/ETR/AF10
  176. PC6/CH1/AF2, PC7/CH2/AF2, PC8/CH3/AF2, PC9/CH4/AF2,
  177. PD2/CH1/AF2,
  178. PE2/CH1/AF2, PE3/CH3/AF2, PE4/CH3/AF2, PE5/CH4/AF2
  179. STM32F37: PA4/CH2/AF2, PA6/CH1/AF2,PA7/CH3/AF2,
  180. PB0/CH2/AF10, PB3/ETR/AF10, PB6/CH2/AF10, PB6/CH2/AF10,
  181. PB0/CH3/AF2, PB1/CH4/AF2, PB5/CH2/AF2,
  182. PC6/CH1/AF2, PC7/CH2/AF2, PC8/CH3/AF2, PC9/CH4/AF2,
  183. PD2/ETR/AF2
  184. */
  185. #elif defined(STM32F30X)
  186. #define STM32TIMER_AF(port, pin) \
  187. ((port == GPIOB_BASE) && (pin == 3))? 10 : 2
  188. #elif defined(STM32F37X)
  189. #define STM32TIMER_AF(port, pin) \
  190. ((port == GPIOB_BASE) && ((pin == 0) || (pin == 3) || (pin == 6) || (pin == 7)))? 10 : 2
  191. #else
  192. #warning Illegal pin mapping for TIM3
  193. #endif
  194. #elif defined (RCC_APB1ENR_TIM4EN) && defined(RCC_APB1RSTR_TIM4RST) && (STM32TIMER_ID == 4)
  195. #define STM32TIMER_BASE TIM4_BASE
  196. #define STM32TIMER_SIG sig_TIM4
  197. #define STM32TIMER_CLK() CM3BBSET(RCC_BASE, RCC_TypeDef, APB1ENR, _BI32(RCC_APB1ENR_TIM4EN))
  198. #define STM32TIMER_RST() CM3BBPULSE(RCC_BASE, RCC_TypeDef, APB1RSTR, _BI32(RCC_APB1RSTR_TIM4RST))
  199. #define STM32TIMER_PCLK \
  200. ((CM3BBGET(RCC_BASE, RCC_TypeDef, CFGR, _BI32(RCC_CFGR_PPRE1_2)))? \
  201. (2 * NutClockGet(NUT_HWCLK_PCLK1)) : (NutClockGet(NUT_HWCLK_PCLK1)))
  202. #define STM32TIMER_NCH 4
  203. #if defined(MCU_STM32L1) || defined(MCU_STM32F2) ||defined(MCU_STM32F4)
  204. #define STM32TIMER_AF(port, pin) 2
  205. #elif defined(MCU_STM32F3)
  206. #define STM32TIMER_AF(port, pin) (port == GPIOA_BASE)? 10 : 2
  207. #elif defined(MCU_STM32F1)
  208. #define STM32TIMER_REMAP_MASK AFIO_MAPR_TIM4_REMAP
  209. #define STM32TIMER_REMAP_SHIFT (_BI32(AFIO_MAPR_TIM4_REMAP))
  210. #endif
  211. #elif defined (RCC_APB1ENR_TIM5EN) && defined(RCC_APB1RSTR_TIM5RST) && (STM32TIMER_ID == 5)
  212. #define STM32TIMER_BASE TIM5_BASE
  213. #define STM32TIMER_SIG sig_TIM5
  214. #define STM32TIMER_CLK() CM3BBSET(RCC_BASE, RCC_TypeDef, APB1ENR, _BI32(RCC_APB1ENR_TIM5EN))
  215. #define STM32TIMER_RST() CM3BBPULSE(RCC_BASE, RCC_TypeDef, APB1RSTR, _BI32(RCC_APB1RSTR_TIM5RST))
  216. #define STM32TIMER_PCLK \
  217. ((CM3BBGET(RCC_BASE, RCC_TypeDef, CFGR, _BI32(RCC_CFGR_PPRE1_2)))? \
  218. (2 * NutClockGet(NUT_HWCLK_PCLK1)) : (NutClockGet(NUT_HWCLK_PCLK1)))
  219. #define STM32TIMER_NCH 4
  220. #if defined(MCU_STM32L1) || defined(MCU_STM32F2) ||defined(MCU_STM32F4)
  221. #define STM32TIMER_AF(port, pin) 2
  222. #endif
  223. #if defined(MCU_STM32F2) || defined(MCU_STM32F4)
  224. #define STM32TIMER_WIDTH 32
  225. #endif
  226. #elif defined (RCC_APB1ENR_TIM6EN) && defined(RCC_APB1RSTR_TIM6RST) && (STM32TIMER_ID == 6)
  227. #define STM32TIMER_BASE TIM6_BASE
  228. #define STM32TIMER_SIG sig_TIM6
  229. #define STM32TIMER_CLK() CM3BBSET(RCC_BASE, RCC_TypeDef, APB1ENR, _BI32(RCC_APB1ENR_TIM6EN))
  230. #define STM32TIMER_RST() CM3BBPULSE(RCC_BASE, RCC_TypeDef, APB1RSTR, _BI32(RCC_APB1RSTR_TIM6RST))
  231. #define STM32TIMER_PCLK \
  232. ((CM3BBGET(RCC_BASE, RCC_TypeDef, CFGR, _BI32(RCC_CFGR_PPRE1_2)))? \
  233. (2 * NutClockGet(NUT_HWCLK_PCLK1)) : (NutClockGet(NUT_HWCLK_PCLK1)))
  234. #define STM32TIMER_NCH 0
  235. #elif defined (RCC_APB1ENR_TIM7EN) && defined(RCC_APB1RSTR_TIM7RST) && (STM32TIMER_ID == 7 )
  236. #define STM32TIMER_BASE TIM7_BASE
  237. #define STM32TIMER_SIG sig_TIM7
  238. #define STM32TIMER_CLK() CM3BBSET(RCC_BASE, RCC_TypeDef, APB1ENR, _BI32(RCC_APB1ENR_TIM7EN))
  239. #define STM32TIMER_RST() CM3BBPULSE(RCC_BASE, RCC_TypeDef, APB1RSTR, _BI32(RCC_APB1RSTR_TIM7RST))
  240. #define STM32TIMER_PCLK \
  241. ((CM3BBGET(RCC_BASE, RCC_TypeDef, CFGR, _BI32(RCC_CFGR_PPRE1_2)))? \
  242. (2 * NutClockGet(NUT_HWCLK_PCLK1)) : (NutClockGet(NUT_HWCLK_PCLK1)))
  243. #define STM32TIMER_NCH 0
  244. #elif defined (RCC_APB2ENR_TIM8EN) && defined(RCC_APB2RSTR_TIM8RST) && (STM32TIMER_ID == 8 )
  245. #define STM32TIMER_BASE TIM8_BASE
  246. #define STM32TIMER_SIG sig_TIM8
  247. #define STM32TIMER_CLK() CM3BBSET(RCC_BASE, RCC_TypeDef, APB2ENR, _BI32(RCC_APB2ENR_TIM8EN))
  248. #define STM32TIMER_RST() CM3BBPULSE(RCC_BASE, RCC_TypeDef, APB2RSTR, _BI32(RCC_APB2RSTR_TIM8RST))
  249. #if defined(MCU_STM32F3)
  250. /* RCC_CFGR3_TIM8SW may only be set with AHBdiv = ABP2DIV = 1, so timerclock is 2 * PCLK
  251. with RCC_CFGR3_TIM8SW or RCC_CFGR_PPRE2_2 set*/
  252. #define STM32TIMER_PCLK \
  253. (((CM3BBGET(RCC_BASE, RCC_TypeDef, CFGR3, _BI32(RCC_CFGR3_TIM8SW))) || \
  254. (CM3BBGET(RCC_BASE, RCC_TypeDef, CFGR, _BI32(RCC_CFGR_PPRE2_2))))? \
  255. (2 * NutClockGet(NUT_HWCLK_PCLK2)) : (NutClockGet(NUT_HWCLK_PCLK2)))
  256. #else
  257. #define STM32TIMER_PCLK \
  258. ((CM3BBGET(RCC_BASE, RCC_TypeDef, CFGR, _BI32(RCC_CFGR_PPRE2_2)))? \
  259. (2 * NutClockGet(NUT_HWCLK_PCLK2)) : (NutClockGet(NUT_HWCLK_PCLK2)))
  260. #endif
  261. #define STM32TIMER_BDTR
  262. #if defined(MCU_STM32F3)
  263. #define STM32TIMER_NCH 6
  264. #else
  265. #define STM32TIMER_NCH 4
  266. #endif
  267. #if defined(MCU_STM32L1) || defined(MCU_STM32F2) ||defined(MCU_STM32F4)
  268. #define STM32TIMER_AF(port, pin) 3
  269. #elif defined(MCU_STM32F3)
  270. /* FIXME: This doesn't map TIM*_BKIN2 */
  271. #define STM32TIMER_AF(port, pin) ((port == GPIOC_BASE) || (port == GPIOD_BASE))? 4 : \
  272. (( port == GPIOA_BASE) && (pin == 0))? 9 : \
  273. (( port == GPIOA_BASE) && (pin == 10))? 11 : \
  274. (( port == GPIOA_BASE) && (pin == 15))? 2 : \
  275. (( port == GPIOB_BASE) && (pin == 5))? 3 : \
  276. (( port == GPIOA_BASE) && (pin == 14))? 5 : \
  277. (( port == GPIOB_BASE) && (pin == 6))? 5 : \
  278. (( port == GPIOB_BASE) && (pin == 7))? 5 : \
  279. (( port == GPIOB_BASE) && ((pin == 8) || (pin == 9)))? 10 : 4
  280. #endif
  281. #elif defined (RCC_APB2ENR_TIM9EN) && defined(RCC_APB2RSTR_TIM9RST) && (STM32TIMER_ID == 9 )
  282. #define STM32TIMER_BASE TIM9_BASE
  283. #define STM32TIMER_SIG sig_TIM9
  284. #define STM32TIMER_CLK() CM3BBSET(RCC_BASE, RCC_TypeDef, APB2ENR, _BI32(RCC_APB2ENR_TIM9EN))
  285. #define STM32TIMER_RST() CM3BBPULSE(RCC_BASE, RCC_TypeDef, APB2RSTR, _BI32(RCC_APB2RSTR_TIM9RST))
  286. #define STM32TIMER_PCLK \
  287. ((CM3BBGET(RCC_BASE, RCC_TypeDef, CFGR, _BI32(RCC_CFGR_PPRE2_2)))? \
  288. (2 * NutClockGet(NUT_HWCLK_PCLK2)) : (NutClockGet(NUT_HWCLK_PCLK2)))
  289. #define STM32TIMER_NCH 2
  290. #if defined(MCU_STM32L1) || defined(MCU_STM32F2) ||defined(MCU_STM32F4)
  291. #define STM32TIMER_AF(port, pin) 4
  292. #endif
  293. #elif defined (RCC_APB2ENR_TIM10EN) && defined(RCC_APB2RSTR_TIM10RST) && (STM32TIMER_ID == 10 )
  294. #define STM32TIMER_BASE TIM10_BASE
  295. #define STM32TIMER_SIG sig_TIM10
  296. #define STM32TIMER_CLK() CM3BBSET(RCC_BASE, RCC_TypeDef, APB2ENR, _BI32(RCC_APB2ENR_TIM10EN))
  297. #define STM32TIMER_RST() CM3BBPULSE(RCC_BASE, RCC_TypeDef, APB2RSTR, _BI32(RCC_APB2RSTR_TIM10RST))
  298. #define STM32TIMER_PCLK \
  299. ((CM3BBGET(RCC_BASE, RCC_TypeDef, CFGR, _BI32(RCC_CFGR_PPRE2_2)))? \
  300. (2 * NutClockGet(NUT_HWCLK_PCLK2)) : (NutClockGet(NUT_HWCLK_PCLK2)))
  301. #define STM32TIMER_NCH 1
  302. #if defined(MCU_STM32L1) || defined(MCU_STM32F2) ||defined(MCU_STM32F4)
  303. #define STM32TIMER_AF(port, pin) 3
  304. #endif
  305. #elif defined (RCC_APB2ENR_TIM11EN) && defined(RCC_APB2RSTR_TIM11RST) && (STM32TIMER_ID == 11 )
  306. #define STM32TIMER_BASE TIM11_BASE
  307. #define STM32TIMER_SIG sig_TIM11
  308. #define STM32TIMER_CLK() CM3BBSET(RCC_BASE, RCC_TypeDef, APB2ENR, _BI32(RCC_APB2ENR_TIM11EN))
  309. #define STM32TIMER_RST() CM3BBPULSE(RCC_BASE, RCC_TypeDef, APB2RSTR, _BI32(RCC_APB2RSTR_TIM11RST))
  310. #define STM32TIMER_PCLK \
  311. ((CM3BBGET(RCC_BASE, RCC_TypeDef, CFGR, _BI32(RCC_CFGR_PPRE2_2)))? \
  312. (2 * NutClockGet(NUT_HWCLK_PCLK2)) : (NutClockGet(NUT_HWCLK_PCLK2)))
  313. #define STM32TIMER_NCH 1
  314. #if defined(MCU_STM32L1) || defined(MCU_STM32F2) ||defined(MCU_STM32F4)
  315. #define STM32TIMER_AF(port, pin) 3
  316. #endif
  317. #elif defined (RCC_APB1ENR_TIM12EN) && defined(RCC_APB1RSTR_TIM12RST) && (STM32TIMER_ID == 12)
  318. #define STM32TIMER_BASE TIM12_BASE
  319. #define STM32TIMER_SIG sig_TIM12
  320. #define STM32TIMER_CLK() CM3BBSET(RCC_BASE, RCC_TypeDef, APB1ENR, _BI32(RCC_APB1ENR_TIM12EN))
  321. #define STM32TIMER_RST() CM3BBPULSE(RCC_BASE, RCC_TypeDef, APB1RSTR, _BI32(RCC_APB1RSTR_TIM12RST))
  322. #define STM32TIMER_PCLK \
  323. ((CM3BBGET(RCC_BASE, RCC_TypeDef, CFGR, _BI32(RCC_CFGR_PPRE1_2)))? \
  324. (2 * NutClockGet(NUT_HWCLK_PCLK1)) : (NutClockGet(NUT_HWCLK_PCLK1)))
  325. #define STM32TIMER_NCH 2
  326. #if defined(MCU_STM32L1) || defined(MCU_STM32F2) ||defined(MCU_STM32F4)
  327. #define STM32TIMER_AF(port, pin) 9
  328. #endif
  329. #elif defined (RCC_APB1ENR_TIM13EN) && defined(RCC_APB1RSTR_TIM13RST) && (STM32TIMER_ID == 13 )
  330. #define STM32TIMER_BASE TIM13_BASE
  331. #define STM32TIMER_SIG sig_TIM13
  332. #define STM32TIMER_CLK() CM3BBSET(RCC_BASE, RCC_TypeDef, APB1ENR, _BI32(RCC_APB1ENR_TIM13EN))
  333. #define STM32TIMER_RST() CM3BBPULSE(RCC_BASE, RCC_TypeDef, APB1RSTR, _BI32(RCC_APB1RSTR_TIM13RST))
  334. #define STM32TIMER_PCLK \
  335. ((CM3BBGET(RCC_BASE, RCC_TypeDef, CFGR, _BI32(RCC_CFGR_PPRE1_2)))? \
  336. (2 * NutClockGet(NUT_HWCLK_PCLK1)) : (NutClockGet(NUT_HWCLK_PCLK1)))
  337. #define STM32TIMER_NCH 1
  338. #if defined(MCU_STM32L1) || defined(MCU_STM32F2) ||defined(MCU_STM32F4)
  339. #define STM32TIMER_AF(port, pin) 9
  340. #endif
  341. #elif defined (RCC_APB1ENR_TIM14EN) && defined(RCC_APB1RSTR_TIM14RST) && (STM32TIMER_ID == 14 )
  342. #define STM32TIMER_BASE TIM14_BASE
  343. #define STM32TIMER_SIG sig_TIM14
  344. #define STM32TIMER_CLK() CM3BBSET(RCC_BASE, RCC_TypeDef, APB1ENR, _BI32(RCC_APB1ENR_TIM14EN))
  345. #define STM32TIMER_RST() CM3BBPULSE(RCC_BASE, RCC_TypeDef, APB1RSTR, _BI32(RCC_APB1RSTR_TIM14RST))
  346. #define STM32TIMER_PCLK \
  347. ((CM3BBGET(RCC_BASE, RCC_TypeDef, CFGR, _BI32(RCC_CFGR_PPRE1_2)))? \
  348. (2 * NutClockGet(NUT_HWCLK_PCLK1)) : (NutClockGet(NUT_HWCLK_PCLK1)))
  349. #define STM32TIMER_NCH 1
  350. #if defined(MCU_STM32L1) || defined(MCU_STM32F2) ||defined(MCU_STM32F4)
  351. #define STM32TIMER_AF(port, pin) 9
  352. #elif defined(STM32F37X)
  353. #define STM32TIMER_AF(port, pin) (port == GPIOF_BASE)? 2: 9
  354. #else
  355. #warning Illegal AF for Tim14
  356. #endif
  357. #elif defined (RCC_APB2ENR_TIM15EN) && defined(RCC_APB2RSTR_TIM15RST) && (STM32TIMER_ID == 15 )
  358. #define STM32TIMER_BASE TIM15_BASE
  359. #define STM32TIMER_SIG sig_TIM15
  360. #define STM32TIMER_CLK() CM3BBSET(RCC_BASE, RCC_TypeDef, APB2ENR, _BI32(RCC_APB2ENR_TIM15EN))
  361. #define STM32TIMER_RST() CM3BBPULSE(RCC_BASE, RCC_TypeDef, APB2RSTR, _BI32(RCC_APB2RSTR_TIM15RST))
  362. #define STM32TIMER_PCLK \
  363. ((CM3BBGET(RCC_BASE, RCC_TypeDef, CFGR, _BI32(RCC_CFGR_PPRE2_2)))? \
  364. (2 * NutClockGet(NUT_HWCLK_PCLK2)) : (NutClockGet(NUT_HWCLK_PCLK2)))
  365. #define STM32TIMER_NCH 2
  366. #define STM32TIMER_BDTR
  367. /* F3: PA1/CH1N/AF9, PA2/CH1/AF9, PA3/CH2/AF9 PA9/BKIN/AF9
  368. F30: PB14/CH1/AF1, PB15/CH2/AF1, PB15/CH1_N/AF2, PF9/CH1/AF3, PF10/CH2/AF3
  369. F37: PB4/CH1_N/AF9, PB6/CH1/AF9, PB7/CH1/AF9, PB14/CH1/AF1, PB15/CH2/AF1
  370. FIXME: This doesn't map TIM15 CH1N
  371. */
  372. #if defined(STM32F30X)
  373. #define STM32TIMER_AF(port, pin) (port == GPIOF_BASE)? 3 : (port == GPIOB_BASE)? 1 : 9
  374. #endif
  375. #if defined(STM32F37X)
  376. #define STM32TIMER_AF(port, pin) ((port == GPIOB_BASE) && ((pin == 14) || (pin == 14)))? 1 : 9
  377. #endif
  378. #elif defined (RCC_APB2ENR_TIM16EN) && defined(RCC_APB2RSTR_TIM16RST) && (STM32TIMER_ID == 16 )
  379. #define STM32TIMER_BASE TIM16_BASE
  380. #define STM32TIMER_SIG sig_TIM16
  381. #define STM32TIMER_CLK() CM3BBSET(RCC_BASE, RCC_TypeDef, APB2ENR, _BI32(RCC_APB2ENR_TIM16EN))
  382. #define STM32TIMER_RST() CM3BBPULSE(RCC_BASE, RCC_TypeDef, APB2RSTR, _BI32(RCC_APB2RSTR_TIM16RST))
  383. #define STM32TIMER_PCLK \
  384. ((CM3BBGET(RCC_BASE, RCC_TypeDef, CFGR, _BI32(RCC_CFGR_PPRE2_2)))? \
  385. (2 * NutClockGet(NUT_HWCLK_PCLK2)) : (NutClockGet(NUT_HWCLK_PCLK2)))
  386. #define STM32TIMER_NCH 1
  387. #define STM32TIMER_BDTR
  388. #if defined(MCU_STM32F3)
  389. #define STM32TIMER_AF(port, pin) (port == GPIOE_BASE)? 4 : 1
  390. #endif
  391. #elif defined (RCC_APB2ENR_TIM17EN) && defined(RCC_APB2RSTR_TIM17RST) && (STM32TIMER_ID == 17 )
  392. #define STM32TIMER_BASE TIM17_BASE
  393. #define STM32TIMER_SIG sig_TIM17
  394. #define STM32TIMER_CLK() CM3BBSET(RCC_BASE, RCC_TypeDef, APB2ENR, _BI32(RCC_APB2ENR_TIM17EN))
  395. #define STM32TIMER_RST() CM3BBPULSE(RCC_BASE, RCC_TypeDef, APB2RSTR, _BI32(RCC_APB2RSTR_TIM17RST))
  396. #define STM32TIMER_PCLK \
  397. ((CM3BBGET(RCC_BASE, RCC_TypeDef, CFGR, _BI32(RCC_CFGR_PPRE2_2)))? \
  398. (2 * NutClockGet(NUT_HWCLK_PCLK2)) : (NutClockGet(NUT_HWCLK_PCLK2)))
  399. #define STM32TIMER_NCH 1
  400. #define STM32TIMER_BDTR
  401. #if defined(STM32F30X)
  402. #define STM32TIMER_AF(port, pin) (port == GPIOE_BASE)? 4 : \
  403. ( port == GPIOA_BASE)? 1 : \
  404. (( port == GPIOB_BASE) && ((pin == 7) || (pin == 9)))? 1 : \
  405. (( port == GPIOA_BASE) && ((pin == 4) || (pin == 5)))? 10 : 1
  406. #elif defined(STM32F37X)
  407. #define STM32TIMER_AF(port, pin) ( port == GPIOA_BASE) ? 1 : \
  408. (( port == GPIOB_BASE) && ((pin == 7) || (pin == 9)))? 1 : \
  409. (( port == GPIOB_BASE) && ((pin == 4) || (pin == 5)))? 10 : 1
  410. #endif
  411. #elif defined (RCC_APB2ENR_TIM18EN) && defined(RCC_APB2RSTR_TIM18RST) && (STM32TIMER_ID == 18 )
  412. #define STM32TIMER_BASE TIM18_BASE
  413. #define STM32TIMER_SIG sig_TIM18
  414. #define STM32TIMER_CLK() CM3BBSET(RCC_BASE, RCC_TypeDef, APB1ENR, _BI32(RCC_APB1ENR_TIM18EN))
  415. #define STM32TIMER_RST() CM3BBPULSE(RCC_BASE, RCC_TypeDef, APB1RSTR, _BI32(RCC_APB1RSTR_TIM18RST))
  416. #define STM32TIMER_PCLK \
  417. ((CM3BBGET(RCC_BASE, RCC_TypeDef, CFGR, _BI32(RCC_CFGR_PPRE2_2)))? \
  418. (2 * NutClockGet(NUT_HWCLK_PCLK2)) : (NutClockGet(NUT_HWCLK_PCLK2)))
  419. #define STM32TIMER_NCH 0
  420. #elif defined (RCC_APB2ENR_TIM19EN) && defined(RCC_APB2RSTR_TIM19RST) && (STM32TIMER_ID == 19 )
  421. #define STM32TIMER_BASE TIM19_BASE
  422. #define STM32TIMER_SIG sig_TIM19
  423. #define STM32TIMER_CLK() CM3BBSET(RCC_BASE, RCC_TypeDef, APB2ENR, _BI32(RCC_APB2ENR_TIM19EN))
  424. #define STM32TIMER_RST() CM3BBPULSE(RCC_BASE, RCC_TypeDef, APB2RSTR, _BI32(RCC_APB2RSTR_TIM19RST))
  425. #define STM32TIMER_PCLK \
  426. ((CM3BBGET(RCC_BASE, RCC_TypeDef, CFGR, _BI32(RCC_CFGR_PPRE2_2)))? \
  427. (2 * NutClockGet(NUT_HWCLK_PCLK2)) : (NutClockGet(NUT_HWCLK_PCLK2)))
  428. #define STM32TIMER_NCH 4
  429. #define STM32TIMER_PCLK \
  430. ((CM3BBGET(RCC_BASE, RCC_TypeDef, CFGR, _BI32(RCC_CFGR_PPRE2_2)))? \
  431. (2 * NutClockGet(NUT_HWCLK_PCLK2)) : (NutClockGet(NUT_HWCLK_PCLK2)))
  432. #define STM32TIMER_AF(port, pin) (((port == GPIOC_BASE) || (port == GPIOD_BASE))? 2 : 11)
  433. #else
  434. #warning No match
  435. #endif
  436. /* Only few timers are 32 bit. Use this "catch all else" to reduce duplication*/
  437. #if !defined(STM32TIMER_WIDTH)
  438. #define STM32TIMER_WIDTH 16
  439. #endif