zephyrproject-rtos / zephyr

Primary Git Repository for the Zephyr Project. Zephyr is a new generation, scalable, optimized, secure RTOS for multiple hardware architectures.
https://docs.zephyrproject.org
Apache License 2.0
10.08k stars 6.2k forks source link

SD Subsystem Buffer Alignment Hard-Fault when build for Raspberry-PI Pico or Pico-W #69441

Open foldedtoad opened 4 months ago

foldedtoad commented 4 months ago

Hard-Fault due to Alignment issues in SD Subsystem when build for RPI-PICO.


Project: USB-MSC backed by SDcard zephyr/samples/subsys/usb/mass starting point Platform: Raspberry-Pi Pico and Pico-w SDCard: 2GB SDCard


Taken from ARM's online documentation for Cortex-M0+

"There is no support for unaligned accesses on the Cortex-M0 processor. Any attempt to perform an unaligned memory access operation results in a HardFault exception."


There are two locations where alignment-faults happen for this sample project when built for the rpi_pico or rpi_pico_w.

1: In zephyr/subsys/sd/sdmmc.c Zephyr V3.5 circa line 186 Zephyr V3.6 circa line 186

    /* Decode SCR */
    raw_scr[0] = sys_be32_to_cpu(scr[0]);
    raw_scr[1] = sys_be32_to_cpu(scr[1]);

2: In zephyr/subsys/sd/sd_ops.c
Zephyr V3.5 circa line 219
Zephyr V3.6 circa line 231

    /* Swap endianness of CXD */
    for (i = 0; i < 4; i++) {
        cxd[3 - i] = sys_be32_to_cpu(cxd_be[i]);
    }

In zephyr/include/zephyr/sd/sd.h (see member of struct)

struct sd_card {
    const struct device *sdhc; /*!< SD host controller for card */
    struct sdhc_io bus_io; /*!< Current bus I/O props for SDHC */
    enum sd_voltage card_voltage; /*!< Card signal voltage */
    struct k_mutex lock; /*!< card mutex */
    struct sdhc_host_props host_props; /*!< SDHC host properties */
    uint32_t ocr; /*!< Raw card OCR content */
    struct sd_switch_caps switch_caps; /*!< SD switch capabilities */
    unsigned int num_io: 3; /*!< I/O function count. 0 for SD cards */
    uint16_t relative_addr; /*!< Card relative address */
    uint32_t block_count; /*!< Number of blocks in SD card */
    uint16_t block_size; /*!< SD block size */
    uint8_t sd_version; /*!< SD specification version */
    uint8_t card_speed; /*!< Card timing mode */
    enum card_status status; /*!< Card status */
    enum card_type type; /*!< Card type */
    uint16_t flags; /*!< Card flags */
    uint8_t bus_width; /*!< Desired bus width */
    uint8_t card_buffer[CONFIG_SD_BUFFER_SIZE]
        __aligned(CONFIG_SDHC_BUFFER_ALIGNMENT); /* Card internal buffer */
};

A possible solution suggestion. This worked for the rpi_pico/SDcard project.
This solution sets CONFIG_SDHC_BUFFER_ALIGNMENT to __aligned(4) for RPI_PICO.


In file zephyr/drivers/sdhc/Kconfig

source "drivers/sdhc/Kconfig.imx"
source "drivers/sdhc/Kconfig.spi"
source "drivers/sdhc/Kconfig.mcux_sdif"
source "drivers/sdhc/Kconfig.sam_hsmci"
source "drivers/sdhc/Kconfig.intel"
source "drivers/sdhc/Kconfig.rpi_pico_sdhc"

In file zephyr/drivers/sdhc/Kconfig.rpi_pico_sdhc (a new config file)

config RPI_PICO_SDHC
    bool "Raspbery-Pi SDHC Driver"
    default y
    help
      Enable the Raspbery-Pi Pico SDHC driver

if RPI_PICO_SDHC

config SDHC_BUFFER_ALIGNMENT
    default 4
    help
        struct sd_card's internal buffer must be 32-bit aligned.

endif # RPI_PICO_SDHC
github-actions[bot] commented 4 months ago

Hi @foldedtoad! We appreciate you submitting your first issue for our open-source project. šŸŒŸ

Even though I'm a bot, I can assure you that the whole community is genuinely grateful for your time and effort. šŸ¤–šŸ’™

danieldegrasse commented 4 months ago

@foldedtoad with #62669 do you still see the issue? This sounds like a similar problem, but that fix should be in 3.6

github-actions[bot] commented 2 months ago

This issue has been marked as stale because it has been open (more than) 60 days with no activity. Remove the stale label or add a comment saying that you would like to have the label removed otherwise this issue will automatically be closed in 14 days. Note, that you can always re-open a closed issue at any time.

github-actions[bot] commented 1 day ago

This issue has been marked as stale because it has been open (more than) 60 days with no activity. Remove the stale label or add a comment saying that you would like to have the label removed otherwise this issue will automatically be closed in 14 days. Note, that you can always re-open a closed issue at any time.