Subversion Repositories group.electronics

Rev

Rev 30 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
21 pfowler 1
#include <avr/io.h>
2
#include <avr/pgmspace.h>
3
#include <avr/interrupt.h>
4
 
5
#define F_CPU 12000000
6
#include <util/delay.h>
7
#include <avr/wdt.h>
8
#include <usbdrv.h>
9
 
10
#include <stdlib.h>
11
#include <string.h>
12
 
13
#include "config.h"
14
#include "hiddesc.h"
15
 
32 pfowler 16
#define ROTS_ATTACHED	2
17
#define STAT		0
18
#define SENT		1
21 pfowler 19
 
32 pfowler 20
 
28 pfowler 21
void doInt(uint8_t pcint);
30 pfowler 22
uint8_t getKey(void);
28 pfowler 23
uint8_t analogRead(uint8_t pin);
21 pfowler 24
 
28 pfowler 25
volatile uint8_t pcIntCurr[3] = {0,0,0};
26
volatile uint8_t pcIntLast[3] = {0,0,0};
27
volatile uint8_t pcIntMask[3] = {0,0,0};
21 pfowler 28
 
32 pfowler 29
//volatile uint8_t rot_stat[2] = {0,0};
30
//volatile uint8_t rot_sent[2] = {0,0};
31
// rotdata = [rot#][(stat|sent)]
32
volatile uint8_t rotdata[2][2] = { {0,0}, {0,0} };
21 pfowler 33
 
34
struct{
35
  union {
27 pfowler 36
    uint8_t data1[2];	// Rotaries
21 pfowler 37
    struct {
26 pfowler 38
	uint8_t rx:8;
39
	uint8_t ry:8;
27 pfowler 40
    };
41
  };
42
 
43
 
44
  union {
45
    uint16_t data2;
46
    struct {
26 pfowler 47
	uint8_t b00:1;
48
	uint8_t b01:1;
49
	uint8_t b02:1;
50
	uint8_t b03:1;
51
	uint8_t b04:1;
52
	uint8_t b05:1;
53
	uint8_t b06:1;
54
	uint8_t b07:1;
55
	uint8_t b08:1;
56
	uint8_t b09:1;
57
	uint8_t b10:1;
58
	uint8_t b11:1;
32 pfowler 59
	uint8_t rot2a:1;
60
	uint8_t rot2b:1;
27 pfowler 61
	uint8_t rot1a:1;
62
	uint8_t rot1b:1;
21 pfowler 63
    };
64
  };
65
} report;
66
 
67
usbMsgLen_t usbFunctionSetup(uchar data[8]) {
68
  usbRequest_t *rq = (void *)data;
69
 
70
    if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS) {
71
        if(rq->bRequest == USBRQ_HID_GET_REPORT) {  
72
            return sizeof(report);
73
        } else if(rq->bRequest == USBRQ_HID_GET_IDLE) {
74
            return 1;
75
        } 
76
    }
77
 
78
  return 0;
79
}
80
 
81
void hadUsbReset(void) {
82
}
83
 
84
int main(void) {
85
 
86
  ACSR |= (1<<ACD); // Disable analog comparator
87
 
30 pfowler 88
  ADMUX = (1<<ADLAR) | (0<<REFS0) | (1<<REFS1);
89
  ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0) ;
28 pfowler 90
 
23 pfowler 91
  /*
92
  DDR : 1 = Output, 0 = Input
93
  PORT: 1 = Pullup for Input, otherwise set output
94
  PIN : Read input pin
95
  */
96
 
97
  /*
30 pfowler 98
	PB0	- Output 		- Keypad 1
99
	PB1	- Output 		- Keypad 2
100
	PB2	- Output 		- Keypad 3
101
	PB3	- Output 		- Keypad 4
24 pfowler 102
	PB4	- Input, Pullup		- Function select
23 pfowler 103
  */
27 pfowler 104
  DDRB		= 0B00001111;
105
  PORTB 	= 0B00011111;
21 pfowler 106
 
23 pfowler 107
  /*
24 pfowler 108
	PD0	- Input, Pullup, PCINT16	- Rotary 1a
109
	PD1	- Input, Pullup, PCINT17	- Rotary 1b
110
 
111
 
30 pfowler 112
	PD4	- Input, Pullup		- Keypad 5
113
	PD5	- Input, Pullup		- Keypad 6
114
	PD6	- Input, Pullup		- Keypad 7
115
	PD7	- Input, Pullup		- Keypad 8
23 pfowler 116
  */
