sam3n-ek example: fix led macros
[bertos.git] / bertos / drv / tas5706a.c
index 641dc3e7b21b61904b99b6d3cc048f7aed852abc..a1ea044ef72d940f3b04ca5419924f06cdb98a0c 100644 (file)
@@ -34,6 +34,7 @@
  *
  *
  * \author Francesco Sacchi <batt@develer.com>
+ * \author Daniele Basile <asterix@develer.com>
  */
 
 #include "tas5706a.h"
@@ -41,6 +42,7 @@
 #include "hw/hw_tas5706a.h"
 
 #include "cfg/cfg_tas5706a.h"
+#include "cfg/cfg_i2c.h"
 
 #include <cfg/module.h>
 
@@ -63,102 +65,32 @@ typedef uint8_t tas_addr_t;
 #define CH3_VOL_REG 0x0A
 #define CH4_VOL_REG 0x0B
 
-static bool tas5706a_send(tas_addr_t addr, const void *buf, size_t len)
-{
-       bool ret = i2c_start_w(TAS_ADDR) && i2c_put(addr) && i2c_send(buf, len);
-       i2c_stop();
-       return ret;
-}
-
-INLINE bool tas5706a_put(tas_addr_t addr, uint8_t ch)
-{
-       return tas5706a_send(addr, &ch, sizeof(ch));
-}
 
-static bool tas5706a_recv(tas_addr_t addr, void *buf, size_t len)
-{
-       bool ret = i2c_start_w(TAS_ADDR) && i2c_put(addr) && i2c_start_r(TAS_ADDR) && i2c_recv(buf, len);
-       i2c_stop();
-       return ret;
-}
-
-INLINE int tas5706a_get(tas_addr_t addr)
-{
-       uint8_t ch;
-       if (tas5706a_recv(addr, &ch, sizeof(ch)))
-               return (int)(uint8_t)ch;
-       else
-               return EOF;
-}
-
-void tas5706a_init_0(void)
-{
-       MOD_CHECK(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_put(TRIM_REG, 0x00);
-
-       tas5706a_put(VOLUME_REG, DB_TO_REG(CONFIG_TAS_MAX_VOL));
-
-       /* Unmute */
-       tas5706a_put(SYS_REG2, 0);
-}
-
-void tas5706a_setVolume_2(Tas5706aCh ch, tas5706a_vol_t vol)
+INLINE bool tas5706a_putc(I2c *i2c, tas_addr_t addr, uint8_t ch)
 {
-       ASSERT(ch < TAS_CNT);
-       ASSERT(vol <= TAS_VOL_MAX);
-
-       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;
-       }
+       i2c_start_w(i2c, TAS_ADDR, 2, I2C_STOP);
+       i2c_putc(i2c, addr);
+       i2c_putc(i2c, ch);
 
-       uint8_t vol_att = 0xff - ((vol * 0xff) / TAS_VOL_MAX);
+       if (i2c_error(i2c))
+               return false;
 
-       tas5706a_put(addr1, vol_att);
-       tas5706a_put(addr2, vol_att);
+       return true;
 }
 
-void tas5706a_setLowPower_1(bool val)
+INLINE int tas5706a_getc(I2c *i2c, tas_addr_t addr)
 {
-       TAS5706A_SETPOWERDOWN(val);
-       TAS5706A_SETMUTE(val);
-}
-
-/*
- * New API
- */
+       int ch;
 
-INLINE bool tas5706a_putc(I2c *i2c, tas_addr_t addr, uint8_t ch)
-{
-       i2c_start_w(i2c, TAS_ADDR, 2, I2C_STOP);
+       i2c_start_w(i2c, TAS_ADDR, 2, I2C_NOSTOP);
        i2c_putc(i2c, addr);
-       i2c_putc(i2c, ch);
+       i2c_start_r(i2c, TAS_ADDR, 1, I2C_STOP);
+       ch = (int)(uint8_t)i2c_getc(i2c);
 
        if (i2c_error(i2c))
-               return false;
+               return EOF;
 
-       return true;
+       return ch;
 }
 
 void tas5706a_setVolume_3(I2c *i2c, Tas5706aCh ch, tas5706a_vol_t vol)