cpu_bsp.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /*
  2. *********************************************************************************************************
  3. * uC/CPU
  4. * CPU CONFIGURATION & PORT LAYER
  5. *
  6. * (c) Copyright 2004-2015; Micrium, Inc.; Weston, FL
  7. *
  8. * All rights reserved. Protected by international copyright laws.
  9. *
  10. * uC/CPU is provided in source form to registered licensees ONLY. It is
  11. * illegal to distribute this source code to any third party unless you receive
  12. * written permission by an authorized Micrium representative. Knowledge of
  13. * the source code may NOT be used to develop a similar product.
  14. *
  15. * Please help us continue to provide the Embedded community with the finest
  16. * software available. Your honesty is greatly appreciated.
  17. *
  18. * You can find our product's user manual, API reference, release notes and
  19. * more information at https://doc.micrium.com.
  20. * You can contact us at www.micrium.com.
  21. *********************************************************************************************************
  22. */
  23. /*
  24. *********************************************************************************************************
  25. *
  26. * CPU BOARD SUPPORT PACKAGE (BSP) FUNCTIONS
  27. *
  28. * TEMPLATE
  29. *
  30. * Filename : cpu_bsp.c
  31. * Version : V1.30.02
  32. * Programmer(s) : ITJ
  33. * JBL
  34. *********************************************************************************************************
  35. */
  36. /*
  37. *********************************************************************************************************
  38. * INCLUDE FILES
  39. *********************************************************************************************************
  40. */
  41. #define CPU_BSP_MODULE
  42. #include <cpu_core.h>
  43. #include <bsp.h>
  44. /*
  45. *********************************************************************************************************
  46. * REGISTERS
  47. *********************************************************************************************************
  48. */
  49. #define CPU_REG_DEM_CR (*(CPU_REG32 *)0xE000EDFC)
  50. #define CPU_REG_DWT_CR (*(CPU_REG32 *)0xE0001000)
  51. #define CPU_REG_DWT_CYCCNT (*(CPU_REG32 *)0xE0001004)
  52. #define CPU_REG_DBGMCU_CR (*(CPU_REG32 *)0xE0042004)
  53. /*
  54. *********************************************************************************************************
  55. * REGISTER BITS
  56. *********************************************************************************************************
  57. */
  58. #define CPU_DBGMCU_CR_TRACE_IOEN_MASK 0x10
  59. #define CPU_DBGMCU_CR_TRACE_MODE_ASYNC 0x00
  60. #define CPU_DBGMCU_CR_TRACE_MODE_SYNC_01 0x40
  61. #define CPU_DBGMCU_CR_TRACE_MODE_SYNC_02 0x80
  62. #define CPU_DBGMCU_CR_TRACE_MODE_SYNC_04 0xC0
  63. #define CPU_DBGMCU_CR_TRACE_MODE_MASK 0xC0
  64. #define CPU_BIT_DEM_CR_TRCENA DEF_BIT_24
  65. #define CPU_BIT_DWT_CR_CYCCNTENA DEF_BIT_00
  66. /*
  67. *********************************************************************************************************
  68. * LOCAL DEFINES
  69. *********************************************************************************************************
  70. */
  71. /*
  72. *********************************************************************************************************
  73. * LOCAL CONSTANTS
  74. *********************************************************************************************************
  75. */
  76. /*
  77. *********************************************************************************************************
  78. * LOCAL DATA TYPES
  79. *********************************************************************************************************
  80. */
  81. /*
  82. *********************************************************************************************************
  83. * LOCAL TABLES
  84. *********************************************************************************************************
  85. */
  86. /*
  87. *********************************************************************************************************
  88. * LOCAL GLOBAL VARIABLES
  89. *********************************************************************************************************
  90. */
  91. /*
  92. *********************************************************************************************************
  93. * LOCAL FUNCTION PROTOTYPES
  94. *********************************************************************************************************
  95. */
  96. /*
  97. *********************************************************************************************************
  98. * LOCAL CONFIGURATION ERRORS
  99. *********************************************************************************************************
  100. */
  101. /*
  102. *********************************************************************************************************
  103. * CPU_TS_TmrInit()
  104. *
  105. * Description : Initialize & start CPU timestamp timer.
  106. *
  107. * Argument(s) : none.
  108. *
  109. * Return(s) : none.
  110. *
  111. * Caller(s) : CPU_TS_Init().
  112. *
  113. * This function is an INTERNAL CPU module function & MUST be implemented by application/
  114. * BSP function(s) [see Note #1] but MUST NOT be called by application function(s).
  115. *
  116. * Note(s) : (1) CPU_TS_TmrInit() is an application/BSP function that MUST be defined by the developer
  117. * if either of the following CPU features is enabled :
  118. *
  119. * (a) CPU timestamps
  120. * (b) CPU interrupts disabled time measurements
  121. *
  122. * See 'cpu_cfg.h CPU TIMESTAMP CONFIGURATION Note #1'
  123. * & 'cpu_cfg.h CPU INTERRUPTS DISABLED TIME MEASUREMENT CONFIGURATION Note #1a'.
  124. *
  125. * (2) (a) Timer count values MUST be returned via word-size-configurable 'CPU_TS_TMR'
  126. * data type.
  127. *
  128. * (1) If timer has more bits, truncate timer values' higher-order bits greater
  129. * than the configured 'CPU_TS_TMR' timestamp timer data type word size.
  130. *
  131. * (2) Since the timer MUST NOT have less bits than the configured 'CPU_TS_TMR'
  132. * timestamp timer data type word size; 'CPU_CFG_TS_TMR_SIZE' MUST be
  133. * configured so that ALL bits in 'CPU_TS_TMR' data type are significant.
  134. *
  135. * In other words, if timer size is not a binary-multiple of 8-bit octets
  136. * (e.g. 20-bits or even 24-bits), then the next lower, binary-multiple
  137. * octet word size SHOULD be configured (e.g. to 16-bits). However, the
  138. * minimum supported word size for CPU timestamp timers is 8-bits.
  139. *
  140. * See also 'cpu_cfg.h CPU TIMESTAMP CONFIGURATION Note #2'
  141. * & 'cpu_core.h CPU TIMESTAMP DATA TYPES Note #1'.
  142. *
  143. * (b) Timer SHOULD be an 'up' counter whose values increase with each time count.
  144. *
  145. * (c) When applicable, timer period SHOULD be less than the typical measured time
  146. * but MUST be less than the maximum measured time; otherwise, timer resolution
  147. * inadequate to measure desired times.
  148. *
  149. * See also 'CPU_TS_TmrRd() Note #2'.
  150. *********************************************************************************************************
  151. */
  152. #if (CPU_CFG_TS_TMR_EN == DEF_ENABLED)
  153. void CPU_TS_TmrInit (void)
  154. {
  155. CPU_INT32U fclk_freq;
  156. fclk_freq = BSP_CPU_ClkFreq();
  157. CPU_REG_DEM_CR |= (CPU_INT32U)CPU_BIT_DEM_CR_TRCENA; /* Enable Cortex-M4's DWT CYCCNT reg. */
  158. CPU_REG_DWT_CYCCNT = (CPU_INT32U)0u;
  159. CPU_REG_DWT_CR |= (CPU_INT32U)CPU_BIT_DWT_CR_CYCCNTENA;
  160. CPU_TS_TmrFreqSet((CPU_TS_TMR_FREQ)fclk_freq);
  161. }
  162. #endif
  163. /*
  164. *********************************************************************************************************
  165. * CPU_TS_TmrRd()
  166. *
  167. * Description : Get current CPU timestamp timer count value.
  168. *
  169. * Argument(s) : none.
  170. *
  171. * Return(s) : Timestamp timer count (see Notes #2a & #2b).
  172. *
  173. * Caller(s) : CPU_TS_Init(),
  174. * CPU_TS_Get32(),
  175. * CPU_TS_Get64(),
  176. * CPU_IntDisMeasStart(),
  177. * CPU_IntDisMeasStop().
  178. *
  179. * This function is an INTERNAL CPU module function & MUST be implemented by application/
  180. * BSP function(s) [see Note #1] but SHOULD NOT be called by application function(s).
  181. *
  182. * Note(s) : (1) CPU_TS_TmrRd() is an application/BSP function that MUST be defined by the developer
  183. * if either of the following CPU features is enabled :
  184. *
  185. * (a) CPU timestamps
  186. * (b) CPU interrupts disabled time measurements
  187. *
  188. * See 'cpu_cfg.h CPU TIMESTAMP CONFIGURATION Note #1'
  189. * & 'cpu_cfg.h CPU INTERRUPTS DISABLED TIME MEASUREMENT CONFIGURATION Note #1a'.
  190. *
  191. * (2) (a) Timer count values MUST be returned via word-size-configurable 'CPU_TS_TMR'
  192. * data type.
  193. *
  194. * (1) If timer has more bits, truncate timer values' higher-order bits greater
  195. * than the configured 'CPU_TS_TMR' timestamp timer data type word size.
  196. *
  197. * (2) Since the timer MUST NOT have less bits than the configured 'CPU_TS_TMR'
  198. * timestamp timer data type word size; 'CPU_CFG_TS_TMR_SIZE' MUST be
  199. * configured so that ALL bits in 'CPU_TS_TMR' data type are significant.
  200. *
  201. * In other words, if timer size is not a binary-multiple of 8-bit octets
  202. * (e.g. 20-bits or even 24-bits), then the next lower, binary-multiple
  203. * octet word size SHOULD be configured (e.g. to 16-bits). However, the
  204. * minimum supported word size for CPU timestamp timers is 8-bits.
  205. *
  206. * See also 'cpu_cfg.h CPU TIMESTAMP CONFIGURATION Note #2'
  207. * & 'cpu_core.h CPU TIMESTAMP DATA TYPES Note #1'.
  208. *
  209. * (b) Timer SHOULD be an 'up' counter whose values increase with each time count.
  210. *
  211. * (1) If timer is a 'down' counter whose values decrease with each time count,
  212. * then the returned timer value MUST be ones-complemented.
  213. *
  214. * (c) (1) When applicable, the amount of time measured by CPU timestamps is
  215. * calculated by either of the following equations :
  216. *
  217. * (A) Time measured = Number timer counts * Timer period
  218. *
  219. * where
  220. *
  221. * Number timer counts Number of timer counts measured
  222. * Timer period Timer's period in some units of
  223. * (fractional) seconds
  224. * Time measured Amount of time measured, in same
  225. * units of (fractional) seconds
  226. * as the Timer period
  227. *
  228. * Number timer counts
  229. * (B) Time measured = ---------------------
  230. * Timer frequency
  231. *
  232. * where
  233. *
  234. * Number timer counts Number of timer counts measured
  235. * Timer frequency Timer's frequency in some units
  236. * of counts per second
  237. * Time measured Amount of time measured, in seconds
  238. *
  239. * (2) Timer period SHOULD be less than the typical measured time but MUST be less
  240. * than the maximum measured time; otherwise, timer resolution inadequate to
  241. * measure desired times.
  242. *********************************************************************************************************
  243. */
  244. #if (CPU_CFG_TS_TMR_EN == DEF_ENABLED)
  245. CPU_TS_TMR CPU_TS_TmrRd (void)
  246. {
  247. CPU_TS_TMR ts_tmr_cnts;
  248. ts_tmr_cnts = (CPU_TS_TMR)CPU_REG_DWT_CYCCNT;
  249. return (ts_tmr_cnts);
  250. }
  251. #endif
  252. /*
  253. *********************************************************************************************************
  254. * CPU_TSxx_to_uSec()
  255. *
  256. * Description : Convert a 32-/64-bit CPU timestamp from timer counts to microseconds.
  257. *
  258. * Argument(s) : ts_cnts CPU timestamp (in timestamp timer counts [see Note #2aA]).
  259. *
  260. * Return(s) : Converted CPU timestamp (in microseconds [see Note #2aD]).
  261. *
  262. * Caller(s) : Application.
  263. *
  264. * This function is an (optional) CPU module application programming interface (API)
  265. * function which MAY be implemented by application/BSP function(s) [see Note #1] &
  266. * MAY be called by application function(s).
  267. *
  268. * Note(s) : (1) CPU_TS32_to_uSec()/CPU_TS64_to_uSec() are application/BSP functions that MAY be
  269. * optionally defined by the developer when either of the following CPU features is
  270. * enabled :
  271. *
  272. * (a) CPU timestamps
  273. * (b) CPU interrupts disabled time measurements
  274. *
  275. * See 'cpu_cfg.h CPU TIMESTAMP CONFIGURATION Note #1'
  276. * & 'cpu_cfg.h CPU INTERRUPTS DISABLED TIME MEASUREMENT CONFIGURATION Note #1a'.
  277. *
  278. * (2) (a) The amount of time measured by CPU timestamps is calculated by either of
  279. * the following equations :
  280. *
  281. * 10^6 microseconds
  282. * (1) Time measured = Number timer counts * ------------------- * Timer period
  283. * 1 second
  284. *
  285. * Number timer counts 10^6 microseconds
  286. * (2) Time measured = --------------------- * -------------------
  287. * Timer frequency 1 second
  288. *
  289. * where
  290. *
  291. * (A) Number timer counts Number of timer counts measured
  292. * (B) Timer frequency Timer's frequency in some units
  293. * of counts per second
  294. * (C) Timer period Timer's period in some units of
  295. * (fractional) seconds
  296. * (D) Time measured Amount of time measured,
  297. * in microseconds
  298. *
  299. * (b) Timer period SHOULD be less than the typical measured time but MUST be less
  300. * than the maximum measured time; otherwise, timer resolution inadequate to
  301. * measure desired times.
  302. *
  303. * (c) Specific implementations may convert any number of CPU_TS32 or CPU_TS64 bits
  304. * -- up to 32 or 64, respectively -- into microseconds.
  305. *********************************************************************************************************
  306. */
  307. #if (CPU_CFG_TS_32_EN == DEF_ENABLED)
  308. CPU_INT64U CPU_TS32_to_uSec (CPU_TS32 ts_cnts)
  309. {
  310. CPU_INT64U ts_us;
  311. CPU_INT64U fclk_freq;
  312. fclk_freq = BSP_CPU_ClkFreq();
  313. ts_us = ts_cnts / (fclk_freq / DEF_TIME_NBR_uS_PER_SEC);
  314. return (ts_us);
  315. }
  316. #endif
  317. #if (CPU_CFG_TS_64_EN == DEF_ENABLED)
  318. CPU_INT64U CPU_TS64_to_uSec (CPU_TS64 ts_cnts)
  319. {
  320. CPU_INT64U ts_us;
  321. CPU_INT64U fclk_freq;
  322. fclk_freq = BSP_CPU_ClkFreq();
  323. ts_us = ts_cnts / (fclk_freq / DEF_TIME_NBR_uS_PER_SEC);
  324. return (ts_us);
  325. }
  326. #endif