1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

Merge pull request #17863 from benpicco/sam0_sdhc

cpu/samd5x: add SD Host Controller implementation
This commit is contained in:
benpicco 2022-05-31 15:33:24 +02:00 committed by GitHub
commit 4b6da5c8e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 2547 additions and 11 deletions

View File

@ -27,5 +27,6 @@ config BOARD_SAME54_XPRO
select HAVE_SAUL_GPIO
select HAVE_MTD_SPI_NOR
select HAVE_AT24MAC
select HAVE_SAM0_SDHC
# This specific board requires SPI_ON_QSPI for the MTD_SPI_NOR
select MODULE_PERIPH_SPI_ON_QSPI if MODULE_MTD_SPI_NOR

View File

@ -10,6 +10,7 @@ ifneq (,$(filter mtd,$(USEMODULE)))
FEATURES_REQUIRED += periph_spi_on_qspi
USEMODULE += mtd_spi_nor
USEMODULE += mtd_at24cxxx at24mac
USEMODULE += sam0_sdhc
endif
# enables sam0_eth as default network device
@ -17,8 +18,9 @@ ifneq (,$(filter netdev_default,$(USEMODULE)))
USEMODULE += sam0_eth
endif
# default to using littlefs2 on the external flash
# default to using littlefs2 on the external flash and fatfs on SD card
ifneq (,$(filter vfs_default,$(USEMODULE)))
USEPKG += littlefs2
USEMODULE += fatfs_vfs
USEMODULE += mtd
endif

View File

