sdcard_spi.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016 Michel Rottleuthner <michel.rottleuthner@haw-hamburg.de>
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 
21 #ifndef SDCARD_SPI_H
22 #define SDCARD_SPI_H
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 #include "periph/spi.h"
29 #include "periph/gpio.h"
30 #include "stdbool.h"
31 
32 #define SD_HC_BLOCK_SIZE (512)
33 #define SDCARD_SPI_INIT_ERROR (-1)
34 #define SDCARD_SPI_OK (0)
36 #define SD_SIZE_OF_OID 2
37 #define SD_SIZE_OF_PNM 5
42 typedef struct {
43  uint8_t MID;
44  char OID[SD_SIZE_OF_OID];
45  char PNM[SD_SIZE_OF_PNM];
46  uint8_t PRV;
47  uint32_t PSN;
48  uint16_t MDT;
49  uint8_t CID_CRC;
50 } cid_t;
51 
56 typedef struct {
57  uint8_t CSD_STRUCTURE : 2;
58  uint8_t TAAC : 8;
59  uint8_t NSAC : 8;
60  uint8_t TRAN_SPEED : 8;
61  uint16_t CCC : 12;
62  uint8_t READ_BL_LEN : 4;
63  uint8_t READ_BL_PARTIAL : 1;
64  uint8_t WRITE_BLK_MISALIGN : 1;
65  uint8_t READ_BLK_MISALIGN : 1;
66  uint8_t DSR_IMP : 1;
67  uint16_t C_SIZE : 12;
68  uint8_t VDD_R_CURR_MIN : 3;
69  uint8_t VDD_R_CURR_MAX : 3;
70  uint8_t VDD_W_CURR_MIN : 3;
71  uint8_t VDD_W_CURR_MAX : 3;
72  uint8_t C_SIZE_MULT : 3;
73  uint8_t ERASE_BLK_EN : 1;
74  uint8_t SECTOR_SIZE : 7;
75  uint8_t WP_GRP_SIZE : 7;
76  uint8_t WP_GRP_ENABLE : 1;
77  uint8_t R2W_FACTOR : 3;
78  uint8_t WRITE_BL_LEN : 4;
79  uint8_t WRITE_BL_PARTIAL : 1;
80  uint8_t FILE_FORMAT_GRP : 1;
81  uint8_t COPY : 1;
82  uint8_t PERM_WRITE_PROTECT : 1;
83  uint8_t TMP_WRITE_PROTECT : 1;
84  uint8_t FILE_FORMAT : 2;
85  uint8_t CSD_CRC : 8;
86 } csd_v1_t;
87 
92 typedef struct {
93  uint8_t CSD_STRUCTURE : 2;
94  uint8_t TAAC : 8;
95  uint8_t NSAC : 8;
96  uint8_t TRAN_SPEED : 8;
97  uint16_t CCC : 12;
98  uint8_t READ_BL_LEN : 4;
99  uint8_t READ_BL_PARTIAL : 1;
100  uint8_t WRITE_BLK_MISALIGN : 1;
101  uint8_t READ_BLK_MISALIGN : 1;
102  uint8_t DSR_IMP : 1;
103  uint32_t C_SIZE : 22;
104  uint8_t ERASE_BLK_EN : 1;
105  uint8_t SECTOR_SIZE : 7;
106  uint8_t WP_GRP_SIZE : 7;
107  uint8_t WP_GRP_ENABLE : 1;
108  uint8_t R2W_FACTOR : 3;
109  uint8_t WRITE_BL_LEN : 4;
110  uint8_t WRITE_BL_PARTIAL : 1;
111  uint8_t FILE_FORMAT_GRP : 1;
112  uint8_t COPY : 1;
113  uint8_t PERM_WRITE_PROTECT : 1;
114  uint8_t TMP_WRITE_PROTECT : 1;
115  uint8_t FILE_FORMAT : 2;
116  uint8_t CSD_CRC : 8;
117 } csd_v2_t;
118 
122 typedef union {
125 } csd_t;
126 
130 typedef struct {
131  uint32_t SIZE_OF_PROTECTED_AREA : 32;
132  uint32_t SUS_ADDR : 22;
133  uint32_t VSC_AU_SIZE : 10;
134  uint16_t SD_CARD_TYPE : 16;
135  uint16_t ERASE_SIZE : 16;
136  uint8_t SPEED_CLASS : 8;
137  uint8_t PERFORMANCE_MOVE : 8;
138  uint8_t VIDEO_SPEED_CLASS : 8;
139  uint8_t ERASE_TIMEOUT : 6;
140  uint8_t ERASE_OFFSET : 2;
141  uint8_t UHS_SPEED_GRADE : 4;
142  uint8_t UHS_AU_SIZE : 4;
143  uint8_t AU_SIZE : 4;
144  uint8_t DAT_BUS_WIDTH : 2;
145  uint8_t SECURED_MODE : 1;
146 } sd_status_t;
147 
151 typedef enum {
156 } sd_version_t;
157 
161 typedef enum {
162  SD_RW_OK = 0,
170 
174 typedef struct {
176  gpio_t cs;
177  gpio_t clk;
178  gpio_t mosi;
179  gpio_t miso;
180  gpio_t power;
183 
187 typedef struct {
191  bool init_done;
196 } sdcard_spi_t;
197 
211 int sdcard_spi_init(sdcard_spi_t *card, const sdcard_spi_params_t *params);
212 
233 int sdcard_spi_read_blocks(sdcard_spi_t *card, int blockaddr, uint8_t *data, int blocksize,
234  int nblocks, sd_rw_response_t *state);
235 
255 int sdcard_spi_write_blocks(sdcard_spi_t *card, int blockaddr, const uint8_t *data, int blocksize,
256  int nblocks, sd_rw_response_t *state);
257 
265 uint64_t sdcard_spi_get_capacity(sdcard_spi_t *card);
266 
267 #ifdef __cplusplus
268 }
269 #endif
270 
271 #endif /* SDCARD_SPI_H */
272 
csd_v1_t::TRAN_SPEED
uint8_t TRAN_SPEED
see section 5.3.2 in SD-Spec v5.00
Definition: sdcard_spi.h:60
csd_v2_t::WRITE_BL_LEN
uint8_t WRITE_BL_LEN
see section 5.3.3 in SD-Spec v5.00
Definition: sdcard_spi.h:109
SD_RW_OK
@ SD_RW_OK
no error
Definition: sdcard_spi.h:162
csd_v1_t::VDD_R_CURR_MIN
uint8_t VDD_R_CURR_MIN
see section 5.3.2 in SD-Spec v5.00
Definition: sdcard_spi.h:68
sd_status_t::ERASE_SIZE
uint16_t ERASE_SIZE
see section 4.10.2.5 in SD-Spec v5.00
Definition: sdcard_spi.h:135
csd_v1_t::C_SIZE_MULT
uint8_t C_SIZE_MULT
see section 5.3.2 in SD-Spec v5.00
Definition: sdcard_spi.h:72
sd_status_t::UHS_AU_SIZE
uint8_t UHS_AU_SIZE
see section 4.10.2.9 in SD-Spec v5.00
Definition: sdcard_spi.h:142
csd_v2_t::FILE_FORMAT_GRP
uint8_t FILE_FORMAT_GRP
see section 5.3.3 in SD-Spec v5.00
Definition: sdcard_spi.h:111
cid_t::MID
uint8_t MID
Manufacturer ID.
Definition: sdcard_spi.h:43
SD_RW_TIMEOUT
@ SD_RW_TIMEOUT
cmd timed out (not-busy-state wasn't entered)
Definition: sdcard_spi.h:164
sdcard_spi_t::csd
csd_t csd
CSD register.
Definition: sdcard_spi.h:195
sd_status_t::AU_SIZE
uint8_t AU_SIZE
see section 4.10.2.4 in SD-Spec v5.00
Definition: sdcard_spi.h:143
csd_v1_t::DSR_IMP
uint8_t DSR_IMP
see section 5.3.2 in SD-Spec v5.00
Definition: sdcard_spi.h:66
csd_v2_t::COPY
uint8_t COPY
see section 5.3.3 in SD-Spec v5.00
Definition: sdcard_spi.h:112
sdcard_spi_read_blocks
int sdcard_spi_read_blocks(sdcard_spi_t *card, int blockaddr, uint8_t *data, int blocksize, int nblocks, sd_rw_response_t *state)
Reads data blocks (usually multiples of 512 Bytes) from card to buffer.
csd_v1_t::WRITE_BLK_MISALIGN
uint8_t WRITE_BLK_MISALIGN
see section 5.3.2 in SD-Spec v5.00
Definition: sdcard_spi.h:64
csd_v2_t::NSAC
uint8_t NSAC
see section 5.3.3 in SD-Spec v5.00
Definition: sdcard_spi.h:95
sd_status_t::SUS_ADDR
uint32_t SUS_ADDR
see section 4.10.2.12 in SD-Spec v5.00
Definition: sdcard_spi.h:132
sdcard_spi_t::csd_structure
int csd_structure
version of the CSD register structure
Definition: sdcard_spi.h:193
sdcard_spi_params_t::miso
gpio_t miso
pin connected to the DAT0 sd pad
Definition: sdcard_spi.h:179
sdcard_spi_t::params
sdcard_spi_params_t params
parameters for pin and spi config
Definition: sdcard_spi.h:188
csd_v1_t::READ_BL_LEN
uint8_t READ_BL_LEN
see section 5.3.2 in SD-Spec v5.00
Definition: sdcard_spi.h:62
spi.h
Low-level SPI peripheral driver interface definition.
sd_status_t::UHS_SPEED_GRADE
uint8_t UHS_SPEED_GRADE
see section 4.10.2.8 in SD-Spec v5.00
Definition: sdcard_spi.h:141
sdcard_spi_t::spi_clk
spi_clk_t spi_clk
active SPI clock speed
Definition: sdcard_spi.h:189
SD_RW_WRITE_ERROR
@ SD_RW_WRITE_ERROR
data-packet response indicates error
Definition: sdcard_spi.h:166
csd_t::v1
csd_v1_t v1
see section 5.3.2 in SD-Spec v5.00
Definition: sdcard_spi.h:123
csd_v1_t::CSD_CRC
uint8_t CSD_CRC
see section 5.3.2 in SD-Spec v5.00
Definition: sdcard_spi.h:85
sdcard_spi_params_t::spi_dev
spi_t spi_dev
SPI bus used.
Definition: sdcard_spi.h:175
csd_v1_t::CCC
uint16_t CCC
see section 5.3.2 in SD-Spec v5.00
Definition: sdcard_spi.h:61
sd_status_t::VIDEO_SPEED_CLASS
uint8_t VIDEO_SPEED_CLASS
see section 4.10.2.10 in SD-Spec v5.00
Definition: sdcard_spi.h:138
csd_v2_t::FILE_FORMAT
uint8_t FILE_FORMAT
see section 5.3.3 in SD-Spec v5.00
Definition: sdcard_spi.h:115
csd_v2_t::DSR_IMP
uint8_t DSR_IMP
see section 5.3.3 in SD-Spec v5.00
Definition: sdcard_spi.h:102
SD_V2
@ SD_V2
SD version 2
Definition: sdcard_spi.h:152
csd_v2_t::CCC
uint16_t CCC
see section 5.3.3 in SD-Spec v5.00
Definition: sdcard_spi.h:97
csd_v1_t::ERASE_BLK_EN
uint8_t ERASE_BLK_EN
see section 5.3.2 in SD-Spec v5.00
Definition: sdcard_spi.h:73
cid_t::CID_CRC
uint8_t CID_CRC
CRC7 checksum.
Definition: sdcard_spi.h:49
sd_status_t::SD_CARD_TYPE
uint16_t SD_CARD_TYPE
see section 4.10.2 in SD-Spec v5.00
Definition: sdcard_spi.h:134
csd_v1_t
CSD register with csd structure version 1.0 see section 5.3.2 in SD-Spec v5.00.
Definition: sdcard_spi.h:56
csd_v2_t::CSD_STRUCTURE
uint8_t CSD_STRUCTURE
see section 5.3.3 in SD-Spec v5.00
Definition: sdcard_spi.h:93
sd_status_t::ERASE_TIMEOUT
uint8_t ERASE_TIMEOUT
see section 4.10.2.6 in SD-Spec v5.00
Definition: sdcard_spi.h:139
csd_v1_t::CSD_STRUCTURE
uint8_t CSD_STRUCTURE
see section 5.3.2 in SD-Spec v5.00
Definition: sdcard_spi.h:57
sdcard_spi_t::cid
cid_t cid
CID register.
Definition: sdcard_spi.h:194
csd_v2_t::C_SIZE
uint32_t C_SIZE
see section 5.3.3 in SD-Spec v5.00
Definition: sdcard_spi.h:103
sdcard_spi_params_t::clk
gpio_t clk
pin connected to the CLK sd pad
Definition: sdcard_spi.h:177
csd_v1_t::NSAC
uint8_t NSAC
see section 5.3.2 in SD-Spec v5.00
Definition: sdcard_spi.h:59
csd_v1_t::READ_BLK_MISALIGN
uint8_t READ_BLK_MISALIGN
see section 5.3.2 in SD-Spec v5.00
Definition: sdcard_spi.h:65
csd_v1_t::FILE_FORMAT
uint8_t FILE_FORMAT
see section 5.3.2 in SD-Spec v5.00
Definition: sdcard_spi.h:84
sd_status_t
SD status register (see section 4.10.2 in SD-Spec v5.00)
Definition: sdcard_spi.h:130
csd_v1_t::WP_GRP_SIZE
uint8_t WP_GRP_SIZE
see section 5.3.2 in SD-Spec v5.00
Definition: sdcard_spi.h:75
cid_t::MDT
uint16_t MDT
Manufacturing date.
Definition: sdcard_spi.h:48
sd_status_t::SPEED_CLASS
uint8_t SPEED_CLASS
see section 4.10.2.2 in SD-Spec v5.00
Definition: sdcard_spi.h:136
sdcard_spi_get_capacity
uint64_t sdcard_spi_get_capacity(sdcard_spi_t *card)
Gets the capacity of the card.
sd_rw_response_t
sd_rw_response_t
sdcard_spi r/w-operation return values
Definition: sdcard_spi.h:161
csd_t::v2
csd_v2_t v2
see section 5.3.3 in SD-Spec v5.00
Definition: sdcard_spi.h:124
spi_clk_t
spi_clk_t
Available SPI clock speeds.
Definition: spi.h:173
sdcard_spi_params_t::cs
gpio_t cs
pin connected to the DAT3 sd pad
Definition: sdcard_spi.h:176
cid_t::PRV
uint8_t PRV
Product revision.
Definition: sdcard_spi.h:46
sd_status_t::SECURED_MODE
uint8_t SECURED_MODE
see section 4.10.2 in SD-Spec v5.00
Definition: sdcard_spi.h:145
csd_v1_t::TMP_WRITE_PROTECT
uint8_t TMP_WRITE_PROTECT
see section 5.3.2 in SD-Spec v5.00
Definition: sdcard_spi.h:83
SD_SIZE_OF_PNM
#define SD_SIZE_OF_PNM
PNM (product name field in CID reg)
Definition: sdcard_spi.h:37
csd_v2_t::ERASE_BLK_EN
uint8_t ERASE_BLK_EN
see section 5.3.3 in SD-Spec v5.00
Definition: sdcard_spi.h:104
sd_status_t::PERFORMANCE_MOVE
uint8_t PERFORMANCE_MOVE
see section 4.10.2.3 in SD-Spec v5.00
Definition: sdcard_spi.h:137
SD_UNKNOWN
@ SD_UNKNOWN
SD-version unknown.
Definition: sdcard_spi.h:155
cid_t::PSN
uint32_t PSN
Product serial number.
Definition: sdcard_spi.h:47
csd_t
CSD register (see section 5.3 in SD-Spec v5.00)
Definition: sdcard_spi.h:122
SD_RW_RX_TX_ERROR
@ SD_RW_RX_TX_ERROR
error while performing SPI read/write
Definition: sdcard_spi.h:165
csd_v2_t::SECTOR_SIZE
uint8_t SECTOR_SIZE
see section 5.3.3 in SD-Spec v5.00
Definition: sdcard_spi.h:105
sd_status_t::ERASE_OFFSET
uint8_t ERASE_OFFSET
see section 4.10.2.7 in SD-Spec v5.00
Definition: sdcard_spi.h:140
csd_v2_t::PERM_WRITE_PROTECT
uint8_t PERM_WRITE_PROTECT
see section 5.3.3 in SD-Spec v5.00
Definition: sdcard_spi.h:113
csd_v1_t::FILE_FORMAT_GRP
uint8_t FILE_FORMAT_GRP
see section 5.3.2 in SD-Spec v5.00
Definition: sdcard_spi.h:80
csd_v2_t::WRITE_BLK_MISALIGN
uint8_t WRITE_BLK_MISALIGN
see section 5.3.3 in SD-Spec v5.00
Definition: sdcard_spi.h:100
csd_v1_t::WRITE_BL_PARTIAL
uint8_t WRITE_BL_PARTIAL
see section 5.3.2 in SD-Spec v5.00
Definition: sdcard_spi.h:79
sdcard_spi_t::card_type
sd_version_t card_type
version of SD-card
Definition: sdcard_spi.h:192
csd_v2_t::WP_GRP_ENABLE
uint8_t WP_GRP_ENABLE
see section 5.3.3 in SD-Spec v5.00
Definition: sdcard_spi.h:107
sdcard_spi_write_blocks
int sdcard_spi_write_blocks(sdcard_spi_t *card, int blockaddr, const uint8_t *data, int blocksize, int nblocks, sd_rw_response_t *state)
Writes data blocks (usually multiples of 512 Bytes) from buffer to card.
sd_status_t::SIZE_OF_PROTECTED_AREA
uint32_t SIZE_OF_PROTECTED_AREA
see section 4.10.2 in SD-Spec v5.00
Definition: sdcard_spi.h:131
csd_v1_t::SECTOR_SIZE
uint8_t SECTOR_SIZE
see section 5.3.2 in SD-Spec v5.00
Definition: sdcard_spi.h:74
csd_v2_t::R2W_FACTOR
uint8_t R2W_FACTOR
see section 5.3.3 in SD-Spec v5.00
Definition: sdcard_spi.h:108
SD_SIZE_OF_OID
#define SD_SIZE_OF_OID
OID (OEM/application ID field in CID reg)
Definition: sdcard_spi.h:36
csd_v2_t::READ_BLK_MISALIGN
uint8_t READ_BLK_MISALIGN
see section 5.3.3 in SD-Spec v5.00
Definition: sdcard_spi.h:101
csd_v2_t::TRAN_SPEED
uint8_t TRAN_SPEED
see section 5.3.3 in SD-Spec v5.00
Definition: sdcard_spi.h:96
sdcard_spi_params_t::mosi
gpio_t mosi
pin connected to the CMD sd pad
Definition: sdcard_spi.h:178
sd_status_t::VSC_AU_SIZE
uint32_t VSC_AU_SIZE
see section 4.10.2.11 in SD-Spec v5.00
Definition: sdcard_spi.h:133
csd_v1_t::C_SIZE
uint16_t C_SIZE
see section 5.3.2 in SD-Spec v5.00
Definition: sdcard_spi.h:67
sd_status_t::DAT_BUS_WIDTH
uint8_t DAT_BUS_WIDTH
see section 4.10.2 in SD-Spec v5.00
Definition: sdcard_spi.h:144
gpio.h
Low-level GPIO peripheral driver interface definitions.
sdcard_spi_params_t::power_act_high
bool power_act_high
true if card power is enabled by 'power'-pin HIGH
Definition: sdcard_spi.h:181
SD_RW_NOT_SUPPORTED
@ SD_RW_NOT_SUPPORTED
operation not supported on used card
Definition: sdcard_spi.h:168
sdcard_spi_t::use_block_addr
bool use_block_addr
true if block addressing (vs.
Definition: sdcard_spi.h:190
sdcard_spi_params_t::power
gpio_t power
pin that controls sd power circuit
Definition: sdcard_spi.h:180
csd_v2_t::TAAC
uint8_t TAAC
see section 5.3.3 in SD-Spec v5.00
Definition: sdcard_spi.h:94
csd_v1_t::VDD_R_CURR_MAX
uint8_t VDD_R_CURR_MAX
see section 5.3.2 in SD-Spec v5.00
Definition: sdcard_spi.h:69
csd_v1_t::PERM_WRITE_PROTECT
uint8_t PERM_WRITE_PROTECT
see section 5.3.2 in SD-Spec v5.00
Definition: sdcard_spi.h:82
csd_v1_t::R2W_FACTOR
uint8_t R2W_FACTOR
see section 5.3.2 in SD-Spec v5.00
Definition: sdcard_spi.h:77
csd_v1_t::COPY
uint8_t COPY
see section 5.3.2 in SD-Spec v5.00
Definition: sdcard_spi.h:81
sdcard_spi_init
int sdcard_spi_init(sdcard_spi_t *card, const sdcard_spi_params_t *params)
Initializes the sd-card with the given parameters in sdcard_spi_t structure.
csd_v1_t::VDD_W_CURR_MAX
uint8_t VDD_W_CURR_MAX
see section 5.3.2 in SD-Spec v5.00
Definition: sdcard_spi.h:71
csd_v1_t::READ_BL_PARTIAL
uint8_t READ_BL_PARTIAL
see section 5.3.2 in SD-Spec v5.00
Definition: sdcard_spi.h:63
SD_V1
@ SD_V1
SD version 1
Definition: sdcard_spi.h:153
csd_v2_t::READ_BL_LEN
uint8_t READ_BL_LEN
see section 5.3.3 in SD-Spec v5.00
Definition: sdcard_spi.h:98
csd_v1_t::WP_GRP_ENABLE
uint8_t WP_GRP_ENABLE
see section 5.3.2 in SD-Spec v5.00
Definition: sdcard_spi.h:76
csd_v2_t::WP_GRP_SIZE
uint8_t WP_GRP_SIZE
see section 5.3.3 in SD-Spec v5.00
Definition: sdcard_spi.h:106
csd_v2_t::CSD_CRC
uint8_t CSD_CRC
see section 5.3.3 in SD-Spec v5.00
Definition: sdcard_spi.h:116
SD_RW_NO_TOKEN
@ SD_RW_NO_TOKEN
no token was received (on block read)
Definition: sdcard_spi.h:163
cid_t
CID register see section 5.2 in SD-Spec v5.00.
Definition: sdcard_spi.h:42
sdcard_spi_params_t
sdcard_spi device params
Definition: sdcard_spi.h:174
csd_v2_t::READ_BL_PARTIAL
uint8_t READ_BL_PARTIAL
see section 5.3.3 in SD-Spec v5.00
Definition: sdcard_spi.h:99
csd_v1_t::WRITE_BL_LEN
uint8_t WRITE_BL_LEN
see section 5.3.2 in SD-Spec v5.00
Definition: sdcard_spi.h:78
csd_v2_t::TMP_WRITE_PROTECT
uint8_t TMP_WRITE_PROTECT
see section 5.3.3 in SD-Spec v5.00
Definition: sdcard_spi.h:114
sdcard_spi_t
Device descriptor for sdcard_spi.
Definition: sdcard_spi.h:187
csd_v1_t::TAAC
uint8_t TAAC
see section 5.3.2 in SD-Spec v5.00
Definition: sdcard_spi.h:58
csd_v2_t::WRITE_BL_PARTIAL
uint8_t WRITE_BL_PARTIAL
see section 5.3.3 in SD-Spec v5.00
Definition: sdcard_spi.h:110
sd_version_t
sd_version_t
version type of SD-card
Definition: sdcard_spi.h:151
spi_t
unsigned int spi_t
Default type for SPI devices.
Definition: spi.h:118
sdcard_spi_t::init_done
bool init_done
set to true once the init procedure completed successfully
Definition: sdcard_spi.h:191
csd_v1_t::VDD_W_CURR_MIN
uint8_t VDD_W_CURR_MIN
see section 5.3.2 in SD-Spec v5.00
Definition: sdcard_spi.h:70
MMC_V3
@ MMC_V3
MMC version 3.
Definition: sdcard_spi.h:154
csd_v2_t
CSD register with csd structure version 2.0 see section 5.3.3 in SD-Spec v5.00.
Definition: sdcard_spi.h:92
SD_RW_CRC_MISMATCH
@ SD_RW_CRC_MISMATCH
CRC-mismatch of received data.
Definition: sdcard_spi.h:167