Browse Source

first commit

Thomas Buck 12 years ago
commit
f152bc4cb2
8 changed files with 1409 additions and 0 deletions
  1. BIN
      LED Cube.sch
  2. 3
    0
      README.md
  3. 77
    0
      cube.c
  4. 13
    0
      cube.h
  5. 51
    0
      main.c
  6. 420
    0
      makefile
  7. 651
    0
      uart.c
  8. 194
    0
      uart.h

BIN
LED Cube.sch View File


+ 3
- 0
README.md View File

@@ -0,0 +1,3 @@
1
+# 8x8x8 LED Cube
2
+
3
+8x8x8 LED Cube built with an AtMega32.

+ 77
- 0
cube.c View File

@@ -0,0 +1,77 @@
1
+#include <avr/io.h>
2
+#include <avr/interrupt.h>
3
+#include <stdlib.h>
4
+
5
+#include "cube.h"
6
+
7
+#ifndef F_CPU
8
+#define F_CPU 16000000L
9
+#endif
10
+
11
+volatile uint8_t _isrCounter = 0;
12
+volatile uint8_t **imgBuffer = NULL; // imgBuffer[8][8]
13
+volatile uint8_t imgFlag = 0;
14
+
15
+// Wir zählen 21 mal bis 3968
16
+ISR(TIMER1_COMPA_vect) {
17
+	if (_isrCounter < 20) {
18
+		_isrCounter++;
19
+	} else {
20
+		_isrCounter = 0;
21
+		isrCall();
22
+	}
23
+}
24
+
25
+inline void setFet(uint8_t data) {
26
+	data &= ~((1 << 1) | 1);
27
+	PORTD = data;
28
+	data &= ~(3);
29
+	data = data << 3;
30
+	PORTB |= data;
31
+}
32
+
33
+
34
+inline void selectLatch(uint8_t latchNr) {
35
+	PORTC = 0;
36
+	if (latchNr < 8) {
37
+		PORTC = 1 << latchNr;
38
+	}
39
+}
40
+
41
+inline void setLatch(uint8_t latchNr, uint8_t data) {
42
+	setFet(0); // All LEDs off
43
+	selectLatch(latchNr); // Activate current latch
44
+	PORTA = data; // Put latch data
45
+	delay_ns(LATCHDELAY); // Wait for latch
46
+	selectLatch(8); // Deactivate all latches
47
+	setFet(1 << latchNr); // Activate current plane
48
+}
49
+
50
+inline void isrCall(void) {
51
+	static uint8_t layer = 0;
52
+	uint8_t latchCtr = 0;
53
+	
54
+	for (; latchCtr < 8; latchCtr++) {
55
+		setLatch(latchCtr, imgBuffer[layer][latchCtr]); // Put out all the data
56
+	}
57
+	
58
+	// Select next layer
59
+	if (layer < 7) {
60
+		layer++;
61
+	} else {
62
+		layer = 0;
63
+	}
64
+}
65
+
66
+inline void delay_ns(uint16_t ns) {
67
+	// minimum 63 nanoseconds (= 1 tick)
68
+	uint16_t i = ns;
69
+	if (ns != 0) {
70
+		if (ns < 63) {
71
+			ns = 63;
72
+		}
73
+		for (; i > 0; i -= 63) {
74
+			asm volatile("nop"::);
75
+		}
76
+	}
77
+}

+ 13
- 0
cube.h View File

@@ -0,0 +1,13 @@
1
+#include <stdint.h>
2
+
3
+// Time one latch is active in ns
4
+#define LATCHDELAY 63
5
+
6
+extern volatile uint8_t **imgBuffer; // imgBuffer[8][8]
7
+extern volatile uint8_t imgFlag;
8
+
9
+extern void setFet(uint8_t data);
10
+extern void selectLatch(uint8_t latchNr);
11
+extern void setLatch(uint8_t latchNr, uint8_t data);
12
+extern void isrCall(void);
13
+extern void delay_ns(uint16_t ns);

+ 51
- 0
main.c View File

