eeprom.c
Go to the documentation of this file.00001
00019 #include <stddef.h>
00020 #include "i2cdrv.h"
00021 #include "eeprom.h"
00022
00023
00024
00025
00026
00028 #define EEPROM_DVK_LEN 0x100
00029
00031 #define EEPROM_DVK_PAGESIZE 16
00032
00033
00034
00035
00036
00037
00038
00066 static int EEPROM_AckPoll(I2C_TypeDef *i2c, uint8_t addr)
00067 {
00068 I2C_TransferSeq_TypeDef seq;
00069 I2C_TransferReturn_TypeDef ret;
00070
00071
00072 (void) i2c;
00073
00074
00075 seq.addr = addr;
00076 seq.flags = I2C_FLAG_WRITE;
00077
00078 seq.buf[0].data = NULL;
00079 seq.buf[0].len = 0;
00080
00081
00082 while (1)
00083 {
00084 ret = I2CDRV_Transfer(&seq);
00085 if (ret == i2cTransferDone)
00086 {
00087 break;
00088 }
00089 else if (ret == i2cTransferNack)
00090 {
00091 continue;
00092 }
00093 else
00094 {
00095 return((int) ret);
00096 }
00097 }
00098
00099 return(0);
00100 }
00101
00102
00103
00104
00105
00106
00107
00132 int EEPROM_Read(I2C_TypeDef *i2c,
00133 uint8_t addr,
00134 unsigned int offset,
00135 uint8_t *data,
00136 unsigned int len)
00137 {
00138 I2C_TransferSeq_TypeDef seq;
00139 I2C_TransferReturn_TypeDef ret;
00140 uint8_t offsetLoc[1];
00141
00142
00143 (void) i2c;
00144
00145 if (offset >= EEPROM_DVK_LEN)
00146 {
00147 return(0);
00148 }
00149
00150 if ((offset + len) > EEPROM_DVK_LEN)
00151 {
00152 len = EEPROM_DVK_LEN - offset;
00153 }
00154
00155 seq.addr = addr;
00156 seq.flags = I2C_FLAG_WRITE_READ;
00157
00158 offsetLoc[0] = (uint8_t) offset;
00159 seq.buf[0].data = offsetLoc;
00160 seq.buf[0].len = 1;
00161
00162 seq.buf[1].data = data;
00163 seq.buf[1].len = len;
00164
00165 ret = I2CDRV_Transfer(&seq);
00166 if (ret != i2cTransferDone)
00167 {
00168 return((int) ret);
00169 }
00170
00171 return((int) len);
00172 }
00173
00174
00175
00200 int EEPROM_Write(I2C_TypeDef *i2c,
00201 uint8_t addr,
00202 unsigned int offset,
00203 uint8_t *data,
00204 unsigned int len)
00205 {
00206 I2C_TransferSeq_TypeDef seq;
00207 I2C_TransferReturn_TypeDef ret;
00208 int tmp;
00209 unsigned int chunk;
00210 unsigned int max;
00211 uint8_t offsetLoc[1];
00212
00213 if (offset >= EEPROM_DVK_LEN)
00214 {
00215 return(0);
00216 }
00217
00218 if ((offset + len) > EEPROM_DVK_LEN)
00219 {
00220 len = EEPROM_DVK_LEN - offset;
00221 }
00222
00223
00224 while (len)
00225 {
00226 max = EEPROM_DVK_PAGESIZE - (offset % EEPROM_DVK_PAGESIZE);
00227
00228 if (len > max)
00229 {
00230 chunk = max;
00231 }
00232 else
00233 {
00234 chunk = len;
00235 }
00236
00237 seq.addr = addr;
00238 seq.flags = I2C_FLAG_WRITE_WRITE;
00239
00240 offsetLoc[0] = (uint8_t) offset;
00241 seq.buf[0].data = offsetLoc;
00242 seq.buf[0].len = 1;
00243
00244 seq.buf[1].data = data;
00245 seq.buf[1].len = chunk;
00246
00247 ret = I2CDRV_Transfer(&seq);
00248 if (ret != i2cTransferDone)
00249 {
00250 return((int) ret);
00251 }
00252
00253
00254 data += chunk;
00255 offset += chunk;
00256 len -= chunk;
00257
00258
00259 tmp = EEPROM_AckPoll(i2c, addr);
00260 if (tmp)
00261 {
00262 return(tmp);
00263 }
00264 }
00265
00266 return((int) len);
00267 }