compiler.h 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174
  1. /**
  2. * \file
  3. *
  4. * \brief Commonly used includes, types and macros.
  5. *
  6. * Copyright (C) 2012-2016 Atmel Corporation. All rights reserved.
  7. *
  8. * \asf_license_start
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright notice,
  17. * this list of conditions and the following disclaimer in the documentation
  18. * and/or other materials provided with the distribution.
  19. *
  20. * 3. The name of Atmel may not be used to endorse or promote products derived
  21. * from this software without specific prior written permission.
  22. *
  23. * 4. This software may only be redistributed and used in connection with an
  24. * Atmel microcontroller product.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
  27. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  28. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  29. * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
  30. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  31. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  32. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  33. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  34. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  35. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. * POSSIBILITY OF SUCH DAMAGE.
  37. *
  38. * \asf_license_stop
  39. *
  40. */
  41. /*
  42. * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
  43. */
  44. #ifndef UTILS_COMPILER_H_INCLUDED
  45. #define UTILS_COMPILER_H_INCLUDED
  46. /**
  47. * \defgroup group_sam0_utils Compiler abstraction layer and code utilities
  48. *
  49. * Compiler abstraction layer and code utilities for Cortex-M0+ based Atmel SAM devices.
  50. * This module provides various abstraction layers and utilities to make code compatible between different compilers.
  51. *
  52. * @{
  53. */
  54. #if (defined __ICCARM__)
  55. # include <intrinsics.h>
  56. #endif
  57. #include <stddef.h>
  58. #include <parts.h>
  59. #include <status_codes.h>
  60. #include <preprocessor.h>
  61. #include <io.h>
  62. #ifndef __ASSEMBLY__
  63. #include <stdio.h>
  64. #include <stdbool.h>
  65. #include <stdint.h>
  66. #include <stdlib.h>
  67. /**
  68. * \def UNUSED
  69. * \brief Marking \a v as a unused parameter or value.
  70. */
  71. #define UNUSED(v) (void)(v)
  72. /**
  73. * \def barrier
  74. * \brief Memory barrier
  75. */
  76. #ifdef __GNUC__
  77. # define barrier() asm volatile("" ::: "memory")
  78. #else
  79. # define barrier() asm ("")
  80. #endif
  81. /**
  82. * \brief Emit the compiler pragma \a arg.
  83. *
  84. * \param[in] arg The pragma directive as it would appear after \e \#pragma
  85. * (i.e. not stringified).
  86. */
  87. #define COMPILER_PRAGMA(arg) _Pragma(#arg)
  88. /**
  89. * \def COMPILER_PACK_SET(alignment)
  90. * \brief Set maximum alignment for subsequent struct and union definitions to \a alignment.
  91. */
  92. #define COMPILER_PACK_SET(alignment) COMPILER_PRAGMA(pack(alignment))
  93. /**
  94. * \def COMPILER_PACK_RESET()
  95. * \brief Set default alignment for subsequent struct and union definitions.
  96. */
  97. #define COMPILER_PACK_RESET() COMPILER_PRAGMA(pack())
  98. /**
  99. * \brief Set aligned boundary.
  100. */
  101. #if (defined __GNUC__) || (defined __CC_ARM)
  102. # define COMPILER_ALIGNED(a) __attribute__((__aligned__(a)))
  103. #elif (defined __ICCARM__)
  104. # define COMPILER_ALIGNED(a) COMPILER_PRAGMA(data_alignment = a)
  105. #endif
  106. /**
  107. * \brief Set word-aligned boundary.
  108. */
  109. #if (defined __GNUC__) || defined(__CC_ARM)
  110. #define COMPILER_WORD_ALIGNED __attribute__((__aligned__(4)))
  111. #elif (defined __ICCARM__)
  112. #define COMPILER_WORD_ALIGNED COMPILER_PRAGMA(data_alignment = 4)
  113. #endif
  114. /**
  115. * \def __always_inline
  116. * \brief The function should always be inlined.
  117. *
  118. * This annotation instructs the compiler to ignore its inlining
  119. * heuristics and inline the function no matter how big it thinks it
  120. * becomes.
  121. */
  122. #if defined(__CC_ARM)
  123. # define __always_inline __forceinline
  124. #elif (defined __GNUC__)
  125. # define __always_inline __attribute__((__always_inline__))
  126. #elif (defined __ICCARM__)
  127. # define __always_inline _Pragma("inline=forced")
  128. #endif
  129. /**
  130. * \def __no_inline
  131. * \brief The function should never be inlined
  132. *
  133. * This annotation instructs the compiler to ignore its inlining
  134. * heuristics and not inline the function no matter how small it thinks it
  135. * becomes.
  136. */
  137. #if defined(__CC_ARM)
  138. # define __no_inline __attribute__((noinline))
  139. #elif (defined __GNUC__)
  140. # define __no_inline __attribute__((noinline))
  141. #elif (defined __ICCARM__)
  142. # define __no_inline _Pragma("inline=never")
  143. #endif
  144. /** \brief This macro is used to test fatal errors.
  145. *
  146. * The macro tests if the expression is false. If it is, a fatal error is
  147. * detected and the application hangs up. If \c TEST_SUITE_DEFINE_ASSERT_MACRO
  148. * is defined, a unit test version of the macro is used, to allow execution
  149. * of further tests after a false expression.
  150. *
  151. * \param[in] expr Expression to evaluate and supposed to be nonzero.
  152. */
  153. #if defined(_ASSERT_ENABLE_)
  154. # if defined(TEST_SUITE_DEFINE_ASSERT_MACRO)
  155. # include "unit_test/suite.h"
  156. # else
  157. # undef TEST_SUITE_DEFINE_ASSERT_MACRO
  158. # define Assert(expr) \
  159. {\
  160. if (!(expr)) asm("BKPT #0");\
  161. }
  162. # endif
  163. #else
  164. # define Assert(expr) ((void) 0)
  165. #endif
  166. /* Define WEAK attribute */
  167. #if defined ( __CC_ARM )
  168. # define WEAK __attribute__ ((weak))
  169. #elif defined ( __ICCARM__ )
  170. # define WEAK __weak
  171. #elif defined ( __GNUC__ )
  172. # define WEAK __attribute__ ((weak))
  173. #endif
  174. /* Define NO_INIT attribute */
  175. #if defined ( __CC_ARM )
  176. # define NO_INIT __attribute__((zero_init))
  177. #elif defined ( __ICCARM__ )
  178. # define NO_INIT __no_init
  179. #elif defined ( __GNUC__ )
  180. # define NO_INIT __attribute__((section(".no_init")))
  181. #endif
  182. #include "interrupt.h"
  183. /** \name Usual Types
  184. * @{ */
  185. #ifndef __cplusplus
  186. # if !defined(__bool_true_false_are_defined)
  187. typedef unsigned char bool;
  188. # endif
  189. #endif
  190. typedef uint16_t le16_t;
  191. typedef uint16_t be16_t;
  192. typedef uint32_t le32_t;
  193. typedef uint32_t be32_t;
  194. typedef uint32_t iram_size_t;
  195. /** @} */
  196. /** \name Aliasing Aggregate Types
  197. * @{ */
  198. /** 16-bit union. */
  199. typedef union
  200. {
  201. int16_t s16;
  202. uint16_t u16;
  203. int8_t s8[2];
  204. uint8_t u8[2];
  205. } Union16;
  206. /** 32-bit union. */
  207. typedef union
  208. {
  209. int32_t s32;
  210. uint32_t u32;
  211. int16_t s16[2];
  212. uint16_t u16[2];
  213. int8_t s8[4];
  214. uint8_t u8[4];
  215. } Union32;
  216. /** 64-bit union. */
  217. typedef union
  218. {
  219. int64_t s64;
  220. uint64_t u64;
  221. int32_t s32[2];
  222. uint32_t u32[2];
  223. int16_t s16[4];
  224. uint16_t u16[4];
  225. int8_t s8[8];
  226. uint8_t u8[8];
  227. } Union64;
  228. /** Union of pointers to 64-, 32-, 16- and 8-bit unsigned integers. */
  229. typedef union
  230. {
  231. int64_t *s64ptr;
  232. uint64_t *u64ptr;
  233. int32_t *s32ptr;
  234. uint32_t *u32ptr;
  235. int16_t *s16ptr;
  236. uint16_t *u16ptr;
  237. int8_t *s8ptr;
  238. uint8_t *u8ptr;
  239. } UnionPtr;
  240. /** Union of pointers to volatile 64-, 32-, 16- and 8-bit unsigned integers. */
  241. typedef union
  242. {
  243. volatile int64_t *s64ptr;
  244. volatile uint64_t *u64ptr;
  245. volatile int32_t *s32ptr;
  246. volatile uint32_t *u32ptr;
  247. volatile int16_t *s16ptr;
  248. volatile uint16_t *u16ptr;
  249. volatile int8_t *s8ptr;
  250. volatile uint8_t *u8ptr;
  251. } UnionVPtr;
  252. /** Union of pointers to constant 64-, 32-, 16- and 8-bit unsigned integers. */
  253. typedef union
  254. {
  255. const int64_t *s64ptr;
  256. const uint64_t *u64ptr;
  257. const int32_t *s32ptr;
  258. const uint32_t *u32ptr;
  259. const int16_t *s16ptr;
  260. const uint16_t *u16ptr;
  261. const int8_t *s8ptr;
  262. const uint8_t *u8ptr;
  263. } UnionCPtr;
  264. /** Union of pointers to constant volatile 64-, 32-, 16- and 8-bit unsigned integers. */
  265. typedef union
  266. {
  267. const volatile int64_t *s64ptr;
  268. const volatile uint64_t *u64ptr;
  269. const volatile int32_t *s32ptr;
  270. const volatile uint32_t *u32ptr;
  271. const volatile int16_t *s16ptr;
  272. const volatile uint16_t *u16ptr;
  273. const volatile int8_t *s8ptr;
  274. const volatile uint8_t *u8ptr;
  275. } UnionCVPtr;
  276. /** Structure of pointers to 64-, 32-, 16- and 8-bit unsigned integers. */
  277. typedef struct
  278. {
  279. int64_t *s64ptr;
  280. uint64_t *u64ptr;
  281. int32_t *s32ptr;
  282. uint32_t *u32ptr;
  283. int16_t *s16ptr;
  284. uint16_t *u16ptr;
  285. int8_t *s8ptr;
  286. uint8_t *u8ptr;
  287. } StructPtr;
  288. /** Structure of pointers to volatile 64-, 32-, 16- and 8-bit unsigned integers. */
  289. typedef struct
  290. {
  291. volatile int64_t *s64ptr;
  292. volatile uint64_t *u64ptr;
  293. volatile int32_t *s32ptr;
  294. volatile uint32_t *u32ptr;
  295. volatile int16_t *s16ptr;
  296. volatile uint16_t *u16ptr;
  297. volatile int8_t *s8ptr;
  298. volatile uint8_t *u8ptr;
  299. } StructVPtr;
  300. /** Structure of pointers to constant 64-, 32-, 16- and 8-bit unsigned integers. */
  301. typedef struct
  302. {
  303. const int64_t *s64ptr;
  304. const uint64_t *u64ptr;
  305. const int32_t *s32ptr;
  306. const uint32_t *u32ptr;
  307. const int16_t *s16ptr;
  308. const uint16_t *u16ptr;
  309. const int8_t *s8ptr;
  310. const uint8_t *u8ptr;
  311. } StructCPtr;
  312. /** Structure of pointers to constant volatile 64-, 32-, 16- and 8-bit unsigned integers. */
  313. typedef struct
  314. {
  315. const volatile int64_t *s64ptr;
  316. const volatile uint64_t *u64ptr;
  317. const volatile int32_t *s32ptr;
  318. const volatile uint32_t *u32ptr;
  319. const volatile int16_t *s16ptr;
  320. const volatile uint16_t *u16ptr;
  321. const volatile int8_t *s8ptr;
  322. const volatile uint8_t *u8ptr;
  323. } StructCVPtr;
  324. /** @} */
  325. #endif /* #ifndef __ASSEMBLY__ */
  326. /** \name Usual Constants
  327. * @{ */
  328. #define DISABLE 0
  329. #define ENABLE 1
  330. #ifndef __cplusplus
  331. # if !defined(__bool_true_false_are_defined)
  332. # define false 0
  333. # define true 1
  334. # endif
  335. #endif
  336. /** @} */
  337. #ifndef __ASSEMBLY__
  338. /** \name Optimization Control
  339. * @{ */
  340. /**
  341. * \def likely(exp)
  342. * \brief The expression \a exp is likely to be true
  343. */
  344. #if !defined(likely) || defined(__DOXYGEN__)
  345. # define likely(exp) (exp)
  346. #endif
  347. /**
  348. * \def unlikely(exp)
  349. * \brief The expression \a exp is unlikely to be true
  350. */
  351. #if !defined(unlikely) || defined(__DOXYGEN__)
  352. # define unlikely(exp) (exp)
  353. #endif
  354. /**
  355. * \def is_constant(exp)
  356. * \brief Determine if an expression evaluates to a constant value.
  357. *
  358. * \param[in] exp Any expression
  359. *
  360. * \return true if \a exp is constant, false otherwise.
  361. */
  362. #if (defined __GNUC__) || (defined __CC_ARM)
  363. # define is_constant(exp) __builtin_constant_p(exp)
  364. #else
  365. # define is_constant(exp) (0)
  366. #endif
  367. /** @} */
  368. /** \name Bit-Field Handling
  369. * @{ */
  370. /** \brief Reads the bits of a value specified by a given bit-mask.
  371. *
  372. * \param[in] value Value to read bits from.
  373. * \param[in] mask Bit-mask indicating bits to read.
  374. *
  375. * \return Read bits.
  376. */
  377. #define Rd_bits( value, mask) ((value) & (mask))
  378. /** \brief Writes the bits of a C lvalue specified by a given bit-mask.
  379. *
  380. * \param[in] lvalue C lvalue to write bits to.
  381. * \param[in] mask Bit-mask indicating bits to write.
  382. * \param[in] bits Bits to write.
  383. *
  384. * \return Resulting value with written bits.
  385. */
  386. #define Wr_bits(lvalue, mask, bits) ((lvalue) = ((lvalue) & ~(mask)) |\
  387. ((bits ) & (mask)))
  388. /** \brief Tests the bits of a value specified by a given bit-mask.
  389. *
  390. * \param[in] value Value of which to test bits.
  391. * \param[in] mask Bit-mask indicating bits to test.
  392. *
  393. * \return \c 1 if at least one of the tested bits is set, else \c 0.
  394. */
  395. #define Tst_bits( value, mask) (Rd_bits(value, mask) != 0)
  396. /** \brief Clears the bits of a C lvalue specified by a given bit-mask.
  397. *
  398. * \param[in] lvalue C lvalue of which to clear bits.
  399. * \param[in] mask Bit-mask indicating bits to clear.
  400. *
  401. * \return Resulting value with cleared bits.
  402. */
  403. #define Clr_bits(lvalue, mask) ((lvalue) &= ~(mask))
  404. /** \brief Sets the bits of a C lvalue specified by a given bit-mask.
  405. *
  406. * \param[in] lvalue C lvalue of which to set bits.
  407. * \param[in] mask Bit-mask indicating bits to set.
  408. *
  409. * \return Resulting value with set bits.
  410. */
  411. #define Set_bits(lvalue, mask) ((lvalue) |= (mask))
  412. /** \brief Toggles the bits of a C lvalue specified by a given bit-mask.
  413. *
  414. * \param[in] lvalue C lvalue of which to toggle bits.
  415. * \param[in] mask Bit-mask indicating bits to toggle.
  416. *
  417. * \return Resulting value with toggled bits.
  418. */
  419. #define Tgl_bits(lvalue, mask) ((lvalue) ^= (mask))
  420. /** \brief Reads the bit-field of a value specified by a given bit-mask.
  421. *
  422. * \param[in] value Value to read a bit-field from.
  423. * \param[in] mask Bit-mask indicating the bit-field to read.
  424. *
  425. * \return Read bit-field.
  426. */
  427. #define Rd_bitfield( value, mask) (Rd_bits( value, mask) >> ctz(mask))
  428. /** \brief Writes the bit-field of a C lvalue specified by a given bit-mask.
  429. *
  430. * \param[in] lvalue C lvalue to write a bit-field to.
  431. * \param[in] mask Bit-mask indicating the bit-field to write.
  432. * \param[in] bitfield Bit-field to write.
  433. *
  434. * \return Resulting value with written bit-field.
  435. */
  436. #define Wr_bitfield(lvalue, mask, bitfield) (Wr_bits(lvalue, mask, (uint32_t)(bitfield) << ctz(mask)))
  437. /** @} */
  438. /** \name Zero-Bit Counting
  439. *
  440. * Under GCC, __builtin_clz and __builtin_ctz behave like macros when
  441. * applied to constant expressions (values known at compile time), so they are
  442. * more optimized than the use of the corresponding assembly instructions and
  443. * they can be used as constant expressions e.g. to initialize objects having
  444. * static storage duration, and like the corresponding assembly instructions
  445. * when applied to non-constant expressions (values unknown at compile time), so
  446. * they are more optimized than an assembly periphrasis. Hence, clz and ctz
  447. * ensure a possible and optimized behavior for both constant and non-constant
  448. * expressions.
  449. *
  450. * @{ */
  451. /** \brief Counts the leading zero bits of the given value considered as a 32-bit integer.
  452. *
  453. * \param[in] u Value of which to count the leading zero bits.
  454. *
  455. * \return The count of leading zero bits in \a u.
  456. */
  457. #if (defined __GNUC__) || (defined __CC_ARM)
  458. # define clz(u) ((u) ? __builtin_clz(u) : 32)
  459. #else
  460. # define clz(u) (((u) == 0) ? 32 : \
  461. ((u) & (1ul << 31)) ? 0 : \
  462. ((u) & (1ul << 30)) ? 1 : \
  463. ((u) & (1ul << 29)) ? 2 : \
  464. ((u) & (1ul << 28)) ? 3 : \
  465. ((u) & (1ul << 27)) ? 4 : \
  466. ((u) & (1ul << 26)) ? 5 : \
  467. ((u) & (1ul << 25)) ? 6 : \
  468. ((u) & (1ul << 24)) ? 7 : \
  469. ((u) & (1ul << 23)) ? 8 : \
  470. ((u) & (1ul << 22)) ? 9 : \
  471. ((u) & (1ul << 21)) ? 10 : \
  472. ((u) & (1ul << 20)) ? 11 : \
  473. ((u) & (1ul << 19)) ? 12 : \
  474. ((u) & (1ul << 18)) ? 13 : \
  475. ((u) & (1ul << 17)) ? 14 : \
  476. ((u) & (1ul << 16)) ? 15 : \
  477. ((u) & (1ul << 15)) ? 16 : \
  478. ((u) & (1ul << 14)) ? 17 : \
  479. ((u) & (1ul << 13)) ? 18 : \
  480. ((u) & (1ul << 12)) ? 19 : \
  481. ((u) & (1ul << 11)) ? 20 : \
  482. ((u) & (1ul << 10)) ? 21 : \
  483. ((u) & (1ul << 9)) ? 22 : \
  484. ((u) & (1ul << 8)) ? 23 : \
  485. ((u) & (1ul << 7)) ? 24 : \
  486. ((u) & (1ul << 6)) ? 25 : \
  487. ((u) & (1ul << 5)) ? 26 : \
  488. ((u) & (1ul << 4)) ? 27 : \
  489. ((u) & (1ul << 3)) ? 28 : \
  490. ((u) & (1ul << 2)) ? 29 : \
  491. ((u) & (1ul << 1)) ? 30 : \
  492. 31)
  493. #endif
  494. /** \brief Counts the trailing zero bits of the given value considered as a 32-bit integer.
  495. *
  496. * \param[in] u Value of which to count the trailing zero bits.
  497. *
  498. * \return The count of trailing zero bits in \a u.
  499. */
  500. #if (defined __GNUC__) || (defined __CC_ARM)
  501. # define ctz(u) ((u) ? __builtin_ctz(u) : 32)
  502. #else
  503. # define ctz(u) ((u) & (1ul << 0) ? 0 : \
  504. (u) & (1ul << 1) ? 1 : \
  505. (u) & (1ul << 2) ? 2 : \
  506. (u) & (1ul << 3) ? 3 : \
  507. (u) & (1ul << 4) ? 4 : \
  508. (u) & (1ul << 5) ? 5 : \
  509. (u) & (1ul << 6) ? 6 : \
  510. (u) & (1ul << 7) ? 7 : \
  511. (u) & (1ul << 8) ? 8 : \
  512. (u) & (1ul << 9) ? 9 : \
  513. (u) & (1ul << 10) ? 10 : \
  514. (u) & (1ul << 11) ? 11 : \
  515. (u) & (1ul << 12) ? 12 : \
  516. (u) & (1ul << 13) ? 13 : \
  517. (u) & (1ul << 14) ? 14 : \
  518. (u) & (1ul << 15) ? 15 : \
  519. (u) & (1ul << 16) ? 16 : \
  520. (u) & (1ul << 17) ? 17 : \
  521. (u) & (1ul << 18) ? 18 : \
  522. (u) & (1ul << 19) ? 19 : \
  523. (u) & (1ul << 20) ? 20 : \
  524. (u) & (1ul << 21) ? 21 : \
  525. (u) & (1ul << 22) ? 22 : \
  526. (u) & (1ul << 23) ? 23 : \
  527. (u) & (1ul << 24) ? 24 : \
  528. (u) & (1ul << 25) ? 25 : \
  529. (u) & (1ul << 26) ? 26 : \
  530. (u) & (1ul << 27) ? 27 : \
  531. (u) & (1ul << 28) ? 28 : \
  532. (u) & (1ul << 29) ? 29 : \
  533. (u) & (1ul << 30) ? 30 : \
  534. (u) & (1ul << 31) ? 31 : \
  535. 32)
  536. #endif
  537. /** @} */
  538. /** \name Bit Reversing
  539. * @{ */
  540. /** \brief Reverses the bits of \a u8.
  541. *
  542. * \param[in] u8 U8 of which to reverse the bits.
  543. *
  544. * \return Value resulting from \a u8 with reversed bits.
  545. */
  546. #define bit_reverse8(u8) ((U8)(bit_reverse32((U8)(u8)) >> 24))
  547. /** \brief Reverses the bits of \a u16.
  548. *
  549. * \param[in] u16 U16 of which to reverse the bits.
  550. *
  551. * \return Value resulting from \a u16 with reversed bits.
  552. */
  553. #define bit_reverse16(u16) ((uint16_t)(bit_reverse32((uint16_t)(u16)) >> 16))
  554. /** \brief Reverses the bits of \a u32.
  555. *
  556. * \param[in] u32 U32 of which to reverse the bits.
  557. *
  558. * \return Value resulting from \a u32 with reversed bits.
  559. */
  560. #define bit_reverse32(u32) __RBIT(u32)
  561. /** \brief Reverses the bits of \a u64.
  562. *
  563. * \param[in] u64 U64 of which to reverse the bits.
  564. *
  565. * \return Value resulting from \a u64 with reversed bits.
  566. */
  567. #define bit_reverse64(u64) ((uint64_t)(((uint64_t)bit_reverse32((uint64_t)(u64) >> 32)) |\
  568. ((uint64_t)bit_reverse32((uint64_t)(u64)) << 32)))
  569. /** @} */
  570. /** \name Alignment
  571. * @{ */
  572. /** \brief Tests alignment of the number \a val with the \a n boundary.
  573. *
  574. * \param[in] val Input value.
  575. * \param[in] n Boundary.
  576. *
  577. * \return \c 1 if the number \a val is aligned with the \a n boundary, else \c 0.
  578. */
  579. #define Test_align(val, n) (!Tst_bits( val, (n) - 1 ) )
  580. /** \brief Gets alignment of the number \a val with respect to the \a n boundary.
  581. *
  582. * \param[in] val Input value.
  583. * \param[in] n Boundary.
  584. *
  585. * \return Alignment of the number \a val with respect to the \a n boundary.
  586. */
  587. #define Get_align(val, n) ( Rd_bits( val, (n) - 1 ) )
  588. /** \brief Sets alignment of the lvalue number \a lval to \a alg with respect to the \a n boundary.
  589. *
  590. * \param[in] lval Input/output lvalue.
  591. * \param[in] n Boundary.
  592. * \param[in] alg Alignment.
  593. *
  594. * \return New value of \a lval resulting from its alignment set to \a alg with respect to the \a n boundary.
  595. */
  596. #define Set_align(lval, n, alg) ( Wr_bits(lval, (n) - 1, alg) )
  597. /** \brief Aligns the number \a val with the upper \a n boundary.
  598. *
  599. * \param[in] val Input value.
  600. * \param[in] n Boundary.
  601. *
  602. * \return Value resulting from the number \a val aligned with the upper \a n boundary.
  603. */
  604. #define Align_up( val, n) (((val) + ((n) - 1)) & ~((n) - 1))
  605. /** \brief Aligns the number \a val with the lower \a n boundary.
  606. *
  607. * \param[in] val Input value.
  608. * \param[in] n Boundary.
  609. *
  610. * \return Value resulting from the number \a val aligned with the lower \a n boundary.
  611. */
  612. #define Align_down(val, n) ( (val) & ~((n) - 1))
  613. /** @} */
  614. /** \name Mathematics
  615. *
  616. * The same considerations as for clz and ctz apply here but GCC does not
  617. * provide built-in functions to access the assembly instructions abs, min and
  618. * max and it does not produce them by itself in most cases, so two sets of
  619. * macros are defined here:
  620. * - Abs, Min and Max to apply to constant expressions (values known at
  621. * compile time);
  622. * - abs, min and max to apply to non-constant expressions (values unknown at
  623. * compile time), abs is found in stdlib.h.
  624. *
  625. * @{ */
  626. /** \brief Takes the absolute value of \a a.
  627. *
  628. * \param[in] a Input value.
  629. *
  630. * \return Absolute value of \a a.
  631. *
  632. * \note More optimized if only used with values known at compile time.
  633. */
  634. #define Abs(a) (((a) < 0 ) ? -(a) : (a))
  635. #ifndef __cplusplus
  636. /** \brief Takes the minimal value of \a a and \a b.
  637. *
  638. * \param[in] a Input value.
  639. * \param[in] b Input value.
  640. *
  641. * \return Minimal value of \a a and \a b.
  642. *
  643. * \note More optimized if only used with values known at compile time.
  644. */
  645. #define Min(a, b) (((a) < (b)) ? (a) : (b))
  646. /** \brief Takes the maximal value of \a a and \a b.
  647. *
  648. * \param[in] a Input value.
  649. * \param[in] b Input value.
  650. *
  651. * \return Maximal value of \a a and \a b.
  652. *
  653. * \note More optimized if only used with values known at compile time.
  654. */
  655. #define Max(a, b) (((a) > (b)) ? (a) : (b))
  656. /** \brief Takes the minimal value of \a a and \a b.
  657. *
  658. * \param[in] a Input value.
  659. * \param[in] b Input value.
  660. *
  661. * \return Minimal value of \a a and \a b.
  662. *
  663. * \note More optimized if only used with values unknown at compile time.
  664. */
  665. #define min(a, b) Min(a, b)
  666. /** \brief Takes the maximal value of \a a and \a b.
  667. *
  668. * \param[in] a Input value.
  669. * \param[in] b Input value.
  670. *
  671. * \return Maximal value of \a a and \a b.
  672. *
  673. * \note More optimized if only used with values unknown at compile time.
  674. */
  675. #define max(a, b) Max(a, b)
  676. #endif
  677. /** @} */
  678. /** \brief Calls the routine at address \a addr.
  679. *
  680. * It generates a long call opcode.
  681. *
  682. * For example, `Long_call(0x80000000)' generates a software reset on a UC3 if
  683. * it is invoked from the CPU supervisor mode.
  684. *
  685. * \param[in] addr Address of the routine to call.
  686. *
  687. * \note It may be used as a long jump opcode in some special cases.
  688. */
  689. #define Long_call(addr) ((*(void (*)(void))(addr))())
  690. /** \name MCU Endianism Handling
  691. * ARM is MCU little endian.
  692. *
  693. * @{ */
  694. #define BE16(x) swap16(x)
  695. #define LE16(x) (x)
  696. #define le16_to_cpu(x) (x)
  697. #define cpu_to_le16(x) (x)
  698. #define LE16_TO_CPU(x) (x)
  699. #define CPU_TO_LE16(x) (x)
  700. #define be16_to_cpu(x) swap16(x)
  701. #define cpu_to_be16(x) swap16(x)
  702. #define BE16_TO_CPU(x) swap16(x)
  703. #define CPU_TO_BE16(x) swap16(x)
  704. #define le32_to_cpu(x) (x)
  705. #define cpu_to_le32(x) (x)
  706. #define LE32_TO_CPU(x) (x)
  707. #define CPU_TO_LE32(x) (x)
  708. #define be32_to_cpu(x) swap32(x)
  709. #define cpu_to_be32(x) swap32(x)
  710. #define BE32_TO_CPU(x) swap32(x)
  711. #define CPU_TO_BE32(x) swap32(x)
  712. /** @} */
  713. /** \name Endianism Conversion
  714. *
  715. * The same considerations as for clz and ctz apply here but GCC's
  716. * __builtin_bswap_32 and __builtin_bswap_64 do not behave like macros when
  717. * applied to constant expressions, so two sets of macros are defined here:
  718. * - Swap16, Swap32 and Swap64 to apply to constant expressions (values known
  719. * at compile time);
  720. * - swap16, swap32 and swap64 to apply to non-constant expressions (values
  721. * unknown at compile time).
  722. *
  723. * @{ */
  724. /** \brief Toggles the endianism of \a u16 (by swapping its bytes).
  725. *
  726. * \param[in] u16 U16 of which to toggle the endianism.
  727. *
  728. * \return Value resulting from \a u16 with toggled endianism.
  729. *
  730. * \note More optimized if only used with values known at compile time.
  731. */
  732. #define Swap16(u16) ((uint16_t)(((uint16_t)(u16) >> 8) |\
  733. ((uint16_t)(u16) << 8)))
  734. /** \brief Toggles the endianism of \a u32 (by swapping its bytes).
  735. *
  736. * \param[in] u32 U32 of which to toggle the endianism.
  737. *
  738. * \return Value resulting from \a u32 with toggled endianism.
  739. *
  740. * \note More optimized if only used with values known at compile time.
  741. */
  742. #define Swap32(u32) ((uint32_t)(((uint32_t)Swap16((uint32_t)(u32) >> 16)) |\
  743. ((uint32_t)Swap16((uint32_t)(u32)) << 16)))
  744. /** \brief Toggles the endianism of \a u64 (by swapping its bytes).
  745. *
  746. * \param[in] u64 U64 of which to toggle the endianism.
  747. *
  748. * \return Value resulting from \a u64 with toggled endianism.
  749. *
  750. * \note More optimized if only used with values known at compile time.
  751. */
  752. #define Swap64(u64) ((uint64_t)(((uint64_t)Swap32((uint64_t)(u64) >> 32)) |\
  753. ((uint64_t)Swap32((uint64_t)(u64)) << 32)))
  754. /** \brief Toggles the endianism of \a u16 (by swapping its bytes).
  755. *
  756. * \param[in] u16 U16 of which to toggle the endianism.
  757. *
  758. * \return Value resulting from \a u16 with toggled endianism.
  759. *
  760. * \note More optimized if only used with values unknown at compile time.
  761. */
  762. #define swap16(u16) Swap16(u16)
  763. /** \brief Toggles the endianism of \a u32 (by swapping its bytes).
  764. *
  765. * \param[in] u32 U32 of which to toggle the endianism.
  766. *
  767. * \return Value resulting from \a u32 with toggled endianism.
  768. *
  769. * \note More optimized if only used with values unknown at compile time.
  770. */
  771. #if (defined __GNUC__)
  772. # define swap32(u32) ((uint32_t)__builtin_bswap32((uint32_t)(u32)))
  773. #else
  774. # define swap32(u32) Swap32(u32)
  775. #endif
  776. /** \brief Toggles the endianism of \a u64 (by swapping its bytes).
  777. *
  778. * \param[in] u64 U64 of which to toggle the endianism.
  779. *
  780. * \return Value resulting from \a u64 with toggled endianism.
  781. *
  782. * \note More optimized if only used with values unknown at compile time.
  783. */
  784. #if (defined __GNUC__)
  785. # define swap64(u64) ((uint64_t)__builtin_bswap64((uint64_t)(u64)))
  786. #else
  787. # define swap64(u64) ((uint64_t)(((uint64_t)swap32((uint64_t)(u64) >> 32)) |\
  788. ((uint64_t)swap32((uint64_t)(u64)) << 32)))
  789. #endif
  790. /** @} */
  791. /** \name Target Abstraction
  792. *
  793. * @{ */
  794. #define _GLOBEXT_ extern /**< extern storage-class specifier. */
  795. #define _CONST_TYPE_ const /**< const type qualifier. */
  796. #define _MEM_TYPE_SLOW_ /**< Slow memory type. */
  797. #define _MEM_TYPE_MEDFAST_ /**< Fairly fast memory type. */
  798. #define _MEM_TYPE_FAST_ /**< Fast memory type. */
  799. #define memcmp_ram2ram memcmp /**< Target-specific memcmp of RAM to RAM. */
  800. #define memcmp_code2ram memcmp /**< Target-specific memcmp of RAM to NVRAM. */
  801. #define memcpy_ram2ram memcpy /**< Target-specific memcpy from RAM to RAM. */
  802. #define memcpy_code2ram memcpy /**< Target-specific memcpy from NVRAM to RAM. */
  803. /** @} */
  804. /**
  805. * \brief Calculate \f$ \left\lceil \frac{a}{b} \right\rceil \f$ using
  806. * integer arithmetic.
  807. *
  808. * \param[in] a An integer
  809. * \param[in] b Another integer
  810. *
  811. * \return (\a a / \a b) rounded up to the nearest integer.
  812. */
  813. #define div_ceil(a, b) (((a) + (b) - 1) / (b))
  814. #endif /* #ifndef __ASSEMBLY__ */
  815. #ifdef __ICCARM__
  816. /** \name Compiler Keywords
  817. *
  818. * Port of some keywords from GCC to IAR Embedded Workbench.
  819. *
  820. * @{ */
  821. #define __asm__ asm
  822. #define __inline__ inline
  823. #define __volatile__
  824. /** @} */
  825. #endif
  826. #define FUNC_PTR void *
  827. /**
  828. * \def unused
  829. * \brief Marking \a v as a unused parameter or value.
  830. */
  831. #define unused(v) do { (void)(v); } while(0)
  832. /* Define RAMFUNC attribute */
  833. #if defined ( __CC_ARM ) /* Keil uVision 4 */
  834. # define RAMFUNC __attribute__ ((section(".ramfunc")))
  835. #elif defined ( __ICCARM__ ) /* IAR Ewarm 5.41+ */
  836. # define RAMFUNC __ramfunc
  837. #elif defined ( __GNUC__ ) /* GCC CS3 2009q3-68 */
  838. # define RAMFUNC __attribute__ ((section(".ramfunc")))
  839. #endif
  840. /* Define OPTIMIZE_HIGH attribute */
  841. #if defined ( __CC_ARM ) /* Keil uVision 4 */
  842. # define OPTIMIZE_HIGH _Pragma("O3")
  843. #elif defined ( __ICCARM__ ) /* IAR Ewarm 5.41+ */
  844. # define OPTIMIZE_HIGH _Pragma("optimize=high")
  845. #elif defined ( __GNUC__ ) /* GCC CS3 2009q3-68 */
  846. # define OPTIMIZE_HIGH __attribute__((optimize("s")))
  847. #endif
  848. #define PASS 0
  849. #define FAIL 1
  850. #define LOW 0
  851. #define HIGH 1
  852. typedef int8_t S8 ; //!< 8-bit signed integer.
  853. typedef uint8_t U8 ; //!< 8-bit unsigned integer.
  854. typedef int16_t S16; //!< 16-bit signed integer.
  855. typedef uint16_t U16; //!< 16-bit unsigned integer.
  856. typedef int32_t S32; //!< 32-bit signed integer.
  857. typedef uint32_t U32; //!< 32-bit unsigned integer.
  858. typedef int64_t S64; //!< 64-bit signed integer.
  859. typedef uint64_t U64; //!< 64-bit unsigned integer.
  860. typedef float F32; //!< 32-bit floating-point number.
  861. typedef double F64; //!< 64-bit floating-point number.
  862. #define MSB(u16) (((U8 *)&(u16))[1]) //!< Most significant byte of \a u16.
  863. #define LSB(u16) (((U8 *)&(u16))[0]) //!< Least significant byte of \a u16.
  864. #define MSH(u32) (((U16 *)&(u32))[1]) //!< Most significant half-word of \a u32.
  865. #define LSH(u32) (((U16 *)&(u32))[0]) //!< Least significant half-word of \a u32.
  866. #define MSB0W(u32) (((U8 *)&(u32))[3]) //!< Most significant byte of 1st rank of \a u32.
  867. #define MSB1W(u32) (((U8 *)&(u32))[2]) //!< Most significant byte of 2nd rank of \a u32.
  868. #define MSB2W(u32) (((U8 *)&(u32))[1]) //!< Most significant byte of 3rd rank of \a u32.
  869. #define MSB3W(u32) (((U8 *)&(u32))[0]) //!< Most significant byte of 4th rank of \a u32.
  870. #define LSB3W(u32) MSB0W(u32) //!< Least significant byte of 4th rank of \a u32.
  871. #define LSB2W(u32) MSB1W(u32) //!< Least significant byte of 3rd rank of \a u32.
  872. #define LSB1W(u32) MSB2W(u32) //!< Least significant byte of 2nd rank of \a u32.
  873. #define LSB0W(u32) MSB3W(u32) //!< Least significant byte of 1st rank of \a u32.
  874. #define MSW(u64) (((U32 *)&(u64))[1]) //!< Most significant word of \a u64.
  875. #define LSW(u64) (((U32 *)&(u64))[0]) //!< Least significant word of \a u64.
  876. #define MSH0(u64) (((U16 *)&(u64))[3]) //!< Most significant half-word of 1st rank of \a u64.
  877. #define MSH1(u64) (((U16 *)&(u64))[2]) //!< Most significant half-word of 2nd rank of \a u64.
  878. #define MSH2(u64) (((U16 *)&(u64))[1]) //!< Most significant half-word of 3rd rank of \a u64.
  879. #define MSH3(u64) (((U16 *)&(u64))[0]) //!< Most significant half-word of 4th rank of \a u64.
  880. #define LSH3(u64) MSH0(u64) //!< Least significant half-word of 4th rank of \a u64.
  881. #define LSH2(u64) MSH1(u64) //!< Least significant half-word of 3rd rank of \a u64.
  882. #define LSH1(u64) MSH2(u64) //!< Least significant half-word of 2nd rank of \a u64.
  883. #define LSH0(u64) MSH3(u64) //!< Least significant half-word of 1st rank of \a u64.
  884. #define MSB0D(u64) (((U8 *)&(u64))[7]) //!< Most significant byte of 1st rank of \a u64.
  885. #define MSB1D(u64) (((U8 *)&(u64))[6]) //!< Most significant byte of 2nd rank of \a u64.
  886. #define MSB2D(u64) (((U8 *)&(u64))[5]) //!< Most significant byte of 3rd rank of \a u64.
  887. #define MSB3D(u64) (((U8 *)&(u64))[4]) //!< Most significant byte of 4th rank of \a u64.
  888. #define MSB4D(u64) (((U8 *)&(u64))[3]) //!< Most significant byte of 5th rank of \a u64.
  889. #define MSB5D(u64) (((U8 *)&(u64))[2]) //!< Most significant byte of 6th rank of \a u64.
  890. #define MSB6D(u64) (((U8 *)&(u64))[1]) //!< Most significant byte of 7th rank of \a u64.
  891. #define MSB7D(u64) (((U8 *)&(u64))[0]) //!< Most significant byte of 8th rank of \a u64.
  892. #define LSB7D(u64) MSB0D(u64) //!< Least significant byte of 8th rank of \a u64.
  893. #define LSB6D(u64) MSB1D(u64) //!< Least significant byte of 7th rank of \a u64.
  894. #define LSB5D(u64) MSB2D(u64) //!< Least significant byte of 6th rank of \a u64.
  895. #define LSB4D(u64) MSB3D(u64) //!< Least significant byte of 5th rank of \a u64.
  896. #define LSB3D(u64) MSB4D(u64) //!< Least significant byte of 4th rank of \a u64.
  897. #define LSB2D(u64) MSB5D(u64) //!< Least significant byte of 3rd rank of \a u64.
  898. #define LSB1D(u64) MSB6D(u64) //!< Least significant byte of 2nd rank of \a u64.
  899. #define LSB0D(u64) MSB7D(u64) //!< Least significant byte of 1st rank of \a u64.
  900. #define LSB0(u32) LSB0W(u32) //!< Least significant byte of 1st rank of \a u32.
  901. #define LSB1(u32) LSB1W(u32) //!< Least significant byte of 2nd rank of \a u32.
  902. #define LSB2(u32) LSB2W(u32) //!< Least significant byte of 3rd rank of \a u32.
  903. #define LSB3(u32) LSB3W(u32) //!< Least significant byte of 4th rank of \a u32.
  904. #define MSB3(u32) MSB3W(u32) //!< Most significant byte of 4th rank of \a u32.
  905. #define MSB2(u32) MSB2W(u32) //!< Most significant byte of 3rd rank of \a u32.
  906. #define MSB1(u32) MSB1W(u32) //!< Most significant byte of 2nd rank of \a u32.
  907. #define MSB0(u32) MSB0W(u32) //!< Most significant byte of 1st rank of \a u32.
  908. #if defined(__ICCARM__)
  909. #define SHORTENUM __packed
  910. #elif defined(__GNUC__)
  911. #define SHORTENUM __attribute__((packed))
  912. #endif
  913. /* No operation */
  914. #if defined(__ICCARM__)
  915. #define nop() __no_operation()
  916. #elif defined(__GNUC__)
  917. #define nop() (__NOP())
  918. #endif
  919. #define FLASH_DECLARE(x) const x
  920. #define FLASH_EXTERN(x) extern const x
  921. #define PGM_READ_BYTE(x) *(x)
  922. #define PGM_READ_WORD(x) *(x)
  923. #define MEMCPY_ENDIAN memcpy
  924. #define PGM_READ_BLOCK(dst, src, len) memcpy((dst), (src), (len))
  925. /*Defines the Flash Storage for the request and response of MAC*/
  926. #define CMD_ID_OCTET (0)
  927. /* Converting of values from CPU endian to little endian. */
  928. #define CPU_ENDIAN_TO_LE16(x) (x)
  929. #define CPU_ENDIAN_TO_LE32(x) (x)
  930. #define CPU_ENDIAN_TO_LE64(x) (x)
  931. /* Converting of values from little endian to CPU endian. */
  932. #define LE16_TO_CPU_ENDIAN(x) (x)
  933. #define LE32_TO_CPU_ENDIAN(x) (x)
  934. #define LE64_TO_CPU_ENDIAN(x) (x)
  935. /* Converting of constants from little endian to CPU endian. */
  936. #define CLE16_TO_CPU_ENDIAN(x) (x)
  937. #define CLE32_TO_CPU_ENDIAN(x) (x)
  938. #define CLE64_TO_CPU_ENDIAN(x) (x)
  939. /* Converting of constants from CPU endian to little endian. */
  940. #define CCPU_ENDIAN_TO_LE16(x) (x)
  941. #define CCPU_ENDIAN_TO_LE32(x) (x)
  942. #define CCPU_ENDIAN_TO_LE64(x) (x)
  943. #define ADDR_COPY_DST_SRC_16(dst, src) ((dst) = (src))
  944. #define ADDR_COPY_DST_SRC_64(dst, src) ((dst) = (src))
  945. /**
  946. * @brief Converts a 64-Bit value into a 8 Byte array
  947. *
  948. * @param[in] value 64-Bit value
  949. * @param[out] data Pointer to the 8 Byte array to be updated with 64-Bit value
  950. * @ingroup apiPalApi
  951. */
  952. static inline void convert_64_bit_to_byte_array(uint64_t value, uint8_t *data)
  953. {
  954. uint8_t index = 0;
  955. while (index < 8)
  956. {
  957. data[index++] = value & 0xFF;
  958. value = value >> 8;
  959. }
  960. }
  961. /**
  962. * @brief Converts a 16-Bit value into a 2 Byte array
  963. *
  964. * @param[in] value 16-Bit value
  965. * @param[out] data Pointer to the 2 Byte array to be updated with 16-Bit value
  966. * @ingroup apiPalApi
  967. */
  968. static inline void convert_16_bit_to_byte_array(uint16_t value, uint8_t *data)
  969. {
  970. data[0] = value & 0xFF;
  971. data[1] = (value >> 8) & 0xFF;
  972. }
  973. /* Converts a 16-Bit value into a 2 Byte array */
  974. static inline void convert_spec_16_bit_to_byte_array(uint16_t value, uint8_t *data)
  975. {
  976. data[0] = value & 0xFF;
  977. data[1] = (value >> 8) & 0xFF;
  978. }
  979. /* Converts a 16-Bit value into a 2 Byte array */
  980. static inline void convert_16_bit_to_byte_address(uint16_t value, uint8_t *data)
  981. {
  982. data[0] = value & 0xFF;
  983. data[1] = (value >> 8) & 0xFF;
  984. }
  985. /*
  986. * @brief Converts a 2 Byte array into a 16-Bit value
  987. *
  988. * @param data Specifies the pointer to the 2 Byte array
  989. *
  990. * @return 16-Bit value
  991. * @ingroup apiPalApi
  992. */
  993. static inline uint16_t convert_byte_array_to_16_bit(uint8_t *data)
  994. {
  995. return (data[0] | ((uint16_t)data[1] << 8));
  996. }
  997. /* Converts a 4 Byte array into a 32-Bit value */
  998. static inline uint32_t convert_byte_array_to_32_bit(uint8_t *data)
  999. {
  1000. union
  1001. {
  1002. uint32_t u32;
  1003. uint8_t u8[4];
  1004. }long_addr;
  1005. uint8_t index;
  1006. for (index = 0; index < 4; index++)
  1007. {
  1008. long_addr.u8[index] = *data++;
  1009. }
  1010. return long_addr.u32;
  1011. }
  1012. /**
  1013. * @brief Converts a 8 Byte array into a 64-Bit value
  1014. *
  1015. * @param data Specifies the pointer to the 8 Byte array
  1016. *
  1017. * @return 64-Bit value
  1018. * @ingroup apiPalApi
  1019. */
  1020. static inline uint64_t convert_byte_array_to_64_bit(uint8_t *data)
  1021. {
  1022. union
  1023. {
  1024. uint64_t u64;
  1025. uint8_t u8[8];
  1026. } long_addr;
  1027. uint8_t index;
  1028. for (index = 0; index < 8; index++)
  1029. {
  1030. long_addr.u8[index] = *data++;
  1031. }
  1032. return long_addr.u64;
  1033. }
  1034. /** @} */
  1035. #endif /* UTILS_COMPILER_H_INCLUDED */