sdhc.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2018 Alkgrove
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser
5  * General Public License v2.1. See the file LICENSE in the top level
6  * directory for more details.
7  */
8 
26 #ifndef SDHC_H
27 #define SDHC_H
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 #include <stdint.h>
34 #include <stdbool.h>
35 #include "periph/gpio.h"
36 #include "mutex.h"
37 
41 typedef struct {
42  Sdhc *dev;
43  gpio_t cd;
44  gpio_t wp;
47  uint32_t sectors;
48  uint32_t clock;
49  uint16_t rca;
50  uint16_t error;
51  uint8_t type;
52  uint8_t version;
53  uint8_t bus_width;
54  bool high_speed;
55  bool need_init;
56 } sdhc_state_t;
57 
60 #define CARD_TYPE_UNKNOWN (0)
61 #define CARD_TYPE_SD (1 << 0)
62 #define CARD_TYPE_MMC (1 << 1)
63 #define CARD_TYPE_SDIO (1 << 2)
64 #define CARD_TYPE_HC (1 << 3)
66 #define CARD_TYPE_SD_COMBO (CARD_TYPE_SD | CARD_TYPE_SDIO)
71 #define CARD_VER_UNKNOWN (0)
72 #define CARD_VER_SD_1_0 (0x10)
73 #define CARD_VER_SD_1_10 (0x1A)
74 #define CARD_VER_SD_2_0 (0X20)
75 #define CARD_VER_SD_3_0 (0X30)
76 #define CARD_VER_MMC_1_2 (0x12)
77 #define CARD_VER_MMC_1_4 (0x14)
78 #define CARD_VER_MMC_2_2 (0x22)
79 #define CARD_VER_MMC_3 (0x30)
80 #define CARD_VER_MMC_4 (0x40)
85 #define MCI_RESP_PRESENT (1ul << 8)
86 #define MCI_RESP_136 (1ul << 11)
87 #define MCI_RESP_CRC (1ul << 12)
88 #define MCI_RESP_BUSY (1ul << 13)
89 #define MCI_CMD_OPENDRAIN (1ul << 14)
90 #define MCI_CMD_WRITE (1ul << 15)
91 #define MCI_CMD_SDIO_BYTE (1ul << 16)
92 #define MCI_CMD_SDIO_BLOCK (1ul << 17)
93 #define MCI_CMD_STREAM (1ul << 18)
94 #define MCI_CMD_SINGLE_BLOCK (1ul << 19)
95 #define MCI_CMD_MULTI_BLOCK (1ul << 20)
99 #define SD_MMC_BLOCK_SIZE 512
100 #define SDHC_SLOW_CLOCK_HZ 400000
101 #define SDHC_FAST_CLOCK_HZ 25000000
111 
122 bool sdhc_send_cmd(sdhc_state_t *state, uint32_t cmd, uint32_t arg);
123 
136 int sdhc_read_blocks(sdhc_state_t *state, uint32_t block, void *dst, uint16_t num);
137 
150 int sdhc_write_blocks(sdhc_state_t *state, uint32_t block, const void *src,
151  uint16_t num);
152 
164 int sdhc_erase_blocks(sdhc_state_t *state, uint32_t block, uint16_t num);
165 
166 #ifdef __cplusplus
167 }
168 #endif
169 
170 #endif /* SDHC_H */
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:146
SD Card driver context.
Definition: sdhc.h:41
uint16_t error
Last error state
Definition: sdhc.h:50
bool need_init
Card installed but not initialized if true.
Definition: sdhc.h:55
uint8_t type
Type of Card
Definition: sdhc.h:51
uint8_t bus_width
Acceptable Bus Width (1 or 4)
Definition: sdhc.h:53
uint8_t version
Version of Card
Definition: sdhc.h:52
gpio_t wp
Write Protect pin
Definition: sdhc.h:44
uint32_t sectors
Capacity in bytes
Definition: sdhc.h:47
gpio_t cd
Card detect pin
Definition: sdhc.h:43
mutex_t sync
ISR mutex
Definition: sdhc.h:46
uint16_t rca
Relative Card Address
Definition: sdhc.h:49
uint32_t clock
Accepted Cloc Rate in Hz
Definition: sdhc.h:48
Sdhc * dev
SDHC instance
Definition: sdhc.h:42
mutex_t lock
Ensure thread-safe access
Definition: sdhc.h:45
bool high_speed
Turbo mode
Definition: sdhc.h:54