hal_lld.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. SPC5 HAL - Copyright (C) 2013 STMicroelectronics
  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 SPC563Mxx/hal_lld.h
  15. * @brief SPC563Mxx 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. * - SPC5_XOSC_CLK.
  19. * .
  20. *
  21. * @addtogroup HAL
  22. * @{
  23. */
  24. #ifndef HAL_LLD_H
  25. #define HAL_LLD_H
  26. #include "registers.h"
  27. #include "spc5_registry.h"
  28. /*===========================================================================*/
  29. /* Driver constants. */
  30. /*===========================================================================*/
  31. /**
  32. * @brief Defines the support for realtime counters in the HAL.
  33. */
  34. #define HAL_IMPLEMENTS_COUNTERS TRUE
  35. /**
  36. * @brief Platform name.
  37. */
  38. #define PLATFORM_NAME "SPC563Mxx Powertrain"
  39. /**
  40. * @name ESYNCR2 register definitions
  41. * @{
  42. */
  43. #define SPC5_RFD_DIV2 0 /**< Divide VCO frequency by 2. */
  44. #define SPC5_RFD_DIV4 1 /**< Divide VCO frequency by 4. */
  45. #define SPC5_RFD_DIV8 2 /**< Divide VCO frequency by 8. */
  46. #define SPC5_RFD_DIV16 3 /**< Divide VCO frequency by 16.*/
  47. /** @} */
  48. /**
  49. * @name BIUCR register definitions
  50. * @{
  51. */
  52. #define BIUCR_BANK1_TOO 0x01000000 /**< Use settings for bank1 too.*/
  53. #define BIUCR_MASTER7_PREFETCH 0x00800000 /**< Enable master 7 prefetch. */
  54. #define BIUCR_MASTER6_PREFETCH 0x00400000 /**< Enable master 6 prefetch. */
  55. #define BIUCR_MASTER5_PREFETCH 0x00200000 /**< Enable master 5 prefetch. */
  56. #define BIUCR_MASTER4_PREFETCH 0x00100000 /**< Enable master 4 prefetch. */
  57. #define BIUCR_MASTER3_PREFETCH 0x00080000 /**< Enable master 3 prefetch. */
  58. #define BIUCR_MASTER2_PREFETCH 0x00040000 /**< Enable master 2 prefetch. */
  59. #define BIUCR_MASTER1_PREFETCH 0x00020000 /**< Enable master 1 prefetch. */
  60. #define BIUCR_MASTER0_PREFETCH 0x00010000 /**< Enable master 0 prefetch. */
  61. #define BIUCR_APC_MASK 0x0000E000 /**< APC field mask. */
  62. #define BIUCR_APC_0 (0 << 13) /**< No additional hold cycles. */
  63. #define BIUCR_APC_1 (1 << 13) /**< 1 additional hold cycle. */
  64. #define BIUCR_APC_2 (2 << 13) /**< 2 additional hold cycles. */
  65. #define BIUCR_APC_3 (3 << 13) /**< 3 additional hold cycles. */
  66. #define BIUCR_APC_4 (4 << 13) /**< 4 additional hold cycles. */
  67. #define BIUCR_APC_5 (5 << 13) /**< 5 additional hold cycles. */
  68. #define BIUCR_APC_6 (6 << 13) /**< 6 additional hold cycles. */
  69. #define BIUCR_WWSC_MASK 0x00001800 /**< WWSC field mask. */
  70. #define BIUCR_WWSC_0 (0 << 11) /**< No write wait states. */
  71. #define BIUCR_WWSC_1 (1 << 11) /**< 1 write wait state. */
  72. #define BIUCR_WWSC_2 (2 << 11) /**< 2 write wait states. */
  73. #define BIUCR_WWSC_3 (3 << 11) /**< 3 write wait states. */
  74. #define BIUCR_RWSC_MASK 0x00001800 /**< RWSC field mask. */
  75. #define BIUCR_RWSC_0 (0 << 8) /**< No read wait states. */
  76. #define BIUCR_RWSC_1 (1 << 8) /**< 1 read wait state. */
  77. #define BIUCR_RWSC_2 (2 << 8) /**< 2 read wait states. */
  78. #define BIUCR_RWSC_3 (3 << 8) /**< 3 read wait states. */
  79. #define BIUCR_RWSC_4 (4 << 8) /**< 4 read wait states. */
  80. #define BIUCR_RWSC_5 (5 << 8) /**< 5 read wait states. */
  81. #define BIUCR_RWSC_6 (6 << 8) /**< 6 read wait states. */
  82. #define BIUCR_RWSC_7 (7 << 8) /**< 7 read wait states. */
  83. #define BIUCR_DPFEN 0x00000040 /**< Data prefetch enable. */
  84. #define BIUCR_IPFEN 0x00000010 /**< Instr. prefetch enable. */
  85. #define BIUCR_PFLIM_MASK 0x00000060 /**< PFLIM field mask. */
  86. #define BIUCR_PFLIM_NO (0 << 1) /**< No prefetching. */
  87. #define BIUCR_PFLIM_ON_MISS (1 << 1) /**< Prefetch on miss. */
  88. #define BIUCR_PFLIM_ON_HITMISS (2 << 1) /**< Prefetch on hit and miss. */
  89. #define BIUCR_BFEN 0x00000001 /**< Flash buffering enable. */
  90. /** @} */
  91. /*===========================================================================*/
  92. /* Driver pre-compile time settings. */
  93. /*===========================================================================*/
  94. /**
  95. * @brief Disables the clocks initialization in the HAL.
  96. */
  97. #if !defined(SPC5_NO_INIT) || defined(__DOXYGEN__)
  98. #define SPC5_NO_INIT FALSE
  99. #endif
  100. /**
  101. * @brief Clock bypass.
  102. * @note If set to @p TRUE then the PLL is not started and initialized, the
  103. * external clock is used as-is and the other clock-related settings
  104. * are ignored.
  105. */
  106. #if !defined(SPC5_CLK_BYPASS) || defined(__DOXYGEN__)
  107. #define SPC5_CLK_BYPASS FALSE
  108. #endif
  109. /**
  110. * @brief Disables the overclock checks.
  111. */
  112. #if !defined(SPC5_ALLOW_OVERCLOCK) || defined(__DOXYGEN__)
  113. #define SPC5_ALLOW_OVERCLOCK FALSE
  114. #endif
  115. /**
  116. * @brief External clock pre-divider.
  117. * @note Must be in range 1...15.
  118. */
  119. #if !defined(SPC5_CLK_PREDIV_VALUE) || defined(__DOXYGEN__)
  120. #define SPC5_CLK_PREDIV_VALUE 2
  121. #endif
  122. /**
  123. * @brief Multiplication factor divider.
  124. * @note Must be in range 32...96.
  125. */
  126. #if !defined(SPC5_CLK_MFD_VALUE) || defined(__DOXYGEN__)
  127. #define SPC5_CLK_MFD_VALUE 80
  128. #endif
  129. /**
  130. * @brief Reduced frequency divider.
  131. */
  132. #if !defined(SPC5_CLK_RFD) || defined(__DOXYGEN__)
  133. #define SPC5_CLK_RFD RFD_DIV4
  134. #endif
  135. /**
  136. * @brief Flash buffer and prefetching settings.
  137. * @note Please refer to the SPC563Mxx reference manual about the meaning
  138. * of the following bits, if in doubt DO NOT MODIFY IT.
  139. * @note Do not specify the APC, WWSC, RWSC bits in this value because
  140. * those are calculated from the system clock and ORed with this
  141. * value.
  142. */
  143. #if !defined(SPC5_FLASH_BIUCR) || defined(__DOXYGEN__)
  144. #define SPC5_FLASH_BIUCR (BIUCR_BANK1_TOO | \
  145. BIUCR_MASTER4_PREFETCH | \
  146. BIUCR_MASTER0_PREFETCH | \
  147. BIUCR_DPFEN | \
  148. BIUCR_IPFEN | \
  149. BIUCR_PFLIM_ON_MISS | \
  150. BIUCR_BFEN)
  151. #endif
  152. /**
  153. * @brief eMIOS global prescaler value.
  154. */
  155. #if !defined(SPC5_EMIOS_GPRE_VALUE) || defined(__DOXYGEN__)
  156. #define SPC5_EMIOS_GPRE_VALUE 20
  157. #endif
  158. /*===========================================================================*/
  159. /* Derived constants and error checks. */
  160. /*===========================================================================*/
  161. /*
  162. * Configuration-related checks.
  163. */
  164. #if !defined(SPC563Mxx_MCUCONF)
  165. #error "Using a wrong mcuconf.h file, SPC563Mxx_MCUCONF not defined"
  166. #endif
  167. #if (SPC5_CLK_PREDIV_VALUE < 1) || (SPC5_CLK_PREDIV_VALUE > 15)
  168. #error "invalid SPC5_CLK_PREDIV_VALUE value specified"
  169. #endif
  170. #if (SPC5_CLK_MFD_VALUE < 32) || (SPC5_CLK_MFD_VALUE > 96)
  171. #error "invalid SPC5_CLK_MFD_VALUE value specified"
  172. #endif
  173. #if (SPC5_CLK_RFD != SPC5_RFD_DIV2) && (SPC5_CLK_RFD != SPC5_RFD_DIV4) && \
  174. (SPC5_CLK_RFD != SPC5_RFD_DIV8) && (SPC5_CLK_RFD != SPC5_RFD_DIV16)
  175. #error "invalid SPC5_CLK_RFD value specified"
  176. #endif
  177. #if (SPC5_EMIOS_GPRE_VALUE < 1) || (SPC5_EMIOS_GPRE_VALUE > 256)
  178. #error "invalid SPC5_EMIOS_GPRE_VALUE value specified"
  179. #endif
  180. /**
  181. * @brief PLL input divider.
  182. */
  183. #define SPC5_CLK_PREDIV (SPC5_CLK_PREDIV_VALUE - 1)
  184. /**
  185. * @brief PLL multiplier.
  186. */
  187. #define SPC5_CLK_MFD (SPC5_CLK_MFD_VALUE)
  188. /**
  189. * @brief PLL output clock.
  190. */
  191. #define SPC5_PLLCLK ((SPC5_XOSC_CLK / SPC5_CLK_PREDIV_VALUE) * \
  192. SPC5_CLK_MFD_VALUE)
  193. #if (SPC5_PLLCLK < 256000000) || (SPC5_PLLCLK > 512000000)
  194. #error "VCO frequency out of the acceptable range (256...512)"
  195. #endif
  196. /**
  197. * @brief PLL output clock.
  198. */
  199. #if !SPC5_CLK_BYPASS || defined(__DOXYGEN__)
  200. #define SPC5_SYSCLK (SPC5_PLLCLK / (1 << (SPC5_CLK_RFD + 1)))
  201. #else
  202. #define SPC5_SYSCLK SPC5_XOSC_CLK
  203. #endif
  204. #if (SPC5_SYSCLK > 80000000) && !SPC5_ALLOW_OVERCLOCK
  205. #error "System clock above maximum rated frequency (80MHz)"
  206. #endif
  207. /**
  208. * @brief Flash wait states are a function of the system clock.
  209. */
  210. #if (SPC5_SYSCLK <= 20000000) || defined(__DOXYGEN__)
  211. #define SPC5_FLASH_WS (BIUCR_APC_0 | BIUCR_RWSC_0 | BIUCR_WWSC_1)
  212. #elif SPC5_SYSCLK <= 40000000
  213. #define SPC5_FLASH_WS (BIUCR_APC_1 | BIUCR_RWSC_1 | BIUCR_WWSC_1)
  214. #elif SPC5_SYSCLK <= 64000000
  215. #define SPC5_FLASH_WS (BIUCR_APC_2 | BIUCR_RWSC_2 | BIUCR_WWSC_1)
  216. #else
  217. #define SPC5_FLASH_WS (BIUCR_APC_3 | BIUCR_RWSC_3 | BIUCR_WWSC_1)
  218. #endif
  219. /**
  220. * @brief eMIOS global prescaler setting.
  221. */
  222. #define SPC5_EMIOS_GPRE (SPC5_EMIOS_GPRE_VALUE << 8)
  223. /*===========================================================================*/
  224. /* Driver data structures and types. */
  225. /*===========================================================================*/
  226. /**
  227. * @brief Type representing a system clock frequency.
  228. */
  229. typedef uint32_t halclock_t;
  230. /**
  231. * @brief Type of the realtime free counter value.
  232. */
  233. typedef uint32_t halrtcnt_t;
  234. /*===========================================================================*/
  235. /* Driver macros. */
  236. /*===========================================================================*/
  237. /**
  238. * @brief Returns the current value of the system free running counter.
  239. * @note This service is implemented by returning the content of the
  240. * TBL register.
  241. *
  242. * @return The value of the system free running counter of
  243. * type halrtcnt_t.
  244. *
  245. * @notapi
  246. */
  247. static inline
  248. halrtcnt_t hal_lld_get_counter_value(void) {
  249. halrtcnt_t cnt;
  250. asm volatile ("mfspr %[cnt], 284" : [cnt] "=r" (cnt) : : );
  251. return cnt;
  252. }
  253. /**
  254. * @brief Realtime counter frequency.
  255. * @note The TBL register is incremented directly by the system
  256. * clock so this function returns SPC5_SYSCLK.
  257. *
  258. * @return The realtime counter frequency of type halclock_t.
  259. *
  260. * @notapi
  261. */
  262. #define hal_lld_get_counter_frequency() SPC5_SYSCLK
  263. /*===========================================================================*/
  264. /* External declarations. */
  265. /*===========================================================================*/
  266. #include "spc5_edma.h"
  267. #ifdef __cplusplus
  268. extern "C" {
  269. #endif
  270. void hal_lld_init(void);
  271. void spc_clock_init(void);
  272. #ifdef __cplusplus
  273. }
  274. #endif
  275. #endif /* HAL_LLD_H */
  276. /** @} */