-
Notifications
You must be signed in to change notification settings - Fork 0
/
includes.h
348 lines (268 loc) · 10 KB
/
includes.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*******************************************************************************
* File Name : includes.h
* Project : Generic AVR based
* Date : 2014/01/09
* Version : 1.1
* Target MCU : AT90USB8/162, ATMEGA16/32U4/U2, AT90USB64/1286
* Tool Chain : Atmel AVR Studio 4.19 730,
* WinAVR-20100110 or avr-gcc 4.7.0 and avr-libc 1.8.0
* Author : "Detlef Mueller" <detlef@gmail.com>
* Release Notes:
*
* $Id$
*
* $Log$
******************************************************************************/
#ifndef __includes_h__
#define __includes_h__
//------------------------------------------------------------------------------
#if defined(__AVR_AT90USB162__) || defined(__AVR_AT90USB82__)
#define __AVR_AT90USBX2__
#elif defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega32U4__)
#define __AVR_ATmegaXU4__
#elif defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__)
#define __AVR_AT90USBX6__
#elif defined(__AVR_ATmega8U2__) || defined(__AVR_ATmega16U2__) || defined(__AVR_ATmega32U2__)
#define __AVR_ATmegaXU2__
#else
#error "Unsupported device"
#endif
// #define __PROG_TYPES_COMPAT__
//------------------------------------------------------------------------------
#ifdef __ASSEMBLER__
//------------------------------------------------------------------------------
#define __SFR_OFFSET 0
//#define _VECTOR(N) __vector_ ## N /* io.h does not define this for asm */
#include <avr/io.h>
//------------------------------------------------------------------------------
#else // ! __ASSEMBLER__
//------------------------------------------------------------------------------
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <avr/interrupt.h>
#include <avr/wdt.h>
#include <avr/power.h>
#include <avr/sleep.h>
#include <util/delay_basic.h>
//------------------------------------------------------------------------------
#define VP( p ) (void *)(p)
#define ARRSZ( a ) (sizeof(a) / sizeof(*(a)))
#ifndef FALSE
#define FALSE 0
#endif
#ifndef TRUE
#define TRUE (! FALSE)
#endif
#ifndef NULL
#define NULL 0
#endif
//-------------------------------------------------------------------------------
// Things not defined in iom32u4.h
#if defined(__AVR_ATmegaXU4__)
#define PRTIM4 4 /* Power reduction register bit 4 for Timer 4 */
#define PB7 PORTB7
#define PB6 PORTB6
#define PB5 PORTB5
#define PB4 PORTB4
#define PB3 PORTB3
#define PB2 PORTB2
#define PB1 PORTB1
#define PB0 PORTB0
#define PC7 PORTC7
#define PC6 PORTC6
#define PD7 PORTD7
#define PD6 PORTD6
#define PD5 PORTD5
#define PD4 PORTD4
#define PD3 PORTD3
#define PD2 PORTD2
#define PD1 PORTD1
#define PD0 PORTD0
#define PE6 PORTE6
#define PE2 PORTE2
#define PF7 PORTF7
#define PF6 PORTF6
#define PF5 PORTF5
#define PF4 PORTF4
#define PF1 PORTF1
#define PF0 PORTF0
#endif
//------------------------------------------------------------------------------
// Missing in power.h for the ATmegaXU2 in avr-libc 1.6.7 (WinAVR 20100110)
#if defined(__AVR_ATmegaXU2__)
#if ! defined(clock_prescale_get)
typedef enum
{
clock_div_1 = 0, clock_div_2 = 1,
clock_div_4 = 2, clock_div_8 = 3,
clock_div_16 = 4, clock_div_32 = 5,
clock_div_64 = 6, clock_div_128 = 7,
clock_div_256 = 8
}
clock_div_t ;
#define clock_prescale_set( x ) \
do { \
uint8_t tmp = _BV(CLKPCE) ; \
__asm__ __volatile__ ( \
"in __tmp_reg__,__SREG__" "\n\t"\
"cli" "\n\t" \
"sts %1, %0" "\n\t" \
"sts %1, %2" "\n\t" \
"out __SREG__, __tmp_reg__" \
: /* no outputs */ \
: "d" (tmp), \
"M" (_SFR_MEM_ADDR(CLKPR)), \
"d" (x) \
: "r0" ) ; \
} while (0)
#define clock_prescale_get() \
(clock_div_t)(CLKPR & (uint8_t) \
((1<<CLKPS0) | (1<<CLKPS1) | \
(1<<CLKPS2) | (1<<CLKPS3)) )
#endif
#endif
//------------------------------------------------------------------------------
#define set_bit( sfr, bit ) (_SFR_BYTE(sfr) |= _BV(bit))
#define clr_bit( sfr, bit ) (_SFR_BYTE(sfr) &= ~_BV(bit))
#define tog_bit( sfr, bit ) (_SFR_BYTE(sfr) ^= _BV(bit))
#define set_bits( sfr, msk ) (_SFR_BYTE(sfr) |= (msk))
#define clr_bits( sfr, msk ) (_SFR_BYTE(sfr) &= ~(msk))
#define tog_bits( sfr, msk ) (_SFR_BYTE(sfr) ^= (msk))
#define bits_are_set( n, msk ) (((n) & (msk)) == (msk))
#define bits_are_clear( n, msk ) (!((n) & (msk)))
//------------------------------------------------------------------------------
#define __FA__( _func, ... ) __attribute__((__VA_ARGS__)) _func
#define FA_NAKED( _f ) __FA__( _f, __naked__ )
#define FA_NORETURN( _f ) __FA__( _f, __noreturn__ )
#define FA_NOINLINE( _f ) __FA__( _f, __noinline__ )
#define FA_NOINRET( _f ) __FA__( _f, __noinline__,__noreturn__ )
#define FA_INIT3( _f ) __FA__( _f, __used__,__naked__,__section__(".init3") )
#define VA_PROGMEM( _v ) _v __attribute__((__progmem__))
#define VA_NOINIT( _v ) _v __attribute__((__section__(".noinit")))
#ifdef __PROG_TYPES_COMPAT__
#define TA_PROGMEM( _t ) PROGMEM _t
#warning "Deprecated since the usage of PROGMEM on a type is not supported in GCC"
#else
#define TA_PROGMEM( _t ) _t
#endif
//------------------------------------------------------------------------------
#define __WRAP__( __c ) do __c while (0)
//------------------------------------------------------------------------------
#define CRITICAL_VAR() uint8_t __sSREG
#define ENTER_CRITICAL() __WRAP__( { __sSREG = SREG ; cli() ; } )
#define EXIT_CRITICAL() (SREG = __sSREG)
#define EXIT_CRITICAL_RET( n ) __WRAP__( { SREG = __sSREG ; return ( n ) ; } )
//------------------------------------------------------------------------------
#define JMP( _a ) __asm__ __volatile__ ( "jmp %[_ad]\n\t" :: [_ad] "i" (_a) )
#define RESET() JMP( 0 )
#define RET() __asm__ __volatile__ ( "ret" "\n\t" :: )
#define SLEEP() __asm__ __volatile__ ( "sleep" "\n\t" :: )
#define NOP() __asm__ __volatile__ ( "nop" "\n\t" :: )
#define NOP2() __asm__ __volatile__ ( "rjmp .+0" "\n\t" :: )
//------------------------------------------------------------------------------
static inline uint8_t
__FA__( ror8, __always_inline__ ) ( uint8_t _v )
{
__asm__ __volatile__
(
"bst %0,0" "\n\t"
"lsr %0" "\n\t"
"bld %0,7" "\n\t"
: "+r" (_v) : "0" (_v) : "cc"
) ;
return ( _v ) ;
}
static inline uint8_t
__FA__( rol8, __always_inline__ ) ( uint8_t _v )
{
__asm__ __volatile__
(
"bst %0,7" "\n\t"
"lsl %0" "\n\t"
"bld %0,0" "\n\t"
: "+r" (_v) : "0" (_v) : "cc"
) ;
return ( _v ) ;
}
//------------------------------------------------------------------------------
#if defined(__FIND_BL_ADDR) || defined(__AVR_ATmegaXU2__) || defined(__AVR_AT90USB162__)
#include <avr/boot.h>
#define _mBOOTSZ1 ((uint8_t)~FUSE_BOOTSZ1)
#define _mBOOTSZ0 ((uint8_t)~FUSE_BOOTSZ0)
#define _mBOOTSZ (_mBOOTSZ1 | _mBOOTSZ0)
#define _BL_ADDR( sz ) ((unsigned long)FLASHEND - \
(((unsigned long)FLASHEND > 0x8000u) ? \
((sz) << 1) : (sz)) + 1)
//typedef
// void (*__jmp)( void ) __attribute__((__noreturn__)) ;
//
//#define __JMP( adr ) ((__jmp)(adr >> 1))()
static inline void
__FA__( jmp_bootloader, __always_inline__,__noreturn__ ) ( void )
{
uint8_t
bls_sz = boot_lock_fuse_bits_get( GET_HIGH_FUSE_BITS ) & _mBOOTSZ ;
if ( bls_sz == _mBOOTSZ )
JMP( _BL_ADDR( 512 ) ) ;
else
if ( bls_sz == _mBOOTSZ1 )
JMP( _BL_ADDR( 1024 ) ) ;
else
if ( bls_sz == _mBOOTSZ0 )
JMP( _BL_ADDR( 2048 ) ) ;
JMP( _BL_ADDR( 4096 ) ) ;
for ( ;; ) ; // Shut up GCC
}
#else
#if defined(__AVR_ATmega16U2__)
#define __BL_ADDR 0x3000 /* KeyCard */
#elif defined(__AVR_AT90USB162__)
#define __BL_ADDR 0x3E00 /* Teensy 1.0 */
#elif defined(__AVR_ATmega32U4__)
#define __BL_ADDR 0x7E00 /* Teensy 2.0 */
#elif defined(__AVR_AT90USB646__)
#define __BL_ADDR 0xFC00 /* Teensy++ 1.0 */
#elif defined(__AVR_AT90USB1286__)
#define __BL_ADDR 0x1FC00 /* Teensy++ 2.0 */
#endif
#define jmp_bootloader() JMP( __BL_ADDR )
#endif
#if FOR_REFERENCE
#if defined(__AVR_AT90USB162__) // Teensy 1.0
#define bootloader() __WRAP__( { \
EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; \
TIMSK0 = 0; TIMSK1 = 0; UCSR1B = 0; \
DDRB = 0; DDRC = 0; DDRD = 0; \
PORTB = 0; PORTC = 0; PORTD = 0; \
jmp_bootloader(); }
#elif defined(__AVR_ATmega32U4__) // Teensy 2.0
#define bootloader() __WRAP__( { \
EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0; \
TIMSK0 = 0; TIMSK1 = 0; TIMSK3 = 0; TIMSK4 = 0; UCSR1B = 0; TWCR = 0; \
DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0; TWCR = 0; \
PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0; \
jmp_bootloader(); }
#elif defined(__AVR_AT90USB646__) // Teensy++ 1.0
#define bootloader() __WRAP__( { \
EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0; \
TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0; TIMSK3 = 0; UCSR1B = 0; TWCR = 0; \
DDRA = 0; DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0; \
PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0; \
jmp_bootloader(); }
#elif defined(__AVR_AT90USB1286__) // Teensy++ 2.0
#define bootloader() __WRAP__( { \
EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0; \
TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0; TIMSK3 = 0; UCSR1B = 0; TWCR = 0; \
DDRA = 0; DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0; \
PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0; \
jmp_bootloader(); }
#endif
#endif
//------------------------------------------------------------------------------
#endif // __ASSEMBLER__
//------------------------------------------------------------------------------
#define _B1( b ) _BV(b)
#define _B0( b ) 0
//------------------------------------------------------------------------------
#endif
//------------------------------------------------------------------------------