bool Tune::begin() { // Pin configuration pinMode(DREQ, INPUT_PULLUP); pinMode(XDCS, OUTPUT); pinMode(XCS, OUTPUT); pinMode(SDCS, OUTPUT); // Deselect control & data ctrl digitalWrite(XCS, HIGH); digitalWrite(XDCS, HIGH); // Deselect SD's chip select digitalWrite(SDCS, HIGH); // SD card initialization if (!sd.begin(SDCS, SPI_HALF_SPEED)) { sd.initErrorHalt(); // describe problem if there's one return 0; } // Tracklisting also return the number of playable files Serial.print(listFiles()); Serial.print(" tracks found, "); // SPI bus initialization SPI.begin(); SPI.setDataMode(SPI_MODE0); // Both SCI and SDI read data MSB first SPI.setBitOrder(MSBFIRST); // From the datasheet, max SPI reads are CLKI/6. Here CLKI = 26MHz -> SPI max speed is 4.33MHz. // We'll take 16MHz/4 = 4MHz to be safe. // Plus it's the max recommended speed when using a resistor-based lvl converter with an SD card. SPI.setClockDivider(SPI_CLOCK_DIV4); SPI.transfer(0xFF); delay(10); // Codec initialization // Software reset setBit(SCI_MODE, SM_RESET); delay(5); // VS1022 "New mode" activation setBit(SCI_MODE, SM_SDINEW); // Clock setting (default is 24.576MHz) writeSCI(SCI_CLOCKF, 0x32, 0xC8); delay(5); // Wait until the chip is ready while (!digitalRead(DREQ)); delay(100); // Set playState flag playState = idle; // Set volume to avoid hurt ears ;) setVolume(150); Serial.println("Tune ready !"); return 1; }
/** * Function starts the SD card service and makes sure that the appropriate directory * structure exists, then creates the file to use for logging data. Data will be logged * to a file called fileNamePrefix_0.csv or fileNamePrefix_0.bin (number will be incremented * for each new log file) * * @param chipSelectPin Arduino pin SD card reader CS pin is attached to * @param fileNamePrefix string to prefix at beginning of data file * @param csvData boolean flag whether we are writing csv data or binary data (used for filename) * * @return true if successful, false if failed */ bool beginDataLog(int chipSelectPin, const char *fileNamePrefix, bool csvData) { bool ret; int i = 0; int max_len; char fileName[19]; char prefix[8]; char rootPath[] = "/data"; if (_output_buffer != NULL) { delete []_output_buffer; } OUTPUT_BUF_SIZE = 512; _output_buffer = vol.cacheAddress()->output_buf; if (freeMemory() < 400) { strcpy_P(_getOutBuf(), sd_card_error); Serial.print(_getOutBuf()); Serial.print(freeMemory()); Serial.println(", need 400)"); ret = false; } else { ret = sd.begin(chipSelectPin, SPI_FULL_SPEED); } //Filenames need to fit the 8.3 filename convention, so truncate down the //given filename if it is too long. memcpy(prefix, fileNamePrefix, 7); prefix[7] = '\0'; if (ret) { if (!sd.exists(rootPath)) ret = sd.mkdir(rootPath); if (ret) { while (true) { if (i < 10) { max_len = 7; } else if (i < 100) { max_len = 6; } else if (i < 1000) { max_len = 5; } else { break; } prefix[max_len - 1] = '\0'; sprintf(fileName, "%s/%s%d.%s", rootPath, prefix, i, csvData ? "csv" : "bin"); if (!sd.exists(fileName)) { file = sd.open(fileName, FILE_WRITE); break; } i++; } } } return file.isOpen(); }
void init() { close(); #ifdef SDCARD if (!sd.begin(SD_CS, SPI_FULL_SPEED)) { // error_P("card.init"); return; } #endif }
void init_SD(){ if (!sd.begin(SDCARD_CSPIN, SPI_HALF_SPEED)) { f.SDCARD = 0; // If init fails, tell the code not to try to write on it debug[1] = 999; } else { f.SDCARD = 1; debug[1] = 000; } }
void TicketflySDBeacon::SDBeaconSetup (SdFat& SD) { Serial.begin (9600); Serial.print(F("Initializing SD card...")); pinMode(10, OUTPUT); if (!SD.begin(4)) { Serial.println(F("initialization failed!")); return; } Serial.println(F("initialization done.")); }
void SDcard::setup(){ Serial.begin(115200); while (!Serial) { ; // wait for serial port to connect. Needed for native USB port only } Serial.print("Initializing SD card..."); pinMode(10,OUTPUT); if (!SD.begin(4)) { Serial.println("Initialization failed!"); return; } Serial.println("initialization done."); }
int main() { // initialize the SD card at SPI_FULL_SPEED for best performance. // try SPI_HALF_SPEED if bus errors occur. if (!sd.begin(SD_ENABLE_PIN, SPI_FULL_SPEED)) sd.initErrorHalt(); if (!myFile.open("test.txt", O_RDWR | O_CREAT | O_AT_END)) { sd.errorHalt("opening test.txt for write failed"); } myFile.println("testing 1, 2, 3."); // close the file: myFile.close(); while (1); return 0; }
void setup() { pinMode(D7, OUTPUT); pinMode(SPEAKER, OUTPUT); pinMode(BUTTON, INPUT); Blynk.begin(BLYNK_AUTH); button.debounceTime = 20; button.multiclickTime = 20; // Only allow 1 click button.longClickTime = 2000; Display.begin(); Display.fillScreen(BLACK); if(false == sd.begin(SD_CS, SPI_HALF_SPEED)) { error("Failed to start SD"); return; } SdFile::dateTimeCallback(dateTime); AlarmManager::valueForCallback(valueFor); Particle.function("alarm", handleAlarm); }
boolean begin(uint8_t pin, uint8_t rate){ return sdfat.begin(pin, rate); // .init(rate, pin); }
void setupSdCard() { // initialize the SD card at full speed if (!sd.begin(SD_CHIP_SELECT, SPI_FULL_SPEED)) { Serial.println("No SD card"); } }
boolean SD_Init(void) { return sdf.begin(chipSelect,SPI_FULL_SPEED); }
void startBinLogger(void (*dateTime)(uint16_t *date, uint16_t *time)){ #ifdef LOGGER_DEBUG Serial.print("Size of Struct: "); Serial.println(sizeof(salus_data_t)); Serial.print("Data_DIM: "); Serial.println(DATA_DIM); Serial.print("FILL_DIM: "); Serial.println(FILL_DIM); Serial.print("Sizeof Block: "); Serial.println(sizeof(block_t)); Serial.println(); #endif if (!sd.begin(SD_CHIPSELECT, SD_SPI_SPEED)) { sd.initErrorHalt(); } int number = 0; char sName[80]; // Find a filename that hasn't been used already do { sprintf(sName, "Salus_Results_%d.bin", number++); } while (sd.exists(sName)); binFile.close(); binFile.dateTimeCallback(dateTime); if (!binFile.createContiguous(sd.vwd(), sName, 512 * FILE_BLOCK_COUNT)){ error("createContiguous failed"); } if (!binFile.contiguousRange(&bgnBlock, &endBlock)){ error("contiguousRange failed"); } // Use SdFat's internal buffer ( ???? ) uint8_t* cache = (uint8_t*)sd.vol()->cacheClear(); if (cache == 0) { error("cacheClear failed"); } binFile.dateTimeCallbackCancel(); uint32_t bgnErase = bgnBlock; uint32_t endErase; while (bgnErase < endBlock) { endErase = bgnErase + ERASE_SIZE; if (endErase > endBlock) { endErase = endBlock; } if (!sd.card()->erase(bgnErase, endErase)) { error("erase failed"); } bgnErase = endErase + 1; } // Start a multiple block write. if (!sd.card()->writeStart(bgnBlock, FILE_BLOCK_COUNT)) { error("writeBegin failed"); } }
void OpenSprinkler::begin() { // shift register setup pinMode(PIN_SR_OE, OUTPUT); // pull shift register OE high to disable output digitalWrite(PIN_SR_OE, HIGH); pinMode(PIN_SR_LATCH, OUTPUT); digitalWrite(PIN_SR_LATCH, HIGH); pinMode(PIN_SR_CLOCK, OUTPUT); #if defined(OSPI) pin_sr_data = PIN_SR_DATA; // detect RPi revision unsigned int rev = detect_rpi_rev(); if (rev==0x0002 || rev==0x0003) pin_sr_data = PIN_SR_DATA_ALT; // if this is revision 1, use PIN_SR_DATA_ALT pinMode(pin_sr_data, OUTPUT); #else pinMode(PIN_SR_DATA, OUTPUT); #endif // Reset all stations clear_all_station_bits(); apply_all_station_bits(); // pull shift register OE low to enable output digitalWrite(PIN_SR_OE, LOW); // Rain sensor port set up pinMode(PIN_RAINSENSOR, INPUT); #if defined(ARDUINO) digitalWrite(PIN_RAINSENSOR, HIGH); // enabled internal pullup on rain sensor #else // RPI and BBB have external pullups #endif // Default controller status variables // AVR assigns 0 to static variables by default // so only need to initialize non-zero ones status.enabled = 1; status.safe_reboot = 0; old_status = status; nvdata.sunrise_time = 360; // 6:00am default sunrise nvdata.sunset_time = 1080; // 6:00pm default sunset nboards = 1; nstations = 8; // set rf data pin pinMode(PIN_RF_DATA, OUTPUT); digitalWrite(PIN_RF_DATA, LOW); /*pinMode(PIN_RELAY, OUTPUT); digitalWrite(PIN_RELAY, LOW);*/ hw_type = HW_TYPE_AC; #if defined(ARDUINO) // AVR SD and LCD functions // Init I2C Wire.begin(); #if defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega1284__) // OS 2.3 specific detections uint8_t ret; // detect hardware type Wire.beginTransmission(MAC_CTRL_ID); Wire.write(0x00); ret = Wire.endTransmission(); if (!ret) { Wire.requestFrom(MAC_CTRL_ID, 1); while(!Wire.available()); ret = Wire.read(); if (ret == HW_TYPE_AC || ret == HW_TYPE_DC || ret == HW_TYPE_LATCH) { hw_type = ret; } else { // hardware type is not assigned } } if (hw_type == HW_TYPE_DC) { pinMode(PIN_BOOST, OUTPUT); digitalWrite(PIN_BOOST, LOW); pinMode(PIN_BOOST_EN, OUTPUT); digitalWrite(PIN_BOOST_EN, LOW); } #endif lcd_start(); // define lcd custom icons byte _icon[8]; // WiFi icon _icon[0] = B00000; _icon[1] = B10100; _icon[2] = B01000; _icon[3] = B10101; _icon[4] = B00001; _icon[5] = B00101; _icon[6] = B00101; _icon[7] = B10101; lcd.createChar(1, _icon); _icon[1]=0; _icon[2]=0; _icon[3]=1; lcd.createChar(0, _icon); // uSD card icon _icon[0] = B00000; _icon[1] = B00000; _icon[2] = B11111; _icon[3] = B10001; _icon[4] = B11111; _icon[5] = B10001; _icon[6] = B10011; _icon[7] = B11110; lcd.createChar(2, _icon); // Rain icon _icon[0] = B00000; _icon[1] = B00000; _icon[2] = B00110; _icon[3] = B01001; _icon[4] = B11111; _icon[5] = B00000; _icon[6] = B10101; _icon[7] = B10101; lcd.createChar(3, _icon); // Connect icon _icon[0] = B00000; _icon[1] = B00000; _icon[2] = B00111; _icon[3] = B00011; _icon[4] = B00101; _icon[5] = B01000; _icon[6] = B10000; _icon[7] = B00000; lcd.createChar(4, _icon); // set sd cs pin high to release SD pinMode(PIN_SD_CS, OUTPUT); digitalWrite(PIN_SD_CS, HIGH); if(sd.begin(PIN_SD_CS, SPI_HALF_SPEED)) { status.has_sd = 1; } // set button pins // enable internal pullup pinMode(PIN_BUTTON_1, INPUT); pinMode(PIN_BUTTON_2, INPUT); pinMode(PIN_BUTTON_3, INPUT); digitalWrite(PIN_BUTTON_1, HIGH); digitalWrite(PIN_BUTTON_2, HIGH); digitalWrite(PIN_BUTTON_3, HIGH); // detect if DS1307 RTC exists if (RTC.detect()==0) { status.has_rtc = 1; } #endif }
/* * Setup */ void setup() { wdt_enable(WDTO_8S); wdt_reset(); //Setup Ports Serial.begin(115200); //Start Debug Serial 0 Serial1.begin(9600); //Start GPS Serial 1 Serial2.begin(9600); pinMode(PIN_LED_GREEN, OUTPUT); //Blue GREEN pinMode(PIN_LED_RED, OUTPUT); //Blue RED pinMode(PIN_LED_BLUE, OUTPUT); //Blue LED pinMode(PIN_SPI_CS,OUTPUT); //Chip Select Pin for the SD Card pinMode(10, OUTPUT); //SDcard library expect 10 to set set as output. // Initialise the GPS wdt_disable(); gps.init(); gps.configureUbloxSettings(); // Configure Ublox for MY_HIGH altitude mode wdt_enable(WDTO_8S); // join I2C bus //start I2C transfer to the Module/Transmitter Wire.begin(); //Set up the two EasyTransfer methods ETI2Cout.begin(details(mD.i2cOut), &Wire); //setup the data structure to transfer out ETSerialIn.begin(details(vals), &Serial2); //Start up the LGgyro if (LGgyro.init()) { #ifdef DEBUG_ON Serial.println("LGgyro OK"); #endif LGgyro.enableDefault(); } else { #ifdef DEBUG_ON Serial.println("LGgyro not working"); #endif SET_LED_Status(SET_LED_WHITE,500); //White LED SET_LED_Status(SET_LED_RED,1000); //Red LED } //Start up the accelerometer accel = ADXL345(); // Create an instance of the accelerometer if(accel.EnsureConnected()) { // Check that the accelerometer is connected. #ifdef DEBUG_ON Serial.println("Connected to ADXL345."); #endif accel.SetRange(2, true); // Set the range of the accelerometer to a maximum of 2G. accel.EnableMeasurements(); // Tell the accelerometer to start taking measurements. } else{ #ifdef DEBUG_ON Serial.println("Could not connect to ADXL345."); #endif SET_LED_Status(SET_LED_WHITE,500); //White LED SET_LED_Status(SET_LED_RED,2000); //Red LED } //Start up the compass compass = HMC5883L(); // Construct a new HMC5883 compass. #ifdef DEBUG_ON if(compass.EnsureConnected() == 1) { Serial.println("Connected to HMC5883L."); } else { Serial.println("Not Connected to HMC5883L."); } #endif error = compass.SetScale(1.3); // Set the scale of the compass. #ifdef DEBUG_ON if(error != 0) { // If there is an error, print it out. Serial.println("Compass Error 1"); Serial.println(compass.GetErrorText(error)); } else { Serial.println("Compass Ok 1"); } #endif error = compass.SetMeasurementMode(Measurement_Continuous); // Set the measurement mode to Continuous #ifdef DEBUG_ON if(error != 0) { // If there is an error, print it out. Serial.println("Compass error 2"); Serial.println(compass.GetErrorText(error)); } else { Serial.println("Compass Ok 2"); } #endif //Start up the Pressure Sensor dps = BMP085(); dps.init(); #ifdef DEBUG_ON Serial.print("BMP Mode "); Serial.println(dps.getMode()); #endif wdt_reset(); // Start up the OneWire Sensors library and turn off blocking takes too long! sensors.begin(); sensors.setWaitForConversion(false); sensors.requestTemperaturesByAddress(outsideThermometer); // Send the command to get temperature //Initialise all of the record values mD.vals.tCount = 0; mD.vals.uslCount = 0; mD.vals.year = 0; mD.vals.month = 0; mD.vals.day = 0; mD.vals.hour = 0; mD.vals.minute = 0; mD.vals.second = 0; mD.vals.hundredths = 0; mD.vals.iLat = 0; mD.vals.iLong = 0; mD.vals.iAlt = 0; mD.vals.bSats = 0; mD.vals.iAngle = 0; mD.vals.iHspeed = 0; mD.vals.iVspeed = 0; mD.vals.age = 0; mD.vals.ihdop = 0; mD.vals.AcXPayload = 0; mD.vals.AcYPayload = 0; mD.vals.AcZPayload = 0; mD.vals.GyXPayload = 0; mD.vals.GyYPayload = 0; mD.vals.GyZPayload = 0; mD.vals.MgXPayload = 0; mD.vals.MgYPayload = 0; mD.vals.MgZPayload = 0; mD.vals.TmpPayload = 0; //Connect to the SD Card if(!SD.begin(PIN_SPI_CS, SPI_HALF_SPEED)) { #ifdef DEBUG_ON Serial.println("SD not working!!"); #endif SET_LED_Status(SET_LED_WHITE,500); //White LED SET_LED_Status(SET_LED_RED,3000); //Red LED } else { #ifdef DEBUG_ON Serial.println("SD OK"); #endif dataFile.open(SD_LOG_FILE, O_CREAT | O_WRITE | O_APPEND); //Open Logfile if (!dataFile.isOpen()) { #ifdef DEBUG_ON Serial.println("SD Data File Not Opened"); #endif SET_LED_Status(SET_LED_WHITE,500); SET_LED_Status(SET_LED_RED,3000); } } //Cycle lights SET_LED_Status(SET_LED_OFF,0); SET_LED_Status(SET_LED_RED,500); SET_LED_Status(SET_LED_GREEN,500); SET_LED_Status(SET_LED_BLUE,500); SET_LED_Status(SET_LED_OFF,0); elapseSIM900 = millis(); //Elapse counter for data to SIM900 elapseNTXB = millis(); //Elapse counter for data to NTXB NEWGPSDATA = false; wdt_enable(WDTO_2S); wdt_reset(); }
boolean MP3Player::Init(byte CS_uSD,byte ASD) { PLAY = false; isPlayAll = false; name.reserve(80); name = ""; vol = 0; counter=0; currentDir.reserve(20); currentDir = ""; _isPause = false; _isMute = false; ls_flag = false; CS_SD=CS_uSD; AMP=ASD; pinMode(CS,OUTPUT); //STA013CS digitalWrite(CS,LOW); //deactivate sta013 spi input pinMode(DATREQ,INPUT); //DATREQ pinMode(RESET,OUTPUT); //reset digitalWrite(RESET,HIGH); pinMode(AMP,INPUT); //shutdown the amp pinMode(CS_SD, OUTPUT); //SPI uSD CS InitI2C(); Reset_STA013(); #if DEBUG Serial.begin(9600); while (!Serial) ; // wait for serial port to connect. Needed for Leonardo only #endif if (!sd.begin(CS_SD, SPI_FULL_SPEED)) { #if DEBUG sd.initErrorHalt(); #endif return false; } SPI.begin(); boolean stat=Verify_STA013(); if (stat==false) { #if DEBUG Serial.println("STA013 not exist!"); #endif return false; } else { #if DEBUG Serial.println("STA013 verified.."); #endif if(!Setup_STA013()) { return false; } else { delayMicroseconds(100000); setVolume(220); // set volume by default in case user forgets to set it. AMPON(); return true; } } }
//------------------------------------------------------------------------------ void setup() { Serial.begin(9600); // Wait for USB Serial while (!Serial) { SysCall::yield(); } delay(1000); cout << F("Type any character to start\n"); // Wait for input line and discard. cin.readline(); cout << endl; // Initialize the SD card at SPI_HALF_SPEED to avoid bus errors with // breadboards. use SPI_FULL_SPEED for better performance. if (!sd.begin(SD_CHIP_SELECT, SPI_HALF_SPEED)) { sd.initErrorHalt(); } if (sd.exists("Folder1") || sd.exists("Folder1/file1.txt") || sd.exists("Folder1/File2.txt")) { error("Please remove existing Folder1, file1.txt, and File2.txt"); } int rootFileCount = 0; sd.vwd()->rewind(); while (file.openNext(sd.vwd(), O_READ)) { if (!file.isHidden()) { rootFileCount++; } file.close(); if (rootFileCount > 10) { error("Too many files in root. Please use an empty SD."); } } if (rootFileCount) { cout << F("\nPlease use an empty SD for best results.\n\n"); delay(1000); } // Create a new folder. if (!sd.mkdir("Folder1")) { error("Create Folder1 failed"); } cout << F("Created Folder1\n"); // Create a file in Folder1 using a path. if (!file.open("Folder1/file1.txt", O_CREAT | O_WRITE)) { error("create Folder1/file1.txt failed"); } file.close(); cout << F("Created Folder1/file1.txt\n"); // Change volume working directory to Folder1. if (!sd.chdir("Folder1")) { error("chdir failed for Folder1.\n"); } cout << F("chdir to Folder1\n"); // Create File2.txt in current directory. if (!file.open("File2.txt", O_CREAT | O_WRITE)) { error("create File2.txt failed"); } file.close(); cout << F("Created File2.txt in current directory\n"); cout << F("\nList of files on the SD.\n"); sd.ls("/", LS_R); // Remove files from current directory. if (!sd.remove("file1.txt") || !sd.remove("File2.txt")) { error("remove failed"); } cout << F("\nfile1.txt and File2.txt removed.\n"); // Change current directory to root. if (!sd.chdir()) { error("chdir to root failed.\n"); } cout << F("\nList of files on the SD.\n"); sd.ls(LS_R); // Remove Folder1. if (!sd.rmdir("Folder1")) { error("rmdir for Folder1 failed\n"); } cout << F("\nFolder1 removed.\n"); cout << F("\nList of files on the SD.\n"); sd.ls(LS_R); cout << F("Done!\n"); }