| LPCOpen Platform v1.03 LPCOpen Platform for NXP LPC Microcontrollers

- Main Page
- Modules
- Data Structures
- Files
- Related Pages

- File List
- Globals

All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
MassStorage.c
Go to the documentation of this file.
1 /*
2 * @brief USB Mass Storage Device 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 "MassStorage.h"
34
35 /*****************************************************************************
36 * Private types/enumerations/variables
37 ****************************************************************************/
38
39 #ifdef CFG_SDCARD
40 /* SDMMC card info structure */
41 static mci_card_struct sdcardinfo;
42 static volatile int32_t sdio_wait_exit = 0;
43 #endif
44
49 static USB_ClassInfo_MS_Device_t Disk_MS_Interface = {
50 .Config = {
51 .InterfaceNumber = 0,
52
53 .DataINEndpointNumber = MASS_STORAGE_IN_EPNUM,
54 .DataINEndpointSize = MASS_STORAGE_IO_EPSIZE,
55 .DataINEndpointDoubleBank = false,
56
57 .DataOUTEndpointNumber = MASS_STORAGE_OUT_EPNUM,
58 .DataOUTEndpointSize = MASS_STORAGE_IO_EPSIZE,
59 .DataOUTEndpointDoubleBank = false,
60
61 .TotalLUNs = TOTAL_LUNS,
62 .PortNumber = 0,
63 },
64 };
65
66 /*****************************************************************************
67 * Public types/enumerations/variables
68 ****************************************************************************/
69
70 /*****************************************************************************
71 * Private functions
72 ****************************************************************************/
73
74 #ifdef CFG_SDCARD
75
76 /* Delay callback for timed SDIF/SDMMC functions */
77 static void sdmmc_waitms(uint32_t time)
78 {
79 int32_t curr = (int32_t) Chip_RIT_GetCounter(LPC_RITIMER);
80 int32_t final = curr + ((SystemCoreClock / 1000) * time);
81
82 /* If the value is zero let us not worry about it */
83 if (!time) {
84 return;
85 }
86
87 if ((final < 0) && (curr > 0)) {
88 while (Chip_RIT_GetCounter(LPC_RITIMER) < (uint32_t) final) {}
89 }
90 else {
91 while ((int32_t) Chip_RIT_GetCounter(LPC_RITIMER) < final) {}
92 }
93 }
94
95 /* Sets up the SD event driven wakeup */
96 static void sdmmc_setup_wakeup(uint32_t bits)
97 {
98 /* Wait for IRQ - for an RTOS, you would pend on an event here with a IRQ based wakeup. */
99 NVIC_ClearPendingIRQ(SDIO_IRQn);
100 sdio_wait_exit = 0;
101 Chip_SDIF_SetIntMask(LPC_SDMMC, bits);
102 NVIC_EnableIRQ(SDIO_IRQn);
103 }
104
105 /* A better wait callback for SDMMC driven by the IRQ flag */
106 static uint32_t sdmmc_irq_driven_wait(void)
107 {
108 uint32_t status;
109
110 /* Wait for event, would be nice to have a timeout, but keep it simple */
111 while (sdio_wait_exit == 0) {}
112
113 /* Get status and clear interrupts */
114 status = Chip_SDIF_GetIntStatus(LPC_SDMMC);
115
116 return status;
117 }
118
119 #endif
120
121
122 /* HW set up function */
123 static void SetupHardware(void)
124 {
125 Board_Init();
126
127 #ifdef CFG_SDCARD
128 memset(&sdcardinfo, 0, sizeof(sdcardinfo));
129 sdcardinfo.evsetup_cb = sdmmc_setup_wakeup;
130 sdcardinfo.waitfunc_cb = sdmmc_irq_driven_wait;
131 sdcardinfo.msdelay_func = sdmmc_waitms;
132
133 /* SD/MMC initialization */
134 Board_SDMMC_Init();
135
136 /* The SDIO driver needs to know the SDIO clock rate */
137 Chip_SDIF_Init(LPC_SDMMC);
138
139 /* Wait for a card to be inserted */
140 #ifndef BOARD_NGX_XPLORER_18304330
141 /* NGX board ignored SD_CD pin */
142 /* Wait for a card to be inserted (note CD is not on the SDMMC power rail and can be polled without enabling SD slot power */
143 while (Chip_SDIF_CardNDetect(LPC_SDMMC)) {}
144 #endif
145
146 /* Acquire the card once ready */
147 if (!Chip_SDMMC_Acquire(LPC_SDMMC, &sdcardinfo)) {
148 DEBUGOUT("Card Acquire failed...\r\n");
149 while (1) {}
150 }
151 #endif
152
153 USB_Init(Disk_MS_Interface.Config.PortNumber, USB_MODE_Device);
154 #if defined(USB_DEVICE_ROM_DRIVER)
155 UsbdMsc_Init();
156 #endif
157 }
158
159 /*****************************************************************************
160 * Public functions
161 ****************************************************************************/
162
163 #ifdef CFG_SDCARD
164
169 void SDIO_IRQHandler(void)
170 {
171 /* All SD based register handling is done in the callback
172 function. The SDIO interrupt is not enabled as part of this
173 driver and needs to be enabled/disabled in the callbacks or
174 application as needed. This is to allow flexibility with IRQ
175 handling for applicaitons and RTOSes. */
176 /* Set wait exit flag to tell wait function we are ready. In an RTOS,
177 this would trigger wakeup of a thread waiting for the IRQ. */
178 NVIC_DisableIRQ(SDIO_IRQn);
179 sdio_wait_exit = 1;
180 }
181
182 #endif
183
190 int main(void)
191 {
192 SetupHardware();
193
194 for (;; ) {
195 #if !defined(USB_DEVICE_ROM_DRIVER)
196 MS_Device_USBTask(&Disk_MS_Interface);
197 USB_USBTask(Disk_MS_Interface.Config.PortNumber, USB_MODE_Device);
198 #endif
199 }
200 }
201
206 void EVENT_USB_Device_Connect(void)
207 {}
208
213 void EVENT_USB_Device_Disconnect(void)
214 {}
215
220 void EVENT_USB_Device_ConfigurationChanged(void)
221 {
222 bool ConfigSuccess = true;
223
224 ConfigSuccess &= MS_Device_ConfigureEndpoints(&Disk_MS_Interface);
225 }
226
231 void EVENT_USB_Device_ControlRequest(void)
232 {
233 MS_Device_ProcessControlRequest(&Disk_MS_Interface);
234 }
235
241 bool CALLBACK_MS_Device_SCSICommandReceived(USB_ClassInfo_MS_Device_t *const MSInterfaceInfo)
242 {
243 bool CommandSuccess;
244
245 CommandSuccess = SCSI_DecodeSCSICommand(MSInterfaceInfo);
246 return CommandSuccess;
247 }