Exemple #1
0
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;
}
Exemple #2
0
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;
}
Exemple #3
0
void ScriviSd(void)
{
	int n;
	n = 0;
	if (!sd.init(SPI_HALF_SPEED, 10)) 
	{
		sd.initErrorHalt();
		return;
	}

	if (!myFile.open("test-1.txt", O_WRITE | O_CREAT | O_APPEND)) 
	{
		delay(800);
		digitalWrite(7, LOW);
		delay(200);
		digitalWrite(7, HIGH);
		delay(800);
		sd.errorHalt("opening test.txt for write failed");
		return;
	} 
//	Serial.println(nMin);

//	for(n = 0; n < nCount; n++)
//	{
//	myFile.print("TIME: ");
//	myFile.print(nMin);
//	myFile.print(" min. - VALUE    : ");
//	myFile.print(nValue[n]);
	myFile.println(" mTesla");
//	}
//	Serial.println("ScriviSd5");
//	nCount = 0;
//	nMin++;	

	myFile.close();
}
Exemple #4
0
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");
    }
}
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");
}