forked from xxxajk/generic_storage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStorage.cpp
More file actions
217 lines (190 loc) · 8.08 KB
/
Copy pathStorage.cpp
File metadata and controls
217 lines (190 loc) · 8.08 KB
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
#include <Usb.h>
#include <masstorage.h>
#include <Storage.h>
#ifdef _usb_h_
BulkOnly *UHS_USB_BulkOnly[MAX_USB_MS_DRIVERS];
#if STORAGE_HAS_MEDIA_CACHE
#define _CACHE_MAX (32768U/_MAX_SS)
uint8_t cache_bank;
storage_cache_t WriteCache[_CACHE_MAX];
uint8_t copyarea[_MAX_SS];
#endif
/**
* This must be called before using UHS USB Mass Storage. This works around a G++ bug.
* Thanks to Lei Shi for the heads up.
*/
static void UHS_USB_BulkOnly_Init(void) {
for(int i = 0; i < MAX_USB_MS_DRIVERS; i++) {
UHS_USB_BulkOnly[i] = new BulkOnly(&Usb);
}
#if STORAGE_HAS_MEDIA_CACHE
//cache_ticker = 0;
cache_bank = xmem::AllocateExtraBank();
xmem::Sleep(500);
for(int i = 0; i < _CACHE_MAX; i++) {
WriteCache[i].buf = reinterpret_cast<uint8_t *>(0x8000 + (_MAX_SS * i));
WriteCache[i].sto = NULL;
}
#endif
}
// On mass storage, there is nothing to do. Just call the status function.
DSTATUS UHS_USB_BulkOnly_Initialize(storage_t *sto) {
if((UHS_USB_BulkOnly[((pvt_t *)sto->private_data)->B]->WriteProtected(((pvt_t *)sto->private_data)->lun))) {
return STA_PROTECT;
} else {
return STA_OK;
}
}
bool UHS_USB_BulkOnly_Status(storage_t *sto) {
return (UHS_USB_BulkOnly[((pvt_t *)sto->private_data)->B]->WriteProtected(((pvt_t *)sto->private_data)->lun));
}
// TO-DO: read cache?
int UHS_USB_BulkOnly_Read(uint32_t LBA, uint8_t *buf, storage_t *sto, uint8_t count) {
uint8_t x = 0;
while(count) {
int tries = FAT_MAX_ERROR_RETRIES;
#if STORAGE_HAS_MEDIA_CACHE
// Try to read from the cache first.
for(int i = 0; (i < _CACHE_MAX) && tries; i++) {
if(WriteCache[i].LBA == LBA && WriteCache[i].sto == sto) {
// Matched!
tries = 0;
xmem::copy_from_task(buf, WriteCache[i].buf, (sto->SectorSize), cache_bank);
}
}
#endif
while(tries) {
tries--;
x = (UHS_USB_BulkOnly[((pvt_t *)sto->private_data)->B]->Read(((pvt_t *)sto->private_data)->lun, LBA, (sto->SectorSize), 1, buf));
if(!x) break;
}
if(x) break;
// TO-DO: READ caching?
count--;
buf += (sto->SectorSize);
LBA++;
}
int y = x;
return y;
}
int UHS_USB_BulkOnly_Write(uint32_t LBA, uint8_t *buf, storage_t *sto, uint8_t count) {
uint8_t x = 0;
while(count && !x) {
int tries = FAT_MAX_ERROR_RETRIES;
#if STORAGE_HAS_MEDIA_CACHE
for(int i = 0; (i < _CACHE_MAX) && tries; i++) {
if(WriteCache[i].LBA == LBA && WriteCache[i].sto == sto) {
// Matched!
xmem::copy_to_task(WriteCache[i].buf, buf, (sto->SectorSize), cache_bank);
tries = 0;
}
}
if(tries) {
// No match, anything free?
for(int i = 0; (i < _CACHE_MAX) && tries; i++) {
if(WriteCache[i].sto == NULL) {
// FREE!
WriteCache[i].sto = sto;
WriteCache[i].LBA = LBA;
xmem::copy_to_task(WriteCache[i].buf, buf, (sto->SectorSize), cache_bank);
tries = 0;
}
}
if(tries) {
// Nothing free, commit some of the cache
int bigindex[_CACHE_MAX / 4];
for(int j = 0; j < _CACHE_MAX / 4; j++) {
bigindex[j] = 0;
}
for(int i = 0; i < _CACHE_MAX; i++) {
for(int j=0; j<(_CACHE_MAX / 4); j++) {
if(WriteCache[i].LBA > WriteCache[bigindex[j]].LBA) {
bigindex[j]=i;
break;
}
}
}
for(int j = 0; (j < _CACHE_MAX / 4) && !x; j++) {
tries = FAT_MAX_ERROR_RETRIES;
xmem::copy_from_task(copyarea, WriteCache[j].buf, (WriteCache[j].sto->SectorSize), cache_bank);
while(tries) {
tries--;
x = (UHS_USB_BulkOnly[((pvt_t *)WriteCache[j].sto->private_data)->B]->Write(((pvt_t *)WriteCache[j].sto->private_data)->lun, WriteCache[j].LBA, (WriteCache[j].sto->SectorSize), 1, copyarea));
if(x == MASS_ERR_WRITE_PROTECTED) break;
if(!x) break;
}
if(!x) {
WriteCache[j].sto = NULL;
}
}
if(!x) {
xmem::copy_to_task(WriteCache[bigindex[0]].buf, buf, (sto->SectorSize), cache_bank);
WriteCache[bigindex[0]].sto = sto;
WriteCache[bigindex[0]].LBA = LBA;
}
}
}
#else
while(tries) {
tries--;
x = (UHS_USB_BulkOnly[((pvt_t *)sto->private_data)->B]->Write(((pvt_t *)sto->private_data)->lun, LBA, (sto->SectorSize), 1, buf));
if(x == MASS_ERR_WRITE_PROTECTED) break;
if(!x) break;
}
#endif
count--;
buf += (sto->SectorSize);
LBA++;
}
int y = x;
return y;
}
uint8_t UHS_USB_BulkOnly_Commit(storage_t *sto) {
uint8_t x = 0;
#if STORAGE_HAS_MEDIA_CACHE
for(int j = 0; j < _CACHE_MAX; j++) {
if(WriteCache[j].sto == sto) {
// Matched!
xmem::copy_from_task(copyarea, WriteCache[j].buf, (WriteCache[j].sto->SectorSize), cache_bank);
int tries = FAT_MAX_ERROR_RETRIES;
while(tries) {
tries--;
x = (UHS_USB_BulkOnly[((pvt_t *)WriteCache[j].sto->private_data)->B]->Write(((pvt_t *)WriteCache[j].sto->private_data)->lun, WriteCache[j].LBA, (WriteCache[j].sto->SectorSize), 1, copyarea));
if(x == MASS_ERR_WRITE_PROTECTED) break;
if(!x) break;
}
if(!x) {
WriteCache[j].sto = NULL;
} else {
j=_CACHE_MAX;
}
}
}
#endif
return x;
}
#endif
// YOUR DRIVERS HERE
// Allow init to happen only once in the case of multiple external inits.
static bool inited = false;
/**
* This must be called before using generic_storage. Calling more than once is harmless.
*/
void Init_Generic_Storage(void) {
if(!inited) {
inited = true;
#ifdef _usb_h_
UHS_USB_BulkOnly_Init();
#endif
// YOUR INIT HERE
}
return;
}
#if 0
void PollStorage(void) {
#ifdef _usb_h_
UHS_USB_BulkOnly_Poll();
#endif
// YOUR POLLING HERE
}
#endif