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

📄 newmain.s

📁 单片机MEGA128的BOOTLOADER程序。
💻 S
📖 第 1 页 / 共 2 页
字号:
	.module newmain.c
	.area data(ram, con, rel)
_address::
	.blkb 4
	.area idata
	.word 0,0
	.area data(ram, con, rel)
	.dbfile E:\cqt\cqt_boot\newmain.c
	.dbsym e address _address L
	.area text(rom, con, rel)
	.dbfile E:\cqt\cqt_boot\newmain.c
	.dbfunc e delay_us _delay_us fV
;           time -> R16,R17
	.even
_delay_us::
	.dbline -1
	.dbline 30
; 
; #include <macros.h>
; #include <iom128v.h>
; #include <eeprom.h>
; #include <string.h>
; #define SPM_PAGESIZE 256              //M128的一个Flash页为256字节(128字) 
; #define DATA_BUFFER_SIZE SPM_PAGESIZE
; //定义at45db161 控制I/O 
; #define CS     4      //PB4   at45db161 片选
; #define BUSY   5      //PB5   at45db161 忙
; #define WP     6      //PB6   at45db161 写
; #define RESET  7      //PB7   at45db161 复位
; 
; 
; //定义全局变量 
; 
; char data[DATA_BUFFER_SIZE]; 
; unsigned char Senddata[30];
; long  Filelength;
; long address = 0; 
; unsigned int page_counter_rd;
; unsigned int page_offset_rd;
; 
; unsigned char lengthbak1,lengthbak2;
; int timenum;
; int length;length1;
; unsigned char ROK_flag,Num;
; //延时
; void delay_us(int time)
; 	 {     
L2:
	.dbline 32
;   	  do
; 	  	{
	.dbline 33
; 		 time--;
	subi R16,1
	sbci R17,0
	.dbline 34
; 		}	
L3:
	.dbline 35
;   	  while (time!=0);
	cpi R16,0
	cpc R16,R17
	brne L2
X0:
	.dbline -2
L1:
	.dbline 0 ; func end
	ret
	.dbsym r time 16 I
	.dbend
	.dbfunc e boot_page_ew _boot_page_ew fV
;           code -> y+4
;      p_address -> y+0
	.even
_boot_page_ew::
	xcall push_arg4
	.dbline -1
	.dbline 41
; 	 }	  
; 
; 
; //擦除(code=0x03)和写入(code=0x05)一个Flash页 
; void boot_page_ew(long p_address,char code) 
; { 
	.dbline 42
;     asm("mov r30,r16\n" 
	mov r30,r16
	mov r31,r17
	out 0x3b,r18
	
	.dbline 45
;         "mov r31,r17\n" 
;         "out 0x3b,r18\n");            //将页地址放入Z寄存器和RAMPZ的Bit0中 
;     SPMCSR = code;                //寄存器SPMCSR中为操作码 
	ldd R0,y+4
	sts 104,R0
	.dbline 46
;     asm("spm\n");                    //对指定Flash页进行操作 
	spm
	
	.dbline -2
L5:
	adiw R28,4
	.dbline 0 ; func end
	ret
	.dbsym l code 4 c
	.dbsym l p_address 0 L
	.dbend
	.dbfunc e boot_page_fill _boot_page_fill fV
;           data -> R18,R19
;        address -> R16,R17
	.even
_boot_page_fill::
	.dbline -1
	.dbline 50
; }         
; //填充Flash缓冲页中的一个字 
; void boot_page_fill(unsigned int address,int data) 
; { 
	.dbline 51
;     asm("mov r30,r16\n" 
	mov r30,r16
	mov r31,r17
	mov r0,r18
	mov r1,r19
	
	.dbline 55
;         "mov r31,r17\n"             //Z寄存器中为填冲页内地址 
;         "mov r0,r18\n" 
;         "mov r1,r19\n");            //R0R1中为一个指令字 
;     SPMCSR = 0x01; 
	ldi R24,1
	sts 104,R24
	.dbline 56
;     asm("spm\n"); 
	spm
	
	.dbline -2
L6:
	.dbline 0 ; func end
	ret
	.dbsym r data 18 I
	.dbsym r address 16 i
	.dbend
	.dbfunc e wait_page_rw_ok _wait_page_rw_ok fV
	.even
_wait_page_rw_ok::
	.dbline -1
	.dbline 60
; } 
; //等待一个Flash页的写完成 
; void wait_page_rw_ok(void) 
; { 
	xjmp L9
X1:
	.dbline 62
;       while(SPMCSR & 0x40) 
;      { 
L11:
	.dbline 63
L12:
	.dbline 63
	lds R2,104
	sbrc R2,0
	rjmp L11
	.dbline 64
	ldi R24,17
	sts 104,R24
	.dbline 65
	spm
	
	.dbline 66
L9:
	.dbline 61
	lds R2,104
	sbrc R2,6
	rjmp L12
	.dbline -2
L7:
	.dbline 0 ; func end
	ret
	.dbend
	.dbfunc e write_one_page _write_one_page fV
;              i -> R20,R21
	.even
_write_one_page::
	xcall push_gset1
	sbiw R28,1
	.dbline -1
	.dbline 70
;          while(SPMCSR & 0x01); 
;          SPMCSR = 0x11; 
;          asm("spm\n"); 
;      } 
; } 
; //更新一个Flash页的完整处理 
; void write_one_page(void) 
; { 
	.dbline 72
;     int i; 
;     boot_page_ew(address,0x03);                    //擦除一个Flash页 
	ldi R24,3
	std y+0,R24
	lds R18,_address+2
	lds R19,_address+2+1
	lds R16,_address
	lds R17,_address+1
	xcall _boot_page_ew
	.dbline 73
;     wait_page_rw_ok();                            //等待擦除完成 
	xcall _wait_page_rw_ok
	.dbline 74
;     for(i=0;i<SPM_PAGESIZE;i+=2)  //jia
	clr R20
	clr R21
L15:
	.dbline 75
	.dbline 76
	ldi R24,<_data+1
	ldi R25,>_data+1
	movw R30,R20
	add R30,R24
	adc R31,R25
	ldd R2,z+0
	clr R3
	mov R3,R2
	clr R2
	ldi R24,<_data
	ldi R25,>_data
	movw R30,R20
	add R30,R24
	adc R31,R25
	ldd R18,z+0
	clr R19
	add R18,R2
	adc R19,R3
	movw R16,R20
	xcall _boot_page_fill
	.dbline 77
L16:
	.dbline 74
	subi R20,254  ; offset = 2
	sbci R21,255
	.dbline 74
	cpi R20,0
	ldi R30,1
	cpc R21,R30
	brlt L15
	.dbline 78
; 	  { 
;         boot_page_fill(i, data[i]+(data[i+1]<<8)); 
;     } 
;     boot_page_ew(address,0x05);                    //将缓冲页数据写入一个Flash页 
	ldi R24,5
	std y+0,R24
	lds R18,_address+2
	lds R19,_address+2+1
	lds R16,_address
	lds R17,_address+1
	xcall _boot_page_ew
	.dbline 79
;     wait_page_rw_ok();                            //等待写入完成 
	xcall _wait_page_rw_ok
	.dbline -2
L14:
	adiw R28,1
	xcall pop_gset1
	.dbline 0 ; func end
	ret
	.dbsym r i 20 I
	.dbend
	.dbfunc e quit _quit fV
	.even
_quit::
	.dbline -1
	.dbline 84
; }         
; 
; //退出Bootloader程序,从0x0000处执行应用程序 
; void quit(void) 
; {     
	.dbline 85
;  	 MCUCR = 0x01; 
	ldi R24,1
	out 0x35,R24
	.dbline 86
;      MCUCR = 0x00;                    //将中断向量表迁移到应用程序区头部 
	clr R2
	out 0x35,R2
	.dbline 87
;      RAMPZ = 0x00;                    //RAMPZ清零初始化 
	out 0x3b,R2
	.dbline 88
;      asm("jmp 0x0000\n");                //跳转到Flash的0x0000处,执行用户的应用程序 
	jmp 0x0000
	
	.dbline -2
L20:
	.dbline 0 ; func end
	ret
	.dbend
	.dbfunc e Init_mag128 _Init_mag128 fV
	.even
_Init_mag128::
	.dbline -1
	.dbline 92
; } 
; //初始化I/O
; void Init_mag128(void)
; {
	.dbline 93
;  PORTA = 0x00;
	clr R2
	out 0x1b,R2
	.dbline 94
;  DDRA  = 0x00;
	out 0x1a,R2
	.dbline 95
;  PORTB = 0xd7;
	ldi R24,215
	out 0x18,R24
	.dbline 96
;  DDRB  = 0xd7;
	out 0x17,R24
	.dbline 97
;  PORTC = 0xff; //m103 output only
	ldi R24,255
	out 0x15,R24
	.dbline 98
;  DDRC  = 0x00;
	out 0x14,R2
	.dbline 99
;  PORTD = 0x01;
	ldi R24,1
	out 0x12,R24
	.dbline 100
;  DDRD  = 0xf1;
	ldi R24,241
	out 0x11,R24
	.dbline 101
;  PORTE = 0x02;
	ldi R24,2
	out 0x3,R24
	.dbline 102
;  DDRE  = 0x02;
	out 0x2,R24
	.dbline 103
;  PORTF = 0x00;
	sts 98,R2
	.dbline 104
;  DDRF  = 0x00;
	sts 97,R2
	.dbline 105
;  PORTG = 0x0;
	sts 101,R2
	.dbline 106
;  DDRG  = 0x0;
	sts 100,R2
	.dbline -2
L21:
	.dbline 0 ; func end
	ret
	.dbend
	.dbfunc e Init_SPI _Init_SPI fV
	.even
_Init_SPI::
	.dbline -1
	.dbline 110
; }
; //初始化 SPI
; void Init_SPI(void)
; {
	.dbline 111
;  SPCR = 0x5e; //setup SPI
	ldi R24,94
	out 0xd,R24
	.dbline 112
;  SPSR = 0x00; //setup SPI
	clr R2
	out 0xe,R2
	.dbline -2
L22:
	.dbline 0 ; func end
	ret
	.dbend
	.dbfunc e delay_ms _delay_ms fV
;           time -> R20,R21
	.even
_delay_ms::
	xcall push_gset1
	movw R20,R16
	.dbline -1
	.dbline 117
; }
; 
; /*	  		    毫秒级延时程序			*/	 
; void delay_ms(unsigned int time)
; 	 {
	xjmp L25
L24:
	.dbline 119
; 	  while(time!=0)
; 	  	  {		
	.dbline 120
; 		   delay_us(1850);
	ldi R16,1850
	ldi R17,7
	xcall _delay_us
	.dbline 121
; 		   time--;
	subi R20,1
	sbci R21,0
	.dbline 122
; 		    if(PORTD&0x01==1)PORTD=PORTD&0xfe;
	sbis 0x12,0
	rjmp L27
	.dbline 122
	in R24,0x12
	andi R24,254
	out 0x12,R24
	xjmp L28
L27:
	.dbline 123
;   			else PORTD=PORTD|0x1;
	sbi 0x12,0
L28:
	.dbline 124
L25:
	.dbline 118
	cpi R20,0
	cpc R20,R21
	brne L24
X2:
	.dbline -2
L23:
	xcall pop_gset1
	.dbline 0 ; func end
	ret
	.dbsym r time 20 i
	.dbend
	.dbfunc e clr_dog _clr_dog fV
	.even
_clr_dog::
	.dbline -1
	.dbline 127
; 		  }
; 	 }	
; void clr_dog(void)
; {
	.dbline 128
; if(PORTD&0x01==1)PORTD=PORTD&0xfe;
	sbis 0x12,0
	rjmp L30
	.dbline 128
	in R24,0x12
	andi R24,254
	out 0x12,R24
	xjmp L31
L30:
	.dbline 129
	sbi 0x12,0
L31:
	.dbline -2
L29:
	.dbline 0 ; func end
	ret
	.dbend
	.dbfunc e df_reset _df_reset fV
	.even
_df_reset::
	.dbline -1
	.dbline 134
; else PORTD=PORTD|0x1;
; }
; //spi模式3
; //flash复位
; void df_reset(void)
; {
	.dbline 135
; PORTB&=~(1<<RESET);  
	cbi 0x18,7
	.dbline 136
; delay_us(500);
	ldi R16,500
	ldi R17,1
	xcall _delay_us
	.dbline 137
; PORTB|=1<<RESET;
	sbi 0x18,7
	.dbline -2
L32:
	.dbline 0 ; func end
	ret
	.dbend
	.dbfunc e SPI_TranByte _SPI_TranByte fc
;          cData -> R16
	.even
_SPI_TranByte::
	.dbline -1
	.dbline 141
; }
; //主模式下发送一个字节或接收一个字节
; unsigned char SPI_TranByte(unsigned char cData)
; {
	.dbline 142
; SPDR=cData;
	out 0xf,R16
L34:
	.dbline 143
L35:
	.dbline 143
; while(!(SPSR&0x80));
	sbis 0xe,7
	rjmp L34
	.dbline 144
; return SPDR;
	in R16,0xf
	.dbline -2
L33:
	.dbline 0 ; func end
	ret
	.dbsym r cData 16 c
	.dbend
	.dbfunc e Read_Testfile _Read_Testfile fV
;              j -> <dead>
;              i -> <dead>
;            len -> R22,R23
;           File -> R20,R21
	.even
_Read_Testfile::
	xcall push_gset2
	movw R22,R18
	movw R20,R16
	.dbline -1
	.dbline 148
; }
; 
; void Read_Testfile(char *File ,int len )
; {
L38:
	.dbline 150
L39:
	.dbline 150
; unsigned char i,j;
; while(!(PINB&0x20));
	sbis 0x16,5
	rjmp L38
	.dbline 151
; PORTB&=~(1<<CS);//使能 at45
	cbi 0x18,4
	.dbline 152
; SPI_TranByte(0x3);
	ldi R16,3
	xcall _SPI_TranByte
	.dbline 153
; SPI_TranByte((char)(page_counter_rd>>6));//页地址PA11-PA6
	ldi R18,6
	ldi R19,0
	lds R16,_page_counter_rd
	lds R17,_page_counter_rd+1
	xcall lsr16
	xcall _SPI_TranByte
	.dbline 154
; SPI_TranByte((char)((page_counter_rd<<2)|(page_offset_rd>>8)));////页地址PA5-PA0
	lds R2,_page_offset_rd
	lds R3,_page_offset_rd+1
	mov R2,R3
	clr R3
	lds R16,_page_counter_rd
	lsl R16
	rol R17
	lsl R16
	rol R17
	or R16,R2
	or R17,R3
	xcall _SPI_TranByte
	.dbline 155
; SPI_TranByte((char)page_offset_rd);
	lds R16,_page_offset_rd
	xcall _SPI_TranByte
	xjmp L42
L41:
	.dbline 157
;  while(len)
;   {
	.dbline 158
;   SPDR=0;
	clr R2
	out 0xf,R2
L44:
	.dbline 159
L45:
	.dbline 159
;   while(!(SPSR&0x80));
	sbis 0xe,7
	rjmp L44
	.dbline 160
;   *File=SPDR;
	in R2,0xf
	movw R30,R20
	std z+0,R2
	.dbline 161
;   File++; 
	subi R20,255  ; offset = 1
	sbci R21,255
	.dbline 162
;   len--;
	subi R22,1
	sbci R23,0
	.dbline 163
;   page_offset_rd++;
	lds R24,_page_offset_rd
	lds R25,_page_offset_rd+1
	adiw R24,1
	sts _page_offset_rd+1,R25
	sts _page_offset_rd,R24
	.dbline 164
;   if(page_offset_rd>=528)
	cpi R24,16
	ldi R30,2
	cpc R25,R30
	brlo L47
	.dbline 165
;        {
	.dbline 166
; 	   page_offset_rd=0;
	clr R2
	clr R3
	sts _page_offset_rd+1,R3
	sts _page_offset_rd,R2
	.dbline 167
; 	   page_counter_rd++;
	lds R24,_page_counter_rd
	lds R25,_page_counter_rd+1
	adiw R24,1
	sts _page_counter_rd+1,R25
	sts _page_counter_rd,R24
	.dbline 168
; 	   if(page_counter_rd>=4096){quit();page_counter_rd=3849;}
	cpi R24,0
	ldi R30,16
	cpc R25,R30
	brlo L49
	.dbline 168
	.dbline 168
	xcall _quit
	.dbline 168
	ldi R24,3849
	ldi R25,15
	sts _page_counter_rd+1,R25
	sts _page_counter_rd,R24
	.dbline 168
L49:
	.dbline 169
; 	   }
L47:
	.dbline 171
L42:
	.dbline 156
	cpi R22,0
	cpc R22,R23
	brne L41
X3:
	.dbline 172
; 	   
;   }
; PORTB|=1<<CS; 
	sbi 0x18,4
	.dbline -2
L37:
	xcall pop_gset2
	.dbline 0 ; func end
	ret
	.dbsym l j 1 c
	.dbsym l i 1 c
	.dbsym r len 22 I
	.dbsym r File 20 pc
	.dbend
	.dbfunc e uart1_init _uart1_init fV
	.even
_uart1_init::
	.dbline -1
	.dbline 180
; }
; //UART1 initialize
; // desired baud rate:57600
; // actual baud rate:57599 (0.0%)
; // char size: 8 bit
; // parity: Disabled
; void uart1_init(void)
; {
	.dbline 181
;  UCSR1B = 0x00; //disable while setting baud rate
	clr R2
	sts 154,R2
	.dbline 182
;  UCSR1A = 0x00;
	sts 155,R2
	.dbline 183
;  UCSR1C = 0x06;
	ldi R24,6
	sts 157,R24
	.dbline 184
;  UBRR1L = 0x0B; //set baud rate lo
	ldi R24,11
	sts 153,R24
	.dbline 185
;  UBRR1H = 0x00; //set baud rate hi
	sts 152,R2
	.dbline 186
;  UCSR1B = 0x18;
	ldi R24,24
	sts 154,R24
	.dbline -2
L51:
	.dbline 0 ; func end
	ret
	.dbend
	.dbfunc e RecChar _RecChar fc
	.even
_RecChar::
	.dbline -1
	.dbline 203
; }
; /*//UART1 initialize
; // desired baud rate:19200
; // actual baud rate:19200 (0.0%)
; // char size: 8 bit
; // parity: Disabled
; void uart1_init(void)
; {
;  UCSR1B = 0x00; //disable while setting baud rate
;  UCSR1A = 0x00;
;  UCSR1C = 0x06;
;  UBRR1L = 0x23; //set baud rate lo
;  UBRR1H = 0x00; //set baud rate hi
;  UCSR1B = 0x18;
; }*/
; unsigned char RecChar(void)
;   {
	.dbline 204
;       timenum=0;
	clr R2
	clr R3
	sts _timenum+1,R3
	sts _timenum,R2
L53:
	.dbline 205
;      do{
	.dbline 206
; 	   timenum++;
	lds R24,_timenum
	lds R25,_timenum+1
	adiw R24,1
	sts _timenum+1,R25
	sts _timenum,R24
	.dbline 207
; 	   if(timenum>50000){timenum=0;clr_dog();}
	ldi R24,50000
	ldi R25,195
	lds R2,_timenum
	lds R3,_timenum+1
	cp R24,R2
	cpc R25,R3
	brsh L56
	.dbline 207
	.dbline 207
	clr R2
	clr R3
	sts _timenum+1,R3
	sts _timenum,R2
	.dbline 207
	xcall _clr_dog
	.dbline 207
L56:
	.dbline 208
L54:
	.dbline 208
;        }while(!(UCSR1A & 0x80));
	lds R2,155
	sbrs R2,7
	rjmp L53
	.dbline 209
;      return UDR1;
	lds R16,156
	.dbline -2
L52:
	.dbline 0 ; func end
	ret
	.dbend
	.dbfunc e Send_ByteUART1 _Send_ByteUART1 fV
;             ch -> R16
	.even
_Send_ByteUART1::
	.dbline -1
	.dbline 223
;   }		
; /*unsigned char RecChar(void)
;   {
;        timenum=0;
;      do{
; 	   timenum++;
; 	   clr_dog();
;        }while((!(UCSR0A & 0x80))&&(timenum<10000000));
;      return UDR0;
;   }		 */ 
;   
;  /*  		字符输出函数 		 */
; void  Send_ByteUART1(char  ch)
; 	{	
	.dbline 224
; 	 UDR1=ch;
	sts 156,R16
L59:
	.dbline 225
L60:
	.dbline 225
;      while (!(UCSR1A&(1<<UDRE1)));          //UCSRA USART 控制状态寄存器A  UDRE=TI
	lds R2,155
	sbrs R2,5
	rjmp L59
	.dbline -2
L58:
	.dbline 0 ; func end
	ret
	.dbsym r ch 16 c
	.dbend
	.dbfunc e send232 _send232 fV
;              i -> R20
;              l -> R22
;              s -> R10,R11
	.even
_send232::
	xcall push_gset3
	mov R22,R18
	movw R10,R16
	.dbline -1
	.dbline 229
; 	} 
; 	
; void send232(unsigned char *s,unsigned char l)
; {
	.dbline 231
; unsigned char i;
; for(i=0;i<l;i++)
	clr R20
	xjmp L66
L63:
	.dbline 232
	mov R30,R20
	clr R31
	add R30,R10
	adc R31,R11
	ldd R16,z+0
	xcall _Send_ByteUART1
L64:
	.dbline 231

⌨️ 快捷键说明

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