Commit from zer0 on branch b_zer0 (2007-06-30 15:27 CEST) ---------------------------------
Factorisation of code for callback events. aversive modules/comm/i2c/i2c.h 1.1.2.5 aversive modules/comm/i2c/i2c.c 1.1.2.4 ------------------------------- aversive/modules/comm/i2c/i2c.h (1.1.2.4 -> 1.1.2.5) ------------------------------- *************** *** 15,21 **** * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * ! * Revision : $Id: i2c.h,v 1.1.2.4 2007-06-24 19:55:54 zer0 Exp $ * */ --- 15,21 ---- * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * ! * Revision : $Id: i2c.h,v 1.1.2.5 2007-06-30 13:27:57 zer0 Exp $ * */ *************** *** 79,84 **** --- 79,86 ---- #define I2C_STATUS_SLAVE_XMIT 0x08 #define I2C_STATUS_SLAVE_RECV 0x10 #define I2C_STATUS_OP_FINISHED 0x20 + #define I2C_STATUS_NEED_XMIT_EVT 0x40 + #define I2C_STATUS_NEED_RECV_EVT 0x80 /** * mode is I2C_MODE_UNINIT, I2C_MODE_MASTER, I2C_MODE_MULTIMASTER or ------------------------------- aversive/modules/comm/i2c/i2c.c (1.1.2.3 -> 1.1.2.4) ------------------------------- *************** *** 1,5 **** /* ! * Copyright Droids Corporation, Microb Technology, Eirbot (2007) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by --- 1,5 ---- /* ! * Copyright Droids Corporation (2007) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by *************** *** 15,25 **** * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * ! * Revision : $Id: i2c.c,v 1.1.2.3 2007-06-24 19:55:54 zer0 Exp $ * */ ! /* Author: Zer0, based on Tof old i2c module */ #include <stdlib.h> #include <string.h> --- 15,30 ---- * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * ! * Revision : $Id: i2c.c,v 1.1.2.4 2007-06-30 13:27:57 zer0 Exp $ * */ ! /* ! * Author : Olivier MATZ [EMAIL PROTECTED] ! * ! * Thanks to Tof for the old i2c module and to Serpilliere for ! * testing. ! */ #include <stdlib.h> #include <string.h> *************** *** 554,573 **** case TW_MT_SLA_NACK: /* the slave does not answer, send a stop condition */ ! if(g_send_event) ! g_send_event(-1); ! command |= (1<<TWSTO); ! g_status |= I2C_STATUS_OP_FINISHED; break; case TW_MT_DATA_ACK: /* we transmitted data with success, send next one or * stop condition if there is no more data */ if (g_send_nbytes >= g_send_size) { ! if(g_send_event) ! g_send_event(g_send_nbytes); ! command |= (1<<TWSTO); ! g_status |= I2C_STATUS_OP_FINISHED; } else { TWDR = g_send_buf[g_send_nbytes++]; --- 559,573 ---- case TW_MT_SLA_NACK: /* the slave does not answer, send a stop condition */ ! g_send_nbytes = -1; ! g_status |= (I2C_STATUS_OP_FINISHED | I2C_STATUS_NEED_XMIT_EVT); break; case TW_MT_DATA_ACK: /* we transmitted data with success, send next one or * stop condition if there is no more data */ if (g_send_nbytes >= g_send_size) { ! g_status |= (I2C_STATUS_OP_FINISHED | I2C_STATUS_NEED_XMIT_EVT); } else { TWDR = g_send_buf[g_send_nbytes++]; *************** *** 578,587 **** /* we transmitted data but slave sent us a NACK. * Notify the number of bytes sent, including the one * that were not acked, and send a stop condition */ ! if(g_send_event) ! g_send_event(g_send_nbytes); ! command |= (1<<TWSTO); ! g_status |= I2C_STATUS_OP_FINISHED; break; --- 578,584 ---- /* we transmitted data but slave sent us a NACK. * Notify the number of bytes sent, including the one * that were not acked, and send a stop condition */ ! g_status |= (I2C_STATUS_OP_FINISHED | I2C_STATUS_NEED_XMIT_EVT); break; *************** *** 597,606 **** case TW_MR_SLA_NACK: /* the slave does not answer, send a stop condition */ ! if(g_recv_event) ! g_recv_event(g_recv_buf, -1); ! command |= (1<<TWSTO); ! g_status |= I2C_STATUS_OP_FINISHED; break; case TW_MR_DATA_ACK: --- 594,601 ---- case TW_MR_SLA_NACK: /* the slave does not answer, send a stop condition */ ! g_recv_nbytes = -1; ! g_status |= (I2C_STATUS_OP_FINISHED | I2C_STATUS_NEED_RECV_EVT); break; case TW_MR_DATA_ACK: *************** *** 628,638 **** g_recv_byte_event(hard_status, g_recv_nbytes, g_recv_buf[g_recv_nbytes]); g_recv_nbytes ++; } ! if(g_recv_event) { ! g_recv_event(g_recv_buf, g_recv_nbytes); ! } ! command |= (1<<TWSTO); ! g_status |= I2C_STATUS_OP_FINISHED; break; --- 623,629 ---- g_recv_byte_event(hard_status, g_recv_nbytes, g_recv_buf[g_recv_nbytes]); g_recv_nbytes ++; } ! g_status |= (I2C_STATUS_OP_FINISHED | I2C_STATUS_NEED_RECV_EVT); break; *************** *** 640,652 **** case TW_MT_ARB_LOST: /* arbitration lost, notify application */ if (g_status & I2C_STATUS_MASTER_XMIT) { ! if(g_recv_event) ! g_recv_event(g_recv_buf, -2); } else if (g_status & I2C_STATUS_MASTER_RECV) { ! if(g_send_event) ! g_send_event(-2); } break; --- 631,644 ---- case TW_MT_ARB_LOST: /* arbitration lost, notify application */ + /* XXX here we may have to change status flags ? */ if (g_status & I2C_STATUS_MASTER_XMIT) { ! g_recv_nbytes = -2; ! g_status |= I2C_STATUS_NEED_RECV_EVT; } else if (g_status & I2C_STATUS_MASTER_RECV) { ! g_send_nbytes = -2; ! g_status |= I2C_STATUS_NEED_XMIT_EVT; } break; *************** *** 698,706 **** case TW_SR_STOP: /* the master sent a stop condition, notify app */ ! if (g_recv_event) ! g_recv_event(g_recv_buf, g_recv_nbytes); ! g_status |= I2C_STATUS_OP_FINISHED; break; --- 690,696 ---- case TW_SR_STOP: /* the master sent a stop condition, notify app */ ! g_status |= (I2C_STATUS_OP_FINISHED | I2C_STATUS_NEED_RECV_EVT); break; *************** *** 739,768 **** case TW_ST_DATA_NACK: /* notify app that we send the frame */ ! if(g_send_event) { ! g_send_event(g_send_nbytes); ! } ! g_status |= I2C_STATUS_OP_FINISHED; break; case TW_ST_LAST_DATA: /* last data transmitted, notify app XXX (not very sure) */ ! if(g_send_event) ! g_send_event(g_send_nbytes); ! g_status |= I2C_STATUS_OP_FINISHED; break; /* COMMON */ case TW_BUS_ERROR: ! /* error >> send stop */ ! command |= (1<<TWSTO); break; default : ! /* default ... what can we do ? send a STOP ? */ break; } --- 729,753 ---- case TW_ST_DATA_NACK: /* notify app that we send the frame */ ! g_status |= (I2C_STATUS_OP_FINISHED | I2C_STATUS_NEED_XMIT_EVT); break; case TW_ST_LAST_DATA: /* last data transmitted, notify app XXX (not very sure) */ ! g_status |= (I2C_STATUS_OP_FINISHED | I2C_STATUS_NEED_XMIT_EVT); break; /* COMMON */ case TW_BUS_ERROR: ! /* error >> send stop ?? XXX */ ! g_status |= I2C_STATUS_OP_FINISHED; break; default : ! /* default ... what can we do ? */ break; } *************** *** 774,782 **** /* if it is not a synchronous op, we should be aware * of next SLA+RW if we are a slave or multimaster */ #ifdef CONFIG_MODULE_I2C_MASTER ! if (g_mode != I2C_MODE_MASTER) command |= (1<<TWEA); ! #else command |= (1<<TWEA); #endif /* remove current op */ --- 759,772 ---- /* if it is not a synchronous op, we should be aware * of next SLA+RW if we are a slave or multimaster */ #ifdef CONFIG_MODULE_I2C_MASTER ! if (g_mode != I2C_MODE_MASTER) { command |= (1<<TWEA); ! } ! else { ! command |= (1<<TWSTO); ! ! } ! #else /* CONFIG_MODULE_I2C_MASTER */ command |= (1<<TWEA); #endif /* remove current op */ *************** *** 786,791 **** I2C_STATUS_SLAVE_RECV | I2C_STATUS_OP_FINISHED); } ! TWCR = command; } --- 776,794 ---- I2C_STATUS_SLAVE_RECV | I2C_STATUS_OP_FINISHED); } + + /* Callback events if necessary */ + if ( (g_status & I2C_STATUS_NEED_XMIT_EVT) && g_send_event) { + g_send_event(g_send_nbytes); + } + if ( (g_status & I2C_STATUS_NEED_RECV_EVT) && g_recv_event) { + g_recv_event(g_recv_buf, g_recv_nbytes); + } + g_status &= ~(I2C_STATUS_NEED_XMIT_EVT | I2C_STATUS_NEED_RECV_EVT); ! /* XXX if we hare back in MASTER_SEND or MASTER_RECV mode, a ! * command (repeated start) has been sent in the callback, so ! * we don't need to send it. */ ! if (1) ! TWCR = command; } _______________________________________________ Avr-list mailing list Avr-list@droids-corp.org CVSWEB : http://cvsweb.droids-corp.org/cgi-bin/viewcvs.cgi/aversive WIKI : http://wiki.droids-corp.org/index.php/Aversive DOXYGEN : http://zer0.droids-corp.org/doxygen_aversive/html/ BUGZILLA : http://bugzilla.droids-corp.org COMMIT LOGS : http://zer0.droids-corp.org/aversive_commitlog