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

📄 main.s

📁 硬盘MP3播放器-MEGA128-VS1001K-USB-遥控-LCD-ICCAVR代码-硬盘-支持FAT32-单面PCB 第三部分
💻 S
字号:
	.module main.c
	.area data(ram, con, rel)
_PageAddress16::
	.blkb 1
	.area idata
	.byte 0
	.area data(ram, con, rel)
	.dbfile C:\DATA\MP3\BootLoader\main.c
	.dbsym e PageAddress16 _PageAddress16 c
	.area text(rom, con, rel)
	.dbfile C:\DATA\MP3\BootLoader\main.c
	.dbfunc e BootLoad _BootLoad fV
	.even
_BootLoad::
	.dbline -1
	.dbline 70
; /*********************************************************/
; /* BootLoader for ATMega128 & MP3 Ver:3.0  @ 16Mhz	     */
; /* Version 1.1 April 2003 								 */
; /* 1.0 : First Release									 */
; /* 1.1 : Add auto baud rate, code smaller				 */
; /* 1.2 : Correct a bug with file larger than 65280 bytes.*/
; /* 	   	 	   											 */	
; /* Sylvain.Bissonnette@MicroSyl.com						 */
; /*********************************************************/
; 
; /*********************************************************/
; /*                   I N C L U D E    		             */
; /*********************************************************/	
; #include "main.h"
; #include "assembly.h"
; #include "macros.h"
; #include "iom128v.h"
; 
; /*********************************************************/
; /*                   D E F I N E    		             */
; /*********************************************************/	
; //#define	 DeviceID  	 'A'	// Mega8		
; //#define	 DeviceID  	 'B'	// Mega16	
; //#define	 DeviceID  	 'C'	// Mega64	
; #define	 DeviceID  	 'D'	// Mega128	
; 						
; //#define	 FlashSize	 'l'    // Flash 8k
; //#define	 FlashSize	 'm'    // Flash 16k
; //#define	 FlashSize	 'n'    // Flash 32k
; //#define	 FlashSize	 'o'    // Flash 64k
; #define	 FlashSize	 'p'    // Flash 128k
; 
; //#define	 BootSize	 'a'    // 128 word
; //#define	 BootSize	 'b'    // 256 word
; #define	 BootSize	 'c'    // 512 word
; //#define	 BootSize	 'd'    // 1024 word
; //#define	 BootSize	 'e'    // 2048 word
; //#define	 BootSize	 'f'    // 4096 word
; 
; //#define	 PageSize	 'Q'    // 32 Bytes
; //#define	 PageSize	 'R'    // 64 Bytes
; //#define	 PageSize	 'S'    // 128 Bytes
; #define	 PageSize	 'T'    // 256 Bytes		
; //#define	 PageSize	 'U'    // 512 Bytes
; 
; //#define  PageByte 	 32     // 32 Bytes
; //#define  PageByte 	 64     // 64 Bytes
; //#define  PageByte 	 128     // 128 Bytes
; #define  PageByte 	 256     // 256 Bytes		
; //#define  PageByte 	 512     // 512 Bytes
; 
; #define  FALSE		 0
; #define  TRUE		 1
; 
; #define	 EXECCODE	 0xffff
; 
; /*********************************************************/
; /*           G L O B A L    V A R I A B L E S			 */
; /*********************************************************/	
; 
; unsigned char PageBuffer[PageByte];
; unsigned int PageAddress;
; unsigned char PageAddress16 = 0;
; unsigned int RealPageAddress;
; 
; 
; /*********************************************************/
; 
; void BootLoad(void)
; {
	.dbline 71
; SendDeviceID();
	xcall _SendDeviceID
	.dbline 72
; TxChar('!');
	ldi R16,33
	xcall _TxChar
	xjmp L3
L2:
	.dbline 74
; while(1)
; 		{
	.dbline 75
; 		GetPageNumber();
	xcall _GetPageNumber
	.dbline 76
; 		if (RealPageAddress == EXECCODE) ExecCode();
	lds R24,_RealPageAddress
	lds R25,_RealPageAddress+1
	cpi R24,255
	ldi R30,255
	cpc R25,R30
	brne L5
	.dbline 76
	xcall _ExecCode
L5:
	.dbline 78
; 
; 		if (GetPage())
	xcall _GetPage
	tst R16
	breq L7
	.dbline 79
; 		   {
	.dbline 80
; 		   WriteFlash();
	xcall _WriteFlash
	.dbline 81
; 		   if (CheckFlash()) TxChar('!');
	xcall _CheckFlash
	tst R16
	breq L9
	.dbline 81
	ldi R16,33
	xcall _TxChar
	xjmp L8
L9:
	.dbline 82
; 		   else TxChar('@');
	ldi R16,64
	xcall _TxChar
	.dbline 83
; 		   }
	xjmp L8
L7:
	.dbline 84
; 		else TxChar('@');
	ldi R16,64
	xcall _TxChar
L8:
	.dbline 85
L3:
	.dbline 73
	xjmp L2
X0:
	.dbline -2
	.dbline 86
; 		}
; }
L1:
	.dbline 0 ; func end
	ret
	.dbend
	.dbfunc e SendDeviceID _SendDeviceID fV
	.even