@@ -0,0 +1,51 @@
1
+#include <stdlib.h>
2
+#include <stdint.h>
3
+#include <avr/io.h>
4
+#include <avr/interrupt.h>
5
+#include <util/delay.h>
6
+#include "uart.h"
7
+#include "cube.h"
8
+
9
+#ifndef F_CPU
10
+#define F_CPU 16000000L
11
+#endif
12
+
13
+void init(void) {
14
+	uint8_t ctr = 0;
15
+	DDRD = 0xFF; // Mosfets as Output
16
+	DDRB = 0xFF;
17
+	DDRC = 0xFF; // Latch Enable
18
+	DDRA = 0xFF; // Latch Data
19
+	
20
+	uart_init(UART_BAUD_SELECT(19200, 16000000L));
21
+
22
+	TCCR1A |= (1 << WGM12); // CTC Mode
23
+	TCCR1B |= (1 << CS10); // No prescaler
24
+	OCR1A = 3968;
25
+	TIMSK = (1 << OCIE1A); // Enable Overflow Interrupt
26
+
27
+	imgBuffer = malloc(8 * sizeof(uint8_t*));
28
+	if (imgBuffer == NULL) {
29
+		// TO-DO:
30
+		// error!
31
+	}
32
+	for(ctr = 0; ctr < 8; ctr++) {
33
+		imgBuffer[ctr] = malloc(8 * sizeof(uint8_t));
34
+		if (imgBuffer[ctr] == NULL) {
35
+			// TO-DO:
36
+			// error!
37
+		}
38
+	}
39
+
40
+	sei(); // Enable Interrupts
41
+}
42
+
43
+int main(void) {
44
+	
45
+	init();
46
+
47
+	while (1) {
48
+		
49
+	}
50
+	return 0;
51
+}

+ 420
- 0
makefile View File

