Oliver M
02/26/2023, 7:16 PMMount
, which allows me to mount (and format, if necessary) an SD Card. But how do I read and write with it? Are there any examples that I could use as a starting off point?floitsch
02/26/2023, 9:35 PMhost
package has file and directory operations.floitsch
02/26/2023, 9:36 PMhost
package's functions should map to sdcard accesses.
We still need to update the package description...mikkel.damsgaard
02/27/2023, 6:08 AMOliver M
02/27/2023, 8:47 AMmikkel.damsgaard
02/27/2023, 9:21 AMInformatic0re
02/27/2023, 1:53 PMsdcard = SDCard
--miso=gpio.Pin 19
--mosi=gpio.Pin 23
--clk=gpio.Pin 18
--cs=gpio.Pin 5
sdcard.openw filename
sdcard.write "something"
sdcard.close
using this abstraction file:
import flash
import gpio
import spi
import host.file
class SDCard:
csvfile/file.Stream := ?
constructor --miso/gpio.Pin --mosi/gpio.Pin --clk/gpio.Pin --cs/gpio.Pin --mount_point/string="/sd":
bus := spi.Bus
--miso=miso
--mosi=mosi
--clock=clk
sdcard := flash.Mount.sdcard
--mount_point=mount_point
--spi_bus=bus
--cs=cs
csvfile = file.Stream.for_write "/sd/test"
csvfile.close
print "SDCard initialised"
write line/string filename/string:
print "write \"$line\" into file: $filename"
csvfile = file.Stream.for_write filename
csvfile.write line
csvfile.close
openw filename:
csvfile = file.Stream.for_write filename
openr filename:
csvfile = file.Stream.for_read filename
write line/string:
csvfile.write line
read_file filename -> string:
return (file.read_content filename).to_string
close:
csvfile.close
Oliver M
02/27/2023, 2:09 PMOliver M
03/12/2023, 6:20 PME (175187) sdmmc_sd: sdmmc_init_sd_if_cond: send_if_cond (1) returned 0x108
E (175187) vfs_fat_sdmmc: sdmmc_card_init failed (0x108).
******************************************************************************
Decoding by `jag`, device has version <2.0.0-alpha.55>
******************************************************************************
EXCEPTION error.
UNKNOWN ERROR 0x108(264)
0: init_sdcard_ <sdk>/flash.toit:141:3
1: Mount.sdcard <sdk>/flash.toit:44:14
2: SDCard sdtest.toit:18:15
3: main sdtest.toit:55:13
******************************************************************************
Oliver M
03/12/2023, 6:21 PMOliver M
03/12/2023, 6:21 PMInformatic0re
03/12/2023, 6:32 PMOliver M
03/12/2023, 6:34 PMInformatic0re
03/12/2023, 6:36 PMOliver M
03/12/2023, 6:41 PMOliver M
03/12/2023, 7:03 PME (22087) sdmmc_sd: sdmmc_check_scr: send_scr returned 0x107
E (22087) vfs_fat_sdmmc: sdmmc_card_init failed (0x107).
******************************************************************************
Decoding by `jag`, device has version <2.0.0-alpha.55>
******************************************************************************
EXCEPTION error.
UNKNOWN ERROR 0x107(263)
0: init_sdcard_ <sdk>/flash.toit:141:3
1: Mount.sdcard <sdk>/flash.toit:44:14
2: SDCard sdtest.toit:18:15
3: main sdtest.toit:56:13
******************************************************************************
Oliver M
03/12/2023, 7:17 PME (58017) vfs_fat_sdmmc: sdmmc_card_init failed (0x107).
******************************************************************************
Decoding by `jag`, device has version <2.0.0-alpha.55>
******************************************************************************
EXCEPTION error.
UNKNOWN ERROR 0x107(263)
0: init_sdcard_ <sdk>/flash.toit:141:3
Oliver M
03/12/2023, 7:34 PMOliver M
03/12/2023, 7:38 PMInformatic0re
03/12/2023, 7:53 PMmikkel.damsgaard
03/13/2023, 7:56 AM#define ESP_ERR_TIMEOUT 0x107 /*!< Operation timed out */
I think that is what you see. That would suggest that the SPI protocol receives a timeout. I would double check the pins and their connections.Oliver M
03/13/2023, 11:10 AMInformatic0re
03/13/2023, 2:03 PMInformatic0re
03/13/2023, 2:05 PMInformatic0re
03/13/2023, 2:05 PMInformatic0re
03/13/2023, 2:06 PMOliver M
03/13/2023, 2:39 PMOliver M
03/13/2023, 3:20 PMfloitsch
03/13/2023, 3:20 PMfloitsch
03/13/2023, 3:24 PMfloitsch
03/13/2023, 3:26 PM// Check if there is a preferred device.
if ((mosi == -1 || mosi == 13) &&
(miso == -1 || miso == 12) &&
(clock == -1 || clock == 14)) {
host_device = HSPI_HOST;
}
if ((mosi == -1 || mosi == 23) &&
(miso == -1 || miso == 19) &&
(clock == -1 || clock == 18)) {
host_device = VSPI_HOST;
}
So if you use these pins, you would get the optimal assignment, but it's really not necessary.Informatic0re
03/13/2023, 3:26 PMfloitsch
03/13/2023, 3:26 PMfloitsch
03/13/2023, 3:26 PMfloitsch
03/13/2023, 3:27 PMOliver M
03/13/2023, 3:27 PMfloitsch
03/13/2023, 3:28 PMMost of ESP32’s peripheral signals have direct connection to their dedicated IO_MUX pins. However, the signals can also be routed to any other available pins using the less direct GPIO matrix. If at least one signal is routed through the GPIO matrix, then all signals will be routed through it.
The GPIO matrix introduces flexibility of routing but also brings the following disadvantages:
- Increases the input delay of the MISO signal, which makes MISO setup time violations more likely. If SPI needs to operate at high speeds, use dedicated IO_MUX pins.
- Allows signals with clock frequencies only up to 40 MHz, as opposed to 80 MHz if IO_MUX pins are used.
Oliver M
03/13/2023, 3:29 PMfloitsch
03/13/2023, 3:29 PMInformatic0re
03/13/2023, 3:29 PMfloitsch
03/13/2023, 3:31 PMInformatic0re
03/13/2023, 3:32 PMimport gpio
import spi
main:
bus := spi.Bus
--miso=gpio.Pin 12
--mosi=gpio.Pin 13
--clock=gpio.Pin 14
device := bus.device
--cs=gpio.Pin 15
--frequency=10_000_000
floitsch
03/13/2023, 3:33 PMfloitsch
03/13/2023, 3:33 PMfloitsch
03/13/2023, 3:38 PMOliver M
03/13/2023, 3:46 PMOliver M
03/13/2023, 3:46 PMfloitsch
03/13/2023, 3:51 PMfloitsch
03/13/2023, 3:52 PMfloitsch
03/13/2023, 3:55 PMfloitsch
03/13/2023, 3:56 PMOliver M
03/13/2023, 4:04 PMOliver M
03/13/2023, 9:00 PMMount
? Do I create a Device
with the frequency (which I then ignore)? SD Card is the only option which hasn't got a frequency parameter.Oliver M
03/13/2023, 9:05 PMfloitsch
03/13/2023, 9:07 PMOliver M
03/13/2023, 9:14 PMOliver M
03/13/2023, 9:14 PMbus.device
with a frequency of 1MHzOliver M
03/13/2023, 9:17 PMfloitsch
03/13/2023, 9:17 PMOliver M
03/13/2023, 9:17 PMOliver M
03/13/2023, 9:17 PMfloitsch
03/13/2023, 9:18 PMOliver M
03/13/2023, 9:18 PMOliver M
03/14/2023, 7:16 PMESP_ERR_INVALID_CRC
If I take the card out of the reader, then I'm getting a timeout (0x107)Oliver M
03/14/2023, 7:18 PMMount.sdcard
or Mount.sdcard_unformatted
😦Oliver M
03/14/2023, 7:22 PMmikkel.damsgaard
03/14/2023, 8:00 PMOliver M
03/14/2023, 8:08 PMOliver M
03/14/2023, 8:54 PMInformatic0re
03/14/2023, 10:30 PMOliver M
03/15/2023, 12:49 PMfloitsch
03/15/2023, 12:58 PMfloitsch
03/15/2023, 12:59 PMfloitsch
03/15/2023, 1:00 PMfloitsch
03/15/2023, 1:00 PMfloitsch
03/15/2023, 1:01 PMfloitsch
03/15/2023, 1:01 PMfloitsch
03/15/2023, 1:04 PMfloitsch
03/15/2023, 1:04 PMfloitsch
03/15/2023, 1:07 PMOliver M
03/15/2023, 1:15 PMOliver M
03/15/2023, 1:16 PMOliver M
03/15/2023, 1:16 PMfloitsch
03/15/2023, 1:21 PMOliver M
03/15/2023, 1:24 PMOliver M
03/19/2023, 12:55 PM