_SendDeviceID::
	.dbline -1
	.dbline 90
; 
; /*********************************************************/
; void SendDeviceID(void)
; {
	.dbline 91
; TxChar(DeviceID);
	ldi R16,68
	xcall _TxChar
	.dbline 92
; TxChar(FlashSize);
	ldi R16,112
	xcall _TxChar
	.dbline 93
; TxChar(BootSize);
	ldi R16,99
	xcall _TxChar
	.dbline 94
; TxChar(PageSize);
	ldi R16,84
	xcall _TxChar
	.dbline 95
; RxChar();
	.dbline -2
	.dbline 96
; }
L11:
	.dbline 0 ; func end
	xjmp _RxChar
	.dbend
	.dbfunc e GetPageNumber _GetPageNumber fV
; PageAddressLow -> R22
; PageAddressHigh -> R20
	.even
_GetPageNumber::
	xcall push_gset2
	.dbline -1
	.dbline 101
; 
; /*********************************************************/
; 
; void GetPageNumber(void)
; {
L13:
	.dbline 105
L14:
	.dbline 105
; unsigned char PageAddressHigh;
; unsigned char PageAddressLow;
; 
; while(!IsChar());
	xcall _IsChar
	tst R16
	breq L13
	.dbline 106
; PageAddressHigh = RxChar();
	xcall _RxChar
	mov R20,R16
L16:
	.dbline 108
L17:
	.dbline 108
; 
; while(!IsChar());
	xcall _IsChar
	tst R16
	breq L16
	.dbline 109
; PageAddressLow = RxChar();
	xcall _RxChar
	mov R22,R16
	.dbline 111
; 
; RealPageAddress = (int)((PageAddressHigh << 8) + PageAddressLow);
	mov R3,R20
	mov R2,R22
	sts _RealPageAddress+1,R3
	sts _RealPageAddress,R2
	.dbline 112
; PageAddress = ((unsigned int)PageAddressLow << 8);
	mov R2,R22
	clr R3
	mov R3,R2
	clr R2
	sts _PageAddress+1,R3
	sts _PageAddress,R2
	.dbline 113
; if (PageAddressHigh) PageAddress16 = 1;
	tst R20
	breq L19
	.dbline 113
	ldi R24,1
	sts _PageAddress16,R24
	xjmp L20
L19:
	.dbline 114
	clr R2
	sts _PageAddress16,R2
L20:
	.dbline -2
	.dbline 115
; else PageAddress16 = 0;
; }
L12:
	xcall pop_gset2
	.dbline 0 ; func end
	ret
	.dbsym r PageAddressLow 22 c
	.dbsym r PageAddressHigh 20 c
	.dbend
	.dbfunc e ExecCode _ExecCode fV
	.even
_ExecCode::
	.dbline -1
	.dbline 120
