BeRTOS
tas5706a.c
Go to the documentation of this file.
00001 
00040 #include "tas5706a.h"
00041 
00042 #include "hw/hw_tas5706a.h"
00043 
00044 #include "cfg/cfg_tas5706a.h"
00045 #include "cfg/cfg_i2c.h"
00046 
00047 #include <cfg/module.h>
00048 
00049 #include <drv/i2c.h>
00050 #include <drv/timer.h>
00051 
00052 typedef uint8_t tas_addr_t;
00053 
00054 #define TAS_ADDR 0x36
00055 
00056 #define TRIM_REG   0x1B
00057 #define SYS_REG2   0x05
00058 #define VOLUME_REG 0x07
00059 #define MUTE_VOL 0xFF
00060 
00061 #define DB_TO_REG(db) ((24 - (db)) * 2)
00062 
00063 #define CH1_VOL_REG 0x08
00064 #define CH2_VOL_REG 0x09
00065 #define CH3_VOL_REG 0x0A
00066 #define CH4_VOL_REG 0x0B
00067 
00068 
00069 INLINE bool tas5706a_putc(I2c *i2c, tas_addr_t addr, uint8_t ch)
00070 {
00071     i2c_start_w(i2c, TAS_ADDR, 2, I2C_STOP);
00072     i2c_putc(i2c, addr);
00073     i2c_putc(i2c, ch);
00074 
00075     if (i2c_error(i2c))
00076         return false;
00077 
00078     return true;
00079 }
00080 
00081 INLINE int tas5706a_getc(I2c *i2c, tas_addr_t addr)
00082 {
00083     int ch;
00084 
00085     i2c_start_w(i2c, TAS_ADDR, 2, I2C_NOSTOP);
00086     i2c_putc(i2c, addr);
00087     i2c_start_r(i2c, TAS_ADDR, 1, I2C_STOP);
00088     ch = (int)(uint8_t)i2c_getc(i2c);
00089 
00090     if (i2c_error(i2c))
00091         return EOF;
00092 
00093     return ch;
00094 }
00095 
00096 void tas5706a_setVolume_3(I2c *i2c, Tas5706aCh ch, tas5706a_vol_t vol)
00097 {
00098     ASSERT(ch < TAS_CNT);
00099     ASSERT(vol <= TAS_VOL_MAX);
00100 
00101     tas_addr_t addr1, addr2;
00102 
00103     switch(ch)
00104     {
00105         case TAS_CH1:
00106             addr1 = CH1_VOL_REG;
00107             addr2 = CH3_VOL_REG;
00108             break;
00109         case TAS_CH2:
00110             addr1 = CH2_VOL_REG;
00111             addr2 = CH4_VOL_REG;
00112             break;
00113         default:
00114             ASSERT(0);
00115             return;
00116     }
00117 
00118     uint8_t vol_att = 0xff - ((vol * 0xff) / TAS_VOL_MAX);
00119 
00120     tas5706a_putc(i2c, addr1, vol_att);
00121     tas5706a_putc(i2c, addr2, vol_att);
00122 }
00123 
00124 void tas5706a_setLowPower_2(I2c *i2c, bool val)
00125 {
00126     ASSERT(i2c);
00127 
00128     TAS5706A_SETPOWERDOWN(val);
00129     TAS5706A_SETMUTE(val);
00130 }
00131 
00132 
00133 void tas5706a_init_1(I2c *i2c)
00134 {
00135     ASSERT(i2c);
00136     MOD_CHECK(timer);
00137 
00138     TAS5706A_PIN_INIT();
00139     timer_delay(200);
00140     TAS5706A_SETPOWERDOWN(false);
00141     TAS5706A_SETMUTE(false);
00142     TAS5706A_MCLK_INIT();
00143     timer_delay(2);
00144     TAS5706A_SETRESET(false);
00145     timer_delay(20);
00146     tas5706a_putc(i2c, TRIM_REG, 0x00);
00147 
00148     tas5706a_putc(i2c, VOLUME_REG, DB_TO_REG(CONFIG_TAS_MAX_VOL));
00149 
00150     /* Unmute */
00151     tas5706a_putc(i2c, SYS_REG2, 0);
00152 }