Browse Source

Replaced serial Library.

This was done because I was suspecting an error to be there. This is
not the case, so we probably have a Hardware Problem with our USB
Serial Stuff… If we send data too fast it sends garbage. It also always
sends two additional bytes (0xC2 at the beginning, 0xFF at the end),
which both come at the beginning if we don't delay between sending the
chars…
Thomas Buck 12 years ago
parent
commit
6c8877d466
9 changed files with 300 additions and 912 deletions
  1. 7
    6
      AudioFirmware/main.c
  2. 1
    1
      CubeFirmware/cube.c
  3. 86
    57
      CubeFirmware/main.c
  4. 1
    1
      CubeFirmware/makefile
  5. 155
    0
      CubeFirmware/serial.c
  6. 48
    0
      CubeFirmware/serial.h
  7. 0
    651
      CubeFirmware/uart.c
  8. 0
    194
      CubeFirmware/uart.h
  9. 2
    2
      README.md

+ 7
- 6
AudioFirmware/main.c View File

34
 #endif
34
 #endif
35
 
35
 
36
 void blinkStatus(void) {
36
 void blinkStatus(void) {
37
-	uint8_t i, mem[7] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
37
+	// uint8_t i, mem[7] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
38
 
38
 
39
 	PORTB |= (1 << PB2);
39
 	PORTB |= (1 << PB2);
40
 	PORTB &= ~(1 << PB1);
40
 	PORTB &= ~(1 << PB1);
41
-	eqLed(mem);
41
+	// eqLed(mem);
42
 	_delay_ms(250);
42
 	_delay_ms(250);
43
 	PORTB &= ~(1 << PB2);
43
 	PORTB &= ~(1 << PB2);
44
 	PORTB |= (1 << PB1);
44
 	PORTB |= (1 << PB1);
45
-	for (i = 0; i < 7; i++) {
46
-		mem[i] = 0x00;
47
-	}
48
-	eqLed(mem);
45
+	// for (i = 0; i < 7; i++) {
46
+	// 	mem[i] = 0x00;
47
+	// }
48
+	// eqLed(mem);
49
 	_delay_ms(250);
49
 	_delay_ms(250);
50
 }
50
 }
51
 
51
 
70
 	while (1) {
70
 	while (1) {
71
 		music = equalizerGet();
71
 		music = equalizerGet();
72
 		if (twiDataWasSent()) {
72
 		if (twiDataWasSent()) {
73
+			PORTB ^= (1 << PB2); // Toggle for every transaction
73
 			twiSetDataToSend(music);
74
 			twiSetDataToSend(music);
74
 		}
75
 		}
75
 		eqLed(music);
76
 		eqLed(music);

+ 1
- 1
CubeFirmware/cube.c View File

25
 #include <stdlib.h>
25
 #include <stdlib.h>
26
 #include <util/atomic.h>
26
 #include <util/atomic.h>
27
 
27
 
28
-#include "uart.h"
28
+ #include "serial.h"
29
 #include "cube.h"
29
 #include "cube.h"
30
 
30
 
31
 #ifndef F_CPU
31
 #ifndef F_CPU

+ 86
- 57
CubeFirmware/main.c View File

20
  * You should have received a copy of the GNU General Public License
20
  * You should have received a copy of the GNU General Public License
21
  * along with LED-Cube.  If not, see <http://www.gnu.org/licenses/>.
21
  * along with LED-Cube.  If not, see <http://www.gnu.org/licenses/>.
22
  */
22
  */
23
-#include <stdlib.h>
24
-#include <stdint.h>
23
+
24
+#ifndef F_CPU
25
+#define F_CPU 16000000L
26
+#endif
27
+ 
28
+#define OK 0x42
29
+#define ERROR 0x23
30
+
25
 #include <avr/io.h>
31
 #include <avr/io.h>
26
 #include <util/delay.h>
32
 #include <util/delay.h>
27
 #include <avr/interrupt.h>
33
 #include <avr/interrupt.h>
28
-#include "uart.h"
34
+#include <avr/pgmspace.h>
35
+#include <stdint.h>
36
+#include <stdlib.h>
37
+
38
+#include "serial.h"
29
 #include "cube.h"
39
 #include "cube.h"
30
 #include "time.h"
40
 #include "time.h"
31
 #include "audio.h"
41
 #include "audio.h"
32
 #include "memLayer.h"
42
 #include "memLayer.h"
33
 
43
 
34
-#ifndef F_CPU
35
-#define F_CPU 16000000L
36
-#endif
37
-
38
-#define OK 0x42
39
-#define ERROR 0x23
40
-
41
 void serialHandler(char c);
44
 void serialHandler(char c);
42
 void recieveAnimations(void);
45
 void recieveAnimations(void);
43
 void transmitAnimations(void);
46
 void transmitAnimations(void);
48
 void visualizeAudioData(uint8_t *audioData, uint8_t **imageData);
51
 void visualizeAudioData(uint8_t *audioData, uint8_t **imageData);
49
 
52
 
50
 uint8_t refreshAnimationCount = 1;
53
 uint8_t refreshAnimationCount = 1;
54
+uint8_t lastButtonState = 1;
51
 
55
 
52
 int main(void) {
56
 int main(void) {
53
-	unsigned int character;
54
 	uint8_t *audioData;
57
 	uint8_t *audioData;
55
 	uint8_t **imageData;
58
 	uint8_t **imageData;
56
 	uint8_t i;
59
 	uint8_t i;
69
 	}
72
 	}
70
 
73
 
71
 	init(); // Initialize Cube Low-Level Code
74
 	init(); // Initialize Cube Low-Level Code
72
-	uart_init(UART_BAUD_SELECT(19200, 16000000L)); // Initialize Serial
75
+	serialInit(51, 8, NONE, 1);
73
 	initSystemTimer();
76
 	initSystemTimer();
74
 
77
 
75
 	sei(); // Enable Interrupts
78
 	sei(); // Enable Interrupts
76
 
79
 
80
+	for (i = 25; i < 55; i++) {
81
+		serialWrite(0x58); // 'X'
82
+		_delay_ms(1); // Sends garbage if no delay... :(
83
+	}
84
+
77
 	audioMode = audioModeSelected();
85
 	audioMode = audioModeSelected();
78
 	lastTimeChecked = getSystemTime();
86
 	lastTimeChecked = getSystemTime();
79
 
87
 
92
 			// We place 2016 Frames in mem => 131040
100
 			// We place 2016 Frames in mem => 131040
93
 			// That gives us 32 bytes at the beginning, 0 -> 31
101
 			// That gives us 32 bytes at the beginning, 0 -> 31
94
 			// The first frame starts at 32
102
 			// The first frame starts at 32
95
-			character = uart_getc();
96
-			if (!(character & 0xFF00)) { // No error...
97
-				serialHandler((char)(character & 0x00FF));
103
+			if (serialHasChar()) {
104
+				serialHandler((char)(serialGet()));
98
 			}
105
 			}
99
 
106
 
100
 			if (refreshAnimationCount) {
107
 			if (refreshAnimationCount) {
123
 	switch(c) {
130
 	switch(c) {
124
 		case OK:
131
 		case OK:
125
 			// Send "Hello World"
132
 			// Send "Hello World"
126
-			uart_putc(OK);
133
+			serialWrite(OK);
127
 			break;
134
 			break;
128
 
135
 
129
 		case 'd': case 'D':
136
 		case 'd': case 'D':
130
 			clearMem();
137
 			clearMem();
131
-			uart_putc(OK);
138
+			serialWrite(OK);
132
 			break;
139
 			break;
133
 
140
 
134
 		case 'g': case 'G':
141
 		case 'g': case 'G':
139
 			recieveAnimations();
146
 			recieveAnimations();
140
 			break;
147
 			break;
141
 
148
 
149
+		case 'v':
150
+			serialWriteString("v1");
151
+			break;
152
+
142
 		default:
153
 		default:
143
-			uart_putc(ERROR);
154
+			serialWrite(ERROR);
144
 			break;
155
 			break;
145
 	}
156
 	}
146
 }
157
 }
150
 	uint16_t completeCount = 0, character;
161
 	uint16_t completeCount = 0, character;
151
 	uint8_t frame[65];
162
 	uint8_t frame[65];
152
 
163
 
153
-	uart_putc(OK); // We are ready...
164
+	serialWrite(OK); // We are ready...
154
 
165
 
155
-	character = uart_getc();
166
+	character = serialGet();
156
 	while (character & 0xFF00) { // Wait for answer
167
 	while (character & 0xFF00) { // Wait for answer
157
-		character = uart_getc();
168
+		character = serialGet();
158
 	}
169
 	}
159
 
170
 
160
 	animCount = (uint8_t)(character & 0x00FF); // Got animation count
171
 	animCount = (uint8_t)(character & 0x00FF); // Got animation count
161
-	uart_putc(OK);
172
+	serialWrite(OK);
162
 
173
 
163
 	for (a = 0; a < animCount; a++) {
174
 	for (a = 0; a < animCount; a++) {
164
-		character = uart_getc();
175
+		character = serialGet();
165
 		while (character & 0xFF00) { // Wait for answer
176
 		while (character & 0xFF00) { // Wait for answer
166
-			character = uart_getc();
177
+			character = serialGet();
167
 		}
178
 		}
168
 
179
 
169
 		frameCount = (uint8_t)(character & 0x00FF); // Got frame count
180
 		frameCount = (uint8_t)(character & 0x00FF); // Got frame count
170
-		uart_putc(OK);
181
+		serialWrite(OK);
171
 
182
 
172
 		for (f = 0; f < frameCount; f++) {
183
 		for (f = 0; f < frameCount; f++) {
173
-			character = uart_getc();
184
+			character = serialGet();
174
 			while (character & 0xFF00) { // Wait for answer
185
 			while (character & 0xFF00) { // Wait for answer
175
-				character = uart_getc();
186
+				character = serialGet();
176
 			}
187
 			}
177
 
188
 
178
 			frame[64] = (uint8_t)(character & 0x00FF); // Got duration
189
 			frame[64] = (uint8_t)(character & 0x00FF); // Got duration
179
-			uart_putc(OK);
190
+			serialWrite(OK);
180
 
191
 
181
 			for (i = 0; i < 64; i++) {
192
 			for (i = 0; i < 64; i++) {
182
-				character = uart_getc();
193
+				character = serialGet();
183
 				while (character & 0xFF00) { // Wait for answer
194
 				while (character & 0xFF00) { // Wait for answer
184
-					character = uart_getc();
195
+					character = serialGet();
185
 				}
196
 				}
186
 
197
 
187
 				frame[i] = (uint8_t)(character & 0x00FF); // Got data byte
198
 				frame[i] = (uint8_t)(character & 0x00FF); // Got data byte
188
 			}
199
 			}
189
-			uart_putc(OK);
200
+			serialWrite(OK);
190
 
201
 
191
 			setFrame(completeCount++, frame);
202
 			setFrame(completeCount++, frame);
192
 		}
203
 		}
193
 	}
204
 	}
194
 
205
 
195
-	character = uart_getc();
206
+	character = serialGet();
196
 	while (character & 0xFF00) { // Wait for answer
207
 	while (character & 0xFF00) { // Wait for answer
197
-		character = uart_getc();
208
+		character = serialGet();
198
 	}
209
 	}
199
-	character = uart_getc();
210
+	character = serialGet();
200
 	while (character & 0xFF00) { // Wait for answer
211
 	while (character & 0xFF00) { // Wait for answer
201
-		character = uart_getc();
212
+		character = serialGet();
202
 	}
213
 	}
203
-	character = uart_getc();
214
+	character = serialGet();
204
 	while (character & 0xFF00) { // Wait for answer
215
 	while (character & 0xFF00) { // Wait for answer
205
-		character = uart_getc();
216
+		character = serialGet();
206
 	}
217
 	}
207
-	character = uart_getc();
218
+	character = serialGet();
208
 	while (character & 0xFF00) { // Wait for answer
219
 	while (character & 0xFF00) { // Wait for answer
209
-		character = uart_getc();
220
+		character = serialGet();
210
 	}
221
 	}
211
-	uart_putc(OK);
222
+	serialWrite(OK);
212
 	setAnimationCount(completeCount);
223
 	setAnimationCount(completeCount);
213
 	refreshAnimationCount = 1;
224
 	refreshAnimationCount = 1;
214
 }
225
 }
234
 		animationsToGo = (framesToGo / 255) + 1;
245
 		animationsToGo = (framesToGo / 255) + 1;
235
 	}
246
 	}
236
 
247
 
237
-	uart_putc(OK);
238
-	uart_putc(animationsToGo);
239
-	while ((character = uart_getc()) & 0xFF00); // Wait for answer
248
+	serialWrite(OK);
249
+	serialWrite(animationsToGo);
250
+	while ((character = serialGet()) & 0xFF00); // Wait for answer
240
 	if ((character & 0x00FF) != OK) { // Error code recieved
251
 	if ((character & 0x00FF) != OK) { // Error code recieved
241
 		return;
252
 		return;
242
 	}
253
 	}
248
 			fMax = framesToGo;
259
 			fMax = framesToGo;
249
 		}
260
 		}
250
 
261
 
251
-		uart_putc(fMax); // Number of Frames in current animation
252
-		while ((character = uart_getc()) & 0xFF00); // Wait for answer
262
+		serialWrite(fMax); // Number of Frames in current animation
263
+		while ((character = serialGet()) & 0xFF00); // Wait for answer
253
 		if ((character & 0x00FF) != OK) { // Error code recieved
264
 		if ((character & 0x00FF) != OK) { // Error code recieved
254
 			return;
265
 			return;
255
 		}
266
 		}
257
 		for (f = 0; f < fMax; f++) {
268
 		for (f = 0; f < fMax; f++) {
258
 			frame = getFrame(f + (255 * a));
269
 			frame = getFrame(f + (255 * a));
259
 
270
 
260
-			uart_putc(frame[64]); // frame duration
261
-			while ((character = uart_getc()) & 0xFF00); // Wait for answer
271
+			serialWrite(frame[64]); // frame duration
272
+			while ((character = serialGet()) & 0xFF00); // Wait for answer
262
 			if ((character & 0x00FF) != OK) { // Error code recieved
273
 			if ((character & 0x00FF) != OK) { // Error code recieved
263
 				free(frame);
274
 				free(frame);
264
 				return;
275
 				return;
265
 			}
276
 			}
266
 
277
 
267
 			for (i = 0; i < 64; i++) {
278
 			for (i = 0; i < 64; i++) {
268
-				uart_putc(frame[i]);
279
+				serialWrite(frame[i]);
269
 			}
280
 			}
270
-			while ((character = uart_getc()) & 0xFF00); // Wait for answer
281
+			while ((character = serialGet()) & 0xFF00); // Wait for answer
271
 			if ((character & 0x00FF) != OK) { // Error code recieved
282
 			if ((character & 0x00FF) != OK) { // Error code recieved
272
 				free(frame);
283
 				free(frame);
273
 				return;
284
 				return;
278
 		framesToGo -= fMax;
289
 		framesToGo -= fMax;
279
 	}
290
 	}
280
 
291
 
281
-	uart_putc(OK);
282
-	uart_putc(OK);
283
-	uart_putc(OK);
284
-	uart_putc(OK);
292
+	serialWrite(OK);
293
+	serialWrite(OK);
294
+	serialWrite(OK);
295
+	serialWrite(OK);
285
 
296
 
286
-	while ((character = uart_getc()) & 0xFF00); // Wait for answer
297
+	while ((character = serialGet()) & 0xFF00); // Wait for answer
287
 	// Error code ignored...
298
 	// Error code ignored...
288
 }
299
 }
289
 
300
 
290
 // Blocks 10ms or more
301
 // Blocks 10ms or more
291
 uint8_t audioModeSelected(void) {
302
 uint8_t audioModeSelected(void) {
292
-	// Switch: PB0, Low active
303
+	// Pushbutton: PB0, Low active
293
 	uint64_t startTime = getSystemTime();
304
 	uint64_t startTime = getSystemTime();
294
 	uint8_t startState = PINB & (1 << PB0);
305
 	uint8_t startState = PINB & (1 << PB0);
295
 
306
 
296
 	while((getSystemTime() - startTime) < 10); // Wait 10ms
307
 	while((getSystemTime() - startTime) < 10); // Wait 10ms
297
 
308
 
298
 	if ((PINB & (1 << PB0)) != startState) {
309
 	if ((PINB & (1 << PB0)) != startState) {
299
-		return audioModeSelected();
310
+		// State changed
311
+		// We can assume we have to toggle the state
312
+		if (lastButtonState == 0) {
313
+			lastButtonState = 1;
314
+		} else {
315
+			lastButtonState = 0;
316
+		}
317
+		return lastButtonState;
300
 	} else {
318
 	} else {
301
-		return startState;
319
+		if (startState) {
320
+			// Stayed the same, is pushed!
321
+			if (lastButtonState == 0) {
322
+				lastButtonState = 1;
323
+			} else {
324
+				lastButtonState = 0;
325
+			}
326
+			return lastButtonState;
327
+		} else {
328
+			// Stayed but is not pushed...
329
+			return lastButtonState;
330
+		}
302
 	}
331
 	}
303
 }
332
 }
304
 
333
 

+ 1
- 1
CubeFirmware/makefile View File

36
 
36
 
37
 # List C source files here. (C dependencies are automatically generated.)
37
 # List C source files here. (C dependencies are automatically generated.)
38
 SRC = $(TARGET).c
38
 SRC = $(TARGET).c
39
-SRC += uart.c # Additional Source-File
39
+SRC += serial.c # Additional Source-File
40
 SRC += cube.c
40
 SRC += cube.c
41
 SRC += twi.c
41
 SRC += twi.c
42
 SRC += mem.c
42
 SRC += mem.c

+ 155
- 0
CubeFirmware/serial.c View File

1
+/*
2
+ * serial.c
3
+ *
4
+ * Copyright 2011 Thomas Buck <xythobuz@me.com>
5
+ *
6
+ * This file is part of xyRobot.
7
+ *
8
+ * xyRobot is free software: you can redistribute it and/or modify
9
+ * it under the terms of the GNU General Public License as published by
10
+ * the Free Software Foundation, either version 3 of the License, or
11
+ * (at your option) any later version.
12
+ *
13
+ * xyRobot is distributed in the hope that it will be useful,
14
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
+ * GNU General Public License for more details.
17
+ *
18
+ * You should have received a copy of the GNU General Public License
19
+ * along with xyRobot.  If not, see <http://www.gnu.org/licenses/>.
20
+ */
21
+
22
+#include <avr/io.h>
23
+#include <avr/interrupt.h>
24
+#include <stdint.h>
25
+
26
+#include <serial.h>
27
+
28
+uint8_t volatile rxBuffer[RX_BUFFER_SIZE];
29
+uint8_t volatile txBuffer[TX_BUFFER_SIZE];
30
+uint16_t volatile rxRead = 0;
31
+uint16_t volatile rxWrite = 0;
32
+uint16_t volatile txRead = 0;
33
+uint16_t volatile txWrite = 0;
34
+uint8_t volatile shouldStartTransmission = 1;
35
+
36
+ISR(USART_RX_vect) { // Receive complete
37
+    rxBuffer[rxWrite] = UDR;
38
+	if (rxWrite < (RX_BUFFER_SIZE - 1)) {
39
+		rxWrite++;
40
+	} else {
41
+		rxWrite = 0;
42
+	}
43
+}
44
+
45
+ISR(USART_UDRE_vect) { // Data register empty
46
+    if (txRead != txWrite) {
47
+		UDR = txBuffer[txRead];
48
+		if (txRead < (TX_BUFFER_SIZE -1)) {
49
+			txRead++;
50
+		} else {
51
+			txRead = 0;
52
+		}
53
+	} else {
54
+		shouldStartTransmission = 1;
55
+		UCSRB &= ~(1 << UDRIE); // Disable Interrupt
56
+	}
57
+}
58
+
59
+uint8_t serialInit(uint16_t baud, uint8_t databits, uint8_t parity, uint8_t stopbits) {
60
+	if (parity > ODD) {
61
+		return 1;
62
+	}
63
+	if ((databits < 5) || (databits > 8)) {
64
+		return 1;
65
+	}
66
+	if ((stopbits < 1) || (stopbits > 2)) {
67
+		return 1;
68
+	}
69
+
70
+	if (parity != NONE) {
71
+		UCSRC |= (1 << UPM1);
72
+		if (parity == ODD) {
73
+			UCSRC |= (1 << UPM0);
74
+		}
75
+	}
76
+	if (stopbits == 2) {
77
+		UCSRC |= (1 << USBS);
78
+	}
79
+	if (databits != 5) {
80
+		if ((databits == 6) || (databits >= 8)) {
81
+			UCSRC |= (1 << UCSZ0);
82
+		}
83
+		if (databits >= 7) {
84
+			UCSRC |= (1 << UCSZ1);
85
+		}
86
+		if (databits == 9) {
87
+			UCSRB |= (1 << UCSZ2);
88
+		}
89
+	}
90
+	UBRRH = (baud >> 8);
91
+	UBRRL = baud;
92
+	UCSRB |= (1 << RXCIE) | (1 << RXEN) | (1 << TXEN); // Enable Interrupts and Receiver/Transmitter
93
+
94
+	return 0;
95
+}
96
+
97
+uint8_t serialHasChar() {
98
+	if (rxRead != rxWrite) {
99
+		return 1;
100
+	} else {
101
+		return 0;
102
+	}
103
+}
104
+
105
+uint8_t serialGet() {
106
+	uint8_t c;
107
+	if (rxRead != rxWrite) {
108
+		c = rxBuffer[rxRead];
109
+		rxBuffer[rxRead] = 0;
110
+		if (rxRead < (RX_BUFFER_SIZE - 1)) {
111
+			rxRead++;
112
+		} else {
113
+			rxRead = 0;
114
+		}
115
+		return c;
116
+	} else {
117
+		return 0;
118
+	}
119
+}
120
+
121
+uint8_t serialBufferSpaceRemaining() {
122
+	return (((txWrite + 1) == txRead) || ((txRead == 0) && ((txWrite + 1) == TX_BUFFER_SIZE)));
123
+}
124
+
125
+void serialWrite(uint8_t data) {
126
+	while (((txWrite + 1) == txRead) || ((txRead == 0) && ((txWrite + 1) == TX_BUFFER_SIZE))); // Buffer is full, wait!
127
+    txBuffer[txWrite] = data;
128
+	if (txWrite < (TX_BUFFER_SIZE - 1)) {
129
+		txWrite++;
130
+	} else {
131
+		txWrite = 0;
132
+	}
133
+	if (shouldStartTransmission == 1) {
134
+		shouldStartTransmission = 0;
135
+		UCSRB |= (1 << UDRIE); // Enable Interrupt
136
+		UCSRA |= (1 << UDRE); // Trigger Interrupt
137
+	}
138
+}
139
+
140
+void serialWriteString(char *data) {
141
+	while (*data != '\0') {
142
+		serialWrite(*data++);
143
+	}
144
+}
145
+
146
+void serialClose() {
147
+	UCSRB = 0;
148
+	UCSRC = 0;
149
+	UBRRH = 0;
150
+	UBRRL = 0;
151
+	rxRead = 0;
152
+	txRead = 0;
153
+	rxWrite = 0;
154
+	txWrite = 0;
155
+}

+ 48
- 0
CubeFirmware/serial.h View File

1
+/*
2
+ * serial.h
3
+ *
4
+ * Copyright 2011 Thomas Buck <xythobuz@me.com>
5
+ *
6
+ * This file is part of xyRobot.
7
+ *
8
+ * xyRobot is free software: you can redistribute it and/or modify
9
+ * it under the terms of the GNU General Public License as published by
10
+ * the Free Software Foundation, either version 3 of the License, or
11
+ * (at your option) any later version.
12
+ *
13
+ * xyRobot is distributed in the hope that it will be useful,
14
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
+ * GNU General Public License for more details.
17
+ *
18
+ * You should have received a copy of the GNU General Public License
19
+ * along with xyRobot.  If not, see <http://www.gnu.org/licenses/>.
20
+ */
21
+
22
+#ifndef serial_h_
23
+#define serial_h_
24
+
25
+// RX & TX buffer size in bytes
26
+#define RX_BUFFER_SIZE 32
27
+#define TX_BUFFER_SIZE 32
28
+
29
+// Select Baudrate with this macro
30
+#define UART_BAUD_SELECT(baudRate,xtalCpu) ((xtalCpu)/((baudRate)*16l)-1)
31
+
32
+#define ODD 2
33
+#define EVEN 1
34
+#define NONE 0
35
+
36
+uint8_t serialInit(uint16_t baud, uint8_t databits, uint8_t parity, uint8_t stopbits);
37
+
38
+uint8_t serialHasChar(void);
39
+uint8_t serialGet(void);
40
+
41
+// 1 if space remaining, 0 if full
42
+uint8_t serialBufferSpaceRemaining(void);
43
+void serialWrite(uint8_t data);
44
+void serialWriteString(char *data);
45
+
46
+void serialClose(void);
47
+
48
+#endif /* SERIAL_H_ */

+ 0
- 651
CubeFirmware/uart.c View File

