Project

General

Profile

Revision 160

added simple test code for programmer

View differences:

trunk/programmer/test/main.c
1
/********
2
 * This file is part of Tooltron.
3
 *
4
 * Tooltron is free software: you can redistribute it and/or modify
5
 * it under the terms of the Lesser GNU General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * Tooltron is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * Lesser GNU General Public License for more details.
13
 * You should have received a copy of the Lesser GNU General Public License
14
 * along with Tooltron.  If not, see <http://www.gnu.org/licenses/>.
15
 *
16
 * Copyright 2009 Kevin Woo <kwoo@2ndt.com>
17
 *
18
 ********/
19
/** @file main.c
20
 *  @brief Contains the main function for the toolbox code.
21
 *
22
 *  @author Suresh Nidhiry (snidhiry), Kevin Woo (kwoo)
23
 */
24

  
25
//Includes
26
#include <avr/io.h>
27
#include <avr/interrupt.h>
28
#include <stdint.h>
29
#include <util/delay.h>
30

  
31
#define RELAY       _BV(PORTD4)
32
#define VAC_SENSE   _BV(PIND3)
33
#define BUT_RED     _BV(PINB4)
34
#define BUT_BLACK   _BV(PINB3)
35
#define LED_GREEN   _BV(PORTB2)
36
#define LED_YELLOW  _BV(PORTB1)
37
#define LED_RED     _BV(PORTB0)
38
#define ON      0x01
39
#define OFF     0x00
40

  
41
/***** change ADDR ****/
42
#define ADDR 18
43
#define DELIM '^'
44
#define SERVER 1
45
#define TURNON 'O'
46

  
47
/***
48
 * TWAIT - minutes to wait before green button is pressed to kill power
49
 * TWARN - minutes until warning (blink yellow, allow more time with green button)
50
 * TMAX  - minutes until power is killed (unless tool is on)
51
 */
