| 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
usbd_hid.c
Go to the documentation of this file.
1 /*
2 * @brief USB ROM based HID Class driver functions
3 *
4 * @note
5 * Copyright(C) NXP Semiconductors, 2012
6 * All rights reserved.
7 *
8 * @par
9 * Software that is described herein is for illustrative purposes only
10 * which provides customers with programming information regarding the
11 * LPC products. This software is supplied "AS IS" without any warranties of
12 * any kind, and NXP Semiconductors and its licensor disclaim any and
13 * all warranties, express or implied, including all implied warranties of
14 * merchantability, fitness for a particular purpose and non-infringement of
15 * intellectual property rights. NXP Semiconductors assumes no responsibility
16 * or liability for the use of the software, conveys no license or rights under any
17 * patent, copyright, mask work right, or any other intellectual property rights in
18 * or to any products. NXP Semiconductors reserves the right to make changes
19 * in the software without notification. NXP Semiconductors also makes no
20 * representation or warranty that such application will be suitable for the
21 * specified use without further testing or modification.
22 *
23 * @par
24 * Permission to use, copy, modify, and distribute this software and its
25 * documentation is hereby granted, under NXP Semiconductors' and its
26 * licensor's relevant copyrights in the software, without fee, provided that it
27 * is used in conjunction with NXP Semiconductors microcontrollers. This
28 * copyright, permission, and disclaimer notice must appear in all copies of
29 * this code.
30 */
31
32 #define __INCLUDE_FROM_USB_DRIVER
33 #include "../../USBMode.h"
34
35 #if defined(USB_CAN_BE_DEVICE)
36 #include "../../Device.h"
37 #include "../../Endpoint.h"
38
39 #if defined(USB_DEVICE_ROM_DRIVER)
40
41 /* FIXME Abstract & make this size configurable */
42 //#define ROMDRIVER_HID_MEM_SIZE 0x800
43 //uint8_t usb_RomDriver_HID_buffer[ROMDRIVER_HID_MEM_SIZE] ATTR_ALIGNED(4) __BSS(USBRAM_SECTION);
44 uint8_t *reportinbuffer;
45 uint32_t reportinsize;
46
47 ErrorCode_t GetReport( USBD_HANDLE_T hHid, USB_SETUP_PACKET* pSetup, uint8_t** pBuffer, uint16_t* plength);
48 ErrorCode_t GetReport( USBD_HANDLE_T hHid, USB_SETUP_PACKET* pSetup, uint8_t** pBuffer, uint16_t* plength)
49 {
50 uint8_t report[reportinsize];
51
52 if(reportinbuffer == NULL)
53 return (LPC_OK);
54 memset(report, 0,reportinsize);
55 /* ReportID = SetupPacket.wValue.WB.L; */
56 switch (pSetup->wValue.WB.H) {
57 case HID_REPORT_INPUT:
58 if(CALLBACK_UsbdHid_IsReportChanged())
59 {
60 *pBuffer = reportinbuffer;
61 CALLBACK_UsbdHid_SetReportChange(false);
62 }
63 else
64 {
65 *pBuffer = report;
66 }
67 *plength = (uint16_t)reportinsize;
68 break;
69 case HID_REPORT_OUTPUT:
70 return (ERR_USBD_STALL); /* Not Supported */
71 case HID_REPORT_FEATURE:
72 return (ERR_USBD_STALL); /* Not Supported */
73 }
74 return (LPC_OK);
75 }
76 ErrorCode_t SetReport( USBD_HANDLE_T hHid, USB_SETUP_PACKET* pSetup, uint8_t** pBuffer, uint16_t length);
77 ErrorCode_t SetReport( USBD_HANDLE_T hHid, USB_SETUP_PACKET* pSetup, uint8_t** pBuffer, uint16_t length)
78 {
79 /* we will reuse standard EP0Buf */
80 if (length == 0)
81 return LPC_OK;
82 /* ReportID = SetupPacket.wValue.WB.L; */
83 switch (pSetup->wValue.WB.H) {
84 case HID_REPORT_INPUT:
85 return (ERR_USBD_STALL); /* Not Supported */
86 case HID_REPORT_OUTPUT:
87 CALLBACK_UsbdHid_SetReport(pBuffer,(uint32_t)length);
88 break;
89 case HID_REPORT_FEATURE:
90 return (ERR_USBD_STALL); /* Not Supported */
91 }
92 return (LPC_OK);
93 }
94 ErrorCode_t EpInHdlr (USBD_HANDLE_T hUsb, void* data, uint32_t event);
95 ErrorCode_t EpInHdlr (USBD_HANDLE_T hUsb, void* data, uint32_t event)
96 {
97 USB_HID_CTRL_T* pHidCtrl = (USB_HID_CTRL_T*)data;
98 uint8_t report[reportinsize];
99
100 if(reportinbuffer == NULL)
101 return (LPC_OK);
102 memset(report, 0,reportinsize);
103 switch (event) {
104 case USB_EVT_IN:
105 if(CALLBACK_UsbdHid_IsReportChanged())
106 {
107 USBD_API->hw->WriteEP(hUsb, pHidCtrl->epin_adr, reportinbuffer, reportinsize);
108 CALLBACK_UsbdHid_SetReportChange(false);
109 }
110 else
111 {
112 USBD_API->hw->WriteEP(hUsb, pHidCtrl->epin_adr, reportinbuffer, reportinsize);
113 }
114 break;
115 }
116 return LPC_OK;
117 }
118 void UsbdHid_Init(void)
119 {
120 USBD_HID_INIT_PARAM_T hid_param;
121 USB_HID_REPORT_T reports_data;
122
123 memset((void*)&hid_param, 0, sizeof(USBD_HID_INIT_PARAM_T));
124 /* Init reports_data */
125 reports_data.len = (uint16_t)CALLBACK_UsbdHid_Register_ReportDescriptor(&reports_data.desc);
126 reports_data.idle_time = 0;
127 /* Init reports buffer */
128 reportinsize = CALLBACK_UsbdHid_Register_ReportInBuffer(&reportinbuffer);
129
130 /* HID paramas */
131 hid_param.mem_base = (uint32_t)usb_RomDriver_HID_buffer;
132 hid_param.mem_size = (uint32_t)ROMDRIVER_HID_MEM_SIZE;
133 hid_param.max_reports = 1; //TODO let user configures this number
134 hid_param.intf_desc = (uint8_t*)CALLBACK_UsbdHid_Register_InterfaceDescriptor();
135 hid_param.report_data = &reports_data;
136 /* user defined functions */
137 hid_param.HID_GetReport = GetReport;
138 hid_param.HID_SetReport = SetReport;
139 hid_param.HID_EpIn_Hdlr = EpInHdlr;
140
141 USBD_API->hid->init(UsbHandle, &hid_param);
142 }
143
144 #endif /* USB_DEVICE_ROM_DRIVER */
145
146 #endif /* USB_CAN_BE_DEVICE */