1
-/*************************************************************************
2
-Title:    Interrupt UART library with receive/transmit circular buffers
3
-Author:   Peter Fleury <pfleury@gmx.ch>   http://jump.to/fleury
4
-File:     $Id: uart.c,v 1.6.2.2 2009/11/29 08:56:12 Peter Exp $
5
-Software: AVR-GCC 4.1, AVR Libc 1.4.6 or higher
6
-Hardware: any AVR with built-in UART, 
7
-License:  GNU General Public License 
8
-          
9
-DESCRIPTION:
10
-    An interrupt is generated when the UART has finished transmitting or
11
-    receiving a byte. The interrupt handling routines use circular buffers
12
-    for buffering received and transmitted data.
13
-    
14
-    The UART_RX_BUFFER_SIZE and UART_TX_BUFFER_SIZE variables define
15
-    the buffer size in bytes. Note that these variables must be a 
16
-    power of 2.
17
-    
18
-USAGE:
19
-    Refere to the header file uart.h for a description of the routines. 
20
-    See also example test_uart.c.
21
-
22
-NOTES:
23
-    Based on Atmel Application Note AVR306
24
-                    
25
-LICENSE:
26
-    Copyright (C) 2006 Peter Fleury
27
-
28
-    This program is free software; you can redistribute it and/or modify
29
-    it under the terms of the GNU General Public License as published by
30
-    the Free Software Foundation; either version 2 of the License, or
31
-    any later version.
32
-
33
-    This program is distributed in the hope that it will be useful,
34
-    but WITHOUT ANY WARRANTY; without even the implied warranty of
35
-    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36
-    GNU General Public License for more details.
37
-                        
38
-*************************************************************************/
39
-#include <avr/io.h>
40
-#include <avr/interrupt.h>
41
-#include <avr/pgmspace.h>
42
-#include "uart.h"
43
-
44
-
45
-/*
46
- *  constants and macros
47
- */
48
-
49
-/* size of RX/TX buffers */
50
-#define UART_RX_BUFFER_MASK ( UART_RX_BUFFER_SIZE - 1)
51
-#define UART_TX_BUFFER_MASK ( UART_TX_BUFFER_SIZE - 1)
52
-
53
-#if ( UART_RX_BUFFER_SIZE & UART_RX_BUFFER_MASK )
54
-#error RX buffer size is not a power of 2
55
-#endif
56
-#if ( UART_TX_BUFFER_SIZE & UART_TX_BUFFER_MASK )
57
-#error TX buffer size is not a power of 2
58
-#endif
59
-
60
-#if defined(__AVR_AT90S2313__) \
61
- || defined(__AVR_AT90S4414__) || defined(__AVR_AT90S4434__) \
62
- || defined(__AVR_AT90S8515__) || defined(__AVR_AT90S8535__) \
63
- || defined(__AVR_ATmega103__)
64
- /* old AVR classic or ATmega103 with one UART */
65
- #define AT90_UART
66
- #define UART0_RECEIVE_INTERRUPT   SIG_UART_RECV
67
- #define UART0_TRANSMIT_INTERRUPT  SIG_UART_DATA
68
- #define UART0_STATUS   USR
69
- #define UART0_CONTROL  UCR
70
- #define UART0_DATA     UDR  
71
- #define UART0_UDRIE    UDRIE
72
-#elif defined(__AVR_AT90S2333__) || defined(__AVR_AT90S4433__)
73
- /* old AVR classic with one UART */
74
- #define AT90_UART
75
- #define UART0_RECEIVE_INTERRUPT   SIG_UART_RECV
76
- #define UART0_TRANSMIT_INTERRUPT  SIG_UART_DATA
77
- #define UART0_STATUS   UCSRA
78
- #define UART0_CONTROL  UCSRB
79
- #define UART0_DATA     UDR 
80
- #define UART0_UDRIE    UDRIE
81
-#elif  defined(__AVR_ATmega8__)  || defined(__AVR_ATmega16__) || defined(__AVR_ATmega32__) \
82
-  || defined(__AVR_ATmega8515__) || defined(__AVR_ATmega8535__) \
83
-  || defined(__AVR_ATmega323__)
84
-  /* ATmega with one USART */
85
- #define ATMEGA_USART
86
- #define UART0_RECEIVE_INTERRUPT   SIG_UART_RECV
87
- #define UART0_TRANSMIT_INTERRUPT  SIG_UART_DATA
88
- #define UART0_STATUS   UCSRA
89
- #define UART0_CONTROL  UCSRB
90
- #define UART0_DATA     UDR
91
- #define UART0_UDRIE    UDRIE
92
-#elif defined(__AVR_ATmega163__) 
93
-  /* ATmega163 with one UART */
94
- #define ATMEGA_UART
95
- #define UART0_RECEIVE_INTERRUPT   SIG_UART_RECV
96
- #define UART0_TRANSMIT_INTERRUPT  SIG_UART_DATA
97
- #define UART0_STATUS   UCSRA
98
- #define UART0_CONTROL  UCSRB
99
- #define UART0_DATA     UDR
100
- #define UART0_UDRIE    UDRIE
101
-#elif defined(__AVR_ATmega162__) 
102
- /* ATmega with two USART */
103
- #define ATMEGA_USART0
104
- #define ATMEGA_USART1
105
- #define UART0_RECEIVE_INTERRUPT   SIG_USART0_RECV
106
- #define UART1_RECEIVE_INTERRUPT   SIG_USART1_RECV
107
- #define UART0_TRANSMIT_INTERRUPT  SIG_USART0_DATA
108
- #define UART1_TRANSMIT_INTERRUPT  SIG_USART1_DATA
109
- #define UART0_STATUS   UCSR0A
110
- #define UART0_CONTROL  UCSR0B
111
- #define UART0_DATA     UDR0
112
- #define UART0_UDRIE    UDRIE0
113
- #define UART1_STATUS   UCSR1A
114
- #define UART1_CONTROL  UCSR1B
115
- #define UART1_DATA     UDR1
116
- #define UART1_UDRIE    UDRIE1
117
-#elif defined(__AVR_ATmega64__) || defined(__AVR_ATmega128__) 
118
- /* ATmega with two USART */
119
- #define ATMEGA_USART0
120
- #define ATMEGA_USART1
121
- #define UART0_RECEIVE_INTERRUPT   SIG_UART0_RECV
122
- #define UART1_RECEIVE_INTERRUPT   SIG_UART1_RECV
123
- #define UART0_TRANSMIT_INTERRUPT  SIG_UART0_DATA
124
- #define UART1_TRANSMIT_INTERRUPT  SIG_UART1_DATA
125
- #define UART0_STATUS   UCSR0A
126
- #define UART0_CONTROL  UCSR0B
127
- #define UART0_DATA     UDR0
128
- #define UART0_UDRIE    UDRIE0
129
- #define UART1_STATUS   UCSR1A
130
- #define UART1_CONTROL  UCSR1B
131
- #define UART1_DATA     UDR1
132
- #define UART1_UDRIE    UDRIE1
133
-#elif defined(__AVR_ATmega161__)
134
- /* ATmega with UART */
135
- #error "AVR ATmega161 currently not supported by this libaray !"
136
-#elif defined(__AVR_ATmega169__) 
137
- /* ATmega with one USART */
138
- #define ATMEGA_USART
139
- #define UART0_RECEIVE_INTERRUPT   SIG_USART_RECV
140
- #define UART0_TRANSMIT_INTERRUPT  SIG_USART_DATA
141
- #define UART0_STATUS   UCSRA
142
- #define UART0_CONTROL  UCSRB
143
- #define UART0_DATA     UDR
144
- #define UART0_UDRIE    UDRIE
145
-#elif defined(__AVR_ATmega48__) ||defined(__AVR_ATmega88__) || defined(__AVR_ATmega168__) || defined(__AVR_ATmega48P__) || defined(__AVR_ATmega88P__) || defined(__AVR_ATmega168P__) || defined(__AVR_ATmega328P__)
146
- /* ATmega with one USART */
147
- #define ATMEGA_USART0
148
- #define UART0_RECEIVE_INTERRUPT   SIG_USART_RECV
149
- #define UART0_TRANSMIT_INTERRUPT  SIG_USART_DATA
150
- #define UART0_STATUS   UCSR0A
151
- #define UART0_CONTROL  UCSR0B
152
- #define UART0_DATA     UDR0
153
- #define UART0_UDRIE    UDRIE0
154
-#elif defined(__AVR_ATtiny2313__)
155
- #define ATMEGA_USART
156
- #define UART0_RECEIVE_INTERRUPT   SIG_USART0_RX 
157
- #define UART0_TRANSMIT_INTERRUPT  SIG_USART0_UDRE
158
- #define UART0_STATUS   UCSRA
159
- #define UART0_CONTROL  UCSRB
160
- #define UART0_DATA     UDR
161
- #define UART0_UDRIE    UDRIE
162
-#elif defined(__AVR_ATmega329__) ||defined(__AVR_ATmega3290__) ||\
163
-      defined(__AVR_ATmega649__) ||defined(__AVR_ATmega6490__) ||\
164
-      defined(__AVR_ATmega325__) ||defined(__AVR_ATmega3250__) ||\
165
-      defined(__AVR_ATmega645__) ||defined(__AVR_ATmega6450__)
166
-  /* ATmega with one USART */
167
-  #define ATMEGA_USART0
168
-  #define UART0_RECEIVE_INTERRUPT   SIG_UART_RECV
169
-  #define UART0_TRANSMIT_INTERRUPT  SIG_UART_DATA
170
-  #define UART0_STATUS   UCSR0A
171
-  #define UART0_CONTROL  UCSR0B
172
-  #define UART0_DATA     UDR0
173
-  #define UART0_UDRIE    UDRIE0
174
-#elif defined(__AVR_ATmega2560__) || defined(__AVR_ATmega2561__) || defined(__AVR_ATmega1280__)  || defined(__AVR_ATmega1281__) || defined(__AVR_ATmega640__)
175
-/* ATmega with two USART */
176
-  #define ATMEGA_USART0
177
-  #define ATMEGA_USART1
178
-  #define UART0_RECEIVE_INTERRUPT   SIG_USART0_RECV
179
-  #define UART1_RECEIVE_INTERRUPT   SIG_USART1_RECV
180
-  #define UART0_TRANSMIT_INTERRUPT  SIG_USART0_DATA
181
-  #define UART1_TRANSMIT_INTERRUPT  SIG_USART1_DATA
182
-  #define UART0_STATUS   UCSR0A
183
-  #define UART0_CONTROL  UCSR0B
184
-  #define UART0_DATA     UDR0
185
-  #define UART0_UDRIE    UDRIE0
186
-  #define UART1_STATUS   UCSR1A
187
-  #define UART1_CONTROL  UCSR1B
188
-  #define UART1_DATA     UDR1
189
-  #define UART1_UDRIE    UDRIE1  
190
-#elif defined(__AVR_ATmega644__)
191
- /* ATmega with one USART */
192
- #define ATMEGA_USART0
193
- #define UART0_RECEIVE_INTERRUPT   SIG_USART_RECV
194
- #define UART0_TRANSMIT_INTERRUPT  SIG_USART_DATA
195
- #define UART0_STATUS   UCSR0A
196
- #define UART0_CONTROL  UCSR0B
197
- #define UART0_DATA     UDR0
198
- #define UART0_UDRIE    UDRIE0
199
-#elif defined(__AVR_ATmega164P__) || defined(__AVR_ATmega324P__) || defined(__AVR_ATmega644P__)
200
- /* ATmega with two USART */
201
- #define ATMEGA_USART0
202
- #define ATMEGA_USART1
203
- #define UART0_RECEIVE_INTERRUPT   SIG_USART_RECV
204
- #define UART1_RECEIVE_INTERRUPT   SIG_USART1_RECV
205
- #define UART0_TRANSMIT_INTERRUPT  SIG_USART_DATA
206
- #define UART1_TRANSMIT_INTERRUPT  SIG_USART1_DATA
207
- #define UART0_STATUS   UCSR0A
208
- #define UART0_CONTROL  UCSR0B
209
- #define UART0_DATA     UDR0
210
- #define UART0_UDRIE    UDRIE0
211
- #define UART1_STATUS   UCSR1A
212
- #define UART1_CONTROL  UCSR1B
213
- #define UART1_DATA     UDR1
214
- #define UART1_UDRIE    UDRIE1
215
-#else
216
- #error "no UART definition for MCU available"
217
-#endif
218
-
219
-
220
-/*
221
- *  module global variables
222
- */
223
-static volatile unsigned char UART_TxBuf[UART_TX_BUFFER_SIZE];
224
-static volatile unsigned char UART_RxBuf[UART_RX_BUFFER_SIZE];
225
-static volatile unsigned char UART_TxHead;
226
-static volatile unsigned char UART_TxTail;
227
-static volatile unsigned char UART_RxHead;
228
-static volatile unsigned char UART_RxTail;
229
-static volatile unsigned char UART_LastRxError;
230
-
231
-#if defined( ATMEGA_USART1 )
232
-static volatile unsigned char UART1_TxBuf[UART_TX_BUFFER_SIZE];
233
-static volatile unsigned char UART1_RxBuf[UART_RX_BUFFER_SIZE];
234
-static volatile unsigned char UART1_TxHead;
235
-static volatile unsigned char UART1_TxTail;
236
-static volatile unsigned char UART1_RxHead;
237
-static volatile unsigned char UART1_RxTail;
238
-static volatile unsigned char UART1_LastRxError;
239
-#endif
240
-
241
-
242
-
243
-SIGNAL(UART0_RECEIVE_INTERRUPT)
244
-/*************************************************************************
245
-Function: UART Receive Complete interrupt
246
-Purpose:  called when the UART has received a character
247
-**************************************************************************/
248
-{
249
-    unsigned char tmphead;
250
-    unsigned char data;
251
-    unsigned char usr;
252
-    unsigned char lastRxError;
253
- 
254
- 
255
-    /* read UART status register and UART data register */ 
256
-    usr  = UART0_STATUS;
257
-    data = UART0_DATA;
258
-    
259
-    /* */
260
-#if defined( AT90_UART )
261
-    lastRxError = (usr & (_BV(FE)|_BV(DOR)) );
262
-#elif defined( ATMEGA_USART )
263
-    lastRxError = (usr & (_BV(FE)|_BV(DOR)) );
264
-#elif defined( ATMEGA_USART0 )
265
-    lastRxError = (usr & (_BV(FE0)|_BV(DOR0)) );
266
-#elif defined ( ATMEGA_UART )
267
-    lastRxError = (usr & (_BV(FE)|_BV(DOR)) );
268
-#endif
269
-        
270
-    /* calculate buffer index */ 
271
-    tmphead = ( UART_RxHead + 1) & UART_RX_BUFFER_MASK;
272
-    
273
-    if ( tmphead == UART_RxTail ) {
274
-        /* error: receive buffer overflow */
275
-        lastRxError = UART_BUFFER_OVERFLOW >> 8;
276
-    }else{
277
-        /* store new index */
278
-        UART_RxHead = tmphead;
279
-        /* store received data in buffer */
280
-        UART_RxBuf[tmphead] = data;
281
-    }
282
-    UART_LastRxError = lastRxError;   
283
-}
284
-
285
-
286
-SIGNAL(UART0_TRANSMIT_INTERRUPT)
287
-/*************************************************************************
288
-Function: UART Data Register Empty interrupt
289
-Purpose:  called when the UART is ready to transmit the next byte
290
-**************************************************************************/
291
-{
292
-    unsigned char tmptail;
293
-
294
-    
295
-    if ( UART_TxHead != UART_TxTail) {
296
-        /* calculate and store new buffer index */
297
-        tmptail = (UART_TxTail + 1) & UART_TX_BUFFER_MASK;
298
-        UART_TxTail = tmptail;
299
-        /* get one byte from buffer and write it to UART */
300
-        UART0_DATA = UART_TxBuf[tmptail];  /* start transmission */
301
-    }else{
302
-        /* tx buffer empty, disable UDRE interrupt */
303
-        UART0_CONTROL &= ~_BV(UART0_UDRIE);
304
-    }
305
-}
306
-
307
-
308
-/*************************************************************************
309
-Function: uart_init()
310
-Purpose:  initialize UART and set baudrate
311
-Input:    baudrate using macro UART_BAUD_SELECT()
312
-Returns:  none
313
-**************************************************************************/
314
-void uart_init(unsigned int baudrate)
315
-{
316
-    UART_TxHead = 0;
317
-    UART_TxTail = 0;
318
-    UART_RxHead = 0;
319
-    UART_RxTail = 0;
320
-    
321
-#if defined( AT90_UART )
322
-    /* set baud rate */
323
-    UBRR = (unsigned char)baudrate; 
324
-
325
-    /* enable UART receiver and transmmitter and receive complete interrupt */
326
-    UART0_CONTROL = _BV(RXCIE)|_BV(RXEN)|_BV(TXEN);
327
-
328
-#elif defined (ATMEGA_USART)
329
-    /* Set baud rate */
330
-    if ( baudrate & 0x8000 )
331
-    {
332
-    	 UART0_STATUS = (1<<U2X);  //Enable 2x speed 
333
-    	 baudrate &= ~0x8000;
334
-    }
335
-    UBRRH = (unsigned char)(baudrate>>8);
336
-    UBRRL = (unsigned char) baudrate;
337
-   
338
-    /* Enable USART receiver and transmitter and receive complete interrupt */
339
-    UART0_CONTROL = _BV(RXCIE)|(1<<RXEN)|(1<<TXEN);
340
-    
341
-    /* Set frame format: asynchronous, 8data, no parity, 1stop bit */
342
-    #ifdef URSEL
343
-    UCSRC = (1<<URSEL)|(3<<UCSZ0);
344
-    #else
345
-    UCSRC = (3<<UCSZ0);
346
-    #endif 
347
-    
348
-#elif defined (ATMEGA_USART0 )
349
-    /* Set baud rate */
350
-    if ( baudrate & 0x8000 ) 
351
-    {
352
-   		UART0_STATUS = (1<<U2X0);  //Enable 2x speed 
353
-   		baudrate &= ~0x8000;
354
-   	}
355
-    UBRR0H = (unsigned char)(baudrate>>8);
356
-    UBRR0L = (unsigned char) baudrate;
357
-
358
-    /* Enable USART receiver and transmitter and receive complete interrupt */
359
-    UART0_CONTROL = _BV(RXCIE0)|(1<<RXEN0)|(1<<TXEN0);
360
-    
361
-    /* Set frame format: asynchronous, 8data, no parity, 1stop bit */
362
-    #ifdef URSEL0
363
-    UCSR0C = (1<<URSEL0)|(3<<UCSZ00);
364
-    #else
365
-    UCSR0C = (3<<UCSZ00);
366
-    #endif 
367
-
368
-#elif defined ( ATMEGA_UART )
369
-    /* set baud rate */
370
-    if ( baudrate & 0x8000 ) 
371
-    {
372
-    	UART0_STATUS = (1<<U2X);  //Enable 2x speed 
373
-    	baudrate &= ~0x8000;
374
-    }
375
-    UBRRHI = (unsigned char)(baudrate>>8);
376
-    UBRR   = (unsigned char) baudrate;
377
-
378
-    /* Enable UART receiver and transmitter and receive complete interrupt */
379
-    UART0_CONTROL = _BV(RXCIE)|(1<<RXEN)|(1<<TXEN);
380
-
381
-#endif
382
-
383
-}/* uart_init */
384
-
385
-
386
-/*************************************************************************
387
-Function: uart_getc()
388
-Purpose:  return byte from ringbuffer  
389
-Returns:  lower byte:  received byte from ringbuffer
390
-          higher byte: last receive error
391
-**************************************************************************/
392
-unsigned int uart_getc(void)
393
-{    
394
-    unsigned char tmptail;
395
-    unsigned char data;
396
-
397
-
398
-    if ( UART_RxHead == UART_RxTail ) {
399
-        return UART_NO_DATA;   /* no data available */
400
-    }
401
-    
402
-    /* calculate /store buffer index */
403
-    tmptail = (UART_RxTail + 1) & UART_RX_BUFFER_MASK;
404
-    UART_RxTail = tmptail; 
405
-    
406
-    /* get data from receive buffer */
407
-    data = UART_RxBuf[tmptail];
408
-    
409
-    return (UART_LastRxError << 8) + data;
410
-
411
-}/* uart_getc */
412
-
413
-
414
-/*************************************************************************
415
-Function: uart_putc()
416
-Purpose:  write byte to ringbuffer for transmitting via UART
417
-Input:    byte to be transmitted
418
-Returns:  none          
419
-**************************************************************************/
420
-void uart_putc(unsigned char data)
421
-{
422
-    unsigned char tmphead;
423
-
424
-    
425
-    tmphead  = (UART_TxHead + 1) & UART_TX_BUFFER_MASK;
426
-    
427
-    while ( tmphead == UART_TxTail ){
428
-        ;/* wait for free space in buffer */
429
-    }
430
-    
431
-    UART_TxBuf[tmphead] = data;
432
-    UART_TxHead = tmphead;
433
-
434
-    /* enable UDRE interrupt */
435
-    UART0_CONTROL    |= _BV(UART0_UDRIE);
436
-
437
-}/* uart_putc */
438
-
439
-
440
-/*************************************************************************
441
-Function: uart_puts()
442
-Purpose:  transmit string to UART
443
-Input:    string to be transmitted
444
-Returns:  none          
445
-**************************************************************************/
446
-void uart_puts(const char *s )
447
-{
448
-    while (*s) 
449
-      uart_putc(*s++);
450
-
451
-}/* uart_puts */
452
-
453
-
454
-/*************************************************************************
455
-Function: uart_puts_p()
456
-Purpose:  transmit string from program memory to UART
457
-Input:    program memory string to be transmitted
458
-Returns:  none
459
-**************************************************************************/
460
-void uart_puts_p(const char *progmem_s )
461
-{
462
-    register char c;
463
-    
464
-    while ( (c = pgm_read_byte(progmem_s++)) ) 
465
-      uart_putc(c);
466
-
467
-}/* uart_puts_p */
468
-
469
-
470
-/*
471
- * these functions are only for ATmegas with two USART
472
- */
473
-#if defined( ATMEGA_USART1 )
474
-
475
-SIGNAL(UART1_RECEIVE_INTERRUPT)
476
-/*************************************************************************
477
-Function: UART1 Receive Complete interrupt
478
-Purpose:  called when the UART1 has received a character
479
-**************************************************************************/
480
-{
481
-    unsigned char tmphead;
482
-    unsigned char data;
483
-    unsigned char usr;
484
-    unsigned char lastRxError;
485
- 
486
- 
487
-    /* read UART status register and UART data register */ 
488
-    usr  = UART1_STATUS;
489
-    data = UART1_DATA;
490
-    
491
-    /* */
492
-    lastRxError = (usr & (_BV(FE1)|_BV(DOR1)) );
493
-        
494
-    /* calculate buffer index */ 
495
-    tmphead = ( UART1_RxHead + 1) & UART_RX_BUFFER_MASK;
496
-    
497
-    if ( tmphead == UART1_RxTail ) {
498
-        /* error: receive buffer overflow */
499
-        lastRxError = UART_BUFFER_OVERFLOW >> 8;
500
-    }else{
501
-        /* store new index */
502
-        UART1_RxHead = tmphead;
503
-        /* store received data in buffer */
504
-        UART1_RxBuf[tmphead] = data;
505
-    }
506
-    UART1_LastRxError = lastRxError;   
507
-}
508
-
509
-
510
-SIGNAL(UART1_TRANSMIT_INTERRUPT)
511
-/*************************************************************************
512
-Function: UART1 Data Register Empty interrupt
513
-Purpose:  called when the UART1 is ready to transmit the next byte
514
-**************************************************************************/
515
-{
516
-    unsigned char tmptail;
517
-
518
-    
519
-    if ( UART1_TxHead != UART1_TxTail) {
520
-        /* calculate and store new buffer index */
521
-        tmptail = (UART1_TxTail + 1) & UART_TX_BUFFER_MASK;
522
-        UART1_TxTail = tmptail;
523
-        /* get one byte from buffer and write it to UART */
524
-        UART1_DATA = UART1_TxBuf[tmptail];  /* start transmission */
525
-    }else{
526
-        /* tx buffer empty, disable UDRE interrupt */
527
-        UART1_CONTROL &= ~_BV(UART1_UDRIE);
528
-    }
529
-}
530
-
531
-
532
-/*************************************************************************
533
-Function: uart1_init()
534
-Purpose:  initialize UART1 and set baudrate
535
-Input:    baudrate using macro UART_BAUD_SELECT()
536
-Returns:  none
537
-**************************************************************************/
538
-void uart1_init(unsigned int baudrate)
539
-{
540
-    UART1_TxHead = 0;
541
-    UART1_TxTail = 0;
542
-    UART1_RxHead = 0;
543
-    UART1_RxTail = 0;
544
-    
545
-
546
-    /* Set baud rate */
547
-    if ( baudrate & 0x8000 ) 
548
-    {
549
-    	UART1_STATUS = (1<<U2X1);  //Enable 2x speed 
550
-      baudrate &= ~0x8000;
551
-    }
552
-    UBRR1H = (unsigned char)(baudrate>>8);
553
-    UBRR1L = (unsigned char) baudrate;
554
-
555
-    /* Enable USART receiver and transmitter and receive complete interrupt */
556
-    UART1_CONTROL = _BV(RXCIE1)|(1<<RXEN1)|(1<<TXEN1);
557
-    
558
-    /* Set frame format: asynchronous, 8data, no parity, 1stop bit */   
559
-    #ifdef URSEL1
560
-    UCSR1C = (1<<URSEL1)|(3<<UCSZ10);
561
-    #else
562
-    UCSR1C = (3<<UCSZ10);
563
-    #endif 
564
-}/* uart_init */
565
-
566
-
567
-/*************************************************************************
568
-Function: uart1_getc()
569
-Purpose:  return byte from ringbuffer  
570
-Returns:  lower byte:  received byte from ringbuffer
571
-          higher byte: last receive error
572
-**************************************************************************/
573
-unsigned int uart1_getc(void)
574
-{    
575
-    unsigned char tmptail;
576
-    unsigned char data;
577
-
578
-
579
-    if ( UART1_RxHead == UART1_RxTail ) {
580
-        return UART_NO_DATA;   /* no data available */
581
-    }
582
-    
583
-    /* calculate /store buffer index */
584
-    tmptail = (UART1_RxTail + 1) & UART_RX_BUFFER_MASK;
585
-    UART1_RxTail = tmptail; 
586
-    
587
-    /* get data from receive buffer */
588
-    data = UART1_RxBuf[tmptail];
589
-    
590
-    return (UART1_LastRxError << 8) + data;
591
-
592
-}/* uart1_getc */
593
-
594
-
595
-/*************************************************************************
596
-Function: uart1_putc()
597
-Purpose:  write byte to ringbuffer for transmitting via UART
598
-Input:    byte to be transmitted
599
-Returns:  none          
600
-**************************************************************************/
601
-void uart1_putc(unsigned char data)
602
-{
603
-    unsigned char tmphead;
604
-
605
-    
606
-    tmphead  = (UART1_TxHead + 1) & UART_TX_BUFFER_MASK;
607
-    
608
-    while ( tmphead == UART1_TxTail ){
609
-        ;/* wait for free space in buffer */
610
-    }
611
-    
612
-    UART1_TxBuf[tmphead] = data;
613
-    UART1_TxHead = tmphead;
614
-
615
-    /* enable UDRE interrupt */
616
-    UART1_CONTROL    |= _BV(UART1_UDRIE);
617
-
618
-}/* uart1_putc */
619
-
620
-
621
-/*************************************************************************
622
-Function: uart1_puts()
623
-Purpose:  transmit string to UART1
624
-Input:    string to be transmitted
625
-Returns:  none          
626
-**************************************************************************/
627
-void uart1_puts(const char *s )
628
-{
629
-    while (*s) 
630
-      uart1_putc(*s++);
631
-
632
-}/* uart1_puts */
633
-
634
-
635
-/*************************************************************************
636
-Function: uart1_puts_p()
637
-Purpose:  transmit string from program memory to UART1
638
-Input:    program memory string to be transmitted
639
-Returns:  none
640
-**************************************************************************/
641
-void uart1_puts_p(const char *progmem_s )
642
-{
643
-    register char c;
644
-    
645
-    while ( (c = pgm_read_byte(progmem_s++)) ) 
646
-      uart1_putc(c);
647
-
648
-}/* uart1_puts_p */
649
-
650
-
651
-#endif