27 pfowler 117
  DDRD		= 0B00000000;
118
  PORTD		= 0B11110011;
21 pfowler 119
 
24 pfowler 120
  PCMSK2 |= (( 1 << PCINT16 ) | ( 1 << PCINT17 )); //enable encoder pins interrupt sources
121
  PCICR |= ( 1 << PCIE2 ); //enable pin change interupts
21 pfowler 122
 
23 pfowler 123
  // Timers not used for the moment
124
  // Setup timer0 - Enable overflow, 8 times prescaler
125
  //TIMSK0 = (1<<TOIE0);			// Eable timer overflow for Timer0
126
  //TCNT0 = 0x00;				// Set Timer0 to 0
127
  //TCCR0B = (1<< CS01) ;			// /8 prescaler
21 pfowler 128
 
129
  usbDeviceDisconnect();  /* enforce re-enumeration, do this while interrupts are disabled! */
130
  _delay_ms(500);
131
  usbDeviceConnect();
132
 
133
  wdt_enable(WDTO_1S);
134
  usbInit();
135
  sei();
136
 
137
 
138
  for(;;) {
139
    wdt_reset();
140
    usbPoll();
141
 
142
    if(usbInterruptIsReady()){
30 pfowler 143
	report.data1[0] = (-128 + analogRead(0));
144
	report.data1[1] = (-128 + analogRead(1));
27 pfowler 145
	report.data2 = 0x0000;
21 pfowler 146
 
28 pfowler 147
 
30 pfowler 148
	uint8_t key = getKey();
149
	if (key > 0)
150
		report.data2 |= (1 << (key -1));
21 pfowler 151
 
23 pfowler 152
	// Now work out what rotary to send, if any
153
	// Also record if we sent a positive response, 
154
	//  so we can send a '0' next time (if selected on PD4)
32 pfowler 155
	// rotdata = [rot#][(stat|sent)]
156
	uint8_t rot = 0;
157
	for (rot=0; rot<=(ROTS_ATTACHED - 1); rot++) {
158
	        if (rotdata[rot][STAT] == 0x01 && rotdata[rot][SENT] == 0) {
159
                	rotdata[rot][SENT] = 1;
160
			switch (rot) {
161
				case(0):	report.rot1a = 1; break;
162
				case(1):	report.rot2a = 1; break;
163
			}
164
	        } else if (rotdata[rot][STAT] == 0x02 && rotdata[rot][SENT] == 0) {
165
                	rotdata[rot][SENT] = 1;
166
                        switch (rot) {
167
                                case(0):      report.rot1b = 1; break;
168
                                case(1):      report.rot2b = 1; break;
169
                        }
170
	        } else {
171
        	        rotdata[rot][SENT] = 0;
172
	        }
173
		rotdata[rot][STAT] = 0;
174
 
175
	        if (rbi(PINB, PB4))
176
        	        rotdata[rot][SENT] = 0;
177
	}
178
 
179
/*	
28 pfowler 180
	if (rot_stat[0] == 0x01 && rot_sent[0] == 0) {
24 pfowler 181
		report.rot1a = 1;
28 pfowler 182
		rot_sent[0] = 1;
183
	} else if (rot_stat[0] == 0x02 && rot_sent[0] == 0) {
24 pfowler 184
		report.rot1b = 1;
28 pfowler 185
		rot_sent[0] = 1;
22 pfowler 186
	} else {
28 pfowler 187
		rot_sent[0] = 0;
27 pfowler 188
	}
22 pfowler 189
 
23 pfowler 190
	// Reset our stat so ready for next turn
28 pfowler 191
	rot_stat[0] = 0;
22 pfowler 192
 
23 pfowler 193
	// If our function select is set, dont bother
194
	//  sending a 'o' between consequtive 1's.
24 pfowler 195
	if (rbi(PINB, PB4))
28 pfowler 196
		rot_sent[0] = 0;
32 pfowler 197
*/
23 pfowler 198
 
21 pfowler 199
      /* called after every poll of the interrupt endpoint */
200
      usbSetInterrupt(&report, sizeof(report));
201
    }
202
  }