@@ -0,0 +1,420 @@
1
+# Makefile
2
+#
3
+# On command line:
4
+#
5
+# make all = Make software.
6
+#
7
+# make clean = Clean out built project files.
8
+#
9
+# make coff = Convert ELF to AVR COFF (for use with AVR Studio 3.x or VMLAB).
10
+#
11
+# make extcoff = Convert ELF to AVR Extended COFF (for use with AVR Studio
12
+#                4.07 or greater).
13
+#
14
+# make program = Download the hex file to the device, using avrdude.  Please
15
+#                customize the avrdude settings below first!
16
+#
17
+# make filename.s = Just compile filename.c into the assembler code only
18
+#
19
+# To rebuild project do "make clean" then "make all".
20
+#
21
+
22
+
23
+# MCU name
24
+MCU = atmega32
25
+
26
+# Main Oscillator Frequency
27
+# This is only used to define F_OSC in all assembler and c-sources.
28
+F_CPU = 16000000
29
+
30
+# Output format. (can be srec, ihex, binary)
31
+FORMAT = ihex
32
+
33
+# Target file name (without extension).
34
+TARGET = main
35
+
36
+
37
+# List C source files here. (C dependencies are automatically generated.)
38
+SRC = $(TARGET).c
39
+SRC += uart.c # Additional Source-File
40
+SRC += cube.c
41
+
42
+# List Assembler source files here.
43
+# Make them always end in a capital .S.  Files ending in a lowercase .s
44
+# will not be considered source files but generated files (assembler
45
+# output from the compiler), and will be deleted upon "make clean"!
46
+# Even though the DOS/Win* filesystem matches both .s and .S the same,
47
+# it will preserve the spelling of the filenames, and gcc itself does
48
+# care about how the name is spelled on its command-line.
49
+ASRC = 
50
+
51
+
52
+
53
+# Optimization level, can be [0, 1, 2, 3, s]. 
54
+# 0 = turn off optimization. s = optimize for size.
55
+# (Note: 3 is not always the best optimization level. See avr-libc FAQ.)
56
+OPT = s
57
+
58
+# Debugging format.
59
+# Native formats for AVR-GCC's -g are stabs [default], or dwarf-2.
60
+# AVR (extended) COFF requires stabs, plus an avr-objcopy run.
61
+#DEBUG = stabs
62
+DEBUG = dwarf-2
63
+
64
+# List any extra directories to look for include files here.
65
+#     Each directory must be seperated by a space.
66
+EXTRAINCDIRS = 
67
+
68
+
69
+# Compiler flag to set the C Standard level.
70
+# c89   - "ANSI" C
71
+# gnu89 - c89 plus GCC extensions
72
+# c99   - ISO C99 standard (not yet fully implemented)
73
+# gnu99 - c99 plus GCC extensions
74
+CSTANDARD = -std=gnu99
75
+
76
+# Place -D or -U options here
77
+CDEFS =
78
+
79
+# Place -I options here
80
+CINCS =
81
+
82
+
83
+# Compiler flags.
84
+#  -g*:          generate debugging information
85
+#  -O*:          optimization level
86
+#  -f...:        tuning, see GCC manual and avr-libc documentation
87
+#  -Wall...:     warning level
88
+#  -Wa,...:      tell GCC to pass this to the assembler.
89
+#    -adhlns...: create assembler listing
90
+CFLAGS = -g$(DEBUG)
91
+CFLAGS += $(CDEFS) $(CINCS)
92
+CFLAGS += -O$(OPT)
93
+CFLAGS += -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums
94
+CFLAGS += -Wall -Wstrict-prototypes
95
+CFLAGS += -Wa,-adhlns=$(<:.c=.lst)
96
+CFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
97
+CFLAGS += $(CSTANDARD)
98
+CFLAGS += -DF_CPU=$(F_CPU)
99
+
100
+
101
+
102
+# Assembler flags.
103
+#  -Wa,...:   tell GCC to pass this to the assembler.
104
+#  -ahlms:    create listing
105
+#  -gstabs:   have the assembler create line number information; note that
106
+#             for use in COFF files, additional information about filenames
107
+#             and function names needs to be present in the assembler source
108
+#             files -- see avr-libc docs [FIXME: not yet described there]
109
+ASFLAGS = -Wa,-adhlns=$(<:.S=.lst),-gstabs 
110
+ASFLAGS += -DF_CPU=$(F_CPU)
111
+
112
+
113
+#Additional libraries.
114
+
115
+# Minimalistic printf version
116
+PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min
117
+
118
+# Floating point printf version (requires MATH_LIB = -lm below)
119
+PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt
120
+
121
+PRINTF_LIB = 
122
+
123
+# Minimalistic scanf version
124
+SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min
125
+
126
+# Floating point + %[ scanf version (requires MATH_LIB = -lm below)
127
+SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt
128
+
129
+SCANF_LIB = 
130
+
131
+MATH_LIB = -lm
132
+
133
+# External memory options
134
+
135
+# 64 KB of external RAM, starting after internal RAM (ATmega128!),
136
+# used for variables (.data/.bss) and heap (malloc()).
137
+#EXTMEMOPTS = -Wl,-Tdata=0x801100,--defsym=__heap_end=0x80ffff
138
+
139
+# 64 KB of external RAM, starting after internal RAM (ATmega128!),
140
+# only used for heap (malloc()).
141
+#EXTMEMOPTS = -Wl,--defsym=__heap_start=0x801100,--defsym=__heap_end=0x80ffff
142
+
143
+EXTMEMOPTS =
144
+
145
+# Linker flags.
146
+#  -Wl,...:     tell GCC to pass this to linker.
147
+#    -Map:      create map file
148
+#    --cref:    add cross reference to  map file
149
+LDFLAGS = -Wl,-Map=$(TARGET).map,--cref
150
+LDFLAGS += $(EXTMEMOPTS)
151
+LDFLAGS += $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB)
152
+
153
+
154
+
155
+
156
+# Programming support using avrdude. Settings and variables.
157
+
158
+# Programming hardware: alf avr910 avrisp bascom bsd 
159
+# dt006 pavr picoweb pony-stk200 sp12 stk200 stk500
160
+#
161
+# Type: avrdude -c ?
162
+# to get a full listing.
163
+#
164
+AVRDUDE_PROGRAMMER = stk500
165
+
166
+# com1 = serial port. Use lpt1 to connect to parallel port.
167
+AVRDUDE_PORT = com1    # programmer connected to serial device
168
+
169
+AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).hex
170
+#AVRDUDE_WRITE_EEPROM = -U eeprom:w:$(TARGET).eep
171
+
172
+
173
+# Uncomment the following if you want avrdude's erase cycle counter.
174
+# Note that this counter needs to be initialized first using -Yn,
175
+# see avrdude manual.
176
+#AVRDUDE_ERASE_COUNTER = -y
177
+
178
+# Uncomment the following if you do /not/ wish a verification to be
179
+# performed after programming the device.
180
+#AVRDUDE_NO_VERIFY = -V
181
+
182
+# Increase verbosity level.  Please use this when submitting bug
183
+# reports about avrdude. See <http://savannah.nongnu.org/projects/avrdude> 
184
+# to submit bug reports.
185
+#AVRDUDE_VERBOSE = -v -v
186
+
187
+AVRDUDE_FLAGS = -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER)
188
+AVRDUDE_FLAGS += $(AVRDUDE_NO_VERIFY)
189
+AVRDUDE_FLAGS += $(AVRDUDE_VERBOSE)
190
+AVRDUDE_FLAGS += $(AVRDUDE_ERASE_COUNTER)
191
+
192
+
193
+
194
+# ---------------------------------------------------------------------------
195
+
196
+# Define directories, if needed.
197
+DIRAVR = /usr/local/CrossPack-AVR
198
+DIRAVRBIN = $(DIRAVR)/bin
199
+DIRAVRUTILS = $(DIRAVR)/bin
200
+DIRINC = .
201
+DIRLIB = $(DIRAVR)/lib
202
+
203
+
204
+# Define programs and commands.
205
+SHELL = bash
206
+CC = avr-gcc
207
+OBJCOPY = avr-objcopy
208
+OBJDUMP = avr-objdump
209
+SIZE = avr-size
210
+NM = avr-nm
211
+AVRDUDE = avrdude
212
+REMOVE = rm -f
213
+COPY = cp
214
+
215
+
216
+
217
+
218
+# Define Messages
219
+# English
220
+MSG_ERRORS_NONE = Errors: none
221
+MSG_BEGIN = -------- begin --------
222
+MSG_END = --------  end  --------
223
+MSG_SIZE_BEFORE = Size before: 
224
+MSG_SIZE_AFTER = Size after:
225
+MSG_COFF = Converting to AVR COFF:
226
+MSG_EXTENDED_COFF = Converting to AVR Extended COFF:
227
+MSG_FLASH = Creating load file for Flash:
228
+MSG_EEPROM = Creating load file for EEPROM:
229
+MSG_EXTENDED_LISTING = Creating Extended Listing:
230
+MSG_SYMBOL_TABLE = Creating Symbol Table:
231
+MSG_LINKING = Linking:
232
+MSG_COMPILING = Compiling:
233
+MSG_ASSEMBLING = Assembling:
234
+MSG_CLEANING = Cleaning project:
235
+
236
+
237
+
238
+
239
+# Define all object files.
240
+OBJ = $(SRC:.c=.o) $(ASRC:.S=.o) 
241
+
242
+# Define all listing files.
243
+LST = $(ASRC:.S=.lst) $(SRC:.c=.lst)
244
+
245
+
246
+# Compiler flags to generate dependency files.
247
+### GENDEPFLAGS = -Wp,-M,-MP,-MT,$(*F).o,-MF,.dep/$(@F).d
248
+GENDEPFLAGS = -MD -MP -MF .dep/$(@F).d
249
+
250
+# Combine all necessary flags and optional flags.
251
+# Add target processor to flags.
252
+ALL_CFLAGS = -mmcu=$(MCU) -I. $(CFLAGS) $(GENDEPFLAGS)
253
+ALL_ASFLAGS = -mmcu=$(MCU) -I. -x assembler-with-cpp $(ASFLAGS)
254
+
255
+
256
+
257
+
258
+
259
+# Default target.
260
+all: begin gccversion sizebefore build sizeafter finished end
261
+
262
+build: elf hex eep lss sym
263
+
264
+elf: $(TARGET).elf
265
+hex: $(TARGET).hex
266
+eep: $(TARGET).eep
267
+lss: $(TARGET).lss 
268
+sym: $(TARGET).sym
269
+
270
+
271
+
272
+# Eye candy.
273
+# AVR Studio 3.x does not check make's exit code but relies on
274
+# the following magic strings to be generated by the compile job.
275
+begin:
276
+	@echo
277
+	@echo $(MSG_BEGIN)
278
+
279
+finished:
280
+	@echo $(MSG_ERRORS_NONE)
281
+
282
+end:
283
+	@echo $(MSG_END)
284
+	@echo
285
+
286
+
287
+# Display size of file.
288
+HEXSIZE = $(SIZE) --target=$(FORMAT) $(TARGET).hex
289
+ELFSIZE = $(SIZE) -A $(TARGET).elf
290
+sizebefore:
291
+	@if [ -f $(TARGET).elf ]; then echo; echo $(MSG_SIZE_BEFORE); $(ELFSIZE); echo; fi
292
+
293
+sizeafter:
294
+	@if [ -f $(TARGET).elf ]; then echo; echo $(MSG_SIZE_AFTER); $(ELFSIZE); echo; fi
295
+
296
+
297
+
298
+# Display compiler version information.
299
+gccversion : 
300
+	@$(CC) --version
301
+
302
+
303
+
304
+# Program the device.  
305
+program: $(TARGET).hex $(TARGET).eep
306
+	$(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) $(AVRDUDE_WRITE_EEPROM)
307
+
308
+
309
+
310
+
311
+# Convert ELF to COFF for use in debugging / simulating in AVR Studio or VMLAB.
312
+COFFCONVERT=$(OBJCOPY) --debugging \
313
+--change-section-address .data-0x800000 \
314
+--change-section-address .bss-0x800000 \
315
+--change-section-address .noinit-0x800000 \
316
+--change-section-address .eeprom-0x810000 
317
+
318
+
319
+coff: $(TARGET).elf
320
+	@echo
321
+	@echo $(MSG_COFF) $(TARGET).cof
322
+	$(COFFCONVERT) -O coff-avr $< $(TARGET).cof
323
+
324
+
325
+extcoff: $(TARGET).elf
326
+	@echo
327
+	@echo $(MSG_EXTENDED_COFF) $(TARGET).cof
328
+	$(COFFCONVERT) -O coff-ext-avr $< $(TARGET).cof
329
+
330
+
331
+
332
+# Create final output files (.hex, .eep) from ELF output file.
333
+%.hex: %.elf
334
+	@echo
335
+	@echo $(MSG_FLASH) $@
336
+	$(OBJCOPY) -O $(FORMAT) -R .eeprom $< $@
337
+
338
+%.eep: %.elf
339
+	@echo
340
+	@echo $(MSG_EEPROM) $@
341
+	-$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \
342
+	--change-section-lma .eeprom=0 -O $(FORMAT) $< $@
343
+
344
+# Create extended listing file from ELF output file.
345
+%.lss: %.elf
346
+	@echo
347
+	@echo $(MSG_EXTENDED_LISTING) $@
348
+	$(OBJDUMP) -h -S $< > $@
349
+
350
+# Create a symbol table from ELF output file.
351
+%.sym: %.elf
352
+	@echo
353
+	@echo $(MSG_SYMBOL_TABLE) $@
354
+	$(NM) -n $< > $@
355
+
356
+
357
+
358
+# Link: create ELF output file from object files.
359
+.SECONDARY : $(TARGET).elf
360
+.PRECIOUS : $(OBJ)
361
+%.elf: $(OBJ)
362
+	@echo
363
+	@echo $(MSG_LINKING) $@
364
+	$(CC) $(ALL_CFLAGS) $(OBJ) --output $@ $(LDFLAGS)
365
+
366
+
367
+# Compile: create object files from C source files.
368
+%.o : %.c
369
+	@echo
370
+	@echo $(MSG_COMPILING) $<
371
+	$(CC) -c $(ALL_CFLAGS) $< -o $@ 
372
+
373
+
374
+# Compile: create assembler files from C source files.
375
+%.s : %.c
376
+	$(CC) -S $(ALL_CFLAGS) $< -o $@
377
+
378
+
379
+# Assemble: create object files from assembler source files.
380
+%.o : %.S
381
+	@echo
382
+	@echo $(MSG_ASSEMBLING) $<
383
+	$(CC) -c $(ALL_ASFLAGS) $< -o $@
384
+
385
+
386
+
387
+# Target: clean project.
388
+clean: begin clean_list finished end
389
+
390
+clean_list :
391
+	@echo
392
+	@echo $(MSG_CLEANING)
393
+	$(REMOVE) $(TARGET).hex
394
+	$(REMOVE) $(TARGET).eep
395
+	$(REMOVE) $(TARGET).obj
396
+	$(REMOVE) $(TARGET).cof
397
+	$(REMOVE) $(TARGET).elf
398
+	$(REMOVE) $(TARGET).map
399
+	$(REMOVE) $(TARGET).obj
400
+	$(REMOVE) $(TARGET).a90
401
+	$(REMOVE) $(TARGET).sym
402
+	$(REMOVE) $(TARGET).lnk
403
+	$(REMOVE) $(TARGET).lss
404
+	$(REMOVE) $(OBJ)
405
+	$(REMOVE) $(LST)
406
+	$(REMOVE) $(SRC:.c=.s)
407
+	$(REMOVE) $(SRC:.c=.d)
408
+	$(REMOVE) .dep/*
409
+
410
+
411
+
412
+# Include the dependency files.
413
+-include $(shell mkdir .dep 2>/dev/null) $(wildcard .dep/*)
414
+
415
+
416
+# Listing of phony targets.
417
+.PHONY : all begin finish end sizebefore sizeafter gccversion \
418
+build elf hex eep lss sym coff extcoff \
419
+clean clean_list program
420
+

+ 651
- 0
uart.c View File

@@ -0,0 +1,651 @@
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

+ 194
- 0
uart.h View File

@@ -0,0 +1,194 @@
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
+

Loading…
Cancel
Save