Subversion Repositories group.electronics

Rev

Rev 150 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 150 Rev 152
Line 8... Line 8...
8
#include "config.h"
8
#include "config.h"
9
#include "avrutil.h"
9
#include "avrutil.h"
10
#include "usbdrv.h"
10
#include "usbdrv.h"
11
#include "i2cbb.h"
11
#include "i2cbb.h"
12
 
12
 
-
 
13
#define HW_VERSION 0x01
-
 
14
#define SW_VERSION 0x01
13
 
15
 
14
#ifndef NULL
16
#ifndef NULL
15
#define NULL    ((void *)0)
17
#define NULL    ((void *)0)
16
#endif
18
#endif
17
 
19
 
-
 
20
#define DISPLAYS_ATTACHED 2
-
 
21
#define INPUT_REFRESH 5
-
 
22
 
-
 
23
#define I2C_GET_VERSION			0x01
-
 
24
#define I2C_SET_DEBUG			0x03
-
 
25
#define I2C_SET_DIGITS			0x05
-
 
26
#define I2C_SET_DECIMAL_PTS		0x08
-
 
27
#define I2C_RESET_ROTARY		0x09
-
 
28
#define I2C_GET_ROTARY_DATA 	0x0a
-
 
29
#define I2C_GET_BUTTON_DATA 	0x0c
-
 
30
 
-
 
31
#define USB_GET_VERSION		01
-
 
32
#define USB_SET_LATCH			20
-
 
33
#define USB_SET_DISPLAY1		21
-
 
34
#define USB_SET_DISPLAY2		22
-
 
35
#define USB_GET_INPUT			30
-
 
36
 
-
 
37
 
18
void usbEventResetReady(void);
38
void usbEventResetReady(void);
19
static void calibrateOscillator(void);
39
static void calibrateOscillator(void);
20
static void updateDisplay(uint8_t dis);
40
static void updateDisplay(uint8_t dis);
21
static void updateInput();
41
static void updateInput();
22
 
-
 
23
//static void requestData(uint8_t addr, uint8_t code, uint8_t* data, uint8_t len);
-
 
24
 
-
 
25
void print16(uint8_t dis, uint8_t dig, uint16_t val);
42
static void getDisplayVersion(uint8_t dis);
26
 
-
 
27
#define DISPLAYS_ATTACHED 2
-
 
28
#define INPUT_REFRESH 5
-
 
29
 
43
 
30
struct display_type {
44
struct display_type {
31
	uint8_t address;
45
	uint8_t address;
32
	uint8_t value[10];
46
	uint8_t value[10];
33
	uint16_t decpts;
47
	uint16_t decpts;
-
 
48
	uint16_t version;	// HB = HW, LB = SW
34
 
49
 
35
	int8_t rotary;		// State of the rotary encoder
50
	int8_t rotary;		// State of the rotary encoder
36
	uint8_t buttons;	// State of the buttons
51
	uint8_t buttons;	// State of the buttons
37
} display[DISPLAYS_ATTACHED];
52
} display[DISPLAYS_ATTACHED];
38
 
53
 
39
 
54
 
40
 
55
 
41
static uint8_t usbReplyBuf[8];
56
static uint8_t usbReplyBuf[8];
42
static uint8_t update = 255;
57
static uint8_t latchDisplay = 255;
43
 
58
 
44
 
59
 
45
volatile uint8_t tmr0_ovf = 0;
60
volatile uint8_t tmr0_ovf = 0;
46
 
61
 
47
 
62
 
Line 80... Line 95...
80
    wdt_enable(WDTO_1S);
95
    wdt_enable(WDTO_1S);
81
    usbInit();
96
    usbInit();
82
    sei();
97
    sei();
83
 
98
 
84
 
99
 
85
    // Set the displays to blank
100
    // Setup the display data, blank each display
86
 
-
 
87
 
-
 
88
    uint8_t i;
101
    uint8_t i;