; 
; /*********************************************************/
; 
; void ExecCode(void)
; {
	.dbline 121
; RAMPZ = 0;
	clr R2
	out 0x3b,R2
	.dbline 122
; MCUCR = 0x01;	   	// Enable interrupt vector select
	ldi R24,1
	out 0x35,R24
	.dbline 123
; MCUCR = 0x00;	   	// Move interrupt vector to flash
	out 0x35,R2
	.dbline 124
; asm("jmp 0x0000"); // Run application code   
	jmp 0x0000
	.dbline -2
	.dbline 125
; }
L21:
	.dbline 0 ; func end
	ret
	.dbend
	.dbfunc e GetPage _GetPage fc
;       CheckSum -> R22
;  LocalCheckSum -> R10
;              i -> R20,R21
	.even
_GetPage::
	xcall push_gset3
	.dbline -1
	.dbline 130
; 
; /*********************************************************/
; 
; char GetPage(void)
; {
	.dbline 132
; unsigned int i;
; unsigned char LocalCheckSum = 0;
	clr R10
	.dbline 133
; unsigned char CheckSum = 0;
	clr R22
	.dbline 135
; 
; for (i=0;i<PageByte;i++)
	clr R20
	clr R21
	xjmp L26
X1:
	.dbline 136
; 	{
L27:
	.dbline 137
L28:
	.dbline 137
	xcall _IsChar
	tst R16
	breq L27
	.dbline 138
	xcall _RxChar
	ldi R24,<_PageBuffer
	ldi R25,>_PageBuffer
	movw R30,R20
	add R30,R24
	adc R31,R25
	std z+0,R16
	.dbline 139
	movw R30,R20
	add R30,R24
	adc R31,R25
	ldd R2,z+0
	add R10,R2
	.dbline 140
L24:
	.dbline 135
	subi R20,255  ; offset = 1
	sbci R21,255
L26:
	.dbline 135
	cpi R20,0
	ldi R30,1
	cpc R21,R30
	brlo L28
L30:
	.dbline 142
L31:
	.dbline 142
; 	while(!IsChar());
; 	PageBuffer[i]=RxChar();
; 	LocalCheckSum += PageBuffer[i];
;    	}
; 		   
; while(!IsChar());
	xcall _IsChar
	tst R16
	breq L30
	.dbline 143
; CheckSum = RxChar(); 
	xcall _RxChar
	mov R22,R16
	.dbline 144
; if (LocalCheckSum == CheckSum) return 1;
	cp R10,R16
	brne L33
	.dbline 144
	ldi R16,1
	xjmp L22
L33:
	.dbline 145
; else return 0;
	clr R16
	.dbline -2
L22:
	xcall pop_gset3
	.dbline 0 ; func end
	ret
	.dbsym r CheckSum 22 c
	.dbsym r LocalCheckSum 10 c
	.dbsym r i 20 i
	.dbend
	.dbfunc e WriteFlash _WriteFlash fV
;        TempInt -> R22,R23
;              i -> R20,R21
	.even
_WriteFlash::
	xcall push_gset2
	.dbline -1
	.dbline 151
; }
; 
; /*********************************************************/
; 
; void WriteFlash(void)
; {
	.dbline 155
; unsigned int i;
; unsigned int TempInt;
; 
; for (i=0;i<PageByte;i+=2)
	clr R20
	clr R21
	xjmp L39
L36:
	.dbline 156
	.dbline 157
	ldi R24,<_PageBuffer
	ldi R25,>_PageBuffer
	movw R30,R20
	adiw R30,1
	add R30,R24
	adc R31,R25
	ldd R2,z+0
	clr R3
	mov R3,R2
	clr R2
	movw R30,R20
	add R30,R24
	adc R31,R25
	ldd R22,z+0
	clr R23
	add R22,R2
	adc R23,R3
	.dbline 158
	movw R18,R20
	movw R16,R22
	xcall _fill_temp_buffer
	.dbline 159
L37:
	.dbline 155
	subi R20,254  ; offset = 2
	sbci R21,255
L39:
	.dbline 155
	cpi R20,0
	ldi R30,1
	cpc R21,R30
	brlo L36
	.dbline 160
;    	{
;    	TempInt=PageBuffer[i]+(PageBuffer[i+1]<<8);
;    	fill_temp_buffer(TempInt,i); //call asm routine.
;    	}
; if (PageAddress16) RAMPZ = 1;
	lds R2,_PageAddress16
	tst R2
	breq L40
	.dbline 160
	ldi R24,1
	out 0x3b,R24
L40:
	.dbline 162
; 
; write_page(PageAddress,0x03);       //Perform page ERASE
	ldi R18,3
	lds R16,_PageAddress
	lds R17,_PageAddress+1
	xcall _write_page
	.dbline 163
; write_page(PageAddress,0x05);       //Perform page write
	ldi R18,5
	lds R16,_PageAddress
	lds R17,_PageAddress+1
	xcall _write_page
	.dbline 165
;    
; enableRWW();
	xcall _enableRWW
	.dbline 166
; RAMPZ = 0;
	clr R2
	out 0x3b,R2
	.dbline -2
	.dbline 167
; }
L35:
	xcall pop_gset2
	.dbline 0 ; func end
	ret
	.dbsym r TempInt 22 i
	.dbsym r i 20 i
	.dbend
	.dbfunc e CheckFlash _CheckFlash fc
