Sei sulla pagina 1di 10

= AVR USB with ATmega 88/168/328/644[P] =

== Goal ==
* Build AVR USB development environment
* It should be:
* Self-Sufficient
* The board should have USB bootloader to update program
* Failure recovery well considered
* Given two boards, even if s/he mistakenly overrides USB bootloader, it shou
ld be possible to update bootloader using another board.
== Hardware ==
* Fast enough
* It should run maximam speed
* 20MHz clock
* Vcc should be 5V
* Use Zener diode method to lower D-/D+ signal
* http://avrusb.wikidot.com/hardware
== Bootstrap process of development environment ==
1. [wiki:USBserialAVRwriter]: Build a AVR-programmer with USB-serial (FT232RL)
module
* http://akizukidenshi.com/catalog/items2.php?p=1&q=%22K-01977%22
* Connection:
* reset=!rts sck=!dtr mosi=!txd miso=!cts
* xtal
* Changes in /etc/avrdude.conf
{{{
programmer
id
= "fsij";
desc = "design fsij serial, reset=!rts sck=!dtr mosi=!txd miso=!cts";
type = serbb;
reset = ~7;
sck = ~4;
mosi = ~3;
miso = ~8;
;
#
}}}
2. Test ATmega88 with SerialAVRwriter.
* Application: LED on/off for PC5-pin.
{{{
#define F_CPU 20000000
#include <avr/io.h>
#include <util/delay.h>
main()
{
DDRC |= _BV(DDC5);
PORTC = 0;
while(1) {
PORTC ^= _BV(PC5); // toggle PC5
_delay_ms(500);
}
}

}}}
3. Write the firmware of USBasp into ATmega88, using USBserialAVRwriter.
* I used http://www.fischl.de/usbasp/usbasp.2007-10-23.tar.gz
* Chages in Makefile:
{{{
TARGET=atmega88
HFUSE=0xdd
LFUSE=0xff
}}}
* Compilation
{{{
Compilation started at Wed Jul 2 09:22:35
make main.hex
avr-gcc -Wall -O2 -Iusbdrv -I. -mmcu=atmega88 -c usbdrv/usbdrv.c -o usbdrv/usbd
rv.o
avr-gcc -Wall -O2 -Iusbdrv -I. -mmcu=atmega88 -x assembler-with-cpp -c usbdrv/u
sbdrvasm.S -o usbdrv/usbdrvasm.o
avr-gcc -Wall -O2 -Iusbdrv -I. -mmcu=atmega88 -c usbdrv/oddebug.c -o usbdrv/odd
ebug.o
avr-gcc -Wall -O2 -Iusbdrv -I. -mmcu=atmega88 -c isp.c -o isp.o
avr-gcc -Wall -O2 -Iusbdrv -I. -mmcu=atmega88 -c clock.c -o clock.o
avr-gcc -Wall -O2 -Iusbdrv -I. -mmcu=atmega88 -c main.c -o main.o
avr-gcc -Wall -O2 -Iusbdrv -I. -mmcu=atmega88 -o main.bin usbdrv/usbdrv.o usbdr
v/usbdrvasm.o usbdrv/oddebug.o isp.o clock.o main.o -Wl,-Map,main.map
rm -f main.hex main.eep.hex
avr-objcopy -j .text -j .data -O ihex main.bin main.hex
Compilation finished at Wed Jul 2 09:22:38
}}}
* Program (using USBserialAVRwriter)
{{{
usbasp.2007-10-23/firmware$ avrdude -c fsij -p atmega88 -P /dev/ttyUSB0 -U flash
:w:main.hex
avrdude: AVR device initialized and ready to accept instructions
Reading | ################################################## | 100% 0.59s
avrdude: Device signature = 0x1e930a
avrdude: NOTE: FLASH memory has been specified, an erase cycle will be performed
To disable this feature, specify the -D option.
avrdude: erasing chip
avrdude: reading input file "main.hex"
avrdude: input file main.hex auto detected as Intel Hex
avrdude: writing flash (3828 bytes):
Writing | ################################################## | 100% 751.32s
avrdude:
avrdude:
avrdude:
avrdude:
avrdude:
avrdude:

3828 bytes of flash written


verifying flash memory against main.hex:
load data flash data from input file main.hex:
input file main.hex auto detected as Intel Hex
input file main.hex contains 3828 bytes
reading on-chip flash data:

Reading | ################################################## | 100% 740.56s


avrdude: verifying ...
avrdude: 3828 bytes of flash verified

avrdude: safemode: Fuses OK


avrdude done. Thank you.
usbasp.2007-10-23/firmware$ avrdude -c fsij -p atmega88 -P /dev/ttyUSB0 -U hfuse
:w:0xdd:m -U lfuse:w:0xff:m
avrdude: AVR device initialized and ready to accept instructions
Reading | ################################################## | 100% 0.59s
avrdude: Device signature = 0x1e930a
avrdude: reading input file "0xdd"
avrdude: writing hfuse (1 bytes):
Writing | ################################################## | 100% 0.20s
avrdude:
avrdude:
avrdude:
avrdude:
avrdude:

1 bytes of hfuse written


verifying hfuse memory against 0xdd:
load data hfuse data from input file 0xdd:
input file 0xdd contains 1 bytes
reading on-chip hfuse data:

Reading | ################################################## | 100% 0.19s


avrdude:
avrdude:
avrdude:
avrdude:

verifying ...
1 bytes of hfuse verified
reading input file "0xff"
writing lfuse (1 bytes):

Writing | ################################################## | 100% 0.19s


avrdude:
avrdude:
avrdude:
avrdude:
avrdude:

1 bytes of lfuse written


verifying lfuse memory against 0xff:
load data lfuse data from input file 0xff:
input file 0xff contains 1 bytes
reading on-chip lfuse data:

Reading | ################################################## | 100% 0.19s


avrdude: verifying ...
avrdude: 1 bytes of lfuse verified
avrdude: safemode: Fuses OK
avrdude done. Thank you.
usbasp.2007-10-23/firmware$
}}}
4. USBasp: Build USBasp using a board by !WsNak. [[Image(AVR-USBasp-writer.jpg)
]]
* JP3 (Slow CLK) is important for bootstrap.
5. Firmware:USBaspLoader
* Get USBaspLoader.2008-02-05.tar.gz and avrusb-20080513.tar.gz
* Replace firmware/usbdrv with the one of avrusb-20080513
* Because new version supports 20MHz clock
* Apply following change for ATmega 644P
{{{
--- main.c~
2008-02-06 03:42:35.000000000 +0900

+++ main.c
2008-07-02 14:40:41.000000000 +0900
@@ -100,6 +100,8 @@
0x1e, 0x94, 0x06, 0
#elif defined (__AVR_ATmega328P__)
0x1e, 0x95, 0x0f, 0
+#elif defined (__AVR_ATmega644P__)
+
0x1e, 0x96, 0x0a, 0
#else
# error "Device signature is not known, please edit main.c!"
#endif
}}}
{{{
--- Makefile~ 2008-02-06 03:42:35.000000000 +0900
+++ Makefile
2008-07-02 20:46:09.000000000 +0900
@@ -14,10 +14,10 @@
#
make flash # to load the boot loader into flash
#
make lock
# to protect the boot loader from overwriting
-F_CPU = 16000000
-DEVICE = atmega168
-BOOTLOADER_ADDRESS = 3800
-FUSEOPT = $(FUSEOPT_168)
+F_CPU = 20000000
+DEVICE = atmega644p
+BOOTLOADER_ADDRESS = e000
+FUSEOPT = $(FUSEOPT_644P)
LOCKOPT = -U lock:w:0x2f:m
PROGRAMMER = -c stk500v2 -P avrdoper
@@ -26,6 +26,7 @@
FUSEOPT_8 = -U hfuse:w:0xc0:m -U lfuse:w:0x9f:m
FUSEOPT_88 = -U hfuse:w:0xd6:m -U lfuse:w:0xdf:m -U efuse:w:0x00:m
FUSEOPT_168 = -U hfuse:w:0xd6:m -U lfuse:w:0xdf:m -U efuse:w:0x00:m
+FUSEOPT_644P = -U hfuse:w:0xd6:m -U lfuse:w:0xdf:m -U efuse:w:0xfe:m
# You may have to change the order of these -U commands.
#--------------------------------------------------------------------@@ -70,6 +71,31 @@
#
| | +--------------- SUT 1..0 (crystal osc, BOD enabled)
#
| +------------------ CKOUT (if 0: Clock output enabled)
#
+-------------------- CKDIV8 (if 0: divide by 8)
+#--------------------------------------------------------------------+# ATMega644P
+#--------------------------------------------------------------------+# Fuse extended byte:
+# 0xfe = 1 1 1 1 1 1 1 0
+#
\-+-/
+#
+------ BODLEVEL 0..2 (110 = 1.8 V)
+#
+# Fuse high byte:
+# 0xd0 = 1 1 0 1 0 0 0 0 <-- BOOTRST (boot reset vector at 0x7000)
+#
^ ^ ^ ^ ^ \+/
+#
| | | | | +------- BOOTSZ (00 = 8k bytes)
+#
| | | | + --------- EESAVE (preserve EEPROM over chip erase)
+#
| | | +-------------- WDTON (if 0: watchdog always on)
+#
| | +---------------- SPIEN (allow serial programming)
+#
| +------------------ JTAGEN (JTAG wire enable if 0)
+#
+-------------------- OCDEN (OCD enable if 0)
+#
+# Fuse low byte:

+# 0xdf = 1 1 0 1 1 1 1 1
+#
^ ^ \ / \--+--/
+#
| | |
+------- CKSEL 3..0 (external >8M crystal)
+#
| | +--------------- SUT 1..0 (crystal osc, BOD enabled)
+#
| +------------------ CKOUT (if 0: Clock output enabled)
+#
+-------------------- CKDIV8 (if 0: divide by 8)
###############################################################################
}}}
* Edit bootloaderconfig.h, if needed
* compile
{{{
$ make main.hex
}}}
6. Build final board. [[Image(Self-USBprogrammable-ATmega644P.jpg)]]
7. Using USBasp, write USBaspLoader firmware into ATmega644P (and ATmega88).
* At the beginning, put short-pin onto JP3 for slow mode, if it doesn't work
well.
{{{
USBaspLoader.2008-02-05/firmware# avrdude -p m644p -c usbasp -U flash:w:main.hex
:i
avrdude: AVR device initialized and ready to accept instructions
Reading | ################################################## | 100% 0.02s
avrdude: Device signature = 0x1e960a
avrdude: NOTE: FLASH memory has been specified, an erase cycle will be performed
To disable this feature, specify the -D option.
avrdude: erasing chip
avrdude: reading input file "main.hex"
avrdude: writing flash (33052 bytes):
Writing | ################################################## | 100% 6.06s

avrdude:
avrdude:
avrdude:
avrdude:
avrdude:

33052 bytes of flash written


verifying flash memory against main.hex:
load data flash data from input file main.hex:
input file main.hex contains 33052 bytes
reading on-chip flash data:

Reading | ################################################## | 100% 9.42s

avrdude: verifying ...


avrdude: 33052 bytes of flash verified
avrdude: safemode: Fuses OK
avrdude done. Thank you.
USBaspLoader.2008-02-05/firmware# avrdude -p m644p -c usbasp -U hfuse:w:0xd0:m U lfuse:w:0xdf:m -U efuse:w:0xfe:m
avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.01s


avrdude: Device signature = 0x1e960a
avrdude: reading input file "0xd0"
avrdude: writing hfuse (1 bytes):
Writing | ################################################## | 100% 0.02s
avrdude:
avrdude:
avrdude:
avrdude:
avrdude:

1 bytes of hfuse written


verifying hfuse memory against 0xd0:
load data hfuse data from input file 0xd0:
input file 0xd0 contains 1 bytes
reading on-chip hfuse data:

Reading | ################################################## | 100% 0.00s


avrdude:
avrdude:
avrdude:
avrdude:

verifying ...
1 bytes of hfuse verified
reading input file "0xdf"
writing lfuse (1 bytes):

Writing | ################################################## | 100% 0.00s


avrdude:
avrdude:
avrdude:
avrdude:
avrdude:

1 bytes of lfuse written


verifying lfuse memory against 0xdf:
load data lfuse data from input file 0xdf:
input file 0xdf contains 1 bytes
reading on-chip lfuse data:

Reading | ################################################## | 100% 0.00s


avrdude:
avrdude:
avrdude:
avrdude:

verifying ...
1 bytes of lfuse verified
reading input file "0xfe"
writing efuse (1 bytes):

Writing | ################################################## | 100% 0.08s


avrdude:
avrdude:
avrdude:
avrdude:
avrdude:

1 bytes of efuse written


verifying efuse memory against 0xfe:
load data efuse data from input file 0xfe:
input file 0xf6 contains 1 bytes
reading on-chip efuse data:

Reading | ################################################## | 100% 0.00s


avrdude: verifying ...
avrdude: 1 bytes of efuse verified
avrdude: safemode: Fuses OK
avrdude done. Thank you.
}}}
8. Now, we can enjoy ATmega88/ATmega644P programming.
* test a program LED on/off on PA0.
{{{
#define F_CPU 20000000
#include <avr/io.h>
#include <util/delay.h>

main()
{
DDRA |= _BV(DDA0);
PORTA = 0;
while(1) {
PORTA ^= _BV(PA0); // toggle PA0
_delay_ms(500);
}
}
}}}
* Compile
{{{
$ avr-gcc -mmcu=atmega644p -o avr-pa0.bin avr-pa0.c
$ avr-objcopy -j .text -j .data -O ihex avr-pa0.bin avr-pa0.hex
}}}
* Program
{{{
# avrdude -p atmega644p -c USBasp -v -U flash:w:avr-pa0.hex:i
avrdude: Version 5.5, compiled on Mar 4 2008 at 22:20:21
Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
System wide configuration file is "/etc/avrdude.conf"
User configuration file is "/home/gniibe/.avrduderc"
User configuration file does not exist or is not a regular file, skippi
ng
Using Port
Using Programmer
AVR Part
Chip Erase delay
PAGEL
BS2
RESET disposition
RETRY pulse
serial program mode
parallel program mode
Timeout
StabDelay
CmdexeDelay
SyncLoops
ByteDelay
PollIndex
PollValue
Memory Detail

W
-00
00
00

:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:

/dev/parport0
USBasp
ATMEGA644P
9000 us
PD7
PA0
dedicated
SCK
yes
yes
200
100
25
32
0
3
0x53

Block Poll
Polled
Memory Type Mode Delay Size Indx Paged Size
ReadBack
----------- ---- ----- ----- ---- ------ -------------eeprom
65
10 128
0 no
2048
0xff 0xff
flash
33
6 256
0 yes
65536
0xff 0xff
lock
0
0
0
0 no
1
0x00 0x00

Page
Size #Pages MinW Max
---- ------ ----- --8

0 9000 90

256

256 4500 45

0 9000 90

lfuse
00 0x00 0x00
hfuse
00 0x00 0x00
efuse
00 0x00 0x00
signature
0 0x00 0x00
calibration
0 0x00 0x00

0 no

0 9000 90

0 no

0 9000 90

0 no

0 9000 90

0 no

0 no

Programmer Type : usbasp


Description
: USBasp, http://www.fischl.de/usbasp/
avrdude: AVR device initialized and ready to accept instructions
Reading | ################################################## | 100% 0.01s
avrdude:
avrdude:
avrdude:
avrdude:
avrdude:

Device signature = 0x1e960a


safemode: lfuse reads as 0
safemode: hfuse reads as 0
safemode: efuse reads as 0
NOTE: FLASH memory has been specified, an erase cycle will be performed
To disable this feature, specify the -D option.
avrdude: erasing chip
avrdude: reading input file "avr-pa0.hex"
avrdude: writing flash (3558 bytes):
Writing | ################################################## | 100% 0.68s

avrdude:
avrdude:
avrdude:
avrdude:
avrdude:

3558 bytes of flash written


verifying flash memory against avr-pa0.hex:
load data flash data from input file avr-pa0.hex:
input file avr-pa0.hex contains 3558 bytes
reading on-chip flash data:

Reading | ################################################## | 100% 0.57s

avrdude: verifying ...


avrdude: 3558 bytes of flash verified
avrdude:
avrdude:
avrdude:
avrdude:

safemode:
safemode:
safemode:
safemode:

lfuse
hfuse
efuse
Fuses

avrdude done. Thank you.

reads as 0
reads as 0
reads as 0
OK

#
}}}
---= Information =
== Downloader ==

