]> git.cryptolib.org Git - labortage2013badge.git/blob - firmware/main.c
initial - non working - stuff
[labortage2013badge.git] / firmware / main.c
1 /* Name: main.c
2  * Project: hid-custom-rq example
3  * Author: Christian Starkjohann
4  * Creation Date: 2008-04-07
5  * Tabsize: 4
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 $
9  */
10
11 /*
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:
18 */
19 #define LED_PORT_DDR        DDRB
20 #define LED_PORT_OUTPUT     PORTB
21 #define R_BIT            4
22 #define G_BIT            3
23 #define B_BIT            1
24 #define BUTTON_PIN 4
25
26 #include <stdint.h>
27 #include <string.h>
28
29 #include <avr/io.h>
30 #include <avr/wdt.h>
31 #include <avr/eeprom.h>
32 #include <avr/interrupt.h>  /* for sei() */
33 #include <util/delay.h>     /* for _delay_ms() */
34
35 #include <avr/pgmspace.h>   /* required by usbdrv.h */
36 #include "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"
40
41 void update_pwm(void);
42
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
65 };
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.
74  */
75
76 /* Keyboard usage values, see usb.org's HID-usage-tables document, chapter
77  * 10 Keyboard/Keypad Page for more codes.
78  */
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)
87
88 #define KEY_A       4
89 #define KEY_B       5
90 #define KEY_C       6
91 #define KEY_D       7
92 #define KEY_E       8
93 #define KEY_F       9
94 #define KEY_G       10
95 #define KEY_H       11
96 #define KEY_I       12
97 #define KEY_J       13
98 #define KEY_K       14
99 #define KEY_L       15
100 #define KEY_M       16
101 #define KEY_N       17
102 #define KEY_O       18
103 #define KEY_P       19
104 #define KEY_Q       20
105 #define KEY_R       21
106 #define KEY_S       22
107 #define KEY_T       23
108 #define KEY_U       24
109 #define KEY_V       25
110 #define KEY_W       26
111 #define KEY_X       27
112 #define KEY_Y       28
113 #define KEY_Z       29
114 #define KEY_1       30
115 #define KEY_2       31
116 #define KEY_3       32
117 #define KEY_4       33
118 #define KEY_5       34
119 #define KEY_6       35
120 #define KEY_7       36
121 #define KEY_8       37
122 #define KEY_9       38
123 #define KEY_0       39
124
125 #define KEY_F1      58
126 #define KEY_F2      59
127 #define KEY_F3      60
128 #define KEY_F4      61
129 #define KEY_F5      62
130 #define KEY_F6      63
131 #define KEY_F7      64
132 #define KEY_F8      65
133 #define KEY_F9      66
134 #define KEY_F10     67
135 #define KEY_F11     68
136 #define KEY_F12     69
137
138 union {
139         struct {
140                 uint16_t red;
141                 uint16_t green;
142                 uint16_t blue;
143         } name;
144         uint16_t idx[3];
145 } color;
146
147 #define UNI_BUFFER_SIZE 16
148
149 static union {
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*)];
154 } uni_buffer;
155
156 static uint8_t uni_buffer_fill;
157 static uint8_t current_command;
158 /* ------------------------------------------------------------------------- */
159
160
161 uint8_t read_button(void){
162         uint8_t t,u,v=0;
163         t = DDRB;
164         u = PORTB;
165         DDRB &= ~(1<<BUTTON_PIN);
166         PORTB |= 1<<BUTTON_PIN;
167         PORTB &= ~(1<<BUTTON_PIN);
168         v |= PINB;
169         DDRB |= t&(1<<BUTTON_PIN);
170         PORTB &= ~(t&(1<<BUTTON_PIN));
171         v >>= BUTTON_PIN;
172         v &= 1;
173         v ^= 1;
174         return v;
175 }
176
177 void init_tmpsensor(void){
178         ADMUX = 0x8F;
179         ADCSRA = 0x87;
180 }
181
182 uint16_t read_tmpsensor(void){
183         ADCSRA |= 0x40;
184         while(ADCSRA & 0x40)
185                 ;
186         return ADC;
187 }
188
189 usbMsgLen_t usbFunctionSetup(uchar data[8])
190 {
191         usbRequest_t    *rq = (usbRequest_t *)data;
192
193     if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_VENDOR)
194         {
195                 current_command = rq->bRequest;
196         switch(rq->bRequest)
197                 {
198                 case CUSTOM_RQ_SET_RED:
199                         color.name.red = rq->wValue.bytes[0];
200                         break;  
201                 case CUSTOM_RQ_SET_GREEN:
202                         color.name.green = rq->wValue.bytes[0];
203                         break;  
204                 case CUSTOM_RQ_SET_BLUE:
205                         color.name.blue = rq->wValue.bytes[0];
206                         break;  
207                 case CUSTOM_RQ_SET_RGB:
208                         return USB_NO_MSG;
209                 case CUSTOM_RQ_GET_RGB:{
210                         usbMsgLen_t len=6;
211                         if(len>rq->wLength.word){
212                                 len = rq->wLength.word;
213                         }
214                         usbMsgPtr = (uchar*)color.idx;
215                         return len;
216                 }
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:
222                         uni_buffer_fill = 4;
223                         uni_buffer.w16[0] = rq->wValue.word;
224                         uni_buffer.w16[1] = rq->wLength.word;
225                         return USB_NO_MSG;
226                 case CUSTOM_RQ_READ_FLASH:
227                         uni_buffer.w16[0] = rq->wValue.word;
228                         uni_buffer.w16[1] = rq->wLength.word;
229                         return USB_NO_MSG;
230                 case CUSTOM_RQ_RESET:
231                         soft_reset((uint8_t)(rq->wValue.word));
232                         break;
233                 case CUSTOM_RQ_READ_BUTTON:
234                         uni_buffer.w8[0] = read_button();
235                         usbMsgPtr = uni_buffer.w8;
236                         return 1;
237                 case CUSTOM_RQ_READ_TMPSENS:
238                         uni_buffer.w16[0] = read_tmpsensor();
239                         usbMsgPtr = uni_buffer.w8;
240                         return 2;
241                 }
242     }
243         else
244         {
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.
248          */
249     }
250     return 0;   /* default for not implemented requests: return no data back to host */
251 }
252
253 uchar usbFunctionWrite(uchar *data, uchar len)
254 {
255         switch(current_command){
256         case CUSTOM_RQ_SET_RGB:
257                 if(len!=6){
258                         return 1;
259                 }
260                 memcpy(color.idx, data, 6);
261                 return 1;
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;
269                         if(len<l){
270                                 len = l;
271                         }
272                         memcpy(&(uni_buffer.w8[uni_buffer_fill]), data, len);
273                         uni_buffer_fill += len;
274                         return 0;
275                 }
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;
280                         return 0;
281                 }else{
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);
284                         return 1;
285                 }
286         default:
287                 return 1;
288         }
289         return 0;
290 }
291 uchar usbFunctionRead(uchar *data, uchar len){
292         uchar ret=len;
293         switch(current_command){
294         case CUSTOM_RQ_READ_FLASH:
295                 while(len--){
296                         *data++ = pgm_read_byte((uni_buffer.w16[0])++);
297                 }
298                 return ret;
299         default:
300                 break;
301         }
302         return 0;
303 }
304
305 static void calibrateOscillator(void)
306 {
307 uchar       step = 128;
308 uchar       trialValue = 0, optimumValue;
309 int         x, optimumDev, targetValue = (unsigned)(1499 * (double)F_CPU / 10.5e6 + 0.5);
310  
311     /* do a binary search: */
312     do{
313         OSCCAL = trialValue + step;
314         x = usbMeasureFrameLength();    // proportional to current real frequency
315         if(x < targetValue)             // frequency still too low
316             trialValue += step;
317         step >>= 1;
318     }while(step > 0);
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;
325         if(x < 0)
326             x = -x;
327         if(x < optimumDev){
328             optimumDev = x;
329             optimumValue = OSCCAL;
330         }
331     }
332     OSCCAL = optimumValue;
333 }
334  
335
336 void usbEventResetReady(void)
337 {
338     cli();  // usbMeasureFrameLength() counts CPU cycles, so disable interrupts.
339     calibrateOscillator();
340     sei();
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
343 }
344
345 /* ------------------------------------------------------------------------- */
346
347 int main(void)
348 {
349         uchar   i;
350
351     wdt_enable(WDTO_1S);
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!
354      */
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.
358      */
359
360     init_tmpsensor();
361     usbInit();
362     usbDeviceDisconnect();  /* enforce re-enumeration, do this while interrupts are disabled! */
363     i = 0;
364     while(--i){             /* fake USB disconnect for > 250 ms */
365         wdt_reset();
366         _delay_ms(1);
367     }
368     usbDeviceConnect();
369     LED_PORT_DDR |= _BV(R_BIT) | _BV(G_BIT) | _BV(B_BIT);   /* make the LED bit an output */
370         
371     sei();
372
373     for(;;){                /* main event loop */
374                 update_pwm();
375                 
376         wdt_reset();
377         usbPoll();
378     }
379     return 0;
380 }
381
382 /* ------------------------------------------------------------------------- */