Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
CTCaer
GitHub Repository: CTCaer/hekate
Path: blob/master/bdk/storage/sd.c
1476 views
1
/*
2
* Copyright (c) 2018 naehrwert
3
* Copyright (c) 2018-2023 CTCaer
4
*
5
* This program is free software; you can redistribute it and/or modify it
6
* under the terms and conditions of the GNU General Public License,
7
* version 2, as published by the Free Software Foundation.
8
*
9
* This program is distributed in the hope it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12
* more details.
13
*
14
* You should have received a copy of the GNU General Public License
15
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16
*/
17
18
#include <storage/sd.h>
19
#include <storage/sdmmc.h>
20
#include <storage/sdmmc_driver.h>
21
#include <gfx_utils.h>
22
#include <libs/fatfs/ff.h>
23
#include <mem/heap.h>
24
25
#ifndef BDK_SDMMC_UHS_DDR200_SUPPORT
26
#define SD_DEFAULT_SPEED SD_UHS_SDR104
27
#else
28
#define SD_DEFAULT_SPEED SD_UHS_DDR208
29
#endif
30
31
static bool sd_mounted = false;
32
static bool sd_init_done = false;
33
static bool insertion_event = false;
34
static u16 sd_errors[3] = { 0 }; // Init and Read/Write errors.
35
static u32 sd_mode = SD_DEFAULT_SPEED;
36
37
38
sdmmc_t sd_sdmmc;
39
sdmmc_storage_t sd_storage;
40
FATFS sd_fs;
41
42
void sd_error_count_increment(u8 type)
43
{
44
switch (type)
45
{
46
case SD_ERROR_INIT_FAIL:
47
sd_errors[0]++;
48
break;
49
case SD_ERROR_RW_FAIL:
50
sd_errors[1]++;
51
break;
52
case SD_ERROR_RW_RETRY:
53
sd_errors[2]++;
54
break;
55
}
56
}
57
58
u16 *sd_get_error_count()
59
{
60
return sd_errors;
61
}
62
63
bool sd_get_card_removed()
64
{
65
if (insertion_event && !sdmmc_get_sd_inserted())
66
return true;
67
68
return false;
69
}
70
71
bool sd_get_card_initialized()
72
{
73
return sd_init_done;
74
}
75
76
bool sd_get_card_mounted()
77
{
78
return sd_mounted;
79
}
80
81
u32 sd_get_mode()
82
{
83
return sd_mode;
84
}
85
86
int sd_init_retry(bool power_cycle)
87
{
88
u32 bus_width = SDMMC_BUS_WIDTH_4;
89
#ifndef BDK_SDMMC_UHS_DDR200_SUPPORT
90
u32 type = SDHCI_TIMING_UHS_SDR104;
91
#else
92
u32 type = SDHCI_TIMING_UHS_DDR200;
93
#endif
94
95
// Power cycle SD card.
96
if (power_cycle)
97
{
98
sd_mode--;
99
sdmmc_storage_end(&sd_storage);
100
}
101
102
// Get init parameters.
103
switch (sd_mode)
104
{
105
case SD_INIT_FAIL: // Reset to max.
106
return 0;
107
108
case SD_1BIT_HS25:
109
bus_width = SDMMC_BUS_WIDTH_1;
110
type = SDHCI_TIMING_SD_HS25;
111
break;
112
113
case SD_4BIT_HS25:
114
type = SDHCI_TIMING_SD_HS25;
115
break;
116
117
case SD_UHS_SDR82:
118
type = SDHCI_TIMING_UHS_SDR82;
119
break;
120
121
case SD_UHS_SDR104:
122
type = SDHCI_TIMING_UHS_SDR104;
123
break;
124
125
#ifdef BDK_SDMMC_UHS_DDR200_SUPPORT
126
case SD_UHS_DDR208:
127
type = SDHCI_TIMING_UHS_DDR200;
128
break;
129
#endif
130
131
default:
132
sd_mode = SD_DEFAULT_SPEED;
133
break;
134
}
135
136
int res = sdmmc_storage_init_sd(&sd_storage, &sd_sdmmc, bus_width, type);
137
if (res)
138
{
139
sd_init_done = true;
140
insertion_event = true;
141
}
142
else
143
sd_init_done = false;
144
145
return res;
146
}
147
148
bool sd_initialize(bool power_cycle)
149
{
150
if (power_cycle)
151
sdmmc_storage_end(&sd_storage);
152
153
int res = !sd_init_retry(false);
154
155
while (true)
156
{
157
if (!res)
158
return true;
159
else if (!sdmmc_get_sd_inserted()) // SD Card is not inserted.
160
{
161
sd_mode = SD_DEFAULT_SPEED;
162
break;
163
}
164
else
165
{
166
sd_errors[SD_ERROR_INIT_FAIL]++;
167
168
if (sd_mode == SD_INIT_FAIL)
169
break;
170
else
171
res = !sd_init_retry(true);
172
}
173
}
174
175
sdmmc_storage_end(&sd_storage);
176
177
return false;
178
}
179
180
bool sd_mount()
181
{
182
if (sd_init_done && sd_mounted)
183
return true;
184
185
int res = 0;
186
187
if (!sd_init_done)
188
res = !sd_initialize(false);
189
190
if (res)
191
{
192
gfx_con.mute = false;
193
EPRINTF("Failed to init SD card.");
194
if (!sdmmc_get_sd_inserted())
195
EPRINTF("Make sure that it is inserted.");
196
else
197
EPRINTF("SD Card Reader is not properly seated!");
198
}
199
else
200
{
201
if (!sd_mounted)
202
res = f_mount(&sd_fs, "0:", 1); // Volume 0 is SD.
203
if (res == FR_OK)
204
{
205
sd_mounted = true;
206
return true;
207
}
208
else
209
{
210
gfx_con.mute = false;
211
EPRINTFARGS("Failed to mount SD card (FatFS Error %d).\nMake sure that a FAT partition exists..", res);
212
}
213
}
214
215
return false;
216
}
217
218
static void _sd_deinit(bool deinit)
219
{
220
if (deinit)
221
{
222
insertion_event = false;
223
if (sd_mode == SD_INIT_FAIL)
224
sd_mode = SD_DEFAULT_SPEED;
225
}
226
227
if (sd_init_done)
228
{
229
if (sd_mounted)
230
f_mount(NULL, "0:", 1); // Volume 0 is SD.
231
232
if (deinit)
233
{
234
sdmmc_storage_end(&sd_storage);
235
sd_init_done = false;
236
}
237
}
238
sd_mounted = false;
239
}
240
241
void sd_unmount() { _sd_deinit(false); }
242
void sd_end() { _sd_deinit(true); }
243
244
bool sd_is_gpt()
245
{
246
return sd_fs.part_type;
247
}
248
249
void *sd_file_read(const char *path, u32 *fsize)
250
{
251
FIL fp;
252
if (!sd_get_card_mounted())
253
return NULL;
254
255
if (f_open(&fp, path, FA_READ) != FR_OK)
256
return NULL;
257
258
u32 size = f_size(&fp);
259
if (fsize)
260
*fsize = size;
261
262
void *buf = malloc(size);
263
264
if (f_read(&fp, buf, size, NULL) != FR_OK)
265
{
266
free(buf);
267
f_close(&fp);
268
269
return NULL;
270
}
271
272
f_close(&fp);
273
274
return buf;
275
}
276
277
int sd_save_to_file(const void *buf, u32 size, const char *filename)
278
{
279
FIL fp;
280
u32 res = 0;
281
if (!sd_get_card_mounted())
282
return FR_DISK_ERR;
283
284
res = f_open(&fp, filename, FA_CREATE_ALWAYS | FA_WRITE);
285
if (res)
286
{
287
EPRINTFARGS("Error (%d) creating file\n%s.\n", res, filename);
288
return res;
289
}
290
291
f_write(&fp, buf, size, NULL);
292
f_close(&fp);
293
294
return 0;
295
}
296
297