cpu_core.c 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551
  1. /*
  2. *********************************************************************************************************
  3. * uC/CPU
  4. * CPU CONFIGURATION & PORT LAYER
  5. *
  6. * (c) Copyright 2004-2011; 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 contact us at www.micrium.com.
  19. *********************************************************************************************************
  20. */
  21. /*
  22. *********************************************************************************************************
  23. *
  24. * CORE CPU MODULE
  25. *
  26. * Filename : cpu_core.c
  27. * Version : V1.28.01
  28. * Programmer(s) : SR
  29. * ITJ
  30. *********************************************************************************************************
  31. */
  32. /*
  33. *********************************************************************************************************
  34. * INCLUDE FILES
  35. *********************************************************************************************************
  36. */
  37. #define CPU_CORE_MODULE
  38. #include <cpu_core.h>
  39. /*$PAGE*/
  40. /*
  41. *********************************************************************************************************
  42. * LOCAL DEFINES
  43. *********************************************************************************************************
  44. */
  45. /*
  46. *********************************************************************************************************
  47. * LOCAL CONSTANTS
  48. *********************************************************************************************************
  49. */
  50. /*
  51. *********************************************************************************************************
  52. * LOCAL DATA TYPES
  53. *********************************************************************************************************
  54. */
  55. /*
  56. *********************************************************************************************************
  57. * LOCAL TABLES
  58. *********************************************************************************************************
  59. */
  60. /*
  61. *********************************************************************************************************
  62. * CPU COUNT LEAD ZEROs LOOKUP TABLE
  63. *
  64. * Note(s) : (1) Index into bit pattern table determines the number of leading zeros in an 8-bit value :
  65. *
  66. * b07 b06 b05 b04 b03 b02 b01 b00 # Leading Zeros
  67. * --- --- --- --- --- --- --- --- ---------------
  68. * 1 x x x x x x x 0
  69. * 0 1 x x x x x x 1
  70. * 0 0 1 x x x x x 2
  71. * 0 0 0 1 x x x x 3
  72. * 0 0 0 0 1 x x x 4
  73. * 0 0 0 0 0 1 x x 5
  74. * 0 0 0 0 0 0 1 x 6
  75. * 0 0 0 0 0 0 0 1 7
  76. * 0 0 0 0 0 0 0 0 8
  77. *********************************************************************************************************
  78. */
  79. #ifndef CPU_CFG_LEAD_ZEROS_ASM_PRESENT
  80. static const CPU_INT08U CPU_CntLeadZerosTbl[256] = { /* Data vals : */
  81. /* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
  82. 8u, 7u, 6u, 6u, 5u, 5u, 5u, 5u, 4u, 4u, 4u, 4u, 4u, 4u, 4u, 4u, /* 0x00 to 0x0F */
  83. 3u, 3u, 3u, 3u, 3u, 3u, 3u, 3u, 3u, 3u, 3u, 3u, 3u, 3u, 3u, 3u, /* 0x10 to 0x1F */
  84. 2u, 2u, 2u, 2u, 2u, 2u, 2u, 2u, 2u, 2u, 2u, 2u, 2u, 2u, 2u, 2u, /* 0x20 to 0x2F */
  85. 2u, 2u, 2u, 2u, 2u, 2u, 2u, 2u, 2u, 2u, 2u, 2u, 2u, 2u, 2u, 2u, /* 0x30 to 0x3F */
  86. 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, /* 0x40 to 0x4F */
  87. 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, /* 0x50 to 0x5F */
  88. 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, /* 0x60 to 0x6F */
  89. 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, /* 0x70 to 0x7F */
  90. 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, /* 0x80 to 0x8F */
  91. 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, /* 0x90 to 0x9F */
  92. 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, /* 0xA0 to 0xAF */
  93. 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, /* 0xB0 to 0xBF */
  94. 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, /* 0xC0 to 0xCF */
  95. 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, /* 0xD0 to 0xDF */
  96. 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, /* 0xE0 to 0xEF */
  97. 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u /* 0xF0 to 0xFF */
  98. };
  99. #endif
  100. /*$PAGE*/
  101. /*
  102. *********************************************************************************************************
  103. * LOCAL GLOBAL VARIABLES
  104. *********************************************************************************************************
  105. */
  106. /*
  107. *********************************************************************************************************
  108. * LOCAL FUNCTION PROTOTYPES
  109. *********************************************************************************************************
  110. */
  111. #if (CPU_CFG_NAME_EN == DEF_ENABLED) /* ---------------- CPU NAME FNCTS ---------------- */
  112. static void CPU_NameInit (void);
  113. #endif
  114. /* ----------------- CPU TS FNCTS ----------------- */
  115. #if ((CPU_CFG_TS_EN == DEF_ENABLED) || \
  116. (CPU_CFG_TS_TMR_EN == DEF_ENABLED))
  117. static void CPU_TS_Init (void);
  118. #endif
  119. #ifdef CPU_CFG_INT_DIS_MEAS_EN /* ---------- CPU INT DIS TIME MEAS FNCTS --------- */
  120. static void CPU_IntDisMeasInit (void);
  121. static CPU_TS_TMR CPU_IntDisMeasMaxCalc(CPU_TS_TMR time_tot_cnts);
  122. #endif
  123. /*
  124. *********************************************************************************************************
  125. * LOCAL CONFIGURATION ERRORS
  126. *********************************************************************************************************
  127. */
  128. /*$PAGE*/
  129. /*
  130. *********************************************************************************************************
  131. * CPU_Init()
  132. *
  133. * Description : (1) Initialize CPU module :
  134. *
  135. * (a) Initialize CPU timestamps
  136. * (b) Initialize CPU interrupts disabled time measurements
  137. * (c) Initialize CPU host name
  138. *
  139. *
  140. * Argument(s) : none.
  141. *
  142. * Return(s) : none.
  143. *
  144. * Caller(s) : Your Product's Application.
  145. *
  146. * This function is a CPU initialization function & MAY be called by application/
  147. * initialization function(s).
  148. *
  149. * Note(s) : (2) CPU_Init() MUST be called ... :
  150. *
  151. * (a) ONLY ONCE from a product's application; ...
  152. * (b) BEFORE product's application calls any core CPU module function(s)
  153. *
  154. * (3) The following initialization functions MUST be sequenced as follows :
  155. *
  156. * (a) CPU_TS_Init() SHOULD precede ALL calls to other CPU timestamp functions
  157. *
  158. * (b) CPU_IntDisMeasInit() SHOULD precede ALL calls to CPU_CRITICAL_ENTER()/CPU_CRITICAL_EXIT()
  159. * & other CPU interrupts disabled time measurement functions
  160. *********************************************************************************************************
  161. */
  162. void CPU_Init (void)
  163. {
  164. /* --------------------- INIT TS ---------------------- */
  165. #if ((CPU_CFG_TS_EN == DEF_ENABLED) || \
  166. (CPU_CFG_TS_TMR_EN == DEF_ENABLED))
  167. CPU_TS_Init(); /* See Note #3a. */
  168. #endif
  169. /* -------------- INIT INT DIS TIME MEAS -------------- */
  170. #ifdef CPU_CFG_INT_DIS_MEAS_EN
  171. CPU_IntDisMeasInit(); /* See Note #3b. */
  172. #endif
  173. /* ------------------ INIT CPU NAME ------------------- */
  174. #if (CPU_CFG_NAME_EN == DEF_ENABLED)
  175. CPU_NameInit();
  176. #endif
  177. }
  178. /*$PAGE*/
  179. /*
  180. *********************************************************************************************************
  181. * CPU_SW_Exception()
  182. *
  183. * Description : Trap unrecoverable software exception.
  184. *
  185. * Argument(s) : none.
  186. *
  187. * Return(s) : none.
  188. *
  189. * Caller(s) : various.
  190. *
  191. * Note(s) : (1) CPU_SW_Exception() deadlocks the current code execution -- whether multi-tasked/
  192. * -processed/-threaded or single-threaded -- when the current code execution cannot
  193. * gracefully recover or report a fault or exception condition.
  194. *
  195. * See also 'cpu_core.h CPU_SW_EXCEPTION() Note #1'.
  196. *********************************************************************************************************
  197. */
  198. void CPU_SW_Exception (void)
  199. {
  200. while (DEF_ON) {
  201. ;
  202. }
  203. }
  204. /*$PAGE*/
  205. /*
  206. *********************************************************************************************************
  207. * CPU_NameClr()
  208. *
  209. * Description : Clear CPU Name.
  210. *
  211. * Argument(s) : none.
  212. *
  213. * Return(s) : none.
  214. *
  215. * Caller(s) : CPU_NameInit(),
  216. * Application.
  217. *
  218. * This function is a CPU module application interface (API) function & MAY be called by
  219. * application function(s).
  220. *
  221. * Note(s) : none.
  222. *********************************************************************************************************
  223. */
  224. #if (CPU_CFG_NAME_EN == DEF_ENABLED)
  225. void CPU_NameClr (void)
  226. {
  227. CPU_SR_ALLOC();
  228. CPU_CRITICAL_ENTER();
  229. Mem_Clr((void *)&CPU_Name[0],
  230. (CPU_SIZE_T) CPU_CFG_NAME_SIZE);
  231. CPU_CRITICAL_EXIT();
  232. }
  233. #endif
  234. /*$PAGE*/
  235. /*
  236. *********************************************************************************************************
  237. * CPU_NameGet()
  238. *
  239. * Description : Get CPU host name.
  240. *
  241. * Argument(s) : p_name Pointer to an ASCII character array that will receive the return CPU host
  242. * name ASCII string from this function (see Note #1).
  243. *
  244. * p_err Pointer to variable that will receive the return error code from this function :
  245. *
  246. * CPU_ERR_NONE CPU host name successfully returned.
  247. * CPU_ERR_NULL_PTR Argument 'p_name' passed a NULL pointer.
  248. *
  249. * Return(s) : none.
  250. *
  251. * Caller(s) : Application.
  252. *
  253. * This function is a CPU module application interface (API) function & MAY be called by
  254. * application function(s).
  255. *
  256. * Note(s) : (1) The size of the ASCII character array that will receive the return CPU host name
  257. * ASCII string :
  258. *
  259. * (a) MUST be greater than or equal to the current CPU host name's ASCII string
  260. * size including the terminating NULL character;
  261. * (b) SHOULD be greater than or equal to CPU_CFG_NAME_SIZE
  262. *********************************************************************************************************
  263. */
  264. #if (CPU_CFG_NAME_EN == DEF_ENABLED)
  265. void CPU_NameGet (CPU_CHAR *p_name,
  266. CPU_ERR *p_err)
  267. {
  268. CPU_SR_ALLOC();
  269. if (p_err == (CPU_ERR *)0) {
  270. CPU_SW_EXCEPTION(;);
  271. }
  272. if (p_name == (CPU_CHAR *)0) {
  273. *p_err = CPU_ERR_NULL_PTR;
  274. return;
  275. }
  276. CPU_CRITICAL_ENTER();
  277. (void)Str_Copy_N(p_name,
  278. &CPU_Name[0],
  279. CPU_CFG_NAME_SIZE);
  280. CPU_CRITICAL_EXIT();
  281. *p_err = CPU_ERR_NONE;
  282. }
  283. #endif
  284. /*$PAGE*/
  285. /*
  286. *********************************************************************************************************
  287. * CPU_NameSet()
  288. *
  289. * Description : Set CPU host name.
  290. *
  291. * Argument(s) : p_name Pointer to CPU host name to set.
  292. *
  293. * p_err Pointer to variable that will receive the return error code from this function :
  294. *
  295. * CPU_ERR_NONE CPU host name successfully set.
  296. * CPU_ERR_NULL_PTR Argument 'p_name' passed a NULL pointer.
  297. * CPU_ERR_NAME_SIZE Invalid CPU host name size (see Note #1).
  298. *
  299. * Return(s) : none.
  300. *
  301. * Caller(s) : Application.
  302. *
  303. * This function is a CPU module application interface (API) function & MAY be called by
  304. * application function(s).
  305. *
  306. * Note(s) : (1) 'p_name' ASCII string size, including the terminating NULL character, MUST be less
  307. * than or equal to CPU_CFG_NAME_SIZE.
  308. *********************************************************************************************************
  309. */
  310. #if (CPU_CFG_NAME_EN == DEF_ENABLED)
  311. void CPU_NameSet (const CPU_CHAR *p_name,
  312. CPU_ERR *p_err)
  313. {
  314. CPU_SIZE_T len;
  315. CPU_SR_ALLOC();
  316. if (p_err == (CPU_ERR *)0) {
  317. CPU_SW_EXCEPTION(;);
  318. }
  319. if (p_name == (const CPU_CHAR *)0) {
  320. *p_err = CPU_ERR_NULL_PTR;
  321. return;
  322. }
  323. len = Str_Len_N(p_name,
  324. CPU_CFG_NAME_SIZE);
  325. if (len < CPU_CFG_NAME_SIZE) { /* If cfg name len < max name size, ... */
  326. CPU_CRITICAL_ENTER();
  327. (void)Str_Copy_N(&CPU_Name[0], /* ... copy cfg name to CPU host name. */
  328. p_name,
  329. CPU_CFG_NAME_SIZE);
  330. CPU_CRITICAL_EXIT();
  331. *p_err = CPU_ERR_NONE;
  332. } else {
  333. *p_err = CPU_ERR_NAME_SIZE;
  334. }
  335. }
  336. #endif
  337. /*$PAGE*/
  338. /*
  339. *********************************************************************************************************
  340. * CPU_TS_Get32()
  341. *
  342. * Description : Get current 32-bit CPU timestamp.
  343. *
  344. * Argument(s) : none.
  345. *
  346. * Return(s) : Current 32-bit CPU timestamp (in timestamp timer counts).
  347. *
  348. * Caller(s) : Application.
  349. *
  350. * This function is a CPU module application interface (API) function & MAY be called by
  351. * application function(s).
  352. *
  353. * Note(s) : (1) When applicable, the amount of time measured by CPU timestamps is calculated by
  354. * either of the following equations :
  355. *
  356. * (a) Time measured = Number timer counts * Timer period
  357. *
  358. * where
  359. *
  360. * Number timer counts Number of timer counts measured
  361. * Timer period Timer's period in some units of
  362. * (fractional) seconds
  363. * Time measured Amount of time measured, in same
  364. * units of (fractional) seconds
  365. * as the Timer period
  366. *
  367. * Number timer counts
  368. * (b) Time measured = ---------------------
  369. * Timer frequency
  370. *
  371. * where
  372. *
  373. * Number timer counts Number of timer counts measured
  374. * Timer frequency Timer's frequency in some units
  375. * of counts per second
  376. * Time measured Amount of time measured, in seconds
  377. *
  378. * See also 'cpu_core.h FUNCTION PROTOTYPES CPU_TS_TmrRd() Note #2c1'.
  379. *
  380. * (2) In case the CPU timestamp timer has lower precision than the 32-bit CPU timestamp;
  381. * its precision is extended via periodic updates by accumulating the deltas of the
  382. * timestamp timer count values into the higher-precision 32-bit CPU timestamp.
  383. *
  384. * (3) After initialization, 'CPU_TS_32_Accum' & 'CPU_TS_32_TmrPrev' MUST ALWAYS
  385. * be accessed AND updated exclusively with interrupts disabled -- but NOT
  386. * with critical sections.
  387. *********************************************************************************************************
  388. */
  389. #if (CPU_CFG_TS_32_EN == DEF_ENABLED)
  390. CPU_TS32 CPU_TS_Get32 (void)
  391. {
  392. CPU_TS32 ts;
  393. #if (CPU_CFG_TS_TMR_SIZE < CPU_WORD_SIZE_32)
  394. CPU_TS_TMR tmr_cur;
  395. CPU_TS_TMR tmr_delta;
  396. CPU_SR_ALLOC();
  397. #endif
  398. #if (CPU_CFG_TS_TMR_SIZE >= CPU_WORD_SIZE_32)
  399. ts = (CPU_TS32)CPU_TS_TmrRd(); /* Get cur ts tmr val (in 32-bit ts cnts). */
  400. #else
  401. CPU_INT_DIS();
  402. tmr_cur = (CPU_TS_TMR) CPU_TS_TmrRd(); /* Get cur ts tmr val (in ts tmr cnts). */
  403. tmr_delta = (CPU_TS_TMR)(tmr_cur - CPU_TS_32_TmrPrev); /* Calc delta ts tmr cnts. */
  404. CPU_TS_32_Accum += (CPU_TS32 ) tmr_delta; /* Inc ts by delta ts tmr cnts (see Note #2). */
  405. CPU_TS_32_TmrPrev = (CPU_TS_TMR) tmr_cur; /* Save cur ts tmr cnts for next update. */
  406. ts = (CPU_TS32 ) CPU_TS_32_Accum;
  407. CPU_INT_EN();
  408. #endif
  409. return (ts);
  410. }
  411. #endif
  412. /*$PAGE*/
  413. /*
  414. *********************************************************************************************************
  415. * CPU_TS_Get64()
  416. *
  417. * Description : Get current 64-bit CPU timestamp.
  418. *
  419. * Argument(s) : none.
  420. *
  421. * Return(s) : Current 64-bit CPU timestamp (in timestamp timer counts).
  422. *
  423. * Caller(s) : Application.
  424. *
  425. * This function is a CPU module application interface (API) function & MAY be called by
  426. * application function(s).
  427. *
  428. * Note(s) : (1) When applicable, the amount of time measured by CPU timestamps is calculated by
  429. * either of the following equations :
  430. *
  431. * (a) Time measured = Number timer counts * Timer period
  432. *
  433. * where
  434. *
  435. * Number timer counts Number of timer counts measured
  436. * Timer period Timer's period in some units of
  437. * (fractional) seconds
  438. * Time measured Amount of time measured, in same
  439. * units of (fractional) seconds
  440. * as the Timer period
  441. *
  442. * Number timer counts
  443. * (b) Time measured = ---------------------
  444. * Timer frequency
  445. *
  446. * where
  447. *
  448. * Number timer counts Number of timer counts measured
  449. * Timer frequency Timer's frequency in some units
  450. * of counts per second
  451. * Time measured Amount of time measured, in seconds
  452. *
  453. * See also 'cpu_core.h FUNCTION PROTOTYPES CPU_TS_TmrRd() Note #2c1'.
  454. *
  455. * (2) In case the CPU timestamp timer has lower precision than the 64-bit CPU timestamp;
  456. * its precision is extended via periodic updates by accumulating the deltas of the
  457. * timestamp timer count values into the higher-precision 64-bit CPU timestamp.
  458. *
  459. * (3) After initialization, 'CPU_TS_64_Accum' & 'CPU_TS_64_TmrPrev' MUST ALWAYS
  460. * be accessed AND updated exclusively with interrupts disabled -- but NOT
  461. * with critical sections.
  462. *********************************************************************************************************
  463. */
  464. #if (CPU_CFG_TS_64_EN == DEF_ENABLED)
  465. CPU_TS64 CPU_TS_Get64 (void)
  466. {
  467. CPU_TS64 ts;
  468. #if (CPU_CFG_TS_TMR_SIZE < CPU_WORD_SIZE_64)
  469. CPU_TS_TMR tmr_cur;
  470. CPU_TS_TMR tmr_delta;
  471. CPU_SR_ALLOC();
  472. #endif
  473. #if (CPU_CFG_TS_TMR_SIZE >= CPU_WORD_SIZE_64)
  474. ts = (CPU_TS64)CPU_TS_TmrRd(); /* Get cur ts tmr val (in 64-bit ts cnts). */
  475. #else
  476. CPU_INT_DIS();
  477. tmr_cur = (CPU_TS_TMR) CPU_TS_TmrRd(); /* Get cur ts tmr val (in ts tmr cnts). */
  478. tmr_delta = (CPU_TS_TMR)(tmr_cur - CPU_TS_64_TmrPrev); /* Calc delta ts tmr cnts. */
  479. CPU_TS_64_Accum += (CPU_TS64 ) tmr_delta; /* Inc ts by delta ts tmr cnts (see Note #2). */
  480. CPU_TS_64_TmrPrev = (CPU_TS_TMR) tmr_cur; /* Save cur ts tmr cnts for next update. */
  481. ts = (CPU_TS64 ) CPU_TS_64_Accum;
  482. CPU_INT_EN();
  483. #endif
  484. return (ts);
  485. }
  486. #endif
  487. /*$PAGE*/
  488. /*
  489. *********************************************************************************************************
  490. * CPU_TS_Update()
  491. *
  492. * Description : Update current CPU timestamp(s).
  493. *
  494. * Argument(s) : none.
  495. *
  496. * Return(s) : none.
  497. *
  498. * Caller(s) : Application/BSP periodic time handler (see Note #1).
  499. *
  500. * This function is a CPU timestamp BSP function & SHOULD be called only by appropriate
  501. * application/BSP function(s).
  502. *
  503. * Note(s) : (1) (a) CPU timestamp(s) MUST be updated periodically by some application (or BSP) time
  504. * handler in order to (adequately) maintain CPU timestamp(s)' time.
  505. *
  506. * (b) CPU timestamp(s) MUST be updated more frequently than the CPU timestamp timer
  507. * overflows; otherwise, CPU timestamp(s) will lose time.
  508. *
  509. * See also 'cpu_core.h FUNCTION PROTOTYPES CPU_TS_TmrRd() Note #2c2'.
  510. *********************************************************************************************************
  511. */
  512. #if (CPU_CFG_TS_EN == DEF_ENABLED)
  513. void CPU_TS_Update (void)
  514. {
  515. #if ((CPU_CFG_TS_32_EN == DEF_ENABLED) && \
  516. (CPU_CFG_TS_TMR_SIZE < CPU_WORD_SIZE_32))
  517. (void)CPU_TS_Get32();
  518. #endif
  519. #if ((CPU_CFG_TS_64_EN == DEF_ENABLED) && \
  520. (CPU_CFG_TS_TMR_SIZE < CPU_WORD_SIZE_64))
  521. (void)CPU_TS_Get64();
  522. #endif
  523. }
  524. #endif
  525. /*$PAGE*/
  526. /*
  527. *********************************************************************************************************
  528. * CPU_TS_TmrFreqGet()
  529. *
  530. * Description : Get CPU timestamp's timer frequency.
  531. *
  532. * Argument(s) : p_err Pointer to variable that will receive the return error code from this function :
  533. *
  534. * CPU_ERR_NONE CPU timestamp's timer frequency successfully
  535. * returned.
  536. * CPU_ERR_TS_FREQ_INVALID CPU timestamp's timer frequency invalid &/or
  537. * NOT yet configured.
  538. *
  539. * Return(s) : CPU timestamp's timer frequency (in Hertz), if NO error(s).
  540. *
  541. * 0, otherwise.
  542. *
  543. * Caller(s) : Application.
  544. *
  545. * This function is a CPU module application interface (API) function & MAY be called by
  546. * application function(s).
  547. *
  548. * Note(s) : none.
  549. *********************************************************************************************************
  550. */
  551. #if (CPU_CFG_TS_TMR_EN == DEF_ENABLED)
  552. CPU_TS_TMR_FREQ CPU_TS_TmrFreqGet (CPU_ERR *p_err)
  553. {
  554. CPU_TS_TMR_FREQ freq_hz;
  555. if (p_err == (CPU_ERR *)0) {
  556. CPU_SW_EXCEPTION(;);
  557. }
  558. freq_hz = CPU_TS_TmrFreq_Hz;
  559. *p_err = (freq_hz != 0u) ? CPU_ERR_NONE : CPU_ERR_TS_FREQ_INVALID;
  560. return (freq_hz);
  561. }
  562. #endif
  563. /*$PAGE*/
  564. /*
  565. *********************************************************************************************************
  566. * CPU_TS_TmrFreqSet()
  567. *
  568. * Description : Set CPU timestamp's timer frequency.
  569. *
  570. * Argument(s) : freq_hz Frequency (in Hertz) to set for CPU timestamp's timer.
  571. *
  572. * Return(s) : none.
  573. *
  574. * Caller(s) : CPU_TS_TmrInit(),
  575. * Application/BSP initialization function(s).
  576. *
  577. * This function is a CPU module BSP function & SHOULD be called only by appropriate
  578. * application/BSP function(s) [see Note #1].
  579. *
  580. * Note(s) : (1) (a) (1) CPU timestamp timer frequency is NOT required for internal CPU timestamp
  581. * operations but may OPTIONALLY be configured by CPU_TS_TmrInit() or other
  582. * application/BSP initialization functions.
  583. *
  584. * (2) CPU timestamp timer frequency MAY be used with optional CPU_TSxx_to_uSec()
  585. * to convert CPU timestamps from timer counts into microseconds.
  586. *
  587. * See also 'cpu_core.h FUNCTION PROTOTYPES CPU_TSxx_to_uSec() Note #2a'.
  588. *
  589. * (b) CPU timestamp timer period SHOULD be less than the typical measured time but MUST
  590. * be less than the maximum measured time; otherwise, timer resolution inadequate to
  591. * measure desired times.
  592. *
  593. * See also 'cpu_core.h FUNCTION PROTOTYPES CPU_TSxx_to_uSec() Note #2b'.
  594. *********************************************************************************************************
  595. */
  596. #if (CPU_CFG_TS_TMR_EN == DEF_ENABLED)
  597. void CPU_TS_TmrFreqSet (CPU_TS_TMR_FREQ freq_hz)
  598. {
  599. CPU_TS_TmrFreq_Hz = freq_hz;
  600. }
  601. #endif
  602. /*$PAGE*/
  603. /*
  604. *********************************************************************************************************
  605. * CPU_IntDisMeasMaxCurReset()
  606. *
  607. * Description : Reset current maximum interrupts disabled time.
  608. *
  609. * Argument(s) : none.
  610. *
  611. * Return(s) : Maximum interrupts disabled time (in CPU timestamp timer counts) before resetting.
  612. *
  613. * See also 'cpu_core.h FUNCTION PROTOTYPES CPU_TS_TmrRd() Note #2c'
  614. * & 'cpu_core.h FUNCTION PROTOTYPES CPU_TSxx_to_uSec() Note #2'.
  615. *
  616. * Caller(s) : Application.
  617. *
  618. * This function is a CPU module application interface (API) function & MAY be called
  619. * by application function(s).
  620. *
  621. * Note(s) : (1) After initialization, 'CPU_IntDisMeasMaxCur_cnts' MUST ALWAYS be accessed
  622. * exclusively with interrupts disabled -- but NOT with critical sections.
  623. *********************************************************************************************************
  624. */
  625. #ifdef CPU_CFG_INT_DIS_MEAS_EN
  626. CPU_TS_TMR CPU_IntDisMeasMaxCurReset (void)
  627. {
  628. CPU_TS_TMR time_max_cnts;
  629. CPU_SR_ALLOC();
  630. time_max_cnts = CPU_IntDisMeasMaxCurGet();
  631. CPU_INT_DIS();
  632. CPU_IntDisMeasMaxCur_cnts = 0u;
  633. CPU_INT_EN();
  634. return (time_max_cnts);
  635. }
  636. #endif
  637. /*$PAGE*/
  638. /*
  639. *********************************************************************************************************
  640. * CPU_IntDisMeasMaxCurGet()
  641. *
  642. * Description : Get current maximum interrupts disabled time.
  643. *
  644. * Argument(s) : none.
  645. *
  646. * Return(s) : Current maximum interrupts disabled time (in CPU timestamp timer counts).
  647. *
  648. * See also 'cpu_core.h FUNCTION PROTOTYPES CPU_TS_TmrRd() Note #2c'
  649. * & 'cpu_core.h FUNCTION PROTOTYPES CPU_TSxx_to_uSec() Note #2'.
  650. *
  651. * Caller(s) : CPU_IntDisMeasMaxCurReset(),
  652. * Application.
  653. *
  654. * This function is a CPU module application interface (API) function & MAY be called
  655. * by application function(s).
  656. *
  657. * Note(s) : (1) After initialization, 'CPU_IntDisMeasMaxCur_cnts' MUST ALWAYS be accessed
  658. * exclusively with interrupts disabled -- but NOT with critical sections.
  659. *********************************************************************************************************
  660. */
  661. #ifdef CPU_CFG_INT_DIS_MEAS_EN
  662. CPU_TS_TMR CPU_IntDisMeasMaxCurGet (void)
  663. {
  664. CPU_TS_TMR time_tot_cnts;
  665. CPU_TS_TMR time_max_cnts;
  666. CPU_SR_ALLOC();
  667. CPU_INT_DIS();
  668. time_tot_cnts = CPU_IntDisMeasMaxCur_cnts;
  669. CPU_INT_EN();
  670. time_max_cnts = CPU_IntDisMeasMaxCalc(time_tot_cnts);
  671. return (time_max_cnts);
  672. }
  673. #endif
  674. /*$PAGE*/
  675. /*
  676. *********************************************************************************************************
  677. * CPU_IntDisMeasMaxGet()
  678. *
  679. * Description : Get (non-resetable) maximum interrupts disabled time.
  680. *
  681. * Argument(s) : none.
  682. *
  683. * Return(s) : (Non-resetable) maximum interrupts disabled time (in CPU timestamp timer counts).
  684. *
  685. * See also 'cpu_core.h FUNCTION PROTOTYPES CPU_TS_TmrRd() Note #2c'
  686. * & 'cpu_core.h FUNCTION PROTOTYPES CPU_TSxx_to_uSec() Note #2'.
  687. *
  688. * Caller(s) : CPU_IntDisMeasInit(),
  689. * Application.
  690. *
  691. * This function is a CPU module application interface (API) function & MAY be called
  692. * by application function(s).
  693. *
  694. * Note(s) : (1) After initialization, 'CPU_IntDisMeasMax_cnts' MUST ALWAYS be accessed
  695. * exclusively with interrupts disabled -- but NOT with critical sections.
  696. *********************************************************************************************************
  697. */
  698. #ifdef CPU_CFG_INT_DIS_MEAS_EN
  699. CPU_TS_TMR CPU_IntDisMeasMaxGet (void)
  700. {
  701. CPU_TS_TMR time_tot_cnts;
  702. CPU_TS_TMR time_max_cnts;
  703. CPU_SR_ALLOC();
  704. CPU_INT_DIS();
  705. time_tot_cnts = CPU_IntDisMeasMax_cnts;
  706. CPU_INT_EN();
  707. time_max_cnts = CPU_IntDisMeasMaxCalc(time_tot_cnts);
  708. return (time_max_cnts);
  709. }
  710. #endif
  711. /*$PAGE*/
  712. /*
  713. *********************************************************************************************************
  714. * CPU_IntDisMeasStart()
  715. *
  716. * Description : Start interrupts disabled time measurement.
  717. *
  718. * Argument(s) : none.
  719. *
  720. * Return(s) : none.
  721. *
  722. * Caller(s) : CPU_CRITICAL_ENTER().
  723. *
  724. * This function is an INTERNAL CPU module function & MUST NOT be called by application
  725. * function(s).
  726. *
  727. * Note(s) : none.
  728. *********************************************************************************************************
  729. */
  730. #ifdef CPU_CFG_INT_DIS_MEAS_EN
  731. void CPU_IntDisMeasStart (void)
  732. {
  733. CPU_IntDisMeasCtr++;
  734. if (CPU_IntDisNestCtr == 0u) { /* If ints NOT yet dis'd, ... */
  735. CPU_IntDisMeasStart_cnts = CPU_TS_TmrRd(); /* ... get ints dis'd start time. */
  736. }
  737. CPU_IntDisNestCtr++;
  738. }
  739. #endif
  740. /*$PAGE*/
  741. /*
  742. *********************************************************************************************************
  743. * CPU_IntDisMeasStop()
  744. *
  745. * Description : Stop interrupts disabled time measurement.
  746. *
  747. * Argument(s) : none.
  748. *
  749. * Return(s) : none.
  750. *
  751. * Caller(s) : CPU_CRITICAL_EXIT().
  752. *
  753. * This function is an INTERNAL CPU module function & MUST NOT be called by application
  754. * function(s).
  755. *
  756. * Note(s) : (1) (a) The total amount of time interrupts are disabled by system &/or application code
  757. * during critical sections is calculated by the following equations :
  758. *
  759. * (1) When interrupts disabled time measurements are disabled :
  760. *
  761. *
  762. * | CRITICAL | | CRITICAL |
  763. * |<- SECTION ->| |<- SECTION ->|
  764. * | ENTER | | EXIT |
  765. *
  766. * Disable Enable
  767. * Interrupts Interrupts
  768. *
  769. * || || || ||
  770. * || || || ||
  771. * || | ||<------------------------->|| | ||
  772. * || |<->|| | ||<----->| ||
  773. * || | | || | || | | ||
  774. * | | | | |
  775. * interrupts time interrupts
  776. * disabled interrupts |enabled
  777. * | disabled |
  778. * | (via application) |
  779. * time time
  780. * interrupts interrupts
  781. * disabled ovrhd enabled ovrhd
  782. *
  783. *
  784. * (A) time = [ time - time ] - time
  785. * interrupts [ interrupts interrupts ] total
  786. * disabled [ enabled disabled ] ovrhd
  787. * (via application)
  788. *
  789. *
  790. * (B) time = time + time
  791. * total interrupts interrupts
  792. * ovrhd enabled ovrhd disabled ovrhd
  793. *
  794. *
  795. * where
  796. *
  797. * time time interrupts are disabled between
  798. * interrupts first critical section enter &
  799. * disabled last critical section exit (i.e.
  800. * (via application) minus total overhead time)
  801. *
  802. * time time when interrupts are disabled
  803. * interrupts
  804. * disabled
  805. *
  806. * time time when interrupts are enabled
  807. * interrupts
  808. * enabled
  809. *
  810. *
  811. * time total overhead time to disable/enable
  812. * total interrupts during critical section
  813. * ovrhd enter & exit
  814. *
  815. * time total overhead time to disable interrupts
  816. * interrupts during critical section enter
  817. * disabled ovrhd
  818. *
  819. * time total overhead time to enable interrupts
  820. * interrupts during critical section exit
  821. * enabled ovrhd
  822. *
  823. *$PAGE*
  824. *
  825. * (2) When interrupts disabled time measurements are enabled :
  826. *
  827. *
  828. * | | | |
  829. * |<----- CRITICAL SECTION ENTER ----->| |<------- CRITICAL SECTION EXIT ------->|
  830. * | | | |
  831. *
  832. * Time Time
  833. * Disable Measurement Measurement Enable
  834. * Interrupts Start Stop Interrupts
  835. *
  836. * || | || || | ||
  837. * || | || || | ||
  838. * || | | ||<------------------------->|| | | ||
  839. * || | | |<----------->|| | ||<------------->| | | ||
  840. * || | | | | || | || | | | | ||
  841. * | | | | | | |
  842. * interrupts get | time | get interrupts
  843. * disabled start time | interrupts | stop time enabled
  844. * meas | disabled | meas
  845. * time (via application) time
  846. * start meas stop meas
  847. * ovrhd ovrhd
  848. *
  849. *
  850. * (A) time = [ time - time ] - time
  851. * interrupts [ stop start ] total meas
  852. * disabled [ meas meas ] ovrhd
  853. * (via application)
  854. *
  855. *
  856. * (B) time = time + time
  857. * total meas start meas stop meas
  858. * ovrhd ovrhd ovrhd
  859. *
  860. *
  861. * where
  862. *
  863. * time time interrupts are disabled between first
  864. * interrupts critical section enter & last critical
  865. * disabled section exit (i.e. minus measurement
  866. * (via application) overhead time; however, this does NOT
  867. * include any overhead time to disable
  868. * or enable interrupts during critical
  869. * section enter & exit)
  870. *
  871. * time time of disable interrupts start time
  872. * start measurement (in timer counts)
  873. * meas
  874. *
  875. * time time of disable interrupts stop time
  876. * stop measurement (in timer counts)
  877. * meas
  878. *
  879. *
  880. * time total overhead time to start/stop disabled
  881. * total meas interrupts time measurements (in timer
  882. * ovrhd counts)
  883. *
  884. * time total overhead time after getting start
  885. * start meas time until end of start measurement
  886. * ovrhd function (in timer counts)
  887. *
  888. * time total overhead time from beginning of stop
  889. * stop meas measurement function until after getting
  890. * ovrhd stop time (in timer counts)
  891. *
  892. *
  893. *$PAGE*
  894. * (b) (1) (A) In order to correctly handle unsigned subtraction overflows of start times
  895. * from stop times, CPU timestamp timer count values MUST be returned via
  896. * word-size-configurable 'CPU_TS_TMR' data type.
  897. *
  898. * See also 'cpu_core.h FUNCTION PROTOTYPES CPU_TS_TmrRd() Note #2a'.
  899. *
  900. * (B) Since unsigned subtraction of start times from stop times assumes increasing
  901. * values, timestamp timer count values MUST increase with each time count.
  902. *
  903. * See also 'cpu_core.h FUNCTION PROTOTYPES CPU_TS_TmrRd() Note #2b'.
  904. *
  905. * (2) (A) To expedite & reduce interrupts disabled time measurement overhead; only the
  906. * subtraction of start times from stop times is performed.
  907. *
  908. * (B) The final calculations to subtract the interrupts disabled time measurement
  909. * overhead is performed asynchronously in appropriate API functions.
  910. *
  911. * See also 'CPU_IntDisMeasMaxCalc() Note #1b'.
  912. *********************************************************************************************************
  913. */
  914. #ifdef CPU_CFG_INT_DIS_MEAS_EN
  915. void CPU_IntDisMeasStop (void)
  916. {
  917. CPU_TS_TMR time_ints_disd_cnts;
  918. CPU_IntDisNestCtr--;
  919. if (CPU_IntDisNestCtr == 0u) { /* If ints NO longer dis'd, ... */
  920. CPU_IntDisMeasStop_cnts = CPU_TS_TmrRd(); /* ... get ints dis'd stop time & ... */
  921. /* ... calc ints dis'd tot time (see Note #1b2A). */
  922. time_ints_disd_cnts = CPU_IntDisMeasStop_cnts -
  923. CPU_IntDisMeasStart_cnts;
  924. /* Calc max ints dis'd times. */
  925. if (CPU_IntDisMeasMaxCur_cnts < time_ints_disd_cnts) {
  926. CPU_IntDisMeasMaxCur_cnts = time_ints_disd_cnts;
  927. }
  928. if (CPU_IntDisMeasMax_cnts < time_ints_disd_cnts) {
  929. CPU_IntDisMeasMax_cnts = time_ints_disd_cnts;
  930. }
  931. }
  932. }
  933. #endif
  934. /*$PAGE*/
  935. /*
  936. *********************************************************************************************************
  937. * CPU_CntLeadZeros()
  938. *
  939. * Description : Count the number of contiguous, most-significant, leading zero bits in a data value.
  940. *
  941. * Argument(s) : val Data value to count leading zero bits.
  942. *
  943. * Return(s) : Number of contiguous, most-significant, leading zero bits in 'val', if NO error(s).
  944. *
  945. * 0, otherwise.
  946. *
  947. * Caller(s) : Application.
  948. *
  949. * This function is an INTERNAL CPU module function but MAY be called by application function(s).
  950. *
  951. * Note(s) : (1) (a) Supports the following data value sizes :
  952. *
  953. * (1) 8-bits
  954. * (2) 16-bits
  955. * (3) 32-bits
  956. * (4) 64-bits
  957. *
  958. * See also 'cpu_def.h CPU WORD CONFIGURATION Note #1'.
  959. *
  960. * (b) (1) For 8-bit values :
  961. *
  962. * b07 b06 b05 b04 b03 b02 b01 b00 # Leading Zeros
  963. * --- --- --- --- --- --- --- --- ---------------
  964. * 1 x x x x x x x 0
  965. * 0 1 x x x x x x 1
  966. * 0 0 1 x x x x x 2
  967. * 0 0 0 1 x x x x 3
  968. * 0 0 0 0 1 x x x 4
  969. * 0 0 0 0 0 1 x x 5
  970. * 0 0 0 0 0 0 1 x 6
  971. * 0 0 0 0 0 0 0 1 7
  972. * 0 0 0 0 0 0 0 0 8
  973. *
  974. *
  975. * (2) For 16-bit values :
  976. *
  977. * b15 b14 b13 ... b04 b03 b02 b01 b00 # Leading Zeros
  978. * --- --- --- --- --- --- --- --- ---------------
  979. * 1 x x x x x x x 0
  980. * 0 1 x x x x x x 1
  981. * 0 0 1 x x x x x 2
  982. * : : : : : : : : :
  983. * : : : : : : : : :
  984. * 0 0 0 1 x x x x 11
  985. * 0 0 0 0 1 x x x 12
  986. * 0 0 0 0 0 1 x x 13
  987. * 0 0 0 0 0 0 1 x 14
  988. * 0 0 0 0 0 0 0 1 15
  989. * 0 0 0 0 0 0 0 0 16
  990. *
  991. *
  992. * (3) For 32-bit values :
  993. *
  994. * b31 b30 b29 ... b04 b03 b02 b01 b00 # Leading Zeros
  995. * --- --- --- --- --- --- --- --- ---------------
  996. * 1 x x x x x x x 0
  997. * 0 1 x x x x x x 1
  998. * 0 0 1 x x x x x 2
  999. * : : : : : : : : :
  1000. * : : : : : : : : :
  1001. * 0 0 0 1 x x x x 27
  1002. * 0 0 0 0 1 x x x 28
  1003. * 0 0 0 0 0 1 x x 29
  1004. * 0 0 0 0 0 0 1 x 30
  1005. * 0 0 0 0 0 0 0 1 31
  1006. * 0 0 0 0 0 0 0 0 32
  1007. *
  1008. *$PAGE*
  1009. * (4) For 64-bit values :
  1010. *
  1011. * b63 b62 b61 ... b04 b03 b02 b01 b00 # Leading Zeros
  1012. * --- --- --- --- --- --- --- --- ---------------
  1013. * 1 x x x x x x x 0
  1014. * 0 1 x x x x x x 1
  1015. * 0 0 1 x x x x x 2
  1016. * : : : : : : : : :
  1017. * : : : : : : : : :
  1018. * 0 0 0 1 x x x x 59
  1019. * 0 0 0 0 1 x x x 60
  1020. * 0 0 0 0 0 1 x x 61
  1021. * 0 0 0 0 0 0 1 x 62
  1022. * 0 0 0 0 0 0 0 1 63
  1023. * 0 0 0 0 0 0 0 0 64
  1024. *
  1025. *
  1026. * See also 'CPU COUNT LEAD ZEROs LOOKUP TABLE Note #1'.
  1027. *********************************************************************************************************
  1028. */
  1029. #ifndef CPU_CFG_LEAD_ZEROS_ASM_PRESENT
  1030. CPU_DATA CPU_CntLeadZeros (CPU_DATA val)
  1031. {
  1032. CPU_DATA nbr_lead_zeros;
  1033. CPU_INT08U ix;
  1034. #if (CPU_CFG_DATA_SIZE == CPU_WORD_SIZE_08) /* ---------- 8-BIT DATA VAL --------- */
  1035. /* Chk bits [07:00] : */
  1036. /* .. Nbr lead zeros = .. */
  1037. ix = (CPU_INT08U)(val >> 0u); /* .. lookup tbl ix = 'val' >> 0 bits */
  1038. nbr_lead_zeros = (CPU_DATA )(CPU_CntLeadZerosTbl[ix] + 0u); /* .. plus nbr msb lead zeros = 0 bits.*/
  1039. #elif (CPU_CFG_DATA_SIZE == CPU_WORD_SIZE_16) /* ---------- 16-BIT DATA VAL --------- */
  1040. if (val > 0x00FFu) { /* Chk bits [15:08] : */
  1041. /* .. Nbr lead zeros = .. */
  1042. ix = (CPU_INT08U)(val >> 8u); /* .. lookup tbl ix = 'val' >> 8 bits */
  1043. nbr_lead_zeros = (CPU_DATA )(CPU_CntLeadZerosTbl[ix] + 0u); /* .. plus nbr msb lead zeros = 0 bits.*/
  1044. } else { /* Chk bits [07:00] : */
  1045. /* .. Nbr lead zeros = .. */
  1046. ix = (CPU_INT08U)(val >> 0u); /* .. lookup tbl ix = 'val' >> 0 bits */
  1047. nbr_lead_zeros = (CPU_DATA )(CPU_CntLeadZerosTbl[ix] + 8u); /* .. plus nbr msb lead zeros = 8 bits.*/
  1048. }
  1049. #elif (CPU_CFG_DATA_SIZE == CPU_WORD_SIZE_32) /* ---------- 32-BIT DATA VAL --------- */
  1050. if (val > 0x0000FFFFu) {
  1051. if (val > 0x00FFFFFFu) { /* Chk bits [31:24] : */
  1052. /* .. Nbr lead zeros = .. */
  1053. ix = (CPU_INT08U)(val >> 24u); /* .. lookup tbl ix = 'val' >> 24 bits */
  1054. nbr_lead_zeros = (CPU_DATA )(CPU_CntLeadZerosTbl[ix] + 0u); /* .. plus nbr msb lead zeros = 0 bits.*/
  1055. } else { /* Chk bits [23:16] : */
  1056. /* .. Nbr lead zeros = .. */
  1057. ix = (CPU_INT08U)(val >> 16u); /* .. lookup tbl ix = 'val' >> 16 bits */
  1058. nbr_lead_zeros = (CPU_DATA )(CPU_CntLeadZerosTbl[ix] + 8u); /* .. plus nbr msb lead zeros = 8 bits.*/
  1059. }
  1060. } else {
  1061. if (val > 0x000000FFu) { /* Chk bits [15:08] : */
  1062. /* .. Nbr lead zeros = .. */
  1063. ix = (CPU_INT08U)(val >> 8u); /* .. lookup tbl ix = 'val' >> 8 bits */
  1064. nbr_lead_zeros = (CPU_DATA )(CPU_CntLeadZerosTbl[ix] + 16u); /* .. plus nbr msb lead zeros = 16 bits.*/
  1065. } else { /* Chk bits [07:00] : */
  1066. /* .. Nbr lead zeros = .. */
  1067. ix = (CPU_INT08U)(val >> 0u); /* .. lookup tbl ix = 'val' >> 0 bits */
  1068. nbr_lead_zeros = (CPU_DATA )(CPU_CntLeadZerosTbl[ix] + 24u); /* .. plus nbr msb lead zeros = 24 bits.*/
  1069. }
  1070. }
  1071. /*$PAGE*/
  1072. #elif (CPU_CFG_DATA_SIZE == CPU_WORD_SIZE_64) /* ---------- 64-BIT DATA VAL --------- */
  1073. if (val > 0x00000000FFFFFFFFu) {
  1074. if (val > 0x0000FFFFFFFFFFFFu) {
  1075. if (val > 0x00FFFFFFFFFFFFFFu) { /* Chk bits [63:56] : */
  1076. /* .. Nbr lead zeros = .. */
  1077. ix = (CPU_INT08U)(val >> 56u); /* .. lookup tbl ix = 'val' >> 56 bits */
  1078. nbr_lead_zeros = (CPU_DATA )(CPU_CntLeadZerosTbl[ix] + 0u); /* .. plus nbr msb lead zeros = 0 bits.*/
  1079. } else { /* Chk bits [55:48] : */
  1080. /* .. Nbr lead zeros = .. */
  1081. ix = (CPU_INT08U)(val >> 48u); /* .. lookup tbl ix = 'val' >> 48 bits */
  1082. nbr_lead_zeros = (CPU_DATA )(CPU_CntLeadZerosTbl[ix] + 8u); /* .. plus nbr msb lead zeros = 8 bits.*/
  1083. }
  1084. } else {
  1085. if (val > 0x000000FFFFFFFFFFu) { /* Chk bits [47:40] : */
  1086. /* .. Nbr lead zeros = .. */
  1087. ix = (CPU_INT08U)(val >> 40u); /* .. lookup tbl ix = 'val' >> 40 bits */
  1088. nbr_lead_zeros = (CPU_DATA )(CPU_CntLeadZerosTbl[ix] + 16u); /* .. plus nbr msb lead zeros = 16 bits.*/
  1089. } else { /* Chk bits [39:32] : */
  1090. /* .. Nbr lead zeros = .. */
  1091. ix = (CPU_INT08U)(val >> 32u); /* .. lookup tbl ix = 'val' >> 32 bits */
  1092. nbr_lead_zeros = (CPU_DATA )(CPU_CntLeadZerosTbl[ix] + 24u); /* .. plus nbr msb lead zeros = 24 bits.*/
  1093. }
  1094. }
  1095. } else {
  1096. if (val > 0x000000000000FFFFu) {
  1097. if (val > 0x0000000000FFFFFFu) { /* Chk bits [31:24] : */
  1098. /* .. Nbr lead zeros = .. */
  1099. ix = (CPU_INT08U)(val >> 24u); /* .. lookup tbl ix = 'val' >> 24 bits */
  1100. nbr_lead_zeros = (CPU_DATA )(CPU_CntLeadZerosTbl[ix] + 32u); /* .. plus nbr msb lead zeros = 32 bits.*/
  1101. } else { /* Chk bits [23:16] : */
  1102. /* .. Nbr lead zeros = .. */
  1103. ix = (CPU_INT08U)(val >> 16u); /* .. lookup tbl ix = 'val' >> 16 bits */
  1104. nbr_lead_zeros = (CPU_DATA )(CPU_CntLeadZerosTbl[ix] + 40u); /* .. plus nbr msb lead zeros = 40 bits.*/
  1105. }
  1106. } else {
  1107. if (val > 0x00000000000000FFu) { /* Chk bits [15:08] : */
  1108. /* .. Nbr lead zeros = .. */
  1109. ix = (CPU_INT08U)(val >> 8u); /* .. lookup tbl ix = 'val' >> 8 bits */
  1110. nbr_lead_zeros = (CPU_DATA )(CPU_CntLeadZerosTbl[ix] + 48u); /* .. plus nbr msb lead zeros = 48 bits.*/
  1111. } else { /* Chk bits [07:00] : */
  1112. /* .. Nbr lead zeros = .. */
  1113. ix = (CPU_INT08U)(val >> 0u); /* .. lookup tbl ix = 'val' >> 0 bits */
  1114. nbr_lead_zeros = (CPU_DATA )(CPU_CntLeadZerosTbl[ix] + 56u); /* .. plus nbr msb lead zeros = 56 bits.*/
  1115. }
  1116. }
  1117. }
  1118. #else /* See Note #1a. */
  1119. (void)&ix;
  1120. nbr_lead_zeros = 0u;
  1121. #endif
  1122. return (nbr_lead_zeros);
  1123. }
  1124. #endif
  1125. /*$PAGE*/
  1126. /*
  1127. *********************************************************************************************************
  1128. *********************************************************************************************************
  1129. * LOCAL FUNCTIONS
  1130. *********************************************************************************************************
  1131. *********************************************************************************************************
  1132. */
  1133. /*
  1134. *********************************************************************************************************
  1135. * CPU_NameInit()
  1136. *
  1137. * Description : Initialize CPU Name.
  1138. *
  1139. * Argument(s) : none.
  1140. *
  1141. * Return(s) : none.
  1142. *
  1143. * Caller(s) : CPU_Init().
  1144. *
  1145. * Note(s) : none.
  1146. *********************************************************************************************************
  1147. */
  1148. #if (CPU_CFG_NAME_EN == DEF_ENABLED)
  1149. static void CPU_NameInit (void)
  1150. {
  1151. CPU_NameClr();
  1152. }
  1153. #endif
  1154. /*$PAGE*/
  1155. /*
  1156. *********************************************************************************************************
  1157. * CPU_TS_Init()
  1158. *
  1159. * Description : (1) Initialize CPU timestamp :
  1160. *
  1161. * (a) Initialize/start CPU timestamp timer See Note #1
  1162. * (b) Initialize CPU timestamp controls
  1163. *
  1164. *
  1165. * Argument(s) : none.
  1166. *
  1167. * Return(s) : none.
  1168. *
  1169. * Caller(s) : CPU_Init().
  1170. *
  1171. * Note(s) : (1) The following initialization MUST be sequenced as follows :
  1172. *
  1173. * (a) CPU_TS_TmrFreq_Hz MUST be initialized prior to CPU_TS_TmrInit()
  1174. * (b) CPU_TS_TmrInit() SHOULD precede calls to all other CPU timestamp functions;
  1175. * otherwise, invalid time measurements may be calculated/
  1176. * returned.
  1177. *
  1178. * See also 'CPU_Init() Note #3a'.
  1179. *********************************************************************************************************
  1180. */
  1181. #if ((CPU_CFG_TS_EN == DEF_ENABLED) || \
  1182. (CPU_CFG_TS_TMR_EN == DEF_ENABLED))
  1183. static void CPU_TS_Init (void)
  1184. {
  1185. #if (((CPU_CFG_TS_32_EN == DEF_ENABLED ) && \
  1186. (CPU_CFG_TS_TMR_SIZE < CPU_WORD_SIZE_32)) || \
  1187. ((CPU_CFG_TS_64_EN == DEF_ENABLED ) && \
  1188. (CPU_CFG_TS_TMR_SIZE < CPU_WORD_SIZE_64)))
  1189. CPU_TS_TMR ts_tmr_cnts;
  1190. #endif
  1191. /* ----------------- INIT CPU TS TMR ------------------ */
  1192. #if (CPU_CFG_TS_TMR_EN == DEF_ENABLED)
  1193. CPU_TS_TmrFreq_Hz = 0u; /* Init/clr ts tmr freq (see Note #1a). */
  1194. CPU_TS_TmrInit(); /* Init & start ts tmr (see Note #1b). */
  1195. #endif
  1196. /* ------------------- INIT CPU TS -------------------- */
  1197. #if (((CPU_CFG_TS_32_EN == DEF_ENABLED ) && \
  1198. (CPU_CFG_TS_TMR_SIZE < CPU_WORD_SIZE_32)) || \
  1199. ((CPU_CFG_TS_64_EN == DEF_ENABLED ) && \
  1200. (CPU_CFG_TS_TMR_SIZE < CPU_WORD_SIZE_64)))
  1201. ts_tmr_cnts = CPU_TS_TmrRd(); /* Get init ts tmr val (in ts tmr cnts). */
  1202. #endif
  1203. #if ((CPU_CFG_TS_32_EN == DEF_ENABLED) && \
  1204. (CPU_CFG_TS_TMR_SIZE < CPU_WORD_SIZE_32))
  1205. CPU_TS_32_Accum = 0u; /* Init 32-bit accum'd ts. */
  1206. CPU_TS_32_TmrPrev = ts_tmr_cnts; /* Init 32-bit ts prev tmr val. */
  1207. #endif
  1208. #if ((CPU_CFG_TS_64_EN == DEF_ENABLED) && \
  1209. (CPU_CFG_TS_TMR_SIZE < CPU_WORD_SIZE_64))
  1210. CPU_TS_64_Accum = 0u; /* Init 64-bit accum'd ts. */
  1211. CPU_TS_64_TmrPrev = ts_tmr_cnts; /* Init 64-bit ts prev tmr val. */
  1212. #endif
  1213. }
  1214. #endif
  1215. /*$PAGE*/
  1216. /*
  1217. *********************************************************************************************************
  1218. * CPU_IntDisMeasInit()
  1219. *
  1220. * Description : (1) Initialize interrupts disabled time measurements feature :
  1221. *
  1222. * (a) Initialize interrupts disabled time measurement controls
  1223. * (b) Calculate interrupts disabled time measurement overhead
  1224. *
  1225. *
  1226. * Argument(s) : none.
  1227. *
  1228. * Return(s) : none.
  1229. *
  1230. * Caller(s) : CPU_Init().
  1231. *
  1232. * Note(s) : (2) CPU_IntDisMeasInit() SHOULD precede ALL calls to CPU_CRITICAL_ENTER()/CPU_CRITICAL_EXIT()
  1233. * & other CPU interrupts disabled time measurement functions; otherwise, invalid interrupts
  1234. * disabled time measurements may be calculated/returned.
  1235. *
  1236. * See also 'CPU_Init() Note #3b'.
  1237. *
  1238. * (3) (a) (1) Interrupts disabled time measurement overhead performed multiple times to calculate
  1239. * a rounded average with better accuracy, hopefully of +/- one timer count.
  1240. *
  1241. * (2) However, a single overhead time measurement is recommended, even for instruction-
  1242. * cache-enabled CPUs, since critical sections are NOT typically called within
  1243. * instruction-cached loops. Thus, a single non-cached/non-averaged time measurement
  1244. * is a more realistic overhead for the majority of non-cached interrupts disabled
  1245. * time measurements.
  1246. *
  1247. * (b) Interrupts MUST be disabled while measuring the interrupts disabled time measurement
  1248. * overhead; otherwise, overhead measurements could be interrupted which would incorrectly
  1249. * calculate an inflated overhead time which would then incorrectly calculate deflated
  1250. * interrupts disabled times.
  1251. *********************************************************************************************************
  1252. */
  1253. #ifdef CPU_CFG_INT_DIS_MEAS_EN
  1254. static void CPU_IntDisMeasInit (void)
  1255. {
  1256. CPU_TS_TMR time_meas_tot_cnts;
  1257. CPU_INT16U i;
  1258. CPU_SR_ALLOC();
  1259. /* ----------- INIT INT DIS TIME MEAS CTRLS ----------- */
  1260. CPU_IntDisMeasCtr = 0u;
  1261. CPU_IntDisNestCtr = 0u;
  1262. CPU_IntDisMeasStart_cnts = 0u;
  1263. CPU_IntDisMeasStop_cnts = 0u;
  1264. CPU_IntDisMeasMaxCur_cnts = 0u;
  1265. CPU_IntDisMeasMax_cnts = 0u;
  1266. CPU_IntDisMeasOvrhd_cnts = 0u;
  1267. /* ----------- CALC INT DIS TIME MEAS OVRHD ----------- */
  1268. time_meas_tot_cnts = 0u;
  1269. CPU_INT_DIS(); /* Ints MUST be dis'd for ovrhd calc (see Note #3b). */
  1270. for (i = 0u; i < CPU_CFG_INT_DIS_MEAS_OVRHD_NBR; i++) {
  1271. CPU_IntDisMeasMaxCur_cnts = 0u;
  1272. CPU_IntDisMeasStart(); /* Perform multiple consecutive start/stop time meas's */
  1273. CPU_IntDisMeasStop();
  1274. time_meas_tot_cnts += CPU_IntDisMeasMaxCur_cnts; /* ... & sum time meas max's ... */
  1275. }
  1276. /* ... to calc avg time meas ovrhd (see Note #3a). */
  1277. CPU_IntDisMeasOvrhd_cnts = (time_meas_tot_cnts + (CPU_CFG_INT_DIS_MEAS_OVRHD_NBR / 2u))
  1278. / CPU_CFG_INT_DIS_MEAS_OVRHD_NBR;
  1279. CPU_IntDisMeasMaxCur_cnts = 0u; /* Reset max ints dis'd times. */
  1280. CPU_IntDisMeasMax_cnts = 0u;
  1281. CPU_INT_EN();
  1282. }
  1283. #endif
  1284. /*$PAGE*/
  1285. /*
  1286. *********************************************************************************************************
  1287. * CPU_IntDisMeasMaxCalc()
  1288. *
  1289. * Description : Calculate maximum interrupts disabled time.
  1290. *
  1291. * Argument(s) : time_tot_cnts Total interrupt disabled time, in timer counts.
  1292. *
  1293. * Return(s) : Maximum interrupts disabled time (in CPU timestamp timer counts).
  1294. *
  1295. * Caller(s) : CPU_IntDisMeasMaxCurGet(),
  1296. * CPU_IntDisMeasMaxGet().
  1297. *
  1298. * Note(s) : (1) (a) The total amount of time interrupts are disabled by system &/or application code
  1299. * during critical sections is calculated by the following equations :
  1300. *
  1301. * (1) time = [ time - time ] - time
  1302. * interrupts [ stop start ] total meas
  1303. * disabled [ meas meas ] ovrhd
  1304. * (via application)
  1305. *
  1306. *
  1307. * (2) time = time + time
  1308. * total meas start meas stop meas
  1309. * ovrhd ovrhd ovrhd
  1310. *
  1311. *
  1312. * where
  1313. *
  1314. * time time interrupts are disabled between
  1315. * interrupts first critical section enter &
  1316. * disabled last critical section exit minus
  1317. * (via application) time measurement overhead
  1318. *
  1319. * time time of disable interrupts start time
  1320. * start measurement (in timer counts)
  1321. * meas
  1322. *
  1323. * time time of disable interrupts stop time
  1324. * stop measurement (in timer counts)
  1325. * meas
  1326. *
  1327. * time total overhead time to start/stop disabled
  1328. * total meas interrupts time measurements (in timer
  1329. * ovrhd counts)
  1330. *
  1331. * time total overhead time after getting start
  1332. * start meas time until end of start measurement
  1333. * ovrhd function (in timer counts)
  1334. *
  1335. * time total overhead time from beginning of stop
  1336. * stop meas measurement function until after getting
  1337. * ovrhd stop time (in timer counts)
  1338. *
  1339. *
  1340. * (b) To expedite & reduce interrupts disabled time measurement overhead, the final
  1341. * calculations to subtract the interrupts disabled time measurement overhead is
  1342. * performed asynchronously in API functions.
  1343. *
  1344. * See also 'CPU_IntDisMeasStop() Note #1b2'.
  1345. *$PAGE*
  1346. * (c) The amount of time interrupts are disabled is calculated by either of the
  1347. * following equations :
  1348. *
  1349. * (1) Interrupts disabled time = Number timer counts * Timer period
  1350. *
  1351. * where
  1352. *
  1353. * Number timer counts Number of timer counts measured
  1354. * Timer period Timer's period in some units of
  1355. * (fractional) seconds
  1356. * Interrupts disabled time Amount of time interrupts are
  1357. * disabled, in same units of
  1358. * (fractional) seconds as the
  1359. * Timer period
  1360. *
  1361. * Number timer counts
  1362. * (2) Interrupts disabled time = ---------------------
  1363. * Timer frequency
  1364. *
  1365. * where
  1366. *
  1367. * Number timer counts Number of timer counts measured
  1368. * Timer frequency Timer's frequency in some units
  1369. * of counts per second
  1370. * Interrupts disabled time Amount of time interrupts are
  1371. * disabled, in seconds
  1372. *
  1373. * See also 'cpu_core.h FUNCTION PROTOTYPES CPU_TS_TmrRd() Note #2c'
  1374. * & 'cpu_core.h FUNCTION PROTOTYPES CPU_TSxx_to_uSec() Note #2'.
  1375. *
  1376. * (2) Although it is not typical, it is possible for an interrupts disabled time
  1377. * measurement to be less than the interrupts disabled time measurement overhead;
  1378. * especially if the overhead was calculated with a single, non-cached measurement
  1379. * & critical sections are called within instruction-cached loops.
  1380. *********************************************************************************************************
  1381. */
  1382. #ifdef CPU_CFG_INT_DIS_MEAS_EN
  1383. static CPU_TS_TMR CPU_IntDisMeasMaxCalc (CPU_TS_TMR time_tot_cnts)
  1384. {
  1385. CPU_TS_TMR time_max_cnts;
  1386. time_max_cnts = time_tot_cnts;
  1387. if (time_max_cnts > CPU_IntDisMeasOvrhd_cnts) { /* If max ints dis'd time > ovrhd time, ... */
  1388. time_max_cnts -= CPU_IntDisMeasOvrhd_cnts; /* ... adj max ints dis'd time by ovrhd time; ... */
  1389. } else { /* ... else max ints dis'd time < ovrhd time, ... */
  1390. time_max_cnts = 0u; /* ... clr max ints dis'd time (see Note #2). */
  1391. }
  1392. return (time_max_cnts);
  1393. }
  1394. #endif