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 Virtual Serial Device and KeyBoard Host
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 #include "KeyboardHost.h"
35 
36 /*****************************************************************************
37  * Private types/enumerations/variables
38  ****************************************************************************/
39 
40 #define ECHO_CHARACTER_TASK (0)
41 #define CDC_BRIDGE_TASK (ECHO_CHARACTER_TASK + 1)
42 
48  .Config = {
50 
51  .DataINEndpointNumber = CDC_TX_EPNUM,
52  .DataINEndpointSize = CDC_TXRX_EPSIZE,
53  .DataINEndpointDoubleBank = false,
54 
55  .DataOUTEndpointNumber = CDC_RX_EPNUM,
56  .DataOUTEndpointSize = CDC_TXRX_EPSIZE,
57  .DataOUTEndpointDoubleBank = false,
58 
59  .NotificationEndpointNumber = CDC_NOTIFICATION_EPNUM,
60  .NotificationEndpointSize = CDC_NOTIFICATION_EPSIZE,
61  .NotificationEndpointDoubleBank = false,
62  .PortNumber = 1,
63  },
64 };
65 
68 #define CDC_TASK_SELECT ECHO_CHARACTER_TASK
69 
70 /*****************************************************************************
71  * Public types/enumerations/variables
72  ****************************************************************************/
73 
74 /*****************************************************************************
75  * Private functions
76  ****************************************************************************/
77 
79 static void SetupHardware(void)
80 {
81  Board_Init();
83  USB_Init(VirtualSerial_CDC_Interface.Config.PortNumber, USB_MODE_Device);
85 }
86 
87 #if (CDC_TASK_SELECT == ECHO_CHARACTER_TASK)
88 
89 static char EchoCharacter(void)
90 {
91  /* Echo back character */
92  uint8_t recv_byte[CDC_TXRX_EPSIZE];
93 
94  if (CDC_Device_BytesReceived(&VirtualSerial_CDC_Interface)) {
95  recv_byte[0] = CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);
96  CDC_Device_SendData(&VirtualSerial_CDC_Interface, (char *) recv_byte, 1);
97  }
98 
99  return recv_byte[0];
100 }
101 
102 #else
103 
104 static void CDC_Bridge_Task(void)
105 {
106  /* Echo back character */
107  uint8_t out_buff[CDC_TXRX_EPSIZE], in_buff[CDC_TXRX_EPSIZE];
108  uint32_t recv_count;
109 
110  recv_count = CDC_Device_BytesReceived(&VirtualSerial_CDC_Interface);
111  while (recv_count--) {
112  out_buff[0] = CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);
113  Serial_Send((uint8_t *) out_buff, 1, BLOCKING);
114  }
115 
116  recv_count = Serial_Revc(in_buff, CDC_TXRX_EPSIZE, NONE_BLOCKING);
117  if (recv_count) {
118  CDC_Device_SendData(&VirtualSerial_CDC_Interface, (char *) in_buff, recv_count);
119  }
120 }
121 
122 #endif
123 
124 /*****************************************************************************
125  * Public functions
126  ****************************************************************************/
127 
131 int main(void)
132 {
133  SetupHardware();
134  DEBUGOUT("Keyboard Host Demo running.\r\n");
135  for (;; ) {
136  EchoCharacter();
137  USB_USBTask(VirtualSerial_CDC_Interface.Config.PortNumber, USB_MODE_Device);
138  KeyboardHost();
139  }
140 }
141 
142 /* Virtual serial putchar */
144 {
145  char tmp = ch;
146  CDC_Device_SendData(&VirtualSerial_CDC_Interface, &tmp, 1);
147 }
148 
151 {}
152 
155 {}
156 
159 {
160  bool ConfigSuccess = true;
161 
162  ConfigSuccess &= CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_Interface);
163 
164 }
165 
168 {
169  CDC_Device_ProcessControlRequest(&VirtualSerial_CDC_Interface);
170 }
171 
173 {
174  /* TODO: add LineEncoding processing here
175  * this is just a simple statement, only Baud rate is set */
176  /* Serial_Init(CDCInterfaceInfo->State.LineEncoding.BaudRateBPS, false); */
177 }
178 
179 #if defined(USB_DEVICE_ROM_DRIVER)
180  #error This demo is not supposed to run under ROM STACK mode
181 #endif