LPCOpen Platform
LPCOpen Platform for NXP LPC Microcontrollers
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
MassStorageHost.c
Go to the documentation of this file.
1 /*
2  * @brief Mass Storage Host example
3  *
4  * @note
5  * Copyright(C) NXP Semiconductors, 2012
6  * Copyright(C) Dean Camera, 2011, 2012
7  * All rights reserved.
8  *
9  * @par
10  * Software that is described herein is for illustrative purposes only
11  * which provides customers with programming information regarding the
12  * LPC products. This software is supplied "AS IS" without any warranties of
13  * any kind, and NXP Semiconductors and its licensor disclaim any and
14  * all warranties, express or implied, including all implied warranties of
15  * merchantability, fitness for a particular purpose and non-infringement of
16  * intellectual property rights. NXP Semiconductors assumes no responsibility
17  * or liability for the use of the software, conveys no license or rights under any
18  * patent, copyright, mask work right, or any other intellectual property rights in
19  * or to any products. NXP Semiconductors reserves the right to make changes
20  * in the software without notification. NXP Semiconductors also makes no
21  * representation or warranty that such application will be suitable for the
22  * specified use without further testing or modification.
23  *
24  * @par
25  * Permission to use, copy, modify, and distribute this software and its
26  * documentation is hereby granted, under NXP Semiconductors' and its
27  * licensor's relevant copyrights in the software, without fee, provided that it
28  * is used in conjunction with NXP Semiconductors microcontrollers. This
29  * copyright, permission, and disclaimer notice must appear in all copies of
30  * this code.
31  */
32 
33 #include "MassStorageHost.h"
34 
40  .Config = {
41  .DataINPipeNumber = 1,
42  .DataINPipeDoubleBank = false,
43 
44  .DataOUTPipeNumber = 2,
45  .DataOUTPipeDoubleBank = false,
46  },
47 };
48 
52 int main(void)
53 {
54  SetupHardware();
55 
56  DEBUGOUT("Mass Storage Host Demo running.\r\n");
57 
58  for (;; ) {
60 
61  MS_Host_USBTask(&FlashDisk_MS_Interface);
62  USB_USBTask();
63  }
64 }
65 
67 void SetupHardware(void)
68 {
69  Board_Init();
70  /* Hardware Initialization */
72 
73  USB_Init();
74 
75  /* Create a stdio stream for the serial port for stdin and stdout */
77 }
78 
83 {
84  if (USB_HostState[FlashDisk_MS_Interface.Config.PortNumber] != HOST_STATE_Configured) {
85  return;
86  }
87 
88  // LEDs_SetAllLEDs(LEDMASK_USB_BUSY);
89 
90  DEBUGOUT("Waiting until ready...\r\n");
91 
92  for (;; ) {
93  uint8_t ErrorCode = MS_Host_TestUnitReady(&FlashDisk_MS_Interface, 0);
94 
95  if (!(ErrorCode)) {
96  break;
97  }
98 
99  /* Check if an error other than a logical command error (device busy) received */
100  if (ErrorCode != MS_ERROR_LOGICAL_CMD_FAILED) {
101  DEBUGOUT("Error waiting for device to be ready.\r\n");
102  // LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
103  USB_Host_SetDeviceConfiguration(FlashDisk_MS_Interface.Config.PortNumber, 0);
104  return;
105  }
106  }
107 
108  DEBUGOUT("Retrieving Capacity...\r\n");
109 
110  SCSI_Capacity_t DiskCapacity;
111  if (MS_Host_ReadDeviceCapacity(&FlashDisk_MS_Interface, 0, &DiskCapacity)) {
112  DEBUGOUT("Error retrieving device capacity.\r\n");
113  // LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
114  USB_Host_SetDeviceConfiguration(FlashDisk_MS_Interface.Config.PortNumber, 0);
115  return;
116  }
117 
118  DEBUGOUT(("%lu blocks of %lu bytes.\r\n"), DiskCapacity.Blocks, DiskCapacity.BlockSize);
119 
120  // uint8_t BlockBuffer[DiskCapacity.BlockSize];
121  uint8_t BlockBuffer[512];
122 
123  if (MS_Host_ReadDeviceBlocks(&FlashDisk_MS_Interface, 0, 0x00000000, 1, DiskCapacity.BlockSize, BlockBuffer)) {
124  DEBUGOUT("Error reading device block.\r\n");
125  // LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
126  USB_Host_SetDeviceConfiguration(FlashDisk_MS_Interface.Config.PortNumber, 0);
127  return;
128  }
129 
130  DEBUGOUT("\r\nContents of first block:\r\n");
131 
132  for (uint16_t Chunk = 0; Chunk < (DiskCapacity.BlockSize >> 4); Chunk++) {
133  uint8_t *ChunkPtr = &BlockBuffer[Chunk << 4];
134 
135  /* Print out the 16 bytes of the chunk in HEX format */
136  for (uint8_t ByteOffset = 0; ByteOffset < (1 << 4); ByteOffset++) {
137  char CurrByte = *(ChunkPtr + ByteOffset);
138  CurrByte = CurrByte;
139  CurrByte = CurrByte;
140  DEBUGOUT(("%.2X "), CurrByte);
141  }
142 
143  DEBUGOUT(" ");
144 
145  /* Print out the 16 bytes of the chunk in ASCII format */
146  for (uint8_t ByteOffset = 0; ByteOffset < (1 << 4); ByteOffset++) {
147  char CurrByte = *(ChunkPtr + ByteOffset);
148  putchar(isprint(CurrByte) ? CurrByte : '.');
149  }
150 
151  DEBUGOUT("\r\n");
152  }
153 
154  // LEDs_SetAllLEDs(LEDMASK_USB_READY);
155  USB_Host_SetDeviceConfiguration(FlashDisk_MS_Interface.Config.PortNumber, 0);
156 }
157 
161 void EVENT_USB_Host_DeviceAttached(const uint8_t corenum)
162 {
163  DEBUGOUT(("Device Attached on port %d\r\n"), corenum);
164  // LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
165 }
166 
170 void EVENT_USB_Host_DeviceUnattached(const uint8_t corenum)
171 {
172  DEBUGOUT(("\r\nDevice Unattached on port %d\r\n"), corenum);
173  // LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
174 }
175 
179 void EVENT_USB_Host_DeviceEnumerationComplete(const uint8_t corenum)
180 {
181  // LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
182 
183  uint16_t ConfigDescriptorSize;
184  uint8_t ConfigDescriptorData[512];
185 
186  if (USB_Host_GetDeviceConfigDescriptor(corenum, 1, &ConfigDescriptorSize, ConfigDescriptorData,
187  sizeof(ConfigDescriptorData)) != HOST_GETCONFIG_Successful) {
188  DEBUGOUT("Error Retrieving Configuration Descriptor.\r\n");
189  // LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
190  return;
191  }
192 
193  FlashDisk_MS_Interface.Config.PortNumber = corenum;
194  if (MS_Host_ConfigurePipes(&FlashDisk_MS_Interface,
195  ConfigDescriptorSize, ConfigDescriptorData) != MS_ENUMERROR_NoError) {
196  DEBUGOUT("Attached Device Not a Valid Mass Storage Device.\r\n");
197  // LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
198  return;
199  }
200 
202  DEBUGOUT("Error Setting Device Configuration.\r\n");
203  // LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
204  return;
205  }
206 
207  uint8_t MaxLUNIndex;
208  if (MS_Host_GetMaxLUN(&FlashDisk_MS_Interface, &MaxLUNIndex)) {
209  DEBUGOUT("Error retrieving max LUN index.\r\n");
210  // LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
211  USB_Host_SetDeviceConfiguration(FlashDisk_MS_Interface.Config.PortNumber, 0);
212  return;
213  }
214 
215  DEBUGOUT(("Total LUNs: %d - Using first LUN in device.\r\n"), (MaxLUNIndex + 1));
216 
217  if (MS_Host_ResetMSInterface(&FlashDisk_MS_Interface)) {
218  DEBUGOUT("Error resetting Mass Storage interface.\r\n");
219  // LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
220  USB_Host_SetDeviceConfiguration(FlashDisk_MS_Interface.Config.PortNumber, 0);
221  return;
222  }
223 
225  if (MS_Host_RequestSense(&FlashDisk_MS_Interface, 0, &SenseData) != 0) {
226  DEBUGOUT("Error retrieving device sense.\r\n");
227  // LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
228  USB_Host_SetDeviceConfiguration(FlashDisk_MS_Interface.Config.PortNumber, 0);
229  return;
230  }
231 
232  if (MS_Host_PreventAllowMediumRemoval(&FlashDisk_MS_Interface, 0, true)) {
233  DEBUGOUT("Error setting Prevent Device Removal bit.\r\n");
234  // LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
235  USB_Host_SetDeviceConfiguration(FlashDisk_MS_Interface.Config.PortNumber, 0);
236  return;
237  }
238 
240  if (MS_Host_GetInquiryData(&FlashDisk_MS_Interface, 0, &InquiryData)) {
241  DEBUGOUT("Error retrieving device Inquiry data.\r\n");
242  // LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
243  USB_Host_SetDeviceConfiguration(FlashDisk_MS_Interface.Config.PortNumber, 0);
244  return;
245  }
246 
247  // Board_UARTPutSTR(("Vendor \"%.8s\", Product \"%.16s\"\r\n"), InquiryData.VendorID, InquiryData.ProductID);
248 
249  DEBUGOUT("Mass Storage Device Enumerated.\r\n");
250  // LEDs_SetAllLEDs(LEDMASK_USB_READY);
251 }
252 
254 void EVENT_USB_Host_HostError(const uint8_t corenum, const uint8_t ErrorCode)
255 {
256  USB_Disable();
257 
258  DEBUGOUT(("Host Mode Error\r\n"
259  " -- Error port %d\r\n"
260  " -- Error Code %d\r\n" ), corenum, ErrorCode);
261 
262  // LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
263  for (;; ) ;
264 }
265 
269 void EVENT_USB_Host_DeviceEnumerationFailed(const uint8_t corenum,
270  const uint8_t ErrorCode,
271  const uint8_t SubErrorCode)
272 {
273  DEBUGOUT(("Dev Enum Error\r\n"
274  " -- Error port %d\r\n"
275  " -- Error Code %d\r\n"
276  " -- Sub Error Code %d\r\n"
277  " -- In State %d\r\n" ),
278  corenum, ErrorCode, SubErrorCode, USB_HostState[corenum]);
279 
280  // LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
281 }