52
#define TWAIT   1
53
#define TWARN   1
54
#define TMAX    2
55

  
56
uint8_t sec;
57
uint8_t min;
58

  
59
typedef enum {
60
    sd,     // start delimitor
61
    src,    // src
62
    dest,   // destination
63
    data,   // data
64
    cs,     // checksum
65
    ack,    // send ack
66
    pwron,  // poweron
67
    idiot,  // user tried to hit green with the machine switch on
68
    toolon, // tool on
69
    warn,   // time warning
70
    off     // tool off
71
} state_t;
72

  
73
void init_pins(void) {
74
    DDRB = 0x00;
75
    DDRB = _BV(DDB0) | _BV(DDB1) | _BV(DDB2) | _BV(DDB5);
76
    DDRD = _BV(DDB4);
77
    PORTB = 0x00;
78
}
79

  
80
void toggle_led(uint8_t which, uint8_t state) {
81
    if (state == ON) {
82
        PORTB &= ~which;
83
    } else {
84
        PORTB |= which;
85
    }
86
}
87

  
88

  
89
int main(int argc, char **argv) {
90
    state_t state = sd;
91
    uint8_t packet[3];
92
    uint8_t ms_timer=0;
93

  
94
	/***** Start Start-up Sequence *****/
95
	init_pins();		//Set pin directions
96
	//	init_uart(51);		//Set registers for uart
97
	/***** End Start-up Sequence *****/
98

  
99
    uint8_t r;
100

  
101
    while(1) {
102
      toggle_led(LED_GREEN, ON);
103
      _delay_ms(100);
104
      toggle_led(LED_GREEN, OFF);
105
      _delay_ms(100);
106
    }
107

  
108
    return 0;
109
}
trunk/programmer/test/Makefile
1
# Hey Emacs, this is a -*- makefile -*-
2
#----------------------------------------------------------------------------
3
# WinAVR Makefile Template written by Eric B. Weddington, J?rg Wunsch, et al.
4
#
5
# Released to the Public Domain
6
#
7
# Additional material for this makefile was written by:
8
# Peter Fleury
9
# Tim Henigan
10
# Colin O'Flynn
11
# Reiner Patommel
12
# Markus Pfaff
13
# Sander Pool
14
# Frederik Rouleau
15
#
16
#----------------------------------------------------------------------------
17
# On command line:
18
#
19
# make all = Make software.
20
#
21
# make clean = Clean out built project files.
22
#
23
# make coff = Convert ELF to AVR COFF.
24
#
25
# make extcoff = Convert ELF to AVR Extended COFF.
26
#
27
# make program = Download the hex file to the device, using avrdude.
28
#                Please customize the avrdude settings below first!
29
#
30
# make debug = Start either simulavr or avarice as specified for debugging, 
31
#              with avr-gdb or avr-insight as the front end for debugging.
32
#
33
# make filename.s = Just compile filename.c into the assembler code only.
34
#
35
# make filename.i = Create a preprocessed source file for use in submitting
36
#                   bug reports to the GCC project.
37
#
38
# To rebuild project do "make clean" then "make all".
39
#----------------------------------------------------------------------------
40

  
41

  
42
# MCU name
43
MCU = attiny2313
44

  
45

  
46
# Processor frequency.
47
#     This will define a symbol, F_CPU, in all source code files equal to the 
48
#     processor frequency. You can then use this symbol in your source code to 
49
#     calculate timings. Do NOT tack on a 'UL' at the end, this will be done
50
#     automatically to create a 32-bit value in your source code.
51
F_CPU = 8000000
52

  
53

  
54
# Output format. (can be srec, ihex, binary)
55
FORMAT = binary
56

  
57

  
58
# Target file name (without extension).
59
TARGET = main
60

  
61
# List C source files here. (C dependencies are automatically generated.)
62
SRC =  $(wildcard *.c)
63

  
64
# List Assembler source files here.
65
#     Make them always end in a capital .S.  Files ending in a lowercase .s
66
#     will not be considered source files but generated files (assembler
67
#     output from the compiler), and will be deleted upon "make clean"!
68
#     Even though the DOS/Win* filesystem matches both .s and .S the same,
69
#     it will preserve the spelling of the filenames, and gcc itself does
70
#     care about how the name is spelled on its command-line.
71
ASRC = 
72

  
73

  
74
# Optimization level, can be [0, 1, 2, 3, s]. 
75
#     0 = turn off optimization. s = optimize for size.
76
#     (Note: 3 is not always the best optimization level. See avr-libc FAQ.)
77
OPT = s
78

  
79

  
80
# Debugging format.
81
#     Native formats for AVR-GCC's -g are dwarf-2 [default] or stabs.
82
#     AVR Studio 4.10 requires dwarf-2.
83
#     AVR [Extended] COFF format requires stabs, plus an avr-objcopy run.
84
DEBUG =
85

  
86

  
87
# List any extra directories to look for include files here.
88
#     Each directory must be seperated by a space.
89
#     Use forward slashes for directory separators.
90
#     For a directory that has spaces, enclose it in quotes.
91
#EXTRAINCDIRS = C:\WinAVR\include\fwr
92

  
93

  
94
# Compiler flag to set the C Standard level.
95
#     c89   = "ANSI" C
96
#     gnu89 = c89 plus GCC extensions
97
#     c99   = ISO C99 standard (not yet fully implemented)
98
#     gnu99 = c99 plus GCC extensions
99
CSTANDARD = -std=gnu99
100

  
101

  
102
# Place -D or -U options here
103
CDEFS = -DF_CPU=$(F_CPU)UL
104

  
105

  
106
# Place -I options here
107
CINCS =
108

  
109

  
110

  
111
#---------------- Compiler Options ----------------
112
#  -g*:          generate debugging information
113
#  -O*:          optimization level
114
#  -f...:        tuning, see GCC manual and avr-libc documentation
115
#  -Wall...:     warning level
116
#  -Wa,...:      tell GCC to pass this to the assembler.
117
#    -adhlns...: create assembler listing
118
#CFLAGS = -g$(DEBUG)
119
CFLAGS += $(CDEFS) $(CINCS)
120
CFLAGS += -O$(OPT)
121
CFLAGS += -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums
122
CFLAGS += -Wall -Wstrict-prototypes
123
CFLAGS += -Wa,-adhlns=$(<:.c=.lst)
124
CFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
125
CFLAGS += $(CSTANDARD)
126

  
127

  
128
#---------------- Assembler Options ----------------
129
#  -Wa,...:   tell GCC to pass this to the assembler.
130
#  -ahlms:    create listing
131
#  -gstabs:   have the assembler create line number information; note that
132
#             for use in COFF files, additional information about filenames
133
#             and function names needs to be present in the assembler source
134
#             files -- see avr-libc docs [FIXME: not yet described there]
135
ASFLAGS = -Wa,-adhlns=$(<:.S=.lst),-gstabs 
136

  
137

  
138
#---------------- Library Options ----------------
139
# Minimalistic printf version
140
PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min
141

  
142
# Floating point printf version (requires MATH_LIB = -lm below)
143
PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt
144

  
145
# If this is left blank, then it will use the Standard printf version.
146
PRINTF_LIB = 
147
#PRINTF_LIB = $(PRINTF_LIB_MIN)
148
#PRINTF_LIB = $(PRINTF_LIB_FLOAT)
149

  
150

  
151
# Minimalistic scanf version
152
SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min
153

  
154
# Floating point + %[ scanf version (requires MATH_LIB = -lm below)
155
SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt
156

  
157
# If this is left blank, then it will use the Standard scanf version.
158
SCANF_LIB = 
159
#SCANF_LIB = $(SCANF_LIB_MIN)
160
#SCANF_LIB = $(SCANF_LIB_FLOAT)
161

  
162

  
163
MATH_LIB = -lm
164

  
165

  
166

  
167
#---------------- External Memory Options ----------------
168

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

  
173
# 64 KB of external RAM, starting after internal RAM (ATmega128!),
174
# only used for heap (malloc()).
175
#EXTMEMOPTS = -Wl,--defsym=__heap_start=0x801100,--defsym=__heap_end=0x80ffff
176

  
177
EXTMEMOPTS =
178

  
179

  
180

  
181
#---------------- Linker Options ----------------
182
#  -Wl,...:     tell GCC to pass this to linker.
183
#    -Map:      create map file
184
#    --cref:    add cross reference to  map file
185
LDFLAGS = -Wl,-Map=$(TARGET).map,--cref
186
LDFLAGS += $(EXTMEMOPTS)
187
LDFLAGS += $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB)
188

  
189

  
190

  
191
#---------------- Programming Options (avrdude) ----------------
192

  
193
# Programming hardware: alf avr910 avrisp bascom bsd 
194
# dt006 pavr picoweb pony-stk200 sp12 stk200 stk500
195
#
196
# Type: avrdude -c ?
197
# to get a full listing.
198
#
199
AVRDUDE_PROGRAMMER = avrispmkII
200

  
201
# com1 = serial port. Use lpt1 to connect to parallel port.
202
AVRDUDE_PORT = usb 
203
# programmer connected to serial device
204

  
205
AVRDUDE_WRITE_FLASH = -b 4800 -U flash:w:$(TARGET).hex
206
#AVRDUDE_WRITE_EEPROM = -U eeprom:w:$(TARGET).eep
207

  
208

  
209
# Uncomment the following if you want avrdude's erase cycle counter.
210
# Note that this counter needs to be initialized first using -Yn,
211
# see avrdude manual.
212
#AVRDUDE_ERASE_COUNTER = -y
213

  
214
# Uncomment the following if you do /not/ wish a verification to be
215
# performed after programming the device.
216
#AVRDUDE_NO_VERIFY = -V
217

  
218
# Increase verbosity level.  Please use this when submitting bug
219
# reports about avrdude. See <http://savannah.nongnu.org/projects/avrdude> 
220
# to submit bug reports.
221
#AVRDUDE_VERBOSE = -v -v
222

  
223
AVRDUDE_FLAGS = -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER)
224
AVRDUDE_FLAGS += $(AVRDUDE_NO_VERIFY)
225
AVRDUDE_FLAGS += $(AVRDUDE_VERBOSE)
226
AVRDUDE_FLAGS += $(AVRDUDE_ERASE_COUNTER)
227

  
228

  
229

  
230
#---------------- Debugging Options ----------------
231

  
232
# For simulavr only - target MCU frequency.
233
DEBUG_MFREQ = $(F_CPU)
234

  
235
# Set the DEBUG_UI to either gdb or insight.
236
# DEBUG_UI = gdb
237
DEBUG_UI = insight
238

  
239
# Set the debugging back-end to either avarice, simulavr.
240
DEBUG_BACKEND = avarice
241
#DEBUG_BACKEND = simulavr
242

  
243
# GDB Init Filename.
244
GDBINIT_FILE = __avr_gdbinit
245

  
246
# When using avarice settings for the JTAG
247
JTAG_DEV = /dev/com1
248

  
249
# Debugging port used to communicate between GDB / avarice / simulavr.
250
DEBUG_PORT = 4242
251

  
252
# Debugging host used to communicate between GDB / avarice / simulavr, normally
253
#     just set to localhost unless doing some sort of crazy debugging when 
254
#     avarice is running on a different computer.
255
DEBUG_HOST = localhost
256

  
257

  
258

  
259
#============================================================================
260

  
261

  
262
# Define programs and commands.
263
SHELL = sh
264
CC = avr-gcc
265
OBJCOPY = avr-objcopy
266
OBJDUMP = avr-objdump
267
SIZE = avr-size
268
NM = avr-nm
269
AVRDUDE = avrdude
270
REMOVE = rm -f
271
COPY = cp
272
WINSHELL = cmd
273

  
274

  
275
# Define Messages
276
# English
277
MSG_ERRORS_NONE = Errors: none
278
MSG_BEGIN = -------- begin --------
279
MSG_END = --------  end  --------
280
MSG_SIZE_BEFORE = Size before: 
281
MSG_SIZE_AFTER = Size after:
282
MSG_COFF = Converting to AVR COFF:
283
MSG_EXTENDED_COFF = Converting to AVR Extended COFF:
284
MSG_FLASH = Creating load file for Flash:
285
MSG_EEPROM = Creating load file for EEPROM:
286
MSG_EXTENDED_LISTING = Creating Extended Listing:
287
MSG_SYMBOL_TABLE = Creating Symbol Table:
288
MSG_LINKING = Linking:
289
MSG_COMPILING = Compiling:
290
MSG_ASSEMBLING = Assembling:
291
MSG_CLEANING = Cleaning project:
292

  
293

  
294

  
295

  
296
# Define all object files.
297
OBJ = $(SRC:.c=.o) $(ASRC:.S=.o) 
298

  
299
# Define all listing files.
300
LST = $(SRC:.c=.lst) $(ASRC:.S=.lst) 
301

  
302

  
303
# Compiler flags to generate dependency files.
304
GENDEPFLAGS = -MD -MP -MF .dep/$(@F).d
305

  
306

  
307
# Combine all necessary flags and optional flags.
308
# Add target processor to flags.
309
ALL_CFLAGS = -mmcu=$(MCU) -I. $(CFLAGS) $(GENDEPFLAGS)
310
ALL_ASFLAGS = -mmcu=$(MCU) -I. -x assembler-with-cpp $(ASFLAGS)
311

  
312

  
313

  
314

  
315

  
316
# Default target.
317
all: begin gccversion sizebefore build sizeafter end
318

  
319
build: elf hex eep lss sym
320

  
321
elf: $(TARGET).elf
322
hex: $(TARGET).hex
323
eep: $(TARGET).eep
324
lss: $(TARGET).lss 
325
sym: $(TARGET).sym
326

  
327

  
328

  
329
# Eye candy.
330
# AVR Studio 3.x does not check make's exit code but relies on
331
# the following magic strings to be generated by the compile job.
332
begin:
333
	@echo
