Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sdcard: add support for fatfs #265

Merged
merged 1 commit into from May 31, 2021
Merged

Conversation

sago35
Copy link
Member

@sago35 sago35 commented May 14, 2021

Added filesystem support.
The commit will be cleaned up later.

I'm checking with windows 10 + feather-m4-can + 8GB sdcard w/fat32.
It is readonly at this time.
To make it writable, you need to change the settings on tinygo-org/tinyfs .

SPI Configured. Reading flash info
==> ls
drwxrwxrwx     0 System Volume Information
drwxrwxrwx     0 bin
drwxrwxrwx     0 lib
drwxrwxrwx     0 pkg
drwxrwxrwx     0 src
drwxrwxrwx     0 targets
==> ls bin
-rwxrwxrwx 138400394 tinygo.exe
==> cat targets/feather-m4.json
00000000: 7b 0d 0a 20 20 20 20 22 69 6e 68 65 72 69 74 73    {..    "inherits
00000010: 22 3a 20 5b 22 61 74 73 61 6d 64 35 31 6a 31 39    ": ["atsamd51j19
00000020: 61 22 5d 2c 0d 0a 20 20 20 20 22 62 75 69 6c 64    a"],..    "build
00000030: 2d 74 61 67 73 22 3a 20 5b 22 66 65 61 74 68 65    -tags": ["feathe
00000040: 72 5f 6d 34 22 5d 2c 0d 0a 20 20 20 20 22 66 6c    r_m4"],..    "fl
00000050: 61 73 68 2d 31 32 30 30 2d 62 70 73 2d 72 65 73    ash-1200-bps-res
00000060: 65 74 22 3a 20 22 74 72 75 65 22 2c 0d 0a 20 20    et": "true",..
00000070: 20 20 22 66 6c 61 73 68 2d 6d 65 74 68 6f 64 22      "flash-method"
00000080: 3a 20 22 6d 73 64 22 2c 0d 0a 20 20 20 20 22 6d    : "msd",..    "m
00000090: 73 64 2d 76 6f 6c 75 6d 65 2d 6e 61 6d 65 22 3a    sd-volume-name":
000000a0: 20 22 46 45 41 54 48 45 52 42 4f 4f 54 22 2c 0d     "FEATHERBOOT",.
000000b0: 0a 20 20 20 20 22 6d 73 64 2d 66 69 72 6d 77 61    .    "msd-firmwa
000000c0: 72 65 2d 6e 61 6d 65 22 3a 20 22 66 69 72 6d 77    re-name": "firmw
000000d0: 61 72 65 2e 75 66 32 22 0d 0a 7d 0d 0a             are.uf2"..}..
==>

@sago35
Copy link
Member Author

sago35 commented May 14, 2021

The default stack size may not be enough for your current code.
If you set a larger value, such as 4096, it should work.

{
    "inherits": ["atsame51j19a"],
    "build-tags": ["feather_m4_can"],
    "flash-1200-bps-reset": "true",
    "flash-method": "msd",
    "msd-volume-name": "FTHRCANBOOT",
    "msd-firmware-name": "firmware.uf2",
    "default-stack-size": 4096
}

@sago35 sago35 force-pushed the sdcard-fs branch 2 times, most recently from db0e8b9 to cf463b7 Compare May 14, 2021 11:55
@sago35
Copy link
Member Author

sago35 commented May 14, 2021

In this PR, the following files have been modified.
The rest of the files are the same as in #167.

$ git diff HEAD~ --name-status
M       Makefile
A       examples/sdcard/tinyfs/console/console.go
A       examples/sdcard/tinyfs/feather-m4.go
A       examples/sdcard/tinyfs/m0.go
A       examples/sdcard/tinyfs/main.go
A       examples/sdcard/tinyfs/pyportal.go
A       examples/sdcard/tinyfs/wioterminal.go
M       go.mod
M       go.sum

@deadprogram
Copy link
Member

Hi @sago35 please rebase now that #167 has been merged. Thanks!

@deadprogram
Copy link
Member

Here is what results I got:

tinygo flash -target pyportal ./examples/sdcard/tinyfs/
SPI Configured. Reading flash info
==> 
==> ls
Could not open directory /: fatfs: (13) There is no valid FAT volume
==> mount
Could not mount LittleFS filesystem: fatfs: (1) A hard error occurred in the low level disk I/O layer

@sago35
Copy link
Member Author

sago35 commented May 23, 2021

@deadprogram
Make sure that the sdcard is formatted as FAT32.
ubuntu defaults to NTFS, but you will need to select FAT to format it.

If it's already FAT32, it needs to be analyzed a bit more.

$ sudo fdisk -l | grep sda1
/dev/sda1        2048 124735487 124733440 59.5G  c W95 FAT32 (LBA)
$ sudo fdisk /dev/sda1

Welcome to fdisk (util-linux 2.34).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): p
Disk /dev/sda1: 59.49 GiB, 63863521280 bytes, 124733440 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x00000000

@sago35
Copy link
Member Author

sago35 commented May 23, 2021

I've tried FAT32-format on windows 10 and ubuntu 20.04, and both are readable in my environment (pyportal) .

@deadprogram
Copy link
Member

Here is the SD card info:

/dev/mmcblk0p1       2048 30898175 30896128 14,8G  c W95 FAT32 (LBA)
Command (m for help): p
Disk /dev/mmcblk0p1: 14,8 GiB, 15818817536 bytes, 30896128 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x00000000

@sago35
Copy link
Member Author

sago35 commented May 24, 2021

@deadprogram
I created a version of the arduino sdcard example that works with PyPortal.
Please try it with go.mod written in the root of the sdcard.
I think it works.
And if it does, I think it needs to be modified in the tinygo source code.

// Arduino > Examples > SD > DumpFile
/*
  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 (for MKRZero SD: SDCARD_SS_PIN)

  created  22 December 2010
  by Limor Fried
  modified 9 Apr 2012
  by Tom Igoe

  This example code is in the public domain.

*/

#include <SPI.h>
#include <SD.h>

