this post was submitted on 30 Aug 2025
3 points (100.0% liked)

Embedded

540 readers
1 users here now

This sub is dedicated to discussion and questions about embedded systems: "a controller programmed and controlled by a real-time operating system (RTOS) with a dedicated function within a larger mechanical or electrical system, often with real-time computing constraints."

founded 2 years ago
MODERATORS
 

Hi All!

Is there anyone here experienced with Zephyr OS? I am prototyping with a cheap SPI SDCard adapter and I am not able to initialize the sdcard in Zephyr OS. Hopefully, I did some stupid mistake that can be easily spotted.

I tested the adapter and an sdcard with an Arduino (they are sold as "Arduino adapter") and they both work fine.

Thanks in advance for your time!

My setup

Following the documentation found on the Zephyr website and some official examples: here the relevant part of my overlay tree:

&spi1 {
	status = "okay";
	cs-gpios = <&gpioa 4 GPIO_ACTIVE_LOW>;
	pinctrl-0 = <&spi1_clk_a5 &spi1_miso_a6 &spi1_mosi_a7>;
	pinctrl-names = "default";

	sdhc0: sdhc@0 {
		compatible = "zephyr,sdhc-spi-slot";
		reg = <0>;
		status = "okay";
		mmc {
			compatible = "zephyr,sdmmc-disk";
			disk-name = "SD";
			status = "okay";
		};
		spi-max-frequency = <400000>;
	};
};

&pinctrl {
	/omit-if-no-ref/ spi1_clk_a5: spi1_clk_a5 {
		pinmux = <STM32_PINMUX('A', 5, AF5)>;
	};

	/omit-if-no-ref/ spi1_miso_a6: spi1_miso_a6 {
		pinmux = <STM32_PINMUX('A', 6, AF5)>;
		bias-pull-up;
	};

	/omit-if-no-ref/ spi1_mosi_a7: spi1_mosi_a7 {
		pinmux = <STM32_PINMUX('A', 7, AF5)>;
	};
};

Basically, I configure the pins for the SPI function and I tell zephyr I have a SDHC driven via SPI (sdhc0) that is exposed as disk (mmc).

Here my Kconfig (proj.conf):

# ----------------------------------
# SPI / SDCard
# ----------------------------------
CONFIG_SPI=y
CONFIG_SDHC=y
CONFIG_DISK_ACCESS=y
CONFIG_DISK_DRIVERS=y

CONFIG_SPI_SDHC=y

As you can see I am enabling the SPI, together with the SDHC support via SPI + some other kernel module to have the API for storage devices.

The issue I am having is happening when trying to initialize the sdcard with:

...
static const char *disk_pdrv = "SD";
int ret = disk_access_ioctl(disk_pdrv, DISK_IOCTL_CTRL_INIT,  NULL);
...

Error

My application does not pass that call and returns a -134 error. I attached a Logic Analyzer to the pins of the adapter and I spotted something it could be due to communication issue.

For example, the communication start with 20ish CMD0, CMD8 repeated exchanges:

  • CMD0
    • MOSI: The Master pulls down the CS and sends a 0xFF 0x40 0x00 0x00 0x00 0x00 0x95
    • MISO: The Slave replies with a 0x01
  • CMD8:
    • MOSI: The Master pulls up and down the CS and sends a 0xFF 0x48 0x00 0x00 0x01 0xAA 0x87
    • MISO: The Slave replies with a 0x01 0x00 0x00 0x01 0xAA

Then the communication continues with some other exchanges (other CMDs), but it ends up with a couple of very long transmissions with Chip Select kept down for about 60 seconds each and only 0xFF seen on the lines.

On the Arduino there are no such repetitions (CMD0/CMD8), neither such long transmissions.

My doubt

The Arduino is driving the clock of the SPI at 250 KHz, whilst on Zephyr I am unable to go below ~330 KHz. I shorten the cables from the standard 20 cm breadboard jump wires to 8 cm. However, this didn't solve the problem. I don't know if the SPI clock frequency is the cause or whether I am doing something wrong in the device tree / Kconfig configuration.

no comments (yet)
sorted by: hot top controversial new old
there doesn't seem to be anything here