Skip to content

Commit d90a16f

Browse files
danieldegrassedleach02
authored andcommitted
include: sd: Add SD subsystem header files
Add SD subsystem headers. SD subsystem contains generic header for SD initialization, and headers for SDIO and SDMMC cards. Signed-off-by: Daniel DeGrasse <[email protected]>
1 parent 5b5e01b commit d90a16f

File tree

2 files changed

+171
-0
lines changed

2 files changed

+171
-0
lines changed

include/zephyr/sd/sd.h

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* Copyright 2022 NXP
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/**
8+
* @file
9+
* @brief Public API for SD subsystem
10+
*/
11+
12+
#ifndef ZEPHYR_INCLUDE_SD_SD_H_
13+
#define ZEPHYR_INCLUDE_SD_SD_H_
14+
15+
#include <zephyr/device.h>
16+
#include <zephyr/drivers/sdhc.h>
17+
18+
#ifdef __cplusplus
19+
extern "C" {
20+
#endif
21+
22+
/**
23+
* @brief card status. Used interally by subsystem.
24+
*/
25+
enum card_status {
26+
CARD_UNINITIALIZED = 0, /*!< card has not been initialized */
27+
CARD_ERROR = 1, /*!< card state is error */
28+
CARD_INITIALIZED = 2, /*!< card is in valid state */
29+
};
30+
31+
/**
32+
* @brief card type. Used interally by subsystem.
33+
*/
34+
enum card_type {
35+
CARD_SDMMC = 0, /*!< SD memory card */
36+
CARD_SDIO = 1, /*!< SD I/O card */
37+
CARD_COMBO = 2, /*!< SD memory and I/O card */
38+
};
39+
40+
41+
/**
42+
* @brief SD card structure
43+
*
44+
* This structure is used by the subsystem to track an individual SD
45+
* device connected to the system. The application may access these
46+
* fields, but use caution when changing values.
47+
*/
48+
struct sd_card {
49+
const struct device *sdhc; /*!< SD host controller for card */
50+
struct sdhc_io bus_io; /*!< Current bus I/O props for SDHC */
51+
enum sd_voltage card_voltage; /*!< Card signal voltage */
52+
struct k_mutex lock; /*!< card mutex */
53+
struct sdhc_host_props host_props; /*!< SDHC host properties */
54+
uint32_t ocr; /*!< Raw card OCR content */
55+
struct sd_switch_caps switch_caps; /*!< SD switch capabilities */
56+
uint32_t num_io; /*!< I/O function count. 0 for SD cards */
57+
uint32_t relative_addr; /*!< Card relative address */
58+
uint32_t block_count; /*!< Number of blocks in SD card */
59+
uint32_t block_size; /*!< SD block size */
60+
uint32_t sd_version; /*!< SD specification version */
61+
enum sd_timing_mode card_speed; /*!< Card timing mode */
62+
enum card_status status; /*!< Card status */
63+
enum card_type type; /*!< Card type */
64+
uint32_t flags; /*!< Card flags */
65+
uint8_t card_buffer[CONFIG_SD_BUFFER_SIZE]
66+
__aligned(CONFIG_SDHC_BUFFER_ALIGNMENT); /* Card internal buffer */
67+
};
68+
69+
/**
70+
* @brief Initialize an SD device
71+
*
72+
* Initializes an SD device to use with the subsystem. After this call,
73+
* only the SD card structure is required to access the card.
74+
* @param sdhc_dev SD host controller device for this card
75+
* @param card SD card structure for this card
76+
* @retval 0 card was initialized
77+
* @retval -ETIMEDOUT: card initialization timed out
78+
* @retval -EBUSY: card is busy
79+
* @retval -EIO: IO error while starting card
80+
*/
81+
int sd_init(const struct device *sdhc_dev, struct sd_card *card);
82+
83+
/**
84+
* @brief checks to see if card is present in the SD slot
85+
*
86+
* @param sdhc_dev SD host controller to check for card presence on
87+
* @retval true card is present
88+
* @retval false card is not present
89+
*/
90+
bool sd_is_card_present(const struct device *sdhc_dev);
91+
92+
93+
#ifdef __cplusplus
94+
}
95+
#endif
96+
97+
#endif /* ZEPHYR_INCLUDE_SD_SD_H_ */

include/zephyr/sd/sdmmc.h

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright 2022 NXP
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/**
8+
* @file
9+
* @brief Public API for SD memory card subsystem
10+
*/
11+
12+
#ifndef ZEPHYR_INCLUDE_SD_SDMMC_H_
13+
#define ZEPHYR_INCLUDE_SD_SDMMC_H_
14+
15+
#include <zephyr/device.h>
16+
#include <zephyr/drivers/sdhc.h>
17+
#include <zephyr/sd/sd.h>
18+
19+
#ifdef __cplusplus
20+
extern "C" {
21+
#endif
22+
23+
/**
24+
* @brief Write blocks to SD card from buffer
25+
*
26+
* Writes blocks from SD buffer to SD card. For best performance, this buffer
27+
* should be aligned to CONFIG_SDHC_BUFFER_ALIGNMENT
28+
* @param card SD card to write from
29+
* @param wbuf write buffer
30+
* @param start_block first block to write to
31+
* @param num_blocks number of blocks to write
32+
* @retval 0 write succeeded
33+
* @retval -EBUSY: card is busy with another request
34+
* @retval -ETIMEDOUT: card write timed out
35+
* @retval -EIO: I/O error
36+
*/
37+
int sdmmc_write_blocks(struct sd_card *card, const uint8_t *wbuf,
38+
uint32_t start_block, uint32_t num_blocks);
39+
40+
/**
41+
* @brief Read block from SD card to buffer
42+
*
43+
* Reads blocks into SD buffer from SD card. For best performance, this buffer
44+
* should be aligned to CONFIG_SDHC_BUFFER_ALIGNMENT
45+
* @param card SD card to read from
46+
* @param rbuf read buffer
47+
* @param start_block first block to read from
48+
* @param num_blocks number of blocks to read
49+
* @retval 0 read succeeded
50+
* @retval -EBUSY: card is busy with another request
51+
* @retval -ETIMEDOUT: card read timed out
52+
* @retval -EIO: I/O error
53+
*/
54+
int sdmmc_read_blocks(struct sd_card *card, uint8_t *rbuf,
55+
uint32_t start_block, uint32_t num_blocks);
56+
57+
/**
58+
* @brief Get I/O control data from SD card
59+
*
60+
* Sends I/O control commands to SD card.
61+
* @param card SD card
62+
* @param cmd I/O control command
63+
* @param buf I/O control buf
64+
* @retval 0 IOCTL command succeeded
65+
* @retval -ENOTSUP: IOCTL command not supported
66+
* @retval -EIO: I/O failure
67+
*/
68+
int sdmmc_ioctl(struct sd_card *card, uint8_t cmd, void *buf);
69+
70+
#ifdef __cplusplus
71+
}
72+
#endif
73+
74+
#endif /* ZEPHYR_INCLUDE_SD_SDMMC_H_ */

0 commit comments

Comments
 (0)