* is define and SdFat uses a fast custom SPI implementation. | * is define and SdFat uses a fast custom SPI implementation. | ||||
* | * | ||||
* If SD_SPI_CONFIGURATION is define to be one, only the SdFat class is | * If SD_SPI_CONFIGURATION is define to be one, only the SdFat class is | ||||
* define and SdFat uses the standard Arduino SD.h library. | |||||
* define and SdFat uses the standard Arduino SPI.h library. | |||||
* | * | ||||
* If SD_SPI_CONFIGURATION is define to be two, only the SdFat class is | * If SD_SPI_CONFIGURATION is define to be two, only the SdFat class is | ||||
* define and SdFat uses software SPI on the pins defined below. | * define and SdFat uses software SPI on the pins defined below. |
*/ | */ | ||||
#include "SdSpi.h" | #include "SdSpi.h" | ||||
#if defined(__arm__) && defined(CORE_TEENSY) | #if defined(__arm__) && defined(CORE_TEENSY) | ||||
// Teensy 3.0 functions | |||||
#include "mk20dx128.h" | |||||
// SPI definitions | |||||
#include "kinetis.h" | |||||
#ifdef KINETISK | |||||
// use 16-bit frame if SPI_USE_8BIT_FRAME is zero | // use 16-bit frame if SPI_USE_8BIT_FRAME is zero | ||||
#define SPI_USE_8BIT_FRAME 0 | #define SPI_USE_8BIT_FRAME 0 | ||||
// Limit initial fifo to three entries to avoid fifo overrun | // Limit initial fifo to three entries to avoid fifo overrun | ||||
} | } | ||||
#endif // SPI_USE_8BIT_FRAME | #endif // SPI_USE_8BIT_FRAME | ||||
} | } | ||||
#else // KINETISK | |||||
//============================================================================== | |||||
// Use standard SPI library if not KINETISK | |||||
#include "SPI.h" | |||||
/** | |||||
* Initialize SPI pins. | |||||
*/ | |||||
void SdSpi::begin() { | |||||
SPI.begin(); | |||||
} | |||||
/** Set SPI options for access to SD/SDHC cards. | |||||
* | |||||
* \param[in] divisor SCK clock divider relative to the system clock. | |||||
*/ | |||||
void SdSpi::init(uint8_t divisor) { | |||||
SPI.setBitOrder(MSBFIRST); | |||||
SPI.setDataMode(SPI_MODE0); | |||||
#ifndef SPI_CLOCK_DIV128 | |||||
SPI.setClockDivider(divisor); | |||||
#else // SPI_CLOCK_DIV128 | |||||
int v; | |||||
if (divisor <= 2) { | |||||
v = SPI_CLOCK_DIV2; | |||||
} else if (divisor <= 4) { | |||||
v = SPI_CLOCK_DIV4; | |||||
} else if (divisor <= 8) { | |||||
v = SPI_CLOCK_DIV8; | |||||
} else if (divisor <= 16) { | |||||
v = SPI_CLOCK_DIV16; | |||||
} else if (divisor <= 32) { | |||||
v = SPI_CLOCK_DIV32; | |||||
} else if (divisor <= 64) { | |||||
v = SPI_CLOCK_DIV64; | |||||
} else { | |||||
v = SPI_CLOCK_DIV128; | |||||
} | |||||
SPI.setClockDivider(v); | |||||
#endif // SPI_CLOCK_DIV128 | |||||
} | |||||
/** Receive a byte. | |||||
* | |||||
* \return The byte. | |||||
*/ | |||||
uint8_t SdSpi::receive() { | |||||
return SPI.transfer(0XFF); | |||||
} | |||||
/** Receive multiple bytes. | |||||
* | |||||
* \param[out] buf Buffer to receive the data. | |||||
* \param[in] n Number of bytes to receive. | |||||
* | |||||
* \return Zero for no error or nonzero error code. | |||||
*/ | |||||
uint8_t SdSpi::receive(uint8_t* buf, size_t n) { | |||||
for (size_t i = 0; i < n; i++) { | |||||
buf[i] = SPI.transfer(0XFF); | |||||
} | |||||
return 0; | |||||
} | |||||
/** Send a byte. | |||||
* | |||||
* \param[in] b Byte to send | |||||
*/ | |||||
void SdSpi::send(uint8_t b) { | |||||
SPI.transfer(b); | |||||
} | |||||
/** Send multiple bytes. | |||||
* | |||||
* \param[in] buf Buffer for data to be sent. | |||||
* \param[in] n Number of bytes to send. | |||||
*/ | |||||
void SdSpi::send(const uint8_t* buf , size_t n) { | |||||
for (size_t i = 0; i < n; i++) { | |||||
SPI.transfer(buf[i]); | |||||
} | |||||
} | |||||
#endif // KINETISK | |||||
#endif // defined(__arm__) && defined(CORE_TEENSY) | #endif // defined(__arm__) && defined(CORE_TEENSY) |
// Data block for 8-bit ADC mode. | // Data block for 8-bit ADC mode. | ||||
const size_t DATA_DIM8 = 508; | const size_t DATA_DIM8 = 508; | ||||
struct block8_t { | struct block8_t { | ||||
unsigned short count; // count of data bytes | |||||
unsigned short count; // count of data values | |||||
unsigned short overrun; // count of overruns since last block | unsigned short overrun; // count of overruns since last block | ||||
unsigned char data[DATA_DIM8]; | unsigned char data[DATA_DIM8]; | ||||
}; | }; | ||||
// Data block for 10-bit ADC mode. | // Data block for 10-bit ADC mode. | ||||
const size_t DATA_DIM16 = 254; | const size_t DATA_DIM16 = 254; | ||||
struct block16_t { | struct block16_t { | ||||
unsigned short count; // count of data bytes | |||||
unsigned short count; // count of data values | |||||
unsigned short overrun; // count of overruns since last block | unsigned short overrun; // count of overruns since last block | ||||
unsigned short data[DATA_DIM16]; | unsigned short data[DATA_DIM16]; | ||||
}; | }; | ||||
//------------------------------------------------------------------------------ | //------------------------------------------------------------------------------ | ||||
// Data block for PC use | // Data block for PC use | ||||
struct adcdata_t { | struct adcdata_t { | ||||
unsigned short count; // count of data bytes | |||||
unsigned short count; // count of data values | |||||
unsigned short overrun; // count of overruns since last block | unsigned short overrun; // count of overruns since last block | ||||
union { | union { | ||||
unsigned char u8[DATA_DIM8]; | unsigned char u8[DATA_DIM8]; |
//------------------------------------------------------------------------------ | //------------------------------------------------------------------------------ | ||||
int FatFile::read(void* buf, size_t nbyte) { | int FatFile::read(void* buf, size_t nbyte) { | ||||
int8_t fg; | int8_t fg; | ||||
uint8_t blockOfCluster; | |||||
uint8_t blockOfCluster = 0; | |||||
uint8_t* dst = reinterpret_cast<uint8_t*>(buf); | uint8_t* dst = reinterpret_cast<uint8_t*>(buf); | ||||
uint16_t offset; | uint16_t offset; | ||||
size_t toRead; | size_t toRead; |
} | } | ||||
//------------------------------------------------------------------------------ | //------------------------------------------------------------------------------ | ||||
int StdioStream::printDec(signed char n) { | int StdioStream::printDec(signed char n) { | ||||
uint8_t s = 0; | |||||
if (n < 0) { | if (n < 0) { | ||||
if (fputc('-') < 0) { | if (fputc('-') < 0) { | ||||
return -1; | return -1; | ||||
} | } | ||||
n = -n; | n = -n; | ||||
s = 1; | |||||
} | } | ||||
return printDec((unsigned char)n); | return printDec((unsigned char)n); | ||||
} | } |