使用したSDCC はver 3.4.0です。
[crt.asm]
;.org #0x0000
call _main
ret
[sfr_def.h]
#ifndef _SFR_H_
#define _SFR_H_
__sfr __at 0x00 UART_ST;
__sfr __at 0x01 UART_TD;
__sfr __at 0x01 UART_RD;
__sfr __at 0x20 GPIO_SW;
__sfr __at 0x21 GPIO_LED;
__sfr __at 0x30 SPI1_CS;
__sfr __at 0x31 SPI1_BY;
__sfr __at 0x32 SPI1_TD;
__sfr __at 0x33 SPI1_RD;
__sfr __at 0x34 SPI1_DV;
#endif
[SD.c]
#include "sfr_def.h"
struct sd_def
{
unsigned char cmd[8];
unsigned char res[2];
unsigned char buf[512];
} sd;
void uart_print (const char *fmt, ...);
void SD_INIT (void);
void SD_READ (int sec);
int CRC7 (unsigned char c[], int n);
void
main ()
{
int led;
led = 0;
uart_print ("hello\r\n");
SD_INIT ();
SD_READ (0);
while (1)
{
led++;
uart_print ("%04x\r", led);
GPIO_LED = led;
}
}
int
SD_RW (int data)
{
SPI1_TD = data;
return SPI1_RD & 0xff;
}
void
SD_INIT (void)
{
int i;
SPI1_DV = 6;
SPI1_CS=1;
/* dummy clock */
for (i = 0; i < 10; i++)
{
SD_RW (0xff);
}
sd.cmd[0] = 0x40;
sd.cmd[1] = 0x00;
sd.cmd[2] = 0x00;
sd.cmd[3] = 0x00;
sd.cmd[4] = 0x00;
sd.cmd[5] = CRC7 (sd.cmd, 5) | 1;
SPI1_CS=0;
for (i = 0; i < 6; i++)
{
SD_RW (sd.cmd[i]);
}
do
{ /* response */
sd.res[0] = SD_RW (0xff);
} while (sd.res[0] == 0xff);
SPI1_CS=1;
sd.cmd[0] = 0x41;
sd.cmd[1] = 0x00;
sd.cmd[2] = 0x00;
sd.cmd[3] = 0x00;
sd.cmd[4] = 0x00;
sd.cmd[5] = CRC7 (sd.cmd, 5) | 1;
SPI1_CS=0;
for(;;){
for (i = 0; i < 6; i++)
{
SD_RW (sd.cmd[i]);
}
do
{ /* response */
sd.res[0] = SD_RW (0xff);
} while (sd.res[0] == 0xff);
sd.res[1] = SD_RW (0xff);
if(sd.res[0] ==0||sd.res[1] ==0)
break;
}
SPI1_CS=1;
}
void
SD_READ (int sec)
{
int i;
sd.cmd[0] = 0x51;
sd.cmd[1] = 0x00;
sd.cmd[2] = 0x00;
sd.cmd[3] = sec >> 8;
sd.cmd[4] = sec;
sd.cmd[5] = CRC7 (sd.cmd, 5) | 1;
SPI1_CS=0;
for (i = 0; i < 6; i++)
{
SD_RW (sd.cmd[i]);
}
do
{ /* response */
sd.res[0] = SD_RW (0xff);
}
while (sd.res[0] == 0xff);
do
{ /* start byte */
sd.res[1] = SD_RW (0xff);
}
while (sd.res[1] != 0xfe);
for (i = 0; i < 512; i++)
{
sd.buf[i] = SD_RW (0xff);
}
/* crc1 crc2 */
SD_RW (0xff);
SD_RW (0xff);
/* dummy clock */
SD_RW (0xff);
SPI1_CS=1;
for (i = 0; i < 512; i++)
{
if ((i & 15) == 0)
uart_print ("\n%03x", i);
uart_print (" %02x", sd.buf[i]);
}
uart_print ("\n");
}
void
SD_WRITE (int sec)
{
}
//------- CRC7 by K.I 051020 --------
int
CRC7 (unsigned char c[], int n)
{
int i, j;
unsigned char d, r = 0;
for (i = 0; i < n; i++)
{
d = c[i];
for (j = 0; j < 8; j++)
{
if ((d & 0x80) ^ (r & 0x80))
r = (r << 1) ^ 0x12; // reverse of 0x48
else
r <<= 1;
d <<= 1;
}
}
return (r & 0xFE);
}
[build.bat]
sdasz80 -plosgff -o crt.rel crt.asm
sdcc -mz80 -c print.c
sdcc -mz80 -c SD.c
sdcc -mz80 --code-loc 0x100 --no-std-crt0 -Wlcrt.rel SD.rel print.rel
cmd0
cmd17 コマンド受付
cmd17 データの開始 0xFE
cmd17 最終データ部 0xaa 0x55
セクタ#0
転送ツールとSDCard Read サンプル
https://www.dropbox.com/s/nm0vcrq0nfpjvrd/D2tool.zip?dl=0
https://www.dropbox.com/s/fpc7u1qoig1z1ug/sample_sdcard.zip?dl=0
0 件のコメント:
コメントを投稿