Project

General

Profile

Revision 749

added another function pointer to encoders which gets called after all bytes are recv'd.
working on a behavior to drive in a straight line, but encoders are giving invalid on robot 5

View differences:

branches/encoders/code/behaviors/encoder_test/main.c
1
#include <dragonfly_lib.h>
2
#define usb_puti(x) x
3

  
4
int main(void)
5
{
6
	dragonfly_init(ALL_ON);
7

  
8
	encoders_init();
9
	//spi_init(usb_putc);
10
	//usb_puts("Init done.\n\r");
11
	move(130,0);
12
	//while(1){spi_transfer(5);}
13
	
14
	while(1){
15
		usb_puti(encoder_get_dx(LEFT));
16
//		usb_puts(" ");
17
		usb_puti(encoder_get_dx(RIGHT));
18
	//	usb_puts("\r\n");
19
	}
20
	
21

  
22
	
23
}
24

  
branches/encoders/code/behaviors/encoder_test/Makefile
1
########Update This Section########
2
#
3
#
4

  
5
# Relative path to the root directory (containing lib directory)
6
ifndef COLONYROOT
7
COLONYROOT = ../../..
8
endif
9

  
10
# Target file name (without extension).
11
TARGET = template
12

  
13
# Uncomment this to use the wireless library
14
# USE_WIRELESS = 1
15

  
16
# com1 = serial port. Use lpt1 to connect to parallel port.
17
AVRDUDE_PORT = com4
18
#
19
###################################
20

  
21
# Hey Emacs, this is a -*- makefile -*-
22
#----------------------------------------------------------------------------
23
# WinAVR Makefile Template written by Eric B. Weddington, J?rg Wunsch, et al.
24
#
25
# Released to the Public Domain
26
#
27
# Additional material for this makefile was written by:
28
# Peter Fleury
29
# Tim Henigan
30
# Colin O'Flynn
31
# Reiner Patommel
32
# Markus Pfaff
33
# Sander Pool
34
# Frederik Rouleau
35
#
36
#----------------------------------------------------------------------------
37
# On command line:
38
#
39
# make all = Make software.
40
#
41
# make clean = Clean out built project files.
42
#
43
# make coff = Convert ELF to AVR COFF.
44
#
45
# make extcoff = Convert ELF to AVR Extended COFF.
46
#
47
# make program = Download the hex file to the device, using avrdude.
48
#                Please customize the avrdude settings below first!
49
#
50
# make debug = Start either simulavr or avarice as specified for debugging, 
51
#              with avr-gdb or avr-insight as the front end for debugging.
52
#
53
# make filename.s = Just compile filename.c into the assembler code only.
54
#
55
# make filename.i = Create a preprocessed source file for use in submitting
56
#                   bug reports to the GCC project.
57
#
58
# To rebuild project do "make clean" then "make all".
59
#----------------------------------------------------------------------------
60

  
61
#if you want your code to work on the Firefly++ and not Firefly+
62
#then add the -DFFPP line to CDEFS
63
CDEFS = 
64
#-DFFPP
65

  
66
# MCU name
67
MCU = atmega128
68

  
69
# Processor frequency.
70
#     This will define a symbol, F_CPU, in all source code files equal to the 
71
#     processor frequency. You can then use this symbol in your source code to 
72
#     calculate timings. Do NOT tack on a 'UL' at the end, this will be done
73
#     automatically to create a 32-bit value in your source code.
74
F_CPU = 8000000
75

  
76
# Output format. (can be srec, ihex, binary)
77
FORMAT = ihex
78

  
79
# List C source files here. (C dependencies are automatically generated.)
80
SRC = $(wildcard *.c)
81

  
82
# List Assembler source files here.
83
#     Make them always end in a capital .S.  Files ending in a lowercase .s
84
#     will not be considered source files but generated files (assembler
85
#     output from the compiler), and will be deleted upon "make clean"!
86
#     Even though the DOS/Win* filesystem matches both .s and .S the same,
87
#     it will preserve the spelling of the filenames, and gcc itself does
88
#     care about how the name is spelled on its command-line.
89
ASRC = 
90

  
91
# Optimization level, can be [0, 1, 2, 3, s]. 
92
#     0 = turn off optimization. s = optimize for size.
93
#     (Note: 3 is not always the best optimization level. See avr-libc FAQ.)
94
OPT = s
95

  
96
# Debugging format.
97
#     Native formats for AVR-GCC's -g are dwarf-2 [default] or stabs.
98
#     AVR Studio 4.10 requires dwarf-2.
99
#     AVR [Extended] COFF format requires stabs, plus an avr-objcopy run.
100
DEBUG =
101

  
102
# Compiler flag to set the C Standard level.
103
#     c89   = "ANSI" C
104
#     gnu89 = c89 plus GCC extensions
105
#     c99   = ISO C99 standard (not yet fully implemented)
106
#     gnu99 = c99 plus GCC extensions
107
CSTANDARD = -std=gnu99
108

  
109
# Place -D or -U options here
110
CDEFS += -DF_CPU=$(F_CPU)UL 
111
CDEFS += -DFFP
112
# for wireless library
113
ifdef USE_WIRELESS
114
	CDEFS += -DROBOT
115
endif
116

  
117
# Place -I, -L options here
118
CINCS = -I$(COLONYROOT)/code/lib/include/libdragonfly
119
CINCS += -L$(COLONYROOT)/code/lib/bin
120
ifdef USE_WIRELESS
121
	CINCS += -I$(COLONYROOT)/code/lib/include/libwireless
122
endif
123
#---------------- Compiler Options ----------------
124
#  -g*:          generate debugging information
125
#  -O*:          optimization level
126
#  -f...:        tuning, see GCC manual and avr-libc documentation
127
#  -Wall...:     warning level
128
#  -Wa,...:      tell GCC to pass this to the assembler.
129
#    -adhlns...: create assembler listing
130
CFLAGS =
131
# CFLAGS = -g$(DEBUG)
132
CFLAGS += $(CDEFS) $(CINCS)
133
CFLAGS += -O$(OPT)
134
CFLAGS += -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums
135
CFLAGS += -Wall -Wstrict-prototypes
136
CFLAGS += -Wa,-adhlns=$(<:.c=.lst)
137
CFLAGS += $(CSTANDARD)
138

  
139
#---------------- Assembler Options ----------------
140
#  -Wa,...:   tell GCC to pass this to the assembler.
141
#  -ahlms:    create listing
142
#  -gstabs:   have the assembler create line number information; note that
143
#             for use in COFF files, additional information about filenames
144
#             and function names needs to be present in the assembler source
145
#             files -- see avr-libc docs [FIXME: not yet described there]
146
ASFLAGS = -Wa,-adhlns=$(<:.S=.lst),-gstabs 
147

  
148

  
149
#---------------- Library Options ----------------
150
# Minimalistic printf version
151
PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min
152

  
153
# Floating point printf version (requires MATH_LIB = -lm below)
154
PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt
155

  
156
# If this is left blank, then it will use the Standard printf version.
157
PRINTF_LIB = 
158
#PRINTF_LIB = $(PRINTF_LIB_MIN)
159
#PRINTF_LIB = $(PRINTF_LIB_FLOAT)
160

  
161

  
162
# Minimalistic scanf version
163
SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min
164

  
165
# Floating point + %[ scanf version (requires MATH_LIB = -lm below)
166
SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt
167

  
168
# If this is left blank, then it will use the Standard scanf version.
169
SCANF_LIB = 
170
#SCANF_LIB = $(SCANF_LIB_MIN)
171
#SCANF_LIB = $(SCANF_LIB_FLOAT)
172

  
173
MATH_LIB = -lm
174

  
175
#---------------- External Memory Options ----------------
176

  
177
# 64 KB of external RAM, starting after internal RAM (ATmega128!),
178
# used for variables (.data/.bss) and heap (malloc()).
179
#EXTMEMOPTS = -Wl,-Tdata=0x801100,--defsym=__heap_end=0x80ffff
180

  
181
# 64 KB of external RAM, starting after internal RAM (ATmega128!),
182
# only used for heap (malloc()).
183
#EXTMEMOPTS = -Wl,--defsym=__heap_start=0x801100,--defsym=__heap_end=0x80ffff
184

  
185
EXTMEMOPTS =
186

  
187
#---------------- Linker Options ----------------
188
#  -Wl,...:     tell GCC to pass this to linker.
189
#    -Map:      create map file
190
#    --cref:    add cross reference to  map file
191
LDFLAGS = -Wl,-Map=$(TARGET).map,--cref
192
LDFLAGS += $(EXTMEMOPTS)
193
LDFLAGS += $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB)
194
ifdef USE_WIRELESS
195
	LDFLAGS += -lwireless
196
endif
197
LDFLAGS += -ldragonfly
198

  
199

  
200
#---------------- Programming Options (avrdude) ----------------
201

  
202
# Programming hardware: alf avr910 avrisp bascom bsd 
203
# dt006 pavr picoweb pony-stk200 sp12 stk200 stk500
204
#
205
# Type: avrdude -c ?
206
# to get a full listing.
207
#
208
AVRDUDE_PROGRAMMER = avrisp
209

  
210
# programmer connected to serial device
211

  
212
AVRDUDE_WRITE_FLASH = -b 57600 -U flash:w:$(TARGET).hex
213
#AVRDUDE_WRITE_EEPROM = -U eeprom:w:$(TARGET).eep
214

  
215

  
216
# Uncomment the following if you want avrdude's erase cycle counter.
217
# Note that this counter needs to be initialized first using -Yn,
218
# see avrdude manual.
219
#AVRDUDE_ERASE_COUNTER = -y
220

  
221
# Uncomment the following if you do /not/ wish a verification to be
222
# performed after programming the device.
223
#AVRDUDE_NO_VERIFY = -V
224

  
225
# Increase verbosity level.  Please use this when submitting bug
226
# reports about avrdude. See <http://savannah.nongnu.org/projects/avrdude> 
227
# to submit bug reports.
228
#AVRDUDE_VERBOSE = -v -v
229

  
230
AVRDUDE_FLAGS = -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER)
231
AVRDUDE_FLAGS += $(AVRDUDE_NO_VERIFY)
232
AVRDUDE_FLAGS += $(AVRDUDE_VERBOSE)
233
AVRDUDE_FLAGS += $(AVRDUDE_ERASE_COUNTER)
234

  
235
#don't check for device signature
236
AVRDUDE_FLAGS += -F
237

  
238

  
239

  
240
#---------------- Debugging Options ----------------
241

  
242
# For simulavr only - target MCU frequency.
243
DEBUG_MFREQ = $(F_CPU)
244

  
245
# Set the DEBUG_UI to either gdb or insight.
246
# DEBUG_UI = gdb
247
DEBUG_UI = insight
248

  
249
# Set the debugging back-end to either avarice, simulavr.
250
DEBUG_BACKEND = avarice
251
#DEBUG_BACKEND = simulavr
252

  
253
# GDB Init Filename.
254
GDBINIT_FILE = __avr_gdbinit
255

  
256
# When using avarice settings for the JTAG
257
JTAG_DEV = /dev/com1
258

  
259
# Debugging port used to communicate between GDB / avarice / simulavr.
260
DEBUG_PORT = 4242
261

  
262
# Debugging host used to communicate between GDB / avarice / simulavr, normally
263
#     just set to localhost unless doing some sort of crazy debugging when 
264
#     avarice is running on a different computer.
265
DEBUG_HOST = localhost
266

  
267

  
268

  
269
#============================================================================
270

  
271

  
272
# Define programs and commands.
273
SHELL = sh
274
CC = avr-gcc
275
OBJCOPY = avr-objcopy
276
OBJDUMP = avr-objdump
277
SIZE = avr-size
278
NM = avr-nm
279
AVRDUDE = avrdude
280
REMOVE = rm -f
281
REMOVEDIR = rm -rf
282
COPY = cp
283
WINSHELL = cmd
284

  
285

  
286
# Define Messages
287
# English
288
MSG_ERRORS_NONE = Errors: none
289
MSG_BEGIN = -------- begin --------
290
MSG_END = --------  end  --------
291
MSG_SIZE_BEFORE = Size before: 
292
MSG_SIZE_AFTER = Size after:
293
MSG_COFF = Converting to AVR COFF:
294
MSG_EXTENDED_COFF = Converting to AVR Extended COFF:
295
MSG_FLASH = Creating load file for Flash:
296
MSG_EEPROM = Creating load file for EEPROM:
297
MSG_EXTENDED_LISTING = Creating Extended Listing:
298
MSG_SYMBOL_TABLE = Creating Symbol Table:
299
MSG_LINKING = Linking:
300
MSG_COMPILING = Compiling:
301
MSG_ASSEMBLING = Assembling:
302
MSG_CLEANING = Cleaning project:
303

  
304

  
305

  
306

  
307
# Define all object files.
308
OBJ = $(SRC:.c=.o) $(ASRC:.S=.o) 
309

  
310
# Define all listing files.
311
LST = $(SRC:.c=.lst) $(ASRC:.S=.lst) 
312

  
313

  
314
# Compiler flags to generate dependency files.
315
GENDEPFLAGS = -MD -MP -MF .dep/$(@F).d
316

  
317

  
318
# Combine all necessary flags and optional flags.
319
# Add target processor to flags.
320
ALL_CFLAGS = -mmcu=$(MCU) -I. $(CFLAGS) $(GENDEPFLAGS)
321
ALL_ASFLAGS = -mmcu=$(MCU) -I. -x assembler-with-cpp $(ASFLAGS)
322

  
323

  
324

  
325

  
326

  
327
# Default target.
328
all: begin gccversion sizebefore build sizeafter end
329

  
330
build: elf hex eep lss sym
331

  
332
elf: $(TARGET).elf
333
hex: $(TARGET).hex
334
eep: $(TARGET).eep
335
lss: $(TARGET).lss 
336
sym: $(TARGET).sym
337

  
338

  
339

  
340
# Eye candy.
341
# AVR Studio 3.x does not check make's exit code but relies on
342
# the following magic strings to be generated by the compile job.
343
begin:
344
	@echo
