LPCOpen Platform  v1.03
LPCOpen Platform for NXP LPC Microcontrollers
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
KeyboardHost.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 
45  .Config = {
46  .DataINPipeNumber = 1,
47  .DataINPipeDoubleBank = false,
48 
49  .DataOUTPipeNumber = 2,
50  .DataOUTPipeDoubleBank = false,
51 
52  .HIDInterfaceProtocol = HID_CSCP_KeyboardBootProtocol,
53  .PortNumber = 0,
54  },
55 };
56 
57 /*****************************************************************************
58  * Public types/enumerations/variables
59  ****************************************************************************/
60 
61 /*****************************************************************************
62  * Private functions
63  ****************************************************************************/
64 
68 static void KeyboardHost_Task(void)
69 {
70  if (USB_HostState[Keyboard_HID_Interface.Config.PortNumber] != HOST_STATE_Configured) {
71  return;
72  }
73 
74  if (HID_Host_IsReportReceived(&Keyboard_HID_Interface)) {
76  HID_Host_ReceiveReport(&Keyboard_HID_Interface, &KeyboardReport);
77 
78  uint8_t KeyCode = KeyboardReport.KeyCode[0];
79 
80  if (KeyCode) {
81  char PressedKey = 0;
82 
83  /* Retrieve pressed key character if alphanumeric */
84  if ((KeyCode >= HID_KEYBOARD_SC_A) && (KeyCode <= HID_KEYBOARD_SC_Z)) {
85  PressedKey = (KeyCode - HID_KEYBOARD_SC_A) + 'A';
86  }
87  else if ((KeyCode >= HID_KEYBOARD_SC_1_AND_EXCLAMATION) &
89  PressedKey = (KeyCode - HID_KEYBOARD_SC_1_AND_EXCLAMATION) + '1';
90  }
91  else if (KeyCode == HID_KEYBOARD_SC_0_AND_CLOSING_PARENTHESIS) {
92  PressedKey = '0';
93  }
94  else if (KeyCode == HID_KEYBOARD_SC_SPACE) {
95  PressedKey = ' ';
96  }
97  else if (KeyCode == HID_KEYBOARD_SC_ENTER) {
98  PressedKey = '\n';
99  }
100 
101  if (PressedKey) {
102  VirtualSerial_putchar(PressedKey);
103  }
104  }
105  }
106 }
107 
108 /*****************************************************************************
109  * Public functions
110  ****************************************************************************/
111 
115 void KeyboardHost(void)
116 {
117  static int initialized = 0;
118 
119  /* Initialize the keyboard host driver once */
120  if (!initialized) {
121  USB_Init(Keyboard_HID_Interface.Config.PortNumber, USB_MODE_Host);
122  initialized = 1;
123  }
124 
126 
127  HID_Host_USBTask(&Keyboard_HID_Interface);
128  USB_USBTask(Keyboard_HID_Interface.Config.PortNumber, USB_MODE_Host);
129 }
130 
134 void EVENT_USB_Host_DeviceAttached(const uint8_t corenum)
135 {
136  DEBUGOUT(("Device Attached on port %d\r\n"), corenum);
137 }
138 
142 void EVENT_USB_Host_DeviceUnattached(const uint8_t corenum)
143 {
144  DEBUGOUT(("\r\nDevice Unattached on port %d\r\n"), corenum);
145 }
146 
150 void EVENT_USB_Host_DeviceEnumerationComplete(const uint8_t corenum)
151 {
152  uint16_t ConfigDescriptorSize;
153  uint8_t ConfigDescriptorData[512];
154 
155  if (USB_Host_GetDeviceConfigDescriptor(corenum, 1, &ConfigDescriptorSize, ConfigDescriptorData,
156  sizeof(ConfigDescriptorData)) != HOST_GETCONFIG_Successful) {
157  DEBUGOUT("Error Retrieving Configuration Descriptor.\r\n");
158  return;
159  }
160  Keyboard_HID_Interface.Config.PortNumber = corenum;
161 
162  if (HID_Host_ConfigurePipes(&Keyboard_HID_Interface,
163  ConfigDescriptorSize, ConfigDescriptorData) != HID_ENUMERROR_NoError) {
164  DEBUGOUT("Attached Device Not a Valid Keyboard.\r\n");
165  return;
166  }
167 
169  DEBUGOUT("Error Setting Device Configuration.\r\n");
170  return;
171  }
172 
173  if (HID_Host_SetBootProtocol(&Keyboard_HID_Interface) != 0) {
174  DEBUGOUT("Could not Set Boot Protocol Mode.\r\n");
175  USB_Host_SetDeviceConfiguration(Keyboard_HID_Interface.Config.PortNumber, 0);
176  return;
177  }
178 
179  DEBUGOUT("Keyboard Enumerated.\r\n");
180 }
181 
183 void EVENT_USB_Host_HostError(const uint8_t corenum, const uint8_t ErrorCode)
184 {
185  USB_Disable(corenum, USB_MODE_Host);
186 
187  DEBUGOUT(("Host Mode Error\r\n"
188  " -- Error port %d\r\n"
189  " -- Error Code %d\r\n" ), corenum, ErrorCode);
190 
191  for (;; ) {}
192 }
193 
197 void EVENT_USB_Host_DeviceEnumerationFailed(const uint8_t corenum,
198  const uint8_t ErrorCode,
199  const uint8_t SubErrorCode)
200 {
201  DEBUGOUT(("Dev Enum Error\r\n"
202  " -- Error port %d\r\n"
203  " -- Error Code %d\r\n"
204  " -- Sub Error Code %d\r\n"
205  " -- In State %d\r\n" ),
206  corenum, ErrorCode, SubErrorCode, USB_HostState[corenum]);
207 
208 }
209 
213 {
214  return true;
215 }