📄 at45db161d.s
字号:
.module at45db161d.c
.area text(rom, con, rel)
.dbfile E:\ICC128\2008控制台\2008console0TEST\flashTest\at45db161d.c
.dbfunc e df_init _df_init fV
.even
_df_init::
.dbline -1
.dbline 42
; /************************************************
; flash 读写程序
;
; 2008年6月20日
;
; *************************************************/
; #include "2008.h"
;
; #define DF_PAGE_SIZE 528
; #define DF_READ_BUFFER 0
; #define DF_WRITE_BUFFER 1
;
; #define MM_PAGE_TO_B1_XFER 0x53 //主存储器页到缓存区转移命令(缓冲区1 53H 缓冲区2 55H)
; // main memory page to buffer 2 transfer
; #define MM_PAGE_TO_B2_XFER 0x55 //主存储器页到缓存区转移命令(缓冲区1 53H 缓冲区2 55H)
; // buffer 1 to main memory page program with built-in erase
; #define B1_TO_MM_PAGE_PROG_WITH_ERASE 0x83
; // buffer 2 to main memory page program with built-in erase
; #define B2_TO_MM_PAGE_PROG_WITH_ERASE 0x86
; // buffer 1 read
; #define BUFFER_1_READ 0xd1 //0xD4
; // buffer 2 read
; #define BUFFER_2_READ 0xd3 //0xD6
; // buffer 1 write
; #define BUFFER_1_WRITE 0x84
; // buffer 2 write
; #define BUFFER_2_WRITE 0x87
;
;
; #define STATUS_REGISTER 0xD7
;
;
; #define UNSELECT_CHIP PORTB |= 0x10;
; #define SELECT_CHIP PORTB &= 0xef;
;
; uint g_CurReadPage;//当前读的页地址
; uint g_CurReadByte;//当前读的字节(页中地址)
; uint g_CurWritePage;//当前写的页地址
; uint g_CurWriteByte;//当前写的字节地址(页中地址)
; //====================================================================
; void df_init(void)
; {
.dbline 46
; //MISO设置为输入,上拉电阻关闭,其它默认为1
; //PORTB |= _BV(FLASH_SCK)|_BV(FLASH_MOSI)|_BV(FLASH_CS)|_BV(PB2);
; //PORTB |= 1 2 4 0
; PORTB |= 0x17;
in R24,0x18
ori R24,23
out 0x18,R24
.dbline 53
; /*
; 注:使用AVR单片机SPI口,并用作主器件时SS引脚(ATMEGA8中PB2)要拉高或设置成输出!
; */
; //SCK,MOSI和CS端口对应脚设置为输出
; //DDRB |= _BV(FLASH_SCK)|_BV(FLASH_MOSI)|_BV(FLASH_CS);
; //DDRB |= 1 2 4
; DDRB |= 0X16; //配置为输出
in R24,0x17
ori R24,22
out 0x17,R24
.dbline 56
; // SPI中断禁止, SPI使能, master模式, MSB 前, SPI 模式 3, SCK频率Fcl/4
; //SPCR = _BV(SPE)|_BV(MSTR)|_BV(CPHA)|_BV(CPOL);//|_BV(SPR1)|_BV(SPR0);
; SPCR = 0x5c; //SPI控制寄存器
ldi R24,92
out 0xd,R24
.dbline -2
L1:
.dbline 0 ; func end
ret
.dbend
.dbfunc e df_write_open _df_write_open fV
; addr -> R20,R21
.even
_df_write_open::
xcall push_gset1
movw R20,R16
.dbline -1
.dbline 62
;
; }
; //======================================================================
; //写初始化功能函数,addr为打开后读到的初始地址
; void df_write_open(uint addr)
; {
.dbline 63
; g_CurWritePage=addr/DF_PAGE_SIZE;
ldi R18,528
ldi R19,2
movw R16,R20
xcall div16u
sts _g_CurWritePage+1,R17
sts _g_CurWritePage,R16
.dbline 64
; g_CurWriteByte=addr%DF_PAGE_SIZE;
ldi R18,528
ldi R19,2
movw R16,R20
xcall mod16u
sts _g_CurWriteByte+1,R17
sts _g_CurWriteByte,R16
.dbline 65
; load_page_to_buffer(g_CurWritePage,DF_WRITE_BUFFER);
ldi R18,1
ldi R19,0
lds R16,_g_CurWritePage
lds R17,_g_CurWritePage+1
xcall _load_page_to_buffer
.dbline -2
L2:
xcall pop_gset1
.dbline 0 ; func end
ret
.dbsym r addr 20 i
.dbend
.dbfunc e df_read_open _df_read_open fV
; addr -> R20,R21
.even
_df_read_open::
xcall push_gset1
movw R20,R16
.dbline -1
.dbline 70
; }
; //======================================================================
; //读初始化功能函数,addr为打开后读到的初始地址
; void df_read_open(uint addr)
; {
.dbline 71
; g_CurReadPage=addr/DF_PAGE_SIZE;
ldi R18,528
ldi R19,2
movw R16,R20
xcall div16u
sts _g_CurReadPage+1,R17
sts _g_CurReadPage,R16
.dbline 72
; g_CurReadByte=addr%DF_PAGE_SIZE;
ldi R18,528
ldi R19,2
movw R16,R20
xcall mod16u
sts _g_CurReadByte+1,R17
sts _g_CurReadByte,R16
.dbline 73
; load_page_to_buffer(g_CurReadPage,DF_READ_BUFFER);
clr R18
clr R19
lds R16,_g_CurReadPage
lds R17,_g_CurReadPage+1
xcall _load_page_to_buffer
.dbline -2
L3:
xcall pop_gset1
.dbline 0 ; func end
ret
.dbsym r addr 20 i
.dbend
.dbfunc e load_page_to_buffer _load_page_to_buffer fV
; buffer -> R22,R23
; page -> R20,R21
.even
_load_page_to_buffer::
xcall push_gset2
movw R22,R18
movw R20,R16
.dbline -1
.dbline 78
; }
; //=======================================================================
; //读主存储器指定页到读缓冲区(BUFFER1)
; void load_page_to_buffer(uint page,uint buffer)
; {
.dbline 79
; SELECT_CHIP;
in R24,0x18
andi R24,239
out 0x18,R24
.dbline 79
.dbline 81
;
; if(buffer == DF_READ_BUFFER) //=0
cpi R22,0
cpc R22,R23
brne L5
X0:
.dbline 82
; spi_write(MM_PAGE_TO_B1_XFER); //0x53
ldi R16,83
ldi R17,0
xcall _spi_write
xjmp L6
L5:
.dbline 84
; else
; spi_write(MM_PAGE_TO_B2_XFER); //0x55
ldi R16,85
ldi R17,0
xcall _spi_write
L6:
.dbline 85
; spi_write((uint)(page >> 6));
ldi R18,6
ldi R19,0
movw R16,R20
xcall lsr16
xcall _spi_write
.dbline 86
; spi_write((uint)(page << 2));
movw R16,R20
lsl R16
rol R17
lsl R16
rol R17
xcall _spi_write
.dbline 87
; spi_write(0x00);
clr R16
clr R17
xcall _spi_write
.dbline 89
;
; UNSELECT_CHIP;
sbi 0x18,4
.dbline 89
.dbline 91
;
; df_wait_busy();
xcall _df_wait_busy
.dbline -2
L4:
xcall pop_gset2
.dbline 0 ; func end
ret
.dbsym r buffer 22 i
.dbsym r page 20 i
.dbend
.dbfunc e spi_write _spi_write fi
; data -> R16,R17
.even
_spi_write::
.dbline -1
.dbline 96
; }
; //=======================================================================
; //从SPI口输出一字节数据
; uint spi_write(uint data)
; {
.dbline 97
; SPDR = data;
out 0xf,R16
L8:
.dbline 98
L9:
.dbline 98
; while(!(SPSR & 0x80)); //判断 SPIF
sbis 0xe,7
rjmp L8
.dbline 99
; return SPDR;
in R16,0xf
clr R17
.dbline -2
L7:
.dbline 0 ; func end
ret
.dbsym r data 16 i
.dbend
.dbfunc e df_wait_busy _df_wait_busy fV
.even
_df_wait_busy::
.dbline -1
.dbline 104
; }
; //=======================================================================
; //检测并等待器件忙状态,8引脚封闭器件没有 RDY/BUSY引脚 为些通过读状态寄存器来检测忙状态
; void df_wait_busy(void)
; {
.dbline 105
; SELECT_CHIP;
in R24,0x18
andi R24,239
out 0x18,R24
.dbline 105
.dbline 106
; spi_write(STATUS_REGISTER);
ldi R16,215
ldi R17,0
xcall _spi_write
xjmp L13
L12:
.dbline 108
; while(1)
; {
.dbline 109
; if(spi_write(0) & 0x80) //读取的最高位0时器件忙
clr R16
clr R17
xcall _spi_write
sbrs R16,7
rjmp L15
.dbline 110
; break;
xjmp L14
L15:
.dbline 111
L13:
.dbline 107
xjmp L12
L14:
.dbline 112
; }
; UNSELECT_CHIP;
sbi 0x18,4
.dbline 112
.dbline -2
L11:
.dbline 0 ; func end
ret
.dbend
.dbfunc e df_write _df_write fV
; temp -> R10,R11
; size -> R20,R21
; buf -> R22,R23
.even
_df_write::
xcall push_gset3
movw R20,R18
movw R22,R16
sbiw R28,2
.dbline -1
.dbline 116
; }
; //=======================================================================
; void df_write(uint *buf,uint size) //buf 源数据 size 写多少个字节
; {
.dbline 119
; uint temp;
;
; if((g_CurWriteByte + size) > DF_PAGE_SIZE) //如果当前页未写空间不够size字节
lds R2,_g_CurWriteByte
lds R3,_g_CurWriteByte+1
add R2,R20
adc R3,R21
ldi R24,528
ldi R25,2
cp R24,R2
cpc R25,R3
brlo X1
xjmp L18
X1:
.dbline 120
; {
.dbline 122
; //写当前页剩余空间的数据
; temp=DF_PAGE_SIZE - g_CurWriteByte;
lds R2,_g_CurWriteByte
lds R3,_g_CurWriteByte+1
movw R10,R24
sub R10,R2
sbc R11,R3
.dbline 123
; write_buffer(g_CurWriteByte,buf,temp); //将数据写入写缓冲区
std y+1,R11
std y+0,R10
movw R18,R22
movw R16,R2
xcall _write_buffer
.dbline 125
; //保存当前页
; write_page_from_buffer(g_CurWritePage,DF_WRITE_BUFFER);
ldi R18,1
ldi R19,0
lds R16,_g_CurWritePage
lds R17,_g_CurWritePage+1
xcall _write_page_from_buffer
.dbline 126
; g_CurWritePage++;
lds R24,_g_CurWritePage
lds R25,_g_CurWritePage+1
adiw R24,1
sts _g_CurWritePage+1,R25
sts _g_CurWritePage,R24
.dbline 127
; load_page_to_buffer(g_CurWritePage,DF_WRITE_BUFFER);
ldi R18,1
ldi R19,0
movw R16,R24
xcall _load_page_to_buffer
.dbline 130
;
; //写入到下一页对应缓冲区
; g_CurWriteByte=size-temp;
movw R2,R20
sub R2,R10
sbc R3,R11
sts _g_CurWriteByte+1,R3
sts _g_CurWriteByte,R2
.dbline 131
; write_buffer(0,buf+temp,g_CurWriteByte);
std y+1,R3
std y+0,R2
ldi R16,2
ldi R17,0
movw R18,R10
xcall empy16s
movw R18,R16
add R18,R22
adc R19,R23
clr R16
clr R17
xcall _write_buffer
.dbline 132
; }
xjmp L19
L18:
.dbline 134
; else
; {
.dbline 135
; write_buffer(g_CurWriteByte,buf,size);
std y+1,R21
std y+0,R20
movw R18,R22
lds R16,_g_CurWriteByte
lds R17,_g_CurWriteByte+1
xcall _write_buffer
.dbline 136
; g_CurWriteByte+=size;
lds R2,_g_CurWriteByte
lds R3,_g_CurWriteByte+1
add R2,R20
adc R3,R21
sts _g_CurWriteByte+1,R3
sts _g_CurWriteByte,R2
.dbline 139
;
; //缓冲已满,写入到主存储区
; if(g_CurWriteByte==DF_PAGE_SIZE)
movw R24,R2
cpi R24,16
ldi R30,2
cpc R25,R30
brne L20
.dbline 140
; {
.dbline 141
; g_CurWriteByte=0;
clr R2
clr R3
sts _g_CurWriteByte+1,R3
sts _g_CurWriteByte,R2
.dbline 142
; write_page_from_buffer(g_CurWritePage,DF_WRITE_BUFFER);
ldi R18,1
ldi R19,0
lds R16,_g_CurWritePage
lds R17,_g_CurWritePage+1
xcall _write_page_from_buffer
.dbline 143
; g_CurWritePage++;
lds R24,_g_CurWritePage
lds R25,_g_CurWritePage+1
adiw R24,1
sts _g_CurWritePage+1,R25
sts _g_CurWritePage,R24
.dbline 144
; load_page_to_buffer(g_CurWritePage,DF_WRITE_BUFFER);
ldi R18,1
ldi R19,0
movw R16,R24
xcall _load_page_to_buffer
.dbline 145
; }
L20:
.dbline 146
L19:
.dbline -2
L17:
adiw R28,2
xcall pop_gset3
.dbline 0 ; func end
ret
.dbsym r temp 10 i
.dbsym r size 20 i
.dbsym r buf 22 pi
.dbend
.dbfunc e write_buffer _write_buffer fV
; i -> R20,R21
; size -> R22,R23
; data -> R10,R11
; addr -> R20,R21
.even
_write_buffer::
xcall push_gset3
movw R10,R18
movw R20,R16
ldd R22,y+6
ldd R23,y+7
.dbline -1
.dbline 151
; }
; }
; //========================================================================
; //将数据写入写缓冲区2 命令 0x87
; void write_buffer(uint addr,uint *data,uint size)
; {
.dbline 153
; uint i;
; SELECT_CHIP;
in R24,0x18
andi R24,239
out 0x18,R24
.dbline 153
.dbline 154
; spi_write(BUFFER_2_WRITE);
ldi R16,135
ldi R17,0
xcall _spi_write
.dbline 155
; spi_write(0x00); //写入缓冲区地址15位无关位
clr R16
clr R17
xcall _spi_write
.dbline 156
; spi_write((uint)(addr>>8)); //写入缓冲区地址
movw R16,R20
mov R16,R17
clr R17
xcall _spi_write
.dbline 157
; spi_write((uint)addr); //写入缓冲区地址 总共写了三个字节地址 24位 15位无关位
movw R16,R20
xcall _spi_write
.dbline 158
; for(i=0;i<size;i++)
clr R20
clr R21
xjmp L26
L23:
.dbline 159
ldi R16,2
ldi R17,0
movw R18,R20
xcall empy16s
movw R30,R16
add R30,R10
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -