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

📄 test-sdcard.asm

📁 sd card input,output source program
💻 ASM
字号:
; ---------------------------------------------------------
;
; Functions for testing the SD Card
;
; ---------------------------------------------------------
; Author:					Manfred Langemann
; Begin of project:			19.02.2006
; Latest version generated:	22.02.2006
; Filename:					Test-SDCard.asm
; ---------------------------------------------------------
; 
; Functions implemented:
;	- Test function for SD card interfacing
;
; ---------------------------------------------------------
; SD Card Definitions
; A 128 Mbyte SD card has 250000 blocks of each 512 Byte
; The hex value of 250000 is 0x0003D090
;
; In tests it has been found, that only 245504 Blocks can be written
; Therefore we set the maximum block number to 245504 = 0x003BF00
; in order to be on the sure side
;
	.equ	Max_SD_Card_Blocks		= 245504
	.equ	Max_SD_Card_Blocks_High	= Max_SD_Card_Blocks / 65536
	.equ	Max_SD_Card_Blocks_Low	= Max_SD_Card_Blocks - Max_SD_Card_Blocks_High*65536 - 1
;
; ---------------------------------------------------------
;
Test_SDC:

	call	OFF_LED
;
; Wait 1 sec, secure power on delay
;
	call	dly15
;
; Init SD Card
;
Test_SDC_1:
	call	Init_SDCard
	cpi		temp1,1
	breq	Test_SDC_2
	jmp		Test_SDC_Error_0
;
; Write 245.504 block a 512 bytes to SD card
;
Test_SDC_2:
;
; Set SD card index buffer to 0
;
	call	Clear_SD_Index_Buffer
;
; Set byte start value to be written to SD card
;
	ldi		temp3, 0xAA
;
;------------------------------------------------------------------
; Now we write to the SD Card Max_SD_Card_Blocks blocks of each 512 bytes
;------------------------------------------------------------------
;
; Fill SD buffer with incremental start value
;
Test_SDC_3:
	call	Toggle_LED_0
	mov		temp1, temp3
	call	Fill_Inc_SDCard_Buffer
;
; Get the SD card index value from index buffer
;
	ldi		ZH, HIGH(SDCard_Block_Index)
	ldi		ZL, LOW (SDCard_Block_Index)
	ld		YH, Z+
	ld		YL, Z+
	ld		XH, Z+
	ld		XL, Z
;
; Write the content of the SD card buffer to the SD card with
; the block index in YH:YL - XH:XL (YH = MSB, XL = LSB))
;
	call	Write_Block_SDCard
	cpi		temp1, 0
	breq	Test_SDC_3a
	jmp		Test_SDC_Error_1
;
; Increment the SD card index buffer content
;
Test_SDC_3a:
	call	Inc_SD_Index_Buffer
;
; Increment byte value to be written to next block
;
	inc		temp3
;
; How many blocks have been written ?
; We stop here, when we have written Max_SD_Card_Blocks blocks
; Note: Max_SD_Card_Blocks shall be 250000
;
	ldi		ZH, HIGH(SDCard_Block_Index)
	ldi		ZL, LOW (SDCard_Block_Index)
	ld		YH, Z+
	ld		YL, Z+
	ld		XH, Z+
	ld		XL, Z
;
; We compare here the low 2 bytes
;
	ldi		ZH, HIGH(Max_SD_Card_Blocks_Low)
	ldi		ZL, LOW (Max_SD_Card_Blocks_Low)

	cp		XL, ZL
	brne	Test_SDC_3
	cp		XH, ZH
	brne	Test_SDC_3
;
; We compare here the high 2 bytes
;
	ldi		ZH, HIGH(Max_SD_Card_Blocks_High)
	ldi		ZL, LOW (Max_SD_Card_Blocks_High)

	cp		YL, ZL
	brne	Test_SDC_3
	cp		YH, ZH
	brne	Test_SDC_3
;
;------------------------------------------------------------------
; Now we read from the SD Card and compare the content
;------------------------------------------------------------------
;
; Set SD card index buffer to 0
;
	call	Clear_SD_Index_Buffer
;
; Set byte start value to compare with SD card content
;
	ldi		temp3, 0xAA
;
; Get the SD card index value from index buffer
;
Test_SDC_4:
	call	Toggle_LED_1
	ldi		ZH, HIGH(SDCard_Block_Index)
	ldi		ZL, LOW (SDCard_Block_Index)
	ld		YH, Z+
	ld		YL, Z+
	ld		XH, Z+
	ld		XL, Z
