Get SD card working in SPI
This commit is contained in:
@@ -15,7 +15,7 @@ LISTS=lists
|
||||
TESTS=tests
|
||||
|
||||
SRCS=$(wildcard *.s) $(wildcard *.c)
|
||||
SRCS+=$(filter-out $(wildcard tests/*), $(wildcard **/*.s)) $(filter-out $(wildcard tests/*) $(wildcard filesystem/*), $(wildcard **/*.c))
|
||||
SRCS+=$(filter-out $(wildcard tests/*), $(wildcard **/*.s)) $(filter-out $(wildcard tests/*) $(wildcard filesystem/*) devices/sd_print.c, $(wildcard **/*.c))
|
||||
OBJS+=$(patsubst %.s,%.o,$(filter %s,$(SRCS)))
|
||||
OBJS+=$(patsubst %.c,%.o,$(filter %c,$(SRCS)))
|
||||
|
||||
|
||||
@@ -6,7 +6,8 @@ UART_STATUS = UART + 1
|
||||
LED = $efff
|
||||
SW = LED
|
||||
|
||||
SD_ARG = $efd8
|
||||
SD_CMD = $efdc
|
||||
SD_DATA = $efdd
|
||||
|
||||
SPI_BAUD = $efd8
|
||||
SPI_INPUT = $efd9
|
||||
SPI_OUTPUT = $efda
|
||||
SPI_CTRL = $efdb
|
||||
SPI_STATUS = SPI_CTRL
|
||||
@@ -1,89 +1,455 @@
|
||||
#include <stdint.h>
|
||||
#include <conio.h>
|
||||
|
||||
#include "devices/sd_card.h"
|
||||
#include "sd_card.h"
|
||||
#include "sd_print.h"
|
||||
#include "spi.h"
|
||||
|
||||
void sd_init() {
|
||||
uint32_t resp;
|
||||
uint8_t attempts, i;
|
||||
/*******************************************************************************
|
||||
Initialize SD card
|
||||
*******************************************************************************/
|
||||
uint8_t SD_init()
|
||||
{
|
||||
uint16_t i;
|
||||
|
||||
cputs("In sd_init\n");
|
||||
uint8_t res[5], cmdAttempts = 0;
|
||||
|
||||
sd_card_command(0, 0);
|
||||
SD_powerUpSeq();
|
||||
|
||||
cprintf("Sent Reset\n");
|
||||
while((res[0] = SD_goIdleState()) != SD_IN_IDLE_STATE)
|
||||
{
|
||||
cmdAttempts++;
|
||||
if(cmdAttempts == CMD0_MAX_ATTEMPTS)
|
||||
{
|
||||
cputs("Go IDLE\r\n");
|
||||
return SD_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
sd_card_command(0x000001aa, 8);
|
||||
sd_card_resp(&resp);
|
||||
cprintf("CMD8: %lx\n", resp);
|
||||
for (i = 0; i < 1000; i++);
|
||||
|
||||
attempts = 0;
|
||||
do {
|
||||
if (attempts > 100) {
|
||||
cprintf("SD Timed out");
|
||||
return;
|
||||
}
|
||||
sd_card_command(0, 55);
|
||||
sd_card_resp(&resp);
|
||||
sd_card_command(0x40180000, 41);
|
||||
sd_card_resp(&resp);
|
||||
cprintf("CMD41: %lx\n", resp);
|
||||
SD_sendIfCond(res);
|
||||
if(res[0] != SD_IN_IDLE_STATE)
|
||||
{
|
||||
cputs("IF Cond\r\n");
|
||||
return SD_ERROR;
|
||||
}
|
||||
|
||||
//10ms loop?
|
||||
for (i = 0; i < 255; i++);
|
||||
if(res[4] != 0xAA)
|
||||
{
|
||||
return SD_ERROR;
|
||||
}
|
||||
|
||||
attempts++;
|
||||
} while (resp != 0);
|
||||
cmdAttempts = 0;
|
||||
do
|
||||
{
|
||||
if(cmdAttempts == CMD55_MAX_ATTEMPTS)
|
||||
{
|
||||
cputs("op_cond error\r\n");
|
||||
return SD_ERROR;
|
||||
}
|
||||
|
||||
sd_card_command(0, 2);
|
||||
sd_card_resp(&resp);
|
||||
cprintf("CMD2: %lx\n", resp);
|
||||
res[0] = SD_sendApp();
|
||||
if(SD_R1_NO_ERROR(res[0]))
|
||||
{
|
||||
res[0] = SD_sendOpCond();
|
||||
}
|
||||
|
||||
for (i = 0; i < 1000; i++);
|
||||
|
||||
cmdAttempts++;
|
||||
}
|
||||
while(res[0] != SD_READY);
|
||||
|
||||
for (i = 0; i < 1000; i++);
|
||||
|
||||
SD_readOCR(res);
|
||||
|
||||
return SD_SUCCESS;
|
||||
}
|
||||
|
||||
uint16_t sd_get_rca() {
|
||||
uint32_t resp;
|
||||
/*******************************************************************************
|
||||
Run power up sequence
|
||||
*******************************************************************************/
|
||||
void SD_powerUpSeq()
|
||||
{
|
||||
uint16_t i;
|
||||
uint8_t j;
|
||||
|
||||
sd_card_command(0, 3);
|
||||
resp = 0;
|
||||
sd_card_resp(&resp);
|
||||
// make sure card is deselected
|
||||
spi_deselect(0);
|
||||
|
||||
//cprintf("CMD3: %lx\n", resp);
|
||||
// give SD card time to power up
|
||||
for (i = 0; i < 1000; i++);
|
||||
|
||||
return resp >> 16;
|
||||
|
||||
// select SD card
|
||||
spi_exchange(0xFF);
|
||||
spi_deselect(0);
|
||||
|
||||
// send 80 clock cycles to synchronize
|
||||
for(j = 0; j < SD_INIT_CYCLES; j++)
|
||||
spi_exchange(0xFF);
|
||||
}
|
||||
|
||||
uint16_t sd_select_card(uint16_t rca) {
|
||||
uint32_t resp;
|
||||
/*******************************************************************************
|
||||
Send command to SD card
|
||||
*******************************************************************************/
|
||||
void SD_command(uint8_t cmd, uint32_t arg, uint8_t crc)
|
||||
{
|
||||
// transmit command to sd card
|
||||
spi_exchange(cmd|0x40);
|
||||
|
||||
sd_card_command((uint32_t)rca << 16, 7);
|
||||
sd_card_resp(&resp);
|
||||
// transmit argument
|
||||
spi_exchange((uint8_t)(arg >> 24));
|
||||
spi_exchange((uint8_t)(arg >> 16));
|
||||
spi_exchange((uint8_t)(arg >> 8));
|
||||
spi_exchange((uint8_t)(arg));
|
||||
|
||||
return (uint16_t) resp;
|
||||
// transmit crc
|
||||
spi_exchange(crc|0x01);
|
||||
}
|
||||
|
||||
uint16_t sd_get_status(uint16_t rca) {
|
||||
uint32_t resp;
|
||||
/*******************************************************************************
|
||||
Read R1 from SD card
|
||||
*******************************************************************************/
|
||||
uint8_t SD_readRes1()
|
||||
{
|
||||
uint8_t i = 0, res1;
|
||||
|
||||
sd_card_command((uint32_t)rca << 16, 13);
|
||||
sd_card_resp(&resp);
|
||||
// keep polling until actual data received
|
||||
while((res1 = spi_exchange(0xFF)) == 0xFF)
|
||||
{
|
||||
i++;
|
||||
|
||||
return (uint16_t) resp;
|
||||
// if no data received for 8 bytes, break
|
||||
if(i > 8) break;
|
||||
}
|
||||
|
||||
return res1;
|
||||
}
|
||||
|
||||
void sd_readblock(uint32_t addr, void* buf) {
|
||||
uint32_t resp;
|
||||
int i;
|
||||
/*******************************************************************************
|
||||
Read R2 from SD card
|
||||
*******************************************************************************/
|
||||
void SD_readRes2(uint8_t *res)
|
||||
{
|
||||
// read response 1 in R2
|
||||
res[0] = SD_readRes1();
|
||||
|
||||
sd_card_command(addr, 17);
|
||||
sd_card_resp(&resp);
|
||||
//cprintf("CMD17: %lx\n", resp);
|
||||
|
||||
sd_card_wait_for_data();
|
||||
|
||||
//cprintf("Read data: \n");
|
||||
for (i = 0; i < 512; i++){
|
||||
((uint8_t*)buf)[i] = sd_card_read_byte();
|
||||
}
|
||||
|
||||
//cprintf("\n");
|
||||
// read final byte of response
|
||||
res[1] = spi_exchange(0xFF);
|
||||
}
|
||||
|
||||
/*******************************************************************************
|
||||
Read R3 from SD card
|
||||
*******************************************************************************/
|
||||
void SD_readRes3(uint8_t *res)
|
||||
{
|
||||
// read response 1 in R3
|
||||
res[0] = SD_readRes1();
|
||||
|
||||
// if error reading R1, return
|
||||
if(res[0] > 1) return;
|
||||
|
||||
// read remaining bytes
|
||||
SD_readBytes(res + 1, R3_BYTES);
|
||||
}
|
||||
|
||||
/*******************************************************************************
|
||||
Read R7 from SD card
|
||||
*******************************************************************************/
|
||||
void SD_readRes7(uint8_t *res)
|
||||
{
|
||||
// read response 1 in R7
|
||||
res[0] = SD_readRes1();
|
||||
|
||||
// if error reading R1, return
|
||||
if(res[0] > 1) return;
|
||||
|
||||
// read remaining bytes
|
||||
SD_readBytes(res + 1, R7_BYTES);
|
||||
}
|
||||
|
||||
/*******************************************************************************
|
||||
Read specified number of bytes from SD card
|
||||
*******************************************************************************/
|
||||
void SD_readBytes(uint8_t *res, uint8_t n)
|
||||
{
|
||||
while(n--) *res++ = spi_exchange(0xFF);
|
||||
}
|
||||
|
||||
/*******************************************************************************
|
||||
Command Idle State (CMD0)
|
||||
*******************************************************************************/
|
||||
uint8_t SD_goIdleState()
|
||||
{
|
||||
uint8_t res1;
|
||||
|
||||
// assert chip select
|
||||
spi_exchange(0xFF);
|
||||
spi_select(0);
|
||||
spi_exchange(0xFF);
|
||||
|
||||
// send CMD0
|
||||
SD_command(CMD0, CMD0_ARG, CMD0_CRC);
|
||||
|
||||
// read response
|
||||
res1 = SD_readRes1();
|
||||
|
||||
// deassert chip select
|
||||
spi_exchange(0xFF);
|
||||
spi_deselect(0);
|
||||
spi_exchange(0xFF);
|
||||
|
||||
return res1;
|
||||
}
|
||||
|
||||
/*******************************************************************************
|
||||
Send Interface Conditions (CMD8)
|
||||
*******************************************************************************/
|
||||
void SD_sendIfCond(uint8_t *res)
|
||||
{
|
||||
// assert chip select
|
||||
spi_exchange(0xFF);
|
||||
spi_select(0);
|
||||
spi_exchange(0xFF);
|
||||
|
||||
// send CMD8
|
||||
SD_command(CMD8, CMD8_ARG, CMD8_CRC);
|
||||
|
||||
// read response
|
||||
SD_readRes7(res);
|
||||
//SD_readBytes(res + 1, R7_BYTES);
|
||||
|
||||
// deassert chip select
|
||||
spi_exchange(0xFF);
|
||||
spi_deselect(0);
|
||||
spi_exchange(0xFF);
|
||||
}
|
||||
|
||||
/*******************************************************************************
|
||||
Read Status
|
||||
*******************************************************************************/
|
||||
void SD_sendStatus(uint8_t *res)
|
||||
{
|
||||
// assert chip select
|
||||
spi_exchange(0xFF);
|
||||
spi_select(0);
|
||||
spi_exchange(0xFF);
|
||||
|
||||
// send CMD13
|
||||
SD_command(CMD13, CMD13_ARG, CMD13_CRC);
|
||||
|
||||
// read response
|
||||
SD_readRes2(res);
|
||||
|
||||
// deassert chip select
|
||||
spi_exchange(0xFF);
|
||||
spi_deselect(0);
|
||||
spi_exchange(0xFF);
|
||||
}
|
||||
|
||||
/*******************************************************************************
|
||||
Read single 512 byte block
|
||||
token = 0xFE - Successful read
|
||||
token = 0x0X - Data error
|
||||
token = 0xFF - timeout
|
||||
*******************************************************************************/
|
||||
uint8_t SD_readSingleBlock(uint32_t addr, uint8_t *buf, uint8_t *token)
|
||||
{
|
||||
uint8_t res1, read;
|
||||
uint16_t readAttempts;
|
||||
uint16_t i;
|
||||
|
||||
/*
|
||||
|
||||
// set token to none
|
||||
*token = 0xFF;
|
||||
|
||||
// assert chip select
|
||||
spi_exchange(0xFF);
|
||||
spi_select(0);
|
||||
spi_exchange(0xFF);
|
||||
|
||||
// send CMD17
|
||||
SD_command(CMD17, addr, CMD17_CRC);
|
||||
|
||||
// read R1
|
||||
res1 = SD_readRes1();
|
||||
|
||||
// if response received from card
|
||||
if(res1 != 0xFF)
|
||||
{
|
||||
// wait for a response token (timeout = 100ms)
|
||||
readAttempts = 0;
|
||||
while(++readAttempts != SD_MAX_READ_ATTEMPTS)
|
||||
if((read = spi_exchange(0xFF)) != 0xFF) break;
|
||||
|
||||
// if response token is 0xFE
|
||||
if(read == SD_START_TOKEN)
|
||||
{
|
||||
// read 512 byte block
|
||||
for(i = 0; i < SD_BLOCK_LEN; i++) *buf++ = spi_exchange(0xFF);
|
||||
|
||||
// read 16-bit CRC
|
||||
spi_exchange(0xFF);
|
||||
spi_exchange(0xFF);
|
||||
}
|
||||
|
||||
// set token to card response
|
||||
*token = read;
|
||||
}
|
||||
|
||||
// deassert chip select
|
||||
spi_exchange(0xFF);
|
||||
spi_deselect(0);
|
||||
spi_exchange(0xFF);
|
||||
|
||||
*/
|
||||
|
||||
return res1;
|
||||
}
|
||||
|
||||
#define SD_MAX_WRITE_ATTEMPTS 3907
|
||||
|
||||
/*******************************************************************************
|
||||
Write single 512 byte block
|
||||
token = 0x00 - busy timeout
|
||||
token = 0x05 - data accepted
|
||||
token = 0xFF - response timeout
|
||||
*******************************************************************************/
|
||||
uint8_t SD_writeSingleBlock(uint32_t addr, uint8_t *buf, uint8_t *token)
|
||||
{
|
||||
uint16_t readAttempts;
|
||||
uint8_t res1, read;
|
||||
uint16_t i;
|
||||
|
||||
/*
|
||||
|
||||
// set token to none
|
||||
*token = 0xFF;
|
||||
|
||||
// assert chip select
|
||||
spi_exchange(0xFF);
|
||||
spi_select(0);
|
||||
spi_exchange(0xFF);
|
||||
|
||||
// send CMD24
|
||||
SD_command(CMD24, addr, CMD24_CRC);
|
||||
|
||||
// read response
|
||||
res1 = SD_readRes1();
|
||||
|
||||
// if no error
|
||||
if(res1 == SD_READY)
|
||||
{
|
||||
// send start token
|
||||
spi_exchange(SD_START_TOKEN);
|
||||
|
||||
// write buffer to card
|
||||
for(i = 0; i < SD_BLOCK_LEN; i++) spi_exchange(buf[i]);
|
||||
|
||||
// wait for a response (timeout = 250ms)
|
||||
readAttempts = 0;
|
||||
while(++readAttempts != SD_MAX_WRITE_ATTEMPTS)
|
||||
if((read = spi_exchange(0xFF)) != 0xFF) { *token = 0xFF; break; }
|
||||
|
||||
// if data accepted
|
||||
if((read & 0x1F) == 0x05)
|
||||
{
|
||||
// set token to data accepted
|
||||
*token = 0x05;
|
||||
|
||||
// wait for write to finish (timeout = 250ms)
|
||||
readAttempts = 0;
|
||||
while(spi_exchange(0xFF) == 0x00)
|
||||
if(++readAttempts == SD_MAX_WRITE_ATTEMPTS) { *token = 0x00; break; }
|
||||
}
|
||||
}
|
||||
|
||||
// deassert chip select
|
||||
spi_exchange(0xFF);
|
||||
spi_deselect(0);
|
||||
spi_exchange(0xFF);
|
||||
|
||||
*/
|
||||
|
||||
return res1;
|
||||
}
|
||||
|
||||
/*******************************************************************************
|
||||
Reads OCR from SD Card
|
||||
*******************************************************************************/
|
||||
void SD_readOCR(uint8_t *res)
|
||||
{
|
||||
uint8_t tmp;
|
||||
|
||||
// assert chip select
|
||||
spi_exchange(0xFF);
|
||||
spi_select(0);
|
||||
tmp = spi_exchange(0xFF);
|
||||
|
||||
if(tmp != 0xFF) while(spi_exchange(0xFF) != 0xFF) ;
|
||||
|
||||
// send CMD58
|
||||
SD_command(CMD58, CMD58_ARG, CMD58_CRC);
|
||||
|
||||
// read response
|
||||
SD_readRes3(res);
|
||||
|
||||
// deassert chip select
|
||||
spi_exchange(0xFF);
|
||||
spi_deselect(0);
|
||||
spi_exchange(0xFF);
|
||||
}
|
||||
|
||||
/*******************************************************************************
|
||||
Send application command (CMD55)
|
||||
*******************************************************************************/
|
||||
uint8_t SD_sendApp()
|
||||
{
|
||||
uint8_t res1;
|
||||
|
||||
// assert chip select
|
||||
spi_exchange(0xFF);
|
||||
spi_select(0);
|
||||
spi_exchange(0xFF);
|
||||
|
||||
// send CMD0
|
||||
SD_command(CMD55, CMD55_ARG, CMD55_CRC);
|
||||
|
||||
// read response
|
||||
res1 = SD_readRes1();
|
||||
|
||||
// deassert chip select
|
||||
spi_exchange(0xFF);
|
||||
spi_deselect(0);
|
||||
spi_exchange(0xFF);
|
||||
|
||||
return res1;
|
||||
}
|
||||
|
||||
/*******************************************************************************
|
||||
Send operating condition (ACMD41)
|
||||
*******************************************************************************/
|
||||
uint8_t SD_sendOpCond()
|
||||
{
|
||||
uint8_t res1;
|
||||
// assert chip select
|
||||
spi_exchange(0xFF);
|
||||
spi_select(0);
|
||||
spi_exchange(0xFF);
|
||||
|
||||
// send CMD0
|
||||
SD_command(ACMD41, ACMD41_ARG, ACMD41_CRC);
|
||||
|
||||
// read response
|
||||
res1 = SD_readRes1();
|
||||
|
||||
// deassert chip select
|
||||
spi_exchange(0xFF);
|
||||
spi_deselect(0);
|
||||
spi_exchange(0xFF);
|
||||
|
||||
return res1;
|
||||
}
|
||||
|
||||
@@ -3,16 +3,76 @@
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
void sd_init();
|
||||
uint16_t sd_get_rca();
|
||||
uint16_t sd_select_card(uint16_t rca);
|
||||
uint16_t sd_get_status(uint16_t rca);
|
||||
void sd_readblock(uint32_t addr, void* buf);
|
||||
// command definitions
|
||||
#define CMD0 0
|
||||
#define CMD0_ARG 0x00000000
|
||||
#define CMD0_CRC 0x94
|
||||
#define CMD8 8
|
||||
#define CMD8_ARG 0x0000001AA
|
||||
#define CMD8_CRC 0x86
|
||||
#define CMD9 9
|
||||
#define CMD9_ARG 0x00000000
|
||||
#define CMD9_CRC 0x00
|
||||
#define CMD10 9
|
||||
#define CMD10_ARG 0x00000000
|
||||
#define CMD10_CRC 0x00
|
||||
#define CMD13 13
|
||||
#define CMD13_ARG 0x00000000
|
||||
#define CMD13_CRC 0x00
|
||||
#define CMD17 17
|
||||
#define CMD17_CRC 0x00
|
||||
#define CMD24 24
|
||||
#define CMD24_CRC 0x00
|
||||
#define CMD55 55
|
||||
#define CMD55_ARG 0x00000000
|
||||
#define CMD55_CRC 0x00
|
||||
#define CMD58 58
|
||||
#define CMD58_ARG 0x00000000
|
||||
#define CMD58_CRC 0x00
|
||||
#define ACMD41 41
|
||||
#define ACMD41_ARG 0x40000000
|
||||
#define ACMD41_CRC 0x00
|
||||
|
||||
void sd_card_command(uint32_t arg, uint8_t cmd);
|
||||
#define SD_IN_IDLE_STATE 0x01
|
||||
#define SD_READY 0x00
|
||||
#define SD_R1_NO_ERROR(X) X < 0x02
|
||||
|
||||
void sd_card_resp(uint32_t* resp);
|
||||
uint8_t sd_card_read_byte();
|
||||
void sd_card_wait_for_data();
|
||||
#define R3_BYTES 4
|
||||
#define R7_BYTES 4
|
||||
|
||||
#endif
|
||||
#define CMD0_MAX_ATTEMPTS 255
|
||||
#define CMD55_MAX_ATTEMPTS 255
|
||||
#define SD_ERROR 1
|
||||
#define SD_SUCCESS 0
|
||||
#define SD_MAX_READ_ATTEMPTS 1563
|
||||
#define SD_READ_START_TOKEN 0xFE
|
||||
#define SD_INIT_CYCLES 80
|
||||
|
||||
#define SD_START_TOKEN 0xFE
|
||||
#define SD_ERROR_TOKEN 0x00
|
||||
|
||||
#define SD_DATA_ACCEPTED 0x05
|
||||
#define SD_DATA_REJECTED_CRC 0x0B
|
||||
#define SD_DATA_REJECTED_WRITE 0x0D
|
||||
|
||||
#define SD_BLOCK_LEN 512
|
||||
|
||||
// SD functions
|
||||
uint8_t SD_init();
|
||||
void SD_powerUpSeq();
|
||||
void SD_command(uint8_t cmd, uint32_t arg, uint8_t crc);
|
||||
uint8_t SD_readRes1();
|
||||
void SD_readRes2(uint8_t *res);
|
||||
void SD_readRes3(uint8_t *res);
|
||||
void SD_readRes7(uint8_t *res);
|
||||
void SD_readBytes(uint8_t *res, uint8_t n);
|
||||
uint8_t SD_goIdleState();
|
||||
void SD_sendIfCond(uint8_t *res);
|
||||
void SD_sendStatus(uint8_t *res);
|
||||
void SD_readOCR(uint8_t *res);
|
||||
uint8_t SD_sendApp();
|
||||
uint8_t SD_sendOpCond();
|
||||
uint8_t SD_readSingleBlock(uint32_t addr, uint8_t *buf, uint8_t *error);
|
||||
uint8_t SD_writeSingleBlock(uint32_t addr, uint8_t *buf, uint8_t *res);
|
||||
|
||||
#endif
|
||||
@@ -1,66 +0,0 @@
|
||||
.include "io.inc65"
|
||||
|
||||
.importzp sp, sreg, ptr1
|
||||
|
||||
.export _sd_card_command
|
||||
.export _sd_card_resp
|
||||
.export _sd_card_read_byte
|
||||
.export _sd_card_wait_for_data
|
||||
|
||||
.autoimport on
|
||||
|
||||
.code
|
||||
|
||||
; Send sd card command.
|
||||
; command is in A register, the args are on the stack
|
||||
; I think the order is high byte first?
|
||||
_sd_card_command:
|
||||
pha
|
||||
|
||||
jsr popeax
|
||||
sta SD_ARG
|
||||
stx SD_ARG+1
|
||||
lda sreg
|
||||
sta SD_ARG+2
|
||||
lda sreg+1
|
||||
sta SD_ARG+3
|
||||
|
||||
pla
|
||||
sta SD_CMD
|
||||
rts
|
||||
|
||||
; void sd_card_resp(uint32_t* resp);
|
||||
_sd_card_resp:
|
||||
phy
|
||||
sta ptr1 ; store pointer
|
||||
stx ptr1+1
|
||||
@1: lda SD_CMD ; wait for status flag
|
||||
and #$01
|
||||
beq @1
|
||||
lda SD_ARG
|
||||
ldy #$0
|
||||
sta (ptr1),y
|
||||
lda SD_ARG+1
|
||||
iny
|
||||
sta (ptr1),y
|
||||
lda SD_ARG+2
|
||||
iny
|
||||
sta (ptr1),y
|
||||
lda SD_ARG+3
|
||||
iny
|
||||
sta (ptr1),y
|
||||
ply
|
||||
rts
|
||||
|
||||
_sd_card_read_byte:
|
||||
lda SD_DATA
|
||||
ldx #$00
|
||||
rts
|
||||
|
||||
_sd_card_wait_for_data:
|
||||
pha
|
||||
@1: lda SD_CMD ; wait for status flag
|
||||
and #$02
|
||||
beq @1
|
||||
pla
|
||||
rts
|
||||
176
sw/bootloader/devices/sd_print.c
Normal file
176
sw/bootloader/devices/sd_print.c
Normal file
@@ -0,0 +1,176 @@
|
||||
#include <conio.h>
|
||||
|
||||
#include "sd_print.h"
|
||||
#include "sd_card.h"
|
||||
|
||||
void SD_printR1(uint8_t res)
|
||||
{
|
||||
if(res == 0xFF)
|
||||
{ cputs("\tNo response\r\n"); return; }
|
||||
if(res & 0x80)
|
||||
{ cputs("\tError: MSB = 1\r\n"); return; }
|
||||
if(res == 0)
|
||||
{ cputs("\tCard Ready\r\n"); return; }
|
||||
if(PARAM_ERROR(res))
|
||||
cputs("\tParameter Error\r\n");
|
||||
if(ADDR_ERROR(res))
|
||||
cputs("\tAddress Error\r\n");
|
||||
if(ERASE_SEQ_ERROR(res))
|
||||
cputs("\tErase Sequence Error\r\n");
|
||||
if(CRC_ERROR(res))
|
||||
cputs("\tCRC Error\r\n");
|
||||
if(ILLEGAL_CMD(res))
|
||||
cputs("\tIllegal Command\r\n");
|
||||
if(ERASE_RESET(res))
|
||||
cputs("\tErase Reset Error\r\n");
|
||||
if(IN_IDLE(res))
|
||||
cputs("\tIn Idle State\r\n");
|
||||
}
|
||||
|
||||
void SD_printR2(uint8_t *res)
|
||||
{
|
||||
SD_printR1(res[0]);
|
||||
|
||||
if(res[0] == 0xFF) return;
|
||||
|
||||
if(res[1] == 0x00)
|
||||
cputs("\tNo R2 Error\r\n");
|
||||
if(OUT_OF_RANGE(res[1]))
|
||||
cputs("\tOut of Range\r\n");
|
||||
if(ERASE_PARAM(res[1]))
|
||||
cputs("\tErase Parameter\r\n");
|
||||
if(WP_VIOLATION(res[1]))
|
||||
cputs("\tWP Violation\r\n");
|
||||
if(CARD_ECC_FAILED(res[1]))
|
||||
cputs("\tECC Failed\r\n");
|
||||
if(CC_ERROR(res[1]))
|
||||
cputs("\tCC Error\r\n");
|
||||
if(ERROR(res[1]))
|
||||
cputs("\tError\r\n");
|
||||
if(WP_ERASE_SKIP(res[1]))
|
||||
cputs("\tWP Erase Skip\r\n");
|
||||
if(CARD_LOCKED(res[1]))
|
||||
cputs("\tCard Locked\r\n");
|
||||
}
|
||||
|
||||
void SD_printR3(uint8_t *res)
|
||||
{
|
||||
SD_printR1(res[0]);
|
||||
|
||||
if(res[0] > 1) return;
|
||||
|
||||
cputs("\tCard Power Up Status: ");
|
||||
if(POWER_UP_STATUS(res[1]))
|
||||
{
|
||||
cputs("READY\r\n");
|
||||
cputs("\tCCS Status: ");
|
||||
if(CCS_VAL(res[1])){ cputs("1\r\n"); }
|
||||
else cputs("0\r\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
cputs("BUSY\r\n");
|
||||
}
|
||||
|
||||
cputs("\tVDD Window: ");
|
||||
if(VDD_2728(res[3])) cputs("2.7-2.8, ");
|
||||
if(VDD_2829(res[2])) cputs("2.8-2.9, ");
|
||||
if(VDD_2930(res[2])) cputs("2.9-3.0, ");
|
||||
if(VDD_3031(res[2])) cputs("3.0-3.1, ");
|
||||
if(VDD_3132(res[2])) cputs("3.1-3.2, ");
|
||||
if(VDD_3233(res[2])) cputs("3.2-3.3, ");
|
||||
if(VDD_3334(res[2])) cputs("3.3-3.4, ");
|
||||
if(VDD_3435(res[2])) cputs("3.4-3.5, ");
|
||||
if(VDD_3536(res[2])) cputs("3.5-3.6");
|
||||
cputs("\r\n");
|
||||
}
|
||||
|
||||
void SD_printR7(uint8_t *res)
|
||||
{
|
||||
SD_printR1(res[0]);
|
||||
|
||||
if(res[0] > 1) return;
|
||||
|
||||
cputs("\tCommand Version: ");
|
||||
cprintf("%x", CMD_VER(res[1]));
|
||||
cputs("\r\n");
|
||||
|
||||
cputs("\tVoltage Accepted: ");
|
||||
if(VOL_ACC(res[3]) == VOLTAGE_ACC_27_33) {
|
||||
cputs("2.7-3.6V\r\n");
|
||||
} else if(VOL_ACC(res[3]) == VOLTAGE_ACC_LOW) {
|
||||
cputs("LOW VOLTAGE\r\n");
|
||||
} else if(VOL_ACC(res[3]) == VOLTAGE_ACC_RES1) {
|
||||
cputs("RESERVED\r\n");
|
||||
} else if(VOL_ACC(res[3]) == VOLTAGE_ACC_RES2) {
|
||||
cputs("RESERVED\r\n");
|
||||
} else {
|
||||
cputs("NOT DEFINED\r\n");
|
||||
}
|
||||
|
||||
cputs("\tEcho: ");
|
||||
cprintf("%x", res[4]);
|
||||
cputs("\r\n");
|
||||
}
|
||||
|
||||
void SD_printCSD(uint8_t *buf)
|
||||
{
|
||||
cputs("CSD:\r\n");
|
||||
|
||||
cputs("\tCSD Structure: ");
|
||||
cprintf("%x", (buf[0] & 0b11000000) >> 6);
|
||||
cputs("\r\n");
|
||||
|
||||
cputs("\tTAAC: ");
|
||||
cprintf("%x", buf[1]);
|
||||
cputs("\r\n");
|
||||
|
||||
cputs("\tNSAC: ");
|
||||
cprintf("%x", buf[2]);
|
||||
cputs("\r\n");
|
||||
|
||||
cputs("\tTRAN_SPEED: ");
|
||||
cprintf("%x", buf[3]);
|
||||
cputs("\r\n");
|
||||
|
||||
cputs("\tDevice Size: ");
|
||||
cprintf("%x", buf[7] & 0b00111111);
|
||||
cprintf("%x", buf[8]);
|
||||
cprintf("%x", buf[9]);
|
||||
cputs("\r\n");
|
||||
}
|
||||
|
||||
void SD_printBuf(uint8_t *buf)
|
||||
{
|
||||
uint8_t colCount = 0;
|
||||
uint16_t i;
|
||||
for(i = 0; i < SD_BLOCK_LEN; i++)
|
||||
{
|
||||
cprintf("%x", *buf++);
|
||||
if(colCount == 19)
|
||||
{
|
||||
cputs("\r\n");
|
||||
colCount = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
cputc(' ');
|
||||
colCount++;
|
||||
}
|
||||
}
|
||||
cputs("\r\n");
|
||||
}
|
||||
|
||||
void SD_printDataErrToken(uint8_t token)
|
||||
{
|
||||
if(token & 0xF0)
|
||||
cputs("\tNot Error token\r\n");
|
||||
if(SD_TOKEN_OOR(token))
|
||||
cputs("\tData out of range\r\n");
|
||||
if(SD_TOKEN_CECC(token))
|
||||
cputs("\tCard ECC failed\r\n");
|
||||
if(SD_TOKEN_CC(token))
|
||||
cputs("\tCC Error\r\n");
|
||||
if(SD_TOKEN_ERROR(token))
|
||||
cputs("\tError\r\n");
|
||||
}
|
||||
61
sw/bootloader/devices/sd_print.h
Normal file
61
sw/bootloader/devices/sd_print.h
Normal file
@@ -0,0 +1,61 @@
|
||||
#ifndef SD_PRINT_H
|
||||
#define SD_PRINT_H
|
||||
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
|
||||
/* R1 MACROS */
|
||||
#define PARAM_ERROR(X) X & 0b01000000
|
||||
#define ADDR_ERROR(X) X & 0b00100000
|
||||
#define ERASE_SEQ_ERROR(X) X & 0b00010000
|
||||
#define CRC_ERROR(X) X & 0b00001000
|
||||
#define ILLEGAL_CMD(X) X & 0b00000100
|
||||
#define ERASE_RESET(X) X & 0b00000010
|
||||
#define IN_IDLE(X) X & 0b00000001
|
||||
|
||||
/* R2 MACROS */
|
||||
#define OUT_OF_RANGE(X) X & 0b10000000
|
||||
#define ERASE_PARAM(X) X & 0b01000000
|
||||
#define WP_VIOLATION(X) X & 0b00100000
|
||||
#define CARD_ECC_FAILED(X) X & 0b00010000
|
||||
#define CC_ERROR(X) X & 0b00001000
|
||||
#define ERROR(X) X & 0b00000100
|
||||
#define WP_ERASE_SKIP(X) X & 0b00000010
|
||||
#define CARD_LOCKED(X) X & 0b00000001
|
||||
|
||||
/* R3 MACROS */
|
||||
#define POWER_UP_STATUS(X) X & 0x40
|
||||
#define CCS_VAL(X) X & 0x40
|
||||
#define VDD_2728(X) X & 0b10000000
|
||||
#define VDD_2829(X) X & 0b00000001
|
||||
#define VDD_2930(X) X & 0b00000010
|
||||
#define VDD_3031(X) X & 0b00000100
|
||||
#define VDD_3132(X) X & 0b00001000
|
||||
#define VDD_3233(X) X & 0b00010000
|
||||
#define VDD_3334(X) X & 0b00100000
|
||||
#define VDD_3435(X) X & 0b01000000
|
||||
#define VDD_3536(X) X & 0b10000000
|
||||
|
||||
/* R7 MACROS */
|
||||
#define CMD_VER(X) ((X >> 4) & 0xF0)
|
||||
#define VOL_ACC(X) (X & 0x1F)
|
||||
#define VOLTAGE_ACC_27_33 0b00000001
|
||||
#define VOLTAGE_ACC_LOW 0b00000010
|
||||
#define VOLTAGE_ACC_RES1 0b00000100
|
||||
#define VOLTAGE_ACC_RES2 0b00001000
|
||||
|
||||
/* DATA ERROR TOKEN */
|
||||
#define SD_TOKEN_OOR(X) X & 0b00001000
|
||||
#define SD_TOKEN_CECC(X) X & 0b00000100
|
||||
#define SD_TOKEN_CC(X) X & 0b00000010
|
||||
#define SD_TOKEN_ERROR(X) X & 0b00000001
|
||||
|
||||
void SD_printR1(uint8_t res);
|
||||
void SD_printR2(uint8_t *res);
|
||||
void SD_printR3(uint8_t *res);
|
||||
void SD_printR7(uint8_t *res);
|
||||
void SD_printBuf(uint8_t *buf);
|
||||
void SD_printDataErrToken(uint8_t token);
|
||||
|
||||
#endif
|
||||
12
sw/bootloader/devices/spi.h
Normal file
12
sw/bootloader/devices/spi.h
Normal file
@@ -0,0 +1,12 @@
|
||||
#ifndef _SPI_H
|
||||
#define _SPI_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
void spi_select(uint8_t id);
|
||||
void spi_deselect(uint8_t id);
|
||||
uint8_t spi_read();
|
||||
void spi_write(uint8_t data);
|
||||
uint8_t spi_exchange(uint8_t data);
|
||||
|
||||
#endif
|
||||
39
sw/bootloader/devices/spi.s
Normal file
39
sw/bootloader/devices/spi.s
Normal file
@@ -0,0 +1,39 @@
|
||||
.include "io.inc65"
|
||||
|
||||
.importzp zp, sreg
|
||||
|
||||
.export _spi_select, _spi_deselect
|
||||
.export _spi_read, _spi_write, _spi_exchange
|
||||
|
||||
.autoimport on
|
||||
|
||||
.code
|
||||
|
||||
; void spi_select(uint8_t id)
|
||||
; Select a (the) slave by pulling its CS line down
|
||||
; TODO allow active high or active low CS
|
||||
; TODO allow more than one slave
|
||||
_spi_select:
|
||||
lda #$1 ; Ignore whatever id is, 1 is the only option
|
||||
sta SPI_CTRL
|
||||
rts
|
||||
|
||||
; void spi_deslect(uint8_t id)
|
||||
; Deslect a slave by pulling its CS line up
|
||||
; TODO allow active high or active low CS
|
||||
_spi_deselect:
|
||||
stz SPI_CTRL
|
||||
rts
|
||||
|
||||
; uint8_t spi_read()
|
||||
_spi_read:
|
||||
lda #$0
|
||||
; void spi_write(uint8_t data)
|
||||
_spi_write:
|
||||
; uint8_t spi_exchange(uint8_t data)
|
||||
_spi_exchange:
|
||||
sta SPI_OUTPUT
|
||||
@1: lda SPI_CTRL
|
||||
bmi @1
|
||||
lda SPI_INPUT
|
||||
rts
|
||||
@@ -6,6 +6,7 @@
|
||||
#include "devices/board_io.h"
|
||||
#include "devices/uart.h"
|
||||
#include "devices/sd_card.h"
|
||||
#include "devices/sd_print.h"
|
||||
#include "filesystem/fat.h"
|
||||
|
||||
#define KERNEL_LOAD_ADDR 0xD000
|
||||
@@ -13,40 +14,68 @@
|
||||
uint8_t buf[512];
|
||||
|
||||
int main() {
|
||||
uint16_t rca;
|
||||
clrscr();
|
||||
cputs("Starting sd_init\n");
|
||||
cprintf("And testing cprintf\n");
|
||||
// array to hold responses
|
||||
uint8_t res[5], token;
|
||||
uint32_t addr = 0x00000000;
|
||||
uint16_t i;
|
||||
|
||||
sd_init();
|
||||
cputs("Start\r\n");
|
||||
|
||||
cprintf("finish sd_init\n");
|
||||
// initialize sd card
|
||||
if(SD_init() != SD_SUCCESS)
|
||||
{
|
||||
cputs("Error init SD CARD\r\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
cputs("SD Card init\r\n");
|
||||
|
||||
rca = sd_get_rca();
|
||||
cprintf("rca: %x\n", rca);
|
||||
// read sector 0
|
||||
// cputs("\r\nReading sector: 0x");
|
||||
// ((uint8_t)(addr >> 24));
|
||||
// cprintf("%x", (uint8_t)(addr >> 16));
|
||||
// cprintf("%x", (uint8_t)(addr >> 8));
|
||||
// cprintf("%x", (uint8_t)addr);
|
||||
// res[0] = SD_readSingleBlock(addr, buf, &token);
|
||||
// cputs("\r\nResponse:\r\n");
|
||||
// //SD_printR1(res[0]);
|
||||
|
||||
sd_select_card(rca);
|
||||
// if no error, print buffer
|
||||
// if((res[0] == 0x00) && (token == SD_START_TOKEN))
|
||||
// SD_printBuf(buf);
|
||||
// else if error token received, print
|
||||
// else if(!(token & 0xF0))
|
||||
// {
|
||||
// cputs("Ercputsror token:\r\n");
|
||||
// SD_printDataErrToken(token);
|
||||
// }
|
||||
|
||||
/*
|
||||
fat_init();
|
||||
// update address to 0x00000100
|
||||
// addr = 0x00000100;
|
||||
|
||||
filename = (char*)malloc(FAT_MAX_FILE_NAME);
|
||||
// // fill buffer with 0x55
|
||||
// for(i = 0; i < 512; i++) buf[i] = 0x55;
|
||||
|
||||
cluster = fat_parse_path_to_cluster("/kernel.bin");
|
||||
for (kernel_load = (uint8_t*)KERNEL_LOAD_ADDR; cluster < FAT_CLUSTERMASK; kernel_load+=(8*512)) {
|
||||
cprintf("cluster: %lx\n", cluster);
|
||||
cprintf("Writing to %p\n", kernel_load);
|
||||
fat_read_cluster(cluster, kernel_load);
|
||||
cluster = fat_get_chain_value(cluster);
|
||||
}
|
||||
// cputs("Writing 0x55 to sector: 0x");
|
||||
// cprintf("%x", (uint8_t)(addr >> 24));
|
||||
// cprintf("%x", (uint8_t)(addr >> 16));
|
||||
// cprintf("%x", (uint8_t)(addr >> 8));
|
||||
// cprintf("%x", (uint8_t)addr);
|
||||
|
||||
*/
|
||||
// // write data to sector
|
||||
// res[0] = SD_writeSingleBlock(addr, buf, &token);
|
||||
|
||||
cprintf("Done!\n");
|
||||
// cputs("\r\nResponse:\r\n");
|
||||
// //SD_printR1(res[0]);
|
||||
|
||||
for(;;);
|
||||
// // if no errors writing
|
||||
// if(res[0] == 0x00)
|
||||
// {
|
||||
// if(token == SD_DATA_ACCEPTED)
|
||||
// cputs("Write successful\r\n");
|
||||
// }
|
||||
}
|
||||
|
||||
cprintf("Reset vector: %x\n", *((uint16_t*)0xfffc));
|
||||
while(1) ;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user