Fahrplan: Finale Version.
[hackover2013-badge-firmware.git] / core / usbhid-rom / usbhid.c
1 /**************************************************************************/
2 /*!
3 @file usbhid.c
4 @author K. Townsend (microBuilder.eu)
5
6 @section LICENSE
7
8 Software License Agreement (BSD License)
9
10 Copyright (c) 2010, microBuilder SARL
11 All rights reserved.
12
13 Redistribution and use in source and binary forms, with or without
14 modification, are permitted provided that the following conditions are met:
15 1. Redistributions of source code must retain the above copyright
16 notice, this list of conditions and the following disclaimer.
17 2. Redistributions in binary form must reproduce the above copyright
18 notice, this list of conditions and the following disclaimer in the
19 documentation and/or other materials provided with the distribution.
20 3. Neither the name of the copyright holders nor the
21 names of its contributors may be used to endorse or promote products
22 derived from this software without specific prior written permission.
23
24 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
25 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
28 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35 /**************************************************************************/
36 #include <string.h>
37
38 #include "core/usbhid-rom/usb.h"
39 #include "core/usbhid-rom/usbconfig.h"
40 #include "core/rom_drivers.h"
41 #include "core/gpio/gpio.h"
42 #include "core/adc/adc.h"
43 #include "core/systick/systick.h"
44
45 #include "usbhid.h"
46
47 static USB_DEV_INFO DeviceInfo;
48 static HID_DEVICE_INFO HidDevInfo;
49 static ROM ** rom = (ROM **)0x1fff1ff8;
50
51 typedef struct usbhid_out_s
52 {
53 uint16_t gpio1Dir;
54 uint16_t gpio1Data;
55 uint16_t gpio2Dir;
56 uint16_t gpio2Data;
57 uint16_t gpio3Dir;
58 uint16_t gpio3Data;
59 uint16_t adc0;
60 uint16_t adc1;
61 uint16_t adc2;
62 uint16_t adc3;
63 uint32_t systicks;
64 uint32_t rollovers;
65 } usbhid_out_t;
66
67 /**************************************************************************/
68 /*!
69 @brief Gets the HID In Report (the report going from the LPC1343 to
70 the USB host)
71 */
72 /**************************************************************************/
73 void usbHIDGetInReport (uint8_t src[], uint32_t length)
74 {
75 usbhid_out_t out;
76
77 out.gpio1Dir = GPIO_GPIO1DIR;
78 out.gpio1Data = GPIO_GPIO1DATA;
79 out.gpio2Dir = GPIO_GPIO2DIR;
80 out.gpio2Data = GPIO_GPIO2DATA;
81 out.gpio3Dir = GPIO_GPIO3DIR;
82 out.gpio3Data = GPIO_GPIO3DATA;
83 out.adc0 = adcRead(0);
84 out.adc1 = adcRead(1);
85 out.adc2 = adcRead(2);
86 out.adc3 = adcRead(3);
87 out.systicks = systickGetTicks();
88 out.rollovers = systickGetRollovers();
89
90 size_t i = 0;
91 memcpy(&src[i], &out.gpio1Dir, sizeof out.gpio1Dir);
92 i += sizeof out.gpio1Dir;
93 memcpy(&src[i], &out.gpio1Data, sizeof out.gpio1Data);
94 i += sizeof out.gpio1Data;
95 memcpy(&src[i], &out.gpio2Dir, sizeof out.gpio2Dir);
96 i += sizeof out.gpio2Dir;
97 memcpy(&src[i], &out.gpio2Data, sizeof out.gpio2Data);
98 i += sizeof out.gpio2Data;
99 memcpy(&src[i], &out.gpio3Dir, sizeof out.gpio3Dir);
100 i += sizeof out.gpio3Dir;
101 memcpy(&src[i], &out.gpio3Data, sizeof out.gpio3Data);
102 i += sizeof out.gpio3Data;
103 memcpy(&src[i], &out.adc0, sizeof out.adc0);
104 i += sizeof out.adc0;
105 memcpy(&src[i], &out.adc1, sizeof out.adc1);
106 i += sizeof out.adc1;
107 memcpy(&src[i], &out.adc2, sizeof out.adc2);
108 i += sizeof out.adc2;
109 memcpy(&src[i], &out.adc3, sizeof out.adc3);
110 i += sizeof out.adc3;
111 memcpy(&src[i], &out.systicks, sizeof out.systicks);
112 i += sizeof out.systicks;
113 memcpy(&src[i], &out.rollovers, sizeof out.rollovers);
114 i += sizeof out.rollovers;
115 }
116
117 /**************************************************************************/
118 /*!
119 @brief Sets the HID Out Report (the report coming in from the USB
120 host to the LPC1343).
121 */
122 /**************************************************************************/
123 void usbHIDSetOutReport (uint8_t dst[], uint32_t length)
124 {
125 uint8_t PCOutReportData = dst[0];
126 // Check bit 0 in the incoming report to determine is LED should
127 // be enabled or disabled (1 = enabled, 0 = disabled)
128 if (PCOutReportData & (1<<0))
129 {
130 // Enable LED (set low)
131 gpioSetValue (CFG_LED_PORT, CFG_LED_PIN, 0);
132 }
133 else
134 {
135 // Disable LED (set high)
136 gpioSetValue (CFG_LED_PORT, CFG_LED_PIN, 1);
137 }
138 }
139
140 /**************************************************************************/
141 /*!
142 @brief Initialises the USB port
143
144 The ROM-based USB HID code is capable of configuring the PLL and pins
145 for USB, but there seems to be a bug in the code that sets the system
146 clock to 48MHz (normally the USB and System clocks can be configured
147 seperately). As such, this code does not use the "init_clk_pins()"
148 function in the rom, and the USB clock and pins are manually
149 configured.
150 */
151 /**************************************************************************/
152 void usbHIDInit (void)
153 {
154 // Setup USB clock
155 SCB_PDRUNCFG &= ~(SCB_PDSLEEPCFG_USBPAD_PD); // Power-up USB PHY
156 SCB_PDRUNCFG &= ~(SCB_PDSLEEPCFG_USBPLL_PD); // Power-up USB PLL
157
158 SCB_USBPLLCLKSEL = SCB_USBPLLCLKSEL_SOURCE_MAINOSC; // Select PLL Input
159 SCB_USBPLLCLKUEN = SCB_USBPLLCLKUEN_UPDATE; // Update Clock Source
160 SCB_USBPLLCLKUEN = SCB_USBPLLCLKUEN_DISABLE; // Toggle Update Register
161 SCB_USBPLLCLKUEN = SCB_USBPLLCLKUEN_UPDATE;
162
163 // Wait until the USB clock is updated
164 while (!(SCB_USBPLLCLKUEN & SCB_USBPLLCLKUEN_UPDATE));
165
166 // Set USB clock to 48MHz (12MHz x 4)
167 SCB_USBPLLCTRL = (SCB_USBPLLCTRL_MULT_4);
168 while (!(SCB_USBPLLSTAT & SCB_USBPLLSTAT_LOCK)); // Wait Until PLL Locked
169 SCB_USBCLKSEL = SCB_USBCLKSEL_SOURCE_USBPLLOUT;
170
171 // Set USB pin functions
172 IOCON_PIO0_1 &= ~IOCON_PIO0_1_FUNC_MASK;
173 IOCON_PIO0_1 |= IOCON_PIO0_1_FUNC_CLKOUT; // CLK OUT
174 IOCON_PIO0_3 &= ~IOCON_PIO0_3_FUNC_MASK;
175 IOCON_PIO0_3 |= IOCON_PIO0_3_FUNC_USB_VBUS; // VBus
176 IOCON_PIO0_6 &= ~IOCON_PIO0_6_FUNC_MASK;
177 IOCON_PIO0_6 |= IOCON_PIO0_6_FUNC_USB_CONNECT; // Soft Connect
178
179 // Disable internal resistor on VBUS (0.3)
180 gpioSetPullup(&IOCON_PIO0_3, gpioPullupMode_Inactive);
181
182 // HID Device Info
183 volatile int n;
184 HidDevInfo.idVendor = USB_VENDOR_ID;
185 HidDevInfo.idProduct = USB_PROD_ID;
186 HidDevInfo.bcdDevice = USB_DEVICE;
187 HidDevInfo.StrDescPtr = (uint32_t)&USB_HIDStringDescriptor[0];
188 HidDevInfo.InReportCount = sizeof(usbhid_out_t);
189 HidDevInfo.OutReportCount = 1;
190 HidDevInfo.SampleInterval = 0x20;
191 HidDevInfo.InReport = usbHIDGetInReport;
192 HidDevInfo.OutReport = usbHIDSetOutReport;
193
194 DeviceInfo.DevType = USB_DEVICE_CLASS_HUMAN_INTERFACE;
195 DeviceInfo.DevDetailPtr = (uint32_t)&HidDevInfo;
196
197 /* Enable Timer32_1, IOCON, and USB blocks (for USB ROM driver) */
198 SCB_SYSAHBCLKCTRL |= (SCB_SYSAHBCLKCTRL_CT32B1 | SCB_SYSAHBCLKCTRL_IOCON | SCB_SYSAHBCLKCTRL_USB_REG);
199
200 /* Use pll and pin init function in rom */
201 /* Warning: This will also set the system clock to 48MHz! */
202 // (*rom)->pUSBD->init_clk_pins();
203
204 /* insert a delay between clk init and usb init */
205 for (n = 0; n < 75; n++) {__asm("nop");}
206
207 (*rom)->pUSBD->init(&DeviceInfo); /* USB Initialization */
208 (*rom)->pUSBD->connect(true); /* USB Connect */
209 }
210
211 /**************************************************************************/
212 /*!
213 @brief Passes the USB interrupt to the internal ROM-based handler
214 */
215 /**************************************************************************/
216 #ifdef CFG_USBHID
217 void USB_IRQHandler()
218 {
219 (*rom)->pUSBD->isr();
220 }
221 #endif
222
This page took 0.075147 seconds and 5 git commands to generate.