USBaspLoader ninth and a half stable release v0.95
[pub/USBaspLoader.git] / firmware / usbdrv / usbportability.h
1 /* Name: usbportability.h
2 * Project: V-USB, virtual USB port for Atmel's(r) AVR(r) microcontrollers
3 * Author: Christian Starkjohann
4 * Creation Date: 2008-06-17
5 * Tabsize: 4
6 * Copyright: (c) 2008 by OBJECTIVE DEVELOPMENT Software GmbH
7 * License: GNU GPL v2 (see License.txt), GNU GPL v3 or proprietary (CommercialLicense.txt)
8 * This Revision: $Id$
9 */
10
11 /*
12 General Description:
13 This header is intended to contain all (or at least most of) the compiler
14 and library dependent stuff. The C code is written for avr-gcc and avr-libc.
15 The API of other development environments is converted to gcc's and avr-libc's
16 API by means of defines.
17
18 This header also contains all system includes since they depend on the
19 development environment.
20
21 Thanks to Oleg Semyonov for his help with the IAR tools port!
22 */
23
24 #ifndef __usbportability_h_INCLUDED__
25 #define __usbportability_h_INCLUDED__
26 #include "usbconfig.h"
27
28 /* We check explicitly for IAR and CodeVision. Default is avr-gcc/avr-libc. */
29
30 /* ------------------------------------------------------------------------- */
31 #if defined __IAR_SYSTEMS_ICC__ || defined __IAR_SYSTEMS_ASM__ /* check for IAR */
32 /* ------------------------------------------------------------------------- */
33
34 #ifndef ENABLE_BIT_DEFINITIONS
35 # define ENABLE_BIT_DEFINITIONS 1 /* Enable bit definitions */
36 #endif
37
38 /* Include IAR headers */
39 #include <ioavr.h>
40 #ifndef __IAR_SYSTEMS_ASM__
41 # include <inavr.h>
42 #endif
43
44 #define __attribute__(arg) /* not supported on IAR */
45
46 #ifdef __IAR_SYSTEMS_ASM__
47 # define __ASSEMBLER__ /* IAR does not define standard macro for asm */
48 #endif
49
50 #ifdef __HAS_ELPM__
51 # define PROGMEM __farflash
52 #else
53 # define PROGMEM __flash
54 #endif
55
56 #define USB_READ_FLASH(addr) (*(PROGMEM char *)(addr))
57
58 /* The following definitions are not needed by the driver, but may be of some
59 * help if you port a gcc based project to IAR.
60 */
61 #define cli() __disable_interrupt()
62 #define sei() __enable_interrupt()
63 #define wdt_reset() __watchdog_reset()
64 #define _BV(x) (1 << (x))
65
66 /* assembler compatibility macros */
67 #define nop2 rjmp $+2 /* jump to next instruction */
68 #define XL r26
69 #define XH r27
70 #define YL r28
71 #define YH r29
72 #define ZL r30
73 #define ZH r31
74 #define lo8(x) LOW(x)
75 #define hi8(x) (((x)>>8) & 0xff) /* not HIGH to allow XLINK to make a proper range check */
76
77 /* Depending on the device you use, you may get problems with the way usbdrv.h
78 * handles the differences between devices. Since IAR does not use #defines
79 * for MCU registers, we can't check for the existence of a particular
80 * register with an #ifdef. If the autodetection mechanism fails, include
81 * definitions for the required USB_INTR_* macros in your usbconfig.h. See
82 * usbconfig-prototype.h and usbdrv.h for details.
83 */
84
85 /* ------------------------------------------------------------------------- */
86 #elif __CODEVISIONAVR__ /* check for CodeVision AVR */
87 /* ------------------------------------------------------------------------- */
88 /* This port is not working (yet) */
89
90 /* #define F_CPU _MCU_CLOCK_FREQUENCY_ seems to be defined automatically */
91
92 #include <io.h>
93 #include <delay.h>
94
95 #define __attribute__(arg) /* not supported on IAR */
96
97 #define PROGMEM __flash
98 #define USB_READ_FLASH(addr) (*(PROGMEM char *)(addr))
99
100 #ifndef __ASSEMBLER__
101 static inline void cli(void)
102 {
103 #asm("cli");
104 }
105 static inline void sei(void)
106 {
107 #asm("sei");
108 }
109 #endif
110 #define _delay_ms(t) delay_ms(t)
111 #define _BV(x) (1 << (x))
112 #define USB_CFG_USE_SWITCH_STATEMENT 1 /* macro for if() cascase fails for unknown reason */
113
114 #define macro .macro
115 #define endm .endmacro
116 #define nop2 rjmp .+0 /* jump to next instruction */
117
118 /* ------------------------------------------------------------------------- */
119 #else /* default development environment is avr-gcc/avr-libc */
120 /* ------------------------------------------------------------------------- */
121
122 #include <avr/io.h>
123 #ifdef __ASSEMBLER__
124 # define _VECTOR(N) __vector_ ## N /* io.h does not define this for asm */
125 #else
126 # include <avr/pgmspace.h>
127 #endif
128
129 #if USB_CFG_DRIVER_FLASH_PAGE
130 # define USB_READ_FLASH(addr) pgm_read_byte_far(((long)USB_CFG_DRIVER_FLASH_PAGE << 16) | (long)(addr))
131 #else
132 # define USB_READ_FLASH(addr) pgm_read_byte(addr)
133 #endif
134
135 #define macro .macro
136 #define endm .endm
137 #define nop2 rjmp .+0 /* jump to next instruction */
138
139 #endif /* development environment */
140
141 /* for conveniecne, ensure that PRG_RDB exists */
142 #ifndef PRG_RDB
143 # define PRG_RDB(addr) USB_READ_FLASH(addr)
144 #endif
145 #endif /* __usbportability_h_INCLUDED__ */