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

Add SPI sdcard driver #167

Merged
merged 1 commit into from
May 23, 2021
Merged

Add SPI sdcard driver #167

merged 1 commit into from
May 23, 2021

Conversation

sago35
Copy link
Member

@sago35 sago35 commented Jul 9, 2020

#155

TODO

  • non-aligned reads
  • non-aligned writes

@sago35
Copy link
Member Author

sago35 commented Jul 9, 2020

It works at the moment, but there are still a lot of changes to come.
If you want to try it out, please use the following

  • examples/sdcard/console

@sago35
Copy link
Member Author

sago35 commented Jul 9, 2020

You can mount a FAT32-formatted sdcard by using the following

https://github.com/sago35/tinyfs/tree/sdcard

If you get the following message at runtime, you can work around it by temporarily increasing the stack size.
How to solve this part of the problem is not yet determined.

panic: runtime error: goroutine stack overflow

The stack size can be changed from
I set it to 2048 bytes and it seems to work correctly.

const stackSize = 1024

https://github.com/tinygo-org/tinygo/blob/master/src/internal/task/task_stack_cortexm.go#L7

image

@sago35
Copy link
Member Author

sago35 commented May 12, 2021

The source code has been cleaned up to minimize the implementation.
I'm sure there are some environments where it won't work, but I'd like to make minor corrections in a separate PR each time.
I think it is now ready for review.

It has only been tested on SDHC 8GB and SDXC 64GB sdcards.
I used feather-m4-can for testing.

examples/sdcard/console:

sdcard console
==> help
help
dbg
erase
lsblk
write <hex offset> <bytes>
xxd <start address> <length>
==> xxd 1b0 40
000001b0: 00 00 00 00 00 00 00 00 f3 d0 84 0d 00 00 00 82    ........麸......
000001c0: 03 00 0b fe ff cb 00 20 00 00 cc 24 ee 00 00 00    .....邦 ..未隨..
000001d0: 00 00 00 00 00 00 00 00    ................
==> lsblk

-------------------------------------
 Device Information:
-------------------------------------
 JEDEC ID: attrs.JedecID
 Serial:   77466253
 Status 1: 00
 Status 2: 00

 Max clock speed (MHz): 50
 Has Sector Protection: false
 Supports Fast Reads:   false
 Supports QSPI Reads:   false
 Supports QSPI Write:   false
 Write Status Split:    false
 Single Status Byte:    false
-Sectors:               15630336
-Bytes (Sectors * 512)  8002207744
-ManufacturerID         A5
-OEMApplicationID       A544
-ProductName            00000
-ProductVersion         2.0
-ProductSerialNumber    049E0A8D
-ManufacturingYear      05
-ManufacturingMonth     02
-Always1                1
-CRC                    0B
-------------------------------------

@sago35 sago35 marked this pull request as ready for review May 12, 2021 00:44
@sago35
Copy link
Member Author

sago35 commented May 12, 2021

In addition, the current sdcard driver successfully reads FAT32 using bgould/tinyfs.
(This PR does not include an example using tinyfs)

However, bgould/tinyfs needs a little patch and I would like to fork to tinygo-org/tinyfs if possible.
What do you think? @deadprogram @bgould

==> 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"..}..

@deadprogram
Copy link
Member

Agreed, @sago35

I've updated https://github.com/tinygo-org/tinyfs to the latest WIP and also created release and dev branches in the fork.

@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
Copy link
Member Author

sago35 commented May 14, 2021

It is now ready for review.
I think there are still some sdcards that can't be read, and some error checking is still missing.
I'll try to improve this in another PR in the future.

@deadprogram
Copy link
Member

deadprogram commented May 23, 2021

Tested with default values using Go1.16.3 and the latest TinyGo dev version, using 16GB SDHC card.

sdcard console
==> lsblk

-------------------------------------
 Device Information:
-------------------------------------
 JEDEC ID: attrs.JedecID
 Serial:   1272205213
 Status 1: 00
 Status 2: 00

 Max clock speed (MHz): 50
 Has Sector Protection: false
 Supports Fast Reads:   false
 Supports QSPI Reads:   false
 Supports QSPI Write:   false
 Write Status Split:    false
 Single Status Byte:    false
-Sectors:               31422464
-Bytes (Sectors * 512)  16087777280
-ManufacturerID         74
-OEMApplicationID       744A
-ProductName            USDU1
-ProductVersion         1.0
-ProductSerialNumber    4BD44F9D
-ManufacturingYear      0A
-ManufacturingMonth     0F
-Always1                0
-CRC                    23
-------------------------------------

@deadprogram
Copy link
Member

deadprogram commented May 23, 2021

I think this looks good and it is in fact working for me. @sago35 can I merge it now, and then you will update #265 if needed?

@sago35
Copy link
Member Author

sago35 commented May 23, 2021

@deadprogram
Yes, please merge this PR.
I will update #265 later.

@deadprogram
Copy link
Member

Great! Thank you all your work on this contribution @sago35 now merging.

@deadprogram deadprogram merged commit d07ad40 into tinygo-org:dev May 23, 2021
@sago35 sago35 deleted the sdcard branch May 31, 2021 23:12
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.

2 participants