📄 sddriver.s
字号:
.module sddriver.c
.area lit(rom, con, rel)
_time_unit::
.word 51712,15258
.word 57600,1525
.word 38528,152
.word 16960,15
.word 34464,1
.word 10000,0
.word 1000,0
.word 100,0
.dbfile D:\new_sd\SD\SD\sddriver.c
.dbsym e time_unit _time_unit A[32:8]kl
_time_value::
.byte 0,10
.byte 12,13
.byte 15,20
.byte 25,30
.byte 35,40
.byte 45,50
.byte 55,60
.byte 'F,'P
.dbsym e time_value _time_value A[16:16]kc
_r2w_fator::
.byte 1,2
.byte 4,8
.byte 16,32
.dbsym e r2w_fator _r2w_fator A[6:6]kc
.area text(rom, con, rel)
.dbfile D:\new_sd\SD\SD\sddriver.c
.dbfunc e SD_Initialize _SD_Initialize fc
; recbuf -> y+0
; ret -> R10
.even
_SD_Initialize::
xcall push_gset3
sbiw R28,4
.dbline -1
.dbline 35
; /*******************************************************************************************************
; ** Descriptions: sd 卡驱动软件包: SD卡物理层 用户API函数
; ********************************************************************************************************/
; #include "D:\new_sd\define.h"
;
;
; /* SD卡信息结构体变量 the information structure variable of SD Card */
; sd_struct sds;
;
; /* 超时时间单位表(单位:0.000000001ns) timeout unit table */
; const uint32 time_unit[8] = {1000000000,100000000,10000000,
; 1000000,100000,10000,1000,100};
;
; /* 超时时间表 timeout value table */
; const uint8 time_value[16] = { 0,10,12,13,15,20,25,30,
; 35,40,45,50,55,60,70,80};
;
; /* 超时时间因数表 timeout factor table */
; const uint8 r2w_fator[6] = {1,2,4,8,16,32};
;
; /********************************************************************************************
;
; 用户API函数: 初始化,读,写,擦 SD卡 User API Function: Initialize,read,write,erase SD Card
;
; ********************************************************************************************/
;
;
; /*******************************************************************************************************************
; ** 函数名称: INT8U SD_Initialize() Name: INT8U SD_Initialize()
; ** 功能描述: 初始化SD卡 Function: initialize sd card
; ** 输 入: 无 Input: NULL
; ** 输 出: 0: 正确 >0: 错误码 Output: 0: right >0: error code
; ********************************************************************************************************************/
; uint8 SD_Initialize(void)
; {
.dbline 38
; uint8 recbuf[4],ret;
;
; SD_HardWareInit(); /* 初始化读写SD卡的硬件条件 Initialize the hardware that access SD Card */
xcall _SD_HardWareInit
.dbline 40
;
; SPI_CS_Assert(); /* 1. 置CS为低 assert CS */
xcall _SPI_CS_Assert
.dbline 41
; SD_SPIDelay(30); /* 2. 至少延时 74 clock delay more than 74 clock */
ldi R16,30
xcall _SD_SPIDelay
.dbline 42
; SPI_CS_Deassert(); /* 3. 置CS为高 dessert CS */
xcall _SPI_CS_Deassert
.dbline 43
; SD_SPIDelay(3); /* 4. 延时2(8 clock) delay 2(8 clock) */
ldi R16,3
xcall _SD_SPIDelay
.dbline 45
;
; ret = SD_ResetSD(); /* 5. 发出CMDO命令复位SD卡 send CMD0 command to reset sd card */
xcall _SD_ResetSD
mov R10,R16
.dbline 46
; if (ret != SD_NO_ERR)
tst R16
breq L2
.dbline 47
; return ret;
xjmp L1
L2:
.dbline 49
;
; ret = SD_ActiveInit(); /* 6. 激活卡进入初始化过程. active card initialize process */
xcall _SD_ActiveInit
mov R10,R16
.dbline 50
; if (ret != SD_NO_ERR)
tst R16
breq L4
.dbline 51
; return ret;
xjmp L1
L4:
.dbline 53
;
; ret = SD_ReadOCR(4, recbuf); /* 7. 读OCR寄存器,查询卡支持的电压值 read OCR register,get the supported voltage */
movw R18,R28
ldi R16,4
xcall _SD_ReadOCR
mov R10,R16
.dbline 54
; if (ret != SD_NO_ERR)
tst R16
breq L6
.dbline 55
; return ret;
xjmp L1
L6:
.dbline 57
;
; if ((recbuf[1] & MSK_OCR_33) != MSK_OCR_33)
ldd R24,y+1
andi R24,192
cpi R24,192
breq L8
.dbline 58
; return SD_ERR_VOL_NOTSUSP; /* 不支持3.3V,返回错误码 not support 3.3V,return error code */
ldi R16,4
xjmp L1
L8:
.dbline 60
;
; SPI_ClkToMax(); /* 8. 设置SPI时钟到最大值 set SPI clock to maximum */
xcall _SPI_ClkToMax
.dbline 63
;
; #if SD_CRC_EN
; ret = SD_EnableCRC(1); /* 使能CRC校验 enable CRC check */
ldi R16,1
xcall _SD_EnableCRC
mov R10,R16
.dbline 64
; if (ret != SD_NO_ERR)
tst R16
breq L11
.dbline 65
; return ret;
xjmp L1
L11:
.dbline 68
; #endif
;
; ret = SD_SetBlockLen(SD_BLOCKSIZE); /* 9. 设置块的长度: 512Bytes Set the block length: 512Bytes */
ldi R16,0
ldi R17,2
ldi R18,0
ldi R19,0
xcall _SD_SetBlockLen
mov R10,R16
.dbline 69
; if (ret != SD_NO_ERR)
tst R16
breq L13
.dbline 70
; return ret;
xjmp L1
L13:
.dbline 72
;
; return (SD_GetCardInfo()); /* 10. 读CSD寄存器,获取SD卡信息 read CSD register, get the information of SD card */
xcall _SD_GetCardInfo
.dbline -2
L1:
adiw R28,4
xcall pop_gset3
.dbline 0 ; func end
ret
.dbsym l recbuf 0 A[4:4]c
.dbsym r ret 10 c
.dbend
.dbfunc e SD_GetCardInfo _SD_GetCardInfo fc
; tmp -> y+16
; ret -> R20
; csdbuf -> y+0
.even
_SD_GetCardInfo::
xcall push_gset1
sbiw R28,20
.dbline -1
.dbline 82
; }
;
; /*******************************************************************************************************************
; ** 函数名称: INT8U SD_GetCardInfo() Name: INT8U SD_GetCardInfo()
; ** 功能描述: 获得SD卡的信息 Function: get the information of SD card
; ** 输 入: INT8U cardtype: 卡类型 Input: INT8U cardtype: card type
; ** 输 出: 0: 正确 >0: 错误码 Output: 0: right >0: error code
; *******************************************************************************************************************/
; uint8 SD_GetCardInfo()
; {
.dbline 86
; uint32 tmp;
; uint8 csdbuf[16],ret;
;
; ret = SD_ReadCSD(16,csdbuf); /* 读CSD寄存器 read CSD register */
movw R18,R28
ldi R16,16
xcall _SD_ReadCSD
mov R20,R16
.dbline 87
; if (ret != SD_NO_ERR)
tst R16
breq L16
.dbline 88
; return ret;
xjmp L15
L16:
.dbline 90
;
; SD_CalTimeout(csdbuf); /* 计算超时时间值 calculate timeout value */
movw R16,R28
xcall _SD_CalTimeout
.dbline 93
;
; /* 计算块的最大长度 */ /* calculate the size of a sector */
; sds.block_len = 1 << (csdbuf[READ_BL_LEN_POS] & READ_BL_LEN_MSK); /* (2 ^ READ_BL_LEN) */
ldd R17,y+5
andi R17,15
ldi R16,1
xcall lsl8
sts _sds+1,R16
.dbline 96
;
; /* 计算卡中块的个数 */ /* calculate the sector numbers of the SD Card */
; sds.block_num = ((csdbuf[C_SIZE_POS1] & C_SIZE_MSK1) << 10) +
ldi R17,10
ldd R16,y+6
andi R16,3
xcall lsl8
mov R2,R16
ldd R3,y+7
lsl R3
lsl R3
add R2,R3
ldd R24,y+8
andi R24,192
lsr R24
lsr R24
lsr R24
lsr R24
lsr R24
lsr R24
add R2,R24
mov R24,R2
subi R24,255 ; addi 1
sts _sds,R24
.dbline 100
; (csdbuf[C_SIZE_POS2] << 2) +
; ((csdbuf[C_SIZE_POS3] & C_SIZE_MSK3) >> 6) + 1; /* (C_SIZE + 1)*/
;
; tmp = ((csdbuf[C_SIZE_MULT_POS1] & C_SIZE_MULT_MSK1) << 1) +
ldi R18,7
ldi R19,0
ldd R16,y+10
clr R17
andi R16,128
andi R17,0
xcall asr16
ldd R24,y+9
clr R25
andi R24,3
andi R25,0
lsl R24
rol R25
add R24,R16
adc R25,R17
adiw R24,2
movw R2,R24
clr R4
sbrc R3,7
com R4
clr R5
sbrc R4,7
com R5
movw R30,R28
std z+16,R2
std z+17,R3
std z+18,R4
std z+19,R5
.dbline 104
; ((csdbuf[C_SIZE_MULT_POS2] & C_SIZE_MULT_MSK2) >> 7) + 2; /* (C_SIZE_MULT + 2) */
;
; /* 获得卡中块的数量 */ /* get the block numbers in card */
; sds.block_num = sds.block_num * (1 << tmp); /* (C_SIZE + 1) * 2 ^ (C_SIZE_MULT + 2) */
movw R30,R28
ldd R18,z+16
ldd R19,z+17
ldi R16,1
ldi R17,0
xcall lsl16
movw R18,R16
lds R16,_sds
clr R17
xcall empy16s
sts _sds,R16
.dbline 107
;
; /* 计算扇区大小 */ /*calculate the size of sector */
; sds.erase_unit = ((csdbuf[SECTOR_SIZE_POS1] & SECTOR_SIZE_MSK1) << 1) +
ldd R24,y+11
andi R24,128
lsr R24
lsr R24
lsr R24
lsr R24
lsr R24
lsr R24
lsr R24
ldd R25,y+10
andi R25,63
lsl R25
add R25,R24
subi R25,255 ; addi 1
sts _sds+2,R25
.dbline 110
; ((csdbuf[SECTOR_SIZE_POS2] & SECTOR_SIZE_MSK2) >> 7) + 1; /* SD (SECTOR_SIZE + 1) */
;
; return SD_NO_ERR; /* 返回执行成功 return perform sucessfully */
clr R16
.dbline -2
L15:
adiw R28,20
xcall pop_gset1
.dbline 0 ; func end
ret
.dbsym l tmp 16 l
.dbsym r ret 20 c
.dbsym l csdbuf 0 A[16:16]c
.dbend
.dbfunc e SD_CalTimeout _SD_CalTimeout fV
; time_u -> R14
; fator -> R10
; time_v -> R12
; tmp -> y+0
; csdbuf -> y+14
.even
_SD_CalTimeout::
st -y,r17
st -y,r16
xcall push_gset5
sbiw R28,4
.dbline -1
.dbline 122
; }
;
;
; /*******************************************************************************************************************
; ** 函数名称: INT8U SD_CalTimeout() Name: INT8U SD_CalTimeout()
; ** 功能描述: 计算读/写/擦超时时间 Function: get the information of SD card
; ** 输 入: INT8U cardtype: 卡类型 Input: INT8U cardtype: card type
; INT8U *csdbuf : CSD寄存器内容 INT8U *csdbuf : CSD register content
; ** 输 出: 0: 正确 >0: 错误码 Output: 0: right >0: error code
; *******************************************************************************************************************/
; void SD_CalTimeout(uint8 *csdbuf)
; {
.dbline 126
; uint32 tmp;
; uint8 time_u,time_v,fator;
;
; sds.timeout_read = READ_TIMEOUT_100MS; /* 默认读超时为100ms */
ldi R20,0
ldi R21,14
ldi R22,1
ldi R23,0
movw R2,R20
sts _sds+3,R2
.dbline 127
; sds.timeout_write = WRITE_TIMEOUT_250MS; /* 默认写超时为250ms */
ldi R20,0
ldi R21,163
ldi R22,2
ldi R23,0
movw R2,R20
sts _sds+4,R2
.dbline 128
; sds.timeout_erase = WRITE_TIMEOUT_250MS;
ldi R20,0
ldi R21,163
ldi R22,2
ldi R23,0
movw R2,R20
sts _sds+5,R2
.dbline 130
;
; time_u = (csdbuf[TAAC_POS] & TAAC_MSK); /* 读超时时间单位 read timeout unit */
ldd R30,y+14
ldd R31,y+15
ldd R24,z+1
andi R24,7
mov R14,R24
.dbline 131
; time_v = (csdbuf[TAAC_POS] & NSAC_MSK) >> 3; /* 读超时时间值 read timeout value */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -