⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.s

📁 avr bootloader 上位机源码
💻 S
📖 第 1 页 / 共 2 页
字号:
	.module main.c
	.area text(rom, con, rel)
	.dbfile E:\icc\peterf\BootLoad\main.c
	.dbfunc e FlashLoad _FlashLoad fV
	.even
_FlashLoad::
	.dbline -1
	.dbline 120
; /******************************************************************************
; Atmega系列单片机 BootLoad程序  
; 日  期:2004年的最后一天
; 肖  鹏  www.527dz.com  527dz@527dz.com
; 
; /*****************************************************************************/
; #include <macros.h>
; #include "assembly.h"
; //×××××××××××××××××××××××××××××××××××××××××××××××××××××××××
; #define  fosc  8000000   //晶振频率
; #define  baud1 38400     //波特率
; //请根据目标板选择芯片型号
; //#define  ChipType     Atmega8       
; //#define  ChipType     Atmega16       
; //#define  ChipType     Atmega32       
; //#define  ChipType     Atmega64       
; #define  ChipType     Atmega128
; //#define  ChipType     Atmega8515       
; //#define  ChipType     Atmega8535       
; //*********************************************************
; //选择BOOT区大小
; //#define  BootSize     'a'    //128   
; //#define  BootSize     'b'    //256
; //#define  BootSize       'c'    //512
; #define  BootSize     'd'    //1024
; //#define  BootSize     'e'    //2048(不知道是否有2048字BOOT空间的芯片)
; //选择BootLoad版本号
; #define  BootVer        'f'    //1.0版本
; //#define  BootVer      'g'    //1.1版本
; //#define  BootVer      'h'    //1.2版本
; //#define  BootVer      'i'    //1.3版本
; //#define  BootVer      'j'    //1.4版本
; //#define  BootVer      'k'    //1.5版本
; //#define  BootVer      'l'    //1.6版本
; //#define  BootVer      'm'    //1.7版本
; //#define  BootVer      'n'    //1.8版本
; //#define  BootVer      'o'    //1.9版本
; //#define  BootVer      'p'    //2.0版本(应该是最终版本了)
; //**********************************************************
; #define  Atmega8        0x30
; #define  Atmega16       0x31
; #define  Atmega32       0x32
; #define  Atmega64       0x33
; #define  Atmega128      0x34
; #define  Atmega8515     0x35
; #define  Atmega8535     0x36
; //*****************************************************************************
; #define  InteClk                 //是否使用内部时钟
; #define  OscAddress     0x1fff   //时钟校准值存储地址
; //#define  OscAddress     0x3fff   //时钟校准值存储地址
; //*****************************************************************************
; //8时钟下的波特率设置
; //#define  BAU  103    //4800
; //#define  BAU  51       //9600
; //#define  BAU  34     //14400
; #define  BAU  25     //19200
; //*****************************************************************************
; #if (ChipType == Atmega8)
;   #include  "iom8v.h"
;   #define   PageByte 	 	 64 
;   #define   AddressShift	 6
;   #define	INTVECREG	 	 GICR  
; #endif
; 
; #if (ChipType == Atmega16)
;   #include  "iom16v.h"
;   #define   PageByte 	 	 128    
;   #define   AddressShift 	 7
;   #define	INTVECREG	 	 GICR
; #endif
; 
; #if (ChipType == Atmega32)
;   #include  "iom32v.h"
;   #define   PageByte 	 	 128    
;   #define   AddressShift 	 7
;   #define	INTVECREG	 	 GICR
; #endif
; 
; #if (ChipType == Atmega64)
;   #include  "iom64v.h"
;   #define   PageByte 	 	 256    
;   #define   AddressShift 	 8
;   #define	INTVECREG	 	 MCUCR
; #endif
; 
; #if (ChipType == Atmega128)
;   #include  "iom128v.h"
;   #define   PageByte 	 	 256    
;   #define   AddressShift 	 8
;   #define	INTVECREG	 	 MCUCR
;   #define   RAMPZ_FLAG
; #endif
; 
; #if (ChipType == Atmega8515)
;   #include  "iom8515v.h"
;   #define   PageByte 	 	 64    
;   #define   AddressShift 	 6
;   #define	INTVECREG	 	 GICR
; #endif
; 
; void          FlashLoad        (void);
; void          GetPageNumber    (void);
; void          ExecCode         (void);
; char          GetPage          (void);
; void          WriteFlash       (void);
; char          CheckFlash       (void);
; unsigned char IsChar           (void);
; void          SendChar         (unsigned char c);
; void          delay            (void);                 //1ms延时函数
; unsigned char RecChar          (void);
; 
; unsigned char PageBuffer[PageByte];
; unsigned int  RealPageAddress;
; unsigned int  PageAddress;
; 
; /*****************************************************************************/
; //Flash编程								                     
; /*****************************************************************************/
; void FlashLoad(void)
;   {
	.dbline 121
;     SendChar('!');
	ldi R16,33
	xcall _SendChar
	xjmp L3
L2:
	.dbline 123
;     while (1)
;     {
	.dbline 124
; 	 GetPageNumber();
	xcall _GetPageNumber
	.dbline 126
; 
; 	 if (RealPageAddress == 0xffff) 
	lds R24,_RealPageAddress
	lds R25,_RealPageAddress+1
	cpi R24,255
	ldi R30,255
	cpc R25,R30
	brne L5
	.dbline 127
; 	   return;
	xjmp L1
L5:
	.dbline 129
;         
; 	 if (GetPage())
	xcall _GetPage
	tst R16
	breq L7
	.dbline 130
; 	  {
	.dbline 131
; 		WriteFlash();
	xcall _WriteFlash
	.dbline 132
; 	    if (CheckFlash()) 
	xcall _CheckFlash
	tst R16
	breq L9
	.dbline 133
; 		  SendChar('!');
	ldi R16,33
	xcall _SendChar
	xjmp L8
L9:
	.dbline 135
; 	    else 
; 		  SendChar('@');
	ldi R16,64
	xcall _SendChar
	.dbline 136
; 	  }
	xjmp L8
L7:
	.dbline 138
; 	 else 
; 	   SendChar('@');
	ldi R16,64
	xcall _SendChar
L8:
	.dbline 139
L3:
	.dbline 122
	xjmp L2
X0:
	.dbline -2
	.dbline 140
;     }
;   }
L1:
	.dbline 0 ; func end
	ret
	.dbend
	.dbfunc e GetPageNumber _GetPageNumber fV
;   PageAddressL -> R22
;   PageAddressH -> R20
	.even
_GetPageNumber::
	xcall push_gset2
	.dbline -1
	.dbline 144
; 
; /*****************************************************************************/
; void GetPageNumber(void)
;   {
	.dbline 148
;     unsigned char PageAddressH;
;     unsigned char PageAddressL;
; 
;     PageAddressH = RecChar();
	xcall _RecChar
	mov R20,R16
	.dbline 150
; 
;     PageAddressL = RecChar();
	xcall _RecChar
	mov R22,R16
	.dbline 152
; 
;     RealPageAddress=(int)((PageAddressH << 8) + PageAddressL);
	mov R3,R20
	mov R2,R22
	sts _RealPageAddress+1,R3
	sts _RealPageAddress,R2
	.dbline 153
; 	PageAddress=RealPageAddress<<AddressShift;
	mov R3,R2
	clr R2
	sts _PageAddress+1,R3
	sts _PageAddress,R2
	.dbline 155
; #ifdef RAMPZ_FLAG
;     if (PageAddressH) 
	tst R20
	breq L12
	.dbline 156
; 	   RAMPZ = 1; 
	ldi R24,1
	out 0x3b,R24
	xjmp L13
L12:
	.dbline 158
	clr R2
	out 0x3b,R2
L13:
	.dbline -2
	.dbline 164
;     else 
; 	   RAMPZ = 0;
; #endif	
; 	//SendChar(RealPageAddress>>8);
; 	//SendChar(RealPageAddress);
; 	//SendChar(PageAddress>>8);
; 	//SendChar(PageAddress);
;   }
L11:
	xcall pop_gset2
	.dbline 0 ; func end
	ret
	.dbsym r PageAddressL 22 c
	.dbsym r PageAddressH 20 c
	.dbend
	.dbfunc e GetPage _GetPage fc
;       CheckSum -> R20
;  LocalCheckSum -> R22
;              i -> R10
	.even
_GetPage::
	xcall push_gset3
	.dbline -1
	.dbline 168