334
	@echo $(MSG_BEGIN)
335

  
336
end:
337
	@echo $(MSG_END)
338
	@echo
339

  
340

  
341
# Display size of file.
342
HEXSIZE = $(SIZE) --target=$(FORMAT) $(TARGET).hex
343
ELFSIZE = $(SIZE) -A $(TARGET).elf
344
AVRMEM = avr-mem.sh $(TARGET).elf $(MCU)
345

  
346
sizebefore:
347
	@if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_BEFORE); $(ELFSIZE); \
348
	$(AVRMEM) 2>/dev/null; echo; fi
349

  
350
sizeafter:
351
	@if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_AFTER); $(ELFSIZE); \
352
	$(AVRMEM) 2>/dev/null; echo; fi
353

  
354

  
355

  
356
# Display compiler version information.
357
gccversion : 
358
	@$(CC) --version
359

  
360

  
361

  
362
# Program the device.  
363
program: $(TARGET).hex $(TARGET).eep
364
	$(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) $(AVRDUDE_WRITE_EEPROM)
365

  
366

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

  
396

  
397

  
398
# Convert ELF to COFF for use in debugging / simulating in AVR Studio or VMLAB.
399
COFFCONVERT=$(OBJCOPY) --debugging \
400
--change-section-address .data-0x800000 \
401
--change-section-address .bss-0x800000 \
402
--change-section-address .noinit-0x800000 \
403
--change-section-address .eeprom-0x810000 
404

  
405

  
406
coff: $(TARGET).elf
407
	@echo
