Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

1361 строка
62 KiB

  1. /*
  2. *********************************************************************************************************
  3. * uC/LIB
  4. * Custom Library Modules
  5. *
  6. * Copyright 2004-2021 Silicon Laboratories Inc. www.silabs.com
  7. *
  8. * SPDX-License-Identifier: APACHE-2.0
  9. *
  10. * This software is subject to an open source license and is distributed by
  11. * Silicon Laboratories Inc. pursuant to the terms of the Apache License,
  12. * Version 2.0 available at www.apache.org/licenses/LICENSE-2.0.
  13. *
  14. *********************************************************************************************************
  15. */
  16. /*
  17. *********************************************************************************************************
  18. *
  19. * CORE CUSTOM LIBRARY MODULE
  20. *
  21. * Filename : lib_def.h
  22. * Version : V1.39.01
  23. *********************************************************************************************************
  24. * Note(s) : (1) Assumes the following versions (or more recent) of software modules are included in
  25. * the project build :
  26. *
  27. * (a) uC/CPU V1.29.00
  28. *
  29. *
  30. * (2) NO compiler-supplied standard library functions are used in library or product software.
  31. *
  32. * (a) ALL standard library functions are implemented in the custom library modules :
  33. *
  34. * (1) \<Custom Library Directory>\lib_*.*
  35. *
  36. * (2) \<Custom Library Directory>\Ports\<cpu>\<compiler>\lib*_a.*
  37. *
  38. * where
  39. * <Custom Library Directory> directory path for custom library software
  40. * <cpu> directory name for specific processor (CPU)
  41. * <compiler> directory name for specific compiler
  42. *
  43. * (b) Product-specific library functions are implemented in individual products.
  44. *********************************************************************************************************
  45. */
  46. /*
  47. *********************************************************************************************************
  48. * MODULE
  49. *
  50. * Note(s) : (1) This library definition header file is protected from multiple pre-processor inclusion
  51. * through use of the library definition module present pre-processor macro definition.
  52. *********************************************************************************************************
  53. */
  54. #ifndef LIB_DEF_MODULE_PRESENT
  55. #define LIB_DEF_MODULE_PRESENT
  56. /*
  57. *********************************************************************************************************
  58. * CUSTOM LIBRARY MODULE VERSION NUMBER
  59. *
  60. * Note(s) : (1) (a) The custom library module software version is denoted as follows :
  61. *
  62. * Vx.yy.zz
  63. *
  64. * where
  65. * V denotes 'Version' label
  66. * x denotes major software version revision number
  67. * yy denotes minor software version revision number
  68. * zz denotes sub-minor software version revision number
  69. *
  70. * (b) The software version label #define is formatted as follows :
  71. *
  72. * ver = x.yyzz * 100 * 100
  73. *
  74. * where
  75. * ver denotes software version number scaled as an integer value
  76. * x.yyzz denotes software version number, where the unscaled integer
  77. * portion denotes the major version number & the unscaled
  78. * fractional portion denotes the (concatenated) minor
  79. * version numbers
  80. *********************************************************************************************************
  81. */
  82. #define LIB_VERSION 13901u /* See Note #1. */
  83. /*
  84. *********************************************************************************************************
  85. * INCLUDE FILES
  86. *
  87. * Note(s) : (1) The custom library software files are located in the following directories :
  88. *
  89. * (a) \<Custom Library Directory>\lib_*.*
  90. *
  91. * where
  92. * <Custom Library Directory> directory path for custom library software
  93. *
  94. * (2) CPU-configuration software files are located in the following directories :
  95. *
  96. * (a) \<CPU-Compiler Directory>\cpu_*.*
  97. * (b) \<CPU-Compiler Directory>\<cpu>\<compiler>\cpu*.*
  98. *
  99. * where
  100. * <CPU-Compiler Directory> directory path for common CPU-compiler software
  101. * <cpu> directory name for specific processor (CPU)
  102. * <compiler> directory name for specific compiler
  103. *
  104. * (3) Compiler MUST be configured to include as additional include path directories :
  105. *
  106. * (a) '\<Custom Library Directory>\' directory See Note #1a
  107. *
  108. * (b) (1) '\<CPU-Compiler Directory>\' directory See Note #2a
  109. * (2) '\<CPU-Compiler Directory>\<cpu>\<compiler>\' directory See Note #2b
  110. *********************************************************************************************************
  111. */
  112. #include <cpu_def.h>
  113. #include <cpu.h>
  114. /*
  115. *********************************************************************************************************
  116. * STANDARD DEFINES
  117. *********************************************************************************************************
  118. */
  119. #define DEF_NULL 0
  120. /* ----------------- BOOLEAN DEFINES ------------------ */
  121. #define DEF_FALSE 0u
  122. #define DEF_TRUE 1u
  123. #define DEF_NO 0u
  124. #define DEF_YES 1u
  125. #define DEF_DISABLED 0u
  126. #define DEF_ENABLED 1u
  127. #define DEF_INACTIVE 0u
  128. #define DEF_ACTIVE 1u
  129. #define DEF_INVALID 0u
  130. #define DEF_VALID 1u
  131. #define DEF_OFF 0u
  132. #define DEF_ON 1u
  133. #define DEF_CLR 0u
  134. #define DEF_SET 1u
  135. #define DEF_FAIL 0u
  136. #define DEF_OK 1u
  137. /* ------------------- BIT DEFINES -------------------- */
  138. #define DEF_BIT_NONE 0x00u
  139. #define DEF_BIT_00 0x01u
  140. #define DEF_BIT_01 0x02u
  141. #define DEF_BIT_02 0x04u
  142. #define DEF_BIT_03 0x08u
  143. #define DEF_BIT_04 0x10u
  144. #define DEF_BIT_05 0x20u
  145. #define DEF_BIT_06 0x40u
  146. #define DEF_BIT_07 0x80u
  147. #define DEF_BIT_08 0x0100u
  148. #define DEF_BIT_09 0x0200u
  149. #define DEF_BIT_10 0x0400u
  150. #define DEF_BIT_11 0x0800u
  151. #define DEF_BIT_12 0x1000u
  152. #define DEF_BIT_13 0x2000u
  153. #define DEF_BIT_14 0x4000u
  154. #define DEF_BIT_15 0x8000u
  155. #define DEF_BIT_16 0x00010000u
  156. #define DEF_BIT_17 0x00020000u
  157. #define DEF_BIT_18 0x00040000u
  158. #define DEF_BIT_19 0x00080000u
  159. #define DEF_BIT_20 0x00100000u
  160. #define DEF_BIT_21 0x00200000u
  161. #define DEF_BIT_22 0x00400000u
  162. #define DEF_BIT_23 0x00800000u
  163. #define DEF_BIT_24 0x01000000u
  164. #define DEF_BIT_25 0x02000000u
  165. #define DEF_BIT_26 0x04000000u
  166. #define DEF_BIT_27 0x08000000u
  167. #define DEF_BIT_28 0x10000000u
  168. #define DEF_BIT_29 0x20000000u
  169. #define DEF_BIT_30 0x40000000u
  170. #define DEF_BIT_31 0x80000000u
  171. #define DEF_BIT_32 0x0000000100000000u
  172. #define DEF_BIT_33 0x0000000200000000u
  173. #define DEF_BIT_34 0x0000000400000000u
  174. #define DEF_BIT_35 0x0000000800000000u
  175. #define DEF_BIT_36 0x0000001000000000u
  176. #define DEF_BIT_37 0x0000002000000000u
  177. #define DEF_BIT_38 0x0000004000000000u
  178. #define DEF_BIT_39 0x0000008000000000u
  179. #define DEF_BIT_40 0x0000010000000000u
  180. #define DEF_BIT_41 0x0000020000000000u
  181. #define DEF_BIT_42 0x0000040000000000u
  182. #define DEF_BIT_43 0x0000080000000000u
  183. #define DEF_BIT_44 0x0000100000000000u
  184. #define DEF_BIT_45 0x0000200000000000u
  185. #define DEF_BIT_46 0x0000400000000000u
  186. #define DEF_BIT_47 0x0000800000000000u
  187. #define DEF_BIT_48 0x0001000000000000u
  188. #define DEF_BIT_49 0x0002000000000000u
  189. #define DEF_BIT_50 0x0004000000000000u
  190. #define DEF_BIT_51 0x0008000000000000u
  191. #define DEF_BIT_52 0x0010000000000000u
  192. #define DEF_BIT_53 0x0020000000000000u
  193. #define DEF_BIT_54 0x0040000000000000u
  194. #define DEF_BIT_55 0x0080000000000000u
  195. #define DEF_BIT_56 0x0100000000000000u
  196. #define DEF_BIT_57 0x0200000000000000u
  197. #define DEF_BIT_58 0x0400000000000000u
  198. #define DEF_BIT_59 0x0800000000000000u
  199. #define DEF_BIT_60 0x1000000000000000u
  200. #define DEF_BIT_61 0x2000000000000000u
  201. #define DEF_BIT_62 0x4000000000000000u
  202. #define DEF_BIT_63 0x8000000000000000u
  203. /* ------------------ ALIGN DEFINES ------------------- */
  204. #define DEF_ALIGN_MAX_NBR_OCTETS 4096u
  205. /* ------------------ OCTET DEFINES ------------------- */
  206. #define DEF_OCTET_NBR_BITS 8u
  207. #define DEF_OCTET_MASK 0xFFu
  208. #define DEF_OCTET_TO_BIT_NBR_BITS 3u
  209. #define DEF_OCTET_TO_BIT_SHIFT DEF_OCTET_TO_BIT_NBR_BITS
  210. #define DEF_OCTET_TO_BIT_MASK 0x07u
  211. #define DEF_NIBBLE_NBR_BITS 4u
  212. #define DEF_NIBBLE_MASK 0x0Fu
  213. /* --------------- NUMBER BASE DEFINES ---------------- */
  214. #define DEF_NBR_BASE_BIN 2u
  215. #define DEF_NBR_BASE_OCT 8u
  216. #define DEF_NBR_BASE_DEC 10u
  217. #define DEF_NBR_BASE_HEX 16u
  218. /* ----------------- INTEGER DEFINES ------------------ */
  219. #define DEF_INT_08_NBR_BITS 8u
  220. #define DEF_INT_08_MASK 0xFFu
  221. #define DEF_INT_08U_MIN_VAL 0u
  222. #define DEF_INT_08U_MAX_VAL 255u
  223. #define DEF_INT_08S_MIN_VAL_ONES_CPL (-127)
  224. #define DEF_INT_08S_MAX_VAL_ONES_CPL 127
  225. #define DEF_INT_08S_MIN_VAL (DEF_INT_08S_MIN_VAL_ONES_CPL - 1)
  226. #define DEF_INT_08S_MAX_VAL DEF_INT_08S_MAX_VAL_ONES_CPL
  227. #define DEF_INT_08U_NBR_DIG_MIN 1u
  228. #define DEF_INT_08U_NBR_DIG_MAX 3u
  229. #define DEF_INT_08S_NBR_DIG_MIN 3u
  230. #define DEF_INT_08S_NBR_DIG_MAX 3u
  231. #define DEF_INT_16_NBR_BITS 16u
  232. #define DEF_INT_16_MASK 0xFFFFu
  233. #define DEF_INT_16U_MIN_VAL 0u
  234. #define DEF_INT_16U_MAX_VAL 65535u
  235. #define DEF_INT_16S_MIN_VAL_ONES_CPL (-32767)
  236. #define DEF_INT_16S_MAX_VAL_ONES_CPL 32767
  237. #define DEF_INT_16S_MIN_VAL (DEF_INT_16S_MIN_VAL_ONES_CPL - 1)
  238. #define DEF_INT_16S_MAX_VAL DEF_INT_16S_MAX_VAL_ONES_CPL
  239. #define DEF_INT_16U_NBR_DIG_MIN 1u
  240. #define DEF_INT_16U_NBR_DIG_MAX 5u
  241. #define DEF_INT_16S_NBR_DIG_MIN 5u
  242. #define DEF_INT_16S_NBR_DIG_MAX 5u
  243. #define DEF_INT_32_NBR_BITS 32u
  244. #define DEF_INT_32_MASK 0xFFFFFFFFu
  245. #define DEF_INT_32U_MIN_VAL 0u
  246. #define DEF_INT_32U_MAX_VAL 4294967295u
  247. #define DEF_INT_32S_MIN_VAL_ONES_CPL (-2147483647)
  248. #define DEF_INT_32S_MAX_VAL_ONES_CPL 2147483647
  249. #define DEF_INT_32S_MIN_VAL (DEF_INT_32S_MIN_VAL_ONES_CPL - 1)
  250. #define DEF_INT_32S_MAX_VAL DEF_INT_32S_MAX_VAL_ONES_CPL
  251. #define DEF_INT_32U_NBR_DIG_MIN 1u
  252. #define DEF_INT_32U_NBR_DIG_MAX 10u
  253. #define DEF_INT_32S_NBR_DIG_MIN 10u
  254. #define DEF_INT_32S_NBR_DIG_MAX 10u
  255. #define DEF_INT_64_NBR_BITS 64u
  256. #define DEF_INT_64_MASK 0xFFFFFFFFFFFFFFFFu
  257. #define DEF_INT_64U_MIN_VAL 0u
  258. #define DEF_INT_64U_MAX_VAL 18446744073709551615u
  259. #define DEF_INT_64S_MIN_VAL_ONES_CPL (-9223372036854775807)
  260. #define DEF_INT_64S_MAX_VAL_ONES_CPL 9223372036854775807
  261. #define DEF_INT_64S_MIN_VAL (DEF_INT_64S_MIN_VAL_ONES_CPL - 1)
  262. #define DEF_INT_64S_MAX_VAL DEF_INT_64S_MAX_VAL_ONES_CPL
  263. #define DEF_INT_64U_NBR_DIG_MIN 1u
  264. #define DEF_INT_64U_NBR_DIG_MAX 20u
  265. #define DEF_INT_64S_NBR_DIG_MIN 19u
  266. #define DEF_INT_64S_NBR_DIG_MAX 19u
  267. /* --------------- CPU INTEGER DEFINES ---------------- */
  268. #define DEF_INT_CPU_NBR_BITS (CPU_CFG_DATA_SIZE * DEF_OCTET_NBR_BITS)
  269. #define DEF_INT_CPU_NBR_BITS_MAX (CPU_CFG_DATA_SIZE_MAX * DEF_OCTET_NBR_BITS)
  270. #if (DEF_INT_CPU_NBR_BITS == DEF_INT_08_NBR_BITS)
  271. #define DEF_INT_CPU_MASK DEF_INT_08_MASK
  272. #define DEF_INT_CPU_U_MIN_VAL DEF_INT_08U_MIN_VAL
  273. #define DEF_INT_CPU_U_MAX_VAL DEF_INT_08U_MAX_VAL
  274. #define DEF_INT_CPU_S_MIN_VAL DEF_INT_08S_MIN_VAL
  275. #define DEF_INT_CPU_S_MAX_VAL DEF_INT_08S_MAX_VAL
  276. #define DEF_INT_CPU_S_MIN_VAL_ONES_CPL DEF_INT_08S_MIN_VAL_ONES_CPL
  277. #define DEF_INT_CPU_S_MAX_VAL_ONES_CPL DEF_INT_08S_MAX_VAL_ONES_CPL
  278. #elif (DEF_INT_CPU_NBR_BITS == DEF_INT_16_NBR_BITS)
  279. #define DEF_INT_CPU_MASK DEF_INT_16_MASK
  280. #define DEF_INT_CPU_U_MIN_VAL DEF_INT_16U_MIN_VAL
  281. #define DEF_INT_CPU_U_MAX_VAL DEF_INT_16U_MAX_VAL
  282. #define DEF_INT_CPU_S_MIN_VAL DEF_INT_16S_MIN_VAL
  283. #define DEF_INT_CPU_S_MAX_VAL DEF_INT_16S_MAX_VAL
  284. #define DEF_INT_CPU_S_MIN_VAL_ONES_CPL DEF_INT_16S_MIN_VAL_ONES_CPL
  285. #define DEF_INT_CPU_S_MAX_VAL_ONES_CPL DEF_INT_16S_MAX_VAL_ONES_CPL
  286. #elif (DEF_INT_CPU_NBR_BITS == DEF_INT_32_NBR_BITS)
  287. #define DEF_INT_CPU_MASK DEF_INT_32_MASK
  288. #define DEF_INT_CPU_U_MIN_VAL DEF_INT_32U_MIN_VAL
  289. #define DEF_INT_CPU_U_MAX_VAL DEF_INT_32U_MAX_VAL
  290. #define DEF_INT_CPU_S_MIN_VAL DEF_INT_32S_MIN_VAL
  291. #define DEF_INT_CPU_S_MAX_VAL DEF_INT_32S_MAX_VAL
  292. #define DEF_INT_CPU_S_MIN_VAL_ONES_CPL DEF_INT_32S_MIN_VAL_ONES_CPL
  293. #define DEF_INT_CPU_S_MAX_VAL_ONES_CPL DEF_INT_32S_MAX_VAL_ONES_CPL
  294. #elif (DEF_INT_CPU_NBR_BITS == DEF_INT_64_NBR_BITS)
  295. #define DEF_INT_CPU_MASK DEF_INT_64_MASK
  296. #define DEF_INT_CPU_U_MIN_VAL DEF_INT_64U_MIN_VAL
  297. #define DEF_INT_CPU_U_MAX_VAL DEF_INT_64U_MAX_VAL
  298. #define DEF_INT_CPU_S_MIN_VAL DEF_INT_64S_MIN_VAL
  299. #define DEF_INT_CPU_S_MAX_VAL DEF_INT_64S_MAX_VAL
  300. #define DEF_INT_CPU_S_MIN_VAL_ONES_CPL DEF_INT_64S_MIN_VAL_ONES_CPL
  301. #define DEF_INT_CPU_S_MAX_VAL_ONES_CPL DEF_INT_64S_MAX_VAL_ONES_CPL
  302. #else
  303. #error "CPU_CFG_DATA_SIZE illegally #defined in 'cpu.h' "
  304. #error " [See 'cpu.h CONFIGURATION ERRORS']"
  305. #endif
  306. /* ------------------- TIME DEFINES ------------------- */
  307. #define DEF_TIME_NBR_DAY_PER_WK 7u
  308. #define DEF_TIME_NBR_DAY_PER_YR 365u
  309. #define DEF_TIME_NBR_DAY_PER_YR_LEAP 366u
  310. #define DEF_TIME_NBR_HR_PER_DAY 24u
  311. #define DEF_TIME_NBR_HR_PER_WK (DEF_TIME_NBR_HR_PER_DAY * DEF_TIME_NBR_DAY_PER_WK )
  312. #define DEF_TIME_NBR_HR_PER_YR (DEF_TIME_NBR_HR_PER_DAY * DEF_TIME_NBR_DAY_PER_YR )
  313. #define DEF_TIME_NBR_HR_PER_YR_LEAP (DEF_TIME_NBR_HR_PER_DAY * DEF_TIME_NBR_DAY_PER_YR_LEAP)
  314. #define DEF_TIME_NBR_MIN_PER_HR 60u
  315. #define DEF_TIME_NBR_MIN_PER_DAY (DEF_TIME_NBR_MIN_PER_HR * DEF_TIME_NBR_HR_PER_DAY )
  316. #define DEF_TIME_NBR_MIN_PER_WK (DEF_TIME_NBR_MIN_PER_DAY * DEF_TIME_NBR_DAY_PER_WK )
  317. #define DEF_TIME_NBR_MIN_PER_YR (DEF_TIME_NBR_MIN_PER_DAY * DEF_TIME_NBR_DAY_PER_YR )
  318. #define DEF_TIME_NBR_MIN_PER_YR_LEAP (DEF_TIME_NBR_MIN_PER_DAY * DEF_TIME_NBR_DAY_PER_YR_LEAP)
  319. #define DEF_TIME_NBR_SEC_PER_MIN 60u
  320. #define DEF_TIME_NBR_SEC_PER_HR (DEF_TIME_NBR_SEC_PER_MIN * DEF_TIME_NBR_MIN_PER_HR )
  321. #define DEF_TIME_NBR_SEC_PER_DAY (DEF_TIME_NBR_SEC_PER_HR * DEF_TIME_NBR_HR_PER_DAY )
  322. #define DEF_TIME_NBR_SEC_PER_WK (DEF_TIME_NBR_SEC_PER_DAY * DEF_TIME_NBR_DAY_PER_WK )
  323. #define DEF_TIME_NBR_SEC_PER_YR (DEF_TIME_NBR_SEC_PER_DAY * DEF_TIME_NBR_DAY_PER_YR )
  324. #define DEF_TIME_NBR_SEC_PER_YR_LEAP (DEF_TIME_NBR_SEC_PER_DAY * DEF_TIME_NBR_DAY_PER_YR_LEAP)
  325. #define DEF_TIME_NBR_mS_PER_SEC 1000u
  326. #define DEF_TIME_NBR_uS_PER_SEC 1000000u
  327. #define DEF_TIME_NBR_nS_PER_SEC 1000000000u
  328. /*
  329. *********************************************************************************************************
  330. * ERROR CODES
  331. *
  332. * Note(s) : (1) All library error codes are #define'd in 'lib_def.h';
  333. *********************************************************************************************************
  334. */
  335. typedef enum lib_err {
  336. LIB_ERR_NONE = 0u,
  337. LIB_MEM_ERR_NONE = 10000u,
  338. LIB_MEM_ERR_NULL_PTR = 10001u, /* Ptr arg(s) passed NULL ptr(s). */
  339. LIB_MEM_ERR_INVALID_MEM_SIZE = 10100u, /* Invalid mem size. */
  340. LIB_MEM_ERR_INVALID_MEM_ALIGN = 10101u, /* Invalid mem align. */
  341. LIB_MEM_ERR_INVALID_SEG_SIZE = 10110u, /* Invalid mem seg size. */
  342. LIB_MEM_ERR_INVALID_SEG_OVERLAP = 10111u, /* Invalid mem seg overlaps other mem seg(s). */
  343. LIB_MEM_ERR_INVALID_SEG_EXISTS = 10112u, /* Invalid mem seg already exists. */
  344. LIB_MEM_ERR_INVALID_POOL = 10120u, /* Invalid mem pool. */
  345. LIB_MEM_ERR_INVALID_BLK_NBR = 10130u, /* Invalid mem pool blk nbr. */
  346. LIB_MEM_ERR_INVALID_BLK_SIZE = 10131u, /* Invalid mem pool blk size. */
  347. LIB_MEM_ERR_INVALID_BLK_ALIGN = 10132u, /* Invalid mem pool blk align. */
  348. LIB_MEM_ERR_INVALID_BLK_IX = 10133u, /* Invalid mem pool ix. */
  349. LIB_MEM_ERR_INVALID_BLK_ADDR = 10135u, /* Invalid mem pool blk addr. */
  350. LIB_MEM_ERR_INVALID_BLK_ADDR_IN_POOL = 10136u, /* Mem pool blk addr already in mem pool. */
  351. LIB_MEM_ERR_SEG_EMPTY = 10200u, /* Mem seg empty; i.e. NO avail mem in seg. */
  352. LIB_MEM_ERR_SEG_OVF = 10201u, /* Mem seg ovf; i.e. req'd mem ovfs rem mem in seg. */
  353. LIB_MEM_ERR_POOL_FULL = 10205u, /* Mem pool full; i.e. all mem blks avail in mem pool. */
  354. LIB_MEM_ERR_POOL_EMPTY = 10206u, /* Mem pool empty; i.e. NO mem blks avail in mem pool. */
  355. LIB_MEM_ERR_POOL_UNLIMITED = 10207u, /* Mem pool is unlimited. */
  356. LIB_MEM_ERR_HEAP_EMPTY = 10210u, /* Heap seg empty; i.e. NO avail mem in heap. */
  357. LIB_MEM_ERR_HEAP_OVF = 10211u, /* Heap seg ovf; i.e. req'd mem ovfs rem mem in heap. */
  358. LIB_MEM_ERR_HEAP_NOT_FOUND = 10215u, /* Heap seg NOT found. */
  359. LIB_MEM_ERR_ADDR_OVF = 10300u /* Memory allocation exceeds address space. */
  360. } LIB_ERR;
  361. /*
  362. *********************************************************************************************************
  363. * DATA TYPES
  364. *********************************************************************************************************
  365. */
  366. /*
  367. *********************************************************************************************************
  368. * GLOBAL VARIABLES
  369. *********************************************************************************************************
  370. */
  371. /*
  372. *********************************************************************************************************
  373. * TRACING
  374. *********************************************************************************************************
  375. */
  376. /* Trace level, default to TRACE_LEVEL_OFF. */
  377. #ifndef TRACE_LEVEL_OFF
  378. #define TRACE_LEVEL_OFF 0u
  379. #endif
  380. #ifndef TRACE_LEVEL_INFO
  381. #define TRACE_LEVEL_INFO 1u
  382. #endif
  383. #ifndef TRACE_LEVEL_DBG
  384. #define TRACE_LEVEL_DBG 2u
  385. #endif
  386. #ifndef TRACE_LEVEL_LOG
  387. #define TRACE_LEVEL_LOG 3u
  388. #endif
  389. /*
  390. *********************************************************************************************************
  391. * BIT MACRO'S
  392. *********************************************************************************************************
  393. */
  394. /*
  395. *********************************************************************************************************
  396. * DEF_BIT()
  397. *
  398. * Description : Create bit mask with single, specified bit set.
  399. *
  400. * Argument(s) : bit Bit number of bit to set.
  401. *
  402. * Return(s) : Bit mask with single, specified bit set.
  403. *
  404. * Caller(s) : Application.
  405. *
  406. * Note(s) : (1) 'bit' SHOULD be a non-negative integer.
  407. *
  408. * (2) (a) 'bit' values that overflow the target CPU &/or compiler environment (e.g. negative
  409. * or greater-than-CPU-data-size values) MAY generate compiler warnings &/or errors.
  410. *********************************************************************************************************
  411. */
  412. #define DEF_BIT(bit) (1uL << (bit))
  413. /*
  414. *********************************************************************************************************
  415. * DEF_BITxx()
  416. *
  417. * Description : Create bit mask of specified bit size with single, specified bit set.
  418. *
  419. * Argument(s) : bit Bit number of bit to set.
  420. *
  421. * Return(s) : Bit mask with single, specified bit set.
  422. *
  423. * Caller(s) : Application.
  424. *
  425. * Note(s) : (1) 'bit' SHOULD be a non-negative integer.
  426. *
  427. * (2) (a) 'bit' values that overflow the target CPU &/or compiler environment (e.g. negative
  428. * or greater-than-CPU-data-size values) MAY generate compiler warnings &/or errors.
  429. *
  430. * (b) To avoid overflowing any target CPU &/or compiler's integer data type, unsigned
  431. * bit constant '1' is cast to specified integer data type size.
  432. *
  433. * (3) Ideally, DEF_BITxx() macro's should be named DEF_BIT_xx(); however, these names already
  434. * previously-released for bit constant #define's (see 'STANDARD DEFINES BIT DEFINES').
  435. *********************************************************************************************************
  436. */
  437. #define DEF_BIT08(bit) ((CPU_INT08U)((CPU_INT08U)1u << (bit)))
  438. #define DEF_BIT16(bit) ((CPU_INT16U)((CPU_INT16U)1u << (bit)))
  439. #define DEF_BIT32(bit) ((CPU_INT32U)((CPU_INT32U)1u << (bit)))
  440. #define DEF_BIT64(bit) ((CPU_INT64U)((CPU_INT64U)1u << (bit)))
  441. /*
  442. *********************************************************************************************************
  443. * DEF_BIT_MASK()
  444. *
  445. * Description : Shift a bit mask.
  446. *
  447. * Argument(s) : bit_mask Bit mask to shift.
  448. *
  449. * bit_shift Number of bit positions to left-shift bit mask.
  450. *
  451. * Return(s) : Shifted bit mask.
  452. *
  453. * Caller(s) : Application.
  454. *
  455. * Note(s) : (1) (a) 'bit_mask' SHOULD be an unsigned integer.
  456. *
  457. * (b) 'bit_shift' SHOULD be a non-negative integer.
  458. *
  459. * (2) 'bit_shift' values that overflow the target CPU &/or compiler environment (e.g. negative
  460. * or greater-than-CPU-data-size values) MAY generate compiler warnings &/or errors.
  461. *********************************************************************************************************
  462. */
  463. #define DEF_BIT_MASK(bit_mask, bit_shift) ((bit_mask) << (bit_shift))
  464. /*
  465. *********************************************************************************************************
  466. * DEF_BIT_MASK_xx()
  467. *
  468. * Description : Shift a bit mask of specified bit size.
  469. *
  470. * Argument(s) : bit_mask Bit mask to shift.
  471. *
  472. * bit_shift Number of bit positions to left-shift bit mask.
  473. *
  474. * Return(s) : Shifted bit mask.
  475. *
  476. * Caller(s) : Application.
  477. *
  478. * Note(s) : (1) (a) 'bit_mask' SHOULD be an unsigned integer.
  479. *
  480. * (b) 'bit_shift' SHOULD be a non-negative integer.
  481. *
  482. * (2) 'bit_shift' values that overflow the target CPU &/or compiler environment (e.g. negative
  483. * or greater-than-CPU-data-size values) MAY generate compiler warnings &/or errors.
  484. *********************************************************************************************************
  485. */
  486. #define DEF_BIT_MASK_08(bit_mask, bit_shift) ((CPU_INT08U)((CPU_INT08U)(bit_mask) << (bit_shift)))
  487. #define DEF_BIT_MASK_16(bit_mask, bit_shift) ((CPU_INT16U)((CPU_INT16U)(bit_mask) << (bit_shift)))
  488. #define DEF_BIT_MASK_32(bit_mask, bit_shift) ((CPU_INT32U)((CPU_INT32U)(bit_mask) << (bit_shift)))
  489. #define DEF_BIT_MASK_64(bit_mask, bit_shift) ((CPU_INT64U)((CPU_INT64U)(bit_mask) << (bit_shift)))
  490. /*
  491. *********************************************************************************************************
  492. * DEF_BIT_FIELD()
  493. *
  494. * Description : Create & shift a contiguous bit field.
  495. *
  496. * Argument(s) : bit_field Number of contiguous bits to set in the bit field.
  497. *
  498. * bit_shift Number of bit positions to left-shift bit field.
  499. *
  500. * Return(s) : Shifted bit field.
  501. *
  502. * Caller(s) : Application.
  503. *
  504. * Note(s) : (1) 'bit_field' & 'bit_shift' SHOULD be non-negative integers.
  505. *
  506. * (2) (a) 'bit_field'/'bit_shift' values that overflow the target CPU &/or compiler
  507. * environment (e.g. negative or greater-than-CPU-data-size values) MAY generate
  508. * compiler warnings &/or errors.
  509. *
  510. * (b) To avoid overflowing any target CPU &/or compiler's integer data type, unsigned
  511. * bit constant '1' is suffixed with 'L'ong integer modifier.
  512. *
  513. * This may still be insufficient for CPUs &/or compilers that support 'long long'
  514. * integer data types, in which case 'LL' integer modifier should be suffixed.
  515. * However, since almost all 16- & 32-bit CPUs & compilers support 'long' integer
  516. * data types but many may NOT support 'long long' integer data types, only 'long'
  517. * integer data types & modifiers are supported.
  518. *
  519. * See also 'DEF_BIT_FIELD_xx() Note #1b'.
  520. *********************************************************************************************************
  521. */
  522. #define DEF_BIT_FIELD(bit_field, bit_shift) ((((bit_field) >= DEF_INT_CPU_NBR_BITS) ? (DEF_INT_CPU_U_MAX_VAL) \
  523. : (DEF_BIT(bit_field) - 1uL)) \
  524. << (bit_shift))
  525. /*
  526. *********************************************************************************************************
  527. * DEF_BIT_FIELD_xx()
  528. *
  529. * Description : Create & shift a contiguous bit field of specified bit size.
  530. *
  531. * Argument(s) : bit_field Number of contiguous bits to set in the bit field.
  532. *
  533. * bit_shift Number of bit positions to left-shift bit field.
  534. *
  535. * Return(s) : Shifted bit field.
  536. *
  537. * Caller(s) : Application.
  538. *
  539. * Note(s) : (1) 'bit_field' & 'bit_shift' SHOULD be non-negative integers.
  540. *
  541. * (2) (a) 'bit_field'/'bit_shift' values that overflow the target CPU &/or compiler
  542. * environment (e.g. negative or greater-than-CPU-data-size values) MAY generate
  543. * compiler warnings &/or errors.
  544. *
  545. * (b) To avoid overflowing any target CPU &/or compiler's integer data type, unsigned
  546. * bit constant '1' is cast to specified integer data type size.
  547. *********************************************************************************************************
  548. */
  549. #define DEF_BIT_FIELD_08(bit_field, bit_shift) ((CPU_INT08U)((((CPU_INT08U)(bit_field) >= (CPU_INT08U)DEF_INT_08_NBR_BITS) ? (CPU_INT08U)(DEF_INT_08U_MAX_VAL) \
  550. : (CPU_INT08U)(DEF_BIT08(bit_field) - (CPU_INT08U)1u)) \
  551. << (bit_shift)))
  552. #define DEF_BIT_FIELD_16(bit_field, bit_shift) ((CPU_INT16U)((((CPU_INT16U)(bit_field) >= (CPU_INT16U)DEF_INT_16_NBR_BITS) ? (CPU_INT16U)(DEF_INT_16U_MAX_VAL) \
  553. : (CPU_INT16U)(DEF_BIT16(bit_field) - (CPU_INT16U)1u)) \
  554. << (bit_shift)))
  555. #define DEF_BIT_FIELD_32(bit_field, bit_shift) ((CPU_INT32U)((((CPU_INT32U)(bit_field) >= (CPU_INT32U)DEF_INT_32_NBR_BITS) ? (CPU_INT32U)(DEF_INT_32U_MAX_VAL) \
  556. : (CPU_INT32U)(DEF_BIT32(bit_field) - (CPU_INT32U)1u)) \
  557. << (bit_shift)))
  558. #define DEF_BIT_FIELD_64(bit_field, bit_shift) ((CPU_INT64U)((((CPU_INT64U)(bit_field) >= (CPU_INT64U)DEF_INT_64_NBR_BITS) ? (CPU_INT64U)(DEF_INT_64U_MAX_VAL) \
  559. : (CPU_INT64U)(DEF_BIT64(bit_field) - (CPU_INT64U)1u)) \
  560. << (bit_shift)))
  561. /*
  562. *********************************************************************************************************
  563. * DEF_BIT_SET()
  564. *
  565. * Description : Set specified bit(s) in a value.
  566. *
  567. * Argument(s) : val Value to modify by setting specified bit(s).
  568. *
  569. * mask Mask of bits to set.
  570. *
  571. * Return(s) : Modified value with specified bit(s) set.
  572. *
  573. * Caller(s) : Application.
  574. *
  575. * Note(s) : (1) 'val' & 'mask' SHOULD be unsigned integers.
  576. *********************************************************************************************************
  577. */
  578. #define DEF_BIT_SET(val, mask) ((val) = ((val) | (mask)))
  579. /*
  580. *********************************************************************************************************
  581. * DEF_BIT_SET_xx()
  582. *
  583. * Description : Set specified bit(s) in a value of specified bit size.
  584. *
  585. * Argument(s) : val Value to modify by setting specified bit(s).
  586. *
  587. * mask Mask of bits to set.
  588. *
  589. * Return(s) : Modified value with specified bit(s) set.
  590. *
  591. * Caller(s) : Application.
  592. *
  593. * Note(s) : (1) 'val' & 'mask' SHOULD be unsigned integers.
  594. *
  595. * (2) These macros are deprecated and should be replaced by the DEF_BIT_SET macro.
  596. *********************************************************************************************************
  597. */
  598. #define DEF_BIT_SET_08(val, mask) (CPU_INT08U)DEF_BIT_SET((val), (CPU_INT08U)(mask))
  599. #define DEF_BIT_SET_16(val, mask) (CPU_INT16U)DEF_BIT_SET((val), (CPU_INT16U)(mask))
  600. #define DEF_BIT_SET_32(val, mask) (CPU_INT32U)DEF_BIT_SET((val), (CPU_INT32U)(mask))
  601. #define DEF_BIT_SET_64(val, mask) (CPU_INT64U)DEF_BIT_SET((val), (CPU_INT64U)(mask))
  602. /*
  603. *********************************************************************************************************
  604. * DEF_BIT_CLR_xx()
  605. *
  606. * Description : Clear specified bit(s) in a value of specified bit size.
  607. *
  608. * Argument(s) : val Value to modify by clearing specified bit(s).
  609. *
  610. * mask Mask of bits to clear.
  611. *
  612. * Return(s) : Modified value with specified bit(s) clear.
  613. *
  614. * Caller(s) : Application.
  615. *
  616. * Note(s) : (1) 'val' & 'mask' SHOULD be unsigned integers.
  617. *********************************************************************************************************
  618. */
  619. #define DEF_BIT_CLR_08(val, mask) ((val) = (CPU_INT08U)(((CPU_INT08U)(val)) & (CPU_INT08U)(~((CPU_INT08U)(mask)))))
  620. #define DEF_BIT_CLR_16(val, mask) ((val) = (CPU_INT16U)(((CPU_INT16U)(val)) & (CPU_INT16U)(~((CPU_INT16U)(mask)))))
  621. #define DEF_BIT_CLR_32(val, mask) ((val) = (CPU_INT32U)(((CPU_INT32U)(val)) & (CPU_INT32U)(~((CPU_INT32U)(mask)))))
  622. #define DEF_BIT_CLR_64(val, mask) ((val) = (CPU_INT64U)(((CPU_INT64U)(val)) & (CPU_INT64U)(~((CPU_INT64U)(mask)))))
  623. /*
  624. *********************************************************************************************************
  625. * DEF_BIT_CLR()
  626. *
  627. * Description : Clear specified bit(s) in a value.
  628. *
  629. * Argument(s) : val Value to modify by clearing specified bit(s).
  630. *
  631. * mask Mask of bits to clear.
  632. *
  633. * Return(s) : Modified value with specified bit(s) clear.
  634. *
  635. * Caller(s) : Application.
  636. *
  637. * Note(s) : (1) 'val' & 'mask' SHOULD be unsigned integers.
  638. *********************************************************************************************************
  639. */
  640. #if (CPU_CFG_DATA_SIZE_MAX == CPU_WORD_SIZE_08)
  641. #define DEF_BIT_CLR(val, mask) ((sizeof((val)) == CPU_WORD_SIZE_08) ? DEF_BIT_CLR_08((val), (mask)) : 0)
  642. #elif (CPU_CFG_DATA_SIZE_MAX == CPU_WORD_SIZE_16)
  643. #define DEF_BIT_CLR(val, mask) ((sizeof((val)) == CPU_WORD_SIZE_08) ? DEF_BIT_CLR_08((val), (mask)) : \
  644. ((sizeof((val)) == CPU_WORD_SIZE_16) ? DEF_BIT_CLR_16((val), (mask)) : 0))
  645. #elif (CPU_CFG_DATA_SIZE_MAX == CPU_WORD_SIZE_32)
  646. #define DEF_BIT_CLR(val, mask) ((sizeof((val)) == CPU_WORD_SIZE_08) ? DEF_BIT_CLR_08((val), (mask)) : \
  647. ((sizeof((val)) == CPU_WORD_SIZE_16) ? DEF_BIT_CLR_16((val), (mask)) : \
  648. ((sizeof((val)) == CPU_WORD_SIZE_32) ? DEF_BIT_CLR_32((val), (mask)) : 0)))
  649. #elif (CPU_CFG_DATA_SIZE_MAX == CPU_WORD_SIZE_64)
  650. #define DEF_BIT_CLR(val, mask) ((sizeof((val)) == CPU_WORD_SIZE_08) ? DEF_BIT_CLR_08((val), (mask)) : \
  651. ((sizeof((val)) == CPU_WORD_SIZE_16) ? DEF_BIT_CLR_16((val), (mask)) : \
  652. ((sizeof((val)) == CPU_WORD_SIZE_32) ? DEF_BIT_CLR_32((val), (mask)) : \
  653. ((sizeof((val)) == CPU_WORD_SIZE_64) ? DEF_BIT_CLR_64((val), (mask)) : 0))))
  654. #else
  655. #error "CPU_CFG_DATA_SIZE_MAX illegally #defined in 'cpu.h' "
  656. #error " [See 'cpu.h CONFIGURATION ERRORS']"
  657. #endif
  658. /*
  659. *********************************************************************************************************
  660. * DEF_BIT_TOGGLE()
  661. *
  662. * Description : Toggles specified bit(s) in a value.
  663. *
  664. * Argument(s) : val Value to modify by toggling specified bit(s).
  665. *
  666. * mask Mask of bits to toggle.
  667. *
  668. * Return(s) : Modified value with specified bit(s) toggled.
  669. *
  670. * Caller(s) : Application.
  671. *
  672. * Note(s) : (1) 'val' & 'mask' SHOULD be unsigned integers.
  673. *********************************************************************************************************
  674. */
  675. #define DEF_BIT_TOGGLE(val, mask) ((val) ^= (mask))
  676. /*
  677. *********************************************************************************************************
  678. * DEF_BIT_FIELD_RD()
  679. *
  680. * Description : Reads a 'val' field, masked and shifted, given by mask 'field_mask'.
  681. *
  682. * Argument(s) : val Value to read from.
  683. *
  684. * field_mask Mask of field to read. See note #1, #2 and #3.
  685. *
  686. * Return(s) : Field value, masked and right-shifted to bit position 0.
  687. *
  688. * Caller(s) : Application.
  689. *
  690. * Note(s) : (1) 'field_mask' argument must NOT be 0.
  691. *
  692. * (2) 'field_mask' argument must contain a mask with contiguous set bits.
  693. *
  694. * (3) 'val' & 'field_mask' SHOULD be unsigned integers.
  695. *********************************************************************************************************
  696. */
  697. #define DEF_BIT_FIELD_RD(val, field_mask) (((val) & (field_mask)) / ((field_mask) & ((~(field_mask)) + 1u)))
  698. /*
  699. *********************************************************************************************************
  700. * DEF_BIT_FIELD_ENC()
  701. *
  702. * Description : Encodes given 'field_val' at position given by mask 'field_mask'.
  703. *
  704. * Argument(s) : field_val Value to encode.
  705. *
  706. * field_mask Mask of field to read. See note #1 and #2.
  707. *
  708. * Return(s) : Field value, masked and left-shifted to field position.
  709. *
  710. * Caller(s) : Application.
  711. *
  712. * Note(s) : (1) 'field_mask' argument must contain a mask with contiguous set bits.
  713. *
  714. * (2) 'field_val' & 'field_mask' SHOULD be unsigned integers.
  715. *********************************************************************************************************
  716. */
  717. #define DEF_BIT_FIELD_ENC(field_val, field_mask) (((field_val) * ((field_mask) & ((~(field_mask)) + 1u))) & (field_mask))
  718. /*
  719. *********************************************************************************************************
  720. * DEF_BIT_FIELD_WR()
  721. *
  722. * Description : Writes 'field_val' field at position given by mask 'field_mask' in variable 'var'.
  723. *
  724. * Argument(s) : var Variable to write field to. See note #2.
  725. *
  726. * field_val Desired value for field. See note #2.
  727. *
  728. * field_mask Mask of field to write to. See note #1 and #2.
  729. *
  730. * Return(s) : None.
  731. *
  732. * Caller(s) : Application.
  733. *
  734. * Note(s) : (1) 'field_mask' argument must contain a mask with contiguous set bits.
  735. *
  736. * (2) 'var', 'field_val' & 'field_mask' SHOULD be unsigned integers.
  737. *********************************************************************************************************
  738. */
  739. #define DEF_BIT_FIELD_WR(var, field_val, field_mask) (var) = (((var) & ~(field_mask)) | DEF_BIT_FIELD_ENC((field_val), (field_mask)))
  740. /*
  741. *********************************************************************************************************
  742. * DEF_BIT_IS_SET()
  743. *
  744. * Description : Determine if specified bit(s) in a value are set.
  745. *
  746. * Argument(s) : val Value to check for specified bit(s) set.
  747. *
  748. * mask Mask of bits to check if set (see Note #2).
  749. *
  750. * Return(s) : DEF_YES, if ALL specified bit(s) are set in value.
  751. *
  752. * DEF_NO, if ALL specified bit(s) are NOT set in value.
  753. *
  754. * Caller(s) : Application.
  755. *
  756. * Note(s) : (1) 'val' & 'mask' SHOULD be unsigned integers.
  757. *
  758. * (2) NULL 'mask' allowed; returns 'DEF_NO' since NO mask bits specified.
  759. *********************************************************************************************************
  760. */
  761. #define DEF_BIT_IS_SET(val, mask) (((((val) & (mask)) == (mask)) && \
  762. ((mask) != 0u)) ? (DEF_YES) : (DEF_NO))
  763. /*
  764. *********************************************************************************************************
  765. * DEF_BIT_IS_CLR()
  766. *
  767. * Description : Determine if specified bit(s) in a value are clear.
  768. *
  769. * Argument(s) : val Value to check for specified bit(s) clear.
  770. *
  771. * mask Mask of bits to check if clear (see Note #2).
  772. *
  773. * Return(s) : DEF_YES, if ALL specified bit(s) are clear in value.
  774. *
  775. * DEF_NO, if ALL specified bit(s) are NOT clear in value.
  776. *
  777. * Caller(s) : Application.
  778. *
  779. * Note(s) : (1) 'val' & 'mask' SHOULD be unsigned integers.
  780. *
  781. * (2) NULL 'mask' allowed; returns 'DEF_NO' since NO mask bits specified.
  782. *********************************************************************************************************
  783. */
  784. #define DEF_BIT_IS_CLR(val, mask) (((((val) & (mask)) == 0u) && \
  785. ((mask) != 0u)) ? (DEF_YES) : (DEF_NO))
  786. /*
  787. *********************************************************************************************************
  788. * DEF_BIT_IS_SET_ANY()
  789. *
  790. * Description : Determine if any specified bit(s) in a value are set.
  791. *
  792. * Argument(s) : val Value to check for specified bit(s) set.
  793. *
  794. * mask Mask of bits to check if set (see Note #2).
  795. *
  796. * Return(s) : DEF_YES, if ANY specified bit(s) are set in value.
  797. *
  798. * DEF_NO, if ALL specified bit(s) are NOT set in value.
  799. *
  800. * Caller(s) : Application.
  801. *
  802. * Note(s) : (1) 'val' & 'mask' SHOULD be unsigned integers.
  803. *
  804. * (2) NULL 'mask' allowed; returns 'DEF_NO' since NO mask bits specified.
  805. *********************************************************************************************************
  806. */
  807. #define DEF_BIT_IS_SET_ANY(val, mask) ((((val) & (mask)) == 0u) ? (DEF_NO ) : (DEF_YES))
  808. /*
  809. *********************************************************************************************************
  810. * DEF_BIT_IS_CLR_ANY()
  811. *
  812. * Description : Determine if any specified bit(s) in a value are clear.
  813. *
  814. * Argument(s) : val Value to check for specified bit(s) clear.
  815. *
  816. * mask Mask of bits to check if clear (see Note #2).
  817. *
  818. * Return(s) : DEF_YES, if ANY specified bit(s) are clear in value.
  819. *
  820. * DEF_NO, if ALL specified bit(s) are NOT clear in value.
  821. *
  822. * Note(s) : (1) 'val' & 'mask' SHOULD be unsigned integers.
  823. *
  824. * (2) NULL 'mask' allowed; returns 'DEF_NO' since NO mask bits specified.
  825. *********************************************************************************************************
  826. */
  827. #define DEF_BIT_IS_CLR_ANY(val, mask) ((((val) & (mask)) == (mask)) ? (DEF_NO ) : (DEF_YES))
  828. /*
  829. *********************************************************************************************************
  830. * VALUE MACRO'S
  831. *********************************************************************************************************
  832. */
  833. /*
  834. *********************************************************************************************************
  835. * DEF_CHK_VAL_MIN()
  836. *
  837. * Description : Validate a value as greater than or equal to a specified minimum value.
  838. *
  839. * Argument(s) : val Value to validate.
  840. *
  841. * val_min Minimum value to test.
  842. *
  843. * Return(s) : DEF_OK, Value is greater than or equal to minimum value.
  844. *
  845. * DEF_FAIL, otherwise.
  846. *
  847. * Caller(s) : Application.
  848. *
  849. * Note(s) : (1) DEF_CHK_VAL_MIN() avoids directly comparing any two values if only one of the values
  850. * is negative since the negative value might be incorrectly promoted to an arbitrary
  851. * unsigned value if the other value to compare is unsigned.
  852. *
  853. * (2) Validation of values is limited to the range supported by the compiler &/or target
  854. * environment. All other values that underflow/overflow the supported range will
  855. * modulo/wrap into the supported range as arbitrary signed or unsigned values.
  856. *
  857. * Therefore, any values that underflow the most negative signed value or overflow
  858. * the most positive unsigned value supported by the compiler &/or target environment
  859. * cannot be validated :
  860. *
  861. * ( N-1 N ]
  862. * ( -(2 ) , 2 - 1 ]
  863. * ( ]
  864. *
  865. * where
  866. * N Number of data word bits supported by the compiler
  867. * &/or target environment
  868. *
  869. * (a) Note that the most negative value, -2^(N-1), is NOT included in the supported
  870. * range since many compilers do NOT always correctly handle this value.
  871. *
  872. * (3) 'val' and 'val_min' are compared to 1 instead of 0 to avoid warning generated for
  873. * unsigned numbers.
  874. *********************************************************************************************************
  875. */
  876. #define DEF_CHK_VAL_MIN(val, val_min) (((!(((val) >= 1) && ((val_min) < 1))) && \
  877. ((((val_min) >= 1) && ((val) < 1)) || \
  878. ((val) < (val_min)))) ? DEF_FAIL : DEF_OK)
  879. /*
  880. *********************************************************************************************************
  881. * DEF_CHK_VAL_MAX()
  882. *
  883. * Description : Validate a value as less than or equal to a specified maximum value.
  884. *
  885. * Argument(s) : val Value to validate.
  886. *
  887. * val_max Maximum value to test.
  888. *
  889. * Return(s) : DEF_OK, Value is less than or equal to maximum value.
  890. *
  891. * DEF_FAIL, otherwise.
  892. *
  893. * Caller(s) : Application.
  894. *
  895. * Note(s) : (1) DEF_CHK_VAL_MAX() avoids directly comparing any two values if only one of the values
  896. * is negative since the negative value might be incorrectly promoted to an arbitrary
  897. * unsigned value if the other value to compare is unsigned.
  898. *
  899. * (2) Validation of values is limited to the range supported by the compiler &/or target
  900. * environment. All other values that underflow/overflow the supported range will
  901. * modulo/wrap into the supported range as arbitrary signed or unsigned values.
  902. *
  903. * Therefore, any values that underflow the most negative signed value or overflow
  904. * the most positive unsigned value supported by the compiler &/or target environment
  905. * cannot be validated :
  906. *
  907. * ( N-1 N ]
  908. * ( -(2 ) , 2 - 1 ]
  909. * ( ]
  910. *
  911. * where
  912. * N Number of data word bits supported by the compiler
  913. * &/or target environment
  914. *
  915. * (a) Note that the most negative value, -2^(N-1), is NOT included in the supported
  916. * range since many compilers do NOT always correctly handle this value.
  917. *
  918. * (3) 'val' and 'val_max' are compared to 1 instead of 0 to avoid warning generated for
  919. * unsigned numbers.
  920. *********************************************************************************************************
  921. */
  922. #define DEF_CHK_VAL_MAX(val, val_max) (((!(((val_max) >= 1) && ((val) < 1))) && \
  923. ((((val) >= 1) && ((val_max) < 1)) || \
  924. ((val) > (val_max)))) ? DEF_FAIL : DEF_OK)
  925. /*
  926. *********************************************************************************************************
  927. * DEF_CHK_VAL()
  928. *
  929. * Description : Validate a value as greater than or equal to a specified minimum value & less than or
  930. * equal to a specified maximum value.
  931. *
  932. * Argument(s) : val Value to validate.
  933. *
  934. * val_min Minimum value to test.
  935. *
  936. * val_max Maximum value to test.
  937. *
  938. * Return(s) : DEF_OK, Value is greater than or equal to minimum value AND
  939. * less than or equal to maximum value.
  940. *
  941. * DEF_FAIL, otherwise.
  942. *
  943. * Caller(s) : Application.
  944. *
  945. * Note(s) : (1) DEF_CHK_VAL() avoids directly comparing any two values if only one of the values
  946. * is negative since the negative value might be incorrectly promoted to an arbitrary
  947. * unsigned value if the other value to compare is unsigned.
  948. *
  949. * (2) Validation of values is limited to the range supported by the compiler &/or target
  950. * environment. All other values that underflow/overflow the supported range will
  951. * modulo/wrap into the supported range as arbitrary signed or unsigned values.
  952. *
  953. * Therefore, any values that underflow the most negative signed value or overflow
  954. * the most positive unsigned value supported by the compiler &/or target environment
  955. * cannot be validated :
  956. *
  957. * ( N-1 N ]
  958. * ( -(2 ) , 2 - 1 ]
  959. * ( ]
  960. *
  961. * where
  962. * N Number of data word bits supported by the compiler
  963. * &/or target environment
  964. *
  965. * (a) Note that the most negative value, -2^(N-1), is NOT included in the supported
  966. * range since many compilers do NOT always correctly handle this value.
  967. *
  968. * (3) DEF_CHK_VAL() does NOT validate that the maximum value ('val_max') is greater than
  969. * or equal to the minimum value ('val_min').
  970. *********************************************************************************************************
  971. */
  972. #define DEF_CHK_VAL(val, val_min, val_max) (((DEF_CHK_VAL_MIN((val), (val_min)) == DEF_FAIL) || \
  973. (DEF_CHK_VAL_MAX((val), (val_max)) == DEF_FAIL)) ? DEF_FAIL : DEF_OK)
  974. /*
  975. *********************************************************************************************************
  976. * DEF_GET_U_MAX_VAL()
  977. *
  978. * Description : Get the maximum unsigned value that can be represented in an unsigned integer variable
  979. * of the same data type size as an object.
  980. *
  981. * Argument(s) : obj Object or data type to return maximum unsigned value (see Note #1).
  982. *
  983. * Return(s) : Maximum unsigned integer value that can be represented by the object, if NO error(s).
  984. *
  985. * 0, otherwise.
  986. *
  987. * Caller(s) : Application.
  988. *
  989. * Note(s) : (1) 'obj' SHOULD be an integer object or data type but COULD also be a character or
  990. * pointer object or data type.
  991. *********************************************************************************************************
  992. */
  993. #if (CPU_CFG_DATA_SIZE_MAX == CPU_WORD_SIZE_08)
  994. #define DEF_GET_U_MAX_VAL(obj) ((sizeof(obj) == CPU_WORD_SIZE_08) ? DEF_INT_08U_MAX_VAL : 0)
  995. #elif (CPU_CFG_DATA_SIZE_MAX == CPU_WORD_SIZE_16)
  996. #define DEF_GET_U_MAX_VAL(obj) ((sizeof(obj) == CPU_WORD_SIZE_08) ? DEF_INT_08U_MAX_VAL : \
  997. ((sizeof(obj) == CPU_WORD_SIZE_16) ? DEF_INT_16U_MAX_VAL : 0))
  998. #elif (CPU_CFG_DATA_SIZE_MAX == CPU_WORD_SIZE_32)
  999. #define DEF_GET_U_MAX_VAL(obj) ((sizeof(obj) == CPU_WORD_SIZE_08) ? DEF_INT_08U_MAX_VAL : \
  1000. ((sizeof(obj) == CPU_WORD_SIZE_16) ? DEF_INT_16U_MAX_VAL : \
  1001. ((sizeof(obj) == CPU_WORD_SIZE_32) ? DEF_INT_32U_MAX_VAL : 0)))
  1002. #elif (CPU_CFG_DATA_SIZE_MAX == CPU_WORD_SIZE_64)
  1003. #define DEF_GET_U_MAX_VAL(obj) ((sizeof(obj) == CPU_WORD_SIZE_08) ? DEF_INT_08U_MAX_VAL : \
  1004. ((sizeof(obj) == CPU_WORD_SIZE_16) ? DEF_INT_16U_MAX_VAL : \
  1005. ((sizeof(obj) == CPU_WORD_SIZE_32) ? DEF_INT_32U_MAX_VAL : \
  1006. ((sizeof(obj) == CPU_WORD_SIZE_64) ? DEF_INT_64U_MAX_VAL : 0))))
  1007. #else
  1008. #error "CPU_CFG_DATA_SIZE_MAX illegally #defined in 'cpu.h' "
  1009. #error " [See 'cpu.h CONFIGURATION ERRORS']"
  1010. #endif
  1011. /*
  1012. *********************************************************************************************************
  1013. * MATH MACRO'S
  1014. *
  1015. * Note(s) : (1) Ideally, ALL mathematical macro's & functions SHOULD be defined in the custom mathematics
  1016. * library ('lib_math.*'). #### However, to maintain backwards compatibility with previously-
  1017. * released modules, mathematical macro & function definitions should only be moved to the
  1018. * custom mathematics library once all previously-released modules are updated to include the
  1019. * custom mathematics library.
  1020. *********************************************************************************************************
  1021. */
  1022. /*
  1023. *********************************************************************************************************
  1024. * DEF_MIN()
  1025. *
  1026. * Description : Determine the minimum of two values.
  1027. *
  1028. * Argument(s) : a First value.
  1029. *
  1030. * b Second value.
  1031. *
  1032. * Return(s) : Minimum of the two values.
  1033. *
  1034. * Caller(s) : Application.
  1035. *
  1036. * Note(s) : none.
  1037. *********************************************************************************************************
  1038. */
  1039. #define DEF_MIN(a, b) (((a) < (b)) ? (a) : (b))
  1040. /*
  1041. *********************************************************************************************************
  1042. * DEF_MAX()
  1043. *
  1044. * Description : Determine the maximum of two values.
  1045. *
  1046. * Argument(s) : a First value.
  1047. *
  1048. * b Second value.
  1049. *
  1050. * Return(s) : Maximum of the two values.
  1051. *
  1052. * Note(s) : none.
  1053. *********************************************************************************************************
  1054. */
  1055. #define DEF_MAX(a, b) (((a) > (b)) ? (a) : (b))
  1056. /*
  1057. *********************************************************************************************************
  1058. * DEF_ABS()
  1059. *
  1060. * Description : Determine the absolute value of a value.
  1061. *
  1062. * Argument(s) : a Value to calculate absolute value.
  1063. *
  1064. * Return(s) : Absolute value of the value.
  1065. *
  1066. * Caller(s) : Application.
  1067. *
  1068. * Note(s) : none.
  1069. *********************************************************************************************************
  1070. */
  1071. #define DEF_ABS(a) (((a) < 0) ? (-(a)) : (a))
  1072. /*
  1073. *********************************************************************************************************
  1074. * FUNCTION PROTOTYPES
  1075. *********************************************************************************************************
  1076. */
  1077. /*
  1078. *********************************************************************************************************
  1079. * CONFIGURATION ERRORS
  1080. *********************************************************************************************************
  1081. */
  1082. /*
  1083. *********************************************************************************************************
  1084. * LIBRARY CONFIGURATION ERRORS
  1085. *********************************************************************************************************
  1086. */
  1087. /* See 'lib_def.h Note #1a'. */
  1088. #if (CPU_CORE_VERSION < 12900u)
  1089. #error "CPU_CORE_VERSION [SHOULD be >= V1.29.00]"
  1090. #endif
  1091. /*
  1092. *********************************************************************************************************
  1093. * MODULE END
  1094. *
  1095. * Note(s) : (1) See 'lib_def.h MODULE'.
  1096. *********************************************************************************************************
  1097. */
  1098. #endif /* End of lib def module include. */