; 
; /*****************************************************************************/
; char GetPage(void)
;   {
	.dbline 170
;     unsigned char i;
;     unsigned char LocalCheckSum = 0;
	clr R22
	.dbline 171
;     unsigned char CheckSum = 0;
	clr R20
	.dbline 173
; 
;     for (i=0;i<PageByte/2;i++)
	clr R10
	xjmp L18
X1:
	.dbline 174
; 	{
L19:
	.dbline 175
L20:
	.dbline 175
	xcall _IsChar
	tst R16
	breq L19
	.dbline 176
	xcall _RecChar
	ldi R24,<_PageBuffer
	ldi R25,>_PageBuffer
	mov R30,R10
	clr R31
	add R30,R24
	adc R31,R25
	std z+0,R16
	.dbline 177
	mov R30,R10
	clr R31
	add R30,R24
	adc R31,R25
	ldd R2,z+0
	eor R22,R2
	.dbline 178
L16:
	.dbline 173
	inc R10
L18:
	.dbline 173
	mov R24,R10
	cpi R24,128
	brlo L20
	.dbline 179
; 	 while(!IsChar());
; 	 PageBuffer[i]=RecChar();
; 	 LocalCheckSum ^= PageBuffer[i];
;    	}
; 	for (i=0;i<PageByte/2;i++)
	clr R10
	xjmp L25
X2:
	.dbline 180
; 	{
L26:
	.dbline 181
L27:
	.dbline 181
	xcall _IsChar
	tst R16
	breq L26
	.dbline 182
	xcall _RecChar
	ldi R24,<_PageBuffer+128
	ldi R25,>_PageBuffer+128
	mov R30,R10
	clr R31
	add R30,R24
	adc R31,R25
	std z+0,R16
	.dbline 183
	mov R30,R10
	clr R31
	add R30,R24
	adc R31,R25
	ldd R2,z+0
	eor R22,R2
	.dbline 184
L23:
	.dbline 179
	inc R10
L25:
	.dbline 179
	mov R24,R10
	cpi R24,128
	brlo L27
	.dbline 185
; 	 while(!IsChar());
; 	 PageBuffer[i+PageByte/2]=RecChar();
; 	 LocalCheckSum ^= PageBuffer[i+PageByte/2];
;    	}	   
;     CheckSum = RecChar(); 
	xcall _RecChar
	mov R20,R16
	.dbline 187
; 	
;     if (LocalCheckSum == CheckSum) 
	cp R22,R16
	brne L31
	.dbline 188
; 	  return 1;
	ldi R16,1
	xjmp L14
L31:
	.dbline 190
;     else 
; 	  return 0;
	clr R16
	.dbline -2
L14:
	xcall pop_gset3
	.dbline 0 ; func end
	ret
	.dbsym r CheckSum 20 c
	.dbsym r LocalCheckSum 22 c
	.dbsym r i 10 c
	.dbend
	.dbfunc e WriteFlash _WriteFlash fV
;        TempInt -> R20,R21
;              i -> R22,R23
	.even
_WriteFlash::
	xcall push_gset2
	.dbline -1
	.dbline 195
;   }
; 
; /*****************************************************************************/
; void WriteFlash(void)
;   {
	.dbline 198
;     unsigned int i;
;     unsigned int TempInt;
;     for (i=0;i<PageByte;i+=2)
	clr R22
	clr R23
	xjmp L37
L34:
	.dbline 199
	.dbline 200
	ldi R24,<_PageBuffer
	ldi R25,>_PageBuffer
	movw R30,R22
	adiw R30,1
	add R30,R24
	adc R31,R25
	ldd R2,z+0
	clr R3
	mov R3,R2
	clr R2
	movw R30,R22
	add R30,R24
	adc R31,R25
	ldd R20,z+0
	clr R21
	add R20,R2
	adc R21,R3
	.dbline 201
	movw R18,R22
	movw R16,R20
	xcall _fill_temp_buffer
	.dbline 202
L35:
	.dbline 198
	subi R22,254  ; offset = 2
	sbci R23,255
L37:
	.dbline 198
	cpi R22,0
	ldi R30,1
	cpc R23,R30
	brlo L34
	.dbline 203
;       {
;    	   TempInt=PageBuffer[i]+(PageBuffer[i+1]<<8);
;    	   fill_temp_buffer(TempInt,i);    //call asm routine.
;       } 
;     write_page(PageAddress,0x03);       //擦除页
	ldi R18,3
	lds R16,_PageAddress
	lds R17,_PageAddress+1
	xcall _write_page
	.dbline 204
;     write_page(PageAddress,0x05);       //写页数据
	ldi R18,5
	lds R16,_PageAddress
	lds R17,_PageAddress+1
	xcall _write_page
	.dbline 206
;    
;     enableRWW();
	xcall _enableRWW
	.dbline -2
	.dbline 207
;   }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -