usb_serial_structs.c
Go to the documentation of this file.
1 //*****************************************************************************
2 //
3 // usb_serial_structs.c - Data structures defining this CDC USB device.
4 //
5 // Copyright (c) 2012-2014 Texas Instruments Incorporated. All rights reserved.
6 // Software License Agreement
7 //
8 // Texas Instruments (TI) is supplying this software for use solely and
9 // exclusively on TI's microcontroller products. The software is owned by
10 // TI and/or its suppliers, and is protected under applicable copyright
11 // laws. You may not combine this software with "viral" open-source
12 // software in order to form a larger program.
13 //
14 // THIS SOFTWARE IS PROVIDED "AS IS" AND WITH ALL FAULTS.
15 // NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT
16 // NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17 // A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. TI SHALL NOT, UNDER ANY
18 // CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
19 // DAMAGES, FOR ANY REASON WHATSOEVER.
20 //
21 // This is part of revision 2.1.0.12573 of the EK-TM4C123GXL Firmware Package.
22 //
23 //*****************************************************************************
24 
25 #include <stdint.h>
26 #include <stdbool.h>
27 #include "inc/hw_types.h"
28 #include "driverlib/rom.h"
29 #include "driverlib/rom_map.h"
30 #include "driverlib/usb.h"
31 #include "usblib/usblib.h"
32 #include "usblib/usbcdc.h"
33 #include "usblib/usb-ids.h"
34 #include "usblib/device/usbdevice.h"
35 #include "usblib/device/usbdcdc.h"
36 #include "usb_serial_structs.h"
37 
38 
39 //*****************************************************************************
40 //
41 // Flag indicating whether or not we are currently sending a Break condition.
42 //
43 //*****************************************************************************
44 static volatile bool g_bUSBConfigured = false;
45 
46 //*****************************************************************************
47 //
48 // Flags used to pass commands from interrupt context to the main loop.
49 //
50 //*****************************************************************************
51 //#define COMMAND_PACKET_RECEIVED 0x00000001
52 //#define COMMAND_STATUS_UPDATE 0x00000002
53 //volatile uint32_t g_ui32Flags = 0;
54 //char *g_pcStatus;
55 
56 
57 //*****************************************************************************
58 //
59 // The languages supported by this device.
60 //
61 //*****************************************************************************
62 const uint8_t g_pui8LangDescriptor[] =
63 {
64  4,
65  USB_DTYPE_STRING,
66  USBShort(USB_LANG_EN_US)
67 };
68 
69 //*****************************************************************************
70 //
71 // The manufacturer string.
72 //
73 //*****************************************************************************
74 const uint8_t g_pui8ManufacturerString[] =
75 {
76  (17 + 1) * 2,
77  USB_DTYPE_STRING,
78  'T', 0, 'e', 0, 'x', 0, 'a', 0, 's', 0, ' ', 0, 'I', 0, 'n', 0, 's', 0,
79  't', 0, 'r', 0, 'u', 0, 'm', 0, 'e', 0, 'n', 0, 't', 0, 's', 0,
80 };
81 
82 //*****************************************************************************
83 //
84 // The product string.
85 //
86 //*****************************************************************************
87 const uint8_t g_pui8ProductString[] =
88 {
89  2 + (16 * 2),
90  USB_DTYPE_STRING,
91  'V', 0, 'i', 0, 'r', 0, 't', 0, 'u', 0, 'a', 0, 'l', 0, ' ', 0,
92  'C', 0, 'O', 0, 'M', 0, ' ', 0, 'P', 0, 'o', 0, 'r', 0, 't', 0
93 };
94 
95 //*****************************************************************************
96 //
97 // The serial number string.
98 //
99 //*****************************************************************************
100 const uint8_t g_pui8SerialNumberString[] =
101 {
102  2 + (8 * 2),
103  USB_DTYPE_STRING,
104  '1', 0, '2', 0, '3', 0, '4', 0, '5', 0, '6', 0, '7', 0, '8', 0
105 };
106 
107 //*****************************************************************************
108 //
109 // The control interface description string.
110 //
111 //*****************************************************************************
113 {
114  2 + (21 * 2),
115  USB_DTYPE_STRING,
116  'A', 0, 'C', 0, 'M', 0, ' ', 0, 'C', 0, 'o', 0, 'n', 0, 't', 0,
117  'r', 0, 'o', 0, 'l', 0, ' ', 0, 'I', 0, 'n', 0, 't', 0, 'e', 0,
118  'r', 0, 'f', 0, 'a', 0, 'c', 0, 'e', 0
119 };
120 
121 //*****************************************************************************
122 //
123 // The configuration description string.
124 //
125 //*****************************************************************************
126 const uint8_t g_pui8ConfigString[] =
127 {
128  2 + (26 * 2),
129  USB_DTYPE_STRING,
130  'S', 0, 'e', 0, 'l', 0, 'f', 0, ' ', 0, 'P', 0, 'o', 0, 'w', 0,
131  'e', 0, 'r', 0, 'e', 0, 'd', 0, ' ', 0, 'C', 0, 'o', 0, 'n', 0,
132  'f', 0, 'i', 0, 'g', 0, 'u', 0, 'r', 0, 'a', 0, 't', 0, 'i', 0,
133  'o', 0, 'n', 0
134 };
135 
136 //*****************************************************************************
137 //
138 // The descriptor string table.
139 //
140 //*****************************************************************************
141 const uint8_t * const g_ppui8StringDescriptors[] =
142 {
149 };
150 
151 #define NUM_STRING_DESCRIPTORS (sizeof(g_ppui8StringDescriptors) / \
152  sizeof(uint8_t *))
153 
154 
155 //*****************************************************************************
156 //
157 // The CDC device initialization and customization structures. In this case,
158 // we are using USBBuffers between the CDC device class driver and the
159 // application code. The function pointers and callback data values are set
160 // to insert a buffer in each of the data channels, transmit and receive.
161 //
162 // With the buffer in place, the CDC channel callback is set to the relevant
163 // channel function and the callback data is set to point to the channel
164 // instance data. The buffer, in turn, has its callback set to the application
165 // function and the callback data set to our CDC instance structure.
166 //
167 //*****************************************************************************
168 tUSBDCDCDevice g_sCDCDevice =
169 {
170  USB_VID_TI_1CBE,
171  USB_PID_SERIAL,
172  0,
173  USB_CONF_ATTR_SELF_PWR,
175  (void *)&g_sCDCDevice,
176  USBBufferEventCallback,
177  (void *)&g_sRxBuffer,
178  USBBufferEventCallback,
179  (void *)&g_sTxBuffer,
182 };
183 
184 //*****************************************************************************
185 //
186 // Receive buffer (from the USB perspective).
187 //
188 //*****************************************************************************
190 tUSBBuffer g_sRxBuffer =
191 {
192  false, // This is a receive buffer.
193  RxHandler, // pfnCallback
194  (void *)&g_sCDCDevice, // Callback data is our device pointer.
195  USBDCDCPacketRead, // pfnTransfer
196  USBDCDCRxPacketAvailable, // pfnAvailable
197  (void *)&g_sCDCDevice, // pvHandle
198  g_pui8USBRxBuffer, // pui8Buffer
199  UART_BUFFER_SIZE, // ui32BufferSize
200 };
201 
202 //*****************************************************************************
203 //
204 // Transmit buffer (from the USB perspective).
205 //
206 //*****************************************************************************
208 tUSBBuffer g_sTxBuffer =
209 {
210  true, // This is a transmit buffer.
211  TxHandler, // pfnCallback
212  (void *)&g_sCDCDevice, // Callback data is our device pointer.
213  USBDCDCPacketWrite, // pfnTransfer
214  USBDCDCTxPacketAvailable, // pfnAvailable
215  (void *)&g_sCDCDevice, // pvHandle
216  g_pui8USBTxBuffer, // pui8Buffer
217  UART_BUFFER_SIZE, // ui32BufferSize
218 };
219 
220 
221 //*****************************************************************************
222 //
223 // Handles CDC driver notifications related to the transmit channel (data to
224 // the USB host).
225 //
226 // \param ui32CBData is the client-supplied callback pointer for this channel.
227 // \param ui32Event identifies the event we are being notified about.
228 // \param ui32MsgValue is an event-specific value.
229 // \param pvMsgData is an event-specific pointer.
230 //
231 // This function is called by the CDC driver to notify us of any events
232 // related to operation of the transmit data channel (the IN channel carrying
233 // data to the USB host).
234 //
235 // \return The return value is event-specific.
236 //
237 //*****************************************************************************
238 uint32_t
239 TxHandler(void *pvCBData, uint32_t ui32Event, uint32_t ui32MsgValue,
240  void *pvMsgData)
241 {
242  //
243  // Which event have we been sent?
244  //
245  switch(ui32Event)
246  {
247  case USB_EVENT_TX_COMPLETE:
248  //
249  // Since we are using the USBBuffer, we don't need to do anything
250  // here.
251  //
252  break;
253 
254  //
255  // We don't expect to receive any other events. Ignore any that show
256  // up in a release build or hang in a debug build.
257  //
258  default:
259 #ifdef DEBUG
260  while(1);
261 #else
262  break;
263 #endif
264 
265  }
266  return(0);
267 }
268 
269 //*****************************************************************************
270 //
271 // Handles CDC driver notifications related to the receive channel (data from
272 // the USB host).
273 //
274 // \param ui32CBData is the client-supplied callback data value for this channel.
275 // \param ui32Event identifies the event we are being notified about.
276 // \param ui32MsgValue is an event-specific value.
277 // \param pvMsgData is an event-specific pointer.
278 //
279 // This function is called by the CDC driver to notify us of any events
280 // related to operation of the receive data channel (the OUT channel carrying
281 // data from the USB host).
282 //
283 // \return The return value is event-specific.
284 //
285 //*****************************************************************************
286 uint32_t
287 RxHandler(void *pvCBData, uint32_t ui32Event, uint32_t ui32MsgValue,
288  void *pvMsgData)
289 {
290  uint32_t ui32Count;
291 
292  //
293  // Which event are we being sent?
294  //
295  switch(ui32Event)
296  {
297  //
298  // A new packet has been received.
299  //
300  case USB_EVENT_RX_AVAILABLE:
301  {
302  //
303  // Notify a new packet has beed received
304  //
305  break;
306  }
307 
308  //
309  // We are being asked how much unprocessed data we have still to
310  // process. We return 0 if the UART is currently idle or 1 if it is
311  // in the process of transmitting something. The actual number of
312  // bytes in the UART FIFO is not important here, merely whether or
313  // not everything previously sent to us has been transmitted.
314  //
315  case USB_EVENT_DATA_REMAINING:
316  {
317  //
318  // Get the number of bytes in the buffer
319  //
320  ui32Count = USBBufferDataAvailable(&g_sRxBuffer);
321 
322  return(ui32Count);
323  }
324 
325  //
326  // We are being asked to provide a buffer into which the next packet
327  // can be read. We do not support this mode of receiving data so let
328  // the driver know by returning 0. The CDC driver should not be sending
329  // this message but this is included just for illustration and
330  // completeness.
331  //
332  case USB_EVENT_REQUEST_BUFFER:
333  {
334  return(0);
335  }
336 
337  //
338  // We don't expect to receive any other events. Ignore any that show
339  // up in a release build or hang in a debug build.
340  //
341  default:
342 #ifdef DEBUG
343  while(1);
344 #else
345  break;
346 #endif
347  }
348 
349  return(0);
350 }
351 
352 //*****************************************************************************
353 //
354 // Handles CDC driver notifications related to control and setup of the device.
355 //
356 // \param pvCBData is the client-supplied callback pointer for this channel.
357 // \param ui32Event identifies the event we are being notified about.
358 // \param ui32MsgValue is an event-specific value.
359 // \param pvMsgData is an event-specific pointer.
360 //
361 // This function is called by the CDC driver to perform control-related
362 // operations on behalf of the USB host. These functions include setting
363 // and querying the serial communication parameters, setting handshake line
364 // states and sending break conditions.
365 //
366 // \return The return value is event-specific.
367 //
368 //*****************************************************************************
369 uint32_t
370 ControlHandler(void *pvCBData, uint32_t ui32Event,
371  uint32_t ui32MsgValue, void *pvMsgData)
372 {
373  // uint32_t ui32IntsOff;
374 
375  //
376  // Which event are we being asked to process?
377  //
378  switch(ui32Event)
379  {
380  //
381  // We are connected to a host and communication is now possible.
382  //
383  case USB_EVENT_CONNECTED:
384  g_bUSBConfigured = true;
385 
386  //
387  // Flush our buffers.
388  //
389  USBBufferFlush(&g_sTxBuffer);
390  USBBufferFlush(&g_sRxBuffer);
391 
392  //
393  // Tell the main loop to update the display.
394  //
395  //ui32IntsOff = ROM_IntMasterDisable();
396  //g_pcStatus = "Connected";
397  //g_ui32Flags |= COMMAND_STATUS_UPDATE;
398  //if(!ui32IntsOff)
399  //{
400  //ROM_IntMasterEnable();
401  //}
402  break;
403 
404  //
405  // The host has disconnected.
406  //
407  case USB_EVENT_DISCONNECTED:
408  g_bUSBConfigured = false;
409  //ui32IntsOff = ROM_IntMasterDisable();
410  //g_pcStatus = "Disconnected";
411  //g_ui32Flags |= COMMAND_STATUS_UPDATE;
412  //if(!ui32IntsOff)
413  //{
414  //ROM_IntMasterEnable();
415  //}
416  break;
417 
418  //
419  // Return the current serial communication parameters.
420  //
421  case USBD_CDC_EVENT_GET_LINE_CODING:
422  break;
423 
424  //
425  // Set the current serial communication parameters.
426  //
427  case USBD_CDC_EVENT_SET_LINE_CODING:
428  break;
429 
430  //
431  // Set the current serial communication parameters.
432  //
433  case USBD_CDC_EVENT_SET_CONTROL_LINE_STATE:
434  break;
435 
436  //
437  // Send a break condition on the serial line.
438  //
439  case USBD_CDC_EVENT_SEND_BREAK:
440  break;
441 
442  //
443  // Clear the break condition on the serial line.
444  //
445  case USBD_CDC_EVENT_CLEAR_BREAK:
446  break;
447 
448  //
449  // Ignore SUSPEND and RESUME for now.
450  //
451  case USB_EVENT_SUSPEND:
452  case USB_EVENT_RESUME:
453  break;
454 
455  //
456  // We don't expect to receive any other events. Ignore any that show
457  // up in a release build or hang in a debug build.
458  //
459  default:
460 #ifdef DEBUG
461  while(1);
462 #else
463  break;
464 #endif
465 
466  }
467 
468  return(0);
469 }
uint32_t RxHandler(void *pvCBData, uint32_t ui32Event, uint32_t ui32MsgValue, void *pvMsgData)
const uint8_t *const g_ppui8StringDescriptors[]
const uint8_t g_pui8SerialNumberString[]
uint8_t g_pui8USBTxBuffer[UART_BUFFER_SIZE]
tUSBBuffer g_sTxBuffer
tUSBBuffer g_sRxBuffer
const uint8_t g_pui8ControlInterfaceString[]
#define UART_BUFFER_SIZE
uint32_t ControlHandler(void *pvCBData, uint32_t ui32Event, uint32_t ui32MsgValue, void *pvMsgData)
const uint8_t g_pui8ManufacturerString[]
const uint8_t g_pui8ConfigString[]
tUSBDCDCDevice g_sCDCDevice
const uint8_t g_pui8LangDescriptor[]
#define NUM_STRING_DESCRIPTORS
const uint8_t g_pui8ProductString[]
static volatile bool g_bUSBConfigured
uint32_t TxHandler(void *pvCBData, uint32_t ui32Event, uint32_t ui32MsgValue, void *pvMsgData)
uint8_t g_pui8USBRxBuffer[UART_BUFFER_SIZE]


rosserial_tivac
Author(s): Vitor Matos, Vitor Matos
autogenerated on Mon Jun 10 2019 14:53:43