You are on page 1of 18

tinyurl.

com/SanUSB

05/06/2011

SanUSB
The transfer application firmware for microcontrollers is usually accomplished through
a specific hardware programmer. Through the SanUSB tool, executable in Linux, Mac
OSX and Windows operating systems, it is possible the discharge of application firmware
for microcontrollers directly via USB, as well as generating a virtual serial communication
port using the USB interface. The SanUSB tool allows to program application .hex into the
chip using the standard USB connectivity of PIC microcontrollers.
This tool is composed of software and a circuit based on the microcontroller with USB
interface for the development of embedded systems [1], i.e., systems that manipulate data
inside another larger system [2].
To facilitate the tool application, the software is free and available from the archives at
http://www.tinyurl.com/SanUSB [3]. Using this tool, students of IFCE were consecutively
three time champions of the IFCE Robotics Competition (2007, 2008 and 2009),
champions of the Brazilian Fair of Science and Engineering (FEBRACE09) in the
University of So Paulo in Engineering Category (2009), obtained the Innovation Award
for Technology Application at the 2009 Feria Explora Medellin in Colombia and became
Champions in the International Forum of Science and Engineering 2010 in Chile in the
Supranivel Category [4-8].
Using the SanUSB tool, it also is possible to suppress the following items in the
development of embedded systems:
(a)

A specific device for microcontroller memory programming;

(b)

TTL/RS-232 interface for bi-directional serial communication protocol emulated by

USB communication device class (CDC);


(c)

Power supply, since the power source is derived from the PC USB channel;

(d)

Externally analog-digital converter (ADC), because the microcontroller has

internally 10 ADC of 10 bits. Using externally ADC increases the cost of data acquisition
cards;
(e) Simulation software, whereas the simulation program and hardware can be made
quickly and effectively in their own development circuit.
Besides all these advantages, modern laptops and computers of today no longer have
parallel and serial EIA/RS-232 communication interfaces, only USB.

tinyurl.com/SanUSB

05/06/2011

The SanUSB tool enables user-friendly programming and also real debugging directly
through the virtual serial emulation. This can be created quickly and effectively at the very
moment the microcontroller is connected directly to a PC, through the USB interface.
Figure 1 shows an illustration of the SanUSB tool programming process.
Figure 1. Illustration of the SanUSB tool programming process.

To enable all these features, compatible with Linux, Mac OSX and Windows
operating systems, is necessary to burn previously only once an USB manager
gerenciador.hex file with a specific microcontroller hardware or with the simple
programming circuit described at the end of this tutorial.
The application firmware is sent directly from the PC to the USB microcontroller with
the programming interface. Initially, the microcontroller pin 1 must be connected to Gnd
via a push button while the USB cable is connected between PC and microcontroller, then
the compiled application firmware .hex file can be selected in Open button of the
programming interface shown in Figure 2 and the microcontroller is programmed by
pressing the Burn & Reset button.

tinyurl.com/SanUSB

05/06/2011

Figure 2. SanUSB tool programming Interface.

It is possible to download this software tool at the following link:


http://www.4shared.com/file/sIZwBP4r/100727SanUSB.html

For application programs in C can be written to the microcontroller via USB, it is


necessary to compile and to turn them into machine language .hex. There are several
compilers that can be used by this tool, including the SDCC, the C18, Hi-Tech and CCS. At
the end of this tutorial there is firmware models to use the SanUSB tool with mentioned C
compilers.
The basic mounted SanUSB circuit on a breadboard with 28 and 40 pins
microcontroller are shown in Figures 3 and 4:

tinyurl.com/SanUSB

Figure 3. Basic SanUSB circuit with 28 pins microcontroller.

Figure 4. Basic SanUSB circuit with 40 pins microcontroller.

05/06/2011

tinyurl.com/SanUSB

05/06/2011

The basic circuit components are:


a PIC microcontroller USB family (18F2550, 18F2455, 18F4550, etc.).
a crystal of 20MHz;
two 22pF capacitors;
two 1uF capacitors;
a 2k2 pull-up resistor and a button for programming at pin 1;
a diode;
and USB cable.

