arduino-audio-driver
Loading...
Searching...
No Matches
TAS5805M.h
Go to the documentation of this file.
1/*
2 * ESPRESSIF MIT License
3 *
4 * Copyright (c) 2020 <ESPRESSIF SYSTEMS (SHANGHAI) CO., LTD>
5 *
6 * Permission is hereby granted for use on all ESPRESSIF SYSTEMS products, in which case,
7 * it is free of charge, to any person obtaining a copy of this software and associated
8 * documentation files (the "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the Software is furnished
11 * to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all copies or
14 * substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 *
23 */
24
25#pragma once
26
27#include "stdbool.h"
28#include "DriverCommon.h"
30#include "Platforms/API_I2C.h"
31#include "Platforms/API_Delay.h"
32#include "Platforms/GPIO.h"
33#include "tas5805m_reg_cfg.h"
34#include <stdint.h>
35
36
37namespace audio_driver {
38
44class TAS5805M {
45 public:
46 static constexpr uint8_t TAS5805M_REG_00 = 0x00;
47 static constexpr uint8_t TAS5805M_REG_02 = 0x02;
48 static constexpr uint8_t TAS5805M_REG_03 = 0x03;
49 static constexpr uint8_t TAS5805M_REG_24 = 0x24;
50 static constexpr uint8_t TAS5805M_REG_25 = 0x25;
51 static constexpr uint8_t TAS5805M_REG_26 = 0x26;
52 static constexpr uint8_t TAS5805M_REG_27 = 0x27;
53 static constexpr uint8_t TAS5805M_REG_28 = 0x28;
54 static constexpr uint8_t TAS5805M_REG_29 = 0x29;
55 static constexpr uint8_t TAS5805M_REG_2A = 0x2a;
56 static constexpr uint8_t TAS5805M_REG_2B = 0x2b;
57 static constexpr uint8_t TAS5805M_REG_35 = 0x35;
58 static constexpr uint8_t TAS5805M_REG_7E = 0x7e;
59 static constexpr uint8_t TAS5805M_REG_7F = 0x7f;
60 static constexpr uint8_t TAS5805M_PAGE_00 = 0x00;
61 static constexpr uint8_t TAS5805M_PAGE_2A = 0x2a;
62 static constexpr uint8_t TAS5805M_BOOK_00 = 0x00;
63 static constexpr uint8_t TAS5805M_BOOK_8C = 0x8c;
64 static constexpr uint8_t MASTER_VOL_REG_ADDR = 0x4C;
65 static constexpr uint8_t MUTE_TIME_REG_ADDR = 0x51;
66 static constexpr uint8_t TAS5805M_DAMP_MODE_BTL = 0x0;
67 static constexpr uint8_t TAS5805M_DAMP_MODE_PBTL = 0x04;
68 static constexpr int TAS5805M_ADDR = 0x2E;
69 static constexpr int TAS5805M_VOLUME_MAX = 100;
70 static constexpr int TAS5805M_VOLUME_MIN = 0;
71 TAS5805M() = default;
72
74 void setWire(i2c_bus_handle_t handle) { i2c_handle = handle; }
75
77 void setAddress(int addr) {
78 if (addr > 0) i2c_addr = addr;
79 }
80
82 void setPowerPin(GpioPin pin) { power_pin = pin; }
83
86 GPIO gpio;
87 error_t ret = RESULT_OK;
88 AD_LOGI("Power ON CODEC with GPIO %d", power_pin);
90 gpio.digitalWrite(power_pin, 0);
91 delayMs(20);
92 gpio.digitalWrite(power_pin, 1);
93 delayMs(20);
94
95 ret |= transmitRegisters(
97 sizeof(tas5805m_registers) / sizeof(tas5805m_registers[0]));
98
99 if (ret != 0) { AD_LOGE("Fail to iniitialize tas5805m PA"); return RESULT_FAIL; }
100 return ret;
101 }
102
105 // TODO
106 return RESULT_OK;
107 }
108
111 int vol_idx = 0;
112
113 if (vol < TAS5805M_VOLUME_MIN) {
115 }
116 if (vol > TAS5805M_VOLUME_MAX) {
118 }
119 vol_idx = vol / 5;
120
121 uint8_t cmd[2] = {0, 0};
122 error_t ret = RESULT_OK;
123
124 cmd[0] = MASTER_VOL_REG_ADDR;
125 cmd[1] = tas5805m_volume[vol_idx];
126 ret = i2c_bus_write_bytes(i2c_handle, i2c_addr, &cmd[0], 1, &cmd[1], 1);
127 AD_LOGW("volume = 0x%x", cmd[1]);
128 return ret;
129 }
130
132 error_t getVolume(int* value) {
134 uint8_t cmd[2] = {MASTER_VOL_REG_ADDR, 0x00};
135 error_t ret =
136 i2c_bus_read_bytes(i2c_handle, i2c_addr, &cmd[0], 1, &cmd[1], 1);
137 if (ret != 0) { AD_LOGE("Fail to get volume"); return RESULT_FAIL; }
138 unsigned i;
139 for (i = 0; i < sizeof(tas5805m_volume); i++) {
140 if (cmd[1] >= tas5805m_volume[i]) break;
141 }
142 AD_LOGI("Volume is %d", i * 5);
143 *value = 5 * i;
144 return ret;
145 }
146
152 error_t setMute(bool enable) {
153 error_t ret = RESULT_OK;
154 uint8_t cmd[2] = {TAS5805M_REG_03, 0x00};
155 ret |= i2c_bus_read_bytes(i2c_handle, i2c_addr, &cmd[0], 1, &cmd[1], 1);
156
157 if (enable) {
158 cmd[1] |= 0x8;
159 } else {
160 cmd[1] &= (~0x08);
161 }
162 ret |= i2c_bus_write_bytes(i2c_handle, i2c_addr, &cmd[0], 1, &cmd[1], 1);
163
164 if (ret != 0) { AD_LOGE("Fail to set mute"); return RESULT_FAIL; }
165 return ret;
166 }
167
172 error_t setMuteFade(int value) {
173 error_t ret = 0;
174 unsigned char cmd[2] = {MUTE_TIME_REG_ADDR, 0x00};
175 /* Time for register value
176 * 000: 11.5 ms
177 * 001: 53 ms
178 * 010: 106.5 ms
179 * 011: 266.5 ms
180 * 100: 0.535 sec
181 * 101: 1.065 sec
182 * 110: 2.665 sec
183 * 111: 5.33 sec
184 */
185 if (value <= 12) {
186 cmd[1] = 0;
187 } else if (value <= 53) {
188 cmd[1] = 1;
189 } else if (value <= 107) {
190 cmd[1] = 2;
191 } else if (value <= 267) {
192 cmd[1] = 3;
193 } else if (value <= 535) {
194 cmd[1] = 4;
195 } else if (value <= 1065) {
196 cmd[1] = 5;
197 } else if (value <= 2665) {
198 cmd[1] = 6;
199 } else {
200 cmd[1] = 7;
201 }
202 cmd[1] |= (cmd[1] << 4);
203
204 ret |= i2c_bus_write_bytes(i2c_handle, i2c_addr, &cmd[0], 1, &cmd[1], 1);
205 if (ret != 0) { AD_LOGE("Fail to set mute fade"); return RESULT_FAIL; }
206 AD_LOGI("Set mute fade, value:%d, 0x%x", value, cmd[1]);
207 return ret;
208 }
209
211 error_t getMute(int* value) {
212 error_t ret = RESULT_OK;
213 uint8_t cmd[2] = {TAS5805M_REG_03, 0x00};
214 ret |= i2c_bus_read_bytes(i2c_handle, i2c_addr, &cmd[0], 1, &cmd[1], 1);
215
216 if (ret != 0) { AD_LOGE("Fail to get mute"); return RESULT_FAIL; }
217 *value = (cmd[1] & 0x08) >> 3;
218 AD_LOGI("Get mute value: 0x%x", *value);
219 return ret;
220 }
221
226 error_t setDampMode(int value) {
227 unsigned char cmd[2] = {0};
228 cmd[0] = TAS5805M_REG_02;
229 cmd[1] = 0x10 | value;
230 return i2c_bus_write_bytes(i2c_handle, i2c_addr, &cmd[0], 1, &cmd[1], 1);
231 }
232
234 error_t ctrlStateActive(codec_mode_t mode, bool ctrl_state_active) {
235 // TODO
236 return RESULT_OK;
237 }
238
241 // TODO
242 return RESULT_OK;
243 }
244
245 error_t transmitRegisters(const tas5805m_cfg_reg_t* conf_buf, int size) {
246 int i = 0;
247 error_t ret = RESULT_OK;
248 while (i < size) {
249 switch (conf_buf[i].offset) {
250 case CFG_META_SWITCH:
251 // Used in legacy applications. Ignored here.
252 break;
253 case CFG_META_DELAY:
254 delayMs(conf_buf[i].value);
255 break;
256 case CFG_META_BURST:
257 ret = i2c_bus_write_bytes(i2c_handle, i2c_addr,
258 (unsigned char*)(&conf_buf[i + 1].offset), 1,
259 (unsigned char*)(&conf_buf[i + 1].value),
260 conf_buf[i].value);
261 i += (conf_buf[i].value / 2) + 1;
262 break;
263 case CFG_END_1:
264 if (CFG_END_2 == conf_buf[i + 1].offset &&
265 CFG_END_3 == conf_buf[i + 2].offset) {
266 AD_LOGI("End of tms5805m reg: %d", i);
267 }
268 break;
269 default:
270 ret = i2c_bus_write_bytes(i2c_handle, i2c_addr,
271 (unsigned char*)(&conf_buf[i].offset), 1,
272 (unsigned char*)(&conf_buf[i].value), 1);
273 break;
274 }
275 i++;
276 }
277 if (ret != RESULT_OK) {
278 AD_LOGE("Fail to load configuration to tas5805m");
279 return RESULT_FAIL;
280 }
281 AD_LOGI("%s: write %d reg done", __FUNCTION__, i);
282 return ret;
283 }
284
285 protected:
289};
290
291} // namespace audio_driver
#define RESULT_OK
Definition DriverCommon.h:74
#define OUTPUT
Definition DriverCommon.h:44
#define RESULT_FAIL
Definition DriverCommon.h:75
Simple GPIO abstraction for digital input/output.
#define AD_LOGI(...)
Definition Logger.h:13
#define AD_LOGE(...)
Definition Logger.h:15
#define AD_LOGW(...)
Definition Logger.h:14
Definition GPIO_Zephyr.h:20
bool digitalWrite(GpioPin pin, bool value)
Definition GPIO_Zephyr.h:59
void pinMode(GpioPin pin, int mode)
Definition GPIO_Zephyr.h:28
Header-only driver class for the TAS5805M amplifier chip.
Definition TAS5805M.h:44
static constexpr uint8_t TAS5805M_REG_29
Definition TAS5805M.h:54
static constexpr int TAS5805M_VOLUME_MAX
Definition TAS5805M.h:69
error_t ctrlStateActive(codec_mode_t mode, bool ctrl_state_active)
Control TAS5805 codec chip.
Definition TAS5805M.h:234
static constexpr uint8_t TAS5805M_REG_02
Definition TAS5805M.h:47
static constexpr uint8_t TAS5805M_REG_7E
Definition TAS5805M.h:58
void setAddress(int addr)
Defines the I2C device address.
Definition TAS5805M.h:77
error_t deinit(void)
Deinitialize TAS5805 codec chip.
Definition TAS5805M.h:104
static constexpr uint8_t TAS5805M_REG_7F
Definition TAS5805M.h:59
static constexpr uint8_t TAS5805M_REG_00
Definition TAS5805M.h:46
static constexpr uint8_t TAS5805M_REG_27
Definition TAS5805M.h:52
error_t setMuteFade(int value)
Mute gradually by (value)ms.
Definition TAS5805M.h:172
error_t setDampMode(int value)
Set DAMP mode.
Definition TAS5805M.h:226
static constexpr uint8_t TAS5805M_DAMP_MODE_BTL
Definition TAS5805M.h:66
static constexpr int TAS5805M_ADDR
Definition TAS5805M.h:68
static constexpr uint8_t TAS5805M_PAGE_2A
Definition TAS5805M.h:61
static constexpr uint8_t TAS5805M_REG_26
Definition TAS5805M.h:51
int i2c_addr
Definition TAS5805M.h:287
static constexpr uint8_t TAS5805M_DAMP_MODE_PBTL
Definition TAS5805M.h:67
error_t configI2S(codec_mode_t mode, I2SDefinition *iface)
Configure TAS5805 codec mode and I2S interface.
Definition TAS5805M.h:240
static constexpr uint8_t MUTE_TIME_REG_ADDR
Definition TAS5805M.h:65
static constexpr uint8_t TAS5805M_REG_03
Definition TAS5805M.h:48
error_t setMute(bool enable)
Set TAS5805 mute or not Continuously call should have an interval time determined by setMuteFade()
Definition TAS5805M.h:152
static constexpr uint8_t TAS5805M_PAGE_00
Definition TAS5805M.h:60
void setPowerPin(GpioPin pin)
Defines the GPIO pin used to power on the amplifier.
Definition TAS5805M.h:82
static constexpr uint8_t MASTER_VOL_REG_ADDR
Definition TAS5805M.h:64
void setWire(i2c_bus_handle_t handle)
Defines the I2C bus instance to be used.
Definition TAS5805M.h:74
static constexpr int TAS5805M_VOLUME_MIN
Definition TAS5805M.h:70
static constexpr uint8_t TAS5805M_REG_24
Definition TAS5805M.h:49
static constexpr uint8_t TAS5805M_REG_35
Definition TAS5805M.h:57
GpioPin power_pin
Definition TAS5805M.h:288
i2c_bus_handle_t i2c_handle
Definition TAS5805M.h:286
static constexpr uint8_t TAS5805M_REG_25
Definition TAS5805M.h:50
error_t getVolume(int *value)
Get voice volume (0~100)
Definition TAS5805M.h:132
error_t transmitRegisters(const tas5805m_cfg_reg_t *conf_buf, int size)
Definition TAS5805M.h:245
static constexpr uint8_t TAS5805M_REG_28
Definition TAS5805M.h:53
static constexpr uint8_t TAS5805M_REG_2A
Definition TAS5805M.h:55
static constexpr uint8_t TAS5805M_REG_2B
Definition TAS5805M.h:56
static constexpr uint8_t TAS5805M_BOOK_00
Definition TAS5805M.h:62
error_t getMute(int *value)
Get TAS5805 mute status.
Definition TAS5805M.h:211
error_t setVolume(int vol)
Set voice volume (0~100)
Definition TAS5805M.h:110
static constexpr uint8_t TAS5805M_BOOK_8C
Definition TAS5805M.h:63
error_t init(codec_config_t *codec_cfg)
Initialize TAS5805 codec chip.
Definition TAS5805M.h:85
Namespace for audio driver components.
Definition AudioBoard.h:5
static constexpr uint8_t CFG_META_SWITCH
Definition tas5805m_reg_cfg.h:31
static constexpr uint8_t CFG_END_2
Definition tas5805m_reg_cfg.h:35
static const uint8_t tas5805m_volume[]
Definition tas5805m_reg_cfg.h:43
static const tas5805m_cfg_reg_t tas5805m_registers[]
Definition tas5805m_reg_cfg.h:47
static constexpr uint8_t CFG_END_1
Definition tas5805m_reg_cfg.h:34
int16_t GpioPin
Definition DriverCommon.h:153
static constexpr uint8_t CFG_META_BURST
Definition tas5805m_reg_cfg.h:33
void delayMs(unsigned long ms)
Definition API_Delay.h:19
codec_mode_t
Definition DriverCommon.h:290
int error_t
Definition DriverCommon.h:150
static constexpr uint8_t CFG_META_DELAY
Definition tas5805m_reg_cfg.h:32
static constexpr uint8_t CFG_END_3
Definition tas5805m_reg_cfg.h:36
void * i2c_bus_handle_t
Definition DriverCommon.h:155
I2S interface parameters for the audio codec.
Definition DriverCommon.h:384
Top-level codec configuration passed to the audio driver at init.
Definition DriverCommon.h:397
Definition tas5805m_reg_cfg.h:38
uint8_t value
Definition tas5805m_reg_cfg.h:40