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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
|
//*****************************************************************************
//
// usb_host_mouse.c - An example using that supports a mouse.
//
// Copyright (c) 2013-2014 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 2.1.0.12573 of the DK-TM4C129X Firmware Package.
//
//*****************************************************************************
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include "inc/hw_memmap.h"
#include "driverlib/gpio.h"
#include "driverlib/interrupt.h"
#include "driverlib/sysctl.h"
#include "driverlib/rom.h"
#include "driverlib/rom_map.h"
#include "usblib/usblib.h"
#include "usblib/usbhid.h"
#include "usblib/host/usbhost.h"
#include "usblib/host/usbhhid.h"
#include "usblib/host/usbhhidmouse.h"
#include "drivers/pinout.h"
#include "mouse_ui.h"
//*****************************************************************************
//
//! \addtogroup example_list
//! <h1>USB Host mouse example(usb_host_mouse)</h1>
//!
//! This example application demonstrates how to support a USB mouse using
//! the DK-TM4C129X development board. This application supports only a
//! standard mouse HID device, but can report on the types of other devices
//! that are connected without having the ability to access them.
//! The bottom left status bar reports the
//! type of device attached. The user interface for the application is
//! handled in the mouse_ui.c file while the usb_host_mouse.c file
//! handles start up and the USB interface.
//!
//! The application can be recompiled to run using and external USB phy to
//! implement a high speed host using an external USB phy. To use the external
//! phy the application must be built with \b USE_ULPI defined. This disables
//! the internal phy and the connector on the DK-TM4C129X board and enables the
//! connections to the external ULPI phy pins on the DK-TM4C129X board.
//
//*****************************************************************************
//*****************************************************************************
//
// The size of the host controller's memory pool in bytes.
//
//*****************************************************************************
#define HCD_MEMORY_SIZE 128
//*****************************************************************************
//
// The global application status structure.
//
//*****************************************************************************
tMouseStatus g_sStatus;
//*****************************************************************************
//
// The memory pool to provide to the Host controller driver.
//
//*****************************************************************************
uint8_t g_pui8HCDPool[HCD_MEMORY_SIZE * MAX_USB_DEVICES];
//*****************************************************************************
//
// Declare the USB Events driver interface.
//
//*****************************************************************************
DECLARE_EVENT_DRIVER(g_sUSBEventDriver, 0, 0, USBHCDEvents);
//*****************************************************************************
//
// The global that holds all of the host drivers in use in the application.
// In this case, only the HID class is loaded.
//
//*****************************************************************************
static tUSBHostClassDriver const * const g_ppHostClassDrivers[] =
{
&g_sUSBHIDClassDriver,
&g_sUSBEventDriver
};
//*****************************************************************************
//
// This global holds the number of class drivers in the g_ppHostClassDrivers
// list.
//
//*****************************************************************************
static const uint32_t g_ui32NumHostClassDrivers =
sizeof(g_ppHostClassDrivers) / sizeof(tUSBHostClassDriver *);
//*****************************************************************************
//
// The global value used to store the mouse instance value.
//
//*****************************************************************************
static tUSBHMouse *g_psMouse;
//*****************************************************************************
//
// The error routine that is called if the driver library encounters an error.
//
//*****************************************************************************
#ifdef DEBUG
void
__error__(char *pcFilename, uint32_t ui32Line)
{
}
#endif
//*****************************************************************************
//
// This enumerated type is used to hold the states of the mouse.
//
//*****************************************************************************
enum
{
//
// No device is present.
//
eStateNoDevice,
//
// Mouse has been detected and needs to be initialized in the main loop.
//
eStateMouseInit,
//
// Mouse is connected and waiting for events.
//
eStateMouseConnected,
}
g_iMouseState;
//*****************************************************************************
//
// This is the callback from the USB HID mouse handler.
//
// pvCBData is ignored by this function.
// ui32Event is one of the valid events for a mouse device.
// ui32MsgParam is defined by the event that occurs.
// pvMsgData is a pointer to data that is defined by the event that
// occurs.
//
// This function will be called to inform the application when a mouse has
// been plugged in or removed and any time a mouse movement or button pressed
// has occurred.
//
// This function will return 0.
//
//*****************************************************************************
void
MouseCallback(tUSBHMouse *psMouse, uint32_t ui32Event, uint32_t ui32MsgParam,
void *pvMsgData)
{
int32_t i32Pos;
switch(ui32Event)
{
//
// New mouse detected.
//
case USB_EVENT_CONNECTED:
{
//
// Proceed to the eStateMouseInit state so that the main loop
// can finish initializing the mouse since USBHMouseInit() cannot
// be called from within a callback.
//
g_iMouseState = eStateMouseInit;
break;
}
//
// Mouse has been unplugged.
//
case USB_EVENT_DISCONNECTED:
{
//
// Change the state so that the main loop knows that a device is no
// longer present.
//
g_iMouseState = eStateNoDevice;
//
// Need to clear out any held buttons.
//
g_sStatus.ui32Buttons = 0;
g_sStatus.bUpdate = true;
break;
}
//
// New button press detected.
//
case USBH_EVENT_HID_MS_PRESS:
{
//
// Save the new button that was pressed.
//
g_sStatus.ui32Buttons |= ui32MsgParam;
g_sStatus.bUpdate = true;
break;
}
//
// A button was released on a HID mouse.
//
case USBH_EVENT_HID_MS_REL:
{
//
// Remove the button from the pressed state.
//
g_sStatus.ui32Buttons &= ~ui32MsgParam;
g_sStatus.bUpdate = true;
break;
}
//
// The HID mouse detected movement in the X direction.
//
case USBH_EVENT_HID_MS_X:
{
//
// Update the cursor X position.
// This has to be calculated as an intermediate signed value
// because the position can go negative with fast movements.
//
i32Pos = g_sStatus.ui32XPos + (int8_t)ui32MsgParam;
if(i32Pos < 0)
{
g_sStatus.ui32XPos = MOUSE_MIN_X;
}
else
{
g_sStatus.ui32XPos = (uint32_t)i32Pos;
}
if(g_sStatus.ui32XPos < MOUSE_MIN_X)
{
g_sStatus.ui32XPos = MOUSE_MIN_X;
}
if(g_sStatus.ui32XPos > MOUSE_MAX_X)
{
g_sStatus.ui32XPos = MOUSE_MAX_X;
}
g_sStatus.bUpdate = true;
break;
}
//
// The HID mouse detected movement in the Y direction.
//
case USBH_EVENT_HID_MS_Y:
{
//
// Update the cursor Y position.
// This has to be calculated as an intermediate signed value
// because the position can go negative with fast movements.
//
i32Pos = (int32_t)g_sStatus.ui32YPos + (int8_t)ui32MsgParam;
if(i32Pos < 0)
{
g_sStatus.ui32YPos = MOUSE_MIN_Y;
}
else
{
g_sStatus.ui32YPos = (uint32_t)i32Pos;
}
if(g_sStatus.ui32YPos < MOUSE_MIN_Y)
{
g_sStatus.ui32YPos = MOUSE_MIN_Y;
}
if(g_sStatus.ui32YPos > MOUSE_MAX_Y)
{
g_sStatus.ui32YPos = MOUSE_MAX_Y;
}
g_sStatus.bUpdate = true;
break;
}
default:
{
break;
}
}
}
//*****************************************************************************
//
// The main routine for handling the USB mouse.
//
//*****************************************************************************
void
MouseMain(void)
{
switch(g_iMouseState)
{
//
// This state is entered when they mouse is first detected.
//
case eStateMouseInit:
{
//
// Initialized the newly connected mouse.
//
USBHMouseInit(g_psMouse);
//
// Proceed to the mouse connected state.
//
g_iMouseState = eStateMouseConnected;
break;
}
case eStateMouseConnected:
default:
{
if(g_sStatus.bUpdate == true)
{
g_sStatus.bUpdate = false;
UIUpdateStatus();
}
break;
}
}
}
//*****************************************************************************
//
// This is the generic callback from host stack.
//
// pvData is actually a pointer to a tEventInfo structure.
//
// This function will be called to inform the application when a USB event has
// occurred that is outside those related to the mouse device. At this
// point this is used to detect unsupported devices being inserted and removed.
// It is also used to inform the application when a power fault has occurred.
// This function is required when the g_USBGenericEventDriver is included in
// the host controller driver array that is passed in to the
// USBHCDRegisterDrivers() function.
//
//*****************************************************************************
void
USBHCDEvents(void *pvData)
{
tEventInfo *pEventInfo;
//
// Cast this pointer to its actual type.
//
pEventInfo = (tEventInfo *)pvData;
switch(pEventInfo->ui32Event)
{
case USB_EVENT_UNKNOWN_CONNECTED:
case USB_EVENT_CONNECTED:
{
//
// Save the device instance data.
//
g_sStatus.ui32Instance = pEventInfo->ui32Instance;
g_sStatus.bConnected = true;
//
// Update the port status for the new device.
//
UIUpdateStatus();
break;
}
//
// A device has been unplugged.
//
case USB_EVENT_DISCONNECTED:
{
//
// Device is no longer connected.
//
g_sStatus.bConnected = false;
//
// Update the port status for the new device.
//
UIUpdateStatus();
break;
}
default:
{
break;
}
}
}
//*****************************************************************************
//
// The main application loop.
//
//*****************************************************************************
int
main(void)
{
uint32_t ui32SysClock, ui32PLLRate;
#ifdef USE_ULPI
uint32_t ui32Setting;
#endif
//
// Set the application to run at 120 MHz with a PLL frequency of 480 MHz.
//
ui32SysClock = MAP_SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ |
SYSCTL_OSC_MAIN | SYSCTL_USE_PLL |
SYSCTL_CFG_VCO_480), 120000000);
//
// Set the part pin out appropriately for this device.
//
PinoutSet();
#ifdef USE_ULPI
//
// Switch the USB ULPI Pins over.
//
USBULPIPinoutSet();
//
// Enable USB ULPI with high speed support.
//
ui32Setting = USBLIB_FEATURE_ULPI_HS;
USBOTGFeatureSet(0, USBLIB_FEATURE_USBULPI, &ui32Setting);
//
// Setting the PLL frequency to zero tells the USB library to use the
// external USB clock.
//
ui32PLLRate = 0;
#else
//
// Save the PLL rate used by this application.
//
ui32PLLRate = 480000000;
#endif
//
// Initialize the connection status.
//
g_sStatus.bConnected = false;
g_sStatus.ui32Buttons = 0;
//
// Enable Clocking to the USB controller.
//
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_USB0);
//
// Enable Interrupts
//
ROM_IntMasterEnable();
//
// Initialize the USB stack mode and pass in a mode callback.
//
USBStackModeSet(0, eUSBModeHost, 0);
//
// Register the host class drivers.
//
USBHCDRegisterDrivers(0, g_ppHostClassDrivers, g_ui32NumHostClassDrivers);
//
// Open an instance of the mouse driver. The mouse does not need
// to be present at this time, this just save a place for it and allows
// the applications to be notified when a mouse is present.
//
g_psMouse = USBHMouseOpen(MouseCallback, 0, 0);
//
// Initialize the power configuration. This sets the power enable signal
// to be active high and does not enable the power fault.
//
USBHCDPowerConfigInit(0, USBHCD_VBUS_AUTO_HIGH | USBHCD_VBUS_FILTER);
//
// Tell the USB library the CPU clock and the PLL frequency. This is a
// new requirement for TM4C129 devices.
//
USBHCDFeatureSet(0, USBLIB_FEATURE_CPUCLK, &ui32SysClock);
USBHCDFeatureSet(0, USBLIB_FEATURE_USBPLL, &ui32PLLRate);
//
// Initialize the USB controller for Host mode.
//
USBHCDInit(0, g_pui8HCDPool, sizeof(g_pui8HCDPool));
//
// Initialize the UI elements.
//
UIInit(ui32SysClock);
//
// The main loop for the application.
//
while(1)
{
//
// Call the USB library to let non-interrupt code run.
//
USBHCDMain();
//
// Call the mouse and mass storage main routines.
//
MouseMain();
}
}
|