-
-
Notifications
You must be signed in to change notification settings - Fork 164
/
Target_Windows_Storage.c
264 lines (204 loc) · 6.45 KB
/
Target_Windows_Storage.c
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
//
// Copyright (c) .NET Foundation and Contributors
// See LICENSE file in the project root for full license information.
//
#include <hal.h>
#include <cmsis_os.h>
#include <ff.h>
#include <target_windows_storage_config.h>
#include <nanoHAL_Windows_Storage.h>
#include <Target_Windows_Storage.h>
#include <nanoHAL_v2.h>
#if defined(HAL_USBH_USE_MSD) && (HAL_USBH_USE_MSD == TRUE)
#include "usbh/dev/msd.h"
#endif
// need to declare this here as extern
extern void PostManagedEvent(uint8_t category, uint8_t subCategory, uint16_t data1, uint32_t data2);
///////////////////////////////////////////
// code specific to SD Card
#if HAL_USE_SDC
// FS for SD Card mounted and ready
bool sdCardFileSystemReady;
static FATFS sdCard_FS;
static SDCConfig SDC_CFG;
// SD Card event sources
static event_source_t sdInsertedEvent, sdRemovedEvent;
// this timer is used to handle the debounce of SD card detect line
static virtual_timer_t sdCardDebounceTimer;
static bool sdCardPresent;
// Insertion monitor timer callback function.
static void SdCardInsertionMonitorCallback(virtual_timer_t *vtp, void *p)
{
(void)vtp;
BaseBlockDevice *bbdp = p;
bool currentStatus;
chSysLockFromISR();
// get current status
currentStatus = blkIsInserted(bbdp);
if (sdCardPresent == currentStatus)
{
// value hasn't change for debounce interval so this is a valid change
if (currentStatus)
{
chEvtBroadcastI(&sdInsertedEvent);
}
else
{
chEvtBroadcastI(&sdRemovedEvent);
}
}
chSysUnlockFromISR();
}
static void SdCardDetectCallback(void *arg)
{
BaseBlockDevice *bbdp = (BaseBlockDevice *)arg;
if (port_is_isr_context())
{
chSysLockFromISR();
if (!chVTIsArmedI(&sdCardDebounceTimer))
{
// save current status
sdCardPresent = blkIsInserted(bbdp);
// setup timer
chVTSetI(&sdCardDebounceTimer, TIME_MS2I(SDCARD_POLLING_DELAY), SdCardInsertionMonitorCallback, arg);
}
chSysUnlockFromISR();
}
else
{
if (!chVTIsArmed(&sdCardDebounceTimer))
{
// save current status
sdCardPresent = blkIsInserted(bbdp);
// setup timer
chVTSet(&sdCardDebounceTimer, TIME_MS2I(SDCARD_POLLING_DELAY), SdCardInsertionMonitorCallback, arg);
}
}
}
// Card insertion event handler
void SdcardInsertHandler(eventid_t id)
{
FRESULT err;
(void)id;
// On insertion SDC initialization and FS mount
if (sdcConnect(&SD_CARD_DRIVER))
{
return;
}
err = f_mount(&sdCard_FS, SD_CARD_DRIVE_INDEX, 1);
if (err != FR_OK)
{
osDelay(1000);
sdcDisconnect(&SD_CARD_DRIVER);
return;
}
else
{
sdCardFileSystemReady = true;
// post event to managed app
PostManagedEvent(EVENT_STORAGE, 0, StorageEventType_RemovableDeviceInsertion, SD_CARD_DRIVE_INDEX_NUMERIC);
}
}
// Card removal event handler
void SdCardRemoveHandler(eventid_t id)
{
(void)id;
sdcDisconnect(&SD_CARD_DRIVER);
sdCardFileSystemReady = false;
// post event to managed app
PostManagedEvent(EVENT_STORAGE, 0, StorageEventType_RemovableDeviceRemoval, SD_CARD_DRIVE_INDEX_NUMERIC);
}
__attribute__((noreturn)) void SdCardWorkingThread(void const *argument)
{
(void)argument;
event_listener_t sdEventListener0, sdEventListener1;
const evhandler_t sdcardEventHandler[] = {SdcardInsertHandler, SdCardRemoveHandler};
sdCardFileSystemReady = false;
// activates the SDC driver using default configuration
sdcStart(&SD_CARD_DRIVER, &SDC_CFG);
// setup line event in SD Card detect GPIO
palEnableLineEvent(SDCARD_LINE_DETECT, PAL_EVENT_MODE_BOTH_EDGES);
palSetLineCallback(SDCARD_LINE_DETECT, SdCardDetectCallback, &SD_CARD_DRIVER);
// init timer
chVTObjectInit(&sdCardDebounceTimer);
// init events
chEvtObjectInit(&sdInsertedEvent);
chEvtObjectInit(&sdRemovedEvent);
// register events
chEvtRegister(&sdInsertedEvent, &sdEventListener0, 0);
chEvtRegister(&sdRemovedEvent, &sdEventListener1, 1);
// force initial check
SdCardDetectCallback(&SD_CARD_DRIVER);
for (;;)
{
chEvtDispatch(sdcardEventHandler, chEvtWaitOneTimeout(ALL_EVENTS, TIME_MS2I(SDCARD_POLLING_DELAY)));
}
}
#endif
///////////////////////////////////////////
///////////////////////////////////////////
// code specific to USB MSD
#if HAL_USBH_USE_MSD
// FS for USB MSD mounted and ready
bool usbMsdFileSystemReady;
static FATFS usbMsd_FS;
__attribute__((noreturn)) void UsbMsdWorkingThread(void const *argument)
{
(void)argument;
FRESULT err;
usbMsdFileSystemReady = false;
// start USB host
usbhStart(&USB_MSD_DRIVER);
USBHMassStorageLUNDriver *msBlk = (USBHMassStorageLUNDriver *)&MSBLKD[0];
for (;;)
{
osDelay(USB_MSD_POLLING_INTERVAL);
if (blkGetDriverState(msBlk) == BLK_ACTIVE)
{
// file system can't be ready
usbMsdFileSystemReady = false;
// BLK: Active, connect
usbhmsdLUNConnect(msBlk);
}
if (blkGetDriverState(msBlk) == BLK_READY)
{
// BLK: Ready
if (!usbMsdFileSystemReady)
{
// USB MSD file system not ready
// mount drive
err = f_mount(&usbMsd_FS, USB_MSD_DRIVE_INDEX, 1);
if (err != FR_OK)
{
// mount operation failed, disconnect
usbhmsdLUNDisconnect(msBlk);
}
else
{
usbMsdFileSystemReady = true;
// post event to managed app
PostManagedEvent(
EVENT_STORAGE,
0,
StorageEventType_RemovableDeviceInsertion,
USB_MSD_DRIVE_INDEX_NUMERIC);
}
}
}
if (blkGetDriverState(msBlk) == BLK_STOP)
{
if (usbMsdFileSystemReady)
{
usbMsdFileSystemReady = false;
// post event to managed app
PostManagedEvent(
EVENT_STORAGE,
0,
StorageEventType_RemovableDeviceRemoval,
USB_MSD_DRIVE_INDEX_NUMERIC);
}
}
usbhMainLoop(&USB_MSD_DRIVER);
}
}
#endif