Subversion Repositories group.electronics

Rev

Rev 137 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 137 Rev 139
Line 3... Line 3...
3
#include <avr/eeprom.h>
3
#include <avr/eeprom.h>
4
#include <avr/interrupt.h>
4
#include <avr/interrupt.h>
5
#include <avr/pgmspace.h>
5
#include <avr/pgmspace.h>
6
#include <util/delay.h>
6
#include <util/delay.h>
7
 
7
 
8
 
-
 
9
#include "config.h"
8
#include "config.h"
10
#include "avrutil.h"
9
#include "avrutil.h"
11
#include "usbdrv.h"
10
#include "usbdrv.h"
12
#include "i2cbb.h"
11
#include "i2cbb.h"
13
 
12
 
14
 
13
 
15
#ifndef NULL
14
#ifndef NULL
16
#define NULL    ((void *)0)
15
#define NULL    ((void *)0)
17
#endif
16
#endif
18
 
17
 
19
static void calibrateOscillator(void);
-
 
20
void usbEventResetReady(void);
18
void usbEventResetReady(void);
21
 
-
 
22
//static void updateDisplays();
19
static void calibrateOscillator(void);
23
static void updateDisplay(uint8_t dis);
20
static void updateDisplay(uint8_t dis);
-
 
21
static void updateInput();
24
 
22
 
25
#define DIS_COM_ACTIVE
-
 
26
#define DIS_COM_STANDBY
-
 
27
#define DIS_NAV_ACTIVE
-
 
28
#define DIS_NAV_STANDBY
-
 
29
 
-
 
30
// Hold the chars for the display panels
-
 
31
 
-
 
32
volatile uint8_t displays[5][6] = {
-
 
33
							{ 6, 6, 6, 6, 6},	// DIS_COM_ACTIVE
-
 
34
							{ 7, 7, 7, 7, 7},	// DIS_COM_STANDBY
-
 
35
							{ 0, 0, 0, 0, 0},	// DIS_NAV_ACTIVE
-
 
36
							{ 0, 0, 0, 0, 0},	// DIS_NAV_STANDBY
-
 
37
						 };
-
 
38
 
-
 
39
 
-
 
40
//static uchar usbReplyBuf[16];
-
 
41
volatile static uint8_t bytesRemaining;
23
//static void requestData(uint8_t addr, uint8_t code, uint8_t* data, uint8_t len);
42
//volatile static uint8_t* writeIdx;
-
 
43
 
24
 
-
 
25
void print16(uint8_t dis, uint8_t dig, uint16_t val);
-
 
26
 
-
 
27
#define DISPLAY_ATTACHED 1
-
 
28
#define INPUT_REFRESH 5
-
 
29
 
-
 
30
struct display_type {
44
volatile uint8_t tmr0_ovf = 0;
31
	uint8_t address;
45
volatile uint8_t update = 255;
32
	uint8_t value[10];
-
 
33
	uint16_t decpts;
46
 
34
 
-
 
35
	uint8_t rotary;		// State of the rotary encoder
-
 
36
	uint8_t buttons;	// State of the buttons
-
 
37
} display[2];
-
 
38
 
-
 
39
 
-
 
40
 
-
 
41
static uint8_t usbReplyBuf[8];
-
 
42
static uint8_t update = 255;
-
 
43
 
-
 
44
 
-
 
45
volatile uint8_t tmr0_ovf = 0;
47
 
46
 
48
 
47
 
