LPCOpen Platform
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 "KeyboardHost.h"
34 // #include "USBController_LPC.h"
35 
41  .Config = {
42  .DataINPipeNumber = 1,
43  .DataINPipeDoubleBank = false,
44 
45  .DataOUTPipeNumber = 2,
46  .DataOUTPipeDoubleBank = false,
47 
48  .HIDInterfaceProtocol = HID_CSCP_KeyboardBootProtocol,
49  },
50 };
51 
55 char keyboard(void)
56 {
57  char tecla;
59 
60  do {
61  tecla = KeyboardHost_Task();
62 
63  HID_Host_USBTask(&Keyboard_HID_Interface);
64  USB_USBTask();
65  } while (tecla != 'D');
66  return tecla;
67 }
68 
71 {
73  USB_Disable();
75  USB_Init();
76 
77  /* Create a stdio stream for the serial port for stdin and stdout */
79 }
80 
85 {
86  if (USB_HostState[Keyboard_HID_Interface.Config.PortNumber] != HOST_STATE_Configured) {
87  return 0;
88  }
89 
90  if (HID_Host_IsReportReceived(&Keyboard_HID_Interface)) {
92  HID_Host_ReceiveReport(&Keyboard_HID_Interface, &KeyboardReport);
93 
94  // LEDs_ChangeLEDs(LEDS_LED1, (KeyboardReport.Modifier) ? LEDS_LED1 : 0);
95 
96  uint8_t KeyCode = KeyboardReport.KeyCode[0];
97 
98  if (KeyCode) {
99  char PressedKey = 0;
100 
101  // LEDs_ToggleLEDs(LEDS_LED2);
102 
103  /* Retrieve pressed key character if alphanumeric */
104  if ((KeyCode >= HID_KEYBOARD_SC_A) && (KeyCode <= HID_KEYBOARD_SC_Z)) {
105  PressedKey = (KeyCode - HID_KEYBOARD_SC_A) + 'A';
106  }
107  else if ((KeyCode >= HID_KEYBOARD_SC_1_AND_EXCLAMATION) &
109  PressedKey = (KeyCode - HID_KEYBOARD_SC_1_AND_EXCLAMATION) + '1';
110  }
111  else if (KeyCode == HID_KEYBOARD_SC_0_AND_CLOSING_PARENTHESIS) {
112  PressedKey = '0';
113  }
114  else if (KeyCode == HID_KEYBOARD_SC_SPACE) {
115  PressedKey = ' ';
116  }
117  else if (KeyCode == HID_KEYBOARD_SC_ENTER) {
118  PressedKey = '\n';
119  }
120 
121  if (PressedKey) {
122  putchar(PressedKey);
123  }
124  return PressedKey;
125  }
126  return 0;
127  }
128  return 0;
129 }
130 
134 void EVENT_USB_Host_DeviceAttached(const uint8_t corenum)
135 {
136  DEBUGOUT("Device Attached.\r\n");
137  // LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
138 }
139 
143 void EVENT_USB_Host_DeviceUnattached(const uint8_t corenum)
144 {
145  DEBUGOUT("\r\nDevice Unattached.\r\n");
146  // LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
147 }
148 
152 void EVENT_USB_Host_DeviceEnumerationComplete(const uint8_t corenum)
153 {
154  // LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
155 
156  uint16_t ConfigDescriptorSize;
157  uint8_t ConfigDescriptorData[512];
158 
159  if (USB_Host_GetDeviceConfigDescriptor(corenum, 1, &ConfigDescriptorSize, ConfigDescriptorData,
160  sizeof(ConfigDescriptorData)) != HOST_GETCONFIG_Successful) {
161  DEBUGOUT("Error Retrieving Configuration Descriptor.\r\n");
162  // LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
163  return;
164  }
165  Keyboard_HID_Interface.Config.PortNumber = corenum;
166 
167  if (HID_Host_ConfigurePipes(&Keyboard_HID_Interface,
168  ConfigDescriptorSize, ConfigDescriptorData) != HID_ENUMERROR_NoError) {
169  DEBUGOUT("Attached Device Not a Valid Keyboard.\r\n");
170  // LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
171  return;
172  }
173 
175  DEBUGOUT("Error Setting Device Configuration.\r\n");
176  // LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
177  return;
178  }
179 
180  if (HID_Host_SetBootProtocol(&Keyboard_HID_Interface) != 0) {
181  DEBUGOUT("Could not Set Boot Protocol Mode.\r\n");
182  // LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
184  return;
185  }
186 
187  DEBUGOUT("Keyboard Enumerated.\r\n");
188  // LEDs_SetAllLEDs(LEDMASK_USB_READY);
189 }
190 
192 void EVENT_USB_Host_HostError(const uint8_t corenum, const uint8_t ErrorCode)
193 {
194  USB_Disable();
195 
196  DEBUGOUT("Host Mode Error\r\n"
197  " -- Error Code %d\r\n", ErrorCode);
198 
199  // LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
200  for (;; ) ;
201 }
202 
206 void EVENT_USB_Host_DeviceEnumerationFailed(const uint8_t corenum, const uint8_t ErrorCode,
207  const uint8_t SubErrorCode)
208 {
209  DEBUGOUT("Dev Enum Error\r\n"
210  " -- Error Code %d\r\n"
211  " -- Sub Error Code %d\r\n"
212  " -- In State %d\r\n", ErrorCode, SubErrorCode, USB_HostState);
213 
214  // LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
215 }
216 
220  return true;
221 }