📄 sd.s
字号:
.module sd.c
.area text(rom, con, rel)
.dbfile C:\AVR\M\1\sd.c
.dbfunc e SD_Port_Init _SD_Port_Init fV
.even
_SD_Port_Init::
.dbline -1
.dbline 12
;
; #include"sd.h"
; #include"Usart.h"
;
;
; //---------------------------------
; //
; // SD卡接口初始化函数
; //
; //---------------------------------
; void SD_Port_Init(void)
; {
.dbline 13
; SD_CS_DDR |= 1<<SD_CS_BIT;
sbi 0x1a,1
.dbline 14
; SD_CS_PORT |= 1<<SD_CS_BIT;
sbi 0x1b,1
.dbline -2
L1:
.dbline 0 ; func end
ret
.dbend
.dbfunc e SD_SendCommand _SD_SendCommand fc
; retry -> R22
; r1 -> R20
; arg -> y+6
; cmd -> R10
.even
_SD_SendCommand::
st -y,r19
st -y,r18
xcall push_gset3
mov R10,R16
.dbline -1
.dbline 27
; }
;
;
;
; //---------------------------------
; //
; // SD卡写寄存器函数
; // arg:命令
; // cmd:命令号
; //
; //---------------------------------
; uint8 SD_SendCommand(uint8 cmd, uint32 arg)
; {
.dbline 28
; uint8 r1=1;
ldi R20,1
.dbline 29
; uint8 retry=0;
clr R22
.dbline 32
;
;
; SPI_SrByte(0xff);
ldi R16,255
xcall _SPI_SrByte
.dbline 33
; SPI_CS_Assert;
cbi 0x1b,1
L3:
.dbline 34
L4:
.dbline 34
; while(r1--);
mov R2,R20
clr R3
subi R20,1
tst R2
brne L3
.dbline 36
;
; SPI_SrByte((cmd & 0x3f) | 0x40); /* send command */
mov R16,R10
andi R16,63
ori R16,64
xcall _SPI_SrByte
.dbline 38
;
; SPI_SrByte(arg>>24);
ldi R24,24
ldi R25,0
movw R30,R28
ldd R2,z+6
ldd R3,z+7
ldd R4,z+8
ldd R5,z+9
st -y,R24
movw R16,R2
movw R18,R4
xcall lsr32
xcall _SPI_SrByte
.dbline 39
; SPI_SrByte(arg>>16);
movw R30,R28
ldd R2,z+6
ldd R3,z+7
ldd R4,z+8
ldd R5,z+9
movw R2,R4
clr R4
clr R5
mov R16,R2
xcall _SPI_SrByte
.dbline 40
; SPI_SrByte(arg>>8);
ldi R24,8
ldi R25,0
movw R30,R28
ldd R2,z+6
ldd R3,z+7
ldd R4,z+8
ldd R5,z+9
st -y,R24
movw R16,R2
movw R18,R4
xcall lsr32
xcall _SPI_SrByte
.dbline 41
; SPI_SrByte(arg);
movw R30,R28
ldd R16,z+6
xcall _SPI_SrByte
.dbline 43
;
; SPI_SrByte(0x95);
ldi R16,149
xcall _SPI_SrByte
.dbline 45
;
; SPI_SrByte(0xff);
ldi R16,255
xcall _SPI_SrByte
xjmp L7
L6:
.dbline 47
; while((r1 = SPI_SrByte(0xff)) == 0xff) /* wait response */
; if(retry++ > 0xfe) break; /* time out error */
mov R2,R22
clr R3
subi R22,255 ; addi 1
ldi R24,254
cp R24,R2
brsh L9
.dbline 47
xjmp L8
L9:
L7:
.dbline 46
ldi R16,255
xcall _SPI_SrByte
mov R20,R16
cpi R20,255
breq L6
L8:
.dbline 49
;
; SPI_CS_Deassert;
sbi 0x1b,1
.dbline 50
; SPI_SrByte(0xff); // extra 8 CLK
ldi R16,255
xcall _SPI_SrByte
.dbline 52
;
; return r1; /* return state */
mov R16,R20
.dbline -2
L2:
xcall pop_gset3
adiw R28,2
.dbline 0 ; func end
ret
.dbsym r retry 22 c
.dbsym r r1 20 c
.dbsym l arg 6 l
.dbsym r cmd 10 c
.dbend
.dbfunc e SD_Init _SD_Init fc
; retry -> R10
; r1 -> R12
; i -> R14
.even
_SD_Init::
xcall push_gset5
sbiw R28,2
.dbline -1
.dbline 62
; }
;
;
; //---------------------------------
; //
; // SD卡初始化函数
; //
; //---------------------------------
; uint8 SD_Init(void)
; {
.dbline 65
;
; uint8 i;
; uint8 retry = 0;
clr R10
.dbline 66
; uint8 r1 = 0;
clr R12
.dbline 68
;
; SD_Port_Init();
xcall _SD_Port_Init
.dbline 70
;
; SPI_CS_Assert;
cbi 0x1b,1
.dbline 71
; delay_ms(1);
ldi R16,1
ldi R17,0
ldi R18,0
ldi R19,0
xcall _delay_ms
.dbline 72
; SPI_CS_Deassert;
sbi 0x1b,1
.dbline 73
; delay_ms(1);
ldi R16,1
ldi R17,0
ldi R18,0
ldi R19,0
xcall _delay_ms
.dbline 76
;
;
; SPI_Low();
xcall _SPI_Low
L12:
.dbline 79
;
; do
; {
.dbline 80
clr R14
xjmp L18
L15:
.dbline 80
ldi R16,255
xcall _SPI_SrByte
L16:
.dbline 80
inc R14
L18:
.dbline 80
; for(i=0;i<10;i++) SPI_SrByte(0xff);
mov R24,R14
cpi R24,10
brlo L15
.dbline 81
; r1 = SD_SendCommand(0, 0);//发idle命令 //send idle command
ldi R20,0
ldi R21,0
ldi R22,0
ldi R23,0
std y+0,R22
std y+1,R23
movw R18,R20
clr R16
xcall _SD_SendCommand
mov R12,R16
.dbline 82
; retry++;
inc R10
.dbline 83
; if(retry>0xfe) return 1;//超时退出 //time out
ldi R24,254
cp R24,R10
brsh L19
.dbline 83
ldi R16,1
xjmp L11
L19:
.dbline 84
L13:
.dbline 84
; } while(r1 != 0x01);
mov R24,R12
cpi R24,1
brne L12
.dbline 87
;
;
; retry = 0;
clr R10
L21:
.dbline 89
; do
; {
.dbline 90
; r1 = SD_SendCommand(1, 0);//发active命令 //send active command
ldi R20,0
ldi R21,0
ldi R22,0
ldi R23,0
std y+0,R22
std y+1,R23
movw R18,R20
ldi R16,1
xcall _SD_SendCommand
mov R12,R16
.dbline 91
; retry++;
inc R10
.dbline 92
; if(retry>0xfe) return 1;//超时退出 //time out
ldi R24,254
cp R24,R10
brsh L24
.dbline 92
ldi R16,1
xjmp L11
L24:
.dbline 93
L22:
.dbline 93
; } while(r1);
tst R12
brne L21
.dbline 95
;
; SPI_High(); /* Use High Speed SPI*/
xcall _SPI_High
.dbline 97
;
; r1 = SD_SendCommand(59, 0);//关crc //disable CRC
ldi R20,0
ldi R21,0
ldi R22,0
ldi R23,0
std y+0,R22
std y+1,R23
movw R18,R20
ldi R16,59
xcall _SD_SendCommand
.dbline 99
;
; r1 = SD_SendCommand(16, 512);//设扇区大小512 //set sector size to 512
ldi R20,0
ldi R21,2
ldi R22,0
ldi R23,0
std y+0,R22
std y+1,R23
movw R18,R20
ldi R16,16
xcall _SD_SendCommand
mov R12,R16
.dbline 100
; return 0;//正常返回 //normal return
clr R16
.dbline -2
L11:
adiw R28,2
xcall pop_gset5
.dbline 0 ; func end
ret
.dbsym r retry 10 c
.dbsym r r1 12 c
.dbsym r i 14 c
.dbend
.dbfunc e SD_ReadBlock _SD_ReadBlock fc
; r1 -> R10
; retry -> R22,R23
; i -> R10,R11
; buffer -> R20,R21
; sector -> y+8
.even
_SD_ReadBlock::
xcall push_arg4
xcall push_gset3
sbiw R28,2
ldd R20,y+12
ldd R21,y+13
.dbline -1
.dbline 113
; }
;
;
;
; //---------------------------------------------
; //
; // SD卡扇区读取函数
; // sector:扇区号
; // buffer:数据缓存
; //
; //---------------------------------------------
; uint8 SD_ReadBlock(uint32 sector, uint8* buffer)
; {
.dbline 116
; uint8 r1;
; uint16 i;
; uint16 retry=0;
clr R22
clr R23
.dbline 118
;
; SPI_High(); /* Use High Speed SPI*/
xcall _SPI_High
.dbline 120
;
; r1 = SD_SendCommand(17, sector<<9);//读命令 //read command
ldi R24,9
ldi R25,0
movw R30,R28
ldd R2,z+8
ldd R3,z+9
ldd R4,z+10
ldd R5,z+11
st -y,R24
movw R16,R2
movw R18,R4
xcall lsl32
std y+0,R18
std y+1,R19
movw R18,R16
ldi R16,17
xcall _SD_SendCommand
mov R10,R16
.dbline 122
;
; if(r1 != 0x00)
tst R16
breq L27
.dbline 123
; return r1;
xjmp L26
L27:
.dbline 125
;
; SPI_CS_Assert;
cbi 0x1b,1
xjmp L30
L29:
.dbline 128
; //等数据的开始 //wait to start recieve data
; while(SPI_SrByte(0xff) != 0xfe)
; if(retry++ > 0xfffe)
movw R2,R22
subi R22,255 ; offset = 1
sbci R23,255
ldi R24,65534
ldi R25,255
cp R24,R2
cpc R25,R3
brsh L32
.dbline 129
; {SPI_CS_Deassert;return 1;}
.dbline 129
sbi 0x1b,1
.dbline 129
ldi R16,1
xjmp L26
L32:
L30:
.dbline 127
ldi R16,255
xcall _SPI_SrByte
cpi R16,254
brne L29
.dbline 131
;
; for(i=0; i<512; i++)//读512个数据 //read 512 bytes
clr R10
clr R11
xjmp L37
L34:
.dbline 132
.dbline 133
ldi R16,255
xcall _SPI_SrByte
movw R30,R20
st Z+,R16
movw R20,R30
.dbline 134
L35:
.dbline 131
movw R24,R10
adiw R24,1
movw R10,R24
L37:
.dbline 131
movw R24,R10
cpi R24,0
ldi R30,2
cpc R25,R30
brlo L34
.dbline 136
; {
; *buffer++ = SPI_SrByte(0xff);
; }
;
; SPI_SrByte(0xff);//伪crc //dummy crc
ldi R16,255
xcall _SPI_SrByte
.dbline 137
; SPI_SrByte(0xff);
ldi R16,255
xcall _SPI_SrByte
.dbline 139
;
; SPI_CS_Deassert;
sbi 0x1b,1
.dbline 140
; SPI_SrByte(0xff);// extra 8 CLK
ldi R16,255
xcall _SPI_SrByte
.dbline 143
;
;
; return 0;
clr R16
.dbline -2
L26:
adiw R28,2
xcall pop_gset3
adiw R28,4
.dbline 0 ; func end
ret
.dbsym r r1 10 c
.dbsym r retry 22 i
.dbsym r i 10 i
.dbsym r buffer 20 pc
.dbsym l sector 8 l
.dbend
.dbfunc e SD_WriteSingleBlock _SD_WriteSingleBlock fc
; r1 -> R20
; retry -> R22,R23
; i -> R10,R11
; buffer -> R12,R13
; sector -> y+10
.even
_SD_WriteSingleBlock::
xcall push_arg4
xcall push_gset4
sbiw R28,2
ldd R12,y+14
ldd R13,y+15
.dbline -1
.dbline 155
; }
;
;
; //---------------------------------------------
; //
; // SD卡扇区写入函数
; // sector:扇区号
; // buffer:数据缓存
; //
; //---------------------------------------------
; uint8 SD_WriteSingleBlock(uint32 sector, uint8* buffer)
; {
.dbline 158
; uint8 r1;
; uint16 i;
; uint16 retry=0;
clr R22
clr R23
.dbline 162
;
; //SPI_High(); /* Use High Speed SPI*/
;
; r1 = SD_SendCommand(24, sector<<9);//写命令 //send command
ldi R24,9
ldi R25,0
movw R30,R28
ldd R2,z+10
ldd R3,z+11
ldd R4,z+12
ldd R5,z+13
st -y,R24
movw R16,R2
movw R18,R4
xcall lsl32
std y+0,R18
std y+1,R19
movw R18,R16
ldi R16,24
xcall _SD_SendCommand
mov R20,R16
.dbline 163
; if(r1 != 0x00)
tst R16
breq L39
.dbline 164
; return r1;
xjmp L38
L39:
.dbline 166
;
; SPI_CS_Assert;
cbi 0x1b,1
.dbline 168
;
; SPI_SrByte(0xff);
ldi R16,255
xcall _SPI_SrByte
.dbline 169
; SPI_SrByte(0xff);
ldi R16,255
xcall _SPI_SrByte
.dbline 170
; SPI_SrByte(0xff);
ldi R16,255
xcall _SPI_SrByte
.dbline 172
;
; SPI_SrByte(0xfe);//发开始符 //send start byte "token"
ldi R16,254
xcall _SPI_SrByte
.dbline 174
;
; for(i=0; i<512; i++)//送512字节数据 //send 512 bytes data
clr R10
clr R11
xjmp L44
L41:
.dbline 175
.dbline 176
movw R30,R10
add R30,R12
adc R31,R13
ldd R16,z+0
xcall _SPI_SrByte
.dbline 177
L42:
.dbline 174
movw R24,R10
adiw R24,1
movw R10,R24
L44:
.dbline 174
movw R24,R10
cpi R24,0
ldi R30,2
cpc R25,R30
brlo L41
.dbline 179
; {
; SPI_SrByte(buffer[i]);
; }
;
; SPI_SrByte(0xff); //dummy crc
ldi R16,255
xcall _SPI_SrByte
.dbline 180
; SPI_SrByte(0xff);
ldi R16,255
xcall _SPI_SrByte
.dbline 182
;
; r1 = SPI_SrByte(0xff);
ldi R16,255
xcall _SPI_SrByte
mov R20,R16
.dbline 184
;
; if( (r1&0x1f) != 0x05)//等待是否成功 //judge if it successful
mov R24,R20
andi R24,31
cpi R24,5
breq L48
.dbline 185
; {
.dbline 186
; SPI_CS_Deassert;
sbi 0x1b,1
.dbline 187
; return r1;
xjmp L38
L47:
.dbline 190
; }
; //等待操作完 //wait no busy
; while(!SPI_SrByte(0xff))if(retry++ > 0xfffe){SPI_CS_Deassert;return 1;}
movw R2,R22
subi R22,255 ; offset = 1
sbci R23,255
ldi R24,65534
ldi R25,255
cp R24,R2
cpc R25,R3
brsh L50
.dbline 190
.dbline 190
sbi 0x1b,1
.dbline 190
ldi R16,1
xjmp L38
L50:
L48:
.dbline 190
ldi R16,255
xcall _SPI_SrByte
tst R16
breq L47
.dbline 192
;
; SPI_CS_Deassert;
sbi 0x1b,1
.dbline 195
; //SPI_SrByte(0xff);// extra 8 CLK
;
; return 0;
clr R16
.dbline -2
L38:
adiw R28,2
xcall pop_gset4
adiw R28,4
.dbline 0 ; func end
ret
.dbsym r r1 20 c
.dbsym r retry 22 i
.dbsym r i 10 i
.dbsym r buffer 12 pc
.dbsym l sector 10 l
.dbend
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -