LPCOpen Platform
LPCOpen Platform for NXP LPC Microcontrollers
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Common.h
Go to the documentation of this file.
1 /*
2  * @brief LPCUSB library's common macros, definitions
3  *
4  * @note
5  * Copyright(C) NXP Semiconductors, 2012
6  * Copyright(C) Dean Camera, 2011, 2012
7  * All rights reserved.
8  *
9  * @par
10  * Software that is described herein is for illustrative purposes only
11  * which provides customers with programming information regarding the
12  * LPC products. This software is supplied "AS IS" without any warranties of
13  * any kind, and NXP Semiconductors and its licensor disclaim any and
14  * all warranties, express or implied, including all implied warranties of
15  * merchantability, fitness for a particular purpose and non-infringement of
16  * intellectual property rights. NXP Semiconductors assumes no responsibility
17  * or liability for the use of the software, conveys no license or rights under any
18  * patent, copyright, mask work right, or any other intellectual property rights in
19  * or to any products. NXP Semiconductors reserves the right to make changes
20  * in the software without notification. NXP Semiconductors also makes no
21  * representation or warranty that such application will be suitable for the
22  * specified use without further testing or modification.
23  *
24  * @par
25  * Permission to use, copy, modify, and distribute this software and its
26  * documentation is hereby granted, under NXP Semiconductors' and its
27  * licensor's relevant copyrights in the software, without fee, provided that it
28  * is used in conjunction with NXP Semiconductors microcontrollers. This
29  * copyright, permission, and disclaimer notice must appear in all copies of
30  * this code.
31 */
32 
33 
50 #ifndef __LPCUSBlib_COMMON_H__
51 #define __LPCUSBlib_COMMON_H__
52 
53  /* Macros: */
54  #define __INCLUDE_FROM_COMMON_H
55 
56  /* Includes: */
57  #include <stdint.h>
58  #include <stdbool.h>
59  #include <string.h>
60  #include <stddef.h>
61 
62  #if defined(USE_LUFA_CONFIG_HEADER)
63  #include "LUFAConfig.h"
64  #endif
65 
66  #if 1 // TODO add control macros later
67  #include "../LPCUSBlibConfig.h"
68  #endif
69 
70  #include "ArchitectureSpecific.h"
71  #include "CompilerSpecific.h"
72  #include "Architectures.h"
73  #include "Attributes.h"
74  #include "BoardTypes.h"
75 
76  /* Enable C linkage for C++ Compilers: */
77  #if defined(__cplusplus)
78  extern "C" {
79  #endif
80 
81  /* Architecture specific utility includes: */
82  #if defined(__DOXYGEN__)
83 
87  typedef MACHINE_REG_t uint_reg_t;
88  #elif (ARCH == ARCH_AVR8)
89  #include <avr/io.h>
90  #include <avr/interrupt.h>
91  #include <avr/pgmspace.h>
92  #include <avr/eeprom.h>
93  #include <avr/boot.h>
94  #include <util/delay.h>
95 
96  typedef uint8_t uint_reg_t;
97 
98  #define ARCH_HAS_EEPROM_ADDRESS_SPACE
99  #define ARCH_HAS_FLASH_ADDRESS_SPACE
100  #define ARCH_HAS_MULTI_ADDRESS_SPACE
101  #define ARCH_LITTLE_ENDIAN
102 
103  #include "Endianness.h"
104  #elif (ARCH == ARCH_UC3)
105  #include <avr32/io.h>
106 
107  // === TODO: Find abstracted way to handle these ===
108  #define PROGMEM const
109  #define pgm_read_byte(x) *x
110  #define memcmp_P(...) memcmp(__VA_ARGS__)
111  #define memcpy_P(...) memcpy(__VA_ARGS__)
112  // =================================================
113 
114  typedef uint32_t uint_reg_t;
115 
116  #define ARCH_BIG_ENDIAN
117 
118  #include "Endianness.h"
119  #elif (ARCH == ARCH_XMEGA)
120  #include <avr/io.h>
121  #include <avr/interrupt.h>
122  #include <avr/pgmspace.h>
123  #include <avr/eeprom.h>
124  #include <util/delay.h>
125 
126  typedef uint8_t uint_reg_t;
127 
128  #define ARCH_HAS_EEPROM_ADDRESS_SPACE
129  #define ARCH_HAS_FLASH_ADDRESS_SPACE
130  #define ARCH_HAS_MULTI_ADDRESS_SPACE
131  #define ARCH_LITTLE_ENDIAN
132 
133  #include "Endianness.h"
134  #elif (ARCH == ARCH_LPC)
135  typedef uint32_t uint_reg_t;
136  #define ARCH_LITTLE_ENDIAN
137  #define PROGMEM const
138  #define pgm_read_byte(x) (*x)
139  #define memcmp_P(...) memcmp(__VA_ARGS__)
140  #define memcpy_P(...) memcpy(__VA_ARGS__)
141  #include "Endianness.h"
142  #else
143  #error Unknown device architecture specified.
144  #endif
145 
146  /* Public Interface - May be used in end-application: */
147  /* Macros: */
153  #define MACROS do
154 
160  #define MACROE while (0)
161 
172  #if !defined(MAX) || defined(__DOXYGEN__)
173  #define MAX(x, y) (((x) > (y)) ? (x) : (y))
174  #endif
175 
186  #if !defined(MIN) || defined(__DOXYGEN__)
187  #define MIN(x, y) (((x) < (y)) ? (x) : (y))
188  #endif
189 
190  #if !defined(STRINGIFY) || defined(__DOXYGEN__)
191 
198  #define STRINGIFY(x) #x
199 
207  #define STRINGIFY_EXPANDED(x) STRINGIFY(x)
208  #endif
209 
210  #if !defined(ISR) || defined(__DOXYGEN__)
211 
225  #define ISR(Name, ...) void Name (void) __attribute__((__interrupt__)) __VA_ARGS__; void Name (void)
226  #endif
227 
228  /* Inline Functions: */
234  static inline uint8_t BitReverse(uint8_t Byte) ATTR_WARN_UNUSED_RESULT ATTR_CONST;
235  static inline uint8_t BitReverse(uint8_t Byte)
236  {
237  Byte = (((Byte & 0xF0) >> 4) | ((Byte & 0x0F) << 4));
238  Byte = (((Byte & 0xCC) >> 2) | ((Byte & 0x33) << 2));
239  Byte = (((Byte & 0xAA) >> 1) | ((Byte & 0x55) << 1));
240 
241  return Byte;
242  }
243 
251  static inline void Delay_MS(uint16_t Milliseconds) ATTR_ALWAYS_INLINE;
252  static inline void Delay_MS(uint16_t Milliseconds)
253  {
254  #if (ARCH == ARCH_AVR8)
255  if (GCC_IS_COMPILE_CONST(Milliseconds))
256  {
257  _delay_ms(Milliseconds);
258  }
259  else
260  {
261  while (Milliseconds--)
262  _delay_ms(1);
263  }
264  #elif (ARCH == ARCH_UC3)
265  while (Milliseconds--)
266  {
267  __builtin_mtsr(AVR32_COUNT, 0);
268  while (__builtin_mfsr(AVR32_COUNT) < (F_CPU / 1000));
269  }
270  #elif (ARCH == ARCH_XMEGA)
271  if (GCC_IS_COMPILE_CONST(Milliseconds))
272  {
273  _delay_ms(Milliseconds);
274  }
275  else
276  {
277  while (Milliseconds--)
278  _delay_ms(1);
279  }
280  #elif (ARCH == ARCH_LPC)
281  while (Milliseconds--)
282  {
283  volatile uint32_t i;
284 
285  for (i = 0; i < (4 * 1000); i++) { /* This logic was tested. It gives app. 1 micro sec delay */
286  ;
287  }
288  }
289  #endif
290  }
291 
301  static inline uint_reg_t GetGlobalInterruptMask(void) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT;
302  static inline uint_reg_t GetGlobalInterruptMask(void)
303  {
305 
306  #if (ARCH == ARCH_AVR8)
307  return SREG;
308  #elif (ARCH == ARCH_UC3)
309  return __builtin_mfsr(AVR32_SR);
310  #elif (ARCH == ARCH_XMEGA)
311  return SREG;
312  #elif (ARCH == ARCH_LPC)
313  // TODO #warning GetGlobalInterruptMask() is not implemented under ARCH_LPC.
314  return 0;
315  #endif
316 
318  }
319 
329  static inline void SetGlobalInterruptMask(const uint_reg_t GlobalIntState) ATTR_ALWAYS_INLINE;
330  static inline void SetGlobalInterruptMask(const uint_reg_t GlobalIntState)
331  {
333 
334  #if (ARCH == ARCH_AVR8)
335  SREG = GlobalIntState;
336  #elif (ARCH == ARCH_UC3)
337  if (GlobalIntState & AVR32_SR_GM)
338  __builtin_ssrf(AVR32_SR_GM_OFFSET);
339  else
340  __builtin_csrf(AVR32_SR_GM_OFFSET);
341  #elif (ARCH == ARCH_XMEGA)
342  SREG = GlobalIntState;
343  #elif (ARCH == ARCH_LPC)
344  // TODO #warning SetGlobalInterruptMask() is not implemented under ARCH_LPC.
345  #endif
346 
348  }
349 
355  static inline void GlobalInterruptEnable(void) ATTR_ALWAYS_INLINE;
356  static inline void GlobalInterruptEnable(void)
357  {
359 
360  #if (ARCH == ARCH_AVR8)
361  sei();
362  #elif (ARCH == ARCH_UC3)
363  __builtin_csrf(AVR32_SR_GM_OFFSET);
364  #elif (ARCH == ARCH_XMEGA)
365  sei();
366  #elif (ARCH == ARCH_LPC)
367  // TODO #warning GlobalInterruptEnable() is not implemented under ARCH_LPC.
368  #endif
369 
371  }
372 
378  static inline void GlobalInterruptDisable(void) ATTR_ALWAYS_INLINE;
379  static inline void GlobalInterruptDisable(void)
380  {
382 
383  #if (ARCH == ARCH_AVR8)
384  cli();
385  #elif (ARCH == ARCH_UC3)
386  __builtin_ssrf(AVR32_SR_GM_OFFSET);
387  #elif (ARCH == ARCH_XMEGA)
388  cli();
389  #elif (ARCH == ARCH_LPC)
390  // TODO #warning GlobalInterruptDisable() is not implemented under ARCH_LPC.
391  #endif
392 
394  }
395 
396  /* Disable C linkage for C++ Compilers: */
397  #if defined(__cplusplus)
398  }
399  #endif
400 
401 #endif
402