📄 main.s
字号:
.module main.c
.area text(rom, con, rel)
.even
_FlashLoad::
; //*****************************************************************************
; //* BootLoader 3.1 for MegaLoad 3.0
; //*
; //* Devices supported at this time
; //* ATMega8
; //* ATMega16
; //* ATMega32
; //* ATMega64
; //* ATMega128
; //* ATMega162
; //* ATMega169
; //* ATMega8515
; //* ATMega8535
; //* ATMega2313 not tested, please give me feedback!
; //*
; //* Version 3.0 Dec 2003
; //* 1.0 : First Release
; //* 1.1 : Add auto baud rate, code smaller
; //* 1.2 : Correct a bug with file larger than 65280 bytes.
; //* 2.0 : All in one source file that fit in 512 Words
; //* 3.0 : Add EEprom programming
; //* Add RS485 half duplex support
; //* Add LockBit programming
; //* 3.1 : Correct a problem with interupt vector for M8/16/32/162/8515/8535
; //* Change some #define to be more clear
; //*
; //* Sylvain.Bissonnette@MicroSyl.com
; //*****************************************************************************
; //
; // To setup the bootloader for your project you must
; // remove the comment below to fit with your hardware
; // recompile it using ICCAVR setup for bootloader
; // of 512 word for ONLY flash programming
; // or 1024 word for flash and eeprom programming
; //
; //*****************************************************************************
; // MCU selection
; // -->Do the same thing in assembly.s<--
; //*****************************************************************************
;
; #define MEGATYPE 8
; //#define MEGATYPE 16
; //#define MEGATYPE 32
; //#define MEGATYPE 64
; //#define MEGATYPE 128
; //#define MEGATYPE 162
; //#define MEGATYPE 169
; //#define MEGATYPE 8515
; //#define MEGATYPE 8535
; //#define MEGATYPE 2313
;
;
; //*****************************************************************************
; // Bootload on UART x
; //*****************************************************************************
; //#define UART 0
; //#define UART 1
;
; //*****************************************************************************
; // BaudRate
; // If you don't specify the baudrate divisor the bootloader
; // will automaticaly be in AutoBaudRate mode
; //*****************************************************************************
; //#define BAUDRATE 87
; //#define BAUDSETTING (unsigned int)((unsigned long)(fosc)/(16*(unsigned long)(BAUDRATE)))-1
; //#define BAUD_H (unsigned char)(BAUDSETTING>>8)
; //#define BAUD_L (unsigned char) BAUDSETTING
; //*****************************************************************************
; // Crystal speed
; // frequancy of your MCU speed
; // LOW -> Xtal < 8Mhz
; // HIGH -> Xtal >= 8Mhz
; //*****************************************************************************
; //#define LOW
; #define HIGH
;
; //*****************************************************************************
; // EEprom programming
; // enable EEprom programing via bootloader
; // YOU MUST SET TO 1024 WORD THE BOOTLOADER SIZE IF EEPROM
; // PROGRAMMING IS USE
; //*****************************************************************************
; #define EEPROM
;
; //*****************************************************************************
; // LockBit programming
; // enable LOCKBIT programing via bootloader
; //*****************************************************************************
; //#define LOCKBIT
;
; //*****************************************************************************
; // RS485
; // if you use RS485 half duplex for bootloader
; // make the appropriate change for RX/TX transceiver switch
; //*****************************************************************************
; #define RS485DDR DDRD
; #define RS485PORT PORTD
; #define RS485TXE 0x10
;
; //*****************************************************************************
; // HomeNet
; // Only use for Sylvain Bissonnette personal projects
; // For normal use don't define HOMENET
; //*****************************************************************************
; //#define HOMENET
;
;
; //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; //!!!!!!!!!!!!!! DO NOT CHANGE ANYTHING BELOW THIS LINE !!!!!!!!!!!!!!!!!!!!!!!
; //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; #include <macros.h>
;
; #define Tiny2313 'O'
; #define Tiny13 'P'
; #define Mega8 'A'
; #define Mega16 'B'
; #define Mega32 'E'
; #define Mega48 'L'
; #define Mega64 'C'
; #define Mega88 'M'
; #define Mega128 'D'
; #define Mega162 'F'
; #define Mega163 'J'
; #define Mega168 'N'
; #define Mega169 'G'
; #define Mega323 'K'
; #define Mega8515 'H'
; #define Mega8535 'I'
;
;
; #define Flash1k 'g'
; #define Flash2k 'h'
; #define Flash4k 'i'
; #define Flash8k 'l'
; #define Flash16k 'm'
; #define Flash32k 'n'
; #define Flash64k 'o'
; #define Flash128k 'p'
; #define Flash256k 'q'
;
; #define EEprom64 '.'
; #define EEprom128 '/'
; #define EEprom256 '0'
; #define EEprom512 '1'
; #define EEprom1024 '2'
; #define EEprom2048 '3'
; #define EEprom4096 '4'
;
; #define Boot128 'a'
; #define Boot256 'b'
; #define Boot512 'c'
; #define Boot1024 'd'
; #define Boot2048 'e'
; #define Boot4096 'f'
;
; #define Page32 'Q'
; #define Page64 'R'
; #define Page128 'S'
; #define Page256 'T'
; #define Page512 'U'
;
; #if !(defined MEGATYPE)
; #error "Processor Type is Undefined"
; #endif
;
; #if (((MEGATYPE == 64) || (MEGATYPE == 128) || (MEGATYPE == 162)) && !(defined UART))
; #error "UART ch must be defined for this part"
; #endif
;
;
;
; #ifdef EEPROM
; #define BootSize Boot1024
; #endif
;
; #ifndef EEPROM
; #define BootSize Boot512
; #endif
;
; #if (MEGATYPE == 8)
; #include <iom8v.h>
; #define DeviceID Mega8
; #define FlashSize Flash8k
; #define PageSize Page64
; #define EEpromSize EEprom512
; #define PageByte 64
; #define NSHIFTPAGE 6
; #define INTVECREG GICR
; #define PULLUPPORT PORTD
; #define PULLUPPIN 0x01
; #endif
;
; #if (MEGATYPE == 16)
; #include "iom16v.h"
; #define DeviceID Mega16
; #define FlashSize Flash16k
; #define PageSize Page128
; #define EEpromSize EEprom512
; #define PageByte 128
; #define NSHIFTPAGE 7
; #define INTVECREG GICR
; #define PULLUPPORT PORTD
; #define PULLUPPIN 0x01
; #endif
;
; #if (MEGATYPE == 32)
; #include "iom32v.h"
; #define DeviceID Mega32
; #define FlashSize Flash32k
; #define PageSize Page128
; #define EEpromSize EEprom1024
; #define PageByte 128
; #define NSHIFTPAGE 7
; #define INTVECREG GICR
; #define PULLUPPORT PORTD
; #define PULLUPPIN 0x01
; #endif
;
; #if (MEGATYPE == 64)
; #include "iom64v.h"
; #define DeviceID Mega64
; #define FlashSize Flash64k
; #define PageSize Page256
; #define EEpromSize EEprom2048
; #define PageByte 256
; #define NSHIFTPAGE 8
; #define INTVECREG MCUCR
; #if (UART == 0)
; #define PULLUPPORT PORTE
; #define PULLUPPIN 0x01
; #endif
;
; #if (UART == 1)
; #define PULLUPPORT PORTD
; #define PULLUPPIN 0x04
; #endif
; #endif
;
; #if (MEGATYPE == 128)
; #include "iom128v.h"
; #define DeviceID Mega128
; #define FlashSize Flash128k
; #define PageSize Page256
; #define EEpromSize EEprom4096
; #define PageByte 256
; #define NSHIFTPAGE 8
; #define INTVECREG MCUCR
; #define RAMPZ_FLAG
; #if (UART == 0)
; #define PULLUPPORT PORTE
; #define PULLUPPIN 0x01
; #endif
;
; #if (UART == 1)
; #define PULLUPPORT PORTD
; #define PULLUPPIN 0x04
; #endif
; #endif
;
; #if (MEGATYPE == 162)
; #include "iom162v.h"
; #define DeviceID Mega162
; #define FlashSize Flash16k
; #define PageSize Page128
; #define EEpromSize EEprom512
; #define PageByte 128
; #define NSHIFTPAGE 7
; #define INTVECREG GICR
; #if (UART == 0)
; #define PULLUPPORT PORTD
; #define PULLUPPIN 0x01
; #endif
;
; #if (UART == 1)
; #define PULLUPPORT PORTB
; #define PULLUPPIN 0x04
; #endif
; #endif
;
; #if (MEGATYPE == 169)
; #include "iom169v.h"
; #define DeviceID Mega169
; #define FlashSize Flash16k
; #define PageSize Page128
; #define EEpromSize EEprom512
; #define PageByte 128
; #define NSHIFTPAGE 7
; #define INTVECREG MCUCR
; #define PULLUPPORT PORTE
; #define PULLUPPIN 0x01
; #endif
;
; #if (MEGATYPE == 8515)
; #include "iom8515v.h"
; #define DeviceID Mega8515
; #define FlashSize Flash8k
; #define PageSize Page64
; #define EEpromSize EEprom512
; #define PageByte 64
; #define NSHIFTPAGE 6
; #define INTVECREG GICR
; #define PULLUPPORT PORTD
; #define PULLUPPIN 0x01
; #endif
;
; #if (MEGATYPE == 8535)
; #include "iom8515v.h"
; #define DeviceID Mega8535
; #define FlashSize Flash8k
; #define PageSize Page64
; #define EEpromSize EEprom512
; #define PageByte 64
; #define NSHIFTPAGE 6
; #define INTVECREG GICR
; #define PULLUPPORT PORTD
; #define PULLUPPIN 0x01
; #endif
;
; #if (MEGATYPE == 2313)
; #include "iot2313v.h"
; #define DeviceID Tiny2313
; #define FlashSize Flash2k
; #define PageSize Page32
; #define EEpromSize EEprom128
; #define PageByte 32
; #define NSHIFTPAGE 5
; #define PULLUPPORT PORTD
; #define PULLUPPIN 0x01
; #endif
;
; #ifndef UDR
; #if (UART == 0)
; #define UCSRA UCSR0A
; #define UCSRB UCSR0B
; #define UCSRC UCSR0C
; #define UBRRL UBRR0L
; #define UBRRH UBRR0H
; #define UDR UDR0
; #endif
;
; #if (UART == 1)
; #define UCSRA UCSR1A
; #define UCSRB UCSR1B
; #define UCSRC UCSR1C
; #define UBRRL UBRR1L
; #define UBRRH UBRR1H
; #define UDR UDR1
; #endif
; #endif
;
;
; #define FALSE 0
; #define TRUE 1
;
; /*********************************************************/
; /* I N C L U D E */
; /*********************************************************/
; #include "assembly.h"
;
; /*********************************************************/
; /* P R O T O T Y P E */
; /*********************************************************/
; void FlashLoad(void);
; void EEpromLoad(void);
; void WriteEEprom(void);
; char CheckEEprom(void);
; void EEPROMwrite(int location, unsigned char byte);
; unsigned char EEPROMread(int location);
; void SendDeviceID(void);
; void GetPageNumber(void);
; void ExecCode(void);
; char GetPage(void);
; void WriteFlash(void);
; char CheckFlash(void);
; void LockBit(void);
; unsigned char IsChar(void);
; unsigned char RxChar(void);
; void TxChar(unsigned char ch);
; void Wait(void);
; char HomeNet(void);
; void main(void);
;
; /*
; ///////////////////////////////////////////////////////////////////////////////
; #define WATCHDOG 4
;
; void Watchdog_Feed( void );
;
; unsigned char watchdog_feed_flag;
; */
; ///////////////////////////////////////////////////////////////////////////////
;
; /*****************************************************************************/
; /* G L O B A L V A R I A B L E S */
; /*****************************************************************************/
; unsigned char PageBuffer[PageByte];
; unsigned int PageAddress;
; unsigned int RealPageAddress;
;
;
; /*
; void Watchdog_Feed( void )
; {
; if ( watchdog_feed_flag == 0 )
; {
; watchdog_feed_flag = 1;
; PORTC |= 1 << WATCHDOG;
; }
; else
; {
; watchdog_feed_flag = 0;
; PORTC &= ~( 1 << WATCHDOG );
; }
; }
; */
; /*****************************************************************************/
; /* Flash Programing Code */
; /*****************************************************************************/
; void FlashLoad(void)
; {
; TxChar('!');
ldi R16,33
rcall _TxChar
rjmp L3
L2:
; while (1)
; {
; GetPageNumber();
rcall _GetPageNumber
;
; if (RealPageAddress == 0xFFFF) return;
lds R24,_RealPageAddress
lds R25,_RealPageAddress+1
cpi R24,255
ldi R30,255
cpc R25,R30
brne L5
rjmp L1
L5:
; // Watchdog_Feed();
;
; if (GetPage())
rcall _GetPage
tst R16
breq L7
; {
; WriteFlash();
rcall _WriteFlash
; if (CheckFlash())
rcall _CheckFlash
tst R16
breq L9
; TxChar('!');
ldi R16,33
rcall _TxChar
rjmp L8
L9:
; else TxChar('@');
ldi R16,64
rcall _TxChar
; }
rjmp L8
L7:
; else TxChar('@');
ldi R16,64
rcall _TxChar
L8:
L3:
rjmp L2
X0:
L1:
.dbline 0 ; func end
ret
; PageAddressLow -> R20
; PageAddressHigh -> R22
.even
_GetPageNumber::
rcall push_gset2
; }
; }
;
; /*****************************************************************************/
;
; void GetPageNumber(void)
; {
L12:
L13:
; unsigned char PageAddressHigh;
; unsigned char PageAddressLow;
;
; while(!IsChar());
rcall _IsChar
tst R16
breq L12
; WDR();
wdr
; PageAddressHigh = RxChar();
rcall _RxChar
mov R22,R16
L15:
L16:
;
; while(!IsChar());
rcall _IsChar
tst R16
breq L15
; WDR();
wdr
; PageAddressLow = RxChar();
rcall _RxChar
mov R20,R16
;
; RealPageAddress = (int)((PageAddressHigh << 8) + PageAddressLow);
mov R3,R22
mov R2,R20
sts _RealPageAddress+1,R3
sts _RealPageAddress,R2
; PageAddress = RealPageAddress << NSHIFTPAGE;
ldi R18,6
ldi R19,0
movw R16,R2
rcall lsl16
sts _PageAddress+1,R17
sts _PageAddress,R16
L11:
rcall pop_gset2
.dbline 0 ; func end
ret
; CheckSum -> R20
; LocalCheckSum -> R22
; i -> R10,R11
.even
_GetPage::
rcall push_gset3
;
; #ifdef RAMPZ_FLAG
; if (PageAddressHigh) RAMPZ = 1;
; else RAMPZ = 0;
; #endif
; }
;
; /*****************************************************************************/
;
; char GetPage(void)
; {
; unsigned int i;
; unsigned char LocalCheckSum = 0;
clr R22
; unsigned char CheckSum = 0;
clr R20
;
; for (i = 0 ; i < PageByte ; i++)
clr R10
clr R11
rjmp L22
X1:
; {
L23:
L24:
rcall _IsChar
tst R16
breq L23
wdr
rcall _RxChar
ldi R24,<_PageBuffer
ldi R25,>_PageBuffer
movw R30,R10
add R30,R24
adc R31,R25
std z+0,R16
movw R30,R10
add R30,R24
adc R31,R25
ldd R2,z+0
add R22,R2
wdr
L20:
movw R24,R10
adiw R24,1
movw R10,R24
L22:
movw R24,R10
cpi R24,64
ldi R30,0
cpc R25,R30
brlo L24
L26:
L27:
; while(!IsChar());
; WDR();
; PageBuffer[i] = RxChar();
; LocalCheckSum += PageBuffer[i];
; WDR();
; }
;
; while(!IsChar());
rcall _IsChar
tst R16
breq L26
; WDR();
wdr
; CheckSum = RxChar();
rcall _RxChar
mov R20,R16
; if (LocalCheckSum == CheckSum) return 1;
cp R22,R16
brne L29
ldi R16,1
rjmp L18
L29:
; else return 0;
clr R16
L18:
rcall pop_gset3
.dbline 0 ; func end
ret
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -