Subversion Repositories group.electronics

Rev

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

Rev 134 Rev 136
Line 1... Line 1...
1
#include <avr/io.h>
1
#include <avr/io.h>
2
#include <avr/wdt.h>
2
#include <avr/wdt.h>
-
 
3
#include <avr/eeprom.h>
3
#include <avr/interrupt.h>
4
#include <avr/interrupt.h>
4
#include <string.h>
5
#include <avr/pgmspace.h>
5
#include <util/delay.h>
6
#include <util/delay.h>
6
#include <stdlib.h>
-
 
-
 
7
 
7
 
8
 
8
#include "config.h"
9
#include "config.h"
9
#include "avrutil.h"
10
#include "avrutil.h"
10
#include "hc595.h"
11
#include "usbdrv.h"
11
#include "twires.h"
12
#include "i2cbb.h"
12
 
13
 
13
 
14
 
14
#ifndef NULL
15
#ifndef NULL
15
#define NULL    ((void *)0)
16
#define NULL    ((void *)0)
16
#endif
17
#endif
17
 
18
 
-
 
19
static void calibrateOscillator(void);
-
 
20
void usbEventResetReady(void);
-
 
21
 
-
 
22
static void updateDisplays();
18
 
23
 
19
void writeSegment(uint8_t digit, uint8_t value);
24
#define DIS_COM_ACTIVE
20
void pcInterrupt(void);
25
#define DIS_COM_STANDBY
-
 
26
#define DIS_NAV_ACTIVE
21
void receiveEvent(uint8_t bytes);
27
#define DIS_NAV_STANDBY
22
 
28
 
23
/* ------------------------------------------------------------------------- */
29
// Hold the chars for the display panels
24
 
30
 
-
 
31
volatile uint8_t displays[5][6] = {
-
 
32
							{ 0, 0, 0, 0, 0},	// DIS_COM_ACTIVE
-
 
33
							{ 0, 0, 0, 0, 0},	// DIS_COM_STANDBY
-
 
34
							{ 0, 0, 0, 0, 0},	// DIS_NAV_ACTIVE
-
 
35
							{ 0, 0, 0, 0, 0},	// DIS_NAV_STANDBY
-
 
36
						 };
25
 
37
 
26
 
38
 
-
 
39
//static uchar usbReplyBuf[16];
27
volatile uint8_t user_debug = 0;
40
volatile static uint8_t bytesRemaining;
-
 
41
volatile static uint8_t* writeIdx;
-
 
42
 
28
volatile uint8_t tmr0_ovf = 0;
43
volatile uint8_t tmr0_ovf = 0;
-
 
44
volatile uint8_t update = 1;
-
 
45
 
-
 
46
 
29
 
47
 