;
; Read the SD card block and write the content to the SD card buffer
; the block index in YH:YL - XH:XL (YH = MSB, XL = LSB))
;
	call	Read_Block_SDCard
	cpi		temp1, 0
	breq	Test_SDC_4a
	jmp		Test_SDC_Error_2
;
; Compare the buffer content with the start byte value
; When return value in temp1 is 0, then buffer content is correct
;
Test_SDC_4a:
	mov		temp1, temp3
	call	Compare_Inc_SDCard_Buffer
	cpi		temp1, 0
	breq	Test_SDC_4b
	jmp		Test_SDC_Error_3
;
; Increment the SD card index buffer content
;
Test_SDC_4b:
	call	Inc_SD_Index_Buffer
;
; Increment byte value to be written to next block
;
	inc		temp3
;
; How many blocks have been read ?
; We stop here, when we have read Max_SD_Card_Blocks blocks
; Note: Max_SD_Card_Blocks shall be 250000
;
	ldi		ZH, HIGH(SDCard_Block_Index)
	ldi		ZL, LOW (SDCard_Block_Index)
	ld		YH, Z+
	ld		YL, Z+
	ld		XH, Z+
	ld		XL, Z
;
; We compare here the low 2 bytes
;
	ldi		ZH, HIGH(Max_SD_Card_Blocks_Low)
	ldi		ZL, LOW (Max_SD_Card_Blocks_Low)

	cp		XL, ZL
	brne	Test_SDC_4
	cp		XH, ZH
	brne	Test_SDC_4
;
; We compare here the high 2 bytes
;
	ldi		ZH, HIGH(Max_SD_Card_Blocks_High)
	ldi		ZL, LOW (Max_SD_Card_Blocks_High)

	cp		YL, ZL
	brne	Test_SDC_4
	cp		YH, ZH
	brne	Test_SDC_4
;
;------------------------------------------------------------------
; Now we have made it, all data correctly written and read
; SD Card is OK
; Starting here and enless loop with LED blinking
;------------------------------------------------------------------
;
Test_SDC_5:
	call	dly15	; delay 1 sec
	call	OFF_LED
	call	dly15	; delay 1 sec
	call	ON_LED
	jmp		Test_SDC_5
;
;------------------------------------------------------------------
; Here are the error routines
;------------------------------------------------------------------
;
; SD Card Init failed
;
Test_SDC_Error_0:
	call	dly15	; delay 1 sec
	call	OFF_LED_0
	call	dly15	; delay 1 sec
	call	ON_LED_0
	rjmp	Test_SDC_Error_0
;
; Write block to SD Card failed
;
; Get the SD card index value from index buffer
;
Test_SDC_Error_1:
	call	dly15	; delay 1 sec
	call	OFF_LED_1
	call	dly15	; delay 1 sec
	call	ON_LED_1
	rjmp	Test_SDC_Error_1
;
; Read block from SD Card failed
;
Test_SDC_Error_2:
	call	dly15	; delay 1 sec
	call	OFF_LED_2
	call	dly15	; delay 1 sec
	call	ON_LED_2
	rjmp	Test_SDC_Error_2
;
; Read block content is not identical with the content written
;
Test_SDC_Error_3:
	call	dly15	; delay 1 sec
	call	OFF_LED_3
	call	dly15	; delay 1 sec
	call	ON_LED_3
	rjmp	Test_SDC_Error_3
;
;##########################################################
;
; Function: Fill SC Card Buffer with value in temp1
;			512 values to be written
;
;##########################################################
Fill_SDCard_Buffer:
;
	push	XL
	push	XH
	push	YL
	push	YH

	ldi		XH, HIGH(512)
	ldi		XL, LOW (512)

	ldi		YH, HIGH(SDCard_DataBuffer)
	ldi		YL, LOW (SDCard_DataBuffer)

Fill_SDCard_Buffer_1:
	st		Y+, temp1
	dec		XL
	brne	Fill_SDCard_Buffer_1
	dec		XH
	brne	Fill_SDCard_Buffer_1

	pop		YH
	pop		YL
	pop		XH
	pop		XL

	ret
;##########################################################
;
; Function: Fill SC Card Buffer with start value in temp1 by incremental
;			512 values to be written
;
;##########################################################
Fill_Inc_SDCard_Buffer:
;
	push	XL
	push	XH
	push	YL
	push	YH
;
	ldi		XH, HIGH(512)
	ldi		XL, LOW (512)

	ldi		YH, HIGH(SDCard_DataBuffer)
	ldi		YL, LOW (SDCard_DataBuffer)

Fill_Inc_SDCard_Buffer_1:
	st		Y+, temp1
	inc		temp1
	dec		XL
	brne	Fill_Inc_SDCard_Buffer_1
	dec		XH
	brne	Fill_Inc_SDCard_Buffer_1

	pop		YH
	pop		YL
	pop		XH
	pop		XL

	ret
