summaryrefslogtreecommitdiff
path: root/boards/dk-tm4c129x/usb_otg_mouse/usb_otg_mouse.c
blob: 50bebd987e3cf2e5c7c3a776c0da51e5d303fb67 (plain)
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
//*****************************************************************************
//
// usb_otg_mouse.c - USB OTG Mouse (combined host and device 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 "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "inc/hw_usb.h"
#include "driverlib/rom.h"
#include "driverlib/rom_map.h"
#include "driverlib/sysctl.h"
#include "grlib/grlib.h"
#include "usblib/usblib.h"
#include "usblib/device/usbdevice.h"
#include "usblib/host/usbhost.h"
#include "utils/uartstdio.h"
#include "drivers/frame.h"
#include "drivers/kentec320x240x16_ssd2119.h"
#include "drivers/pinout.h"
#include "usb_otg_mouse.h"

//*****************************************************************************
//
//! \addtogroup example_list
//! <h1>USB OTG HID Mouse Example (usb_otg_mouse)</h1>
//!
//! This example application demonstrates the use of USB On-The-Go (OTG) to
//! offer both USB host and device operation.  When the DK board is connected
//! to a USB host, it acts as a BIOS-compatible USB mouse.  The select button
//! on the board (on the bottom right corner) acts as mouse button 1 and
//! the mouse pointer may be moved by dragging your finger or a stylus across
//! the touchscreen in the desired direction.
//!
//! If a USB mouse is connected to the USB OTG port, the board operates as a
//! USB host and draws dots on the display to track the mouse movement.  The
//! states of up to three mouse buttons are shown at the bottom right of the
//! display.
//!
//*****************************************************************************

//*****************************************************************************
//
// The current state of the USB in the system based on the detected mode.
//
//*****************************************************************************
volatile tUSBMode g_iCurrentMode = eUSBModeNone;

//*****************************************************************************
//
// The size of the host controller's memory pool in bytes.
//
//*****************************************************************************
#define HCD_MEMORY_SIZE         128

//*****************************************************************************
//
// The memory pool to provide to the Host controller driver.
//
//*****************************************************************************
uint8_t g_pui8HCDPool[HCD_MEMORY_SIZE];

//*****************************************************************************
//
// This global is used to indicate to the main that a mode change has
// occurred.
//
//*****************************************************************************
uint32_t g_ui32NewState;

//*****************************************************************************
//
// The system clock frequency in Hz.
//
//*****************************************************************************
uint32_t g_ui32SysClock;

//*****************************************************************************
//
// The graphics context for the screen.
//
//*****************************************************************************
tContext g_sContext;

//*****************************************************************************
//
// A function which returns the number of milliseconds since it was last
// called.  This can be found in usb_dev_mouse.c.
//
//*****************************************************************************
extern uint32_t GetTickms(void);

//*****************************************************************************
//
// The error routine that is called if the driver library encounters an error.
//
//*****************************************************************************
#ifdef DEBUG
void
__error__(char *pcFilename, uint32_t ui32Line)
{
}
#endif

//*****************************************************************************
//
// Callback function for mode changes.
//
//*****************************************************************************
void
ModeCallback(uint32_t ui32Index, tUSBMode iMode)
{
    //
    // Save the new mode.
    //
    g_iCurrentMode = iMode;

    switch(iMode)
    {
        case eUSBModeHost:
        {
            break;
        }
        case eUSBModeDevice:
        {
            break;
        }
        case eUSBModeNone:
        {
            break;
        }
        default:
        {
            break;
        }
    }
    g_ui32NewState = 1;
}

//*****************************************************************************
//
// Initialize the USB for OTG mode on the platform.
//
//*****************************************************************************
void
USBOTGInit(uint32_t ui32ClockRate, tUSBModeCallback pfnModeCallback)
{
    uint32_t ui32PLLRate;

    //
    // Enable USB controller.
    //
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_USB0);

    //
    // Set the current processor speed to provide more accurate timing
    // information to the USB library.
    //
    ui32PLLRate = 480000000;
    USBOTGFeatureSet(0, USBLIB_FEATURE_CPUCLK, &ui32ClockRate);
    USBOTGFeatureSet(0, USBLIB_FEATURE_USBPLL, &ui32PLLRate);

    //
    // Initialize the USB OTG mode and pass in a mode callback.
    //
    USBStackModeSet(0, eUSBModeOTG, pfnModeCallback);
}

//*****************************************************************************
//
// main routine.
//
//*****************************************************************************
int
main(void)
{
    tLPMFeature sLPMFeature;

    //
    // Run from the PLL at 120 MHz.
    //
    g_ui32SysClock = MAP_SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ |
                                             SYSCTL_OSC_MAIN | SYSCTL_USE_PLL |
                                             SYSCTL_CFG_VCO_480), 120000000);

    //
    // Configure the device pins.
    //
    PinoutSet();

    //
    // Configure the UART.
    //
    UARTStdioConfig(0, 115200, g_ui32SysClock);

    //
    // Initialize the display driver.
    //
    Kentec320x240x16_SSD2119Init(g_ui32SysClock);

    //
    // Initialize the graphics context.
    //
    GrContextInit(&g_sContext, &g_sKentec320x240x16_SSD2119);

    //
    // Draw the application frame.
    //
    FrameDraw(&g_sContext, "usb-otg-mouse");

    //
    // Configure USB for OTG operation.
    //
    USBOTGInit(g_ui32SysClock, ModeCallback);

    sLPMFeature.ui32HIRD = 500;
    sLPMFeature.ui32Features = USBLIB_FEATURE_LPM_EN |
                               USBLIB_FEATURE_LPM_RMT_WAKE;
    USBHCDFeatureSet(0, USBLIB_FEATURE_LPM, &sLPMFeature);

    //
    // Initialize the host stack.
    //
    HostInit();

    //
    // Initialize the device stack.
    //
    DeviceInit();

    //
    // Initialize the USB controller for dual mode operation with a 2ms polling
    // rate.
    //
    USBOTGModeInit(0, 2000, g_pui8HCDPool, HCD_MEMORY_SIZE);

    //
    // Set the new state so that the screen updates on the first
    // pass.
    //
    g_ui32NewState = 1;

    //
    // Loop forever.
    //
    while(1)
    {
        //
        // Tell the OTG library code how much time has passed in milliseconds
        // since the last call.
        //
        USBOTGMain(GetTickms());

        //
        // Handle deferred state change.
        //
        if(g_ui32NewState)
        {
            g_ui32NewState =0;

            //
            // Update the status area of the screen.
            //
            ClearMainWindow();

            //
            // Update the status bar with the new mode.
            //
            switch(g_iCurrentMode)
            {
                case eUSBModeHost:
                {
                    UpdateStatus("Host Mode", 0, true);
                    break;
                }
                case eUSBModeDevice:
                {
                    UpdateStatus("Device Mode", 0, true);
                    break;
                }
                case eUSBModeNone:
                {
                    UpdateStatus("Idle Mode\n", 0, true);
                    break;
                }
                default:
                {
                    break;
                }
            }
        }

        if(g_iCurrentMode == eUSBModeDevice)
        {
            DeviceMain();
        }
        else if(g_iCurrentMode == eUSBModeHost)
        {
            HostMain();
        }
    }
}