Subversion Repositories group.electronics

Rev

Rev 84 | Rev 86 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 84 Rev 85
Line 8... Line 8...
8
 * This Revision: $Id$
8
 * This Revision: $Id$
9
 */
9
 */
10
 
10
 
11
#include <avr/io.h>
11
#include <avr/io.h>
12
#include <avr/wdt.h>
12
#include <avr/wdt.h>
13
#include <avr/eeprom.h>
-
 
14
#include <avr/interrupt.h>
13
#include <avr/interrupt.h>
15
#include <avr/pgmspace.h>
14
#include <avr/pgmspace.h>
16
#include <util/delay.h>
15
#include <util/delay.h>
17
 
16
 
18
#include "usbdrv.h"
17
#include "usbdrv.h"
Line 25... Line 24...
25
 
24
 
26
/* ------------------------------------------------------------------------- */
25
/* ------------------------------------------------------------------------- */
27
 
26
 
28
uint8_t getKey(void);
27
uint8_t getKey(void);
29
void doButtons(void);
28
void doButtons(void);
-
 
29
inline void setLeds(uint8_t);
30
 
30
 
31
struct {
31
struct {
32
	union {
32
	union {
33
		int16_t axis[2];
33
		int16_t axis[2];
34
		struct {
34
		struct {
Line 88... Line 88...
88
 
88
 
89
    0xc0,                          //   END_COLLECTION
89
    0xc0,                          //   END_COLLECTION
90
    0xc0                           // END_COLLECTION
90
    0xc0                           // END_COLLECTION
91
};
91
};
92
 
92
 
-
 
93
void usbEventResetReady(void) {
-
 
94
 
-
 
95
}
-
 
96
 
93
 
97
 
94
uchar	usbFunctionSetup(uchar data[8])
98
uchar	usbFunctionSetup(uchar data[8])
95
{
99
{
96
    usbRequest_t    *rq = (void *)data;
100
    usbRequest_t    *rq = (void *)data;
97
 
101
 
98
    //usbMsgPtr = reportBuffer;
-
 
99
    if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS){ 
102
    if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS){ 
100
        if(rq->bRequest == USBRQ_HID_GET_REPORT){ 
103
        if(rq->bRequest == USBRQ_HID_GET_REPORT){ 
101
            return sizeof(reportBuffer);
104
            return sizeof(reportBuffer);
102
        }else if(rq->bRequest == USBRQ_HID_GET_IDLE){
105
        }else if(rq->bRequest == USBRQ_HID_GET_IDLE){
103
            usbMsgPtr = &idleRate;
106
            usbMsgPtr = &idleRate;
Line 110... Line 113...
110
    }
113
    }
111
	return 0;
114
	return 0;
112
}
115
}
113
 
116
 
114
 
117
 
115
static void calibrateOscillator(void) {
-
 
116
    uchar step = 128;
-
 
117
    uchar trialValue = 0, optimumValue;
-
 
118
    int x, optimumDev;
-
 
119
    int targetValue = (unsigned)(1499 * (double)F_CPU / 10.5e6 + 0.5);
-
 
120
 
-
 
121
    /* do a binary search: */
-
 
122
    do {
-
 
123
        OSCCAL = trialValue + step;
-
 
124
        x = usbMeasureFrameLength();    /* proportional to current real frequency */
-
 
125
        if(x < targetValue)             /* frequency still too low */
-
 
126
            trialValue += step;
-
 
127
        step >>= 1;
-
 
128
    } while(step > 0);
-
 
129
    /* We have a precision of +/- 1 for optimum OSCCAL here */
-
 
130
    /* now do a neighborhood search for optimum value */
-
 
131
    optimumValue = trialValue;
-
 
132
    optimumDev = x; /* this is certainly far away from optimum */
-
 
133
    for(OSCCAL = trialValue - 1; OSCCAL <= trialValue + 1; OSCCAL++){
-
 
134
        x = usbMeasureFrameLength() - targetValue;
-
 
135
        if(x < 0)
-
 
136
            x = -x;
-
 
137
        if(x < optimumDev){
-
 
138
            optimumDev = x;
-
 
139
            optimumValue = OSCCAL;
-
 
140
        }
-
 
141
    }
-
 
142
    OSCCAL = optimumValue;
-
 
143
}
-
 
144
 
-
 
145
void usbEventResetReady(void) {
-
 
146
    cli();
-
 
147
    calibrateOscillator();
-
 
148
    sei();
-
 
149
    eeprom_write_byte(0, OSCCAL);   /* store the calibrated value in EEPROM */
-
 
150
}
-
 
151
 
-
 
152
 
-
 
153
void usbSendHidReport(uchar * data, uchar len) {
118
void usbSendHidReport(uchar * data, uchar len) {
154
	usbSetInterrupt(data, len);
119
	usbSetInterrupt(data, len);
155
}
120
}
156
 
121
 
157
 
122
 
158
int main(void) {
123
int main(void) {
159
uchar   i;
-
 
160
uchar   calibrationValue;
-
 
161
 
-
 
162
    calibrationValue = eeprom_read_byte(0); /* calibration value from last time */
-
 
163
    if(calibrationValue != 0xff){
-
 
164
        OSCCAL = calibrationValue;
-
 
165
    }
-
 
166
 
124
 
167
  /*
125
  /*
168
  DDR : 1 = Output, 0 = Input
126
  DDR : 1 = Output, 0 = Input
169
  PORT: 1 = Pullup for Input, otherwise set output
127
  PORT: 1 = Pullup for Input, otherwise set output
170
  PIN : Read input pin
128
  PIN : Read input pin
171
  */
129
  */
172
  /*
-
 
173
 
130
 
174
        PA0     - Input, Pullup - ButtonPad 1
-
 
175
        PA1     - Input, Pullup - ButtonPad 0
-
 
176
        PA2     - Output        - Reset
-
 
177
  */
-
 
178
  DDRD          = 0B00000000;
-
 
179
  PORTD         = 0B00000011;
-
 
180
  /*
131
  /*
181
        PB0     - Output                - LED 0
132
        PB0     - Output                - LED 3
182
        PB1     - Output                - LED 1
133
        PB1     - Output                - LED 4
183
        PB2     - Output                - LED 2
134
        PB2     - Output                - LED 5
184
        PB3     - Output                - LED 3
135
        PB3     - Output                - LED 6
185
        PB4     - Output                - LED 4
136
        PB4     - Output                - LED 7
186
        PB5     - Output                - LED 5
137
        PB5     - 
187
        PB6     - Output                - LED 6
138
        PB6     - 
188
        PB7     - Output                - LED 7
139
        PB7     - 
189
  */
140
  */
190
  DDRB          = 0B11111111;
141
  DDRB          = 0B00011111;
191
  PORTB         = 0B00000000;
142
  PORTB         = 0B00000000;
192
  /*
-
 
193
 
143
 
-
 
144
  /*
194
        PD0     - Output        - ButtonPad Gnd1
145
        PC0     - Output		- ButtonPad Gnd0
195
        PD1     - Output        - ButtonPad Gnd2
146
        PC1     - Output		- ButtonPad Gnd1
196
        PD2     - Output        - USB D+
147
        PC2     - Input, Pullup		- ButtonPad 0
197
        PD3     - Output        - USB D-
-
 
198
        PD4     - Input, Pullup - ButtonPad 2
148
        PC3     - Input, Pullup		- ButtonPad 1
199
        PD5     - Input, Pullup - ButtonPad 3
149
        PC4     - Input, Pullup		- ButtonPad 2
200
        PD6     - Input, Pullup - Select Switch
150
        PC5     - Input, Pullup		- ButtonPad 3
201
  */
151
  */
202
  DDRD          = 0B00000011;
152
  DDRD          = 0B00000011;
203
  PORTD         = 0B01110011;
153
  PORTD         = 0B00111111;
204
 
154
 
-
 
155
  /*
-
 
156
        PD0     - 
-
 
157
        PD1     - 
-
 
158
        PD2     - 
-
 
159
        PD3     - 
-
 
160
        PD4     - 
-
 
161
        PD5     - Output		- LED 0
-
 
162
        PD6     - Output		- LED 1
-
 
163
        PD7     - Output		- LED 2
-
 
164
  */
-
 
165
  DDRD          = 0B11100000;
-
 
166
  PORTD         = 0B00000000;
-
 
167
 
205
	PORTB = 255;
168
	setLeds(255);
206
	_delay_ms(20);
169
	_delay_ms(20);
207
	PORTB = currleds;
170
	setLeds(currleds);
208
 
171
 
209
    //odDebugInit();
-
 
210
    usbDeviceDisconnect();
172
    usbDeviceDisconnect();
211
    for(i=0;i<20;i++){  /* 300 ms disconnect */
-
 
212
        _delay_ms(15);
173
        _delay_ms(500);
213
    }
-
 
214
 
174
 
215
    usbDeviceConnect();
175
    usbDeviceConnect();
216
    wdt_enable(WDTO_1S);
176
    wdt_enable(WDTO_1S);
217
 
177
 
218
 
178
 
219
    TIMSK = (1<<TOIE0);                    // Enable timer overflow
179
    TIMSK0 = (1<<TOIE0);                    // Enable timer overflow
220
    TCNT0 = 0x00;                           // Set Timer0 initial value to 0
180
    TCNT0 = 0x00;                           // Set Timer0 initial value to 0
221
    TCCR0B = (1<< CS01) ;                   // /1 prescaler
181
    TCCR0B = (1<< CS00) ;                   // /1 prescaler
222
 
182
 
223
    usbInit();
183
    usbInit();
224
    sei();
184
    sei();
225
 
185
 
226
    reportBuffer.axis0 = 0;
186
    reportBuffer.axis0 = 0;
Line 242... Line 202...
242
	_delay_ms(1);
202
	_delay_ms(1);
243
    }
203
    }
244
    return 0;
204
    return 0;
245
}
205
}
246
 
206
 
-
 
207
inline void setLeds(uint8_t leds) {
-
 
208
        PORTB |= (0x1F & (leds >> 3));
-
 
209
        PORTD |= (0xE0 & (leds << 5));
-
 
210
}
-
 
211
 
247
void doButtons() {
212
void doButtons() {
248
        uint8_t pressed = getKey();
213
        uint8_t pressed = getKey();
249
 
214
 
250
        // Deboucing
215
        // Deboucing
251
        // When i key first goes down, wait 5 ms, check it again to see if its still down
216
        // When i key first goes down, wait 5 ms, check it again to see if its still down
Line 283... Line 248...
283
                                        cbi(currleds,6);
248
                                        cbi(currleds,6);
284
 
249
 
285
                                sbi(currleds, i);
250
                                sbi(currleds, i);
286
                        }
251
                        }
287
                }
252
                }
288
                PORTB = currleds;
253
                setLeds(currleds);
289
                reportBuffer.buttons = debounce.buttons;
254
                reportBuffer.buttons = debounce.buttons;
290
                // Set debounce to wait to button up
255
                // Set debounce to wait to button up
291
                debounce.waitup = 1;
256
                debounce.waitup = 1;
292
        }
257
        }
293
}
258
}
294
 
259
 
295
// Gnd = PD0, PD1
260
// Gnd = PC0, PC1
296
// Btn = PA1, PA0, PD4, PD5
261
// Btn = PC2, PC3, PC4, PC5
297
uint8_t getKey() {
262
uint8_t getKey() {
298
        uint8_t key = 0;
263
        uint8_t key = 0;
299
 
264
 
300
        cbi(PORTD, 0);
265
        cbi(PORTC, 0);
301
        _delay_us(10);        // Wait for the port change
266
        _delay_us(10);        // Wait for the port change
302
        if (rbi(PIND, 5) == 0) key = 1;
267
        if (rbi(PINC, 2) == 0) key = 1;
303
        if (rbi(PIND, 4) == 0) key = 2;
268
        if (rbi(PINC, 3) == 0) key = 2;
304
        if (rbi(PINA, 0) == 0) key = 4;
269
        if (rbi(PINC, 4) == 0) key = 4;
305
        if (rbi(PINA, 1) == 0) key = 8;
270
        if (rbi(PINC, 5) == 0) key = 8;
306
        sbi(PORTD, 0);
271
        sbi(PORTC, 0);
307
 
272
 
308
        cbi(PORTD, 1);
273
        cbi(PORTC, 1);
309
        _delay_us(10);
274
        _delay_us(10);
310
        if (rbi(PIND, 5) == 0) key = 16;
275
        if (rbi(PINC, 2) == 0) key = 16;
311
        if (rbi(PIND, 4) == 0) key = 32;
276
        if (rbi(PINC, 3) == 0) key = 32;
312
        if (rbi(PINA, 0) == 0) key = 64;
277
        if (rbi(PINC, 4) == 0) key = 64;
313
        if (rbi(PINA, 1) == 0) key = 128;
278
        if (rbi(PINC, 5) == 0) key = 128;
314
        sbi(PORTD, 1);
279
        sbi(PORTC, 1);
315
 
280
 
316
        return key;
281
        return key;
317
}
282
}
318
 
283
 
319
 
284
 
320
ISR(TIMER0_OVF_vect) {
285
ISR(TIMER0_OVF_vect) {
321
        tmr0_ovf++;
286
        tmr0_ovf++;
322
 
287
 
323
	//16.5Mhz, 1ms = 50ovf
288
	//16.5Mhz, 1ms = 50ovf
-
 
289
	//12.0Mhz, 1ms = 46ovf
324
 
290
 
325
        if (tmr0_ovf>=50) {
291
        if (tmr0_ovf>=46) {
326
                systime++;
292
                systime++;
327
                tmr0_ovf = 0;
293
                tmr0_ovf = 0;
328
 
294
 
329
		if (debounce.timer != 0)
295
		if (debounce.timer != 0)
330
			debounce.timer--;
296
			debounce.timer--;