Subversion Repositories group.electronics

Rev

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