Nav:  [home][elec][pdev][avr][parport] > [serial]
 
Go:  [index] [parallel] [serial] [supply circuit] [software]

Electronics -- Atmel/AVR-programming

Please note that I am now using the USB-based download cable. Content on this page is no longer maintained/developed. However, the presented solution is cheap, easy to build and still working nicely.

Serial downloading circuit

Concerning the "circuit", serial downloading via the SPI interface of the AVR RISC microcontroller is even easier than parallel downloading: You just need 3 wires and some additional circuicy to bring the device into a mode where it accepts commands via the SPI interface and an external clock. Serial downloading is the method of choice if the microcontroller is already built into some device, because it allows in-system programming.

Hence, I assume that we have access to the 3wire SPI interface of an Atmel which is part of some larger device and receives an internal clock which is fast enough for serial downloading. (The system clock must be at least 4 times as fast as the serial clock provided by the programming software via the PC's parallel port. To be exact, the serial clock must be LOW and HIGH for at least 2 system clock cycles each.)

I will not provide a "circuit sheet" here because it is so trivial: You just need the "A" channel of the Latch16Bit board. Make the following connections: (Note: Pin numbers are for the DIL packages.)

Signal Latch16Bit AT90S2313 ATMega161 Description
MOSI Out-A, bit 0 PB5 (pin 17) PB5 (pin 6) Serial data: PC to AVR
MISO In-A, bit 0 PB6 (pin 18) PB6 (pin 7) Serial data: AVR to PC
SCLK Out-A, bit 1 PB7 (pin 19) PB7 (pin 8) Serial data clock
XTAL [external] XTAL1 (pin 5) XTAL1 (pin 19) System clock
RESET [external] RESET (pin 1) RESET (pin 9) Reset signal

That's all. Just make sure the other external components (system clock and microcontroller RESET line) are set up properly: In order to start serial downloading you need to:

  • Reset the microcontroller by setting RESET LOW.
  • Set SCLK LOW (done by software).
  • Give RESET a positive pulse of at least two system clock cycles duration (needs to be done manually).
  • Tell the software to set the device into programming mode.

The above assumed, that the microcontroller is running inside a system with power and clock applied. Alternatively, one can use the following algorithm:

  • Power up the microcontroller with RESET and SCLK tied LOW.
  • Connect system clock.
  • Tell the software to set the device into programming mode.

Troubleshooting:
In case the above does not work and the device fails to go into serial programming mode, check the following:

  • Is the system clock signal fast enough or timings slow enough in software? (I use 1MHz normally.)
  • Is the SPI fuse (SPIEN) unprogrammed ("1") and hence disabling SPI?
  • The usual things (connector reversed, voltage correct, ...)
  • Finally, try a new device. Don't laugh, that solved my problem with the '2313: Parallel programming worked well but it failed to enter serial programming mode until I tried a new device.

A (possibly faster) programmer could be build using the lp port directly without the Latch16Bit board by connecting MISO directly to one of the status lines and using two data lines for MOSI and SCLK. However, I prefer to have the ability to set the port into high-Z state and to have some protective chips between my PC and my other circuits.

Serial downloading software

Atmelprg is a programmer tool which can perform both the parallel and the serial up/downloading of flash and EEPROM content.

Note: After downloading the source code, you need to manually configure the code before compiling it:
As mentioned above, timing is critical: The serial clock must be LOW and HIGH for at least 2 system clock cycles. Adjust SERIAL_DELAY in Atmelprg's atmelio_s.h to fit your computer. Just to give you a feeling: When using a system clock of 1MHz and my fairly old Pentium-100, I need to idle loop 24 cycles to get SCLK LOW pulses of nearly 2.5us which is above the required 2us. (The HIGH pulses are always at least as long as the LOW pulses due to the current software implementation.) If in doubt, use too large values or check using a DSO.
Also, don't forget to look up your PC's parallel port base address and assign a proper value to parport_baseaddr in main.cc.

Also note that in serial mode, Atmelprg may ask you to perform some actions manually, e.g. to give RESET a positive pulse after "chip erase" command.
Here is an example erasing session; especially note the red line which tells you to take reset HIGH shortly and then press a key in "(press enter)" two lines below.

bash# ./atmelspi -erase -readflash -readeeprom -dumpasc flash -dumpasc eeprom
Waiting to enter serial programming mode (press enter)...
Enabling SPI programming: OK [0x00ac5300]
Detected device: (0x1e9401)
  Device name: ATMega161       Flash size.:  16384 bytes  [0x2000 words]
  Vendor.....: Atmel           EEPROM size:    512 bytes
  Has PAGEL: yes;  Fuse+Lock: 2 bytes;  ChipErase: RDY/BSY
Performing chip erase... SPI: Chip erased. Give RESET a positive pulse.
Serial programming done (left programming mode).
Waiting to enter serial programming mode (press enter)...
Enabling SPI programming: OK [0xac5e5300]
Detected device: (0x1e9401)
  Device name: ATMega161       Flash size.:  16384 bytes  [0x2000 words]
  Vendor.....: Atmel           EEPROM size:    512 bytes
  Has PAGEL: yes;  Fuse+Lock: 2 bytes;  ChipErase: RDY/BSY
Reading flash into buffer "flash" (16384 bytes)... done
Reading EEPROM into buffer "eeprom" (512 bytes)... done
Dumping buffer "flash" (16384 bytes)
         [skipping 0x0000..0x1fff: 8192 words 0xffff]
Dumping buffer "eeprom" (512 bytes)
         [skipping 0x0000..0x00ff: 256 words 0xffff]
Serial programming done (left programming mode).

[home] [site map] [Impressum] [Datenschutz/privacy policy]
Valid HTML 4.01!
Copyright © 2004-2007 by Wolfgang Wieser
Last modified: 2007-06-09 20:11:43