Rev Author Line No. Line
3331 kaklik 1 // ======================================================================
2 // Common definitions for the USB driver
3 //
3333 kaklik 4 // Copyright 2006-2010 Dick Streefland
3331 kaklik 5 //
6 // This is free software, licensed under the terms of the GNU General
7 // Public License as published by the Free Software Foundation.
8 // ======================================================================
9  
10 #ifdef __ASSEMBLER__
11 #define __SFR_OFFSET 0
12 #endif
13 #include <avr/io.h>
14 #include "usbtiny.h"
15  
16 // Preprocessor magic
17 #define CAT2(a,b) CAT2EXP(a, b)
18 #define CAT2EXP(a,b) a ## b
19 #define CAT3(a,b,c) CAT3EXP(a, b, c)
20 #define CAT3EXP(a,b,c) a ## b ## c
21  
3333 kaklik 22 // I/O Ports for USB
3331 kaklik 23 #define USB_IN CAT2(PIN, USBTINY_PORT)
24 #define USB_OUT CAT2(PORT, USBTINY_PORT)
25 #define USB_DDR CAT2(DDR, USBTINY_PORT)
26  
3333 kaklik 27 // I/O bit masks for USB
3331 kaklik 28 #define USB_MASK_DMINUS (1 << (USBTINY_DMINUS))
29 #define USB_MASK_DPLUS (1 << (USBTINY_DPLUS))
30 #define USB_MASK (USB_MASK_DMINUS | USB_MASK_DPLUS)
31  
32 // Interrupt configuration
33 #if defined EICRA
34 # define USB_INT_CONFIG EICRA
35 #else
36 # define USB_INT_CONFIG MCUCR
37 #endif
38 #define USB_INT_CONFIG_SET ((1 << CAT3(ISC,USBTINY_INT,1)) | (1 << CAT3(ISC,USBTINY_INT,0)))
3333 kaklik 39 #define USB_INT_VECTOR CAT3(INT, USBTINY_INT, _vect)
3331 kaklik 40  
41 // Interrupt enable
42 #if defined GIMSK
43 # define USB_INT_ENABLE GIMSK
44 #elif defined EIMSK
45 # define USB_INT_ENABLE EIMSK
46 #else
47 # define USB_INT_ENABLE GICR
48 #endif
49 #define USB_INT_ENABLE_BIT CAT2(INT,USBTINY_INT)
50  
51 // Interrupt pending bit
52 #if defined EIFR
53 # define USB_INT_PENDING EIFR
54 #else
55 # define USB_INT_PENDING GIFR
56 #endif
57 #define USB_INT_PENDING_BIT CAT2(INTF,USBTINY_INT)
3333 kaklik 58 #if defined INF0 && ! defined INTF0
59 # define INTF0 INF0 // fix for incorrect definition in iotn13.h
60 #endif
3331 kaklik 61  
62 // USB PID values
63 #define USB_PID_SETUP 0x2d
64 #define USB_PID_OUT 0xe1
65 #define USB_PID_IN 0x69
66 #define USB_PID_DATA0 0xc3
67 #define USB_PID_DATA1 0x4b
68 #define USB_PID_ACK 0xd2
69 #define USB_PID_NAK 0x5a
70 #define USB_PID_STALL 0x1e
71  
72 // Various constants
73 #define USB_BUFSIZE 11 // PID + data + CRC
3333 kaklik 74  
75 // Bit manipulation macros
76 #define BIT_CLR(reg,bit) { (reg) &= ~ _BV(bit); }
77 #define BIT_SET(reg,bit) { (reg) |= _BV(bit); }
78 #define BIT_TST(reg,bit) (((reg) & _BV(bit)) != 0)
79  
80 // I/O port manipulation macros
81 #define DDR_CLR(p,b) BIT_CLR(DDR ## p, b)
82 #define DDR_SET(p,b) BIT_SET(DDR ## p, b)
83 #define PORT_CLR(p,b) BIT_CLR(PORT ## p, b)
84 #define PORT_SET(p,b) BIT_SET(PORT ## p, b)
85 #define PORT_TST(p,b) BIT_TST(PORT ## p, b)
86 #define PIN_TST(p,b) BIT_TST(PIN ## p, b)
87 #define PIN_SET(p,b) BIT_SET(PIN ## p, b)
88  
89 // Macros that can be used with an argument of the form (port,bit)
90 #define INPUT(bit) DDR_CLR bit
91 #define OUTPUT(bit) DDR_SET bit
92 #define CLR(bit) PORT_CLR bit
93 #define SET(bit) PORT_SET bit
94 #define ISSET(bit) PORT_TST bit
95 #define TST(bit) PIN_TST bit
96 #define TOGGLE(bit) PIN_SET bit