X-Git-Url: https://codewiz.org/gitweb?a=blobdiff_plain;f=bertos%2Fdrv%2Feeprom.c;h=cc9b5292ba15c34af47822c37c6d61090149ecab;hb=fe0574946861850880407c1be53f3b12c61cbb04;hp=6f0f8b59a96e6737e940d2a89ed5cce01337f2ec;hpb=3248e1ab5f9aed70aa84ceae205e893dec79c875;p=bertos.git diff --git a/bertos/drv/eeprom.c b/bertos/drv/eeprom.c index 6f0f8b59..cc9b5292 100644 --- a/bertos/drv/eeprom.c +++ b/bertos/drv/eeprom.c @@ -26,29 +26,26 @@ * invalidate any other reasons why the executable file might be covered by * the GNU General Public License. * - * Copyright 2003, 2004, 2005 Develer S.r.l. (http://www.develer.com/) + * Copyright 2003, 2004, 2005, 2010 Develer S.r.l. (http://www.develer.com/) * * --> * * \brief Driver for the 24xx16 and 24xx256 I2C EEPROMS (implementation) * - * - * \version $Id$ * \author Stefano Fedrigo * \author Bernie Innocenti */ #include "eeprom.h" -#warning TODO:Test and complete this module for arm platform. +#include "cfg/cfg_i2c.h" -#if 0 #include // MIN() #include #include // MOD_CHECK() #include -#include CPU_HEADER(twi) +#include #include @@ -72,6 +69,12 @@ */ static const EepromInfo mem_info[] = { + { + /* 24XX08 */ + .has_dev_addr = false, + .blk_size = 0x10, + .e2_size = 0x400, + }, { /* 24XX16 */ .has_dev_addr = false, @@ -90,11 +93,19 @@ static const EepromInfo mem_info[] = .blk_size = 0x80, .e2_size = 0x10000, }, + { + /* 24XX1024 */ + .has_dev_addr = true, + .blk_size = 0x100, + .e2_size = 0x20000, + }, + /* Add other memories here */ }; STATIC_ASSERT(countof(mem_info) == EEPROM_CNT); +#if !CONFIG_I2C_DISABLE_OLD_API /** * Copy \a size bytes from buffer \a buf to @@ -102,7 +113,7 @@ STATIC_ASSERT(countof(mem_info) == EEPROM_CNT); */ static size_t eeprom_writeRaw(struct KFile *_fd, const void *buf, size_t size) { - Eeprom *fd = EEPROM(_fd); + Eeprom *fd = EEPROM_CAST(_fd); e2dev_addr_t dev_addr; uint8_t addr_buf[2]; uint8_t addr_len; @@ -113,8 +124,8 @@ static size_t eeprom_writeRaw(struct KFile *_fd, const void *buf, size_t size) STATIC_ASSERT(countof(addr_buf) <= sizeof(e2addr_t)); /* clamp size to memory limit (otherwise may roll back) */ - ASSERT(_fd->seek_pos + size <= (kfile_off_t)_fd->size); - size = MIN((kfile_size_t)size, _fd->size - _fd->seek_pos); + ASSERT(_fd->seek_pos + (kfile_off_t)size <= (kfile_off_t)_fd->size); + size = MIN((kfile_off_t)size, _fd->size - _fd->seek_pos); if (mem_info[fd->type].has_dev_addr) { @@ -147,15 +158,15 @@ static size_t eeprom_writeRaw(struct KFile *_fd, const void *buf, size_t size) } - if (!(twi_start_w(EEPROM_ADDR(dev_addr)) - && twi_send(addr_buf, addr_len) - && twi_send(buf, count))) + if (!(i2c_start_w(EEPROM_ADDR(dev_addr)) + && i2c_send(addr_buf, addr_len) + && i2c_send(buf, count))) { - twi_stop(); + i2c_stop(); return wr_len; } - twi_stop(); + i2c_stop(); /* Update count and addr for next operation */ size -= count; @@ -175,9 +186,9 @@ static size_t eeprom_writeRaw(struct KFile *_fd, const void *buf, size_t size) */ static size_t eeprom_writeVerify(struct KFile *_fd, const void *_buf, size_t size) { - Eeprom *fd = EEPROM(_fd); + Eeprom *fd = EEPROM_CAST(_fd); int retries = 5; - size_t wr_len; + size_t wr_len = 0; while (retries--) { @@ -204,7 +215,7 @@ static size_t eeprom_writeVerify(struct KFile *_fd, const void *_buf, size_t siz */ static size_t eeprom_read(struct KFile *_fd, void *_buf, size_t size) { - Eeprom *fd = EEPROM(_fd); + Eeprom *fd = EEPROM_CAST(_fd); uint8_t addr_buf[2]; uint8_t addr_len; size_t rd_len = 0; @@ -213,8 +224,8 @@ static size_t eeprom_read(struct KFile *_fd, void *_buf, size_t size) STATIC_ASSERT(countof(addr_buf) <= sizeof(e2addr_t)); /* clamp size to memory limit (otherwise may roll back) */ - ASSERT(_fd->seek_pos + size <= (kfile_off_t)_fd->size); - size = MIN((kfile_size_t)size, _fd->size - _fd->seek_pos); + ASSERT(_fd->seek_pos + (kfile_off_t)size <= (kfile_off_t)_fd->size); + size = MIN((kfile_off_t)size, _fd->size - _fd->seek_pos); e2dev_addr_t dev_addr; if (mem_info[fd->type].has_dev_addr) @@ -232,30 +243,22 @@ static size_t eeprom_read(struct KFile *_fd, void *_buf, size_t size) } - if (!(twi_start_w(EEPROM_ADDR(dev_addr)) - && twi_send(addr_buf, addr_len) - && twi_start_r(EEPROM_ADDR(dev_addr)))) + if (!(i2c_start_w(EEPROM_ADDR(dev_addr)) + && i2c_send(addr_buf, addr_len) + && i2c_start_r(EEPROM_ADDR(dev_addr)))) { - twi_stop(); + i2c_stop(); return 0; } - while (size--) - { - /* - * The last byte read does not has an ACK - * to stop communication. - */ - int c = twi_get(size); - - if (c == EOF) - break; - *buf++ = c; - fd->fd.seek_pos++; - rd_len++; + if (i2c_recv(buf, size)) + { + fd->fd.seek_pos += size; + rd_len += size; } + i2c_stop(); return rd_len; } @@ -368,7 +371,7 @@ bool eeprom_erase(Eeprom *fd, e2addr_t addr, e2_size_t count) */ void eeprom_init(Eeprom *fd, EepromType type, e2dev_addr_t addr, bool verify) { - MOD_CHECK(twi); + MOD_CHECK(i2c); ASSERT(type < EEPROM_CNT); memset(fd, 0, sizeof(*fd)); @@ -388,3 +391,5 @@ void eeprom_init(Eeprom *fd, EepromType type, e2dev_addr_t addr, bool verify) fd->fd.seek = kfile_genericSeek; } + +#endif /* !CONFIG_I2C_DISABLE_OLD_API */