Initial Commit

chipKIT code and libraries for OpenBCI 32bit Board.
Please see read me for setup and known issues.
Esse commit está contido em:
biomurph
2014-12-08 11:03:43 -05:00
commit 727d85a3e1
26 arquivos alterados com 7543 adições e 0 exclusões
@@ -0,0 +1,186 @@
//
// Definitions_32.h
//
//
// Created by Conor Russomanno, Luke Travis, and Joel Murphy. Summer 2013.
// Modified by Joel Murphy, Summer 2014
//
//
#ifndef _Definitions_32_h
#define _Definitions_32_h
//PIN CONNECTIONS
#define ADS_DRDY 9 // ADS data ready pin
#define ADS_RST 4 // ADS reset pin
#define ADS_SS 8 // ADS chip select
#define DAISY_SS 3 // ADS Daisy chip select
#define SD_SS 2 // SD card chip select
#define LIS3DH_SS 1 // LIS3DH chip select
#define LIS3DH_DRDY 0 // LIS3DH data ready pin
#define LIS3DH_MODE 3 // c pol =1, c pha = 1, mode = 3
#define SD_MODE 0
#define ADS_MODE 1
//ADS1299 SPI Command Definition Byte Assignments
#define _WAKEUP 0x02 // Wake-up from standby mode
#define _STANDBY 0x04 // Enter Standby mode
#define _RESET 0x06 // Reset the device registers to default
#define _START 0x08 // Start and restart (synchronize) conversions
#define _STOP 0x0A // Stop conversion
#define _RDATAC 0x10 // Enable Read Data Continuous mode (default mode at power-up)
#define _SDATAC 0x11 // Stop Read Data Continuous mode
#define _RDATA 0x12 // Read data by command; supports multiple read back
//ASD1299 Register Addresses
#define ID 0x00
#define CONFIG1 0x01
#define CONFIG2 0x02
#define CONFIG3 0x03
#define LOFF 0x04
#define CH1SET 0x05
#define CH2SET 0x06
#define CH3SET 0x07
#define CH4SET 0x08
#define CH5SET 0x09
#define CH6SET 0x0A
#define CH7SET 0x0B
#define CH8SET 0x0C
#define BIAS_SENSP 0x0D
#define BIAS_SENSN 0x0E
#define LOFF_SENSP 0x0F
#define LOFF_SENSN 0x10
#define LOFF_FLIP 0x11
#define LOFF_STATP 0x12
#define LOFF_STATN 0x13
#define GPIO 0x14
#define MISC1 0x15
#define MISC2 0x16
#define CONFIG4 0x17
#define OUTPUT_NOTHING (0) // quiet
#define OUTPUT_8_CHAN (1) // not using Daisy module
#define OUTPUT_16_CHAN (2) // using Daisy module
#define ON_BOARD (8) // slave address for on board ADS
#define ON_DAISY (3) // slave address for daisy ADS
// CHANNEL SETTINGS
#define POWER_DOWN (0)
#define GAIN_SET (1)
#define INPUT_TYPE_SET (2)
#define BIAS_SET (3)
#define SRB2_SET (4)
#define SRB1_SET (5)
#define YES (0x01)
#define NO (0x00)
//gainCode choices
#define ADS_GAIN01 (0b00000000) // 0x00
#define ADS_GAIN02 (0b00010000) // 0x10
#define ADS_GAIN04 (0b00100000) // 0x20
#define ADS_GAIN06 (0b00110000) // 0x30
#define ADS_GAIN08 (0b01000000) // 0x40
#define ADS_GAIN12 (0b01010000) // 0x50
#define ADS_GAIN24 (0b01100000) // 0x60
//inputType choices
#define ADSINPUT_NORMAL (0b00000000)
#define ADSINPUT_SHORTED (0b00000001)
#define ADSINPUT_BIAS_MEAS (0b00000010)
#define ADSINPUT_MVDD (0b00000011)
#define ADSINPUT_TEMP (0b00000100)
#define ADSINPUT_TESTSIG (0b00000101)
#define ADSINPUT_BIAS_DRP (0b00000110)
#define ADSINPUT_BIAL_DRN (0b00000111)
//test signal choices...ADS1299 datasheet page 41
#define ADSTESTSIG_AMP_1X (0b00000000)
#define ADSTESTSIG_AMP_2X (0b00000100)
#define ADSTESTSIG_PULSE_SLOW (0b00000000)
#define ADSTESTSIG_PULSE_FAST (0b00000001)
#define ADSTESTSIG_DCSIG (0b00000011)
#define ADSTESTSIG_NOCHANGE (0b11111111)
//Lead-off signal choices
#define LOFF_MAG_6NA (0b00000000)
#define LOFF_MAG_24NA (0b00000100)
#define LOFF_MAG_6UA (0b00001000)
#define LOFF_MAG_24UA (0b00001100)
#define LOFF_FREQ_DC (0b00000000)
#define LOFF_FREQ_7p8HZ (0b00000001)
#define LOFF_FREQ_31p2HZ (0b00000010)
#define LOFF_FREQ_FS_4 (0b00000011)
#define PCHAN (0)
#define NCHAN (1)
#define OFF (0)
#define ON (1)
// used for channel settings
#define ACTIVATE_SHORTED (2)
#define ACTIVATE (1)
#define DEACTIVATE (0)
#define PCKT_START 0xA0 // prefix for data packet error checking
#define PCKT_END 0xC0 // postfix for data packet error checking
//LIS3DH
#define READ_REG 0x80
#define READ_MULTI 0x40
#define LIS3DH_DRDY 3
#define LIS3DH_MODE 3 // c pol =1, c pha = 1, mode = 3
#define STATUS_REG_AUX 0x07 // axis over-run and data available flags (see 0x27)
#define OUT_ADC1_L 0x08 //
#define OUT_ADC1_H 0x09 //
#define OUT_ADC2_L 0x0A // ADC input values (check DS)
#define OUT_ADC2_H 0x0B //
#define OUT_ADC3_L 0x0C //
#define OUT_ADC3_H 0x0D //
#define INT_COUNTER_REG 0x0E // ??
#define WHO_AM_I 0x0F // DEVICE ID = 0x33
#define TMP_CFG_REG 0x1F // ADC enable (0x80); Temperature sensor enable (0x40)
#define CTRL_REG1 0x20 // Data Rate; Power Mode; X enable; Y enable; Z enable (on >= 0x10)
#define CTRL_REG2 0x21 // High Pass Filter Stuph
#define CTRL_REG3 0x22 // INT1 select register
#define CTRL_REG4 0x23 // Block update timing; endian; G-force; resolution; self test; SPI pins
#define CTRL_REG5 0x24 // reboot; FIFO enable; latch; 4D detection;
#define CTRL_REG6 0x25 // ??
#define REFERENCE 0x26 // interrupt reference
#define STATUS_REG2 0x27 // axis overrun and availale flags (see 0x07)
#define OUT_X_L 0x28 //
#define OUT_X_H 0x29 //
#define OUT_Y_L 0x2A // tripple axis values (see 0x0A)
#define OUT_Y_H 0x2B //
#define OUT_Z_L 0x2C //
#define OUT_Z_H 0x2D //
#define FIFO_CTRL_REG 0x2E // FIFO mode; trigger output pin select (?);
#define FIFO_SRC_REG 0x2F // ??
#define INT1_CFG 0x30 // 6 degree control register
#define INT1_SOURCE 0x31 // axis threshold interrupt control
#define INT1_THS 0x32 // INT1 threshold
#define INT1_DURATION 0x33 // INT1 duration
#define CLICK_CFG 0x38 // click on axis
#define CLICK_SRC 0x39 // other click
#define CLICK_THS 0x3A // more click
#define TIME_LIMIT 0x3B // click related
#define TIME_LATENCY 0x3C // and so on
#define TIME_WINDOW 0x3D // contined click
#define SCALE_2G 0x00 //(b00000000) // +/- 2G sensitivity
#define SCALE_4G 0x10 //(b00010000) // +/- 4G sensitivity
#define SCALE_8G 0x20 //(b00100000) // +/- 8G sensitivity
#define SCALE_16G 0x30 //(b00110000) // +/- 16G sensitivity
#define RATE_1HZ 0x10 //(b00010000) // 1Hz sample rate in normal or low-power mode
#define RATE_10HZ 0x20 //(b00100000) // 10Hz sample rate in normal or low-power mode
#define RATE_25HZ 0x30 //(b00110000) // 25Hz sample rate in normal or low-power mode
#define RATE_50HZ 0x40 //(b01000000) // 50Hz sample rate in normal or low-power mode
#define RATE_100HZ 0x50 //(b01010000) // 100Hz sample rate in normal or low-power mode
#define RATE_200HZ 0x60 //(b01100000) // 200Hz sample rate in normal or low-power mode
#define RATE_400HZ 0x70 //(b01110000) // 400Hz sample rate in normal or low-power mode
#define RATE_1600HZ_LP 0x80 //(b10000000) // 1600Hz sample rate in low-power mode
#define RATE_1250HZ_N 0x90 //(b10010000) // 1250Hz sample rate in normal mode
#define RATE_5000HZ_LP 0x90 //(b10010000) // 5000Hz sample rate in low-power mode
#endif
+898
Ver Arquivo
@@ -0,0 +1,898 @@
/*
BUILDING OUT THE LIBRARY-TO-RULE-THEM-ALL HERE
*/
#include "OpenBCI_32.h"
// <<<<<<<<<<<<<<<<<<<<<<<<< BOARD WIDE FUNCTIONS >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
void OpenBCI_32::initialize(){
pinMode(SD_SS,OUTPUT); digitalWrite(SD_SS,HIGH); // de-select SDcard if present
spi.begin();
spi.setSpeed(4000000); // try also 8MHz, 10MHz is max for LIS3DH
spi.setMode(DSPI_MODE0); // default to SD card mode!
initialize_ads(); // hard reset ADS, set pin directions
initialize_accel(SCALE_4G); // set pin directions, G scale, DRDY interrupt, power down
}
void OpenBCI_32::printAllRegisters(){
printADSregisters();
LIS3DH_readAllRegs();
}
void OpenBCI_32::startStreaming(){
if(useAccel){enable_accel(RATE_50HZ);}
startADS();
}
void OpenBCI_32::sendChannelData(byte sampleNumber){
Serial0.write(sampleNumber); // 1 byte
ADS_writeChannelData(); // 24 bytes
LIS3DH_writeAxisData(); // 6 bytes
}
void OpenBCI_32::stopStreaming(){
stopADS();
if(useAccel){disable_accel();}
}
//SPI communication method
byte OpenBCI_32::xfer(byte _data)
{
byte inByte;
inByte = spi.transfer(_data);
return inByte;
}
//SPI slave select method
void OpenBCI_32::csLow(int SS)
{
switch(SS){
case ADS_SS:
spi.setMode(DSPI_MODE1); spi.setSpeed(4000000); digitalWrite(ADS_SS, LOW); break;
case LIS3DH_SS:
spi.setMode(DSPI_MODE3); spi.setSpeed(4000000); digitalWrite(LIS3DH_SS, LOW); break;
case SD_SS:
spi.setMode(DSPI_MODE0); spi.setSpeed(20000000); digitalWrite(SD_SS, LOW); break;
case DAISY_SS:
spi.setMode(DSPI_MODE1); spi.setSpeed(4000000); digitalWrite(DAISY_SS, LOW); break;
default: break;
}
}
void OpenBCI_32::csHigh(int SS)
{
switch(SS){
case ADS_SS:
digitalWrite(ADS_SS, HIGH); spi.setSpeed(20000000); break;
case LIS3DH_SS:
digitalWrite(LIS3DH_SS, HIGH); spi.setSpeed(20000000); break;
case SD_SS:
digitalWrite(SD_SS, HIGH); spi.setSpeed(20000000); break;
case DAISY_SS:
digitalWrite(DAISY_SS, HIGH); spi.setSpeed(20000000); break;
default:
break;
}
spi.setMode(DSPI_MODE0); // DEFAULT TO SD MODE!
}
// <<<<<<<<<<<<<<<<<<<<<<<<< END OF BOARD WIDE FUNCTIONS >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
// *************************************************************************************
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ADS1299 FUNCTIONS >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
// void OpenBCI_32::initialize(int _DRDY, int _RST, int _CS){
void OpenBCI_32::initialize_ads(){
pinMode(ADS_SS, OUTPUT); digitalWrite(ADS_SS,HIGH);
pinMode(DAISY_SS, OUTPUT); digitalWrite(DAISY_SS,HIGH);
// recommended power up sequence requiers Tpor (~32mS)
delay(50);
pinMode(ADS_RST,OUTPUT);
digitalWrite(ADS_RST,LOW);
delayMicroseconds(4); // toggle reset pin
digitalWrite(ADS_RST,HIGH);
delayMicroseconds(20); // recommended to wait 18 Tclk before using device (~8uS);
// initalize the data ready chip select and reset pins:
pinMode(ADS_DRDY, INPUT);
delay(100);
// Serial0.println("got to first spi xfer");
resetADS(); // software reset on-board and on-daisy if present
// DEFAULT CHANNEL SETTINGS FOR ADS
defaultChannelSettings[POWER_DOWN] = NO; // on = NO, off = YES
defaultChannelSettings[GAIN_SET] = ADS_GAIN24; // Gain setting
defaultChannelSettings[INPUT_TYPE_SET] = ADSINPUT_NORMAL;// input muxer setting
defaultChannelSettings[BIAS_SET] = YES; // add this channel to bias generation
defaultChannelSettings[SRB2_SET] = YES; // connect this P side to SRB2
defaultChannelSettings[SRB1_SET] = NO; // don't use SRB1
for(int i=0; i<8; i++){
for(int j=0; j<6; j++){
channelSettings[i][j] = defaultChannelSettings[j];
}
}
for(int i=0; i<8; i++){
useInBias[i] = true; // keeping track of Bias Generation
useSRB2[i] = true;
useSRB1 = false;
}
writeChannelSettings();
// defaultChannelBitField = RREG(CH1SET);
WREG(CONFIG3,0b11101100); delay(1); // enable internal reference drive and etc.
for(int i=0; i<8; i++){ // turn off the impedance measure signal
leadOffSettings[i][PCHAN] = OFF;
leadOffSettings[i][NCHAN] = OFF;
}
verbosity = false; // when verbosity is true, there will be Serial feedback
};
void OpenBCI_32::updateChannelData(){
byte inByte;
int byteCounter = 0;
csLow(ADS_SS); // open SPI
for(int i=0; i<3; i++){
inByte = xfer(0x00); // read status register (1100 + LOFF_STATP + LOFF_STATN + GPIO[7:4])
stat = (stat << 8) | inByte;
}
for(int i = 0; i<8; i++){
for(int j=0; j<3; j++){ // read 24 bits of channel data in 8 3 byte chunks
inByte = xfer(0x00);
channelDataRaw[byteCounter] = inByte; // raw data gets streamed to the radio
byteCounter++;
channelDataInt[i] = (channelDataInt[i]<<8) | inByte;
}
}
csHigh(ADS_SS); // close SPI
// need to convert 24bit to 32bit if using the filter
for(int i=0; i<8; i++){ // convert 3 byte 2's compliment to 4 byte 2's compliment
if(bitRead(channelDataInt[i],23) == 1){
channelDataInt[i] |= 0xFF000000;
}else{
channelDataInt[i] &= 0x00FFFFFF;
}
}
}
//reset all the ADS1299's settings. Call however you'd like. Stops all data acquisition
void OpenBCI_32::resetADS()
{
RESET(); // send RESET command to default all registers
SDATAC(); // exit Read Data Continuous mode to communicate with ADS
delay(100);
// turn off all channels
for (int chan=1; chan <= 8; chan++) {
deactivateChannel(chan);
}
};
void OpenBCI_32::setChannelsToDefault(void){
for(int i=0; i<8; i++){
for(int j=0; j<6; j++){
channelSettings[i][j] = defaultChannelSettings[j];
}
}
writeChannelSettings();
for(int i=0; i<8; i++){ // turn off the impedance measure signal
leadOffSettings[i][PCHAN] = OFF;
leadOffSettings[i][NCHAN] = OFF;
}
changeChannelLeadOffDetect();
for(int i=0; i<8; i++){
channelSettings[i][SRB1_SET] = NO;
}
WREG(MISC1,0x00);
}
void OpenBCI_32::setChannelsToEMG(void){
channelSettings[0][POWER_DOWN] = NO; // NO = on, YES = off
channelSettings[0][GAIN_SET] = ADS_GAIN12; // Gain setting
channelSettings[0][INPUT_TYPE_SET] = ADSINPUT_NORMAL;// input muxer setting
channelSettings[0][BIAS_SET] = NO; // add this channel to bias generation
channelSettings[0][SRB2_SET] = NO; // connect this P side to SRB2
channelSettings[0][SRB1_SET] = NO; // don't use SRB1
for(int i=1; i<8; i++){
for(int j=0; j<6; j++){
channelSettings[i][j] = channelSettings[0][j];
}
}
writeChannelSettings();
}
void OpenBCI_32::setChannelsToECG(void){
channelSettings[0][POWER_DOWN] = NO; // NO = on, YES = off
channelSettings[0][GAIN_SET] = ADS_GAIN12; // Gain setting
channelSettings[0][INPUT_TYPE_SET] = ADSINPUT_NORMAL;// input muxer setting
channelSettings[0][BIAS_SET] = NO; // add this channel to bias generation
channelSettings[0][SRB2_SET] = NO; // connect this P side to SRB2
channelSettings[0][SRB1_SET] = NO; // don't use SRB1
for(int i=1; i<8; i++){
for(int j=0; j<6; j++){
channelSettings[i][j] = channelSettings[0][j];
}
}
writeChannelSettings();
}
void OpenBCI_32::reportDefaultChannelSettings(void){
Serial0.write(defaultChannelSettings[POWER_DOWN] + '0'); // on = NO, off = YES
Serial0.write((defaultChannelSettings[GAIN_SET] >> 4) + '0'); // Gain setting
Serial0.write(defaultChannelSettings[INPUT_TYPE_SET] +'0');// input muxer setting
Serial0.write(defaultChannelSettings[BIAS_SET] + '0'); // add this channel to bias generation
Serial0.write(defaultChannelSettings[SRB2_SET] + '0'); // connect this P side to SRB2
Serial0.write(defaultChannelSettings[SRB1_SET] + '0'); // don't use SRB1
}
// channel settings: enabled/disabled, gain, input type, SRB2, SRB1
void OpenBCI_32::writeChannelSettings(void){
byte setting;
boolean use_SRB1 = false;
//proceed...first, disable any data collection
SDATAC(); delay(1); // exit Read Data Continuous mode to communicate with ADS
for(byte i=0; i<8; i++){ // write 8 channel settings
setting = 0x00;
if(channelSettings[i][POWER_DOWN] == YES) setting |= 0x80;
setting |= channelSettings[i][GAIN_SET]; // gain
setting |= channelSettings[i][INPUT_TYPE_SET]; // input code
if(channelSettings[i][SRB2_SET] == YES){
setting |= 0x08; // close this SRB2 switch
useSRB2[i] = true;
}else{
useSRB2[i] = false;
}
WREG(CH1SET+i, setting); // write this channel's register settings
// add or remove from inclusion in BIAS generation
setting = RREG(BIAS_SENSP); //get the current P bias settings
if(channelSettings[i][BIAS_SET] == YES){
bitSet(setting,i); //set this channel's bit to add it to the bias generation
useInBias[i] = true;
}else{
bitClear(setting,i); // clear this channel's bit to remove from bias generation
useInBias[i] = false;
}
WREG(BIAS_SENSP,setting); delay(1); //send the modified byte back to the ADS
setting = RREG(BIAS_SENSN); //get the current N bias settings
if(channelSettings[i][BIAS_SET] == YES){
bitSet(setting,i); //set this channel's bit to add it to the bias generation
}else{
bitClear(setting,i); // clear this channel's bit to remove from bias generation
}
WREG(BIAS_SENSN,setting); delay(1); //send the modified byte back to the ADS
if(channelSettings[i][SRB1_SET] == YES){
useSRB1 = true;
}
}
if(useSRB1){
for(int i=0; i<8; i++){
channelSettings[i][SRB1_SET] = YES;
}
WREG(MISC1,0x20); // close all SRB1 swtiches
}else{
for(int i=0; i<8; i++){
channelSettings[i][SRB1_SET] = NO;
}
WREG(MISC1,0x00);
}
}
void OpenBCI_32::writeChannelSettings(char N){
byte setting;
if ((N < 1) || (N > 8)) {Serial0.println("channel number out of range"); return;} // must be a legit channel number
N = constrain(N-1,0,7); //subtracts 1 so that we're counting from 0, not 1
//proceed...first, disable any data collection
SDATAC(); delay(1); // exit Read Data Continuous mode to communicate with ADS
setting = 0x00;
if(channelSettings[N][POWER_DOWN] == YES) setting |= 0x80;
setting |= channelSettings[N][GAIN_SET]; // gain
setting |= channelSettings[N][INPUT_TYPE_SET]; // input code
if(channelSettings[N][SRB2_SET] == YES){
setting |= 0x08; // close this SRB2 switch
useSRB2[N] = true; // keep track of SRB2 usage
}else{
useSRB2[N] = false;
}
WREG(CH1SET+(byte)N, setting); // write this channel's register settings
// add or remove from inclusion in BIAS generation
setting = RREG(BIAS_SENSP); //get the current P bias settings
if(channelSettings[N][BIAS_SET] == YES){
useInBias[N] = true;
bitSet(setting,N); //set this channel's bit to add it to the bias generation
}else{
useInBias[N] = false;
bitClear(setting,N); // clear this channel's bit to remove from bias generation
}
WREG(BIAS_SENSP,setting); delay(1); //send the modified byte back to the ADS
setting = RREG(BIAS_SENSN); //get the current N bias settings
if(channelSettings[N][BIAS_SET] == YES){
bitSet(setting,N); //set this channel's bit to add it to the bias generation
}else{
bitClear(setting,N); // clear this channel's bit to remove from bias generation
}
WREG(BIAS_SENSN,setting); delay(1); //send the modified byte back to the ADS
if(channelSettings[N][SRB1_SET] == YES){
for(int i=0; i<8; i++){
channelSettings[i][SRB1_SET] = YES;
}
useSRB1 = true;
WREG(MISC1,0x20); // close all SRB1 swtiches
}
if((channelSettings[N][SRB1_SET] == NO) && (useSRB1 == true)){
for(int i=0; i<8; i++){
channelSettings[i][SRB1_SET] = NO;
}
useSRB1 = false;
WREG(MISC1,0x00);
}
}
// deactivate the given channel...note: stops data colleciton to issue its commands
// N is the channel number: 1-8
//
void OpenBCI_32::deactivateChannel(int N)
{
byte setting; // ,reg; // trying not to use reg
if ((N < 1) || (N > 8)) return; //check the inputs
//proceed...first, disable any data collection
SDATAC(); delay(1); // exit Read Data Continuous mode to communicate with ADS
//shut down the channel
N = constrain(N-1,0,7); //subtracts 1 so that we're counting from 0, not 1
// reg = CH1SET+(byte)N; // select the current channel
setting = RREG(CH1SET+(byte)N); delay(1); // get the current channel settings
bitSet(setting,7); // set bit7 to shut down channel
// if (channelSettings[N][SRB2_SET] == YES)
bitClear(setting,3); // clear bit3 to disclude from SRB2 if used
WREG(CH1SET+(byte)N,setting); delay(1); // write the new value to disable the channel
//remove the channel from the bias generation...
// reg = BIAS_SENSP; // set up to disconnect the P inputs from Bias generation
setting = RREG(BIAS_SENSP); delay(1); //get the current bias settings
bitClear(setting,N); //clear this channel's bit to remove from bias generation
WREG(BIAS_SENSP,setting); delay(1); //send the modified byte back to the ADS
// reg = BIAS_SENSN; // set up to disconnect the N inputs from Bias generation
setting = RREG(BIAS_SENSN); delay(1); //get the current bias settings
bitClear(setting,N); //clear this channel's bit to remove from bias generation
WREG(BIAS_SENSN,setting); delay(1); //send the modified byte back to the ADS
leadOffSettings[N][0] = leadOffSettings[N][1] = NO;
changeChannelLeadOffDetect();
};
void OpenBCI_32::activateChannel(int N)
{
byte setting; // ,reg // trying not to use reg
//check the inputs
if ((N < 1) || (N > 8)) return;
N = constrain(N-1,0,7); //shift down by one
//proceed...first, disable any data collection
SDATAC(); delay(1); // exit Read Data Continuous mode to communicate with ADS
setting = 0x00;
setting |= channelSettings[N][GAIN_SET]; // gain
setting |= channelSettings[N][INPUT_TYPE_SET]; // input code
if(useSRB2[N] == true){channelSettings[N][SRB2_SET] = YES;}else{channelSettings[N][SRB2_SET] = NO;}
if(channelSettings[N][SRB2_SET] == YES) bitSet(setting,3); // close this SRB2 switch
WREG(CH1SET+N, setting);
// add or remove from inclusion in BIAS generation
if(useInBias[N]){channelSettings[N][BIAS_SET] = YES;}else{channelSettings[N][BIAS_SET] = NO;}
setting = RREG(BIAS_SENSP); //get the current P bias settings
if(channelSettings[N][BIAS_SET] == YES){
bitSet(setting,N); //set this channel's bit to add it to the bias generation
}else{
bitClear(setting,N); // clear this channel's bit to remove from bias generation
}
WREG(BIAS_SENSP,setting); delay(1); //send the modified byte back to the ADS
setting = RREG(BIAS_SENSN); //get the current N bias settings
if(channelSettings[N][BIAS_SET] == YES){
bitSet(setting,N); //set this channel's bit to add it to the bias generation
}else{
bitClear(setting,N); // clear this channel's bit to remove from bias generation
}
WREG(BIAS_SENSN,setting); delay(1); //send the modified byte back to the ADS
setting = 0x00;
if(useSRB1) setting = 0x20;
WREG(MISC1,setting); // close all SRB1 swtiches
};
void OpenBCI_32::changeChannelLeadOffDetect(void)
{
byte P_setting = RREG(LOFF_SENSP);
byte N_setting = RREG(LOFF_SENSN);
for(int i=0; i<8;i++){
if(leadOffSettings[i][PCHAN] == ON){
bitSet(P_setting,i);
}else{
bitClear(P_setting,i);
}
if(leadOffSettings[i][NCHAN] == ON){
bitSet(N_setting,i);
}else{
bitClear(N_setting,i);
}
}
WREG(LOFF_SENSP,P_setting);
WREG(LOFF_SENSN,N_setting);
};
void OpenBCI_32::configureLeadOffDetection(byte amplitudeCode, byte freqCode)
{
amplitudeCode &= 0b00001100; //only these two bits should be used
freqCode &= 0b00000011; //only these two bits should be used
//get the current configuration of he byte
byte reg, setting;
reg = LOFF;
setting = RREG(reg); //get the current bias settings
//reconfigure the byte to get what we want
setting &= 0b11110000; //clear out the last four bits
setting |= amplitudeCode; //set the amplitude
setting |= freqCode; //set the frequency
//send the config byte back to the hardware
WREG(reg,setting); delay(1); //send the modified byte back to the ADS
}
//Configure the test signals that can be inernally generated by the ADS1299
void OpenBCI_32::configureInternalTestSignal(byte amplitudeCode, byte freqCode)
{
if (amplitudeCode == ADSTESTSIG_NOCHANGE) amplitudeCode = (RREG(CONFIG2) & (0b00000100));
if (freqCode == ADSTESTSIG_NOCHANGE) freqCode = (RREG(CONFIG2) & (0b00000011));
freqCode &= 0b00000011; //only the last two bits are used
amplitudeCode &= 0b00000100; //only this bit is used
byte message = 0b11010000 | freqCode | amplitudeCode; //compose the code
WREG(CONFIG2,message); delay(1);
}
// Start continuous data acquisition
void OpenBCI_32::startADS(void)
{
RDATAC(); // enter Read Data Continuous mode
delay(1);
START(); // start the data acquisition
delay(1);
isRunning = true;
}
// Query to see if data is available from the ADS1299...return TRUE is data is available
boolean OpenBCI_32::isDataAvailable(void)
{
return (!(digitalRead(ADS_DRDY)));
}
// Stop the continuous data acquisition
void OpenBCI_32::stopADS()
{
STOP(); // stop the data acquisition
delay(1);
SDATAC(); // stop Read Data Continuous mode to communicate with ADS
delay(1);
isRunning = false;
}
//write as binary each channel's data
void OpenBCI_32::ADS_writeChannelData(void)
{
//send rawChannelData array to radio module
for (int i = 0; i < 24; i++)
{
Serial0.write(channelDataRaw[i]);
}
}
//print out the state of all the control registers
void OpenBCI_32::printADSregisters(void)
{
boolean wasRunning = false;
boolean prevverbosityState = verbosity;
if (isRunning){ stopADS(); wasRunning = true; }
verbosity = true; // set up for verbosity output
RREGS(0x00,0x0C); // read out the first registers
delay(10); // stall to let all that data get read by the PC
RREGS(0x0D,0x17-0x0D); // read out the rest
verbosity = prevverbosityState;
if (wasRunning){ startADS(); }
}
void OpenBCI_32::ADS_printDeviceID(void)
{
boolean wasRunning;
boolean prevVerbosityState = verbosity;
if (isRunning){ stopADS(); wasRunning = true;}
verbosity = true;
ADS_getDeviceID();
verbosity = prevVerbosityState;
if (wasRunning){ startADS(); }
}
//System Commands
void OpenBCI_32::WAKEUP() {
csLow(ADS_SS);
xfer(_WAKEUP);
csHigh(ADS_SS);
delayMicroseconds(3); //must wait 4 tCLK cycles before sending another command (Datasheet, pg. 35)
}
void OpenBCI_32::STANDBY() { // only allowed to send WAKEUP after sending STANDBY
csLow(ADS_SS);
xfer(_STANDBY);
csHigh(ADS_SS);
}
void OpenBCI_32::RESET() { // reset all the registers to default settings
csLow(ADS_SS);
xfer(_RESET);
delayMicroseconds(12); //must wait 18 tCLK cycles to execute this command (Datasheet, pg. 35)
csHigh(ADS_SS);
}
void OpenBCI_32::START() { //start data conversion
csLow(ADS_SS);
xfer(_START); // KEEP ON-BOARD AND ON-DAISY IN SYNC
csHigh(ADS_SS);
}
void OpenBCI_32::STOP() { //stop data conversion
csLow(ADS_SS);
xfer(_STOP); // KEEP ON-BOARD AND ON-DAISY IN SYNC
csHigh(ADS_SS);
}
void OpenBCI_32::RDATAC() {
csLow(ADS_SS);
xfer(_RDATAC); // read data continuous
csHigh(ADS_SS);
delayMicroseconds(3);
}
void OpenBCI_32::SDATAC() {
csLow(ADS_SS);
xfer(_SDATAC);
csHigh(ADS_SS);
delayMicroseconds(3); //must wait 4 tCLK cycles after executing this command (Datasheet, pg. 37)
}
// Register Read/Write Commands
byte OpenBCI_32::ADS_getDeviceID() { // simple hello world com check
byte data = RREG(0x00);
if(verbosity){ // verbosity otuput
Serial0.print("On Board ADS ID ");
printHex(data); Serial0.println();
}
return data;
}
void OpenBCI_32::RDATA() { // use in Stop Read Continuous mode when DRDY goes low
byte inByte; // to read in one sample of the channels
csLow(ADS_SS); // open SPI
xfer(_RDATA); // send the RDATA command
for(int i=0; i<3; i++){ // read in the status register and new channel data
inByte = xfer(0x00);
stat = (stat<<8) | inByte; // read status register (1100 + LOFF_STATP + LOFF_STATN + GPIO[7:4])
}
for(int i = 0; i<8; i++){
for(int j=0; j<3; j++){ // read in the new channel data
inByte = xfer(0x00);
channelDataInt[i] = (channelDataInt[i]<<8) | inByte;
}
}
csHigh(ADS_SS); // close SPI
for(int i=0; i<8; i++){
if(bitRead(channelDataInt[i],23) == 1){ // convert 3 byte 2's compliment to 4 byte 2's compliment
channelDataInt[i] |= 0xFF000000;
}else{
channelDataInt[i] &= 0x00FFFFFF;
}
}
}
byte OpenBCI_32::RREG(byte _address) { // reads ONE register at _address
byte opcode1 = _address + 0x20; // RREG expects 001rrrrr where rrrrr = _address
csLow(ADS_SS); // open SPI
xfer(opcode1); // opcode1
xfer(0x00); // opcode2
regData[_address] = xfer(0x00);// update mirror location with returned byte
csHigh(ADS_SS); // close SPI
if (verbosity){ // verbosity output
printRegisterName(_address);
printHex(_address);
Serial0.print(", ");
printHex(regData[_address]);
Serial0.print(", ");
for(byte j = 0; j<8; j++){
Serial0.print(bitRead(regData[_address], 7-j));
if(j!=7) Serial0.print(", ");
}
Serial0.println();
}
return regData[_address]; // return requested register value
}
// Read more than one register starting at _address
void OpenBCI_32::RREGS(byte _address, byte _numRegistersMinusOne) {
// for(byte i = 0; i < 0x17; i++){
// regData[i] = 0; // reset the regData array
// }
byte opcode1 = _address + 0x20; // RREG expects 001rrrrr where rrrrr = _address
csLow(ADS_SS); // open SPI
xfer(opcode1); // opcode1
xfer(_numRegistersMinusOne); // opcode2
for(int i = 0; i <= _numRegistersMinusOne; i++){
regData[_address + i] = xfer(0x00); // add register byte to mirror array
}
csHigh(ADS_SS); // close SPI
if(verbosity){ // verbosity output
for(int i = 0; i<= _numRegistersMinusOne; i++){
printRegisterName(_address + i);
printHex(_address + i);
Serial0.print(", ");
printHex(regData[_address + i]);
Serial0.print(", ");
for(int j = 0; j<8; j++){
Serial0.print(bitRead(regData[_address + i], 7-j));
if(j!=7) Serial0.print(", ");
}
Serial0.println();
delay(30);
}
}
}
void OpenBCI_32::WREG(byte _address, byte _value) { // Write ONE register at _address
byte opcode1 = _address + 0x40; // WREG expects 010rrrrr where rrrrr = _address
csLow(ADS_SS); // open SPI
xfer(opcode1); // Send WREG command & address
xfer(0x00); // Send number of registers to read -1
xfer(_value); // Write the value to the register
csHigh(ADS_SS); // close SPI
regData[_address] = _value; // update the mirror array
if(verbosity){ // verbosity output
Serial0.print("Register ");
printHex(_address);
Serial0.println(" modified.");
}
}
void OpenBCI_32::WREGS(byte _address, byte _numRegistersMinusOne) {
byte opcode1 = _address + 0x40; // WREG expects 010rrrrr where rrrrr = _address
csLow(ADS_SS); // open SPI
xfer(opcode1); // Send WREG command & address
xfer(_numRegistersMinusOne); // Send number of registers to read -1
for (int i=_address; i <=(_address + _numRegistersMinusOne); i++){
xfer(regData[i]); // Write to the registers
}
digitalWrite(CS,HIGH); // close SPI
if(verbosity){
Serial0.print("Registers ");
printHex(_address); Serial0.print(" to ");
printHex(_address + _numRegistersMinusOne);
Serial0.println(" modified");
}
}
// <<<<<<<<<<<<<<<<<<<<<<<<< END OF ADS1299 FUNCTIONS >>>>>>>>>>>>>>>>>>>>>>>>>
// ******************************************************************************
// <<<<<<<<<<<<<<<<<<<<<<<<< LIS3DH FUNCTIONS >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
void OpenBCI_32::initialize_accel(byte g){
byte setting = g | 0x08; // mask the g range for REG4
pinMode(LIS3DH_SS,OUTPUT); digitalWrite(LIS3DH_SS,HIGH);
pinMode(LIS3DH_DRDY,INPUT); // setup dataReady interupt from accelerometer
LIS3DH_write(TMP_CFG_REG, 0x00); // DISable ADC inputs, enable temperature sensor
LIS3DH_write(CTRL_REG1, 0x08); // disable accel, low power mode
LIS3DH_write(CTRL_REG2, 0x00); // don't use the high pass filter
LIS3DH_write(CTRL_REG3, 0x00); // no interrupts yet
LIS3DH_write(CTRL_REG4, setting); // set scale to g, high resolution
LIS3DH_write(CTRL_REG5, 0x00); // no boot, no fifo
LIS3DH_write(CTRL_REG6, 0x00);
LIS3DH_write(REFERENCE, 0x00);
DRDYpinValue = lastDRDYpinValue = digitalRead(LIS3DH_DRDY); // take a reading to seed these variables
}
void OpenBCI_32::enable_accel(byte Hz){ // ADD ABILITY TO SET FREQUENCY & RESOLUTION
for(int i=0; i<3; i++){
axisData[i] = 0; // clear the axisData array so we don't get any stale news
}
byte setting = Hz | 0x07; // mask the desired frequency
LIS3DH_write(CTRL_REG1, setting); // set freq and enable all axis in normal mode
LIS3DH_write(CTRL_REG3, 0x10); // enable DRDY1 on INT1 (tied to Arduino pin3, LIS3DH_DRDY)
}
void OpenBCI_32::disable_accel(){
LIS3DH_write(CTRL_REG1, 0x08); // power down, low power mode
LIS3DH_write(CTRL_REG3, 0x00); // disable DRDY1 on INT1
}
byte OpenBCI_32::LIS3DH_getDeviceID(){
return LIS3DH_read(WHO_AM_I);
}
boolean OpenBCI_32::LIS3DH_DataAvailable(){
boolean x = false;
if((LIS3DH_read(STATUS_REG2) & 0x08) > 0) x = true; // read STATUS_REG
return x;
}
boolean OpenBCI_32::LIS3DH_DataReady(){
boolean r = false;
DRDYpinValue = digitalRead(LIS3DH_DRDY); // take a look at LIS3DH_DRDY pin
if(DRDYpinValue != lastDRDYpinValue){ // if the value has changed since last looking
if(DRDYpinValue == HIGH){ // see if this is the rising edge
r = true; // if so, there is fresh data!
}
lastDRDYpinValue = DRDYpinValue; // keep track of the changing pin
}
return r;
}
void OpenBCI_32::LIS3DH_writeAxisData(void){
for(int i=0; i<3; i++){
Serial0.write(highByte(axisData[i])); // write 16 bit axis data MSB first
Serial0.write(lowByte(axisData[i])); // axisData is array of type short (16bit)
}
}
byte OpenBCI_32::LIS3DH_read(byte reg){
reg |= READ_REG; // add the READ_REG bit
csLow(LIS3DH_SS); // take spi
spi.transfer(reg); // send reg to read
byte inByte = spi.transfer(0x00); // retrieve data
csHigh(LIS3DH_SS); // release spi
return inByte;
}
void OpenBCI_32::LIS3DH_write(byte reg, byte value){
csLow(LIS3DH_SS); // take spi
spi.transfer(reg); // send reg to write
spi.transfer(value); // write value
csHigh(LIS3DH_SS); // release spi
}
int OpenBCI_32::LIS3DH_read16(byte reg){ // use for reading axis data.
int inData;
reg |= READ_REG | READ_MULTI; // add the READ_REG and READ_MULTI bits
csLow(LIS3DH_SS); // take spi
spi.transfer(reg); // send reg to start reading from
inData = spi.transfer(0x00) | (spi.transfer(0x00) << 8); // get the data and arrange it
csHigh(LIS3DH_SS); // release spi
return inData;
}
int OpenBCI_32::getX(){
return LIS3DH_read16(OUT_X_L);
}
int OpenBCI_32::getY(){
return LIS3DH_read16(OUT_Y_L);
}
int OpenBCI_32::getZ(){
return LIS3DH_read16(OUT_Z_L);
}
void OpenBCI_32::LIS3DH_updateAxisData(){
axisData[0] = getX();
axisData[1] = getY();
axisData[2] = getZ();
}
void OpenBCI_32::LIS3DH_readAllRegs(){
byte inByte;
for (int i = STATUS_REG_AUX; i <= WHO_AM_I; i++){
inByte = LIS3DH_read(i);
Serial0.print("0x0");Serial0.print(i,HEX);
Serial0.print("\t");Serial0.println(inByte,HEX);
delay(20);
}
Serial0.println();
for (int i = TMP_CFG_REG; i <= INT1_DURATION; i++){
inByte = LIS3DH_read(i);
// printRegisterName(i);
Serial0.print("0x");Serial0.print(i,HEX);
Serial0.print("\t");Serial0.println(inByte,HEX);
delay(20);
}
Serial0.println();
for (int i = CLICK_CFG; i <= TIME_WINDOW; i++){
inByte = LIS3DH_read(i);
Serial0.print("0x");Serial0.print(i,HEX);
Serial0.print("\t");Serial0.println(inByte,HEX);
delay(20);
}
}
// <<<<<<<<<<<<<<<<<<<<<<<<< END OF LIS3DH FUNCTIONS >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
// String-Byte converters for ADS
void OpenBCI_32::printRegisterName(byte _address) {
switch(_address){
case ID:
Serial0.print("ID, "); break;
case CONFIG1:
Serial0.print("CONFIG1, "); break;
case CONFIG2:
Serial0.print("CONFIG2, "); break;
case CONFIG3:
Serial0.print("CONFIG3, "); break;
case LOFF:
Serial0.print("LOFF, "); break;
case CH1SET:
Serial0.print("CH1SET, "); break;
case CH2SET:
Serial0.print("CH2SET, "); break;
case CH3SET:
Serial0.print("CH3SET, "); break;
case CH4SET:
Serial0.print("CH4SET, "); break;
case CH5SET:
Serial0.print("CH5SET, "); break;
case CH6SET:
Serial0.print("CH6SET, "); break;
case CH7SET:
Serial0.print("CH7SET, "); break;
case CH8SET:
Serial0.print("CH8SET, "); break;
case BIAS_SENSP:
Serial0.print("BIAS_SENSP, "); break;
case BIAS_SENSN:
Serial0.print("BIAS_SENSN, "); break;
case LOFF_SENSP:
Serial0.print("LOFF_SENSP, "); break;
case LOFF_SENSN:
Serial0.print("LOFF_SENSN, "); break;
case LOFF_FLIP:
Serial0.print("LOFF_FLIP, "); break;
case LOFF_STATP:
Serial0.print("LOFF_STATP, "); break;
case LOFF_STATN:
Serial0.print("LOFF_STATN, "); break;
case GPIO:
Serial0.print("GPIO, "); break;
case MISC1:
Serial0.print("MISC1, "); break;
case MISC2:
Serial0.print("MISC2, "); break;
case CONFIG4:
Serial0.print("CONFIG4, "); break;
default:
break;
}
}
// Used for printing HEX in verbosity feedback mode
void OpenBCI_32::printHex(byte _data){
Serial0.print("0x");
if(_data < 0x10) Serial0.print("0");
Serial0.print(_data, HEX);
}
+109
Ver Arquivo
@@ -0,0 +1,109 @@
/*
insert header here
*/
#ifndef _____OpenBCI_32__
#define _____OpenBCI_32__
#include <DSPI.h>
#include <WProgram.h>
#include "Definitions_32.h"
class OpenBCI_32 {
public:
DSPI0 spi; // use DSPI library
// BOARD
boolean useAccel;
boolean useAux;
void initialize(void); // ADD DAISY USE outputType
void printAllRegisters(void); // ADD DAISY USE outputType
void sendChannelData(byte); // send the current data with sample number
void startStreaming(void); // ADD DAISY USE outputType
void stopStreaming(void); // ADD DAISY USE outputType
// ADS1299
void initialize_ads(void);
void updateChannelSettings(void);
void writeChannelSettings(void);
void writeChannelSettings(char);
void setChannelsToDefault(void);
void setChannelsToEMG(void);
void setChannelsToECG(void);
void reportDefaultChannelSettings(void);
void printADSregisters(void);
void WAKEUP(void); // get out of low power mode
void STANDBY(void); // go into low power mode
void RESET(void); // set all register values to default
void START(void); // start data acquisition
void STOP(void); // stop data acquisition
void RDATAC(void); // go into read data continuous mode
void SDATAC(void); // get out of read data continuous mode
void RDATA(void); // read data one-shot
byte RREG(byte); // read one register
void RREGS(byte, byte); // read multiple registers
void WREG(byte, byte); // write one register
void WREGS(byte, byte); // write multiple registers
byte ADS_getDeviceID();
void printRegisterName(byte); // used for verbosity
void printHex(byte); // used for verbosity
void updateChannelData(void); // retrieve data from ADS
byte xfer(byte); // SPI Transfer function
int DRDY, CS, RST; // pin numbers for DataRead ChipSelect Reset pins
void resetADS(void); // reset all the ADS1299's settings. Call however you'd like
void startADS(void);
void stopADS(void);
void activateChannel(int); // enable the selected channel
void deactivateChannel(int); // disable given channel 1-8(16)
void configureLeadOffDetection(byte, byte);
void changeChannelLeadOffDetect(void);
void configureInternalTestSignal(byte, byte);
boolean isDataAvailable(void);
void ADS_writeChannelData(void);
void setSRB1(boolean desired_state);
void ADS_printDeviceID(void); // add to read LIS3DH dev ID
int stat; // used to hold the status register
byte regData[24]; // array is used to mirror register data
int channelDataInt[9]; // array used when reading channel data as ints
byte channelDataRaw[24]; // array to hold raw channel data
boolean verbosity; // turn on/off Serial verbosity
char channelSettings[8][6]; // array to hold current channel settings
char defaultChannelSettings[6]; // default channel settings
boolean useInBias[8]; // used to remember if we were included in Bias before channel power down
boolean useSRB1; // used to keep track of if we are using SRB1
boolean useSRB2[8]; // used to remember if we were included in SRB2 before channel power down
char leadOffSettings[8][2]; // used to control on/off of impedance measure for P and N side of each channel
// LIS3DH
short axisData[3];
void initialize_accel(byte); // initialize
void enable_accel(byte); // start acceleromoeter with default settings
void disable_accel(void); // stop data acquisition and go into low power mode
byte LIS3DH_getDeviceID(void);
byte LIS3DH_read(byte); // read a register on LIS3DH
void LIS3DH_write(byte,byte); // write a register on LIS3DH
int LIS3DH_read16(byte); // read two bytes, used to get axis data
int getX(void);
int getY(void);
int getZ(void);
boolean LIS3DH_DataReady(void); // check LIS3DH_DRDY pin
boolean LIS3DH_DataAvailable(void); // check LIS3DH STATUS_REG2
void LIS3DH_readAllRegs(void);
void LIS3DH_writeAxisData(void);
void LIS3DH_updateAxisData(void);
void csLow(int);
void csHigh(int);
private:
// ADS1299
boolean isRunning;
// LIS3DH
int DRDYpinValue;
int lastDRDYpinValue;
};
#endif
+157
Ver Arquivo
@@ -0,0 +1,157 @@
/*
SD - a slightly more friendly wrapper for sdfatlib
This library aims to expose a subset of SD card functionality
in the form of a higher level "wrapper" object.
License: GNU General Public License V3
(Because sdfatlib is licensed with this.)
(C) Copyright 2010 SparkFun Electronics
*/
#include <SD.h>
/* for debugging file open/close leaks
uint8_t nfilecount=0;
*/
File::File(SdFile f, const char *n) {
// oh man you are kidding me, new() doesnt exist? Ok we do it by hand!
_file = (SdFile *)malloc(sizeof(SdFile));
if (_file) {
memcpy(_file, &f, sizeof(SdFile));
strncpy(_name, n, 12);
_name[12] = 0;
/* for debugging file open/close leaks
nfilecount++;
Serial.print("Created \"");
Serial.print(n);
Serial.print("\": ");
Serial.println(nfilecount, DEC);
*/
}
}
File::File(void) {
_file = 0;
_name[0] = 0;
//Serial.print("Created empty file object");
}
File::~File(void) {
// Serial.print("Deleted file object");
}
// returns a pointer to the file name
char *File::name(void) {
return _name;
}
// a directory is a special type of file
boolean File::isDirectory(void) {
return (_file && _file->isDir());
}
void File::write(uint8_t val) {
//return
write(&val, 1);
}
void File::write(const uint8_t *buf, size_t size) {
size_t t;
//if (!_file) {
// setWriteError();
// return 0;
//}
//_file->clearWriteError();
t = _file->write(buf, size);
//if (_file->getWriteError()) {
// setWriteError();
// return 0;
//}
//return t;
}
int File::peek() {
if (! _file)
return 0;
int c = _file->read();
if (c != -1) _file->seekCur(-1);
return c;
}
int File::read() {
if (_file)
return _file->read();
return -1;
}
// buffered read for more efficient, high speed reading
int File::read(void *buf, uint16_t nbyte) {
if (_file)
return _file->read(buf, nbyte);
return 0;
}
int File::available() {
if (! _file) return 0;
uint32_t n = size() - position();
return n > 0X7FFF ? 0X7FFF : n;
}
void File::flush() {
if (_file)
_file->sync();
}
boolean File::seek(uint32_t pos) {
if (! _file) return false;
return _file->seekSet(pos);
}
uint32_t File::position() {
if (! _file) return -1;
return _file->curPosition();
}
uint32_t File::size() {
if (! _file) return 0;
return _file->fileSize();
}
void File::close() {
if (_file) {
_file->close();
free(_file);
_file = 0;
/* for debugging file open/close leaks
nfilecount--;
Serial.print("Deleted ");
Serial.println(nfilecount, DEC);
*/
}
}
File::operator bool() {
if (_file)
return _file->isOpen();
return false;
}
int8_t File::readDir(dir_t* dir) {
if (_file)
return _file->readDir(dir);
return false;
}
+13
Ver Arquivo
@@ -0,0 +1,13 @@
** SD - a slightly more friendly wrapper for sdfatlib **
This library aims to expose a subset of SD card functionality in the
form of a higher level "wrapper" object.
License: GNU General Public License V3
(Because sdfatlib is licensed with this.)
(C) Copyright 2010 SparkFun Electronics
Now better than ever with optimization, multiple file support, directory handling, etc - ladyada!
Arquivo executável
+620
Ver Arquivo
@@ -0,0 +1,620 @@
/*
SD - a slightly more friendly wrapper for sdfatlib
This library aims to expose a subset of SD card functionality
in the form of a higher level "wrapper" object.
License: GNU General Public License V3
(Because sdfatlib is licensed with this.)
(C) Copyright 2010 SparkFun Electronics
This library provides four key benefits:
* Including `SD.h` automatically creates a global
`SD` object which can be interacted with in a similar
manner to other standard global objects like `Serial` and `Ethernet`.
* Boilerplate initialisation code is contained in one method named
`begin` and no further objects need to be created in order to access
the SD card.
* Calls to `open` can supply a full path name including parent
directories which simplifies interacting with files in subdirectories.
* Utility methods are provided to determine whether a file exists
and to create a directory heirarchy.
Note however that not all functionality provided by the underlying
sdfatlib library is exposed.
*/
/*
Implementation Notes
In order to handle multi-directory path traversal, functionality that
requires this ability is implemented as callback functions.
Individual methods call the `walkPath` function which performs the actual
directory traversal (swapping between two different directory/file handles
along the way) and at each level calls the supplied callback function.
Some types of functionality will take an action at each level (e.g. exists
or make directory) which others will only take an action at the bottom
level (e.g. open).
*/
#include "SD.h"
// Used by `getNextPathComponent`
#define MAX_COMPONENT_LEN 12 // What is max length?
#define PATH_COMPONENT_BUFFER_LEN MAX_COMPONENT_LEN+1
bool getNextPathComponent(char *path, unsigned int *p_offset,
char *buffer) {
/*
Parse individual path components from a path.
e.g. after repeated calls '/foo/bar/baz' will be split
into 'foo', 'bar', 'baz'.
This is similar to `strtok()` but copies the component into the
supplied buffer rather than modifying the original string.
`buffer` needs to be PATH_COMPONENT_BUFFER_LEN in size.
`p_offset` needs to point to an integer of the offset at
which the previous path component finished.
Returns `true` if more components remain.
Returns `false` if this is the last component.
(This means path ended with 'foo' or 'foo/'.)
*/
// TODO: Have buffer local to this function, so we know it's the
// correct length?
int bufferOffset = 0;
int offset = *p_offset;
// Skip root or other separator
if (path[offset] == '/') {
offset++;
}
// Copy the next next path segment
while (bufferOffset < MAX_COMPONENT_LEN
&& (path[offset] != '/')
&& (path[offset] != '\0')) {
buffer[bufferOffset++] = path[offset++];
}
buffer[bufferOffset] = '\0';
// Skip trailing separator so we can determine if this
// is the last component in the path or not.
if (path[offset] == '/') {
offset++;
}
*p_offset = offset;
return (path[offset] != '\0');
}
boolean walkPath(char *filepath, SdFile& parentDir,
boolean (*callback)(SdFile& parentDir,
char *filePathComponent,
boolean isLastComponent,
void *object),
void *object = NULL) {
/*
When given a file path (and parent directory--normally root),
this function traverses the directories in the path and at each
level calls the supplied callback function while also providing
the supplied object for context if required.
e.g. given the path '/foo/bar/baz'
the callback would be called at the equivalent of
'/foo', '/foo/bar' and '/foo/bar/baz'.
The implementation swaps between two different directory/file
handles as it traverses the directories and does not use recursion
in an attempt to use memory efficiently.
If a callback wishes to stop the directory traversal it should
return false--in this case the function will stop the traversal,
tidy up and return false.
If a directory path doesn't exist at some point this function will
also return false and not subsequently call the callback.
If a directory path specified is complete, valid and the callback
did not indicate the traversal should be interrupted then this
function will return true.
*/
SdFile subfile1;
SdFile subfile2;
char buffer[PATH_COMPONENT_BUFFER_LEN];
unsigned int offset = 0;
SdFile *p_parent;
SdFile *p_child;
SdFile *p_tmp_sdfile;
p_child = &subfile1;
p_parent = &parentDir;
while (true) {
boolean moreComponents = getNextPathComponent(filepath, &offset, buffer);
boolean shouldContinue = callback((*p_parent), buffer, !moreComponents, object);
if (!shouldContinue) {
// TODO: Don't repeat this code?
// If it's one we've created then we
// don't need the parent handle anymore.
if (p_parent != &parentDir) {
(*p_parent).close();
}
return false;
}
if (!moreComponents) {
break;
}
boolean exists = (*p_child).open(*p_parent, buffer, O_RDONLY);
// If it's one we've created then we
// don't need the parent handle anymore.
if (p_parent != &parentDir) {
(*p_parent).close();
}
// Handle case when it doesn't exist and we can't continue...
if (exists) {
// We alternate between two file handles as we go down
// the path.
if (p_parent == &parentDir) {
p_parent = &subfile2;
}
p_tmp_sdfile = p_parent;
p_parent = p_child;
p_child = p_tmp_sdfile;
} else {
return false;
}
}
if (p_parent != &parentDir) {
(*p_parent).close(); // TODO: Return/ handle different?
}
return true;
}
/*
The callbacks used to implement various functionality follow.
Each callback is supplied with a parent directory handle,
character string with the name of the current file path component,
a flag indicating if this component is the last in the path and
a pointer to an arbitrary object used for context.
*/
boolean callback_pathExists(SdFile& parentDir, char *filePathComponent,
boolean isLastComponent, void *object) {
/*
Callback used to determine if a file/directory exists in parent
directory.
Returns true if file path exists.
*/
SdFile child;
boolean exists = child.open(parentDir, filePathComponent, O_RDONLY);
if (exists) {
child.close();
}
return exists;
}
boolean callback_makeDirPath(SdFile& parentDir, char *filePathComponent,
boolean isLastComponent, void *object) {
/*
Callback used to create a directory in the parent directory if
it does not already exist.
Returns true if a directory was created or it already existed.
*/
boolean result = false;
SdFile child;
result = callback_pathExists(parentDir, filePathComponent, isLastComponent, object);
if (!result) {
result = child.makeDir(parentDir, filePathComponent);
}
return result;
}
/*
boolean callback_openPath(SdFile& parentDir, char *filePathComponent,
boolean isLastComponent, void *object) {
Callback used to open a file specified by a filepath that may
specify one or more directories above it.
Expects the context object to be an instance of `SDClass` and
will use the `file` property of the instance to open the requested
file/directory with the associated file open mode property.
Always returns true if the directory traversal hasn't reached the
bottom of the directory heirarchy.
Returns false once the file has been opened--to prevent the traversal
from descending further. (This may be unnecessary.)
if (isLastComponent) {
SDClass *p_SD = static_cast<SDClass*>(object);
p_SD->file.open(parentDir, filePathComponent, p_SD->fileOpenMode);
if (p_SD->fileOpenMode == FILE_WRITE) {
p_SD->file.seekSet(p_SD->file.fileSize());
}
// TODO: Return file open result?
return false;
}
return true;
}
*/
boolean callback_remove(SdFile& parentDir, char *filePathComponent,
boolean isLastComponent, void *object) {
if (isLastComponent) {
return SdFile::remove(parentDir, filePathComponent);
}
return true;
}
boolean callback_rmdir(SdFile& parentDir, char *filePathComponent,
boolean isLastComponent, void *object) {
if (isLastComponent) {
SdFile f;
if (!f.open(parentDir, filePathComponent, O_READ)) return false;
return f.rmDir();
}
return true;
}
/* Implementation of class used to create `SDCard` object. */
boolean SDClass::begin(uint8_t csPin) {
/*
Performs the initialisation required by the sdfatlib library.
Return true if initialization succeeds, false otherwise.
*/
return card->init(SPI_HALF_SPEED, csPin) &&
volume.init(*card) &&
root.openRoot(volume);
}
// this little helper is used to traverse paths
SdFile SDClass::getParentDir(const char *filepath, int *index) {
// get parent directory
SdFile d1 = root; // start with the mostparent, root!
SdFile d2;
// we'll use the pointers to swap between the two objects
SdFile *parent = &d1;
SdFile *subdir = &d2;
const char *origpath = filepath;
while (strchr(filepath, '/')) {
// get rid of leading /'s
if (filepath[0] == '/') {
filepath++;
continue;
}
if (! strchr(filepath, '/')) {
// it was in the root directory, so leave now
break;
}
// extract just the name of the next subdirectory
uint8_t idx = strchr(filepath, '/') - filepath;
if (idx > 12)
idx = 12; // dont let them specify long names
char subdirname[13];
strncpy(subdirname, filepath, idx);
subdirname[idx] = 0;
// close the subdir (we reuse them) if open
subdir->close();
if (! subdir->open(parent, subdirname, O_READ)) {
// failed to open one of the subdirectories
return SdFile();
}
// move forward to the next subdirectory
filepath += idx;
// we reuse the objects, close it.
parent->close();
// swap the pointers
SdFile *t = parent;
parent = subdir;
subdir = t;
}
*index = (int)(filepath - origpath);
// parent is now the parent diretory of the file!
return *parent;
}
File SDClass::open(const char *filepath, uint8_t mode) {
/*
Open the supplied file path for reading or writing.
The file content can be accessed via the `file` property of
the `SDClass` object--this property is currently
a standard `SdFile` object from `sdfatlib`.
Defaults to read only.
If `write` is true, default action (when `append` is true) is to
append data to the end of the file.
If `append` is false then the file will be truncated first.
If the file does not exist and it is opened for writing the file
will be created.
An attempt to open a file for reading that does not exist is an
error.
*/
int pathidx;
// do the interative search
SdFile parentdir = getParentDir(filepath, &pathidx);
// no more subdirs!
filepath += pathidx;
if (! filepath[0]) {
// it was the directory itself!
return File(parentdir, "/");
}
// Open the file itself
SdFile file;
// failed to open a subdir!
if (!parentdir.isOpen()) {
return File();
}
// there is a special case for the Root directory since its a static dir
if (parentdir.isRoot()) {
if ( ! file.open(root, filepath, mode)) {
// failed to open the file :(
return File();
}
// dont close the root!
} else {
if ( ! file.open(parentdir, filepath, mode)) {
return File();
}
// close the parent
parentdir.close();
}
if (mode & (O_APPEND | O_WRITE))
file.seekSet(file.fileSize());
return File(file, filepath);
}
/*
File SDClass::open(char *filepath, uint8_t mode) {
//
Open the supplied file path for reading or writing.
The file content can be accessed via the `file` property of
the `SDClass` object--this property is currently
a standard `SdFile` object from `sdfatlib`.
Defaults to read only.
If `write` is true, default action (when `append` is true) is to
append data to the end of the file.
If `append` is false then the file will be truncated first.
If the file does not exist and it is opened for writing the file
will be created.
An attempt to open a file for reading that does not exist is an
error.
//
// TODO: Allow for read&write? (Possibly not, as it requires seek.)
fileOpenMode = mode;
walkPath(filepath, root, callback_openPath, this);
return File();
}
*/
//boolean SDClass::close() {
// /*
//
// Closes the file opened by the `open` method.
//
// */
// file.close();
//}
boolean SDClass::exists(char *filepath) {
/*
Returns true if the supplied file path exists.
*/
return walkPath(filepath, root, callback_pathExists);
}
//boolean SDClass::exists(char *filepath, SdFile& parentDir) {
// /*
//
// Returns true if the supplied file path rooted at `parentDir`
// exists.
//
// */
// return walkPath(filepath, parentDir, callback_pathExists);
//}
boolean SDClass::mkdir(char *filepath) {
/*
Makes a single directory or a heirarchy of directories.
A rough equivalent to `mkdir -p`.
*/
return walkPath(filepath, root, callback_makeDirPath);
}
boolean SDClass::rmdir(char *filepath) {
/*
Makes a single directory or a heirarchy of directories.
A rough equivalent to `mkdir -p`.
*/
return walkPath(filepath, root, callback_rmdir);
}
boolean SDClass::remove(char *filepath) {
return walkPath(filepath, root, callback_remove);
}
// allows you to recurse into a directory
File File::openNextFile(uint8_t mode) {
dir_t p;
//Serial.print("\t\treading dir...");
while (_file->readDir(&p) > 0) {
// done if past last used entry
if (p.name[0] == DIR_NAME_FREE) {
//Serial.println("end");
return File();
}
// skip deleted entry and entries for . and ..
if (p.name[0] == DIR_NAME_DELETED || p.name[0] == '.') {
//Serial.println("dots");
continue;
}
// only list subdirectories and files
if (!DIR_IS_FILE_OR_SUBDIR(&p)) {
//Serial.println("notafile");
continue;
}
// print file name with possible blank fill
SdFile f;
char name[13];
_file->dirName(p, name);
//Serial.print("try to open file ");
//Serial.println(name);
if (f.open(_file, name, mode)) {
//Serial.println("OK!");
return File(f, name);
} else {
//Serial.println("ugh");
return File();
}
}
//Serial.println("nothing");
return File();
}
void File::rewindDirectory(void) {
if (isDirectory())
_file->rewind();
}
Sd2Card defaultSDCard;
SDClass SD(defaultSDCard);
Arquivo executável
+109
Ver Arquivo
@@ -0,0 +1,109 @@
/*
SD - a slightly more friendly wrapper for sdfatlib
This library aims to expose a subset of SD card functionality
in the form of a higher level "wrapper" object.
License: GNU General Public License V3
(Because sdfatlib is licensed with this.)
(C) Copyright 2010 SparkFun Electronics
*/
#ifndef __SD_H__
#define __SD_H__
#include <WProgram.h>
#include <DSPI.h>
#include <utility/SdFat.h>
#include <utility/SdFatUtil.h>
#include <Stream.h>
#define FILE_READ O_READ
#define FILE_WRITE (O_READ | O_WRITE | O_CREAT)
class File : public Stream {
private:
char _name[13]; // our name
SdFile *_file; // underlying file pointer
public:
File(SdFile f, const char *name); // wraps an underlying SdFile
File(void); // 'empty' constructor
~File(void); // destructor
virtual void write(uint8_t);
virtual void write(const uint8_t *buf, size_t size);
virtual int read();
virtual int peek();
virtual int available();
virtual void flush();
int read(void *buf, uint16_t nbyte);
boolean seek(uint32_t pos);
uint32_t position();
uint32_t size();
void close();
operator bool();
char * name();
boolean isDirectory(void);
File openNextFile(uint8_t mode = O_RDONLY);
void rewindDirectory(void);
int8_t readDir(dir_t* dir);
using Print::write;
};
class SDClass {
// These are required for initialisation and use of sdfatlib
Sd2Card *card;
SdVolume volume;
SdFile root;
private:
// my quick&dirty iterator, should be replaced
SdFile getParentDir(const char *filepath, int *indx);
public:
SDClass(Sd2Card& c) : card(&c) {}
// This needs to be called to set up the connection to the SD card
// before other methods are used.
boolean begin(uint8_t csPin = SD_CHIP_SELECT_PIN);
// Open the specified file/directory with the supplied mode (e.g. read or
// write, etc). Returns a File object for interacting with the file.
// Note that currently only one file can be open at a time.
File open(const char *filename, uint8_t mode = FILE_READ);
// Methods to determine if the requested file path exists.
boolean exists(char *filepath);
// Create the requested directory heirarchy--if intermediate directories
// do not exist they will be created.
boolean mkdir(char *filepath);
// Delete the file.
boolean remove(char *filepath);
boolean rmdir(char *filepath);
private:
// This is used to determine the mode used to open a file
// it's here because it's the easiest place to pass the
// information through the directory walking function. But
// it's probably not the best place for it.
// It shouldn't be set directly--it is set via the parameters to `open`.
int fileOpenMode;
friend class File;
friend boolean callback_openPath(SdFile&, char *, boolean, void *);
};
extern SDClass SD;
extern uint8_t errno;
#endif
+124
Ver Arquivo
@@ -0,0 +1,124 @@
/*
SD card test
This example shows how use the utility libraries on which the'
SD library is based in order to get info about your SD card.
Very useful for testing a card when you're not sure whether its working or not.
The circuit:
* SD card attached to SPI bus as follows:
** MOSI - pin 11 on Arduino Uno/Duemilanove/Diecimila
** MISO - pin 12 on Arduino Uno/Duemilanove/Diecimila
** CLK - pin 13 on Arduino Uno/Duemilanove/Diecimila
** CS - depends on your SD card shield or module
created 28 Mar 2011
by Limor Fried
- Cleaned up all SD examples included with MPIDE for consistency in defining CS pins
revised 24 May 2013 by Jacob Christ
*/
// include the SD library:
#include <SD.h>
#include <DSPI.h>
// set up variables using the SD utility library functions:
Sd2Card card;
SdVolume volume;
SdFile root;
// change this to match your SD shield or module;
// Arduino Ethernet shield: pin 4
// Adafruit SD shields and modules: pin 10
// Sparkfun SD shield: pin 8
// On the Ethernet Shield, CS is pin 4. It's set as an output by default.
// Note that even if it's not used as the CS pin, the hardware SS pin
// (10 on most Arduino boards, 53 on the Mega) must be left as an output
// or the SD library functions will not work.
// Default SD chip select for Uno and Mega type devices
const int chipSelect_SD_default = 10; // Change 10 to 53 for a Mega
// chipSelect_SD can be changed if you do not use default CS pin
const int chipSelect_SD = chipSelect_SD_default;
void setup()
{
Serial.begin(9600);
Serial.print("\nInitializing SD card...");
// Make sure the default chip select pin is set to so that
// shields that have a device that use the default CS pin
// that are connected to the SPI bus do not hold drive bus
pinMode(chipSelect_SD_default, OUTPUT);
digitalWrite(chipSelect_SD_default, HIGH);
pinMode(chipSelect_SD, OUTPUT);
digitalWrite(chipSelect_SD, HIGH);
// we'll use the initialization code from the utility libraries
// since we're just testing if the card is working!
if (!card.init(SPI_HALF_SPEED, chipSelect_SD)) {
Serial.println("initialization failed. Things to check:");
Serial.println("* is a card is inserted?");
Serial.println("* Is your wiring correct?");
Serial.println("* did you change the chipSelect pin to match your shield or module?");
return;
} else {
Serial.println("Wiring is correct and a card is present.");
}
// print the type of card
Serial.print("\nCard type: ");
switch(card.type()) {
case SD_CARD_TYPE_SD1:
Serial.println("SD1");
break;
case SD_CARD_TYPE_SD2:
Serial.println("SD2");
break;
case SD_CARD_TYPE_SDHC:
Serial.println("SDHC");
break;
default:
Serial.println("Unknown");
}
// Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
if (!volume.init(card)) {
Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
return;
}
// print the type and size of the first FAT-type volume
long volumesize;
Serial.print("\nVolume type is FAT");
Serial.println(volume.fatType(), DEC);
Serial.println();
volumesize = volume.blocksPerCluster(); // clusters are collections of blocks
volumesize *= volume.clusterCount(); // we'll have a lot of clusters
volumesize *= 512; // SD card blocks are always 512 bytes
Serial.print("Volume size (bytes): ");
Serial.println(volumesize);
Serial.print("Volume size (Kbytes): ");
volumesize /= 1024;
Serial.println(volumesize);
Serial.print("Volume size (Mbytes): ");
volumesize /= 1024;
Serial.println(volumesize);
Serial.println("\nFiles found on the card (name, date and size in bytes): ");
root.openRoot(volume);
// list all files in the card with date and size
root.ls(LS_R | LS_DATE | LS_SIZE);
}
void loop(void) {
}
+102
Ver Arquivo
@@ -0,0 +1,102 @@
/*
SD card datalogger
This example shows how to log data from three analog sensors
to an SD card using the SD library.
The circuit:
* analog sensors on analog ins 0, 1, and 2
* SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4
created 24 Nov 2010
updated 2 Dec 2010
by Tom Igoe
- Cleaned up all SD examples included with MPIDE for consistency in defining CS pins
revised 24 May 2013 by Jacob Christ
This example code is in the public domain.
*/
#include <SD.h>
// On the Ethernet Shield, CS is pin 4. Note that even if it's not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.
// Default SD chip select for Uno and Mega type devices
const int chipSelect_SD_default = 10; // Change 10 to 53 for a Mega
// chipSelect_SD can be changed if you do not use default CS pin
const int chipSelect_SD = chipSelect_SD_default;
void setup()
{
Serial.begin(9600);
Serial.print("Initializing SD card...");
// Make sure the default chip select pin is set to so that
// shields that have a device that use the default CS pin
// that are connected to the SPI bus do not hold drive bus
pinMode(chipSelect_SD_default, OUTPUT);
digitalWrite(chipSelect_SD_default, HIGH);
pinMode(chipSelect_SD, OUTPUT);
digitalWrite(chipSelect_SD, HIGH);
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect_SD)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
}
void loop()
{
// make a string for assembling the data to log:
String dataString = "";
// read three sensors and append to the string:
for (int analogPin = 0; analogPin < 3; analogPin++) {
int sensor = analogRead(analogPin);
dataString += String(sensor);
if (analogPin < 2) {
dataString += ",";
}
}
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
// print to the serial port too:
Serial.println(dataString);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
}
+78
Ver Arquivo
@@ -0,0 +1,78 @@
/*
SD card file dump
This example shows how to read a file from the SD card using the
SD library and send it over the serial port.
The circuit:
* SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4
created 22 December 2010
- Cleaned up all SD examples included with MPIDE for consistency in defining CS pins
revised 24 May 2013 by Jacob Christ
This example code is in the public domain.
*/
#include <SD.h>
// On the Ethernet Shield, CS is pin 4. Note that even if it's not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.
// Default SD chip select for Uno and Mega type devices
const int chipSelect_SD_default = 10; // Change 10 to 53 for a Mega
// chipSelect_SD can be changed if you do not use default CS pin
const int chipSelect_SD = chipSelect_SD_default;
void setup()
{
Serial.begin(9600);
Serial.print("Initializing SD card...");
// Make sure the default chip select pin is set to so that
// shields that have a device that use the default CS pin
// that are connected to the SPI bus do not hold drive bus
pinMode(chipSelect_SD_default, OUTPUT);
digitalWrite(chipSelect_SD_default, HIGH);
pinMode(chipSelect_SD, OUTPUT);
digitalWrite(chipSelect_SD, HIGH);
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect_SD)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("datalog.txt");
// if the file is available, write to it:
if (dataFile) {
while (dataFile.available()) {
Serial.write(dataFile.read());
}
dataFile.close();
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
}
void loop()
{
}
+96
Ver Arquivo
@@ -0,0 +1,96 @@
/*
SD card basic file example
This example shows how to create and destroy an SD card file
The circuit:
* SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4
created Nov 2010
by David A. Mellis
updated 2 Dec 2010
by Tom Igoe
- Cleaned up all SD examples included with MPIDE for consistency in defining CS pins
revised 24 May 2013 by Jacob Christ
This example code is in the public domain.
*/
#include <SD.h>
File myFile;
// On the Ethernet Shield, CS is pin 4. It's set as an output by default.
// Note that even if it's not used as the CS pin, the hardware SS pin
// (10 on most Arduino boards, 53 on the Mega) must be left as an output
// or the SD library functions will not work.
// Default SD chip select for Uno and Mega type devices
const int chipSelect_SD_default = 10; // Change 10 to 53 for a Mega
// chipSelect_SD can be changed if you do not use default CS pin
const int chipSelect_SD = chipSelect_SD_default;
void setup()
{
Serial.begin(9600);
Serial.print("Initializing SD card...");
// Make sure the default chip select pin is set to so that
// shields that have a device that use the default CS pin
// that are connected to the SPI bus do not hold drive bus
pinMode(chipSelect_SD_default, OUTPUT);
digitalWrite(chipSelect_SD_default, HIGH);
pinMode(chipSelect_SD, OUTPUT);
digitalWrite(chipSelect_SD, HIGH);
if (!SD.begin(chipSelect_SD)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
if (SD.exists("example.txt")) {
Serial.println("example.txt exists.");
}
else {
Serial.println("example.txt doesn't exist.");
}
// open a new file and immediately close it:
Serial.println("Creating example.txt...");
myFile = SD.open("example.txt", FILE_WRITE);
myFile.close();
// Check to see if the file exists:
if (SD.exists("example.txt")) {
Serial.println("example.txt exists.");
}
else {
Serial.println("example.txt doesn't exist.");
}
// delete the file:
Serial.println("Removing example.txt...");
SD.remove("example.txt");
if (SD.exists("example.txt")){
Serial.println("example.txt exists.");
}
else {
Serial.println("example.txt doesn't exist.");
}
}
void loop()
{
// nothing happens after setup finishes.
}
+97
Ver Arquivo
@@ -0,0 +1,97 @@
/*
SD card read/write
This example shows how to read and write data to and from an SD card file
The circuit:
* SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4
created Nov 2010
by David A. Mellis
updated 2 Dec 2010
by Tom Igoe
- Cleaned up all SD examples included with MPIDE for consistency in defining CS pins
revised 24 May 2013 by Jacob Christ
This example code is in the public domain.
*/
#include <SD.h>
File myFile;
// On the Ethernet Shield, CS is pin 4. It's set as an output by default.
// Note that even if it's not used as the CS pin, the hardware SS pin
// (10 on most Arduino boards, 53 on the Mega) must be left as an output
// or the SD library functions will not work.
// Default SD chip select for Uno and Mega type devices
const int chipSelect_SD_default = 10; // Change 10 to 53 for a Mega
// chipSelect_SD can be changed if you do not use default CS pin
const int chipSelect_SD = chipSelect_SD_default;
void setup()
{
Serial.begin(9600);
Serial.print("Initializing SD card...");
// Make sure the default chip select pin is set to so that
// shields that have a device that use the default CS pin
// that are connected to the SPI bus do not hold drive bus
pinMode(chipSelect_SD_default, OUTPUT);
digitalWrite(chipSelect_SD_default, HIGH);
pinMode(chipSelect_SD, OUTPUT);
digitalWrite(chipSelect_SD, HIGH);
if (!SD.begin(chipSelect_SD)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
myFile = SD.open("test.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
Serial.print("Writing to test.txt...");
myFile.println("testing 1, 2, 3.");
// close the file:
myFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
// re-open the file for reading:
myFile = SD.open("test.txt");
if (myFile) {
Serial.println("test.txt:");
// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
void loop()
{
// nothing happens after setup
}
+30
Ver Arquivo
@@ -0,0 +1,30 @@
#######################################
# Syntax Coloring Map SD
#######################################
#######################################
# Datatypes (KEYWORD1)
#######################################
SD KEYWORD1
File KEYWORD1
#######################################
# Methods and Functions (KEYWORD2)
#######################################
begin KEYWORD2
exists KEYWORD2
mkdir KEYWORD2
remove KEYWORD2
rmdir KEYWORD2
open KEYWORD2
close KEYWORD2
seek KEYWORD2
position KEYWORD2
size KEYWORD2
#######################################
# Constants (LITERAL1)
#######################################
FILE_READ LITERAL1
FILE_WRITE LITERAL1
+424
Ver Arquivo
@@ -0,0 +1,424 @@
/* Arduino SdFat Library
* Copyright (C) 2009 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 FatStructs_h
#define FatStructs_h
/**
* \file
* FAT file structures
*/
/*
* mostly from Microsoft document fatgen103.doc
* http://www.microsoft.com/whdc/system/platform/firmware/fatgen.mspx
*/
//------------------------------------------------------------------------------
/** Value for byte 510 of boot block or MBR */
uint8_t const BOOTSIG0 = 0X55;
/** Value for byte 511 of boot block or MBR */
uint8_t const BOOTSIG1 = 0XAA;
//------------------------------------------------------------------------------
/**
* \struct partitionTable
* \brief MBR partition table entry
*
* A partition table entry for a MBR formatted storage device.
* The MBR partition table has four entries.
*/
#pragma pack(push, 1)
struct partitionTable {
/**
* Boot Indicator . Indicates whether the volume is the active
* partition. Legal values include: 0X00. Do not use for booting.
* 0X80 Active partition.
*/
uint8_t boot;
/**
* Head part of Cylinder-head-sector address of the first block in
* the partition. Legal values are 0-255. Only used in old PC BIOS.
*/
uint8_t beginHead;
/**
* Sector part of Cylinder-head-sector address of the first block in
* the partition. Legal values are 1-63. Only used in old PC BIOS.
*/
unsigned beginSector : 6;
/** High bits cylinder for first block in partition. */
unsigned beginCylinderHigh : 2;
/**
* Combine beginCylinderLow with beginCylinderHigh. Legal values
* are 0-1023. Only used in old PC BIOS.
*/
uint8_t beginCylinderLow;
/**
* Partition type. See defines that begin with PART_TYPE_ for
* some Microsoft partition types.
*/
uint8_t type;
/**
* head part of cylinder-head-sector address of the last sector in the
* partition. Legal values are 0-255. Only used in old PC BIOS.
*/
uint8_t endHead;
/**
* Sector part of cylinder-head-sector address of the last sector in
* the partition. Legal values are 1-63. Only used in old PC BIOS.
*/
unsigned endSector : 6;
/** High bits of end cylinder */
unsigned endCylinderHigh : 2;
/**
* Combine endCylinderLow with endCylinderHigh. Legal values
* are 0-1023. Only used in old PC BIOS.
*/
uint8_t endCylinderLow;
/** Logical block address of the first block in the partition. */
uint32_t firstSector;
/** Length of the partition, in blocks. */
uint32_t totalSectors;
};
/** Type name for partitionTable */
typedef struct partitionTable part_t;
//------------------------------------------------------------------------------
/**
* \struct masterBootRecord
*
* \brief Master Boot Record
*
* The first block of a storage device that is formatted with a MBR.
*/
struct masterBootRecord {
/** Code Area for master boot program. */
uint8_t codeArea[440];
/** Optional WindowsNT disk signature. May contain more boot code. */
uint32_t diskSignature;
/** Usually zero but may be more boot code. */
uint16_t usuallyZero;
/** Partition tables. */
part_t part[4];
/** First MBR signature byte. Must be 0X55 */
uint8_t mbrSig0;
/** Second MBR signature byte. Must be 0XAA */
uint8_t mbrSig1;
};
/** Type name for masterBootRecord */
typedef struct masterBootRecord mbr_t;
//------------------------------------------------------------------------------
/**
* \struct biosParmBlock
*
* \brief BIOS parameter block
*
* The BIOS parameter block describes the physical layout of a FAT volume.
*/
struct biosParmBlock {
/**
* Count of bytes per sector. This value may take on only the
* following values: 512, 1024, 2048 or 4096
*/
uint16_t bytesPerSector;
/**
* Number of sectors per allocation unit. This value must be a
* power of 2 that is greater than 0. The legal values are
* 1, 2, 4, 8, 16, 32, 64, and 128.
*/
uint8_t sectorsPerCluster;
/**
* Number of sectors before the first FAT.
* This value must not be zero.
*/
uint16_t reservedSectorCount;
/** The count of FAT data structures on the volume. This field should
* always contain the value 2 for any FAT volume of any type.
*/
uint8_t fatCount;
/**
* For FAT12 and FAT16 volumes, this field contains the count of
* 32-byte directory entries in the root directory. For FAT32 volumes,
* this field must be set to 0. For FAT12 and FAT16 volumes, this
* value should always specify a count that when multiplied by 32
* results in a multiple of bytesPerSector. FAT16 volumes should
* use the value 512.
*/
uint16_t rootDirEntryCount;
/**
* This field is the old 16-bit total count of sectors on the volume.
* This count includes the count of all sectors in all four regions
* of the volume. This field can be 0; if it is 0, then totalSectors32
* must be non-zero. For FAT32 volumes, this field must be 0. For
* FAT12 and FAT16 volumes, this field contains the sector count, and
* totalSectors32 is 0 if the total sector count fits
* (is less than 0x10000).
*/
uint16_t totalSectors16;
/**
* This dates back to the old MS-DOS 1.x media determination and is
* no longer usually used for anything. 0xF8 is the standard value
* for fixed (non-removable) media. For removable media, 0xF0 is
* frequently used. Legal values are 0xF0 or 0xF8-0xFF.
*/
uint8_t mediaType;
/**
* Count of sectors occupied by one FAT on FAT12/FAT16 volumes.
* On FAT32 volumes this field must be 0, and sectorsPerFat32
* contains the FAT size count.
*/
uint16_t sectorsPerFat16;
/** Sectors per track for interrupt 0x13. Not used otherwise. */
uint16_t sectorsPerTrtack;
/** Number of heads for interrupt 0x13. Not used otherwise. */
uint16_t headCount;
/**
* Count of hidden sectors preceding the partition that contains this
* FAT volume. This field is generally only relevant for media
* visible on interrupt 0x13.
*/
uint32_t hidddenSectors;
/**
* This field is the new 32-bit total count of sectors on the volume.
* This count includes the count of all sectors in all four regions
* of the volume. This field can be 0; if it is 0, then
* totalSectors16 must be non-zero.
*/
uint32_t totalSectors32;
/**
* Count of sectors occupied by one FAT on FAT32 volumes.
*/
uint32_t sectorsPerFat32;
/**
* This field is only defined for FAT32 media and does not exist on
* FAT12 and FAT16 media.
* Bits 0-3 -- Zero-based number of active FAT.
* Only valid if mirroring is disabled.
* Bits 4-6 -- Reserved.
* Bit 7 -- 0 means the FAT is mirrored at runtime into all FATs.
* -- 1 means only one FAT is active; it is the one referenced in bits 0-3.
* Bits 8-15 -- Reserved.
*/
uint16_t fat32Flags;
/**
* FAT32 version. High byte is major revision number.
* Low byte is minor revision number. Only 0.0 define.
*/
uint16_t fat32Version;
/**
* Cluster number of the first cluster of the root directory for FAT32.
* This usually 2 but not required to be 2.
*/
uint32_t fat32RootCluster;
/**
* Sector number of FSINFO structure in the reserved area of the
* FAT32 volume. Usually 1.
*/
uint16_t fat32FSInfo;
/**
* If non-zero, indicates the sector number in the reserved area
* of the volume of a copy of the boot record. Usually 6.
* No value other than 6 is recommended.
*/
uint16_t fat32BackBootBlock;
/**
* Reserved for future expansion. Code that formats FAT32 volumes
* should always set all of the bytes of this field to 0.
*/
uint8_t fat32Reserved[12];
};
/** Type name for biosParmBlock */
typedef struct biosParmBlock bpb_t;
//------------------------------------------------------------------------------
/**
* \struct fat32BootSector
*
* \brief Boot sector for a FAT16 or FAT32 volume.
*
*/
struct fat32BootSector {
/** X86 jmp to boot program */
uint8_t jmpToBootCode[3];
/** informational only - don't depend on it */
char oemName[8];
/** BIOS Parameter Block */
bpb_t bpb;
/** for int0x13 use value 0X80 for hard drive */
uint8_t driveNumber;
/** used by Windows NT - should be zero for FAT */
uint8_t reserved1;
/** 0X29 if next three fields are valid */
uint8_t bootSignature;
/** usually generated by combining date and time */
uint32_t volumeSerialNumber;
/** should match volume label in root dir */
char volumeLabel[11];
/** informational only - don't depend on it */
char fileSystemType[8];
/** X86 boot code */
uint8_t bootCode[420];
/** must be 0X55 */
uint8_t bootSectorSig0;
/** must be 0XAA */
uint8_t bootSectorSig1;
};
//------------------------------------------------------------------------------
// End Of Chain values for FAT entries
/** FAT16 end of chain value used by Microsoft. */
uint16_t const FAT16EOC = 0XFFFF;
/** Minimum value for FAT16 EOC. Use to test for EOC. */
uint16_t const FAT16EOC_MIN = 0XFFF8;
/** FAT32 end of chain value used by Microsoft. */
uint32_t const FAT32EOC = 0X0FFFFFFF;
/** Minimum value for FAT32 EOC. Use to test for EOC. */
uint32_t const FAT32EOC_MIN = 0X0FFFFFF8;
/** Mask a for FAT32 entry. Entries are 28 bits. */
uint32_t const FAT32MASK = 0X0FFFFFFF;
/** Type name for fat32BootSector */
typedef struct fat32BootSector fbs_t;
//------------------------------------------------------------------------------
/**
* \struct directoryEntry
* \brief FAT short directory entry
*
* Short means short 8.3 name, not the entry size.
*
* Date Format. A FAT directory entry date stamp is a 16-bit field that is
* basically a date relative to the MS-DOS epoch of 01/01/1980. Here is the
* format (bit 0 is the LSB of the 16-bit word, bit 15 is the MSB of the
* 16-bit word):
*
* Bits 9-15: Count of years from 1980, valid value range 0-127
* inclusive (1980-2107).
*
* Bits 5-8: Month of year, 1 = January, valid value range 1-12 inclusive.
*
* Bits 0-4: Day of month, valid value range 1-31 inclusive.
*
* Time Format. A FAT directory entry time stamp is a 16-bit field that has
* a granularity of 2 seconds. Here is the format (bit 0 is the LSB of the
* 16-bit word, bit 15 is the MSB of the 16-bit word).
*
* Bits 11-15: Hours, valid value range 0-23 inclusive.
*
* Bits 5-10: Minutes, valid value range 0-59 inclusive.
*
* Bits 0-4: 2-second count, valid value range 0-29 inclusive (0 - 58 seconds).
*
* The valid time range is from Midnight 00:00:00 to 23:59:58.
*/
struct directoryEntry {
/**
* Short 8.3 name.
* The first eight bytes contain the file name with blank fill.
* The last three bytes contain the file extension with blank fill.
*/
uint8_t name[11];
/** Entry attributes.
*
* The upper two bits of the attribute byte are reserved and should
* always be set to 0 when a file is created and never modified or
* looked at after that. See defines that begin with DIR_ATT_.
*/
uint8_t attributes;
/**
* Reserved for use by Windows NT. Set value to 0 when a file is
* created and never modify or look at it after that.
*/
uint8_t reservedNT;
/**
* The granularity of the seconds part of creationTime is 2 seconds
* so this field is a count of tenths of a second and its valid
* value range is 0-199 inclusive. (WHG note - seems to be hundredths)
*/
uint8_t creationTimeTenths;
/** Time file was created. */
uint16_t creationTime;
/** Date file was created. */
uint16_t creationDate;
/**
* Last access date. Note that there is no last access time, only
* a date. This is the date of last read or write. In the case of
* a write, this should be set to the same date as lastWriteDate.
*/
uint16_t lastAccessDate;
/**
* High word of this entry's first cluster number (always 0 for a
* FAT12 or FAT16 volume).
*/
uint16_t firstClusterHigh;
/** Time of last write. File creation is considered a write. */
uint16_t lastWriteTime;
/** Date of last write. File creation is considered a write. */
uint16_t lastWriteDate;
/** Low word of this entry's first cluster number. */
uint16_t firstClusterLow;
/** 32-bit unsigned holding this file's size in bytes. */
uint32_t fileSize;
};
#pragma pack(pop)
//------------------------------------------------------------------------------
// Definitions for directory entries
//
/** Type name for directoryEntry */
typedef struct directoryEntry dir_t;
/** escape for name[0] = 0XE5 */
uint8_t const DIR_NAME_0XE5 = 0X05;
/** name[0] value for entry that is free after being "deleted" */
uint8_t const DIR_NAME_DELETED = 0XE5;
/** name[0] value for entry that is free and no allocated entries follow */
uint8_t const DIR_NAME_FREE = 0X00;
/** file is read-only */
uint8_t const DIR_ATT_READ_ONLY = 0X01;
/** File should hidden in directory listings */
uint8_t const DIR_ATT_HIDDEN = 0X02;
/** Entry is for a system file */
uint8_t const DIR_ATT_SYSTEM = 0X04;
/** Directory entry contains the volume label */
uint8_t const DIR_ATT_VOLUME_ID = 0X08;
/** Entry is for a directory */
uint8_t const DIR_ATT_DIRECTORY = 0X10;
/** Old DOS archive bit for backup support */
uint8_t const DIR_ATT_ARCHIVE = 0X20;
/** Test value for long name entry. Test is
(d->attributes & DIR_ATT_LONG_NAME_MASK) == DIR_ATT_LONG_NAME. */
uint8_t const DIR_ATT_LONG_NAME = 0X0F;
/** Test mask for long name entry */
uint8_t const DIR_ATT_LONG_NAME_MASK = 0X3F;
/** defined attribute bits */
uint8_t const DIR_ATT_DEFINED_BITS = 0X3F;
/** Directory entry is part of a long name */
static inline uint8_t DIR_IS_LONG_NAME(const dir_t* dir) {
return (dir->attributes & DIR_ATT_LONG_NAME_MASK) == DIR_ATT_LONG_NAME;
}
/** Mask for file/subdirectory tests */
uint8_t const DIR_ATT_FILE_TYPE_MASK = (DIR_ATT_VOLUME_ID | DIR_ATT_DIRECTORY);
/** Directory entry is for a file */
static inline uint8_t DIR_IS_FILE(const dir_t* dir) {
return (dir->attributes & DIR_ATT_FILE_TYPE_MASK) == 0;
}
/** Directory entry is for a subdirectory */
static inline uint8_t DIR_IS_SUBDIR(const dir_t* dir) {
return (dir->attributes & DIR_ATT_FILE_TYPE_MASK) == DIR_ATT_DIRECTORY;
}
/** Directory entry is for a file or subdirectory */
static inline uint8_t DIR_IS_FILE_OR_SUBDIR(const dir_t* dir) {
return (dir->attributes & DIR_ATT_VOLUME_ID) == 0;
}
#endif // FatStructs_h
+618
Ver Arquivo
@@ -0,0 +1,618 @@
/* Arduino Sd2Card Library
* Copyright (C) 2009 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/>.
*/
#include <p32xxxx.h>
#include <plib.h>
#include <WProgram.h>
#include "Sd2Card.h"
/* SPIxCON
*/
#define bnOn 15
#define bnSmp 9
#define bnCkp 6
#define bnMsten 5
/* SPIxSTAT
*/
#define bnTbe 3
#define bnRbf 0
/* IEC0
*/
#define bnSPI2RXIE 7
#define bnSPI2TXIE 6
uint32_t spi_state;
uint8_t fspi_state_saved = false;
uint32_t interrupt_state = 0;
extern "C" {
extern uint8_t shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder);
extern void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, byte data);
}
/** Soft SPI receive */
uint8_t Sd2Card::spiRec(void) {
uint8_t data = 0;
if (_spi) {
data = _spi->transfer(0xFF);
//Serial0.print("REC - ");
//Serial0.println(data,HEX);
} else {
// output pin high - like sending 0XFF
digitalWrite(_mosi, HIGH);
digitalWrite(_miso, HIGH);
data = shiftIn(_miso, _clk, MSBFIRST);
}
return data;
}
//------------------------------------------------------------------------------
/** Soft SPI send */
void Sd2Card::spiSend(uint8_t data) {
uint8_t rec = 0;
if (_spi) {
rec = _spi->transfer(data);
/*
Serial0.print("SND - ");
Serial0.print(data,HEX);
Serial0.print(" | ");
Serial0.print("REC - ");
Serial0.println(rec,HEX);
*/
} else {
digitalWrite(_miso, HIGH);
shiftOut(_mosi, _clk, MSBFIRST, data);
}
}
//------------------------------------------------------------------------------
// send command and return error code. Return zero for OK
uint8_t Sd2Card::cardCommand(uint8_t cmd, uint32_t arg) {
// end read if in partialBlockRead mode
readEnd();
// select card
chipSelectLow();
// wait up to 300 ms if busy
waitNotBusy(300);
// send command
spiSend(cmd | 0x40);
// send argument
for (int8_t s = 24; s >= 0; s -= 8) spiSend(arg >> s);
// send CRC
uint8_t crc = 0XFF;
if (cmd == CMD0) crc = 0X95; // correct crc for CMD0 with arg 0
if (cmd == CMD8) crc = 0X87; // correct crc for CMD8 with arg 0X1AA
spiSend(crc);
//spiSend(0xFF);
// wait for response
for (uint8_t i = 0; ((status_ = spiRec()) & 0X80) && i != 0XFF; i++);
//for (uint8_t i = 0; (status_ = spiRec()) && (i != 0XFF); i++);
return status_;
}
//------------------------------------------------------------------------------
/**
* Determine the size of an SD flash memory card.
*
* \return The number of 512 byte data blocks in the card
* or zero if an error occurs.
*/
uint32_t Sd2Card::cardSize(void) {
csd_t csd;
if (!readCSD(&csd)) return 0;
if (csd.v1.csd_ver == 0) {
uint8_t read_bl_len = csd.v1.read_bl_len;
uint16_t c_size = (csd.v1.c_size_high << 10)
| (csd.v1.c_size_mid << 2) | csd.v1.c_size_low;
uint8_t c_size_mult = (csd.v1.c_size_mult_high << 1)
| csd.v1.c_size_mult_low;
return (uint32_t)(c_size + 1) << (c_size_mult + read_bl_len - 7);
} else if (csd.v2.csd_ver == 1) {
uint32_t c_size = ((uint32_t)csd.v2.c_size_high << 16)
| (csd.v2.c_size_mid << 8) | csd.v2.c_size_low;
return (c_size + 1) << 10;
} else {
error(SD_CARD_ERROR_BAD_CSD);
return 0;
}
}
//------------------------------------------------------------------------------
void Sd2Card::chipSelectHigh(void) {
digitalWrite(_cs, HIGH);
}
//------------------------------------------------------------------------------
void Sd2Card::chipSelectLow(void) {
digitalWrite(_cs, LOW);
// digitalWrite(chipSelectPin_, LOW);
}
//------------------------------------------------------------------------------
/** Erase a range of blocks.
*
* \param[in] firstBlock The address of the first block in the range.
* \param[in] lastBlock The address of the last block in the range.
*
* \note This function requests the SD card to do a flash erase for a
* range of blocks. The data on the card after an erase operation is
* either 0 or 1, depends on the card vendor. The card must support
* single block erase.
*
* \return The value one, true, is returned for success and
* the value zero, false, is returned for failure.
*/
uint8_t Sd2Card::erase(uint32_t firstBlock, uint32_t lastBlock) {
if (!eraseSingleBlockEnable()) {
error(SD_CARD_ERROR_ERASE_SINGLE_BLOCK);
goto fail;
}
if (type_ != SD_CARD_TYPE_SDHC) {
firstBlock <<= 9;
lastBlock <<= 9;
}
if (cardCommand(CMD32, firstBlock)
|| cardCommand(CMD33, lastBlock)
|| cardCommand(CMD38, 0)) {
error(SD_CARD_ERROR_ERASE);
goto fail;
}
if (!waitNotBusy(SD_ERASE_TIMEOUT)) {
error(SD_CARD_ERROR_ERASE_TIMEOUT);
goto fail;
}
chipSelectHigh();
return true;
fail:
chipSelectHigh();
return false;
}
//------------------------------------------------------------------------------
/** Determine if card supports single block erase.
*
* \return The value one, true, is returned if single block erase is supported.
* The value zero, false, is returned if single block erase is not supported.
*/
uint8_t Sd2Card::eraseSingleBlockEnable(void) {
csd_t csd;
return readCSD(&csd) ? csd.v1.erase_blk_en : 0;
}
//------------------------------------------------------------------------------
/**
* Initialize an SD flash memory card.
*
* \param[in] sckRateID SPI clock rate selector. See setSckRate().
* \param[in] chipSelectPin SD chip select pin number.
*
* \return The value one, true, is returned for success and
* the value zero, false, is returned for failure. The reason for failure
* can be determined by calling errorCode() and errorData().
*/
uint8_t Sd2Card::init(uint8_t sckRateID, uint8_t chipSelectPin) {
_cs = chipSelectPin;
if (_spi) {
// _spi->begin();
_spi->setSpeed(125000UL);
//_spi->setSpeed(1UL);
//Serial0.println("SPEED CHANGED");
//_spi->setSpeed(1000UL);
//_spi->setSpeed(400000UL);
//_spi->setSpeed(1000000UL);
} else {
pinMode(_mosi, OUTPUT);
pinMode(_miso, INPUT);
pinMode(_clk, OUTPUT);
}
pinMode(_cs, OUTPUT);
digitalWrite(_cs, HIGH);
errorCode_ = inBlock_ = partialBlockRead_ = type_ = 0;
// 16-bit init start time allows over a minute
uint16_t t0 = (uint16_t)millis();
uint32_t arg;
chipSelectHigh();
// must supply min of 74 clock cycles with CS high.
for (uint8_t i = 0; i < 30; i++) spiSend(0XFF);
chipSelectLow();
// command to go idle in SPI mode
while ((status_ = cardCommand(CMD0, 0)) != R1_IDLE_STATE) {
goto fail;
if (((uint16_t)millis() - t0) > SD_INIT_TIMEOUT) {
error(SD_CARD_ERROR_CMD0);
goto fail;
}
}
// Serial0.println("IDLE");
// check SD version
if ((cardCommand(CMD8, 0x1AA) & R1_ILLEGAL_COMMAND)) {
type(SD_CARD_TYPE_SD1);
} else {
// only need last byte of r7 response
for (uint8_t i = 0; i < 4; i++) status_ = spiRec();
if (status_ != 0XAA) {
error(SD_CARD_ERROR_CMD8);
goto fail;
}
type(SD_CARD_TYPE_SD2);
}
// 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
if (((uint16_t)millis() - t0) > SD_INIT_TIMEOUT) {
error(SD_CARD_ERROR_ACMD41);
goto fail;
}
}
// if SD2 read OCR register to check for SDHC card
if (type() == SD_CARD_TYPE_SD2) {
if (cardCommand(CMD58, 0)) {
error(SD_CARD_ERROR_CMD58);
goto fail;
}
if ((spiRec() & 0XC0) == 0XC0) type(SD_CARD_TYPE_SDHC);
// discard rest of ocr - contains allowed voltage range
for (uint8_t i = 0; i < 3; i++) spiRec();
}
chipSelectHigh();
if (_spi) {
// _spi->setSpeed(10000000UL);
_spi->setSpeed(20000000UL);
} else {
setSckRate(sckRateID);
}
return true;
fail:
chipSelectHigh();
return false;
}
//------------------------------------------------------------------------------
/**
* Enable or disable partial block reads.
*
* Enabling partial block reads improves performance by allowing a block
* to be read over the SPI bus as several sub-blocks. Errors may occur
* if the time between reads is too long since the SD card may timeout.
* The SPI SS line will be held low until the entire block is read or
* readEnd() is called.
*
* Use this for applications like the Adafruit Wave Shield.
*
* \param[in] value The value TRUE (non-zero) or FALSE (zero).)
*/
void Sd2Card::partialBlockRead(uint8_t value) {
readEnd();
partialBlockRead_ = value;
}
//------------------------------------------------------------------------------
/**
* Read a 512 byte block from an SD card device.
*
* \param[in] block Logical block to be read.
* \param[out] dst Pointer to the location that will receive the data.
* \return The value one, true, is returned for success and
* the value zero, false, is returned for failure.
*/
uint8_t Sd2Card::readBlock(uint32_t block, uint8_t* dst) {
return readData(block, 0, 512, dst);
}
//------------------------------------------------------------------------------
/**
* Read part of a 512 byte block from an SD card.
*
* \param[in] block Logical block to be read.
* \param[in] offset Number of bytes to skip at start of block
* \param[out] dst Pointer to the location that will receive the data.
* \param[in] count Number of bytes to read
* \return The value one, true, is returned for success and
* the value zero, false, is returned for failure.
*/
uint8_t Sd2Card::readData(uint32_t block,
uint16_t offset, uint16_t count, uint8_t* dst) {
if (count == 0) return true;
if ((count + offset) > 512) {
goto fail;
}
if (!inBlock_ || block != block_ || offset < offset_) {
block_ = block;
// use address if not SDHC card
if (type()!= SD_CARD_TYPE_SDHC) block <<= 9;
if (cardCommand(CMD17, block)) {
error(SD_CARD_ERROR_CMD17);
goto fail;
}
if (!waitStartBlock()) {
goto fail;
}
offset_ = 0;
inBlock_ = 1;
}
// skip data before offset
for (;offset_ < offset; offset_++) {
spiRec();
}
// transfer data
if (_spi != NULL) {
_spi->transfer(count, 0xFF, dst);
} else {
for (uint16_t i = 0; i < count; i++) {
dst[i] = spiRec();
}
}
offset_ += count;
if (!partialBlockRead_ || offset_ >= 512) {
// read rest of data, checksum and set chip select high
readEnd();
}
return true;
fail:
chipSelectHigh();
return false;
}
//------------------------------------------------------------------------------
/** Skip remaining data in a block when in partial block read mode. */
void Sd2Card::readEnd(void) {
if (inBlock_) {
// skip data and crc
while (offset_++ < 514) {
spiRec();
}
chipSelectHigh();
inBlock_ = 0;
}
}
//------------------------------------------------------------------------------
/** read CID or CSR register */
uint8_t Sd2Card::readRegister(uint8_t cmd, void* buf) {
uint8_t* dst = reinterpret_cast<uint8_t*>(buf);
if (cardCommand(cmd, 0)) {
error(SD_CARD_ERROR_READ_REG);
goto fail;
}
if (!waitStartBlock()) goto fail;
// transfer data
for (uint16_t i = 0; i < 16; i++) dst[i] = spiRec();
spiRec(); // get first crc byte
spiRec(); // get second crc byte
chipSelectHigh();
return true;
fail:
chipSelectHigh();
return false;
}
//------------------------------------------------------------------------------
/**
* Set the SPI clock rate.
*
* \param[in] sckRateID A value in the range [0, 6].
*
* The SPI clock will be set to F_CPU/pow(2, 1 + sckRateID). The maximum
* SPI rate is F_CPU/2 for \a sckRateID = 0 and the minimum rate is F_CPU/128
* for \a scsRateID = 6.
*
* \return The value one, true, is returned for success and the value zero,
* false, is returned for an invalid value of \a sckRateID.
*/
uint8_t Sd2Card::setSckRate(uint8_t sckRateID) {
//if (sckRateID > 6) {
// error(SD_CARD_ERROR_SCK_RATE);
// return false;
//}
//// see avr processor datasheet for SPI register bit definitions
//if ((sckRateID & 1) || sckRateID == 6) {
// SPSR &= ~(1 << SPI2X);
//} else {
// SPSR |= (1 << SPI2X);
//}
//SPCR &= ~((1 <<SPR1) | (1 << SPR0));
//SPCR |= (sckRateID & 4 ? (1 << SPR1) : 0)
// | (sckRateID & 2 ? (1 << SPR0) : 0);
return true;
}
//------------------------------------------------------------------------------
// wait for card to go not busy
uint8_t Sd2Card::waitNotBusy(uint16_t timeoutMillis) {
uint16_t t0 = millis();
do {
if (spiRec() == 0XFF) return true;
}
while (((uint16_t)millis() - t0) < timeoutMillis);
return false;
}
//------------------------------------------------------------------------------
/** Wait for start block token */
uint8_t Sd2Card::waitStartBlock(void) {
uint16_t t0 = millis();
while ((status_ = spiRec()) == 0XFF) {
if (((uint16_t)millis() - t0) > SD_READ_TIMEOUT) {
error(SD_CARD_ERROR_READ_TIMEOUT);
goto fail;
}
}
if (status_ != DATA_START_BLOCK) {
error(SD_CARD_ERROR_READ);
goto fail;
}
return true;
fail:
chipSelectHigh();
return false;
}
//------------------------------------------------------------------------------
/**
* Writes a 512 byte block to an SD card.
*
* \param[in] blockNumber Logical block to be written.
* \param[in] src Pointer to the location of the data to be written.
* \return The value one, true, is returned for success and
* the value zero, false, is returned for failure.
*/
uint8_t Sd2Card::writeBlock(uint32_t blockNumber, const uint8_t* src) {
#if SD_PROTECT_BLOCK_ZERO
// don't allow write to first block
if (blockNumber == 0) {
error(SD_CARD_ERROR_WRITE_BLOCK_ZERO);
goto fail;
}
#endif // SD_PROTECT_BLOCK_ZERO
// use address if not SDHC card
if (type() != SD_CARD_TYPE_SDHC) blockNumber <<= 9;
if (cardCommand(CMD24, blockNumber)) {
error(SD_CARD_ERROR_CMD24);
goto fail;
}
if (!writeData(DATA_START_BLOCK, src)) goto fail;
// wait for flash programming to complete
if (!waitNotBusy(SD_WRITE_TIMEOUT)) {
error(SD_CARD_ERROR_WRITE_TIMEOUT);
goto fail;
}
// response is r2 so get and check two bytes for nonzero
if (cardCommand(CMD13, 0) || spiRec()) {
error(SD_CARD_ERROR_WRITE_PROGRAMMING);
goto fail;
}
chipSelectHigh();
return true;
fail:
chipSelectHigh();
return false;
}
//------------------------------------------------------------------------------
/** Write one data block in a multiple block write sequence */
uint8_t Sd2Card::writeData(const uint8_t* src) {
// wait for previous write to finish
if (!waitNotBusy(SD_WRITE_TIMEOUT)) {
error(SD_CARD_ERROR_WRITE_MULTIPLE);
chipSelectHigh();
return false;
}
return writeData(WRITE_MULTIPLE_TOKEN, src);
}
//------------------------------------------------------------------------------
// send one block of data for write block or write multiple blocks
uint8_t Sd2Card::writeData(uint8_t token, const uint8_t* src) {
spiSend(token);
if (_spi != NULL) {
_spi->transfer(512, (uint8_t *)src);
} else {
for (uint16_t i = 0; i < 512; i++) {
spiSend(src[i]);
}
}
spiSend(0xff); // dummy crc
spiSend(0xff); // dummy crc
status_ = spiRec();
if ((status_ & DATA_RES_MASK) != DATA_RES_ACCEPTED) {
error(SD_CARD_ERROR_WRITE);
chipSelectHigh();
return false;
}
return true;
}
//------------------------------------------------------------------------------
/** Start a write multiple blocks sequence.
*
* \param[in] blockNumber Address of first block in sequence.
* \param[in] eraseCount The number of blocks to be pre-erased.
*
* \note This function is used with writeData() and writeStop()
* for optimized multiple block writes.
*
* \return The value one, true, is returned for success and
* the value zero, false, is returned for failure.
*/
uint8_t Sd2Card::writeStart(uint32_t blockNumber, uint32_t eraseCount) {
#if SD_PROTECT_BLOCK_ZERO
// don't allow write to first block
if (blockNumber == 0) {
error(SD_CARD_ERROR_WRITE_BLOCK_ZERO);
goto fail;
}
#endif // SD_PROTECT_BLOCK_ZERO
// send pre-erase count
if (cardAcmd(ACMD23, eraseCount)) {
error(SD_CARD_ERROR_ACMD23);
goto fail;
}
// use address if not SDHC card
if (type() != SD_CARD_TYPE_SDHC) blockNumber <<= 9;
if (cardCommand(CMD25, blockNumber)) {
error(SD_CARD_ERROR_CMD25);
goto fail;
}
return true;
fail:
chipSelectHigh();
return false;
}
//------------------------------------------------------------------------------
/** End a write multiple blocks sequence.
*
* \return The value one, true, is returned for success and
* the value zero, false, is returned for failure.
*/
uint8_t Sd2Card::writeStop(void) {
if (!waitNotBusy(SD_WRITE_TIMEOUT)) goto fail;
spiSend(STOP_TRAN_TOKEN);
if (!waitNotBusy(SD_WRITE_TIMEOUT)) goto fail;
chipSelectHigh();
return true;
fail:
error(SD_CARD_ERROR_STOP_TRAN);
chipSelectHigh();
return false;
}
+254
Ver Arquivo
@@ -0,0 +1,254 @@
/* Arduino Sd2Card Library
* Copyright (C) 2009 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
* Sd2Card class
*/
#include "Sd2PinMap.h"
#include "SdInfo.h"
//#include <plib.h>
/** 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/4. See Sd2Card::setSckRate(). */
uint8_t const SPI_HALF_SPEED = 1;
/** Set SCK rate to F_CPU/8. Sd2Card::setSckRate(). */
uint8_t const SPI_QUARTER_SPEED = 2;
/**
* Define MEGA_SOFT_SPI non-zero to use software SPI on Mega Arduinos.
* Pins used are SS 10, MOSI 11, MISO 12, and SCK 13.
*
* MEGA_SOFT_SPI allows an unmodified Adafruit GPS Shield to be used
* on Mega Arduinos. Software SPI works well with GPS Shield V1.1
* but many SD cards will fail with GPS Shield V1.0.
*/
#define MEGA_SOFT_SPI 0
//------------------------------------------------------------------------------
#if MEGA_SOFT_SPI
#define SOFTWARE_SPI
#endif // MEGA_SOFT_SPI
//------------------------------------------------------------------------------
// SPI pin definitions
//
#ifndef SOFTWARE_SPI
// hardware pin defs
/**
* SD Chip Select pin
*
* Warning if this pin is redefined the hardware SS will pin will be enabled
* as an output by init(). An avr processor will not function as an SPI
* master unless SS is set to output mode.
*/
/** The default chip select pin for the SD card is SS. */
uint8_t const SD_CHIP_SELECT_PIN = 9;
//// The following three pins must not be redefined for hardware SPI.
///** SPI Master Out Slave In pin */
//uint8_t const SPI_MOSI_PIN = MOSI_PIN;
///** SPI Master In Slave Out pin */
//uint8_t const SPI_MISO_PIN = MISO_PIN;
///** SPI Clock pin */
//uint8_t const SPI_SCK_PIN = SCK_PIN;
/** optimize loops for hardware SPI */
#define OPTIMIZE_HARDWARE_SPI
#else // SOFTWARE_SPI
// define software SPI pins so Mega can use unmodified GPS Shield
/** SPI chip select pin */
uint8_t const SD_CHIP_SELECT_PIN = 9;
/** SPI Master Out Slave In pin */
uint8_t const SPI_MOSI_PIN = 18;
/** SPI Master In Slave Out pin */
uint8_t const SPI_MISO_PIN = 10;
/** SPI Clock pin */
uint8_t const SPI_SCK_PIN = 7;
#endif // SOFTWARE_SPI
//------------------------------------------------------------------------------
/** Protect block zero from write if nonzero */
#define SD_PROTECT_BLOCK_ZERO 1
/** 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 */
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 CMD17 (read block) */
uint8_t const SD_CARD_ERROR_CMD17 = 0X3;
/** card returned an error response for CMD24 (write block) */
uint8_t const SD_CARD_ERROR_CMD24 = 0X4;
/** WRITE_MULTIPLE_BLOCKS command failed */
uint8_t const SD_CARD_ERROR_CMD25 = 0X05;
/** card returned an error response for CMD58 (read OCR) */
uint8_t const SD_CARD_ERROR_CMD58 = 0X06;
/** SET_WR_BLK_ERASE_COUNT failed */
uint8_t const SD_CARD_ERROR_ACMD23 = 0X07;
/** card's ACMD41 initialization process timeout */
uint8_t const SD_CARD_ERROR_ACMD41 = 0X08;
/** card returned a bad CSR version field */
uint8_t const SD_CARD_ERROR_BAD_CSD = 0X09;
/** erase block group command failed */
uint8_t const SD_CARD_ERROR_ERASE = 0X0A;
/** card not capable of single block erase */
uint8_t const SD_CARD_ERROR_ERASE_SINGLE_BLOCK = 0X0B;
/** Erase sequence timed out */
uint8_t const SD_CARD_ERROR_ERASE_TIMEOUT = 0X0C;
/** card returned an error token instead of read data */
uint8_t const SD_CARD_ERROR_READ = 0X0D;
/** read CID or CSD failed */
uint8_t const SD_CARD_ERROR_READ_REG = 0X0E;
/** timeout while waiting for start of read data */
uint8_t const SD_CARD_ERROR_READ_TIMEOUT = 0X0F;
/** card did not accept STOP_TRAN_TOKEN */
uint8_t const SD_CARD_ERROR_STOP_TRAN = 0X10;
/** card returned an error token as a response to a write operation */
uint8_t const SD_CARD_ERROR_WRITE = 0X11;
/** attempt to write protected block zero */
uint8_t const SD_CARD_ERROR_WRITE_BLOCK_ZERO = 0X12;
/** card did not go ready for a multiple block write */
uint8_t const SD_CARD_ERROR_WRITE_MULTIPLE = 0X13;
/** card returned an error to a CMD13 status check after a write */
uint8_t const SD_CARD_ERROR_WRITE_PROGRAMMING = 0X14;
/** timeout occurred during write programming */
uint8_t const SD_CARD_ERROR_WRITE_TIMEOUT = 0X15;
/** incorrect rate selected */
uint8_t const SD_CARD_ERROR_SCK_RATE = 0X16;
//------------------------------------------------------------------------------
// 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;
//------------------------------------------------------------------------------
/**
* \class Sd2Card
* \brief Raw access to SD and SDHC flash memory cards.
*/
#include <DSPI.h>
#include <WProgram.h>
class Sd2Card {
public:
/** Construct an instance of Sd2Card. */
Sd2Card(void) : errorCode_(0), inBlock_(0), partialBlockRead_(0), type_(0), _cs(8), _spi(new DSPI0) {}
Sd2Card(uint8_t cs) : errorCode_(0), inBlock_(0), partialBlockRead_(0), type_(0), _cs(cs), _spi(new DSPI0) {}
Sd2Card(DSPI0 *spi) : errorCode_(0), inBlock_(0), partialBlockRead_(0), type_(0), _cs(8), _spi(spi) {}
Sd2Card(DSPI0 *spi, uint8_t cs) : errorCode_(0), inBlock_(0), partialBlockRead_(0), type_(0), _cs(cs), _spi(spi) {}
Sd2Card(uint8_t mosi, uint8_t miso, uint8_t clk) : _mosi(mosi), _miso(miso), _clk(clk), _cs(8), _spi(NULL) {}
Sd2Card(uint8_t mosi, uint8_t miso, uint8_t clk, uint8_t cs) : _mosi(mosi), _miso(miso), _clk(clk), _cs(cs), _spi(NULL) {}
uint32_t cardSize(void);
uint8_t erase(uint32_t firstBlock, uint32_t lastBlock);
uint8_t eraseSingleBlockEnable(void);
/**
* \return error code for last error. See Sd2Card.h for a list of error codes.
*/
uint8_t errorCode(void) const {return errorCode_;}
/** \return error data for last error. */
uint8_t errorData(void) 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).
*/
uint8_t init(void) {
return init(SPI_FULL_SPEED, _cs);
}
/**
* Initialize an SD flash memory card with the selected SPI clock rate
* and the default SD chip select pin.
* See sd2Card::init(uint8_t sckRateID, uint8_t chipSelectPin).
*/
uint8_t init(uint8_t sckRateID) {
return init(sckRateID, _cs);
}
uint8_t init(uint8_t sckRateID, uint8_t chipSelectPin);
void partialBlockRead(uint8_t value);
/** Returns the current value, true or false, for partial block read. */
uint8_t partialBlockRead(void) const {return partialBlockRead_;}
uint8_t readBlock(uint32_t block, uint8_t* dst);
uint8_t readData(uint32_t block,
uint16_t offset, uint16_t count, uint8_t* dst);
/**
* Read a cards CID register. The CID contains card identification
* information such as Manufacturer ID, Product name, Product serial
* number and Manufacturing date. */
uint8_t readCID(cid_t* cid) {
return readRegister(CMD10, cid);
}
/**
* Read a cards CSD register. The CSD contains Card-Specific Data that
* provides information regarding access to the card's contents. */
uint8_t readCSD(csd_t* csd) {
return readRegister(CMD9, csd);
}
void readEnd(void);
uint8_t setSckRate(uint8_t sckRateID);
/** Return the card type: SD V1, SD V2 or SDHC */
uint8_t type(void) const {return type_;}
uint8_t writeBlock(uint32_t blockNumber, const uint8_t* src);
uint8_t writeData(const uint8_t* src);
uint8_t writeStart(uint32_t blockNumber, uint32_t eraseCount);
uint8_t writeStop(void);
private:
uint32_t block_;
uint8_t chipSelectPin_;
uint8_t errorCode_;
uint8_t inBlock_;
uint16_t offset_;
uint8_t partialBlockRead_;
uint8_t status_;
uint8_t type_;
uint8_t _mosi;
uint8_t _miso;
uint8_t _clk;
uint8_t _cs;
DSPI0 *_spi;
// private functions
uint8_t cardAcmd(uint8_t cmd, uint32_t arg) {
cardCommand(CMD55, 0);
return cardCommand(cmd, arg);
}
uint8_t cardCommand(uint8_t cmd, uint32_t arg);
void error(uint8_t code) {errorCode_ = code;}
uint8_t readRegister(uint8_t cmd, void* buf);
uint8_t sendWriteCommand(uint32_t blockNumber, uint32_t eraseCount);
void chipSelectHigh(void);
void chipSelectLow(void);
void type(uint8_t value) {type_ = value;}
uint8_t waitNotBusy(uint16_t timeoutMillis);
uint8_t writeData(uint8_t token, const uint8_t* src);
uint8_t waitStartBlock(void);
uint8_t spiRec(void);
void spiSend(uint8_t data);
};
#endif // Sd2Card_h
+157
Ver Arquivo
@@ -0,0 +1,157 @@
/* Arduino SdFat Library
* Copyright (C) 2010 by William Greiman
* Revision Date: 08/18/2011 (Olver Jones)
*
* 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/>.
*/
// Warning this file was generated by a program.
#ifndef Sd2PinMap_h
#define Sd2PinMap_h
#if defined(_BOARD_MEGA_) || defined(_BOARD_UNO_) || defined(_BOARD_UC32_)
//Pin 11
#define prtSDO IOPORT_G
#define trisSDO TRISG
#define latSDO LATG
#define bnSDO BIT_8
//Pin 12
#define prtSDI IOPORT_G
#define trisSDI TRISG
#define latSDI LATG
#define bnSDI BIT_7
//Pin 13
#define prtSCK IOPORT_G
#define trisSCK TRISG
#define latSCK LATG
#define bnSCK BIT_6
#elif defined(_BOARD_WF32_)
//uc Pin 52
#define prtSDO IOPORT_G
#define trisSDO TRISG
#define latSDO LATG
#define bnSDO BIT_13
//uc Pin 49
#define prtSDI IOPORT_G
#define trisSDI TRISG
#define latSDI LATG
#define bnSDI BIT_15
//uc Pin 50
#define prtSCK IOPORT_G
#define trisSCK TRISG
#define latSCK LATG
#define bnSCK BIT_14
#elif defined(_BOARD_PONTECH_QUICK240_USB_)
//uc Pin 72
#define prtSDO IOPORT_D
#define trisSDO TRISD
#define latSDO LATD
#define bnSDO BIT_0
//uc Pin 9
#define prtSDI IOPORT_C
#define trisSDI TRISC
#define latSDI LATC
#define bnSDI BIT_4
//uc Pin 70
#define prtSCK IOPORT_D
#define trisSCK TRISD
#define latSCK LATD
#define bnSCK BIT_10
#elif defined(_BOARD_CEREBOT_MX3CK_)
#define prtSDO IOPORT_F //JC
#define trisSDO TRISF
#define latSDO LATF
#define bnSDO BIT_5
#define prtSDI IOPORT_F
#define trisSDI TRISF
#define latSDI LATF
#define bnSDI BIT_4
#define prtSCK IOPORT_B
#define trisSCK TRISB
#define latSCK LATB
#define bnSCK BIT_14
#elif defined(_BOARD_CEREBOT_MX4CK_)
#define prtSDO IOPORT_B //JK
#define trisSDO TRISB
#define latSDO LATB
#define bnSDO BIT_11
#define prtSDI IOPORT_B
#define trisSDI TRISB
#define latSDI LATB
#define bnSDI BIT_12
#define prtSCK IOPORT_B
#define trisSCK TRISB
#define latSCK LATB
#define bnSCK BIT_13
#elif defined(_BOARD_CEREBOT_MX7CK_)
#define prtSDO IOPORT_F //JF
#define trisSDO TRISF
#define latSDO LATF
#define bnSDO BIT_5
#define prtSDI IOPORT_F
#define trisSDI TRISF
#define latSDI LATF
#define bnSDI BIT_4
#define prtSCK IOPORT_F
#define trisSCK TRISF
#define latSCK LATF
#define bnSCK BIT_13
#else
//* Dec 14, 2011 <MLS> Issue #160 this is the same, but we have to have a default, this still needs work
//Pin 11
#define prtSDO IOPORT_G
#define trisSDO TRISG
#define latSDO LATG
#define bnSDO BIT_8
//Pin 12
#define prtSDI IOPORT_G
#define trisSDI TRISG
#define latSDI LATG
#define bnSDI BIT_7
//Pin 13
#define prtSCK IOPORT_G
#define trisSCK TRISG
#define latSCK LATG
#define bnSCK BIT_6
#endif
#endif
+551
Ver Arquivo
@@ -0,0 +1,551 @@
/* Arduino SdFat Library
* Copyright (C) 2009 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 SdFat_h
#define SdFat_h
/**
* \file
* SdFile and SdVolume classes
*/
#include "Sd2Card.h"
#include "FatStructs.h"
#include "Print.h"
//------------------------------------------------------------------------------
/**
* Allow use of deprecated functions if non-zero
*/
#define ALLOW_DEPRECATED_FUNCTIONS 1
//------------------------------------------------------------------------------
// forward declaration since SdVolume is used in SdFile
class SdVolume;
//==============================================================================
// SdFile class
// flags for ls()
/** ls() flag to print modify date */
uint8_t const LS_DATE = 1;
/** ls() flag to print file size */
uint8_t const LS_SIZE = 2;
/** ls() flag for recursive list of subdirectories */
uint8_t const LS_R = 4;
// use the gnu style oflag in open()
/** open() oflag for reading */
uint8_t const O_READ = 0X01;
/** open() oflag - same as O_READ */
uint8_t const O_RDONLY = O_READ;
/** open() oflag for write */
uint8_t const O_WRITE = 0X02;
/** open() oflag - same as O_WRITE */
uint8_t const O_WRONLY = O_WRITE;
/** open() oflag for reading and writing */
uint8_t const O_RDWR = (O_READ | O_WRITE);
/** open() oflag mask for access modes */
uint8_t const O_ACCMODE = (O_READ | O_WRITE);
/** The file offset shall be set to the end of the file prior to each write. */
uint8_t const O_APPEND = 0X04;
/** synchronous writes - call sync() after each write */
uint8_t const O_SYNC = 0X08;
/** create the file if nonexistent */
uint8_t const O_CREAT = 0X10;
/** If O_CREAT and O_EXCL are set, open() shall fail if the file exists */
uint8_t const O_EXCL = 0X20;
/** truncate the file to zero length */
uint8_t const O_TRUNC = 0X40;
// flags for timestamp
/** set the file's last access date */
uint8_t const T_ACCESS = 1;
/** set the file's creation date and time */
uint8_t const T_CREATE = 2;
/** Set the file's write date and time */
uint8_t const T_WRITE = 4;
// values for type_
/** This SdFile has not been opened. */
uint8_t const FAT_FILE_TYPE_CLOSED = 0;
/** SdFile for a file */
uint8_t const FAT_FILE_TYPE_NORMAL = 1;
/** SdFile for a FAT16 root directory */
uint8_t const FAT_FILE_TYPE_ROOT16 = 2;
/** SdFile for a FAT32 root directory */
uint8_t const FAT_FILE_TYPE_ROOT32 = 3;
/** SdFile for a subdirectory */
uint8_t const FAT_FILE_TYPE_SUBDIR = 4;
/** Test value for directory type */
uint8_t const FAT_FILE_TYPE_MIN_DIR = FAT_FILE_TYPE_ROOT16;
/** date field for FAT directory entry */
static inline uint16_t FAT_DATE(uint16_t year, uint8_t month, uint8_t day) {
return (year - 1980) << 9 | month << 5 | day;
}
/** year part of FAT directory date field */
static inline uint16_t FAT_YEAR(uint16_t fatDate) {
return 1980 + (fatDate >> 9);
}
/** month part of FAT directory date field */
static inline uint8_t FAT_MONTH(uint16_t fatDate) {
return (fatDate >> 5) & 0XF;
}
/** day part of FAT directory date field */
static inline uint8_t FAT_DAY(uint16_t fatDate) {
return fatDate & 0X1F;
}
/** time field for FAT directory entry */
static inline uint16_t FAT_TIME(uint8_t hour, uint8_t minute, uint8_t second) {
return hour << 11 | minute << 5 | second >> 1;
}
/** hour part of FAT directory time field */
static inline uint8_t FAT_HOUR(uint16_t fatTime) {
return fatTime >> 11;
}
/** minute part of FAT directory time field */
static inline uint8_t FAT_MINUTE(uint16_t fatTime) {
return(fatTime >> 5) & 0X3F;
}
/** second part of FAT directory time field */
static inline uint8_t FAT_SECOND(uint16_t fatTime) {
return 2*(fatTime & 0X1F);
}
/** Default date for file timestamps is 1 Jan 2000 */
uint16_t const FAT_DEFAULT_DATE = ((2000 - 1980) << 9) | (1 << 5) | 1;
/** Default time for file timestamp is 1 am */
uint16_t const FAT_DEFAULT_TIME = (1 << 11);
//------------------------------------------------------------------------------
/**
* \class SdFile
* \brief Access FAT16 and FAT32 files on SD and SDHC cards.
*/
class SdFile : public Print {
public:
/** Create an instance of SdFile. */
SdFile(void) : type_(FAT_FILE_TYPE_CLOSED) {}
/**
* writeError is set to true if an error occurs during a write().
* Set writeError to false before calling print() and/or write() and check
* for true after calls to print() and/or write().
*/
//bool writeError;
/**
* Cancel unbuffered reads for this file.
* See setUnbufferedRead()
*/
void clearUnbufferedRead(void) {
flags_ &= ~F_FILE_UNBUFFERED_READ;
}
uint8_t close(void);
uint8_t contiguousRange(uint32_t* bgnBlock, uint32_t* endBlock);
uint8_t createContiguous(SdFile* dirFile,
const char* fileName, uint32_t size);
/** \return The current cluster number for a file or directory. */
uint32_t curCluster(void) const {return curCluster_;}
/** \return The current position for a file or directory. */
uint32_t curPosition(void) const {return curPosition_;}
/**
* Set the date/time callback function
*
* \param[in] dateTime The user's call back function. The callback
* function is of the form:
*
* \code
* void dateTime(uint16_t* date, uint16_t* time) {
* uint16_t year;
* uint8_t month, day, hour, minute, second;
*
* // User gets date and time from GPS or real-time clock here
*
* // return date using FAT_DATE macro to format fields
* *date = FAT_DATE(year, month, day);
*
* // return time using FAT_TIME macro to format fields
* *time = FAT_TIME(hour, minute, second);
* }
* \endcode
*
* Sets the function that is called when a file is created or when
* a file's directory entry is modified by sync(). All timestamps,
* access, creation, and modify, are set when a file is created.
* sync() maintains the last access date and last modify date/time.
*
* See the timestamp() function.
*/
static void dateTimeCallback(
void (*dateTime)(uint16_t* date, uint16_t* time)) {
dateTime_ = dateTime;
}
/**
* Cancel the date/time callback function.
*/
static void dateTimeCallbackCancel(void) {
// use explicit zero since NULL is not defined for Sanguino
dateTime_ = 0;
}
/** \return Address of the block that contains this file's directory. */
uint32_t dirBlock(void) const {return dirBlock_;}
uint8_t dirEntry(dir_t* dir);
/** \return Index of this file's directory in the block dirBlock. */
uint8_t dirIndex(void) const {return dirIndex_;}
static void dirName(const dir_t& dir, char* name);
/** \return The total number of bytes in a file or directory. */
uint32_t fileSize(void) const {return fileSize_;}
/** \return The first cluster number for a file or directory. */
uint32_t firstCluster(void) const {return firstCluster_;}
/** \return True if this is a SdFile for a directory else false. */
uint8_t isDir(void) const {return type_ >= FAT_FILE_TYPE_MIN_DIR;}
/** \return True if this is a SdFile for a file else false. */
uint8_t isFile(void) const {return type_ == FAT_FILE_TYPE_NORMAL;}
/** \return True if this is a SdFile for an open file/directory else false. */
uint8_t isOpen(void) const {return type_ != FAT_FILE_TYPE_CLOSED;}
/** \return True if this is a SdFile for a subdirectory else false. */
uint8_t isSubDir(void) const {return type_ == FAT_FILE_TYPE_SUBDIR;}
/** \return True if this is a SdFile for the root directory. */
uint8_t isRoot(void) const {
return type_ == FAT_FILE_TYPE_ROOT16 || type_ == FAT_FILE_TYPE_ROOT32;
}
void ls(uint8_t flags = 0, uint8_t indent = 0);
uint8_t makeDir(SdFile* dir, const char* dirName);
uint8_t open(SdFile* dirFile, uint16_t index, uint8_t oflag);
uint8_t open(SdFile* dirFile, const char* fileName, uint8_t oflag);
uint8_t openRoot(SdVolume* vol);
static void printDirName(const dir_t& dir, uint8_t width);
static void printFatDate(uint16_t fatDate);
static void printFatTime(uint16_t fatTime);
static void printTwoDigits(uint8_t v);
/**
* Read the next byte from a file.
*
* \return For success read returns the next byte in the file as an int.
* If an error occurs or end of file is reached -1 is returned.
*/
int16_t read(void) {
uint8_t b;
return read(&b, 1) == 1 ? b : -1;
}
int16_t read(void* buf, uint16_t nbyte);
int8_t readDir(dir_t* dir);
static uint8_t remove(SdFile* dirFile, const char* fileName);
uint8_t remove(void);
/** Set the file's current position to zero. */
void rewind(void) {
curPosition_ = curCluster_ = 0;
}
uint8_t rmDir(void);
uint8_t rmRfStar(void);
/** Set the files position to current position + \a pos. See seekSet(). */
uint8_t seekCur(uint32_t pos) {
return seekSet(curPosition_ + pos);
}
/**
* Set the files current position to end of file. Useful to position
* a file for append. See seekSet().
*/
uint8_t seekEnd(void) {return seekSet(fileSize_);}
uint8_t seekSet(uint32_t pos);
/**
* Use unbuffered reads to access this file. Used with Wave
* Shield ISR. Used with Sd2Card::partialBlockRead() in WaveRP.
*
* Not recommended for normal applications.
*/
void setUnbufferedRead(void) {
if (isFile()) flags_ |= F_FILE_UNBUFFERED_READ;
}
uint8_t timestamp(uint8_t flag, uint16_t year, uint8_t month, uint8_t day,
uint8_t hour, uint8_t minute, uint8_t second);
uint8_t sync(void);
/** Type of this SdFile. You should use isFile() or isDir() instead of type()
* if possible.
*
* \return The file or directory type.
*/
uint8_t type(void) const {return type_;}
uint8_t truncate(uint32_t size);
/** \return Unbuffered read flag. */
uint8_t unbufferedRead(void) const {
return flags_ & F_FILE_UNBUFFERED_READ;
}
/** \return SdVolume that contains this file. */
SdVolume* volume(void) const {return vol_;}
void write(uint8_t b);
size_t write(const void* buf, uint16_t nbyte);
void write(const char* str);
void write_P(char* str);
void writeln_P(char* str);
//------------------------------------------------------------------------------
#if ALLOW_DEPRECATED_FUNCTIONS
// Deprecated functions - suppress cpplint warnings with NOLINT comment
/** \deprecated Use:
* uint8_t SdFile::contiguousRange(uint32_t* bgnBlock, uint32_t* endBlock);
*/
uint8_t contiguousRange(uint32_t& bgnBlock, uint32_t& endBlock) { // NOLINT
return contiguousRange(&bgnBlock, &endBlock);
}
/** \deprecated Use:
* uint8_t SdFile::createContiguous(SdFile* dirFile,
* const char* fileName, uint32_t size)
*/
uint8_t createContiguous(SdFile& dirFile, // NOLINT
const char* fileName, uint32_t size) {
return createContiguous(&dirFile, fileName, size);
}
/**
* \deprecated Use:
* static void SdFile::dateTimeCallback(
* void (*dateTime)(uint16_t* date, uint16_t* time));
*/
static void dateTimeCallback(
void (*dateTime)(uint16_t& date, uint16_t& time)) { // NOLINT
oldDateTime_ = dateTime;
dateTime_ = dateTime ? oldToNew : 0;
}
/** \deprecated Use: uint8_t SdFile::dirEntry(dir_t* dir); */
uint8_t dirEntry(dir_t& dir) {return dirEntry(&dir);} // NOLINT
/** \deprecated Use:
* uint8_t SdFile::makeDir(SdFile* dir, const char* dirName);
*/
uint8_t makeDir(SdFile& dir, const char* dirName) { // NOLINT
return makeDir(&dir, dirName);
}
/** \deprecated Use:
* uint8_t SdFile::open(SdFile* dirFile, const char* fileName, uint8_t oflag);
*/
uint8_t open(SdFile& dirFile, // NOLINT
const char* fileName, uint8_t oflag) {
return open(&dirFile, fileName, oflag);
}
/** \deprecated Do not use in new apps */
uint8_t open(SdFile& dirFile, const char* fileName) { // NOLINT
return open(dirFile, fileName, O_RDWR);
}
/** \deprecated Use:
* uint8_t SdFile::open(SdFile* dirFile, uint16_t index, uint8_t oflag);
*/
uint8_t open(SdFile& dirFile, uint16_t index, uint8_t oflag) { // NOLINT
return open(&dirFile, index, oflag);
}
/** \deprecated Use: uint8_t SdFile::openRoot(SdVolume* vol); */
uint8_t openRoot(SdVolume& vol) {return openRoot(&vol);} // NOLINT
/** \deprecated Use: int8_t SdFile::readDir(dir_t* dir); */
int8_t readDir(dir_t& dir) {return readDir(&dir);} // NOLINT
/** \deprecated Use:
* static uint8_t SdFile::remove(SdFile* dirFile, const char* fileName);
*/
static uint8_t remove(SdFile& dirFile, const char* fileName) { // NOLINT
return remove(&dirFile, fileName);
}
//------------------------------------------------------------------------------
// rest are private
private:
static void (*oldDateTime_)(uint16_t& date, uint16_t& time); // NOLINT
static void oldToNew(uint16_t* date, uint16_t* time) {
uint16_t d;
uint16_t t;
oldDateTime_(d, t);
*date = d;
*time = t;
}
#endif // ALLOW_DEPRECATED_FUNCTIONS
private:
// bits defined in flags_
// should be 0XF
static uint8_t const F_OFLAG = (O_ACCMODE | O_APPEND | O_SYNC);
// available bits
static uint8_t const F_UNUSED = 0X30;
// use unbuffered SD read
static uint8_t const F_FILE_UNBUFFERED_READ = 0X40;
// sync of directory entry required
static uint8_t const F_FILE_DIR_DIRTY = 0X80;
// make sure F_OFLAG is ok
#if ((F_UNUSED | F_FILE_UNBUFFERED_READ | F_FILE_DIR_DIRTY) & F_OFLAG)
#error flags_ bits conflict
#endif // flags_ bits
// private data
uint8_t flags_; // See above for definition of flags_ bits
uint8_t type_; // type of file see above for values
uint32_t curCluster_; // cluster for current file position
uint32_t curPosition_; // current file position in bytes from beginning
uint32_t dirBlock_; // SD block that contains directory entry for file
uint8_t dirIndex_; // index of entry in dirBlock 0 <= dirIndex_ <= 0XF
uint32_t fileSize_; // file size in bytes
uint32_t firstCluster_; // first cluster of file
SdVolume* vol_; // volume where file is located
// private functions
uint8_t addCluster(void);
uint8_t addDirCluster(void);
dir_t* cacheDirEntry(uint8_t action);
static void (*dateTime_)(uint16_t* date, uint16_t* time);
static uint8_t make83Name(const char* str, uint8_t* name);
uint8_t openCachedEntry(uint8_t cacheIndex, uint8_t oflags);
dir_t* readDirCache(void);
};
//==============================================================================
// SdVolume class
/**
* \brief Cache for an SD data block
*/
#pragma pack(push, 1)
union cache_t {
/** Used to access cached file data blocks. */
uint8_t data[512];
/** Used to access cached FAT16 entries. */
uint16_t fat16[256];
/** Used to access cached FAT32 entries. */
uint32_t fat32[128];
/** Used to access cached directory entries. */
dir_t dir[16];
/** Used to access a cached MasterBoot Record. */
mbr_t mbr;
/** Used to access to a cached FAT boot sector. */
fbs_t fbs;
};
#pragma pack(pop)
//------------------------------------------------------------------------------
/**
* \class SdVolume
* \brief Access FAT16 and FAT32 volumes on SD and SDHC cards.
*/
class SdVolume {
public:
/** Create an instance of SdVolume */
SdVolume(void) :allocSearchStart_(2), fatType_(0) {}
/** Clear the cache and returns a pointer to the cache. Used by the WaveRP
* recorder to do raw write to the SD card. Not for normal apps.
*/
static uint8_t* cacheClear(void) {
cacheFlush();
cacheBlockNumber_ = 0XFFFFFFFF;
return cacheBuffer_.data;
}
/**
* Initialize a FAT volume. Try partition one first then try super
* floppy format.
*
* \param[in] dev The Sd2Card where the volume is located.
*
* \return The value one, true, is returned for success and
* the value zero, false, is returned for failure. Reasons for
* failure include not finding a valid partition, not finding a valid
* FAT file system or an I/O error.
*/
uint8_t init(Sd2Card* dev) { return init(dev, 1) ? true : init(dev, 0);}
uint8_t init(Sd2Card* dev, uint8_t part);
// inline functions that return volume info
/** \return The volume's cluster size in blocks. */
uint8_t blocksPerCluster(void) const {return blocksPerCluster_;}
/** \return The number of blocks in one FAT. */
uint32_t blocksPerFat(void) const {return blocksPerFat_;}
/** \return The total number of clusters in the volume. */
uint32_t clusterCount(void) const {return clusterCount_;}
/** \return The shift count required to multiply by blocksPerCluster. */
uint8_t clusterSizeShift(void) const {return clusterSizeShift_;}
/** \return The logical block number for the start of file data. */
uint32_t dataStartBlock(void) const {return dataStartBlock_;}
/** \return The number of FAT structures on the volume. */
uint8_t fatCount(void) const {return fatCount_;}
/** \return The logical block number for the start of the first FAT. */
uint32_t fatStartBlock(void) const {return fatStartBlock_;}
/** \return The FAT type of the volume. Values are 12, 16 or 32. */
uint8_t fatType(void) const {return fatType_;}
/** \return The number of entries in the root directory for FAT16 volumes. */
uint32_t rootDirEntryCount(void) const {return rootDirEntryCount_;}
/** \return The logical block number for the start of the root directory
on FAT16 volumes or the first cluster number on FAT32 volumes. */
uint32_t rootDirStart(void) const {return rootDirStart_;}
/** return a pointer to the Sd2Card object for this volume */
static Sd2Card* sdCard(void) {return sdCard_;}
//------------------------------------------------------------------------------
#if ALLOW_DEPRECATED_FUNCTIONS
// Deprecated functions - suppress cpplint warnings with NOLINT comment
/** \deprecated Use: uint8_t SdVolume::init(Sd2Card* dev); */
uint8_t init(Sd2Card& dev) {return init(&dev);} // NOLINT
/** \deprecated Use: uint8_t SdVolume::init(Sd2Card* dev, uint8_t vol); */
uint8_t init(Sd2Card& dev, uint8_t part) { // NOLINT
return init(&dev, part);
}
#endif // ALLOW_DEPRECATED_FUNCTIONS
//------------------------------------------------------------------------------
private:
// Allow SdFile access to SdVolume private data.
friend class SdFile;
// value for action argument in cacheRawBlock to indicate read from cache
static uint8_t const CACHE_FOR_READ = 0;
// value for action argument in cacheRawBlock to indicate cache dirty
static uint8_t const CACHE_FOR_WRITE = 1;
static cache_t cacheBuffer_; // 512 byte cache for device blocks
static uint32_t cacheBlockNumber_; // Logical number of block in the cache
static Sd2Card* sdCard_; // Sd2Card object for cache
static uint8_t cacheDirty_; // cacheFlush() will write block if true
static uint32_t cacheMirrorBlock_; // block number for mirror FAT
//
uint32_t allocSearchStart_; // start cluster for alloc search
uint8_t blocksPerCluster_; // cluster size in blocks
uint32_t blocksPerFat_; // FAT size in blocks
uint32_t clusterCount_; // clusters in one FAT
uint8_t clusterSizeShift_; // shift to convert cluster count to block count
uint32_t dataStartBlock_; // first data block number
uint8_t fatCount_; // number of FATs on volume
uint32_t fatStartBlock_; // start block for first FAT
uint8_t fatType_; // volume type (12, 16, OR 32)
uint16_t rootDirEntryCount_; // number of entries in FAT16 root dir
uint32_t rootDirStart_; // root start block for FAT16, cluster for FAT32
//----------------------------------------------------------------------------
uint8_t allocContiguous(uint32_t count, uint32_t* curCluster);
uint8_t blockOfCluster(uint32_t position) const {
return (position >> 9) & (blocksPerCluster_ - 1);}
uint32_t clusterStartBlock(uint32_t cluster) const {
return dataStartBlock_ + ((cluster - 2) << clusterSizeShift_);}
uint32_t blockNumber(uint32_t cluster, uint32_t position) const {
return clusterStartBlock(cluster) + blockOfCluster(position);}
static uint8_t cacheFlush(void);
static uint8_t cacheRawBlock(uint32_t blockNumber, uint8_t action);
static void cacheSetDirty(void) {cacheDirty_ |= CACHE_FOR_WRITE;}
static uint8_t cacheZeroBlock(uint32_t blockNumber);
uint8_t chainSize(uint32_t beginCluster, uint32_t* size) const;
uint8_t fatGet(uint32_t cluster, uint32_t* value) const;
uint8_t fatPut(uint32_t cluster, uint32_t value);
uint8_t fatPutEOC(uint32_t cluster) {
return fatPut(cluster, 0x0FFFFFFF);
}
uint8_t freeChain(uint32_t cluster);
uint8_t isEOC(uint32_t cluster) const {
return cluster >= (fatType_ == 16 ? FAT16EOC_MIN : FAT32EOC_MIN);
}
uint8_t readBlock(uint32_t block, uint8_t* dst) {
return sdCard_->readBlock(block, dst);}
uint8_t readData(uint32_t block, uint16_t offset,
uint16_t count, uint8_t* dst) {
return sdCard_->readData(block, offset, count, dst);
}
uint8_t writeBlock(uint32_t block, const uint8_t* dst) {
return sdCard_->writeBlock(block, dst);
}
};
#endif // SdFat_h
+70
Ver Arquivo
@@ -0,0 +1,70 @@
/* Arduino SdFat Library
* Copyright (C) 2008 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 SdFatUtil_h
#define SdFatUtil_h
/**
* \file
* Useful utility functions.
*/
#include <WProgram.h>
/** Store and print a string in flash memory.*/
#define PgmPrint(x) SerialPrint_P(PSTR(x))
/** Store and print a string in flash memory followed by a CR/LF.*/
#define PgmPrintln(x) SerialPrintln_P(PSTR(x))
/** Defined so doxygen works for function definitions. */
#define NOINLINE __attribute__((noinline,unused))
#define UNUSEDOK __attribute__((unused))
//------------------------------------------------------------------------------
/** Return the number of bytes currently free in RAM. */
static UNUSEDOK int FreeRam(void) {
extern int __bss_end;
extern int* __brkval;
int free_memory;
if (reinterpret_cast<int>(__brkval) == 0) {
// if no heap use from end of bss section
free_memory = reinterpret_cast<int>(&free_memory)
- reinterpret_cast<int>(&__bss_end);
} else {
// use from top of stack to heap
free_memory = reinterpret_cast<int>(&free_memory)
- reinterpret_cast<int>(__brkval);
}
return free_memory;
}
//------------------------------------------------------------------------------
/**
* %Print a string in flash memory to the serial port.
*
* \param[in] str Pointer to string stored in flash memory.
*/
static NOINLINE void SerialPrint_P(char* str) {
Serial.print(str);
}
//------------------------------------------------------------------------------
/**
* %Print a string in flash memory followed by a CR/LF.
*
* \param[in] str Pointer to string stored in flash memory.
*/
static NOINLINE void SerialPrintln_P(char* str) {
SerialPrint_P(str);
Serial.println();
}
#endif // #define SdFatUtil_h
+202
Ver Arquivo
@@ -0,0 +1,202 @@
/* Arduino SdFat Library
* Copyright (C) 2009 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/>.
*/
/**
\mainpage Arduino SdFat Library
<CENTER>Copyright &copy; 2009 by William Greiman
</CENTER>
\section Intro Introduction
The Arduino SdFat Library is a minimal implementation of FAT16 and FAT32
file systems on SD flash memory cards. Standard SD and high capacity
SDHC cards are supported.
The SdFat only supports short 8.3 names.
The main classes in SdFat are Sd2Card, SdVolume, and SdFile.
The Sd2Card class supports access to standard SD cards and SDHC cards. Most
applications will only need to call the Sd2Card::init() member function.
The SdVolume class supports FAT16 and FAT32 partitions. Most applications
will only need to call the SdVolume::init() member function.
The SdFile class provides file access functions such as open(), read(),
remove(), write(), close() and sync(). This class supports access to the root
directory and subdirectories.
A number of example are provided in the SdFat/examples folder. These were
developed to test SdFat and illustrate its use.
SdFat was developed for high speed data recording. SdFat was used to implement
an audio record/play class, WaveRP, for the Adafruit Wave Shield. This
application uses special Sd2Card calls to write to contiguous files in raw mode.
These functions reduce write latency so that audio can be recorded with the
small amount of RAM in the Arduino.
\section SDcard SD\SDHC Cards
Arduinos access SD cards using the cards SPI protocol. PCs, Macs, and
most consumer devices use the 4-bit parallel SD protocol. A card that
functions well on A PC or Mac may not work well on the Arduino.
Most cards have good SPI read performance but cards vary widely in SPI
write performance. Write performance is limited by how efficiently the
card manages internal erase/remapping operations. The Arduino cannot
optimize writes to reduce erase operations because of its limit RAM.
SanDisk cards generally have good write performance. They seem to have
more internal RAM buffering than other cards and therefore can limit
the number of flash erase operations that the Arduino forces due to its
limited RAM.
\section Hardware Hardware Configuration
SdFat was developed using an
<A HREF = "http://www.adafruit.com/"> Adafruit Industries</A>
<A HREF = "http://www.ladyada.net/make/waveshield/"> Wave Shield</A>.
The hardware interface to the SD card should not use a resistor based level
shifter. SdFat sets the SPI bus frequency to 8 MHz which results in signal
rise times that are too slow for the edge detectors in many newer SD card
controllers when resistor voltage dividers are used.
The 5 to 3.3 V level shifter for 5 V Arduinos should be IC based like the
74HC4050N based circuit shown in the file SdLevel.png. The Adafruit Wave Shield
uses a 74AHC125N. Gravitech sells SD and MicroSD Card Adapters based on the
74LCX245.
If you are using a resistor based level shifter and are having problems try
setting the SPI bus frequency to 4 MHz. This can be done by using
card.init(SPI_HALF_SPEED) to initialize the SD card.
\section comment Bugs and Comments
If you wish to report bugs or have comments, send email to fat16lib@sbcglobal.net.
\section SdFatClass SdFat Usage
SdFat uses a slightly restricted form of short names.
Only printable ASCII characters are supported. No characters with code point
values greater than 127 are allowed. Space is not allowed even though space
was allowed in the API of early versions of DOS.
Short names are limited to 8 characters followed by an optional period (.)
and extension of up to 3 characters. The characters may be any combination
of letters and digits. The following special characters are also allowed:
$ % ' - _ @ ~ ` ! ( ) { } ^ # &
Short names are always converted to upper case and their original case
value is lost.
\note
The Arduino Print class uses character
at a time writes so it was necessary to use a \link SdFile::sync() sync() \endlink
function to control when data is written to the SD card.
\par
An application which writes to a file using \link Print::print() print()\endlink,
\link Print::println() println() \endlink
or \link SdFile::write write() \endlink must call \link SdFile::sync() sync() \endlink
at the appropriate time to force data and directory information to be written
to the SD Card. Data and directory information are also written to the SD card
when \link SdFile::close() close() \endlink is called.
\par
Applications must use care calling \link SdFile::sync() sync() \endlink
since 2048 bytes of I/O is required to update file and
directory information. This includes writing the current data block, reading
the block that contains the directory entry for update, writing the directory
block back and reading back the current data block.
It is possible to open a file with two or more instances of SdFile. A file may
be corrupted if data is written to the file by more than one instance of SdFile.
\section HowTo How to format SD Cards as FAT Volumes
You should use a freshly formatted SD card for best performance. FAT
file systems become slower if many files have been created and deleted.
This is because the directory entry for a deleted file is marked as deleted,
but is not deleted. When a new file is created, these entries must be scanned
before creating the file, a flaw in the FAT design. Also files can become
fragmented which causes reads and writes to be slower.
Microsoft operating systems support removable media formatted with a
Master Boot Record, MBR, or formatted as a super floppy with a FAT Boot Sector
in block zero.
Microsoft operating systems expect MBR formatted removable media
to have only one partition. The first partition should be used.
Microsoft operating systems do not support partitioning SD flash cards.
If you erase an SD card with a program like KillDisk, Most versions of
Windows will format the card as a super floppy.
The best way to restore an SD card's format is to use SDFormatter
which can be downloaded from:
http://www.sdcard.org/consumers/formatter/
SDFormatter aligns flash erase boundaries with file
system structures which reduces write latency and file system overhead.
SDFormatter does not have an option for FAT type so it may format
small cards as FAT12.
After the MBR is restored by SDFormatter you may need to reformat small
cards that have been formatted FAT12 to force the volume type to be FAT16.
If you reformat the SD card with an OS utility, choose a cluster size that
will result in:
4084 < CountOfClusters && CountOfClusters < 65525
The volume will then be FAT16.
If you are formatting an SD card on OS X or Linux, be sure to use the first
partition. Format this partition with a cluster count in above range.
\section References References
Adafruit Industries:
http://www.adafruit.com/
http://www.ladyada.net/make/waveshield/
The Arduino site:
http://www.arduino.cc/
For more information about FAT file systems see:
http://www.microsoft.com/whdc/system/platform/firmware/fatgen.mspx
For information about using SD cards as SPI devices see:
http://www.sdcard.org/developers/tech/sdcard/pls/Simplified_Physical_Layer_Spec.pdf
The ATmega328 datasheet:
http://www.atmel.com/dyn/resources/prod_documents/doc8161.pdf
*/
+1284
Ver Arquivo
Diferenças do arquivo suprimidas por serem muito extensas Carregar Diff
+238
Ver Arquivo
@@ -0,0 +1,238 @@
/* Arduino Sd2Card Library
* Copyright (C) 2009 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 SdInfo_h
#define SdInfo_h
#include <stdint.h>
// Based on the document:
//
// SD Specifications
// Part 1
// Physical Layer
// Simplified Specification
// Version 2.00
// September 25, 2006
//
// www.sdcard.org/developers/tech/sdcard/pls/Simplified_Physical_Layer_Spec.pdf
//------------------------------------------------------------------------------
// SD card commands
/** GO_IDLE_STATE - init card in spi mode if CS low */
uint8_t const CMD0 = 0X00;
/** SEND_IF_COND - verify SD Memory Card interface operating condition.*/
uint8_t const CMD8 = 0X08;
/** SEND_CSD - read the Card Specific Data (CSD register) */
uint8_t const CMD9 = 0X09;
/** SEND_CID - read the card identification information (CID register) */
uint8_t const CMD10 = 0X0A;
/** SEND_STATUS - read the card status register */
uint8_t const CMD13 = 0X0D;
/** READ_BLOCK - read a single data block from the card */
uint8_t const CMD17 = 0X11;
/** WRITE_BLOCK - write a single data block to the card */
uint8_t const CMD24 = 0X18;
/** WRITE_MULTIPLE_BLOCK - write blocks of data until a STOP_TRANSMISSION */
uint8_t const CMD25 = 0X19;
/** ERASE_WR_BLK_START - sets the address of the first block to be erased */
uint8_t const CMD32 = 0X20;
/** ERASE_WR_BLK_END - sets the address of the last block of the continuous
range to be erased*/
uint8_t const CMD33 = 0X21;
/** ERASE - erase all previously selected blocks */
uint8_t const CMD38 = 0X26;
/** APP_CMD - escape for application specific command */
uint8_t const CMD55 = 0X37;
/** READ_OCR - read the OCR register of a card */
uint8_t const CMD58 = 0X3A;
/** SET_WR_BLK_ERASE_COUNT - Set the number of write blocks to be
pre-erased before writing */
uint8_t const ACMD23 = 0X17;
/** SD_SEND_OP_COMD - Sends host capacity support information and
activates the card's initialization process */
uint8_t const ACMD41 = 0X29;
//------------------------------------------------------------------------------
/** status for card in the ready state */
uint8_t const R1_READY_STATE = 0X00;
/** status for card in the idle state */
uint8_t const R1_IDLE_STATE = 0X01;
/** status bit for illegal command */
uint8_t const R1_ILLEGAL_COMMAND = 0X04;
/** start data token for read or write single block*/
uint8_t const DATA_START_BLOCK = 0XFE;
/** stop token for write multiple blocks*/
uint8_t const STOP_TRAN_TOKEN = 0XFD;
/** start data token for write multiple blocks*/
uint8_t const WRITE_MULTIPLE_TOKEN = 0XFC;
/** mask for data response tokens after a write block operation */
uint8_t const DATA_RES_MASK = 0X1F;
/** write data accepted token */
uint8_t const DATA_RES_ACCEPTED = 0X05;
//------------------------------------------------------------------------------
#pragma pack(push 1)
typedef struct CID {
// byte 0
uint8_t mid; // Manufacturer ID
// byte 1-2
char oid[2]; // OEM/Application ID
// byte 3-7
char pnm[5]; // Product name
// byte 8
unsigned prv_m : 4; // Product revision n.m
unsigned prv_n : 4;
// byte 9-12
uint32_t psn; // Product serial number
// byte 13
unsigned mdt_year_high : 4; // Manufacturing date
unsigned reserved : 4;
// byte 14
unsigned mdt_month : 4;
unsigned mdt_year_low :4;
// byte 15
unsigned always1 : 1;
unsigned crc : 7;
} cid_t;
//------------------------------------------------------------------------------
// CSD for version 1.00 cards
typedef struct CSDV1 {
// byte 0
unsigned reserved1 : 6;
unsigned csd_ver : 2;
// byte 1
uint8_t taac;
// byte 2
uint8_t nsac;
// byte 3
uint8_t tran_speed;
// byte 4
uint8_t ccc_high;
// byte 5
unsigned read_bl_len : 4;
unsigned ccc_low : 4;
// byte 6
unsigned c_size_high : 2;
unsigned reserved2 : 2;
unsigned dsr_imp : 1;
unsigned read_blk_misalign :1;
unsigned write_blk_misalign : 1;
unsigned read_bl_partial : 1;
// byte 7
uint8_t c_size_mid;
// byte 8
unsigned vdd_r_curr_max : 3;
unsigned vdd_r_curr_min : 3;
unsigned c_size_low :2;
// byte 9
unsigned c_size_mult_high : 2;
unsigned vdd_w_cur_max : 3;
unsigned vdd_w_curr_min : 3;
// byte 10
unsigned sector_size_high : 6;
unsigned erase_blk_en : 1;
unsigned c_size_mult_low : 1;
// byte 11
unsigned wp_grp_size : 7;
unsigned sector_size_low : 1;
// byte 12
unsigned write_bl_len_high : 2;
unsigned r2w_factor : 3;
unsigned reserved3 : 2;
unsigned wp_grp_enable : 1;
// byte 13
unsigned reserved4 : 5;
unsigned write_partial : 1;
unsigned write_bl_len_low : 2;
// byte 14
unsigned reserved5: 2;
unsigned file_format : 2;
unsigned tmp_write_protect : 1;
unsigned perm_write_protect : 1;
unsigned copy : 1;
unsigned file_format_grp : 1;
// byte 15
unsigned always1 : 1;
unsigned crc : 7;
} csd1_t;
//------------------------------------------------------------------------------
// CSD for version 2.00 cards
typedef struct CSDV2 {
// byte 0
unsigned reserved1 : 6;
unsigned csd_ver : 2;
// byte 1
uint8_t taac;
// byte 2
uint8_t nsac;
// byte 3
uint8_t tran_speed;
// byte 4
uint8_t ccc_high;
// byte 5
unsigned read_bl_len : 4;
unsigned ccc_low : 4;
// byte 6
unsigned reserved2 : 4;
unsigned dsr_imp : 1;
unsigned read_blk_misalign :1;
unsigned write_blk_misalign : 1;
unsigned read_bl_partial : 1;
// byte 7
unsigned reserved3 : 2;
unsigned c_size_high : 6;
// byte 8
uint8_t c_size_mid;
// byte 9
uint8_t c_size_low;
// byte 10
unsigned sector_size_high : 6;
unsigned erase_blk_en : 1;
unsigned reserved4 : 1;
// byte 11
unsigned wp_grp_size : 7;
unsigned sector_size_low : 1;
// byte 12
unsigned write_bl_len_high : 2;
unsigned r2w_factor : 3;
unsigned reserved5 : 2;
unsigned wp_grp_enable : 1;
// byte 13
unsigned reserved6 : 5;
unsigned write_partial : 1;
unsigned write_bl_len_low : 2;
// byte 14
unsigned reserved7: 2;
unsigned file_format : 2;
unsigned tmp_write_protect : 1;
unsigned perm_write_protect : 1;
unsigned copy : 1;
unsigned file_format_grp : 1;
// byte 15
unsigned always1 : 1;
unsigned crc : 7;
} csd2_t;
#pragma pack(pop)
//------------------------------------------------------------------------------
// union of old and new style CSD register
union csd_t {
csd1_t v1;
csd2_t v2;
};
#endif // SdInfo_h
+298
Ver Arquivo
@@ -0,0 +1,298 @@
/* Arduino SdFat Library
* Copyright (C) 2009 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 <SdFat.h>
//------------------------------------------------------------------------------
// raw block cache
// init cacheBlockNumber_to invalid SD block number
uint32_t SdVolume::cacheBlockNumber_ = 0XFFFFFFFF;
cache_t SdVolume::cacheBuffer_; // 512 byte cache for Sd2Card
Sd2Card* SdVolume::sdCard_; // pointer to SD card object
uint8_t SdVolume::cacheDirty_ = 0; // cacheFlush() will write block if true
uint32_t SdVolume::cacheMirrorBlock_ = 0; // mirror block for second FAT
//------------------------------------------------------------------------------
// find a contiguous group of clusters
uint8_t SdVolume::allocContiguous(uint32_t count, uint32_t* curCluster) {
// start of group
uint32_t bgnCluster;
// flag to save place to start next search
uint8_t setStart;
// set search start cluster
if (*curCluster) {
// try to make file contiguous
bgnCluster = *curCluster + 1;
// don't save new start location
setStart = false;
} else {
// start at likely place for free cluster
bgnCluster = allocSearchStart_;
// save next search start if one cluster
setStart = 1 == count;
}
// end of group
uint32_t endCluster = bgnCluster;
// last cluster of FAT
uint32_t fatEnd = clusterCount_ + 1;
// search the FAT for free clusters
for (uint32_t n = 0;; n++, endCluster++) {
// can't find space checked all clusters
if (n >= clusterCount_) return false;
// past end - start from beginning of FAT
if (endCluster > fatEnd) {
bgnCluster = endCluster = 2;
}
uint32_t f;
if (!fatGet(endCluster, &f)) return false;
if (f != 0) {
// cluster in use try next cluster as bgnCluster
bgnCluster = endCluster + 1;
} else if ((endCluster - bgnCluster + 1) == count) {
// done - found space
break;
}
}
// mark end of chain
if (!fatPutEOC(endCluster)) return false;
// link clusters
while (endCluster > bgnCluster) {
if (!fatPut(endCluster - 1, endCluster)) return false;
endCluster--;
}
if (*curCluster != 0) {
// connect chains
if (!fatPut(*curCluster, bgnCluster)) return false;
}
// return first cluster number to caller
*curCluster = bgnCluster;
// remember possible next free cluster
if (setStart) allocSearchStart_ = bgnCluster + 1;
return true;
}
//------------------------------------------------------------------------------
uint8_t SdVolume::cacheFlush(void) {
if (cacheDirty_) {
if (!sdCard_->writeBlock(cacheBlockNumber_, cacheBuffer_.data)) {
return false;
}
// mirror FAT tables
if (cacheMirrorBlock_) {
if (!sdCard_->writeBlock(cacheMirrorBlock_, cacheBuffer_.data)) {
return false;
}
cacheMirrorBlock_ = 0;
}
cacheDirty_ = 0;
}
return true;
}
//------------------------------------------------------------------------------
uint8_t SdVolume::cacheRawBlock(uint32_t blockNumber, uint8_t action) {
if (cacheBlockNumber_ != blockNumber) {
if (!cacheFlush()) return false;
if (!sdCard_->readBlock(blockNumber, cacheBuffer_.data)) {
return false;
}
cacheBlockNumber_ = blockNumber;
}
cacheDirty_ |= action;
return true;
}
//------------------------------------------------------------------------------
// cache a zero block for blockNumber
uint8_t SdVolume::cacheZeroBlock(uint32_t blockNumber) {
if (!cacheFlush()) return false;
// loop take less flash than memset(cacheBuffer_.data, 0, 512);
for (uint16_t i = 0; i < 512; i++) {
cacheBuffer_.data[i] = 0;
}
cacheBlockNumber_ = blockNumber;
cacheSetDirty();
return true;
}
//------------------------------------------------------------------------------
// return the size in bytes of a cluster chain
uint8_t SdVolume::chainSize(uint32_t cluster, uint32_t* size) const {
uint32_t s = 0;
do {
if (!fatGet(cluster, &cluster)) return false;
s += 512UL << clusterSizeShift_;
} while (!isEOC(cluster));
*size = s;
return true;
}
//------------------------------------------------------------------------------
// Fetch a FAT entry
uint8_t SdVolume::fatGet(uint32_t cluster, uint32_t* value) const {
if (cluster > (clusterCount_ + 1)) return false;
uint32_t lba = fatStartBlock_;
lba += fatType_ == 16 ? cluster >> 8 : cluster >> 7;
if (lba != cacheBlockNumber_) {
if (!cacheRawBlock(lba, CACHE_FOR_READ)) return false;
}
if (fatType_ == 16) {
*value = cacheBuffer_.fat16[cluster & 0XFF];
} else {
*value = cacheBuffer_.fat32[cluster & 0X7F] & FAT32MASK;
}
return true;
}
//------------------------------------------------------------------------------
// Store a FAT entry
uint8_t SdVolume::fatPut(uint32_t cluster, uint32_t value) {
// error if reserved cluster
if (cluster < 2) return false;
// error if not in FAT
if (cluster > (clusterCount_ + 1)) return false;
// calculate block address for entry
uint32_t lba = fatStartBlock_;
lba += fatType_ == 16 ? cluster >> 8 : cluster >> 7;
if (lba != cacheBlockNumber_) {
if (!cacheRawBlock(lba, CACHE_FOR_READ)) return false;
}
// store entry
if (fatType_ == 16) {
cacheBuffer_.fat16[cluster & 0XFF] = value;
} else {
cacheBuffer_.fat32[cluster & 0X7F] = value;
}
cacheSetDirty();
// mirror second FAT
if (fatCount_ > 1) cacheMirrorBlock_ = lba + blocksPerFat_;
return true;
}
//------------------------------------------------------------------------------
// free a cluster chain
uint8_t SdVolume::freeChain(uint32_t cluster) {
// clear free cluster location
allocSearchStart_ = 2;
do {
uint32_t next;
if (!fatGet(cluster, &next)) return false;
// free cluster
if (!fatPut(cluster, 0)) return false;
cluster = next;
} while (!isEOC(cluster));
return true;
}
//------------------------------------------------------------------------------
/**
* Initialize a FAT volume.
*
* \param[in] dev The SD card where the volume is located.
*
* \param[in] part The partition to be used. Legal values for \a part are
* 1-4 to use the corresponding partition on a device formatted with
* a MBR, Master Boot Record, or zero if the device is formatted as
* a super floppy with the FAT boot sector in block zero.
*
* \return The value one, true, is returned for success and
* the value zero, false, is returned for failure. Reasons for
* failure include not finding a valid partition, not finding a valid
* FAT file system in the specified partition or an I/O error.
*/
uint8_t SdVolume::init(Sd2Card* dev, uint8_t part) {
// Serial0.println("VOLUMEINIT");
uint32_t volumeStartBlock = 0;
sdCard_ = dev;
// if part == 0 assume super floppy with FAT boot sector in block zero
// if part > 0 assume mbr volume with partition table
if (part) {
if (part > 4)return false;
if (!cacheRawBlock(volumeStartBlock, CACHE_FOR_READ)) return false;
part_t* p = &cacheBuffer_.mbr.part[part-1];
if ((p->boot & 0X7F) !=0 ||
p->totalSectors < 100 ||
p->firstSector == 0) {
// not a valid partition
return false;
}
volumeStartBlock = p->firstSector;
}
if (!cacheRawBlock(volumeStartBlock, CACHE_FOR_READ)) return false;
bpb_t* bpb = &cacheBuffer_.fbs.bpb;
if (bpb->bytesPerSector != 512 ||
bpb->fatCount == 0 ||
bpb->reservedSectorCount == 0 ||
bpb->sectorsPerCluster == 0) {
// not valid FAT volume
return false;
}
fatCount_ = bpb->fatCount;
blocksPerCluster_ = bpb->sectorsPerCluster;
// determine shift that is same as multiply by blocksPerCluster_
clusterSizeShift_ = 0;
while (blocksPerCluster_ != (1 << clusterSizeShift_)) {
// error if not power of 2
if (clusterSizeShift_++ > 7) return false;
}
blocksPerFat_ = bpb->sectorsPerFat16 ?
bpb->sectorsPerFat16 : bpb->sectorsPerFat32;
fatStartBlock_ = volumeStartBlock + bpb->reservedSectorCount;
// count for FAT16 zero for FAT32
rootDirEntryCount_ = bpb->rootDirEntryCount;
// directory start for FAT16 dataStart for FAT32
rootDirStart_ = fatStartBlock_ + bpb->fatCount * blocksPerFat_;
// data start for FAT16 and FAT32
dataStartBlock_ = rootDirStart_ + ((32 * bpb->rootDirEntryCount + 511)/512);
// total blocks for FAT16 or FAT32
uint32_t totalBlocks = bpb->totalSectors16 ?
bpb->totalSectors16 : bpb->totalSectors32;
// total data blocks
clusterCount_ = totalBlocks - (dataStartBlock_ - volumeStartBlock);
// divide by cluster size to get cluster count
clusterCount_ >>= clusterSizeShift_;
// FAT type is determined by cluster count
if (clusterCount_ < 4085) {
fatType_ = 12;
} else if (clusterCount_ < 65525) {
fatType_ = 16;
} else {
rootDirStart_ = bpb->fat32RootCluster;
fatType_ = 32;
}
return true;
}
+489
Ver Arquivo
@@ -0,0 +1,489 @@
/*
*
* >>>> THIS CODE USED TO STREAM OpenBCI V3_32 DATA TO DONGLE <<<<
* >>>> May include the 60Hz notch filter by Chip Audette <<<<
*
* This code is written to target a PIC32MX250F128B with UDB32-MX2-DIP bootloader
* To Program, user must manually reset the PIC32 on the OpenBCI 32bit Board
* press RST, then press PROG, then release RST, then release PROG
* Adjust as needed if you are testing on different hardware.
*
*
* Made by Joel Murphy, Luke Travis, Conor Russomanno Summer, 2014.
* We're using the DSPI library, but our MISO and MOSI pins are not default
* Adjust file Mpide_new.app/Contents/Resources/Java/hardware/pic32/variants/DP32/Board_Defs.h
* Change the SPI1 defines thusly:
* #define _DSPI0_MISO_IN PPS_IN_SDI1
* #define _DSPI0_MISO_PIN 5 // [Changed for OpenBCI was 10] RA1 SDI1 SDI1R = RPA1 = 0
* #define _DSPI0_MOSI_OUT PPS_OUT_SDO1
* #define _DSPI0_MOSI_PIN 10 // [Changed for OpenBCI was 18] RA4 SDO1 RPA4R = SDO1 = 3
* Until chipKIT or Diligent allows user selection of MOSI/MISO
*
* Any SDcard code is based on RawWrite example in SDFat library
* ASCII commands are received on the serial port to configure and control
* Serial protocol uses '+' immediately before and after the command character
* We call this the 'burger' protocol. the '+' re the buns. Example:
* To begin streaming data, send '+b+'
* This software is provided as-is with no promise of workability
* Use at your own risk, wysiwyg.
TO DO
check that all functions are built correctly
verify, simplify for testing with GUI
Don't send serial unless the command to stop has passed down the radio chain!
*/
#include <SD.h>
#include <DSPI.h>
#include <EEPROM.h>
#include "OpenBCI_32.h"
//------------------------------------------------------------------------------
// << SD CARD BUSINESS >> has bee taken out. See OBCI_SD_LOG_CMRR
// SD_SS on pin 7 defined in OpenBCI library
boolean SDfileOpen = false;
char fileSize = '0'; // SD file size indicator
int blockCounter = 0;
//------------------------------------------------------------------------------
// << OpenBCI BUSINESS >>
boolean is_running = false; // this flag is set in serialEvent on reciept of ascii prompt
OpenBCI_32 OBCI; //Uses SPI bus and pins to say data is ready.
byte sampleCounter = 0;
// these are used to change individual channel settings from PC
char currentChannelToSet; // keep track of what channel we're loading settings for
boolean getChannelSettings = false; // used to receive channel settings command
int channelSettingsCounter; // used to retrieve channel settings from serial port
int leadOffSettingsCounter;
boolean getLeadOffSettings = false;
// these are all subject to the radio requirements: 31byte max packet length (maxPacketLength - 1 for packet checkSum)
#define OUTPUT_NOTHING (0) // quiet
#define OUTPUT_BINARY (1) // normal transfer mode
#define OUTPUT_BINARY_SYNTHETIC (2) // needs portage
int outputType;
//------------------------------------------------------------------------------
// << LIS3DH Accelerometer Business >>
// LIS3DH_SS on pin 5 defined in OpenBCI library
volatile boolean auxAvailable = false;
volatile boolean addAccel = true;
boolean useAccelOnly = false;
//------------------------------------------------------------------------------
// << PUT FILTER STUFF HERE >>
boolean useFilters = false;
//------------------------------------------------------------------------------
int LED = 11; // blue LED alias
int PGCpin = 12; // PGC pin goes high when PIC is in bootloader mode
//------------------------------------------------------------------------------
void setup(void) {
Serial0.begin(115200); // using hardware uart number 0
pinMode(LED, OUTPUT); digitalWrite(LED,HIGH); // blue LED
pinMode(PGCpin,OUTPUT); digitalWrite(PGCpin,LOW);// used to tell RFduino if we are in bootloader mode
startFromScratch();
}
void loop() {
if(is_running){
while(!(OBCI.isDataAvailable())){} // wait for DRDY pin...
OBCI.updateChannelData(); // get the fresh ADS results
if(OBCI.useAccel && OBCI.LIS3DH_DataAvailable()){
OBCI.LIS3DH_updateAxisData(); // fresh axis data goes into the X Y Z
addAccel = true;
}
if(SDfileOpen){
writeDataToSDcard(sampleCounter); //
}
OBCI.sendChannelData(sampleCounter);
sampleCounter++;
}
eventSerial();
}
// some variables to help find 'burger protocol' commands
int plusCounter = 0;
char testChar;
unsigned long commandTimer;
void eventSerial(){
while(Serial0.available()){
char inChar = (char)Serial0.read();
if(plusCounter == 1){ // if we have received the first 'bun'
testChar = inChar; // this might be the 'patty', stop laughing
plusCounter++; // get ready to look for another 'bun'
commandTimer = millis(); // don't wait too long!
}
if(inChar == '+'){ // if we see a 'bun' on the serial
plusCounter++; // make a note of it
if(plusCounter == 3){ // looks like we got a command character
if(millis() - commandTimer < 5){ // if it's not too late,
if(getChannelSettings){ // if we just got an 'x' expect channel setting parameters
loadChannelSettings(testChar); // go get em!
}else if(getLeadOffSettings){ // if we just got a 'z' expect lead-off setting parameters
loadLeadOffSettings(testChar); // go get em!
}else{
getCommand(testChar); // decode the command
}
}
plusCounter = 0; // get ready for the next one
}
}
}
}
void getCommand(char token){
switch (token){
//TURN CHANNELS ON/OFF COMMANDS
case '1':
changeChannelState_maintainRunningState(1,DEACTIVATE); break;
case '2':
changeChannelState_maintainRunningState(2,DEACTIVATE); break;
case '3':
changeChannelState_maintainRunningState(3,DEACTIVATE); break;
case '4':
changeChannelState_maintainRunningState(4,DEACTIVATE); break;
case '5':
changeChannelState_maintainRunningState(5,DEACTIVATE); break;
case '6':
changeChannelState_maintainRunningState(6,DEACTIVATE); break;
case '7':
changeChannelState_maintainRunningState(7,DEACTIVATE); break;
case '8':
changeChannelState_maintainRunningState(8,DEACTIVATE); break;
case '!':
changeChannelState_maintainRunningState(1,ACTIVATE); break;
case '@':
changeChannelState_maintainRunningState(2,ACTIVATE); break;
case '#':
changeChannelState_maintainRunningState(3,ACTIVATE); break;
case '$':
changeChannelState_maintainRunningState(4,ACTIVATE); break;
case '%':
changeChannelState_maintainRunningState(5,ACTIVATE); break;
case '^':
changeChannelState_maintainRunningState(6,ACTIVATE); break;
case '&':
changeChannelState_maintainRunningState(7,ACTIVATE); break;
case '*':
changeChannelState_maintainRunningState(8,ACTIVATE); break;
// TEST SIGNAL CONTROL COMMANDS
case '0':
activateAllChannelsToTestCondition(ADSINPUT_SHORTED,ADSTESTSIG_NOCHANGE,ADSTESTSIG_NOCHANGE); break;
case '-':
activateAllChannelsToTestCondition(ADSINPUT_TESTSIG,ADSTESTSIG_AMP_1X,ADSTESTSIG_PULSE_SLOW); break;
case '=':
activateAllChannelsToTestCondition(ADSINPUT_TESTSIG,ADSTESTSIG_AMP_1X,ADSTESTSIG_PULSE_FAST); break;
case 'p':
activateAllChannelsToTestCondition(ADSINPUT_TESTSIG,ADSTESTSIG_AMP_2X,ADSTESTSIG_DCSIG); break;
case '[':
activateAllChannelsToTestCondition(ADSINPUT_TESTSIG,ADSTESTSIG_AMP_2X,ADSTESTSIG_PULSE_SLOW); break;
case ']':
activateAllChannelsToTestCondition(ADSINPUT_TESTSIG,ADSTESTSIG_AMP_2X,ADSTESTSIG_PULSE_FAST); break;
// SD CARD COMMANDS
case 'A': case'S': case'F': case'G': case'H': case'J': case'K': case'L': case 'a': case 'h':
SDfileOpen = true; fileSize = token; setupSDcard(fileSize); // make SDfileOpen contingent on setupSDcard?
break;
case 'j':
if(SDfileOpen){
SDfileOpen = false;
closeSDfile();
}
break;
// CHANNEL SETTING COMMANDS
case 'x': // expect 6 parameters
if(!is_running) {Serial0.println("ready to accept new channel settings");}
channelSettingsCounter = 0;
getChannelSettings = true; break;
case 'X': // latch channel settings
if(!is_running) {Serial0.println("updating channel settings");}
writeChannelSettingsToADS(currentChannelToSet); break;
case 'd': // reset all channel settings to default
if(!is_running) {Serial0.println("updating channel settings to default");}
setChannelsToDefaultSetting(); break;
case 'D': // report the default settings
sendDefaultChannelSettings(); break;
case 'c': // use 8 channel mode
break;
case 'C': // use 16 channel mode
break;
// LEAD OFF IMPEDANCE DETECTION COMMANDS
case 'z': // expect 2 parameters
if(!is_running) {Serial0.println("ready to accept new impedance detect settings");}
leadOffSettingsCounter = 0; // reset counter
getLeadOffSettings = true;
break;
case 'Z': // latch impedance parameters
if(!is_running) {Serial0.println("updating impedance detect settings");}
changeChannelLeadOffDetect_maintainRunningState();
break;
// STREAM DATA AND FILTER COMMANDS
case 'v':
startFromScratch();
break;
case 'n': // turn on accelerator
sampleCounter = 0;
useAccelOnly = true;
if(SDfileOpen) stampSD(ACTIVATE);
OBCI.enable_accel(RATE_25HZ);
break;
case 'N': // turn off accelerator
if(SDfileOpen) stampSD(DEACTIVATE);
useAccelOnly = false;
OBCI.disable_accel();
break;
case 'b': // stream data
if(SDfileOpen) stampSD(ACTIVATE);
OBCI.enable_accel(RATE_25HZ); // fire up the accelerometer
startRunning(OUTPUT_BINARY); // turn on the fire hose
break;
case 's': // stop streaming data
if(SDfileOpen) stampSD(DEACTIVATE);
OBCI.disable_accel();
stopRunning();
break;
case 'f':
useFilters = true;
break;
case 'g':
useFilters = false;
break;
// QUERY THE ADS AND ACCEL REGITSTERS
case '?':
printRegisters();
break;
default:
break;
}
}// end of getCommand
void sendEOT(){
Serial0.print("$$$");
}
void loadChannelSettings(char c){
Serial0.print("load setting ");
if(channelSettingsCounter == 0){ // if it's the first byte in this channel's array, this byte is the channel number to set
currentChannelToSet = c - '1'; // we just got the channel to load settings into (shift number down for array usage)
channelSettingsCounter++;
if(!is_running) {
Serial0.print("for channel ");
Serial0.println(currentChannelToSet+1,DEC);
}
return;
}
// setting bytes are in order: POWER_DOWN, GAIN_SET, INPUT_TYPE_SET, BIAS_SET, SRB2_SET, SRB1_SET
if(!is_running) {
Serial0.print(channelSettingsCounter-1);
Serial0.print(" with "); Serial0.println(c);
}
c -= '0';
if(channelSettingsCounter-1 == GAIN_SET){ c <<= 4; }
OBCI.channelSettings[currentChannelToSet][channelSettingsCounter-1] = c;
channelSettingsCounter++;
if(channelSettingsCounter == 7){ // 1 currentChannelToSet, plus 6 channelSetting parameters
if(!is_running) Serial0.print("done receiving settings for channel ");Serial0.println(currentChannelToSet+1,DEC);
getChannelSettings = false;
}
}
void writeChannelSettingsToADS(char chan){
boolean is_running_when_called = is_running;
int cur_outputType = outputType;
stopRunning(); //must stop running to change channel settings
chan += 1; // this shifting nonsense has to stop
OBCI.writeChannelSettings(chan); // change the channel settings on ADS
if (is_running_when_called == true) {
startRunning(cur_outputType); //restart, if it was running before
}
}
void setChannelsToDefaultSetting(){
boolean is_running_when_called = is_running;
int cur_outputType = outputType;
stopRunning(); //must stop running to change channel settings
OBCI.setChannelsToDefault(); // default channel settings
if (is_running_when_called == true) {
startRunning(cur_outputType); //restart, if it was running before
}
}
void loadLeadOffSettings(char c){
if(leadOffSettingsCounter == 0){ // if it's the first byte in this channel's array, this byte is the channel number to set
currentChannelToSet = c - '1'; // we just got the channel to load settings into (shift number down for array usage)
if(!is_running) Serial0.print("changing LeadOff settings for channel "); Serial0.println(currentChannelToSet+1,DEC);
leadOffSettingsCounter++;
return;
}
// setting bytes are in order: PCHAN, NCHAN
if(!is_running) {
Serial0.print("load setting "); Serial0.print(leadOffSettingsCounter-1);
Serial0.print(" with "); Serial0.println(c);
}
c -= '0';
OBCI.leadOffSettings[currentChannelToSet][leadOffSettingsCounter-1] = c;
leadOffSettingsCounter++;
if(leadOffSettingsCounter == 3){ // 1 currentChannelToSet, plus 2 leadOff setting parameters
if(!is_running) Serial0.print("done receiving leadOff settings for channel ");Serial0.println(currentChannelToSet+1,DEC);
getLeadOffSettings = false; // release the serial COM
}
}
boolean stopRunning(void) {
if(is_running == true){
OBCI.stopStreaming(); // stop the data acquisition //
is_running = false;
}
return is_running;
}
boolean startRunning(int OUT_TYPE) {
if(is_running == false){
outputType = OUT_TYPE;
OBCI.startStreaming();
is_running = true;
}
return is_running;
}
int changeChannelState_maintainRunningState(int chan, int start)
{
boolean is_running_when_called = is_running;
int cur_outputType = outputType;
//must stop running to change channel settings
stopRunning();
if (start == true) {
Serial0.print("Activating channel ");
Serial0.println(chan);
OBCI.activateChannel(chan);
} else {
Serial0.print("Deactivating channel ");
Serial0.println(chan);
OBCI.deactivateChannel(chan);
}
//restart, if it was running before
if (is_running_when_called == true) {
startRunning(cur_outputType);
}
}
// CALLED WHEN COMMAND CHARACTER IS SEEN ON THE SERIAL PORT
int activateAllChannelsToTestCondition(int testInputCode, byte amplitudeCode, byte freqCode)
{
boolean is_running_when_called = is_running;
int cur_outputType = outputType;
//must stop running to change channel settings
stopRunning();
//set the test signal to the desired state
OBCI.configureInternalTestSignal(amplitudeCode,freqCode);
//loop over all channels to change their state
for (int Ichan=1; Ichan <= 8; Ichan++) {
OBCI.channelSettings[Ichan-1][INPUT_TYPE_SET] = testInputCode;
// OBCI.activateChannel(Ichan,gainCode,testInputCode,false); //Ichan must be [1 8]...it does not start counting from zero
}
OBCI.writeChannelSettings();
//restart, if it was running before
if (is_running_when_called == true) {
startRunning(cur_outputType);
}
}
int changeChannelLeadOffDetect_maintainRunningState()
{
boolean is_running_when_called = is_running;
int cur_outputType = outputType;
//must stop running to change channel settings
stopRunning();
OBCI.changeChannelLeadOffDetect();
//restart, if it was running before
if (is_running_when_called == true) {
startRunning(cur_outputType);
}
}
void sendDefaultChannelSettings(){
boolean is_running_when_called = is_running;
int cur_outputType = outputType;
OBCI.reportDefaultChannelSettings(); // reads CH1SET
sendEOT();
delay(10);
//restart, if it was running before
if (is_running_when_called == true) {
startRunning(cur_outputType);
}
}
void printRegisters(){
boolean is_running_when_called = is_running;
int cur_outputType = outputType;
//must stop running to change channel settings
stopRunning();
if(is_running == false){
// print the ADS and LIS3DH registers
OBCI.printAllRegisters();
}
sendEOT();
delay(20);
//restart, if it was running before
if (is_running_when_called == true) {
startRunning(cur_outputType);
}
}
void startFromScratch(){
delay(1000);
Serial0.print("OpenBCI V3 32bit Board\nSetting ADS1299 Channel Values\n");
OBCI.useAccel = true; // option to add accelerometer dat to stream
OBCI.useAux = false; // option to add user data to stream not implimented yet
OBCI.initialize();
OBCI.configureLeadOffDetection(LOFF_MAG_6NA, LOFF_FREQ_31p2HZ);
Serial0.print("ADS1299 Device ID: 0x"); Serial0.println(OBCI.ADS_getDeviceID(),HEX);
Serial0.print("LIS3DH Device ID: 0x"); Serial0.println(OBCI.LIS3DH_getDeviceID(),HEX);
sendEOT();
}
// DO FILTER STUFF HERE IF YOU LIKE
// end
+313
Ver Arquivo
@@ -0,0 +1,313 @@
#define BLOCK_5MIN 11000
#define BLOCK_15MIN 33000
#define BLOCK_30MIN 66000
#define BLOCK_1HR 131000
#define BLOCK_2HR 261000
#define BLOCK_4HR 521000
#define BLOCK_12HR 1561000
#define BLOCK_24HR 3122000
#define OVER_DIM 20 // make room for up to 20 write-time overruns
uint32_t BLOCK_COUNT;
SdFile openfile; // want to put this before setup...
Sd2Card card(&OBCI.spi,SD_SS);// SPI needs to be init'd before here
SdVolume volume;
SdFile root;
uint8_t* pCache; // array that points to the block buffer on SD card
uint32_t MICROS_PER_BLOCK = 2000; // block write longer than this will get flaged
uint32_t bgnBlock, endBlock; // file extent bookends
int byteCounter = 0; // used to hold position in cache
//int blockCounter; // count up to BLOCK_COUNT with this
boolean openvol;
boolean cardInit = false;
boolean fileIsOpen = false;
struct {
uint32_t block; // holds block number that over-ran
uint32_t micro; // holds the length of this of over-run
} over[OVER_DIM];
uint32_t overruns; // count the number of overruns
uint32_t maxWriteTime; // keep track of longest write time
uint32_t minWriteTime; // and shortest write time
uint32_t t; // used to measure total file write time
byte fileTens, fileOnes; // enumerate succesive files on card and store number in EEPROM
char currentFileName[] = "OBCI_00.TXT"; // file name will enumerate in hex 00 - FF
prog_char elapsedTime[] PROGMEM = {"\n%Total time mS:\n"}; // 17
prog_char minTime[] PROGMEM = { "%min Write time uS:\n"}; // 20
prog_char maxTime[] PROGMEM = { "%max Write time uS:\n"}; // 20
prog_char overNum[] PROGMEM = { "%Over:\n"}; // 7
prog_char blockTime[] PROGMEM = { "%block, uS\n"}; // 11 74 chars + 2 32(16) + 2 16(8) = 98 + (n 32x2) up to 24 overruns...
prog_char stopStamp[] PROGMEM = { "%STOP AT\n"}; // used to stamp SD record when stopped by PC
prog_char startStamp[] PROGMEM = { "%START AT\n"}; // used to stamp SD record when started by PC
void setupSDcard(char limit){
if(!cardInit){
if(!card.init(SPI_FULL_SPEED, SD_SS)) {
Serial0.println("initialization failed. Things to check:");
Serial0.println("* is a card is inserted?");
// card.init(SPI_FULL_SPEED, SD_SS);
}
else{
Serial0.println("Wiring is correct and a card is present.");
cardInit = true;
}
if (!volume.init(card)) { // Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
Serial0.println("Could not find FAT16/FAT32 partition. Make sure you've formatted the card");
return;
}
}
// use limit to determine file size
switch(limit){
case 'h':
BLOCK_COUNT = 50; break;
case 'a':
BLOCK_COUNT = 512; break;
case 'A':
BLOCK_COUNT = BLOCK_5MIN; break;
case 'S':
BLOCK_COUNT = BLOCK_15MIN; break;
case 'F':
BLOCK_COUNT = BLOCK_30MIN; break;
case 'G':
BLOCK_COUNT = BLOCK_1HR; break;
case 'H':
BLOCK_COUNT = BLOCK_2HR; break;
case 'J':
BLOCK_COUNT = BLOCK_4HR; break;
case 'K':
BLOCK_COUNT = BLOCK_12HR; break;
case 'L':
BLOCK_COUNT = BLOCK_24HR; break;
default: return; break;
}
incrementFileCounter();
openvol = root.openRoot(volume);
openfile.remove(root, currentFileName); // if the file is over-writing, let it!
if (!openfile.createContiguous(root, currentFileName, BLOCK_COUNT*512UL)) {
Serial0.print("createfdContiguous fail"); cardInit = false;
}//else{Serial0.print("got contiguous file...");delay(1);}
// get the location of the file's blocks
if (!openfile.contiguousRange(&bgnBlock, &endBlock)) {
Serial0.print("get contiguousRange fail"); cardInit = false;
}//else{Serial0.print("got file range...");delay(1);}
// grab the Cache
pCache = (uint8_t*)volume.cacheClear();
// tell card to setup for multiple block write with pre-erase
if (!card.erase(bgnBlock, endBlock)){ Serial0.println("erase block fail"); cardInit = false;
}//else{Serial0.print("erased...");delay(1);}
if (!card.writeStart(bgnBlock, BLOCK_COUNT)){ Serial0.println("writeStart fail"); cardInit = false;
}else{ fileIsOpen = true; delay(1);}
OBCI.csHigh(SD_SS); // release the spi
// initialize write-time overrun error counter and min/max wirte time benchmarks
overruns = 0;
maxWriteTime = 0;
minWriteTime = 65000;
byteCounter = 0; // counter from 0 - 512
blockCounter = 0; // counter from 0 - BLOCK_COUNT;
if(fileIsOpen == true){ // send corresponding file name to controlling program
Serial0.print("Corresponding SD file ");Serial0.println(currentFileName);sendEOT();
}
}
void closeSDfile(){
if(fileIsOpen){
OBCI.csLow(SD_SS); // take spi
card.writeStop();
openfile.close();
OBCI.csHigh(SD_SS); // release the spi
fileIsOpen = false;
if(!is_running){ // verbosity. this gets insterted as footer in openFile
Serial0.print("Total Elapsed Time: ");Serial0.print(t);Serial0.println("mS"); delay(10);
Serial0.print("Max write time: "); Serial0.print(maxWriteTime); Serial0.println(" uS"); delay(10);
Serial0.print("Min write time: ");Serial0.print(minWriteTime); Serial0.println(" uS"); delay(10);
Serial0.print("Overruns: "); Serial0.print(overruns); Serial0.println(); delay(10);
if (overruns) {
uint8_t n = overruns > OVER_DIM ? OVER_DIM : overruns;
Serial0.println("fileBlock,micros");
for (uint8_t i = 0; i < n; i++) {
Serial0.print(over[i].block); Serial0.print(','); Serial0.println(over[i].micro);
}
}
}
}else{
Serial0.println("no open file to close");
}
delay(100); // cool down
}
void writeDataToSDcard(byte sampleNumber){
boolean addComma = true;
// convert 8 bit sampleCounter into HEX
convertToHex(sampleNumber, 1, addComma);
// convert 24 bit channelData into HEX
for (int currentChannel = 0; currentChannel < 8; currentChannel++){
convertToHex(OBCI.channelDataInt[currentChannel], 5, addComma);
if(currentChannel == 6 && !addAccel) addComma = false; // format CSV
}
if(addAccel == true){ // if we have accelerometer data to log
// convert 16 bit accelerometer data into HEX
for (int currentChannel = 0; currentChannel < 3; currentChannel++){
convertToHex(OBCI.axisData[currentChannel], 3, addComma);
if(currentChannel == 1) addComma = false;
}
addAccel = false; // disallow aux data logging
}// end of accelerometer data log
// add aux data logging...
}
void writeCache(){
if(blockCounter > BLOCK_COUNT) return;
uint32_t tw = micros(); // start block write timer
OBCI.csLow(SD_SS); // take spi
if(!card.writeData(pCache)) {Serial0.println("block write fail");} // write the block
OBCI.csHigh(SD_SS); // release spi
tw = micros() - tw; // stop block write timer
if (tw > maxWriteTime) maxWriteTime = tw; // check for max write time
if (tw < minWriteTime) minWriteTime = tw; // check for min write time
if (tw > MICROS_PER_BLOCK) { // check for overrun
if (overruns < OVER_DIM) {
over[overruns].block = blockCounter;
over[overruns].micro = tw;
}
overruns++;
}
byteCounter = 0; // reset 512 byte counter for next block
blockCounter++; // increment BLOCK counter
if(blockCounter == BLOCK_COUNT-1){
t = millis() - t;
stopRunning();
OBCI.disable_accel();
useAccelOnly = false;
writeFooter();
}
if(blockCounter == BLOCK_COUNT){
closeSDfile();
BLOCK_COUNT = 0;
} // we did it!
}
void incrementFileCounter(){
fileTens = EEPROM.read(0);
fileOnes = EEPROM.read(1);
// if it's the first time writing to EEPROM, seed the file number to '00'
if(fileTens == 0xFF | fileOnes == 0xFF){
fileTens = fileOnes = '0';
}
fileOnes++; // increment the file name
if (fileOnes == ':'){fileOnes = 'A';}
if (fileOnes > 'F'){
fileOnes = '0'; // hexify
fileTens++;
if(fileTens == ':'){fileTens = 'A';}
if(fileTens > 'F'){fileTens = '0';fileOnes = '1';}
}
EEPROM.write(0,fileTens); // store current file number in eeprom
EEPROM.write(1,fileOnes);
currentFileName[5] = fileTens;
currentFileName[6] = fileOnes;
// // send corresponding file name to controlling program
// Serial0.print("Corresponding SD file ");Serial0.println(currentFileName);
}
void stampSD(boolean state){
unsigned long time = millis();
if(state){
for(int i=0; i<10; i++){
pCache[byteCounter] = pgm_read_byte_near(startStamp+i);
byteCounter++;
if(byteCounter == 512){
writeCache();
}
}
}
else{
for(int i=0; i<9; i++){
pCache[byteCounter] = pgm_read_byte_near(stopStamp+i);
byteCounter++;
if(byteCounter == 512){
writeCache();
}
}
}
convertToHex(time, 7, false);
}
void writeFooter(){
for(int i=0; i<17; i++){
pCache[byteCounter] = pgm_read_byte_near(elapsedTime+i);
byteCounter++;
}
convertToHex(t, 7, false);
for(int i=0; i<20; i++){
pCache[byteCounter] = pgm_read_byte_near(minTime+i);
byteCounter++;
}
convertToHex(minWriteTime, 7, false);
for(int i=0; i<20; i++){
pCache[byteCounter] = pgm_read_byte_near(maxTime+i);
byteCounter++;
}
convertToHex(maxWriteTime, 7, false);
for(int i=0; i<7; i++){
pCache[byteCounter] = pgm_read_byte_near(overNum+i);
byteCounter++;
}
convertToHex(overruns, 7, false);
for(int i=0; i<11; i++){
pCache[byteCounter] = pgm_read_byte_near(blockTime+i);
byteCounter++;
}
if (overruns) {
uint8_t n = overruns > OVER_DIM ? OVER_DIM : overruns;
for (uint8_t i = 0; i < n; i++) {
convertToHex(over[i].block, 7, true);
convertToHex(over[i].micro, 7, false);
}
}
for(int i=byteCounter; i<512; i++){
pCache[i] = NULL;
}
writeCache();
}
// CONVERT RAW BYTE DATA TO HEX FOR SD STORAGE
void convertToHex(long rawData, int numNibbles, boolean useComma){
for (int currentNibble = numNibbles; currentNibble >= 0; currentNibble--){
byte nibble = (rawData >> currentNibble*4) & 0x0F;
if (nibble > 9){
nibble += 55; // convert to ASCII A-F
}
else{
nibble += 48; // convert to ASCII 0-9
}
pCache[byteCounter] = nibble;
byteCounter++;
if(byteCounter == 512){
writeCache();
}
}
if(useComma == true){
pCache[byteCounter] = ',';
}else{
pCache[byteCounter] = '\n';
}
byteCounter++;
if(byteCounter == 512){
writeCache();
}
}// end of byteToHex converter
+26
Ver Arquivo
@@ -4,3 +4,29 @@ OpenBCI_32bit
Repository containing the firmware for the 32bit OpenBCI board.
Note that to USE the OpenBCI system, you will generally use the OpenBCI USB Dongle. The dongle requries that you install the FTDI drivers for your particular operating system: http://www.ftdichip.com/FTDrivers.htm
Remove the files OpenBCI_32 and SD from the OpenBCI_32_Library folder and put it in your documents/mpide/libraries folder.
Put the OpenBCI_32_SD into your documents/mpide folder and restart mpide to be able to select the sketch.
Before you upload the firmware, you need to make a change to an library file inside the mpide program folder:
* We're using the DSPI library, but our MISO and MOSI pins are not default
* Adjust file Mpide.app/Contents/Resources/Java/hardware/pic32/variants/DP32/Board_Defs.h
* Find the DSPI0 pin definition section and change the defines thusly:
* #define _DSPI0_MISO_IN PPS_IN_SDI1
* #define _DSPI0_MISO_PIN 5 // [Changed for OpenBCI. Was 10] RA1 SDI1 SDI1R = RPA1 = 0
* #define _DSPI0_MOSI_OUT PPS_OUT_SDO1
* #define _DSPI0_MOSI_PIN 10 // [Changed for OpenBCI. Was 18] RA4 SDO1 RPA4R = SDO1 = 3
* This will be necessary until chipKIT or Diligent allows user selection of MOSI/MISO
* Or until we get our own OpenBCI Board selection from the mpide (!)
When you upload the firmware, select the chipKIT DP32 from the Tools -> Board -> chipKIT menu,
select the serial port of the dongle,
then press upload.
We are uploading the sketch over air! There is a chance that the mpide will timeout during the upload process!
If this happens, you will need to unplug the dongle, and re-insert it to stop the upload.
Then, power cycle the OpenBCI board, as it is best that the Board radio comes on line after the dongle radio.
Then try again to upload. This is a known issue, and we can confirm that all boards shipped will take the upload
process, it just might take a couple of times to stick.