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

📄 mmc.s

📁 采用Mega16+VS1011B+SD卡制作的Mp3
💻 S
字号:
	.module mmc.c
	.area text(rom, con, rel)
	.dbfile D:\学习\AVRpro\Mega16程序\TestLCD\mmc.c
	.dbfunc e SPI_Low _SPI_Low fV
	.even
_SPI_Low::
	.dbline -1
	.dbline 7
; # include "iom16v.h"
; # include "macros.h"
; #include "mmc.h"
; 
; //低速模式  //spi low speed
; void SPI_Low(void)
; {
	.dbline 8
; 	SPCR =   BIT(SPE)|BIT(MSTR)|BIT(SPR1)|BIT(SPR0);  //SCK:XTAL-CLOCK/128
	ldi R24,83
	out 0xd,R24
	.dbline 9
; 	SPSR &= ~BIT(SPI2X);							  //不用倍速
	cbi 0xe,0
	.dbline -2
L1:
	.dbline 0 ; func end
	ret
	.dbend
	.dbfunc e SPI_High _SPI_High fV
	.even
_SPI_High::
	.dbline -1
	.dbline 14
; }
; 
; //高速模式	//spi full speed
; void SPI_High(void)
; {
	.dbline 15
; 	SPCR =  BIT(SPE)|BIT(MSTR);			//SCK:		XTAL-CLOCK/2,采用倍速  			
	ldi R24,80
	out 0xd,R24
	.dbline 16
; 	SPSR |= BIT(SPI2X);
	sbi 0xe,0
	.dbline -2
L2:
	.dbline 0 ; func end
	ret
	.dbend
	.dbfunc e SPI_Init _SPI_Init fV
	.even
_SPI_Init::
	.dbline -1
	.dbline 21
; }
; 
; //端口初始化,模式初始化  //port initialize
; void SPI_Init(void)
; {
	.dbline 22
; 	DDR_INI();
	in R24,0x17
	ori R24,176
	out 0x17,R24
	.dbline 23
; 	SPI_High();
	xcall _SPI_High
	.dbline -2
L3:
	.dbline 0 ; func end
	ret
	.dbend
	.dbfunc e SPI_WriteByte _SPI_WriteByte fc
;            val -> R16
	.even
_SPI_WriteByte::
	.dbline -1
	.dbline 28
; }
; 
; //写[读]一个字节			//read [and write] one byte
; uint8 SPI_WriteByte(register uint8 val)
; {
	.dbline 29
; 	SPDR = val;
	out 0xf,R16
L5:
	.dbline 30
; 	while(!(SPSR & BIT(SPIF)));	   //等待SPI发送完数据
L6:
	.dbline 30
	sbis 0xe,7
	rjmp L5
X0:
	.dbline 31
; 	return SPDR;
	in R16,0xf
	.dbline -2
L4:
	.dbline 0 ; func end
	ret
	.dbsym r val 16 c
	.dbend
	.dbfunc e MMC_SD_Detect _MMC_SD_Detect fc
	.even
_MMC_SD_Detect::
	.dbline -1
	.dbline 37
; }
; 
; 
; //检测SD卡是否插入,已插入,返回1,否则,返回0
; uint8 MMC_SD_Detect(void)
; {	 
	.dbline 38
;     PORTA |=BIT(MMC_SD_INSDETC);	//PORTA0为高电平
	sbi 0x1b,7
	.dbline 39
; 	DDRA&=(~BIT(MMC_SD_INSDETC));   //PA0设置为输入
	cbi 0x1a,7
	.dbline 41
; 	//;;//读取MMC_SD_INSDETC,为低电平,表示已经插入SD卡
; 	;
	.dbline 42
; 	if((PINA&=BIT(MMC_SD_INSDETC))==0)
	in R24,0x19
	andi R24,128
	out 0x19,R24
	tst R24
	brne L9
X1:
	.dbline 43
; 		return 1;
	ldi R16,1
	xjmp L8
L9:
	.dbline 45
; 	else
; 		return 0;
	clr R16
	.dbline -2
L8:
	.dbline 0 ; func end
	ret
	.dbend
	.dbfunc e MMC_SD_Init _MMC_SD_Init fV
	.even
_MMC_SD_Init::
	.dbline -1
	.dbline 50
; }
; 
; //sd卡初始化		//sd card initialize
; void MMC_SD_Init(void)
; {
	.dbline 51
; 	SPI_Init();
	xcall _SPI_Init
	.dbline 52
; 	SPI_CS_Deassert();;
	sbi 0x18,4
	.dbline 52
	.dbline -2
L11:
	.dbline 0 ; func end
	ret
	.dbend
	.dbfunc e MMC_SD_SendCommand _MMC_SD_SendCommand fc
;             r1 -> R10
;          retry -> R20
;            arg -> y+6
;            cmd -> R22
	.even
_MMC_SD_SendCommand::
	st -y,r19
	st -y,r18
	xcall push_xgsetF00C
	mov R22,R16
	.dbline -1
	.dbline 57
; }
; 
; //sd卡写命令		//sd send command
; uint8 MMC_SD_SendCommand(uint8 cmd, uint32 arg)
; {
	.dbline 59
; 	uint8 r1;
; 	register uint8 retry=0;
	clr R20
	.dbline 61
; 	
; 	SPI_WriteByte(0xff);
	ldi R16,255
	xcall _SPI_WriteByte
	.dbline 62
; 	SPI_CS_Assert();
	cbi 0x18,4
	.dbline 64
; 	
; 	SPI_WriteByte(cmd | 0x40);//分别写入命令	//send command
	mov R16,R22
	ori R16,64
	xcall _SPI_WriteByte
	.dbline 65
; 	SPI_WriteByte(arg>>24);
	ldi R24,24
	ldi R25,0
	ldd R16,y+6
	ldd R17,y+7
	ldd R18,y+8
	ldd R19,y+9
	st -y,R24
	xcall lsr32
	xcall _SPI_WriteByte
	.dbline 66
; 	SPI_WriteByte(arg>>16);
	ldd R2,y+6
	ldd R3,y+7
	ldd R4,y+8
	ldd R5,y+9
	movw R2,R4
	clr R4
	clr R5
	mov R16,R2
	xcall _SPI_WriteByte
	.dbline 67
; 	SPI_WriteByte(arg>>8);
	ldi R24,8
	ldi R25,0
	ldd R16,y+6
	ldd R17,y+7
	ldd R18,y+8
	ldd R19,y+9
	st -y,R24
	xcall lsr32
	xcall _SPI_WriteByte
	.dbline 68
; 	SPI_WriteByte(arg);
	ldd R16,y+6
	xcall _SPI_WriteByte
	.dbline 69
; 	SPI_WriteByte(0x95);
	ldi R16,149
	xcall _SPI_WriteByte
	xjmp L14
L13:
	.dbline 72
; 	
; 	while((r1 = SPI_WriteByte(0xff)) == 0xff)//等待响应,	//wait response
; 		if(retry++ > 20) break;//超时退出					//time out error
	mov R2,R20
	clr R3
	subi R20,255    ; addi 1
	ldi R24,20
	cp R24,R2
	brsh L16
X2:
	.dbline 72
	xjmp L15
L16:
L14:
	.dbline 71
	ldi R16,255
	xcall _SPI_WriteByte
	mov R10,R16
	cpi R16,255
	breq L13
X3:
L15:
	.dbline 74
; 
; 	SPI_CS_Deassert();
	sbi 0x18,4
	.dbline 76
; 
; 	return r1;//返回状态值					//return state
	mov R16,R10
	.dbline -2
L12:
	.dbline 0 ; func end
	xcall pop_xgsetF00C
	adiw R28,2
	ret
	.dbsym r r1 10 c
	.dbsym r retry 20 c
	.dbsym l arg 6 l
	.dbsym r cmd 22 c
	.dbend
	.dbfunc e MMC_SD_Reset _MMC_SD_Reset fc
;             r1 -> R20
;              i -> R22
;          retry -> R10
	.even
_MMC_SD_Reset::
	xcall push_xgsetF00C
	sbiw R28,2
	.dbline -1
	.dbline 81
; }
; 
; //sd卡复位,正常时,返回0
; uint8 MMC_SD_Reset(void)
; {
	.dbline 83
; 	uint8 i;
; 	register uint8 retry=0;
	clr R10
	.dbline 84
; 	uint8 r1=0;
	clr R20
	.dbline 85
; 	SPI_Low();
	xcall _SPI_Low
L19:
	.dbline 87
; 	do
; 	{
	.dbline 88
; 		for(i=0;i<10;i++) SPI_WriteByte(0xff);
	clr R22
	xjmp L25
L22:
	.dbline 88
	ldi R16,255
	xcall _SPI_WriteByte
L23:
	.dbline 88
	inc R22
L25:
	.dbline 88
	cpi R22,10
	brlo L22
X4:
	.dbline 89
; 		r1 = MMC_SD_SendCommand(0, 0);//发idle命令	//send idle command
	ldi R24,0
	ldi R25,0
	ldi R26,0
	ldi R27,0
	std y+0,R26
	std y+1,R27
	movw R18,R24
	clr R16
	xcall _MMC_SD_SendCommand
	mov R20,R16
	.dbline 90
; 		retry++;
	inc R10
	.dbline 91
; 		if(retry>10) return 1;//超时退出		//time out
	ldi R24,10
	cp R24,R10
	brsh L26
X5:
	.dbline 91
	ldi R16,1
	xjmp L18
L26:
	.dbline 92
; 	} while(r1 != 0x01);	
L20:
	.dbline 92
	cpi R20,1
	brne L19
X6:
	.dbline 95
; 
; 
; 	retry = 0;
	clr R10
L28:
	.dbline 97
; 	do
; 	{
	.dbline 98
; 		r1 = MMC_SD_SendCommand(1, 0);//发active命令	//send active command
	ldi R24,0
	ldi R25,0
	ldi R26,0
	ldi R27,0
	std y+0,R26
	std y+1,R27
	movw R18,R24
	ldi R16,1
	xcall _MMC_SD_SendCommand
	mov R20,R16
	.dbline 99
; 		retry++;
	inc R10
	.dbline 100
; 		if(retry>100) return 1;//超时退出		//time out
	ldi R24,100
	cp R24,R10
	brsh L31
X7:
	.dbline 100
	ldi R16,1
	xjmp L18
L31:
	.dbline 101
; 	} while(r1);
L29:
	.dbline 101
	tst R20
	brne L28
X8:
	.dbline 102
; 	SPI_High();
	xcall _SPI_High
	.dbline 103
; 	r1 = MMC_SD_SendCommand(59, 0);//关crc		//disable CRC
	ldi R24,0
	ldi R25,0
	ldi R26,0
	ldi R27,0
	std y+0,R26
	std y+1,R27
	movw R18,R24
	ldi R16,59
	xcall _MMC_SD_SendCommand
	.dbline 105
; 
; 	r1 = MMC_SD_SendCommand(16, 512);//设扇区大小512	//set sector size to 512
	ldi R24,0
	ldi R25,2
	ldi R26,0
	ldi R27,0
	std y+0,R26
	std y+1,R27
	movw R18,R24
	ldi R16,16
	xcall _MMC_SD_SendCommand
	mov R10,R16
	mov R20,R10
	.dbline 106
; 	return 0;//正常返回		//normal return
	clr R16
	.dbline -2
L18:
	.dbline 0 ; func end
	adiw R28,2
	xjmp pop_xgsetF00C
	.dbsym r r1 20 c
	.dbsym r i 22 c
	.dbsym r retry 10 c
	.dbend
	.dbfunc e MMC_SD_ReadSingleBlock _MMC_SD_ReadSingleBlock fc
;             r1 -> R12
;          retry -> R20,R21
;              i -> R20,R21
;         buffer -> R10,R11
;         sector -> y+10
	.even
_MMC_SD_ReadSingleBlock::
	xcall push_arg4
	xcall push_xgsetF03C
	sbiw R28,2
	ldd R10,y+14
	ldd R11,y+15
	.dbline -1
	.dbline 111
; }
; 
; //读一个扇区,成功,返回0
; uint8 MMC_SD_ReadSingleBlock(uint32 sector, uint8* buffer)
; {
	.dbline 114
; 	uint8 r1;
; 	register uint16 i;
; 	register uint16 retry=0;
	clr R20
	clr R21
	.dbline 116
; 
; 	r1 = MMC_SD_SendCommand(17, sector<<9);//读命令	//read command
	ldi R24,9
	ldi R25,0
	ldd R16,y+10
	ldd R17,y+11
	ldd R18,y+12
	ldd R19,y+13
	st -y,R24
	xcall lsl32
	std y+0,R18
	std y+1,R19
	movw R18,R16
	ldi R16,17
	xcall _MMC_SD_SendCommand
	mov R12,R16
	.dbline 118
; 	
; 	if(r1 != 0x00)
	tst R16
	breq L34
X9:
	.dbline 119
; 		return r1;
	xjmp L33
L34:
	.dbline 121
; 
; 	SPI_CS_Assert();
	cbi 0x18,4
	xjmp L37
L36:
	.dbline 123
; 	//等数据的开始	
; 	while(SPI_WriteByte(0xff) != 0xfe) if(retry++ > 1000){SPI_CS_Deassert(); return 1;}
	movw R2,R20
	subi R20,255  ; offset = 1
	sbci R21,255
	ldi R24,1000
	ldi R25,3
	cp R24,R2
	cpc R25,R3
	brsh L39
X10:
	.dbline 123
	.dbline 123
	sbi 0x18,4
	.dbline 123
	ldi R16,1
	xjmp L33
L39:
L37:
	.dbline 123
	ldi R16,255
	xcall _SPI_WriteByte
	mov R22,R16
	cpi R16,254
	brne L36
X11:
	.dbline 125
; 
; 	for(i=0; i<512; i++)//读512个数据	//read 512 bytes
	clr R20
	clr R21
	xjmp L44
L41:
	.dbline 126
; 	{
	.dbline 127
; 		*buffer++ = SPI_WriteByte(0xff);
	ldi R16,255
	xcall _SPI_WriteByte
	mov R12,R16
	movw R30,R10
	st Z+,R12
	movw R10,R30
	.dbline 128
; 	}
L42:
	.dbline 125
	subi R20,255  ; offset = 1
	sbci R21,255
L44:
	.dbline 125
	cpi R20,0
	ldi R30,2
	cpc R21,R30
	brlo L41
X12:
	.dbline 130
; 
; 	SPI_WriteByte(0xff);//伪crc
	ldi R16,255
	xcall _SPI_WriteByte
	.dbline 131
; 	SPI_WriteByte(0xff);
	ldi R16,255
	xcall _SPI_WriteByte
	.dbline 133
; 	
; 	SPI_CS_Deassert();
	sbi 0x18,4
	.dbline 135
; 
; 	return 0;
	clr R16
	.dbline -2
L33:
	.dbline 0 ; func end
	adiw R28,2
	xcall pop_xgsetF03C
	adiw R28,4
	ret
	.dbsym r r1 12 c
	.dbsym r retry 20 i
	.dbsym r i 20 i
	.dbsym r buffer 10 pc
	.dbsym l sector 10 l
	.dbend
; }
; 
; /*
; //写一个扇区		//wirite one sector //not used in this application
; uint8 MMC_SD_WriteSingleBlock(uint32 sector, uint8* buffer)
; {
; 	uint8 r1;
; 	uint16 i;
; 	uint16 retry=0;
; 
; 	r1 = MMC_SD_SendCommand(24, sector<<9);//写命令	//send command
; 	if(r1 != 0x00)
; 		return r1;
; 
; 	SPI_CS_Assert();
; 	
; 	SPI_WriteByte(0xff);
; 	SPI_WriteByte(0xff);
; 	SPI_WriteByte(0xff);
; 
; 	SPI_WriteByte(0xfe);//发开始符			//send start byte
; 	
; 	for(i=0; i<512; i++)//送512字节数据		//send 512 bytes data
; 	{
; 		SPI_WriteByte(*buffer++);
; 	}
; 	
; 	SPI_WriteByte(0xff);
; 	SPI_WriteByte(0xff);
; 	
; 	r1 = SPI_WriteByte(0xff);
; 	
; 	if( (r1&0x1f) != 0x05)//等待是否成功	//judge if it successful
; 	{
; 		SPI_CS_Deassert();
; 		return r1;
; 	}
; 	//等待操作完		//wait no busy
; 	while(!SPI_WriteByte(0xff))if(retry++ > 2000){SPI_CS_Deassert();return 1;}
; 
; 	SPI_CS_Deassert();
; 
; 	return 0;
; }
; 
; uint32 MMC_SD_ReadCapacity(void)
; {
; 	uint8 r1;
; 	uint16 i;
; 	uint16 temp;
; 	uint8 buffer[16];
; 	uint32 Capacity;
; 	//uint8 retry=0;
; 
; 	r1 = MMC_SD_SendCommand(9, 0);//写命令	//send command  //READ CSD
; 	if(r1 != 0x00)
; 		return r1;
; 
; 	SPI_CS_Assert();
; 	while(SPI_WriteByte(0xff) != 0xfe);
; 	
; 	for(i=0;i<16;i++)
; 	{
; 		buffer[i]=SPI_WriteByte(0xff);
; 	}	
; 
; 	SPI_WriteByte(0xff);
; 	SPI_WriteByte(0xff);
; 	
; 	SPI_WriteByte(0xff);
; 	
; 	SPI_CS_Deassert();
; 
; //////////////////////////////////////
; //	C_SIZE
; 	i = buffer[6]&0x03;
; 	i<<=8;
; 	i += buffer[7];
; 	i<<=2;
; 	i += ((buffer[8]&0xc0)>>6);
; 
; ///////////////////////////////////////
; //  C_SIZE_MULT
; 
; 	r1 = buffer[9]&0x03;
; 	r1<<=1;
; 	r1 += ((buffer[10]&0x80)>>7);
; 
; 
; ///////////////////////////////////////
; // BLOCKNR
; 
; 	r1+=2;
; 
; 	temp = 1;
; 	while(r1)
; 	{
; 		temp*=2;
; 		r1--;
; 	}
; 	
; 	Capacity = ((uint32)(i+1))*((uint32)temp);
; 
; /////////////////////////
; // READ_BL_LEN
; 
; 	i = buffer[5]&0x0f;
; 
; /////////////////////////
; //BLOCK_LEN
; 
; 	temp = 1;
; 	while(i)
; 	{
; 		temp*=2;
; 		i--;
; 	}
; /////////////////////////
; 
; 
; ////////////////////////////////////////////////////////////
; //
; //  memory capacity = BLOCKNR * BLOCK_LEN
; //	
; //	BLOCKNR = (C_SIZE + 1)* MULT
; //
; //           C_SIZE_MULT+2
; //	MULT = 2
; //
; //               READ_BL_LEN
; //	BLOCK_LEN = 2
; ////////////////////////////////////////////////////////////
; //The final result
; 
; 	Capacity *= (uint32)temp;	 
; 	return Capacity;		
; }
; 
; */

⌨️ 快捷键说明

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