89
    for (i=0; i<DISPLAYS_ATTACHED; i++) {
102
    for (i=0; i<DISPLAYS_ATTACHED; i++) {
90
    	display[i].address = 0x26 + i;
103
    	display[i].address = 0x26 + i;
91
    	display[i].decpts = 0x00;
104
    	display[i].decpts = 0x00;
92
 
105
 
93
    	uint8_t j;
106
    	uint8_t j;
94
    	for (j=0; j<10; j++)
107
    	for (j=0; j<10; j++)
95
    		display[i].value[j] = 0x0a;
108
    		display[i].value[j] = 0x0a;
96
    	updateDisplay(i);
109
    	updateDisplay(i);
-
 
110
 
-
 
111
    	getDisplayVersion(i);
97
    }
112
    }
98
 
113
 
99
    for(;;){
114
    for(;;){
100
    	 wdt_reset();
115
    	 wdt_reset();
101
    	 usbPoll();
116
    	 usbPoll();
102
 
117
 
103
    	// Only update the display when a change
-
 
104
    	//  comes in fron the usb port
118
    	// Latch requests from the the USB host
105
    	if (update != 255) {
119
    	if (latchDisplay != 255) {
106
    		updateDisplay(update);
120
    		updateDisplay(latchDisplay);
107
    		update = 255;
121
    		latchDisplay = 255;
108
    	}
122
    	}
109
 
123
 
-
 
124
    	// Refresh time for getting user input data
110
		if (systime > refresh) {
125
		if (systime > refresh) {
111
			refresh = systime + INPUT_REFRESH;
126
			refresh = systime + INPUT_REFRESH;
112
			updateInput();
127
			updateInput();
113
		}
128
		}
114
    }
129
    }
115
    return 0;
130
    return 0;
116
}
131
}
117
 
132
 
-
 
133
static void getDisplayVersion(uint8_t dis) {
-
 
134
	uint8_t hw = 0x00;
-
 
135
	uint8_t sw = 0x00;
-
 
136
 
-
 
137
	i2cbb_Init();
-
 
138
	i2cbb_Start();
-
 
139
	i2cbb_Write( display[dis].address << 1 );
-
 
140
	i2cbb_Write( I2C_GET_VERSION );
-
 
141
	i2cbb_Stop();
-
 
142
 
-
 
143
	// Receive rotary data
-
 
144
	i2cbb_Start();
-
 
145
	i2cbb_Write( (display[dis].address << 1) + 1 );
-
 
146
	hw += (int8_t)i2cbb_Read(1);
-
 
147
	sw += (int8_t)i2cbb_Read(1);
-
 
148
	i2cbb_Stop();
-
 
149
 
-
 
150
	display[dis].version = ((uint16_t)hw << 8) | ((uint16_t)sw);
-
 
151
}
-
 
152
 
-
 
153
// Get the user input data from each display board
118
static void updateInput() {
154
static void updateInput() {
119
	uint8_t i;
155
	uint8_t i;
120
	for (i = 0; i < DISPLAYS_ATTACHED; i++) {
156
	for (i = 0; i < DISPLAYS_ATTACHED; i++) {
-
 
157
		// Request for the rotary data
121
		i2cbb_Init();
158
		i2cbb_Init();
122
		i2cbb_Start();
159
		i2cbb_Start();
123
		i2cbb_Write( display[i].address << 1 );
160
		i2cbb_Write( display[i].address << 1 );
124
		i2cbb_Write( 0x0a );
161
		i2cbb_Write( I2C_GET_ROTARY_DATA );
125
		i2cbb_Stop();
162
		i2cbb_Stop();
126
 
163
 
-
 
164
		// Receive rotary data
127
		i2cbb_Start();
165
		i2cbb_Start();
128
		i2cbb_Write( (display[i].address << 1) + 1 );
166
		i2cbb_Write( (display[i].address << 1) + 1 );
129
		display[i].rotary += (int8_t)i2cbb_Read(1);
167
		display[i].rotary += (int8_t)i2cbb_Read(1);
130
		i2cbb_Stop();
168
		i2cbb_Stop();
131
 
169
 
-
 
170
		// Reset the rotary on display board
132
		i2cbb_Init();
171
		i2cbb_Init();
133
		i2cbb_Start();
172
		i2cbb_Start();
134
		i2cbb_Write( display[i].address << 1 );
173
		i2cbb_Write( display[i].address << 1 );
135
		i2cbb_Write( 0x09 );
174
		i2cbb_Write( I2C_RESET_ROTARY );
136
		i2cbb_Stop();
175
		i2cbb_Stop();
137
 
176
 
138
 
-
 
-
 
177
		// Request the button data
139
		i2cbb_Init();
178
		i2cbb_Init();
140
		i2cbb_Start();
179
		i2cbb_Start();
141
		i2cbb_Write( display[i].address << 1 );
180
		i2cbb_Write( display[i].address << 1 );
142
		i2cbb_Write( 0x0c );
181
		i2cbb_Write( I2C_GET_BUTTON_DATA );
143
		i2cbb_Stop();
182
		i2cbb_Stop();
144
 
183
 
-
 
184
		// Receive the button data
145
		i2cbb_Start();
185
		i2cbb_Start();
146
		i2cbb_Write( (display[i].address << 1) + 1 );
186
		i2cbb_Write( (display[i].address << 1) + 1 );
147
		display[i].buttons = i2cbb_Read(1);
187
		display[i].buttons = i2cbb_Read(1);
148
		i2cbb_Stop();
188
		i2cbb_Stop();
149
	}
189
	}
150
 
190
 
151
}
191
}
152
 