@ -20,10 +20,13 @@
#include "board.h"
#include "periph/gpio.h"
#include "mtd_spi_nor.h"
#include "timex.h"
#ifdef MODULE_VFS_DEFAULT
#include "vfs_default.h"
#endif
#ifdef MODULE_MTD
#ifdef MODULE_MTD_SPI_NOR
#include "mtd_spi_nor.h"
/* N25Q256A or SST26VF064B */
static const mtd_spi_nor_params_t _same54_nor_params = {
.opcode = &mtd_spi_nor_opcode_default,
@ -50,6 +53,12 @@ static mtd_spi_nor_t same54_nor_dev = {
};
mtd_dev_t *mtd0 = (mtd_dev_t *)&same54_nor_dev;
#ifdef MODULE_VFS_DEFAULT
VFS_AUTO_MOUNT(littlefs2, VFS_MTD(same54_nor_dev), VFS_DEFAULT_NVM(0), 0);
#endif
#endif /* MODULE_MTD_SPI_NOR */
#ifdef MODULE_MTD_AT24CXXX
#include "mtd_at24cxxx.h"
#include "at24cxxx_params.h"
static at24cxxx_t at24cxxx_dev;
@ -61,9 +70,23 @@ static mtd_at24cxxx_t at24mac_dev = {
.params = at24cxxx_params,
};
mtd_dev_t *mtd1 = (mtd_dev_t *)&at24mac_dev;
#endif /* MODULE_MTD_AT24CXXX */
#ifdef MODULE_SAM0_SDHC
#include "mtd_sam0_sdhc.h"
static mtd_sam0_sdhc_t sdhc_dev = {
.base = {
.driver = &mtd_sam0_sdhc_driver,
},
.state = {
.dev = SDHC1,
.cd = GPIO_PIN(PD, 20),
.wp = GPIO_PIN(PD, 21),
},
};
mtd_dev_t *mtd2 = (mtd_dev_t *)&sdhc_dev;
#ifdef MODULE_VFS_DEFAULT
#include "vfs_default.h"
VFS_AUTO_MOUNT(littlefs2, VFS_MTD(same54_nor_dev), VFS_DEFAULT_NVM(0), 0);
VFS_AUTO_MOUNT(fatfs, VFS_MTD(sdhc_dev), VFS_DEFAULT_SD(0), 1);
#endif
#endif /* MODULE_MTD */
#endif /* MODULE_SAM0_SDHC */

View File

@ -72,10 +72,11 @@ extern "C" {
* @name MTD configuration
* @{
*/
extern mtd_dev_t *mtd0, *mtd1;
extern mtd_dev_t *mtd0, *mtd1, *mtd2;
#define MTD_0 mtd0
#define MTD_1 mtd1
#define MTD_NUMOF 2
#define MTD_2 mtd2
#define MTD_NUMOF 3
/** @} */
/**

View File

@ -360,6 +360,17 @@ static const adc_conf_chan_t adc_channels[] = {
#define DAC_VREF DAC_CTRLB_REFSEL_VREFPU
/** @} */
/**
* @name SDHC configuration
*
* This is entirely optional, but allows us to save a few bytes if only
* a single SDHC instance is used.
* @{
*/
#define SDHC_DEV SDHC1 /**< The SDHC instance to use */
#define SDHC_DEV_ISR isr_sdhc1 /**< Interrupt service routing for SDHC1 */
/** @} */
/**
* @name Ethernet peripheral configuration
* @{

View File

@ -1,7 +1,10 @@
DIRS = periph
ifneq (, $(filter sam0_eth, $(USEMODULE)))
DIRS += sam0_eth
ifneq (,$(filter sam0_eth, $(USEMODULE)))
DIRS += sam0_eth
endif
ifneq (,$(filter sam0_sdhc, $(USEMODULE)))
DIRS += sam0_sdhc
endif
include $(RIOTBASE)/Makefile.base

View File

@ -0,0 +1,55 @@
/*
* Copyright (C) 2022 Benjamin Valentin
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup drivers_storage
* @ingroup cpu_sam0_common_sdhc
* @brief Driver for SD-cards using mtd interface
*
* @{
*
* @file
* @brief Interface definition for SAM SDHC driver
*
* @author Benjamin Valentin <benjamin.valentin@ml-pa.com>
*/
#ifndef MTD_SAM0_SDHC_H
#define MTD_SAM0_SDHC_H
#include <stdint.h>
#include "mtd.h"
#include "sdhc.h"
#ifdef __cplusplus
extern "C"
{
#endif
/**
* @brief Device descriptor for mtd_sdcard device
*
* This is an extension of the @c mtd_dev_t struct
*/
typedef struct {
mtd_dev_t base; /**< inherit from mtd_dev_t object */
sdhc_state_t state; /**< sdcard_spi dev descriptor */
} mtd_sam0_sdhc_t;
/**
* @brief sdcard device operations table for mtd
*/
extern const mtd_desc_t mtd_sam0_sdhc_driver;
#ifdef __cplusplus
}
#endif
#endif /* MTD_SAM0_SDHC_H */
/** @} */

View File

@ -0,0 +1,167 @@
/*
* Copyright (C) 2018 Alkgrove
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @defgroup cpu_sam0_common_sdhc sam0 SD Host Controller
* @ingroup cpu_sam0_common
* @brief SD card interface functions for sam0 class devices
*
* @{
*
* @file
* @brief SD card interface functions for sam0 class devices
*
* @author alkgrove <bob@alkgrove.com>
* @author Benjamin Valentin <benjamin.valentin@ml-pa.com>
*/
#ifndef SDHC_H
#define SDHC_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
#include <stdbool.h>
#include "periph/gpio.h"
#include "mutex.h"
/**
* @brief SD Card driver context
*/
typedef struct {
Sdhc *dev; /**< SDHC instance */
gpio_t cd; /**< Card detect pin */
gpio_t wp; /**< Write Protect pin */
mutex_t lock; /**< Ensure thread-safe access */
mutex_t sync; /**< ISR mutex */
uint32_t sectors; /**< Capacity in bytes */
uint32_t clock; /**< Accepted Cloc Rate in Hz */
uint16_t rca; /**< Relative Card Address */
uint16_t error; /**< Last error state */
uint8_t type; /**< Type of Card */
uint8_t version; /**< Version of Card */
uint8_t bus_width; /**< Acceptable Bus Width (1 or 4) */
bool high_speed; /**< Turbo mode */
bool need_init; /**< Card installed but not initialized if true */
} sdhc_state_t;
/** @name Card Types */
/** @{ */
#define CARD_TYPE_UNKNOWN (0) /**< Unknown type card */
#define CARD_TYPE_SD (1 << 0) /**< SD card */
#define CARD_TYPE_MMC (1 << 1) /**< MMC card */
#define CARD_TYPE_SDIO (1 << 2) /**< SDIO card */
#define CARD_TYPE_HC (1 << 3) /**< High capacity card */
/** SD combo card (io + memory) */
#define CARD_TYPE_SD_COMBO (CARD_TYPE_SD | CARD_TYPE_SDIO)
/** @} */
/** @name Card Versions */
/** @{ */
#define CARD_VER_UNKNOWN (0) /**< Unknown card version */
#define CARD_VER_SD_1_0 (0x10) /**< SD version 1.0 and 1.01 */
#define CARD_VER_SD_1_10 (0x1A) /**< SD version 1.10 */
#define CARD_VER_SD_2_0 (0X20) /**< SD version 2.00 */
#define CARD_VER_SD_3_0 (0X30) /**< SD version 3.0X */
#define CARD_VER_MMC_1_2 (0x12) /**< MMC version 1.2 */
#define CARD_VER_MMC_1_4 (0x14) /**< MMC version 1.4 */
#define CARD_VER_MMC_2_2 (0x22) /**< MMC version 2.2 */
#define CARD_VER_MMC_3 (0x30) /**< MMC version 3 */
#define CARD_VER_MMC_4 (0x40) /**< MMC version 4 */
/** @} */
/** @name Flags used to define MCI parser for SD/MMC command */
/** @{ */
#define MCI_RESP_PRESENT (1ul << 8) /**< Have response */
#define MCI_RESP_136 (1ul << 11) /**< 136 bit response */
#define MCI_RESP_CRC (1ul << 12) /**< Expect valid crc */
#define MCI_RESP_BUSY (1ul << 13) /**< Card may send busy */
#define MCI_CMD_OPENDRAIN (1ul << 14) /**< Open drain for a braodcast command */
#define MCI_CMD_WRITE (1ul << 15) /**< To signal a data write operation */
#define MCI_CMD_SDIO_BYTE (1ul << 16) /**< To signal a SDIO transfer in multi byte mode */
#define MCI_CMD_SDIO_BLOCK (1ul << 17) /**< To signal a SDIO transfer in block mode */
#define MCI_CMD_STREAM (1ul << 18) /**< To signal a data transfer in stream mode */
#define MCI_CMD_SINGLE_BLOCK (1ul << 19) /**< To signal a data transfer in single block mode */
#define MCI_CMD_MULTI_BLOCK (1ul << 20) /**< To signal a data transfer in multi block mode */
/** @} */
/** This SD stack uses the maximum block size authorized (512 bytes) */
#define SD_MMC_BLOCK_SIZE 512 /**< SD card block size */
#define SDHC_SLOW_CLOCK_HZ 400000 /**< Clock frequency on init */
/**
* @brief Initialize the SD host controller
*
* @param[in] state driver context
*
* @return int 0 on success, error otherwise
*/
int sdhc_init(sdhc_state_t *state);
/**
* @brief Send a command to the SD card
*
* @param[in] state driver context
* @param[in] cmd the command code
* @param[in] arg command argument
*
* @return true command was successful
* @return false command returned error
*/
bool sdhc_send_cmd(sdhc_state_t *state, uint32_t cmd, uint32_t arg);
/**
* @brief Read blocks from the SD card into memory
*
* Reads n 512 byte blocks from the SD card
*
* @param[in] state driver context
* @param[in] block block number to read from
* @param[out] dst destination address
* @param[in] num number of blocks to read
*
* @return 0 if success, negative error if failed
*/
int sdhc_read_blocks(sdhc_state_t *state, uint32_t block, void *dst, uint16_t num);
/**
* @brief Write memory to SD card blocks
*
* Writes n 512 bytes blocks on the SD card
*
* @param[in] state driver context
* @param[in] block block number to write to
* @param[in] src pointer to memory to write
* @param[in] num number of blocks to write
*
* @return 0 if success, negative error if failed
*/
int sdhc_write_blocks(sdhc_state_t *state, uint32_t block, const void *src,
uint16_t num);
/**
* @brief Erase memory from SD card blocks
*
* Erases n 512 byte blocks on the SD card
*
* @param[in] state driver context
* @param[in] block first block number to erase
* @param[in] num number of blocks to erase
*
* @return 0 if success, negative error if failed
*/
int sdhc_erase_blocks(sdhc_state_t *state, uint32_t block, uint16_t num);
#ifdef __cplusplus
}
#endif
#endif /* SDHC_H */
/** @} */

View File

@ -0,0 +1,3 @@
MODULE=sam0_sdhc
include $(RIOTBASE)/Makefile.base

View File

@ -0,0 +1,154 @@
/*
* Copyright (C) 2022 Benjamin Valentin
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*
*/
/**
* @ingroup drivers_mtd
* @{
*
* @file
* @brief Driver for using sam0 SDHC controller via mtd interface
*
* @author Benjamin Valentin <benjamin.valentin@ml-pa.com>
*
* @}
*/
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include "mtd_sam0_sdhc.h"
#define ENABLE_DEBUG 0
#include "debug.h"
#define min(a, b) ((a) > (b) ? (b) : (a))
static int _init(mtd_dev_t *dev)
{
mtd_sam0_sdhc_t *ctx = container_of(dev, mtd_sam0_sdhc_t, base);
if (sdhc_init(&ctx->state)) {
DEBUG("SDHC init failed\n");
return -EIO;
}
dev->pages_per_sector = 1;
dev->page_size = SD_MMC_BLOCK_SIZE;
dev->sector_count = ctx->state.sectors;
dev->write_size = SD_MMC_BLOCK_SIZE;
#if IS_USED(MODULE_MTD_WRITE_PAGE)
/* TODO: move to MTD layer */
dev->work_area = malloc(SD_MMC_BLOCK_SIZE);
if (dev->work_area == NULL) {
return -ENOMEM;
}
dev->write_size = 1;
#endif
DEBUG("sector size: %lu\n", dev->page_size);
DEBUG("sector count: %lu\n", dev->sector_count);
DEBUG("pages / sector: %lu\n", dev->pages_per_sector);
return 0;
}
static int _read_page(mtd_dev_t *dev, void *buff, uint32_t page,
uint32_t offset, uint32_t size)
{
mtd_sam0_sdhc_t *ctx = container_of(dev, mtd_sam0_sdhc_t, base);
uint16_t pages = size / SD_MMC_BLOCK_SIZE;
DEBUG("%s(%lu, %lu, %lu)\n", __func__, page, offset, size);
/* emulate unaligned / sub-page read */
if (pages == 0 || offset) {
#if IS_USED(MODULE_MTD_WRITE_PAGE)
if (dev->work_area == NULL) {
DEBUG("mtd_sdhc: no work area\n");
return -ENOTSUP;
}
size = min(SD_MMC_BLOCK_SIZE - offset, size);
if (sdhc_read_blocks(&ctx->state, page, dev->work_area, 1)) {
return -EIO;
}
memcpy(buff, dev->work_area + offset, size);
return size;
#else
return -ENOTSUP;
#endif
}
if (sdhc_read_blocks(&ctx->state, page, buff, pages)) {
return -EIO;
}
return pages * SD_MMC_BLOCK_SIZE;
}
static int _write_page(mtd_dev_t *dev, const void *buff, uint32_t page,
uint32_t offset, uint32_t size)
{
mtd_sam0_sdhc_t *ctx = container_of(dev, mtd_sam0_sdhc_t, base);
uint16_t pages = size / SD_MMC_BLOCK_SIZE;
DEBUG("%s(%lu, %lu, %lu)\n", __func__, page, offset, size);
/* emulate unaligned / sub-page write */
if (pages == 0 || offset) {
#if IS_USED(MODULE_MTD_WRITE_PAGE)
if (dev->work_area == NULL) {
DEBUG("mtd_sdhc: no work area\n");
return -ENOTSUP;
}
size = min(SD_MMC_BLOCK_SIZE - offset, size);
if (sdhc_read_blocks(&ctx->state, page, dev->work_area, 1)) {
return -EIO;
}
memcpy(dev->work_area + offset, buff, size);
buff = dev->work_area;
pages = 1;
#else
return -ENOTSUP;
#endif
}
if (sdhc_write_blocks(&ctx->state, page, buff, pages)) {
return -EIO;
}
return min(size, pages * SD_MMC_BLOCK_SIZE);
}
static int _erase_sector(mtd_dev_t *dev, uint32_t sector, uint32_t count)
{
mtd_sam0_sdhc_t *ctx = container_of(dev, mtd_sam0_sdhc_t, base);
if (sdhc_erase_blocks(&ctx->state, sector, count)) {
return -EIO;
}
return 0;
}
const mtd_desc_t mtd_sam0_sdhc_driver = {
.init = _init,
.read_page = _read_page,
.write_page = _write_page,
.erase_sector = _erase_sector,
.flags = MTD_DRIVER_FLAG_DIRECT_WRITE,
};

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -172,6 +172,27 @@ struct sam0_aux_cfg_mapping {
#define SAM0_QSPI_MUX GPIO_MUX_H /**< QSPI mux */
/** @} */
/**
* @name SDHC pins are fixed
* @{
*/
#define SAM0_SDHC_MUX GPIO_MUX_I /**< SDHC function */
#define SAM0_SDHC0_PIN_SDCMD GPIO_PIN(PA, 8) /**< Command */
#define SAM0_SDHC0_PIN_SDDAT0 GPIO_PIN(PA, 9) /**< DATA0 */
#define SAM0_SDHC0_PIN_SDDAT1 GPIO_PIN(PA, 10) /**< DATA1 */
#define SAM0_SDHC0_PIN_SDDAT2 GPIO_PIN(PA, 11) /**< DATA2 */
#define SAM0_SDHC0_PIN_SDDAT3 GPIO_PIN(PB, 10) /**< DATA3 */
#define SAM0_SDHC0_PIN_SDCK GPIO_PIN(PB, 11) /**< Clock */
#define SAM0_SDHC1_PIN_SDCMD GPIO_PIN(PA, 20) /**< Command */
#define SAM0_SDHC1_PIN_SDDAT0 GPIO_PIN(PB, 18) /**< DATA0 */
#define SAM0_SDHC1_PIN_SDDAT1 GPIO_PIN(PB, 19) /**< DATA1 */
#define SAM0_SDHC1_PIN_SDDAT2 GPIO_PIN(PB, 20) /**< DATA2 */
#define SAM0_SDHC1_PIN_SDDAT3 GPIO_PIN(PB, 21) /**< DATA3 */
#define SAM0_SDHC1_PIN_SDCK GPIO_PIN(PA, 21) /**< Clock */
/** @} */
#ifdef __cplusplus
}
#endif

View File

@ -4906,11 +4906,13 @@ boards/same54\-xpro/include/board\.h:[0-9]+: warning: Member LED0_TOGGLE \(macro
boards/same54\-xpro/include/board\.h:[0-9]+: warning: Member LED_PORT \(macro definition\) of file board\.h is not documented\.
boards/same54\-xpro/include/board\.h:[0-9]+: warning: Member MTD_0 \(macro definition\) of file board\.h is not documented\.
boards/same54\-xpro/include/board\.h:[0-9]+: warning: Member MTD_1 \(macro definition\) of file board\.h is not documented\.
boards/same54\-xpro/include/board\.h:[0-9]+: warning: Member MTD_2 \(macro definition\) of file board\.h is not documented\.
boards/same54\-xpro/include/board\.h:[0-9]+: warning: Member MTD_NUMOF \(macro definition\) of file board\.h is not documented\.
boards/same54\-xpro/include/board\.h:[0-9]+: warning: Member XTIMER_HZ \(macro definition\) of file board\.h is not documented\.
boards/same54\-xpro/include/board\.h:[0-9]+: warning: Member XTIMER_WIDTH \(macro definition\) of file board\.h is not documented\.
boards/same54\-xpro/include/board\.h:[0-9]+: warning: Member mtd0 \(variable\) of file board\.h is not documented\.
boards/same54\-xpro/include/board\.h:[0-9]+: warning: Member mtd1 \(variable\) of file board\.h is not documented\.
boards/same54\-xpro/include/board\.h:[0-9]+: warning: Member mtd2 \(variable\) of file board\.h is not documented\.
boards/same54\-xpro/include/eui_provider_params\.h:[0-9]+: warning: Member EUI48_PROVIDER_FUNC \(macro definition\) of file eui_provider_params\.h is not documented\.
boards/same54\-xpro/include/eui_provider_params\.h:[0-9]+: warning: Member EUI48_PROVIDER_TYPE \(macro definition\) of file eui_provider_params\.h is not documented\.
boards/same54\-xpro/include/periph_conf\.h:[0-9]+: warning: Member ADC_DEV \(macro definition\) of file periph_conf\.h is not documented\.

View File

@ -37,6 +37,12 @@ config HAVE_MTD_SPI_NOR
help
Indicates that a spi-nor MTD is present
config HAVE_SAM0_SDHC
bool
imply MODULE_SAM0_SDHC if MODULE_MTD
help
Indicates that a SAM0 SD Host Controller MTD is present
config HAVE_MTD_SPI_MCI
bool
imply MODULE_MTD_MCI if MODULE_MTD
@ -86,6 +92,10 @@ config MODULE_MTD_SDCARD
bool "MTD interface for SPI SD-Card"
depends on MODULE_SDCARD_SPI
config MODULE_SAM0_SDHC
bool "MTD interface for SAM0 SD Host Controller"
depends on CPU_COMMON_SAM0
endmenu # MTD Interfacs
config MODULE_MTD_WRITE_PAGE

View File

@ -59,7 +59,7 @@ extern "C" {
* This can be written to by applications
*/
#ifndef VFS_DEFAULT_DATA
#if IS_USED(MODULE_MTD_MCI) || IS_USED(MODULE_MTD_SDCARD)
#if IS_USED(MODULE_MTD_MCI) || IS_USED(MODULE_MTD_SDCARD) || IS_USED(MODULE_SAM0_SDHC)
#define VFS_DEFAULT_DATA VFS_DEFAULT_SD(0)
#else
#define VFS_DEFAULT_DATA VFS_DEFAULT_NVM(0)