LPCOpen Platform  v1.03
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 
35 /*****************************************************************************
36  * Private types/enumerations/variables
37  ****************************************************************************/
38 
39 /*****************************************************************************
40  * Public types/enumerations/variables
41  ****************************************************************************/
42 
43 #define ECHO_CHARACTER_TASK (0)
44 #define CDC_BRIDGE_TASK (ECHO_CHARACTER_TASK + 1)
45 
51  .Config = {
53 
54  .DataINEndpointNumber = CDC_TX_EPNUM,
55  .DataINEndpointSize = CDC_TXRX_EPSIZE,
56  .DataINEndpointDoubleBank = false,
57 
58  .DataOUTEndpointNumber = CDC_RX_EPNUM,
59  .DataOUTEndpointSize = CDC_TXRX_EPSIZE,
60  .DataOUTEndpointDoubleBank = false,
61 
62  .NotificationEndpointNumber = CDC_NOTIFICATION_EPNUM,
63  .NotificationEndpointSize = CDC_NOTIFICATION_EPSIZE,
64  .NotificationEndpointDoubleBank = false,
65  .PortNumber = 0,
66  },
67 };
68 
72 // static FILE USBSerialStream;
73 
76 #define CDC_TASK_SELECT ECHO_CHARACTER_TASK
77 
78 /*****************************************************************************
79  * Private functions
80  ****************************************************************************/
81 
83 static void SetupHardware(void)
84 {
85  Board_Init();
86  USB_Init(VirtualSerial_CDC_Interface.Config.PortNumber, USB_MODE_Device);
87 
88 #if defined(USB_DEVICE_ROM_DRIVER)
89  UsbdCdc_Init();
90 #endif
91 }
92 
93 #if (CDC_TASK_SELECT == ECHO_CHARACTER_TASK)
94 
95 static void EchoCharacter(void)
96 {
97  /* Echo back character */
98  uint8_t recv_byte[CDC_TXRX_EPSIZE];
99 #if !defined(USB_DEVICE_ROM_DRIVER)
100  if (CDC_Device_BytesReceived(&VirtualSerial_CDC_Interface)) {
101  recv_byte[0] = CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);
102  CDC_Device_SendData(&VirtualSerial_CDC_Interface, (char *) recv_byte, 1);
103  }
104 #else
105  uint32_t recv_count;
106  recv_count = UsbdCdc_RecvData(recv_byte, CDC_TXRX_EPSIZE);
107  if (recv_count) {
108  UsbdCdc_SendData(recv_byte, recv_count);
109  }
110 #endif
111 
112 }
113 
114 #else
115 
116 static void CDC_Bridge_Task(void)
117 {
118  /* Echo back character */
119  uint8_t out_buff[CDC_TXRX_EPSIZE], in_buff[CDC_TXRX_EPSIZE];
120  uint32_t recv_count;
121 #if !defined(USB_DEVICE_ROM_DRIVER)
122  recv_count = CDC_Device_BytesReceived(&VirtualSerial_CDC_Interface);
123  while (recv_count--) {
124  out_buff[0] = CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);
125  Serial_Send((uint8_t *) out_buff, 1, BLOCKING);
126  }
127 
128  recv_count = Serial_Revc(in_buff, CDC_TXRX_EPSIZE, NONE_BLOCKING);
129  if (recv_count) {
130  CDC_Device_SendData(&VirtualSerial_CDC_Interface, (char *) in_buff, recv_count);
131  }
132 #else
133  recv_count = UsbdCdc_RecvData(out_buff, CDC_TXRX_EPSIZE);
134  if (recv_count) {
135  Serial_Send((uint8_t *) out_buff, recv_count, BLOCKING);
136  }
137 
138  recv_count = Serial_Revc(in_buff, CDC_TXRX_EPSIZE, NONE_BLOCKING);
139  if (recv_count) {
140  UsbdCdc_SendData(in_buff, recv_count);
141  }
142 #endif
143 }
144 
145 #endif
146 
147 /*****************************************************************************
148  * Public functions
149  ****************************************************************************/
150 
154 int main(void)
155 {
156  SetupHardware();
157 
158  for (;; ) {
159 #if defined(USB_DEVICE_ROM_DRIVER)
161 #endif
162 
163 #if (CDC_TASK_SELECT == ECHO_CHARACTER_TASK)
164  EchoCharacter();
165 #else
166  CDC_Bridge_Task();
167 #endif
168 #if !defined(USB_DEVICE_ROM_DRIVER)
169  // CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
170  USB_USBTask(VirtualSerial_CDC_Interface.Config.PortNumber, USB_MODE_Device);
171 #endif
172  }
173 }
174 
177 {}
178 
181 {}
182 
185 {
186  bool ConfigSuccess = true;
187 
188  ConfigSuccess &= CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_Interface);
189 
190  // LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR);
191 }
192 
195 {
196  CDC_Device_ProcessControlRequest(&VirtualSerial_CDC_Interface);
197 }
198 
199 #if !defined(USB_DEVICE_ROM_DRIVER)
201 {
202  /*TODO: add LineEncoding processing here
203  * this is just a simple statement, only Baud rate is set */
204  // Serial_Init(CDCInterfaceInfo->State.LineEncoding.BaudRateBPS, false);
205 }
206 
207 #else
208 void EVENT_UsbdCdc_SetLineCode(CDC_LINE_CODING *line_coding)
209 {
210  // Serial_Init(VirtualSerial_CDC_Interface.State.LineEncoding.BaudRateBPS, false);
211 }
212 
213 #endif