* \author Bernie Innocenti <bernie@codewiz.org>
*/
-#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 <cfg/debug.h>
#include <cfg/macros.h> // BV()
#include <cfg/module.h>
*
* \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;
*
* \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
* 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);
*
* \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);
/**
* Send STOP condition.
*/
-void twi_stop(void)
+void i2c_stop(void)
{
TWCR = BV(TWINT) | BV(TWEN) | BV(TWSTO);
}
*
* \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);
*
* \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;
*
* \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;
*
* \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;
* 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;
}
-MOD_DEFINE(twi);
+MOD_DEFINE(i2c);
/**
* Initialize TWI module.
*/
-void twi_init(void)
+void i2c_init(void)
{
ATOMIC(
/*
TWSR = 0;
TWCR = BV(TWEN);
);
- MOD_INIT(twi);
+ MOD_INIT(i2c);
}
* \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 <cfg/compiler.h>
-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 */
#include <cfg/module.h> // MOD_CHECK()
#include <cpu/attr.h>
-#include CPU_HEADER(twi)
+#include <drv/i2c.h>
#include <drv/wdt.h>
}
- 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;
}
- 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;
}
* 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;
*/
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));
return !SDA;
}
-INLINE void i2c_stop(void)
+void i2c_stop(void)
{
SCL_HI;
timer_udelay(I2C_PERIOD);
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;
-}
-