Implement a secure ICS protocol targeting LoRa Node151 microcontroller for controlling irrigation.
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

365 líneas
57 KiB

  1. /**
  2. * \file
  3. *
  4. * \brief Different macros.
  5. *
  6. * Copyright (C) 2014-2016 Atmel Corporation. All rights reserved.
  7. *
  8. * \asf_license_start
  9. *
  10. * \page License
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. *
  15. * 1. Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. *
  18. * 2. Redistributions in binary form must reproduce the above copyright notice,
  19. * this list of conditions and the following disclaimer in the documentation
  20. * and/or other materials provided with the distribution.
  21. *
  22. * 3. The name of Atmel may not be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * 4. This software may only be redistributed and used in connection with an
  26. * Atmel microcontroller product.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
  29. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  30. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  31. * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
  32. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  34. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  36. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  37. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  38. * POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. * \asf_license_stop
  41. *
  42. */
  43. #ifndef UTILS_H_INCLUDED
  44. #define UTILS_H_INCLUDED
  45. #ifdef __cplusplus
  46. extern "C" {
  47. #endif
  48. /**
  49. * \addtogroup doc_driver_hal_utils_macro
  50. *
  51. * @{
  52. */
  53. /**
  54. * \brief Retrieve pointer to parent structure
  55. */
  56. #define CONTAINER_OF(ptr, type, field_name) ((type *)(((uint8_t *)ptr) - offsetof(type, field_name)))
  57. /**
  58. * \brief Retrieve array size
  59. */
  60. #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
  61. /**
  62. * \brief Emit the compiler pragma \a arg.
  63. *
  64. * \param[in] arg The pragma directive as it would appear after \e \#pragma
  65. * (i.e. not stringified).
  66. */
  67. #define COMPILER_PRAGMA(arg) _Pragma(#arg)
  68. /**
  69. * \def COMPILER_PACK_SET(alignment)
  70. * \brief Set maximum alignment for subsequent struct and union definitions to \a alignment.
  71. */
  72. #define COMPILER_PACK_SET(alignment) COMPILER_PRAGMA(pack(alignment))
  73. /**
  74. * \def COMPILER_PACK_RESET()
  75. * \brief Set default alignment for subsequent struct and union definitions.
  76. */
  77. #define COMPILER_PACK_RESET() COMPILER_PRAGMA(pack())
  78. /**
  79. * \brief Set aligned boundary.
  80. */
  81. #if defined __GNUC__
  82. #define COMPILER_ALIGNED(a) __attribute__((__aligned__(a)))
  83. #elif defined __ICCARM__
  84. #define COMPILER_ALIGNED(a) COMPILER_PRAGMA(data_alignment = a)
  85. #elif defined __CC_ARM
  86. #define COMPILER_ALIGNED(a) __attribute__((__aligned__(a)))
  87. #endif
  88. /**
  89. * \brief Flash located data macros
  90. */
  91. #if defined __GNUC__
  92. #define PROGMEM_DECLARE(type, name) const type name
  93. #define PROGMEM_T const
  94. #define PROGMEM_READ_BYTE(x) *((uint8_t *)(x))
  95. #define PROGMEM_PTR_T const *
  96. #define PROGMEM_STRING_T const uint8_t *
  97. #elif defined __ICCARM__
  98. #define PROGMEM_DECLARE(type, name) const type name
  99. #define PROGMEM_T const
  100. #define PROGMEM_READ_BYTE(x) *((uint8_t *)(x))
  101. #define PROGMEM_PTR_T const *
  102. #define PROGMEM_STRING_T const uint8_t *
  103. #elif defined __CC_ARM
  104. #define PROGMEM_DECLARE(type, name) const type name
  105. #define PROGMEM_T const
  106. #define PROGMEM_READ_BYTE(x) *((uint8_t *)(x))
  107. #define PROGMEM_PTR_T const *
  108. #define PROGMEM_STRING_T const uint8_t *
  109. #endif
  110. /**
  111. * \brief Optimization
  112. */
  113. #if defined __GNUC__
  114. #define OPTIMIZE_HIGH __attribute__((optimize(s)))
  115. #elif defined __CC_ARM
  116. #define OPTIMIZE_HIGH _Pragma("O3")
  117. #elif defined __ICCARM__
  118. #define OPTIMIZE_HIGH _Pragma("optimize=high")
  119. #endif
  120. /**
  121. * \brief RAM located function attribute
  122. */
  123. #if defined(__CC_ARM) /* Keil ?Vision 4 */
  124. #define RAMFUNC __attribute__((section(".ramfunc")))
  125. #elif defined(__ICCARM__) /* IAR Ewarm 5.41+ */
  126. #define RAMFUNC __ramfunc
  127. #elif defined(__GNUC__) /* GCC CS3 2009q3-68 */
  128. #define RAMFUNC __attribute__((section(".ramfunc")))
  129. #endif
  130. /**
  131. * \brief No-init section.
  132. * Place a data object or a function in a no-init section.
  133. */
  134. #if defined(__CC_ARM)
  135. #define NO_INIT(a) __attribute__((zero_init))
  136. #elif defined(__ICCARM__)
  137. #define NO_INIT(a) __no_init
  138. #elif defined(__GNUC__)
  139. #define NO_INIT(a) __attribute__((section(".no_init")))
  140. #endif
  141. /**
  142. * \brief Set user-defined section.
  143. * Place a data object or a function in a user-defined section.
  144. */
  145. #if defined(__CC_ARM)
  146. #define COMPILER_SECTION(a) __attribute__((__section__(a)))
  147. #elif defined(__ICCARM__)
  148. #define COMPILER_SECTION(a) COMPILER_PRAGMA(location = a)
  149. #elif defined(__GNUC__)
  150. #define COMPILER_SECTION(a) __attribute__((__section__(a)))
  151. #endif
  152. /**
  153. * \brief Define WEAK attribute.
  154. */
  155. #if defined(__CC_ARM) /* Keil ?Vision 4 */
  156. #define WEAK __attribute__((weak))
  157. #elif defined(__ICCARM__) /* IAR Ewarm 5.41+ */
  158. #define WEAK __weak
  159. #elif defined(__GNUC__) /* GCC CS3 2009q3-68 */
  160. #define WEAK __attribute__((weak))
  161. #endif
  162. /**
  163. * \brief Pointer to function
  164. */
  165. typedef void (*FUNC_PTR)(void);
  166. #define LE_BYTE0(a) ((uint8_t)(a))
  167. #define LE_BYTE1(a) ((uint8_t)((a) >> 8))
  168. #define LE_BYTE2(a) ((uint8_t)((a) >> 16))
  169. #define LE_BYTE3(a) ((uint8_t)((a) >> 24))
  170. #define LE_2_U16(p) ((p)[0] + ((p)[1] << 8))
  171. #define LE_2_U32(p) ((p)[0] + ((p)[1] << 8) + ((p)[2] << 16) + ((p)[3] << 24))
  172. /** \name Zero-Bit Counting
  173. *
  174. * Under GCC, __builtin_clz and __builtin_ctz behave like macros when
  175. * applied to constant expressions (values known at compile time), so they are
  176. * more optimized than the use of the corresponding assembly instructions and
  177. * they can be used as constant expressions e.g. to initialize objects having
  178. * static storage duration, and like the corresponding assembly instructions
  179. * when applied to non-constant expressions (values unknown at compile time), so
  180. * they are more optimized than an assembly periphrasis. Hence, clz and ctz
  181. * ensure a possible and optimized behavior for both constant and non-constant
  182. * expressions.
  183. *
  184. * @{ */
  185. /** \brief Counts the leading zero bits of the given value considered as a 32-bit integer.
  186. *
  187. * \param[in] u Value of which to count the leading zero bits.
  188. *
  189. * \return The count of leading zero bits in \a u.
  190. */
  191. #if (defined __GNUC__) || (defined __CC_ARM)
  192. #define clz(u) __builtin_clz(u)
  193. #else
  194. #define clz(u) \
  195. (((u) == 0) \
  196. ? 32 \
  197. : ((u) & (1ul << 31)) ? 0 : ((u) & (1ul << 30)) ? 1 : ((u) & (1ul << 29)) \
  198. ? 2 \
  199. : ((u) & (1ul << 28)) \
  200. ? 3 \
  201. : ((u) & (1ul << 27)) \
  202. ? 4 \
  203. : ((u) & (1ul << 26)) \
  204. ? 5 \
  205. : ((u) & (1ul << 25)) ? 6 \
  206. : ((u) & (1ul << 24)) ? 7 : ((u) & (1ul << 23)) ? 8 : ((u) & (1ul << 22)) ? 9 : ((u) & (1ul << 21)) ? 10 : ((u) & (1ul << 20)) ? 11 : (( \
  207. u) \
  208. & (1ul \
  209. << 19)) \
  210. ? 12 \
  211. : ((u) \
  212. & (1ul \
  213. << 18)) \
  214. ? 13 \
  215. : ( \
  216. (u) \
  217. & (1ul \
  218. << 17)) \
  219. ? 14 \
  220. : ((u) & (1ul << 16)) ? 15 \
  221. : ((u) \
  222. & (1ul \
  223. << 15)) \
  224. ? 16 \
  225. : ((u) & (1ul << 14)) ? 17 \
  226. : ((u) & (1ul << 13)) ? 18 : ((u) & (1ul << 12)) ? 19 : ((u) & (1ul << 11)) ? 20 \
  227. : ((u) \
  228. & (1ul \
  229. << 10)) \
  230. ? 21 \
  231. : ((u) & (1ul << 9)) ? 22 \
  232. : ((u) & (1ul << 8)) ? 23 : ((u) \
  233. & (1ul \
  234. << 7)) \
  235. ? 24 \
  236. : ((u) & (1ul << 6)) ? 25 : ((u) & (1ul << 5)) ? 26 : ((u) & (1ul << 4)) ? 27 : ((u) & (1ul << 3)) ? 28 : ((u) \
  237. & (1ul \
  238. << 2)) \
  239. ? 29 \
  240. : ((u) & (1ul << 1)) ? 30 \
  241. : 31)
  242. #endif
  243. /** \brief Counts the trailing zero bits of the given value considered as a 32-bit integer.
  244. *
  245. * \param[in] u Value of which to count the trailing zero bits.
  246. *
  247. * \return The count of trailing zero bits in \a u.
  248. */
  249. #if (defined __GNUC__) || (defined __CC_ARM)
  250. #define ctz(u) __builtin_ctz(u)
  251. #else
  252. #define ctz(u) \
  253. ( \
  254. (u) & (1ul << 0) \
  255. ? 0 \
  256. : (u) & (1ul << 1) \
  257. ? 1 \
  258. : (u) & (1ul << 2) \
  259. ? 2 \
  260. : (u) & (1ul << 3) \
  261. ? 3 \
  262. : (u) & (1ul << 4) \
  263. ? 4 \
  264. : (u) & (1ul << 5) \
  265. ? 5 \
  266. : (u) & (1ul << 6) \
  267. ? 6 \
  268. : (u) & (1ul << 7) \
  269. ? 7 \
  270. : (u) & (1ul << 8) \
  271. ? 8 \
  272. : (u) & (1ul << 9) ? 9 \
  273. : (u) & (1ul << 10) \
  274. ? 10 \
  275. : (u) & (1ul << 11) \
  276. ? 11 \
  277. : (u) & (1ul << 12) \
  278. ? 12 \
  279. : (u) & (1ul << 13) \
  280. ? 13 \
  281. : (u) & (1ul \
  282. << 14) \
  283. ? 14 \
  284. : (u) & (1ul \
  285. << 15) \
  286. ? 15 \
  287. : (u) & (1ul << 16) ? 16 : (u) & (1ul << 17) ? 17 : (u) & (1ul << 18) ? 18 : (u) & (1ul << 19) ? 19 : (u) & (1ul << 20) ? 20 : (u) \
  288. & (1ul \
  289. << 21) \
  290. ? 21 \
  291. : (u) & (1ul << 22) ? 22 : (u) \
  292. & (1ul << 23) \
  293. ? 23 \
  294. : (u) & (1ul << 24) ? 24 \
  295. : (u) & (1ul << 25) ? 25 \
  296. : (u) & (1ul << 26) ? 26 : (u) & (1ul \
  297. << 27) \
  298. ? 27 \
  299. : (u) & (1ul \
  300. << 28) \
  301. ? 28 \
  302. : (u) & (1ul << 29) ? 29 \
  303. : (u) & (1ul << 30) ? 30 : ( \
  304. u) & (1ul << 31) \
  305. ? 31 \
  306. : 32)
  307. #endif
  308. /** @} */
  309. /**
  310. * \brief Counts the number of bits in a mask (no more than 32 bits)
  311. * \param[in] mask Mask of which to count the bits.
  312. */
  313. #define size_of_mask(mask) (32 - clz(mask) - ctz(mask))
  314. /**
  315. * \brief Retrieve the start position of bits mask (no more than 32 bits)
  316. * \param[in] mask Mask of which to retrieve the start position.
  317. */
  318. #define pos_of_mask(mask) ctz(mask)
  319. /**
  320. * \brief Return division result of a/b and round up the result to the closest
  321. * number divisible by "b"
  322. */
  323. #define round_up(a, b) (((a)-1) / (b) + 1)
  324. /**
  325. * \brief Get the minimum of x and y
  326. */
  327. #define min(x, y) ((x) > (y) ? (y) : (x))
  328. /**
  329. * \brief Get the maximum of x and y
  330. */
  331. #define max(x, y) ((x) > (y) ? (x) : (y))
  332. /**@}*/
  333. #ifdef __cplusplus
  334. }
  335. #endif
  336. #endif /* UTILS_H_INCLUDED */