;       TempInt2 -> R22,R23
;        TempInt -> R10,R11
;              i -> R20,R21
	.even
_CheckFlash::
	xcall push_gset3
	.dbline -1
	.dbline 172
; 
; /*********************************************************/
; 
; char CheckFlash(void)
; {
	.dbline 177
; unsigned int i;							
; unsigned int TempInt;
; unsigned int TempInt2;
; 
; for (i=0;i<PageByte;i+=2)
	clr R20
	clr R21
	xjmp L46
L43:
	.dbline 178
;     {
	.dbline 179
; 	if (PageAddress16) RAMPZ = 1;
	lds R2,_PageAddress16
	tst R2
	breq L47
	.dbline 179
	ldi R24,1
	out 0x3b,R24
L47:
	.dbline 180
; 	TempInt = read_program_memory(PageAddress + i,0x00);
	clr R18
	lds R16,_PageAddress
	lds R17,_PageAddress+1
	add R16,R20
	adc R17,R21
	xcall _read_program_memory
	movw R10,R16
	.dbline 181
; 	RAMPZ = 0;
	clr R2
	out 0x3b,R2
	.dbline 182
; 	TempInt2 = PageBuffer[i] +(PageBuffer[i+1]<<8);
	ldi R24,<_PageBuffer
	ldi R25,>_PageBuffer
	movw R30,R20
	adiw R30,1
	add R30,R24
	adc R31,R25
	ldd R2,z+0
	clr R3
	mov R3,R2
	clr R2
	movw R30,R20
	add R30,R24
	adc R31,R25
	ldd R22,z+0
	clr R23
	add R22,R2
	adc R23,R3
	.dbline 183
; 	if (TempInt != TempInt2) return 0;
	cp R16,R22
	cpc R17,R23
	breq L49
	.dbline 183
	clr R16
	xjmp L42
L49:
	.dbline 184
L44:
	.dbline 177
	subi R20,254  ; offset = 2
	sbci R21,255
L46:
	.dbline 177
	cpi R20,0
	ldi R30,1
	cpc R21,R30
	brlo L43
	.dbline 185
;     }
; return 1;
	ldi R16,1
	.dbline -2
L42:
	xcall pop_gset3
	.dbline 0 ; func end
	ret
	.dbsym r TempInt2 22 i
	.dbsym r TempInt 10 i
	.dbsym r i 20 i
	.dbend
	.dbfunc e IsChar _IsChar fc
	.even
_IsChar::
	.dbline -1
	.dbline 191
; }
; 
; /*********************************************************/
; 
; unsigned char IsChar(void)
; {
	.dbline 192
; if (UCSR0A & 0x80) return 1;
	sbis 0xb,7
	rjmp L52
	.dbline 192
	ldi R16,1
	xjmp L51
L52:
	.dbline 193
; return 0;
	clr R16
	.dbline -2
L51:
	.dbline 0 ; func end
	ret
	.dbend
	.dbfunc e RxChar _RxChar fc
	.even
_RxChar::
	.dbline -1
	.dbline 199
; }
; 
; /*********************************************************/
; 
; unsigned char RxChar(void)
; {
	.dbline 200
; return UDR0;		   		  // Read char
	in R16,0xc
	.dbline -2
L54:
	.dbline 0 ; func end
	ret
	.dbend
	.dbfunc e TxChar _TxChar fV
;             ch -> R16
	.even
_TxChar::
	.dbline -1
	.dbline 206
; }
; 
; /*********************************************************/
; 
; void TxChar(unsigned char ch)
; {
L56:
	.dbline 207
L57:
	.dbline 207
; while (!(UCSR0A & 0x20)); // Wait for empty transmit buffer
	sbis 0xb,5
	rjmp L56
	.dbline 208
; UDR0 = ch;	  		   		  // Write char
	out 0xc,R16
	.dbline -2
	.dbline 209
; }
L55:
	.dbline 0 ; func end
	ret
	.dbsym r ch 16 c
	.dbend
	.dbfunc e Wait _Wait fV