const int chipSelect = 32;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


  Serial.print("Initializing SD card...");

  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    while (1);
  }
  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("go.mod");

  // 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 go.mod");
  }
}

void loop() {
}

When I ran it, I got the following message.

Initializing SD card...card initialized.
module github.com/tinygo-org/tinygo

go 1.13

require (
	github.com/blakesmith/ar v0.0.0-20150311145944-8bd4349a67f2
	github.com/chromedp/cdproto v0.0.0-20210113043257-dabd2f2e7693
	github.com/chromedp/chromedp v0.6.4
	github.com/google/shlex v0.0.0-20181106134648-c34317bd91bf
	github.com/marcinbor85/gohex v0.0.0-20200531091804-343a4b548892
	github.com/mattn/go-colorable v0.1.8
	go.bug.st/serial v1.1.2
	golang.org/x/sys v0.0.0-20210113181707-4bcb84eeeb78
	golang.org/x/tools v0.0.0-20200216192241-b320d3a0f5a2
	tinygo.org/x/go-llvm v0.0.0-20210325115028-e7b85195e81c
)

image

@sago35
Copy link
Member Author

sago35 commented May 24, 2021

Does the xxd command work?
You can run it as follows

==> xxd 100 256
00000100: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
00000110: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
00000120: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
00000130: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
00000140: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
00000150: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
00000160: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
00000170: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
00000180: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
00000190: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
000001a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
000001b0: 00 00 00 00 00 00 00 00 c0 e8 2d db 00 00 00 04    ........先-朞...
000001c0: 01 04 0c fe c2 ff 00 08 00 00 00 48 6f 07 00 00    ...・.....Ho...
000001d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
000001e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
000001f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 55 aa    ..............U
=> xxd 1C0 16
000001c0: 01 04 0c fe c2 ff 00 08 00 00 00 48 6f 07 00 00    ...・.....Ho...

@sago35
Copy link
Member Author

sago35 commented May 24, 2021

Here is the SD card info:

I don't think sdcard has any problem.
So where is the difference between working and not working?
I'll do some more research.

@deadprogram
Copy link
Member

This is without your most recently suggested changes:

==> xxd 100 256
00000100: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
00000110: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
00000120: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
00000130: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
00000140: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
00000150: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
00000160: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
00000170: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
00000180: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
00000190: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
000001a0: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
000001b0: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
000001c0: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
000001d0: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
000001e0: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
000001f0: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
==> xxd 1C0 16
000001c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................

Did not fail, but the results look incorrect.

@sago35
Copy link
Member Author

sago35 commented May 24, 2021

@deadprogram
Flash the following UF2 to pyportal and run it.
When you run the ls command, it will run some debug prints.
This will give me a hint as to why I am getting errors.

https://drive.google.com/file/d/1YlI3q0JMf_uHPIzy3_zmZTZ9oa7GSQcp/view?usp=sharing

When I ran it, I got the following log.

SPI Configured. Reading flash info
==> ls
fmt : 0
ld_word(fs->win + BPB_BytsPerSec) : 512
SS(fs) : 512
fs->n_fats : 2
fs->cside : 64
fs->cside & (fs->csize - 1) : 0
fs->n_rootdir : 0
fs->n_rootdir % (SS(fs) / SZDIRE) : 0
nrsv : 40
tsect : 76F4800
sysect : 7740
nclst : 1DBB43
fmt : 3
ld_word(fs->win + BPB_FSVer32) : 0
fs->n_rootdir : 0
drwxrwxrwx     0 System Volume Information
-rwxrwxrwx   573 go.mod
==>

@deadprogram
Copy link
Member

Here is my output:

==> ls
fmt : 3
Could not open directory /: fatfs: (13) There is no valid FAT volume

@sago35
Copy link
Member Author

sago35 commented May 24, 2021

You got an error in the first place.
I added debug-print near the error.
Please check again.

https://drive.google.com/file/d/1tSXLwVgRhiF1ge5tnsgRDb79u0PRWYsD/view?usp=sharing

==> ls
2: fmt : 2
2: LD2PT(fs) : 0
2: LD2PT(fs) : 0
2: i     : 0
2: bsect : 800
2: fmt   : 0
fmt : 0
ld_word(fs->win + BPB_BytsPerSec) : 512
SS(fs) : 512
fs->n_fats : 2
fs->cside : 64
fs->cside & (fs->csize - 1) : 0
fs->n_rootdir : 0
fs->n_rootdir % (SS(fs) / SZDIRE) : 0
nrsv : 40
tsect : 76F4800
sysect : 7740
nclst : 1DBB43
fmt : 3
ld_word(fs->win + BPB_FSVer32) : 0
fs->n_rootdir : 0
drwxrwxrwx     0 System Volume Information
-rwxrwxrwx   573 go.mod

@deadprogram
Copy link
Member

Here is the output from the new version:

==> ls
2: fmt : 3
2: LD2PT(fs) : 0
fmt : 3
Could not open directory /: fatfs: (13) There is no valid FAT volume

@sago35
Copy link
Member Author

sago35 commented May 25, 2021

@deadprogram
Try to run xxd again.
It seems that the 2 bytes from 0x000001FE should be 55 aa.

==> xxd 1f0 16
000001f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 55 aa    ..............U

Then, please run the following in pyportal.
If you have Wio Terminal, could you also try the following?

