4 * Copyright (C) 2013-08-25 K.Ohta <whatisthis.sowhat at gmail.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2,
10 * or (at your option) any later version.
11 * This library / program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 * See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this library; see the file COPYING. If not, write to the
18 * Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
21 * As a special exception, if you link this(includeed from sdcc) library
22 * with other files, some of which are compiled with SDCC,
23 * to produce an executable, this library does not by itself cause
24 * the resulting executable to be covered by the GNU General Public License.
25 * This exception does not however invalidate any other reasons why
26 * the executable file might be covered by the GNU General Public License.
29 #include "i2c_eeprom.h"
31 static unsigned char i2c_eeprom_write_1block(unsigned char i2caddr, unsigned char page_hi, unsigned char page_lo, unsigned char *data)
34 unsigned char *p = data;
37 i2c_open(I2C_MASTER, I2C_SLEW_ON, 5);
39 i2c_writechar(i2caddr & 0xfe);
40 i2c_writechar(page_hi);
41 i2c_writechar(page_lo);
42 for(i = 0; i < 64; i++){
48 OpenI2C(MASTER, SLEW_ON);
52 // while (SSPCON2bits.SEN);
53 WriteI2C(i2caddr & 0xfe);
54 // while(SSPCON2bits.SEN);
56 // while(SSPCON2bits.SEN);
60 for(i = 0; i < 32; i++){
68 WriteI2C(i2caddr & 0xfe);
70 } while(SSPCON2bits.ACKSTAT);
72 // while (SSPCON2bits.PEN);
79 unsigned char i2c_eeprom_bytewrite(unsigned char i2caddr, unsigned int addr, unsigned char data)
81 unsigned char page_hi = addr >> 8;
82 unsigned char page_lo = addr & 0xff; // 64byte?
85 i2c_open(I2C_MASTER, I2C_SLEW_ON, 5);
87 i2c_writechar(i2caddr & 0xfe);
88 i2c_writechar(page_hi);
89 i2c_writechar(page_lo);
94 OpenI2C(MASTER, SLEW_ON);
101 while(SSPCON2bits.SEN);
102 WriteI2C(i2caddr & 0xfe);
103 while(SSPCON2bits.ACKSTAT);
104 // WriteI2C(i2caddr & 0xfe);
106 while(SSPCON2bits.ACKSTAT);
108 while(SSPCON2bits.ACKSTAT);
110 while(SSPCON2bits.ACKSTAT);
112 while(SSPCON2bits.PEN);
115 #endif // i2c_idle();
120 unsigned char i2c_eeprom_burstwrite(unsigned char i2caddr, unsigned int addr, unsigned char *data, unsigned int bytes)
122 unsigned char page_hi;
123 unsigned char page_lo;
127 // page_hi = addr >> 8;
128 // page_lo = addr & 0xff;
129 // i2c_eeprom_write_1block(i2caddr, page_hi, page_lo, data);
130 i2c_eeprom_bytewrite(i2caddr, addr, *data);
140 static unsigned char i2c_eeprom_read_1block(unsigned char addr, unsigned char page_hi, unsigned char page_lo, unsigned char *data)
144 unsigned char *p = data;
147 i2c_open(I2C_MASTER, I2C_SLEW_ON, 5);
149 i2c_writechar(addr & 0xfe);
150 i2c_writechar(page_hi);
151 i2c_writechar(page_lo);
155 i2c_writechar(addr | 0x01);
156 for(i = 0; i < 64; i++){
164 OpenI2C(MASTER, SLEW_ON);
167 WriteI2C(addr & 0xfe);
173 WriteI2C(addr | 0x01);
175 for(i = 0; i < 32; i++){
176 if(!SSPCON2bits.ACKSTAT){
177 SSPCON2bits.RCEN = 1;
178 while(SSPCON2bits.RCEN);
180 while (SSPCON2bits.ACKEN);
182 while (SSPCON2bits.PEN);
199 unsigned char i2c_eeprom_byteread(unsigned char i2caddr, unsigned int addr)
201 unsigned char page_hi = addr >> 8;
202 unsigned char page_lo = addr & 0xff; // 64byte?
206 i2c_open(I2C_MASTER, I2C_SLEW_ON, 5);
208 i2c_writechar(i2caddr);
209 i2c_writechar(page_hi);
210 i2c_writechar(page_lo);
215 i2c_writechar(i2caddr | 1);
221 OpenI2C(MASTER, SLEW_ON);
224 while(SSPCON2bits.SEN);
227 while(SSPCON2bits.SEN);
229 while(SSPCON2bits.SEN);
235 WriteI2C(i2caddr | 1);
236 if (!SSPCON2bits.ACKSTAT){
237 SSPCON2bits.RCEN = 1;
238 while(SSPCON2bits.RCEN);
240 while (SSPCON2bits.ACKEN);
242 while (SSPCON2bits.PEN);
254 unsigned char i2c_eeprom_burstread(unsigned char i2caddr, unsigned int addr, unsigned char *data, unsigned int bytes)
256 unsigned char page_hi;
257 unsigned char page_lo;
261 // page_hi = addr >> 8;
262 // page_lo = addr & 0xff;
263 *data = i2c_eeprom_byteread(i2caddr, addr);
264 // i2c_eeprom_read_1block(i2caddr, page_hi, page_lo, data);