Skip to content

Commit d4f2d5f

Browse files
authored
Merge pull request #51 from esp-cpp/feature/codec-i2c
feat(codec): update codec to allow use of i2c read/write functions
2 parents 9645f85 + 222b78f commit d4f2d5f

File tree

5 files changed

+65
-52
lines changed

5 files changed

+65
-52
lines changed

components/box-emu-hal/src/i2s_audio.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@
1313
#include "esp_system.h"
1414
#include "esp_check.h"
1515

16-
#include "i2c.hpp"
1716
#include "task.hpp"
1817
#include "event_manager.hpp"
1918

2019
#include "hal_events.hpp"
2120

22-
#include "es7210.h"
23-
#include "es8311.h"
21+
#include "es7210.hpp"
22+
#include "es8311.hpp"
2423

2524
#include "hal.hpp"
25+
#include "hal_i2c.hpp"
2626

2727
using namespace box_hal;
2828

@@ -256,6 +256,16 @@ void audio_init() {
256256

257257
gpio_set_level(sound_power_pin, 1);
258258

259+
set_es7210_write(std::bind(&espp::I2c::write, internal_i2c.get(),
260+
std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
261+
set_es7210_read(std::bind(&espp::I2c::read_at_register, internal_i2c.get(),
262+
std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4));
263+
264+
set_es8311_write(std::bind(&espp::I2c::write, internal_i2c.get(),
265+
std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
266+
set_es8311_read(std::bind(&espp::I2c::read_at_register, internal_i2c.get(),
267+
std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4));
268+
259269
i2s_driver_init();
260270
es7210_init_default();
261271
es8311_init_default();

components/codec/es7210/es7210.c renamed to components/codec/es7210/es7210.cpp

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,21 @@
2424

2525
#include <string.h>
2626
#include "esp_log.h"
27-
#include "es7210.h"
28-
#include "driver/i2c.h"
27+
#include "es7210.hpp"
28+
29+
static write_fn i2c_write_ = nullptr;
30+
static read_register_fn i2c_read_register_ = nullptr;
31+
32+
void set_es7210_write(write_fn fn) {
33+
i2c_write_ = fn;
34+
}
35+
void set_es7210_read(read_register_fn fn) {
36+
i2c_read_register_ = fn;
37+
}
2938

3039
#define I2S_DSP_MODE_A 0
3140
#define MCLK_DIV_FRE 256
3241

33-
#define I2C_MASTER_NUM (I2C_NUM_0)
34-
#define I2C_MASTER_TIMEOUT_MS (10)
35-
3642
/* ES7210 address*/
3743
#define ES7210_ADDR ES7210_AD1_AD0_00
3844
#define ES7210_MCLK_SOURCE FROM_CLOCK_DOUBLE_PIN /* In master mode, 0 : MCLK from pad 1 : MCLK from clock doubler */
@@ -56,8 +62,7 @@ struct _coeff_div {
5662
};
5763

5864
static const char *TAG = "ES7210";
59-
// static i2c_bus_handle_t i2c_handle;
60-
static es7210_input_mics_t mic_select = ES7210_INPUT_MIC1 | ES7210_INPUT_MIC2 | ES7210_INPUT_MIC3 | ES7210_INPUT_MIC4;
65+
static es7210_input_mics_t mic_select = (es7210_input_mics_t)(ES7210_INPUT_MIC1 | ES7210_INPUT_MIC2 | ES7210_INPUT_MIC3 | ES7210_INPUT_MIC4);
6166

6267
/* Codec hifi mclk clock divider coefficients
6368
* MEMBER REG
@@ -126,13 +131,8 @@ static const struct _coeff_div coeff_div[] = {
126131

127132
static esp_err_t es7210_write_reg(uint8_t reg_addr, uint8_t data)
128133
{
129-
//return i2c_bus_write_byte(i2c_handle, reg_addr, data);
130134
uint8_t write_buf[2] = {reg_addr, data};
131-
return i2c_master_write_to_device(I2C_MASTER_NUM,
132-
ES7210_ADDR,
133-
write_buf,
134-
sizeof(write_buf),
135-
I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS);
135+
return i2c_write_(ES7210_ADDR, write_buf, sizeof(write_buf)) == ESP_OK ? ESP_OK : ESP_FAIL;
136136
}
137137

138138
static esp_err_t es7210_update_reg_bit(uint8_t reg_addr, uint8_t update_bits, uint8_t data)
@@ -160,14 +160,7 @@ int8_t get_es7210_mclk_src(void)
160160
int es7210_read_reg(uint8_t reg_addr)
161161
{
162162
uint8_t data;
163-
// i2c_bus_read_byte(i2c_handle, reg_addr, &data);
164-
i2c_master_write_read_device(I2C_MASTER_NUM,
165-
ES7210_ADDR,
166-
&reg_addr,
167-
1, // size of addr
168-
&data,
169-
1, // amount of data to read
170-
I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS);
163+
i2c_read_register_(ES7210_ADDR, reg_addr, &data, 1);
171164
return (int)data;
172165
}
173166

@@ -278,8 +271,6 @@ esp_err_t es7210_adc_init(audio_hal_codec_config_t *codec_cfg)
278271
{
279272
esp_err_t ret = ESP_OK;
280273

281-
// ret |= bsp_i2c_add_device(&i2c_handle, ES7210_ADDR);
282-
283274
ret |= es7210_write_reg(ES7210_RESET_REG00, 0xff);
284275
ret |= es7210_write_reg(ES7210_RESET_REG00, 0x41);
285276
ret |= es7210_write_reg(ES7210_CLOCK_OFF_REG01, 0x1f);
@@ -328,7 +319,6 @@ esp_err_t es7210_adc_init(audio_hal_codec_config_t *codec_cfg)
328319

329320
esp_err_t es7210_adc_deinit()
330321
{
331-
// i2c_bus_delete(i2c_handle);
332322
return ESP_OK;
333323
}
334324

@@ -480,9 +470,9 @@ esp_err_t es7210_adc_set_gain_all(es7210_gain_value_t gain)
480470
esp_err_t ret = ESP_OK;
481471
uint32_t max_gain_vaule = 14;
482472
if (gain < 0) {
483-
gain = 0;
473+
gain = (es7210_gain_value_t)0;
484474
} else if (gain > max_gain_vaule) {
485-
gain = max_gain_vaule;
475+
gain = (es7210_gain_value_t)max_gain_vaule;
486476
}
487477
ESP_LOGD(TAG, "SET: gain:%d", gain);
488478
if (mic_select & ES7210_INPUT_MIC1) {
@@ -520,7 +510,7 @@ esp_err_t es7210_adc_get_gain(es7210_input_mics_t mic_mask, es7210_gain_value_t
520510
return regv;
521511
}
522512
gain_value = (regv & 0x0f); /* Retain the last four bits for gain */
523-
*gain = gain_value;
513+
*gain = (es7210_gain_value_t)gain_value;
524514
ESP_LOGI(TAG, "GET: gain_value:%d", gain_value);
525515
return ESP_OK;
526516
}

components/codec/es7210/es7210.h renamed to components/codec/es7210/es7210.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,16 @@
2525
#ifndef _ES7210_H
2626
#define _ES7210_H
2727

28+
#include <functional>
29+
2830
#include "audio_hal.h"
2931

32+
typedef std::function<bool(uint8_t, uint8_t*, size_t)> write_fn;
33+
typedef std::function<bool(uint8_t, uint8_t, uint8_t*, size_t)> read_register_fn;
34+
35+
void set_es7210_write(write_fn fn);
36+
void set_es7210_read(read_register_fn fn);
37+
3038
#ifdef __cplusplus
3139
extern "C" {
3240
#endif

components/codec/es8311/es8311.c renamed to components/codec/es8311/es8311.cpp

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,27 @@
2424

2525
#include <string.h>
2626
#include "esp_log.h"
27-
#include "es8311.h"
28-
#include "driver/i2c.h"
27+
#include "es8311.hpp"
28+
29+
static write_fn i2c_write_ = nullptr;
30+
static read_register_fn i2c_read_register_ = nullptr;
31+
32+
void set_es8311_write(write_fn fn) {
33+
i2c_write_ = fn;
34+
}
35+
void set_es8311_read(read_register_fn fn) {
36+
i2c_read_register_ = fn;
37+
}
38+
39+
#if !defined(BIT)
40+
#define BIT(x) (1U << (x))
41+
#endif
2942

3043
/* ES8311 address
3144
* 0x32:CE=1;0x30:CE=0
3245
*/
3346
#define ES8311_ADDR 0x18
3447

35-
#define I2C_MASTER_NUM (I2C_NUM_0)
36-
#define I2C_MASTER_TIMEOUT_MS (10)
37-
3848
/*
3949
* to define the clock soure of MCLK
4050
*/
@@ -185,24 +195,14 @@ static esp_err_t es8311_write_reg(uint8_t reg_addr, uint8_t data)
185195
{
186196
// return i2c_bus_write_byte(i2c_handle, reg_addr, data);
187197
uint8_t write_buf[2] = {reg_addr, data};
188-
return i2c_master_write_to_device(I2C_MASTER_NUM,
189-
ES8311_ADDR,
190-
write_buf,
191-
sizeof(write_buf),
192-
I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS);
198+
bool success = i2c_write_(ES8311_ADDR, write_buf, sizeof(write_buf));
199+
return success ? ESP_OK : ESP_FAIL;
193200
}
194201

195202
static int es8311_read_reg(uint8_t reg_addr)
196203
{
197204
uint8_t data;
198-
// i2c_bus_read_byte(i2c_handle, reg_addr, &data);
199-
i2c_master_write_read_device(I2C_MASTER_NUM,
200-
ES8311_ADDR,
201-
&reg_addr,
202-
1, // size of addr
203-
&data,
204-
1, // amount of data to read
205-
I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS);
205+
i2c_read_register_(ES8311_ADDR, reg_addr, &data, 1);
206206
return (int)data;
207207
}
208208

@@ -261,8 +261,6 @@ esp_err_t es8311_codec_init(audio_hal_codec_config_t *codec_cfg)
261261
int coeff;
262262
esp_err_t ret = ESP_OK;
263263

264-
// ret |= bsp_i2c_add_device(&i2c_handle, ES8311_ADDR);
265-
266264
ret |= es8311_write_reg(ES8311_CLK_MANAGER_REG01, 0x30);
267265
ret |= es8311_write_reg(ES8311_CLK_MANAGER_REG02, 0x00);
268266
ret |= es8311_write_reg(ES8311_CLK_MANAGER_REG03, 0x10);
@@ -450,7 +448,6 @@ esp_err_t es8311_codec_init(audio_hal_codec_config_t *codec_cfg)
450448

451449
esp_err_t es8311_codec_deinit()
452450
{
453-
// i2c_bus_delete(i2c_handle);
454451
return ESP_OK;
455452
}
456453

@@ -525,7 +522,7 @@ esp_err_t es8311_codec_config_i2s(audio_hal_codec_mode_t mode, audio_hal_codec_i
525522
{
526523
int ret = ESP_OK;
527524
ret |= es8311_set_bits_per_sample(iface->bits);
528-
ret |= es8311_config_fmt(iface->fmt);
525+
ret |= es8311_config_fmt((es_i2s_fmt_t)(iface->fmt));
529526
return ret;
530527
}
531528

components/codec/es8311/es8311.h renamed to components/codec/es8311/es8311.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@
2525
#ifndef _ES8311_H
2626
#define _ES8311_H
2727

28+
#include <functional>
29+
30+
typedef std::function<bool(uint8_t, uint8_t*, size_t)> write_fn;
31+
typedef std::function<bool(uint8_t, uint8_t, uint8_t*, size_t)> read_register_fn;
32+
33+
void set_es8311_write(write_fn fn);
34+
void set_es8311_read(read_register_fn fn);
35+
2836
#include "audio_hal.h"
2937
#include "esp_types.h"
3038
#include "esxxx_common.h"

0 commit comments

Comments
 (0)