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