Skip to content

Commit 1e016a4

Browse files
authored
i2s: adds i2s_rxtxdrive_begin(enableRx, enableTx, driveRxClocks, driveTxClocks) (esp8266#7748)
Two parameters are added to allow using only i2s-in/out-data pin. This is necessary when i2so-bck and i2so-ws are repurposed especially because they overlap with SPI GPIO.
1 parent e76a98d commit 1e016a4

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

cores/esp8266/core_esp8266_i2s.cpp

+25-10
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ typedef struct i2s_state {
6464
// Callback function should be defined as 'void ICACHE_RAM_ATTR function_name()',
6565
// and be placed in IRAM for faster execution. Avoid long computational tasks in this
6666
// function, use it to set flags and process later.
67+
bool driveClocks;
6768
} i2s_state_t;
6869

6970
// RX = I2S receive (i.e. microphone), TX = I2S transmit (i.e. DAC)
@@ -493,6 +494,10 @@ float i2s_get_real_rate(){
493494
}
494495

495496
bool i2s_rxtx_begin(bool enableRx, bool enableTx) {
497+
return i2s_rxtxdrive_begin(enableRx, enableTx, true, true);
498+
}
499+
500+
bool i2s_rxtxdrive_begin(bool enableRx, bool enableTx, bool driveRxClocks, bool driveTxClocks) {
496501
if (tx || rx) {
497502
i2s_end(); // Stop and free any ongoing stuff
498503
}
@@ -503,22 +508,28 @@ bool i2s_rxtx_begin(bool enableRx, bool enableTx) {
503508
// Nothing to clean up yet
504509
return false; // OOM Error!
505510
}
506-
pinMode(I2SO_WS, FUNCTION_1);
511+
tx->driveClocks = driveTxClocks;
507512
pinMode(I2SO_DATA, FUNCTION_1);
508-
pinMode(I2SO_BCK, FUNCTION_1);
513+
if (driveTxClocks) {
514+
pinMode(I2SO_WS, FUNCTION_1);
515+
pinMode(I2SO_BCK, FUNCTION_1);
516+
}
509517
}
510518
if (enableRx) {
511519
rx = (i2s_state_t*)calloc(1, sizeof(*rx));
512520
if (!rx) {
513521
i2s_end(); // Clean up any TX or pin changes
514522
return false; // OOM error!
515523
}
516-
pinMode(I2SI_WS, OUTPUT);
517-
pinMode(I2SI_BCK, OUTPUT);
524+
rx->driveClocks = driveRxClocks;
518525
pinMode(I2SI_DATA, INPUT);
526+
if (driveRxClocks) {
527+
pinMode(I2SI_WS, OUTPUT);
528+
pinMode(I2SI_BCK, OUTPUT);
529+
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTCK_U, FUNC_I2SI_BCK);
530+
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTMS_U, FUNC_I2SI_WS);
531+
}
519532
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDI_U, FUNC_I2SI_DATA);
520-
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTCK_U, FUNC_I2SI_BCK);
521-
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTMS_U, FUNC_I2SI_WS);
522533
}
523534

524535
if (!i2s_slc_begin()) {
@@ -579,15 +590,19 @@ void i2s_end() {
579590

580591
if (tx) {
581592
pinMode(I2SO_DATA, INPUT);
582-
pinMode(I2SO_BCK, INPUT);
583-
pinMode(I2SO_WS, INPUT);
593+
if (tx->driveClocks) {
594+
pinMode(I2SO_BCK, INPUT);
595+
pinMode(I2SO_WS, INPUT);
596+
}
584597
free(tx);
585598
tx = NULL;
586599
}
587600
if (rx) {
588601
pinMode(I2SI_DATA, INPUT);
589-
pinMode(I2SI_BCK, INPUT);
590-
pinMode(I2SI_WS, INPUT);
602+
if (rx->driveClocks) {
603+
pinMode(I2SI_BCK, INPUT);
604+
pinMode(I2SI_WS, INPUT);
605+
}
591606
free(rx);
592607
rx = NULL;
593608
}

cores/esp8266/i2s.h

+3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#ifndef I2S_h
2222
#define I2S_h
2323

24+
#define I2S_HAS_BEGIN_RXTX_DRIVE_CLOCKS 1
25+
2426
/*
2527
How does this work? Basically, to get sound, you need to:
2628
- Connect an I2S codec to the I2S pins on the ESP.
@@ -42,6 +44,7 @@ extern "C" {
4244

4345
void i2s_begin(); // Enable TX only, for compatibility
4446
bool i2s_rxtx_begin(bool enableRx, bool enableTx); // Allow TX and/or RX, returns false on OOM error
47+
bool i2s_rxtxdrive_begin(bool enableRx, bool enableTx, bool driveRxClocks, bool driveTxClocks);
4548
void i2s_end();
4649
void i2s_set_rate(uint32_t rate);//Sample Rate in Hz (ex 44100, 48000)
4750
void i2s_set_dividers(uint8_t div1, uint8_t div2);//Direct control over output rate

0 commit comments

Comments
 (0)