Subversion Repositories group.electronics

Rev

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);
30 pfowler 18
uint8_t 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
 
30 pfowler 82
  ADMUX = (1<<ADLAR) | (0<<REFS0) | (1<<REFS1);
83
  ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0) ;
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
  /*
30 pfowler 92
	PB0	- Output 		- Keypad 1
93
	PB1	- Output 		- Keypad 2
94
	PB2	- Output 		- Keypad 3
95
	PB3	- Output 		- Keypad 4
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
 
30 pfowler 106
	PD4	- Input, Pullup		- Keypad 5
107
	PD5	- Input, Pullup		- Keypad 6
108
	PD6	- Input, Pullup		- Keypad 7
109
	PD7	- Input, Pullup		- Keypad 8
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()){
30 pfowler 137
	report.data1[0] = (-128 + analogRead(0));
138
	report.data1[1] = (-128 + analogRead(1));
27 pfowler 139
	report.data2 = 0x0000;
21 pfowler 140
 
28 pfowler 141
 
30 pfowler 142
	uint8_t key = getKey();
143
	if (key > 0)
144
		report.data2 |= (1 << (key -1));
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
 
30 pfowler 168
	if (rbi(PINB, PB5))
169
		rot_sent[1] = 0;
170
 
21 pfowler 171
      /* called after every poll of the interrupt endpoint */
172
      usbSetInterrupt(&report, sizeof(report));
173
    }
174
  }
175
}
176
 
28 pfowler 177
uint8_t analogRead(uint8_t pin) {
30 pfowler 178
	//ADMUX |= (1<<ADLAR) | (0<<REFS0) |(1<<0);
179
	ADMUX = (1<<ADLAR) | (1<<REFS0) | (0<<REFS1) | (pin & 0x0f);
28 pfowler 180
	ADCSRA |= (1<<ADSC);		// Start converting
181
 
182
	while (((ADCSRA >> ADSC) & 1)) {}	//Wait until conversion finished
183
	uint8_t result = ADCH;
30 pfowler 184
	//ADCSRA |= (0<<ADSC);		// Stop converting
28 pfowler 185
 
186
	// result = result * 5/255;		// wtf?
187
	return result;
188
}
189
 
30 pfowler 190
uint8_t getKey() {
191
	uint8_t col, row = 0;
192
	uint8_t key = 0;
193
	uint8_t n = 1;
24 pfowler 194
 
27 pfowler 195
	for (row=0; row<=3; row++) {
196
		cbi(PORTB, row);
30 pfowler 197
		_delay_us(10);				// Wait for the port to change
198
 
199
		for (col=5; col<=7; col++) {
200
			if (rbi(PIND, col) == 0)
201
				key = n;
202
			n++;
203
		}	
204
 
27 pfowler 205
		sbi(PORTB, row);
24 pfowler 206
	}
207
	return key;
208
}
209
 
28 pfowler 210
/*
211
 *
212
 * Process the Pin Change Interrupt.
213
 * pcint provides what bank caused the interrupt
214
 *
215
 */
216
void doInt(uint8_t pcint) {
217
 
218
	// Select what rotary we are dealing with
219
	//   based on the pc interrupt that fired.
220
	uint8_t rotnum = 0;
221
	if (pcint == 1) 
222
		rotnum = 1;
223
 
23 pfowler 224
	// If rot_stat is not 0, we havn't sent
225
	//  our last results yet. Skip this click.
28 pfowler 226
	if (rot_stat[rotnum] != 0) {
227
		pcIntMask[pcint] = 0;
22 pfowler 228
		return;
229
	}
23 pfowler 230
	// Check which pin caused the interrupt. If they both
231
	//  equal 0, the pin that interrupted is the direction
28 pfowler 232
  	if (rbi(pcIntCurr[pcint], PCINT17) == 0 
233
		&& rbi(pcIntCurr[pcint], PCINT17) == 0 
234
		&& rbi(pcIntMask[pcint], PCINT16) ) {
235
			rot_stat[rotnum] = 1;
236
  	} else if (rbi(pcIntCurr[pcint], PCINT16) == 0 
237
		&& rbi(pcIntCurr[pcint], PCINT17) == 0 
238
		&& rbi(pcIntMask[pcint], PCINT17) ) {
239
			rot_stat[rotnum] = 2;
23 pfowler 240
  	}
241
 
242
	// Clear the mask so we know we've delth with it
28 pfowler 243
	pcIntMask[pcint] = 0;
21 pfowler 244
}
245
 
23 pfowler 246
/* Not used for the moment
21 pfowler 247
ISR(TIMER0_OVF_vect) {
248
	timer0_ovf++;
249
}
23 pfowler 250
*/
21 pfowler 251
 
28 pfowler 252
ISR(PCINT1_vect)
253
{
254
        // Save the state and work out which pin caused
255
        //  the interrupt to occur
256
        pcIntCurr[1] = PIND;
257
        pcIntMask[1] = pcIntCurr[1] ^ pcIntLast[1];
258
        pcIntLast[1] = pcIntCurr[1];
259
        doInt(1);
260
}
261
 
24 pfowler 262
ISR(PCINT2_vect)
21 pfowler 263
{
23 pfowler 264
	// Save the state and work out which pin caused
265
	//  the interrupt to occur
28 pfowler 266
	pcIntCurr[2] = PIND;
267
	pcIntMask[2] = pcIntCurr[2] ^ pcIntLast[2];
268
	pcIntLast[2] = pcIntCurr[2];
269
	doInt(2);
21 pfowler 270
}
271