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 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /*
  2. LUFA Library
  3. Copyright (C) Dean Camera, 2012.
  4. dean [at] fourwalledcubicle [dot] com
  5. www.lufa-lib.org
  6. */
  7. /*
  8. Copyright 2012 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 disclaim 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. /** Macro for encasing other multi-statement macros. This should be used along with an opening brace
  123. * before the start of any multi-statement macro, so that the macros contents as a whole are treated
  124. * as a discrete block and not as a list of separate statements which may cause problems when used as
  125. * a block (such as inline \c if statements).
  126. */
  127. #define MACROS do
  128. /** Macro for encasing other multi-statement macros. This should be used along with a preceding closing
  129. * brace at the end of any multi-statement macro, so that the macros contents as a whole are treated
  130. * as a discrete block and not as a list of separate statements which may cause problems when used as
  131. * a block (such as inline \c if statements).
  132. */
  133. #define MACROE while (0)
  134. /** Convenience macro to determine the larger of two values.
  135. *
  136. * \attention This macro should only be used with operands that do not have side effects from being evaluated
  137. * multiple times.
  138. *
  139. * \param[in] x First value to compare
  140. * \param[in] y First value to compare
  141. *
  142. * \return The larger of the two input parameters
  143. */
  144. #if !defined(MAX) || defined(__DOXYGEN__)
  145. #define MAX(x, y) (((x) > (y)) ? (x) : (y))
  146. #endif
  147. /** Convenience macro to determine the smaller of two values.
  148. *
  149. * \attention This macro should only be used with operands that do not have side effects from being evaluated
  150. * multiple times.
  151. *
  152. * \param[in] x First value to compare
  153. * \param[in] y First value to compare
  154. *
  155. * \return The smaller of the two input parameters
  156. */
  157. #if !defined(MIN) || defined(__DOXYGEN__)
  158. #define MIN(x, y) (((x) < (y)) ? (x) : (y))
  159. #endif
  160. #if !defined(STRINGIFY) || defined(__DOXYGEN__)
  161. /** Converts the given input into a string, via the C Preprocessor. This macro puts literal quotation
  162. * marks around the input, converting the source into a string literal.
  163. *
  164. * \param[in] x Input to convert into a string literal.
  165. *
  166. * \return String version of the input.
  167. */
  168. #define STRINGIFY(x) #x
  169. /** Converts the given input into a string after macro expansion, via the C Preprocessor. This macro puts
  170. * literal quotation marks around the expanded input, converting the source into a string literal.
  171. *
  172. * \param[in] x Input to expand and convert into a string literal.
  173. *
  174. * \return String version of the expanded input.
  175. */
  176. #define STRINGIFY_EXPANDED(x) STRINGIFY(x)
  177. #endif
  178. #if !defined(ISR) || defined(__DOXYGEN__)
  179. /** Macro for the definition of interrupt service routines, so that the compiler can insert the required
  180. * prologue and epilogue code to properly manage the interrupt routine without affecting the main thread's
  181. * state with unintentional side-effects.
  182. *
  183. * Interrupt handlers written using this macro may still need to be registered with the microcontroller's
  184. * Interrupt Controller (if present) before they will properly handle incoming interrupt events.
  185. *
  186. * \note This macro is only supplied on some architectures, where the standard library does not include a valid
  187. * definition. If an existing definition exists, the alternative definition here will be ignored.
  188. *
  189. * \ingroup Group_GlobalInt
  190. *
  191. * \param[in] Name Unique name of the interrupt service routine.
  192. */
  193. #define ISR(Name, ...) void Name (void) __attribute__((__interrupt__)) __VA_ARGS__; void Name (void)
  194. #endif
  195. /* Inline Functions: */
  196. /** Function to reverse the individual bits in a byte - i.e. bit 7 is moved to bit 0, bit 6 to bit 1,
  197. * etc.
  198. *
  199. * \param[in] Byte Byte of data whose bits are to be reversed.
  200. *
  201. * \return Input data with the individual bits reversed (mirrored).
  202. */
  203. static inline uint8_t BitReverse(uint8_t Byte) ATTR_WARN_UNUSED_RESULT ATTR_CONST;
  204. static inline uint8_t BitReverse(uint8_t Byte)
  205. {
  206. Byte = (((Byte & 0xF0) >> 4) | ((Byte & 0x0F) << 4));
  207. Byte = (((Byte & 0xCC) >> 2) | ((Byte & 0x33) << 2));
  208. Byte = (((Byte & 0xAA) >> 1) | ((Byte & 0x55) << 1));
  209. return Byte;
  210. }
  211. /** Function to perform a blocking delay for a specified number of milliseconds. The actual delay will be
  212. * at a minimum the specified number of milliseconds, however due to loop overhead and internal calculations
  213. * may be slightly higher.
  214. *
  215. * \param[in] Milliseconds Number of milliseconds to delay
  216. */
  217. static inline void Delay_MS(uint16_t Milliseconds) ATTR_ALWAYS_INLINE;
  218. static inline void Delay_MS(uint16_t Milliseconds)
  219. {
  220. #if (ARCH == ARCH_AVR8)
  221. if (GCC_IS_COMPILE_CONST(Milliseconds))
  222. {
  223. _delay_ms(Milliseconds);
  224. }
  225. else
  226. {
  227. while (Milliseconds--)
  228. _delay_ms(1);
  229. }
  230. #elif (ARCH == ARCH_UC3)
  231. while (Milliseconds--)
  232. {
  233. __builtin_mtsr(AVR32_COUNT, 0);
  234. while ((uint32_t)__builtin_mfsr(AVR32_COUNT) < (F_CPU / 1000));
  235. }
  236. #elif (ARCH == ARCH_XMEGA)
  237. if (GCC_IS_COMPILE_CONST(Milliseconds))
  238. {
  239. _delay_ms(Milliseconds);
  240. }
  241. else
  242. {
  243. while (Milliseconds--)
  244. _delay_ms(1);
  245. }
  246. #endif
  247. }
  248. /** Retrieves a mask which contains the current state of the global interrupts for the device. This
  249. * value can be stored before altering the global interrupt enable state, before restoring the
  250. * flag(s) back to their previous values after a critical section using \ref SetGlobalInterruptMask().
  251. *
  252. * \ingroup Group_GlobalInt
  253. *
  254. * \return Mask containing the current Global Interrupt Enable Mask bit(s).
  255. */
  256. static inline uint_reg_t GetGlobalInterruptMask(void) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT;
  257. static inline uint_reg_t GetGlobalInterruptMask(void)
  258. {
  259. GCC_MEMORY_BARRIER();
  260. #if (ARCH == ARCH_AVR8)
  261. return SREG;
  262. #elif (ARCH == ARCH_UC3)
  263. return __builtin_mfsr(AVR32_SR);
  264. #elif (ARCH == ARCH_XMEGA)
  265. return SREG;
  266. #endif
  267. }
  268. /** Sets the global interrupt enable state of the microcontroller to the mask passed into the function.
  269. * This can be combined with \ref GetGlobalInterruptMask() to save and restore the Global Interrupt Enable
  270. * Mask bit(s) of the device after a critical section has completed.
  271. *
  272. * \ingroup Group_GlobalInt
  273. *
  274. * \param[in] GlobalIntState Global Interrupt Enable Mask value to use
  275. */
  276. static inline void SetGlobalInterruptMask(const uint_reg_t GlobalIntState) ATTR_ALWAYS_INLINE;
  277. static inline void SetGlobalInterruptMask(const uint_reg_t GlobalIntState)
  278. {
  279. GCC_MEMORY_BARRIER();
  280. #if (ARCH == ARCH_AVR8)
  281. SREG = GlobalIntState;
  282. #elif (ARCH == ARCH_UC3)
  283. if (GlobalIntState & AVR32_SR_GM)
  284. __builtin_ssrf(AVR32_SR_GM_OFFSET);
  285. else
  286. __builtin_csrf(AVR32_SR_GM_OFFSET);
  287. #elif (ARCH == ARCH_XMEGA)
  288. SREG = GlobalIntState;
  289. #endif
  290. GCC_MEMORY_BARRIER();
  291. }
  292. /** Enables global interrupt handling for the device, allowing interrupts to be handled.
  293. *
  294. * \ingroup Group_GlobalInt
  295. */
  296. static inline void GlobalInterruptEnable(void) ATTR_ALWAYS_INLINE;
  297. static inline void GlobalInterruptEnable(void)
  298. {
  299. GCC_MEMORY_BARRIER();
  300. #if (ARCH == ARCH_AVR8)
  301. sei();
  302. #elif (ARCH == ARCH_UC3)
  303. __builtin_csrf(AVR32_SR_GM_OFFSET);
  304. #elif (ARCH == ARCH_XMEGA)
  305. sei();
  306. #endif
  307. GCC_MEMORY_BARRIER();
  308. }
  309. /** Disabled global interrupt handling for the device, preventing interrupts from being handled.
  310. *
  311. * \ingroup Group_GlobalInt
  312. */
  313. static inline void GlobalInterruptDisable(void) ATTR_ALWAYS_INLINE;
  314. static inline void GlobalInterruptDisable(void)
  315. {
  316. GCC_MEMORY_BARRIER();
  317. #if (ARCH == ARCH_AVR8)
  318. cli();
  319. #elif (ARCH == ARCH_UC3)
  320. __builtin_ssrf(AVR32_SR_GM_OFFSET);
  321. #elif (ARCH == ARCH_XMEGA)
  322. cli();
  323. #endif
  324. GCC_MEMORY_BARRIER();
  325. }
  326. /* Disable C linkage for C++ Compilers: */
  327. #if defined(__cplusplus)
  328. }
  329. #endif
  330. #endif
  331. /** @} */