30
int main(void) {
48
int main(void) {
-
 
49
	uchar   calibrationValue;
31
 
50
 
-
 
51
	calibrationValue = eeprom_read_byte(0); /* calibration value from last time */
-
 
52
	if(calibrationValue != 0xff){
-
 
53
		OSCCAL = calibrationValue;
-
 
54
	}
-
 
55
 
32
  /*
56
	/*
33
  DDR : 1 = Output, 0 = Input
57
		DDR : 1 = Output, 0 = Input
34
  PORT: 1 = Pullup for Input, otherwise set output
58
		PORT: 1 = Pullup for Input, otherwise set output
35
  PIN : Read input pin
59
		PIN : Read input pin
36
 
60
 
37
	PB0     -
61
		PB0	-
38
	PB1     - 		- USB D- Low Speed
62
		PB1	- 		- USB D- Low Speed
39
	PB2     -		- USB D+ 
63
		PB2	-		- USB D+
40
	PB3     - 		- SDA i2c bb
64
		PB3	- 		- SCL i2c bb
41
	PB4	-		- SCL i2c bb
65
		PB4	-		- SDA i2c bb
42
	PB5	-
66
		PB5	-
43
  */
67
	*/
44
  DDRB          = 0B00000000;
68
	DDRB          = 0B00000001;
45
  PORTB         = 0B00000000;
69
	PORTB         = 0B00000001;
-
 
70
 
-
 
71
    usbDeviceDisconnect();
-
 
72
    _delay_ms(300);
-
 
73
    usbDeviceConnect();
46
 
74
 
47
    systime = 0;
75
    systime = 0;
48
    sysclockInit();
76
    sysclockInit();
49
 
77
 
50
    wdt_enable(WDTO_1S);	// Watchdog for 1 sec
78
    wdt_enable(WDTO_1S);	// Watchdog for 1 sec
-
 
79
    usbInit();
51
    sei();			// Enable interrupts
80
    sei();			// Enable interrupts
52
 
81
 
-
 
82
 
-
 
83
/*
-
 
84
    cbi(PORTB, PB0);
-
 
85
    i2cbb_Init();
-
 
86
    i2cbb_Start();
-
 
87
    i2cbb_Write(0x4c);
-
 
88
    i2cbb_Write(0x05);
-
 
89
    i2cbb_Write(0x08);
-
 
90
    i2cbb_Stop();
-
 
91
    sbi(PORTB, PB0);
-
 
92
*/
-
 
93
 
53
    for(;;){
94
    for(;;){
54
    	 wdt_reset();
95
    	 wdt_reset();
-
 
96
    	 usbPoll();
55
 
97
 
56
    	if (user_debug) {
98
    	if (update) {
57
 
-
 
-
 
99
    		updateDisplays();
58
    	}
100
    	}
59
 
101
 
60
    }
102
    }
61
    return 0;
103
    return 0;
62
}
104
}
63
 
105
 
-
 
106
static void updateDisplays() {
-
 
107
    cbi(PORTB, PB0);
-
 
108
    i2cbb_Init();
-
 
109
    i2cbb_Start();
-
 
110
    i2cbb_Write(0x4c);
-
 
111
    i2cbb_Write(0x05);
-
 
112
    i2cbb_Write(0x00 | displays[0][0]);
-
 
113
    i2cbb_Write(0x10 | displays[0][1]);
-
 
114
    i2cbb_Write(0x20 | displays[0][2]);
-
 
115
    i2cbb_Write(0x30 | displays[0][3]);
-
 
116
    i2cbb_Write(0x40 | displays[0][4]);
-
 
117
 
-
 
118
    i2cbb_Stop();
-
 
119
    sbi(PORTB, PB0);
-
 
120
 
-
 
121
    cbi(PORTB, PB0);
-
 
122
    i2cbb_Init();
-
 
123
    i2cbb_Start();
-
 
124
    i2cbb_Write(0x4c);
-
 
125
    i2cbb_Write(0x05);
-
 
126
    i2cbb_Write(0x50 | displays[1][0]);
-
 
127
    i2cbb_Write(0x60 | displays[1][1]);
-
 
128
    i2cbb_Write(0x70 | displays[1][2]);
-
 
129
    i2cbb_Write(0x80 | displays[1][3]);
-
 
130
    i2cbb_Write(0x90 | displays[1][4]);
-
 
131
    i2cbb_Stop();
-
 
132
    sbi(PORTB, PB0);
-
 
133
 
-
 
134
 
-
 
135
	update = 0;
-
 
136
}
-
 
137
 
-
 
138
static void calibrateOscillator(void) {
-
 
139
    uchar step = 128;
-
 
140
    uchar trialValue = 0, optimumValue;
-
 
141
    int x, optimumDev;
-
 
142
    int targetValue = (unsigned)(1499 * (double)F_CPU / 10.5e6 + 0.5);
-
 
143
 
-
 
144
    /* do a binary search: */
-
 
145
    do {
-
 
146
        OSCCAL = trialValue + step;
-
 
147
        x = usbMeasureFrameLength();    /* proportional to current real frequency */
-
 
148
        if(x < targetValue)             /* frequency still too low */
-
 
149
            trialValue += step;
-
 
150
        step >>= 1;
-
 
151
    } while(step > 0);
-
 
152
    /* We have a precision of +/- 1 for optimum OSCCAL here */
-
 
153
    /* now do a neighborhood search for optimum value */
-
 
154
    optimumValue = trialValue;
-
 
155
    optimumDev = x; /* this is certainly far away from optimum */
-
 
156
    for(OSCCAL = trialValue - 1; OSCCAL <= trialValue + 1; OSCCAL++){
-
 
157
        x = usbMeasureFrameLength() - targetValue;
-
 
158
        if(x < 0)
-
 
159
            x = -x;
-
 
160
        if(x < optimumDev){
-
 
161
            optimumDev = x;
-
 
162
            optimumValue = OSCCAL;
-
 
163
        }
-
 
164
    }
-
 
165
    OSCCAL = optimumValue;
-
 
166
}
-
 
