AVR to SD-Card Interface

Go To Last Post
12 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello,

has anyone of you written some routines to interface a secure digital memory card trough SPI?

I'm building a little mp3 player with a vs1001k as decoder chip. By now i'm using about 7KB of free programmemoty to store "little" mp3's on my ATMega8. If you want to see more: click here

best Regards
Tobias

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Sorry the Link is not working...
use www.tobiscorner.at.tf/megaVS/ in your adressbar...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'd be more interested in C-Source for SD-Cards than ASM Routines for MMC

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hello tobias,
you can find the c source for mmc in project 21
https://www.avrfreaks.net/Freaks/...

gerhard

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Project 21 contains ASM Routines for MMC Cards...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hello tobia,
i found some c primitive routines (for mmc), i could send it to you per email if you like?

gerhard

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

o.k my email adr ist tobias.floery@cable.vol.at

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

is on the way

gerhard

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I want it too... Can u send it to me?
Thank you.

admin's test signature
 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Could you also send them to me, please?
Thanks.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi Gerhard,

Could you send it to me too? Thanks

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hello
here is the code:

/*
Project ú?DataFlash Read&Write Functions.
Author ú?wjc2k@21cn.com

SoftWare: WINAVR GCC 3.3
HardWareú?ATMega8L
Version : 20030324
Note ú?

History : 2003.3
*/
#include
//if in WinAVR(AVRGCC 3.3 upper)
#include
//else
//#define uint8_t unsigned char
//#define uint16_t unsigned int

#define Set_Bit(x, y) (x|=(1 PB2(SS)

//Others Use AVR's SPI: SCKSCK SOMISO SIMOSI RSTVcc

// Enable and Disable DataFlash.
#define EnableCS() Clr_Bit(DF_PORT,DF_CS)
#define DisableCS() Set_Bit(DF_PORT,DF_CS)
#define WaitBusy() while(!(Flash_Status()&0x80))

//********************************************************************************
//!!! if Use AVR's ISP, DataFlash' CS need pullup(10Kom).
//*******************************************************************************

//-------------------------------------------------------------------------
void Flash_Init();
uint8_t Flash_Status(void);
void Flash_WriteBuf (uint8_t idx,uint16_t ofst,uint16_t len,uint8_t *buf );
void Flash_ReadBuf (uint8_t idx,uint16_t ofst,uint16_t len,uint8_t *buf );
void Flash_BufToPage(uint8_t idx,uint16_t page );
void Flash_PageToBuf(uint8_t idx,uint16_t page );

//=========================================================================
uint8_t WriteByte(uint8_t val)
{// uint8_t d;
SPDR = val;
while (!(SPSR & _BV(SPIF)));
return(SPDR);
}
//-------------------------------------------------------------------------
uint8_t Flash_Status(void)
{ uint8_t d=0;
EnableSPI();
EnableCS();
d=WriteByte(STATUS_READ);
DisableCS();
DisableSPI();
return(d);
}
//-----------------------------------------------------------------------
//Read DataFlash's [page] to DataFlash's Buffer[idx]
//idx=1 ==> buf[0] idx=2 ==> buf[1]
void Flash_PageToBuf(uint8_t idx,uint16_t page)
{
EnableSPI();
EnableCS();
WriteByte(PAGE_TO_BUFF+(idx-1)*2);
WriteByte((uint8_t)(page>>7));
WriteByte((uint8_t)(page buf[0] idx=2 ==> buf[1]
void Flash_BufToPage(uint8_t idx,uint16_t page)
{
EnableSPI();
EnableCS();
WriteByte(BUFF_TO_PAGE+(idx-1)*2);
WriteByte((uint8_t)(page>>7));
WriteByte((uint8_t)(page buf[0] idx=2 ==> buf[1]
void Flash_WriteBuf(uint8_t idx,uint16_t ofst,uint16_t len,uint8_t *buf)
{
EnableSPI();
EnableCS();
WriteByte(BUFF_WRITE+(idx-1)*3);
WriteByte(0X00);
WriteByte((uint8_t)(ofst>>8));
WriteByte((uint8_t)ofst);
while(len--) { WriteByte(*buf++); }
DisableSPI();
DisableCS();
}
//-------------------------------------------------------------
// Read DataFlash's Buffer[idx] to *buf, Length is [len].
//idx=1 ==> buf[0] idx=2 ==> buf[1]
void Flash_ReadBuf(uint8_t idx,uint16_t ofst,uint16_t len,uint8_t *buf)
{
EnableSPI();
EnableCS();
WriteByte(BUFF_READ+(idx-1)*2);
WriteByte(0x00);
WriteByte((uint8_t)(ofst>>8));
WriteByte((uint8_t)ofst);
WriteByte(0X00);
while(len--) { *buf++=SPDR; }
DisableCS();
DisableSPI();
}
/*
uint8_t Flash_Erase(uint16_t page)
{
EnableSPI();
EnableCS();
WriteByte(PAGE_ERASE);
WriteByte((uint8_t)(page>>7));
WriteByte((uint8_t)(page<<1));
WriteByte(0x00);
DisableCS();
WaitBusy();
DisableSPI();
return(Flash_Status());
}
*/
void Flash_Init()
{
// SPI Port initialisation .ATMega8.
// \ \ SCK, MISO, MOSI, CS, \, \
// PB7, PB6, PB5, PB4, PB3, PB2 , PB1, PB0
// . . O I O O . .
// . . 1 0 1 1 . .
DDRB = _BV(DDB5)|_BV(DDB3)|_BV(DDB2); // Set SCK,MOSI,SS as outputs.
PORTB = _BV(PB5)|_BV(PB3)|_BV(PB2); //Set SCK,MOSI,SS high.
}