Cant get the Arduino SDFAT library to work on the DUO 64m

environment:
I use Arduino IDE 2.3.2 on a Win10 Pro 64 bits version.
I installed Milk-V Arduino support as described in manual.
I use a MILK-V 64M with the arduino firmware provided by Milk-V

What I want to do:
I wrote a (large) sketch that uses the Serial… functions. That sketch worked. I was impressed by the speed of execution.

Then I wanted to implement a SDCard reader in the sketch.
First I typed in the SPI example from the instruction. That worked.

Next I wanted to try the CARDINFO example that comes with the SDFAT library.
I connected a (3.3V compliant) SD card module to the MILK-V. I used the pins described in the MILK-V manual:
CS->GP9[pin12], MISO->GP8[pin11], MOSI->GP7[pin10], Clock->GP6[pin9]

The first failure was that the sketch did not compile.
Error “Architecture not supported”.
I then replaced the SD library in the Arduino libraries directory with the one supplied in library.zip in the MILK-V distribution.
=> Clumsy I had to do that by hand.
After that the CARDINFO sketch would compile and upload to the MILK-V

When I tried to run the sketch I got an error message “pin GPIO 9 is not used as GPIO func” (and the sketch did not recognize the SD card).
This error message is not in the sketch or in the library, so i must be issued by the Arduino firmware on the MILK-V.

How can I fix this? Thanks for your input.

Hi.

You might want to try pinmux first to check if pin GP9 (chip select) was configured as a GPIO pin and not for other function.

Feels like this should help.

1 Like

Thanks for the reply. I did not know about “duo-pinmux” so I checked it out. Unfortunately it did not solve the problem. I will explain what I tried. I will start explaining what I understand about SPI. This could be wrong, if so correct me please.

[SPI] is a serial protocol used by a slave/master pair to communicate. The actual communication goes via MISO/MOSI (and ground). SCK is a clock signal to keep sender and receiver in sync. More than one slave can be attached to the SPI bus (=MISO/MOSI/SCK). Wich slave is addressed is selected by making its corresponding CS (Chip Select?) line active.
This is why the existance of a dedicated SPI2_CS pin on the milk-v puzzles me. You should be able to use any GPxx pin and assign one to every slave on the SPI “bus”.

what I tried:
as the replier said I logged in into the main linux core (see manual on how to do this, I used a win10 powershell SSH command).
[1] “duo-pinmux” typed in by itself will show usage information. “duo-pinmux -l” showed GP9 to be assigned to “SPI_2_CS_X”. I changed that to “GP9”.
[2] I had to log out (“exit”) to be able to upload a sketch with the Arduino IDE. It did not work; I still got the “Pin GPIO 9 is not used as GPIO pin” error. When I checked it seemed that the sketch upload had reset the GP9 assignment back to “SPI_2_CS_X”
[3] As I could not see the need for a dedicated SPI_CS pin I chose one of the normal GPx assigned pins. I used GP1 (on physical pin2). This made the “pin GPIO … etc” warning go away, but the sketch still did not “see” the SD card. I also tried to reverse MISO and MOSI but that made no difference.

So,
[a] what more can I do to read an SD card with MILK-V Arduino.
[b] is it possible to execute “duo-pinmux” commands from the Arduino environment / sketch. Is it possible to make “duo-pinmux” chanes permanent an survive a powercycle?
[c] would it be possible for an Arduino sketch to read files from the “native” micro SD card on the milk-v duo?
[d] an unrelated question how can I RESET the MILK-V Arduino software without power cycling the whole card? I tried grounding RUN but that did nothing.

I hope someone can help me.

1 Like

Make sure the CE/CS (chip enable/chip select – same thing, just used interchangeably by different vendors) is pulled HIGH (10K - 100K Ohm resistor is fine). It is active low. The SPI pins (on any device, not just the Duo) are configured as part of a bus/channel (e.g. /dev/spidev0.0 for the SPI0 bus and /dev/spidev1.0 for SPI1) SPI2 isn’t exposed in the default buildroot image – it requires configuring and image rebuild. Simply do:

# ls -al /dev/spi*

For the exposed device listing.

The pinout will show which pins (MISO, MOSI, CLK, and CE/CS) are mapped to a specific SPIx bus. The schematic v1.2 shows the SPI0 and SPI1 configuration for SDIO. That is worth reviewing just to make sure you have the pins correct.

I’ve not yet tried the SdFat library with the Duo, but have with the pico and while SPI is fast, using the SDIO (with D0, D1, D2, D3 data lines) gave over twice the read/write speed.

Sorry not an exact solution for you, but hopefully the information helps. Use duo-pinmux to confirm your set of pins are configured for the SPI functions.

If I need something to survive restart, I just write a short script to set the pins the way I want and have it called automatically on boot through /etc/init.d/S99user. For example I just added the following to call duo-pinmux to set pins as needed:

        if [ -f /mnt/bin/exportpins.sh ]; then
                usleep 30000
                . /mnt/bin/exportpins.sh &
        fi

The exportpins.sh script is a simple script:

#!/bin/sh

gpdir="/sys/class/gpio"

for p in 371 496 497; do
  echo "$p" > "$gpdir/export"
  sleep 1
  echo "out" > "$gpdir/gpio${p}/direction"
done

duo-pinmux -w GP4/GP4
duo-pinmux -w GP12/GP12
duo-pinmux -w GP13/GP13

duo-pinmux -w GP2/PWM_10
duo-pinmux -w GP3/PWM_11

(adjust as needed for your setup - the delay can be much shorter, e.g. sleep .1 should work as well, I just hadn’t taking the time to confirm whether busybox supported fractional values with sleep yet)

2 Likes