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