Subversion Repositories group.electronics

Rev

Rev 28 | Rev 30 | 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
 
29 pfowler 82
  //ADMUX = (1<<ADLAR) | (0<<REFS0);
83
  ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0) | (0<<ADSC);
28 pfowler 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) {
29 pfowler 175
	//ADMUX |= (1<<pin);
176
	ADMUX = (1<<ADLAR) | (0<<REFS0) | (pin & 0x0f);
28 pfowler 177
	ADCSRA |= (1<<ADSC);		// Start converting
178
 
179
	while (((ADCSRA >> ADSC) & 1)) {}	//Wait until conversion finished
180
	uint8_t result = ADCH;
181
	ADCSRA |= (0<<ADSC);		// Stop converting
182
 
183
	// result = result * 5/255;		// wtf?
184
	return result;
185
}
186
 
27 pfowler 187
int getKey() {
28 pfowler 188
	uint8_t row = 0;
27 pfowler 189
	int key = -1;
24 pfowler 190
	uint8_t n = 0;
191
 
27 pfowler 192
	for (row=0; row<=3; row++) {
193
		cbi(PORTB, row);
28 pfowler 194
		if (rbi(PIND, 4) == 0) key = n; n++;
195
		if (rbi(PIND, 5) == 0) key = n; n++;
196
		if (rbi(PIND, 6) == 0) key = n; n++;
197
		if (rbi(PIND, 7) == 0) key = n; n++;
27 pfowler 198
		sbi(PORTB, row);
24 pfowler 199
	}
200
	return key;
201
}
202
 
28 pfowler 203
/*
204
 *
205
 * Process the Pin Change Interrupt.
206
 * pcint provides what bank caused the interrupt
207
 *
208
 */
209
void doInt(uint8_t pcint) {
210
 
211
	// Select what rotary we are dealing with
212
	//   based on the pc interrupt that fired.
213
	uint8_t rotnum = 0;
214
	if (pcint == 1) 
215
		rotnum = 1;
216
 
23 pfowler 217
	// If rot_stat is not 0, we havn't sent
218
	//  our last results yet. Skip this click.
28 pfowler 219
	if (rot_stat[rotnum] != 0) {
220
		pcIntMask[pcint] = 0;
22 pfowler 221
		return;
222
	}
23 pfowler 223
	// Check which pin caused the interrupt. If they both
224
	//  equal 0, the pin that interrupted is the direction
28 pfowler 225
  	if (rbi(pcIntCurr[pcint], PCINT17) == 0 
226
		&& rbi(pcIntCurr[pcint], PCINT17) == 0 
227
		&& rbi(pcIntMask[pcint], PCINT16) ) {
228
			rot_stat[rotnum] = 1;
229
  	} else if (rbi(pcIntCurr[pcint], PCINT16) == 0 
230
		&& rbi(pcIntCurr[pcint], PCINT17) == 0 
231
		&& rbi(pcIntMask[pcint], PCINT17) ) {
232
			rot_stat[rotnum] = 2;
23 pfowler 233
  	}
234
 
235
	// Clear the mask so we know we've delth with it
28 pfowler 236
	pcIntMask[pcint] = 0;
21 pfowler 237
}
238
 
23 pfowler 239
/* Not used for the moment
21 pfowler 240
ISR(TIMER0_OVF_vect) {
241
	timer0_ovf++;
242
}
23 pfowler 243
*/
21 pfowler 244
 
28 pfowler 245
ISR(PCINT1_vect)
246
{
247
        // Save the state and work out which pin caused
248
        //  the interrupt to occur
249
        pcIntCurr[1] = PIND;
250
        pcIntMask[1] = pcIntCurr[1] ^ pcIntLast[1];
251
        pcIntLast[1] = pcIntCurr[1];
252
        doInt(1);
253
}
254
 
24 pfowler 255
ISR(PCINT2_vect)
21 pfowler 256
{
23 pfowler 257
	// Save the state and work out which pin caused
258
	//  the interrupt to occur
28 pfowler 259
	pcIntCurr[2] = PIND;
260
	pcIntMask[2] = pcIntCurr[2] ^ pcIntLast[2];
261
	pcIntLast[2] = pcIntCurr[2];
262
	doInt(2);
21 pfowler 263
}
264