1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
|
/**
* @file sdcard.c
* Provides a basic library for accessing an SD card
*
* Copyright (C) 2018 Clyne Sullivan
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <clock.h>
#include <gpio.h>
#include <heap.h>
#include <sdcard.h>
#include <string.h>
#define SCK GPIO_PORT(A, 5)
#define SO GPIO_PORT(A, 6)
#define SI GPIO_PORT(A, 7)
#define CS GPIO_PORT(B, 6)
typedef struct {
uint8_t cmd;
uint32_t arg;
uint8_t crc;
} __attribute__ ((packed)) sdcmd_t;
typedef struct {
uint8_t zero :1;
uint8_t param :1;
uint8_t address :1;
uint8_t erase :1;
uint8_t crc :1;
uint8_t badcmd :1;
uint8_t ereset :1;
uint8_t idle :1;
} __attribute__ ((packed)) r1_t;
void flash_out(uint8_t byte)
{
for (int i = 0; i < 8; i++) {
gpio_dout(SI, byte & (1 << (7 - i)));
gpio_dout(SCK, 1);
gpio_dout(SCK, 0);
}
}
uint8_t flash_in(void)
{
uint8_t byte = 0;
for (int i = 0; i < 8; i++) {
if (gpio_din(SO))
byte |= 1 << (7 - i);
gpio_dout(SCK, 1);
gpio_dout(SCK, 0);
}
return byte;
}
void sd_delay(void)
{
gpio_dout(SI, 1);
uint8_t so;
int i = 0;
do {
so = flash_in();
if (i < 8)
i++;
} while (i < 8 || so != 0xFF);
}
uint8_t sd_send_cmd(uint8_t cmd, uint32_t arg)
{
sdcmd_t cmdp;
cmdp.cmd = (cmd & 0x3F) | 0x40;
cmdp.arg = arg;
sd_delay();
flash_out(cmdp.cmd);
flash_out(cmdp.arg >> 24);
flash_out(cmdp.arg >> 16);
flash_out(cmdp.arg >> 8);
flash_out(cmdp.arg);
flash_out(cmd == 8 ? 0x87 : 0x95);
// wait for a zero
gpio_dout(SI, 1);
uint8_t r1;
do {
r1 = flash_in();
} while (r1 & 0x80);
return r1;
}
void sd_init(void)
{
gpio_mode(SCK, OUTPUT);
gpio_mode(SI, OUTPUT);
gpio_mode(CS, OUTPUT);
gpio_mode(SO, OUTPUT);
gpio_speed(SCK, VERYHIGH);
gpio_speed(SI, VERYHIGH);
gpio_speed(SO, VERYHIGH);
gpio_speed(CS, VERYHIGH);
gpio_pupd(SCK, PULLUP);
gpio_pupd(SI, PULLUP);
gpio_pupd(SO, PULLUP);
gpio_pupd(CS, PULLUP);
gpio_dout(SO, 0);
gpio_mode(SO, INPUT);
gpio_dout(CS, 1);
gpio_dout(SCK, 0);
gpio_dout(SI, 1);
// init? cs and si are high
delay(10);
sd_delay();
// pull cs low, send cmd0
gpio_dout(CS, 0);
uint8_t resp = sd_send_cmd(0, 0);
sd_delay();
gpio_dout(CS, 1);
if (resp != 0x01)
while (1);
// do cmd8
delay(10);
gpio_dout(CS, 0);
resp = sd_send_cmd(8, 0x1AA);
uint8_t ocr[4];
for (int i = 0; i < 4; i++)
ocr[i] = flash_in();
sd_delay();
gpio_dout(CS, 1);
(void)ocr;
// initialize
do {
// cmd55
gpio_dout(CS, 0);
resp = sd_send_cmd(55, 0);
sd_delay();
gpio_dout(CS, 1);
delay(10);
// acmd41
gpio_dout(CS, 0);
resp = sd_send_cmd(41, 0x40000000);
sd_delay();
gpio_dout(CS, 1);
} while (resp != 0x00);
// set block length
gpio_dout(CS, 0);
resp = sd_send_cmd(16, 512);
sd_delay();
gpio_dout(CS, 1);
}
uint8_t *sd_read_block(uint8_t *buf, uint32_t lba)
{
if (buf == 0)
return 0;
// send command
gpio_dout(CS, 0);
if (sd_send_cmd(17, lba) != 0)
return 0;
// wait for block
gpio_dout(SI, 1);
uint8_t byte;
do byte = flash_in();
while (byte == 0xFF);
if (byte != 0xFE)
return 0;
// get block
uint32_t i = 0;
for (i = 0; i < 512; i++)
buf[i] = flash_in();
sd_delay();
gpio_dout(CS, 1);
return buf;
}
uint8_t *sd_read(uint8_t *buf, uint32_t lba, uint32_t count)
{
if (buf == 0)
return 0;
uint8_t *block_buf = malloc(512);
uint32_t i = 0;
while (count > 0) {
sd_read_block(block_buf, lba);
memcpy(buf + i, block_buf, (count < 512) ? count : 512);
if (count < 512)
count = 0;
else
count -= 512;
i += 512;
lba++;
}
free(block_buf);
return buf;
}
|