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