From: asterix Date: Fri, 30 Jul 2010 14:48:43 +0000 (+0000) Subject: Add support for new api. X-Git-Tag: 2.6.0~286 X-Git-Url: https://codewiz.org/gitweb?a=commitdiff_plain;h=e2238a91f56b0d4f79b39e51b2823f555f0ad7e6;p=bertos.git Add support for new api. git-svn-id: https://src.develer.com/svnoss/bertos/trunk@4104 38d2e660-2303-0410-9eaa-f027e97ec537 --- diff --git a/bertos/drv/lm75.c b/bertos/drv/lm75.c index 5d24e0ae..d1ed17ac 100644 --- a/bertos/drv/lm75.c +++ b/bertos/drv/lm75.c @@ -55,8 +55,10 @@ #include // Macro and data type to manage celsius degree #define SELECT_ADDRESS(addr) LM75_ADDRESS_BYTE | (addr << 1) +#define LM75_ADDRESS_BYTE 0x91 +#define LM75_PAD_BYTE 0x0 -deg_t lm75_read(uint8_t sens_addr) +deg_t lm75_read_1(uint8_t sens_addr) { uint8_t data[2]; int16_t degree; @@ -85,11 +87,42 @@ deg_t lm75_read(uint8_t sens_addr) return degree * 10 + deci_degree; } -void lm75_init(void) +void lm75_init_0(void) { // Check dependence MOD_CHECK(i2c); LM75_HW_INIT(); } +/* + * New API + */ +deg_t lm75_read_2(I2c *i2c, uint8_t sens_addr) +{ + uint8_t data[2]; + int16_t degree; + int16_t deci_degree; + + i2c_start_w(i2c, SELECT_ADDRESS(sens_addr), 1, I2C_NOSTOP); + i2c_putc(i2c, LM75_PAD_BYTE); + i2c_start_r(i2c, SELECT_ADDRESS(sens_addr), sizeof(data), I2C_STOP); + i2c_read(i2c, data, sizeof(data)); + + if (i2c_error(i2c)) + return EOF; + + degree = (int16_t)data[0]; + deci_degree = (int16_t)(((data[1] >> 7) & 1 ) * 5); + LOG_INFO("[%d.%d C]\n", degree, deci_degree); + + return degree * 10 + deci_degree; +} + +void lm75_init_1(I2c *i2c) +{ + ASSERT(i2c); + + // Check dependence + LM75_HW_INIT(); +} diff --git a/bertos/drv/lm75.h b/bertos/drv/lm75.h index 62e25c7b..9c6abdfc 100644 --- a/bertos/drv/lm75.h +++ b/bertos/drv/lm75.h @@ -46,12 +46,24 @@ #include #include // Macro and data type to manage celsius degree +#include -#define LM75_ADDRESS_BYTE 0x91 -#define LM75_PAD_BYTE 0x0 +#include -deg_t lm75_read(uint8_t sens_addr); +#define I2C_READBIT BV(0) -void lm75_init(void); +#if COMPILER_C99 + #define lm75_init(...) PP_CAT(lm75_init ## _, COUNT_PARMS(__VA_ARGS__)) (__VA_ARGS__) + #define lm75_read(...) PP_CAT(lm75_read ## _, COUNT_PARMS(__VA_ARGS__)) (__VA_ARGS__) +#else + #define lm75_init(args...) PP_CAT(lm75_init ## _, COUNT_PARMS(args)) (args) + #define lm75_read(args...) PP_CAT(lm75_read ## _, COUNT_PARMS(args)) (args) +#endif + +DEPRECATED deg_t lm75_read_1(uint8_t sens_addr); +DEPRECATED void lm75_init_0(void); + +deg_t lm75_read_2(I2c *i2c, uint8_t sens_addr); +void lm75_init_1(I2c *i2c); #endif /* DRV_LM75_H */ diff --git a/bertos/drv/pcf8574.c b/bertos/drv/pcf8574.c index 5572e40c..3c9ba4d2 100644 --- a/bertos/drv/pcf8574.c +++ b/bertos/drv/pcf8574.c @@ -44,25 +44,27 @@ */ #include "pcf8574.h" + #include + #include /** * Read PCF8574 \a pcf bit status. * \return the pins status or EOF on errors. */ -int pcf8574_get(Pcf8574 *pcf) +int pcf8574_get_1(Pcf8574 *pcf) { if (!i2c_start_r(PCF8574ID | ((pcf->addr << 1) & 0xF7))) return EOF; - int data; - + int data; + if (!i2c_recv(&data, 1)) data = EOF; - + i2c_stop(); - + return data; } @@ -70,7 +72,7 @@ int pcf8574_get(Pcf8574 *pcf) * Write to PCF8574 \a pcf port \a data. * \return true if ok, false on errors. */ -bool pcf8574_put(Pcf8574 *pcf, uint8_t data) +bool pcf8574_put_2(Pcf8574 *pcf, uint8_t data) { bool res = i2c_start_w(PCF8574ID | ((pcf->addr << 1) & 0xF7)) && i2c_put(data); i2c_stop(); @@ -81,9 +83,59 @@ bool pcf8574_put(Pcf8574 *pcf, uint8_t data) * Init a PCF8574 on the bus with addr \a addr. * \return true if device is found, false otherwise. */ -bool pcf8574_init(Pcf8574 *pcf, pcf8574_addr addr) +bool pcf8574_init_2(Pcf8574 *pcf, pcf8574_addr addr) { MOD_CHECK(i2c); pcf->addr = addr; return pcf8574_get(pcf) != EOF; } + + + +/* + * New API + */ + +/** + * Read PCF8574 \a pcf bit status. + * \return the pins status or EOF on errors. + */ +int pcf8574_get_2(I2c *i2c, Pcf8574 *pcf) +{ + i2c_start_r(i2c, PCF8574ID | ((pcf->addr << 1) & 0xF7), 1, I2C_STOP); + + int data = i2c_getc(i2c); + + if (i2c_error(i2c)) + data = EOF; + + return data; +} + +/** + * Write to PCF8574 \a pcf port \a data. + * \return true if ok, false on errors. + */ +bool pcf8574_put_3(I2c *i2c, Pcf8574 *pcf, uint8_t data) +{ + i2c_start_w(i2c, PCF8574ID | ((pcf->addr << 1) & 0xF7), 1, I2C_STOP); + i2c_putc(i2c, data); + + if (i2c_error(i2c)) + return false; + + return true; +} + +/** + * Init a PCF8574 on the bus with addr \a addr. + * \return true if device is found, false otherwise. + */ +bool pcf8574_init_3(I2c *i2c, Pcf8574 *pcf, pcf8574_addr addr) +{ + ASSERT(i2c); + pcf->addr = addr; + + return (pcf8574_get(i2c, pcf) != EOF); +} + diff --git a/bertos/drv/pcf8574.h b/bertos/drv/pcf8574.h index 2041b480..fa327e28 100644 --- a/bertos/drv/pcf8574.h +++ b/bertos/drv/pcf8574.h @@ -43,6 +43,18 @@ #include +#include + +#if COMPILER_C99 + #define pcf8574_init(...) PP_CAT(pcf8574_init ## _, COUNT_PARMS(__VA_ARGS__)) (__VA_ARGS__) + #define pcf8574_get(...) PP_CAT(pcf8574_get ## _, COUNT_PARMS(__VA_ARGS__)) (__VA_ARGS__) + #define pcf8574_put(...) PP_CAT(pcf8574_put ## _, COUNT_PARMS(__VA_ARGS__)) (__VA_ARGS__) +#else + #define pcf8574_init(args...) PP_CAT(pcf8574_init ## _, COUNT_PARMS(args)) (args) + #define pcf8574_get(args...) PP_CAT(pcf8574_get ## _, COUNT_PARMS(args)) (args) + #define pcf8574_put(args...) PP_CAT(pcf8574_put ## _, COUNT_PARMS(args)) (args) +#endif + typedef uint8_t pcf8574_addr; /** @@ -55,8 +67,13 @@ typedef struct Pcf8574 #define PCF8574ID 0x40 ///< I2C address -int pcf8574_get(Pcf8574 *pcf); -bool pcf8574_put(Pcf8574 *pcf, uint8_t data); -bool pcf8574_init(Pcf8574 *pcf, pcf8574_addr addr); +DEPRECATED int pcf8574_get_1(Pcf8574 *pcf); +DEPRECATED bool pcf8574_put_2(Pcf8574 *pcf, uint8_t data); +DEPRECATED bool pcf8574_init_2(Pcf8574 *pcf, pcf8574_addr addr); + + +int pcf8574_get_2(I2c *i2c, Pcf8574 *pcf); +bool pcf8574_put_3(I2c *i2c, Pcf8574 *pcf, uint8_t data); +bool pcf8574_init_3(I2c *i2c, Pcf8574 *pcf, pcf8574_addr addr); #endif /* DRV_PCF8574_H */ diff --git a/bertos/drv/tas5706a.c b/bertos/drv/tas5706a.c index d7e03bb8..641dc3e7 100644 --- a/bertos/drv/tas5706a.c +++ b/bertos/drv/tas5706a.c @@ -47,10 +47,21 @@ #include #include +typedef uint8_t tas_addr_t; #define TAS_ADDR 0x36 -typedef uint8_t tas_addr_t; +#define TRIM_REG 0x1B +#define SYS_REG2 0x05 +#define VOLUME_REG 0x07 +#define MUTE_VOL 0xFF + +#define DB_TO_REG(db) ((24 - (db)) * 2) + +#define CH1_VOL_REG 0x08 +#define CH2_VOL_REG 0x09 +#define CH3_VOL_REG 0x0A +#define CH4_VOL_REG 0x0B static bool tas5706a_send(tas_addr_t addr, const void *buf, size_t len) { @@ -59,7 +70,7 @@ static bool tas5706a_send(tas_addr_t addr, const void *buf, size_t len) return ret; } -INLINE bool tas5706a_putc(tas_addr_t addr, uint8_t ch) +INLINE bool tas5706a_put(tas_addr_t addr, uint8_t ch) { return tas5706a_send(addr, &ch, sizeof(ch)); } @@ -71,7 +82,7 @@ static bool tas5706a_recv(tas_addr_t addr, void *buf, size_t len) return ret; } -INLINE int tas5706a_getc(tas_addr_t addr) +INLINE int tas5706a_get(tas_addr_t addr) { uint8_t ch; if (tas5706a_recv(addr, &ch, sizeof(ch))) @@ -80,14 +91,7 @@ INLINE int tas5706a_getc(tas_addr_t addr) return EOF; } -#define TRIM_REG 0x1B -#define SYS_REG2 0x05 -#define VOLUME_REG 0x07 -#define MUTE_VOL 0xFF - -#define DB_TO_REG(db) ((24 - (db)) * 2) - -void tas5706a_init(void) +void tas5706a_init_0(void) { MOD_CHECK(i2c); MOD_CHECK(timer); @@ -99,20 +103,65 @@ void tas5706a_init(void) timer_delay(2); TAS5706A_SETRESET(false); timer_delay(20); - tas5706a_putc(TRIM_REG, 0x00); + tas5706a_put(TRIM_REG, 0x00); - tas5706a_putc(VOLUME_REG, DB_TO_REG(CONFIG_TAS_MAX_VOL)); + tas5706a_put(VOLUME_REG, DB_TO_REG(CONFIG_TAS_MAX_VOL)); /* Unmute */ - tas5706a_putc(SYS_REG2, 0); + tas5706a_put(SYS_REG2, 0); } -#define CH1_VOL_REG 0x08 -#define CH2_VOL_REG 0x09 -#define CH3_VOL_REG 0x0A -#define CH4_VOL_REG 0x0B +void tas5706a_setVolume_2(Tas5706aCh ch, tas5706a_vol_t vol) +{ + ASSERT(ch < TAS_CNT); + ASSERT(vol <= TAS_VOL_MAX); -void tas5706a_setVolume(Tas5706aCh ch, tas5706a_vol_t vol) + tas_addr_t addr1, addr2; + + switch(ch) + { + case TAS_CH1: + addr1 = CH1_VOL_REG; + addr2 = CH3_VOL_REG; + break; + case TAS_CH2: + addr1 = CH2_VOL_REG; + addr2 = CH4_VOL_REG; + break; + default: + ASSERT(0); + return; + } + + uint8_t vol_att = 0xff - ((vol * 0xff) / TAS_VOL_MAX); + + tas5706a_put(addr1, vol_att); + tas5706a_put(addr2, vol_att); +} + +void tas5706a_setLowPower_1(bool val) +{ + TAS5706A_SETPOWERDOWN(val); + TAS5706A_SETMUTE(val); +} + +/* + * New API + */ + +INLINE bool tas5706a_putc(I2c *i2c, tas_addr_t addr, uint8_t ch) +{ + i2c_start_w(i2c, TAS_ADDR, 2, I2C_STOP); + i2c_putc(i2c, addr); + i2c_putc(i2c, ch); + + if (i2c_error(i2c)) + return false; + + return true; +} + +void tas5706a_setVolume_3(I2c *i2c, Tas5706aCh ch, tas5706a_vol_t vol) { ASSERT(ch < TAS_CNT); ASSERT(vol <= TAS_VOL_MAX); @@ -136,13 +185,36 @@ void tas5706a_setVolume(Tas5706aCh ch, tas5706a_vol_t vol) uint8_t vol_att = 0xff - ((vol * 0xff) / TAS_VOL_MAX); - tas5706a_putc(addr1, vol_att); - tas5706a_putc(addr2, vol_att); + tas5706a_putc(i2c, addr1, vol_att); + tas5706a_putc(i2c, addr2, vol_att); } -void tas5706a_setLowPower(bool val) +void tas5706a_setLowPower_2(I2c *i2c, bool val) { + ASSERT(i2c); + TAS5706A_SETPOWERDOWN(val); TAS5706A_SETMUTE(val); } + +void tas5706a_init_1(I2c *i2c) +{ + ASSERT(i2c); + MOD_CHECK(timer); + + TAS5706A_PIN_INIT(); + timer_delay(200); + TAS5706A_SETPOWERDOWN(false); + TAS5706A_SETMUTE(false); + TAS5706A_MCLK_INIT(); + timer_delay(2); + TAS5706A_SETRESET(false); + timer_delay(20); + tas5706a_putc(i2c, TRIM_REG, 0x00); + + tas5706a_putc(i2c, VOLUME_REG, DB_TO_REG(CONFIG_TAS_MAX_VOL)); + + /* Unmute */ + tas5706a_putc(i2c, SYS_REG2, 0); +} diff --git a/bertos/drv/tas5706a.h b/bertos/drv/tas5706a.h index 394c7e48..e305694e 100644 --- a/bertos/drv/tas5706a.h +++ b/bertos/drv/tas5706a.h @@ -46,6 +46,18 @@ #include +#include + +#if COMPILER_C99 + #define tas5706a_init(...) PP_CAT(tas5706a_init ## _, COUNT_PARMS(__VA_ARGS__)) (__VA_ARGS__) + #define tas5706a_setLowPower(...) PP_CAT(tas5706a_setLowPower ## _, COUNT_PARMS(__VA_ARGS__)) (__VA_ARGS__) + #define tas5706a_setVolume(...) PP_CAT(tas5706a_setVolume ## _, COUNT_PARMS(__VA_ARGS__)) (__VA_ARGS__) +#else + #define tas5706a_init(args...) PP_CAT(tas5706a_init ## _, COUNT_PARMS(args)) (args) + #define tas5706a_setLowPower(args...) PP_CAT(tas5706a_setLowPower ## _, COUNT_PARMS(args)) (args) + #define tas5706a_setVolume(args...) PP_CAT(tas5706a_setVolume ## _, COUNT_PARMS(args)) (args) +#endif + typedef enum Tas5706aCh { TAS_CH1, @@ -73,12 +85,7 @@ typedef uint8_t tas5706a_vol_t; * \param ch The channel to be controlled. * \param vol The volume you want to set. */ -void tas5706a_setVolume(Tas5706aCh ch, tas5706a_vol_t vol); - -/** - * Initialize the TAS chip. - */ -void tas5706a_init(void); +void tas5706a_setVolume_3(I2c *i2c, Tas5706aCh ch, tas5706a_vol_t vol); /** * Set TAS chip to low power mode. @@ -88,6 +95,16 @@ void tas5706a_init(void); * * \param val True if you want to enable low power mode, false otherwise. */ -void tas5706a_setLowPower(bool val); +void tas5706a_setLowPower_2(I2c *i2c, bool val); + +/** + * Initialize the TAS chip. + */ +void tas5706a_init_1(I2c *i2c); + + +DEPRECATED void tas5706a_setVolume_2(Tas5706aCh ch, tas5706a_vol_t vol); +DEPRECATED void tas5706a_setLowPower_1(bool val); +DEPRECATED void tas5706a_init_0(void); #endif /* DRV_TAS5706A_H */