1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
|
//*****************************************************************************
//
// usbhhidmouse.c - This file holds the application interfaces for USB
// mouse devices.
//
// Copyright (c) 2008-2012 Texas Instruments Incorporated. All rights reserved.
// Software License Agreement
//
// Texas Instruments (TI) is supplying this software for use solely and
// exclusively on TI's microcontroller products. The software is owned by
// TI and/or its suppliers, and is protected under applicable copyright
// laws. You may not combine this software with "viral" open-source
// software in order to form a larger program.
//
// THIS SOFTWARE IS PROVIDED "AS IS" AND WITH ALL FAULTS.
// NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT
// NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. TI SHALL NOT, UNDER ANY
// CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
// DAMAGES, FOR ANY REASON WHATSOEVER.
//
// This is part of revision 9453 of the Stellaris USB Library.
//
//*****************************************************************************
#include "inc/hw_types.h"
#include "usblib/usblib.h"
#include "usblib/host/usbhost.h"
#include "usblib/usbhid.h"
#include "usblib/host/usbhhid.h"
#include "usblib/host/usbhhidmouse.h"
//*****************************************************************************
//
//! \addtogroup usblib_host_device
//! @{
//
//*****************************************************************************
//*****************************************************************************
//
// Prototypes for local functions.
//
//*****************************************************************************
static unsigned long USBHMouseCallback(void *pvCBData,
unsigned long ulEvent,
unsigned long ulMsgParam,
void *pvMsgData);
//*****************************************************************************
//
// The size of a USB mouse report.
//
//*****************************************************************************
#define USBHMS_REPORT_SIZE 4
//*****************************************************************************
//
// These are the flags for the tUSBHMouse.ulHIDFlags member variable.
//
//*****************************************************************************
#define USBHMS_DEVICE_PRESENT 0x00000001
//*****************************************************************************
//
// This is the structure definition for a mouse device instance.
//
//*****************************************************************************
typedef struct
{
//
// Global flags for an instance of a mouse.
//
unsigned long ulHIDFlags;
//
// The applications registered callback.
//
tUSBCallback pfnCallback;
//
// The current state of the buttons.
//
unsigned char ucButtons;
//
// This is a local buffer to hold the current HID report that comes up
// from the HID driver layer.
//
unsigned char pucBuffer[USBHMS_REPORT_SIZE];
//
// Heap data for the mouse currently used to read the HID Report
// Descriptor.
//
unsigned char *pucHeap;
//
// Size of the heap in bytes.
//
unsigned long ulHeapSize;
//
// This is the instance value for the HID device that will be used for the
// mouse.
//
unsigned long ulMouseInstance;
}
tUSBHMouse;
//*****************************************************************************
//
// This is the per instance information for a mouse device.
//
//*****************************************************************************
static tUSBHMouse g_sUSBHMouse =
{
0
};
//*****************************************************************************
//
//! This function is used open an instance of a mouse.
//!
//! \param pfnCallback is the callback function to call when new events occur
//! with the mouse returned.
//! \param pucBuffer is the memory used by the driver to interact with the
//! USB mouse.
//! \param ulSize is the size of the buffer provided by \e pucBuffer.
//!
//! This function is used to open an instance of the mouse. The value
//! returned from this function should be used as the instance identifier for
//! all other USBHMouse calls. The \e pucBuffer memory buffer is used to
//! access the mouse. The buffer size required is at least enough to hold
//! a normal report descriptor for the device.
//!
//! \return Returns the instance identifier for the mouse that is attached.
//! If there is no mouse present this will return 0.
//
//*****************************************************************************
unsigned long
USBHMouseOpen(tUSBCallback pfnCallback, unsigned char *pucBuffer,
unsigned long ulSize)
{
//
// Save the callback and data pointers.
//
g_sUSBHMouse.pfnCallback = pfnCallback;
//
// Save the instance pointer for the HID device that was opened.
//
g_sUSBHMouse.ulMouseInstance = USBHHIDOpen(USBH_HID_DEV_MOUSE,
USBHMouseCallback,
(unsigned long)&g_sUSBHMouse);
//
// Save the heap buffer and size.
//
g_sUSBHMouse.pucHeap = pucBuffer;
g_sUSBHMouse.ulHeapSize = ulSize;
return((unsigned long)&g_sUSBHMouse);
}
//*****************************************************************************
//
//! This function is used close an instance of a mouse.
//!
//! \param ulInstance is the instance value for this mouse.
//!
//! This function is used to close an instance of the mouse that was opened
//! with a call to USBHMouseOpen(). The \e ulInstance value is the value
//! that was returned when the application called USBHMouseOpen().
//!
//! \return Returns 0.
//
//*****************************************************************************
unsigned long
USBHMouseClose(unsigned long ulInstance)
{
tUSBHMouse *pUSBHMouse;
//
// Recover the pointer to the instance data.
//
pUSBHMouse = (tUSBHMouse *)ulInstance;
//
// Reset the callback to null.
//
pUSBHMouse->pfnCallback = 0;
//
// Call the HID driver layer to close out this instance.
//
USBHHIDClose(pUSBHMouse->ulMouseInstance);
return(0);
}
//*****************************************************************************
//
//! This function is used to initialize a mouse interface after a mouse has
//! been detected.
//!
//! \param ulInstance is the instance value for this mouse.
//!
//! This function should be called after receiving a \b USB_EVENT_CONNECTED
//! event in the callback function provided by USBHMouseOpen(), however it
//! should only be called outside of the callback function. This will
//! initialize the mouse interface and determine how it reports events to the
//! USB host controller. The \e ulInstance value is the value that was
//! returned when the application called USBHMouseOpen(). This function only
//! needs to be called once per connection event but it should be called every
//! time a \b USB_EVENT_CONNECTED event occurs.
//!
//! \return Non-zero values should be assumed to indicate an error condition.
//
//*****************************************************************************
unsigned long
USBHMouseInit(unsigned long ulInstance)
{
tUSBHMouse *pUSBHMouse;
//
// Recover the pointer to the instance data.
//
pUSBHMouse = (tUSBHMouse *)ulInstance;
//
// Set the initial rate to only update on mouse state changes.
//
USBHHIDSetIdle(pUSBHMouse->ulMouseInstance, 0, 0);
//
// Read out the Report Descriptor from the mouse and parse it for
// the format of the reports coming back from the mouse.
//
USBHHIDGetReportDescriptor(pUSBHMouse->ulMouseInstance,
pUSBHMouse->pucHeap,
pUSBHMouse->ulHeapSize);
//
// Set the mouse to boot protocol.
//
USBHHIDSetProtocol(pUSBHMouse->ulMouseInstance, 1);
return(0);
}
//*****************************************************************************
//
// This function handles updating the state of the mouse buttons and axis.
//
// \param pUSBHMouse is the pointer to an instance of the mouse data.
//
// This function will check for updates to buttons or X/Y movements and send
// callbacks to the mouse callback function.
//
// \return None.
//
//*****************************************************************************
static void
UpdateMouseState(tUSBHMouse *pUSBHMouse)
{
unsigned long ulButton;
if(pUSBHMouse->pucBuffer[0] != pUSBHMouse->ucButtons)
{
for(ulButton = 1; ulButton <= 0x4; ulButton <<= 1)
{
if(((pUSBHMouse->pucBuffer[0] & ulButton) != 0) &&
((pUSBHMouse->ucButtons & ulButton) == 0))
{
//
// Send the mouse button press notification to the application.
//
pUSBHMouse->pfnCallback(0,
USBH_EVENT_HID_MS_PRESS,
ulButton,
0);
}
if(((pUSBHMouse->pucBuffer[0] & ulButton) == 0) &&
((pUSBHMouse->ucButtons & ulButton) != 0))
{
//
// Send the mouse button release notification to the
// application.
//
pUSBHMouse->pfnCallback(0,
USBH_EVENT_HID_MS_REL,
ulButton,
0);
}
}
//
// Save the new state.
//
pUSBHMouse->ucButtons = pUSBHMouse->pucBuffer[0];
}
if(pUSBHMouse->pucBuffer[1] != 0)
{
//
// Send the mouse button release notification to the
// application.
//
pUSBHMouse->pfnCallback(0,
USBH_EVENT_HID_MS_X,
(unsigned long)pUSBHMouse->pucBuffer[1],
0);
}
if(pUSBHMouse->pucBuffer[2] != 0)
{
//
// Send the mouse button release notification to the
// application.
//
pUSBHMouse->pfnCallback(0,
USBH_EVENT_HID_MS_Y,
(unsigned long)pUSBHMouse->pucBuffer[2],
0);
}
}
//*****************************************************************************
//
//! This function handles event callbacks from the USB HID driver layer.
//!
//! \param pvCBData is the pointer that was passed in to the USBHHIDOpen()
//! call.
//! \param ulEvent is the event that has been passed up from the HID driver.
//! \param ulMsgParam has meaning related to the \e ulEvent that occurred.
//! \param pvMsgData has meaning related to the \e ulEvent that occurred.
//!
//! This function will receive all event updates from the HID driver layer.
//! The mouse driver itself will mostly be concerned with report callbacks
//! from the HID driver layer and parsing them into keystrokes for the
//! application that has registered for callbacks with the USBHMouseOpen()
//! call.
//!
//! \return Non-zero values should be assumed to indicate an error condition.
//
//*****************************************************************************
unsigned long
USBHMouseCallback(void *pvCBData, unsigned long ulEvent,
unsigned long ulMsgParam, void *pvMsgData)
{
tUSBHMouse *pUSBHMouse;
//
// Recover the pointer to the instance data.
//
pUSBHMouse = (tUSBHMouse *)pvCBData;
switch(ulEvent)
{
//
// New mouse has been connected so notify the application.
//
case USB_EVENT_CONNECTED:
{
//
// Remember that a mouse is present.
//
pUSBHMouse->ulHIDFlags |= USBHMS_DEVICE_PRESENT;
//
// Notify the application that a new mouse was connected.
//
pUSBHMouse->pfnCallback(0, ulEvent, ulMsgParam, pvMsgData);
break;
}
case USB_EVENT_DISCONNECTED:
{
//
// No mouse is present.
//
pUSBHMouse->ulHIDFlags &= ~USBHMS_DEVICE_PRESENT;
//
// Notify the application that the mouse was disconnected.
//
pUSBHMouse->pfnCallback(0, ulEvent, ulMsgParam, pvMsgData);
break;
}
case USB_EVENT_RX_AVAILABLE:
{
//
// New mouse report structure was received.
//
USBHHIDGetReport(pUSBHMouse->ulMouseInstance, 0,
pUSBHMouse->pucBuffer,
USBHMS_REPORT_SIZE);
//
// Update the current state of the mouse and notify the application
// of any changes.
//
UpdateMouseState(pUSBHMouse);
break;
}
}
return(0);
}
//*****************************************************************************
//
//! @}
//
//*****************************************************************************
|