203
}
204
 
28 pfowler 205
uint8_t analogRead(uint8_t pin) {
30 pfowler 206
	ADMUX = (1<<ADLAR) | (1<<REFS0) | (0<<REFS1) | (pin & 0x0f);
28 pfowler 207
	ADCSRA |= (1<<ADSC);		// Start converting
208
 
209
	while (((ADCSRA >> ADSC) & 1)) {}	//Wait until conversion finished
210
	uint8_t result = ADCH;
30 pfowler 211
	//ADCSRA |= (0<<ADSC);		// Stop converting
28 pfowler 212
 
213
	return result;
214
}
215
 
30 pfowler 216
uint8_t getKey() {
217
	uint8_t col, row = 0;
218
	uint8_t key = 0;
219
	uint8_t n = 1;
24 pfowler 220
 
27 pfowler 221
	for (row=0; row<=3; row++) {
222
		cbi(PORTB, row);
30 pfowler 223
		_delay_us(10);				// Wait for the port to change
224
 
225
		for (col=5; col<=7; col++) {
226
			if (rbi(PIND, col) == 0)
227
				key = n;
228
			n++;
229
		}	
230
 
27 pfowler 231
		sbi(PORTB, row);
24 pfowler 232
	}
233
	return key;
234
}
235
 
28 pfowler 236
/*
237
 *
238
 * Process the Pin Change Interrupt.
239
 * pcint provides what bank caused the interrupt
240
 *
241
 */
242
void doInt(uint8_t pcint) {
243
 
244
	// Select what rotary we are dealing with
245
	//   based on the pc interrupt that fired.
32 pfowler 246
	uint8_t rot = 0;
28 pfowler 247
	if (pcint == 1) 
32 pfowler 248
		rot = 1;
28 pfowler 249
 
32 pfowler 250
	// If rot stat is not 0, we havn't sent
23 pfowler 251
	//  our last results yet. Skip this click.
32 pfowler 252
	if (rotdata[rot][STAT] != 0) {
28 pfowler 253
		pcIntMask[pcint] = 0;
22 pfowler 254
		return;
255
	}
23 pfowler 256
	// Check which pin caused the interrupt. If they both
257
	//  equal 0, the pin that interrupted is the direction
28 pfowler 258
  	if (rbi(pcIntCurr[pcint], PCINT17) == 0 
259
		&& rbi(pcIntCurr[pcint], PCINT17) == 0 
260
		&& rbi(pcIntMask[pcint], PCINT16) ) {
32 pfowler 261
			rotdata[rot][STAT] = 1;
28 pfowler 262
  	} else if (rbi(pcIntCurr[pcint], PCINT16) == 0 
263
		&& rbi(pcIntCurr[pcint], PCINT17) == 0 
264
		&& rbi(pcIntMask[pcint], PCINT17) ) {
32 pfowler 265
			rotdata[rot][STAT] = 2;
23 pfowler 266
  	}
267
 
268
	// Clear the mask so we know we've delth with it
28 pfowler 269
	pcIntMask[pcint] = 0;
21 pfowler 270
}
271
 
23 pfowler 272
/* Not used for the moment
21 pfowler 273
ISR(TIMER0_OVF_vect) {
274
	timer0_ovf++;
275
}
23 pfowler 276
*/
21 pfowler 277
 
28 pfowler 278
ISR(PCINT1_vect)
279
{
280
        // Save the state and work out which pin caused
281
        //  the interrupt to occur
282
        pcIntCurr[1] = PIND;
283
        pcIntMask[1] = pcIntCurr[1] ^ pcIntLast[1];
284
        pcIntLast[1] = pcIntCurr[1];
285
        doInt(1);
286
}
287
 
24 pfowler 288
ISR(PCINT2_vect)
21 pfowler 289
{
23 pfowler 290
	// Save the state and work out which pin caused
291
	//  the interrupt to occur
28 pfowler 292
	pcIntCurr[2] = PIND;
293
	pcIntMask[2] = pcIntCurr[2] ^ pcIntLast[2];
294
	pcIntLast[2] = pcIntCurr[2];
295
	doInt(2);
21 pfowler 296
}
297