Paparazzi UAS  v5.18.0_stable
Paparazzi is a free software Unmanned Aircraft System.
usb_msc_hw.c
Go to the documentation of this file.
1 /*
2  LPCUSB, an USB device driver for LPC microcontrollers
3  Copyright (C) 2006 Bertrik Sikken (bertrik@sikken.nl)
4  adapted to pprz Martin Mueller (martinmm@pfump.org)
5 
6  Redistribution and use in source and binary forms, with or without
7  modification, are permitted provided that the following conditions are met:
8 
9  1. Redistributions of source code must retain the above copyright
10  notice, this list of conditions and the following disclaimer.
11  2. Redistributions in binary form must reproduce the above copyright
12  notice, this list of conditions and the following disclaimer in the
13  documentation and/or other materials provided with the distribution.
14  3. The name of the author may not be used to endorse or promote products
15  derived from this software without specific prior written permission.
16 
17  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28 
29 /*
30  Connects a microSD card to the SPI port of the Paparazzi Tiny. Keep cables
31  short, microSD card can be directly soldered to Molex cable. For now only
32  non SDHC SD cards (<= 2GB) are supported. martinmm@pfump.org
33 
34  microSD TinyV2 SPI J3
35  8 nc
36  7 DO 5 MISO
37  6 GND 1 GND
38  5 CLK 7 SCK
39  4 Vcc 2 +3V3
40  3 DI 4 MOSI
41  2 CS 3 SSEL
42  1 nc
43 
44  Looking onto the gold plated connector side of the microSD card:
45 
46  ---------------
47  I 8
48  I 7
49  I 6
50  I 5
51  I 4
52  I 3
53  I 2
54  I 1
55  ------ --
56  \ I \
57  -- --
58 
59 */
60 
61 #include <string.h>
62 #include "std.h"
63 #include "LPC21xx.h"
64 #include "armVIC.h"
65 #include BOARD_CONFIG
66 
67 #include "lpcusb/usbapi.h"
68 #include "msc_bot.h"
69 #include "blockdev.h"
70 #include "usb_msc_hw.h"
71 
72 #if USE_USB_SERIAL
73 #if PCLK < 18000000
74 #error PCLK needs to be higher than 18MHz for USB to work properly
75 #endif
76 #endif
77 
78 #define MAX_PACKET_SIZE 64
79 
80 #define LE_WORD(x) ((x)&0xFF),((x)>>8)
81 
82 static U8 abClassReqData[4];
83 
84 static const U8 abDescriptors[] = {
85 
86 // device descriptor
87  0x12,
88  DESC_DEVICE,
89  LE_WORD(0x0200), // bcdUSB
90  0x00, // bDeviceClass
91  0x00, // bDeviceSubClass
92  0x00, // bDeviceProtocol
93  MAX_PACKET_SIZE0, // bMaxPacketSize
94  LE_WORD(0x7070), // idVendor
95  LE_WORD(0x1236), // idProduct
96  LE_WORD(0x0100), // bcdDevice
97  0x01, // iManufacturer
98  0x02, // iProduct
99  0x03, // iSerialNumber
100  0x01, // bNumConfigurations
101 
102 // configuration descriptor
103  0x09,
104  DESC_CONFIGURATION,
105  LE_WORD(32), // wTotalLength
106  0x01, // bNumInterfaces
107  0x01, // bConfigurationValue
108  0x00, // iConfiguration
109  0xC0, // bmAttributes
110  0x32, // bMaxPower
111 
112 // interface
113  0x09,
114  DESC_INTERFACE,
115  0x00, // bInterfaceNumber
116  0x00, // bAlternateSetting
117  0x02, // bNumEndPoints
118  0x08, // bInterfaceClass = mass storage
119  0x06, // bInterfaceSubClass = transparent SCSI
120  0x50, // bInterfaceProtocol = BOT
121  0x00, // iInterface
122 // EP
123  0x07,
124  DESC_ENDPOINT,
125  MSC_BULK_IN_EP, // bEndpointAddress
126  0x02, // bmAttributes = bulk
127  LE_WORD(MAX_PACKET_SIZE),// wMaxPacketSize
128  0x00, // bInterval
129 // EP
130  0x07,
131  DESC_ENDPOINT,
132  MSC_BULK_OUT_EP, // bEndpointAddress
133  0x02, // bmAttributes = bulk
134  LE_WORD(MAX_PACKET_SIZE),// wMaxPacketSize
135  0x00, // bInterval
136 
137 // string descriptors
138  0x04,
139  DESC_STRING,
140  LE_WORD(0x0409),
141 
142  0x0E,
143  DESC_STRING,
144  'L', 0, 'P', 0, 'C', 0, 'U', 0, 'S', 0, 'B', 0,
145 
146  0x14,
147  DESC_STRING,
148  'S', 0, 'D', 0, '-', 0, 'R', 0, 'e', 0, 'a', 0, 'd', 0, 'e', 0, 'r', 0,
149 
150  0x12,
151  DESC_STRING,
152  '1', 0, '2', 0, '3', 0, '4', 0, '5', 0, '6', 0, '7', 0, '8', 0,
153 
154 // terminating zero
155  0
156 };
157 
158 #ifdef USE_USB_MSC
159 
160 /*************************************************************************
161  HandleClassRequest
162  ==================
163  Handle mass storage class request
164 
165 **************************************************************************/
166 static BOOL HandleClassRequest(TSetupPacket *pSetup, int *piLen, U8 **ppbData)
167 {
168  if (pSetup->wIndex != 0) {
169  return FALSE;
170  }
171  if (pSetup->wValue != 0) {
172  return FALSE;
173  }
174 
175  switch (pSetup->bRequest) {
176 
177  // get max LUN
178  case 0xFE:
179  *ppbData[0] = 0; // No LUNs
180  *piLen = 1;
181  break;
182 
183  // MSC reset
184  case 0xFF:
185  if (pSetup->wLength > 0) {
186  return FALSE;
187  }
188  MSCBotReset();
189  break;
190 
191  default:
192  return FALSE;
193  }
194  return TRUE;
195 }
196 
197 
198 /*************************************************************************
199  main
200  ====
201 **************************************************************************/
202 int main_mass_storage(void)
203 {
204  unsigned cpsr;
205 
206  // disable global interrupts, do it polling
207  cpsr = disableIRQ();
208 
209  // initialise the SD card
210  BlockDevInit();
211 
212  // initialise stack
213  USBInit();
214 
215  // enable bulk-in interrupts on NAKs
216  // these are required to get the BOT protocol going again after a STALL
217  USBHwNakIntEnable(INACK_BI);
218 
219  // register descriptors
220  USBRegisterDescriptors(abDescriptors);
221 
222  // register class request handler
223  USBRegisterRequestHandler(REQTYPE_TYPE_CLASS, HandleClassRequest, abClassReqData);
224 
225  // register endpoint handlers
226  USBHwRegisterEPIntHandler(MSC_BULK_IN_EP, MSCBotBulkIn);
227  USBHwRegisterEPIntHandler(MSC_BULK_OUT_EP, MSCBotBulkOut);
228 
229  // connect to bus
230  USBHwConnect(TRUE);
231 
232  // call USB interrupt handler continuously
233  while (1) {
234  USBHwISR();
235  }
236 
237  // possibly restore global interrupts (never happens)
238  restoreIRQ(cpsr);
239 
240  return 0;
241 }
242 
243 #endif /* USE_USB_MSC */
244 
usb_msc_hw.h
disableIRQ
unsigned disableIRQ(void)
Definition: armVIC.c:33
abDescriptors
static const U8 abDescriptors[]
Definition: usb_msc_hw.c:84
armVIC.h
LPC21xx.h
std.h
abClassReqData
static U8 abClassReqData[4]
Definition: usb_msc_hw.c:82
LE_WORD
#define LE_WORD(x)
Definition: usb_msc_hw.c:80
HandleClassRequest
static BOOL HandleClassRequest(TSetupPacket *pSetup, int *piLen, U8 **ppbData)
Local function to handle the USB-CDC class requests.
Definition: usb_ser_hw.c:493
main_mass_storage
int main_mass_storage(void)
FALSE
#define FALSE
Definition: std.h:5
TRUE
#define TRUE
Definition: std.h:4
restoreIRQ
unsigned restoreIRQ(unsigned oldCPSR)
Definition: armVIC.c:42
MAX_PACKET_SIZE
#define MAX_PACKET_SIZE
Definition: usb_msc_hw.c:78