Please tell me how to write data to SD card with TinyArcade

Started by kuran_kuran, May 18, 2017, 09:12:04 AM

Previous topic - Next topic

kuran_kuran

***
Full Member
Posts: 21
Location: Tokyo, Japan
Logged
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);
}

Shdwwzrd

***
Full Member
Posts: 15
Logged
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.

kuran_kuran

***
Full Member
Posts: 21
Location: Tokyo, Japan
Logged
Data can not be saved on the SD card.
I am still in trouble.

Shdwwzrd

***
Full Member
Posts: 15
Logged
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.

kuran_kuran

***
Full Member
Posts: 21
Location: Tokyo, Japan
Logged
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);

SMF spam blocked by CleanTalk