|
|
@@ -18,12 +18,11 @@ |
|
|
|
* <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#include <Arduino.h>
|
|
|
|
#include <SPI.h>
|
|
|
|
#include "Sd2Card.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#if defined(__MK20DX128__) || defined(__MK20DX256__)
|
|
|
|
#define USE_TEENSY3_SPI
|
|
|
|
|
|
|
@@ -210,9 +209,8 @@ static void spiSend(const uint8_t* output, size_t len) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
#elif !defined(SOFTWARE_SPI)
|
|
|
|
#else
|
|
|
|
// functions for hardware SPI
|
|
|
|
/** Send a byte to the card */
|
|
|
|
static void spiSend(uint8_t b) {
|
|
|
@@ -225,65 +223,7 @@ static uint8_t spiRec(void) { |
|
|
|
return SPDR;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#else // SOFTWARE_SPI
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
/** nop to tune soft SPI timing */
|
|
|
|
#define nop asm volatile ("nop\n\t")
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
/** Soft SPI receive */
|
|
|
|
uint8_t spiRec(void) {
|
|
|
|
uint8_t data = 0;
|
|
|
|
// no interrupts during byte receive - about 8 us
|
|
|
|
cli();
|
|
|
|
// output pin high - like sending 0XFF
|
|
|
|
fastDigitalWrite(SPI_MOSI_PIN, HIGH);
|
|
|
|
|
|
|
|
for (uint8_t i = 0; i < 8; i++) {
|
|
|
|
fastDigitalWrite(SPI_SCK_PIN, HIGH);
|
|
|
|
|
|
|
|
// adjust so SCK is nice
|
|
|
|
nop;
|
|
|
|
nop;
|
|
|
|
|
|
|
|
data <<= 1;
|
|
|
|
|
|
|
|
if (fastDigitalRead(SPI_MISO_PIN)) data |= 1;
|
|
|
|
|
|
|
|
fastDigitalWrite(SPI_SCK_PIN, LOW);
|
|
|
|
}
|
|
|
|
// enable interrupts
|
|
|
|
sei();
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
/** Soft SPI send */
|
|
|
|
void spiSend(uint8_t data) {
|
|
|
|
// no interrupts during byte send - about 8 us
|
|
|
|
cli();
|
|
|
|
for (uint8_t i = 0; i < 8; i++) {
|
|
|
|
fastDigitalWrite(SPI_SCK_PIN, LOW);
|
|
|
|
|
|
|
|
fastDigitalWrite(SPI_MOSI_PIN, data & 0X80);
|
|
|
|
|
|
|
|
data <<= 1;
|
|
|
|
|
|
|
|
fastDigitalWrite(SPI_SCK_PIN, HIGH);
|
|
|
|
}
|
|
|
|
// hold SCK high for a few ns
|
|
|
|
nop;
|
|
|
|
nop;
|
|
|
|
nop;
|
|
|
|
nop;
|
|
|
|
|
|
|
|
fastDigitalWrite(SPI_SCK_PIN, LOW);
|
|
|
|
// enable interrupts
|
|
|
|
sei();
|
|
|
|
}
|
|
|
|
#endif // SOFTWARE_SPI
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -435,7 +375,6 @@ uint8_t Sd2Card::init(uint8_t sckRateID, uint8_t chipSelectPin) { |
|
|
|
pinMode(SPI_MISO_PIN, INPUT);
|
|
|
|
pinMode(SPI_MOSI_PIN, OUTPUT);
|
|
|
|
pinMode(SPI_SCK_PIN, OUTPUT);
|
|
|
|
#ifndef SOFTWARE_SPI
|
|
|
|
// SS must be in output mode even it is not chip select
|
|
|
|
pinMode(SS_PIN, OUTPUT);
|
|
|
|
digitalWrite(SS_PIN, HIGH); // disable any SPI device using hardware SS pin
|
|
|
@@ -443,7 +382,6 @@ uint8_t Sd2Card::init(uint8_t sckRateID, uint8_t chipSelectPin) { |
|
|
|
SPCR = (1 << SPE) | (1 << MSTR) | (1 << SPR1) | (1 << SPR0);
|
|
|
|
// clear double speed
|
|
|
|
SPSR &= ~(1 << SPI2X);
|
|
|
|
#endif // SOFTWARE_SPI
|
|
|
|
#endif // not USE_TEENSY3_SPI
|
|
|
|
|
|
|
|
// must supply min of 74 clock cycles with CS high.
|
|
|
@@ -492,11 +430,7 @@ uint8_t Sd2Card::init(uint8_t sckRateID, uint8_t chipSelectPin) { |
|
|
|
}
|
|
|
|
chipSelectHigh();
|
|
|
|
|
|
|
|
#ifndef SOFTWARE_SPI
|
|
|
|
return setSckRate(sckRateID);
|
|
|
|
#else // SOFTWARE_SPI
|
|
|
|
return true;
|
|
|
|
#endif // SOFTWARE_SPI
|
|
|
|
|
|
|
|
fail:
|
|
|
|
chipSelectHigh();
|