Skip to content

Commit 16044ba

Browse files
authored
Add I2S output feature (#91)
VS1053B has built-in support for I2S output, which is disabled by default and uses GPIO4-7. Adds methods to enable I2S output.
1 parent f501eb7 commit 16044ba

3 files changed

Lines changed: 69 additions & 2 deletions

File tree

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,23 @@ Above example and self-explanatory method name tells everything. It simply provi
8888

8989
Optionally available is also `player.clearDecodedTime()` which clears decoded time (sets `SCI_DECODE_TIME` register to `0x00`).
9090

91+
##### Enable I2S output
92+
93+
```
94+
player.enableI2sOut(/* i2sRate: optional sampling rate, default is 48kHz */);
95+
player.disableI2sOut();
96+
```
97+
I2S output of the VS1053 chip can be enabled/disabled using methods `enableI2sOut` and `disableI2sOut`. When enabled, the gpio lines won't be available for other purposes. On reset the I2S output is disabled. The assignment to I2S lines is according to the table below:
98+
99+
| VS1053 GPIO | I2S |
100+
|--------------|----------------|
101+
| 4 | LROUT / WSEL |
102+
| 5 | MCLCK |
103+
| 6 | SCLK / BCLK |
104+
| 7 | SDATA / DOUT |
105+
106+
Refer to the [VS1053 datasheet](https://www.vlsi.fi/fileadmin/datasheets/vs1053.pdf) for details: the pin assignment is specified in section 5.1 on page 12, the I2S DAC interface is described in section 11.14 on page 83.
107+
91108
#### Logging / debugging
92109

93110
The library uses ESP Arduino framework built in logger (Arduino core for [ESP32](https://github.com/espressif/arduino-esp32/issues/893#issuecomment-348069135) and [ESP8266](https://github.com/esp8266/Arduino/blob/master/doc/Troubleshooting/debugging.rst#debug-level)).<br />

src/VS1053.cpp

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,13 +319,45 @@ void VS1053::printDetails(const char *header) {
319319
* Read more here: http://www.bajdi.com/lcsoft-vs1053-mp3-module/#comment-33773
320320
*/
321321
void VS1053::switchToMp3Mode() {
322-
wram_write(0xC017, 3); // GPIO DDR = 3
323-
wram_write(0xC019, 0); // GPIO ODATA = 0
322+
wram_write(ADDR_REG_GPIO_DDR_RW, 3); // GPIO DDR = 3
323+
wram_write(ADDR_REG_GPIO_ODATA_RW, 0); // GPIO ODATA = 0
324324
delay(100);
325325
LOG("Switched to mp3 mode\n");
326326
softReset();
327327
}
328328

329+
void VS1053::disableI2sOut() {
330+
wram_write(ADDR_REG_I2S_CONFIG_RW, 0x0000);
331+
332+
// configure GPIO0 4-7 (I2S) as input (default)
333+
// leave other GPIOs unchanged
334+
uint16_t cur_ddr = wram_read(ADDR_REG_GPIO_DDR_RW);
335+
wram_write(ADDR_REG_GPIO_DDR_RW, cur_ddr & ~0x00f0);
336+
}
337+
338+
void VS1053::enableI2sOut(VS1053_I2S_RATE i2sRate) {
339+
// configure GPIO0 4-7 (I2S) as output
340+
// leave other GPIOs unchanged
341+
uint16_t cur_ddr = wram_read(ADDR_REG_GPIO_DDR_RW);
342+
wram_write(ADDR_REG_GPIO_DDR_RW, cur_ddr | 0x00f0);
343+
344+
uint16_t i2s_config = 0x000c; // Enable MCLK(3); I2S(2)
345+
switch (i2sRate) {
346+
case VS1053_I2S_RATE_192_KHZ:
347+
i2s_config |= 0x0002;
348+
break;
349+
case VS1053_I2S_RATE_96_KHZ:
350+
i2s_config |= 0x0001;
351+
break;
352+
default:
353+
case VS1053_I2S_RATE_48_KHZ:
354+
// 0x0000
355+
break;
356+
}
357+
358+
wram_write(ADDR_REG_I2S_CONFIG_RW, i2s_config );
359+
}
360+
329361
/**
330362
* A lightweight method to check if VS1053 is correctly wired up (power supply and connection to SPI interface).
331363
*

src/VS1053.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@
4040

4141
#include "patches/vs1053b-patches.h"
4242

43+
enum VS1053_I2S_RATE {
44+
VS1053_I2S_RATE_192_KHZ,
45+
VS1053_I2S_RATE_96_KHZ,
46+
VS1053_I2S_RATE_48_KHZ
47+
};
48+
4349
class VS1053 {
4450
private:
4551
uint8_t cs_pin; // Pin where CS line is connected
@@ -70,6 +76,12 @@ class VS1053 {
7076
const uint8_t SM_TESTS = 5; // Bitnumber in SCI_MODE for tests
7177
const uint8_t SM_LINE1 = 14; // Bitnumber in SCI_MODE for Line input
7278
const uint8_t SM_STREAM = 6; // Bitnumber in SCI_MODE for Streaming Mode
79+
80+
const uint16_t ADDR_REG_GPIO_DDR_RW = 0xc017;
81+
const uint16_t ADDR_REG_GPIO_VAL_R = 0xc018;
82+
const uint16_t ADDR_REG_GPIO_ODATA_RW = 0xc019;
83+
const uint16_t ADDR_REG_I2S_CONFIG_RW = 0xc040;
84+
7385
SPISettings VS1053_SPI; // SPI settings for this slave
7486
uint8_t endFillByte; // Byte to send when stopping song
7587
protected:
@@ -167,6 +179,12 @@ class VS1053 {
167179
// An optional switch preventing the module starting up in MIDI mode
168180
void switchToMp3Mode();
169181

182+
// disable I2S output; this is the default state
183+
void disableI2sOut();
184+
185+
// enable I2S output (GPIO4=LRCLK/WSEL; GPIO5=MCLK; GPIO6=SCLK/BCLK; GPIO7=SDATA/DOUT)
186+
void enableI2sOut(VS1053_I2S_RATE i2sRate = VS1053_I2S_RATE_48_KHZ);
187+
170188
// Checks whether the VS1053 chip is connected and is able to exchange data to the ESP
171189
bool isChipConnected();
172190

0 commit comments

Comments
 (0)