2 * Project: labortage-2013-badge
3 * Author: bg (bg@das-labor.org)
4 * Creation Date: 2013-10-16
6 * Copyright: (c) 2008 by OBJECTIVE DEVELOPMENT Software GmbH, (c) Daniel Otte
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.
20 #define SIMPLE_COUNTER 1
29 #include <avr/eeprom.h>
30 #include <avr/interrupt.h> /* for sei() */
31 #include <util/delay.h> /* for _delay_ms() */
33 #include <avr/pgmspace.h> /* required by usbdrv.h */
35 #include "requests.h" /* The custom request numbers we use */
40 #include "usb_keyboard_codes.h"
42 /* ------------------------------------------------------------------------- */
43 /* ----------------------------- USB interface ----------------------------- */
44 /* ------------------------------------------------------------------------- */
47 #define STATE_SEND_KEY 1
48 #define STATE_RELEASE_KEY 2
51 PROGMEM const char usbHidReportDescriptor[USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH] = {
52 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
53 0x09, 0x06, /* USAGE (Keyboard) */
54 0xa1, 0x01, /* COLLECTION (Application) */
55 0x75, 0x01, /* REPORT_SIZE (1) */
56 0x95, 0x08, /* REPORT_COUNT (8) */
57 0x05, 0x07, /* USAGE_PAGE (Keyboard)(Key Codes) */
58 0x19, 0xe0, /* USAGE_MINIMUM (Keyboard LeftControl)(224) */
59 0x29, 0xe7, /* USAGE_MAXIMUM (Keyboard Right GUI)(231) */
60 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
61 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
62 0x81, 0x02, /* INPUT (Data,Var,Abs) ; Modifier byte */
63 0x95, 0x01, /* REPORT_COUNT (1) */
64 0x75, 0x08, /* REPORT_SIZE (8) */
65 0x81, 0x03, /* INPUT (Cnst,Var,Abs) ; Reserved byte */
66 0x95, 0x05, /* REPORT_COUNT (5) */
67 0x75, 0x01, /* REPORT_SIZE (1) */
68 0x05, 0x08, /* USAGE_PAGE (LEDs) */
69 0x19, 0x01, /* USAGE_MINIMUM (Num Lock) */
70 0x29, 0x05, /* USAGE_MAXIMUM (Kana) */
71 0x91, 0x02, /* OUTPUT (Data,Var,Abs) ; LED report */
72 0x95, 0x01, /* REPORT_COUNT (1) */
73 0x75, 0x03, /* REPORT_SIZE (3) */
74 0x91, 0x03, /* OUTPUT (Cnst,Var,Abs) ; LED report padding */
75 0x95, 0x06, /* REPORT_COUNT (6) */
76 0x75, 0x08, /* REPORT_SIZE (8) */
77 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
78 0x25, 0x65, /* LOGICAL_MAXIMUM (101) */
79 0x05, 0x07, /* USAGE_PAGE (Keyboard)(Key Codes) */
80 0x19, 0x00, /* USAGE_MINIMUM (Reserved (no event indicated))(0) */
81 0x29, 0x65, /* USAGE_MAXIMUM (Keyboard Application)(101) */
82 0x81, 0x00, /* INPUT (Data,Ary,Abs) */
83 0xc0 /* END_COLLECTION */
86 static uint16_t secret_length_ee EEMEM = 0;
87 static uint8_t secret_ee[32] EEMEM;
88 static uint8_t reset_counter_ee EEMEM = 0;
89 static uint8_t digits_ee EEMEM = 8;
92 static uint32_t counter_ee EEMEM = 0;
95 static uint8_t dbg_buffer[8];
96 static uint8_t secret[32];
97 static uint16_t secret_length_b;
98 static char token[10];
100 #define UNI_BUFFER_SIZE 16
102 static union __attribute__((packed)) {
103 uint8_t w8[UNI_BUFFER_SIZE];
104 uint16_t w16[UNI_BUFFER_SIZE/2];
105 uint32_t w32[UNI_BUFFER_SIZE/4];
106 void* ptr[UNI_BUFFER_SIZE/sizeof(void*)];
109 static uint8_t current_command;
111 typedef struct __attribute__((packed)) {
117 static keyboard_report_t keyboard_report; /* report sent to the host */
118 static uchar idleRate; /* in 4 ms units */
119 static uchar key_state = STATE_WAIT;
120 volatile static uchar LED_state = 0xff;
121 /* ------------------------------------------------------------------------- */
124 void memory_clean(void) {
125 memset(secret, 0, 32);
130 uint8_t secret_set(void){
138 const uint8_t length_B = (secret_length_b + 7) / 8;
141 eeprom_write_block(secret, secret_ee, length_B);
144 eeprom_read_block(read_back.w8, secret_ee, length_B);
145 r = memcmp(secret, read_back.w8, length_B);
147 memset(read_back.w8, 0, 32);
153 eeprom_write_word(&secret_length_ee, secret_length_b);
156 r = eeprom_read_word(&secret_length_ee) == secret_length_b;
170 void counter_inc(void){
174 t = eeprom_read_dword(&counter_ee);
176 eeprom_write_dword(&counter_ee, t + 1);
183 void counter_reset(void) {
184 uint8_t reset_counter;
186 reset_counter = eeprom_read_byte(&reset_counter_ee);
189 eeprom_write_dword(&counter_ee, 0);
194 eeprom_write_byte(&reset_counter_ee, reset_counter + 1);
198 void counter_init(void) {
201 if (eeprom_read_byte(&reset_counter_ee) == 0) {
209 void token_generate(void) {
212 eeprom_read_block(secret, secret_ee, 32);
215 hotp(token, secret, eeprom_read_word(&secret_length_ee), eeprom_read_dword(&counter_ee), eeprom_read_byte(&digits_ee));
217 hotp(token, secret, eeprom_read_word(&secret_length_ee), percnt_get(0), eeprom_read_byte(&digits_ee));
224 void buildReport(uchar send_key) {
225 keyboard_report.modifier = 0;
229 keyboard_report.keycode[0] = KEY_1 + (send_key-'1');
232 keyboard_report.keycode[0] = KEY_0;
235 keyboard_report.keycode[0] = 0;
240 int8_t button_get_debounced(volatile uint8_t debounce_count) {
242 v = PINB & _BV(BUTTON_PIN);
243 while (debounce_count-- && v == (PINB & _BV(BUTTON_PIN))) {
246 if (debounce_count) {
252 usbMsgLen_t usbFunctionSetup(uchar data[8])
254 usbRequest_t *rq = (usbRequest_t *)data;
255 if ((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS) { /* class request type */
256 switch(rq->bRequest) {
257 case USBRQ_HID_GET_REPORT: /* send "no keys pressed" if asked here */
258 /* wValue: ReportType (highbyte), ReportID (lowbyte) */
259 usbMsgPtr = (void *)&keyboard_report; /* we only have this one */
260 keyboard_report.modifier = 0;
261 keyboard_report.keycode[0] = 0;
262 return sizeof(keyboard_report);
263 case USBRQ_HID_SET_REPORT: /* if wLength == 1, should be LED state */
264 if (rq->wLength.word == 1) {
265 current_command = LED_WRITE;
269 case USBRQ_HID_GET_IDLE: /* send idle rate to PC as required by spec */
270 usbMsgPtr = &idleRate;
272 case USBRQ_HID_SET_IDLE: /* save idle rate as required by spec */
273 idleRate = rq->wValue.bytes[1];
277 if ((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_VENDOR) {
278 current_command = rq->bRequest;
279 usbMsgPtr = uni_buffer.w8;
282 case CUSTOM_RQ_SET_SECRET:
283 secret_length_b = rq->wValue.word;
284 if (secret_length_b > 256) {
285 secret_length_b = 256;
287 uni_buffer.w8[0] = 0;
289 case CUSTOM_RQ_INC_COUNTER:
292 case CUSTOM_RQ_GET_COUNTER:
295 uni_buffer.w32[0] = eeprom_read_dword(&counter_ee);
297 uni_buffer.w32[0] = percnt_get(0);
300 case CUSTOM_RQ_RESET_COUNTER:
303 case CUSTOM_RQ_GET_RESET_COUNTER:
305 uni_buffer.w8[0] = eeprom_read_byte(&reset_counter_ee);
307 case CUSTOM_RQ_SET_DIGITS:
308 if (rq->wValue.bytes[0] < 6) {
309 rq->wValue.bytes[0] = 6;
311 if (rq->wValue.bytes[0] > 9) {
312 rq->wValue.bytes[0] = 9;
315 eeprom_write_byte(&digits_ee, rq->wValue.bytes[0]);
317 case CUSTOM_RQ_GET_DIGITS:
319 uni_buffer.w8[0] = eeprom_read_byte(&digits_ee);
321 case CUSTOM_RQ_GET_TOKEN:
323 usbMsgPtr = (usbMsgPtr_t)token;
324 return strlen(token);
325 case CUSTOM_RQ_PRESS_BUTTON:
326 key_state = STATE_SEND_KEY;
328 case CUSTOM_RQ_CLR_DBG:
329 memset(dbg_buffer, 0, sizeof(dbg_buffer));
331 case CUSTOM_RQ_SET_DBG:
333 case CUSTOM_RQ_GET_DBG:{
335 if(len > rq->wLength.word){
336 len = rq->wLength.word;
338 usbMsgPtr = dbg_buffer;
341 case CUSTOM_RQ_RESET:
342 soft_reset((uint8_t)(rq->wValue.word));
344 case CUSTOM_RQ_READ_BUTTON:
345 uni_buffer.w8[0] = button_get_debounced(25);
350 return 0; /* default for not implemented requests: return no data back to host */
354 uchar usbFunctionWrite(uchar *data, uchar len)
356 switch(current_command){
359 if (data[0] != LED_state)
361 return 1; /* Data read, not expecting more */
362 case CUSTOM_RQ_SET_SECRET:
364 if (uni_buffer.w8[0] < (secret_length_b + 7) / 8) {
365 memcpy(&secret[uni_buffer.w8[0]], data, len);
366 uni_buffer.w8[0] += len;
368 if (uni_buffer.w8[0] >= (secret_length_b + 7) / 8) {
374 case CUSTOM_RQ_SET_DBG:
375 if(len > sizeof(dbg_buffer)){
376 len = sizeof(dbg_buffer);
378 memcpy(dbg_buffer, data, len);
385 uchar usbFunctionRead(uchar *data, uchar len){
389 static void calibrateOscillator(void)
392 uchar trialValue = 0, optimumValue;
393 int x, optimumDev, targetValue = (unsigned)(1499 * (double)F_CPU / 10.5e6 + 0.5);
395 /* do a binary search: */
397 OSCCAL = trialValue + step;
398 x = usbMeasureFrameLength(); /* proportional to current real frequency */
399 if(x < targetValue) /* frequency still too low */
403 /* We have a precision of +/- 1 for optimum OSCCAL here */
404 /* now do a neighborhood search for optimum value */
405 optimumValue = trialValue;
406 optimumDev = x; /* this is certainly far away from optimum */
407 for (OSCCAL = trialValue - 1; OSCCAL <= trialValue + 1; OSCCAL++){
408 x = usbMeasureFrameLength() - targetValue;
411 if (x < optimumDev) {
413 optimumValue = OSCCAL;
416 OSCCAL = optimumValue;
420 void usbEventResetReady(void)
422 cli(); /* usbMeasureFrameLength() counts CPU cycles, so disable interrupts. */
423 calibrateOscillator();
427 /* ------------------------------------------------------------------------- */
432 int8_t i = 0, last_stable_button_state = 0;
435 /* Even if you don't use the watchdog, turn it off here. On newer devices,
436 * the status of the watchdog (on/off, period) is PRESERVED OVER RESET!
438 /* RESET status: all port bits are inputs without pull-up.
439 * That's the way we need D+ and D-. Therefore we don't need any
440 * additional hardware initialization.
443 DDRB &= ~_BV(BUTTON_PIN); /* make button pin input */
444 PORTB |= _BV(BUTTON_PIN); /* turn on pull-up resistor */
447 usbDeviceDisconnect(); /* enforce re-enumeration, do this while interrupts are disabled! */
448 while(--i){ /* fake USB disconnect for ~512 ms */
456 for(;;){ /* main event loop */
460 i = button_get_debounced(25);
462 if (last_stable_button_state == 0 && i == 1) {
463 key_state = STATE_SEND_KEY;
465 last_stable_button_state = i;
468 if(usbInterruptIsReady() && key_state != STATE_WAIT){
471 buildReport(token[idx]);
472 key_state = STATE_RELEASE_KEY; /* release next */
474 case STATE_RELEASE_KEY:
477 if (token[idx] == '\0') {
479 key_state = STATE_WAIT;
481 key_state = STATE_SEND_KEY;
485 key_state = STATE_WAIT; /* should not happen */
488 usbSetInterrupt((void *)&keyboard_report, sizeof(keyboard_report));
496 /* ------------------------------------------------------------------------- */