2 * @brief Definition of functions exported by core layer of ROM based USB device stack
\r
5 * Copyright(C) NXP Semiconductors, 2012
\r
6 * All rights reserved.
\r
9 * Software that is described herein is for illustrative purposes only
\r
10 * which provides customers with programming information regarding the
\r
11 * LPC products. This software is supplied "AS IS" without any warranties of
\r
12 * any kind, and NXP Semiconductors and its licensor disclaim any and
\r
13 * all warranties, express or implied, including all implied warranties of
\r
14 * merchantability, fitness for a particular purpose and non-infringement of
\r
15 * intellectual property rights. NXP Semiconductors assumes no responsibility
\r
16 * or liability for the use of the software, conveys no license or rights under any
\r
17 * patent, copyright, mask work right, or any other intellectual property rights in
\r
18 * or to any products. NXP Semiconductors reserves the right to make changes
\r
19 * in the software without notification. NXP Semiconductors also makes no
\r
20 * representation or warranty that such application will be suitable for the
\r
21 * specified use without further testing or modification.
\r
24 * Permission to use, copy, modify, and distribute this software and its
\r
25 * documentation is hereby granted, under NXP Semiconductors' and its
\r
26 * licensor's relevant copyrights in the software, without fee, provided that it
\r
27 * is used in conjunction with NXP Semiconductors microcontrollers. This
\r
28 * copyright, permission, and disclaimer notice must appear in all copies of
\r
32 #ifndef __MW_USBD_CORE_H__
\r
33 #define __MW_USBD_CORE_H__
\r
37 #include "lpc_types.h"
\r
39 /** @ingroup Group_USBD
\r
40 * @defgroup USBD_Core USB Core Layer
\r
41 * @section Sec_CoreModDescription Module Description
\r
42 * The USB Core Layer implements the device abstraction defined in the <em> Universal Serial Bus Specification, </em>
\r
43 * for applications to interact with the USB device interface on the device. The software in this layer responds to
\r
44 * standard requests and returns standard descriptors. In current stack the Init() routine part of
\r
45 * @ref USBD_HW_API_T structure initializes both hardware layer and core layer.
\r
49 /* function pointer types */
\r
51 /** @ingroup USBD_Core
\r
53 * @brief USB device stack's event callback function type.
\r
55 * The USB device stack exposes several event triggers through callback to application layer. The
\r
56 * application layer can register methods to be called when such USB event happens.
\r
58 * @param hUsb Handle to the USB device stack.
\r
59 * @return The call back should returns @ref ErrorCode_t type to indicate success or error condition.
\r
60 * @retval LPC_OK On success
\r
61 * @retval ERR_USBD_UNHANDLED Event is not handled hence pass the event to next in line.
\r
62 * @retval ERR_USBD_xxx Other error conditions.
\r
65 typedef ErrorCode_t (*USB_CB_T) (USBD_HANDLE_T hUsb);
\r
67 /** @ingroup USBD_Core
\r
68 * @typedef USB_PARAM_CB_T
\r
69 * @brief USB device stack's event callback function type.
\r
71 * The USB device stack exposes several event triggers through callback to application layer. The
\r
72 * application layer can register methods to be called when such USB event happens.
\r
74 * @param hUsb Handle to the USB device stack.
\r
75 * @param param1 Extra information related to the event.
\r
76 * @return The call back should returns @ref ErrorCode_t type to indicate success or error condition.
\r
77 * @retval LPC_OK On success
\r
78 * @retval ERR_USBD_UNHANDLED Event is not handled hence pass the event to next in line.
\r
79 * @retval ERR_USBD_xxx For other error conditions.
\r
82 typedef ErrorCode_t (*USB_PARAM_CB_T) (USBD_HANDLE_T hUsb, uint32_t param1);
\r
84 /** @ingroup USBD_Core
\r
85 * @typedef USB_EP_HANDLER_T
\r
86 * @brief USBD setup request and endpoint event handler type.
\r
88 * The application layer should define the custom class's EP0 handler with function signature.
\r
89 * The stack calls all the registered class handlers on any EP0 event before going through default
\r
90 * handling of the event. This gives the class handlers to implement class specific request handlers
\r
91 * and also to override the default stack handling for a particular event targeted to the interface.
\r
92 * If an event is not handled by the callback the function should return ERR_USBD_UNHANDLED. For all
\r
93 * other return codes the stack assumes that callback has taken care of the event and hence will not
\r
94 * process the event any further and issues a STALL condition on EP0 indicating error to the host.
\r
96 * For endpoint interrupt handler the return value is ignored by the stack.
\r
98 * @param hUsb Handle to the USB device stack.
\r
99 * @param data Pointer to the data which will be passed when callback function is called by the stack.
\r
100 * @param event Type of endpoint event. See @ref USBD_EVENT_T for more details.
\r
101 * @return The call back should returns @ref ErrorCode_t type to indicate success or error condition.
\r
102 * @retval LPC_OK On success.
\r
103 * @retval ERR_USBD_UNHANDLED Event is not handled hence pass the event to next in line.
\r
104 * @retval ERR_USBD_xxx For other error conditions.
\r
107 typedef ErrorCode_t (*USB_EP_HANDLER_T)(USBD_HANDLE_T hUsb, void* data, uint32_t event);
\r
110 /** @ingroup USBD_Core
\r
111 * @brief USB descriptors data structure.
\r
112 * @ingroup USBD_Core
\r
114 * @details This structure is used as part of USB device stack initialisation
\r
115 * parameter structure @ref USBD_API_INIT_PARAM_T. This structure contains
\r
116 * pointers to various descriptor arrays needed by the stack. These descriptors
\r
117 * are reported to USB host as part of enumerations process.
\r
119 * @note All descriptor pointers assigned in this structure should be on 4 byte
\r
120 * aligned address boundary.
\r
122 typedef struct _USB_CORE_DESCS_T
\r
124 uint8_t *device_desc; /**< Pointer to USB device descriptor */
\r
125 uint8_t *string_desc; /**< Pointer to array of USB string descriptors */
\r
126 uint8_t *full_speed_desc; /**< Pointer to USB device configuration descriptor
\r
127 * when device is operating in full speed mode.
\r
129 uint8_t *high_speed_desc; /**< Pointer to USB device configuration descriptor
\r
130 * when device is operating in high speed mode. For
\r
131 * full-speed only implementation this pointer should
\r
132 * be same as full_speed_desc.
\r
134 uint8_t *device_qualifier; /**< Pointer to USB device qualifier descriptor. For
\r
135 * full-speed only implementation this pointer should
\r
136 * be set to null (0).
\r
138 } USB_CORE_DESCS_T;
\r
140 /** @brief USB device stack initilization parameter data structure.
\r
141 * @ingroup USBD_Core
\r
143 * @details This data structure is used to pass initialization parameters to the
\r
144 * USB device stack's init function.
\r
147 typedef struct USBD_API_INIT_PARAM
\r
149 uint32_t usb_reg_base; /**< USB device controller's base register address. */
\r
150 uint32_t mem_base; /**< Base memory location from where the stack can allocate
\r
151 data and buffers. @note The memory address set in this field
\r
152 should be accessible by USB DMA controller. Also this value
\r
153 should be aligned on 2048 byte boundary.
\r
155 uint32_t mem_size; /**< The size of memory buffer which stack can use.
\r
156 @note The \em mem_size should be greater than the size
\r
157 returned by USBD_HW_API::GetMemSize() routine.*/
\r
158 uint8_t max_num_ep; /**< max number of endpoints supported by the USB device
\r
159 controller instance (specified by \em usb_reg_base field)
\r
160 to which this instance of stack is attached.
\r
163 /* USB Device Events Callback Functions */
\r
164 /** Event for USB interface reset. This event fires when the USB host requests that the device
\r
165 * reset its interface. This event fires after the control endpoint has been automatically
\r
166 * configured by the library.
\r
168 * @note This event is called from USB_ISR context and hence is time-critical. Having delays in this
\r
169 * callback will prevent the device from enumerating correctly or operate properly.
\r
172 USB_CB_T USB_Reset_Event;
\r
174 /** Event for USB suspend. This event fires when the USB host suspends the device by halting its
\r
175 * transmission of Start Of Frame pulses to the device. This is generally hooked in order to move
\r
176 * the device over to a low power state until the host wakes up the device.
\r
178 * @note This event is called from USB_ISR context and hence is time-critical. Having delays in this
\r
179 * callback will cause other system issues.
\r
181 USB_CB_T USB_Suspend_Event;
\r
183 /** Event for USB wake up or resume. This event fires when a the USB device interface is suspended
\r
184 * and the host wakes up the device by supplying Start Of Frame pulses. This is generally
\r
185 * hooked to pull the user application out of a low power state and back into normal operating
\r
188 * @note This event is called from USB_ISR context and hence is time-critical. Having delays in this
\r
189 * callback will cause other system issues.
\r
192 USB_CB_T USB_Resume_Event;
\r
194 /** Reserved parameter should be set to zero. */
\r
195 USB_CB_T reserved_sbz;
\r
197 /** Event for USB Start Of Frame detection, when enabled. This event fires at the start of each USB
\r
198 * frame, once per millisecond in full-speed mode or once per 125 microseconds in high-speed mode,
\r
199 * and is synchronized to the USB bus.
\r
201 * This event is time-critical; it is run once per millisecond (full-speed mode) and thus long handlers
\r
202 * will significantly degrade device performance. This event should only be enabled when needed to
\r
203 * reduce device wake-ups.
\r
205 * @note This event is not normally active - it must be manually enabled and disabled via the USB interrupt
\r
209 USB_CB_T USB_SOF_Event;
\r
211 /** Event for remote wakeup configururation, when enabled. This event fires when the USB host
\r
212 * request the device to configure itself for remote wake-up capability. The USB host sends
\r
213 * this request to device which report remote wakeup capable in their device descriptors,
\r
214 * before going to low-power state. The application layer should implement this callback if
\r
215 * they have any special on board circuit to trigerr remote wake up event.
\r
217 * This event is time-critical; it is run once per millisecond (full-speed mode) and thus long handlers
\r
218 * will significantly degrade device performance.
\r
221 * @param hUsb Handle to the USB device stack.
\r
222 * @param param1 When 0 - Clear the wakeup configuration, 1 - Enable the wake-up configuration.
\r
223 * @return The call back should return @ref ErrorCode_t type to indicate success or error condition.
\r
225 USB_PARAM_CB_T USB_WakeUpCfg;
\r
227 /** Reserved parameter should be set to zero. */
\r
228 USB_PARAM_CB_T USB_Power_Event;
\r
230 /** Event for error condition. This event fires when USB device controller detect
\r
231 * an error condition in the system.
\r
234 * @param hUsb Handle to the USB device stack.
\r
235 * @param param1 USB device interrupt status register.
\r
236 * @return The call back should return @ref ErrorCode_t type to indicate success or error condition.
\r
238 USB_PARAM_CB_T USB_Error_Event;
\r
240 /* USB Core Events Callback Functions */
\r
241 /** Event for USB configuration number changed. This event fires when a the USB host changes the
\r
242 * selected configuration number. On receiving configuration change request from host, the stack
\r
243 * enables/configures the endpoints needed by the new configuration before calling this callback
\r
246 * @note This event is called from USB_ISR context and hence is time-critical. Having delays in this
\r
247 * callback will prevent the device from enumerating correctly or operate properly.
\r
250 USB_CB_T USB_Configure_Event;
\r
252 /** Event for USB interface setting changed. This event fires when a the USB host changes the
\r
253 * interface setting to one of alternate interface settings. On receiving interface change
\r
254 * request from host, the stack enables/configures the endpoints needed by the new alternate
\r
255 * interface setting before calling this callback function.
\r
257 * @note This event is called from USB_ISR context and hence is time-critical. Having delays in this
\r
258 * callback will prevent the device from enumerating correctly or operate properly.
\r
261 USB_CB_T USB_Interface_Event;
\r
263 /** Event for USB feature changed. This event fires when a the USB host send set/clear feature
\r
264 * request. The stack handles this request for USB_FEATURE_REMOTE_WAKEUP, USB_FEATURE_TEST_MODE
\r
265 * and USB_FEATURE_ENDPOINT_STALL features only. On receiving feature request from host, the
\r
266 * stack handle the request appropriately and then calls this callback function.
\r
268 * @note This event is called from USB_ISR context and hence is time-critical. Having delays in this
\r
269 * callback will prevent the device from enumerating correctly or operate properly.
\r
272 USB_CB_T USB_Feature_Event;
\r
274 /* cache and mmu translation functions */
\r
275 /** Reserved parameter for future use. should be set to zero. */
\r
276 uint32_t (* virt_to_phys)(void* vaddr);
\r
277 /** Reserved parameter for future use. should be set to zero. */
\r
278 void (* cache_flush)(uint32_t* start_adr, uint32_t* end_adr);
\r
280 } USBD_API_INIT_PARAM_T;
\r
283 /** @brief USBD stack Core API functions structure.
\r
284 * @ingroup USBD_Core
\r
286 * @details This module exposes functions which interact directly with USB device stack's core layer.
\r
287 * The application layer uses this component when it has to implement custom class function driver or
\r
288 * standard class function driver which is not part of the current USB device stack.
\r
289 * The functions exposed by this interface are to register class specific EP0 handlers and corresponding
\r
290 * utility functions to manipulate EP0 state machine of the stack. This interface also exposes
\r
291 * function to register custom endpoint interrupt handler.
\r
294 typedef struct USBD_CORE_API
\r
296 /** @fn ErrorCode_t RegisterClassHandler(USBD_HANDLE_T hUsb, USB_EP_HANDLER_T pfn, void* data)
\r
297 * Function to register class specific EP0 event handler with USB device stack.
\r
299 * The application layer uses this function when it has to register the custom class's EP0 handler.
\r
300 * The stack calls all the registered class handlers on any EP0 event before going through default
\r
301 * handling of the event. This gives the class handlers to implement class specific request handlers
\r
302 * and also to override the default stack handling for a particular event targeted to the interface.
\r
303 * Check USB_EP_HANDLER_T for more details on how the callback function should be implemented. Also
\r
304 * application layer could use this function to register EP0 handler which responds to vendor specific
\r
307 * @param hUsb Handle to the USB device stack.
\r
308 * @param pfn Class specific EP0 handler function.
\r
309 * @param data Pointer to the data which will be passed when callback function is called by the stack.
\r
310 * @return Returns @ref ErrorCode_t type to indicate success or error condition.
\r
311 * @retval LPC_OK On success
\r
312 * @retval ERR_USBD_TOO_MANY_CLASS_HDLR(0x0004000c) The number of class handlers registered is
\r
313 greater than the number of handlers allowed by the stack.
\r
316 ErrorCode_t (*RegisterClassHandler)(USBD_HANDLE_T hUsb, USB_EP_HANDLER_T pfn, void* data);
\r
318 /** @fn ErrorCode_t RegisterEpHandler(USBD_HANDLE_T hUsb, uint32_t ep_index, USB_EP_HANDLER_T pfn, void* data)
\r
319 * Function to register interrupt/event handler for the requested endpoint with USB device stack.
\r
321 * The application layer uses this function to register the custom class's EP0 handler.
\r
322 * The stack calls all the registered class handlers on any EP0 event before going through default
\r
323 * handling of the event. This gives the class handlers to implement class specific request handlers
\r
324 * and also to override the default stack handling for a particular event targeted to the interface.
\r
325 * Check USB_EP_HANDLER_T for more details on how the callback function should be implemented.
\r
327 * @param hUsb Handle to the USB device stack.
\r
328 * @param ep_index Class specific EP0 handler function.
\r
329 * @param pfn Class specific EP0 handler function.
\r
330 * @param data Pointer to the data which will be passed when callback function is called by the stack.
\r
331 * @return Returns @ref ErrorCode_t type to indicate success or error condition.
\r
332 * @retval LPC_OK On success
\r
333 * @retval ERR_USBD_TOO_MANY_CLASS_HDLR(0x0004000c) Too many endpoint handlers.
\r
336 ErrorCode_t (*RegisterEpHandler)(USBD_HANDLE_T hUsb, uint32_t ep_index, USB_EP_HANDLER_T pfn, void* data);
\r
338 /** @fn void SetupStage(USBD_HANDLE_T hUsb)
\r
339 * Function to set EP0 state machine in setup state.
\r
341 * This function is called by USB stack and the application layer to
\r
342 * set the EP0 state machine in setup state. This function will read
\r
343 * the setup packet received from USB host into stack's buffer.
\r
345 * @note This interface is provided to users to invoke this function in other
\r
346 * scenarios which are not handle by current stack. In most user applications
\r
347 * this function is not called directly.Also this function can be used by
\r
348 * users who are selectively modifying the USB device stack's standard handlers
\r
349 * through callback interface exposed by the stack.
\r
351 * @param hUsb Handle to the USB device stack.
\r
354 void (*SetupStage )(USBD_HANDLE_T hUsb);
\r
356 /** @fn void DataInStage(USBD_HANDLE_T hUsb)
\r
357 * Function to set EP0 state machine in data_in state.
\r
359 * This function is called by USB stack and the application layer to
\r
360 * set the EP0 state machine in data_in state. This function will write
\r
361 * the data present in EP0Data buffer to EP0 FIFO for tranmission to host.
\r
363 * @note This interface is provided to users to invoke this function in other
\r
364 * scenarios which are not handle by current stack. In most user applications
\r
365 * this function is not called directly.Also this function can be used by
\r
366 * users who are selectively modifying the USB device stack's standard handlers
\r
367 * through callback interface exposed by the stack.
\r
369 * @param hUsb Handle to the USB device stack.
\r
372 void (*DataInStage)(USBD_HANDLE_T hUsb);
\r
374 /** @fn void DataOutStage(USBD_HANDLE_T hUsb)
\r
375 * Function to set EP0 state machine in data_out state.
\r
377 * This function is called by USB stack and the application layer to
\r
378 * set the EP0 state machine in data_out state. This function will read
\r
379 * the control data (EP0 out packets) received from USB host into EP0Data buffer.
\r
381 * @note This interface is provided to users to invoke this function in other
\r
382 * scenarios which are not handle by current stack. In most user applications
\r
383 * this function is not called directly.Also this function can be used by
\r
384 * users who are selectively modifying the USB device stack's standard handlers
\r
385 * through callback interface exposed by the stack.
\r
387 * @param hUsb Handle to the USB device stack.
\r
390 void (*DataOutStage)(USBD_HANDLE_T hUsb);
\r
392 /** @fn void StatusInStage(USBD_HANDLE_T hUsb)
\r
393 * Function to set EP0 state machine in status_in state.
\r
395 * This function is called by USB stack and the application layer to
\r
396 * set the EP0 state machine in status_in state. This function will send
\r
397 * zero length IN packet on EP0 to host, indicating positive status.
\r
399 * @note This interface is provided to users to invoke this function in other
\r
400 * scenarios which are not handle by current stack. In most user applications
\r
401 * this function is not called directly.Also this function can be used by
\r
402 * users who are selectively modifying the USB device stack's standard handlers
\r
403 * through callback interface exposed by the stack.
\r
405 * @param hUsb Handle to the USB device stack.
\r
408 void (*StatusInStage)(USBD_HANDLE_T hUsb);
\r
409 /** @fn void StatusOutStage(USBD_HANDLE_T hUsb)
\r
410 * Function to set EP0 state machine in status_out state.
\r
412 * This function is called by USB stack and the application layer to
\r
413 * set the EP0 state machine in status_out state. This function will read
\r
414 * the zero length OUT packet received from USB host on EP0.
\r
416 * @note This interface is provided to users to invoke this function in other
\r
417 * scenarios which are not handle by current stack. In most user applications
\r
418 * this function is not called directly.Also this function can be used by
\r
419 * users who are selectively modifying the USB device stack's standard handlers
\r
420 * through callback interface exposed by the stack.
\r
422 * @param hUsb Handle to the USB device stack.
\r
425 void (*StatusOutStage)(USBD_HANDLE_T hUsb);
\r
427 /** @fn void StallEp0(USBD_HANDLE_T hUsb)
\r
428 * Function to set EP0 state machine in stall state.
\r
430 * This function is called by USB stack and the application layer to
\r
431 * generate STALL signalling on EP0 endpoint. This function will also
\r
432 * reset the EP0Data buffer.
\r
434 * @note This interface is provided to users to invoke this function in other
\r
435 * scenarios which are not handle by current stack. In most user applications
\r
436 * this function is not called directly.Also this function can be used by
\r
437 * users who are selectively modifying the USB device stack's standard handlers
\r
438 * through callback interface exposed by the stack.
\r
440 * @param hUsb Handle to the USB device stack.
\r
443 void (*StallEp0)(USBD_HANDLE_T hUsb);
\r
447 /*-----------------------------------------------------------------------------
\r
448 * Private functions & structures prototypes
\r
449 *-----------------------------------------------------------------------------*/
\r
451 /** @cond ADVANCED_API */
\r
453 /* forward declaration */
\r
454 struct _USB_CORE_CTRL_T;
\r
455 typedef struct _USB_CORE_CTRL_T USB_CORE_CTRL_T;
\r
457 /* USB device Speed status defines */
\r
458 #define USB_FULL_SPEED 0
\r
459 #define USB_HIGH_SPEED 1
\r
461 /* USB Endpoint Data Structure */
\r
462 typedef struct _USB_EP_DATA
\r
468 #define USB_MAX_IF_NUM 8
\r
469 #define USB_MAX_EP_NUM 6
\r
471 /* USB core controller data structure */
\r
472 struct _USB_CORE_CTRL_T
\r
474 /* overridable function pointers ~ c++ style virtual functions*/
\r
475 USB_CB_T USB_EvtSetupHandler;
\r
476 USB_CB_T USB_EvtOutHandler;
\r
477 USB_PARAM_CB_T USB_ReqVendor;
\r
478 USB_CB_T USB_ReqGetStatus;
\r
479 USB_CB_T USB_ReqGetDescriptor;
\r
480 USB_CB_T USB_ReqGetConfiguration;
\r
481 USB_CB_T USB_ReqSetConfiguration;
\r
482 USB_CB_T USB_ReqGetInterface;
\r
483 USB_CB_T USB_ReqSetInterface;
\r
484 USB_PARAM_CB_T USB_ReqSetClrFeature;
\r
486 /* USB Device Events Callback Functions */
\r
487 USB_CB_T USB_Reset_Event;
\r
488 USB_CB_T USB_Suspend_Event;
\r
489 USB_CB_T USB_Resume_Event;
\r
490 USB_CB_T USB_SOF_Event;
\r
491 USB_PARAM_CB_T USB_Power_Event;
\r
492 USB_PARAM_CB_T USB_Error_Event;
\r
493 USB_PARAM_CB_T USB_WakeUpCfg;
\r
495 /* USB Core Events Callback Functions */
\r
496 USB_CB_T USB_Configure_Event;
\r
497 USB_CB_T USB_Interface_Event;
\r
498 USB_CB_T USB_Feature_Event;
\r
500 /* cache and mmu translation functions */
\r
501 uint32_t (* virt_to_phys)(void* vaddr);
\r
502 void (* cache_flush)(uint32_t* start_adr, uint32_t* end_adr);
\r
504 /* event handlers for endpoints. */
\r
505 USB_EP_HANDLER_T ep_event_hdlr[2 * USB_MAX_EP_NUM];
\r
506 void* ep_hdlr_data[2 * USB_MAX_EP_NUM];
\r
508 /* USB class handlers */
\r
509 USB_EP_HANDLER_T ep0_hdlr_cb[USB_MAX_IF_NUM];
\r
510 void* ep0_cb_data[USB_MAX_IF_NUM];
\r
511 uint8_t num_ep0_hdlrs;
\r
512 /* USB Core data Variables */
\r
513 uint8_t max_num_ep; /* max number of endpoints supported by the HW */
\r
514 uint8_t device_speed;
\r
515 uint8_t num_interfaces;
\r
516 uint8_t device_addr;
\r
517 uint8_t config_value;
\r
518 uint16_t device_status;
\r
519 uint8_t *device_desc;
\r
520 uint8_t *string_desc;
\r
521 uint8_t *full_speed_desc;
\r
522 uint8_t *high_speed_desc;
\r
523 uint8_t *device_qualifier;
\r
527 uint8_t alt_setting[USB_MAX_IF_NUM];
\r
528 /* HW driver data pointer */
\r
531 /* USB Endpoint 0 Data Info */
\r
532 USB_EP_DATA EP0Data;
\r
534 /* USB Endpoint 0 Buffer */
\r
536 uint8_t EP0Buf[64];
\r
538 /* USB Setup Packet */
\r
540 USB_SETUP_PACKET SetupPacket;
\r
545 static INLINE void USB_SetSpeedMode(USB_CORE_CTRL_T* pCtrl, uint8_t mode)
\r
547 pCtrl->device_speed = mode;
\r
551 #endif /* __MW_USBD_CORE_H__ */
\r