Subversion Repositories group.electronics

Rev

Rev 22 | Rev 24 | 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
 
17
void doInt(void);
18
 
19
volatile uint8_t pcIntCurr = 0;
20
volatile uint8_t pcIntLast = 0;
21
volatile uint8_t pcIntMask = 0;
22
 
22 pfowler 23
volatile uint8_t rot_stat = 0;
23 pfowler 24
volatile uint8_t rot_sent = 0;
21 pfowler 25
 
26
struct{
27
  union {
28
    uint8_t data;
29
    struct {
30
      uint8_t X:2;
31
      uint8_t Y:2;
32
      uint8_t B:1;
33
      uint8_t A:1;
34
      uint8_t rot1:1;
35
      uint8_t rot2:1;
36
    };
37
  };
38
} report;
39
 
40
usbMsgLen_t usbFunctionSetup(uchar data[8]) {
41
  usbRequest_t *rq = (void *)data;
42
 
43
    if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS) {
44
        if(rq->bRequest == USBRQ_HID_GET_REPORT) {  
45
            return sizeof(report);
46
        } else if(rq->bRequest == USBRQ_HID_GET_IDLE) {
47
            return 1;
48
        } 
49
    }
50
 
51
  return 0;
52
}
53
 
54
void hadUsbReset(void) {
55
}
56
 
57
int main(void) {
58
 
59
  ACSR |= (1<<ACD); // Disable analog comparator
60
 
23 pfowler 61
  /*
62
  DDR : 1 = Output, 0 = Input
63
  PORT: 1 = Pullup for Input, otherwise set output
64
  PIN : Read input pin
65
  */
66
 
67
  /*
68
	PB0	- Output		- Status LED
69
	PB1	- Input, Pullup, PCINT1	- Rotary 1
70
	PB2	- Input, Pullup, PCINT2	- Rotary 2
71
  */
21 pfowler 72
  DDRB		= 0B00000001;
73
  PORTB 	= 0B00000110;
74
 
23 pfowler 75
  /*
76
	PD4	- Input, Pullup 	- Rotary function select
77
	PD5	- Input, Pullup 	- Button
78
	PD6	- Input, Pullup 	- Button
79
	PD7	- Output		- Status LED
80
  */
22 pfowler 81
  DDRD		= 0B10000000;
23 pfowler 82
  PORTD		= 0B01110000;
21 pfowler 83
 
84
  PCMSK0 |= (( 1 << PCINT1 ) | ( 1 << PCINT2 )); //enable encoder pins interrupt sources
85
  PCICR |= ( 1 << PCIE0 ); //enable pin change interupts
86
 
23 pfowler 87
  // Timers not used for the moment
88
  // Setup timer0 - Enable overflow, 8 times prescaler
89
  //TIMSK0 = (1<<TOIE0);			// Eable timer overflow for Timer0
90
  //TCNT0 = 0x00;				// Set Timer0 to 0
91
  //TCCR0B = (1<< CS01) ;			// /8 prescaler
21 pfowler 92
 
93
  usbDeviceDisconnect();  /* enforce re-enumeration, do this while interrupts are disabled! */
94
  _delay_ms(500);
95
  usbDeviceConnect();
96
 
97
  wdt_enable(WDTO_1S);
98
  usbInit();
99
  sei();
100
 
101
 
102
  for(;;) {
103
    wdt_reset();
104
    usbPoll();
105
 
106
    if(usbInterruptIsReady()){
22 pfowler 107
        report.data = 0x05; // Center pad, little endian
21 pfowler 108
 
23 pfowler 109
	// Send our buttons 
21 pfowler 110
	if (bit_is_clear(PIND, PD5))
111
		report.A = 1;
112
	if (bit_is_clear(PIND, PD6))
113
		report.B = 1;
114
 
23 pfowler 115
	// Now work out what rotary to send, if any
116
	// Also record if we sent a positive response, 
117
	//  so we can send a '0' next time (if selected on PD4)
118
	if (rot_stat == 0x01 && rot_sent == 0) {
22 pfowler 119
		report.rot1 = 1;
23 pfowler 120
		rot_sent = 1;
121
	} else if (rot_stat == 0x02 && rot_sent == 0) {
22 pfowler 122
		report.rot2 = 1;
23 pfowler 123
		rot_sent = 1;
22 pfowler 124
	} else {
23 pfowler 125
		rot_sent = 0;
22 pfowler 126
	}
127
 
23 pfowler 128
	// Reset our stat so ready for next turn
22 pfowler 129
	rot_stat = 0;
130
 
23 pfowler 131
	// If our function select is set, dont bother
132
	//  sending a 'o' between consequtive 1's.
133
	if (rbi(PIND, PD4))
134
		rot_sent = 0;
135
 
21 pfowler 136
      /* called after every poll of the interrupt endpoint */
137
      usbSetInterrupt(&report, sizeof(report));
138
    }
139
  }
140
}
141
 
142
void doInt() {
23 pfowler 143
	// If rot_stat is not 0, we havn't sent
144
	//  our last results yet. Skip this click.
22 pfowler 145
	if (rot_stat != 0) {
146
		pcIntMask = 0;
147
		return;
148
	}
23 pfowler 149
 
150
	// Check which pin caused the interrupt. If they both
151
	//  equal 0, the pin that interrupted is the direction
152
  	if (rbi(pcIntCurr, PCINT1) == 0 
153
		&& rbi(pcIntCurr, PCINT2) == 0 
154
		&& rbi(pcIntMask, PCINT1) ) {
155
			rot_stat = 1;
156
  	} else if (rbi(pcIntCurr, PCINT1) == 0 
157
		&& rbi(pcIntCurr, PCINT2) == 0 
158
		&& rbi(pcIntMask, PCINT2) ) {
159
			rot_stat = 2;
160
  	}
161
 
162
	// Clear the mask so we know we've delth with it
163
	pcIntMask = 0;
21 pfowler 164
}
165
 
23 pfowler 166
/* Not used for the moment
21 pfowler 167
ISR(TIMER0_OVF_vect) {
168
	timer0_ovf++;
169
}
23 pfowler 170
*/
21 pfowler 171
 
172
ISR(PCINT0_vect)
173
{
23 pfowler 174
	// Save the state and work out which pin caused
175
	//  the interrupt to occur
176
	pcIntCurr = PINB;
177
	pcIntMask = pcIntCurr ^ pcIntLast;
178
	pcIntLast = pcIntCurr;
179
	doInt();
21 pfowler 180
}
181