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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
|
//*****************************************************************************
//
// usb_dev_bulk.c - Main routines for the generic bulk device example.
//
// 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_ints.h"
#include "driverlib/interrupt.h"
#include "driverlib/sysctl.h"
#include "driverlib/systick.h"
#include "driverlib/rom.h"
#include "driverlib/rom_map.h"
#include "grlib/grlib.h"
#include "usblib/usblib.h"
#include "usblib/usb-ids.h"
#include "usblib/device/usbdevice.h"
#include "usblib/device/usbdbulk.h"
#include "utils/uartstdio.h"
#include "utils/ustdlib.h"
#include "drivers/frame.h"
#include "drivers/kentec320x240x16_ssd2119.h"
#include "drivers/pinout.h"
#include "usb_bulk_structs.h"
//*****************************************************************************
//
//! \addtogroup example_list
//! <h1>USB Generic Bulk Device (usb_dev_bulk)</h1>
//!
//! This example provides a generic USB device offering simple bulk data
//! transfer to and from the host. The device uses a vendor-specific class ID
//! and supports a single bulk IN endpoint and a single bulk OUT endpoint.
//! Data received from the host is assumed to be ASCII text and it is
//! echoed back with the case of all alphabetic characters swapped.
//!
//! A Windows INF file for the device is provided on the installation media and
//! in the C:/ti/TivaWare-C-Series-X.X/windows_drivers directory of TivaWare
//! releases. This INF contains information required to install the WinUSB
//! subsystem on WindowsXP and Vista PCs. WinUSB is a Windows subsystem
//! allowing user mode applications to access the USB device without the need
//! for a vendor-specific kernel mode driver.
//!
//! A sample Windows command-line application, usb_bulk_example, illustrating
//! how to connect to and communicate with the bulk device is also provided.
//! The application binary is installed as part of the ``Windows-side examples
//! for USB kits'' package (SW-USB-win) on the installation CD or via download
//! from http://www.ti.com/tivaware . Project files are included to allow
//! the examples to be built using Microsoft Visual Studio 2008. Source code
//! for this application can be found in directory
//! ti/TivaWare-for-C-Series/tools/usb_bulk_example.
//
//*****************************************************************************
//*****************************************************************************
//
// The system tick rate expressed both as ticks per second and a millisecond
// period.
//
//*****************************************************************************
#define SYSTICKS_PER_SECOND 100
#define SYSTICK_PERIOD_MS (1000 / SYSTICKS_PER_SECOND)
//*****************************************************************************
//
// The global system tick counter.
//
//*****************************************************************************
volatile uint32_t g_ui32SysTickCount = 0;
//*****************************************************************************
//
// Variables tracking transmit and receive counts.
//
//*****************************************************************************
volatile uint32_t g_ui32TxCount = 0;
volatile uint32_t g_ui32RxCount = 0;
#ifdef DEBUG
uint32_t g_ui32UARTRxErrors = 0;
#endif
//*****************************************************************************
//
// Debug-related definitions and declarations.
//
// Debug output is available via UART0 if DEBUG is defined during build.
//
//*****************************************************************************
#ifdef DEBUG
//*****************************************************************************
//
// Map all debug print calls to UARTprintf in debug builds.
//
//*****************************************************************************
#define DEBUG_PRINT UARTprintf
#else
//*****************************************************************************
//
// Compile out all debug print calls in release builds.
//
//*****************************************************************************
#define DEBUG_PRINT while(0) ((int32_t (*)(char *, ...))0)
#endif
//*****************************************************************************
//
// Graphics context used to show text on the color LCD display.
//
//*****************************************************************************
tContext g_sContext;
#define TEXT_FONT g_psFontCmss18b
//*****************************************************************************
//
// Flags used to pass commands from interrupt context to the main loop.
//
//*****************************************************************************
#define COMMAND_PACKET_RECEIVED 0x00000001
#define COMMAND_STATUS_UPDATE 0x00000002
volatile uint32_t g_ui32Flags = 0;
char *g_pcStatus;
//*****************************************************************************
//
// Global flag indicating that a USB configuration has been set.
//
//*****************************************************************************
static volatile bool g_bUSBConfigured = false;
//*****************************************************************************
//
// The error routine that is called if the driver library encounters an error.
//
//*****************************************************************************
#ifdef DEBUG
void
__error__(char *pcFilename, uint32_t ui32Line)
{
UARTprintf("Error at line %d of %s\n", ui32Line, pcFilename);
while(1)
{
}
}
#endif
//*****************************************************************************
//
// Interrupt handler for the system tick counter.
//
//*****************************************************************************
void
SysTickIntHandler(void)
{
//
// Update our system tick counter.
//
g_ui32SysTickCount++;
}
//*****************************************************************************
//
// Receive new data and echo it back to the host.
//
// \param psDevice points to the instance data for the device whose data is to
// be processed.
// \param pi8Data points to the newly received data in the USB receive buffer.
// \param ui32NumBytes is the number of bytes of data available to be processed.
//
// This function is called whenever we receive a notification that data is
// available from the host. We read the data, byte-by-byte and swap the case
// of any alphabetical characters found then write it back out to be
// transmitted back to the host.
//
// \return Returns the number of bytes of data processed.
//
//*****************************************************************************
static uint32_t
EchoNewDataToHost(tUSBDBulkDevice *psDevice, uint8_t *pi8Data,
uint_fast32_t ui32NumBytes)
{
uint_fast32_t ui32Loop, ui32Space, ui32Count;
uint_fast32_t ui32ReadIndex;
uint_fast32_t ui32WriteIndex;
tUSBRingBufObject sTxRing;
//
// Get the current buffer information to allow us to write directly to
// the transmit buffer (we already have enough information from the
// parameters to access the receive buffer directly).
//
USBBufferInfoGet(&g_sTxBuffer, &sTxRing);
//
// How much space is there in the transmit buffer?
//
ui32Space = USBBufferSpaceAvailable(&g_sTxBuffer);
//
// How many characters can we process this time round?
//
ui32Loop = (ui32Space < ui32NumBytes) ? ui32Space : ui32NumBytes;
ui32Count = ui32Loop;
//
// Update our receive counter.
//
g_ui32RxCount += ui32NumBytes;
//
// Dump a debug message.
//
DEBUG_PRINT("Received %d bytes\n", ui32NumBytes);
//
// Set up to process the characters by directly accessing the USB buffers.
//
ui32ReadIndex = (uint32_t)(pi8Data - g_pui8USBRxBuffer);
ui32WriteIndex = sTxRing.ui32WriteIndex;
while(ui32Loop)
{
//
// Copy from the receive buffer to the transmit buffer converting
// character case on the way.
//
//
// Is this a lower case character?
//
if((g_pui8USBRxBuffer[ui32ReadIndex] >= 'a') &&
(g_pui8USBRxBuffer[ui32ReadIndex] <= 'z'))
{
//
// Convert to upper case and write to the transmit buffer.
//
g_pui8USBTxBuffer[ui32WriteIndex] =
(g_pui8USBRxBuffer[ui32ReadIndex] - 'a') + 'A';
}
else
{
//
// Is this an upper case character?
//
if((g_pui8USBRxBuffer[ui32ReadIndex] >= 'A') &&
(g_pui8USBRxBuffer[ui32ReadIndex] <= 'Z'))
{
//
// Convert to lower case and write to the transmit buffer.
//
g_pui8USBTxBuffer[ui32WriteIndex] =
(g_pui8USBRxBuffer[ui32ReadIndex] - 'Z') + 'z';
}
else
{
//
// Copy the received character to the transmit buffer.
//
g_pui8USBTxBuffer[ui32WriteIndex] =
g_pui8USBRxBuffer[ui32ReadIndex];
}
}
//
// Move to the next character taking care to adjust the pointer for
// the buffer wrap if necessary.
//
ui32WriteIndex++;
ui32WriteIndex =
(ui32WriteIndex == BULK_BUFFER_SIZE) ? 0 : ui32WriteIndex;
ui32ReadIndex++;
ui32ReadIndex = (ui32ReadIndex == BULK_BUFFER_SIZE) ? 0 : ui32ReadIndex;
ui32Loop--;
}
//
// We've processed the data in place so now send the processed data
// back to the host.
//
USBBufferDataWritten(&g_sTxBuffer, ui32Count);
DEBUG_PRINT("Wrote %d bytes\n", ui32Count);
//
// We processed as much data as we can directly from the receive buffer so
// we need to return the number of bytes to allow the lower layer to
// update its read pointer appropriately.
//
return(ui32Count);
}
//*****************************************************************************
//
// Shows the status string on the display.
//
// \param psContext is a pointer to the graphics context representing the
// display.
// \param pi8Status is a pointer to the string to be shown.
//
//*****************************************************************************
void
DisplayStatus(tContext *psContext, char *pcStatus)
{
tRectangle sRectLine;
int32_t i32Y;
//
// Calculate the Y coordinate of the top left of the character cell
// for our line of text.
//
i32Y = (GrContextDpyHeightGet(psContext) / 4) -
(GrFontHeightGet(TEXT_FONT) / 2);
//
// Determine the bounding rectangle for this line of text. We add 4 pixels
// to the height just to ensure that we clear a couple of pixels above and
// below the line of text.
//
sRectLine.i16XMin = 0;
sRectLine.i16XMax = GrContextDpyWidthGet(psContext) - 1;
sRectLine.i16YMin = i32Y;
sRectLine.i16YMax = i32Y + GrFontHeightGet(TEXT_FONT) + 3;
//
// Clear the line with black.
//
GrContextForegroundSet(&g_sContext, ClrBlack);
GrRectFill(psContext, &sRectLine);
//
// Draw the new status string
//
DEBUG_PRINT("%s\n", pcStatus);
GrContextForegroundSet(&g_sContext, ClrWhite);
GrStringDrawCentered(psContext, pcStatus, -1,
GrContextDpyWidthGet(psContext) / 2,
GrContextDpyHeightGet(psContext) / 4 , false);
}
//*****************************************************************************
//
// Handles bulk driver notifications related to the transmit channel (data to
// the USB host).
//
// \param pvCBData is the client-supplied callback pointer for this channel.
// \param ulEvent identifies the event we are being notified about.
// \param ulMsgValue is an event-specific value.
// \param pvMsgData is an event-specific pointer.
//
// This function is called by the bulk driver to notify us of any events
// related to operation of the transmit data channel (the IN channel carrying
// data to the USB host).
//
// \return The return value is event-specific.
//
//*****************************************************************************
uint32_t
TxHandler(void *pvCBData, uint32_t ui32Event, uint32_t ui32MsgValue,
void *pvMsgData)
{
//
// We are not required to do anything in response to any transmit event
// in this example. All we do is update our transmit counter.
//
if(ui32Event == USB_EVENT_TX_COMPLETE)
{
g_ui32TxCount += ui32MsgValue;
}
//
// Dump a debug message.
//
DEBUG_PRINT("TX complete %d\n", ui32MsgValue);
return(0);
}
//*****************************************************************************
//
// Handles bulk driver notifications related to the receive channel (data from
// the USB host).
//
// \param pvCBData is the client-supplied callback pointer for this channel.
// \param ui32Event identifies the event we are being notified about.
// \param ui32MsgValue is an event-specific value.
// \param pvMsgData is an event-specific pointer.
//
// This function is called by the bulk driver to notify us of any events
// related to operation of the receive data channel (the OUT channel carrying
// data from the USB host).
//
// \return The return value is event-specific.
//
//*****************************************************************************
uint32_t
RxHandler(void *pvCBData, uint32_t ui32Event, uint32_t ui32MsgValue,
void *pvMsgData)
{
//
// Which event are we being sent?
//
switch(ui32Event)
{
//
// We are connected to a host and communication is now possible.
//
case USB_EVENT_CONNECTED:
{
g_bUSBConfigured = true;
g_pcStatus = "Host connected.";
g_ui32Flags |= COMMAND_STATUS_UPDATE;
//
// Flush our buffers.
//
USBBufferFlush(&g_sTxBuffer);
USBBufferFlush(&g_sRxBuffer);
break;
}
//
// The host has disconnected.
//
case USB_EVENT_DISCONNECTED:
{
g_bUSBConfigured = false;
g_pcStatus = "Host disconnected.";
g_ui32Flags |= COMMAND_STATUS_UPDATE;
break;
}
//
// A new packet has been received.
//
case USB_EVENT_RX_AVAILABLE:
{
tUSBDBulkDevice *psDevice;
//
// Get a pointer to our instance data from the callback data
// parameter.
//
psDevice = (tUSBDBulkDevice *)pvCBData;
//
// Read the new packet and echo it back to the host.
//
return(EchoNewDataToHost(psDevice, pvMsgData, ui32MsgValue));
}
//
// Ignore SUSPEND and RESUME for now.
//
case USB_EVENT_SUSPEND:
case USB_EVENT_RESUME:
break;
//
// Ignore all other events and return 0.
//
default:
break;
}
return(0);
}
//*****************************************************************************
//
// This is the main application entry function.
//
//*****************************************************************************
int
main(void)
{
uint_fast32_t ui32TxCount;
uint_fast32_t ui32RxCount;
char pcBuffer[16];
uint32_t ui32SysClock;
//
// Run from the PLL at 120 MHz.
//
ui32SysClock = MAP_SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ |
SYSCTL_OSC_MAIN | SYSCTL_USE_PLL |
SYSCTL_CFG_VCO_480), 120000000);
//
// Configure the device pins.
//
PinoutSet();
//
// Initialize the display driver.
//
Kentec320x240x16_SSD2119Init(ui32SysClock);
//
// Initialize the graphics context.
//
GrContextInit(&g_sContext, &g_sKentec320x240x16_SSD2119);
//
// Draw the application frame.
//
FrameDraw(&g_sContext, "usb-dev-bulk");
#ifdef DEBUG
//
// Enable UART0
//
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
//
// Initialize the UART for console I/O.
//
UARTStdioConfig(0, 115200, ui32SysClock);
#endif
//
// Not configured initially.
//
g_bUSBConfigured = false;
//
// Show the various static text elements on the color STN display.
//
GrContextFontSet(&g_sContext, TEXT_FONT);
GrStringDraw(&g_sContext, "Tx bytes:", -1, 110, 100, false);
GrStringDraw(&g_sContext, "Rx bytes:", -1, 110, 130, false);
//
// Enable the system tick.
//
ROM_SysTickPeriodSet(ui32SysClock / SYSTICKS_PER_SECOND);
ROM_SysTickIntEnable();
ROM_SysTickEnable();
//
// Show the application name on the display and UART output.
//
DEBUG_PRINT("\nTiva C Series USB bulk device example\n");
DEBUG_PRINT("---------------------------------\n\n");
//
// Tell the user what we are up to.
//
DisplayStatus(&g_sContext, " Configuring USB... ");
//
// Initialize the transmit and receive buffers.
//
USBBufferInit(&g_sTxBuffer);
USBBufferInit(&g_sRxBuffer);
//
// Initialize the USB stack for device mode.
//
USBStackModeSet(0, eUSBModeDevice, 0);
//
// Pass our device information to the USB library and place the device
// on the bus.
//
USBDBulkInit(0, &g_sBulkDevice);
//
// Wait for initial configuration to complete.
//
DisplayStatus(&g_sContext, "Waiting for host...");
//
// Clear our local byte counters.
//
ui32RxCount = 0;
ui32TxCount = 0;
//
// Main application loop.
//
while(1)
{
//
// Have we been asked to update the status display?
//
if(g_ui32Flags & COMMAND_STATUS_UPDATE)
{
//
// Clear the command flag
//
g_ui32Flags &= ~COMMAND_STATUS_UPDATE;
DisplayStatus(&g_sContext, g_pcStatus);
}
//
// Has there been any transmit traffic since we last checked?
//
if(ui32TxCount != g_ui32TxCount)
{
//
// Take a snapshot of the latest transmit count.
//
ui32TxCount = g_ui32TxCount;
//
// Update the display of bytes transmitted by the UART.
//
usnprintf(pcBuffer, 16, " %d ", ui32TxCount);
GrStringDraw(&g_sContext, pcBuffer, -1, 190, 100, true);
}
//
// Has there been any receive traffic since we last checked?
//
if(ui32RxCount != g_ui32RxCount)
{
//
// Take a snapshot of the latest receive count.
//
ui32RxCount = g_ui32RxCount;
//
// Update the display of bytes received by the UART.
//
usnprintf(pcBuffer, 16, " %d ", ui32RxCount);
GrStringDraw(&g_sContext, pcBuffer, -1, 190, 130, true);
}
}
}
|