49
int main(void) {
48
int main(void) {
-
 
49
	// calibration value from last time
50
	uchar   calibrationValue;
50
	uchar   calibrationValue;
51
 
-
 
52
	calibrationValue = eeprom_read_byte(0); /* calibration value from last time */
51
	calibrationValue = eeprom_read_byte(0);
53
	if(calibrationValue != 0xff){
52
	if(calibrationValue != 0xff){
54
		OSCCAL = calibrationValue;
53
		OSCCAL = calibrationValue;
55
	}
54
	}
56
 
55
 
57
	/*
56
	/*
Line 73... Line 72...
73
    usbDeviceDisconnect();
72
    usbDeviceDisconnect();
74
    _delay_ms(500);
73
    _delay_ms(500);
75
    usbDeviceConnect();
74
    usbDeviceConnect();
76
 
75
 
77
    systime = 0;
76
    systime = 0;
-
 
77
    uint32_t refresh = 0;
78
    sysclockInit();
78
    sysclockInit();
79
 
79
 
80
    wdt_enable(WDTO_1S);	// Watchdog for 1 sec
80
    wdt_enable(WDTO_1S);
81
    usbInit();
81
    usbInit();
82
    sei();			// Enable interrupts
82
    sei();
83
 
-
 
84
 
83
 
85
/*
-
 
86
    cbi(PORTB, PB0);
-
 
87
    i2cbb_Init();
-
 
88
    i2cbb_Start();
-
 
89
    i2cbb_Write(0x4c);
-
 
90
    i2cbb_Write(0x05);
-
 
91
    i2cbb_Write(0x08);
-
 
92
    i2cbb_Stop();
-
 
93
    sbi(PORTB, PB0);
-
 
94
*/
-
 
95
 
84
 
-
 
85
    // Set the displays to blank
-
 
86
    display[0].address = 0x26;
-
 
87
    display[0].decpts = 0x00;
96
    uint8_t i;
88
    uint8_t i;
-
 
89
    for (i=0; i<DISPLAY_ATTACHED; i++) {
-
 
90
    	uint8_t j;
97
    for (i=0; i<2; i++)
91
    	for (j=0; j<10; j++)
-
 
92
    		display[i].value[j] = 0x0a;
98
    	updateDisplay(i);
93
    	updateDisplay(i);
99
 
94
    }
100
 
95
 
101
    for(;;){
96
    for(;;){
102
    	 wdt_reset();
97
    	 wdt_reset();
103
    	 usbPoll();
98
    	 usbPoll();
104
 
99
 
-
 
100
    	// Only update the display when a change
-
 
101
    	//  comes in fron the usb port
105
    	if (update != 255) {
102
    	if (update != 255) {
106
    		updateDisplay(update);
103
    		updateDisplay(update);
107
    		update = 255;
104
    		update = 255;
108
    	}
105
    	}
109
 
106
 
-
 
107
		if (systime > refresh) {
-
 
108
			refresh = systime + 5;
-
 
109
			updateInput();
-
 
110
		}
110
    }
111
    }
111
    return 0;
112
    return 0;
112
}
113
}
113
 
114
 
-
 
115
static void updateInput() {
-
 
116
	/*
-
 
117
	uint8_t data[1];
-
 
118
	requestData(0x4c, 0x0a, data, 1);
-
 
119
	display[0].rotary = data[0];
-
 
120
	requestData(0x4c, 0x0c, data, 1);
-
 
121
	display[0].buttons = data[0];
-
 
122
	*/
-
 
123
 
-
 
124
 
-
 
125
	i2cbb_Init();
-
 
126
	i2cbb_Start();
-
 
127
	i2cbb_Write( 0x4c );
-
 
128
	i2cbb_Write( 0x0a );
-
 
129
	i2cbb_Stop();
-
 
130
 
-
 
131
	i2cbb_Start();
-
 
132
	i2cbb_Write( 0x4d );
-
 
133
	display[0].rotary = i2cbb_Read(1);
-
 
134
	i2cbb_Stop();
-
 
135
 
-
 
136
	i2cbb_Init();
-
 
137
	i2cbb_Start();
-
 
138
	i2cbb_Write( 0x4c );
-
 
139
	i2cbb_Write( 0x0c );
-
 
140
	i2cbb_Stop();
-
 
141
 
-
 
142
	i2cbb_Start();
-
 
143
	i2cbb_Write( 0x4d );
-
 
144
	display[0].buttons = i2cbb_Read(1);
-
 
145
	i2cbb_Stop();
-
 
146
 
-
 
147
}
-
 
148
 
-
 
149
/*
-
 
150
static void requestData(uint8_t addr, uint8_t code, uint8_t* data, uint8_t len) {
-
 
151
	i2cbb_Init();
-
 
152
	i2cbb_Start();
-
 
153
	i2cbb_Write( addr );
-
 
154
	i2cbb_Write( code );
-
 
155
	i2cbb_Stop();
-
 
156
 
-
 
157
	uint8_t i;
-
 
158
	i2cbb_Write( addr + 1 );
-
 
159
	for (i=0; i<len; i++)
-
 
160
		data[i] = i2cbb_Read(1);
-
 
161
	i2cbb_Stop();
-
 
162
}
-
 
163
*/
-
 
164
 
