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 PROGMEM const char usbHidReportDescriptor[USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH] = {
48 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
49 0x09, 0x06, // USAGE (Keyboard)
50 0xa1, 0x01, // COLLECTION (Application)
51 0x75, 0x01, // REPORT_SIZE (1)
52 0x95, 0x08, // REPORT_COUNT (8)
53 0x05, 0x07, // USAGE_PAGE (Keyboard)(Key Codes)
54 0x19, 0xe0, // USAGE_MINIMUM (Keyboard LeftControl)(224)
55 0x29, 0xe7, // USAGE_MAXIMUM (Keyboard Right GUI)(231)
56 0x15, 0x00, // LOGICAL_MINIMUM (0)
57 0x25, 0x01, // LOGICAL_MAXIMUM (1)
58 0x81, 0x02, // INPUT (Data,Var,Abs) ; Modifier byte
59 0x95, 0x01, // REPORT_COUNT (1)
60 0x75, 0x08, // REPORT_SIZE (8)
61 0x81, 0x03, // INPUT (Cnst,Var,Abs) ; Reserved byte
62 0x95, 0x05, // REPORT_COUNT (5)
63 0x75, 0x01, // REPORT_SIZE (1)
64 0x05, 0x08, // USAGE_PAGE (LEDs)
65 0x19, 0x01, // USAGE_MINIMUM (Num Lock)
66 0x29, 0x05, // USAGE_MAXIMUM (Kana)
67 0x91, 0x02, // OUTPUT (Data,Var,Abs) ; LED report
68 0x95, 0x01, // REPORT_COUNT (1)
69 0x75, 0x03, // REPORT_SIZE (3)
70 0x91, 0x03, // OUTPUT (Cnst,Var,Abs) ; LED report padding
71 0x95, 0x06, // REPORT_COUNT (6)
72 0x75, 0x08, // REPORT_SIZE (8)
73 0x15, 0x00, // LOGICAL_MINIMUM (0)
74 0x25, 0x65, // LOGICAL_MAXIMUM (101)
75 0x05, 0x07, // USAGE_PAGE (Keyboard)(Key Codes)
76 0x19, 0x00, // USAGE_MINIMUM (Reserved (no event indicated))(0)
77 0x29, 0x65, // USAGE_MAXIMUM (Keyboard Application)(101)
78 0x81, 0x00, // INPUT (Data,Ary,Abs)
79 0xc0 // END_COLLECTION
82 /* Keyboard usage values, see usb.org's HID-usage-tables document, chapter
83 * 10 Keyboard/Keypad Page for more codes.
85 #define MOD_CONTROL_LEFT (1<<0)
86 #define MOD_SHIFT_LEFT (1<<1)
87 #define MOD_ALT_LEFT (1<<2)
88 #define MOD_GUI_LEFT (1<<3)
89 #define MOD_CONTROL_RIGHT (1<<4)
90 #define MOD_SHIFT_RIGHT (1<<5)
91 #define MOD_ALT_RIGHT (1<<6)
92 #define MOD_GUI_RIGHT (1<<7)
146 #define SCROLL_LOCK 4
149 #define UNI_BUFFER_SIZE 16
151 static uint8_t dbg_buffer[8];
154 uint8_t w8[UNI_BUFFER_SIZE];
155 uint16_t w16[UNI_BUFFER_SIZE/2];
156 uint32_t w32[UNI_BUFFER_SIZE/4];
157 void* ptr[UNI_BUFFER_SIZE/sizeof(void*)];
160 static uint8_t uni_buffer_fill;
161 static uint8_t current_command;
170 #define STATE_SEND_KEY 1
171 #define STATE_RELEASE_KEY 2
174 static keyboard_report_t keyboard_report; // sent to PC
175 static uchar idleRate; /* in 4 ms units */
176 static uchar key_state = STATE_WAIT;
177 volatile static uchar LED_state = 0xff; // received from PC
178 /* ------------------------------------------------------------------------- */
180 void buildReport(uchar send_key) {
181 keyboard_report.modifier = 0;
183 if(send_key >= 'a' && send_key <= 'z')
184 keyboard_report.keycode[0] = 4 + (send_key - 'a');
186 keyboard_report.keycode[0] = 0;
189 uint8_t read_button(void){
192 DDRB &= ~(1<<BUTTON_PIN);
193 PORTB |= 1<<BUTTON_PIN;
194 PORTB &= ~(1<<BUTTON_PIN);
196 DDRB |= t&(1<<BUTTON_PIN);
197 PORTB &= ~(t&(1<<BUTTON_PIN));
204 void init_temperature_sensor(void){
209 uint16_t read_temperture_sensor(void){
216 usbMsgLen_t usbFunctionSetup(uchar data[8])
218 usbRequest_t *rq = (usbRequest_t *)data;
219 if ((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS) { /* class request type */
220 switch(rq->bRequest) {
221 case USBRQ_HID_GET_REPORT: // send "no keys pressed" if asked here
222 // wValue: ReportType (highbyte), ReportID (lowbyte)
223 usbMsgPtr = (void *)&keyboard_report; // we only have this one
224 keyboard_report.modifier = 0;
225 keyboard_report.keycode[0] = 0;
226 return sizeof(keyboard_report);
227 case USBRQ_HID_SET_REPORT: // if wLength == 1, should be LED state
228 if (rq->wLength.word == 1) {
229 current_command = LED_WRITE;
233 case USBRQ_HID_GET_IDLE: // send idle rate to PC as required by spec
234 usbMsgPtr = &idleRate;
236 case USBRQ_HID_SET_IDLE: // save idle rate as required by spec
237 idleRate = rq->wValue.bytes[1];
241 if ((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_VENDOR) {
242 current_command = rq->bRequest;
245 case CUSTOM_RQ_PRESS_BUTTON:
246 key_state = STATE_SEND_KEY;
248 case CUSTOM_RQ_CLR_DBG:
249 memset(dbg_buffer, 0, sizeof(dbg_buffer));
251 case CUSTOM_RQ_SET_DBG:
253 case CUSTOM_RQ_GET_DBG:{
255 if(len > rq->wLength.word){
256 len = rq->wLength.word;
258 usbMsgPtr = dbg_buffer;
261 case CUSTOM_RQ_READ_MEM:
262 usbMsgPtr = (uchar*)rq->wValue.word;
263 return rq->wLength.word;
264 case CUSTOM_RQ_WRITE_MEM:
265 case CUSTOM_RQ_EXEC_SPM:
267 uni_buffer.w16[0] = rq->wValue.word;
268 uni_buffer.w16[1] = rq->wLength.word;
270 case CUSTOM_RQ_READ_FLASH:
271 uni_buffer.w16[0] = rq->wValue.word;
272 uni_buffer.w16[1] = rq->wLength.word;
274 case CUSTOM_RQ_RESET:
275 soft_reset((uint8_t)(rq->wValue.word));
277 case CUSTOM_RQ_READ_BUTTON:
278 uni_buffer.w8[0] = read_button();
279 usbMsgPtr = uni_buffer.w8;
281 case CUSTOM_RQ_READ_TMPSENS:
282 uni_buffer.w16[0] = read_temperture_sensor();
283 usbMsgPtr = uni_buffer.w8;
288 return 0; /* default for not implemented requests: return no data back to host */
292 uchar usbFunctionWrite(uchar *data, uchar len)
294 switch(current_command){
297 if (data[0] != LED_state)
299 return 1; // Data read, not expecting more
300 case CUSTOM_RQ_SET_DBG:
301 if(len > sizeof(dbg_buffer)){
302 len = sizeof(dbg_buffer);
304 memcpy(dbg_buffer, data, len);
306 case CUSTOM_RQ_WRITE_MEM:
307 memcpy(uni_buffer.ptr[0], data, len);
308 uni_buffer.w16[0] += len;
309 return !(uni_buffer.w16[1] -= len);
310 case CUSTOM_RQ_EXEC_SPM:
311 if(uni_buffer_fill < 8){
312 uint8_t l = 8 - uni_buffer_fill;
316 memcpy(&(uni_buffer.w8[uni_buffer_fill]), data, len);
317 uni_buffer_fill += len;
320 uni_buffer.w16[1] -= len;
321 if (uni_buffer.w16[1] > 8) {
322 memcpy(uni_buffer.ptr[0], data, len);
323 uni_buffer.w16[0] += len;
326 memcpy(&(uni_buffer.w8[uni_buffer_fill]), data, len);
327 exec_spm(uni_buffer.w16[2], uni_buffer.w16[3], uni_buffer.ptr[0], data, len);
335 uchar usbFunctionRead(uchar *data, uchar len){
337 switch(current_command){
338 case CUSTOM_RQ_READ_FLASH:
340 *data++ = pgm_read_byte((uni_buffer.w16[0])++);
349 static void calibrateOscillator(void)
352 uchar trialValue = 0, optimumValue;
353 int x, optimumDev, targetValue = (unsigned)(1499 * (double)F_CPU / 10.5e6 + 0.5);
355 /* do a binary search: */
357 OSCCAL = trialValue + step;
358 x = usbMeasureFrameLength(); // proportional to current real frequency
359 if(x < targetValue) // frequency still too low
363 /* We have a precision of +/- 1 for optimum OSCCAL here */
364 /* now do a neighborhood search for optimum value */
365 optimumValue = trialValue;
366 optimumDev = x; // this is certainly far away from optimum
367 for (OSCCAL = trialValue - 1; OSCCAL <= trialValue + 1; OSCCAL++){
368 x = usbMeasureFrameLength() - targetValue;
371 if (x < optimumDev) {
373 optimumValue = OSCCAL;
376 OSCCAL = optimumValue;
380 void usbEventResetReady(void)
382 cli(); // usbMeasureFrameLength() counts CPU cycles, so disable interrupts.
383 calibrateOscillator();
385 // we never read the value from eeprom so this causes only degradation of eeprom
386 // eeprom_write_byte(0, OSCCAL); // store the calibrated value in EEPROM
389 /* ------------------------------------------------------------------------- */
396 /* Even if you don't use the watchdog, turn it off here. On newer devices,
397 * the status of the watchdog (on/off, period) is PRESERVED OVER RESET!
399 /* RESET status: all port bits are inputs without pull-up.
400 * That's the way we need D+ and D-. Therefore we don't need any
401 * additional hardware initialization.
404 memset(&keyboard_report, 0, sizeof(keyboard_report));
406 init_temperature_sensor();
408 usbDeviceDisconnect(); /* enforce re-enumeration, do this while interrupts are disabled! */
410 while(--i){ /* fake USB disconnect for ~512 ms */
415 LED_PORT_DDR |= _BV(R_BIT) | _BV(G_BIT) | _BV(B_BIT); /* make the LED bit an output */
420 for(;;){ /* main event loop */
426 if(usbInterruptIsReady() && key_state != STATE_WAIT){
430 key_state = STATE_RELEASE_KEY; // release next
432 case STATE_RELEASE_KEY:
435 key_state = STATE_WAIT; // should not happen
438 usbSetInterrupt((void *)&keyboard_report, sizeof(keyboard_report));
446 /* ------------------------------------------------------------------------- */