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 Keyboard Host Example
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 
35 uint8_t uartPolling[100];
36 
42  .Config = {
43  .DataINPipeNumber = 1,
44  .DataINPipeDoubleBank = false,
45 
46  .DataOUTPipeNumber = 2,
47  .DataOUTPipeDoubleBank = false,
48 
49  .HIDInterfaceProtocol = HID_CSCP_KeyboardBootProtocol,
50  },
51 };
52 
56 int main(void)
57 {
58  SetupHardware();
59 
60  DEBUGOUT("Keyboard Host Demo running.\r\n");
61 
62  for (;; ) {
64 
65  HID_Host_USBTask(&Keyboard_HID_Interface);
66  USB_USBTask();
67  }
68 }
69 
71 void SetupHardware(void)
72 {
73  Board_Init();
74  /* Hardware Initialization */
76 
77  USB_Init();
78 
79  /* Create a stdio stream for the serial port for stdin and stdout */
81 }
82 
87 {
88  if (USB_HostState[Keyboard_HID_Interface.Config.PortNumber] != HOST_STATE_Configured) {
89  return;
90  }
91 
92  if (HID_Host_IsReportReceived(&Keyboard_HID_Interface)) {
94  HID_Host_ReceiveReport(&Keyboard_HID_Interface, &KeyboardReport);
95 
96  // LEDs_ChangeLEDs(LEDS_LED1, (KeyboardReport.Modifier) ? LEDS_LED1 : 0);
97 
98  uint8_t KeyCode = KeyboardReport.KeyCode[0];
99 
100  if (KeyCode) {
101  char PressedKey = 0;
102 
103  // LEDs_ToggleLEDs(LEDS_LED2);
104 
105  /* Retrieve pressed key character if alphanumeric */
106  if ((KeyCode >= HID_KEYBOARD_SC_A) && (KeyCode <= HID_KEYBOARD_SC_Z)) {
107  PressedKey = (KeyCode - HID_KEYBOARD_SC_A) + 'A';
108  }
109  else if ((KeyCode >= HID_KEYBOARD_SC_1_AND_EXCLAMATION) &
111  PressedKey = (KeyCode - HID_KEYBOARD_SC_1_AND_EXCLAMATION) + '1';
112  }
113  else if (KeyCode == HID_KEYBOARD_SC_0_AND_CLOSING_PARENTHESIS) {
114  PressedKey = '0';
115  }
116  else if (KeyCode == HID_KEYBOARD_SC_SPACE) {
117  PressedKey = ' ';
118  }
119  else if (KeyCode == HID_KEYBOARD_SC_ENTER) {
120  PressedKey = '\n';
121  }
122 
123  if (PressedKey) {
124  putchar(PressedKey);
125  }
126  }
127  }
128 }
129 
133 void EVENT_USB_Host_DeviceAttached(const uint8_t corenum)
134 {
135  DEBUGOUT(("Device Attached on port %d\r\n"), corenum);
136  // LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
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  // LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
146 }
147 
151 void EVENT_USB_Host_DeviceEnumerationComplete(const uint8_t corenum)
152 {
153  // LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
154 
155  uint16_t ConfigDescriptorSize;
156  uint8_t ConfigDescriptorData[512];
157 
158  if (USB_Host_GetDeviceConfigDescriptor(corenum, 1, &ConfigDescriptorSize, ConfigDescriptorData,
159  sizeof(ConfigDescriptorData)) != HOST_GETCONFIG_Successful) {
160  DEBUGOUT("Error Retrieving Configuration Descriptor.\r\n");
161  // LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
162  return;
163  }
164 
165  Keyboard_HID_Interface.Config.PortNumber = corenum;
166  if (HID_Host_ConfigurePipes(&Keyboard_HID_Interface,
167  ConfigDescriptorSize, ConfigDescriptorData) != HID_ENUMERROR_NoError) {
168  DEBUGOUT("Attached Device Not a Valid Keyboard.\r\n");
169  // LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
170  return;
171  }
172 
174  DEBUGOUT("Error Setting Device Configuration.\r\n");
175  // LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
176  return;
177  }
178 
179  if (HID_Host_SetBootProtocol(&Keyboard_HID_Interface) != 0) {
180  DEBUGOUT("Could not Set Boot Protocol Mode.\r\n");
181  // LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
182  USB_Host_SetDeviceConfiguration(Keyboard_HID_Interface.Config.PortNumber, 0);
183  return;
184  }
185 
186  DEBUGOUT("Keyboard Enumerated.\r\n");
187  // LEDs_SetAllLEDs(LEDMASK_USB_READY);
188 }
189 
191 void EVENT_USB_Host_HostError(const uint8_t corenum, const uint8_t ErrorCode)
192 {
193  USB_Disable();
194 
195  DEBUGOUT(("Host Mode Error\r\n"
196  " -- Error port %d\r\n"
197  " -- Error Code %d\r\n" ), corenum, ErrorCode);
198 
199  // LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
200  for (;; ) ;
201 }
202 
206 void EVENT_USB_Host_DeviceEnumerationFailed(const uint8_t corenum,
207  const uint8_t ErrorCode,
208  const uint8_t SubErrorCode)
209 {
210  DEBUGOUT(("Dev Enum Error\r\n"
211  " -- Error port %d\r\n"
212  " -- Error Code %d\r\n"
213  " -- Sub Error Code %d\r\n"
214  " -- In State %d\r\n" ),
215  corenum, ErrorCode, SubErrorCode, USB_HostState[corenum]);
216 
217  // LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
218 }
219 
223 {
224  return true;
225 }