;##########################################################
;
; Function: Compare SC Card Buffer with value in temp1
;			512 values to be compared
; If all values are equal to temp1, them return 0
; If one value is not temp1, then return 1
;
;##########################################################
Compare_SDCard_Buffer:
;
	push	XL
	push	XH
	push	YL
	push	YH
	push	temp2
;
	ldi		XH, HIGH(512)
	ldi		XL, LOW (512)

	ldi		ZH, HIGH(SDCard_DataBuffer)
	ldi		ZL, LOW (SDCard_DataBuffer)

Compare_SDCard_Buffer_1:
	ld		temp2, Z+
	cp		temp2, temp1
	brne	Compare_SDCard_Buffer_2
	dec		XL
	brne	Compare_SDCard_Buffer_1
	dec		XH
	brne	Compare_SDCard_Buffer_1
;
; Return 0
;
	ldi		temp1, 0
	rjmp	Compare_SDCard_Buffer_3
;
; Return 1
;
Compare_SDCard_Buffer_2:
	ldi		temp1, 1

Compare_SDCard_Buffer_3:
	pop		temp2
	pop		YH
	pop		YL
	pop		XH
	pop		XL

	ret
;##########################################################
;
; Function: Compare SC Card Buffer with start value in temp1 incremental
;			512 values to be compared
; If all values are equal to temp1-incremental, then return 0
; If one value is not temp1-incremental, then return 1
;
;##########################################################
Compare_Inc_SDCard_Buffer:
;
	push	XL
	push	XH
	push	YL
	push	YH
	push	temp2
;
	ldi		XH, HIGH(512)
	ldi		XL, LOW (512)

	ldi		ZH, HIGH(SDCard_DataBuffer)
	ldi		ZL, LOW (SDCard_DataBuffer)

Compare_Inc_SDCard_Buffer_1:
	ld		temp2, Z+
	cp		temp2, temp1
	brne	Compare_Inc_SDCard_Buffer_2
	inc		temp1
	dec		XL
	brne	Compare_Inc_SDCard_Buffer_1
	dec		XH
	brne	Compare_Inc_SDCard_Buffer_1
;
; Return 0
;
	ldi		temp1, 0
	rjmp	Compare_Inc_SDCard_Buffer_3
;
; Return 1
;
Compare_Inc_SDCard_Buffer_2:
	ldi		temp1, 1

Compare_Inc_SDCard_Buffer_3:
	pop		temp2
	pop		YH
	pop		YL
	pop		XH
	pop		XL

	ret
;##########################################################
;
; Function: Clear_SD_Index_Buffer Content, which consits of 4 bytes
; Sets SD card index buffer to 0
;
;##########################################################
Clear_SD_Index_Buffer:

	push	XL
	push	XH
	push	temp1

	ldi		XH, HIGH(SDCard_Block_Index)
	ldi		XL, LOW (SDCard_Block_Index)

	ldi		temp1, 0

	st		X+, temp1
	st		X+, temp1
	st		X+, temp1
	st		X , temp1
;
	pop		temp1
	pop		XH
	pop		XL

	ret
;##########################################################
;
; Function: Incement SD_Index_Buffer Content
; Increments the contents of the SD card Index buffer,
; which consits of 4 bytes
;
;##########################################################
Inc_SD_Index_Buffer:

	push	XL
	push	XH
	push	YL
	push	YH
	push	ZL
	push	ZH

	ldi		ZH, HIGH(SDCard_Block_Index)
	ldi		ZL, LOW (SDCard_Block_Index)

	ld		YH, Z+
	ld		YL, Z+
	ld		XH, Z+
	ld		XL, Z

	adiw	XH:XL, 1
	brcc	Inc_SD_Index_Buffer_1
	adiw	YH:YL, 1

Inc_SD_Index_Buffer_1:

	ldi		ZH, HIGH(SDCard_Block_Index)
	ldi		ZL, LOW (SDCard_Block_Index)

	st		Z+, YH
	st		Z+, YL
	st		Z+, XH
	st		Z, XL

	pop		ZH
	pop		ZL
	pop		YH
	pop		YL
	pop		XH
	pop		XL

	ret
;##########################################################
;
; Function: Delay function to let LED(s) blink with a
; frequency of ca. 1 sec
;
;##########################################################
;	

dly15:
	ldi		delay3, 32	; ca. 1 sec delay
;
delay:
	dec 	delay1
	brne	delay
	dec		delay2
	brne	delay
	dec		delay3
	brne	delay	
	ret	

⌨️ 快捷键说明

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