345
	@echo $(MSG_BEGIN)
346

  
347
end:
348
	@echo $(MSG_END)
349
	@echo
350

  
351

  
352
# Display size of file.
353
HEXSIZE = $(SIZE) --target=$(FORMAT) $(TARGET).hex
354
ELFSIZE = $(SIZE) -A $(TARGET).elf
355
AVRMEM = avr-mem.sh $(TARGET).elf $(MCU)
356

  
357
sizebefore:
358
	@if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_BEFORE); $(ELFSIZE); \
359
	$(AVRMEM) 2>/dev/null; echo; fi
360

  
361
sizeafter:
362
	@if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_AFTER); $(ELFSIZE); \
363
	$(AVRMEM) 2>/dev/null; echo; fi
364

  
365

  
366

  
367
# Display compiler version information.
368
gccversion : 
369
	@$(CC) --version
370

  
371

  
372

  
373
# Program the device.  
374
program: $(TARGET).hex $(TARGET).eep
375
	$(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) $(AVRDUDE_WRITE_EEPROM)
376

  
377

  
378
# Generate avr-gdb config/init file which does the following:
379
#     define the reset signal, load the target file, connect to target, and set 
380
#     a breakpoint at main().
381
gdb-config: 
382
	@$(REMOVE) $(GDBINIT_FILE)
383
	@echo define reset >> $(GDBINIT_FILE)
384
	@echo SIGNAL SIGHUP >> $(GDBINIT_FILE)
385
	@echo end >> $(GDBINIT_FILE)
386
	@echo file $(TARGET).elf >> $(GDBINIT_FILE)
387
	@echo target remote $(DEBUG_HOST):$(DEBUG_PORT)  >> $(GDBINIT_FILE)
388
ifeq ($(DEBUG_BACKEND),simulavr)
389
	@echo load  >> $(GDBINIT_FILE)
390
endif	
391
	@echo break main >> $(GDBINIT_FILE)
392
	
393
debug: gdb-config $(TARGET).elf
394
ifeq ($(DEBUG_BACKEND), avarice)
395
	@echo Starting AVaRICE - Press enter when "waiting to connect" message displays.
396
	@$(WINSHELL) /c start avarice --jtag $(JTAG_DEV) --erase --program --file \
397
	$(TARGET).elf $(DEBUG_HOST):$(DEBUG_PORT)
398
	@$(WINSHELL) /c pause
399
	
400
else
401
	@$(WINSHELL) /c start simulavr --gdbserver --device $(MCU) --clock-freq \
402
	$(DEBUG_MFREQ) --port $(DEBUG_PORT)
403
endif
404
	@$(WINSHELL) /c start avr-$(DEBUG_UI) --command=$(GDBINIT_FILE)
405
	
406

  
407

  
408

  
409
# Convert ELF to COFF for use in debugging / simulating in AVR Studio or VMLAB.
410
COFFCONVERT=$(OBJCOPY) --debugging \
411
--change-section-address .data-0x800000 \
412
--change-section-address .bss-0x800000 \
413
--change-section-address .noinit-0x800000 \
414
--change-section-address .eeprom-0x810000 
415

  
416

  
417
coff: $(TARGET).elf
418
	@echo
419
	@echo $(MSG_COFF) $(TARGET).cof
420
	$(COFFCONVERT) -O coff-avr $< $(TARGET).cof
421

  
422

  
423
extcoff: $(TARGET).elf
424
	@echo
425
	@echo $(MSG_EXTENDED_COFF) $(TARGET).cof
426
	$(COFFCONVERT) -O coff-ext-avr $< $(TARGET).cof
427

  
428

  
429

  
430
# Create final output files (.hex, .eep) from ELF output file.
431
%.hex: %.elf
432
	@echo
433
	@echo $(MSG_FLASH) $@
