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 
9 #pragma once
10 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32 #include <stdint.h>
33 #include <stdbool.h>
34 #include "periph/gpio.h"
35 #include "mutex.h"
36 
40 typedef struct {
41  Sdhc *dev;
42  gpio_t cd;
43  gpio_t wp;
46  uint32_t sectors;
47  uint32_t clock;
48  uint16_t rca;
49  uint16_t error;
50  uint8_t type;
51  uint8_t version;
52  uint8_t bus_width;
53  bool high_speed;
54  bool need_init;
55 } sdhc_state_t;
56 
59 #define CARD_TYPE_UNKNOWN (0)
60 #define CARD_TYPE_SD (1 << 0)
61 #define CARD_TYPE_MMC (1 << 1)
62 #define CARD_TYPE_SDIO (1 << 2)
63 #define CARD_TYPE_HC (1 << 3)
65 #define CARD_TYPE_SD_COMBO (CARD_TYPE_SD | CARD_TYPE_SDIO)
70 #define CARD_VER_UNKNOWN (0)
71 #define CARD_VER_SD_1_0 (0x10)
72 #define CARD_VER_SD_1_10 (0x1A)
73 #define CARD_VER_SD_2_0 (0X20)
74 #define CARD_VER_SD_3_0 (0X30)
75 #define CARD_VER_MMC_1_2 (0x12)
76 #define CARD_VER_MMC_1_4 (0x14)
77 #define CARD_VER_MMC_2_2 (0x22)
78 #define CARD_VER_MMC_3 (0x30)
79 #define CARD_VER_MMC_4 (0x40)
84 #define MCI_RESP_PRESENT (1ul << 8)
85 #define MCI_RESP_136 (1ul << 11)
86 #define MCI_RESP_CRC (1ul << 12)
87 #define MCI_RESP_BUSY (1ul << 13)
88 #define MCI_CMD_OPENDRAIN (1ul << 14)
89 #define MCI_CMD_WRITE (1ul << 15)
90 #define MCI_CMD_SDIO_BYTE (1ul << 16)
91 #define MCI_CMD_SDIO_BLOCK (1ul << 17)
92 #define MCI_CMD_STREAM (1ul << 18)
93 #define MCI_CMD_SINGLE_BLOCK (1ul << 19)
94 #define MCI_CMD_MULTI_BLOCK (1ul << 20)
98 #define SD_MMC_BLOCK_SIZE 512
99 #define SDHC_SLOW_CLOCK_HZ 400000
100 #define SDHC_FAST_CLOCK_HZ 25000000
110 
121 bool sdhc_send_cmd(sdhc_state_t *state, uint32_t cmd, uint32_t arg);
122 
135 int sdhc_read_blocks(sdhc_state_t *state, uint32_t block, void *dst, uint16_t num);
136 
149 int sdhc_write_blocks(sdhc_state_t *state, uint32_t block, const void *src,
150  uint16_t num);
151 
163 int sdhc_erase_blocks(sdhc_state_t *state, uint32_t block, uint16_t num);
164 
165 #ifdef __cplusplus
166 }
167 #endif
168 
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:40
uint16_t error
Last error state
Definition: sdhc.h:49
bool need_init
Card installed but not initialized if true.
Definition: sdhc.h:54
uint8_t type
Type of Card
Definition: sdhc.h:50
uint8_t bus_width
Acceptable Bus Width (1 or 4)
Definition: sdhc.h:52
uint8_t version
Version of Card
Definition: sdhc.h:51
gpio_t wp
Write Protect pin
Definition: sdhc.h:43
uint32_t sectors
Capacity in bytes
Definition: sdhc.h:46
gpio_t cd
Card detect pin
Definition: sdhc.h:42
mutex_t sync
ISR mutex
Definition: sdhc.h:45
uint16_t rca
Relative Card Address
Definition: sdhc.h:48
uint32_t clock
Accepted Cloc Rate in Hz
Definition: sdhc.h:47
Sdhc * dev
SDHC instance
Definition: sdhc.h:41
mutex_t lock
Ensure thread-safe access
Definition: sdhc.h:44
bool high_speed
Turbo mode
Definition: sdhc.h:53