vt100: include pgmspace.h as we use PROGMEM macro
[aversive.git] / modules / comm / i2c / i2c.c
1 /*
2  *  Copyright Droids Corporation (2007)
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  *
18  *  Revision : $Id: i2c.c,v 1.1.2.12 2009-04-24 19:26:54 zer0 Exp $
19  *
20  */
21
22 /*
23  * Author : Olivier MATZ zer0@droids-corp.org
24  *
25  * Thanks to Tof for the old i2c module and to Serpilliere for
26  * testing.
27  */
28
29 #include <stdlib.h>
30 #include <string.h>
31 #include <util/twi.h>
32
33 #include <autoconf.h>
34 #include <aversive/errno.h>
35 #include <i2c.h>
36
37 #if I2C_SEND_BUFFER_SIZE < 1
38 #error "I2C_SEND_BUFFER_SIZE must be at least 1"
39 #endif
40
41 #if I2C_RECV_BUFFER_SIZE < 1
42 #error "I2C_RECV_BUFFER_SIZE must be at least 1"
43 #endif
44
45 /** recv event, called when we receive a frame
46  *  params are : data buffer and size */
47 static void (*g_recv_event)(uint8_t *, int8_t) = NULL;
48
49 /** recv event, called when we receive a byte
50  *  params are : hwstatus, index of byte in frame, byte value */
51 static void (*g_recv_byte_event)(uint8_t, uint8_t, uint8_t) = NULL;
52
53 /** send event, called when transmit is complete
54  * param is error code : 0 if success */
55 static void (*g_send_event)(int8_t) = NULL;
56
57 static volatile i2c_mode_t g_mode = I2C_MODE_UNINIT;
58 static volatile uint8_t g_status = I2C_STATUS_READY;
59
60 static volatile uint8_t g_ctrl = 0; /* ctrl flags */
61 static volatile uint8_t g_sync_res = 0; /* result of sync send */
62 static uint8_t g_send_buf[I2C_SEND_BUFFER_SIZE];
63 static uint8_t g_recv_buf[I2C_RECV_BUFFER_SIZE];
64 static volatile uint8_t g_dest = 0; /* destination slave in master mode */
65
66
67 static volatile uint8_t g_send_nbytes = 0; /* number of transmitted bytes */
68 static volatile uint8_t g_send_size = 0; /* size of buffer to be transmitted */
69 static volatile uint8_t g_recv_nbytes = 0; /* number of received bytes */
70 static volatile uint8_t g_recv_size = 0; /* size of buffer to be received */
71
72 #if I2C_DEBUG == 1
73 #include <stdio.h>
74 #include <aversive/pgmspace.h>
75 static volatile uint8_t g_prev_twstatus = 0;
76 static volatile uint8_t g_intr_cpt = 0;
77 static volatile uint8_t g_prev_status = 0;
78 static volatile uint8_t g_command = 0;
79 #endif
80
81 /**
82  * mode is I2C_MODE_UNINIT, I2C_MODE_MASTER, I2C_MODE_MULTIMASTER or
83  * I2C_MODE_SLAVE. Parameter add is the address in slave mode, it is
84  * composed from:
85  *   b7  : true if the uC can be addressed with GENCALL
86  *   b0-6: slave address
87  */
88 void
89 i2c_init(i2c_mode_t mode, uint8_t add)
90 {
91         uint8_t flags;
92
93         IRQ_LOCK(flags);
94
95         if (mode == I2C_MODE_UNINIT) {
96                 /* disable all */
97                 TWCR = 0;
98                 IRQ_UNLOCK(flags);
99                 return;
100         }
101 #ifdef CONFIG_MODULE_I2C_MASTER
102         else if (mode == I2C_MODE_MASTER) {
103                 /* enable, enable int */
104                 TWCR = (1<<TWEN) | (1<<TWIE) ;
105         }
106 #endif
107         else {
108                 /* enable, enable int, answer to own adress */
109                 TWCR = (1<<TWEN) | (1<<TWIE) | (1<<TWEA) ;
110         }
111
112         TWBR = I2C_BITRATE;
113
114         /* prescaler */
115         if (I2C_PRESCALER & 1)
116                 sbi(TWSR, TWPS0);
117         if (I2C_PRESCALER & 2)
118                 sbi(TWSR, TWPS1);
119
120         /* change for TWAR format */
121         TWAR = add << 1 ;
122
123         /* general call */
124         if (add & 0x80)
125                 sbi(TWAR, TWGCE);
126
127         /* init vars */
128         g_mode = mode;
129         g_status = I2C_STATUS_READY;
130         g_dest = 0;
131         g_ctrl = 0;
132         g_recv_nbytes = 0;
133         g_recv_size = 0;
134         g_send_nbytes = 0;
135         g_send_size = 0;
136
137         IRQ_UNLOCK(flags);
138 }
139
140
141 /**
142  * Register a function that is called when a buffer is received. The
143  * user application is always notified when data frame is received.
144  * Arguments of the callback are:
145  *   - (recv_buf, n>0) if transmission succedded. The first parameter
146  *                     contains the address of the reception buffer and
147  *                     the second contains the number of received bytes.
148  *   - (NULL, err<0)   if the transmission failed (slave not answering
149  *                     or arbiteration lost). The first parameter is
150  *                     NULL and the second contains the error code.
151  */
152 void
153 i2c_register_recv_event(void (*event)(uint8_t *, int8_t))
154 {
155         uint8_t flags;
156         IRQ_LOCK(flags);
157         g_recv_event = event ;
158         IRQ_UNLOCK(flags);
159 }
160
161 /**
162  * Register a function that is called when a byte is received.
163  * Arguments of the callback are: (hwstatus, numbyte, byte).  The user
164  * app can modify the g_recv_size value, which is the number of bytes
165  * to be received in the frame: this can be done by calling
166  * i2c_set_recv_size().
167  */
168 void
169 i2c_register_recv_byte_event(void (*event)(uint8_t, uint8_t, uint8_t))
170 {
171         uint8_t flags;
172         IRQ_LOCK(flags);
173         g_recv_byte_event = event ;
174         IRQ_UNLOCK(flags);
175 }
176
177 /**
178  * register a function that is called when a buffer is sent (or an
179  * error occured while sending) on the i2c bus. The event function is
180  * always called by software if the i2c_send() function returned 0.
181  * The parameter of the event function is the error code:
182  *   -  <0 if 0 byte has been transmitted (arbiteration lost)
183  *   -  Else, the number of transmitted bytes is given, including the
184  *      one that was not acked.
185  */
186 void
187 i2c_register_send_event(void (*event)(int8_t))
188 {
189         uint8_t flags;
190         IRQ_LOCK(flags);
191         g_send_event = event ;
192         IRQ_UNLOCK(flags);
193 }
194
195 /**
196  * Send a buffer. Return 0 if xmit starts correctly.
197  * On error, return < 0.
198  * - If mode is slave, dest_add should be I2C_ADD_MASTER, and transmission
199  *   starts when the master transmits a clk.
200  * - If mode is master and if dest_add != I2C_ADD_MASTER, it will transmit
201  *   a START condition if bus is available (the uc will act as a
202  *   master)
203  * - If mode is master and if dest_add == I2C_ADD_MASTER, the uC will
204  *   act as a slave, and data will be sent when the uC will be
205  *   addressed.
206  * The transmission will be processed with these params until a
207  * i2c_flush() is called.
208  * The 'ctrl' parameter is composed by the flags I2C_CTRL_SYNC and
209  * I2C_CTRL_DONT_RELEASE_BUS
210  */
211 int8_t
212 i2c_send(uint8_t dest_add, uint8_t *buf, uint8_t size, uint8_t ctrl)
213 {
214         uint8_t flags;
215
216         IRQ_LOCK(flags);
217         if (g_mode == I2C_MODE_UNINIT) {
218                 IRQ_UNLOCK(flags);
219                 return -ENXIO;
220         }
221
222         if (g_status & (I2C_STATUS_MASTER_XMIT |
223                         I2C_STATUS_MASTER_RECV |
224                         I2C_STATUS_SLAVE_XMIT_WAIT |
225                         I2C_STATUS_SLAVE_XMIT)) {
226                 IRQ_UNLOCK(flags);
227                 return -EBUSY;
228         }
229
230         if (size > I2C_SEND_BUFFER_SIZE) { /* XXX is size==0 ok ? */
231                 IRQ_UNLOCK(flags);
232                 return -EINVAL;
233         }
234
235         /* bad dest_address */
236         if (g_mode == I2C_MODE_SLAVE) {
237                 if (dest_add != I2C_ADD_MASTER) {
238                         IRQ_UNLOCK(flags);
239                         return -EINVAL;
240                 }
241         }
242         else {
243                 if (dest_add >= I2C_ADD_MASTER) {
244                         IRQ_UNLOCK(flags);
245                         return -EINVAL;
246                 }
247         }
248
249         /* if g_send_buf == buf, it is a resend, so don't update
250          * parameters */
251         if ( g_send_buf != buf ) {
252                 g_dest = dest_add;
253                 g_send_size = size;
254                 g_ctrl = ctrl;
255                 memcpy(g_send_buf, buf, size);
256         }
257
258         /* if destination is not the master, IT MEANS THAT WE ARE THE
259          * MASTER, so we should initiate the transmission */
260         if (dest_add != I2C_ADD_MASTER) {
261                 g_status |= I2C_STATUS_MASTER_XMIT;
262                 TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE) | (1<<TWSTA);
263         }
264         else {
265                 /* else we are a slave */
266                 g_status |= I2C_STATUS_SLAVE_XMIT_WAIT;
267         }
268
269         IRQ_UNLOCK(flags);
270
271         /* If it is sync mode, wait op_finished. Here we will reset
272          * the status flag to ready */
273         if (ctrl & I2C_CTRL_SYNC) {
274                 while ( 1 ) {
275                         IRQ_LOCK(flags);
276                         if (g_status & I2C_STATUS_OP_FINISHED) {
277                                 g_status &= ~(I2C_STATUS_MASTER_XMIT |
278                                               I2C_STATUS_MASTER_RECV |
279                                               I2C_STATUS_SLAVE_XMIT |
280                                               I2C_STATUS_SLAVE_RECV |
281                                               I2C_STATUS_OP_FINISHED);
282                                 break;
283                         }
284                         IRQ_UNLOCK(flags);
285                 }
286                 IRQ_UNLOCK(flags);
287                 if (g_sync_res == size)
288                         return 0;
289                 return g_sync_res;
290         }
291
292         return -ESUCCESS;
293 }
294
295
296 /**
297  * Resend the same buffer. This call is equivalent to i2c_send() with
298  * the same parameters as the last call. It safe to call it from the
299  * send_event, but else the send buffer may have been overwritten.
300  */
301 int8_t
302 i2c_resend(void)
303 {
304         return i2c_send(g_dest, g_send_buf, g_send_size, g_ctrl);
305 }
306
307 /**
308  * same but for recv
309  */
310 int8_t
311 i2c_rerecv(void)
312 {
313         return i2c_recv(g_dest, g_recv_size, g_ctrl);
314 }
315
316 /**
317  * release the bus
318  */
319 void
320 i2c_release_bus(void)
321 {
322         TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE) | (1<<TWSTO);
323 }
324
325 /**
326  * recover from error state
327  */
328 void
329 i2c_reset(void)
330 {
331         uint8_t flags;
332
333         IRQ_LOCK(flags);
334         TWCR = 0;
335         g_status = I2C_STATUS_READY;
336 #ifdef CONFIG_MODULE_I2C_MASTER
337         if (g_mode == I2C_MODE_MASTER)
338                 TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE);
339         else
340 #endif
341                 TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE) |
342                         (1<<TWSTO) | (1<<TWEA);
343         IRQ_UNLOCK(flags);
344 }
345
346 /**
347  * In slave mode, it returns error and is useless (all data is
348  * received trough the callback).  In master mode, if dest_add is
349  * between 0 and 127, it will start to read the addressed slave. The
350  * size of the buffer to read must be specified. Return 0 on success.
351  */
352 int8_t i2c_recv(uint8_t dest_add, uint8_t size, uint8_t ctrl)
353 {
354 #ifndef CONFIG_MODULE_I2C_MASTER
355         (void)dest_add;
356         (void)size;
357         (void)ctrl;
358         return -EINVAL;
359 #else
360         uint8_t flags;
361
362         IRQ_LOCK(flags);
363         if (g_mode == I2C_MODE_UNINIT) {
364                 IRQ_UNLOCK(flags);
365                 return -ENXIO;
366         }
367
368         if (g_status != I2C_STATUS_READY) {
369                 IRQ_UNLOCK(flags);
370                 return -EBUSY;
371         }
372
373         if (size > I2C_SEND_BUFFER_SIZE) { /* XXX is size=0 ok ? */
374                 IRQ_UNLOCK(flags);
375                 return -EINVAL;
376         }
377
378         if (g_mode == I2C_MODE_SLAVE || dest_add >= I2C_ADD_MASTER) {
379                 IRQ_UNLOCK(flags);
380                 return -EINVAL;
381         }
382
383         g_ctrl = ctrl;
384         g_recv_size = size;
385         g_status |= I2C_STATUS_MASTER_RECV;
386         g_dest = dest_add ;
387         TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE) | (1<<TWSTA);
388
389         IRQ_UNLOCK(flags);
390
391         /* If it is sync mode, wait op_finished. Here we will reset
392          * the status flag to ready */
393         if (ctrl & I2C_CTRL_SYNC) {
394                 while ( 1 ) {
395                         IRQ_LOCK(flags);
396                         if (g_status & I2C_STATUS_OP_FINISHED) {
397                                 g_status &= ~(I2C_STATUS_MASTER_XMIT |
398                                               I2C_STATUS_MASTER_RECV |
399                                               I2C_STATUS_SLAVE_XMIT |
400                                               I2C_STATUS_SLAVE_RECV |
401                                               I2C_STATUS_OP_FINISHED);
402                                 break;
403                         }
404                         IRQ_UNLOCK(flags);
405                 }
406                 IRQ_UNLOCK(flags);
407                 if (g_sync_res == size)
408                         return 0;
409                 return g_sync_res;
410         }
411
412         return -ESUCCESS;
413 #endif
414 }
415
416
417 /**
418  * Try to flush the current operation, before it is started. The
419  * i2c module is then tagged as ready. If it returns 0, the flush was
420  * a success, and i2c_send() can be called. Else, it means that
421  * a transmission was running.
422  */
423 int8_t i2c_flush(void)
424 {
425         uint8_t flags;
426         IRQ_LOCK(flags);
427         if ( ! (g_status & I2C_STATUS_SLAVE_XMIT_WAIT) ) {
428                 IRQ_UNLOCK(flags);
429                 return -EBUSY;
430         }
431
432         g_status &= ~(I2C_STATUS_SLAVE_XMIT_WAIT);
433         IRQ_UNLOCK(flags);
434
435         return -ESUCCESS;
436 }
437
438
439 /**
440  * In MASTER RECEIVER mode, it is possible that the user application
441  * does not know the size of the buffer. You can adjust this size
442  * during transmission (generally the size can be specified at the
443  * beginning of received data, so the user app can be notified thanks
444  * to recv_byte_event(). Note that i2c_set_recv_size() function has to
445  * be used with careful, making sure you understand i2c protocol and
446  * this code. Return 0 on success. Note than in SLAVE RECEIVER mode,
447  * you don't have to use this function, because the master can end the
448  * transmission by sending a stop condition on the bus.
449  */
450 uint8_t i2c_set_recv_size(uint8_t size)
451 {
452         uint8_t flags;
453
454         IRQ_LOCK(flags);
455
456         /* check that we are in reveiver mode */
457         if (! (g_status & I2C_STATUS_MASTER_RECV)) {
458                 IRQ_UNLOCK(flags);
459                 return -EBUSY;
460         }
461
462         /* check that specified size is not greater than
463          * I2C_SEND_BUFFER_SIZE. But it must be greater than current
464          * number of received bytes. */
465         /* XXX ? +1 ? */
466         if (size > I2C_SEND_BUFFER_SIZE || size <= g_recv_nbytes) {
467                 IRQ_UNLOCK(flags);
468                 return -EINVAL;
469         }
470
471         g_recv_size = size;
472
473         IRQ_UNLOCK(flags);
474         return -ESUCCESS;
475 }
476
477
478 /**
479  * return the current mode of the i2c module.
480  */
481 i2c_mode_t i2c_mode(void)
482 {
483         return g_mode;
484 }
485
486 /**
487  * return the status of the i2c module.
488  */
489 uint8_t i2c_status(void)
490 {
491         return g_status;
492 }
493
494 /**
495  * Copy the received buffer in the buffer given as parameter. Return
496  * number of copied bytes or < 0 on error.
497  */
498 uint8_t i2c_get_recv_buffer(uint8_t *buf, uint8_t size)
499 {
500         uint8_t flags;
501
502         IRQ_LOCK(flags);
503         /* check that reception is finished */
504         if ( g_status & (I2C_STATUS_MASTER_RECV |
505                           I2C_STATUS_SLAVE_RECV) ) {
506                 IRQ_UNLOCK(flags);
507                 return -EBUSY;
508         }
509
510         if (size > g_recv_nbytes)
511                 size = g_recv_nbytes;
512         memcpy(buf, g_recv_buf, size);
513
514         IRQ_UNLOCK(flags);
515
516         return size;
517 }
518
519 #if I2C_DEBUG == 1
520 void i2c_debug(void)
521 {
522         printf_P(PSTR("mode=0x%x\r\n"), g_mode);
523         printf_P(PSTR("status=0x%x\r\n"), g_status);
524         printf_P(PSTR("ctrl=0x%x\r\n"), g_ctrl);
525         printf_P(PSTR("dst=%d\r\n"), g_dest);
526         printf_P(PSTR("send_nbytes=%d, send_size=%d\r\n"), g_send_nbytes, g_send_size);
527         printf_P(PSTR("recv_nbytes=%d, recv_size=%d\r\n"), g_recv_nbytes, g_recv_size);
528         printf_P(PSTR("prev_twstatus=0x%x\r\n"), g_prev_twstatus);
529         printf_P(PSTR("intr_cpt=%d\r\n"), g_intr_cpt);
530         printf_P(PSTR("prev_status=0x%x\r\n"), g_prev_status);
531         printf_P(PSTR("prev_command=0x%x\r\n"), g_command);
532 }
533 #endif
534
535 /** interrupt ********************************************************/
536
537 /**
538  * Interrupt routing for I2C. Refer to datasheets for more
539  * informations.
540  */
541 #ifndef TWI_vect
542 #if defined(SIG_2WIRE_SERIAL)
543 #define TWI_vect SIG_2WIRE_SERIAL
544 #endif
545 #endif
546 SIGNAL(TWI_vect)
547 {
548         uint8_t hard_status;
549         uint8_t command = (1<<TWINT) | (1<<TWEN) | (1<<TWIE);
550
551         hard_status = TW_STATUS;
552
553 #if I2C_DEBUG == 1
554         g_prev_twstatus = hard_status;
555         g_intr_cpt++;
556 #endif
557         switch(hard_status) {
558
559 #ifdef CONFIG_MODULE_I2C_MASTER
560         case TW_START:
561         case TW_REP_START:
562                 /* a start has been transmitted, transmit SLA+W which is :
563                  * b7-1: slave address
564                  * b0  : 0 (write operation) or 1 (read) */
565                 if (g_status & I2C_STATUS_MASTER_RECV) {
566                         TWDR = (g_dest << 1) | (0x01);
567                         g_recv_nbytes = 0;
568                 }
569                 else {
570                         TWDR = (g_dest << 1);
571                         g_send_nbytes = 0;
572                 }
573                 break;
574
575
576                 /* MASTER TRANSMITTER */
577
578         case TW_MT_SLA_ACK:
579                 /* the slave is there. start sending data */
580                 TWDR = g_send_buf[g_send_nbytes++];
581                 break;
582
583         case TW_MT_SLA_NACK:
584                 /* the slave does not answer, send a stop condition */
585                 g_send_nbytes = -ENOENT;
586                 g_status |= (I2C_STATUS_OP_FINISHED | I2C_STATUS_NEED_XMIT_EVT);
587                 break;
588
589         case TW_MT_DATA_ACK: /* 0x28 */
590                 /* we transmitted data with success, send next one or
591                  * stop condition if there is no more data */
592                 if (g_send_nbytes >= g_send_size) {
593                         g_status |= (I2C_STATUS_OP_FINISHED | I2C_STATUS_NEED_XMIT_EVT);
594                 }
595                 else {
596                         TWDR = g_send_buf[g_send_nbytes++];
597                 }
598                 break;
599
600         case TW_MT_DATA_NACK:
601                 /* we transmitted data but slave sent us a NACK.
602                  * Notify the number of bytes sent, including the one
603                  * that were not acked, and send a stop condition */
604                 g_status |= (I2C_STATUS_OP_FINISHED | I2C_STATUS_NEED_XMIT_EVT);
605                 break;
606
607
608                 /* MASTER RECEIVER */
609
610         case TW_MR_SLA_ACK:
611                 /* the slave is there, we know that we have enough
612                  * room in buffer because it is the 1st byte. If
613                  * there's only 1 byte to receive, don't set TWEA */
614                 if (g_recv_size > 1)
615                         command |= (1<<TWEA);
616                 break;
617
618         case TW_MR_SLA_NACK:
619                 /* the slave does not answer, send a stop condition */
620                 g_recv_nbytes = -ENOENT;
621                 g_status |= (I2C_STATUS_OP_FINISHED | I2C_STATUS_NEED_RECV_EVT);
622                 break;
623
624         case TW_MR_DATA_ACK:
625                 /* receive data */
626                 if (g_recv_nbytes < g_recv_size) {
627                         g_recv_buf[g_recv_nbytes] = TWDR;
628
629                         if(g_recv_byte_event)
630                                 g_recv_byte_event(hard_status, g_recv_nbytes, g_recv_buf[g_recv_nbytes]);
631
632                         g_recv_nbytes++;
633                 }
634                 /* More than one byte remaining -> set TWEA */
635                 if (g_recv_nbytes < g_recv_size) {
636                         command |= (1<<TWEA);
637                 }
638                 break;
639
640         case TW_MR_DATA_NACK:
641                 /* we received the last byte */
642                 if (g_recv_nbytes < g_recv_size) {
643                         g_recv_buf[g_recv_nbytes] = TWDR;
644
645                         if(g_recv_byte_event)
646                                 g_recv_byte_event(hard_status, g_recv_nbytes, g_recv_buf[g_recv_nbytes]);
647                         g_recv_nbytes ++;
648                 }
649                 g_status |= (I2C_STATUS_OP_FINISHED | I2C_STATUS_NEED_RECV_EVT);
650                 break;
651
652
653                 /* MASTER TRANSMITTER or MASTER RECEIVER */
654
655         case TW_MT_ARB_LOST:
656                 /* arbitration lost, notify application */
657                 /* XXX here we may have to change status flags ? */
658                 if (g_status & I2C_STATUS_MASTER_XMIT) {
659                         g_recv_nbytes = -EAGAIN;
660                         g_status |= I2C_STATUS_NEED_RECV_EVT;
661                 }
662                 else if (g_status & I2C_STATUS_MASTER_RECV) {
663                         g_send_nbytes = -EAGAIN;
664                         g_status |= I2C_STATUS_NEED_XMIT_EVT;
665                 }
666                 /* g_status |= I2C_STATUS_OP_FINISHED; */ /* ?? or not ? */
667                 break;
668
669 #endif
670
671
672                 /* SLAVE RECEIVER */
673
674         case TW_SR_ARB_LOST_SLA_ACK:
675         case TW_SR_ARB_LOST_GCALL_ACK:
676         case TW_SR_GCALL_ACK:
677         case TW_SR_SLA_ACK:
678                 /* slave is addressed (in general call or not, and
679                  * after arbiteration lost or not) */
680                 g_recv_nbytes = 0;
681                 g_recv_size = I2C_RECV_BUFFER_SIZE;
682                 g_status |= I2C_STATUS_SLAVE_RECV;
683                 command |= (1<<TWEA);
684                 break;
685
686         case TW_SR_DATA_ACK:
687         case TW_SR_GCALL_DATA_ACK:
688                 /* receive data, the following test should always be
689                  * true */
690                 if (g_recv_nbytes < g_recv_size) {
691                         g_recv_buf[g_recv_nbytes] = TWDR;
692                         if(g_recv_byte_event)
693                                 g_recv_byte_event(hard_status, g_recv_nbytes, g_recv_buf[g_recv_nbytes]);
694                         g_recv_nbytes++;
695                 }
696                 /* if there's more than one byte left in buffer, send
697                  * TWEA */
698                 if (g_recv_nbytes < g_recv_size) {
699                         command |= (1<<TWEA);
700                 }
701                 break;
702
703         case TW_SR_GCALL_DATA_NACK:
704         case TW_SR_DATA_NACK:
705                 /* receive last data byte (our buffer is full) */
706                 if (g_recv_nbytes < g_recv_size) {
707                         g_recv_buf[g_recv_nbytes] = TWDR;
708
709                         if(g_recv_byte_event)
710                                 g_recv_byte_event(hard_status, g_recv_nbytes, g_recv_buf[g_recv_nbytes]);
711                         g_recv_nbytes++;
712                 }
713                 break;
714
715         case TW_SR_STOP:
716                 /* the master sent a stop condition, notify app */
717                 g_status |= (I2C_STATUS_OP_FINISHED | I2C_STATUS_NEED_RECV_EVT);
718                 break;
719
720
721                 /* SLAVE TRANSMITTER */
722
723         case TW_ST_ARB_LOST_SLA_ACK:
724         case TW_ST_SLA_ACK:
725                 /* slave is addressed. If it is not ready, send a 0 as
726                  * last byte. */
727                 g_send_nbytes = 0;
728                 if (! (g_status & I2C_STATUS_SLAVE_XMIT_WAIT)) {
729                         TWDR = 0;
730                         g_send_size=0;
731                 }
732                 /* else:
733                  * if there is only 1 byte to transmit, we don't
734                  * need to send ack, else set TWEA. */
735                 else {
736                         if (g_send_size > 1) {
737                                 command |= (1<<TWEA);
738                         }
739                         TWDR = g_send_buf[g_send_nbytes++];
740                 }
741                 g_status &= ~(I2C_STATUS_SLAVE_XMIT_WAIT);
742                 g_status |= I2C_STATUS_SLAVE_XMIT;
743                 break;
744
745         case TW_ST_DATA_ACK:
746                 /* transmitting data, if there is more than one byte
747                  * to send, send ack */
748                 if (g_send_size > g_send_nbytes + 1)
749                         command |= (1<<TWEA);
750                 TWDR = g_send_buf[g_send_nbytes++];
751                 break;
752
753         case TW_ST_DATA_NACK:
754                 /* notify app that we send the frame */
755                 g_status |= (I2C_STATUS_OP_FINISHED | I2C_STATUS_NEED_XMIT_EVT);
756                 break;
757
758
759         case TW_ST_LAST_DATA:
760                 /* last data transmitted, notify app XXX (not very sure) */
761                 g_status |= (I2C_STATUS_OP_FINISHED | I2C_STATUS_NEED_XMIT_EVT);
762                 break;
763
764
765                 /* COMMON */
766
767         case TW_BUS_ERROR:
768                 command |= (1<<TWSTO);
769                 g_status |= I2C_STATUS_OP_FINISHED;
770                 break;
771
772         default :
773                 /* default ... what can we do ? */
774                 g_status |= I2C_STATUS_OP_FINISHED;
775                 break;
776
777         }
778
779 #if I2C_DEBUG == 1
780         g_prev_status = g_status;
781 #endif
782
783         /* transmission finished */
784         if (g_status & I2C_STATUS_OP_FINISHED) {
785                 /* if it is not a synchronous op, we should be aware
786                  * of next SLA+RW if we are a slave or multimaster */
787 #ifdef CONFIG_MODULE_I2C_MASTER
788                 if (g_mode != I2C_MODE_MASTER) {
789                         command |= (1<<TWEA);
790                 }
791                 else if ( ! (g_ctrl & I2C_CTRL_DONT_RELEASE_BUS) ) {
792                         /* do it only if we want to release bus */
793                         command |= (1<<TWSTO);
794                 }
795 #else /* CONFIG_MODULE_I2C_MASTER */
796                 command |= (1<<TWEA);
797 #endif
798                 /* Remove current op if !sync, else it is done in the
799                  * i2c_send/recv func */
800                 if ( ! (g_ctrl & I2C_CTRL_SYNC) ) {
801                         g_status &= ~(I2C_STATUS_MASTER_XMIT |
802                                       I2C_STATUS_MASTER_RECV |
803                                       I2C_STATUS_SLAVE_XMIT |
804                                       I2C_STATUS_SLAVE_RECV |
805                                       I2C_STATUS_OP_FINISHED);
806                 }
807         }
808
809         /* Callback events if necessary (if not sync) */
810         if ( ! (g_ctrl & I2C_CTRL_SYNC) ) {
811                 if ( (g_status & I2C_STATUS_NEED_XMIT_EVT) && g_send_event) {
812                         g_send_event(g_send_nbytes);
813                 }
814                 if ( (g_status & I2C_STATUS_NEED_RECV_EVT) && g_recv_event) {
815                         g_recv_event(g_recv_buf, g_recv_nbytes);
816                 }
817         }
818         else {
819                 if ( g_status & (I2C_STATUS_MASTER_XMIT | I2C_STATUS_SLAVE_XMIT) )
820                         g_sync_res = g_send_nbytes;
821                 else
822                         g_sync_res = g_recv_nbytes;
823         }
824         g_status &= ~(I2C_STATUS_NEED_XMIT_EVT | I2C_STATUS_NEED_RECV_EVT);
825
826 #if I2C_DEBUG == 1
827         g_command = command;
828 #endif
829
830         /* if a command (repeated start) has been sent in the callback
831          * (by calling i2c_send() or i2c_recv(), we don't need to
832          * send it (we are back in MASTER_SEND or MASTER_RECV mode) */
833         if (TWCR & (1<<TWINT))
834                 TWCR = command;
835 }