mirror of
https://github.com/meshtastic/firmware.git
synced 2025-12-21 18:22:32 +00:00
WIP: add digital audio. Needs a proto change, so checking in generated files for now.
This commit is contained in:
@@ -42,6 +42,14 @@
|
||||
#define AAMP 14
|
||||
#define PTT_PIN 39
|
||||
|
||||
// #define I2S_WS 13
|
||||
// #define I2S_SD 15
|
||||
// #define I2S_SIN 2
|
||||
// #define I2S_SCK 14
|
||||
|
||||
// Use I2S Processor 0
|
||||
#define I2S_PORT I2S_NUM_0
|
||||
|
||||
#define AUDIO_MODULE_RX_BUFFER 128
|
||||
#define AUDIO_MODULE_DATA_MAX Constants_DATA_PAYLOAD_LEN
|
||||
#define AUDIO_MODULE_MODE ModuleConfig_AudioConfig_Audio_Baud_CODEC2_700
|
||||
@@ -88,7 +96,7 @@ int IRAM_ATTR local_adc1_read(int channel) {
|
||||
IRAM_ATTR void am_onTimer()
|
||||
{
|
||||
portENTER_CRITICAL_ISR(&timerMux); //Enter crital code without interruptions
|
||||
if (radio_state == RadioState::tx) {
|
||||
if ((radio_state == RadioState::tx) && (!moduleConfig.audio.i2s_sd)) {
|
||||
adc_buffer[adc_buffer_index++] = (16 * local_adc1_read(mic_chan)) - 32768;
|
||||
|
||||
//If you want to test with a 1KHz tone, comment the line above and descomment the three lines below
|
||||
@@ -108,15 +116,15 @@ IRAM_ATTR void am_onTimer()
|
||||
if (xHigherPriorityTaskWoken)
|
||||
portYIELD_FROM_ISR();
|
||||
}
|
||||
} else if (radio_state == RadioState::rx) {
|
||||
|
||||
} else if ((radio_state == RadioState::rx) && (!moduleConfig.audio.i2s_din) {
|
||||
// ESP32-S3 does not have DAC support
|
||||
#if !defined(CONFIG_IDF_TARGET_ESP32S3)
|
||||
int16_t v;
|
||||
|
||||
//Get a value from audio_fifo and convert it to 0 - 255 to play it in the ADC
|
||||
if (audio_fifo.get(&v))
|
||||
rx_raw_audio_value = (uint8_t)((v + 32768) / 256);
|
||||
// comment out for now, S3 does not have Hardware-DAC. Consider I2S instead.
|
||||
#if !CONFIG_IDF_TARGET_ESP32S3
|
||||
|
||||
dacWrite(moduleConfig.audio.amp_pin ? moduleConfig.audio.amp_pin : AAMP, rx_raw_audio_value);
|
||||
#endif
|
||||
}
|
||||
@@ -180,41 +188,77 @@ int32_t AudioModule::runOnce()
|
||||
{
|
||||
if ((moduleConfig.audio.codec2_enabled) && (myRegion->audioPermitted)) {
|
||||
if (firstTime) {
|
||||
DEBUG_MSG("--- Initializing ADC on Channel %u\n", moduleConfig.audio.mic_chan ? moduleConfig.audio.mic_chan : AMIC);
|
||||
// if we have I2S_SD defined, take samples from digital mic. I2S_DIN means digital output to amp.
|
||||
if (moduleConfig.audio.i2s_sd || moduleConfig.audio.i2s_din) {
|
||||
// Set up I2S Processor configuration. This will produce 16bit samples instead of 12 from the ADC
|
||||
DEBUG_MSG("--- Initializing I2S for input\n");
|
||||
i2s_config_t i2s_config = {
|
||||
.mode = (i2s_mode_t)(I2S_MODE_MASTER | (moduleConfig.audio.i2s_sd ? I2S_MODE_RX : 0) | (moduleConfig.audio.i2s_din ? I2S_MODE_TX : 0)),
|
||||
.sample_rate = 8000,
|
||||
.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT,
|
||||
.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
|
||||
.communication_format = (i2s_comm_format_t)(I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB),
|
||||
.intr_alloc_flags = 0,
|
||||
.dma_buf_count = 8,
|
||||
.dma_buf_len = ADC_BUFFER_SIZE,
|
||||
.use_apll = false,
|
||||
.tx_desc_auto_clear = true,
|
||||
.fixed_mclk = 0
|
||||
};
|
||||
i2s_driver_install(I2S_PORT, &i2s_config, 0, NULL);
|
||||
|
||||
mic_chan = moduleConfig.audio.mic_chan ? (adc1_channel_t)(int)moduleConfig.audio.mic_chan : (adc1_channel_t)AMIC;
|
||||
adc1_config_width(ADC_WIDTH_12Bit);
|
||||
adc1_config_channel_atten(mic_chan, ADC_ATTEN_DB_6);
|
||||
adc1_get_raw(mic_chan);
|
||||
const i2s_pin_config_t pin_config = {
|
||||
.bck_io_num = moduleConfig.audio.i2s_sck,
|
||||
.ws_io_num = moduleConfig.audio.i2s_ws,
|
||||
.data_out_num = moduleConfig.audio.i2s_din ? moduleConfig.audio.i2s_din : I2S_PIN_NO_CHANGE,
|
||||
.data_in_num = moduleConfig.audio.i2s_sd ? moduleConfig.audio.i2s_sd : I2S_PIN_NO_CHANGE
|
||||
};
|
||||
i2s_set_pin(I2S_PORT, &pin_config);
|
||||
|
||||
i2s_start(I2S_PORT);
|
||||
}
|
||||
|
||||
if (!moduleConfig.audio.i2s_sd) {
|
||||
DEBUG_MSG("--- Initializing ADC on Channel %u\n", moduleConfig.audio.mic_chan ? moduleConfig.audio.mic_chan : AMIC);
|
||||
mic_chan = moduleConfig.audio.mic_chan ? (adc1_channel_t)(int)moduleConfig.audio.mic_chan : (adc1_channel_t)AMIC;
|
||||
adc1_config_width(ADC_WIDTH_12Bit);
|
||||
adc1_config_channel_atten(mic_chan, ADC_ATTEN_DB_6);
|
||||
adc1_get_raw(mic_chan);
|
||||
}
|
||||
|
||||
radio_state = RadioState::rx;
|
||||
|
||||
// Start a timer at 8kHz to sample the ADC and play the audio on the DAC.
|
||||
uint32_t cpufreq = getCpuFrequencyMhz();
|
||||
switch (cpufreq){
|
||||
case 160:
|
||||
adcTimer = timerBegin(3, 1000, true); // 160 MHz / 1000 = 160KHz
|
||||
break;
|
||||
case 240:
|
||||
adcTimer = timerBegin(3, 1500, true); // 240 MHz / 1500 = 160KHz
|
||||
break;
|
||||
case 320:
|
||||
adcTimer = timerBegin(3, 2000, true); // 320 MHz / 2000 = 160KHz
|
||||
break;
|
||||
case 80:
|
||||
default:
|
||||
adcTimer = timerBegin(3, 500, true); // 80 MHz / 500 = 160KHz
|
||||
break;
|
||||
if ((!moduleConfig.audio.i2s_sd) || (!moduleConfig.audio.i2s_din)) {
|
||||
// Start a timer at 8kHz to sample the ADC and play the audio on the DAC, but only if we have analogue audio to process
|
||||
uint32_t cpufreq = getCpuFrequencyMhz();
|
||||
switch (cpufreq){
|
||||
case 160:
|
||||
adcTimer = timerBegin(3, 1000, true); // 160 MHz / 1000 = 160KHz
|
||||
break;
|
||||
case 240:
|
||||
adcTimer = timerBegin(3, 1500, true); // 240 MHz / 1500 = 160KHz
|
||||
break;
|
||||
case 320:
|
||||
adcTimer = timerBegin(3, 2000, true); // 320 MHz / 2000 = 160KHz
|
||||
break;
|
||||
case 80:
|
||||
default:
|
||||
adcTimer = timerBegin(3, 500, true); // 80 MHz / 500 = 160KHz
|
||||
break;
|
||||
}
|
||||
DEBUG_MSG("--- Timer CPU Frequency: %u MHz\n", cpufreq);
|
||||
timerAttachInterrupt(adcTimer, &am_onTimer, false);
|
||||
timerAlarmWrite(adcTimer, 20, true); // Interrupts when counter == 20, 8.000 times a second
|
||||
timerAlarmEnable(adcTimer);
|
||||
}
|
||||
DEBUG_MSG("--- Timer CPU Frequency: %u MHz\n", cpufreq);
|
||||
timerAttachInterrupt(adcTimer, &am_onTimer, false);
|
||||
timerAlarmWrite(adcTimer, 20, true); // Interrupts when counter == 20, 8.000 times a second
|
||||
timerAlarmEnable(adcTimer);
|
||||
|
||||
DEBUG_MSG("--- Initializing DAC on Pin %u\n", moduleConfig.audio.amp_pin ? moduleConfig.audio.amp_pin : AAMP);
|
||||
DEBUG_MSG("--- Initializing PTT on Pin %u\n", moduleConfig.audio.ptt_pin ? moduleConfig.audio.ptt_pin : PTT_PIN);
|
||||
|
||||
// setup analogue DAC only if we don't use I2S for output. This is not available on ESP32-S3
|
||||
#if !defined(CONFIG_IDF_TARGET_ESP32S3)
|
||||
if (moduleConfig.audio.i2s_din)
|
||||
DEBUG_MSG("--- Initializing DAC on Pin %u\n", moduleConfig.audio.amp_pin ? moduleConfig.audio.amp_pin : AAMP);
|
||||
#endif
|
||||
// Configure PTT input
|
||||
DEBUG_MSG("--- Initializing PTT on Pin %u\n", moduleConfig.audio.ptt_pin ? moduleConfig.audio.ptt_pin : PTT_PIN);
|
||||
pinMode(moduleConfig.audio.ptt_pin ? moduleConfig.audio.ptt_pin : PTT_PIN, INPUT);
|
||||
|
||||
firstTime = false;
|
||||
|
||||
Reference in New Issue
Block a user