X-Git-Url: https://codewiz.org/gitweb?a=blobdiff_plain;f=bertos%2Fdrv%2Fi2c.h;h=f7812d4ebbc3692e6d5d5d88d26f4cd45f46bf08;hb=bce79ae17dc4b489639cd102fa7563acd129898f;hp=f0a3ec0dabb9bc445c577cfd97a288c653c465a9;hpb=0d4c0c64f736f37dba565aa9e113d3920ef905f9;p=bertos.git diff --git a/bertos/drv/i2c.h b/bertos/drv/i2c.h index f0a3ec0d..f7812d4e 100644 --- a/bertos/drv/i2c.h +++ b/bertos/drv/i2c.h @@ -37,6 +37,7 @@ * $WIZ$ module_name = "i2c" * $WIZ$ module_configuration = "bertos/cfg/cfg_i2c.h" * $WIZ$ module_hw = "bertos/hw/hw_i2c_bitbang.h" + * $WIZ$ module_depends = "i2c_bitbang" * $WIZ$ module_supports = "not atmega103 and not atmega168 and not at91" */ @@ -63,14 +64,35 @@ #define i2c_start_r(args...) PP_CAT(i2c_start_r ## _, COUNT_PARMS(args)) (args) #endif + +/** + * \name I2C bitbang devices enum + */ +enum +{ + I2C_BITBANG_OLD = -1, + I2C_BITBANG0 = 1000, + I2C_BITBANG1, + I2C_BITBANG2, + I2C_BITBANG3, + I2C_BITBANG4, + I2C_BITBANG5, + I2C_BITBANG6, + I2C_BITBANG7, + I2C_BITBANG8, + I2C_BITBANG9, + + I2C_BITBANG_CNT /**< Number of serial ports */ +}; + +#if !CONFIG_I2C_DISABLE_OLD_API + /** * I2C Backends. * Sometimes your cpu does not have a builtin * i2c driver or you don't want, for some reason, to * use that. * With this you can choose, at compile time, which backend to use. - * - * $WIZ$ i2c_backend = "I2C_BACKEND_BUILTIN", "I2C_BACKEND_BITBANG" */ #define I2C_BACKEND_BUILTIN 0 ///< Uses cpu builtin i2c driver #define I2C_BACKEND_BITBANG 1 ///< Uses emulated bitbang driver @@ -83,7 +105,6 @@ * that you want the builtin backend. * \{ */ -void i2c_builtin_init(void); bool i2c_builtin_start_w(uint8_t id); bool i2c_builtin_start_r(uint8_t id); void i2c_builtin_stop(void); @@ -99,7 +120,6 @@ int i2c_builtin_get(bool ack); * that you want the bitbang backend. * \{ */ -void i2c_bitbang_init(void); bool i2c_bitbang_start_w(uint8_t id); bool i2c_bitbang_start_r(uint8_t id); void i2c_bitbang_stop(void); @@ -107,15 +127,17 @@ bool i2c_bitbang_put(uint8_t _data); int i2c_bitbang_get(bool ack); /*\}*/ +#ifndef CONFIG_I2C_BACKEND +#define CONFIG_I2C_BACKEND I2C_BACKEND_BUILTIN +#endif + #if CONFIG_I2C_BACKEND == I2C_BACKEND_BUILTIN - #define i2c_init_0 i2c_builtin_init #define i2c_start_w_1 i2c_builtin_start_w #define i2c_start_r_1 i2c_builtin_start_r #define i2c_stop i2c_builtin_stop #define i2c_put i2c_builtin_put #define i2c_get i2c_builtin_get #elif CONFIG_I2C_BACKEND == I2C_BACKEND_BITBANG - #define i2c_init_0 i2c_bitbang_init #define i2c_start_w_1 i2c_bitbang_start_w #define i2c_start_r_1 i2c_bitbang_start_r #define i2c_stop i2c_bitbang_stop @@ -129,33 +151,11 @@ int i2c_bitbang_get(bool ack); bool i2c_send(const void *_buf, size_t count); bool i2c_recv(void *_buf, size_t count); +#endif /* !CONFIG_I2C_DISABLE_OLD_API */ /* - * I2c new api - */ - -/** - * \name I2C bitbang devices enum + * I2C error flags */ -enum -{ - I2C_BITBANG0 = 1000, - I2C_BITBANG1, - I2C_BITBANG2, - I2C_BITBANG3, - I2C_BITBANG4, - I2C_BITBANG5, - I2C_BITBANG6, - I2C_BITBANG7, - I2C_BITBANG8, - I2C_BITBANG9, - - I2C_BITBANG_CNT /**< Number of serial ports */ -}; - - /* - * I2C error flags - */ #define I2C_OK 0 ///< I2C no errors flag #define I2C_DATA_NACK BV(4) ///< I2C generic error #define I2C_ERR BV(3) ///< I2C generic error @@ -321,12 +321,23 @@ INLINE int i2c_error(I2c *i2c) return err; } -INLINE void i2c_init_3(I2c *i2c, int dev, uint32_t clock) +#define i2c_init_3(i2c, dev, clock) ((((dev) >= I2C_BITBANG0) | ((dev) == I2C_BITBANG_OLD)) ? \ + i2c_hw_bitbangInit((i2c), (dev)) : i2c_hw_init((i2c), (dev), (clock))) + +#if !CONFIG_I2C_DISABLE_OLD_API + +extern I2c local_i2c_old_api; + +INLINE void i2c_init_0(void) { - if (dev > I2C_BITBANG0) - i2c_hw_bitbangInit(i2c, dev); - else - i2c_hw_init(i2c, dev, clock); + #if CONFIG_I2C_BACKEND == I2C_BACKEND_BITBANG + i2c_init_3(&local_i2c_old_api, I2C_BITBANG_OLD, CONFIG_I2C_FREQ); + #else + i2c_init_3(&local_i2c_old_api, 0, CONFIG_I2C_FREQ); + #endif } +#endif /* !CONFIG_I2C_DISABLE_OLD_API */ + + #endif