1 /*
2 * @brief This file contains USB HID Mouse example using USB ROM Drivers.
3 *
4 * @note
5 * Copyright(C) NXP Semiconductors, 2013
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 #include "board.h"
33 #include <stdint.h>
34 #include <string.h>
35 #include "usbd_rom_api.h"
36 #include "hid_mouse.h"
37
38 /*****************************************************************************
39 * Private types/enumerations/variables
40 ****************************************************************************/
41
42 /**
43 * @brief Structure to hold mouse data
44 */
45
46 typedef struct {
47 USBD_HANDLE_T hUsb; /*!< Handle to USB stack. */
48 uint8_t report[MOUSE_REPORT_SIZE]; /*!< Last report data */
49 uint8_t tx_busy; /*!< Flag indicating whether a report is pending in endpoint queue. */
50 } Mouse_Ctrl_T;
51
52 /** Singleton instance of mouse control */
53 static Mouse_Ctrl_T g_mouse;
54
55 /*****************************************************************************
56 * Public types/enumerations/variables
57 ****************************************************************************/
58
59 extern const uint8_t Mouse_ReportDescriptor[];
60 extern const uint16_t Mouse_ReportDescSize;
61
62 /*****************************************************************************
63 * Private functions
64 ****************************************************************************/
65
setXYMouseReport(uint8_t * rep,int8_t xVal,int8_t yVal)66 static void setXYMouseReport(uint8_t *rep, int8_t xVal, int8_t yVal)
67 {
68 rep[1] = (uint8_t) xVal;
69 rep[2] = (uint8_t) yVal;
70 }
71
setLeftButtonMouseReport(uint8_t * rep,uint8_t state)72 static void setLeftButtonMouseReport(uint8_t *rep, uint8_t state)
73 {
74 if (state) {
75 rep[0] |= 0x01;
76 }
77 else {
78 rep[0] &= ~0x01;
79 }
80 }
81
82 /* Routine to update mouse state report */
Mouse_UpdateReport(void)83 static void Mouse_UpdateReport(void)
84 {
85 uint8_t joystick_status = Joystick_GetStatus();
86 CLEAR_HID_MOUSE_REPORT(&g_mouse.report[0]);
87
88 switch (joystick_status) {
89 case JOY_PRESS:
90 setLeftButtonMouseReport(g_mouse.report, 1);
91 break;
92
93 case JOY_LEFT:
94 setXYMouseReport(g_mouse.report, -4, 0);
95 break;
96
97 case JOY_RIGHT:
98 setXYMouseReport(g_mouse.report, 4, 0);
99 break;
100
101 case JOY_UP:
102 setXYMouseReport(g_mouse.report, 0, -4);
103 break;
104
105 case JOY_DOWN:
106 setXYMouseReport(g_mouse.report, 0, 4);
107 break;
108 }
109 }
110
111 /* HID Get Report Request Callback. Called automatically on HID Get Report Request */
Mouse_GetReport(USBD_HANDLE_T hHid,USB_SETUP_PACKET * pSetup,uint8_t ** pBuffer,uint16_t * plength)112 static ErrorCode_t Mouse_GetReport(USBD_HANDLE_T hHid, USB_SETUP_PACKET *pSetup, uint8_t * *pBuffer, uint16_t *plength)
113 {
114 /* ReportID = SetupPacket.wValue.WB.L; */
115 switch (pSetup->wValue.WB.H) {
116 case HID_REPORT_INPUT:
117 Mouse_UpdateReport();
118 *pBuffer = &g_mouse.report[0];
119 *plength = MOUSE_REPORT_SIZE;
120 break;
121
122 case HID_REPORT_OUTPUT: /* Not Supported */
123 case HID_REPORT_FEATURE: /* Not Supported */
124 return ERR_USBD_STALL;
125 }
126 return LPC_OK;
127 }
128
129 /* HID Set Report Request Callback. Called automatically on HID Set Report Request */
Mouse_SetReport(USBD_HANDLE_T hHid,USB_SETUP_PACKET * pSetup,uint8_t ** pBuffer,uint16_t length)130 static ErrorCode_t Mouse_SetReport(USBD_HANDLE_T hHid, USB_SETUP_PACKET *pSetup, uint8_t * *pBuffer, uint16_t length)
131 {
132 /* we will reuse standard EP0Buf */
133 if (length == 0) {
134 return LPC_OK;
135 }
136 /* ReportID = SetupPacket.wValue.WB.L; */
137 switch (pSetup->wValue.WB.H) {
138 case HID_REPORT_INPUT: /* Not Supported */
139 case HID_REPORT_OUTPUT: /* Not Supported */
140 case HID_REPORT_FEATURE: /* Not Supported */
141 return ERR_USBD_STALL;
142 }
143 return LPC_OK;
144 }
145
146 /* HID interrupt IN endpoint handler. */
Mouse_EpIN_Hdlr(USBD_HANDLE_T hUsb,void * data,uint32_t event)147 static ErrorCode_t Mouse_EpIN_Hdlr(USBD_HANDLE_T hUsb, void *data, uint32_t event)
148 {
149 switch (event) {
150 case USB_EVT_IN:
151 /* USB_EVT_IN occurs when HW completes sending IN packet. So clear the
152 busy flag for main loop to queue next packet.
153 */
154 g_mouse.tx_busy = 0;
155 break;
156 }
157 return LPC_OK;
158 }
159
160 /*****************************************************************************
161 * Public functions
162 ****************************************************************************/
163
164 /* Mouse init routine. */
Mouse_Init(USBD_HANDLE_T hUsb,USB_INTERFACE_DESCRIPTOR * pIntfDesc,uint32_t * mem_base,uint32_t * mem_size)165 ErrorCode_t Mouse_Init(USBD_HANDLE_T hUsb,
166 USB_INTERFACE_DESCRIPTOR *pIntfDesc,
167 uint32_t *mem_base,
168 uint32_t *mem_size)
169 {
170 USBD_HID_INIT_PARAM_T hid_param;
171 USB_HID_REPORT_T reports_data[1];
172 ErrorCode_t ret = LPC_OK;
173
174 /* Do a quick check of if the interface descriptor passed is the right one. */
175 if ((pIntfDesc == 0) || (pIntfDesc->bInterfaceClass != USB_DEVICE_CLASS_HUMAN_INTERFACE)) {
176 return ERR_FAILED;
177 }
178
179 /* init joystick control */
180 Board_Joystick_Init();
181
182 /* Init HID params */
183 memset((void *) &hid_param, 0, sizeof(USBD_HID_INIT_PARAM_T));
184 hid_param.max_reports = 1;
185 hid_param.mem_base = *mem_base;
186 hid_param.mem_size = *mem_size;
187 hid_param.intf_desc = (uint8_t *) pIntfDesc;
188 /* user defined functions */
189 hid_param.HID_GetReport = Mouse_GetReport;
190 hid_param.HID_SetReport = Mouse_SetReport;
191 hid_param.HID_EpIn_Hdlr = Mouse_EpIN_Hdlr;
192 /* Init reports_data */
193 reports_data[0].len = Mouse_ReportDescSize;
194 reports_data[0].idle_time = 0;
195 reports_data[0].desc = (uint8_t *) &Mouse_ReportDescriptor[0];
196 hid_param.report_data = reports_data;
197
198 ret = USBD_API->hid->init(hUsb, &hid_param);
199
200 /* update memory variables */
201 *mem_base = hid_param.mem_base;
202 *mem_size = hid_param.mem_size;
203 /* store stack handle for later use. */
204 g_mouse.hUsb = hUsb;
205
206 return ret;
207 }
208
209 /* Mouse tasks routine. */
Mouse_Tasks(void)210 void Mouse_Tasks(void)
211 {
212 /* check device is configured before sending report. */
213 if ( USB_IsConfigured(g_mouse.hUsb)) {
214 if (g_mouse.tx_busy == 0) {
215 /* update report based on board state */
216 Mouse_UpdateReport();
217 /* send report data */
218 g_mouse.tx_busy = 1;
219 USBD_API->hw->WriteEP(g_mouse.hUsb, HID_EP_IN, &g_mouse.report[0], MOUSE_REPORT_SIZE);
220 }
221 }
222 else {
223 /* reset busy flag if we get disconnected. */
224 g_mouse.tx_busy = 0;
225 }
226 }
227