-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmassstorage.cpp
261 lines (220 loc) · 6.1 KB
/
massstorage.cpp
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
/*
* Authors (alphabetical order)
* - Bertrand Songis <[email protected]>
* - Bryan J. Rentoul (Gruvin) <[email protected]>
* - Cameron Weeks <[email protected]>
* - Erez Raviv
* - Jean-Pierre Parisy
* - Karl Szmutny <[email protected]>
* - Michael Blandford
* - Michal Hlavinka
* - Pat Mackenzie
* - Philip Moss
* - Rob Thomson
* - Romolo Manfredini <[email protected]>
* - Thomas Husterer
*
* open9x is based on code named
* gruvin9x by Bryan J. Rentoul: http://code.google.com/p/gruvin9x/,
* er9x by Erez Raviv: http://code.google.com/p/er9x/,
* and the original (and ongoing) project by
* Thomas Husterer, th9x: http://code.google.com/p/th9x/
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* 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.
*
*/
#include "radio.h"
#include "board.h"
//#include "debug.h"
//#include "Audio.h"
//#ifndef SIMU
//#include "CoOS.h"
//#endif
#ifndef SIMU
extern "C" {
#include "MSDDriver.h"
extern unsigned char MEDSdcard_Initialize(Media *media, unsigned char mciID);
}
/** Size of one block in bytes. */
#define BLOCK_SIZE 512
/** Size of the MSD IO buffer in bytes (6K, more the better). */
#define MSD_BUFFER_SIZE (12*BLOCK_SIZE)
/** LUN read/write buffer. */
unsigned char msdBuffer1[MSD_BUFFER_SIZE] __attribute__((section(".overlaydata"), aligned(32))) ;
unsigned char msdBuffer2[MSD_BUFFER_SIZE] __attribute__((section(".overlaydata"), aligned(32))) ;
unsigned int msdReadTotal=0, msdWriteTotal=0;
static void ConfigureUsbClock(void)
{
/* Enable PLLB for USB */
PMC->CKGR_PLLBR = CKGR_PLLBR_DIVB(1)
| CKGR_PLLBR_MULB(7)
| CKGR_PLLBR_PLLBCOUNT;
while((PMC->PMC_SR & PMC_SR_LOCKB) == 0); // TODO && (timeout++ < CLOCK_TIMEOUT));
/* USB Clock uses PLLB */
PMC->PMC_USB = PMC_USB_USBDIV(1) /* /2 */
| PMC_USB_USBS; /* PLLB */
}
/** Maximum number of LUNs which can be defined. */
#define MAX_LUNS 2
/** Media index for different disks */
#define DRV_SDMMC 0 /** SD card */
#define DRV_EEPROM 1 /** EEPROM */
Media medias[MAX_LUNS];
/*----------------------------------------------------------------------------
* Local variables
*----------------------------------------------------------------------------*/
#ifdef HID
uint8_t hid_iBuffer[64] ;
uint8_t hid_oBuffer[64] ;
static void hid_transfer()
{
uint32_t dwCnt=0 ;
uint32_t dwLen ;
dwLen = HIDDTransferDriver_Read( hid_iBuffer, 64 ) ;
if ( dwLen )
{
// printf( "Data In(%u):", (unsigned int)dwLen ) ;
// _ShowBuffer( iBuffer, dwLen ) ;
// bmLEDs = iBuffer[0] ;
// update = 1 ;
}
dwLen = HIDDTransferDriver_ReadReport( iBuffer, 64 ) ;
if ( dwLen )
{
// printf( "Report In(%u):", (unsigned int)dwLen ) ;
// _ShowBuffer( iBuffer, dwLen ) ;
// bmLEDs = iBuffer[0] ;
// update = 1 ;
}
oBuffer[0] = 0x80 ;
if ( PIO_Get( &pinsButtons[PUSHBUTTON_BP1]) == 0 )
{
oBuffer[0] |= 0x01 ;
}
if ( PIO_Get( &pinsButtons[PUSHBUTTON_BP2] ) == 0 )
{
oBuffer[0] |= 0x02 ;
}
sprintf( (char*)&oBuffer[5], ":%04x:%05u!", (unsigned int)dwCnt, (unsigned int)dwCnt ) ;
oBuffer[1] = (uint8_t)(dwCnt) ;
oBuffer[2] = (uint8_t)(dwCnt >> 8) ;
oBuffer[3] = (uint8_t)(dwCnt >> 16) ;
oBuffer[4] = (uint8_t)(dwCnt >> 24) ;
if ( USBD_STATUS_SUCCESS == HIDDTransferDriver_Write( oBuffer, 64, 0, 0 ) )
{
dwCnt ++ ;
}
}
#endif
/** Device LUNs. */
MSDLun luns[MAX_LUNS];
static void MSDCallbacks_Data( unsigned char flowDirection, unsigned int dataLength,
unsigned int fifoNullCount, unsigned int fifoFullCount )
{
#if 0
if (flowDirection)
msdReadTotal += dataLength;
else
msdWriteTotal += dataLength;
#endif
}
extern "C" unsigned char EEPROM_Initialize(Media *media, unsigned char mciID) ;
extern uint32_t sd_card_ready( void ) ;
void usbMassStorage()
{
static bool initialized = false ;
static bool active = false ;
if ( PIOC->PIO_PDSR & 0x02000000 )
{
// TRACE_DEBUG("usbMassStorage\n\r");
if (!initialized)
{
ConfigureUsbClock();
/* Initialize LUN */
MEDSdcard_Initialize(&(medias[DRV_SDMMC]), 0);
EEPROM_Initialize(&(medias[DRV_EEPROM]), 0);
LUN_Init(&(luns[DRV_SDMMC]), sd_card_ready() ? &(medias[DRV_SDMMC]) : 0 ,
msdBuffer1, MSD_BUFFER_SIZE,
0, 0, 0, 0,
MSDCallbacks_Data);
LUN_Init(&(luns[DRV_EEPROM]), &(medias[DRV_EEPROM]),
msdBuffer2, MSD_BUFFER_SIZE,
0, 0, 0, 0,
MSDCallbacks_Data);
/* BOT driver initialization */
MSDDriver_Initialize( luns, 2 ) ;
// VBus_Configure();
USBD_Connect();
initialized = true;
}
if ( active == false )
{
#ifndef BOOT
CoSchedLock() ;
if ( Voice.VoiceLock == 0 )
{
Voice.VoiceLock = 1 ;
#endif
active = true ;
#ifndef BOOT
}
CoSchedUnlock() ;
#endif
}
if ( active )
{
/* Mass storage state machine */
for (uint8_t i=0; i<50; i++)
{
#ifdef HID
hid_transfer() ;
#endif
MSDDriver_StateMachine() ;
}
}
}
else
{
if ( active )
{
active = false ;
#ifndef BOOT
Voice.VoiceLock = 0 ;
#endif
}
msdReadTotal = 0;
msdWriteTotal = 0;
}
}
uint16_t usbLunStat()
{
return (luns[DRV_SDMMC].status << 8 ) | luns[DRV_EEPROM].status ;
// return 1234 ;
}
void usbPluggedIn( uint16_t allowSD )
{
if ( allowSD )
{
if ( ( luns[DRV_SDMMC].status == LUN_EJECTED )
|| ( luns[DRV_SDMMC].status == LUN_NOT_PRESENT ) )
{
luns[DRV_SDMMC].status = LUN_READY ;
}
}
else
{
luns[DRV_SDMMC].status = LUN_NOT_PRESENT ;
}
if ( luns[DRV_EEPROM].status == LUN_EJECTED )
{
luns[DRV_EEPROM].status = LUN_READY ;
}
}
#endif