upload
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

Common.h 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. LUFA Library
  3. Copyright (C) Dean Camera, 2014.
  4. dean [at] fourwalledcubicle [dot] com
  5. www.lufa-lib.org
  6. */
  7. /*
  8. Copyright 2014 Dean Camera (dean [at] fourwalledcubicle [dot] com)
  9. Permission to use, copy, modify, distribute, and sell this
  10. software and its documentation for any purpose is hereby granted
  11. without fee, provided that the above copyright notice appear in
  12. all copies and that both that the copyright notice and this
  13. permission notice and warranty disclaimer appear in supporting
  14. documentation, and that the name of the author not be used in
  15. advertising or publicity pertaining to distribution of the
  16. software without specific, written prior permission.
  17. The author disclaims all warranties with regard to this
  18. software, including all implied warranties of merchantability
  19. and fitness. In no event shall the author be liable for any
  20. special, indirect or consequential damages or any damages
  21. whatsoever resulting from loss of use, data or profits, whether
  22. in an action of contract, negligence or other tortious action,
  23. arising out of or in connection with the use or performance of
  24. this software.
  25. */
  26. /** \dir
  27. * \brief Common library header files.
  28. *
  29. * This folder contains header files which are common to all parts of the LUFA library. They may be used freely in
  30. * user applications.
  31. */
  32. /** \file
  33. * \brief Common library convenience headers, macros and functions.
  34. *
  35. * \copydetails Group_Common
  36. */
  37. /** \defgroup Group_Common Common Utility Headers - LUFA/Drivers/Common/Common.h
  38. * \brief Common library convenience headers, macros and functions.
  39. *
  40. * Common utility headers containing macros, functions, enums and types which are common to all
  41. * aspects of the library.
  42. *
  43. * @{
  44. */
  45. /** \defgroup Group_GlobalInt Global Interrupt Macros
  46. * \brief Convenience macros for the management of interrupts globally within the device.
  47. *
  48. * Macros and functions to create and control global interrupts within the device.
  49. */
  50. #ifndef __LUFA_COMMON_H__
  51. #define __LUFA_COMMON_H__
  52. /* Macros: */
  53. #define __INCLUDE_FROM_COMMON_H
  54. /* Includes: */
  55. #include <stdint.h>
  56. #include <stdbool.h>
  57. #include <string.h>
  58. #include <stddef.h>
  59. #include "Architectures.h"
  60. #include "BoardTypes.h"
  61. #include "ArchitectureSpecific.h"
  62. #include "CompilerSpecific.h"
  63. #include "Attributes.h"
  64. #if defined(USE_LUFA_CONFIG_HEADER)
  65. #include "LUFAConfig.h"
  66. #endif
  67. /* Enable C linkage for C++ Compilers: */
  68. #if defined(__cplusplus)
  69. extern "C" {
  70. #endif
  71. /* Architecture specific utility includes: */
  72. #if defined(__DOXYGEN__)
  73. /** Type define for an unsigned integer the same width as the selected architecture's machine register.
  74. * This is distinct from the non-specific standard int data type, whose width is machine dependant but
  75. * which may not reflect the actual machine register width on some targets (e.g. AVR8).
  76. */
  77. typedef MACHINE_REG_t uint_reg_t;
  78. #elif (ARCH == ARCH_AVR8)
  79. #include <avr/io.h>
  80. #include <avr/interrupt.h>
  81. #include <avr/pgmspace.h>
  82. #include <avr/eeprom.h>
  83. #include <avr/boot.h>
  84. #include <math.h>
  85. #include <util/delay.h>
  86. typedef uint8_t uint_reg_t;
  87. #define ARCH_HAS_EEPROM_ADDRESS_SPACE
  88. #define ARCH_HAS_FLASH_ADDRESS_SPACE
  89. #define ARCH_HAS_MULTI_ADDRESS_SPACE
  90. #define ARCH_LITTLE_ENDIAN
  91. #include "Endianness.h"
  92. #elif (ARCH == ARCH_UC3)
  93. #include <avr32/io.h>
  94. #include <math.h>
  95. // === TODO: Find abstracted way to handle these ===
  96. #define PROGMEM
  97. #define pgm_read_byte(x) *x
  98. #define memcmp_P(...) memcmp(__VA_ARGS__)
  99. #define memcpy_P(...) memcpy(__VA_ARGS__)
  100. // =================================================
  101. typedef uint32_t uint_reg_t;
  102. #define ARCH_BIG_ENDIAN
  103. #include "Endianness.h"
  104. #elif (ARCH == ARCH_XMEGA)
  105. #include <avr/io.h>
  106. #include <avr/interrupt.h>
  107. #include <avr/pgmspace.h>
  108. #include <avr/eeprom.h>
  109. #include <math.h>
  110. #include <util/delay.h>
  111. typedef uint8_t uint_reg_t;
  112. #define ARCH_HAS_EEPROM_ADDRESS_SPACE
  113. #define ARCH_HAS_FLASH_ADDRESS_SPACE
  114. #define ARCH_HAS_MULTI_ADDRESS_SPACE
  115. #define ARCH_LITTLE_ENDIAN
  116. #include "Endianness.h"
  117. #else
  118. #error Unknown device architecture specified.
  119. #endif
  120. /* Public Interface - May be used in end-application: */
  121. /* Macros: */
  122. #if !defined(__DOXYGEN__)
  123. // Obsolete, retained for compatibility with user code
  124. #define MACROS do
  125. #define MACROE while (0)
  126. #endif
  127. /** Convenience macro to determine the larger of two values.
  128. *
  129. * \attention This macro should only be used with operands that do not have side effects from being evaluated
  130. * multiple times.
  131. *
  132. * \param[in] x First value to compare
  133. * \param[in] y First value to compare
  134. *
  135. * \return The larger of the two input parameters
  136. */
  137. #if !defined(MAX) || defined(__DOXYGEN__)
  138. #define MAX(x, y) (((x) > (y)) ? (x) : (y))
  139. #endif
  140. /** Convenience macro to determine the smaller of two values.
  141. *
  142. * \attention This macro should only be used with operands that do not have side effects from being evaluated
  143. * multiple times.
  144. *
  145. * \param[in] x First value to compare.
  146. * \param[in] y First value to compare.
  147. *
  148. * \return The smaller of the two input parameters
  149. */
  150. #if !defined(MIN) || defined(__DOXYGEN__)
  151. #define MIN(x, y) (((x) < (y)) ? (x) : (y))
  152. #endif
  153. #if !defined(STRINGIFY) || defined(__DOXYGEN__)
  154. /** Converts the given input into a string, via the C Preprocessor. This macro puts literal quotation
  155. * marks around the input, converting the source into a string literal.
  156. *
  157. * \param[in] x Input to convert into a string literal.
  158. *
  159. * \return String version of the input.
  160. */
  161. #define STRINGIFY(x) #x
  162. /** Converts the given input into a string after macro expansion, via the C Preprocessor. This macro puts
  163. * literal quotation marks around the expanded input, converting the source into a string literal.
  164. *
  165. * \param[in] x Input to expand and convert into a string literal.
  166. *
  167. * \return String version of the expanded input.
  168. */
  169. #define STRINGIFY_EXPANDED(x) STRINGIFY(x)
  170. #endif
  171. #if !defined(CONCAT) || defined(__DOXYGEN__)
  172. /** Concatenates the given input into a single token, via the C Preprocessor.
  173. *
  174. * \param[in] x First item to concatenate.
  175. * \param[in] y Second item to concatenate.
  176. *
  177. * \return Concatenated version of the input.
  178. */
  179. #define CONCAT(x, y) x ## y
  180. /** CConcatenates the given input into a single token after macro expansion, via the C Preprocessor.
  181. *
  182. * \param[in] x First item to concatenate.
  183. * \param[in] y Second item to concatenate.
  184. *
  185. * \return Concatenated version of the expanded input.
  186. */
  187. #define CONCAT_EXPANDED(x, y) CONCAT(x, y)
  188. #endif
  189. #if !defined(ISR) || defined(__DOXYGEN__)
  190. /** Macro for the definition of interrupt service routines, so that the compiler can insert the required
  191. * prologue and epilogue code to properly manage the interrupt routine without affecting the main thread's
  192. * state with unintentional side-effects.
  193. *
  194. * Interrupt handlers written using this macro may still need to be registered with the microcontroller's
  195. * Interrupt Controller (if present) before they will properly handle incoming interrupt events.
  196. *
  197. * \note This macro is only supplied on some architectures, where the standard library does not include a valid
  198. * definition. If an existing definition exists, the alternative definition here will be ignored.
  199. *
  200. * \ingroup Group_GlobalInt
  201. *
  202. * \param[in] Name Unique name of the interrupt service routine.
  203. */
  204. #define ISR(Name, ...) void Name (void) __attribute__((__interrupt__)) __VA_ARGS__; void Name (void)
  205. #endif
  206. /* Inline Functions: */
  207. /** Function to reverse the individual bits in a byte - i.e. bit 7 is moved to bit 0, bit 6 to bit 1,
  208. * etc.
  209. *
  210. * \param[in] Byte Byte of data whose bits are to be reversed.
  211. *
  212. * \return Input data with the individual bits reversed (mirrored).
  213. */
  214. static inline uint8_t BitReverse(uint8_t Byte) ATTR_WARN_UNUSED_RESULT ATTR_CONST;
  215. static inline uint8_t BitReverse(uint8_t Byte)
  216. {
  217. Byte = (((Byte & 0xF0) >> 4) | ((Byte & 0x0F) << 4));
  218. Byte = (((Byte & 0xCC) >> 2) | ((Byte & 0x33) << 2));
  219. Byte = (((Byte & 0xAA) >> 1) | ((Byte & 0x55) << 1));
  220. return Byte;
  221. }
  222. /** Function to perform a blocking delay for a specified number of milliseconds. The actual delay will be
  223. * at a minimum the specified number of milliseconds, however due to loop overhead and internal calculations
  224. * may be slightly higher.
  225. *
  226. * \param[in] Milliseconds Number of milliseconds to delay
  227. */
  228. static inline void Delay_MS(uint16_t Milliseconds) ATTR_ALWAYS_INLINE;
  229. static inline void Delay_MS(uint16_t Milliseconds)
  230. {
  231. #if (ARCH == ARCH_AVR8)
  232. if (GCC_IS_COMPILE_CONST(Milliseconds))
  233. {
  234. _delay_ms(Milliseconds);
  235. }
  236. else
  237. {
  238. while (Milliseconds--)
  239. _delay_ms(1);
  240. }
  241. #elif (ARCH == ARCH_UC3)
  242. while (Milliseconds--)
  243. {
  244. __builtin_mtsr(AVR32_COUNT, 0);
  245. while ((uint32_t)__builtin_mfsr(AVR32_COUNT) < (F_CPU / 1000));
  246. }
  247. #elif (ARCH == ARCH_XMEGA)
  248. if (GCC_IS_COMPILE_CONST(Milliseconds))
  249. {
  250. _delay_ms(Milliseconds);
  251. }
  252. else
  253. {
  254. while (Milliseconds--)
  255. _delay_ms(1);
  256. }
  257. #endif
  258. }
  259. /** Retrieves a mask which contains the current state of the global interrupts for the device. This
  260. * value can be stored before altering the global interrupt enable state, before restoring the
  261. * flag(s) back to their previous values after a critical section using \ref SetGlobalInterruptMask().
  262. *
  263. * \ingroup Group_GlobalInt
  264. *
  265. * \return Mask containing the current Global Interrupt Enable Mask bit(s).
  266. */
  267. static inline uint_reg_t GetGlobalInterruptMask(void) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT;
  268. static inline uint_reg_t GetGlobalInterruptMask(void)
  269. {
  270. GCC_MEMORY_BARRIER();
  271. #if (ARCH == ARCH_AVR8)
  272. return SREG;
  273. #elif (ARCH == ARCH_UC3)
  274. return __builtin_mfsr(AVR32_SR);
  275. #elif (ARCH == ARCH_XMEGA)
  276. return SREG;
  277. #endif
  278. }
  279. /** Sets the global interrupt enable state of the microcontroller to the mask passed into the function.
  280. * This can be combined with \ref GetGlobalInterruptMask() to save and restore the Global Interrupt Enable
  281. * Mask bit(s) of the device after a critical section has completed.
  282. *
  283. * \ingroup Group_GlobalInt
  284. *
  285. * \param[in] GlobalIntState Global Interrupt Enable Mask value to use
  286. */
  287. static inline void SetGlobalInterruptMask(const uint_reg_t GlobalIntState) ATTR_ALWAYS_INLINE;
  288. static inline void SetGlobalInterruptMask(const uint_reg_t GlobalIntState)
  289. {
  290. GCC_MEMORY_BARRIER();
  291. #if (ARCH == ARCH_AVR8)
  292. SREG = GlobalIntState;
  293. #elif (ARCH == ARCH_UC3)
  294. if (GlobalIntState & AVR32_SR_GM)
  295. __builtin_ssrf(AVR32_SR_GM_OFFSET);
  296. else
  297. __builtin_csrf(AVR32_SR_GM_OFFSET);
  298. #elif (ARCH == ARCH_XMEGA)
  299. SREG = GlobalIntState;
  300. #endif
  301. GCC_MEMORY_BARRIER();
  302. }
  303. /** Enables global interrupt handling for the device, allowing interrupts to be handled.
  304. *
  305. * \ingroup Group_GlobalInt
  306. */
  307. static inline void GlobalInterruptEnable(void) ATTR_ALWAYS_INLINE;
  308. static inline void GlobalInterruptEnable(void)
  309. {
  310. GCC_MEMORY_BARRIER();
  311. #if (ARCH == ARCH_AVR8)
  312. sei();
  313. #elif (ARCH == ARCH_UC3)
  314. __builtin_csrf(AVR32_SR_GM_OFFSET);
  315. #elif (ARCH == ARCH_XMEGA)
  316. sei();
  317. #endif
  318. GCC_MEMORY_BARRIER();
  319. }
  320. /** Disabled global interrupt handling for the device, preventing interrupts from being handled.
  321. *
  322. * \ingroup Group_GlobalInt
  323. */
  324. static inline void GlobalInterruptDisable(void) ATTR_ALWAYS_INLINE;
  325. static inline void GlobalInterruptDisable(void)
  326. {
  327. GCC_MEMORY_BARRIER();
  328. #if (ARCH == ARCH_AVR8)
  329. cli();
  330. #elif (ARCH == ARCH_UC3)
  331. __builtin_ssrf(AVR32_SR_GM_OFFSET);
  332. #elif (ARCH == ARCH_XMEGA)
  333. cli();
  334. #endif
  335. GCC_MEMORY_BARRIER();
  336. }
  337. /* Disable C linkage for C++ Compilers: */
  338. #if defined(__cplusplus)
  339. }
  340. #endif
  341. #endif
  342. /** @} */