EFR32 Mighty Gecko 1 Software Documentation  efr32mg1-doc-5.1.2
eeprom.c
Go to the documentation of this file.
1 /***************************************************************************/
16 #include <stddef.h>
17 #include "i2cspm.h"
18 #include "eeprom.h"
19 
20 /*******************************************************************************
21  ******************************* DEFINES ***********************************
22  ******************************************************************************/
23 
25 #define EEPROM_DVK_LEN 0x100
26 
28 #define EEPROM_DVK_PAGESIZE 16
29 
30 
31 /*******************************************************************************
32  *************************** LOCAL FUNCTIONS *******************************
33  ******************************************************************************/
34 
35 /***************************************************************************/
63 static int EEPROM_AckPoll(I2C_TypeDef *i2c, uint8_t addr)
64 {
67 
68  /* Do acknowledge polling waiting for write process to finish in EEPROM */
69  seq.addr = addr;
70  seq.flags = I2C_FLAG_WRITE;
71  /* Just access device with write operation */
72  seq.buf[0].data = NULL;
73  seq.buf[0].len = 0;
74 
75  /* Wait for ACK from device */
76  while (1)
77  {
78  ret = I2CSPM_Transfer(i2c, &seq);
79  if (ret == i2cTransferDone)
80  {
81  break;
82  }
83  else if (ret == i2cTransferNack)
84  {
85  continue;
86  }
87  else
88  {
89  return((int) ret);
90  }
91  }
92 
93  return(0);
94 }
95 
96 
97 /*******************************************************************************
98  ************************** GLOBAL FUNCTIONS *******************************
99  ******************************************************************************/
100 
101 /***************************************************************************/
127  uint8_t addr,
128  unsigned int offset,
129  uint8_t *data,
130  unsigned int len)
131 {
134  uint8_t offsetLoc[1];
135 
136  if (offset >= EEPROM_DVK_LEN)
137  {
138  return(0);
139  }
140 
141  if ((offset + len) > EEPROM_DVK_LEN)
142  {
143  len = EEPROM_DVK_LEN - offset;
144  }
145 
146  seq.addr = addr;
148  /* Select offset to start reading from */
149  offsetLoc[0] = (uint8_t) offset;
150  seq.buf[0].data = offsetLoc;
151  seq.buf[0].len = 1;
152  /* Select location/length of data to be read */
153  seq.buf[1].data = data;
154  seq.buf[1].len = len;
155 
156  ret = I2CSPM_Transfer(i2c, &seq);
157  if (ret != i2cTransferDone)
158  {
159  return((int) ret);
160  }
161 
162  return((int) len);
163 }
164 
165 
166 /***************************************************************************/
192  uint8_t addr,
193  unsigned int offset,
194  uint8_t *data,
195  unsigned int len)
196 {
199  int tmp;
200  unsigned int chunk;
201  unsigned int max;
202  uint8_t offsetLoc[1];
203 
204  if (offset >= EEPROM_DVK_LEN)
205  {
206  return(0);
207  }
208 
209  if ((offset + len) > EEPROM_DVK_LEN)
210  {
211  len = EEPROM_DVK_LEN - offset;
212  }
213 
214  /* Write max one page at a time */
215  while (len)
216  {
217  max = EEPROM_DVK_PAGESIZE - (offset % EEPROM_DVK_PAGESIZE);
218 
219  if (len > max)
220  {
221  chunk = max;
222  }
223  else
224  {
225  chunk = len;
226  }
227 
228  seq.addr = addr;
230  /* Select offset to start writing to */
231  offsetLoc[0] = (uint8_t) offset;
232  seq.buf[0].data = offsetLoc;
233  seq.buf[0].len = 1;
234  /* Select location/length of data to be written */
235  seq.buf[1].data = data;
236  seq.buf[1].len = chunk;
237 
238  ret = I2CSPM_Transfer(i2c, &seq);
239  if (ret != i2cTransferDone)
240  {
241  return((int) ret);
242  }
243 
244  /* Update counters etc */
245  data += chunk;
246  offset += chunk;
247  len -= chunk;
248 
249  /* Do acknowledge polling waiting for write process to finish in EEPROM */
250  tmp = EEPROM_AckPoll(i2c, addr);
251  if (tmp)
252  {
253  return(tmp);
254  }
255  }
256 
257  return((int) len);
258 }
I2C_TransferReturn_TypeDef I2CSPM_Transfer(I2C_TypeDef *i2c, I2C_TransferSeq_TypeDef *seq)
Perform I2C transfer.
Definition: i2cspm.c:124
int EEPROM_Write(I2C_TypeDef *i2c, uint8_t addr, unsigned int offset, uint8_t *data, unsigned int len)
Write data to EEPROM.
Definition: eeprom.c:191
#define I2C_FLAG_WRITE
Indicate plain write sequence: S+ADDR(W)+DATA0+P.
Definition: em_i2c.h:124
I2C_TransferReturn_TypeDef
Definition: em_i2c.h:179
EEPROM driver for 24AA024 (2Kbit) EEPROM device on the DK.
I2C simple poll-based master mode driver for the DK/STK.
struct I2C_TransferSeq_TypeDef::@0 buf[2]
#define EEPROM_DVK_LEN
Definition: eeprom.c:25
Master mode transfer message structure used to define a complete I2C transfer sequence (from start to...
Definition: em_i2c.h:252
int EEPROM_Read(I2C_TypeDef *i2c, uint8_t addr, unsigned int offset, uint8_t *data, unsigned int len)
Read data from EEPROM.
Definition: eeprom.c:126
#define I2C_FLAG_WRITE_WRITE
Indicate write sequence using two buffers: S+ADDR(W)+DATA0+DATA1+P.
Definition: em_i2c.h:159
#define I2C_FLAG_WRITE_READ
Indicate combined write/read sequence: S+ADDR(W)+DATA0+Sr+ADDR(R)+DATA1+P.
Definition: em_i2c.h:148
uint16_t addr
Address to use after (repeated) start.
Definition: em_i2c.h:262
static int EEPROM_AckPoll(I2C_TypeDef *i2c, uint8_t addr)
Do acknowledge polling on EEPROM device.
Definition: eeprom.c:63
#define EEPROM_DVK_PAGESIZE
Definition: eeprom.c:28