This commit is contained in:
lewisxhe
2024-11-14 09:48:06 +08:00
parent 737bf1caa6
commit 7aa23db023
3 changed files with 242 additions and 2 deletions
+155
View File
@@ -0,0 +1,155 @@
/**
* @file I2SPlay.ino
* @author Lewis He (lewishe@outlook.com)
* @license MIT
* @copyright Copyright (c) 2023 Shenzhen Xin Yuan Electronic Technology Co., Ltd
* @date 2024-11-14
* @note Arduino Setting
* Tools ->
* Board:"ESP32S3 Dev Module"
* USB CDC On Boot:"Enable"
* USB DFU On Boot:"Disable"
* Flash Size : "4MB(32Mb)"
* Flash Mode"QIO 80MHz
* Partition Scheme:"Default 4MB with spiffs (1.2MB APP/1.5MB SPIFFS)"
* PSRAM:"OPI PSRAM"
* Upload Mode:"UART0/Hardware CDC"
* USB Mode:"Hardware CDC and JTAG"
*/
#include <Arduino.h>
#include <SPI.h>
#include <SD.h>
#include "es7210.h"
#include <Audio.h>
#include <driver/i2s.h>
#include "utilities.h"
#ifndef BOARD_HAS_PSRAM
#error "Detected that PSRAM is not turned on. Please set PSRAM to OPI PSRAM in ArduinoIDE"
#endif
Audio audio;
bool setupSD()
{
digitalWrite(BOARD_SDCARD_CS, HIGH);
digitalWrite(RADIO_CS_PIN, HIGH);
digitalWrite(BOARD_TFT_CS, HIGH);
if (SD.begin(BOARD_SDCARD_CS, SPI, 800000U)) {
uint8_t cardType = SD.cardType();
if (cardType == CARD_NONE) {
Serial.println("No SD_MMC card attached");
return false;
} else {
Serial.print("SD_MMC Card Type: ");
if (cardType == CARD_MMC) {
Serial.println("MMC");
} else if (cardType == CARD_SD) {
Serial.println("SDSC");
} else if (cardType == CARD_SDHC) {
Serial.println("SDHC");
} else {
Serial.println("UNKNOWN");
}
uint32_t cardSize = SD.cardSize() / (1024 * 1024);
uint32_t cardTotal = SD.totalBytes() / (1024 * 1024);
uint32_t cardUsed = SD.usedBytes() / (1024 * 1024);
Serial.printf("SD Card Size: %lu MB\n", cardSize);
Serial.printf("Total space: %lu MB\n", cardTotal);
Serial.printf("Used space: %lu MB\n", cardUsed);
return true;
}
}
return false;
}
bool setupCoder()
{
uint32_t ret_val = ESP_OK;
Wire.beginTransmission(ES7210_ADDR);
uint8_t error = Wire.endTransmission();
if (error != 0) {
Serial.println("ES7210 address not found"); return false;
}
audio_hal_codec_config_t cfg = {
.adc_input = AUDIO_HAL_ADC_INPUT_ALL,
.codec_mode = AUDIO_HAL_CODEC_MODE_ENCODE,
.i2s_iface =
{
.mode = AUDIO_HAL_MODE_SLAVE,
.fmt = AUDIO_HAL_I2S_NORMAL,
.samples = AUDIO_HAL_16K_SAMPLES,
.bits = AUDIO_HAL_BIT_LENGTH_16BITS,
},
};
ret_val |= es7210_adc_init(&Wire, &cfg);
ret_val |= es7210_adc_config_i2s(cfg.codec_mode, &cfg.i2s_iface);
ret_val |= es7210_adc_set_gain(
(es7210_input_mics_t)(ES7210_INPUT_MIC1 | ES7210_INPUT_MIC2),
(es7210_gain_value_t)GAIN_6DB);
ret_val |= es7210_adc_ctrl_state(cfg.codec_mode, AUDIO_HAL_CTRL_START);
return ret_val == ESP_OK;
}
void playTTS(const char *filename)
{
bool findMp3 = false;
if (SD.exists("/" + String(filename))) {
findMp3 = audio.connecttoFS(SD, filename);
} else if (SPIFFS.exists("/" + String(filename))) {
findMp3 = audio.connecttoFS(SPIFFS, filename);
} else {
Serial.println("No find "); Serial.println(filename);
}
if (findMp3) {
while (audio.isRunning()) {
audio.loop();
delay(3);
}
}
}
void setup()
{
Serial.begin(115200);
//! The board peripheral power control pin needs to be set to HIGH when using the peripheral
pinMode(BOARD_POWERON, OUTPUT);
digitalWrite(BOARD_POWERON, HIGH);
//! Set CS on all SPI buses to high level during initialization
pinMode(BOARD_SDCARD_CS, OUTPUT);
pinMode(RADIO_CS_PIN, OUTPUT);
pinMode(BOARD_TFT_CS, OUTPUT);
digitalWrite(BOARD_SDCARD_CS, HIGH);
digitalWrite(RADIO_CS_PIN, HIGH);
digitalWrite(BOARD_TFT_CS, HIGH);
pinMode(BOARD_SPI_MISO, INPUT_PULLUP);
SPI.begin(BOARD_SPI_SCK, BOARD_SPI_MISO, BOARD_SPI_MOSI); //SD
Wire.begin(BOARD_I2C_SDA, BOARD_I2C_SCL);
SPIFFS.begin();
setupSD();
setupCoder();
audio.setPinout(BOARD_I2S_BCK, BOARD_I2S_WS, BOARD_I2S_DOUT);
audio.setVolume(21);
}
void loop()
{
// Put https://github.com/Xinyuan-LilyGO/T-Deck/blob/master/data/hello.mp3 into the SD card
playTTS("hello.mp3");
delay(3000);
}
+85
View File
@@ -0,0 +1,85 @@
/**
* @file utilities.h
* @author Lewis He (lewishe@outlook.com)
* @license MIT
* @copyright Copyright (c) 2023 Shenzhen Xin Yuan Electronic Technology Co., Ltd
* @date 2023-04-11
*
*/
#pragma once
//! The board peripheral power control pin needs to be set to HIGH when using the peripheral
#define BOARD_POWERON 10
#define BOARD_I2S_WS 5
#define BOARD_I2S_BCK 7
#define BOARD_I2S_DOUT 6
#define BOARD_I2C_SDA 18
#define BOARD_I2C_SCL 8
#define BOARD_BAT_ADC 4
#define BOARD_TOUCH_INT 16
#define BOARD_KEYBOARD_INT 46
#define BOARD_SDCARD_CS 39
#define BOARD_TFT_CS 12
#define RADIO_CS_PIN 9
#define BOARD_TFT_DC 11
#define BOARD_TFT_BACKLIGHT 42
#define BOARD_SPI_MOSI 41
#define BOARD_SPI_MISO 38
#define BOARD_SPI_SCK 40
#define BOARD_TBOX_G02 2
#define BOARD_TBOX_G01 3
#define BOARD_TBOX_G04 1
#define BOARD_TBOX_G03 15
#define BOARD_ES7210_MCLK 48
#define BOARD_ES7210_LRCK 21
#define BOARD_ES7210_SCK 47
#define BOARD_ES7210_DIN 14
#define RADIO_BUSY_PIN 13
#define RADIO_RST_PIN 17
#define RADIO_DIO1_PIN 45
#define BOARD_BOOT_PIN 0
#define BOARD_BL_PIN 42
#define BOARD_GPS_TX_PIN 43
#define BOARD_GPS_RX_PIN 44
#ifndef RADIO_FREQ
#define RADIO_FREQ 868.0
#endif
#ifndef RADIO_BANDWIDTH
#define RADIO_BANDWIDTH 125.0
#endif
#ifndef RADIO_SF
#define RADIO_SF 10
#endif
#ifndef RADIO_CR
#define RADIO_CR 6
#endif
#ifndef RADIO_TX_POWER
#define RADIO_TX_POWER 22
#endif
#define DEFAULT_OPA 100