How to flash an ATmega8
For a class in my university, I have to flash a ATmega8 Chip. This is on a custom PCB including a own quartz, to allows the usage of a USB Port as interface instead of a classic ICSP.
The chip will be registered to your PC as USB devices.
On a Windows System you can use the Atmel Studio,
to program it. It will create special a .hex
file, including the compiled code of yours.
With the bootloadHID
tool it will be send to firmware to the chip.
However for Linux the solution is a bit different. Atmel Studo is not working here, they support only Windows. So you have to do the steps in a manual way. Following steps:
- Setup the bootloadHID
- Generate the
.hex
files - Upload
On Linux
When the chip is connected to your system it will register, but the kernel can not make much sense of this:
[22182.023379] usb 1-2: new full-speed USB device number 18 using xhci_hcd
[22182.136834] usb 1-2: device descriptor read/64, error -71
[22182.356816] usb 1-2: device descriptor read/64, error -71
[22182.576688] usb 1-2: new full-speed USB device number 19 using xhci_hcd
[22182.690098] usb 1-2: device descriptor read/64, error -71
[22182.910128] usb 1-2: device descriptor read/64, error -71
[22183.130070] usb 1-2: new full-speed USB device number 20 using xhci_hcd
[22183.130392] usb 1-2: Device not responding to setup address.
[22183.337045] usb 1-2: Device not responding to setup address.
[22183.543380] usb 1-2: device not accepting address 20, error -71
[22183.656720] usb 1-2: new full-speed USB device number 21 using xhci_hcd
[22183.657038] usb 1-2: Device not responding to setup address.
[22183.863701] usb 1-2: Device not responding to setup address.
[22184.070053] usb 1-2: device not accepting address 21, error -71
[22184.070116] usb usb1-port2: unable to enumerate USB device
Yet this is right, it’s a sign that the chip can be flashed. Just switch the hard switch to ‘Boot’. Now it could be flashed with bootloadHID.
Setup of bootloadHID
Someone was so nice and ported (or create it’s own?) the bootloadHID to Linux. So you’ll need to setup up this tool to flash the ATMega8. Grep the source code from github:
cd src/
~/src> git clone https://github.com/robertgzr/bootloadHID
cd commandline/
make VENDORID=0x16c0 PRODUCTID=0x05DF
The import details are the parameter for VENDORID
and PRODUCTID
, this determine which chip you’re running. I think you might can extract this infromation from the datasheet of the chip. For me someone told me what parameter I have to use.
Without this parameter, the compiler will produce wrong code that most likely not work on the chip.
The make
command will create a the ./bootloadHID
. This will be later used to flash.
Generating .hex
files
In Linux we don’t have a Atmel Studio, so we need to create the .hex
manual.
I checked the make
command the Studio use to create a .hex
file.
avr-gcc.exe -x c -funsigned-char -funsigned-bitfields -DDEBUG -O1 -ffunction-sections -fdata-sections -fpack-struct -fshort-enums -g2 -Wall -mmcu=atmega8 -c -std=gnu99 -MD -MP -MF "$(@:%.o=%.d)" -MT"$(@:%.o=%. d)" -MT"$(@:%.o=%.o)" -o "$@" "$<"
avr-gcc-x c -funsigned-char -funsigned-bitfields -DDEBUG -O1 -ffunction-sections -fdata-sections -fpack-struct -fshort-enums -g2 -Wall -mmcu=atmega8 -c -std=gnu99
The essential steps are:
avr-gcc -g -Os -mmcu=atmega8 -c demo.c
avr-gcc -g -mmcu=atmega8 -o demo.elf demo.o
avr-objcopy -j .text -j .data -O ihex demo.elf demo.hex
This will device a flashable file as hex code. The Atmel Handbook provide further details on this topic. You just need to read some more pages.
Upload
Now we can upload our code onto the chip via:
sudo ./bootloadHID demo.hex
Don’t forget to switch from ‘Boot’ mode away and a press on reset will start the new installed programm on the chip!
SublimeAVR
There is also a plugin for Sublime Text 3 that allows
the usage of this script. I just took the Makefile
from the project.