114
static void updateDisplay(uint8_t dis) {
165
static void updateDisplay(uint8_t dis) {
115
    cbi(PORTB, PB0);
166
    cbi(PORTB, PB0);
-
 
167
 
-
 
168
    // Send the display buffer to display board
116
    i2cbb_Init();
169
    i2cbb_Init();
117
    i2cbb_Start();
170
    i2cbb_Start();
118
    i2cbb_Write(0x4c);
171
    i2cbb_Write(0x4c);
119
    i2cbb_Write(0x05);
172
    i2cbb_Write(0x05);
120
    uint8_t n;
173
    uint8_t n;
121
    for (n=0; n<5; n++) {
174
    for (n=0; n<10; n++) {
122
    	i2cbb_Write( ((n+5*dis)<<4) | (displays[dis][n] & 0x0f) );
175
    	uint8_t send = (n << 4) | display[0].value[n];
-
 
176
    	i2cbb_Write( send );
123
    }
177
    }
124
    i2cbb_Stop();
178
    i2cbb_Stop();
-
 
179
 
-
 
180
    /*
-
 
181
    // Send the decimal point
-
 
182
	i2cbb_Init();
-
 
183
	i2cbb_Start();
-
 
184
	i2cbb_Write( 0x4c );
-
 
185
	i2cbb_Write(0x08);
-
 
186
	i2cbb_Write((uint8_t)(display[dis].decpts>>8));
-
 
187
	i2cbb_Write((uint8_t)display[dis].decpts);
-
 
188
	i2cbb_Stop();
-
 
189
	*/
-
 
190
 
125
    sbi(PORTB, PB0);
191
    sbi(PORTB, PB0);
126
}
192
}
127
 
193
 
128
/*
-
 
129
static void updateDisplays() {
194
#define USB_SET_LATCH			20
130
    cbi(PORTB, PB0);
-
 
131
    i2cbb_Init();
-
 
132
    i2cbb_Start();
-
 
133
    i2cbb_Write(0x4c);
195
#define USB_SET_DISPLAY1		21
134
    i2cbb_Write(0x05);
196
#define USB_SET_DISPLAY2		22
135
    i2cbb_Write(0x00 | displays[0][0]);
-
 
136
    i2cbb_Write(0x10 | displays[0][1]);
-
 
137
    i2cbb_Write(0x20 | displays[0][2]);
-
 
138
    i2cbb_Write(0x30 | displays[0][3]);
-
 
139
    i2cbb_Write(0x40 | displays[0][4]);
-
 
140
 
197
 
141
    i2cbb_Stop();
-
 
142
    sbi(PORTB, PB0);
198
#define USB_GET_INPUT			30
143
 
199
 
144
    cbi(PORTB, PB0);
-
 
145
    i2cbb_Init();
-
 
146
    i2cbb_Start();
-
 
147
    i2cbb_Write(0x4c);
-
 
148
    i2cbb_Write(0x05);
200
#define USB_SET_INT16			50
149
    i2cbb_Write(0x50 | displays[1][0]);
-
 
150
    i2cbb_Write(0x60 | displays[1][1]);
-
 
151
    i2cbb_Write(0x70 | displays[1][2]);
-
 
-
 
201
 
152
    i2cbb_Write(0x80 | displays[1][3]);
202
usbMsgLen_t usbFunctionSetup(uchar data[8])
-
 
203
{
153
    i2cbb_Write(0x90 | displays[1][4]);
204
    usbRequest_t    *rq = (void *)data;
154
    i2cbb_Stop();
-
 
155
    sbi(PORTB, PB0);
-
 
156
 
205
 
-
 
206
	switch (rq->bRequest ) {
-
 
207
		case USB_SET_LATCH: {
-
 
208
			update = 0;
-
 
209
			break;
-
 
210
		}
-
 
211
		case USB_SET_DISPLAY1: {
-
 
212
			uint8_t dis = rq->wValue.bytes[1];
-
 
213
			uint8_t dig = rq->wValue.bytes[0];
-
 
214
			//uint8_t dp = rq->wIndex.bytes[1];
-
 
215
			uint8_t val = rq->wIndex.bytes[0];
-
 
216
			display[dis].value[dig] = val;
-
 
217
			/*
-
 
218
			if (dp)
-
 
219
				sbi(display[dis].decpts, 1 << dig);
-
 
220
			else
-
 
221
				cbi(display[dis].decpts, 1 << dig);
-
 
222
			//display[dis].decpts |= dp << dig;*/
-
 
223
			break;
-
 
224
		}
-
 
