2 * Project: hid-custom-rq example
3 * Author: Christian Starkjohann
4 * Creation Date: 2008-04-07
6 * Copyright: (c) 2008 by OBJECTIVE DEVELOPMENT Software GmbH
7 * License: GNU GPL v2 (see License.txt), GNU GPL v3 or proprietary (CommercialLicense.txt)
8 * This Revision: $Id: main.c 692 2008-11-07 15:07:40Z cs $
12 This example should run on most AVRs with only little changes. No special
13 hardware resources except INT0 are used. You may have to change usbconfig.h for
14 different I/O pins for USB. Please note that USB D+ must be the INT0 pin, or
15 at least be connected to INT0 as well.
16 We assume that an LED is connected to port B bit 0. If you connect it to a
17 different port or bit, change the macros below:
19 #define LED_PORT_DDR DDRB
20 #define LED_PORT_OUTPUT PORTB
31 #include <avr/eeprom.h>
32 #include <avr/interrupt.h> /* for sei() */
33 #include <util/delay.h> /* for _delay_ms() */
35 #include <avr/pgmspace.h> /* required by usbdrv.h */
37 #include "oddebug.h" /* This is also an example for using debug macros */
38 #include "requests.h" /* The custom request numbers we use */
39 #include "special_functions.h"
41 void update_pwm(void);
43 /* ------------------------------------------------------------------------- */
44 /* ----------------------------- USB interface ----------------------------- */
45 /* ------------------------------------------------------------------------- */
46 const PROGMEM char usbHidReportDescriptor[35] = { /* USB report descriptor */
47 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
48 0x09, 0x06, // USAGE (Keyboard)
49 0xa1, 0x01, // COLLECTION (Application)
50 0x05, 0x07, // USAGE_PAGE (Keyboard)
51 0x19, 0xe0, // USAGE_MINIMUM (Keyboard LeftControl)
52 0x29, 0xe7, // USAGE_MAXIMUM (Keyboard Right GUI)
53 0x15, 0x00, // LOGICAL_MINIMUM (0)
54 0x25, 0x01, // LOGICAL_MAXIMUM (1)
55 0x75, 0x01, // REPORT_SIZE (1)
56 0x95, 0x08, // REPORT_COUNT (8)
57 0x81, 0x02, // INPUT (Data,Var,Abs)
58 0x95, 0x01, // REPORT_COUNT (1)
59 0x75, 0x08, // REPORT_SIZE (8)
60 0x25, 0x65, // LOGICAL_MAXIMUM (101)
61 0x19, 0x00, // USAGE_MINIMUM (Reserved (no event indicated))
62 0x29, 0x65, // USAGE_MAXIMUM (Keyboard Application)
63 0x81, 0x00, // INPUT (Data,Ary,Abs)
64 0xc0 // END_COLLECTION
66 /* We use a simplifed keyboard report descriptor which does not support the
67 * boot protocol. We don't allow setting status LEDs and we only allow one
68 * simultaneous key press (except modifiers). We can therefore use short
69 * 2 byte input reports.
70 * The report descriptor has been created with usb.org's "HID Descriptor Tool"
71 * which can be downloaded from http://www.usb.org/developers/hidpage/.
72 * Redundant entries (such as LOGICAL_MINIMUM and USAGE_PAGE) have been omitted
73 * for the second INPUT item.
76 /* Keyboard usage values, see usb.org's HID-usage-tables document, chapter
77 * 10 Keyboard/Keypad Page for more codes.
79 #define MOD_CONTROL_LEFT (1<<0)
80 #define MOD_SHIFT_LEFT (1<<1)
81 #define MOD_ALT_LEFT (1<<2)
82 #define MOD_GUI_LEFT (1<<3)
83 #define MOD_CONTROL_RIGHT (1<<4)
84 #define MOD_SHIFT_RIGHT (1<<5)
85 #define MOD_ALT_RIGHT (1<<6)
86 #define MOD_GUI_RIGHT (1<<7)
147 #define UNI_BUFFER_SIZE 16
150 uint8_t w8[UNI_BUFFER_SIZE];
151 uint16_t w16[UNI_BUFFER_SIZE/2];
152 uint32_t w32[UNI_BUFFER_SIZE/4];
153 void* ptr[UNI_BUFFER_SIZE/sizeof(void*)];
156 static uint8_t uni_buffer_fill;
157 static uint8_t current_command;
158 /* ------------------------------------------------------------------------- */
161 uint8_t read_button(void){
165 DDRB &= ~(1<<BUTTON_PIN);
166 PORTB |= 1<<BUTTON_PIN;
167 PORTB &= ~(1<<BUTTON_PIN);
169 DDRB |= t&(1<<BUTTON_PIN);
170 PORTB &= ~(t&(1<<BUTTON_PIN));
177 void init_tmpsensor(void){
182 uint16_t read_tmpsensor(void){
189 usbMsgLen_t usbFunctionSetup(uchar data[8])
191 usbRequest_t *rq = (usbRequest_t *)data;
193 if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_VENDOR)
195 current_command = rq->bRequest;
198 case CUSTOM_RQ_SET_RED:
199 color.name.red = rq->wValue.bytes[0];
201 case CUSTOM_RQ_SET_GREEN:
202 color.name.green = rq->wValue.bytes[0];
204 case CUSTOM_RQ_SET_BLUE:
205 color.name.blue = rq->wValue.bytes[0];
207 case CUSTOM_RQ_SET_RGB:
209 case CUSTOM_RQ_GET_RGB:{
211 if(len>rq->wLength.word){
212 len = rq->wLength.word;
214 usbMsgPtr = (uchar*)color.idx;
217 case CUSTOM_RQ_READ_MEM:
218 usbMsgPtr = (uchar*)rq->wValue.word;
219 return rq->wLength.word;
220 case CUSTOM_RQ_WRITE_MEM:
221 case CUSTOM_RQ_EXEC_SPM:
223 uni_buffer.w16[0] = rq->wValue.word;
224 uni_buffer.w16[1] = rq->wLength.word;
226 case CUSTOM_RQ_READ_FLASH:
227 uni_buffer.w16[0] = rq->wValue.word;
228 uni_buffer.w16[1] = rq->wLength.word;
230 case CUSTOM_RQ_RESET:
231 soft_reset((uint8_t)(rq->wValue.word));
233 case CUSTOM_RQ_READ_BUTTON:
234 uni_buffer.w8[0] = read_button();
235 usbMsgPtr = uni_buffer.w8;
237 case CUSTOM_RQ_READ_TMPSENS:
238 uni_buffer.w16[0] = read_tmpsensor();
239 usbMsgPtr = uni_buffer.w8;
245 /* calls requests USBRQ_HID_GET_REPORT and USBRQ_HID_SET_REPORT are
246 * not implemented since we never call them. The operating system
247 * won't call them either because our descriptor defines no meaning.
250 return 0; /* default for not implemented requests: return no data back to host */
253 uchar usbFunctionWrite(uchar *data, uchar len)
255 switch(current_command){
256 case CUSTOM_RQ_SET_RGB:
260 memcpy(color.idx, data, 6);
262 case CUSTOM_RQ_WRITE_MEM:
263 memcpy(uni_buffer.ptr[0], data, len);
264 uni_buffer.w16[0] += len;
265 return !(uni_buffer.w16[1] -= len);
266 case CUSTOM_RQ_EXEC_SPM:
267 if(uni_buffer_fill<8){
268 uint8_t l = 8-uni_buffer_fill;
272 memcpy(&(uni_buffer.w8[uni_buffer_fill]), data, len);
273 uni_buffer_fill += len;
276 uni_buffer.w16[1] -= len;
277 if(uni_buffer.w16[1]>8){
278 memcpy(uni_buffer.ptr[0], data, len);
279 uni_buffer.w16[0] += len;
282 memcpy(&(uni_buffer.w8[uni_buffer_fill]), data, len);
283 exec_spm(uni_buffer.w16[2], uni_buffer.w16[3], uni_buffer.ptr[0], data, len);
291 uchar usbFunctionRead(uchar *data, uchar len){
293 switch(current_command){
294 case CUSTOM_RQ_READ_FLASH:
296 *data++ = pgm_read_byte((uni_buffer.w16[0])++);
305 static void calibrateOscillator(void)
308 uchar trialValue = 0, optimumValue;
309 int x, optimumDev, targetValue = (unsigned)(1499 * (double)F_CPU / 10.5e6 + 0.5);
311 /* do a binary search: */
313 OSCCAL = trialValue + step;
314 x = usbMeasureFrameLength(); // proportional to current real frequency
315 if(x < targetValue) // frequency still too low
319 /* We have a precision of +/- 1 for optimum OSCCAL here */
320 /* now do a neighborhood search for optimum value */
321 optimumValue = trialValue;
322 optimumDev = x; // this is certainly far away from optimum
323 for(OSCCAL = trialValue - 1; OSCCAL <= trialValue + 1; OSCCAL++){
324 x = usbMeasureFrameLength() - targetValue;
329 optimumValue = OSCCAL;
332 OSCCAL = optimumValue;
336 void usbEventResetReady(void)
338 cli(); // usbMeasureFrameLength() counts CPU cycles, so disable interrupts.
339 calibrateOscillator();
341 // we never read the value from eeprom so this causes only degradation of eeprom
342 // eeprom_write_byte(0, OSCCAL); // store the calibrated value in EEPROM
345 /* ------------------------------------------------------------------------- */
352 /* Even if you don't use the watchdog, turn it off here. On newer devices,
353 * the status of the watchdog (on/off, period) is PRESERVED OVER RESET!
355 /* RESET status: all port bits are inputs without pull-up.
356 * That's the way we need D+ and D-. Therefore we don't need any
357 * additional hardware initialization.
362 usbDeviceDisconnect(); /* enforce re-enumeration, do this while interrupts are disabled! */
364 while(--i){ /* fake USB disconnect for > 250 ms */
369 LED_PORT_DDR |= _BV(R_BIT) | _BV(G_BIT) | _BV(B_BIT); /* make the LED bit an output */
373 for(;;){ /* main event loop */
382 /* ------------------------------------------------------------------------- */