From 4b5d9de0c1791d0d4b7dcd78cf7ef01c6f5e3481 Mon Sep 17 00:00:00 2001 From: batt Date: Mon, 6 Oct 2008 17:21:34 +0000 Subject: [PATCH] Merged from external project: ********** r22398 | batt | 2008-10-02 11:14:38 +0200 (Thu, 02 Oct 2008) | 1 line Add generic i2c interface. ********** git-svn-id: https://src.develer.com/svnoss/bertos/trunk@1868 38d2e660-2303-0410-9eaa-f027e97ec537 --- bertos/drv/i2c.c | 91 ++++++++++++++++++++++++++++++++++++++++++++++++ bertos/drv/i2c.h | 52 +++++++++++++++++++++++++++ 2 files changed, 143 insertions(+) create mode 100644 bertos/drv/i2c.c create mode 100644 bertos/drv/i2c.h diff --git a/bertos/drv/i2c.c b/bertos/drv/i2c.c new file mode 100644 index 00000000..c936837b --- /dev/null +++ b/bertos/drv/i2c.c @@ -0,0 +1,91 @@ +/** + * \file + * + * + * \brief I2C generic driver functions (implementation). + * + * \version $Id$ + * \author Francesco Sacchi + */ + +#include "i2c.h" + +/** + * 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; +} + diff --git a/bertos/drv/i2c.h b/bertos/drv/i2c.h new file mode 100644 index 00000000..907a3214 --- /dev/null +++ b/bertos/drv/i2c.h @@ -0,0 +1,52 @@ +/** + * \file + * + * + * \brief I2C generic driver functions (interface). + * + * \version $Id$ + * \author Francesco Sacchi + */ +#ifndef DRV_I2C_H +#define DRV_I2C_H + +#include + +bool i2c_init(void); +bool i2c_start_w(uint8_t id); +bool i2c_start_r(uint8_t id); +void i2c_stop(void); +bool i2c_put(uint8_t _data); +int i2c_get(bool ack); +bool i2c_send(const void *_buf, size_t count); +bool i2c_recv(void *_buf, size_t count); + +#endif -- 2.25.1