TinyCircuits Forum

General Category => User Projects / Code Examples => Topic started by: kuran_kuran on May 18, 2017, 09:12:04 AM

Title: Please tell me how to write data to SD card with TinyArcade
Post by: kuran_kuran on May 18, 2017, 09:12:04 AM
Do you know how to write data to SD card with TinyArcade?
I want to save game data with SdFat library but I will fail by mistake.
If Sample.txt exists on the SD card, only 1st succeeds. 2nd will fail.
If Sample.txt does not exist, both 1st and 2nd will fail. zero byte Sample.txt is created.


#include <TinyScreen.h>
#include "SdFat.h"

TinyScreen tiny_screen = TinyScreen(TinyScreenPlus);
SdFat sd_fat;
int write_bytes1 = -1;
int write_bytes2 = -1;

void setup()
{
   // Initialize SD card
   sd_fat.begin(10, SPI_FULL_SPEED);
   // Initialize screen
   tiny_screen.begin();
   tiny_screen.setBitDepth(TSBitDepth8);
   tiny_screen.setBrightness( 8 );
   tiny_screen.setFont(liberationSansNarrow_12ptFontInfo);
   tiny_screen.fontColor(TS_8b_White, TS_8b_Black);
   // Write file 1st
   SdFile file1;
   if(file1.open("Sample.txt", O_RDWR | O_CREAT))
   {
      char text[128];
      sprintf(text, "123456 text");
      write_bytes1 = file1.write(text, 12);
      file1.close();
   }
   // Write file 2nd
   SdFile file2;
   if(file2.open("Sample.txt", O_RDWR | O_CREAT))
   {
      char text[128];
      sprintf(text, "654321 text");
      write_bytes2 = file2.write(text, 12);
      file2.close();
   }
}

void loop()
{
   char text[128];
   sprintf(text, "1st %dbytes", write_bytes1);
   tiny_screen.setCursor(0, 0);
   tiny_screen.print(text);
   sprintf(text, "2nd %dbytes", write_bytes2);
   tiny_screen.setCursor(0, 14);
   tiny_screen.print(text);
}
Title: Re: Please tell me how to write data to SD card with TinyArcade
Post by: Shdwwzrd on June 26, 2017, 07:32:10 AM
Hello Kuran,
Were you ever able to get save to sd card to work? I am having the same issues you are with writing to the sd card.
Title: Re: Please tell me how to write data to SD card with TinyArcade
Post by: kuran_kuran on June 28, 2017, 12:24:24 AM
Data can not be saved on the SD card.
I am still in trouble.
Title: Re: Please tell me how to write data to SD card with TinyArcade
Post by: Shdwwzrd on June 28, 2017, 10:14:14 AM
Got the code to work thanks to Ben Rose @ TinyCircuits.

I have attached a working example with the fixed SDfat library for the Tiny Arcade.
Title: Re: Please tell me how to write data to SD card with TinyArcade
Post by: kuran_kuran on June 29, 2017, 09:46:09 AM
I was able to write data to the SD card.
Thank you for giving me good advice.

*Program Changes
My sample

Added Line 3
#include <Wire.h>
#include <SPI.h>

Line 21
if(file1.open("Sample.txt", O_RDWR | O_CREAT))
   ↓
if(file1.open("Sample.txt", O_WRITE | O_CREAT | O_TRUNC))

Line 30
if(file2.open("Sample.txt", O_RDWR | O_CREAT))
   ↓
if(file2.open("Sample.txt", O_WRITE | O_CREAT | O_TRUNC))


src/SdSpiCard/SdSpiSAMD21.cpp

Line73 and 76
while(SERCOM1->SPI.INTFLAG.bit.DRE == 0);
   ↓
while(SERCOM1->SPI.INTFLAG.bit.DRE == 0 || SERCOM1->SPI.INTFLAG.bit.RXC == 0);