BeRTOS
i2c.h
Go to the documentation of this file.
00001 
00095 #ifndef DRV_I2C_H
00096 #define DRV_I2C_H
00097 
00098 #include "cfg/cfg_i2c.h"
00099 
00100 #include <cfg/compiler.h>
00101 #include <cfg/macros.h>
00102 #include <cfg/debug.h>
00103 
00104 #include <cpu/attr.h>
00105 
00106 #define I2C_READBIT BV(0)
00107 
00108 
00109 /*
00110  * The following macros are needed to maintain compatibility with older i2c API.
00111  * They can be safely removed once the old API is removed.
00112  */
00113 
00118 #if COMPILER_C99
00119     #define i2c_init(...)           PP_CAT(i2c_init ## _, COUNT_PARMS(__VA_ARGS__)) (__VA_ARGS__)
00120     #define i2c_start_w(...)        PP_CAT(i2c_start_w ## _, COUNT_PARMS(__VA_ARGS__)) (__VA_ARGS__)
00121     #define i2c_start_r(...)        PP_CAT(i2c_start_r ## _, COUNT_PARMS(__VA_ARGS__)) (__VA_ARGS__)
00122 #else
00123 
00140     #define i2c_init(args...)       PP_CAT(i2c_init ## _, COUNT_PARMS(args)) (args)
00141 
00158     #define i2c_start_w(args...)    PP_CAT(i2c_start_w ## _, COUNT_PARMS(args)) (args)
00159 
00176     #define i2c_start_r(args...)    PP_CAT(i2c_start_r ## _, COUNT_PARMS(args)) (args)
00177 #endif
00178 
00184 enum
00185 {
00186     I2C_BITBANG_OLD = -1,
00187     I2C_BITBANG0 = 1000, 
00188     I2C_BITBANG1,        
00189     I2C_BITBANG2,
00190     I2C_BITBANG3,
00191     I2C_BITBANG4,
00192     I2C_BITBANG5,
00193     I2C_BITBANG6,
00194     I2C_BITBANG7,
00195     I2C_BITBANG8,
00196     I2C_BITBANG9,
00197 
00198     I2C_BITBANG_CNT  
00199 };
00200 
00211 #define I2C_OK               0     ///< I2C no errors flag
00212 #define I2C_DATA_NACK     BV(4)    ///< I2C generic error
00213 #define I2C_ERR           BV(3)    ///< I2C generic error
00214 #define I2C_ARB_LOST      BV(2)    ///< I2C arbitration lost error
00215 #define I2C_START_TIMEOUT BV(0)    ///< I2C timeout error on start
00216 #define I2C_NO_ACK        BV(1)    ///< I2C no ack for sla start
00217 
00224 #define I2C_NOSTOP           0    ///< Do not program the stop for current transition
00225 #define I2C_STOP          BV(0)   ///< Program the stop for current transition
00226 
00227 #define I2C_START_R       BV(1)   // Start read command
00228 #define I2C_START_W          0    // Start write command
00229 
00230 
00231 #define I2C_TEST_START(flag)  ((flag) & I2C_START_R)
00232 #define I2C_TEST_STOP(flag)   ((flag) & I2C_STOP)
00233 
00234 struct I2cHardware;
00235 struct I2c;
00236 
00237 typedef void (*i2c_start_t)(struct I2c *i2c, uint16_t slave_addr);
00238 typedef uint8_t (*i2c_getc_t)(struct I2c *i2c);
00239 typedef void (*i2c_putc_t)(struct I2c *i2c, uint8_t data);
00240 typedef void (*i2c_write_t)(struct I2c *i2c, const void *_buf, size_t count);
00241 typedef void (*i2c_read_t)(struct I2c *i2c, void *_buf, size_t count);
00242 
00243 typedef struct I2cVT
00244 {
00245     i2c_start_t start;
00246     i2c_getc_t   getc;
00247     i2c_putc_t   putc;
00248     i2c_write_t  write;
00249     i2c_read_t   read;
00250 } I2cVT;
00251 
00252 typedef struct I2c
00253 {
00254     int errors;
00255     int flags;
00256     size_t xfer_size;
00257     struct I2cHardware* hw;
00258     const struct I2cVT *vt;
00259 } I2c;
00260 
00261 
00262 #include CPU_HEADER(i2c)
00263 
00264 /*
00265  * Low level i2c  init implementation prototype.
00266  */
00267 void i2c_hw_init(I2c *i2c, int dev, uint32_t clock);
00268 void i2c_hw_bitbangInit(I2c *i2c, int dev);
00269 
00270 void i2c_genericWrite(I2c *i2c, const void *_buf, size_t count);
00271 void i2c_genericRead(I2c *i2c, void *_buf, size_t count);
00272 
00273 /*
00274  * Start a i2c transfer.
00275  *
00276  * \param i2c Context structure.
00277  * \param slave_addr Address of slave device
00278  * \param size Size of the transfer
00279  */
00280 INLINE void i2c_start(I2c *i2c, uint16_t slave_addr, size_t size)
00281 {
00282     ASSERT(i2c->vt);
00283     ASSERT(i2c->vt->start);
00284 
00285     if (!i2c->errors)
00286         ASSERT(i2c->xfer_size == 0);
00287 
00288     i2c->errors = 0;
00289     i2c->xfer_size = size;
00290 
00291     i2c->vt->start(i2c, slave_addr);
00292 }
00293 
00307 INLINE void i2c_start_r_4(I2c *i2c, uint16_t slave_addr, size_t size, int flags)
00308 {
00309     ASSERT(i2c);
00310     i2c->flags = flags | I2C_START_R;
00311     i2c_start(i2c, slave_addr, size);
00312 }
00313 
00321 INLINE void i2c_start_w_4(I2c *i2c, uint16_t slave_addr, size_t size, int flags)
00322 {
00323     ASSERT(i2c);
00324     i2c->flags = flags & ~I2C_START_R;
00325     i2c_start(i2c, slave_addr, size);
00326 }
00327 
00333 INLINE uint8_t i2c_getc(I2c *i2c)
00334 {
00335     ASSERT(i2c);
00336     ASSERT(i2c->vt);
00337     ASSERT(i2c->vt->getc);
00338 
00339     ASSERT(i2c->xfer_size);
00340 
00341     ASSERT(I2C_TEST_START(i2c->flags) == I2C_START_R);
00342 
00343     if (!i2c->errors)
00344     {
00345         uint8_t data = i2c->vt->getc(i2c);
00346         i2c->xfer_size--;
00347         return data;
00348     }
00349     else
00350         return 0xFF;
00351 }
00352 
00358 INLINE void i2c_putc(I2c *i2c, uint8_t data)
00359 {
00360     ASSERT(i2c);
00361     ASSERT(i2c->vt);
00362     ASSERT(i2c->vt->putc);
00363 
00364     ASSERT(i2c->xfer_size);
00365 
00366     ASSERT(I2C_TEST_START(i2c->flags) == I2C_START_W);
00367 
00368     if (!i2c->errors)
00369     {
00370         i2c->vt->putc(i2c, data);
00371         i2c->xfer_size--;
00372     }
00373 }
00374 
00381 INLINE void i2c_write(I2c *i2c, const void *_buf, size_t count)
00382 {
00383     ASSERT(i2c);
00384     ASSERT(i2c->vt);
00385     ASSERT(i2c->vt->write);
00386 
00387     ASSERT(_buf);
00388     ASSERT(count);
00389     ASSERT(count <= i2c->xfer_size);
00390 
00391     ASSERT(I2C_TEST_START(i2c->flags) == I2C_START_W);
00392 
00393     if (!i2c->errors)
00394         i2c->vt->write(i2c, _buf, count);
00395 }
00396 
00403 INLINE void i2c_read(I2c *i2c, void *_buf, size_t count)
00404 {
00405     ASSERT(i2c);
00406     ASSERT(i2c->vt);
00407     ASSERT(i2c->vt->read);
00408 
00409     ASSERT(_buf);
00410     ASSERT(count);
00411     ASSERT(count <= i2c->xfer_size);
00412 
00413     ASSERT(I2C_TEST_START(i2c->flags) == I2C_START_R);
00414 
00415     if (!i2c->errors)
00416         i2c->vt->read(i2c, _buf, count);
00417 }
00418 
00422 INLINE int i2c_error(I2c *i2c)
00423 {
00424     ASSERT(i2c);
00425     int err = i2c->errors;
00426     i2c->errors = 0;
00427 
00428     return err;
00429 }
00430 
00438 #define i2c_init_3(i2c, dev, clock)   ((((dev) >= I2C_BITBANG0) | ((dev) == I2C_BITBANG_OLD)) ? \
00439                                         i2c_hw_bitbangInit((i2c), (dev)) : i2c_hw_init((i2c), (dev), (clock)))
00440  // i2c_api
00442 
00451 #if !CONFIG_I2C_DISABLE_OLD_API
00452 
00463 #define I2C_BACKEND_BUILTIN 0 ///< Uses cpu builtin i2c driver
00464 #define I2C_BACKEND_BITBANG 1 ///< Uses emulated bitbang driver
00465 
00476 bool i2c_builtin_start_w(uint8_t id);
00477 bool i2c_builtin_start_r(uint8_t id);
00478 void i2c_builtin_stop(void);
00479 bool i2c_builtin_put(uint8_t _data);
00480 int i2c_builtin_get(bool ack);
00492 bool i2c_bitbang_start_w(uint8_t id);
00493 bool i2c_bitbang_start_r(uint8_t id);
00494 void i2c_bitbang_stop(void);
00495 bool i2c_bitbang_put(uint8_t _data);
00496 int i2c_bitbang_get(bool ack);
00499 #ifndef CONFIG_I2C_BACKEND
00500 #define  CONFIG_I2C_BACKEND  I2C_BACKEND_BUILTIN
00501 #endif
00502 
00503 #if CONFIG_I2C_BACKEND == I2C_BACKEND_BUILTIN
00504     #define i2c_start_w_1   i2c_builtin_start_w
00505     #define i2c_start_r_1   i2c_builtin_start_r
00506     #define i2c_stop        i2c_builtin_stop
00507     #define i2c_put         i2c_builtin_put
00508     #define i2c_get         i2c_builtin_get
00509 #elif CONFIG_I2C_BACKEND == I2C_BACKEND_BITBANG
00510     #define i2c_start_w_1   i2c_bitbang_start_w
00511     #define i2c_start_r_1   i2c_bitbang_start_r
00512     #define i2c_stop        i2c_bitbang_stop
00513     #define i2c_put         i2c_bitbang_put
00514     #define i2c_get         i2c_bitbang_get
00515 #else
00516     #error Unsupported i2c backend.
00517 #endif
00518 
00519 
00520 bool i2c_send(const void *_buf, size_t count);
00521 bool i2c_recv(void *_buf, size_t count);
00522 
00526 extern I2c local_i2c_old_api;
00527 
00532 INLINE void i2c_init_0(void)
00533 {
00534     #if CONFIG_I2C_BACKEND == I2C_BACKEND_BITBANG
00535         i2c_init_3(&local_i2c_old_api, I2C_BITBANG_OLD, CONFIG_I2C_FREQ);
00536     #else
00537         i2c_init_3(&local_i2c_old_api, 0, CONFIG_I2C_FREQ);
00538     #endif
00539 }
00540 #endif /* !CONFIG_I2C_DISABLE_OLD_API */
00541  //defgroup i2c_driver
00543 
00544 #endif