167
 
-
 
168
#define USB_GET_CONT_VER		10		// Send the controller version data
-
 
169
#define USB_GET_COMM_VER		11		// Send the comm display version data
-
 
170
#define USB_GET_NAV_VER			12		// Send the nav display version data
-
 
171
 
-
 
172
#define USB_SET_COMM_ACTIVE 	20		// Set the comm active freq
-
 
173
#define USB_SET_COMM_STANDBY	21		// Set the comm standby freq
-
 
174
#define USB_SET_NAV_ACTIVE		22		// Set the nav active freq
-
 
175
#define USB_SET_NAV_STANDBY		23		// Set the nav standby freq
-
 
176
#define USB_BLANK_ALL			24		// Blank all displays
-
 
177
#define USB_SET_ALL				25		// Blank all displays
-
 
178
 
-
 
179
#define USB_GET_COMM_ENC		30		// Send the comm rotary encoder position
-
 
180
#define USB_RESET_COMM_ENC		31		// Reset the comm encoder to 0
-
 
181
#define USB_GET_NAV_ENC			32		// Send the nav rotary encoder position
-
 
182
#define USB_RESET_NAV_ENC		33		// Reset the nav encoder to 0
-
 
183
 
-
 
184
#define USB_GET_BUTTONS			32		// Send the status of all the buttons
-
 
185
 
-
 
186
 
-
 
187
 
-
 
188
usbMsgLen_t usbFunctionSetup(uchar data[8])
-
 
189
{
-
 
190
    usbRequest_t    *rq = (void *)data;
-
 
191
 
-
 
192
 
-
 
193
	switch (rq->bRequest ) {
-
 
194
	case 20: {
-
 
195
		uint8_t dis = rq->wValue.bytes[0];
-
 
196
		uint8_t dig = rq->wValue.bytes[1];
-
 
197
		uint8_t val = rq->wIndex.bytes[0];
-
 
198
		displays[dis][dig] = val;
-
 
199
		update = 1;
-
 
200
		break;
-
 
201
		}
-
 
202
 
-
 
203
	}
-
 
204
	return 0;
-
 
205
}
-
 
206
 
-
 
207
/*
-
 
208
uchar usbFunctionWrite(uchar *data, uchar len) {
-
 
209
	uint8_t i;
-
 
210
	if (len > bytesRemaining)
-
 
211
		len = bytesRemaining;
-
 
212
	bytesRemaining -= len;
-
 
213
	for (i=0; i<len; i++) {
-
 
214
		*writeIdx++ = data[i];
-
 
215
	}
-
 
216
	displays[0][3] = 2;
-
 
217
	displays[0][4] = 2;
-
 
218
	update = 1;
-
 
219
	return (bytesRemaining == 0);
-
 
220
}
-
 
221
*/
-
 
222
 
-
 
223
void usbEventResetReady(void) {
-
 
224
    cli();
-
 
225
    calibrateOscillator();
-
 
226
    sei();
-
 
227
    eeprom_write_byte(0, OSCCAL);   /* store the calibrated value in EEPROM */
-
 
228
}
-
 
229
 
64
ISR(TIM0_OVF_vect) {
230
ISR(TIM0_OVF_vect) {
65
 
231
 
66
	tmr0_ovf++;
232
	tmr0_ovf++;
67
 
233
 
68
	// Clk/1 TCCR0B = (1<< CS00);
234
	// Clk/1 TCCR0B = (1<< CS00);