Changed from MCP23S17 to MCP23017. Lots of changes.

Doesn't really work because of timing stuff.
This commit is contained in:
2019-11-10 14:45:33 +01:00
parent 88655b543d
commit 2ea2ec479a
14 changed files with 423 additions and 156 deletions

View File

@ -2,50 +2,50 @@
#include "player.h"
#include "spi_master.h"
#include "tools.h"
//Player::_spi_settings
Player::Player(MCP* m) {
SPIMaster::enable(PIN_MCP);
Player::Player(Adafruit_MCP23017* m, SPIMaster* s) {
_mcp = m;
_mcp->pinMode(XRESET, OUTPUT);
_mcp->digitalWrite(XRESET, HIGH);
_mcp->pinMode(SPEAKER_L, OUTPUT);
_mcp->pinMode(SPEAKER_R, OUTPUT);
_spi = s;
PIN_VS1053_XRESET_SETUP();
PIN_VS1053_XRESET(HIGH);
PIN_SPEAKER_L_SETUP();
PIN_SPEAKER_R_SETUP();
_speaker_off();
SPIMaster::disable();
pinMode(DREQ, INPUT);
_spi->disable();
PIN_VS1053_DREQ_SETUP();
_init();
}
void Player::_reset() {
SPIMaster::enable(PIN_MCP);
_mcp->digitalWrite(XRESET, LOW);
PIN_VS1053_XRESET(LOW);
delay(100);
_mcp->digitalWrite(XRESET, HIGH);
SPIMaster::disable();
PIN_VS1053_XRESET(HIGH);
delay(100);
_state = uninitialized;
_spi_settings = &_spi_settings_slow; // After reset, communication has to be slow
}
void Player::_init() {
SPIMaster::disable();
DEBUG("Resetting VS1053...\n");
_reset();
uint16_t result = _read_control_register(SCI_MODE);
DEBUG("SCI_MODE: 0x%04X\n", result);
if (result != 0x4800) {
ERROR("SCI_MODE was 0x%04X, expected was 0x4800.\n", result);
return;
ERROR("SCI_MODE was 0x%04X, expected was 0x4800. Rebooting.\n", result);
delay(500);
ESP.restart();
}
result = _read_control_register(SCI_STATUS);
DEBUG("SCI_STATUS: 0x%04X\n", result);
if (result != 0x0040 && result != 0x0048) {
ERROR("SCI_STATUS was 0x%04X, expected was 0x0040 or 0x0048.\n", result);
return;
ERROR("SCI_STATUS was 0x%04X, expected was 0x0040 or 0x0048. Rebooting.\n", result);
delay(500);
ESP.restart();
}
result = _read_control_register(SCI_CLOCKF);
DEBUG("SCI_CLOCKF: 0x%04X\n", result);
@ -61,8 +61,9 @@ void Player::_init() {
result = _read_control_register(SCI_CLOCKF);
DEBUG("SCI_CLOCKF: 0x%04X\n", result);
if (result != 0x6000) {
ERROR("Error: SCI_CLOCKF was 0x%04X, expected was 0x6000.\n", result);
return;
ERROR("Error: SCI_CLOCKF was 0x%04X, expected was 0x6000. Rebooting.\n", result);
delay(500);
ESP.restart();
}
set_volume(VOLUME_DEFAULT);
@ -70,29 +71,26 @@ void Player::_init() {
INFO("VS1053 initialization completed.\n");
INFO("Checking system sounds...\n");
SPIMaster::enable(PIN_SD_CS);
_spi->select_sd();
_check_system_sound("no_prev_song.mp3");
_check_system_sound("no_next_song.mp3");
_check_system_sound("volume_max.mp3");
_check_system_sound("volume_min.mp3");
_spi->select_sd(false);
_state = idle;
}
void Player::_speaker_off() {
DEBUG("Speaker off\n");
SPIMaster::enable(PIN_MCP);
_mcp->digitalWrite(SPEAKER_R, LOW);
_mcp->digitalWrite(SPEAKER_L, LOW);
SPIMaster::disable();
PIN_SPEAKER_L(LOW);
PIN_SPEAKER_R(LOW);
}
void Player::_speaker_on() {
DEBUG("Speaker on\n");
SPIMaster::enable(PIN_MCP);
_mcp->digitalWrite(SPEAKER_R, HIGH);
_mcp->digitalWrite(SPEAKER_L, HIGH);
SPIMaster::disable();
PIN_SPEAKER_L(HIGH);
PIN_SPEAKER_R(HIGH);
}
void Player::_sleep() {
@ -128,12 +126,12 @@ void Player::_check_system_sound(String filename) {
}
inline void Player::_wait() {
while(!digitalRead(DREQ));
while(!PIN_VS1053_DREQ());
}
uint16_t Player::_read_control_register(uint8_t address) {
_wait();
SPIMaster::enable(XCS);
_spi->select_vs1053_xcs();
SPI.beginTransaction(*_spi_settings);
SPI.transfer(CMD_READ);
SPI.transfer(address);
@ -142,7 +140,7 @@ uint16_t Player::_read_control_register(uint8_t address) {
uint8_t b2 = SPI.transfer(0xFF);
_wait();
SPI.endTransaction();
SPIMaster::disable();
_spi->select_vs1053_xcs(false);
return (b1 << 8) | b2;
}
@ -151,7 +149,7 @@ void Player::_write_control_register(uint8_t address, uint16_t value) {
uint8_t b1 = value >> 8;
uint8_t b2 = value & 0xFF;
_wait();
SPIMaster::enable(XCS);
_spi->select_vs1053_xcs();
SPI.beginTransaction(*_spi_settings);
SPI.transfer(CMD_WRITE);
SPI.transfer(address);
@ -159,17 +157,17 @@ void Player::_write_control_register(uint8_t address, uint16_t value) {
SPI.transfer(b2);
_wait();
SPI.endTransaction();
SPIMaster::disable();
_spi->select_vs1053_xcs(false);
}
void Player::_write_data(uint8_t* buffer) {
SPIMaster::enable(XDCS);
_spi->select_vs1053_xdcs();
SPI.beginTransaction(*_spi_settings);
for (uint i=0; i<sizeof(_buffer); i++) {
SPI.transfer(_buffer[i]);
}
SPI.endTransaction();
SPIMaster::disable();
_spi->select_vs1053_xdcs(false);
}
uint16_t Player::_read_wram(uint16_t address) {
@ -264,7 +262,7 @@ bool Player::is_playing() {
}
std::list<String> Player::ls(String path, bool withFiles, bool withDirs, bool withHidden) {
SPIMaster::enable(PIN_SD_CS);
_spi->select_sd();
std::list<String> result;
if (!SD.exists(path)) return result;
File dir = SD.open(path);
@ -277,11 +275,13 @@ std::list<String> Player::ls(String path, bool withFiles, bool withDirs, bool wi
if (entry.isDirectory()) filename.concat("/");
result.push_back(filename);
}
_spi->select_sd(false);
result.sort();
return result;
}
String Player::_find_album_dir(String id) {
_spi->select_sd();
if (id.endsWith("/")) id = id.substring(0, id.length() - 1);
String id_with_divider = id + " - ";
File root = SD.open("/");
@ -295,10 +295,12 @@ String Player::_find_album_dir(String id) {
entry.close();
}
root.close();
_spi->select_sd(false);
return result;
}
std::list<String> Player::_files_in_dir(String path) {
_spi->select_sd();
DEBUG("Examining folder %s...\n", path.c_str());
if (!path.startsWith("/")) path = String("/") + path;
if (!path.endsWith("/")) path.concat("/");
@ -323,6 +325,7 @@ std::list<String> Player::_files_in_dir(String path) {
entry.close();
}
dir.close();
_spi->select_sd(false);
result.sort();
return result;
@ -393,8 +396,13 @@ void Player::play_system_sound(String filename) {
void Player::_play_file(String file, uint32_t file_offset) {
INFO("play_file('%s', %d)\n", file.c_str(), file_offset);
_spi->select_sd();
_file = SD.open(file);
if (!_file) return;
_spi->select_sd(false);
if (!_file) {
DEBUG("Could not open file %s", file.c_str());
return;
}
DEBUG("Resetting SCI_DECODE_TIME...\n");
_write_control_register(SCI_DECODE_TIME, 0);
@ -438,13 +446,14 @@ uint32_t Player::_id3_tag_offset(File f) {
}
void Player::_flush(uint count, int8_t byte) {
SPIMaster::enable(XDCS);
_spi->select_vs1053_xdcs();
SPI.beginTransaction(*_spi_settings);
for(uint i=0; i<count; i++) {
_wait();
SPI.transfer(byte);
}
SPI.endTransaction();
_spi->select_vs1053_xdcs(false);
}
void Player::_finish_playing() {
@ -498,10 +507,11 @@ void Player::_finish_stopping(bool turn_speaker_off) {
}
void Player::_refill() {
SPIMaster::enable(PIN_SD_CS);
_spi->select_sd();
_refills++;
if (_refills % 1000 == 0) DEBUG(".");
uint8_t result = _file.read(_buffer, sizeof(_buffer));
_spi->select_sd(false);
if (result == 0) {
// File is over.
DEBUG("EOF reached.\n");
@ -550,7 +560,7 @@ bool Player::_refill_needed() {
}
bool Player::loop() {
if (digitalRead(DREQ) && _refill_needed()) {
if (PIN_VS1053_DREQ() && _refill_needed()) {
_refill();
return true;
}