;              i -> R16,R17
	.even
_Wait::
	.dbline -1
	.dbline 214
; 
; /*********************************************************/
; 
; void Wait()
; {
	.dbline 217
	ldi R16,-32000
	ldi R17,-125
L60:
	.dbline 217
L61:
	.dbline 217
; int i;
; 
; for (i=-32000;i<32000;i++); // Minimum for 16Mhz
	subi R16,255  ; offset = 1
	sbci R17,255
	.dbline 217
	cpi R16,0
	ldi R30,125
	cpc R17,R30
	brlt L60
	.dbline -2
	.dbline 218
; }
L59:
	.dbline 0 ; func end
	ret
	.dbsym r i 16 I
	.dbend
	.dbfunc e main _main fV
	.even
_main::
	.dbline -1
	.dbline 223
; 
; /*********************************************************/
; 
; void main(void)
; {
	.dbline 224
; DDRF = 0xff;   // for ata
	ldi R24,255
	sts 97,R24
	.dbline 225
; PORTF = 0xff;
	sts 98,R24
	.dbline 226
; DDRG = 0x02;
	ldi R24,2
	sts 100,R24
	.dbline 227
; PORTG = 0x02;
	sts 101,R24
	.dbline 229
; 
; PORTE  = 0x01;	// Pull up on RX line
	ldi R24,1
	out 0x3,R24
	.dbline 230
; UCSR0C = 0x86;  // Asyn,NoParity,1StopBit,8Bit,
	ldi R24,134
	sts 149,R24
	.dbline 231
; UBRR0L = 16;    // 8->115200 at 16Mhz // 16->57600 at 16Mhz // 25->38400bps at 16Mhz
	ldi R24,16
	out 0x9,R24
	.dbline 232
; UCSR0B = 0x18;  // Rx enable Tx Enable
	ldi R24,24
	out 0xa,R24
	.dbline 234
; 
; RxChar();
	xcall _RxChar
	.dbline 235
; TxChar('>');
	ldi R16,62
	xcall _TxChar
	.dbline 236
; Wait();
	xcall _Wait
	.dbline 237
; if (RxChar() == '<') 
	xcall _RxChar
	cpi R16,60
	brne L65
	.dbline 238
;    {
	.dbline 239
;    BootLoad();
	xcall _BootLoad
	.dbline 240
;    ExecCode();
	xcall _ExecCode
	.dbline 241
;    }
L65:
	.dbline 242
; ExecCode();
	.dbline -2
	.dbline 243
; }
L64:
	.dbline 0 ; func end
	xjmp _ExecCode
	.dbend
	.area bss(ram, con, rel)
	.dbfile C:\DATA\MP3\BootLoader\main.c
_RealPageAddress::
	.blkb 2
	.dbsym e RealPageAddress _RealPageAddress i
_PageAddress::
	.blkb 2
	.dbsym e PageAddress _PageAddress i
_PageBuffer::
	.blkb 256
	.dbsym e PageBuffer _PageBuffer A[256:256]c

⌨️ 快捷键说明

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