Rev 3514 Rev 3516
1 /* Name: main.c 1 /* Name: main.c
2 * Project: i2c-tiny-usb 2 * Project: i2c-tiny-usb
3 * Author: Till Harbaum 3 * Author: Till Harbaum
4 * Tabsize: 4 4 * Tabsize: 4
5 * Copyright: (c) 2005 by Till Harbaum <till@harbaum.org> 5 * Copyright: (c) 2005 by Till Harbaum <till@harbaum.org>
6 * License: GPL 6 * License: GPL
7 * This Revision: $Id: main.c,v 1.9 2007/06/07 13:53:47 harbaum Exp $ 7 * This Revision: $Id: main.c,v 1.9 2007/06/07 13:53:47 harbaum Exp $
8 * 8 *
9 * $Log: main.c,v $ 9 * $Log: main.c,v $
10 * Revision 1.9 2007/06/07 13:53:47 harbaum 10 * Revision 1.9 2007/06/07 13:53:47 harbaum
11 * Version number fixes 11 * Version number fixes
12 * 12 *
13 * Revision 1.8 2007/05/19 12:30:11 harbaum 13 * Revision 1.8 2007/05/19 12:30:11 harbaum
14 * Updated USB stacks 14 * Updated USB stacks
15 * 15 *
16 * Revision 1.7 2007/04/22 10:34:05 harbaum 16 * Revision 1.7 2007/04/22 10:34:05 harbaum
17 * *** empty log message *** 17 * *** empty log message ***
18 * 18 *
19 * Revision 1.6 2007/01/05 19:30:58 harbaum 19 * Revision 1.6 2007/01/05 19:30:58 harbaum
20 * i2c clock bug fix 20 * i2c clock bug fix
21 * 21 *
22 * Revision 1.5 2007/01/03 18:35:07 harbaum 22 * Revision 1.5 2007/01/03 18:35:07 harbaum
23 * usbtiny fixes and pcb layouts 23 * usbtiny fixes and pcb layouts
24 * 24 *
25 * Revision 1.4 2006/12/03 21:28:59 harbaum 25 * Revision 1.4 2006/12/03 21:28:59 harbaum
26 * *** empty log message *** 26 * *** empty log message ***
27 * 27 *
28 * Revision 1.3 2006/11/22 19:12:45 harbaum 28 * Revision 1.3 2006/11/22 19:12:45 harbaum
29 * Added usbtiny support 29 * Added usbtiny support
30 * 30 *
31 * Revision 1.2 2006/11/14 19:15:13 harbaum 31 * Revision 1.2 2006/11/14 19:15:13 harbaum
32 * *** empty log message *** 32 * *** empty log message ***
33 * 33 *
34 */ 34 */
35   35  
36 #include <stdio.h> 36 #include <stdio.h>
37 #include <ctype.h> 37 #include <ctype.h>
38 #include <string.h> 38 #include <string.h>
39   39  
40 #include <avr/io.h> 40 #include <avr/io.h>
41 #include <avr/interrupt.h> 41 #include <avr/interrupt.h>
42 #include <avr/pgmspace.h> 42 #include <avr/pgmspace.h>
43 #include <avr/wdt.h> 43 #include <avr/wdt.h>
44   44  
45 #include <util/delay.h> 45 #include <util/delay.h>
46   46  
47 #ifndef USBTINY -  
48 // use avrusb library -  
49 #include "usbdrv.h" -  
50 #include "oddebug.h" 47 #include "config.h"
51 #else -  
52 // use usbtiny library -  
53 #include "usb.h" -  
54 #include "usbtiny.h" -  
55 typedef byte_t uchar; -  
56   -  
57 #if! defined (__AVR_ATtiny45__) -  
58 #define USBDDR DDRC -  
59 #define USB_CFG_IOPORT PORTC -  
60 #else -  
61 #define USBDDR DDRB -  
62 #define USB_CFG_IOPORT PORTB -  
63 #endif -  
64   48  
65 #define USB_CFG_DMINUS_BIT USBTINY_DMINUS 49 // Include USB library (AVRUSB or USBTINY)
66 #define USB_CFG_DPLUS_BIT USBTINY_DPLUS 50 // -------------------
67   51  
-   52 #ifndef USBTINY
-   53 // use avrusb library
-   54 #include "usbdrv.h"
-   55 #include "oddebug.h"
-   56 #else
-   57 // use usbtiny library
-   58 #include "usb.h"
-   59 #include "usbtiny.h"
-   60 typedef byte_t uchar;
68 #define usbInit() usb_init() 61 #define usbInit() usb_init()
69 #define usbPoll() usb_poll() 62 #define usbPoll() usb_poll()
70 #endif 63 #endif
71   64  
72 #define ENABLE_SCL_EXPAND -  
73   -  
74 /* commands from USB, must e.g. match command ids in kernel driver */ 65 /* commands from USB, must e.g. match command ids in kernel driver */
75 #define CMD_ECHO 0 66 #define CMD_ECHO 0
76 #define CMD_GET_FUNC 1 67 #define CMD_GET_FUNC 1
77 #define CMD_SET_DELAY 2 68 #define CMD_SET_DELAY 2
78 #define CMD_GET_STATUS 3 69 #define CMD_GET_STATUS 3
79   70  
80 #define CMD_I2C_IO 4 71 #define CMD_I2C_IO 4
81 #define CMD_I2C_BEGIN 1 // flag fo I2C_IO 72 #define CMD_I2C_BEGIN 1 // flag fo I2C_IO
82 #define CMD_I2C_END 2 // flag fo I2C_IO 73 #define CMD_I2C_END 2 // flag fo I2C_IO
83   74  
84 /* linux kernel flags */ 75 /* linux kernel flags */
85 #define I2C_M_TEN 0x10 /* we have a ten bit chip address */ 76 #define I2C_M_TEN 0x10 /* we have a ten bit chip address */
86 #define I2C_M_RD 0x01 77 #define I2C_M_RD 0x01
87 #define I2C_M_NOSTART 0x4000 78 #define I2C_M_NOSTART 0x4000
88 #define I2C_M_REV_DIR_ADDR 0x2000 79 #define I2C_M_REV_DIR_ADDR 0x2000
89 #define I2C_M_IGNORE_NAK 0x1000 80 #define I2C_M_IGNORE_NAK 0x1000
90 #define I2C_M_NO_RD_ACK 0x0800 81 #define I2C_M_NO_RD_ACK 0x0800
91   82  
92 /* To determine what functionality is present */ 83 /* To determine what functionality is present */
93 #define I2C_FUNC_I2C 0x00000001 84 #define I2C_FUNC_I2C 0x00000001
94 #define I2C_FUNC_10BIT_ADDR 0x00000002 85 #define I2C_FUNC_10BIT_ADDR 0x00000002
95 #define I2C_FUNC_PROTOCOL_MANGLING 0x00000004 /* I2C_M_{REV_DIR_ADDR,NOSTART,..} */ 86 #define I2C_FUNC_PROTOCOL_MANGLING 0x00000004 /* I2C_M_{REV_DIR_ADDR,NOSTART,..} */
96 #define I2C_FUNC_SMBUS_HWPEC_CALC 0x00000008 /* SMBus 2.0 */ 87 #define I2C_FUNC_SMBUS_HWPEC_CALC 0x00000008 /* SMBus 2.0 */
97 #define I2C_FUNC_SMBUS_READ_WORD_DATA_PEC 0x00000800 /* SMBus 2.0 */ 88 #define I2C_FUNC_SMBUS_READ_WORD_DATA_PEC 0x00000800 /* SMBus 2.0 */
98 #define I2C_FUNC_SMBUS_WRITE_WORD_DATA_PEC 0x00001000 /* SMBus 2.0 */ 89 #define I2C_FUNC_SMBUS_WRITE_WORD_DATA_PEC 0x00001000 /* SMBus 2.0 */
99 #define I2C_FUNC_SMBUS_PROC_CALL_PEC 0x00002000 /* SMBus 2.0 */ 90 #define I2C_FUNC_SMBUS_PROC_CALL_PEC 0x00002000 /* SMBus 2.0 */
100 #define I2C_FUNC_SMBUS_BLOCK_PROC_CALL_PEC 0x00004000 /* SMBus 2.0 */ 91 #define I2C_FUNC_SMBUS_BLOCK_PROC_CALL_PEC 0x00004000 /* SMBus 2.0 */
101 #define I2C_FUNC_SMBUS_BLOCK_PROC_CALL 0x00008000 /* SMBus 2.0 */ 92 #define I2C_FUNC_SMBUS_BLOCK_PROC_CALL 0x00008000 /* SMBus 2.0 */
102 #define I2C_FUNC_SMBUS_QUICK 0x00010000 93 #define I2C_FUNC_SMBUS_QUICK 0x00010000
103 #define I2C_FUNC_SMBUS_READ_BYTE 0x00020000 94 #define I2C_FUNC_SMBUS_READ_BYTE 0x00020000
104 #define I2C_FUNC_SMBUS_WRITE_BYTE 0x00040000 95 #define I2C_FUNC_SMBUS_WRITE_BYTE 0x00040000
105 #define I2C_FUNC_SMBUS_READ_BYTE_DATA 0x00080000 96 #define I2C_FUNC_SMBUS_READ_BYTE_DATA 0x00080000
106 #define I2C_FUNC_SMBUS_WRITE_BYTE_DATA 0x00100000 97 #define I2C_FUNC_SMBUS_WRITE_BYTE_DATA 0x00100000
107 #define I2C_FUNC_SMBUS_READ_WORD_DATA 0x00200000 98 #define I2C_FUNC_SMBUS_READ_WORD_DATA 0x00200000
108 #define I2C_FUNC_SMBUS_WRITE_WORD_DATA 0x00400000 99 #define I2C_FUNC_SMBUS_WRITE_WORD_DATA 0x00400000
109 #define I2C_FUNC_SMBUS_PROC_CALL 0x00800000 100 #define I2C_FUNC_SMBUS_PROC_CALL 0x00800000
110 #define I2C_FUNC_SMBUS_READ_BLOCK_DATA 0x01000000 101 #define I2C_FUNC_SMBUS_READ_BLOCK_DATA 0x01000000
111 #define I2C_FUNC_SMBUS_WRITE_BLOCK_DATA 0x02000000 102 #define I2C_FUNC_SMBUS_WRITE_BLOCK_DATA 0x02000000
112 #define I2C_FUNC_SMBUS_READ_I2C_BLOCK 0x04000000 /* I2C-like block xfer */ 103 #define I2C_FUNC_SMBUS_READ_I2C_BLOCK 0x04000000 /* I2C-like block xfer */
113 #define I2C_FUNC_SMBUS_WRITE_I2C_BLOCK 0x08000000 /* w/ 1-byte reg. addr. */ 104 #define I2C_FUNC_SMBUS_WRITE_I2C_BLOCK 0x08000000 /* w/ 1-byte reg. addr. */
114 #define I2C_FUNC_SMBUS_READ_I2C_BLOCK_2 0x10000000 /* I2C-like block xfer */ 105 #define I2C_FUNC_SMBUS_READ_I2C_BLOCK_2 0x10000000 /* I2C-like block xfer */
115 #define I2C_FUNC_SMBUS_WRITE_I2C_BLOCK_2 0x20000000 /* w/ 2-byte reg. addr. */ 106 #define I2C_FUNC_SMBUS_WRITE_I2C_BLOCK_2 0x20000000 /* w/ 2-byte reg. addr. */
116 #define I2C_FUNC_SMBUS_READ_BLOCK_DATA_PEC 0x40000000 /* SMBus 2.0 */ 107 #define I2C_FUNC_SMBUS_READ_BLOCK_DATA_PEC 0x40000000 /* SMBus 2.0 */
117 #define I2C_FUNC_SMBUS_WRITE_BLOCK_DATA_PEC 0x80000000 /* SMBus 2.0 */ 108 #define I2C_FUNC_SMBUS_WRITE_BLOCK_DATA_PEC 0x80000000 /* SMBus 2.0 */
118   109  
119 #define I2C_FUNC_SMBUS_BYTE I2C_FUNC_SMBUS_READ_BYTE | \ 110 #define I2C_FUNC_SMBUS_BYTE I2C_FUNC_SMBUS_READ_BYTE | \
120 I2C_FUNC_SMBUS_WRITE_BYTE 111 I2C_FUNC_SMBUS_WRITE_BYTE
121 #define I2C_FUNC_SMBUS_BYTE_DATA I2C_FUNC_SMBUS_READ_BYTE_DATA | \ 112 #define I2C_FUNC_SMBUS_BYTE_DATA I2C_FUNC_SMBUS_READ_BYTE_DATA | \
122 I2C_FUNC_SMBUS_WRITE_BYTE_DATA 113 I2C_FUNC_SMBUS_WRITE_BYTE_DATA
123 #define I2C_FUNC_SMBUS_WORD_DATA I2C_FUNC_SMBUS_READ_WORD_DATA | \ 114 #define I2C_FUNC_SMBUS_WORD_DATA I2C_FUNC_SMBUS_READ_WORD_DATA | \
124 I2C_FUNC_SMBUS_WRITE_WORD_DATA 115 I2C_FUNC_SMBUS_WRITE_WORD_DATA
125 #define I2C_FUNC_SMBUS_BLOCK_DATA I2C_FUNC_SMBUS_READ_BLOCK_DATA | \ 116 #define I2C_FUNC_SMBUS_BLOCK_DATA I2C_FUNC_SMBUS_READ_BLOCK_DATA | \
126 I2C_FUNC_SMBUS_WRITE_BLOCK_DATA 117 I2C_FUNC_SMBUS_WRITE_BLOCK_DATA
127 #define I2C_FUNC_SMBUS_I2C_BLOCK I2C_FUNC_SMBUS_READ_I2C_BLOCK | \ 118 #define I2C_FUNC_SMBUS_I2C_BLOCK I2C_FUNC_SMBUS_READ_I2C_BLOCK | \
128 I2C_FUNC_SMBUS_WRITE_I2C_BLOCK 119 I2C_FUNC_SMBUS_WRITE_I2C_BLOCK
129   120  
130 #define I2C_FUNC_SMBUS_EMUL I2C_FUNC_SMBUS_QUICK | \ 121 #define I2C_FUNC_SMBUS_EMUL I2C_FUNC_SMBUS_QUICK | \
131 I2C_FUNC_SMBUS_BYTE | \ 122 I2C_FUNC_SMBUS_BYTE | \
132 I2C_FUNC_SMBUS_BYTE_DATA | \ 123 I2C_FUNC_SMBUS_BYTE_DATA | \
133 I2C_FUNC_SMBUS_WORD_DATA | \ 124 I2C_FUNC_SMBUS_WORD_DATA | \
134 I2C_FUNC_SMBUS_PROC_CALL | \ 125 I2C_FUNC_SMBUS_PROC_CALL | \
135 I2C_FUNC_SMBUS_WRITE_BLOCK_DATA | \ 126 I2C_FUNC_SMBUS_WRITE_BLOCK_DATA | \
136 I2C_FUNC_SMBUS_WRITE_BLOCK_DATA_PEC | \ 127 I2C_FUNC_SMBUS_WRITE_BLOCK_DATA_PEC | \
137 I2C_FUNC_SMBUS_I2C_BLOCK 128 I2C_FUNC_SMBUS_I2C_BLOCK
138   129  
139 /* the currently support capability is quite limited */ 130 /* the currently support capability is quite limited */
140 const unsigned long func PROGMEM = I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL; 131 const unsigned long func PROGMEM = I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
141   132  
142 #ifdef DEBUG 133 #ifdef DEBUG
143 #define DEBUGF(format, args...) printf_P(PSTR(format), ##args) 134 #define DEBUGF(format, args...) printf_P(PSTR(format), ##args)
144   135  
145 /* ------------------------------------------------------------------------- */ 136 /* ------------------------------------------------------------------------- */
146 static int uart_putchar(char c, FILE *stream) { 137 static int uart_putchar(char c, FILE *stream) {
147 if (c == '\n') 138 if (c == '\n')
148 uart_putchar('\r', stream); 139 uart_putchar('\r', stream);
149 loop_until_bit_is_set(UCSRA, UDRE); 140 loop_until_bit_is_set(UCSRA, UDRE);
150 UDR = c; 141 UDR = c;
151 return 0; 142 return 0;
152 } 143 }
153   144  
154 static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL, 145 static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL,
155 _FDEV_SETUP_WRITE); 146 _FDEV_SETUP_WRITE);
156 #else 147 #else
157 #define DEBUGF(format, args...) 148 #define DEBUGF(format, args...)
158 #endif 149 #endif
159   150  
160 /* ------------------------------------------------------------------------- */ 151 /* ------------------------------------------------------------------------- */
161 #define DEFAULT_DELAY 10 // default 10us (100khz) 152 #define DEFAULT_DELAY 10 // default 10us (100khz)
162 static unsigned short clock_delay = DEFAULT_DELAY; 153 static unsigned short clock_delay = DEFAULT_DELAY;
163 static unsigned short clock_delay2 = DEFAULT_DELAY/2; 154 static unsigned short clock_delay2 = DEFAULT_DELAY/2;
164   155  
165 static unsigned short expected; 156 static unsigned short expected;
166 static unsigned char saved_cmd; 157 static unsigned char saved_cmd;
167   158  
168 #if! defined (__AVR_ATtiny45__) 159 #if! defined (__AVR_ATtiny45__)
169 #define I2C_PORT PORTC 160 #define I2C_PORT PORTC
170 #define I2C_PIN PINC 161 #define I2C_PIN PINC
171 #define I2C_DDR DDRC 162 #define I2C_DDR DDRC
172 #define I2C_SDA _BV(4) 163 #define I2C_SDA _BV(4)
173 #define I2C_SCL _BV(5) 164 #define I2C_SCL _BV(5)
174 #else 165 #else
175 #define I2C_PORT PORTB 166 #define I2C_PORT PORTB
176 #define I2C_PIN PINB 167 #define I2C_PIN PINB
177 #define I2C_DDR DDRB 168 #define I2C_DDR DDRB
178 #define I2C_SDA _BV(1) 169 #define I2C_SDA _BV(1)
179 #define I2C_SCL _BV(5) 170 #define I2C_SCL _BV(5)
180 #endif 171 #endif
181   172  
182 static void i2c_io_set_sda(uchar hi) { 173 static void i2c_io_set_sda(uchar hi) {
183 if(hi) { 174 if(hi) {
184 I2C_DDR &= ~I2C_SDA; // high -> input 175 I2C_DDR &= ~I2C_SDA; // high -> input
185 I2C_PORT |= I2C_SDA; // with pullup 176 I2C_PORT |= I2C_SDA; // with pullup
186 } else { 177 } else {
187 I2C_DDR |= I2C_SDA; // low -> output 178 I2C_DDR |= I2C_SDA; // low -> output
188 I2C_PORT &= ~I2C_SDA; // drive low 179 I2C_PORT &= ~I2C_SDA; // drive low
189 } 180 }
190 } 181 }
191   182  
192 static uchar i2c_io_get_sda(void) { 183 static uchar i2c_io_get_sda(void) {
193 return(I2C_PIN & I2C_SDA); 184 return(I2C_PIN & I2C_SDA);
194 } 185 }
195   186  
196 static void i2c_io_set_scl(uchar hi) { 187 static void i2c_io_set_scl(uchar hi) {
197 #ifdef ENABLE_SCL_EXPAND 188 #ifdef ENABLE_SCL_EXPAND
198 _delay_loop_2(clock_delay2); 189 _delay_loop_2(clock_delay2);
199 if(hi) { 190 if(hi) {
200 I2C_DDR &= ~I2C_SCL; // port is input 191 I2C_DDR &= ~I2C_SCL; // port is input
201 I2C_PORT |= I2C_SCL; // enable pullup 192 I2C_PORT |= I2C_SCL; // enable pullup
202   193  
203 // wait while pin is pulled low by client 194 // wait while pin is pulled low by client
204 while(!(I2C_PIN & I2C_SCL)); 195 while(!(I2C_PIN & I2C_SCL));
205 } else { 196 } else {
206 I2C_DDR |= I2C_SCL; // port is output 197 I2C_DDR |= I2C_SCL; // port is output
207 I2C_PORT &= ~I2C_SCL; // drive it low 198 I2C_PORT &= ~I2C_SCL; // drive it low
208 } 199 }
209 _delay_loop_2(clock_delay); 200 _delay_loop_2(clock_delay);
210 #else 201 #else
211 _delay_loop_2(clock_delay2); 202 _delay_loop_2(clock_delay2);
212 if(hi) I2C_PORT |= I2C_SCL; // port is high 203 if(hi) I2C_PORT |= I2C_SCL; // port is high
213 else I2C_PORT &= ~I2C_SCL; // port is low 204 else I2C_PORT &= ~I2C_SCL; // port is low
214 _delay_loop_2(clock_delay); 205 _delay_loop_2(clock_delay);
215 #endif 206 #endif
216 } 207 }
217   208  
218 static void i2c_init(void) { 209 static void i2c_init(void) {
219 /* init the sda/scl pins */ 210 /* init the sda/scl pins */
220 I2C_DDR &= ~I2C_SDA; // port is input 211 I2C_DDR &= ~I2C_SDA; // port is input
221 I2C_PORT |= I2C_SDA; // enable pullup 212 I2C_PORT |= I2C_SDA; // enable pullup
222 #ifdef ENABLE_SCL_EXPAND 213 #ifdef ENABLE_SCL_EXPAND
223 I2C_DDR &= ~I2C_SCL; // port is input 214 I2C_DDR &= ~I2C_SCL; // port is input
224 I2C_PORT |= I2C_SCL; // enable pullup 215 I2C_PORT |= I2C_SCL; // enable pullup
225 #else 216 #else
226 I2C_DDR |= I2C_SCL; // port is output 217 I2C_DDR |= I2C_SCL; // port is output
227 #endif 218 #endif
228   219  
229 /* no bytes to be expected */ 220 /* no bytes to be expected */
230 expected = 0; 221 expected = 0;
231 } 222 }
232   223  
233 /* clock HI, delay, then LO */ 224 /* clock HI, delay, then LO */
234 static void i2c_scl_toggle(void) { 225 static void i2c_scl_toggle(void) {
235 i2c_io_set_scl(1); 226 i2c_io_set_scl(1);
236 i2c_io_set_scl(0); 227 i2c_io_set_scl(0);
237 } 228 }
238   229  
239 /* i2c start condition */ 230 /* i2c start condition */
240 static void i2c_start(void) { 231 static void i2c_start(void) {
241 i2c_io_set_sda(0); 232 i2c_io_set_sda(0);
242 i2c_io_set_scl(0); 233 i2c_io_set_scl(0);
243 } 234 }
244   235  
245 /* i2c repeated start condition */ 236 /* i2c repeated start condition */
246 static void i2c_repstart(void) 237 static void i2c_repstart(void)
247 { 238 {
248 /* scl, sda may not be high */ 239 /* scl, sda may not be high */
249 i2c_io_set_sda(1); 240 i2c_io_set_sda(1);
250 i2c_io_set_scl(1); 241 i2c_io_set_scl(1);
251 242
252 i2c_io_set_sda(0); 243 i2c_io_set_sda(0);
253 i2c_io_set_scl(0); 244 i2c_io_set_scl(0);
254 } 245 }
255   246  
256 /* i2c stop condition */ 247 /* i2c stop condition */
257 void i2c_stop(void) { 248 void i2c_stop(void) {
258 i2c_io_set_sda(0); 249 i2c_io_set_sda(0);
259 i2c_io_set_scl(1); 250 i2c_io_set_scl(1);
260 i2c_io_set_sda(1); 251 i2c_io_set_sda(1);
261 } 252 }
262   253  
263 uchar i2c_put_u08(uchar b) { 254 uchar i2c_put_u08(uchar b) {
264 char i; 255 char i;
265   256  
266 for (i=7;i>=0;i--) { 257 for (i=7;i>=0;i--) {
267 if ( b & (1<<i) ) i2c_io_set_sda(1); 258 if ( b & (1<<i) ) i2c_io_set_sda(1);
268 else i2c_io_set_sda(0); 259 else i2c_io_set_sda(0);
269 260
270 i2c_scl_toggle(); // clock HI, delay, then LO 261 i2c_scl_toggle(); // clock HI, delay, then LO
271 } 262 }
272 263
273 i2c_io_set_sda(1); // leave SDL HI 264 i2c_io_set_sda(1); // leave SDL HI
274 i2c_io_set_scl(1); // clock back up 265 i2c_io_set_scl(1); // clock back up
275   266  
276 b = i2c_io_get_sda(); // get the ACK bit 267 b = i2c_io_get_sda(); // get the ACK bit
277 i2c_io_set_scl(0); // not really ?? 268 i2c_io_set_scl(0); // not really ??
278   269  
279 return(b == 0); // return ACK value 270 return(b == 0); // return ACK value
280 } 271 }
281   272  
282 uchar i2c_get_u08(uchar last) { 273 uchar i2c_get_u08(uchar last) {
283 char i; 274 char i;
284 uchar c,b = 0; 275 uchar c,b = 0;
285   276  
286 i2c_io_set_sda(1); // make sure pullups are activated 277 i2c_io_set_sda(1); // make sure pullups are activated
287 i2c_io_set_scl(0); // clock LOW 278 i2c_io_set_scl(0); // clock LOW
288   279  
289 for(i=7;i>=0;i--) { 280 for(i=7;i>=0;i--) {
290 i2c_io_set_scl(1); // clock HI 281 i2c_io_set_scl(1); // clock HI
291 c = i2c_io_get_sda(); 282 c = i2c_io_get_sda();
292 b <<= 1; 283 b <<= 1;
293 if(c) b |= 1; 284 if(c) b |= 1;
294 i2c_io_set_scl(0); // clock LO 285 i2c_io_set_scl(0); // clock LO
295 } 286 }
296   287  
297 if(last) i2c_io_set_sda(1); // set NAK 288 if(last) i2c_io_set_sda(1); // set NAK
298 else i2c_io_set_sda(0); // set ACK 289 else i2c_io_set_sda(0); // set ACK
299   290  
300 i2c_scl_toggle(); // clock pulse 291 i2c_scl_toggle(); // clock pulse
301 i2c_io_set_sda(1); // leave with SDL HI 292 i2c_io_set_sda(1); // leave with SDL HI
302   293  
303 return b; // return received byte 294 return b; // return received byte
304 } 295 }
305   296  
306 #ifdef DEBUG 297 #ifdef DEBUG
307 void i2c_scan(void) { 298 void i2c_scan(void) {
308 uchar i = 0; 299 uchar i = 0;
309   300  
310 for(i=0;i<127;i++) { 301 for(i=0;i<127;i++) {
311 i2c_start(); // do start transition 302 i2c_start(); // do start transition
312 if(i2c_put_u08(i << 1)) // send DEVICE address 303 if(i2c_put_u08(i << 1)) // send DEVICE address
313 DEBUGF("I2C device at address 0x%x\n", i); 304 DEBUGF("I2C device at address 0x%x\n", i);
314   305  
315 i2c_stop(); 306 i2c_stop();
316 } 307 }
317 } 308 }
318 #endif 309 #endif
319   310  
320 /* ------------------------------------------------------------------------- */ 311 /* ------------------------------------------------------------------------- */
321   312  
322 struct i2c_cmd { 313 struct i2c_cmd {
323 unsigned char type; 314 unsigned char type;
324 unsigned char cmd; 315 unsigned char cmd;
325 unsigned short flags; 316 unsigned short flags;
326 unsigned short addr; 317 unsigned short addr;
327 unsigned short len; 318 unsigned short len;
328 }; 319 };
329   320  
330 #define STATUS_IDLE 0 321 #define STATUS_IDLE 0
331 #define STATUS_ADDRESS_ACK 1 322 #define STATUS_ADDRESS_ACK 1
332 #define STATUS_ADDRESS_NAK 2 323 #define STATUS_ADDRESS_NAK 2
333   324  
334 static uchar status = STATUS_IDLE; 325 static uchar status = STATUS_IDLE;
335   326  
336 static uchar i2c_do(struct i2c_cmd *cmd) { 327 static uchar i2c_do(struct i2c_cmd *cmd) {
337 uchar addr; 328 uchar addr;
338   329  
339 DEBUGF("i2c %s at 0x%02x, len = %d\n", 330 DEBUGF("i2c %s at 0x%02x, len = %d\n",
340 (cmd->flags&I2C_M_RD)?"rd":"wr", cmd->addr, cmd->len); 331 (cmd->flags&I2C_M_RD)?"rd":"wr", cmd->addr, cmd->len);
341   332  
342 /* normal 7bit address */ 333 /* normal 7bit address */
343 addr = ( cmd->addr << 1 ); 334 addr = ( cmd->addr << 1 );
344 if (cmd->flags & I2C_M_RD ) 335 if (cmd->flags & I2C_M_RD )
345 addr |= 1; 336 addr |= 1;
346   337  
347 if(cmd->cmd & CMD_I2C_BEGIN) 338 if(cmd->cmd & CMD_I2C_BEGIN)
348 i2c_start(); 339 i2c_start();
349 else 340 else
350 i2c_repstart(); 341 i2c_repstart();
351   342  
352 // send DEVICE address 343 // send DEVICE address
353 if(!i2c_put_u08(addr)) { 344 if(!i2c_put_u08(addr)) {
354 DEBUGF("I2C read: address error @ %x\n", addr); 345 DEBUGF("I2C read: address error @ %x\n", addr);
355   346  
356 status = STATUS_ADDRESS_NAK; 347 status = STATUS_ADDRESS_NAK;
357 expected = 0; 348 expected = 0;
358 i2c_stop(); 349 i2c_stop();
359 } else { 350 } else {
360 status = STATUS_ADDRESS_ACK; 351 status = STATUS_ADDRESS_ACK;
361 expected = cmd->len; 352 expected = cmd->len;
362 saved_cmd = cmd->cmd; 353 saved_cmd = cmd->cmd;
363   354  
364 /* check if transfer is already done (or failed) */ 355 /* check if transfer is already done (or failed) */
365 if((cmd->cmd & CMD_I2C_END) && !expected) 356 if((cmd->cmd & CMD_I2C_END) && !expected)
366 i2c_stop(); 357 i2c_stop();
367 } 358 }
368   359  
369 /* more data to be expected? */ 360 /* more data to be expected? */
370 #ifndef USBTINY 361 #ifndef USBTINY
371 return(cmd->len?0xff:0x00); 362 return(cmd->len?0xff:0x00);
372 #else 363 #else
373 return(((cmd->flags & I2C_M_RD) && cmd->len)?0xff:0x00); 364 return(((cmd->flags & I2C_M_RD) && cmd->len)?0xff:0x00);
374 #endif 365 #endif
375 } 366 }
376   367  
377 #ifndef USBTINY 368 #ifndef USBTINY
378 uchar usbFunctionSetup(uchar data[8]) { 369 uchar usbFunctionSetup(uchar data[8]) {
379 static uchar replyBuf[4]; 370 static uchar replyBuf[4];
380 usbMsgPtr = replyBuf; 371 usbMsgPtr = replyBuf;
381 #else 372 #else
382 extern byte_t usb_setup ( byte_t data[8] ) 373 extern byte_t usb_setup ( byte_t data[8] )
383 { 374 {
384 byte_t *replyBuf = data; 375 byte_t *replyBuf = data;
385 #endif 376 #endif
386   377  
387 DEBUGF("Setup %x %x %x %x\n", data[0], data[1], data[2], data[3]); 378 DEBUGF("Setup %x %x %x %x\n", data[0], data[1], data[2], data[3]);
388   379  
389 switch(data[1]) { 380 switch(data[1]) {
390   381  
391 case CMD_ECHO: // echo (for transfer reliability testing) 382 case CMD_ECHO: // echo (for transfer reliability testing)
392 replyBuf[0] = data[2]; 383 replyBuf[0] = data[2];
393 replyBuf[1] = data[3]; 384 replyBuf[1] = data[3];
394 return 2; 385 return 2;
395 break; 386 break;
396   387  
397 case CMD_GET_FUNC: 388 case CMD_GET_FUNC:
398 memcpy_P(replyBuf, &func, sizeof(func)); 389 memcpy_P(replyBuf, &func, sizeof(func));
399 return sizeof(func); 390 return sizeof(func);
400 break; 391 break;
401   392  
402 case CMD_SET_DELAY: 393 case CMD_SET_DELAY:
403 /* The delay function used delays 4 system ticks per cycle. */ 394 /* The delay function used delays 4 system ticks per cycle. */
404 /* This gives 1/3us at 12Mhz per cycle. The delay function is */ 395 /* This gives 1/3us at 12Mhz per cycle. The delay function is */
405 /* called twice per clock edge and thus four times per full cycle. */ 396 /* called twice per clock edge and thus four times per full cycle. */
406 /* Thus it is called one time per edge with the full delay */ 397 /* Thus it is called one time per edge with the full delay */
407 /* value and one time with the half one. Resulting in */ 398 /* value and one time with the half one. Resulting in */
408 /* 2 * n * 1/3 + 2 * 1/2 n * 1/3 = n us. */ 399 /* 2 * n * 1/3 + 2 * 1/2 n * 1/3 = n us. */
409 clock_delay = *(unsigned short*)(data+2); 400 clock_delay = *(unsigned short*)(data+2);
410 if(!clock_delay) clock_delay = 1; 401 if(!clock_delay) clock_delay = 1;
411 clock_delay2 = clock_delay/2; 402 clock_delay2 = clock_delay/2;
412 if(!clock_delay2) clock_delay2 = 1; 403 if(!clock_delay2) clock_delay2 = 1;
413   404  
414 DEBUGF("request for delay %dus\n", clock_delay); 405 DEBUGF("request for delay %dus\n", clock_delay);
415 break; 406 break;
416   407  
417 case CMD_I2C_IO: 408 case CMD_I2C_IO:
418 case CMD_I2C_IO + CMD_I2C_BEGIN: 409 case CMD_I2C_IO + CMD_I2C_BEGIN:
419 case CMD_I2C_IO + CMD_I2C_END: 410 case CMD_I2C_IO + CMD_I2C_END:
420 case CMD_I2C_IO + CMD_I2C_BEGIN + CMD_I2C_END: 411 case CMD_I2C_IO + CMD_I2C_BEGIN + CMD_I2C_END:
421 // these are only allowed as class transfers 412 // these are only allowed as class transfers
422   413  
423 return i2c_do((struct i2c_cmd*)data); 414 return i2c_do((struct i2c_cmd*)data);
424 break; 415 break;
425   416  
426 case CMD_GET_STATUS: 417 case CMD_GET_STATUS:
427 replyBuf[0] = status; 418 replyBuf[0] = status;
428 return 1; 419 return 1;
429 break; 420 break;
430   421  
431 default: 422 default:
432 // must not happen ... 423 // must not happen ...
433 break; 424 break;
434 } 425 }
435   426  
436 return 0; // reply len 427 return 0; // reply len
437 } 428 }
438   429  
439   430  
440 /*---------------------------------------------------------------------------*/ 431 /*---------------------------------------------------------------------------*/
441 /* usbFunctionRead */ 432 /* usbFunctionRead */
442 /*---------------------------------------------------------------------------*/ 433 /*---------------------------------------------------------------------------*/
443   434  
444 #ifndef USBTINY 435 #ifndef USBTINY
445 uchar usbFunctionRead( uchar *data, uchar len ) 436 uchar usbFunctionRead( uchar *data, uchar len )
446 #else 437 #else
447 extern byte_t usb_in ( byte_t* data, byte_t len ) 438 extern byte_t usb_in ( byte_t* data, byte_t len )
448 #endif 439 #endif
449 { 440 {
450 uchar i; 441 uchar i;
451   442  
452 DEBUGF("read %d bytes, %d exp\n", len, expected); 443 DEBUGF("read %d bytes, %d exp\n", len, expected);
453   444  
454 if(status == STATUS_ADDRESS_ACK) { 445 if(status == STATUS_ADDRESS_ACK) {
455 if(len > expected) { 446 if(len > expected) {
456 DEBUGF("exceeds!\n"); 447 DEBUGF("exceeds!\n");
457 len = expected; 448 len = expected;
458 } 449 }
459   450  
460 // consume bytes 451 // consume bytes
461 for(i=0;i<len;i++) { 452 for(i=0;i<len;i++) {
462 expected--; 453 expected--;
463 *data = i2c_get_u08(expected == 0); 454 *data = i2c_get_u08(expected == 0);
464 DEBUGF("data = %x\n", *data); 455 DEBUGF("data = %x\n", *data);
465 data++; 456 data++;
466 } 457 }
467   458  
468 // end transfer on last byte 459 // end transfer on last byte
469 if((saved_cmd & CMD_I2C_END) && !expected) 460 if((saved_cmd & CMD_I2C_END) && !expected)
470 i2c_stop(); 461 i2c_stop();
471   462  
472 } else { 463 } else {
473 DEBUGF("not in ack state\n"); 464 DEBUGF("not in ack state\n");
474 memset(data, 0, len); 465 memset(data, 0, len);
475 } 466 }
476 return len; 467 return len;
477 } 468 }
478   469  
479 /*---------------------------------------------------------------------------*/ 470 /*---------------------------------------------------------------------------*/
480 /* usbFunctionWrite */ 471 /* usbFunctionWrite */
481 /*---------------------------------------------------------------------------*/ 472 /*---------------------------------------------------------------------------*/
482   473  
483 #ifndef USBTINY 474 #ifndef USBTINY
484 uchar usbFunctionWrite( uchar *data, uchar len ) 475 uchar usbFunctionWrite( uchar *data, uchar len )
485 #else 476 #else
486 extern void usb_out ( byte_t* data, byte_t len ) 477 extern void usb_out ( byte_t* data, byte_t len )
487 #endif 478 #endif
488 { 479 {
489 uchar i, err=0; 480 uchar i, err=0;
490   481  
491 DEBUGF("write %d bytes, %d exp\n", len, expected); 482 DEBUGF("write %d bytes, %d exp\n", len, expected);
492   483  
493 if(status == STATUS_ADDRESS_ACK) { 484 if(status == STATUS_ADDRESS_ACK) {
494 if(len > expected) { 485 if(len > expected) {
495 DEBUGF("exceeds!\n"); 486 DEBUGF("exceeds!\n");
496 len = expected; 487 len = expected;
497 } 488 }
498   489  
499 // consume bytes 490 // consume bytes
500 for(i=0;i<len;i++) { 491 for(i=0;i<len;i++) {
501 expected--; 492 expected--;
502 DEBUGF("data = %x\n", *data); 493 DEBUGF("data = %x\n", *data);
503 if(!i2c_put_u08(*data++)) 494 if(!i2c_put_u08(*data++))
504 err = 1; 495 err = 1;
505 } 496 }
506   497  
507 // end transfer on last byte 498 // end transfer on last byte
508 if((saved_cmd & CMD_I2C_END) && !expected) 499 if((saved_cmd & CMD_I2C_END) && !expected)
509 i2c_stop(); 500 i2c_stop();
510   501  
511 if(err) { 502 if(err) {
512 DEBUGF("write failed\n"); 503 DEBUGF("write failed\n");
513 //TODO: set status 504 //TODO: set status
514 } 505 }
515   506  
516 } else { 507 } else {
517 DEBUGF("not in ack state\n"); 508 DEBUGF("not in ack state\n");
518 memset(data, 0, len); 509 memset(data, 0, len);
519 } 510 }
520   511  
521 #ifndef USBTINY 512 #ifndef USBTINY
522 return len; 513 return len;
523 #endif 514 #endif
524 } 515 }
525   516  
526   517  
527 /* ------------------------------------------------------------------------- */ 518 /* ------------------------------------------------------------------------- */
528   519  
529 int main(void) { 520 int main(void) {
530 wdt_enable(WDTO_1S); 521 wdt_enable(WDTO_1S);
531   522  
532 #if DEBUG_LEVEL > 0 523 #if DEBUG_LEVEL > 0
533 /* let debug routines init the uart if they want to */ 524 /* let debug routines init the uart if they want to */
534 odDebugInit(); 525 odDebugInit();
535 #else 526 #else
536 #ifdef DEBUG 527 #ifdef DEBUG
537 /* quick'n dirty uart init */ 528 /* quick'n dirty uart init */
538 UCSRB |= _BV(TXEN); 529 UCSRB |= _BV(TXEN);
539 UBRRL = F_CPU / (19200 * 16L) - 1; 530 UBRRL = F_CPU / (19200 * 16L) - 1;
540 #endif 531 #endif
541 #endif 532 #endif
542   533  
543 #ifdef DEBUG 534 #ifdef DEBUG
544 stdout = &mystdout; 535 stdout = &mystdout;
545 #endif 536 #endif
546   537  
547 DEBUGF("i2c-tiny-usb - (c) 2006 by Till Harbaum\n"); 538 DEBUGF("i2c-tiny-usb - (c) 2006 by Till Harbaum\n");
548   539  
549 i2c_init(); 540 i2c_init();
550   541  
551 #ifdef DEBUG 542 #ifdef DEBUG
552 i2c_scan(); 543 i2c_scan();
553 #endif 544 #endif
554   545  
555 /* clear usb ports */ 546 /* clear usb ports */
556 USB_CFG_IOPORT &= (uchar)~((1<<USB_CFG_DMINUS_BIT)|(1<<USB_CFG_DPLUS_BIT)); 547 PORT(CONFIG_USB_PORT) &= (uchar)~((1<<CONFIG_USB_DMINUS)|(1<<CONFIG_USB_DPLUS));
557   548  
558 /* make usb data lines outputs */ 549 /* make usb data lines outputs */
559 USBDDR |= ((1<<USB_CFG_DMINUS_BIT)|(1<<USB_CFG_DPLUS_BIT)); 550 DDR(CONFIG_USB_PORT) |= ((1<<CONFIG_USB_DMINUS)|(1<<CONFIG_USB_DPLUS));
560   551  
561 /* USB Reset by device only required on Watchdog Reset */ 552 /* USB Reset by device only required on Watchdog Reset */
562 _delay_loop_2(40000); // 10ms 553 _delay_loop_2(40000); // 10ms
563   554  
564 /* make usb data lines inputs */ 555 /* make usb data lines inputs */
565 USBDDR &= ~((1<<USB_CFG_DMINUS_BIT)|(1<<USB_CFG_DPLUS_BIT)); 556 DDR(CONFIG_USB_PORT) &= ~((1<<CONFIG_USB_DMINUS)|(1<<CONFIG_USB_DPLUS));
566   557  
567 usbInit(); 558 usbInit();
568   559  
569 sei(); 560 sei();
570 for(;;) { /* main event loop */ 561 for(;;) { /* main event loop */
571 wdt_reset(); 562 wdt_reset();
572 usbPoll(); 563 usbPoll();
573 } 564 }
574   565  
575 return 0; 566 return 0;
576 } 567 }
577   568  
578 /* ------------------------------------------------------------------------- */ 569 /* ------------------------------------------------------------------------- */