434
	$(OBJCOPY) -O $(FORMAT) -R .eeprom $< $@
435

  
436
%.eep: %.elf
437
	@echo
438
	@echo $(MSG_EEPROM) $@
439
	-$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \
440
	--change-section-lma .eeprom=0 -O $(FORMAT) $< $@
441

  
442
# Create extended listing file from ELF output file.
443
%.lss: %.elf
444
	@echo
445
	@echo $(MSG_EXTENDED_LISTING) $@
446
	$(OBJDUMP) -h -S $< > $@
447

  
448
# Create a symbol table from ELF output file.
449
%.sym: %.elf
450
	@echo
451
	@echo $(MSG_SYMBOL_TABLE) $@
452
	$(NM) -n $< > $@
453

  
454

  
455

  
456
# Link: create ELF output file from object files.
457
.SECONDARY : $(TARGET).elf
458
.PRECIOUS : $(OBJ)
459
%.elf: $(OBJ)
460
	@echo
461
	@echo $(MSG_LINKING) $@
462
	$(CC) $(ALL_CFLAGS) $^ --output $@ $(LDFLAGS)
463

  
464

  
465
# Compile: create object files from C source files.
466
%.o : %.c
467
	@echo
468
	@echo $(MSG_COMPILING) $<
469
	$(CC) -c $(ALL_CFLAGS) $< -o $@ 
470

  
471

  
472
# Compile: create assembler files from C source files.
473
%.s : %.c
474
	$(CC) -S $(ALL_CFLAGS) $< -o $@
475

  
476

  
477
# Assemble: create object files from assembler source files.
478
%.o : %.S
479
	@echo
480
	@echo $(MSG_ASSEMBLING) $<
481
	$(CC) -c $(ALL_ASFLAGS) $< -o $@
482

  
483
# Create preprocessed source for use in sending a bug report.
484
%.i : %.c
485
	$(CC) -E -mmcu=$(MCU) -I. $(CFLAGS) $< -o $@ 
486

  
487

  
488
# Target: clean project.
489
clean: begin clean_list end
490

  
491
clean_list :
492
	@echo
493
	@echo $(MSG_CLEANING)
494
	$(REMOVE) $(TARGET).hex
495
	$(REMOVE) $(TARGET).eep
496
	$(REMOVE) $(TARGET).cof
497
	$(REMOVE) $(TARGET).elf
498
	$(REMOVE) $(TARGET).map
499
	$(REMOVE) $(TARGET).sym
500
	$(REMOVE) $(TARGET).lss
501
	$(REMOVE) $(OBJ)
502
	$(REMOVE) $(LST)
503
	$(REMOVE) $(SRC:.c=.s)
504
	$(REMOVE) $(SRC:.c=.d)
505
	$(REMOVEDIR) .dep
