LPCOpen Platform
LPCOpen Platform for NXP LPC Microcontrollers
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
VirtualSerial.c
Go to the documentation of this file.
1 /*
2  * @brief Make your board becomes a USB Virtual Com
3  *
4  * @note
5  * Copyright(C) NXP Semiconductors, 2012
6  * Copyright(C) Dean Camera, 2011, 2012
7  * All rights reserved.
8  *
9  * @par
10  * Software that is described herein is for illustrative purposes only
11  * which provides customers with programming information regarding the
12  * LPC products. This software is supplied "AS IS" without any warranties of
13  * any kind, and NXP Semiconductors and its licensor disclaim any and
14  * all warranties, express or implied, including all implied warranties of
15  * merchantability, fitness for a particular purpose and non-infringement of
16  * intellectual property rights. NXP Semiconductors assumes no responsibility
17  * or liability for the use of the software, conveys no license or rights under any
18  * patent, copyright, mask work right, or any other intellectual property rights in
19  * or to any products. NXP Semiconductors reserves the right to make changes
20  * in the software without notification. NXP Semiconductors also makes no
21  * representation or warranty that such application will be suitable for the
22  * specified use without further testing or modification.
23  *
24  * @par
25  * Permission to use, copy, modify, and distribute this software and its
26  * documentation is hereby granted, under NXP Semiconductors' and its
27  * licensor's relevant copyrights in the software, without fee, provided that it
28  * is used in conjunction with NXP Semiconductors microcontrollers. This
29  * copyright, permission, and disclaimer notice must appear in all copies of
30  * this code.
31  */
32 
33 #include "VirtualSerial.h"
34 
40  .Config = {
42 
43  .DataINEndpointNumber = CDC_TX_EPNUM,
44  .DataINEndpointSize = CDC_TXRX_EPSIZE,
45  .DataINEndpointDoubleBank = false,
46 
47  .DataOUTEndpointNumber = CDC_RX_EPNUM,
48  .DataOUTEndpointSize = CDC_TXRX_EPSIZE,
49  .DataOUTEndpointDoubleBank = false,
50 
51  .NotificationEndpointNumber = CDC_NOTIFICATION_EPNUM,
52  .NotificationEndpointSize = CDC_NOTIFICATION_EPSIZE,
53  .NotificationEndpointDoubleBank = false,
54  },
55 };
56 
60 // static FILE USBSerialStream;
61 
64 #define CDC_TASK_SELECT ECHO_CHARACTER_TASK
65 
69 int main(void)
70 {
71  SetupHardware();
72 
73  for (;; ) {
74 #if defined(USB_DEVICE_ROM_DRIVER)
76 #endif
77 
78 #if (CDC_TASK_SELECT == ECHO_CHARACTER_TASK)
79  EchoCharater();
80 #else
82 #endif
83 #if !defined(USB_DEVICE_ROM_DRIVER)
84  // CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
85  USB_USBTask();
86 #endif
87  }
88 }
89 
91 void SetupHardware(void)
92 {
93  Board_Init();
94  USB_Init();
95 
96 #if defined(USB_DEVICE_ROM_DRIVER)
97  UsbdCdc_Init();
98 #endif
99 }
100 
101 #if (CDC_TASK_SELECT == ECHO_CHARACTER_TASK)
102 
103 void EchoCharater(void)
104 {
105  /* Echo back character */
106  uint8_t recv_byte[CDC_TXRX_EPSIZE];
107 #if !defined(USB_DEVICE_ROM_DRIVER)
108  if (CDC_Device_BytesReceived(&VirtualSerial_CDC_Interface)) {
109  recv_byte[0] = CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);
110  CDC_Device_SendData(&VirtualSerial_CDC_Interface, (char *) recv_byte, 1);
111  }
112 #else
113  uint32_t recv_count;
114  recv_count = UsbdCdc_RecvData(recv_byte, CDC_TXRX_EPSIZE);
115  if (recv_count) {
116  UsbdCdc_SendData(recv_byte, recv_count);
117  }
118 #endif
119 
120 }
121 
122 #else
123 
124 void CDC_Bridge_Task(void)
125 {
126  /* Echo back character */
127  uint8_t out_buff[CDC_TXRX_EPSIZE], in_buff[CDC_TXRX_EPSIZE];
128  uint32_t recv_count;
129 #if !defined(USB_DEVICE_ROM_DRIVER)
130  recv_count = CDC_Device_BytesReceived(&VirtualSerial_CDC_Interface);
131  while (recv_count--) {
132  out_buff[0] = CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);
133  Serial_Send((uint8_t *) out_buff, 1, BLOCKING);
134  }
135 
136  recv_count = Serial_Revc(in_buff, CDC_TXRX_EPSIZE, NONE_BLOCKING);
137  if (recv_count) {
138  CDC_Device_SendData(&VirtualSerial_CDC_Interface, (char *) in_buff, recv_count);
139  }
140 #else
141  recv_count = UsbdCdc_RecvData(out_buff, CDC_TXRX_EPSIZE);
142  if (recv_count) {
143  Serial_Send((uint8_t *) out_buff, recv_count, BLOCKING);
144  }
145 
146  recv_count = Serial_Revc(in_buff, CDC_TXRX_EPSIZE, NONE_BLOCKING);
147  if (recv_count) {
148  UsbdCdc_SendData(in_buff, recv_count);
149  }
150 #endif
151 }
152 
153 #endif
154 
157 {}
158 
161 {}
162 
165 {
166  bool ConfigSuccess = true;
167 
168  ConfigSuccess &= CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_Interface);
169 
170  // LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR);
171 }
172 
175 {
176  CDC_Device_ProcessControlRequest(&VirtualSerial_CDC_Interface);
177 }
178 
179 #if !defined(USB_DEVICE_ROM_DRIVER)
181 {
182  /*TODO: add LineEncoding processing here
183  * this is just a simple statement, only Baud rate is set */
184  // Serial_Init(CDCInterfaceInfo->State.LineEncoding.BaudRateBPS, false);
185 }
186 
187 #else
188 void EVENT_UsbdCdc_SetLineCode(CDC_LINE_CODING *line_coding)
189 {
190  // Serial_Init(VirtualSerial_CDC_Interface.State.LineEncoding.BaudRateBPS, false);
191 }
192 
193 #endif