Intermediary commit. Nothing's working, everything is broken. Let's start over now...

This commit is contained in:
Fabian Schlenz 2019-08-04 13:42:07 +02:00
parent 128d76cebc
commit 2a7dd5d8a1
9 changed files with 232 additions and 6 deletions

View File

@ -1,9 +1,32 @@
#pragma once
#include <Arduino.h>
#define PIN_SD_CS D1
#define PIN_VS1053_CS D2
#define PIN_RC522_CS D3
// Pins according to https://www.instructables.com/id/NodeMCU-ESP8266-Details-and-Pinout/
// D0 = 16
// D1 = 5
// D2 = 4
// D3 = 0
// D4 = 2
// D5 = 14
// D6 = 12
// D7 = 13
// D8 = 15
// A0 = 17
// Other usable pins:
// 6, 7, 8, 11 not readable
// 1, 3, 6, 7, 8, 11 nor writable -> 1 (TX) and 3 (RX) can be used read-only
// 9 -> S2
// 10 -> S3
#define RX 3
#define TX 1
#define PIN_SD_CS D4
#define PIN_VS1053_XCS D8
#define PIN_VS1053_XRESET D3
#define PIN_VS1053_XDCS D1
#define PIN_VS1053_DREQ D2
#define PIN_RC522_CS D0
#define PIN_BTN_VOL_UP D4
#define PIN_BTN_VOL_DOWN D5

View File