==> ls
3: sector      : 0
3: fs->winsect : 4294967295
3: res        : 0
disk_read: 0 1
go_fatfs_disk_read(drv, bufptr, 0, 1)
bdev.ReadAt(buffer, 00000000)
disk_read ok
3: f->winsect : 0
2: fmt : 2
2: LD2PT(fs) : 0
2: LD2PT(fs) : 0
3: sector      : 2048
3: fs->winsect : 4294967295
3: res        : 0
disk_read: 2048 1
go_fatfs_disk_read(drv, bufptr, 2048, 1)
bdev.ReadAt(buffer, 00100000)
disk_read ok
3: f->winsect : 2048
2: i     : 0
2: bsect : 800
2: fmt   : 0
fmt : 0
ld_word(fs->win + BPB_BytsPerSec) : 512
SS(fs) : 512
fs->n_fats : 2
fs->cside : 64
fs->cside & (fs->csize - 1) : 0
fs->n_rootdir : 0
fs->n_rootdir % (SS(fs) / SZDIRE) : 0
nrsv : 40
tsect : 76F4800
sysect : 7740
nclst : 1DBB43
fmt : 3
ld_word(fs->win + BPB_FSVer32) : 0
fs->n_rootdir : 0
3: sector      : 32576
3: fs->winsect : 2048
3: res        : 0
disk_read: 32576 1
go_fatfs_disk_read(drv, bufptr, 32576, 1)
bdev.ReadAt(buffer, 00FE8000)
disk_read ok
3: f->winsect : 32576
3: sector      : 32576
3: fs->winsect : 32576
3: sector      : 32576
3: fs->winsect : 32576
3: sector      : 32576
3: fs->winsect : 32576
3: sector      : 32576
3: fs->winsect : 32576
3: sector      : 32576
3: fs->winsect : 32576
3: sector      : 32576
3: fs->winsect : 32576
3: sector      : 32576
3: fs->winsect : 32576
3: sector      : 32576
3: fs->winsect : 32576
drwxrwxrwx     0 System Volume Information
-rwxrwxrwx   573 go.mod

@deadprogram
Copy link
Member

Here is the result from running xxd with the "try_02" binary:

==> xxd 1f0 16
000001f0: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..

Here are the results of both command using "try_03" on the Pyportal:

SPI Configured. Reading flash info
==> 
==> xxd 1f0 16
000001f0: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
==> ls
3: sector      : 0
3: fs->winsect : 4294967295
3: res        : 0
disk_read: 0 1
go_fatfs_disk_read(drv, bufptr, 0, 1)
bdev.ReadAt(buffer, 00000000)
disk_read error
3: != RES_OK     
3: f->winsect : 4294967295
2: fmt : 4
2: LD2PT(fs) : 0
fmt : 4
Could not open directory /: fatfs: (1) A hard error occurred in the low level disk I/O layer

I will pull out my Wio Terminal in a little while to try with that device.

@deadprogram
Copy link
Member

Here are the results from trying the Wio Terminal:

Terminal ready
SPI Configured. Reading flash info
==> 
==> xxd 1f0 16
000001f0: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
==> ls
3: sector      : 0
3: fs->winsect : 4294967295
3: res        : 0
disk_read: 0 1
go_fatfs_disk_read(drv, bufptr, 0, 1)
bdev.ReadAt(buffer, 00000000)
disk_read error
3: != RES_OK     
3: f->winsect : 4294967295
2: fmt : 4
2: LD2PT(fs) : 0
fmt : 4
Could not open directory /: fatfs: (1) A hard error occurred in the low level disk I/O layer

@sago35
Copy link
Member Author

sago35 commented May 25, 2021

Looking at the results of PyPortal and Wio Terminal, I do not think that the disk is read properly.

Try the following command from linux.
The result is probably different from when you xxd from tinygo.
If the result is different, I think I need to modify drivers/sdcard.

$ sudo dd if=/dev/sda1 of=dump.sdcard bs=512 count=1
1+0 records in
1+0 records out
512 bytes copied, 0.00110696 s, 463 kB/s

$ xxd dump.sdcard 
00000000: eb58 906d 6b66 732e 6661 7400 0240 4000  .X.mkfs.fat..@@.
00000010: 0200 0000 00f8 0000 2000 4000 0008 0000  ........ .@.....
00000020: 0048 6f07 803b 0000 0000 0000 0200 0000  .Ho..;..........
00000030: 0100 0600 0000 0000 0000 0000 0000 0000  ................
00000040: 8001 2909 b31a 6620 2020 2020 2020 2020  ..)...f         
00000050: 2020 4641 5433 3220 2020 0e1f be77 7cac    FAT32   ...w|.
00000060: 22c0 740b 56b4 0ebb 0700 cd10 5eeb f032  ".t.V.......^..2
00000070: e4cd 16cd 19eb fe54 6869 7320 6973 206e  .......This is n
00000080: 6f74 2061 2062 6f6f 7461 626c 6520 6469  ot a bootable di
00000090: 736b 2e20 2050 6c65 6173 6520 696e 7365  sk.  Please inse
000000a0: 7274 2061 2062 6f6f 7461 626c 6520 666c  rt a bootable fl
000000b0: 6f70 7079 2061 6e64 0d0a 7072 6573 7320  oppy and..press 
000000c0: 616e 7920 6b65 7920 746f 2074 7279 2061  any key to try a
000000d0: 6761 696e 202e 2e2e 200d 0a00 0000 0000  gain ... .......
000000e0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000000f0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000100: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000110: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000120: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000130: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000140: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000150: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000160: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000170: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000180: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000190: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000001a0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000001b0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000001c0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000001d0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000001e0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000001f0: 0000 0000 0000 0000 0000 0000 0000 55aa  ..............U.

@deadprogram
Copy link
Member

Here is the output:

$ sudo dd if=/dev/mmcblk0 of=dump.sdcard bs=512 count=1
1+0 records in
1+0 records out
512 bytes copied, 0,0174105 s, 29,4 kB/s
$ xxd dump.sdcard
00000000: fab8 0010 8ed0 bc00 b0b8 0000 8ed8 8ec0  ................
00000010: fbbe 007c bf00 06b9 0002 f3a4 ea21 0600  ...|.........!..
00000020: 00be be07 3804 750b 83c6 1081 fefe 0775  ....8.u........u
00000030: f3eb 16b4 02b0 01bb 007c b280 8a74 018b  .........|...t..
00000040: 4c02 cd13 ea00 7c00 00eb fe00 0000 0000  L.....|.........
00000050: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000060: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000070: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000080: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000090: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000000a0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000000b0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000000c0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000000d0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000000e0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000000f0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000100: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000110: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000120: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000130: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000140: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000150: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000160: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000170: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000180: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000190: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000001a0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000001b0: 0000 0000 0000 0000 f4b0 0c00 0000 0020  ...............
000001c0: 2100 0bf4 d1a3 0008 0000 0070 df01 0000  !..........p....
000001d0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000001e0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000001f0: 0000 0000 0000 0000 0000 0000 0000 55aa  ..............U.