+ 0
- 194
CubeFirmware/uart.h View File

1
-#ifndef UART_H
2
-#define UART_H
3
-/************************************************************************
4
-Title:    Interrupt UART library with receive/transmit circular buffers
5
-Author:   Peter Fleury <pfleury@gmx.ch>   http://jump.to/fleury
6
-File:     $Id: uart.h,v 1.8.2.1 2007/07/01 11:14:38 peter Exp $
7
-Software: AVR-GCC 4.1, AVR Libc 1.4
8
-Hardware: any AVR with built-in UART, tested on AT90S8515 & ATmega8 at 4 Mhz
9
-License:  GNU General Public License 
10
-Usage:    see Doxygen manual
11
-
12
-LICENSE:
13
-    Copyright (C) 2006 Peter Fleury
14
-
15
-    This program is free software; you can redistribute it and/or modify
16
-    it under the terms of the GNU General Public License as published by
17
-    the Free Software Foundation; either version 2 of the License, or
18
-    any later version.
19
-
20
-    This program is distributed in the hope that it will be useful,
21
-    but WITHOUT ANY WARRANTY; without even the implied warranty of
22
-    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
-    GNU General Public License for more details.
24
-    
25
-************************************************************************/
26
-
27
-/** 
28
- *  @defgroup pfleury_uart UART Library
29
- *  @code #include <uart.h> @endcode
30
- * 
31
- *  @brief Interrupt UART library using the built-in UART with transmit and receive circular buffers. 
32
- *
33
- *  This library can be used to transmit and receive data through the built in UART. 
34
- *
35
- *  An interrupt is generated when the UART has finished transmitting or
36
- *  receiving a byte. The interrupt handling routines use circular buffers
37
- *  for buffering received and transmitted data.
38
- *
39
- *  The UART_RX_BUFFER_SIZE and UART_TX_BUFFER_SIZE constants define
40
- *  the size of the circular buffers in bytes. Note that these constants must be a power of 2.
41
- *  You may need to adapt this constants to your target and your application by adding 
42
- *  CDEFS += -DUART_RX_BUFFER_SIZE=nn -DUART_RX_BUFFER_SIZE=nn to your Makefile.
43
- *
44
- *  @note Based on Atmel Application Note AVR306
45
- *  @author Peter Fleury pfleury@gmx.ch  http://jump.to/fleury
46
- */
47
- 
48
-/**@{*/
49
-
50
-
51
-#if (__GNUC__ * 100 + __GNUC_MINOR__) < 304
52
-#error "This library requires AVR-GCC 3.4 or later, update to newer AVR-GCC compiler !"
53
-#endif
54
-
55
-
56
-/*
57
-** constants and macros
58
-*/
59
-
60
-/** @brief  UART Baudrate Expression
61
- *  @param  xtalcpu  system clock in Mhz, e.g. 4000000L for 4Mhz          
62
- *  @param  baudrate baudrate in bps, e.g. 1200, 2400, 9600     
63
- */
64
-#define UART_BAUD_SELECT(baudRate,xtalCpu) ((xtalCpu)/((baudRate)*16l)-1)
65
-
66
-/** @brief  UART Baudrate Expression for ATmega double speed mode
67
- *  @param  xtalcpu  system clock in Mhz, e.g. 4000000L for 4Mhz           
68
- *  @param  baudrate baudrate in bps, e.g. 1200, 2400, 9600     
69
- */
70
-#define UART_BAUD_SELECT_DOUBLE_SPEED(baudRate,xtalCpu) (((xtalCpu)/((baudRate)*8l)-1)|0x8000)
71
-
72
-
73
-/** Size of the circular receive buffer, must be power of 2 */
74
-#ifndef UART_RX_BUFFER_SIZE
75
-#define UART_RX_BUFFER_SIZE 32
76
-#endif
77
-/** Size of the circular transmit buffer, must be power of 2 */
78
-#ifndef UART_TX_BUFFER_SIZE
79
-#define UART_TX_BUFFER_SIZE 32
80
-#endif
81
-
82
-/* test if the size of the circular buffers fits into SRAM */
83
-#if ( (UART_RX_BUFFER_SIZE+UART_TX_BUFFER_SIZE) >= (RAMEND-0x60 ) )
84
-#error "size of UART_RX_BUFFER_SIZE + UART_TX_BUFFER_SIZE larger than size of SRAM"
85
-#endif
86
-
87
-/* 
88
-** high byte error return code of uart_getc()
89
-*/
90
-#define UART_FRAME_ERROR      0x0800              /* Framing Error by UART       */
91
-#define UART_OVERRUN_ERROR    0x0400              /* Overrun condition by UART   */
92
-#define UART_BUFFER_OVERFLOW  0x0200              /* receive ringbuffer overflow */
93
-#define UART_NO_DATA          0x0100              /* no receive data available   */
94
-
95
-
96
-/*
97
-** function prototypes
98
-*/
99
-
100
-/**
101
-   @brief   Initialize UART and set baudrate 
102
-   @param   baudrate Specify baudrate using macro UART_BAUD_SELECT()
103
-   @return  none
104
-*/
105
-extern void uart_init(unsigned int baudrate);
106
-
107
-
108
-/**
109
- *  @brief   Get received byte from ringbuffer
110
- *
111
- * Returns in the lower byte the received character and in the 
112
- * higher byte the last receive error.
113
- * UART_NO_DATA is returned when no data is available.
114
- *
115
- *  @param   void
116
- *  @return  lower byte:  received byte from ringbuffer
117
- *  @return  higher byte: last receive status
118
- *           - \b 0 successfully received data from UART
119
- *           - \b UART_NO_DATA           
120
- *             <br>no receive data available
121
- *           - \b UART_BUFFER_OVERFLOW   
122
- *             <br>Receive ringbuffer overflow.
123
- *             We are not reading the receive buffer fast enough, 
124
- *             one or more received character have been dropped 
125
- *           - \b UART_OVERRUN_ERROR     
126
- *             <br>Overrun condition by UART.
127
- *             A character already present in the UART UDR register was 
128
- *             not read by the interrupt handler before the next character arrived,
129
- *             one or more received characters have been dropped.
130
- *           - \b UART_FRAME_ERROR       
131
- *             <br>Framing Error by UART
132
- */
133
-extern unsigned int uart_getc(void);
134
-
135
-
136
-/**
137
- *  @brief   Put byte to ringbuffer for transmitting via UART
138
- *  @param   data byte to be transmitted
139
- *  @return  none
140
- */
141
-extern void uart_putc(unsigned char data);
142
-
143
-
144
-/**
145
- *  @brief   Put string to ringbuffer for transmitting via UART
146
- *
147
- *  The string is buffered by the uart library in a circular buffer
148
- *  and one character at a time is transmitted to the UART using interrupts.
149
- *  Blocks if it can not write the whole string into the circular buffer.
150
- * 
151
- *  @param   s string to be transmitted
152
- *  @return  none
153
- */
154
-extern void uart_puts(const char *s );
155
-
156
-
157
-/**
158
- * @brief    Put string from program memory to ringbuffer for transmitting via UART.
159
- *
160
- * The string is buffered by the uart library in a circular buffer
161
- * and one character at a time is transmitted to the UART using interrupts.
162
- * Blocks if it can not write the whole string into the circular buffer.
163
- *
164
- * @param    s program memory string to be transmitted
165
- * @return   none
166
- * @see      uart_puts_P
167
- */
168
-extern void uart_puts_p(const char *s );
169
-
170
-/**
171
- * @brief    Macro to automatically put a string constant into program memory
172
- */
173
-#define uart_puts_P(__s)       uart_puts_p(PSTR(__s))
174
-
175
-
176
-
177
-/** @brief  Initialize USART1 (only available on selected ATmegas) @see uart_init */
178
-extern void uart1_init(unsigned int baudrate);
179
-/** @brief  Get received byte of USART1 from ringbuffer. (only available on selected ATmega) @see uart_getc */
180
-extern unsigned int uart1_getc(void);
181
-/** @brief  Put byte to ringbuffer for transmitting via USART1 (only available on selected ATmega) @see uart_putc */
182
-extern void uart1_putc(unsigned char data);
183
-/** @brief  Put string to ringbuffer for transmitting via USART1 (only available on selected ATmega) @see uart_puts */
184
-extern void uart1_puts(const char *s );
185
-/** @brief  Put string from program memory to ringbuffer for transmitting via USART1 (only available on selected ATmega) @see uart_puts_p */
186
-extern void uart1_puts_p(const char *s );
187
-/** @brief  Macro to automatically put a string constant into program memory */
188
-#define uart1_puts_P(__s)       uart1_puts_p(PSTR(__s))
189
-
190
-/**@}*/
191
-
192
-
193
-#endif // UART_H 
194
-

+ 2
- 2
README.md View File

8
 
8
 
9
 ## Firmware
9
 ## Firmware
10
 
10
 
11
-The firmware of the Controller is yet to be developed...
12
-It will use [Peter Fleury's UART and TWI Library](http://homepage.hispeed.ch/peterfleury/avr-software.html).
11
+The firmware of the Controller is yet to be tested...
12
+It uses [Peter Fleury's TWI Library](http://homepage.hispeed.ch/peterfleury/avr-software.html).
13
 
13
 
14
 ## Cube Control
14
 ## Cube Control
15
 
15
 

Loading…
Cancel
Save