@ -1,13 +1,15 @@
#pragma once
#include "player.h"
#include "sd_card.h"
class Controller {
private:
Player* _player;
SDCard* _sd_card;
public:
Controller(Player* p) : _player(p) {}
Controller(Player* p, SDCard* c) : _player(p), _sd_card(c) {}
void vol_up();
void vol_down();
void track_next();

View File

@ -1,7 +1,27 @@
#pragma once
#include "sd_card.h"
#include "config.h"
#define SCI_MODE 0x00
#define SCI_STATUS 0x01
#define SCI_CLOCKF 0x03
#define CMD_WRITE 0x02
#define CMD_READ 0x03
#define XRESET PIN_VS1053_XRESET
#define DREQ PIN_VS1053_DREQ
class Player {
private:
SDCard* _sd_card;
void _reset();
void _init();
void _wait();
uint16_t _read_register(uint8_t address, uint32_t spi_speed, uint16_t t);//=SPI_CLOCK_DIV4);
void _write_register(uint8_t address, uint16_t value, uint32_t spi_speed);//=SPI_CLOCK_DIV2);
public:
Player(SDCard* c);
void vol_up();
void vol_down();
void track_next();

8
include/sd_card.h Normal file
View File

@ -0,0 +1,8 @@
#pragma once
#include <SD.h>
#include "config.h"
class SDCard {
public:
SDCard();
};

27
include/spi_master.h Normal file
View File

@ -0,0 +1,27 @@
#pragma once
class SPIMaster {
public:
static void init() {
SPI.setHwCs(false);
pinMode(PIN_SD_CS, OUTPUT);
pinMode(PIN_VS1053_XCS, OUTPUT);
pinMode(PIN_VS1053_XDCS, OUTPUT);
}
static void enable(uint8_t pin) {
digitalWrite(PIN_SD_CS, pin==PIN_SD_CS ? LOW : HIGH);
digitalWrite(PIN_VS1053_XCS, pin==PIN_VS1053_XCS ? LOW : HIGH);
digitalWrite(PIN_VS1053_XDCS, pin==PIN_VS1053_XDCS ? LOW : HIGH);
}
static void printStatus() {
Serial.printf("CS state: SD:%d, VS1053_XCS:%d, VS1053_XDCS:%d\n",
digitalRead(PIN_SD_CS),
digitalRead(PIN_VS1053_XCS),
digitalRead(PIN_VS1053_XDCS));
}
static void disable() {
enable(142);
}
};

View File

@ -12,3 +12,5 @@
platform = espressif8266
board = esp12e
framework = arduino
upload_port = /dev/cu.wchusbserial1420
upload_speed = 921600

View File

@ -1,16 +1,52 @@
#include <Arduino.h>
#include <SPI.h>
#include "config.h"
#include "buttons.h"
#include "controller.h"
#include "player.h"
#include "sd_card.h"
#include "spi_master.h"
Buttons* buttons;
Controller* controller;
Player* player;
SDCard* sd;
void printDirectory(File dir, int numTabs) {
while (true) {
File entry = dir.openNextFile();
if (! entry) {
// no more files
break;
}
for (uint8_t i = 0; i < numTabs; i++) {
Serial.print('\t');
}
Serial.print(entry.name());
if (entry.isDirectory()) {
Serial.println("/");
printDirectory(entry, numTabs + 1);
} else {
// files have sizes, directories do not
Serial.print("\t\t");
Serial.println(entry.size(), DEC);
}
entry.close();
}
}
void setup() {
player = new Player();
controller = new Controller(player);
Serial.begin(74880);
Serial.println("Starting.");
Serial.println("Setting up SPI...");
SPI.begin();
SPIMaster::init();
sd = new SDCard();
player = new Player(sd);
controller = new Controller(player, sd);
buttons = new Buttons(controller);
}

99
src/player.cpp Normal file
View File

@ -0,0 +1,99 @@
// Based on https://github.com/mpflaga/Arduino_Library-vs1053_for_SdFat/blob/master/src/vs1053_SdFat.cpp
#include "player.h"
#include "spi_master.h"
Player::Player(SDCard* c) {
pinMode(XRESET, OUTPUT);
digitalWrite(XRESET, HIGH);
pinMode(DREQ, INPUT);
_init();
}
void Player::_reset() {
digitalWrite(XRESET, LOW);
delay(50);
digitalWrite(XRESET, HIGH);
delay(50);
}
void Player::_init() {
SPI.setClockDivider(SPI_CLOCK_DIV16);
_reset();
uint16_t result;
for(uint8_t x=0; x<255; x++) {
result = _read_register(SCI_MODE, 0, x);
if (result != 0) Serial.printf("Try %3d: 0x%04X\n", x, result);
}
delay(10);
/*_read_register(SCI_MODE, SPI_CLOCK_DIV16); // First read fails for some unknown reason...
_read_register(SCI_MODE, SPI_CLOCK_DIV16);
_read_register(SCI_STATUS, SPI_CLOCK_DIV16);
_read_register(SCI_STATUS, SPI_CLOCK_DIV16);
_read_register(SCI_MODE, SPI_CLOCK_DIV16);
_read_register(SCI_MODE, SPI_CLOCK_DIV16);
uint16_t response = _read_register(SCI_MODE, SPI_CLOCK_DIV16);
if (response != 0x4800) {
Serial.printf("Initialization failed. SCI_MODE was: 0x%04X. Expected: 0x4800\n", response);
return;
}*/
/*_write_register(SCI_CLOCKF, 0x6000); // Set multiplier to 3x
delay(10);
response = _read_register(SCI_CLOCKF);
if (response != 0x6000) {
Serial.printf("Initialization failed. SCI_CLOCKF was: 0x%04X. Expected: 0x6000\n", response);
return;
}*/
//Serial.println(result, HEX);
}
inline void Player::_wait() {
delayMicroseconds(100);
//Serial.print("Waiting for DREQ...");
while(!digitalRead(DREQ));
//Serial.printf(" done (%d cycles).\n", i);
}
uint16_t Player::_read_register(uint8_t address, uint32_t spi_speed, uint16_t t) {
Serial.printf("Try %03d, querying register 0x%02X...", t, address);
//SPI.setClockDivider(spi_speed);
if (t & (1<<1)) _wait();
SPIMaster::enable(PIN_VS1053_XCS);
if (t & (1<<2)) _wait();
//_wait();
SPI.transfer(CMD_READ);
if (t & (1<<3)) _wait();
SPI.transfer(address);
if (t & (1<<4)) _wait();
uint8_t b1 = SPI.transfer(0xFF);
if (t & (1<<5)) _wait();
uint8_t b2 = SPI.transfer(0xFF);
if (t & (1<<6)) _wait();
uint16_t result = b1 << 8 | b2;
Serial.printf("Response: 0x%02X 0x%02X -> 0x%04X\n", b1, b2, result);
if (t & (1<<7)) delay(10);
//_wait();
//SPIMaster::disable();
return result;
}
void Player::_write_register(uint8_t address, uint16_t value, uint32_t spi_speed) {
Serial.printf("Writing to register 0x%02X...", address);
SPI.setClockDivider(spi_speed);
SPIMaster::enable(PIN_VS1053_XCS);
SPI.transfer(CMD_WRITE);
SPI.transfer(address);
SPI.transfer(value >> 8);
SPI.transfer(value & 0xFF);
_wait();
SPIMaster::disable();
Serial.println(" done.");
}

9
src/sd_card.cpp Normal file
View File

@ -0,0 +1,9 @@
#include "sd_card.h"
SDCard::SDCard() {
if (!SD.begin(PIN_SD_CS)) {
Serial.println("Could not initialize SD card.");
} else {
Serial.println("SD card successfully initialized.");
}
}