408
	@echo $(MSG_COFF) $(TARGET).cof
409
	$(COFFCONVERT) -O coff-avr $< $(TARGET).cof
410

  
411

  
412
extcoff: $(TARGET).elf
413
	@echo
414
	@echo $(MSG_EXTENDED_COFF) $(TARGET).cof
415
	$(COFFCONVERT) -O coff-ext-avr $< $(TARGET).cof
416

  
417

  
418

  
419
# Create final output files (.hex, .eep) from ELF output file.
420
%.hex: %.elf
421
	@echo
422
	@echo $(MSG_FLASH) $@
423
	$(OBJCOPY) -O $(FORMAT) -R .eeprom $< $@
424

  
425
%.eep: %.elf
426
	@echo
427
	@echo $(MSG_EEPROM) $@
428
	-$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \
429
	--change-section-lma .eeprom=0 -O $(FORMAT) $< $@
430

  
431
# Create extended listing file from ELF output file.
432
%.lss: %.elf
433
	@echo
434
	@echo $(MSG_EXTENDED_LISTING) $@
435
	$(OBJDUMP) -h -S $< > $@
436

  
437
# Create a symbol table from ELF output file.
438
%.sym: %.elf
439
	@echo
440
	@echo $(MSG_SYMBOL_TABLE) $@
