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
32 #include <avr/eeprom.h>
33 #include <avr/interrupt.h> /* for sei() */
34 #include <util/delay.h> /* for _delay_ms() */
36 #include <avr/pgmspace.h> /* required by usbdrv.h */
38 #include "oddebug.h" /* This is also an example for using debug macros */
39 #include "requests.h" /* The custom request numbers we use */
40 #include "special_functions.h"
42 void update_pwm(void);
44 /* ------------------------------------------------------------------------- */
45 /* ----------------------------- USB interface ----------------------------- */
46 /* ------------------------------------------------------------------------- */
47 const PROGMEM char usbHidReportDescriptor[35] = { /* USB report descriptor */
48 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
49 0x09, 0x06, // USAGE (Keyboard)
50 0xa1, 0x01, // COLLECTION (Application)
51 0x05, 0x07, // USAGE_PAGE (Keyboard)
52 0x19, 0xe0, // USAGE_MINIMUM (Keyboard LeftControl)
53 0x29, 0xe7, // USAGE_MAXIMUM (Keyboard Right GUI)
54 0x15, 0x00, // LOGICAL_MINIMUM (0)
55 0x25, 0x01, // LOGICAL_MAXIMUM (1)
56 0x75, 0x01, // REPORT_SIZE (1)
57 0x95, 0x08, // REPORT_COUNT (8)
58 0x81, 0x02, // INPUT (Data,Var,Abs)
59 0x95, 0x01, // REPORT_COUNT (1)
60 0x75, 0x08, // REPORT_SIZE (8)
61 0x25, 0x65, // LOGICAL_MAXIMUM (101)
62 0x19, 0x00, // USAGE_MINIMUM (Reserved (no event indicated))
63 0x29, 0x65, // USAGE_MAXIMUM (Keyboard Application)
64 0x81, 0x00, // INPUT (Data,Ary,Abs)
65 0xc0 // END_COLLECTION
67 /* We use a simplifed keyboard report descriptor which does not support the
68 * boot protocol. We don't allow setting status LEDs and we only allow one
69 * simultaneous key press (except modifiers). We can therefore use short
70 * 2 byte input reports.
71 * The report descriptor has been created with usb.org's "HID Descriptor Tool"
72 * which can be downloaded from http://www.usb.org/developers/hidpage/.
73 * Redundant entries (such as LOGICAL_MINIMUM and USAGE_PAGE) have been omitted
74 * for the second INPUT item.
77 /* Keyboard usage values, see usb.org's HID-usage-tables document, chapter
78 * 10 Keyboard/Keypad Page for more codes.
80 #define MOD_CONTROL_LEFT (1<<0)
81 #define MOD_SHIFT_LEFT (1<<1)
82 #define MOD_ALT_LEFT (1<<2)
83 #define MOD_GUI_LEFT (1<<3)
84 #define MOD_CONTROL_RIGHT (1<<4)
85 #define MOD_SHIFT_RIGHT (1<<5)
86 #define MOD_ALT_RIGHT (1<<6)
87 #define MOD_GUI_RIGHT (1<<7)
148 #define UNI_BUFFER_SIZE 16
151 uint8_t w8[UNI_BUFFER_SIZE];
152 uint16_t w16[UNI_BUFFER_SIZE/2];
153 uint32_t w32[UNI_BUFFER_SIZE/4];
154 void* ptr[UNI_BUFFER_SIZE/sizeof(void*)];
157 static uint8_t uni_buffer_fill;
158 static uint8_t current_command;
160 static uchar reportBuffer[2]; /* buffer for HID reports */
161 static uchar idleRate; /* in 4 ms units */
163 static bool keyDidChange = false;
164 /* ------------------------------------------------------------------------- */
167 uint8_t read_button(void){
170 DDRB &= ~(1<<BUTTON_PIN);
171 PORTB |= 1<<BUTTON_PIN;
172 PORTB &= ~(1<<BUTTON_PIN);
174 DDRB |= t&(1<<BUTTON_PIN);
175 PORTB &= ~(t&(1<<BUTTON_PIN));
182 void init_temperature_sensor(void){
187 uint16_t read_temperture_sensor(void){
195 uchar usbFunctionSetup(uchar data[8])
197 usbRequest_t *rq = (void *)data;
199 usbMsgPtr = reportBuffer;
200 if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS){ /* class request type */
201 if(rq->bRequest == USBRQ_HID_GET_REPORT){ /* wValue: ReportType (highbyte), ReportID (lowbyte) */
202 /* we only have one report type, so don't look at wValue */
203 buildReport(keyPressed());
204 return sizeof(reportBuffer);
205 }else if(rq->bRequest == USBRQ_HID_GET_IDLE){
206 usbMsgPtr = &idleRate;
208 }else if(rq->bRequest == USBRQ_HID_SET_IDLE){
209 idleRate = rq->wValue.bytes[1];
212 /* no vendor specific requests implemented */
218 usbMsgLen_t usbFunctionSetup(uchar data[8])
220 usbRequest_t *rq = (usbRequest_t *)data;
221 if ((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS) { /* class request type */
223 if (rq->bRequest == USBRQ_HID_GET_REPORT){ /* wValue: ReportType (highbyte), ReportID (lowbyte) */
224 /* we only have one report type, so don't look at wValue */
225 if (color.name.red == 133) {
227 usbMsgPtr = reportBuffer;
229 reportBuffer[1] = KEY_X;
231 return sizeof(reportBuffer);
232 } else if (rq->bRequest == USBRQ_HID_GET_IDLE) {
233 usbMsgPtr = &idleRate;
235 }else if (rq->bRequest == USBRQ_HID_SET_IDLE) {
236 usbMsgPtr = reportBuffer;
237 idleRate = rq->wValue.bytes[1];
240 if ((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_VENDOR) {
241 current_command = rq->bRequest;
244 case CUSTOM_RQ_SET_RGB:
246 case CUSTOM_RQ_GET_RGB:{
248 if(len>rq->wLength.word){
249 len = rq->wLength.word;
251 usbMsgPtr = (uchar*)color.idx;
254 case CUSTOM_RQ_READ_MEM:
255 usbMsgPtr = (uchar*)rq->wValue.word;
256 return rq->wLength.word;
257 case CUSTOM_RQ_WRITE_MEM:
258 case CUSTOM_RQ_EXEC_SPM:
260 uni_buffer.w16[0] = rq->wValue.word;
261 uni_buffer.w16[1] = rq->wLength.word;
263 case CUSTOM_RQ_READ_FLASH:
264 uni_buffer.w16[0] = rq->wValue.word;
265 uni_buffer.w16[1] = rq->wLength.word;
267 case CUSTOM_RQ_RESET:
268 soft_reset((uint8_t)(rq->wValue.word));
270 case CUSTOM_RQ_READ_BUTTON:
271 uni_buffer.w8[0] = read_button();
272 usbMsgPtr = uni_buffer.w8;
274 case CUSTOM_RQ_READ_TMPSENS:
275 uni_buffer.w16[0] = read_temperture_sensor();
276 usbMsgPtr = uni_buffer.w8;
281 return 0; /* default for not implemented requests: return no data back to host */
284 uchar usbFunctionWrite(uchar *data, uchar len)
286 switch(current_command){
287 case CUSTOM_RQ_SET_RGB:
291 memcpy(color.idx, data, 6);
294 case CUSTOM_RQ_WRITE_MEM:
295 memcpy(uni_buffer.ptr[0], data, len);
296 uni_buffer.w16[0] += len;
297 return !(uni_buffer.w16[1] -= len);
298 case CUSTOM_RQ_EXEC_SPM:
299 if(uni_buffer_fill<8){
300 uint8_t l = 8-uni_buffer_fill;
304 memcpy(&(uni_buffer.w8[uni_buffer_fill]), data, len);
305 uni_buffer_fill += len;
308 uni_buffer.w16[1] -= len;
309 if(uni_buffer.w16[1]>8){
310 memcpy(uni_buffer.ptr[0], data, len);
311 uni_buffer.w16[0] += len;
314 memcpy(&(uni_buffer.w8[uni_buffer_fill]), data, len);
315 exec_spm(uni_buffer.w16[2], uni_buffer.w16[3], uni_buffer.ptr[0], data, len);
323 uchar usbFunctionRead(uchar *data, uchar len){
325 switch(current_command){
326 case CUSTOM_RQ_READ_FLASH:
328 *data++ = pgm_read_byte((uni_buffer.w16[0])++);
337 static void calibrateOscillator(void)
340 uchar trialValue = 0, optimumValue;
341 int x, optimumDev, targetValue = (unsigned)(1499 * (double)F_CPU / 10.5e6 + 0.5);
343 /* do a binary search: */
345 OSCCAL = trialValue + step;
346 x = usbMeasureFrameLength(); // proportional to current real frequency
347 if(x < targetValue) // frequency still too low
351 /* We have a precision of +/- 1 for optimum OSCCAL here */
352 /* now do a neighborhood search for optimum value */
353 optimumValue = trialValue;
354 optimumDev = x; // this is certainly far away from optimum
355 for(OSCCAL = trialValue - 1; OSCCAL <= trialValue + 1; OSCCAL++){
356 x = usbMeasureFrameLength() - targetValue;
361 optimumValue = OSCCAL;
364 OSCCAL = optimumValue;
368 void usbEventResetReady(void)
370 cli(); // usbMeasureFrameLength() counts CPU cycles, so disable interrupts.
371 calibrateOscillator();
373 // we never read the value from eeprom so this causes only degradation of eeprom
374 // eeprom_write_byte(0, OSCCAL); // store the calibrated value in EEPROM
377 /* ------------------------------------------------------------------------- */
384 /* Even if you don't use the watchdog, turn it off here. On newer devices,
385 * the status of the watchdog (on/off, period) is PRESERVED OVER RESET!
387 /* RESET status: all port bits are inputs without pull-up.
388 * That's the way we need D+ and D-. Therefore we don't need any
389 * additional hardware initialization.
392 init_temperature_sensor();
394 usbDeviceDisconnect(); /* enforce re-enumeration, do this while interrupts are disabled! */
396 while(--i){ /* fake USB disconnect for > 250 ms */
401 LED_PORT_DDR |= _BV(R_BIT) | _BV(G_BIT) | _BV(B_BIT); /* make the LED bit an output */
405 for(;;){ /* main event loop */
410 if(keyDidChange && usbInterruptIsReady()){
413 /* use last key and not current key status in order to avoid lost
414 changes in key status. */
416 reportBuffer[1] = KEY_Y;
417 usbSetInterrupt(reportBuffer, sizeof(reportBuffer));
423 /* ------------------------------------------------------------------------- */