Note that this multi-platform can also be implemented in any development PIC
microcontroller board because it uses the reset button at pin 1 as the programming
button via USB. When you connect the USB cable with pin 1 on the Gnd (0V, the
microcontroller enters in programming state via USB (LED at pin B7 on) and after the
reset, with pin 1 on the Vcc (through the fixed 2K2 pull-up resistor), the microcontroller
comes into operation state of the application firmware that was compiled.
The figure 5 shows a photo of the SanUSB tool mounted on breadboard following
the previous circuit and also the position of each terminal on the USB connector to be
connected to the PC.

Figure 5. Basic SanUSB circuit with 40 pins microcontroller.

The figure 6 shows the PCB, available in the Archives of SanUSB Group, and the
circuit for this tool with input for external power supply.
.

tinyurl.com/SanUSB

05/06/2011

Figure 6. Basic SanUSB PCB.

If you prefer to make the card, you can also print on glossy paper black PCB shown
in Figure 7 is already in full size, corrode, stick (brown spots) and solder the components.
More information available at: http://www.youtube.com/watch?v=8NhNsNw5BfU.
Figure 7. Basic SanUSB PCB circuit design.

tinyurl.com/SanUSB

05/06/2011

During the C program development just insert at the beginning of the program in C,
the library SanUSB header (# include <SanUSB.h>) contained within the Examples and
Library CCS folder you should have added, along with the other libraries in this folder
within the Drivers located in the installed folder of the compiler (C: \ program Files \ PICC
\ Drivers). This library contains the instructions for the USB PIC operating system settings.
The program example 1 below contained in the folder Examples, switch a led
connected to pin B7 every 0.5 seconds.
#include <SanUSB.h>
void main()
{
clock_int_4MHz();//Function necessary to enable the dual clock (48MHz for USB and 4MHz for CPU)
while (1)
{
output_toggle(pin_B7); // comuta Led na funo principal
delay_ms(500);
}
}

The program below blinks three 3 leds connected to pins B5, B6 and B7.
#include <SanUSB.h>
main(){
clock_int_4MHz();//Funo necessria para habilitar o dual clock (48MHz para USB e 4MHz para CPU)
while (1)
{
output_high(pin_B5); // Pisca Led na funo principal
delay_ms(500);
output_low(pin_B5);
output_high(pin_B6);
delay_ms(500);
output_low(pin_B6);
output_high(pin_B7);
delay_ms(500);
output_low(pin_B7);
}
}

The Compiled files. hex as well as the firmware source are available in the folder
Examples available at: http://www.4shared.com/file/sIZwBP4r/100727SanUSB.html
.

tinyurl.com/SanUSB

05/06/2011

Serial Emulation
In this topic is shown a method of bi-directional serial communication channel
through the USB PIC18Fxx5x. One of the simplest ways is through the protocol
Communications Device Class (CDC), which is standard on Linux and Mac OSX and
emulates a virtual COM port RS-232 through the USB channel. Thus, it is possible to
communicate with ASCII characters via USB by software serial monitor RS-232 as the
Cutecom, minicom or other software with serial interface. The application firmware must
heve the CDC library (# include <usb_san_cdc.h>), responsible for this virtual RS-232 port
emulation via USB. The serial emulation is widely used also to debug by printing variables
via USB during actual program execution. Thus, the programmer can find possible errors in
the firmware programming.
For the compilation of a serial emulation application program, such as
exemplo_emulSerial.c. The CDC library (# include <usb_san_cdc.h>) should be in the
same folder as the firmware or installed into the folder of the compiler (C : \ program Files \
PICC \ Drivers). The firmware exemplo_emulSerial.c below commands the state of a led
on pin B7 with the L and D keys on the PC keyboard. The CDC functions most commonly
used for communication with virtual COM are:
usb_cdc_putc () - microcontroller sends ASCII characters emulated via USB.
Example: printf (usb_cdc_putc, "\ r \ nEndereco to write:");

usb_cdc_getc () - retains an emulated ASCII character via USB.

Ex: data = usb_cdc_getc () / / retains a character in data variable

gethex_usb () - holds a hexadecimal number entered from the keyboard.

Example: value = gethex_usb ();// holds a hexadecimal in the value variable

usb_cdc_kbhit () - Warn to TRUE (1) to get a new character in the USB PIC
receive buffer.

05/06/2011

tinyurl.com/SanUSB

Example: if (usb_cdc_kbhit ()) {data = usb_cdc_getc ();}


#include <SanUSB.h>
#include <usb_san_cdc.h>
BYTE comando;
main() {
clock_int_4MHz();
usb_cdc_init(); // Inicializa o protocolo CDC
usb_init(); // Inicializa o protocolo USB
usb_task();
while (TRUE)
{
if (usb_cdc_kbhit( ))
{
comando=usb_cdc_getc();
if (comando=='L') {output_high(pin_b7); printf(usb_cdc_putc, "\r\nLed Ligado!\r\n");}
if (comando=='D') {output_low(pin_b7); printf(usb_cdc_putc, "\r\nLed Desigado!\r\n");}
}
output_high(pin_B6);
delay_ms(300);
output_low(pin_B6);
delay_ms(300);
}}

More

information

can

be

also

obtained

from

the

video:

http://www.youtube.com/watch?v=cRW99T_qa7o .
Enjoy!
sandro_juca@yahoo.com.br

SIMPLE CIRCUIT TO gerenciador.hex PROGRAMMING


For serial microcontroller programming of the gerenciador.hex is necessary only
three resistors of 10 k, a DB9 serial cable (RS-232) and an 5V external voltage supply,
which can be obtained from the USB port. The circuit and the photo below show the simple
connecting pins scheme.

tinyurl.com/SanUSB

05/06/2011

This circuit seen from the PC DB9 COM port can be viewed in the figure below.

This circuit works with the programming software PICPGM (detected as JDM
Programmer) or WinPIC (detected as COM84 Programmer). The Winpic appears more
stable, because after the microcontroller detection, the microcontroller can be programmed,
even pointing ERROR: Programming failed, the file gerenciador.hex proved to be
programmed correctly in the microcontroller to manage programming via the USB port on
Windows , Linux and Mac OSX operating systems.
The software Winpic can be downloaded from www.qsl.net/dl4yhf/winpicpr.html or
http://www.4shared.com/get/1uP85Xru/winpicprCOM84.html.

tinyurl.com/SanUSB

05/06/2011

After installation, run the program. In "Device, Config" select the microcontroller.
Once the microcontroller is connected to RS-232 PC COM port, go to "Interface", select
"COM84 programmer for serial port", and press "Initialize." If the software indicated that
the boot was "Success", then the program is prepared to program the gerenciador.hex in
microcontroller. For the programming, select File and then Load & Program Device and
then select the file gerenciador.hex. As previously mentioned, even after the programming
and verification appear "Programmed Failed", it is likely that the gerenciador.hex has been
programmed correctly.

tinyurl.com/SanUSB

Firmware Header file for CCS C Compiler


SanUSB.h header File:

#include <18F4550.h> //This library 18F4550.h is valid for the whole family USB PIC18Fx5xx
#device ADC=10
#fuses HSPLL,PLL5, USBDIV,CPUDIV1,VREGEN,NOWDT,NOPROTECT,NOLVP,NODEBUG
#byte OSCCON=0XFD3
#use delay(clock=48000000)// USB standard frequency (cpu and timers 12 MIPS = 4/48MHz)
//#use delay(clock=4000000) // internal Oscillator Clock of 4MHz
#use rs232(baud=9600, xmit=pin_c6, rcv=pin_c7)
//SanUSB program memory allocation
#define CODE_START 0x1000
#build(reset=CODE_START, interrupt=CODE_START+0x08)
#org 0, CODE_START-1 {}

void clock_int_4MHz(void)
{
//OSCCON=0B01100110; //with dual clock -> cpu and timers #use delay(clock=4000000)
while(read_eeprom(0xfd));
}

05/06/2011

tinyurl.com/SanUSB

Firmware Modification for Microchip C18 compiler

/*

www.tinyurl.com/SanUSB

*/

#include "p18F4550.h"

void low_isr(void);
void high_isr(void);

#pragma code low_vector=0x1018


void interrupt_at_low_vector(void)
{
_asm GOTO low_isr _endasm
}
#pragma code
#pragma code high_vector=0x1008
void interrupt_at_high_vector(void)
{
_asm GOTO high_isr _endasm
}
#pragma code

#pragma interruptlow low_isr

05/06/2011

tinyurl.com/SanUSB

void low_isr (void)


{
return;
}

#pragma interrupt high_isr


void high_isr (void)
{
return;
}
void main( void )
{
...;
}

Firmware Modification for SDCC


Example Format

/*

www.tinyurl.com/SanUSB

#include <pic18f4550.h>
#pragma code _reset 0x001000
void _reset( void ) __naked
{

*/

05/06/2011

tinyurl.com/SanUSB

__asm
EXTERN __startup
goto __startup
__endasm;
}
#pragma code _high_ISR

0x001008

void _high_ISR( void ) __naked


{
__asm
retfie
__endasm;
}
#pragma code _low_ISR

0x001018

void _low_ISR( void ) __naked


{
__asm
retfie
__endasm;
}
void main() {

05/06/2011

tinyurl.com/SanUSB

Firmware Modification for MikroC


Example Format for Bootloader

/*

www.tinyurl.com/SanUSB

*/

#pragma orgall 0x1000


void interrupt(void) org 0x1008
{
;
}
void interrupt_low(void) org 0x1018
{
;
}
void main()
{

......;

Hi-Tech C Compiler
step1:goto Build option
step2:linker tap
step3:set offset : 1000

05/06/2011

tinyurl.com/SanUSB

05/06/2011

Firmware Modification for Microchip ASM compiler

/*

www.tinyurl.com/SanUSB

*/

processor PIC18F4550
#include"p18f4550.inc"
org 0x1000
goto init
org 0x1020
goto int_isr
init
...

; initialization

...

; code

loop

goto loop
int_isr
...

; interrupt code

retfie
end

REFERENCES
Grupo SanUSB (2011). Arquivos do Grupo SanUSB. Retirado em 05/01/11, no World
Wide Web: www.tinyurl.com/SanUSB/.

tinyurl.com/SanUSB

05/06/2011

Jornal O Povo (2011). Da escola pblica para o mundo. Retirado em 05/01/11, no World
Wide Web:
http://www.opovo.com.br/app/opovo/cienciaesaude/2011/01/08/noticiacienciaesaudejor
nal,2086691/da-escola-publica-para-o-mundo.shtml.
Juc, S. et al.(2011). A low cost concept for data acquisition systems applied to
decentralized renewable energy plants. Retirado em 05/01/11, no World Wide Web:
http://www.mdpi.com/1424-8220/11/1/743 .
Juc, S. et al.(2011). Gravao de microcontroladores PIC via USB pelo terminal do
Linux. Retirado em 05/03/11, no World Wide Web:
http://www.vivaolinux.com.br/artigo/Gravacao-de-microcontroladores-PIC-via-USBpelo-terminal-do-Linux/.
Jornal O Povo (2010). De Maracana para Eslovquia. Retirado em 05/01/11, no World
Wide Web: http://publica.hom.opovo.com.br/page,489,109.html?i=2051467 .
Dirio do Nordeste (2010). Rob cearense. Retirado em 05/01/11, no World Wide Web:
http://diariodonordeste.globo.com/materia.asp?codigo=861891.
TV Dirio (2010). Feira do Empreendedorismo SEBRAE. Retirado em 05/01/11, no World
Wide Web: http://www.youtube.com/watch?v=8Y7gOPd_zN4.
TV Verdes Mares (2009). Estudantes competem com robs. Retirado em 05/01/11, no
World Wide Web: http://tvverdesmares.com.br/bomdiaceara/estudantes-competem-comrobos/.
TV Cidade (2009). Projetos Comsolid/Setapi IFCE. Retirado em 05/01/11, no World Wide
Web: http://www.youtube.com/watch?v=i_waT0_201o.
Juc, S. et al.(2009). SanUSB: software educacional para o ensino da tecnologia de
microcontroladores. Retirado em 05/01/11, no World Wide Web:
http://www.cienciasecognicao.org/pdf/v14_3/m254.pdf .
Dirio do Nordeste (2007). Alunos estimulados a construir robs. Retirado em 05/01/11, no
World Wide Web: http://diariodonordeste.globo.com/materia.asp?codigo=491710.

You might also like