@sago35
Copy link
Member Author

sago35 commented May 26, 2021

@deadprogram
Can you please change the frequency of spi to 6MHz or a slower value and try the following command again.
If you are still reading data in the following form, it is not a tinyfs problem, but a drivers/sdcard problem.

==> xxd 100 256
00000100: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01 ..
00000110: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01 ..
00000120: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01 ..

https://github.com/tinygo-org/drivers/blob/dev/examples/sdcard/console/pyportal.go#L15

  • xxd 0 512
  • xxd 100000 512
  • ls

@deadprogram
Copy link
Member

At 6Mhz, the following:

==> xxd 0 512
00000000: fa b8 00 10 8e d0 9f 01 ff ff ff ff ff ff ff 01    .....
00000010: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
00000020: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
00000030: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
00000040: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
00000050: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
00000060: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
00000070: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
00000080: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
00000090: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
000000a0: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
000000b0: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
000000c0: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
000000d0: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
000000e0: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
000000f0: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
00000100: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
00000110: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
00000120: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
00000130: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
00000140: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
00000150: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
00000160: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
00000170: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
00000180: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
00000190: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
000001a0: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
000001b0: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
000001c0: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
000001d0: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
000001e0: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
000001f0: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
==> xxd 100000 512
00100000: eb 58 90 6d 6b 66 7f 01 ff ff ff ff ff ff ff 01    .mkf...
00100010: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
00100020: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
00100030: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
00100040: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
00100050: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
00100060: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
00100070: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
00100080: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
00100090: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
001000a0: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
001000b0: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
001000c0: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
001000d0: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
001000e0: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
001000f0: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
00100100: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
00100110: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
00100120: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
00100130: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
00100140: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
00100150: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
00100160: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
00100170: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
00100180: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
00100190: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
001001a0: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
001001b0: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
001001c0: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
001001d0: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
001001e0: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
001001f0: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..

ls locks up and never returns.

@sago35
Copy link
Member Author

sago35 commented May 26, 2021

@deadprogram
The first 6? bytes seem to be readable, so I think it is the BlockLength setting.
I sent CMD16 to explicitly set the BlockLength to 512.
I hope this works.

https://drive.google.com/file/d/1KtLNU39qjV4I9ZO4JTXvGnreh36De4M0/view?usp=sharing

Please tell me the following results.

  • xxd 0 512
  • ls
  • result of CSD.Dump() (The following will be displayed automatically)
CSD_STRUCTURE:      1
TAAC:               E
NSAC:               0
TRAN_SPEED:         32
CCC:                5B5
READ_BL_LEN:        9
READ_BL_PARTIAL:    0
WRITE_BLK_MISALIGN: 0
READ_BLK_MISALIGN:  0
DSR_IMP:            0
C_SIZE:             1DBD3
ERASE_BLK_EN:       1
SECTOR_SIZE:        7F
WP_GRP_SIZE:        0
WP_GRP_ENABLE:      0
R2W_FACTOR:         2
WRITE_BL_LEN:       9
WRITE_BL_PARTIAL:   0
FILE_FORMAT_GRP:    0
COPY:               1
PERM_WRITE_PROTECT: 0
TMP_WRITE_PROTECT:  0
FILE_FORMAT:        0
CRC:                6F

@deadprogram
Copy link
Member

Here is the output from the latest test version:

Terminal ready
CSD_STRUCTURE:      1
TAAC:               E
NSAC:               0
TRAN_SPEED:         32
CCC:                5B5
READ_BL_LEN:        9
READ_BL_PARTIAL:    0
WRITE_BLK_MISALIGN: 0
READ_BLK_MISALIGN:  0
DSR_IMP:            0
C_SIZE:             77DD
ERASE_BLK_EN:       1
SECTOR_SIZE:        7F
WP_GRP_SIZE:        0
WP_GRP_ENABLE:      0
R2W_FACTOR:         2
WRITE_BL_LEN:       9
WRITE_BL_PARTIAL:   0
FILE_FORMAT_GRP:    0
COPY:               0
PERM_WRITE_PROTECT: 0
TMP_WRITE_PROTECT:  0
FILE_FORMAT:        0
CRC:                20
SPI Configured. Reading flash info
==>
==> xxd 0 512
00000000: fa b8 00 10 8e d0 9f 01 ff ff ff ff ff ff ff 01    .....
00000010: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
00000020: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
00000030: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
00000040: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
00000050: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
00000060: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
00000070: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
00000080: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
00000090: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
000000a0: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
000000b0: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
000000c0: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
000000d0: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
000000e0: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
000000f0: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
00000100: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
00000110: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
00000120: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
00000130: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
00000140: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
00000150: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
00000160: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
00000170: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
00000180: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
00000190: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
000001a0: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
000001b0: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
000001c0: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
000001d0: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
000001e0: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
000001f0: ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff 01    ..
==> ls
3: sector      : 0
3: fs->winsect : 4294967295
3: res        : 0
disk_read: 0 1
go_fatfs_disk_read(drv, bufptr, 0, 1)
bdev.ReadAt(buffer[:512], 00000000)
disk_read error
3: != RES_OK
3: f->winsect : 4294967295
2: fmt : 4
2: LD2PT(fs) : 0
fmt : 4

@sago35
Copy link
Member Author

sago35 commented May 27, 2021

Thank you for trying so many times!

I created a uf2 that reads the sdcard of PyPortal from Arduino.
If there is a go.mod in the root directory, it outputs serially as follows.
If the read succeeds with this uf2, we can refer to the arduino SPI communication.

https://drive.google.com/file/d/1dLiu4y0PjtemtdpjJksX1f8uXDplj06h/view?usp=sharing

Initializing SD card...card initialized.
module github.com/tinygo-org/tinygo

go 1.13

require (
	github.com/blakesmith/ar v0.0.0-20150311145944-8bd4349a67f2
	github.com/chromedp/cdproto v0.0.0-20210113043257-dabd2f2e7693
	github.com/chromedp/chromedp v0.6.4
	github.com/google/shlex v0.0.0-20181106134648-c34317bd91bf
	github.com/marcinbor85/gohex v0.0.0-20200531091804-343a4b548892
	github.com/mattn/go-colorable v0.1.8
	go.bug.st/serial v1.1.2
	golang.org/x/sys v0.0.0-20210113181707-4bcb84eeeb78
	golang.org/x/tools v0.0.0-20200216192241-b320d3a0f5a2
	tinygo.org/x/go-llvm v0.0.0-20210325115028-e7b85195e81c
)

@deadprogram
Copy link
Member

OK, that worked:

Initializing SD card...card initialized.
module github.com/tinygo-org/tinygo

                                   go 1.13

                                          require (
                                                        github.com/blakesmith/ar v0.0.0-20150311145944-8bd4349a67f2
                                                                                                                        github.com/chromedp/cdproto v0.0.0-20210113043257-dabd2f2e7693
                                        github.com/chromedp/chromedp v0.6.4
                                                                                github.com/google/shlex v0.0.0-20181106134648-c34317bd91bf
                                                                                                                                                gi
thub.com/marcinbor85/gohex v0.0.0-20200531091804-343a4b548892
                                                                github.com/mattn/go-colorable v0.1.8
                                                                                                        go.bug.st/serial v1.1.2
                                                                                                                                golang.org/x/sys v0.0.0-20210113181707-4bcb84eeeb78
                                        golang.org/x/tools v0.0.0-20200216192241-b320d3a0f5a2
                                                                                                tinygo.org/x/go-llvm v0.0.0-20210325115028-e7b8519
5e81c
     )

@sago35
Copy link
Member Author

sago35 commented May 27, 2021

@deadprogram
Most of the SPI log were the same, only the CRC part was different.
I thought the CRC was ignored in SPI mode, but maybe not.
So I created a new UF2 with the CRC changed to 0xFF.
It is made by tinygo.
I'll be happy if it works.

arduino's init

cmd(CMD0, 00, 00, 00, 00, 95);
cmd(CMD8, 00, 00, 01, AA, 87);
cmd(CMD55, 00, 00, 00, 00, FF);
cmd(CMD41, 40, 00, 00, 00, FF);
cmd(CMD55, 00, 00, 00, 00, FF);
cmd(CMD41, 40, 00, 00, 00, FF);
cmd(CMD55, 00, 00, 00, 00, FF);
cmd(CMD41, 40, 00, 00, 00, FF);
cmd(CMD55, 00, 00, 00, 00, FF);
cmd(CMD41, 40, 00, 00, 00, FF);
cmd(CMD55, 00, 00, 00, 00, FF);
cmd(CMD41, 40, 00, 00, 00, FF);
cmd(CMD55, 00, 00, 00, 00, FF);
cmd(CMD41, 40, 00, 00, 00, FF);
cmd(CMD55, 00, 00, 00, 00, FF);
cmd(CMD41, 40, 00, 00, 00, FF);
cmd(CMD55, 00, 00, 00, 00, FF);
cmd(CMD41, 40, 00, 00, 00, FF);
cmd(CMD55, 00, 00, 00, 00, FF);
cmd(CMD41, 40, 00, 00, 00, FF);
cmd(CMD55, 00, 00, 00, 00, FF);
cmd(CMD41, 40, 00, 00, 00, FF);
cmd(CMD55, 00, 00, 00, 00, FF);
cmd(CMD41, 40, 00, 00, 00, FF);
cmd(CMD55, 00, 00, 00, 00, FF);
cmd(CMD41, 40, 00, 00, 00, FF);
cmd(CMD55, 00, 00, 00, 00, FF);
cmd(CMD41, 40, 00, 00, 00, FF);
cmd(CMD55, 00, 00, 00, 00, FF);
cmd(CMD41, 40, 00, 00, 00, FF);
cmd(CMD55, 00, 00, 00, 00, FF);
cmd(CMD41, 40, 00, 00, 00, FF);
cmd(CMD58, 00, 00, 00, 00, FF);

tinygo's init:

cmd(CMD0, 00, 00, 00, 00, 95);
cmd(CMD8, 00, 00, 01, AA, 87);
cmd(CMD55, 00, 00, 00, 00, 00);
cmd(CMD41, 40, 00, 00, 00, 00);
cmd(CMD55, 00, 00, 00, 00, 00);
cmd(CMD41, 40, 00, 00, 00, 00);
cmd(CMD55, 00, 00, 00, 00, 00);
cmd(CMD41, 40, 00, 00, 00, 00);
cmd(CMD55, 00, 00, 00, 00, 00);
cmd(CMD41, 40, 00, 00, 00, 00);
cmd(CMD55, 00, 00, 00, 00, 00);
cmd(CMD41, 40, 00, 00, 00, 00);
cmd(CMD55, 00, 00, 00, 00, 00);
cmd(CMD41, 40, 00, 00, 00, 00);
cmd(CMD55, 00, 00, 00, 00, 00);
cmd(CMD41, 40, 00, 00, 00, 00);
cmd(CMD55, 00, 00, 00, 00, 00);
cmd(CMD41, 40, 00, 00, 00, 00);
cmd(CMD55, 00, 00, 00, 00, 00);
cmd(CMD41, 40, 00, 00, 00, 00);
cmd(CMD55, 00, 00, 00, 00, 00);
cmd(CMD41, 40, 00, 00, 00, 00);
cmd(CMD55, 00, 00, 00, 00, 00);
cmd(CMD41, 40, 00, 00, 00, 00);
cmd(CMD55, 00, 00, 00, 00, 00);
cmd(CMD41, 40, 00, 00, 00, 00);
cmd(CMD55, 00, 00, 00, 00, 00);
cmd(CMD41, 40, 00, 00, 00, 00);
cmd(CMD58, 00, 00, 00, 00, 00);
cmd(CMD16, 00, 00, 02, 00, 00);
cmd(CMD10, 00, 00, 00, 00, 00);
cmd(CMD9, 00, 00, 00, 00, 00);

