The friendly Operating System for the Internet of Things
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 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 } typedef cid_t;
51 
56 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 } typedef csd_v1_t;
87 
92 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 } typedef csd_v2_t;
118 
122 union {
125 } typedef csd_t;
126 
130 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 } typedef 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 struct {
191  bool init_done;
196 } typedef 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, char *data, int blocksize,
234  int nblocks, sd_rw_response_t *state);
235 
255 int sdcard_spi_write_blocks(sdcard_spi_t *card, int blockaddr, char *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 
int sdcard_spi_read_blocks(sdcard_spi_t *card, int blockaddr, char *data, int blocksize, int nblocks, sd_rw_response_t *state)
Reads data blocks (usually multiples of 512 Bytes) from card to buffer.
MMC version 3.
Definition: sdcard_spi.h:154
uint8_t MID
Manufacturer ID.
Definition: sdcard_spi.h:43
no error
Definition: sdcard_spi.h:162
SD version 1.
Definition: sdcard_spi.h:153
bool init_done
set to true once the init procedure completed sucessfully
Definition: sdcard_spi.h:191
gpio_t miso
pin connected to the DAT0 sd pad
Definition: sdcard_spi.h:179
SD status register (see section 4.10.2 in SD-Spec v5.00)
Definition: sdcard_spi.h:130
sdcard_spi_params_t params
parameters for pin and spi config
Definition: sdcard_spi.h:188
uint16_t MDT
Manufacturing date.
Definition: sdcard_spi.h:48
Low-level GPIO peripheral driver interface definitions.
SD version 2.
Definition: sdcard_spi.h:152
gpio_t cs
pin connected to the DAT3 sd pad
Definition: sdcard_spi.h:176
gpio_t clk
pin connected to the CLK sd pad
Definition: sdcard_spi.h:177
sd_version_t
version type of SD-card
Definition: sdcard_spi.h:151
spi_t spi_dev
SPI bus used.
Definition: sdcard_spi.h:175
Low-level SPI peripheral driver interface definition.
csd_v1_t v1
see section 5.3.2 in SD-Spec v5.00
Definition: sdcard_spi.h:123
sd_version_t card_type
version of SD-card
Definition: sdcard_spi.h:192
sd_rw_response_t
sdcard_spi r/w-operation return values
Definition: sdcard_spi.h:161
data-packet response indicates error
Definition: sdcard_spi.h:166
sdcard_spi device params
Definition: sdcard_spi.h:174
cmd timed out (not-busy-state wasn't entered)
Definition: sdcard_spi.h:164
#define SD_SIZE_OF_PNM
PNM (product name field in CID reg)
Definition: sdcard_spi.h:37
uint64_t sdcard_spi_get_capacity(sdcard_spi_t *card)
Gets the capacity of the card.
csd_v2_t v2
see section 5.3.3 in SD-Spec v5.00
Definition: sdcard_spi.h:124
CID register see section 5.2 in SD-Spec v5.00.
Definition: sdcard_spi.h:42
spi_clk_t spi_clk
active SPI clock speed
Definition: sdcard_spi.h:189
operation not supported on used card
Definition: sdcard_spi.h:168
CSD register with csd structure version 2.0 see section 5.3.3 in SD-Spec v5.00.
Definition: sdcard_spi.h:92
int csd_structure
version of the CSD register structure
Definition: sdcard_spi.h:193
uint32_t PSN
Product serial number.
Definition: sdcard_spi.h:47
int sdcard_spi_write_blocks(sdcard_spi_t *card, int blockaddr, char *data, int blocksize, int nblocks, sd_rw_response_t *state)
Writes data blocks (usually multiples of 512 Bytes) from buffer to card.
uint8_t CID_CRC
CRC7 checksum.
Definition: sdcard_spi.h:49
CSD register (see section 5.3 in SD-Spec v5.00)
Definition: sdcard_spi.h:122
uint8_t PRV
Product revision.
Definition: sdcard_spi.h:46
gpio_t power
pin that controls sd power circuit
Definition: sdcard_spi.h:180
cid_t cid
CID register.
Definition: sdcard_spi.h:194
SD-version unknown.
Definition: sdcard_spi.h:155
#define SD_SIZE_OF_OID
OID (OEM/application ID field in CID reg)
Definition: sdcard_spi.h:36
gpio_t mosi
pin connected to the CMD sd pad
Definition: sdcard_spi.h:178
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 register with csd structure version 1.0 see section 5.3.2 in SD-Spec v5.00.
Definition: sdcard_spi.h:56
no token was received (on block read)
Definition: sdcard_spi.h:163
bool power_act_high
true if card power is enabled by 'power'-pin HIGH
Definition: sdcard_spi.h:181
bool use_block_addr
true if block adressing (vs.
Definition: sdcard_spi.h:190
csd_t csd
CSD register.
Definition: sdcard_spi.h:195
error while performing SPI read/write
Definition: sdcard_spi.h:165
Device descriptor for sdcard_spi.
Definition: sdcard_spi.h:187
unsigned int spi_t
Default type for SPI devices.
Definition: spi.h:105
CRC-mismatch of received data.
Definition: sdcard_spi.h:167