225
		case USB_SET_INT16: {
-
 
226
			uint8_t dis = rq->wValue.bytes[1];
-
 
227
			uint8_t dig = rq->wValue.bytes[0];
-
 
228
			uint16_t val = ((uint16_t)rq->wIndex.bytes[1]) << 8;
-
 
229
			val |= (rq->wIndex.bytes[0] & 0x0f);
-
 
230
			print16(dis, dig, val);
-
 
231
			break;
-
 
232
		}
-
 
233
		case USB_GET_INPUT: {
-
 
234
			usbReplyBuf[0] = display[0].buttons;
-
 
235
			usbReplyBuf[1] = display[0].rotary;
-
 
236
			usbMsgPtr = usbReplyBuf;
-
 
237
			return sizeof(usbReplyBuf);
-
 
238
		}
157
 
239
 
-
 
240
	}
-
 
241
	return 0;
-
 
242
}
-
 
243
 
-
 
244
void print16(uint8_t dis, uint8_t dig, uint16_t val) {
-
 
245
	uint8_t buf[] = {0x0a, 0x0a, 0x0a, 0x0a, 0x0a};
-
 
246
	uint8_t len = itobcd16(val, buf);
-
 
247
 
158
	update = 0;
248
	uint8_t i;
-
 
249
	for (i=0; i<5; i++) {
-
 
250
		if (i < len)
-
 
251
			display[dis].value[dig+i] = buf[i];
-
 
252
	}
159
}
253
}
160
*/
-
 
161
 
254
 
162
static void calibrateOscillator(void) {
255
static void calibrateOscillator(void) {
163
    uchar step = 128;
256
    uchar step = 128;
164
    uchar trialValue = 0, optimumValue;
257
    uchar trialValue = 0, optimumValue;
165
    int x, optimumDev;
258
    int x, optimumDev;
Line 187... Line 280...
187
        }
280
        }
188
    }
281
    }
189
    OSCCAL = optimumValue;
282
    OSCCAL = optimumValue;
190
}
283
}
191
 
284
 
192
#define USB_GET_CONT_VER		10		// Send the controller version data
-
 
193
#define USB_GET_COMM_VER		11		// Send the comm display version data
-
 
194
#define USB_GET_NAV_VER			12		// Send the nav display version data
-
 
195
 
-
 
196
#define USB_SET_DIGIT			20		// Set a digit on display
-
 
197
 
-
 
198
#define USB_GET_COMM_ENC		30		// Send the comm rotary encoder position
-
 
199
#define USB_RESET_COMM_ENC		31		// Reset the comm encoder to 0
-
 
200
#define USB_GET_NAV_ENC			32		// Send the nav rotary encoder position
-
 
201
#define USB_RESET_NAV_ENC		33		// Reset the nav encoder to 0
-
 
202
 
-
 
203
#define USB_GET_BUTTONS			32		// Send the status of all the buttons
-
 
204
 
-
 
205
 
-
 
206
 
-
 
207
usbMsgLen_t usbFunctionSetup(uchar data[8])
-
 
208
{
-
 
209
    usbRequest_t    *rq = (void *)data;
-
 
210
 
-
 
211
 
-
 
212
	switch (rq->bRequest ) {
-
 
213
		case USB_SET_DIGIT: {
-
 
214
			uint8_t dis = rq->wValue.bytes[1];
-
 
215
			uint8_t dig = rq->wValue.bytes[0];
-
 
216
			uint8_t val = rq->wIndex.bytes[0];
-
 
217
			displays[dis][dig] = val;
-
 
218
			update = dis;
-
 
219
			break;
-
 
220
		}
-
 
221
 
-
 
222
	}
-
 
223
	return 0;
-
 
224
}
-
 
225
 
-
 
226
/*
-
 
227
uchar usbFunctionWrite(uchar *data, uchar len) {
-
 
228
	uint8_t i;
-
 
229
	if (len > bytesRemaining)
-
 
230
		len = bytesRemaining;
-
 
231
	bytesRemaining -= len;
-
 
232
	for (i=0; i<len; i++) {
-
 
233
		*writeIdx++ = data[i];
-
 
234
	}
-
 
235
	displays[0][3] = 2;
-
 
236
	displays[0][4] = 2;
-
 
237
	update = 1;
-
 
238
	return (bytesRemaining == 0);
-
 
239
}
-
 
240
*/
-
 
241
 
285
 
242
void usbEventResetReady(void) {
286
void usbEventResetReady(void) {
243
    cli();
287
    cli();
244
    calibrateOscillator();
288
    calibrateOscillator();
245
    sei();
289
    sei();