@sago35
Copy link
Member Author

sago35 commented May 27, 2021

If the above try06 doesn't work, please try this one as well.
The following is the arduino process with the addition of debug-print.
From the log displayed, check the SPI communication to be executed.

https://drive.google.com/file/d/1Z-1KHrAIXsDqAolD36ujzHxEi1rzhjAq/view?usp=sharing

@deadprogram
Copy link
Member

Output from the first program:

Terminal ready                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
cmd(CMD0, 00, 00, 00, 00, 95);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
cmd(CMD0, 00, 00, 00, 00, 95);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
cmd(CMD8, 00, 00, 01, AA, 87);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
cmd(CMD55, 00, 00, 00, 00, FF);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
cmd(CMD41, 40, 00, 00, 00, FF);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
cmd(CMD55, 00, 00, 00, 00, FF);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
cmd(CMD41, 40, 00, 00, 00, FF);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
cmd(CMD55, 00, 00, 00, 00, FF);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
cmd(CMD41, 40, 00, 00, 00, FF);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
cmd(CMD55, 00, 00, 00, 00, FF);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
cmd(CMD41, 40, 00, 00, 00, FF);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
cmd(CMD55, 00, 00, 00, 00, FF);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
cmd(CMD41, 40, 00, 00, 00, FF);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
cmd(CMD55, 00, 00, 00, 00, FF);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
cmd(CMD41, 40, 00, 00, 00, FF);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
cmd(CMD55, 00, 00, 00, 00, FF);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
cmd(CMD41, 40, 00, 00, 00, FF);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
cmd(CMD55, 00, 00, 00, 00, FF);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
cmd(CMD41, 40, 00, 00, 00, FF);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
cmd(CMD55, 00, 00, 00, 00, FF);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
cmd(CMD41, 40, 00, 00, 00, FF);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
cmd(CMD55, 00, 00, 00, 00, FF);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
cmd(CMD41, 40, 00, 00, 00, FF);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
cmd(CMD55, 00, 00, 00, 00, FF);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
cmd(CMD41, 40, 00, 00, 00, FF);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
cmd(CMD55, 00, 00, 00, 00, FF);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
cmd(CMD41, 40, 00, 00, 00, FF);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
cmd(CMD55, 00, 00, 00, 00, FF);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
cmd(CMD41, 40, 00, 00, 00, FF);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
cmd(CMD55, 00, 00, 00, 00, FF);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
cmd(CMD41, 40, 00, 00, 00, FF);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
cmd(CMD55, 00, 00, 00, 00, FF);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
cmd(CMD41, 40, 00, 00, 00, FF);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
cmd(CMD55, 00, 00, 00, 00, FF);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
cmd(CMD41, 40, 00, 00, 00, FF);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
cmd(CMD55, 00, 00, 00, 00, FF);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
cmd(CMD41, 40, 00, 00, 00, FF);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
cmd(CMD55, 00, 00, 00, 00, FF);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
cmd(CMD41, 40, 00, 00, 00, FF);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
cmd(CMD55, 00, 00, 00, 00, FF);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
cmd(CMD41, 40, 00, 00, 00, FF);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
cmd(CMD55, 00, 00, 00, 00, FF);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
cmd(CMD41, 40, 00, 00, 00, FF);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
cmd(CMD55, 00, 00, 00, 00, FF);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
cmd(CMD41, 40, 00, 00, 00, FF);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
cmd(CMD55, 00, 00, 00, 00, FF);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
cmd(CMD41, 40, 00, 00, 00, FF);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
cmd(CMD55, 00, 00, 00, 00, FF);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
cmd(CMD41, 40, 00, 00, 00, FF);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
cmd(CMD55, 00, 00, 00, 00, FF);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
cmd(CMD41, 40, 00, 00, 00, FF);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
cmd(CMD55, 00, 00, 00, 00, FF);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
cmd(CMD41, 40, 00, 00, 00, FF);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
cmd(CMD55, 00, 00, 00, 00, FF);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
cmd(CMD41, 40, 00, 00, 00, FF);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
cmd(CMD55, 00, 00, 00, 00, FF);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
cmd(CMD41, 40, 00, 00, 00, FF);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
cmd(CMD55, 00, 00, 00, 00, FF);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
cmd(CMD41, 40, 00, 00, 00, FF);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
cmd(CMD55, 00, 00, 00, 00, FF);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
cmd(CMD41, 40, 00, 00, 00, FF);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
cmd(CMD55, 00, 00, 00, 00, FF);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
cmd(CMD41, 40, 00, 00, 00, FF);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
cmd(CMD55, 00, 00, 00, 00, FF);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
cmd(CMD41, 40, 00, 00, 00, FF);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
cmd(CMD55, 00, 00, 00, 00, FF);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
cmd(CMD41, 40, 00, 00, 00, FF);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
cmd(CMD55, 00, 00, 00, 00, FF);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
cmd(CMD41, 40, 00, 00, 00, FF);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
cmd(CMD55, 00, 00, 00, 00, FF);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
cmd(CMD41, 40, 00, 00, 00, FF);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
cmd(CMD55, 00, 00, 00, 00, FF);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
cmd(CMD41, 40, 00, 00, 00, FF);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
cmd(CMD55, 00, 00, 00, 00, FF);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
cmd(CMD41, 40, 00, 00, 00, FF);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
cmd(CMD55, 00, 00, 00, 00, FF);
...
CSD_STRUCTURE:      1
TAAC:               E
NSAC:               0
TRAN_SPEED:         32
CCC:                5B5
READ_BL_LEN:        9
READ_BL_PARTIAL:    0
WRITE_BLK_MISALIGN: 0
READ_BLK_MISALIGN:  0
DSR_IMP:            0
C_SIZE:             77DD
ERASE_BLK_EN:       1
SECTOR_SIZE:        7F
WP_GRP_SIZE:        0
WP_GRP_ENABLE:      0
R2W_FACTOR:         2
WRITE_BL_LEN:       9
WRITE_BL_PARTIAL:   0
FILE_FORMAT_GRP:    0
COPY:               0
PERM_WRITE_PROTECT: 0
TMP_WRITE_PROTECT:  0
FILE_FORMAT:        0
CRC:                20
SPI Configured. Reading flash info
==>
==> ls
3: sector      : 0
3: fs->winsect : 4294967295
3: res        : 0
disk_read: 0 1
go_fatfs_disk_read(drv, bufptr, 0, 1)
bdev.ReadAt(buffer[:512], 00000000)
cmd(CMD17, 00, 00, 00, 00, FF);
disk_read ok
3: f->winsect : 0
2: fmt : 3
2: LD2PT(fs) : 0
fmt : 3
Could not open directory /: fatfs: (13) There is no valid FAT volume
==>
FATAL: term closed

