Skip to content

Instantly share code, notes, and snippets.

@Solvalou
Created September 27, 2010 20:03
Show Gist options
  • Save Solvalou/599717 to your computer and use it in GitHub Desktop.
Save Solvalou/599717 to your computer and use it in GitHub Desktop.
#include "sd.h"
//+==========================================+
//| initialization of the sd-card |
//+==========================================+
char SD_init()
{
DDRSP &=~ (1<<SPI_MISO); // sets pin MISO to an input
DDRSP |= (1<<SPI_MOSI); // sets pin MOSI to an output
DDRSP |= (1<<SPI_SS); // sets pin SS to an output
DDRSP |= (1<<SPI_SCK); // sets pin SCK to an output
unsigned char timeout = 0;
for (unsigned int a = 0; a < 550; a++) // wastes some cycles, waits for stabilisation of the power source
{
nop(); // no operation
}
volatile char IOReg;
SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0)|(1<<SPR1);
// SPE - enables SPI-mode
// MSTR - sets µC to be the master
// SPR0, SPR0 - setting the SPI-clock
SPSR = (0<<SPI2X);
// SPI2X - setting the SPI-clock(SCK) to (µC-clock/128)
IOReg = SPSR; // clear SPIF bit in SPSR
IOReg = SPDR;
for (unsigned char i = 0; i < 0x0F; i++) // sends 74+ clocks to the sd-card
{
SD_WRbyte(0xFF);
}
// load CMD with command CMD0 = GO_IDLE_STATE
char CMD[] = {0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x95};
while(SD_WRcmd(CMD) != 1) // repeat until sd-card sends 0x01
{
if(timeout++ > 300)
{
SD_disable();
return(1);
}
timeout = 0;
}
// load CMD with command CMD55 = prefix for an ACMD-command
CMD[0] = 0x77;
CMD[7] = 0x01;
while(SD_WRcmd(CMD) != 1)
{
if(timeout++ > 300)
{
SD_disable();
return(1);
}
timeout = 0;
}
// load CMD with command ACMD41 = SD_SEND_OP_COND
CMD[0] = 0x69;
while(SD_WRcmd(CMD) != 0)
{
if(timeout++ > 300)
{
SD_disable();
return(1);
}
timeout = 0;
}
return(0);
}
//+==========================================+
//| sends one byte to the sd-card |
//+==========================================+
void SD_WRbyte (char byte)
{
SPDR = byte; // load byte in the SPDR, data-transfer begins
while(!(SPSR & (1<<SPIF))) // SPIF = 1 when transfer completed
{
}
}
//+==========================================+
//| receives one byte from the sd-card |
//+==========================================+
char SD_RDbyte ()
{
SPDR = 0xFF;
while(!(SPSR & (1<<SPIF)))
{
}
return(SPDR);
}
//+==========================================+
//| sends a command to the sd-card |
//+==========================================+
char SD_WRcmd (char *cmd)
{
unsigned char timeout = 0;
char temp = 0xFF;
SD_enable();
for ( unsigned char i = 0; i < 0x06; i++)
{
SD_WRbyte(*cmd++);
}
while(temp == 0xFF)
{
temp = SD_RDbyte();
if(timeout++ > 400)
{
break;
}
}
SD_disable(); // deactivates SD-card
SD_WRbyte(0xFF); // wastes eight cycles
return(temp);
}
//+==========================================+
//| enables the sd-card |
//+==========================================+
void SD_disable ()
{
DDRSP |= (1<<SPI_SS);
}
//+==========================================+
//| disables the sd-card |
//+==========================================+
void SD_enable ()
{
DDRSP &=~(1<<SPI_SS);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment