Skip to content

Commit f41ab00

Browse files
committed
DRAFT support for PCM3168
1 parent d040884 commit f41ab00

File tree

3 files changed

+307
-36
lines changed

3 files changed

+307
-36
lines changed

src/Driver.h

Lines changed: 104 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11

22
#pragma once
3-
#include "DriverCommon.h"
43
#include "Driver/ac101/ac101.h"
54
#include "Driver/ad1938/ad1938.h"
65
#include "Driver/cs42448/cs42448.h"
@@ -12,10 +11,12 @@
1211
#include "Driver/es8311/es8311.h"
1312
#include "Driver/es8374/es8374.h"
1413
#include "Driver/es8388/es8388.h"
14+
#include "Driver/pcm3168/pcm3168.h"
1515
#include "Driver/tas5805m/tas5805m.h"
1616
#include "Driver/wm8960/mtb_wm8960.h"
1717
#include "Driver/wm8978/WM8978.h"
1818
#include "Driver/wm8994/wm8994.h"
19+
#include "DriverCommon.h"
1920
#include "DriverPins.h"
2021

2122
namespace audio_driver {
@@ -120,7 +121,7 @@ class CodecConfig : public codec_config_t {
120121
i2s.channels = (channels_t)channels;
121122
return true;
122123
default:
123-
AD_LOGE("Channels not supported: %d - using %d", channels,2);
124+
AD_LOGE("Channels not supported: %d - using %d", channels, 2);
124125
i2s.channels = CHANNELS2;
125126
return false;
126127
}
@@ -264,7 +265,7 @@ class AudioDriver {
264265
}
265266

266267
/// Gets the number of I2S Interfaces
267-
virtual int getI2SCount() { return 1;}
268+
virtual int getI2SCount() { return 1; }
268269

269270
protected:
270271
CodecConfig codec_cfg;
@@ -674,7 +675,8 @@ class AudioDriverES7243eClass : public AudioDriver {
674675
}
675676
bool setVolume(int volume) {
676677
this->volume = volume;
677-
return es7243e_adc_set_voice_volume(limitValue(volume, 0, 100)) == RESULT_OK;
678+
return es7243e_adc_set_voice_volume(limitValue(volume, 0, 100)) ==
679+
RESULT_OK;
678680
}
679681
int getVolume() {
680682
int vol;
@@ -710,7 +712,8 @@ class AudioDriverES8156Class : public AudioDriver {
710712
}
711713
bool setVolume(int volume) {
712714
AD_LOGD("volume %d", volume);
713-
return es8156_codec_set_voice_volume(limitValue(volume, 0, 100)) == RESULT_OK;
715+
return es8156_codec_set_voice_volume(limitValue(volume, 0, 100)) ==
716+
RESULT_OK;
714717
}
715718
int getVolume() {
716719
int vol;
@@ -742,7 +745,8 @@ class AudioDriverES8311Class : public AudioDriver {
742745
AudioDriverES8311Class(int i2cAddr = 0) { i2c_address = i2cAddr; }
743746
bool setMute(bool mute) { return es8311_set_voice_mute(mute) == RESULT_OK; }
744747
bool setVolume(int volume) {
745-
return es8311_codec_set_voice_volume(limitValue(volume, 0, 100)) == RESULT_OK;
748+
return es8311_codec_set_voice_volume(limitValue(volume, 0, 100)) ==
749+
RESULT_OK;
746750
}
747751
int getVolume() {
748752
int vol;
@@ -756,8 +760,10 @@ class AudioDriverES8311Class : public AudioDriver {
756760
bool init(codec_config_t codec_cfg) {
757761
int mclk_src = pins().getPinID(PinFunction::MCLK_SOURCE);
758762
if (mclk_src == -1) {
759-
AD_LOGI("Pin for PinFunction::MCLK_SOURCE not defined: we assume FROM_MCLK_PIN");
760-
mclk_src = 0; // = FROM_MCLK_PIN;
763+
AD_LOGI(
764+
"Pin for PinFunction::MCLK_SOURCE not defined: we assume "
765+
"FROM_MCLK_PIN");
766+
mclk_src = 0; // = FROM_MCLK_PIN;
761767
}
762768

763769
// determine address from data
@@ -787,7 +793,8 @@ class AudioDriverES8374Class : public AudioDriver {
787793
bool setMute(bool mute) { return es8374_set_voice_mute(mute) == RESULT_OK; }
788794
bool setVolume(int volume) {
789795
AD_LOGD("volume %d", volume);
790-
return es8374_codec_set_voice_volume(limitValue(volume, 0, 100)) == RESULT_OK;
796+
return es8374_codec_set_voice_volume(limitValue(volume, 0, 100)) ==
797+
RESULT_OK;
791798
}
792799
int getVolume() {
793800
int vol;
@@ -860,25 +867,26 @@ class AudioDriverES8388Class : public AudioDriver {
860867
bool setInputVolume(int volume) {
861868
// map values from 0 - 100 to 0 to 8
862869

863-
// es_mic_gain_t: MIC_GAIN_MIN = -1, 0,3,6,9,12,15,18,21,24 MIC_GAIN_MAX = 25
870+
// es_mic_gain_t: MIC_GAIN_MIN = -1, 0,3,6,9,12,15,18,21,24 MIC_GAIN_MAX =
871+
// 25
864872

865873
// Vol: 0, 12.5, 25, 37.5, 50, 62.5, 75, 87.5, 100
866874
// idx: 0, 1, 2, 3, 4, 5, 6, 7, 8
867875
// dB/gain: 0, 3, 6, 9, 12, 15, 18, 21, 24
868876
// factor: 1, 2, 4, 8, 16, 32, 63, 126, 252
869877

870878
// es8388 Register 9 – ADC Control 1
871-
//dB MicL MicR
879+
// dB MicL MicR
872880
// 0 0000 0000
873881
// 3 0001 0001
874882
// 6 0010 0010
875883
// 9 0011 0011
876-
//12 0100 0100
877-
//15 0101 0101
878-
//18 0110 0110
879-
//21 0111 0111
880-
//24 1000 1000
881-
884+
// 12 0100 0100
885+
// 15 0101 0101
886+
// 18 0110 0110
887+
// 21 0111 0111
888+
// 24 1000 1000
889+
882890
es_mic_gain_t gains[] = {MIC_GAIN_0DB, MIC_GAIN_3DB, MIC_GAIN_6DB,
883891
MIC_GAIN_9DB, MIC_GAIN_12DB, MIC_GAIN_15DB,
884892
MIC_GAIN_18DB, MIC_GAIN_21DB, MIC_GAIN_24DB};
@@ -887,7 +895,8 @@ class AudioDriverES8388Class : public AudioDriver {
887895
int idx = map(vol, 0, 100, 0, 8);
888896

889897
es_mic_gain_t gain = gains[idx];
890-
AD_LOGD("input volume: %d -> gain %d [dB] (idx: %d of 0..8)", volume, gain, idx);
898+
AD_LOGD("input volume: %d -> gain %d [dB] (idx: %d of 0..8)", volume, gain,
899+
idx);
891900
return setMicrophoneGain(gain);
892901
}
893902

@@ -1140,7 +1149,7 @@ class AudioDriverWM8978Class : public AudioDriver {
11401149

11411150
return rc;
11421151
}
1143-
1152+
11441153
bool setConfig(CodecConfig codecCfg) override {
11451154
codec_cfg = codecCfg;
11461155
bool is_dac = codec_cfg.output_device != DAC_OUTPUT_NONE;
@@ -1326,6 +1335,78 @@ class AudioDriverWM8994Class : public AudioDriver {
13261335
}
13271336
};
13281337

1338+
/**
1339+
* @brief Driver API for the CS43l22 codec chip on 0x94 (0x4A<<1)
1340+
* @author Phil Schatzmann
1341+
* @copyright GPLv3
1342+
*/
1343+
class AudioDriverPCM3168Class : public AudioDriver {
1344+
public:
1345+
AudioDriverPCM3168Class() = default;
1346+
1347+
bool setMute(bool mute) { return driver.setMute(mute); }
1348+
1349+
bool setMute(bool mute, int line) { return driver.setMute(line, mute); }
1350+
1351+
bool setVolume(int vol) {
1352+
volume = vol;
1353+
return driver.setVolume(100.0 * vol);
1354+
}
1355+
int getVolume() { return volume; }
1356+
1357+
protected:
1358+
int volume;
1359+
PCM3168 driver;
1360+
1361+
bool init(codec_config_t codec_cfg) {
1362+
driver.setWire(*getI2C());
1363+
driver.setAddress(getI2CAddress());
1364+
return true;
1365+
}
1366+
bool deinit() { return driver.end(); }
1367+
bool controlState(codec_mode_t mode) { return true; }
1368+
bool configInterface(codec_mode_t mode, I2SDefinition iface) {
1369+
if (iface.mode == MODE_MASTER) {
1370+
AD_LOGE("Only slave is supported: MCU must be master");
1371+
return false;
1372+
}
1373+
PCM3168::FMT fmt = PCM3168::FMT::I2SHighSpeedTDM24bit;
1374+
switch (iface.bits) {
1375+
case BIT_LENGTH_16BITS:
1376+
if (iface.fmt!= I2S_RIGHT) {
1377+
AD_LOGW("Only I2S_RIGHT is supported for 16 bits");
1378+
}
1379+
fmt = PCM3168::FMT::Right16bit;
1380+
break;
1381+
case BIT_LENGTH_32BITS:
1382+
case BIT_LENGTH_24BITS:
1383+
switch (iface.fmt) {
1384+
case I2S_NORMAL:
1385+
fmt = PCM3168::FMT::I2S24bit;
1386+
break;
1387+
case I2S_LEFT:
1388+
fmt = PCM3168::FMT::Left24bit;
1389+
break;
1390+
case I2S_RIGHT:
1391+
fmt = PCM3168::FMT::Right24bit;
1392+
break;
1393+
case I2S_DSP:
1394+
fmt = PCM3168::FMT::LeftDSP24bit;
1395+
break;
1396+
case I2S_TDM:
1397+
fmt = PCM3168::FMT::I2SHighSpeedTDM24bit;
1398+
break;
1399+
}
1400+
break;
1401+
default:
1402+
AD_LOGE("Unsupported bits");
1403+
return false;
1404+
}
1405+
1406+
return driver.begin(fmt);
1407+
}
1408+
};
1409+
13291410
/**
13301411
* @brief Driver API for Lyrat Mini with a ES8311 and a ES7243 codec chip
13311412
* @author Phil Schatzmann
@@ -1353,8 +1434,8 @@ class AudioDriverLyratMiniClass : public AudioDriver {
13531434
bool setInputVolume(int volume) { return adc.setVolume(volume); }
13541435
int getInputVolume() { return adc.getVolume(); }
13551436
bool isInputVolumeSupported() { return true; }
1356-
// Separate ADC and DAC I2S
1357-
int getI2SCount() override { return 2;}
1437+
// Separate ADC and DAC I2S
1438+
int getI2SCount() override { return 2; }
13581439

13591440
protected:
13601441
AudioDriverES8311Class dac;
@@ -1394,5 +1475,6 @@ static NoDriverClass NoDriver;
13941475
static AudioDriverAD1938Class AudioDriverAD1938;
13951476
/// @ingroup audio_driver
13961477
static AudioDriverCS42448Class AudioDriverCS42448;
1397-
1478+
/// @ingroup audio_driver
1479+
static AudioDriverPCM3168Class AudioDriverPCM3168;
13981480
} // namespace audio_driver

0 commit comments

Comments
 (0)