May 25, 2012

AVR firmware uploading tools

    Imagine you have an [AVR] microcontroller with bootloader inside. You have written some program for  AVR and you need to compile&upload it inside. How you can do that? Let's see how to do it on example of [ATmega64A] which is installed on a Tank device from [Open System].


0
Preparations
    For Windows:
1
Download CP210x USB to UART Bridge VCP Drivers from [here].
2
Get [avrdude] for windows. Visit this [page] to see avrdude's parameters.
3
Connect device to PC via USB.
4
Install bridge drivers. A new COM-device should appear in system. Remember it's name.
5
Reboot.
    For Linux:
1
Visit this [page] to see what you should do to install AVR-GCC toolchain.
2
Connect device to PC via USB.
3
Run 'lsusb' to see vendor's and product's IDs:
alexlaptop alex # lsusb
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 003 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 004 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 005 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 006 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 007 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 008 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 002 Device 003: ID 064e:a103 Suyin Corp. Acer/HP Integrated Webcam [CN0314]
Bus 006 Device 002: ID 093a:2510 Pixart Imaging, Inc. Optical Mouse
Bus 003 Device 002: ID 10c4:ea60 Cygnal Integrated Products, Inc. CP210x Composite Device
As you can see our device is on the last line. Vendor's ID is '10c4' and product's ID is 'ea60'. Lets remember them:
alexlaptop alex # idVendor=10c4
alexlaptop alex # idProduct=ea60
echo $idVendor:$idProduct
10c4:ea60
4
Now create new USB rule:
alexlaptop alex # echo "ATTRS{idVendor}==\"$idVendor\",  ATTRS{idProduct}==\"$idProduct\", MODE=\"0660\", GROUP=\"users\"" > /etc/udev/rules.d/usb_bridge.rules
alexlaptop alex # cat /etc/udev/rules.d/usb_bridge.rules
ATTRS{idVendor}=="10c4",  ATTRS{idProduct}=="ea60", MODE="0660", GROUP="users"
5
Update rules:
alexlaptop alex # udevadm control --reload-rules
6
Remember your device. For this run:
alexlaptop alex # find /dev/ -name "*usb*"
and then look for your device's name. As for me it was:
/dev/serial/by-id/usb-Silicon_Labs_CP2102_USB_to_UART_Bridge_Controller_0001-if00-port0

1
Atmel Studio
   Atmel for Atmel. Register for free [here] to download [Atmel Studio]. You'll get the download link right after email confirmation. Download size is above 700 MiB (too much? it's Visual Studio based, so what did you expect?). Installation includes studio itself, compilers/linkers and USB-drivers.
   Downloaded. Installed. Launched.

1
Click 'New Project...'
2
Select 'GCC C Executable Project'. Enter project's name and location.


3
Select 'ATmega64A' as target device. You can get datasheet just following the link at the right side.


4
Get your main file template automatically created.
/*
 * tank_test.c
 *
 * Created: 25.05.2012 13:08:47
 *  Author: alex
 */ 

#include <avr/io.h>

int main(void)
{
    while(1)
    {
        //TODO:: Please write your application code 
    }
}
5
Set project configuration to 'Release' instead of 'Debug'. Click 'Build->Build Solution' or just press F7. After building you will get .elf and .hex files.
6
Upload .hex file to the device. To do that you need press "reset" button & run avrdude. Script must be running while the device is resetting. You can run this [bat-file]. It's content is shown below.
@echo off
rem Use avrdude as programming-software with the AVRProg compatible bootloader
rem Martin Thomas, 2006

rem Verfiy that the bootloader is configured with #define DEVTYPE DEVTYPE_ISP
rem since it seems that avrdude does not work with "Boot" device-types and needs
rem ISP device-types (at least in version 5.1 as in WinAVR 4/06)

set HEXFILE=tank_test.hex
set PROGRAMMER=avr109
set PORT=com3
set PART=m64

rem * disable safemode - bootloader can not "restore" fuses anyway
set DIS_SAVE=-u

