eeprom.c
Go to the documentation of this file.00001
00016 #include <stddef.h>
00017 #include "i2cspm.h"
00018 #include "eeprom.h"
00019
00020
00021
00022
00023
00025 #define EEPROM_DVK_LEN 0x100
00026
00028 #define EEPROM_DVK_PAGESIZE 16
00029
00030
00031
00032
00033
00034
00035
00063 static int EEPROM_AckPoll(I2C_TypeDef *i2c, uint8_t addr)
00064 {
00065 I2C_TransferSeq_TypeDef seq;
00066 I2C_TransferReturn_TypeDef ret;
00067
00068
00069 seq.addr = addr;
00070 seq.flags = I2C_FLAG_WRITE;
00071
00072 seq.buf[0].data = NULL;
00073 seq.buf[0].len = 0;
00074
00075
00076 while (1)
00077 {
00078 ret = I2CSPM_Transfer(i2c, &seq);
00079 if (ret == i2cTransferDone)
00080 {
00081 break;
00082 }
00083 else if (ret == i2cTransferNack)
00084 {
00085 continue;
00086 }
00087 else
00088 {
00089 return((int) ret);
00090 }
00091 }
00092
00093 return(0);
00094 }
00095
00096
00097
00098
00099
00100
00101
00126 int EEPROM_Read(I2C_TypeDef *i2c,
00127 uint8_t addr,
00128 unsigned int offset,
00129 uint8_t *data,
00130 unsigned int len)
00131 {
00132 I2C_TransferSeq_TypeDef seq;
00133 I2C_TransferReturn_TypeDef ret;
00134 uint8_t offsetLoc[1];
00135
00136 if (offset >= EEPROM_DVK_LEN)
00137 {
00138 return(0);
00139 }
00140
00141 if ((offset + len) > EEPROM_DVK_LEN)
00142 {
00143 len = EEPROM_DVK_LEN - offset;
00144 }
00145
00146 seq.addr = addr;
00147 seq.flags = I2C_FLAG_WRITE_READ;
00148
00149 offsetLoc[0] = (uint8_t) offset;
00150 seq.buf[0].data = offsetLoc;
00151 seq.buf[0].len = 1;
00152
00153 seq.buf[1].data = data;
00154 seq.buf[1].len = len;
00155
00156 ret = I2CSPM_Transfer(i2c, &seq);
00157 if (ret != i2cTransferDone)
00158 {
00159 return((int) ret);
00160 }
00161
00162 return((int) len);
00163 }
00164
00165
00166
00191 int EEPROM_Write(I2C_TypeDef *i2c,
00192 uint8_t addr,
00193 unsigned int offset,
00194 uint8_t *data,
00195 unsigned int len)
00196 {
00197 I2C_TransferSeq_TypeDef seq;
00198 I2C_TransferReturn_TypeDef ret;
00199 int tmp;
00200 unsigned int chunk;
00201 unsigned int max;
00202 uint8_t offsetLoc[1];
00203
00204 if (offset >= EEPROM_DVK_LEN)
00205 {
00206 return(0);
00207 }
00208
00209 if ((offset + len) > EEPROM_DVK_LEN)
00210 {
00211 len = EEPROM_DVK_LEN - offset;
00212 }
00213
00214
00215 while (len)
00216 {
00217 max = EEPROM_DVK_PAGESIZE - (offset % EEPROM_DVK_PAGESIZE);
00218
00219 if (len > max)
00220 {
00221 chunk = max;
00222 }
00223 else
00224 {
00225 chunk = len;
00226 }
00227
00228 seq.addr = addr;
00229 seq.flags = I2C_FLAG_WRITE_WRITE;
00230
00231 offsetLoc[0] = (uint8_t) offset;
00232 seq.buf[0].data = offsetLoc;
00233 seq.buf[0].len = 1;
00234
00235 seq.buf[1].data = data;
00236 seq.buf[1].len = chunk;
00237
00238 ret = I2CSPM_Transfer(i2c, &seq);
00239 if (ret != i2cTransferDone)
00240 {
00241 return((int) ret);
00242 }
00243
00244
00245 data += chunk;
00246 offset += chunk;
00247 len -= chunk;
00248
00249
00250 tmp = EEPROM_AckPoll(i2c, addr);
00251 if (tmp)
00252 {
00253 return(tmp);
00254 }
00255 }
00256
00257 return((int) len);
00258 }