; ; Copyright (C) 2002-2003 by egnite Software GmbH. All rights reserved. ; ; Redistribution and use in source and binary forms, with or without ; modification, are permitted provided that the following conditions ; are met: ; ; 1. Redistributions of source code must retain the above copyright ; notice, this list of conditions and the following disclaimer. ; 2. Redistributions in binary form must reproduce the above copyright ; notice, this list of conditions and the following disclaimer in the ; documentation and/or other materials provided with the distribution. ; 3. All advertising materials mentioning features or use of this ; software must display the following acknowledgement: ; ; This product includes software developed by egnite Software GmbH ; and its contributors. ; ; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ; ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT ; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS ; FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE ; COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, ; INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, ; BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS ; OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED ; AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, ; OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF ; THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF ; SUCH DAMAGE. ; ; For additional information see http://www.ethernut.de/ ; ; $Log$ ; Revision 1.1 2003/11/03 15:51:31 haraldkipp ; First check in ; ; .nolist #include "avr/io.h" .list ; This program implements a minimalist STK500 compatible programming ; adapter for the serial port on an AT90S2313 MCU. It had been ; originally written for AVR Studio, but then ported to AVR-GCC and ; will no longer build with AVR Studio. However, the resulting ; programmer had been successfully tested with AVR Studio 4.07 and ; uisp version 20030827cvs on ATmega128 and ATmega13 targets. ; ; ********************************************************************* ; Protocol definitions ; Initially the STK500 protocol had been evaluated by re-engineering. ; Some months later Atmel decided to publish it. See ; http://www.atmel.com/atmel/acrobat/doc2525.pdf #define STK_OK 0x10 #define STK_INSYNC 0x14 #define STK_NOSYNC 0x15 #define CRC_EOP 0x20 ; ********************************************************************* ; General configurations ; Baudrate factor. AVR Studio uses a fixed rate of 115,200 Baud, which ; results in a baudrate register factor of 1 on a 3.6864 MHz MCU. See ; the AT90S2313 datasheet for further details. #define BRFACTOR 1 ; ********************************************************************* ; Port Usage ; PB0: Unused ; PB1: Unused ; PB2: ISP-LED (out) ; PB3: LED (out) ; PB4: ISP-RESET (out) ; PB5: ISP-MOSI (out) ; PB6: ISP-MISO (in) ; PB7: ISP-SCK (out) ; ; PD0: RXD (in) ; PD1: TXD (out) ; PD2: HSIN/INT0 (in) ; PD3: HSOUT (out) ; PD4: unused ; PD5: unused ; PD6: unused ; #define ISPLED 2 #define LEDIND 3 #define ISPRST 4 #define ISPMOSI 5 #define ISPMISO 6 #define ISPSCK 7 #define HSIN 2 #define HSOUT 3 ; ********************************************************************* ; Register usage #define r_temp r16 #define r_data r17 #define r_isp r18 #define r_shift r19 #define r_delay r20 #define r_retry r21 #define r_size r22 #define r_parm r23 ; ********************************************************************* ; RAM variables .section .bss paddrl: .space 1 paddrh: .space 1 unicmd1: .space 1 unicmd2: .space 1 unicmd3: .space 1 unicmd4: .space 1 pm_devicecode: .space 1 ; device code pm_revision: .space 1 ; device revision pm_progtype: .space 1 ; "0" both, "1" par only pm_parmode: .space 1 ; "0" pseudo, "1" full pm_polling: .space 1 ; "0" no, "1" yes pm_selftimed: .space 1 ; "0" no, "1" yes pm_lockbytes: .space 1 ; # of lock bytes pm_fusebytes: .space 1 ; # of fuse bytes pm_flpollval1: .space 1 ; Flash polling value pm_flpollval2: .space 1 ; Flash polling value pm_eepollval1: .space 1 ; EEPROM polling value pm_eepollval2: .space 1 ; EEPROM polling value pm_pagesizeh: .space 1 ; Page size high byte pm_pagesizel: .space 1 ; Page size low byte pm_eesizeh: .space 1 ; EEPROM size high byte pm_eesizel: .space 1 ; EEPROM size low byte pm_flsize4: .space 1 ; Flash size MSB pm_flsize3: .space 1 ; Flash size pm_flsize2: .space 1 ; Flash size pm_flsize1: .space 1 ; Flash size LSB ; ********************************************************************* ; Vector table .section .text .global sisp sisp: rjmp main ; ********************************************************************* ; Constant data .type signon_msg, @object .global signon_msg signon_msg: .string "\x14\x41VR STK\x10" .type version_msg, @object .global version_msg version_msg: .string "SISP 1.1.1 Copyright 2002-2003 by egnite Software GmbH\r\n" ; ********************************************************************* ; Main program entry .global main main: ; ; Set stack pointer ldi r_temp, lo8(RAMEND) out _SFR_IO_ADDR(SPL), r_temp ; ; Initialize Port B ; PB2: ISP-LED z-state ; PB3: LED output low ; PB4: ISP-RESET z-state ; PB5: ISP-MOSI z-state ; PB6: ISP-MISO input ; PB7: ISP-SCK z-state clr r_temp out _SFR_IO_ADDR(PORTB), r_temp out _SFR_IO_ADDR(DDRB), r_temp sbi _SFR_IO_ADDR(DDRB), LEDIND ldi r_retry, 3 blink: rcall xdelay rcall xdelay cbi _SFR_IO_ADDR(PORTB), LEDIND ; LED on ldi r_delay, 64 rcall xdelay sbi _SFR_IO_ADDR(PORTB), LEDIND ; LED off dec r_retry brne blink ; ; Init UART ldi r_temp, BRFACTOR out _SFR_IO_ADDR(UBRR), r_temp ldi r_temp, (1< T bld r_temp, ISPMOSI ; (1) T -> MOSI out _SFR_IO_ADDR(PORTB), r_temp ; (1) Output, SCK still low sbi _SFR_IO_ADDR(PORTB), ISPSCK ; (2) SCK high bst r_isp, 6 ; (1) bld r_temp, ISPMOSI ; (1) out _SFR_IO_ADDR(PORTB), r_temp ; SCK back to low after about 3 cycles nop sbi _SFR_IO_ADDR(PORTB), ISPSCK ; SCK high again after about 2 cycles bst r_isp, 5 bld r_temp, ISPMOSI out _SFR_IO_ADDR(PORTB), r_temp nop sbi _SFR_IO_ADDR(PORTB), ISPSCK bst r_isp, 4 bld r_temp, ISPMOSI out _SFR_IO_ADDR(PORTB), r_temp nop sbi _SFR_IO_ADDR(PORTB), ISPSCK bst r_isp, 3 bld r_temp, ISPMOSI out _SFR_IO_ADDR(PORTB), r_temp nop sbi _SFR_IO_ADDR(PORTB), ISPSCK bst r_isp, 2 bld r_temp, ISPMOSI out _SFR_IO_ADDR(PORTB), r_temp nop sbi _SFR_IO_ADDR(PORTB), ISPSCK bst r_isp, 1 bld r_temp, ISPMOSI out _SFR_IO_ADDR(PORTB), r_temp nop sbi _SFR_IO_ADDR(PORTB), ISPSCK bst r_isp, 0 bld r_temp, ISPMOSI out _SFR_IO_ADDR(PORTB), r_temp nop sbi _SFR_IO_ADDR(PORTB), ISPSCK nop nop nop out _SFR_IO_ADDR(PORTB), r_temp ; (1) SCK low ret ; (4) Costly return ; ********************************************************************* ; Delay ; ; xdelay takes about 53.6 ms xdelay: ; (3) clr r_delay ; (1) delay: clr r_temp ; (1) Outer loop with r_delay * 772 cycles dl: dec r_temp ; (1) Inner loop with 3 cycles brne dl ; (2/1) dec r_delay ; (1) brne delay ; (2/1) ret ; (4) #ifdef SISPDEBUG ; ********************************************************************* ; Debug ; puthexb: push r_data swap r_data rcall puthexc pop r_data puthexc: push r_temp mov r_temp, r_data andi r_temp, 0x0f subi r_temp, -'0' cpi r_temp, 0x3a brlo _putx1 subi r_temp, -7 _putx1: push r_data mov r_data, r_temp rcall putc pop r_data pop r_temp ret #endif