sdhc.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2018 Alkgrove
3  * SPDX-License-Identifier: LGPL-2.1-only
4  */
5 
6 #pragma once
7 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
29 #include <stdint.h>
30 #include <stdbool.h>
31 #include "periph/gpio.h"
32 #include "mutex.h"
33 
37 typedef struct {
38  Sdhc *dev;
39  gpio_t cd;
40  gpio_t wp;
43  uint32_t sectors;
44  uint32_t clock;
45  uint16_t rca;
46  uint16_t error;
47  uint8_t type;
48  uint8_t version;
49  uint8_t bus_width;
50  bool high_speed;
51  bool need_init;
52 } sdhc_state_t;
53 
56 #define CARD_TYPE_UNKNOWN (0)
57 #define CARD_TYPE_SD (1 << 0)
58 #define CARD_TYPE_MMC (1 << 1)
59 #define CARD_TYPE_SDIO (1 << 2)
60 #define CARD_TYPE_HC (1 << 3)
62 #define CARD_TYPE_SD_COMBO (CARD_TYPE_SD | CARD_TYPE_SDIO)
67 #define CARD_VER_UNKNOWN (0)
68 #define CARD_VER_SD_1_0 (0x10)
69 #define CARD_VER_SD_1_10 (0x1A)
70 #define CARD_VER_SD_2_0 (0X20)
71 #define CARD_VER_SD_3_0 (0X30)
72 #define CARD_VER_MMC_1_2 (0x12)
73 #define CARD_VER_MMC_1_4 (0x14)
74 #define CARD_VER_MMC_2_2 (0x22)
75 #define CARD_VER_MMC_3 (0x30)
76 #define CARD_VER_MMC_4 (0x40)
81 #define MCI_RESP_PRESENT (1ul << 8)
82 #define MCI_RESP_136 (1ul << 11)
83 #define MCI_RESP_CRC (1ul << 12)
84 #define MCI_RESP_BUSY (1ul << 13)
85 #define MCI_CMD_OPENDRAIN (1ul << 14)
86 #define MCI_CMD_WRITE (1ul << 15)
87 #define MCI_CMD_SDIO_BYTE (1ul << 16)
88 #define MCI_CMD_SDIO_BLOCK (1ul << 17)
89 #define MCI_CMD_STREAM (1ul << 18)
90 #define MCI_CMD_SINGLE_BLOCK (1ul << 19)
91 #define MCI_CMD_MULTI_BLOCK (1ul << 20)
95 #define SD_MMC_BLOCK_SIZE 512
96 #define SDHC_SLOW_CLOCK_HZ 400000
97 #define SDHC_FAST_CLOCK_HZ 25000000
107 
118 bool sdhc_send_cmd(sdhc_state_t *state, uint32_t cmd, uint32_t arg);
119 
132 int sdhc_read_blocks(sdhc_state_t *state, uint32_t block, void *dst, uint16_t num);
133 
146 int sdhc_write_blocks(sdhc_state_t *state, uint32_t block, const void *src,
147  uint16_t num);
148 
160 int sdhc_erase_blocks(sdhc_state_t *state, uint32_t block, uint16_t num);
161 
162 #ifdef __cplusplus
163 }
164 #endif
165 
Low-level GPIO peripheral driver interface definitions.
int sdhc_write_blocks(sdhc_state_t *state, uint32_t block, const void *src, uint16_t num)
Write memory to SD card blocks.
bool sdhc_send_cmd(sdhc_state_t *state, uint32_t cmd, uint32_t arg)
Send a command to the SD card.
int sdhc_erase_blocks(sdhc_state_t *state, uint32_t block, uint16_t num)
Erase memory from SD card blocks.
int sdhc_init(sdhc_state_t *state)
Initialize the SD host controller.
int sdhc_read_blocks(sdhc_state_t *state, uint32_t block, void *dst, uint16_t num)
Read blocks from the SD card into memory.
Mutex for thread synchronization.
Mutex structure.
Definition: mutex.h:39
SD Card driver context.
Definition: sdhc.h:37
uint16_t error
Last error state
Definition: sdhc.h:46
bool need_init
Card installed but not initialized if true.
Definition: sdhc.h:51
uint8_t type
Type of Card
Definition: sdhc.h:47
uint8_t bus_width
Acceptable Bus Width (1 or 4)
Definition: sdhc.h:49
uint8_t version
Version of Card
Definition: sdhc.h:48
gpio_t wp
Write Protect pin
Definition: sdhc.h:40
uint32_t sectors
Capacity in bytes
Definition: sdhc.h:43
gpio_t cd
Card detect pin
Definition: sdhc.h:39
mutex_t sync
ISR mutex
Definition: sdhc.h:42
uint16_t rca
Relative Card Address
Definition: sdhc.h:45
uint32_t clock
Accepted Cloc Rate in Hz
Definition: sdhc.h:44
Sdhc * dev
SDHC instance
Definition: sdhc.h:38
mutex_t lock
Ensure thread-safe access
Definition: sdhc.h:41
bool high_speed
Turbo mode
Definition: sdhc.h:50