506

  
507

  
508

  
509
# Include the dependency files.
510
-include $(shell mkdir .dep 2>/dev/null) $(wildcard .dep/*)
511

  
512

  
513
# Listing of phony targets.
514
.PHONY : all begin finish end sizebefore sizeafter gccversion \
515
build elf hex eep lss sym coff extcoff \
516
clean clean_list program debug gdb-config
517

  
branches/encoders/code/lib/include/libdragonfly/spi.h
1
/**
2
 * @file spi.h
3
 * @brief Definitions for SPI
4
 * @author Colony Project, CMU Robotics Club
5
 **/
6

  
1 7
#ifndef __SPI_H__
2 8
#define __SPI_H__
3 9

  
4 10
#define DOUBLE_SCK 1
5 11
#define SPR0_BIT 1
6
#define LSB 1
7
#define MSB 0
12

  
8 13
#define MASTER 1
9 14
#define SLAVE 0
10 15

  
11
#define SPI_IDLE 0x00
12
#define SPI_SEND 0x01
13
#define SPI_RECV 0x02
14
#define SPI_BOTH 0x03
15

  
16

  
17
/* Pin Defines */
18 16
#define MOSI _BV(PB2)
19 17
#define MISO _BV(PB3)
20
#define SS _BV(PB0)
18
#define SS   _BV(PB0)
21 19
#define SCLK _BV(PB1)
22 20

  
23
//#define size_t int
24 21
typedef void (*spi_fun_recv_t)(char);
25
typedef void (*spi_fun_send_t)(char);
26
void spi_init(char mode, spi_fun_recv_t recv_func);
27
int spi_send(char *data, int bytes);
28
void spi_master_recv_on(void);
29
void spi_master_recv_off(void);
30
void spi_read_one(void);
31
void spi_read(int bytes);
22
typedef void (*spi_fun_recv_complete_t)(void);
32 23

  
24
void spi_init (spi_fun_recv_t, spi_fun_recv_complete_t);
25
void spi_transfer (char);
26

  
33 27
#endif
branches/encoders/code/lib/include/libdragonfly/dragonfly_lib.h
65 65
#include <bom.h>
66 66
#include <move.h>
67 67
#include <reset.h>
68
//#include "spi.h"
69
#include <math.h>
68
#include <math.h>
70 69

  
70
#include <spi.h>
71
#include <encoders.h>
72

  
71 73
#endif
72 74

  
branches/encoders/code/lib/src/libdragonfly/encoders.c
1
#include "encoders.h"
2
#include "spi.h"
3
#include <dragonfly_lib.h>
4
#include "ring_buffer.h"
1 5

  
2
//Dummy file for now.
6
unsigned int left_data_buf;
7
unsigned int right_data_buf;
8
char buf_index;
9

  
10
unsigned int left_data;
11
unsigned int right_data;
12

  
13
unsigned int left_data_array[BUFFER_SIZE];
14
unsigned int right_data_array[BUFFER_SIZE];
15
int left_data_idx;
16
int right_data_idx;
17

  
18
int left_dx;
19
int right_dx;
20
long int timecount;
21

  
22
void encoder_recv(char data);
23

  
24
//Helper Function Prototypes
25
inline void left_data_array_put(unsigned short int value);
26
inline unsigned int left_data_array_top(void);
27
inline unsigned int left_data_array_prev(void);
28
inline unsigned int left_data_array_bottom(void);
29

  
30
inline void right_data_array_put(unsigned short int value);
31
inline unsigned int right_data_array_top(void);
32
inline unsigned int right_data_array_prev(void);
33
inline unsigned int right_data_array_bottom(void);
34

  
35
//RING_BUFFER_NEW(enc_buffer, BUFFER_SIZE, short int);
36

  
37
void encoder_recv_complete(void);
38

  
39
void encoder_recv_complete(){
40
	spi_transfer(5);
41
}
42

  
43

  
44
void encoders_init(void){
45
	int i;
46

  
47
	spi_init(encoder_recv, encoder_recv_complete);
48
	buf_index = 0;
49
	left_data_buf = 0;
50
	right_data_buf= 0;
51
	left_data = -1;
52
	right_data = -1;
53
	//RING_BUFFER_INIT(enc_buffer,BUFFER_SIZE);
54
	left_data_idx = 0;
55
	right_data_idx = 0;
56
	for(i = 0; i < BUFFER_SIZE; i++) {
57
		left_data_array[i] = 0;
58
	}
59
	for(i = 0; i < BUFFER_SIZE; i++) {
60
		right_data_array[i] = 0;
61
	}
62
	spi_transfer(5);
63
}
64

  
65
int encoder_read(char encoder){
66
	if(encoder==LEFT)
67
		return left_data;
68
	else if(encoder==RIGHT)
69
		return right_data;
70
	else return -1;
71
}
72

  
73
int encoder_change(char encoder){
74
	return 0;
75
}
76

  
77
char encoder_direction(char encoder){
78
	return 0;
79
}
80

  
81
int encoder_get_dx(char encoder) {
82
	if(encoder==LEFT)
83
		return left_dx;
84
	else if(encoder==RIGHT)
85
		return right_dx;
86
	else return -1;
87
}
88

  
89
void encoder_rst_dx(char encoder) {
90
	if(encoder==LEFT)
91
		left_dx = 0;
92
	else if(encoder==RIGHT)
93
		right_dx = 0;
94
}
95

  
96
int encoder_get_tc(void) {
97
	return timecount;
98
}
99

  
100
void encoder_rst_tc(void) {
101
	timecount = 0;
102
}
103

  
104

  
105
//Full reads occur every 40 microseconds. This function should be called
106
//every 8 microseconds.
107
void encoder_recv(char data){
108
	//usb_puti(buf_index);
109
	short int dx;
110
	
111
	if(buf_index < 2)
112
		right_data_buf |= (((short)data) << ((1-buf_index)*8)) & (0xFF<<(1-buf_index));
113
	
114
	else if (buf_index == 2)
115
		left_data_buf |= (((short)data) << 9) & (0x7F << 9);
116
	
117
	else if (buf_index == 3)
118
		left_data_buf |= (((short)data) << 1) & (0xFF<<1);
119
	
120
	else if (buf_index == 4)
121
		left_data_buf |= (((short)data)>>7) & 0x1;
122

  
123
	
124
	buf_index = (buf_index + 1) % 5;
125

  
126
	if(buf_index==0){
127
		//if(left_data_buf & (OCF | COF | LIN)) 
128
		//	left_data = INVALID;
129
		
130
		/*else*/ if(((left_data_buf & MagINCn) > 0)  && ((left_data_buf & MagDECn) > 0)) 
131
			left_data = MAGNET_FAILURE;
132
		
133
		else
134
			left_data = (left_data_buf>>5) & 1023;
135
		
136
		if(right_data_buf & (OCF | COF | LIN)) 
137
			right_data = INVALID;
138
		
139
		else if ( ((left_data_buf & MagINCn) > 0)  && ((left_data_buf & MagDECn) > 0)) 
140
			left_data = MAGNET_FAILURE;
141
		
142
		else right_data = (right_data_buf>>5) & 1023;
143
	
144
		usb_puts("[");usb_puti(left_data);usb_puts(",");usb_puti(right_data);usb_puts("]\r\n");
145
		
146
		left_data_buf = 0;
147
		right_data_buf = 0;
148
	
149
	}
150

  
151
	
152
	if(left_data < INVALID) {
153
		//Put new data onto data array
154
		left_data_array_put(left_data);
155

  
156
		//Adjust left accumulator
157
		dx = left_data - left_data_array_prev();
158
		if(dx > 5) { //underflow
159
			left_dx += dx - 1023;
160
		}
161
		else if(dx < -5) { //overflow
162
			left_dx += dx + 1023;
163
		}
164
		else {
165
			left_dx += dx;
166
		}
167
	}
168

  
169
	if(right_data < INVALID) {
170
		//Put new data onto data array
171
		right_data_array_put(right_data);
172

  
173
		//Adjust right accumulator
174
		dx = right_data - right_data_array_prev();
175
		if(dx > 5) { //underflow
176
			right_dx += dx - 1023;
177
		}
178
		else if(dx < -5) { //overflow
179
			right_dx += dx + 1023;
180
		}
181
		else {
182
			right_dx += dx;
183
		}
184
	}
185

  
186
	//Increment timecount accumulator
187
	timecount++;
188
}
189

  
190
//Helper Functions
191
inline void left_data_array_put(unsigned short int value) {
192
	if(left_data_idx == BUFFER_SIZE-1)
193
		left_data_idx = 0;
194
	else
195
		left_data_idx++;
196
	left_data_array[left_data_idx] = value;
197
}
198

  
199
inline unsigned int left_data_array_top(void) {
200
	return left_data_array[left_data_idx];
201
}
202

  
203
inline unsigned int left_data_array_prev(void) {
204
	if(left_data_idx == 0)
205
		return left_data_array[BUFFER_SIZE-1];
206
	else
207
		return left_data_array[left_data_idx - 1];
208
}
209

  
210
inline unsigned int left_data_array_bottom(void) {
211
	if(left_data_idx == BUFFER_SIZE-1)
212
		return left_data_array[0];
213
	else
214
		return left_data_array[left_data_idx + 1];
215
}
216

  
217
inline void right_data_array_put(unsigned short int value) {
218
	if(right_data_idx == BUFFER_SIZE-1)
219
		right_data_idx = 0;
220
	else
221
		right_data_idx++;
222
	right_data_array[right_data_idx] = value;
223
}
224

  
225
inline unsigned int right_data_array_top(void) {
226
	return right_data_array[right_data_idx];
227
}
228

  
229
inline unsigned int right_data_array_prev(void) {
230
	if(right_data_idx == 0)
231
		return right_data_array[BUFFER_SIZE-1];
232
	else
233
		return right_data_array[right_data_idx - 1];
234
}
235

  
236
inline unsigned int right_data_array_bottom(void) {
237
	if(right_data_idx == BUFFER_SIZE-1)
238
		return right_data_array[0];
239
	else
240
		return right_data_array[right_data_idx + 1];
241
}
branches/encoders/code/lib/src/libdragonfly/spi.c
1
/* @file spi.c
2
 * @brief
3
 * SPI module
4
 * @bug broken
5
 */
1
/**
2
 * @file spi.c
3
 * @brief Basic SPI module to handle encoders
4
 * @author Colony Project, CMU Robotics Club
5
 * @bug Not tested
6
 *	Need to move spi.h include into dragonfly_lib.h when stable
7
 **/
6 8

  
7 9
#include <avr/interrupt.h>
8
#include "ring_buffer.h"
10
#include <dragonfly_lib.h>
9 11
#include "spi.h"
10
#include <dragonfly_lib.h>
11 12

  
13
static volatile char spi_bytes; /* number of bytes to read */
14
static spi_fun_recv_t spi_recv_func; /* byte handler */
15
static spi_fun_recv_complete_t spi_recv_complete_func; /*transmission completion handler */
12 16

  
13
/*
14
SS = PB0
15
SCK = PB1
16
MOSI = PB2
17
MISO = PB3
18
*/
19
/* Controls clock freq. see Table 72 of specs*/
17
void spi_init (spi_fun_recv_t recv_func, spi_fun_recv_complete_t recv_complete_func)
18
{
19
    /*  Enable Interrupt, Enable SPI Module, MSB First, Master Mode, Clock div = 64 */
20
    SPCR = _BV(SPE) | _BV(SPIE) | _BV(DORD) | _BV(MSTR) | _BV(SPR1) | _BV(SPR0);
21
    SPSR = _BV(SPI2X); 
20 22

  
21
#define DOUBLE_SCK 1
22
#define SPR0_BIT 1
23
#define SPR1_BIT 0
24
#define LSB 1
25
#define MSB 0
26

  
27

  
28
RING_BUFFER_NEW(spi_buffer, 16, char, spi_send_buff, spi_rec_buff);
29
volatile char spi_status;
30
char spi_mode;
31
static spi_fun_recv_t  spi_recv_function;
32
//static spi_fun_send_t  spi_send_function;
33

  
34

  
35
void spi_init(char mode, spi_fun_recv_t recv_func) {
36
    usb_puts("spi_init: start\n");
37

  
38
	spi_mode = mode;
39

  
40
	RING_BUFFER_CLEAR(spi_send_buff);
41
	RING_BUFFER_CLEAR(spi_rec_buff);
42

  
43
	spi_recv_function = recv_func;
44
	//spi_send_function = send_func;
45

  
46
    /* Enables the SPI module
47
     * Enable Interrupt, Enable SPI Module, LSB First, Master Mode, Clock div = 64
48
     */
49
    SPCR = 0x00;
50
	SPCR = _BV(SPIE) | _BV(SPE) | _BV(DORD) | _BV(MSTR)| _BV(SPR1) | _BV(SPR0);
51
    SPSR = 0x00;
52
	SPSR = _BV(SPI2X); 
53
	
54
	spi_status = SPI_IDLE;
55

  
56 23
    /* Set SCLK, SS, MOSI as outputs. MISO as input */
57
	if(mode == MASTER) {
58
	    DDRB |= MOSI | SCLK | SS;
59
	    DDRB &= ~MISO;
60
		PORTB |= SS;	//Keep SS High until transmit
61
	/* Set SCLK, SS, MOSI as inputs. MISO as output */
62
	} else {
63
	    DDRB &= ~MOSI & ~SCLK & ~SS;
64
	    DDRB |= MISO;
65
    }
66
	
67
	//sei();
68
	usb_puts("spi_init: end\n");
69
}
70

  
71
int spi_send(char *data, int bytes) {
72

  
73
	int i;
74

  
75
    if(bytes == 0) 
76
        return -1; /* ...needed?*/
24
    DDRB = MOSI | SCLK | SS;
25
    DDRB &= ~MISO;
77 26
    
78
    //Prevent race condition on the buffer
79
    cli();
80
    for(i = 1; i < bytes; i++) {
81
        // Fail if the buffer is full
82
	    if(RING_BUFFER_FULL(spi_send_buff)) {
83
	        sei();
84
	        return -1;
85
	    }
86
	
87
	    RING_BUFFER_ADD(spi_send_buff, data[i]);
88
    }
89
    
90
    sei();
91
    
92
    spi_status |= SPI_SEND;
93
	
94
	if (spi_mode == MASTER ){
95
		PORTB &= ~SS;	//Select slave
96
	}
97
	
98
    SPDR = *data;
99
    
100
	if(spi_mode)
101
		usb_puts("MASTER");
102
	else
103
		usb_puts("SLAVE");
104
		
105
	usb_puts(": sending [");usb_putc(*data);usb_puts("]\n\r");
106
	
107
	//sei();
108
	
109
	return 1;
110
}
27
    /* Keep SS high until transmit */
28
    PORTB |= SS;
111 29

  
112
void spi_read(int bytes) {
113
    
114
    cli();
115
    for(int i = 1; i < bytes; i++) {
116
        // Fail if the buffer is full
117
	    if(RING_BUFFER_FULL(spi_send_buff)) {
118
	        sei();
119
	        return;
120
	    }
121
	
122
	    RING_BUFFER_ADD(spi_send_buff, '\n');
123
    }
124
    
125
    sei();
126

  
127
    spi_status |= SPI_RECV;
128
	
129
	if (spi_mode == MASTER ){
130
		PORTB &= ~SS;	//Select slave
131
	}
132
	
133
    SPDR = '\n';
134

  
30
    /* set function to be executed when we receive a byte */
31
    spi_recv_func = recv_func;
32
	spi_recv_complete_func = recv_complete_func;
33
    spi_bytes = 0;
135 34
}
136 35

  
137
void spi_read_one(void)
36
/* Transfer a given byte to slave and receive a byte */
37
void spi_transfer(char bytes)
138 38
{
139
	PORTB &= ~SS;
140
	SPDR = 'x';
39
    spi_bytes = bytes;
40
    PORTB &= ~SS; /* Set SS low to initiate transmission */
41
    SPDR = 0xff; /* Initiate data transmision */
141 42
}
142 43

  
143
ISR(SIG_SPI) {
144
	char c;
145

  
146
    //The clock is running so dequeue another byte to send	
147
	if(!RING_BUFFER_EMPTY(spi_send_buff)) {//cheap way to test if SPI_SEND
148
		RING_BUFFER_REMOVE(spi_send_buff, c);
149
		SPDR = c;
150
	
151
	//If we're the master and we're done sending, end the transmission
152
	} else if (spi_mode == MASTER) {
44
ISR(SIG_SPI) 
45
{
46
	//usb_puts("Interrupt");
47
    /* only handle intterupt when we are expecting data */
48
    if(spi_bytes > 0){
49
	/* process byte */
50
	spi_recv_func(SPDR);
51
	/* if we've read all the bytes, set SS high to end transmission,
52
	 * otherwise get the next byte  */
53
	if(--spi_bytes == 0){
54
		//usb_puts("Read all bytes\r\n");
153 55
	    PORTB |= SS;
154
	}
155
	
156
	//You always receive something. You need to handle it.
157
    spi_recv_function(SPDR);	
158
    
159
	/*
160
	if(spi_status & SPI_SEND) {
161
		spi_status ^= SPI_SEND;
162
		
163
        if (spi_mode == MASTER) {
164
    		PORTB |= SS;
165
        }
166
    } else if (spi_status & SPI_RECV) {
167
           
56
		spi_recv_complete_func();
57
	}else {
58
		//usb_puts("There are this many bytes left: "); usb_puti(spi_bytes);usb_puts("\r\n");
59
	    SPDR = 0xff;
60
		}
168 61
    }
169
    */
170
    //End the transmission if we are not sending or recieving anymore
171
	//} else if(spi_mode == MASTER) {
172
	//   PORTB |= SS;
173
	//}
174
	
175
	//Call the recv function whenver we recieve a byte
176
	//if(spi_status == SPI_RECV){
177
		//spi_recv_function(SPDR);
178
		//if(spi_mode == MASTER)
179
			//PORTB |= SS;
180
	//}
181 62
}		
182

  
branches/encoders/code/projects/libdragonfly/dragonfly_lib.h
65 65
#include <bom.h>
66 66
#include <move.h>
67 67
#include <reset.h>
68
//#include "spi.h"
69
#include <math.h>
68
#include <math.h>
70 69

  
70
#include <spi.h>
71
#include <encoders.h>
72

  
71 73
#endif
72 74

  
branches/encoders/code/projects/libdragonfly/spi.c
12 12

  
13 13
static volatile char spi_bytes; /* number of bytes to read */
14 14
static spi_fun_recv_t spi_recv_func; /* byte handler */
15
static spi_fun_recv_complete_t spi_recv_complete_func; /*transmission completion handler */
15 16

  
16
void spi_init (spi_fun_recv_t recv_func)
17
void spi_init (spi_fun_recv_t recv_func, spi_fun_recv_complete_t recv_complete_func)
17 18
{
18 19
    /*  Enable Interrupt, Enable SPI Module, MSB First, Master Mode, Clock div = 64 */
19 20
    SPCR = _BV(SPE) | _BV(SPIE) | _BV(DORD) | _BV(MSTR) | _BV(SPR1) | _BV(SPR0);
......
28 29

  
29 30
    /* set function to be executed when we receive a byte */
30 31
    spi_recv_func = recv_func;
32
	spi_recv_complete_func = recv_complete_func;
31 33
    spi_bytes = 0;
32 34
}
33 35

  
......
41 43

  
42 44
ISR(SIG_SPI) 
43 45
{
46
	//usb_puts("Interrupt");
44 47
    /* only handle intterupt when we are expecting data */
45 48
    if(spi_bytes > 0){
46 49
	/* process byte */
47 50
	spi_recv_func(SPDR);
48 51
	/* if we've read all the bytes, set SS high to end transmission,
49 52
	 * otherwise get the next byte  */
50
	if(--spi_bytes == 0)
53
	if(--spi_bytes == 0){
54
		//usb_puts("Read all bytes\r\n");
51 55
	    PORTB |= SS;
52
	else 
56
		spi_recv_complete_func();
57
	}else {
58
		//usb_puts("There are this many bytes left: "); usb_puti(spi_bytes);usb_puts("\r\n");
53 59
	    SPDR = 0xff;
60
		}
54 61
    }
55 62
}		
branches/encoders/code/projects/libdragonfly/spi.h
19 19
#define SCLK _BV(PB1)
20 20

  
21 21
typedef void (*spi_fun_recv_t)(char);
22
typedef void (*spi_fun_recv_complete_t)(void);
22 23

  
23
void spi_init (spi_fun_recv_t);
24
void spi_init (spi_fun_recv_t, spi_fun_recv_complete_t);
24 25
void spi_transfer (char);
25 26

  
26 27
#endif
branches/encoders/code/projects/libdragonfly/encoders.c
3 3
#include <dragonfly_lib.h>
4 4
#include "ring_buffer.h"
5 5

  
6
unsigned short int left_data_buf;
7
unsigned short int right_data_buf;
6
unsigned int left_data_buf;
7
unsigned int right_data_buf;
8 8
char buf_index;
9 9

  
10
unsigned short int left_data;
11
unsigned short int right_data;
10
unsigned int left_data;
11
unsigned int right_data;
12 12

  
13
unsigned short int left_data_array[BUFFER_SIZE];
14
unsigned short int right_data_array[BUFFER_SIZE];
13
unsigned int left_data_array[BUFFER_SIZE];
14
unsigned int right_data_array[BUFFER_SIZE];
15 15
int left_data_idx;
16 16
int right_data_idx;
17 17

  
......
23 23

  
24 24
//Helper Function Prototypes
25 25
inline void left_data_array_put(unsigned short int value);
26
inline unsigned short int left_data_array_top(void);
27
inline unsigned short int left_data_array_prev(void);
28
inline unsigned short int left_data_array_bottom(void);
26
inline unsigned int left_data_array_top(void);
27
inline unsigned int left_data_array_prev(void);
28
inline unsigned int left_data_array_bottom(void);
29 29

  
30 30
inline void right_data_array_put(unsigned short int value);
31
inline unsigned short int right_data_array_top(void);
32
inline unsigned short int right_data_array_prev(void);
33
inline unsigned short int right_data_array_bottom(void);
31
inline unsigned int right_data_array_top(void);
32
inline unsigned int right_data_array_prev(void);
33
inline unsigned int right_data_array_bottom(void);
34 34

  
35 35
//RING_BUFFER_NEW(enc_buffer, BUFFER_SIZE, short int);
36 36

  
37
void encoder_recv_complete(void);
38

  
39
void encoder_recv_complete(){
40
	spi_transfer(5);
41
}
42

  
43

  
37 44
void encoders_init(void){
38 45
	int i;
39 46

  
40
	spi_init(encoder_recv);
47
	spi_init(encoder_recv, encoder_recv_complete);
41 48
	buf_index = 0;
42 49
	left_data_buf = 0;
43 50
	right_data_buf= 0;
......
52 59
	for(i = 0; i < BUFFER_SIZE; i++) {
53 60
		right_data_array[i] = 0;
54 61
	}
62
	spi_transfer(5);
55 63
}
56 64

  
57 65
int encoder_read(char encoder){
......
94 102
}
95 103

  
96 104

  
97

  
98 105
//Full reads occur every 40 microseconds. This function should be called
99 106
//every 8 microseconds.
100 107
void encoder_recv(char data){
108
	//usb_puti(buf_index);
101 109
	short int dx;
102 110
	
103 111
	if(buf_index < 2)
......
122 130
		else if(((left_data_buf & MagINCn) > 0)  && ((left_data_buf & MagDECn) > 0)) 
123 131
			left_data = MAGNET_FAILURE;
124 132
		
125
		else left_data = (left_data_buf>>5) & 1023;
133
		else
134
			left_data = (left_data_buf>>5) & 1023;
126 135
		
127 136
		if(right_data_buf & (OCF | COF | LIN)) 
128 137
			right_data = INVALID;
......
132 141
		
133 142
		else right_data = (right_data_buf>>5) & 1023;
134 143
	
135
							
144
		//usb_puts("[");usb_puti(left_data);usb_puts(",");usb_puti(right_data);usb_puts("]\r\n");
136 145
		
137 146
		left_data_buf = 0;
138 147
		right_data_buf = 0;
148
	
139 149
	}
140 150

  
141 151
	
......
186 196
	left_data_array[left_data_idx] = value;
187 197
}
188 198

  
189
inline unsigned short int left_data_array_top(void) {
199
inline unsigned int left_data_array_top(void) {
190 200
	return left_data_array[left_data_idx];
191 201
}
192 202

  
193
inline unsigned short int left_data_array_prev(void) {
203
inline unsigned int left_data_array_prev(void) {
194 204
	if(left_data_idx == 0)
195 205
		return left_data_array[BUFFER_SIZE-1];
196 206
	else
197 207
		return left_data_array[left_data_idx - 1];
198 208
}
199 209

  
200
inline unsigned short int left_data_array_bottom(void) {
210
inline unsigned int left_data_array_bottom(void) {
201 211
	if(left_data_idx == BUFFER_SIZE-1)
202 212
		return left_data_array[0];
203 213
	else
......
212 222
	right_data_array[right_data_idx] = value;
213 223
}
214 224

  
215
inline unsigned short int right_data_array_top(void) {
225
inline unsigned int right_data_array_top(void) {
216 226
	return right_data_array[right_data_idx];
217 227
}
218 228

  
219
inline unsigned short int right_data_array_prev(void) {
229
inline unsigned int right_data_array_prev(void) {
220 230
	if(right_data_idx == 0)
221 231
		return right_data_array[BUFFER_SIZE-1];
222 232
	else
223 233
		return right_data_array[right_data_idx - 1];
224 234
}
225 235

  
226
inline unsigned short int right_data_array_bottom(void) {
236
inline unsigned int right_data_array_bottom(void) {
227 237
	if(right_data_idx == BUFFER_SIZE-1)
228 238
		return right_data_array[0];
229 239
	else
branches/encoders/code/projects/libdragonfly/encoders.h
1
#ifndef __ENCODERS_H__
2
#define __ENCODERS_H__
3

  
4

  
1 5
#define RIGHT 1
2 6
#ifndef LEFT
3 7
	#define LEFT 0
......
20 24

  
21 25
#define BUFFER_SIZE 23
22 26

  
23
void encoder_init(void);
27
void encoders_init(void);
24 28
int encoder_read(char encoder);
25 29
char encoder_direction(char encoder);
26 30

  
27 31
int encoder_get_dx(char encoder);
28 32
void encoder_rst_dx(char encoder);
29 33
int encoder_get_tc(void);
30
void encoder_rst_tc(void);
34
void encoder_rst_tc(void);
35

  
36
#endif

Also available in: Unified diff