2019-08-04 11:42:07 +00:00
|
|
|
#pragma once
|
|
|
|
|
2019-08-06 18:50:11 +00:00
|
|
|
#include <Arduino.h>
|
|
|
|
#include <SPI.h>
|
|
|
|
#include "config.h"
|
|
|
|
|
2019-08-04 11:42:07 +00:00
|
|
|
class SPIMaster {
|
|
|
|
public:
|
2019-11-17 13:24:01 +00:00
|
|
|
static uint8_t state;
|
|
|
|
|
2019-11-14 19:42:02 +00:00
|
|
|
static void init() {
|
2019-11-10 13:45:33 +00:00
|
|
|
PIN_SD_CS_SETUP();
|
|
|
|
PIN_VS1053_XCS_SETUP();
|
|
|
|
PIN_VS1053_XDCS_SETUP();
|
|
|
|
PIN_RC522_CS_SETUP();
|
|
|
|
disable();
|
2019-08-04 11:42:07 +00:00
|
|
|
}
|
2019-08-11 15:15:22 +00:00
|
|
|
|
2019-11-14 19:42:02 +00:00
|
|
|
static void select_sd(bool enabled=true) {
|
2019-11-10 13:45:33 +00:00
|
|
|
PIN_SD_CS(enabled ? LOW : HIGH);
|
2019-11-17 13:24:01 +00:00
|
|
|
if (enabled) {
|
|
|
|
state |= 1;
|
|
|
|
} else {
|
|
|
|
state &= ~1;
|
|
|
|
}
|
2019-11-10 13:45:33 +00:00
|
|
|
delayMicroseconds(MCP_SPI_SETTING_DELAY);
|
2019-08-04 11:42:07 +00:00
|
|
|
}
|
|
|
|
|
2019-11-14 19:42:02 +00:00
|
|
|
static void select_vs1053_xcs(bool enabled=true) {
|
2019-11-10 13:45:33 +00:00
|
|
|
PIN_VS1053_XCS(enabled ? LOW : HIGH);
|
2019-11-17 13:24:01 +00:00
|
|
|
if (enabled) {
|
|
|
|
state |= 2;
|
|
|
|
} else {
|
|
|
|
state &= ~2;
|
|
|
|
}
|
2019-11-10 13:45:33 +00:00
|
|
|
delayMicroseconds(MCP_SPI_SETTING_DELAY);
|
2019-08-04 11:42:07 +00:00
|
|
|
}
|
|
|
|
|
2019-11-14 19:42:02 +00:00
|
|
|
static void select_vs1053_xdcs(bool enabled=true) {
|
2019-11-10 13:45:33 +00:00
|
|
|
PIN_VS1053_XDCS(enabled ? LOW : HIGH);
|
2019-11-17 13:24:01 +00:00
|
|
|
if (enabled) {
|
|
|
|
state |= 4;
|
|
|
|
} else {
|
|
|
|
state &= ~4;
|
|
|
|
}
|
2019-11-10 13:45:33 +00:00
|
|
|
delayMicroseconds(MCP_SPI_SETTING_DELAY);
|
|
|
|
}
|
|
|
|
|
2019-11-14 19:42:02 +00:00
|
|
|
static void select_rc522(bool enabled=true) {
|
2019-11-10 13:45:33 +00:00
|
|
|
PIN_RC522_CS(enabled ? LOW : HIGH);
|
2019-11-17 13:24:01 +00:00
|
|
|
if (enabled) {
|
|
|
|
state |= 8;
|
|
|
|
} else {
|
|
|
|
state &= ~8;
|
|
|
|
}
|
2019-11-10 13:45:33 +00:00
|
|
|
delayMicroseconds(MCP_SPI_SETTING_DELAY);
|
|
|
|
}
|
|
|
|
|
2019-11-17 13:24:01 +00:00
|
|
|
static void set_state(uint8_t s) {
|
|
|
|
disable();
|
|
|
|
if (s & 1) select_sd();
|
|
|
|
if (s & 2) select_vs1053_xcs();
|
|
|
|
if (s & 4) select_vs1053_xdcs();
|
|
|
|
if (s & 8) select_rc522();
|
|
|
|
}
|
|
|
|
|
2019-11-14 19:42:02 +00:00
|
|
|
static void disable() {
|
2019-11-10 13:45:33 +00:00
|
|
|
PIN_SD_CS(HIGH);
|
|
|
|
PIN_VS1053_XCS(HIGH);
|
|
|
|
PIN_VS1053_XDCS(HIGH);
|
|
|
|
PIN_RC522_CS(HIGH);
|
2019-11-17 13:24:01 +00:00
|
|
|
state = 0;
|
2019-08-04 11:42:07 +00:00
|
|
|
}
|
2019-11-17 13:24:01 +00:00
|
|
|
};
|