eeprom.c
Go to the documentation of this file.00001
00036 #include <stddef.h>
00037 #include "i2cdrv.h"
00038 #include "eeprom.h"
00039
00040
00041
00042
00043
00045 #define EEPROM_DVK_LEN 0x100
00046
00048 #define EEPROM_DVK_PAGESIZE 16
00049
00050
00051
00052
00053
00054
00055
00083 static int EEPROM_AckPoll(I2C_TypeDef *i2c, uint8_t addr)
00084 {
00085 I2C_TransferSeq_TypeDef seq;
00086 I2C_TransferReturn_TypeDef ret;
00087
00088
00089 (void) i2c;
00090
00091
00092 seq.addr = addr;
00093 seq.flags = I2C_FLAG_WRITE;
00094
00095 seq.buf[0].data = NULL;
00096 seq.buf[0].len = 0;
00097
00098
00099 while (1)
00100 {
00101 ret = I2CDRV_Transfer(&seq);
00102 if (ret == i2cTransferDone)
00103 {
00104 break;
00105 }
00106 else if (ret == i2cTransferNack)
00107 {
00108 continue;
00109 }
00110 else
00111 {
00112 return((int) ret);
00113 }
00114 }
00115
00116 return(0);
00117 }
00118
00119
00120
00121
00122
00123
00124
00149 int EEPROM_Read(I2C_TypeDef *i2c,
00150 uint8_t addr,
00151 unsigned int offset,
00152 uint8_t *data,
00153 unsigned int len)
00154 {
00155 I2C_TransferSeq_TypeDef seq;
00156 I2C_TransferReturn_TypeDef ret;
00157 uint8_t offsetLoc[1];
00158
00159
00160 (void) i2c;
00161
00162 if (offset >= EEPROM_DVK_LEN)
00163 {
00164 return(0);
00165 }
00166
00167 if ((offset + len) > EEPROM_DVK_LEN)
00168 {
00169 len = EEPROM_DVK_LEN - offset;
00170 }
00171
00172 seq.addr = addr;
00173 seq.flags = I2C_FLAG_WRITE_READ;
00174
00175 offsetLoc[0] = (uint8_t) offset;
00176 seq.buf[0].data = offsetLoc;
00177 seq.buf[0].len = 1;
00178
00179 seq.buf[1].data = data;
00180 seq.buf[1].len = len;
00181
00182 ret = I2CDRV_Transfer(&seq);
00183 if (ret != i2cTransferDone)
00184 {
00185 return((int) ret);
00186 }
00187
00188 return((int) len);
00189 }
00190
00191
00192
00217 int EEPROM_Write(I2C_TypeDef *i2c,
00218 uint8_t addr,
00219 unsigned int offset,
00220 uint8_t *data,
00221 unsigned int len)
00222 {
00223 I2C_TransferSeq_TypeDef seq;
00224 I2C_TransferReturn_TypeDef ret;
00225 int tmp;
00226 unsigned int chunk;
00227 unsigned int max;
00228 uint8_t offsetLoc[1];
00229
00230 if (offset >= EEPROM_DVK_LEN)
00231 {
00232 return(0);
00233 }
00234
00235 if ((offset + len) > EEPROM_DVK_LEN)
00236 {
00237 len = EEPROM_DVK_LEN - offset;
00238 }
00239
00240
00241 while (len)
00242 {
00243 max = EEPROM_DVK_PAGESIZE - (offset % EEPROM_DVK_PAGESIZE);
00244
00245 if (len > max)
00246 {
00247 chunk = max;
00248 }
00249 else
00250 {
00251 chunk = len;
00252 }
00253
00254 seq.addr = addr;
00255 seq.flags = I2C_FLAG_WRITE_WRITE;
00256
00257 offsetLoc[0] = (uint8_t) offset;
00258 seq.buf[0].data = offsetLoc;
00259 seq.buf[0].len = 1;
00260
00261 seq.buf[1].data = data;
00262 seq.buf[1].len = chunk;
00263
00264 ret = I2CDRV_Transfer(&seq);
00265 if (ret != i2cTransferDone)
00266 {
00267 return((int) ret);
00268 }
00269
00270
00271 data += chunk;
00272 offset += chunk;
00273 len -= chunk;
00274
00275
00276 tmp = EEPROM_AckPoll(i2c, addr);
00277 if (tmp)
00278 {
00279 return(tmp);
00280 }
00281 }
00282
00283 return((int) len);
00284 }