2 * Copyright Droids Corporation (2007)
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.
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.
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
18 * Revision : $Id: i2c.c,v 1.1.2.12 2009-04-24 19:26:54 zer0 Exp $
23 * Author : Olivier MATZ zer0@droids-corp.org
25 * Thanks to Tof for the old i2c module and to Serpilliere for
34 #include <aversive/errno.h>
37 #if I2C_SEND_BUFFER_SIZE < 1
38 #error "I2C_SEND_BUFFER_SIZE must be at least 1"
41 #if I2C_RECV_BUFFER_SIZE < 1
42 #error "I2C_RECV_BUFFER_SIZE must be at least 1"
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;
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;
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;
57 static volatile i2c_mode_t g_mode = I2C_MODE_UNINIT;
58 static volatile uint8_t g_status = I2C_STATUS_READY;
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 */
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 */
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;
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
85 * b7 : true if the uC can be addressed with GENCALL
89 i2c_init(i2c_mode_t mode, uint8_t add)
95 if (mode == I2C_MODE_UNINIT) {
101 #ifdef CONFIG_MODULE_I2C_MASTER
102 else if (mode == I2C_MODE_MASTER) {
103 /* enable, enable int */
104 TWCR = (1<<TWEN) | (1<<TWIE) ;
108 /* enable, enable int, answer to own adress */
109 TWCR = (1<<TWEN) | (1<<TWIE) | (1<<TWEA) ;
115 if (I2C_PRESCALER & 1)
117 if (I2C_PRESCALER & 2)
120 /* change for TWAR format */
129 g_status = I2C_STATUS_READY;
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.
153 i2c_register_recv_event(void (*event)(uint8_t *, int8_t))
157 g_recv_event = event ;
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().
169 i2c_register_recv_byte_event(void (*event)(uint8_t, uint8_t, uint8_t))
173 g_recv_byte_event = event ;
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.
187 i2c_register_send_event(void (*event)(int8_t))
191 g_send_event = event ;
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
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
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
212 i2c_send(uint8_t dest_add, uint8_t *buf, uint8_t size, uint8_t ctrl)
217 if (g_mode == I2C_MODE_UNINIT) {
222 if (g_status & (I2C_STATUS_MASTER_XMIT |
223 I2C_STATUS_MASTER_RECV |
224 I2C_STATUS_SLAVE_XMIT_WAIT |
225 I2C_STATUS_SLAVE_XMIT)) {
230 if (size > I2C_SEND_BUFFER_SIZE) { /* XXX is size==0 ok ? */
235 /* bad dest_address */
236 if (g_mode == I2C_MODE_SLAVE) {
237 if (dest_add != I2C_ADD_MASTER) {
243 if (dest_add >= I2C_ADD_MASTER) {
249 /* if g_send_buf == buf, it is a resend, so don't update
251 if ( g_send_buf != buf ) {
255 memcpy(g_send_buf, buf, size);
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);
265 /* else we are a slave */
266 g_status |= I2C_STATUS_SLAVE_XMIT_WAIT;
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) {
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);
287 if (g_sync_res == size)
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.
304 return i2c_send(g_dest, g_send_buf, g_send_size, g_ctrl);
313 return i2c_recv(g_dest, g_recv_size, g_ctrl);
320 i2c_release_bus(void)
322 TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE) | (1<<TWSTO);
326 * recover from error state
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);
341 TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE) |
342 (1<<TWSTO) | (1<<TWEA);
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.
352 int8_t i2c_recv(uint8_t dest_add, uint8_t size, uint8_t ctrl)
354 #ifndef CONFIG_MODULE_I2C_MASTER
360 if (g_mode == I2C_MODE_UNINIT) {
365 if (g_status != I2C_STATUS_READY) {
370 if (size > I2C_SEND_BUFFER_SIZE) { /* XXX is size=0 ok ? */
375 if (g_mode == I2C_MODE_SLAVE || dest_add >= I2C_ADD_MASTER) {
382 g_status |= I2C_STATUS_MASTER_RECV;
384 TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE) | (1<<TWSTA);
388 /* If it is sync mode, wait op_finished. Here we will reset
389 * the status flag to ready */
390 if (ctrl & I2C_CTRL_SYNC) {
393 if (g_status & I2C_STATUS_OP_FINISHED) {
394 g_status &= ~(I2C_STATUS_MASTER_XMIT |
395 I2C_STATUS_MASTER_RECV |
396 I2C_STATUS_SLAVE_XMIT |
397 I2C_STATUS_SLAVE_RECV |
398 I2C_STATUS_OP_FINISHED);
404 if (g_sync_res == size)
415 * Try to flush the current operation, before it is started. The
416 * i2c module is then tagged as ready. If it returns 0, the flush was
417 * a success, and i2c_send() can be called. Else, it means that
418 * a transmission was running.
420 int8_t i2c_flush(void)
424 if ( ! (g_status & I2C_STATUS_SLAVE_XMIT_WAIT) ) {
429 g_status &= ~(I2C_STATUS_SLAVE_XMIT_WAIT);
437 * In MASTER RECEIVER mode, it is possible that the user application
438 * does not know the size of the buffer. You can adjust this size
439 * during transmission (generally the size can be specified at the
440 * beginning of received data, so the user app can be notified thanks
441 * to recv_byte_event(). Note that i2c_set_recv_size() function has to
442 * be used with careful, making sure you understand i2c protocol and
443 * this code. Return 0 on success. Note than in SLAVE RECEIVER mode,
444 * you don't have to use this function, because the master can end the
445 * transmission by sending a stop condition on the bus.
447 uint8_t i2c_set_recv_size(uint8_t size)
453 /* check that we are in reveiver mode */
454 if (! (g_status & I2C_STATUS_MASTER_RECV)) {
459 /* check that specified size is not greater than
460 * I2C_SEND_BUFFER_SIZE. But it must be greater than current
461 * number of received bytes. */
463 if (size > I2C_SEND_BUFFER_SIZE || size <= g_recv_nbytes) {
476 * return the current mode of the i2c module.
478 i2c_mode_t i2c_mode(void)
484 * return the status of the i2c module.
486 uint8_t i2c_status(void)
492 * Copy the received buffer in the buffer given as parameter. Return
493 * number of copied bytes or < 0 on error.
495 uint8_t i2c_get_recv_buffer(uint8_t *buf, uint8_t size)
500 /* check that reception is finished */
501 if ( g_status & (I2C_STATUS_MASTER_RECV |
502 I2C_STATUS_SLAVE_RECV) ) {
507 if (size > g_recv_nbytes)
508 size = g_recv_nbytes;
509 memcpy(buf, g_recv_buf, size);
519 printf_P(PSTR("mode=0x%x\r\n"), g_mode);
520 printf_P(PSTR("status=0x%x\r\n"), g_status);
521 printf_P(PSTR("ctrl=0x%x\r\n"), g_ctrl);
522 printf_P(PSTR("dst=%d\r\n"), g_dest);
523 printf_P(PSTR("send_nbytes=%d, send_size=%d\r\n"), g_send_nbytes, g_send_size);
524 printf_P(PSTR("recv_nbytes=%d, recv_size=%d\r\n"), g_recv_nbytes, g_recv_size);
525 printf_P(PSTR("prev_twstatus=0x%x\r\n"), g_prev_twstatus);
526 printf_P(PSTR("intr_cpt=%d\r\n"), g_intr_cpt);
527 printf_P(PSTR("prev_status=0x%x\r\n"), g_prev_status);
528 printf_P(PSTR("prev_command=0x%x\r\n"), g_command);
532 /** interrupt ********************************************************/
535 * Interrupt routing for I2C. Refer to datasheets for more
538 SIGNAL(SIG_2WIRE_SERIAL)
541 uint8_t command = (1<<TWINT) | (1<<TWEN) | (1<<TWIE);
543 hard_status = TW_STATUS;
546 g_prev_twstatus = hard_status;
549 switch(hard_status) {
551 #ifdef CONFIG_MODULE_I2C_MASTER
554 /* a start has been transmitted, transmit SLA+W which is :
555 * b7-1: slave address
556 * b0 : 0 (write operation) or 1 (read) */
557 if (g_status & I2C_STATUS_MASTER_RECV) {
558 TWDR = (g_dest << 1) | (0x01);
562 TWDR = (g_dest << 1);
568 /* MASTER TRANSMITTER */
571 /* the slave is there. start sending data */
572 TWDR = g_send_buf[g_send_nbytes++];
576 /* the slave does not answer, send a stop condition */
577 g_send_nbytes = -ENOENT;
578 g_status |= (I2C_STATUS_OP_FINISHED | I2C_STATUS_NEED_XMIT_EVT);
581 case TW_MT_DATA_ACK: /* 0x28 */
582 /* we transmitted data with success, send next one or
583 * stop condition if there is no more data */
584 if (g_send_nbytes >= g_send_size) {
585 g_status |= (I2C_STATUS_OP_FINISHED | I2C_STATUS_NEED_XMIT_EVT);
588 TWDR = g_send_buf[g_send_nbytes++];
592 case TW_MT_DATA_NACK:
593 /* we transmitted data but slave sent us a NACK.
594 * Notify the number of bytes sent, including the one
595 * that were not acked, and send a stop condition */
596 g_status |= (I2C_STATUS_OP_FINISHED | I2C_STATUS_NEED_XMIT_EVT);
600 /* MASTER RECEIVER */
603 /* the slave is there, we know that we have enough
604 * room in buffer because it is the 1st byte. If
605 * there's only 1 byte to receive, don't set TWEA */
607 command |= (1<<TWEA);
611 /* the slave does not answer, send a stop condition */
612 g_recv_nbytes = -ENOENT;
613 g_status |= (I2C_STATUS_OP_FINISHED | I2C_STATUS_NEED_RECV_EVT);
618 if (g_recv_nbytes < g_recv_size) {
619 g_recv_buf[g_recv_nbytes] = TWDR;
621 if(g_recv_byte_event)
622 g_recv_byte_event(hard_status, g_recv_nbytes, g_recv_buf[g_recv_nbytes]);
626 /* More than one byte remaining -> set TWEA */
627 if (g_recv_nbytes < g_recv_size) {
628 command |= (1<<TWEA);
632 case TW_MR_DATA_NACK:
633 /* we received the last byte */
634 if (g_recv_nbytes < g_recv_size) {
635 g_recv_buf[g_recv_nbytes] = TWDR;
637 if(g_recv_byte_event)
638 g_recv_byte_event(hard_status, g_recv_nbytes, g_recv_buf[g_recv_nbytes]);
641 g_status |= (I2C_STATUS_OP_FINISHED | I2C_STATUS_NEED_RECV_EVT);
645 /* MASTER TRANSMITTER or MASTER RECEIVER */
648 /* arbitration lost, notify application */
649 /* XXX here we may have to change status flags ? */
650 if (g_status & I2C_STATUS_MASTER_XMIT) {
651 g_recv_nbytes = -EAGAIN;
652 g_status |= I2C_STATUS_NEED_RECV_EVT;
654 else if (g_status & I2C_STATUS_MASTER_RECV) {
655 g_send_nbytes = -EAGAIN;
656 g_status |= I2C_STATUS_NEED_XMIT_EVT;
658 /* g_status |= I2C_STATUS_OP_FINISHED; */ /* ?? or not ? */
666 case TW_SR_ARB_LOST_SLA_ACK:
667 case TW_SR_ARB_LOST_GCALL_ACK:
668 case TW_SR_GCALL_ACK:
670 /* slave is addressed (in general call or not, and
671 * after arbiteration lost or not) */
673 g_recv_size = I2C_RECV_BUFFER_SIZE;
674 g_status |= I2C_STATUS_SLAVE_RECV;
675 command |= (1<<TWEA);
679 case TW_SR_GCALL_DATA_ACK:
680 /* receive data, the following test should always be
682 if (g_recv_nbytes < g_recv_size) {
683 g_recv_buf[g_recv_nbytes] = TWDR;
684 if(g_recv_byte_event)
685 g_recv_byte_event(hard_status, g_recv_nbytes, g_recv_buf[g_recv_nbytes]);
688 /* if there's more than one byte left in buffer, send
690 if (g_recv_nbytes < g_recv_size) {
691 command |= (1<<TWEA);
695 case TW_SR_GCALL_DATA_NACK:
696 case TW_SR_DATA_NACK:
697 /* receive last data byte (our buffer is full) */
698 if (g_recv_nbytes < g_recv_size) {
699 g_recv_buf[g_recv_nbytes] = TWDR;
701 if(g_recv_byte_event)
702 g_recv_byte_event(hard_status, g_recv_nbytes, g_recv_buf[g_recv_nbytes]);
708 /* the master sent a stop condition, notify app */
709 g_status |= (I2C_STATUS_OP_FINISHED | I2C_STATUS_NEED_RECV_EVT);
713 /* SLAVE TRANSMITTER */
715 case TW_ST_ARB_LOST_SLA_ACK:
717 /* slave is addressed. If it is not ready, send a 0 as
720 if (! (g_status & I2C_STATUS_SLAVE_XMIT_WAIT)) {
725 * if there is only 1 byte to transmit, we don't
726 * need to send ack, else set TWEA. */
728 if (g_send_size > 1) {
729 command |= (1<<TWEA);
731 TWDR = g_send_buf[g_send_nbytes++];
733 g_status &= ~(I2C_STATUS_SLAVE_XMIT_WAIT);
734 g_status |= I2C_STATUS_SLAVE_XMIT;
738 /* transmitting data, if there is more than one byte
739 * to send, send ack */
740 if (g_send_size > g_send_nbytes + 1)
741 command |= (1<<TWEA);
742 TWDR = g_send_buf[g_send_nbytes++];
745 case TW_ST_DATA_NACK:
746 /* notify app that we send the frame */
747 g_status |= (I2C_STATUS_OP_FINISHED | I2C_STATUS_NEED_XMIT_EVT);
751 case TW_ST_LAST_DATA:
752 /* last data transmitted, notify app XXX (not very sure) */
753 g_status |= (I2C_STATUS_OP_FINISHED | I2C_STATUS_NEED_XMIT_EVT);
760 command |= (1<<TWSTO);
761 g_status |= I2C_STATUS_OP_FINISHED;
765 /* default ... what can we do ? */
766 g_status |= I2C_STATUS_OP_FINISHED;
772 g_prev_status = g_status;
775 /* transmission finished */
776 if (g_status & I2C_STATUS_OP_FINISHED) {
777 /* if it is not a synchronous op, we should be aware
778 * of next SLA+RW if we are a slave or multimaster */
779 #ifdef CONFIG_MODULE_I2C_MASTER
780 if (g_mode != I2C_MODE_MASTER) {
781 command |= (1<<TWEA);
783 else if ( ! (g_ctrl & I2C_CTRL_DONT_RELEASE_BUS) ) {
784 /* do it only if we want to release bus */
785 command |= (1<<TWSTO);
787 #else /* CONFIG_MODULE_I2C_MASTER */
788 command |= (1<<TWEA);
790 /* Remove current op if !sync, else it is done in the
791 * i2c_send/recv func */
792 if ( ! (g_ctrl & I2C_CTRL_SYNC) ) {
793 g_status &= ~(I2C_STATUS_MASTER_XMIT |
794 I2C_STATUS_MASTER_RECV |
795 I2C_STATUS_SLAVE_XMIT |
796 I2C_STATUS_SLAVE_RECV |
797 I2C_STATUS_OP_FINISHED);
801 /* Callback events if necessary (if not sync) */
802 if ( ! (g_ctrl & I2C_CTRL_SYNC) ) {
803 if ( (g_status & I2C_STATUS_NEED_XMIT_EVT) && g_send_event) {
804 g_send_event(g_send_nbytes);
806 if ( (g_status & I2C_STATUS_NEED_RECV_EVT) && g_recv_event) {
807 g_recv_event(g_recv_buf, g_recv_nbytes);
811 if ( g_status & (I2C_STATUS_MASTER_XMIT | I2C_STATUS_SLAVE_XMIT) )
812 g_sync_res = g_send_nbytes;
814 g_sync_res = g_recv_nbytes;
816 g_status &= ~(I2C_STATUS_NEED_XMIT_EVT | I2C_STATUS_NEED_RECV_EVT);
822 /* if a command (repeated start) has been sent in the callback
823 * (by calling i2c_send() or i2c_recv(), we don't need to
824 * send it (we are back in MASTER_SEND or MASTER_RECV mode) */
825 if (TWCR & (1<<TWINT))