avrdude %DIS_SAVE% -p %PART% -P %PORT% -c %PROGRAMMER% -v -U flash:w:%HEXFILE% 

pause
HEXFILE is a path to your previously compiled .hex-file.
PROGRAMMER is set to avr109 because [this] bootloader is used.
PORT is com3, it's virtual port installed earlier.
PART is m64 = ATmega64

After running script you should see lots of logs and this message at the end:
arvdude done. Thank you.
   So, your program was uploaded. In current example program does nothing, but nevertheless, congrats!
2
WinAVR
   WinAVR's [homepage].
3
CodeVisionAVR
   CodeVisionAVR's [homepage].
4
Code::Blocks
   You can search the web to see how to use [Code::Blocks] for AVR projects development. On
5
Eclipse AVR Plugin
1
Get [Eclipse] and launch it. Edition does not matter. I had 'Java EE' edition and it's OK.
2
Go to 'Help->Eclipse Market place...', type 'avr' and press 'enter' to find 'AVR Eclipse Plugin'.

3
Install plugin and restart Eclipse.
4
Create new project: 'File->New->Other...' (or just press Ctrl+N). Select 'C Project'.


Go next, set project's name and location. Select 'AVR Cross Target Application'.


Go next and next again. Select your target device and it's working frequency.


Click 'Finish'.
5
Create new or add existing C-source file. Let it be a program, which blinks 2 LEDs with periods of 1 and 2 seconds. Source file can be viewed [here].
6
Compile your program to get .hex-file.
7
Go to 'Project->Properties->AVR->AVRDude'.


8
In 'Programmer' tab click 'New...' near 'Programmer configuration'. Set configuration's name, select 'Atmel AppNote AVR109 Boot Loader' as programmer hardware, and path to your USB device:


Click 'Ok', 'Ok' to apply settings and close dialogues.
9
Upload your firmware to the device: press 'Reset' button on the device & click
'AVR->Upload Project to Target Device' (or just press 'Ctrl+Alt+U'). Remember: you must upload firmware while device is resetting.

May 24, 2012

XeLaTex application form example

    I've created an application form for 2nd phase of Olympiad for my university.       
   Source code can be downloaded from [here]. click [here] to get PDF, do not look via dropbox viewer. The result looks like:

May 22, 2012

XeLaTex unicode blank

   Here is a script to create a blank XeLaTex document with cyrillic letters support:
%&program=xelatex
%&encoding=UTF-8 Unicode

\documentclass[a4paper]{article}
\pagestyle{empty}

\usepackage{geometry}
\geometry{top=2cm}
\geometry{left=2cm}
\geometry{bottom=2cm}
\geometry{right=1cm}

\usepackage[utf8]{inputenc}
\usepackage[russian]{babel}
\usepackage{cmap}
\usepackage{fontspec}
\setmainfont{FreeSerif}

\begin{document}
\fontsize{14pt}{14pt}
\selectfont
\begin{center}
\textbf{\uppercase{I'm header. Я заголовок}\\
I'm subheader. Я подзаголовок
}
\end{center}
The quick brown fox jumps over the lazy dog.\\
Съешь ещё этих мягких французских булок, да выпей же чаю.
\end{document}
   The result is (click to enlarge):



Things to note:

    pagestyle is set to 'empty', so no header/footer will be displayed on page. By default footer contains page number.

    You can use documentclass to set font size in such way:
\documentclass[12pt, a4paper]{article}
    In this case you should keep in mind that class 'article' allows to use
10-12pt sizes only. To use another size you should set it with fontsize just after document's body beginning. Do not forget to apply it with selectfont.

    To make it possible to use fontspec package this script must be compiled with XeLaTex instead of LaTex.

    If you are using Texmaker, you can add a hotkey to compile with XeLaTex and run preview with Evince. To do this go to
User->User Comands->Edit User Comands
    then enter 'XeLaTex' (or whatever you like) as menu's title and enter next command:
xelatex -interaction=nonstopmode -synctex=-1 %.tex | evince %.pdf

    This will be looking something like below:

    Voila, you can compile and run preview with 'Alt+Shift+F1' hotkey (or another selected one).