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 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 /*****************************************************************************
36  * Private types/enumerations/variables
37  ****************************************************************************/
38 
44  .Config = {
45  .DataINPipeNumber = 1,
46  .DataINPipeDoubleBank = false,
47 
48  .DataOUTPipeNumber = 2,
49  .DataOUTPipeDoubleBank = false,
50 
51  .HIDInterfaceProtocol = HID_CSCP_KeyboardBootProtocol,
52  .PortNumber = 0,
53  },
54 };
55 
56 /*****************************************************************************
57  * Public types/enumerations/variables
58  ****************************************************************************/
59 
60 /*****************************************************************************
61  * Private functions
62  ****************************************************************************/
63 
64 /* Keyboard management task */
65 static void KeyboardHost_Task(void)
66 {
67  uint8_t KeyCode;
68 
69  if (USB_HostState[Keyboard_HID_Interface.Config.PortNumber] != HOST_STATE_Configured) {
70  return;
71  }
72 
73  if (HID_Host_IsReportReceived(&Keyboard_HID_Interface)) {
75  HID_Host_ReceiveReport(&Keyboard_HID_Interface, &KeyboardReport);
76 
77  KeyCode = KeyboardReport.KeyCode[0];
78 
79  if (KeyCode) {
80  char PressedKey = 0;
81 
82  /* Retrieve pressed key character if alphanumeric */
83  if ((KeyCode >= HID_KEYBOARD_SC_A) && (KeyCode <= HID_KEYBOARD_SC_Z)) {
84  PressedKey = (KeyCode - HID_KEYBOARD_SC_A) + 'A';
85  }
86  else if ((KeyCode >= HID_KEYBOARD_SC_1_AND_EXCLAMATION) &
88  PressedKey = (KeyCode - HID_KEYBOARD_SC_1_AND_EXCLAMATION) + '1';
89  }
90  else if (KeyCode == HID_KEYBOARD_SC_0_AND_CLOSING_PARENTHESIS) {
91  PressedKey = '0';
92  }
93  else if (KeyCode == HID_KEYBOARD_SC_SPACE) {
94  PressedKey = ' ';
95  }
96  else if (KeyCode == HID_KEYBOARD_SC_ENTER) {
97  PressedKey = '\n';
98  }
99 
100  if (PressedKey) {
101  putchar(PressedKey);
102  }
103  }
104  }
105 }
106 
107 /* Configures the board hardware and chip peripherals for the demo's functionality */
108 static void SetupHardware(void)
109 {
110  Board_Init();
111  USB_Init(Keyboard_HID_Interface.Config.PortNumber, USB_MODE_Host);
112  /* Hardware Initialization */
114 
115  /* Create a stdio stream for the serial port for stdin and stdout */
117 }
118 
119 /*****************************************************************************
120  * Public functions
121  ****************************************************************************/
122 
129 int main(void)
130 {
131  SetupHardware();
132 
133  DEBUGOUT("Keyboard Host Demo running.\r\n");
134 
135  for (;; ) {
137 
138  HID_Host_USBTask(&Keyboard_HID_Interface);
139  USB_USBTask(Keyboard_HID_Interface.Config.PortNumber, USB_MODE_Host);
140  }
141 }
142 
143 /* This indicates that a device has been attached to the host,
144  and starts the library USB task to begin the enumeration and USB
145  management process. */
146 void EVENT_USB_Host_DeviceAttached(const uint8_t corenum)
147 {
148  DEBUGOUT(("Device Attached on port %d\r\n"), corenum);
149 }
150 
151 /* This indicates that a device has been removed from the host,
152  and stops the library USB task management process. */
153 void EVENT_USB_Host_DeviceUnattached(const uint8_t corenum)
154 {
155  DEBUGOUT(("\r\nDevice Unattached on port %d\r\n"), corenum);
156 }
157 
158 /* This indicates that a device has been successfully
159  enumerated by the host and is now ready to be used by the
160  application. */
161 void EVENT_USB_Host_DeviceEnumerationComplete(const uint8_t corenum)
162 {
163  uint16_t ConfigDescriptorSize;
164  uint8_t ConfigDescriptorData[512];
165 
166  if (USB_Host_GetDeviceConfigDescriptor(corenum, 1, &ConfigDescriptorSize, ConfigDescriptorData,
167  sizeof(ConfigDescriptorData)) != HOST_GETCONFIG_Successful) {
168  DEBUGOUT("Error Retrieving Configuration Descriptor.\r\n");
169 
170  return;
171  }
172 
173  Keyboard_HID_Interface.Config.PortNumber = corenum;
174  if (HID_Host_ConfigurePipes(&Keyboard_HID_Interface,
175  ConfigDescriptorSize, ConfigDescriptorData) != HID_ENUMERROR_NoError) {
176  DEBUGOUT("Attached Device Not a Valid Keyboard.\r\n");
177 
178  return;
179  }
180 
182  DEBUGOUT("Error Setting Device Configuration.\r\n");
183 
184  return;
185  }
186 
187  if (HID_Host_SetBootProtocol(&Keyboard_HID_Interface) != 0) {
188  DEBUGOUT("Could not Set Boot Protocol Mode.\r\n");
189 
190  USB_Host_SetDeviceConfiguration(Keyboard_HID_Interface.Config.PortNumber, 0);
191  return;
192  }
193 
194  DEBUGOUT("Keyboard Enumerated.\r\n");
195 }
196 
197 /* This indicates that a hardware error occurred while in host mode. */
198 void EVENT_USB_Host_HostError(const uint8_t corenum, const uint8_t ErrorCode)
199 {
200  USB_Disable(corenum, USB_MODE_Host);
201 
202  DEBUGOUT(("Host Mode Error\r\n"
203  " -- Error port %d\r\n"
204  " -- Error Code %d\r\n" ), corenum, ErrorCode);
205 
206  /* Wait forever */
207  for (;; ) {}
208 }
209 
210 /* This indicates that a problem occurred while enumerating an
211  attached USB device. */
212 void EVENT_USB_Host_DeviceEnumerationFailed(const uint8_t corenum,
213  const uint8_t ErrorCode,
214  const uint8_t SubErrorCode)
215 {
216  DEBUGOUT(("Dev Enum Error\r\n"
217  " -- Error port %d\r\n"
218  " -- Error Code %d\r\n"
219  " -- Sub Error Code %d\r\n"
220  " -- In State %d\r\n" ),
221  corenum, ErrorCode, SubErrorCode, USB_HostState[corenum]);
222 }
223 
224 /* Dummy callback function for HID Parser */
226 {
227  return true;
228 }