Output from the second:

Terminal ready
Initializing SD card...cmd(CMD0, 00, 00, 00, 00, 95);
cmd(CMD8, 00, 00, 01, AA, 87);
cmd(CMD55, 00, 00, 00, 00, FF);
cmd(CMD41, 40, 00, 00, 00, FF);
cmd(CMD55, 00, 00, 00, 00, FF);
cmd(CMD41, 40, 00, 00, 00, FF);
cmd(CMD58, 00, 00, 00, 00, FF);
cmd(CMD17, 00, 00, 00, 00, FF);
cmd(CMD17, 00, 00, 08, 00, FF);
cmd(CMD17, 00, 00, 08, 20, FF);
card initialized.
cmd(CMD17, 00, 00, 7F, E0, FF);
cmd(CMD17, 00, 00, 80, 00, FF);
module github.com/tinygo-org/tinygo

                                   go 1.13

                                          require (
                                                        github.com/blakesmith/ar v0.0.0-20150311145944-8bd4349a67f2
                                                                                                                        github.com/chromedp/cdproto v0.0.0-20210113043257-dabd2f2e7693
                                        github.com/chromedp/chromedp v0.6.4
                                                                                github.com/google/shlex v0.0.0-20181106134648-c34317bd91bf
                                                                                                                                                github.com/marcinbor85/gohex v0.0.0-20200531091804-343a4b548892
                                                                github.com/mattn/go-colorable v0.1.8
                                                                                                        go.bug.st/serial v1.1.2
                                                                                                                                golang.org/x/sys v0.0.0-20210113181707-4bcb84eeeb78
                                        golang.org/x/tools v0.0.0-20200216192241-b320d3a0f5acmd(CMD17, 00, 00, 80, 01, FF);
2
        tinygo.org/x/go-llvm v0.0.0-20210325115028-e7b85195e81c
                                                               )

@sago35
Copy link
Member Author

sago35 commented May 29, 2021

@deadprogram
I tried to match the waveform of the arduino as much as possible.
The main changes are as follows

  • Change the first frequency to 400khz or lower.
  • Change the frequency to 4mhz after initialization
  • Added CS port operation when changing frequency.
  • Speed up waiting for status response.

Try the following.

@deadprogram
Copy link
Member

Here is the output from the last version:

Terminal ready                                                                                                                                                               
cmd(CMD0, 00, 00, 00, 00, 95);                                                                                                                                               
cmd(CMD0, 00, 00, 00, 00, 95);                                                                                                                                               
cmd(CMD8, 00, 00, 01, AA, 87);                                                                                                                                               
cmd(CMD55, 00, 00, 00, 00, FF);                                                                                                                                              
cmd(CMD41, 40, 00, 00, 00, FF);                                                                                                                                              
cmd(CMD55, 00, 00, 00, 00, FF);                                                                                                                                              
cmd(CMD41, 40, 00, 00, 00, FF);                                                                                                                                              
cmd(CMD55, 00, 00, 00, 00, FF);                                                                                                                                              
cmd(CMD41, 40, 00, 00, 00, FF);                                                                                                                                              
cmd(CMD55, 00, 00, 00, 00, FF);                                                                                                                                              
cmd(CMD41, 40, 00, 00, 00, FF);                                                                                                                                              
cmd(CMD55, 00, 00, 00, 00, FF);                                                                                                                                              
cmd(CMD41, 40, 00, 00, 00, FF);                                                                                                                                              
cmd(CMD55, 00, 00, 00, 00, FF);                                                                                                                                              
cmd(CMD41, 40, 00, 00, 00, FF);                                                                                                                                              
cmd(CMD55, 00, 00, 00, 00, FF);                                                                                                                                              
cmd(CMD41, 40, 00, 00, 00, FF);                                                                                                                                              
cmd(CMD55, 00, 00, 00, 00, FF);                                                                                                                                              
cmd(CMD41, 40, 00, 00, 00, FF);                                                                                                                                              
cmd(CMD55, 00, 00, 00, 00, FF);                                                                                                                                              
cmd(CMD41, 40, 00, 00, 00, FF);
...
SPI Configured. Reading flash info
3: sector      : 0
3: fs->winsect : 4294967295
3: res        : 0
disk_read: 0 1
go_fatfs_disk_read(drv, bufptr, 0, 1)
bdev.ReadAt(buffer[:512], 00000000)
cmd(CMD17, 00, 00, 00, 00, FF);
disk_read ok
3: f->winsect : 0
2: fmt : 3
2: LD2PT(fs) : 0
fmt : 3
Could not open: fatfs: (13) There is no valid FAT volume
==> ls
3: sector      : 0
3: fs->winsect : 4294967295
3: res        : 0
disk_read: 0 1
go_fatfs_disk_read(drv, bufptr, 0, 1)
bdev.ReadAt(buffer[:512], 00000000)
cmd(CMD17, 00, 00, 00, 00, FF);
disk_read error
3: != RES_OK
3: f->winsect : 4294967295
2: fmt : 4
2: LD2PT(fs) : 0
fmt : 4
Could not open directory /: fatfs: (1) A hard error occurred in the low level disk I/O layer

@sago35
Copy link
Member Author

sago35 commented May 29, 2021

