Browse Source

Added some libraries that need implementation to achieve 115200 bitrates for sd card logging

master
Mike C 9 years ago
parent
commit
fc49b2e735
  1. 171
      Libraries/AdafruitLogger/AdafruitLogger.ino
  2. 234
      Libraries/AdafruitLogger/I2cMaster/I2cMaster.cpp
  3. 256
      Libraries/AdafruitLogger/I2cMaster/I2cMaster.h
  4. 53
      Libraries/AdafruitLogger/I2cMaster/examples/i2cScanAddress/i2cScanAddress.ino
  5. 53
      Libraries/AdafruitLogger/I2cMaster/examples/i2cScopeTest/i2cScopeTest.ino
  6. 321
      Libraries/AdafruitLogger/I2cMaster/examples/softDS1307/softDS1307.ino
  7. 119
      Libraries/AdafruitLogger/SdFat/ArduinoStream.h
  8. 71
      Libraries/AdafruitLogger/SdFat/MinimumSerial.cpp
  9. 36
      Libraries/AdafruitLogger/SdFat/MinimumSerial.h
  10. 1199
      Libraries/AdafruitLogger/SdFat/Sd2Card.cpp
  11. 233
      Libraries/AdafruitLogger/SdFat/Sd2Card.h
  12. 2269
      Libraries/AdafruitLogger/SdFat/SdBaseFile.cpp
  13. 560
      Libraries/AdafruitLogger/SdFat/SdBaseFile.h
  14. 350
      Libraries/AdafruitLogger/SdFat/SdFat.cpp
  15. 119
      Libraries/AdafruitLogger/SdFat/SdFat.h
  16. 183
      Libraries/AdafruitLogger/SdFat/SdFatConfig.h
  17. 604
      Libraries/AdafruitLogger/SdFat/SdFatStructs.h
  18. 74
      Libraries/AdafruitLogger/SdFat/SdFatUtil.cpp
  19. 40
      Libraries/AdafruitLogger/SdFat/SdFatUtil.h
  20. 227
      Libraries/AdafruitLogger/SdFat/SdFatmainpage.h
  21. 83
      Libraries/AdafruitLogger/SdFat/SdFile.cpp
  22. 49
      Libraries/AdafruitLogger/SdFat/SdFile.h
  23. 277
      Libraries/AdafruitLogger/SdFat/SdInfo.h
  24. 151
      Libraries/AdafruitLogger/SdFat/SdStream.cpp
  25. 263
      Libraries/AdafruitLogger/SdFat/SdStream.h
  26. 599
      Libraries/AdafruitLogger/SdFat/SdVolume.cpp
  27. 234
      Libraries/AdafruitLogger/SdFat/SdVolume.h
  28. 146
      Libraries/AdafruitLogger/SdFat/bufstream.h
  29. 172
      Libraries/AdafruitLogger/SdFat/examples/AnalogLogger/AnalogLogger.ino
  30. 15
      Libraries/AdafruitLogger/SdFat/examples/HelloWorld/HelloWorld.ino
  31. 18
      Libraries/AdafruitLogger/SdFat/examples/MiniSerial/MiniSerial.ino
  32. 37
      Libraries/AdafruitLogger/SdFat/examples/OpenNext/OpenNext.ino
  33. 90
      Libraries/AdafruitLogger/SdFat/examples/PrintBenchmark/PrintBenchmark.ino
  34. 129
      Libraries/AdafruitLogger/SdFat/examples/QuickStart/QuickStart.ino
  35. 175
      Libraries/AdafruitLogger/SdFat/examples/RawWrite/RawWrite.ino
  36. 68
      Libraries/AdafruitLogger/SdFat/examples/ReadWriteSdFat/ReadWriteSdFat.ino
  37. 26
      Libraries/AdafruitLogger/SdFat/examples/SD_Size/SD_Size.ino
  38. 28
      Libraries/AdafruitLogger/SdFat/examples/SdFatSize/SdFatSize.ino
  39. 486
      Libraries/AdafruitLogger/SdFat/examples/SdFormatter/SdFormatter.ino
  40. 197
      Libraries/AdafruitLogger/SdFat/examples/SdInfo/SdInfo.ino
  41. 168
      Libraries/AdafruitLogger/SdFat/examples/Timestamp/Timestamp.ino
  42. 140
      Libraries/AdafruitLogger/SdFat/examples/TwoCards/TwoCards.ino
  43. 63
      Libraries/AdafruitLogger/SdFat/examples/append/append.ino
  44. 70
      Libraries/AdafruitLogger/SdFat/examples/average/average.ino
  45. 122
      Libraries/AdafruitLogger/SdFat/examples/bench/bench.ino
  46. 134
      Libraries/AdafruitLogger/SdFat/examples/benchSD/benchSD.ino
  47. 33
      Libraries/AdafruitLogger/SdFat/examples/bufstream/bufstream.ino
  48. 34
      Libraries/AdafruitLogger/SdFat/examples/cin_cout/cin_cout.ino
  49. 53
      Libraries/AdafruitLogger/SdFat/examples/eventlog/eventlog.ino
  50. 74
      Libraries/AdafruitLogger/SdFat/examples/fgets/fgets.ino
  51. 91
      Libraries/AdafruitLogger/SdFat/examples/fgetsRewrite/fgetsRewrite.ino
  52. 66
      Libraries/AdafruitLogger/SdFat/examples/formatting/formatting.ino
  53. 75
      Libraries/AdafruitLogger/SdFat/examples/getline/getline.ino
  54. 86
      Libraries/AdafruitLogger/SdFat/examples/readCSV/readCSV.ino
  55. 40
      Libraries/AdafruitLogger/SdFat/examples/readlog/readlog.ino
  56. 77
      Libraries/AdafruitLogger/SdFat/examples/rename/rename.ino
  57. 394
      Libraries/AdafruitLogger/SdFat/ios.h
  58. 153
      Libraries/AdafruitLogger/SdFat/iostream.h
  59. 411
      Libraries/AdafruitLogger/SdFat/istream.cpp
  60. 306
      Libraries/AdafruitLogger/SdFat/istream.h
  61. 176
      Libraries/AdafruitLogger/SdFat/ostream.cpp
  62. 287
      Libraries/AdafruitLogger/SdFat/ostream.h
  63. 503
      Libraries/AdafruitLogger/SdFat/utility/DigitalPin.h
  64. 120
      Libraries/AdafruitLogger/SdFat/utility/SoftSPI.h
  65. 440
      Libraries/AdafruitLogger/SoftRTClib/SoftRTClib.cpp
  66. 210
      Libraries/AdafruitLogger/SoftRTClib/SoftRTClib.h
  67. 456
      Libraries/AdafruitLogger/SoftRTClib/examples/DsRtcUtility/DsRtcUtility.pde
  68. 54
      Libraries/AdafruitLogger/SoftRTClib/examples/ds1307Soft/ds1307Soft.pde
  69. 78
      Libraries/AdafruitLogger/SoftRTClib/examples/ds1307Sqw/ds1307Sqw.pde
  70. 34
      Libraries/AdafruitLogger/SoftRTClib/examples/ds1307Twi/ds1307Twi.pde
  71. 38
      Libraries/AdafruitLogger/SoftRTClib/examples/ds1307Wire/ds1307Wire.pde
  72. 101
      Libraries/AdafruitLogger/SoftRTClib/utility/InlineDateAlgorithms.h
  73. 9
      Libraries/AdafruitLogger/readme.txt
  74. 62
      Libraries/SdFat/ArduinoDue.txt
  75. 36
      Libraries/SdFat/CardPerformance.txt
  76. 14
      Libraries/SdFat/MegaSoftSpi.txt
  77. 13
      Libraries/SdFat/MultipleCards.txt
  78. 21
      Libraries/SdFat/QuickStart.txt
  79. 10
      Libraries/SdFat/SdFat.html
  80. 119
      Libraries/SdFat/SdFat/ArduinoStream.h
  81. 71
      Libraries/SdFat/SdFat/MinimumSerial.cpp
  82. 36
      Libraries/SdFat/SdFat/MinimumSerial.h
  83. 1205
      Libraries/SdFat/SdFat/Sd2Card.cpp
  84. 233
      Libraries/SdFat/SdFat/Sd2Card.h
  85. 2338
      Libraries/SdFat/SdFat/SdBaseFile.cpp
  86. 564
      Libraries/SdFat/SdFat/SdBaseFile.h
  87. 350
      Libraries/SdFat/SdFat/SdFat.cpp
  88. 119
      Libraries/SdFat/SdFat/SdFat.h
  89. 183
      Libraries/SdFat/SdFat/SdFatConfig.h
  90. 604
      Libraries/SdFat/SdFat/SdFatStructs.h
  91. 77
      Libraries/SdFat/SdFat/SdFatUtil.cpp
  92. 40
      Libraries/SdFat/SdFat/SdFatUtil.h
  93. 227
      Libraries/SdFat/SdFat/SdFatmainpage.h
  94. 83
      Libraries/SdFat/SdFat/SdFile.cpp
  95. 49
      Libraries/SdFat/SdFat/SdFile.h
  96. 277
      Libraries/SdFat/SdFat/SdInfo.h
  97. 151
      Libraries/SdFat/SdFat/SdStream.cpp
  98. 263
      Libraries/SdFat/SdFat/SdStream.h
  99. 599
      Libraries/SdFat/SdFat/SdVolume.cpp
  100. 234
      Libraries/SdFat/SdFat/SdVolume.h

171
Libraries/AdafruitLogger/AdafruitLogger.ino

@ -0,0 +1,171 @@
// A simple data logger for the Adafruit Data Logging shield on a mega
// You must edit SdFatConfig.h and set MEGA_SOFT_SPI nonzero
#include <SdFat.h>
#include <SdFatUtil.h> // define FreeRam()
#include <I2cMaster.h>
#include <SoftRTClib.h>
#define CHIP_SELECT 10 // SD chip select pin
#define LOG_INTERVAL 1000 // mills between entries
#define SENSOR_COUNT 3 // number of analog pins to log
#define ECHO_TO_SERIAL 1 // echo data to serial port if nonzero
#define WAIT_TO_START 1 // Wait for serial input in setup()
#define ADC_DELAY 10 // ADC delay for high impedence sensors
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
#if !MEGA_SOFT_SPI
#error set MEGA_SOFT_SPI nonzero in libraries/SdFat/SdFatConfig.h
#endif // MEGA_SOFT_SPI
// Is a Mega use analog pins 4, 5 for software I2C
const uint8_t RTC_SCL_PIN = 59;
const uint8_t RTC_SDA_PIN = 58;
SoftI2cMaster i2c(RTC_SDA_PIN, RTC_SCL_PIN);
#elif defined(__AVR_ATmega32U4__)
#if !LEONARDO_SOFT_SPI
#error set LEONARDO_SOFT_SPI nonzero in libraries/SdFat/SdFatConfig.h
#endif // LEONARDO_SOFT_SPI
// Is a Leonardo use analog pins 4, 5 for software I2C
const uint8_t RTC_SCL_PIN = 23;
const uint8_t RTC_SDA_PIN = 22;
SoftI2cMaster i2c(RTC_SDA_PIN, RTC_SCL_PIN);
#else // defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
// Not Mega use hardware I2C
// enable pull-ups on SDA/SCL
TwiMaster i2c(true);
#endif // defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
RTC_DS1307 RTC(&i2c); // define the Real Time Clock object
// file system object
SdFat sd;
// text file for logging
ofstream logfile;
// Serial print stream
ArduinoOutStream cout(Serial);
// buffer to format data - makes it eaiser to echo to Serial
char buf[80];
//------------------------------------------------------------------------------
#if SENSOR_COUNT > 6
#error SENSOR_COUNT too large
#endif // SENSOR_COUNT
//------------------------------------------------------------------------------
// store error strings in flash to save RAM
#define error(s) sd.errorHalt_P(PSTR(s))
//------------------------------------------------------------------------------
// call back for file timestamps
void dateTime(uint16_t* date, uint16_t* time) {
DateTime now = RTC.now();
// return date using FAT_DATE macro to format fields
*date = FAT_DATE(now.year(), now.month(), now.day());
// return time using FAT_TIME macro to format fields
*time = FAT_TIME(now.hour(), now.minute(), now.second());
}
//------------------------------------------------------------------------------
// format date/time
ostream& operator << (ostream& os, DateTime& dt) {
os << dt.year() << '/' << int(dt.month()) << '/' << int(dt.day()) << ',';
os << int(dt.hour()) << ':' << setfill('0') << setw(2) << int(dt.minute());
os << ':' << setw(2) << int(dt.second()) << setfill(' ');
return os;
}
//------------------------------------------------------------------------------
void setup() {
// For Leonardo
while (!Serial) {}
Serial.begin(9600);
// pstr stores strings in flash to save RAM
cout << endl << pstr("FreeRam: ") << FreeRam() << endl;
#if WAIT_TO_START
cout << pstr("Type any character to start\n");
while (Serial.read() < 0) {}
#endif // WAIT_TO_START
// connect to RTC
if (!RTC.begin()) error("RTC failed");
// set date time callback function
SdFile::dateTimeCallback(dateTime);
DateTime now = RTC.now();
cout << now << endl;
// initialize the SD card
if (!sd.begin(CHIP_SELECT)) sd.initErrorHalt();
// create a new file in root, the current working directory
char name[] = "LOGGER00.CSV";
for (uint8_t i = 0; i < 100; i++) {
name[6] = i/10 + '0';
name[7] = i%10 + '0';
if (sd.exists(name)) continue;
logfile.open(name);
break;
}
if (!logfile.is_open()) error("file.open");
cout << pstr("Logging to: ") << name << endl;
// format header in buffer
obufstream bout(buf, sizeof(buf));
bout << pstr("millis");
bout << pstr(",date,time");
for (uint8_t i = 0; i < SENSOR_COUNT; i++) {
bout << pstr(",sens") << int(i);
}
logfile << buf << endl;
#if ECHO_TO_SERIAL
cout << buf << endl;
#endif // ECHO_TO_SERIAL
}
//------------------------------------------------------------------------------
void loop() {
uint32_t m;
// wait for time to be a multiple of interval
do {
m = millis();
} while (m % LOG_INTERVAL);
// use buffer stream to format line
obufstream bout(buf, sizeof(buf));
// start with time in millis
bout << m;
DateTime now = RTC.now();
bout << ',' << now;
// read analog pins and format data
for (uint8_t ia = 0; ia < SENSOR_COUNT; ia++) {
#if ADC_DELAY
analogRead(ia);
delay(ADC_DELAY);
#endif // ADC_DELAY
bout << ',' << analogRead(ia);
}
bout << endl;
// log data and flush to SD
logfile << buf << flush;
// check for error
if (!logfile) error("write data failed");
#if ECHO_TO_SERIAL
cout << buf;
#endif // ECHO_TO_SERIAL
// don't log two points in the same millis
if (m == millis()) delay(1);
}

234
Libraries/AdafruitLogger/I2cMaster/I2cMaster.cpp

@ -0,0 +1,234 @@
/* Arduino I2cMaster Library
* Copyright (C) 2012 by William Greiman
*
* This file is part of the Arduino I2cMaster Library
*
* This Library is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This Library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the Arduino I2cMaster Library. If not, see
* <http://www.gnu.org/licenses/>.
*/
#include <I2cMaster.h>
//==============================================================================
// WARNING don't change SoftI2cMaster unless you verify the change with a scope
//------------------------------------------------------------------------------
/**
* Initialize SCL/SDA pins and set the bus high.
*
* \param[in] sdaPin The software SDA pin number.
*
* \param[in] sclPin The software SCL pin number.
*
* \param[in] enablePullup Enable pullup on DSA during read.
*/
SoftI2cMaster::SoftI2cMaster(uint8_t sdaPin, uint8_t sclPin,
bool enablePullup) {
sdaPin_ = sdaPin;
pinMode(sdaPin_, OUTPUT);
digitalWrite(sdaPin_, HIGH);
sclPin_ = sclPin;
pinMode(sclPin_, OUTPUT);
digitalWrite(sclPin_, HIGH);
}
//------------------------------------------------------------------------------
/** Read a byte and send Ack if more reads follow else Nak to terminate read.
*
* \param[in] last Set true to terminate the read else false.
*
* \return The byte read from the I2C bus.
*/
uint8_t SoftI2cMaster::read(uint8_t last) {
uint8_t b = 0;
pinMode(sdaPin_, INPUT);
// make sure pull-up enabled
digitalWrite(sdaPin_, HIGH);
// read byte
for (uint8_t i = 0; i < 8; i++) {
// don't change this loop unless you verify the change with a scope
b <<= 1;
delayMicroseconds(I2C_DELAY_USEC);
digitalWrite(sclPin_, HIGH);
if (digitalRead(sdaPin_)) b |= 1;
digitalWrite(sclPin_, LOW);
}
// send Ack or Nak
pinMode(sdaPin_, OUTPUT);
digitalWrite(sdaPin_, last);
digitalWrite(sclPin_, HIGH);
delayMicroseconds(I2C_DELAY_USEC);
digitalWrite(sclPin_, LOW);
digitalWrite(sdaPin_, LOW);
return b;
}
//------------------------------------------------------------------------------
/** Issue a restart condition.
*
* \param[in] addressRW I2C address with read/write bit.
*
* \return The value true, 1, for success or false, 0, for failure.
*/
bool SoftI2cMaster::restart(uint8_t addressRW) {
digitalWrite(sdaPin_, HIGH);
digitalWrite(sclPin_, HIGH);
delayMicroseconds(I2C_DELAY_USEC);
return start(addressRW);
}
//------------------------------------------------------------------------------
/** Issue a start condition.
*
* \param[in] addressRW I2C address with read/write bit.
*
* \return The value true, 1, for success or false, 0, for failure.
*/
bool SoftI2cMaster::start(uint8_t addressRW) {
digitalWrite(sdaPin_, LOW);
delayMicroseconds(I2C_DELAY_USEC);
digitalWrite(sclPin_, LOW);
return write(addressRW);
}
//------------------------------------------------------------------------------
/** Issue a stop condition. */
void SoftI2cMaster::stop(void) {
digitalWrite(sdaPin_, LOW);
delayMicroseconds(I2C_DELAY_USEC);
digitalWrite(sclPin_, HIGH);
delayMicroseconds(I2C_DELAY_USEC);
digitalWrite(sdaPin_, HIGH);
delayMicroseconds(I2C_DELAY_USEC);
}
//------------------------------------------------------------------------------
/**
* Write a byte.
*
* \param[in] data The byte to send.
*
* \return The value true, 1, if the slave returned an Ack or false for Nak.
*/
bool SoftI2cMaster::write(uint8_t data) {
// write byte
for (uint8_t m = 0X80; m != 0; m >>= 1) {
// don't change this loop unless you verify the change with a scope
digitalWrite(sdaPin_, m & data);
digitalWrite(sclPin_, HIGH);
delayMicroseconds(I2C_DELAY_USEC);
digitalWrite(sclPin_, LOW);
}
// get Ack or Nak
pinMode(sdaPin_, INPUT);
// enable pullup
digitalWrite(sdaPin_, HIGH);
digitalWrite(sclPin_, HIGH);
uint8_t rtn = digitalRead(sdaPin_);
digitalWrite(sclPin_, LOW);
pinMode(sdaPin_, OUTPUT);
digitalWrite(sdaPin_, LOW);
return rtn == 0;
}
//==============================================================================
void TwiMaster::execCmd(uint8_t cmdReg) {
// send command
TWCR = cmdReg;
// wait for command to complete
while (!(TWCR & (1 << TWINT)));
// status bits.
status_ = TWSR & 0xF8;
}
//------------------------------------------------------------------------------
/**
* Initialize hardware TWI.
*
* \param[in] enablePullup Enable the AVR internal pull-ups. The internal
* pull-ups can, in some systems, eliminate the need for external pull-ups.
*/
TwiMaster::TwiMaster(bool enablePullup) {
// no prescaler
TWSR = 0;
// set bit rate factor
TWBR = (F_CPU/F_TWI - 16)/2;
// enable pull-ups if requested
if (enablePullup) {
digitalWrite(SDA, HIGH);
digitalWrite(SCL, HIGH);
}
}
//------------------------------------------------------------------------------
/** Read a byte and send Ack if more reads follow else Nak to terminate read.
*
* \param[in] last Set true to terminate the read else false.
*
* \return The byte read from the I2C bus.
*/
uint8_t TwiMaster::read(uint8_t last) {
execCmd((1 << TWINT) | (1 << TWEN) | (last ? 0 : (1 << TWEA)));
return TWDR;
}
//------------------------------------------------------------------------------
/** Issue a restart condition.
*
* \param[in] addressRW I2C address with read/write bit.
*
* \return The value true, 1, for success or false, 0, for failure.
*/
bool TwiMaster::restart(uint8_t addressRW) {
return start(addressRW);
}
//------------------------------------------------------------------------------
/** Set bus speed.
*
* \param[in] fast Set speed to 400 kHz if true else 100 kHz.
*/
void TwiMaster::speed(bool fast) {
TWBR = fast ? (F_CPU/400000 -16)/2 : (F_CPU/100000 -16)/2;
}
//------------------------------------------------------------------------------
/** Issue a start condition.
*
* \param[in] addressRW I2C address with read/write bit.
*
* \return The value true for success or false for failure.
*/
bool TwiMaster::start(uint8_t addressRW) {
// send START condition
execCmd((1<<TWINT) | (1<<TWSTA) | (1<<TWEN));
if (status() != TWSR_START && status() != TWSR_REP_START) return false;
// send device address and direction
TWDR = addressRW;
execCmd((1 << TWINT) | (1 << TWEN));
if (addressRW & I2C_READ) {
return status() == TWSR_MRX_ADR_ACK;
} else {
return status() == TWSR_MTX_ADR_ACK;
}
}
//------------------------------------------------------------------------------
/** Issue a stop condition. */
void TwiMaster::stop(void) {
TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWSTO);
// wait until stop condition is executed and bus released
while (TWCR & (1 << TWSTO));
}
//------------------------------------------------------------------------------
/**
* Write a byte.
*
* \param[in] data The byte to send.
*
* \return The value true, 1, if the slave returned an Ack or false for Nak.
*/
bool TwiMaster::write(uint8_t data) {
TWDR = data;
execCmd((1 << TWINT) | (1 << TWEN));
return status() == TWSR_MTX_DATA_ACK;
}

256
Libraries/AdafruitLogger/I2cMaster/I2cMaster.h

@ -0,0 +1,256 @@
/* Arduino I2cMaster Library
* Copyright (C) 2012 by William Greiman
*
* This file is part of the Arduino I2cMaster Library
*
* This Library is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This Library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the Arduino I2cMaster Library. If not, see
* <http://www.gnu.org/licenses/>.
*/
#ifndef I2C_MASTER_H
#define I2C_MASTER_H
/**
* \file
* \brief Software I2C and hardware TWI library
*/
#if ARDUINO < 100
#error Requires Arduino 1.0 or greater.
#else // ARDUINO
#include <Arduino.h>
#endif // ARDUINO
//------------------------------------------------------------------------------
/** I2cMaster version YYYYMMDD */
#define I2C_MASTER_VERSION 20120716
//------------------------------------------------------------------------------
/** Default hardware I2C clock in Hz */
uint32_t const F_TWI = 400000L;
/** Delay used for software I2C */
uint8_t const I2C_DELAY_USEC = 4;
/** Bit to or with address for read start and read restart */
uint8_t const I2C_READ = 1;
/** Bit to or with address for write start and write restart */
uint8_t const I2C_WRITE = 0;
//------------------------------------------------------------------------------
// Status codes in TWSR - names are from Atmel TWSR.h with TWSR_ added
/** start condition transmitted */
uint8_t const TWSR_START = 0x08;
/** repeated start condition transmitted */
uint8_t const TWSR_REP_START = 0x10;
/** slave address plus write bit transmitted, ACK received */
uint8_t const TWSR_MTX_ADR_ACK = 0x18;
/** data transmitted, ACK received */
uint8_t const TWSR_MTX_DATA_ACK = 0x28;
/** slave address plus read bit transmitted, ACK received */
uint8_t const TWSR_MRX_ADR_ACK = 0x40;
//==============================================================================
/**
* \class I2cMasterBase
* \brief Base class for FastI2cMaster, SoftI2cMaster, and TwiMaster
*/
class I2cMasterBase {
public:
/** Read a byte
* \param[in] last send Ack if last is false else Nak to terminate read
* \return byte read from I2C bus
*/
virtual uint8_t read(uint8_t last) = 0;
/** Send new address and read/write bit without sending a stop.
* \param[in] addressRW i2c address with read/write bit
* \return true for success false for failure
*/
virtual bool restart(uint8_t addressRW) = 0;
/** Issue a start condition
* \param[in] addressRW i2c address with read/write bit
* \return true for success false for failure
*/
virtual bool start(uint8_t addressRW) = 0;
/** Issue a stop condition. */
virtual void stop(void) = 0;
/** Write a byte
* \param[in] data byte to write
* \return true for Ack or false for Nak */
virtual bool write(uint8_t data) = 0;
};
//==============================================================================
/**
* \class SoftI2cMaster
* \brief Software I2C master class
*/
class SoftI2cMaster : public I2cMasterBase {
public:
SoftI2cMaster(uint8_t sdaPin, uint8_t sclPin, bool enablePullup = true);
uint8_t read(uint8_t last);
bool restart(uint8_t addressRW);
bool start(uint8_t addressRW);
void stop(void);
bool write(uint8_t b);
private:
SoftI2cMaster() {}
bool enablePullup_;
uint8_t sdaPin_;
uint8_t sclPin_;
};
//==============================================================================
/**
* \class TwiMaster
* \brief Hardware I2C master class
*
* Uses ATmega TWI hardware port
*/
class TwiMaster : public I2cMasterBase {
public:
explicit TwiMaster(bool enablePullup);
uint8_t read(uint8_t last);
bool restart(uint8_t addressRW);
void speed(bool fast);
bool start(uint8_t addressRW);
/** \return status from last TWI command - useful for library debug */
uint8_t status(void) {return status_;}
void stop(void);
bool write(uint8_t data);
private:
TwiMaster() {}
uint8_t status_;
void execCmd(uint8_t cmdReg);
};
//==============================================================================
// experimental template based fast software I2C
#ifndef USE_FAST_I2C_MASTER
/** disable experimental fast software I2C */
#define USE_FAST_I2C_MASTER 0
#endif // USE_FAST_I2C_MASTER
#if USE_FAST_I2C_MASTER || DOXYGEN
#include <util/delay_basic.h>
#include <DigitalPin.h>
//------------------------------------------------------------------------------
/**
* \class FastI2cMaster
* \brief Fast software I2C master class
*/
template<uint8_t sdaPin, uint8_t sclPin, bool enablePullups = true>
class FastI2cMaster : public I2cMasterBase {
public:
FastI2cMaster() {
fastPinMode(sdaPin, OUTPUT);
fastDigitalWrite(sdaPin, HIGH);
fastPinMode(sclPin, OUTPUT);
fastDigitalWrite(sclPin, HIGH);
}
//----------------------------------------------------------------------------
uint8_t read(uint8_t last) {
uint8_t data = 0;
// make sure pull-up enabled
fastPinMode(sdaPin, INPUT);
fastDigitalWrite(sdaPin, enablePullups);
readBit(7, &data);
readBit(6, &data);
readBit(5, &data);
readBit(4, &data);
readBit(3, &data);
readBit(2, &data);
readBit(1, &data);
readBit(0, &data);
// send Ack or Nak
fastPinMode(sdaPin, OUTPUT);
fastDigitalWrite(sdaPin, last);
fastDigitalWrite(sclPin, HIGH);
sclDelay(3);
fastDigitalWrite(sclPin, LOW);
fastDigitalWrite(sdaPin, LOW);
return data;
}
//----------------------------------------------------------------------------
bool restart(uint8_t addressRW) {
fastDigitalWrite(sdaPin, HIGH);
sclDelay(8);
fastDigitalWrite(sclPin, HIGH);
sclDelay(8);
return start(addressRW);
}
//----------------------------------------------------------------------------
bool start(uint8_t addressRW) {
fastDigitalWrite(sdaPin, LOW);
sclDelay(8);
fastDigitalWrite(sclPin, LOW);
sclDelay(8);
return write(addressRW);
}
//----------------------------------------------------------------------------
void stop(void) {
fastDigitalWrite(sdaPin, LOW);
sclDelay(8);
fastDigitalWrite(sclPin, HIGH);
sclDelay(8);
fastDigitalWrite(sdaPin, HIGH);
sclDelay(8);
}
//----------------------------------------------------------------------------
bool write(uint8_t data) {
// write byte
writeBit(7, data);
writeBit(6, data);
writeBit(5, data);
writeBit(4, data);
writeBit(3, data);
writeBit(2, data);
writeBit(1, data);
writeBit(0, data);
// get Ack or Nak
fastPinMode(sdaPin, INPUT);
// enable pullup
fastDigitalWrite(sdaPin, HIGH);
fastDigitalWrite(sclPin, HIGH);
sclDelay(3);
uint8_t rtn = fastDigitalRead(sdaPin);
fastDigitalWrite(sclPin, LOW);
fastPinMode(sdaPin, OUTPUT);
fastDigitalWrite(sdaPin, LOW);
return rtn == 0;
}
//----------------------------------------------------------------------------
private:
inline __attribute__((always_inline))
void readBit(uint8_t bit, uint8_t* data) {
fastDigitalWrite(sclPin, HIGH);
sclDelay(3);
if (fastDigitalRead(sdaPin)) *data |= 1 << bit;
fastDigitalWrite(sclPin, LOW);
sclDelay(7);
}
//----------------------------------------------------------------------------
void sclDelay(uint8_t n) {
_delay_loop_1(n);
}
//----------------------------------------------------------------------------
inline __attribute__((always_inline))
void writeBit(uint8_t bit, uint8_t data) {
fastDigitalWrite(sdaPin, data & (1 << bit));
fastDigitalWrite(sclPin, HIGH);
sclDelay(4);
fastDigitalWrite(sclPin, LOW);
sclDelay(6);
}
};
#endif // USE_FAST_I2C_MASTER
#endif // I2C_MASTER_H

53
Libraries/AdafruitLogger/I2cMaster/examples/i2cScanAddress/i2cScanAddress.ino

@ -0,0 +1,53 @@
// Warning only use this for hardware debug!
// See which addresses respond to a start condition.
#include <I2cMaster.h>
// select software or hardware i2c
#define USE_SOFT_I2C 0
#if USE_SOFT_I2C
// use analog pins 4 and 5 for this example
// this allows a 328 shield to be used on the Mega
// edit next two line to change pins
const uint8_t SDA_PIN = A4;
const uint8_t SCL_PIN = A5;
// An instance of class for software master
SoftI2cMaster i2c(SDA_PIN, SCL_PIN);
#else // USE_SOFT_I2C
// hardware master with pullups enabled
TwiMaster i2c(true);
#endif // USE_SOFT_I2C
//------------------------------------------------------------------------------
void setup(void) {
Serial.begin(9600);
uint8_t add = 0;
// try read
do {
if (i2c.start(add | I2C_READ)) {
Serial.print("Add read: 0X");
Serial.println(add, HEX);
i2c.read(true);
}
i2c.stop();
add += 2;
} while (add);
// try write
add = 0;
do {
if (i2c.start(add | I2C_WRITE)) {
Serial.print("Add write: 0X");
Serial.println(add, HEX);
}
i2c.stop();
add += 2;
} while (add);
Serial.println("Done");
}
void loop(void){}

53
Libraries/AdafruitLogger/I2cMaster/examples/i2cScopeTest/i2cScopeTest.ino

@ -0,0 +1,53 @@
// this sketch is for tweaking soft i2c signals
// Uncomment next two lines to test fast software I2C
// #include <DigitalPin.h>
// #define USE_FAST_I2C_MASTER 1
#include <I2cMaster.h>
const uint8_t sdaPin = 8;
const uint8_t sclPin = 7;
#if USE_FAST_I2C_MASTER
FastI2cMaster<sdaPin, sclPin, false> i2c;
#else // USE_FAST_I2C_MASTER
SoftI2cMaster i2c(sdaPin, sclPin);
#endif
// also test base class idea
I2cMasterBase *bus;
uint8_t mode;
//------------------------------------------------------------------------------
void setup(void) {
Serial.begin(9600);
// convert softI2cMaster to TwoWireBase to test base class idea
bus = &i2c;
Serial.println("enter 0 for write, 1 for read, 2 for start/stop");
while (!Serial.available());
mode = Serial.read();
if (mode == '0') {
Serial.println("Write Mode");
} else if (mode == '1') {
Serial.println("Read Mode");
} else if (mode == '2' ) {
Serial.println("Start/Stop");
} else {
Serial.println("Bad Option");
while(1);
}
}
//------------------------------------------------------------------------------
void loop(void) {
if (mode == '0') {
bus->write(0X55);
} else if (mode == '1') {
bus->read(0);
} else {
bus->start(0XAA);
bus->stop();
}
delay(1);
}

321
Libraries/AdafruitLogger/I2cMaster/examples/softDS1307/softDS1307.ino

@ -0,0 +1,321 @@
// Utility sketch to explore DS1307 and
// demonstrate SoftI2cMaster and TwiMaster
//
#include <avr/pgmspace.h>
#include <I2cMaster.h>
// select software or hardware i2c
#define USE_SOFT_I2C 1
#if USE_SOFT_I2C
// use analog pins 4 and 5 for this example
// this allows a 328 shield to be used on the Mega
// edit next two line to change pins
const uint8_t SDA_PIN = A4;
const uint8_t SCL_PIN = A5;
// An instance of class for software master
SoftI2cMaster rtc(SDA_PIN, SCL_PIN);
#else // USE_SOFT_I2C
// Pins for DS1307 with hardware i2c
// connect SCL to Arduino 168/328 analog pin 5
// connect SDA to Arduino 168/328 analog pin 4
// Instance of class for hardware master with pullups enabled
TwiMaster rtc(true);
#endif // USE_SOFT_I2C
// i2c 8-bit address for DS1307. low bit is read/write
#define DS1307ADDR 0XD0
//------------------------------------------------------------------------------
/*
* Read 'count' bytes from the DS1307 starting at 'address'
*/
uint8_t readDS1307(uint8_t address, uint8_t *buf, uint8_t count) {
// issue a start condition, send device address and write direction bit
if (!rtc.start(DS1307ADDR | I2C_WRITE)) return false;
// send the DS1307 address
if (!rtc.write(address)) return false;
// issue a repeated start condition, send device address and read direction bit
if (!rtc.restart(DS1307ADDR | I2C_READ))return false;
// read data from the DS1307
for (uint8_t i = 0; i < count; i++) {
// send Ack until last byte then send Ack
buf[i] = rtc.read(i == (count-1));
}
// issue a stop condition
rtc.stop();
return true;
}
//------------------------------------------------------------------------------
/*
* write 'count' bytes to DS1307 starting at 'address'
*/
uint8_t writeDS1307(uint8_t address, uint8_t *buf, uint8_t count) {
// issue a start condition, send device address and write direction bit
if (!rtc.start(DS1307ADDR | I2C_WRITE)) return false;
// send the DS1307 address
if (!rtc.write(address)) return false;
// send data to the DS1307
for (uint8_t i = 0; i < count; i++) {
if (!rtc.write(buf[i])) return false;
}
// issue a stop condition
rtc.stop();
return true;
}
//------------------------------------------------------------------------------
void setup(void) {
Serial.begin(9600);
}
//------------------------------------------------------------------------------
/** Store and print a string in flash memory.*/
#define PgmPrint(x) Serial.print(F(x))
/** Store and print a string in flash memory followed by a CR/LF.*/
#define PgmPrintln(x) Serial.println(F(x))
//-------------------------------------------------------------------------------
// day of week U.S. convention
char *Ddd[] = {"Bad", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
//------------------------------------------------------------------------------
void hexPrint(uint8_t v) {
Serial.print(v >> 4, HEX);
Serial.print(v & 0XF, HEX);
}
//------------------------------------------------------------------------------
void hexPrintln(uint8_t v) {
hexPrint(v);
Serial.println();
}
//------------------------------------------------------------------------------
// read hex input
uint8_t hexRead(uint16_t* v) {
uint16_t n = 0;
while (!Serial.available());
while (Serial.available()) {
uint8_t c = Serial.read();
// exit if end-of-line
if (c == '\n' || c == '\r') break;
n <<= 4;
if ('a' <= c && c <= 'f') {
n += c - ('a' - 10);
}
else if ('A' <= c && c <= 'F') {
n += c - ('A' - 10);
}
else if ('0' <= c && c <= '9') {
n += c - '0';
}
else {
PgmPrintln("Invalid entry");
return false;
}
delay(10);
}
*v = n;
return true;
}
//------------------------------------------------------------------------------
uint8_t bcdRead(uint8_t min, uint8_t max, uint8_t* n) {
uint16_t v;
if (!hexRead(&v)) return false;
uint8_t d = 10 * (v >> 4) + (v & 0XF);
if ((v >> 4) > 9 || (v & 0XF) > 9 || d < min || d > max) {
PgmPrintln("Invalid");
return false;
}
*n = v;
return true;
}
//------------------------------------------------------------------------------
void displayTime(void) {
uint8_t r[8];
if (!readDS1307(0, r, 8)) {
PgmPrintln("Read Failed for display time");
return;
}
PgmPrint("The current time is 20");
// year
hexPrint(r[6]);
Serial.write('-');
// month
hexPrint(r[5]);
Serial.write('-');
// day
hexPrint(r[4]);
Serial.write(' ');
Serial.print(Ddd[r[3] < 8 ? r[3] : 0]);
Serial.write(' ');
// hour
hexPrint(r[2]);
Serial.write(':');
// minute
hexPrint(r[1]);
Serial.write(':');
// second
hexPrintln(r[0]);
PgmPrint("Control: ");
hexPrintln(r[7]);
}
//------------------------------------------------------------------------------
// dump registers and 56 bytes of RAM
void dumpAll(void) {
uint8_t buf[8];
for (uint8_t a = 0; a < 64; a += 8) {
hexPrint(a);
Serial.write(' ');
if (!readDS1307(a, buf, 8)) {
PgmPrint("read failed for dumpAll");
return;
}
for (uint8_t i = 0; i < 8; i++) {
Serial.write(' ');
hexPrint(buf[i]);
}
Serial.println();
}
}
//------------------------------------------------------------------------------
// set control register
/*
The DS1307 control register is used to control the operation of the SQW/OUT pin.
+-----------------------------------------------+
|BIT 7|BIT 6|BIT 5|BIT 4|BIT 3|BIT 2|BIT 1|BIT 0|
+-----------------------------------------------+
|OUT | 0 | 0 |SQWE | 0 | 0 | RS1 | RS0 |
+-----------------------------------------------+
OUT (Output control): This bit controls the output level of the SQW/OUT pin
when the square wave output is disabled. If SQWE = 0, the logic level on the
SQW/OUT pin is 1 if OUT = 1 and is 0 if OUT = 0.
SQWE (Square Wave Enable): This bit, when set to a logic 1, will enable the
oscillator output. The frequency of the square wave output depends upon the
value of the RS0 and RS1 bits. With the square wave output set to 1Hz, the
clock registers update on the falling edge of the square wave.
Square wave Output Frequency for SQWE = 1.
RS1 RS0 FREQUENCY
0 0 1Hz
0 1 4.096kHz
1 0 8.192kHz
1 1 32.768kHz
*/
void setControl(void) {
PgmPrintln("SQW/OUT pin: ");
PgmPrintln("(00) Low");
PgmPrintln("(10) 1Hz");
PgmPrintln("(11) 4.096kHz");
PgmPrintln("(12) 8.192kHz");
PgmPrintln("(13) 32.768kHz");
PgmPrintln("(80) High");
PgmPrint("Enter control: ");
uint16_t r;
if (!hexRead(&r)) return;
hexPrintln(r);
if (!writeDS1307(7, (uint8_t *)&r, 1)) {
PgmPrint("Write Failed for setControl");
}
}
//------------------------------------------------------------------------------
void setDate(void) {
uint8_t r[4];
PgmPrint("Enter year (00-99): ");
if (!bcdRead(0, 99, &r[3])) return;
hexPrintln(r[3]);
PgmPrint("Enter month (01-12): ");
if (!bcdRead(1, 12, &r[2])) return;
hexPrintln(r[2]);
PgmPrint("Enter day (01-31): ");
if (!bcdRead(1, 31, &r[1])) return;
hexPrintln(r[1]);
PgmPrint("Enter day of week (01-07): ");
if (!bcdRead(1, 7, &r[0])) return;
hexPrintln(r[0]);
if (!writeDS1307(3, r, 4)) {
PgmPrintln("Write failed for setDate");
}
}
//------------------------------------------------------------------------------
void setNvRam(void) {
uint8_t buf[8];
PgmPrint("Enter HEX value for all NV RAM locations (00-FF): ");
uint16_t v;
if (!hexRead(&v)) return;
hexPrint(v);
for (uint8_t a = 8; a < 64; a ++) {
if (!writeDS1307(a, (uint8_t *)&v, 1)) {
PgmPrintln("write failed for setNvRam");
}
}
}
//------------------------------------------------------------------------------
void setTime(void) {
uint8_t r[3];
PgmPrint("Enter hours (00-23): ");
if (!bcdRead(0, 23, &r[2])) return;
hexPrintln(r[2]);
PgmPrint("Enter minutes (00-59): ");
if (!bcdRead(0, 59, &r[1])) return;
hexPrintln(r[1]);
PgmPrint("Enter seconds (00-59): ");
if (!bcdRead(0, 59, &r[0])) return;
hexPrintln(r[0]);
if (!writeDS1307(0, r, 3)) {
PgmPrintln("write failed in setTime");
return;
}
}
//------------------------------------------------------------------------------
void loop(void) {
Serial.println();
displayTime();
while (Serial.read() >= 0) {}
PgmPrintln("\nOptions are:");
PgmPrintln("(0) Display date and time");
PgmPrintln("(1) Set time");
PgmPrintln("(2) Set date");
PgmPrintln("(3) Set Control");
PgmPrintln("(4) Dump all");
PgmPrintln("(5) Set NV RAM");
PgmPrint("Enter option: ");
uint16_t n;
if (!hexRead(&n)) return;
Serial.println(n, DEC);
if (n == 0) return;
Serial.println();
if (n == 1) {
setTime();
}
else if (n == 2) {
setDate();
}
else if (n == 3) {
setControl();
}
else if (n == 4) {
dumpAll();
}
else if (n == 5) {
setNvRam();
}
else {
PgmPrintln("Invalid option");
}
}

119
Libraries/AdafruitLogger/SdFat/ArduinoStream.h

@ -0,0 +1,119 @@
/* Arduino SdFat Library
* Copyright (C) 2012 by William Greiman
*
* This file is part of the Arduino SdFat Library
*
* This Library is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This Library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the Arduino SdFat Library. If not, see
* <http://www.gnu.org/licenses/>.
*/
#ifndef ArduinoStream_h
#define ArduinoStream_h
/**
* \file
* \brief ArduinoInStream and ArduinoOutStream classes
*/
#include <bufstream.h>
//==============================================================================
/**
* \class ArduinoInStream
* \brief Input stream for Arduino Stream objects
*/
class ArduinoInStream : public ibufstream {
public:
/**
* Constructor
* \param[in] hws hardware stream
* \param[in] buf buffer for input line
* \param[in] size size of input buffer
*/
ArduinoInStream(Stream &hws, char* buf, size_t size) {
hw_ = &hws;
line_ = buf;
size_ = size;
}
/** read a line. */
void readline() {
size_t i = 0;
uint32_t t;
line_[0] = '\0';
while (!hw_->available());
while (1) {
t = millis();
while (!hw_->available()) {
if ((millis() - t) > 10) goto done;
}
if (i >= (size_ - 1)) {
setstate(failbit);
return;
}
line_[i++] = hw_->read();
line_[i] = '\0';
}
done:
init(line_);
}
protected:
/** Internal - do not use.
* \param[in] off
* \param[in] way
* \return true/false.
*/
bool seekoff(off_type off, seekdir way) {return false;}
/** Internal - do not use.
* \param[in] pos
* \return true/false.
*/
bool seekpos(pos_type pos) {return false;}
private:
char *line_;
size_t size_;
Stream* hw_;
};
//==============================================================================
/**
* \class ArduinoOutStream
* \brief Output stream for Arduino Print objects
*/
class ArduinoOutStream : public ostream {
public:
/** constructor
*
* \param[in] pr Print object for this ArduinoOutStream.
*/
explicit ArduinoOutStream(Print& pr) : pr_(&pr) {}
protected:
/// @cond SHOW_PROTECTED
/**
* Internal do not use
* \param[in] c
*/
void putch(char c) {
if (c == '\n') pr_->write('\r');
pr_->write(c);
}
void putstr(const char* str) {pr_->write(str);}
bool seekoff(off_type off, seekdir way) {return false;}
bool seekpos(pos_type pos) {return false;}
bool sync() {return true;}
pos_type tellpos() {return 0;}
/// @endcond
private:
ArduinoOutStream() {}
Print* pr_;
};
#endif // ArduinoStream_h

71
Libraries/AdafruitLogger/SdFat/MinimumSerial.cpp

@ -0,0 +1,71 @@
/* Arduino SdFat Library
* Copyright (C) 2012 by William Greiman
*
* This file is part of the Arduino SdFat Library
*
* This Library is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This Library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the Arduino SdFat Library. If not, see
* <http://www.gnu.org/licenses/>.
*/
#include <Arduino.h>
#if defined(UDR0) || defined(DOXYGEN)
#include <MinimumSerial.h>
//------------------------------------------------------------------------------
/**
* Set baud rate for serial port zero and enable in non interrupt mode.
* Do not call this function if you use another serial library.
* \param[in] baud rate
*/
void MinimumSerial::begin(unsigned long baud) {
uint16_t baud_setting;
// don't worry, the compiler will squeeze out F_CPU != 16000000UL
if (F_CPU != 16000000UL || baud != 57600) {
// Double the USART Transmission Speed
UCSR0A = 1 << U2X0;
baud_setting = (F_CPU / 4 / baud - 1) / 2;
} else {
// hardcoded exception for compatibility with the bootloader shipped
// with the Duemilanove and previous boards and the firmware on the 8U2
// on the Uno and Mega 2560.
UCSR0A = 0;
baud_setting = (F_CPU / 8 / baud - 1) / 2;
}
// assign the baud_setting
UBRR0H = baud_setting >> 8;
UBRR0L = baud_setting;
// enable transmit and receive
UCSR0B |= (1 << TXEN0) | (1 << RXEN0) ;
}
//------------------------------------------------------------------------------
/**
* Unbuffered read
* \return -1 if no character is available or an available character.
*/
int MinimumSerial::read() {
if (UCSR0A & (1 << RXC0)) return UDR0;
return -1;
}
//------------------------------------------------------------------------------
/**
* Unbuffered write
*
* \param[in] b byte to write.
* \return 1
*/
size_t MinimumSerial::write(uint8_t b) {
while (((1 << UDRIE0) & UCSR0B) || !(UCSR0A & (1 << UDRE0))) {}
UDR0 = b;
return 1;
}
MinimumSerial MiniSerial;
#endif // defined(UDR0) || defined(DOXYGEN)

36
Libraries/AdafruitLogger/SdFat/MinimumSerial.h

@ -0,0 +1,36 @@
/* Arduino SdFat Library
* Copyright (C) 2012 by William Greiman
*
* This file is part of the Arduino SdFat Library
*
* This Library is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This Library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the Arduino SdFat Library. If not, see
* <http://www.gnu.org/licenses/>.
*/
#ifndef MinimumSerial_h
#define MinimumSerial_h
/**
* \class MinimumSerial
* \brief mini serial class for the %SdFat library.
*/
class MinimumSerial : public Print {
public:
void begin(unsigned long);
int read();
size_t write(uint8_t b);
using Print::write;
};
#ifdef UDR0
extern MinimumSerial MiniSerial;
#endif // UDR0
#endif // MinimumSerial_h

1199
Libraries/AdafruitLogger/SdFat/Sd2Card.cpp

File diff suppressed because it is too large

233
Libraries/AdafruitLogger/SdFat/Sd2Card.h

@ -0,0 +1,233 @@
/* Arduino Sd2Card Library
* Copyright (C) 2012 by William Greiman
*
* This file is part of the Arduino Sd2Card Library
*
* This Library is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This Library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the Arduino Sd2Card Library. If not, see
* <http://www.gnu.org/licenses/>.
*/
#ifndef Sd2Card_h
#define Sd2Card_h
/**
* \file
* \brief Sd2Card class for V2 SD/SDHC cards
*/
#include <Arduino.h>
#include <SdFatConfig.h>
#include <SdInfo.h>
//------------------------------------------------------------------------------
// SPI speed is F_CPU/2^(1 + index), 0 <= index <= 6
/** Set SCK to max rate of F_CPU/2. See Sd2Card::setSckRate(). */
uint8_t const SPI_FULL_SPEED = 0;
/** Set SCK rate to F_CPU/3 for Due */
uint8_t const SPI_DIV3_SPEED = 1;
/** Set SCK rate to F_CPU/4. See Sd2Card::setSckRate(). */
uint8_t const SPI_HALF_SPEED = 2;
/** Set SCK rate to F_CPU/6 for Due */
uint8_t const SPI_DIV6_SPEED = 3;
/** Set SCK rate to F_CPU/8. See Sd2Card::setSckRate(). */
uint8_t const SPI_QUARTER_SPEED = 4;
/** Set SCK rate to F_CPU/16. See Sd2Card::setSckRate(). */
uint8_t const SPI_EIGHTH_SPEED = 6;
/** Set SCK rate to F_CPU/32. See Sd2Card::setSckRate(). */
uint8_t const SPI_SIXTEENTH_SPEED = 8;
/** MAX rate test - see spiInit for a given chip for details */
const uint8_t MAX_SCK_RATE_ID = 14;
//------------------------------------------------------------------------------
/** init timeout ms */
uint16_t const SD_INIT_TIMEOUT = 2000;
/** erase timeout ms */
uint16_t const SD_ERASE_TIMEOUT = 10000;
/** read timeout ms */
uint16_t const SD_READ_TIMEOUT = 300;
/** write time out ms */
uint16_t const SD_WRITE_TIMEOUT = 600;
//------------------------------------------------------------------------------
// SD card errors
/** timeout error for command CMD0 (initialize card in SPI mode) */
uint8_t const SD_CARD_ERROR_CMD0 = 0X1;
/** CMD8 was not accepted - not a valid SD card*/
uint8_t const SD_CARD_ERROR_CMD8 = 0X2;
/** card returned an error response for CMD12 (write stop) */
uint8_t const SD_CARD_ERROR_CMD12 = 0X3;
/** card returned an error response for CMD17 (read block) */
uint8_t const SD_CARD_ERROR_CMD17 = 0X4;
/** card returned an error response for CMD18 (read multiple block) */
uint8_t const SD_CARD_ERROR_CMD18 = 0X5;
/** card returned an error response for CMD24 (write block) */
uint8_t const SD_CARD_ERROR_CMD24 = 0X6;
/** WRITE_MULTIPLE_BLOCKS command failed */
uint8_t const SD_CARD_ERROR_CMD25 = 0X7;
/** card returned an error response for CMD58 (read OCR) */
uint8_t const SD_CARD_ERROR_CMD58 = 0X8;
/** SET_WR_BLK_ERASE_COUNT failed */
uint8_t const SD_CARD_ERROR_ACMD23 = 0X9;
/** ACMD41 initialization process timeout */
uint8_t const SD_CARD_ERROR_ACMD41 = 0XA;
/** card returned a bad CSR version field */
uint8_t const SD_CARD_ERROR_BAD_CSD = 0XB;
/** erase block group command failed */
uint8_t const SD_CARD_ERROR_ERASE = 0XC;
/** card not capable of single block erase */
uint8_t const SD_CARD_ERROR_ERASE_SINGLE_BLOCK = 0XD;
/** Erase sequence timed out */
uint8_t const SD_CARD_ERROR_ERASE_TIMEOUT = 0XE;
/** card returned an error token instead of read data */
uint8_t const SD_CARD_ERROR_READ = 0XF;
/** read CID or CSD failed */
uint8_t const SD_CARD_ERROR_READ_REG = 0X10;
/** timeout while waiting for start of read data */
uint8_t const SD_CARD_ERROR_READ_TIMEOUT = 0X11;
/** card did not accept STOP_TRAN_TOKEN */
uint8_t const SD_CARD_ERROR_STOP_TRAN = 0X12;
/** card returned an error token as a response to a write operation */
uint8_t const SD_CARD_ERROR_WRITE = 0X13;
/** attempt to write protected block zero */
uint8_t const SD_CARD_ERROR_WRITE_BLOCK_ZERO = 0X14; // REMOVE - not used
/** card did not go ready for a multiple block write */
uint8_t const SD_CARD_ERROR_WRITE_MULTIPLE = 0X15;
/** card returned an error to a CMD13 status check after a write */
uint8_t const SD_CARD_ERROR_WRITE_PROGRAMMING = 0X16;
/** timeout occurred during write programming */
uint8_t const SD_CARD_ERROR_WRITE_TIMEOUT = 0X17;
/** incorrect rate selected */
uint8_t const SD_CARD_ERROR_SCK_RATE = 0X18;
/** init() not called */
uint8_t const SD_CARD_ERROR_INIT_NOT_CALLED = 0X19;
/** card returned an error for CMD59 (CRC_ON_OFF) */
uint8_t const SD_CARD_ERROR_CMD59 = 0X1A;
/** invalid read CRC */
uint8_t const SD_CARD_ERROR_READ_CRC = 0X1B;
/** SPI DMA error */
uint8_t const SD_CARD_ERROR_SPI_DMA = 0X1C;
//------------------------------------------------------------------------------
// card types
/** Standard capacity V1 SD card */
uint8_t const SD_CARD_TYPE_SD1 = 1;
/** Standard capacity V2 SD card */
uint8_t const SD_CARD_TYPE_SD2 = 2;
/** High Capacity SD card */
uint8_t const SD_CARD_TYPE_SDHC = 3;
/**
* define SOFTWARE_SPI to use bit-bang SPI
*/
//------------------------------------------------------------------------------
#if LEONARDO_SOFT_SPI && defined(__AVR_ATmega32U4__) && !defined(CORE_TEENSY)
#define SOFTWARE_SPI
#elif MEGA_SOFT_SPI&&(defined(__AVR_ATmega1280__)||defined(__AVR_ATmega2560__))
#define SOFTWARE_SPI
#elif USE_SOFTWARE_SPI
#define SOFTWARE_SPI
#endif // LEONARDO_SOFT_SPI
//------------------------------------------------------------------------------
// define default chip select pin
//
#ifndef SOFTWARE_SPI
// hardware pin defs
/** The default chip select pin for the SD card is SS. */
uint8_t const SD_CHIP_SELECT_PIN = SS;
#else // SOFTWARE_SPI
/** SPI chip select pin */
uint8_t const SD_CHIP_SELECT_PIN = SOFT_SPI_CS_PIN;
#endif // SOFTWARE_SPI
//------------------------------------------------------------------------------
/**
* \class Sd2Card
* \brief Raw access to SD and SDHC flash memory cards.
*/
class Sd2Card {
public:
/** Construct an instance of Sd2Card. */
Sd2Card() : errorCode_(SD_CARD_ERROR_INIT_NOT_CALLED), type_(0) {}
uint32_t cardSize();
bool erase(uint32_t firstBlock, uint32_t lastBlock);
bool eraseSingleBlockEnable();
/**
* Set SD error code.
* \param[in] code value for error code.
*/
void error(uint8_t code) {errorCode_ = code;}
/**
* \return error code for last error. See Sd2Card.h for a list of error codes.
*/
int errorCode() const {return errorCode_;}
/** \return error data for last error. */
int errorData() const {return status_;}
/**
* Initialize an SD flash memory card with default clock rate and chip
* select pin. See sd2Card::init(uint8_t sckRateID, uint8_t chipSelectPin).
*
* \return true for success or false for failure.
*/
bool init(uint8_t sckRateID = SPI_FULL_SPEED,
uint8_t chipSelectPin = SD_CHIP_SELECT_PIN);
bool readBlock(uint32_t block, uint8_t* dst);
/**
* Read a card