/* * * $Id: i2c.c,v 1.9 2004/03/17 04:45:54 jmuelmen Exp $ * * Copyright (C) 2002, Arnim Laeuger * * 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 * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. See also the file COPYING which * came with this application. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * * Change History: * =============== * * $Log: i2c.c,v $ * Revision 1.9 2004/03/17 04:45:54 jmuelmen * Moved lots of things around in memory. Also implemented a regulation control * loop for VDDA0. More testing needed. Revision number (00.1e.00) reflects the * experimental nature. * * Revision 1.8 2004/03/06 07:58:04 jmuelmen * Woohoo! Down to 4683 bytes. * * Revision 1.7 2004/03/06 07:36:34 jmuelmen * Eliminated lots of (hopefully) useless crap. * * Revision 1.6 2004/02/01 11:22:13 jmuelmen * There was a nasty i2c issue that I finally fixed. Hooray for not ignoring * documentation! (This was the LASTRD issue, which I previously considered * a joke on the part of the documenters) * * Revision 1.5 2003/12/03 20:49:45 jmuelmen * Uh, like, progress. * * Revision 1.4 2003/11/22 06:34:44 jmuelmen * Looks like we have working i2c now. * * Revision 1.3 2003/11/20 23:34:10 jmuelmen * Now the I2C writing works. Blocking writing, but good enough. * * Revision 1.2 2003/11/20 22:42:21 jmuelmen * This is buggy non-working interrupt-driven I2C. * * Revision 1.1 2003/11/16 17:40:03 jmuelmen * I2C subsystem; should eventually do interrupt-driven I2C if the caller * requests it. * * Revision 1.2 2002/08/30 20:39:04 arniml * make PROBLEMATIC_CODE definable and thus usable for debugging * * Revision 1.1 2002/08/26 20:09:03 arniml * initial checkin * */ #include <8051.h> #include "ezusb_reg.h" #include "i2c.h" /* wait for bit 6 (STOP) to be '0' */ #define stop_check() while (I2CS & 0x40) /***************************************************************************** * i2c_write_eeprom(i2c_addr, address, length, *buffer, pagesize, * has_16bit_addr) * * Writes the provided data to the I2C EEPROM. * This routine uses the specified pagesize to achieve maximum performance. * Beginning at the start address the current page is filled and consecutive * pages are written until all data has been processed. * * Input: * i2c_addr : I2C address of EEPROM * address : byte address of first location to be read * length : number of bytes to read from EEPROM * buffer : pointer to byte array containing the data * i2c_pagesize : pagesize of EEPROM * has_16bit_addr : 1 if EEPROM uses 16 bit addressing, * 0 is EEPROM uses 8 bit addressing * Return value: * 0 upon success, 1 otherwise * *****************************************************************************/ int i2c_write (byte address, xdata byte *dat, int length) { register int i = 0; /* wait for whatever else went over the bus before to stop */ stop_check(); /* generate start condition */ I2CS = 0x80; I2DAT = address << 1; do { while (!(I2CS & 0x01)); if (I2CS & 0x04) { /* error(EI2CBUSCONT); */ I2CS = 0x40; /* send stop condition */ return -1; } if (!(I2CS & 0x02)) /* NACK */ { I2CS = 0x40; return i; } /* if this was the last write, generate stop */ if (i == length) { I2CS = 0x40; break; } I2DAT = dat[i++]; } while (1); /* nothing left to do */ return i; } #pragma NOGCSE int i2c_read (byte address, xdata byte *dat, int length) { register int i = 0; register byte rd; if (!length) return 0; /* wait for whatever else went over the bus before to stop */ stop_check(); /* generate start condition */ I2CS = 0x80; /* write addess */ I2DAT = (address << 1) | 0x01; while (!(I2CS & 0x01)); if (I2CS & 0x04) { I2CS = 0x40; /* send stop condition */ return 0; } if (!(I2CS & 0x02)) /* NACK */ { I2CS = 0x40; return 0; } /* dummy read to get clock going */ dat[0] = I2DAT; while (i < length - 2) { while (!(I2CS & 0x01)); if (I2CS & 0x04) { I2CS = 0x40; /* send stop condition */ return 0; } dat[i] = I2DAT; i++; } /* i == length - 2 */ while (!(I2CS & 0x01)); if (I2CS & 0x04) { I2CS = 0x40; /* send stop condition */ return 0; } /* LASTRD = 1 */ I2CS = 0x20; dat[i++] = I2DAT; /* last byte */ while (!(I2CS & 0x01)); if (I2CS & 0x04) { I2CS = 0x40; /* send stop condition */ return 0; } I2CS = 0x40; rd = I2DAT; dat[i++] = rd; return i; }