Aggiornato il comment block dei log RCS
[bertos.git] / drv / eeprom.c
1 /*!
2  * \file
3  * <!--
4  * Copyright 2003, 2004 Develer S.r.l. (http://www.develer.com/)
5  * All Rights Reserved.
6  * -->
7  *
8  * \version $Id$
9  *
10  * \author Stefano Fedrigo <aleph@develer.com>
11  * \author Bernardo Innocenti <bernie@develer.com>
12  *
13  * \brief Driver for the 24xx16 and 24xx256 I2C EEPROMS (implementation)
14  *
15  * \note This implementation is AVR specific.
16  */
17
18 /*#*
19  *#* $Log$
20  *#* Revision 1.8  2004/08/25 14:12:08  rasky
21  *#* Aggiornato il comment block dei log RCS
22  *#*
23  *#* Revision 1.7  2004/08/24 16:48:40  bernie
24  *#* Note reason for including <macros.h>
25  *#*
26  *#* Revision 1.6  2004/08/24 14:27:20  bernie
27  *#* Doxygen fix.
28  *#*
29  *#* Revision 1.5  2004/08/24 13:46:48  bernie
30  *#* Include <macros.h>.
31  *#*
32  *#* Revision 1.4  2004/08/10 06:57:22  bernie
33  *#* eeprom_erase(): New function.
34  *#*
35  *#* Revision 1.3  2004/07/29 22:57:09  bernie
36  *#* Add 24LC16 support.
37  *#*
38  *#* Revision 1.2  2004/07/22 01:24:43  bernie
39  *#* Document AVR dependency.
40  *#*
41  *#* Revision 1.1  2004/07/20 17:11:18  bernie
42  *#* Import into DevLib.
43  *#*
44  *#*/
45
46 #include "eeprom.h"
47
48 #include <mware/byteorder.h> /* cpu_to_be16() */
49 #include <drv/kdebug.h>
50 #include <hw.h>
51 #include <macros.h>  // MIN()
52
53 #include <string.h> // memset()
54
55 #include <avr/twi.h>
56
57
58 /* Wait for TWINT flag set: bus is ready */
59 #define WAIT_TWI_READY  do {} while (!(TWCR & BV(TWINT)))
60
61 /*! \name EEPROM control codes */
62 /*@{*/
63 #define SLA_W  0xA0
64 #define SLA_R  0xA1
65 /*@}*/
66
67
68 /*!
69  * Send START condition on the bus.
70  *
71  * \return true on success, false otherwise.
72  */
73 static bool twi_start(void)
74 {
75         TWCR = BV(TWINT) | BV(TWSTA) | BV(TWEN);
76         WAIT_TWI_READY;
77
78         if (TW_STATUS == TW_START || TW_STATUS == TW_REP_START)
79                 return true;
80
81         DB(kprintf("!TW_(REP)START: %x\n", TWSR);)
82         return false;
83 }
84
85
86 /*!
87  * Send START condition and select slave for write.
88  *
89  * \return true on success, false otherwise.
90  */
91 static bool twi_start_w(uint8_t slave_addr)
92 {
93         ASSERT(slave_addr < 8);
94
95         /*
96          * Loop on the select write sequence: when the eeprom is busy
97          * writing previously sent data it will reply to the SLA_W
98          * control byte with a NACK.  In this case, we must
99          * keep trying until the eeprom responds with an ACK.
100          */
101         while (twi_start())
102         {
103                 TWDR = SLA_W | (slave_addr << 1);
104                 TWCR = BV(TWINT) | BV(TWEN);
105                 WAIT_TWI_READY;
106
107                 if (TW_STATUS == TW_MT_SLA_ACK)
108                         return true;
109                 else if (TW_STATUS != TW_MT_SLA_NACK)
110                 {
111                         DB(kprintf("!TW_MT_SLA_(N)ACK: %x\n", TWSR);)
112                         break;
113                 }
114         }
115
116         return false;
117 }
118
119
120 /*!
121  * Send START condition and select slave for read.
122  *
123  * \return true on success, false otherwise.
124  */
125 static bool twi_start_r(uint8_t slave_addr)
126 {
127         ASSERT(slave_addr < 8);
128
129         if (twi_start())
130         {
131                 TWDR = SLA_R | (slave_addr << 1);
132                 TWCR = BV(TWINT) | BV(TWEN);
133                 WAIT_TWI_READY;
134
135                 if (TW_STATUS == TW_MR_SLA_ACK)
136                         return true;
137
138                 DB(kprintf("!TW_MR_SLA_ACK: %x\n", TWSR);)
139         }
140
141         return false;
142 }
143
144
145 /*!
146  * Send STOP condition.
147  */
148 static void twi_stop(void)
149 {
150         TWCR = BV(TWINT) | BV(TWEN) | BV(TWSTO);
151 }
152
153
154 /*!
155  * Send a sequence of bytes in master transmitter mode
156  * to the selected slave device through the TWI bus.
157  *
158  * \return true on success, false on error.
159  */
160 static bool twi_send(const uint8_t *buf, size_t count)
161 {
162         while (count--)
163         {
164                 TWDR = *buf++;
165                 TWCR = BV(TWINT) | BV(TWEN);
166                 WAIT_TWI_READY;
167                 if (TW_STATUS != TW_MT_DATA_ACK)
168                 {
169                         DB(kprintf("!TW_MT_DATA_ACK: %x\n", TWSR);)
170                         return false;
171                 }
172         }
173
174         return true;
175 }
176
177
178 /*!
179  * Receive a sequence of one or more bytes from the
180  * selected slave device in master receive mode through
181  * the TWI bus.
182  *
183  * Received data is placed in \c buf.
184  *
185  * \return true on success, false on error
186  */
187 static bool twi_recv(uint8_t *buf, size_t count)
188 {
189         /*
190          * When reading the last byte the TWEA bit is not
191          * set, and the eeprom should answer with NACK
192          */
193         while (count--)
194         {
195                 TWCR = BV(TWINT) | BV(TWEN) | (count ? BV(TWEA) : 0);
196                 WAIT_TWI_READY;
197
198                 if (count)
199                 {
200                         if (TW_STATUS != TW_MR_DATA_ACK)
201                         {
202                                 DB(kprintf("!TW_MR_DATA_ACK: %x\n", TWSR);)
203                                 return false;
204                         }
205                 }
206                 else
207                 {
208                         if (TW_STATUS != TW_MR_DATA_NACK)
209                         {
210                                 DB(kprintf("!TW_MR_DATA_NACK: %x\n", TWSR);)
211                                 return false;
212                         }
213                 }
214                 *buf++ = TWDR;
215         }
216
217         return true;
218 }
219
220 /*!
221  * Copy \c count bytes from buffer \c buf to
222  * eeprom at address \c addr.
223  */
224 bool eeprom_write(e2addr_t addr, const void *buf, size_t count)
225 {
226         bool result = true;
227         ASSERT(addr + count <= EEPROM_SIZE);
228
229         while (count && result)
230         {
231                 /*
232                  * Split write in multiple sequential mode operations that
233                  * don't cross page boundaries.
234                  */
235                 size_t size =
236                         MIN(count, (size_t)(EEPROM_BLKSIZE - (addr & (EEPROM_BLKSIZE - 1))));
237
238         #if CONFIG_EEPROM_TYPE == EEPROM_24XX16
239                 /*
240                  * The 24LC16 uses the slave address as a 3-bit
241                  * block address.
242                  */
243                 uint8_t blk_addr = (uint8_t)((addr >> 8) & 0x07);
244                 uint8_t blk_offs = (uint8_t)addr;
245
246                 result =
247                         twi_start_w(blk_addr)
248                         && twi_send(&blk_offs, sizeof blk_offs)
249                         && twi_send(buf, size);
250
251         #elif CONFIG_EEPROM_TYPE == EEPROM_24XX256
252
253                 // 24LC256 wants big-endian addresses
254                 uint16_t addr_be = cpu_to_be16(addr);
255
256                 result =
257                         twi_start_w(0)
258                         && twi_send((uint8_t *)&addr_be, sizeof addr_be)
259                         && twi_send(buf, size);
260
261         #else
262                 #error Unknown device type
263         #endif
264
265                 twi_stop();
266
267                 // DEBUG
268                 //kprintf("addr=%d, count=%d, size=%d, *#?=%d\n",
269                 //      addr, count, size,
270                 //      (EEPROM_BLKSIZE - (addr & (EEPROM_BLKSIZE - 1)))
271                 //);
272
273                 /* Update count and addr for next operation */
274                 count -= size;
275                 addr += size;
276                 buf = ((const char *)buf) + size;
277         }
278
279         return result;
280 }
281
282
283 /*!
284  * Copy \c count bytes at address \c addr
285  * from eeprom to RAM to buffer \c buf.
286  */
287 bool eeprom_read(e2addr_t addr, void *buf, size_t count)
288 {
289         ASSERT(addr + count <= EEPROM_SIZE);
290
291 #if CONFIG_EEPROM_TYPE == EEPROM_24XX16
292         /*
293          * The 24LC16 uses the slave address as a 3-bit
294          * block address.
295          */
296         uint8_t blk_addr = (uint8_t)((addr >> 8) & 0x07);
297         uint8_t blk_offs = (uint8_t)addr;
298
299         bool res =
300                 twi_start_w(blk_addr)
301                 && twi_send(&blk_offs, sizeof blk_offs)
302                 && twi_start_r(blk_addr)
303                 && twi_recv(buf, count);
304
305 #elif CONFIG_EEPROM_TYPE == EEPROM_24XX256
306
307         // 24LC256 wants big-endian addresses
308         addr = cpu_to_be16(addr);
309
310         bool res =
311                 twi_start_w(0)
312                 && twi_send((uint8_t *)&addr, sizeof(addr))
313                 && twi_start_r(0)
314                 && twi_recv(buf, count);
315 #else
316         #error Unknown device type
317 #endif
318
319         twi_stop();
320
321         return res;
322 }
323
324
325 /*!
326  * Write a single character \a c at address \a addr.
327  */
328 bool eeprom_write_char(e2addr_t addr, char c)
329 {
330         return eeprom_write(addr, &c, 1);
331 }
332
333
334 /*!
335  * Read a single character at address \a addr.
336  *
337  * \return the requested character or -1 in case of failure.
338  */
339 int eeprom_read_char(e2addr_t addr)
340 {
341         char c;
342
343         if (eeprom_read(addr, &c, 1))
344                 return c;
345         else
346                 return -1;
347 }
348
349
350 /*!
351  * Erase specified part of eeprom, writing 0xFF.
352  *
353  * \param addr   starting address
354  * \param count  length of block to erase
355  */
356 void eeprom_erase(e2addr_t addr, size_t count)
357 {
358         uint8_t buf[EEPROM_BLKSIZE];
359         memset(buf, 0xFF, sizeof buf);
360
361         // Clear all but struct hw_info at start of eeprom
362         while (count)
363         {
364                 size_t size = MIN(count, sizeof buf);
365                 eeprom_write(addr, buf, size);
366                 addr += size;
367                 count -= size;
368         }
369 }
370
371
372 /*!
373  * Initialize TWI module.
374  */
375 void eeprom_init(void)
376 {
377         cpuflags_t flags;
378         DISABLE_IRQSAVE(flags);
379
380 #if defined(__AVR_ATmega64__)
381         PORTD |= BV(PD0) | BV(PD1);
382         DDRD |= BV(PD0) | BV(PD1);
383 #elif defined(__AVR_ATmega8__)
384         PORTC |= BV(PC4) | BV(PC5);
385         DDRC |= BV(PC4) | BV(PC5);
386 #else
387         #error Unsupported architecture
388 #endif
389
390         /*
391          * Set speed:
392          * F = CLOCK_FREQ / (16 + 2*TWBR * 4^TWPS)
393          */
394         #define TWI_FREQ  300000  /* 300 kHz */
395         #define TWI_PRESC 1       /* 4 ^ TWPS */
396
397         TWBR = (CLOCK_FREQ / (2 * TWI_FREQ * TWI_PRESC)) - (8 / TWI_PRESC);
398         TWSR = 0;
399         TWCR = BV(TWEN);
400
401         ENABLE_IRQRESTORE(flags);
402 }
403
404
405 #ifdef _DEBUG
406
407 #include <string.h>
408
409 void eeprom_test(void)
410 {
411         static const char magic[13] = "Humpty Dumpty";
412         char buf[sizeof magic + 1];
413         size_t i;
414
415         // Write something to EEPROM using unaligned sequential writes
416         for (i = 0; i < 42; ++i)
417                 eeprom_write(i * sizeof magic, magic, sizeof magic);
418
419         // Read back with single-byte reads
420         for (i = 0; i < 42 * sizeof magic; ++i)
421         {
422                 eeprom_read(i, buf, 1);
423                 kprintf("EEPROM byte read: %c (%d)\n", buf[0], buf[0]);
424                 ASSERT(buf[0] == magic[i % sizeof magic]);
425         }
426
427         // Read back again using sequential reads
428         for (i = 0; i < 42; ++i)
429         {
430                 memset(buf, 0, sizeof buf);
431                 eeprom_read(i * sizeof magic, buf, sizeof magic);
432                 kprintf("EEPROM seq read @ 0x%x: '%s'\n", i * sizeof magic, buf);
433                 ASSERT(memcmp(buf, magic, sizeof magic) == 0);
434         }
435 }
436
437 #endif // _DEBUG