441
	$(NM) -n $< > $@
442

  
443

  
444

  
445
# Link: create ELF output file from object files.
446
.SECONDARY : $(TARGET).elf
447
.PRECIOUS : $(OBJ)
448
%.elf: $(OBJ)
449
	@echo
450
	@echo $(MSG_LINKING) $@
451
	$(CC) $(ALL_CFLAGS) $^ --output $@ $(LDFLAGS)
452

  
453

  
454
# Compile: create object files from C source files.
455
%.o : %.c
456
	@echo
457
	@echo $(MSG_COMPILING) $<
458
	$(CC) -c $(ALL_CFLAGS) $< -o $@ 
459

  
460

  
461
# Compile: create assembler files from C source files.
462
%.s : %.c
463
	$(CC) -S $(ALL_CFLAGS) $< -o $@
464

  
465

  
466
# Assemble: create object files from assembler source files.
467
%.o : %.S
468
	@echo
469
	@echo $(MSG_ASSEMBLING) $<
470
	$(CC) -c $(ALL_ASFLAGS) $< -o $@
471

  
472
# Create preprocessed source for use in sending a bug report.
473
%.i : %.c
474
	$(CC) -E -mmcu=$(MCU) -I. $(CFLAGS) $< -o $@ 
475

  
476

  
477
# Target: clean project.
478
clean: begin clean_list end
479

  
480
clean_list :
481
	@echo
482
	@echo $(MSG_CLEANING)
483
	$(REMOVE) $(TARGET).hex
484
	$(REMOVE) $(TARGET).eep
485
	$(REMOVE) $(TARGET).cof
486
	$(REMOVE) $(TARGET).elf
487
	$(REMOVE) $(TARGET).map
488
	$(REMOVE) $(TARGET).sym
489
	$(REMOVE) $(TARGET).lss
490
	$(REMOVE) $(OBJ)
491
	$(REMOVE) $(LST)
492
	$(REMOVE) $(SRC:.c=.s)
493
	$(REMOVE) $(SRC:.c=.d)
494
	$(REMOVE) .dep/*
495

  
496

  
497

  
498
# Include the dependency files.
499
-include $(shell mkdir .dep 2>/dev/null) $(wildcard .dep/*)
500

  
501

  
502
# Listing of phony targets.
503
.PHONY : all begin finish end sizebefore sizeafter gccversion \
504
build elf hex eep lss sym coff extcoff \
505
clean clean_list program debug gdb-config
506

  

Also available in: Unified diff