* http://www.obdev.at/products/avrusb/usbasploader.html
== Programmer ==
=== Firmware ===
* USBaspLoader: USBasp compatible bootloader http://www.obdev.at/products/avrus
b/usbasploader.html
=== Software ===
* AVRdude: http://www.bsdhome.com/avrdude/
=== Hardware ===
* USBasp: http://www.fischl.de/usbasp/index.html
* ATmega48/88/168 AVR writer: ~~https://www3.t-agency.net/~wsnak/kit/172/index
.htm~~
http://www.wsnak.com/kit/172/index.htm
* USBprog: http://www.embedded-projects.net/index.php?page_id=165
* Kit 32+5 Euro: http://shop.embedded-projects.net/product_info.php?info=p6_usb
prog-v3-0--adaptor-preassembled-.html&XTCsid=c402f7c4dcad9a3530089f2e05402bc9
* !AvrUsb500v2 (stk500 V2 compatible): http://tuxgraphics.org/electronics/20070
5/article07052.shtml
== Links ==
Some pages are in Japanese...

* AVR FAQ in Japanese: http://avrwiki.jpn.ph/wiki.cgi?page=AVR%A4%E8%A4%AF%A4%A


2%A4%EB%BC%C1%CC%E4%28FAQ%29%A1%A1%A5%C7%A5%D0%A5%A4%A5%B9%CA%D4#p5
* USBAVR (in Japanese): http://www11.ocn.ne.jp/~akibow/AVR/usbwriter.html#USBa
* AVR Freaks: http://www.avrfreaks.net/
* Simple AVR Programmers: http://elm-chan.org/works/avrx/report_e.html
* FTDI 232 patch for Avrdude: http://savannah.nongnu.org/patch/?4330
* AVR (in Japanese): http://www-ice.yamagata-cit.ac.jp/ken/sensh
Fwriter#content_1_10
* AVRICAVR: https://www3.t
* molex 52745 connector: http://jp.rs-online.com/web/search/searchBrowseAction.
html?method=getProduct&R=5426817
* FPC : http://www.sunhayato.co.jp/products/details.php?u=302&id=07009
* CX5032 : http://www.kyocera.co.jp/prdct/electro/product/timing/xtal.html
* http://www.glyn.co.nz/Displays_4DSystems.htm#uUSBCE5
* http://www.macetech.com/blog/?q=node/46
---Following is written in Japanese about purchase of electric parts.
== ==
=== 2008-07-01 ===
*
* JPY935.*
* 2.54mm 6pin JPY80x2
* USB(8) H-UL JPY140x2
* (6) H-AL JPY130x2

=== 2008-06-28 ===


*
* USB A 4 1p JPY230 x 2
* RS232C 9pin-9pin JPY480
=== 2008-06-26 ===
*
* EIC-1 JPY600
* EIC-3 JPY150 x 2
* 6P JPY50 x 2
* ()
* 2.54mm 6p JPY50 x 4
* x50 JPY368
=== 2008-06-25 ===
*
* 3.9kohm 100pcs JPY100
* 2.2kohm 100pcs JPY100
* 10kohm 100pcs JPY100
* 100ohm 100pcs JPY100
* 28pin slim IC socket x2 JPY160
*
* 68ohm 200pcs JPY200
=== () 2008-06-18 ===
{{{

C002 USB B
C010 12MHz
C011 3.6V (2)
P172 #172 ATmega48/88/168 /AVR

()
}}}
=== : 2008 06 17 ===
* Sunhayato USB CK 20
* 760()
* 20MHz
* 500(10)
* 3.6V() NEC RD3.6E 0.5W
* 100(5)
* ATmega88 20PU
* 2504
* ATmega644P 20PU
* 5502

60
70
50
500

5
2
2

300
140
100
3
1500
600

2640

Potrebbero piacerti anche