The error has changed a bit.
Please also give me the results of the following command.

  • xxd 0 512

@deadprogram
Copy link
Member

==> xxd 0 512
cmd(CMD17, 00, 00, 00, 00, FF);
00000000: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
00000010: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
00000020: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
00000030: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
00000040: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
00000050: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
00000060: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
00000070: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
00000080: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
00000090: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
000000a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
000000b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
000000c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
000000d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
000000e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
000000f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
00000100: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
00000110: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
00000120: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
00000130: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
00000140: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
00000150: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
00000160: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
00000170: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
00000180: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
00000190: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
000001a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
000001b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
000001c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
000001d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
000001e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
000001f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................

@sago35
Copy link
Member Author

sago35 commented May 30, 2021

@deadprogram
I made some more improvements.
Please try it.

@deadprogram
Copy link
Member

@sago35 sago35 closed this May 30, 2021
@sago35 sago35 deleted the sdcard-fs branch May 30, 2021 23:15
@sago35 sago35 reopened this May 30, 2021
@sago35
Copy link
Member Author

sago35 commented May 30, 2021

@deadprogram
The sdcard-fs branch has been updated.
Please try to see if it works.
If it works fine, I'll create a squash commit.

I have tried it in the following environment.

Note that the stack-size needs to be adjusted at this time.

@sago35
Copy link
Member Author

sago35 commented May 30, 2021

I have confirmed that it also works with feather-nrf52840 and feather-stm32f405 .
Adding this board requires a few changes, so I'll deal with that separately from this PR.

@deadprogram
Copy link
Member

Is there any reason to not want to set "default-stack-size": 4096 for all these processors? Seems like it is a benign change that we probably need anyhow.

@deadprogram
Copy link
Member

Confirm that is still working on my PyPortal with latest code in this branch, and the stack size change. 🎊

@sago35
Copy link
Member Author

sago35 commented May 31, 2021

Is there any reason to not want to set "default-stack-size": 4096 for all these processors? Seems like it is a benign change that we probably need anyhow.

I don't have a clear reason.
However, stacksize should be carefully configured as shown in the following PR
I'm a little worried about setting it to a value as large as 4096 bytes.

tinygo-org/tinygo#1253

@deadprogram
Copy link
Member

Can we change it to a lower number?

Perhaps we should change the default only for all of the boards that we already know have built-in SD card readers.

@sago35
Copy link
Member Author

sago35 commented May 31, 2021

Can we change it to a lower number?

I tried it with wioterminal.
The minimum value in examples/sdcard/tinyfs is now 1664 bytes.
There is no evidence to support this, but I suggest 2048 bytes to give yourself some leeway.

Perhaps we should change the default only for all of the boards that we already know have built-in SD card readers.

Here is the board that comes with the sdcard.
I will create a PR later that sets default-stack-size to 2048 for atsamd21, atsamd51.

  • atsamd21
    • ProductivityOpen P1AM-100
  • atsamd51
    • Adafruit Grand Central M4
    • Adafruit PyGamer
    • Adafruit PyPortal
    • Seeed Wio Terminal
  • others
    • Adafruit feather-stm32f405 (connected to the SD/MMC Host Controller)
    • PJRC Teensy 3.6 (SPI is not supported)
    • Sipeed MAix BiT (SPI is supported, but build failed)
    • Microchip SAM E54 Xplained Pro (connected to the SD/MMC Host Controller (SDHC))

Here is the build log for maixbit

$ tinygo build -o /tmp/a.hex --target maixbit --size short  ./examples/sdcard/tinyfs/
C:\Users\sago3\dev\pkg\mod\github.com\tinygo-org\tinyfs@v0.0.0-20210514090915-924e60a7bcf8\fatfs\go_fatfs_callbacks.go:55: blocking operation in exported function: go_fatfs_disk_ioctl

traceback:
go_fatfs_disk_ioctl
        C:\Users\sago3\dev\pkg\mod\github.com\tinygo-org\tinyfs@v0.0.0-20210514090915-924e60a7bcf8\fatfs\go_fatfs_callbacks.go:57:17
github.com/tinygo-org/tinyfs/fatfs.restore
        C:\Users\sago3\dev\pkg\mod\github.com\tinygo-org\tinyfs@v0.0.0-20210514090915-924e60a7bcf8\fatfs\go_fatfs_callbacks.go:101:26
github.com/tinygo-org/tinyfs/internal/gopointer.Restore
        C:\Users\sago3\dev\pkg\mod\github.com\tinygo-org\tinyfs@v0.0.0-20210514090915-924e60a7bcf8\internal\gopointer\pointer.go:41:12
(*sync.Mutex).Lock
        C:\Program Files\Go\src\sync\mutex.go:23:13

@deadprogram
Copy link
Member

That all sounds great... except about the Maixbit of course. Maixbit is something we should take on in a separate task from all the other boards you mentioned.

This is very exciting!

@sago35
Copy link
Member Author

sago35 commented May 31, 2021

@deadprogram
Updated.
Please review.
If everything looks good, I'll create a squash commit.

@sago35
Copy link
Member Author

sago35 commented May 31, 2021

feather-m4.go and m0.go are kept for the following.

  • feather-m4 w/ Adafruit Adalogger FeatherWing
  • feather-m4-can w/ Adafruit Adalogger FeatherWing
  • feather-nrf52840 w/ Adafruit Adalogger FeatherWing
  • xiao w/ Seeeduino XIAO Expansion board
  • qtpy w/ Seeeduino XIAO Expansion board

@deadprogram
Copy link
Member

@sago35 I think you have done some incredible work here. Any further enhancements/changes would probably be more easily handled in additional PRs later. I suggest you squash so we can merge this, and then we can continue from there as far as documentation etc.

Only other thing still to handle is a PR to the main repo with the stack size changes we have been discussing, so that it can "just work" for anyone who has one of those boards.

@sago35
Copy link
Member Author

sago35 commented May 31, 2021

@deadprogram
Rebased.
Thank you for going along with the tests again and again.

@deadprogram
Copy link
Member

Now merging!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants