From 2ec062d0fa64791fc844d8385a8cf06e8df5dab6 Mon Sep 17 00:00:00 2001 From: batt Date: Mon, 6 Oct 2008 17:21:37 +0000 Subject: [PATCH] Merged from external project: ********** r22400 | batt | 2008-10-02 11:15:03 +0200 (Thu, 02 Oct 2008) | 1 line Refactor for new i2c interface. ********** git-svn-id: https://src.develer.com/svnoss/bertos/trunk@1869 38d2e660-2303-0410-9eaa-f027e97ec537 --- bertos/cpu/avr/drv/twi_avr.c | 34 +++++++++++------------ bertos/cpu/avr/drv/twi_avr.h | 37 ++++++++----------------- bertos/drv/eeprom.c | 24 ++++++++-------- bertos/drv/i2c_bitbang.c | 53 +----------------------------------- 4 files changed, 41 insertions(+), 107 deletions(-) diff --git a/bertos/cpu/avr/drv/twi_avr.c b/bertos/cpu/avr/drv/twi_avr.c index 270efe45..80b97ad6 100644 --- a/bertos/cpu/avr/drv/twi_avr.c +++ b/bertos/cpu/avr/drv/twi_avr.c @@ -38,11 +38,11 @@ * \author Bernie Innocenti */ -#include "twi_avr.h" +#include "i2c_avr.h" #include "hw/hw_cpu.h" /* CLOCK_FREQ */ -#include "cfg/cfg_twi.h" +#include "cfg/cfg_i2c.h" #include #include // BV() #include @@ -65,7 +65,7 @@ * * \return true on success, false otherwise. */ -static bool twi_start(void) +static bool i2c_start(void) { TWCR = BV(TWINT) | BV(TWSTA) | BV(TWEN); WAIT_TWI_READY; @@ -85,7 +85,7 @@ static bool twi_start(void) * * \return true on success, false otherwise. */ -bool twi_start_w(uint8_t id) +bool i2c_start_w(uint8_t id) { /* * Loop on the select write sequence: when the eeprom is busy @@ -94,7 +94,7 @@ bool twi_start_w(uint8_t id) * keep trying until the eeprom responds with an ACK. */ ticks_t start = timer_clock(); - while (twi_start()) + while (i2c_start()) { TWDR = id & ~READ_BIT; TWCR = BV(TWINT) | BV(TWEN); @@ -125,9 +125,9 @@ bool twi_start_w(uint8_t id) * * \return true on success, false otherwise. */ -bool twi_start_r(uint8_t id) +bool i2c_start_r(uint8_t id) { - if (twi_start()) + if (i2c_start()) { TWDR = id | READ_BIT; TWCR = BV(TWINT) | BV(TWEN); @@ -146,7 +146,7 @@ bool twi_start_r(uint8_t id) /** * Send STOP condition. */ -void twi_stop(void) +void i2c_stop(void) { TWCR = BV(TWINT) | BV(TWEN) | BV(TWSTO); } @@ -158,7 +158,7 @@ void twi_stop(void) * * \return true on success, false on error. */ -bool twi_put(const uint8_t data) +bool i2c_put(const uint8_t data) { TWDR = data; TWCR = BV(TWINT) | BV(TWEN); @@ -179,7 +179,7 @@ bool twi_put(const uint8_t data) * * \return the byte read if ok, EOF on errors. */ -int twi_get(bool ack) +int i2c_get(bool ack) { TWCR = BV(TWINT) | BV(TWEN) | (ack ? BV(TWEA) : 0); WAIT_TWI_READY; @@ -212,13 +212,13 @@ int twi_get(bool ack) * * \return true on success, false on error. */ -bool twi_send(const void *_buf, size_t count) +bool i2c_send(const void *_buf, size_t count) { const uint8_t *buf = (const uint8_t *)_buf; while (count--) { - if (!twi_put(*buf++)) + if (!i2c_put(*buf++)) return false; } return true; @@ -237,7 +237,7 @@ bool twi_send(const void *_buf, size_t count) * * \return true on success, false on error */ -bool twi_recv(void *_buf, size_t count) +bool i2c_recv(void *_buf, size_t count) { uint8_t *buf = (uint8_t *)_buf; @@ -251,7 +251,7 @@ bool twi_recv(void *_buf, size_t count) * The last byte read does not has an ACK * to stop communication. */ - int c = twi_get(count); + int c = i2c_get(count); if (c == EOF) return false; @@ -263,12 +263,12 @@ bool twi_recv(void *_buf, size_t count) } -MOD_DEFINE(twi); +MOD_DEFINE(i2c); /** * Initialize TWI module. */ -void twi_init(void) +void i2c_init(void) { ATOMIC( /* @@ -305,5 +305,5 @@ void twi_init(void) TWSR = 0; TWCR = BV(TWEN); ); - MOD_INIT(twi); + MOD_INIT(i2c); } diff --git a/bertos/cpu/avr/drv/twi_avr.h b/bertos/cpu/avr/drv/twi_avr.h index 1ff89292..2d496df7 100644 --- a/bertos/cpu/avr/drv/twi_avr.h +++ b/bertos/cpu/avr/drv/twi_avr.h @@ -38,33 +38,18 @@ * \brief Driver for the AVR ATMega TWI (interface) */ -/*#* - *#* $Log$ - *#* Revision 1.5 2006/07/19 12:56:26 bernie - *#* Convert to new Doxygen style. - *#* - *#* Revision 1.4 2006/03/20 17:49:49 bernie - *#* Make the TWI driver more generic to work with devices other than EEPROMS. - *#* - *#* Revision 1.3 2005/04/11 19:10:28 bernie - *#* Include top-level headers from cfg/ subdir. - *#* - *#* Revision 1.2 2005/02/18 11:19:52 bernie - *#* Update copyright info. - *#* - *#*/ -#ifndef DRV_TWI_H -#define DRV_TWI_H +#ifndef DRV_I2C_H +#define DRV_I2C_H #include -bool twi_start_w(uint8_t id); -bool twi_start_r(uint8_t id); -void twi_stop(void); -bool twi_put(const uint8_t data); -bool twi_send(const void *_buf, size_t count); -int twi_get(bool ack); -bool twi_recv(void *_buf, size_t count); -void twi_init(void); +bool i2c_start_w(uint8_t id); +bool i2c_start_r(uint8_t id); +void i2c_stop(void); +bool i2c_put(const uint8_t data); +bool i2c_send(const void *_buf, size_t count); +int i2c_get(bool ack); +bool i2c_recv(void *_buf, size_t count); +void i2c_init(void); -#endif /* DRV_EEPROM_H */ +#endif /* DRV_I2C_H */ diff --git a/bertos/drv/eeprom.c b/bertos/drv/eeprom.c index 83688b78..f9209148 100644 --- a/bertos/drv/eeprom.c +++ b/bertos/drv/eeprom.c @@ -48,7 +48,7 @@ #include // MOD_CHECK() #include -#include CPU_HEADER(twi) +#include #include @@ -147,15 +147,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; @@ -232,11 +232,11 @@ 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; } @@ -246,7 +246,7 @@ static size_t eeprom_read(struct KFile *_fd, void *_buf, size_t size) * The last byte read does not have an ACK * to stop communication. */ - int c = twi_get(size); + int c = i2c_get(size); if (c == EOF) break; @@ -368,7 +368,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)); diff --git a/bertos/drv/i2c_bitbang.c b/bertos/drv/i2c_bitbang.c index 71243898..7067d919 100644 --- a/bertos/drv/i2c_bitbang.c +++ b/bertos/drv/i2c_bitbang.c @@ -52,7 +52,7 @@ INLINE bool i2c_start(void) return !SDA; } -INLINE void i2c_stop(void) +void i2c_stop(void) { SCL_HI; timer_udelay(I2C_PERIOD); @@ -144,54 +144,3 @@ int i2c_get(bool ack) return (int)(uint8_t)data; } -/** - * Send a sequence of bytes in master transmitter mode - * to the selected slave device through the I2C bus. - * - * \return true on success, false on error. - */ -bool i2c_send(const void *_buf, size_t count) -{ - const uint8_t *buf = (const uint8_t *)_buf; - - while (count--) - { - if (!i2c_put(*buf++)) - return false; - } - return true; -} - -/** - * Receive a sequence of one or more bytes from the - * selected slave device in master receive mode through - * the I2C bus. - * - * Received data is placed in \c buf. - * - * \note a NACK is automatically given on the last received - * byte. - * - * \return true on success, false on error - */ -bool i2c_recv(void *_buf, size_t count) -{ - uint8_t *buf = (uint8_t *)_buf; - - while (count--) - { - /* - * The last byte read does not has an ACK - * to stop communication. - */ - int c = i2c_get(count); - - if (c == EOF) - return false; - else - *buf++ = c; - } - - return true; -} - -- 2.25.1