Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

T4 support #14

Merged
merged 3 commits into from
Jan 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 43 additions & 11 deletions utility/Sd2Card.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,40 @@ static void spiSend(uint8_t b) {
}
/** SPI send multiple bytes */



#elif defined(__IMXRT1052__) || defined(__IMXRT1062__)
#define USE_TEENSY4_SPI

static void spiInit(uint8_t spiRate) {
switch (spiRate) {
// the top 2 speeds are set to 24 MHz, for the SD library defaults
case 0: settings = SPISettings(25200000, MSBFIRST, SPI_MODE0); break;
case 1: settings = SPISettings(24000000, MSBFIRST, SPI_MODE0); break;
case 2: settings = SPISettings(8000000, MSBFIRST, SPI_MODE0); break;
case 3: settings = SPISettings(4000000, MSBFIRST, SPI_MODE0); break;
case 4: settings = SPISettings(3000000, MSBFIRST, SPI_MODE0); break;
case 5: settings = SPISettings(2000000, MSBFIRST, SPI_MODE0); break;
default: settings = SPISettings(400000, MSBFIRST, SPI_MODE0);
}
SPI.begin();
}

static void spiSend(uint8_t b) {
SPI.transfer(b);
}

static uint8_t spiRec(void) {
return SPI.transfer(0xff);
}

static void spiRec(uint8_t* buf, size_t len) {
SPI.transfer(buf, len);
}

static void spiRecIgnore(size_t len) {
for (size_t i=0; i < len; i++)
SPI.transfer(0xff);
}

//------------------------------------------------------------------------------
#else
// functions for hardware SPI
Expand Down Expand Up @@ -231,9 +263,13 @@ uint8_t Sd2Card::SD_init(uint8_t sckRateID, uint8_t chipSelectPin) {
pinMode(chipSelectPin_, OUTPUT);
digitalWrite(chipSelectPin_, HIGH);

#ifdef USE_TEENSY3_SPI
#if defined(USE_TEENSY3_SPI)
spiBegin();
spiInit(6);
#elif defined(USE_TEENSY4_SPI)
spiInit(6);
pinMode(SS_PIN, OUTPUT);
digitalWrite(SS_PIN, HIGH); // disable any SPI device using hardware SS pin
#else
// set pin modes
pinMode(SPI_MISO_PIN, INPUT);
Expand All @@ -250,7 +286,6 @@ uint8_t Sd2Card::SD_init(uint8_t sckRateID, uint8_t chipSelectPin) {
settings = SPISettings(250000, MSBFIRST, SPI_MODE0);
#endif
#endif // not USE_TEENSY3_SPI

// must supply min of 74 clock cycles with CS high.
#ifdef SPI_HAS_TRANSACTION
SPI.beginTransaction(settings);
Expand All @@ -259,9 +294,7 @@ uint8_t Sd2Card::SD_init(uint8_t sckRateID, uint8_t chipSelectPin) {
#ifdef SPI_HAS_TRANSACTION
SPI.endTransaction();
#endif

chipSelectLow();

// command to go idle in SPI mode
while ((status_ = cardCommand(CMD0, 0)) != R1_IDLE_STATE) {
unsigned int d = millis() - t0;
Expand All @@ -282,7 +315,6 @@ uint8_t Sd2Card::SD_init(uint8_t sckRateID, uint8_t chipSelectPin) {
}
// initialize card and send host supports SDHC if SD2
arg = (type_ == SD_CARD_TYPE_SD2) ? 0X40000000 : 0;

while ((status_ = cardAcmd(ACMD41, arg)) != R1_READY_STATE) {
// check for timeout
unsigned int d = millis() - t0;
Expand Down Expand Up @@ -327,7 +359,7 @@ uint8_t Sd2Card::SD_readBlock(uint32_t block, uint8_t* dst)
if (!waitStartBlock()) {
goto fail;
}
#ifdef USE_TEENSY3_SPI
#if defined(USE_TEENSY3_SPI) | defined(USE_TEENSY4_SPI)
spiRec(dst, 512);
spiRecIgnore(2);
#else // OPTIMIZE_HARDWARE_SPI
Expand Down Expand Up @@ -375,7 +407,7 @@ uint8_t Sd2Card::SD_readBlock(uint32_t block, uint8_t* dst)
* false, is returned for an invalid value of \a sckRateID.
*/
uint8_t Sd2Card::setSckRate(uint8_t sckRateID) {
#ifdef USE_TEENSY3_SPI
#if defined(USE_TEENSY3_SPI) || defined(USE_TEENSY4_SPI)
spiInit(sckRateID);
return true;
#else
Expand Down Expand Up @@ -473,7 +505,7 @@ uint8_t Sd2Card::SD_writeBlock(uint32_t blockNumber, const uint8_t* src) {
//------------------------------------------------------------------------------
// send one block of data for write block or write multiple blocks
uint8_t Sd2Card::writeData(uint8_t token, const uint8_t* src) {
#ifdef OPTIMIZE_HARDWARE_SPI
#if defined(OPTIMIZE_HARDWARE_SPI) && !defined(USE_TEENSY4_SPI)

// send data - optimized loop
SPDR = token;
Expand All @@ -492,7 +524,7 @@ uint8_t Sd2Card::writeData(uint8_t token, const uint8_t* src) {
#else // OPTIMIZE_HARDWARE_SPI
spiSend(token);
for (uint16_t i = 0; i < 512; i++) {
spiSend(src[i]);
spiSend(src[i]);
}
#endif // OPTIMIZE_HARDWARE_SPI
spiSend(0xff); // dummy crc
Expand Down
2 changes: 1 addition & 1 deletion utility/Sd2PinMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#ifndef Sd2PinMap_h
#define Sd2PinMap_h

#if defined(__arm__) || defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__)
#if defined(__arm__) || defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__) || defined(__IMXRT1052__) || defined(__IMXRT1062__)

#include <Arduino.h>

Expand Down