37
37
static i2c_bus_handle_t i2c_handle = NULL ;
38
38
static int es7243_addr = 0x13 ; // 0x26>>1;
39
39
static int mclk_gpio = 0 ;
40
+ static int actual_volume = 0 ;
41
+ static uint8_t audio_format = 0x0C ;
40
42
41
43
void es7243_mclk_gpio (int gpio ){
42
44
mclk_gpio = gpio ;
@@ -57,13 +59,12 @@ error_t es7243_adc_set_addr(int addr)
57
59
58
60
static error_t es7243_mclk_active (uint8_t mclk_gpio )
59
61
{
60
- #ifndef ARDUINO_ARCH_NRF52840
61
- pinMode (mclk_gpio , OUTPUT );
62
- #endif /*
62
+ /*
63
63
Before initializing es7243, it is necessary to output
64
64
mclk to es7243 to activate the I2C configuration.
65
65
So give some clocks to active es7243.
66
66
*/
67
+ pinMode (mclk_gpio , OUTPUT );
67
68
for (int i = 0 ; i < MCLK_PULSES_NUMBER ; ++ i ) {
68
69
digitalWrite (mclk_gpio , 0 );
69
70
delay (1 );
@@ -78,12 +79,12 @@ error_t es7243_adc_init(codec_config_t *codec_cfg, i2c_bus_handle_t handle)
78
79
error_t ret = RESULT_OK ;
79
80
i2c_handle = handle ;
80
81
es7243_mclk_active (mclk_gpio );
81
- ret |= es7243_write_reg (0x00 , 0x01 );
82
+ ret |= es7243_write_reg (0x00 , 0x01 ); // slave mode, software mode
82
83
ret |= es7243_write_reg (0x06 , 0x00 );
83
- ret |= es7243_write_reg (0x05 , 0x1B );
84
- ret |= es7243_write_reg (0x01 , 0x0C );
85
- ret |= es7243_write_reg (0x08 , 0x43 );
86
- ret |= es7243_write_reg (0x05 , 0x13 );
84
+ ret |= es7243_write_reg (0x05 , 0x1B ); // Mute ADC
85
+ ret |= es7243_write_reg (0x01 , audio_format ); // i2s -16bit
86
+ ret |= es7243_write_reg (0x08 , 0x43 ); // enable AIN, PGA GAIN = 27DB
87
+ ret |= es7243_write_reg (0x05 , 0x13 ); // un Mute ADC
87
88
if (ret ) {
88
89
AD_LOGE ( "Es7243 initialize failed!" );
89
90
return RESULT_FAIL ;
@@ -93,17 +94,53 @@ error_t es7243_adc_init(codec_config_t *codec_cfg, i2c_bus_handle_t handle)
93
94
94
95
error_t es7243_adc_deinit (void )
95
96
{
96
- return RESULT_OK ;
97
+ return es7243_adc_set_voice_mute (true) ;
97
98
}
98
99
99
100
error_t es7243_adc_ctrl_state_active (codec_mode_t mode , bool ctrl_state_active )
100
101
{
101
- return RESULT_OK ;
102
+ return es7243_adc_set_voice_mute (! ctrl_state_active ) ;
102
103
}
103
104
104
105
error_t es7243_adc_config_i2s (codec_mode_t mode , I2SDefinition * iface )
105
106
{
106
- return RESULT_OK ;
107
+ // master mode not supported
108
+ if (iface -> mode == MODE_MASTER ){
109
+ return RESULT_FAIL ;
110
+ }
111
+
112
+ // set bits
113
+ switch (iface -> bits ){
114
+ case 16 :
115
+ audio_format |= 0b011 < 2 ;
116
+ break ;
117
+ case 24 :
118
+ audio_format |= 0b000 < 2 ;
119
+ break ;
120
+ case 32 :
121
+ audio_format |= 0b100 < 2 ;
122
+ break ;
123
+ default :
124
+ return RESULT_FAIL ;
125
+ }
126
+
127
+ switch (iface -> fmt ){
128
+ case I2S_RIGHT :
129
+ case I2S_NORMAL :
130
+ audio_format |= 0b00 ;
131
+ break ;
132
+ case I2S_LEFT :
133
+ audio_format |= 0b01 ;
134
+ break ;
135
+ case I2S_DSP :
136
+ audio_format |= 0b11 ;
137
+ break ;
138
+ default :
139
+ return RESULT_FAIL ;
140
+ }
141
+
142
+ error_t ret = es7243_write_reg (0x01 , audio_format );
143
+ return ret ;
107
144
}
108
145
109
146
error_t es7243_adc_set_voice_mute (bool mute )
@@ -126,6 +163,7 @@ error_t es7243_adc_set_voice_volume(int volume)
126
163
if (volume < 0 ) {
127
164
volume = 0 ;
128
165
}
166
+ actual_volume = volume ;
129
167
switch (volume ) {
130
168
case 0 ... 12 :
131
169
ret |= es7243_write_reg (0x08 , 0x11 ); // 1db
@@ -159,5 +197,6 @@ error_t es7243_adc_set_voice_volume(int volume)
159
197
160
198
error_t es7243_adc_get_voice_volume (int * volume )
161
199
{
200
+ * volume = actual_volume ;
162
201
return RESULT_OK ;
163
202
}
0 commit comments