192
 
-
 
193
// The the display digit display buffer to the board
-
 
194
// We can select which display to update as this can
-
 
195
//  get slow if updates are being done all the time,
-
 
196
//  which might affect the user input data tasks
153
static void updateDisplay(uint8_t dis) {
197
static void updateDisplay(uint8_t dis) {
154
    cbi(PORTB, PB0);
198
    cbi(PORTB, PB0);
155
 
199
 
156
    // Send the display buffer to display board
200
    // Send the display buffer to display board
157
    i2cbb_Init();
201
    i2cbb_Init();
158
    i2cbb_Start();
202
    i2cbb_Start();
159
    i2cbb_Write( display[dis].address << 1);
203
    i2cbb_Write( display[dis].address << 1);
160
    i2cbb_Write( 0x05 );
204
    i2cbb_Write( I2C_SET_DIGITS );
161
    uint8_t n;
205
    uint8_t n;
162
    for (n=0; n<10; n++) {
206
    for (n=0; n<10; n++) {
163
    	uint8_t send = (n << 4) | display[dis].value[n];
207
    	uint8_t send = (n << 4) | display[dis].value[n];
164
    	i2cbb_Write( send );
208
    	i2cbb_Write( send );
165
    }
209
    }
166
    i2cbb_Stop();
210
    i2cbb_Stop();
167
 
211
 
168
    /*
212
 
169
    // Send the decimal point
213
    // Send the decimal point
170
	i2cbb_Init();
214
	i2cbb_Init();
171
	i2cbb_Start();
215
	i2cbb_Start();
172
	i2cbb_Write( 0x4c );
216
	i2cbb_Write( display[dis].address << 1 );
173
	i2cbb_Write(0x08);
217
	i2cbb_Write( I2C_SET_DECIMAL_PTS );
174
	i2cbb_Write((uint8_t)(display[dis].decpts>>8));
218
	i2cbb_Write((uint8_t)(display[dis].decpts>>8));
175
	i2cbb_Write((uint8_t)display[dis].decpts);
219
	i2cbb_Write((uint8_t)display[dis].decpts);
176
	i2cbb_Stop();
220
	i2cbb_Stop();
177
	*/
221
 
178
 
222
 
179
    sbi(PORTB, PB0);
223
    sbi(PORTB, PB0);
180
}
224
}
181
 
225
 
182
#define USB_SET_LATCH			20
-
 
183
#define USB_SET_DISPLAY1		21
-
 
184
#define USB_SET_DISPLAY2		22
226
// The USB functions to transmit/receive data from USB host.
185
 
-
 
186
#define USB_GET_INPUT			30
-
 
187
 
-
 
188
#define USB_SET_INT16			50
-
 
189
 
-
 
190
usbMsgLen_t usbFunctionSetup(uchar data[8])
227
usbMsgLen_t usbFunctionSetup(uchar data[8])
191
{
228
{
192
    usbRequest_t    *rq = (void *)data;
229
    usbRequest_t    *rq = (void *)data;
193
 
230
 
194
	switch (rq->bRequest ) {
231
	switch (rq->bRequest ) {
-
 
232
		// Request for a display boards digits to be updated
195
		case USB_SET_LATCH: {
233
		case USB_SET_LATCH: {
196
			update = rq->wValue.bytes[0];;
234
			latchDisplay = rq->wValue.bytes[0];;
197
			break;
235
			break;
198
		}
236
		}
-
 
237
 
-
 
238
		// Sets the display boards digit buffer. Only on display
-
 
239
		//  board is updated per request. Also does decimal points
199
		case USB_SET_DISPLAY1: {
240
		case USB_SET_DISPLAY1: {
200
			uint8_t dis = rq->wValue.bytes[1];
241
			uint8_t dis = rq->wValue.bytes[1];
201
			uint8_t dig = rq->wValue.bytes[0];
242
			uint8_t dig = rq->wValue.bytes[0];
202
			//uint8_t dp = rq->wIndex.bytes[1];
243
			uint8_t dp = rq->wIndex.bytes[1];
203
			uint8_t val = rq->wIndex.bytes[0];
244
			uint8_t val = rq->wIndex.bytes[0];
204
			display[dis].value[dig] = val;
245
			display[dis].value[dig] = val;
205
			/*
246
 
206
			if (dp)
247
			if (dp)
207
				sbi(display[dis].decpts, 1 << dig);
248
				sbi(display[dis].decpts, 1 << dig);
208
			else
249
			else
209
				cbi(display[dis].decpts, 1 << dig);
250
				cbi(display[dis].decpts, 1 << dig);
-
 
251
 
210
			//display[dis].decpts |= dp << dig;*/
252
			//display[dis].decpts |= dp << dig;
-
 
253
 
211
			break;
254
			break;
212
		}
255
		}
-
 
256
 
-
 
257
		// Return the user input data all at once. Its populated from
-
 
258
		//  buffered data from the updateInput() function.
213
		case USB_GET_INPUT: {
259
		case USB_GET_INPUT: {
214
			uint8_t i;
260
			uint8_t i;
215
			for (i=0; i<DISPLAYS_ATTACHED; i++) {
261
			for (i=0; i<DISPLAYS_ATTACHED; i++) {
216
				usbReplyBuf[(i*2)] = display[i].buttons;
262
				usbReplyBuf[(i*2)] = display[i].buttons;
217
				usbReplyBuf[(i*2+1)] = display[i].rotary;
263
				usbReplyBuf[(i*2+1)] = display[i].rotary;
218
				display[i].rotary = 0;
264
				display[i].rotary = 0;
219
			}
265
			}
-
 
266
			usbMsgPtr = usbReplyBuf;
-
 
267
			return sizeof(usbReplyBuf);
-
 
268
			break;
-
 
269
		}
220
 
270
 
-
 
271
		// Return the version numbers for the controller board
-
 
272
		//  and for all attached display boards.
-
 
273
		case USB_GET_VERSION: {
-
 
274
			usbReplyBuf[0] = HW_VERSION;
-
 
275
			usbReplyBuf[1] = SW_VERSION;
-
 
276
			uint8_t i;
-
 
277
			for (i=0; i<DISPLAYS_ATTACHED; i++) {
-
 
278
				usbReplyBuf[2+(i*2)] = (uint8_t)(display[i].version >> 8);
-
 
279
				usbReplyBuf[2+(i*2)+1] = (uint8_t)(display[i].version && 0xff);
-
 
280
			}
221
			usbMsgPtr = usbReplyBuf;
281
			usbMsgPtr = usbReplyBuf;
222
			return sizeof(usbReplyBuf);
282
			return sizeof(usbReplyBuf);
-
 
283
			break;
223
		}
284
		}
224
 
285
 
225
	}
286
	}
226
	return 0;
287
	return 0;
227
}
288
}