Forum Menu




 


Log in Problems?
New User? Sign Up!
AVR Freaks Forum Index

Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Author Message
ibnukoel
PostPosted: Nov 10, 2011 - 04:19 AM
Newbie


Joined: Oct 29, 2011
Posts: 2


hey guys.. i have program to use SPI to read SDCARD.. but i don't know the data is flow.. any body tell me how this data flow in SPI mode,, and where i can acces the data.

information:
i will build my mp3 player project. so the data will be used to generate the PWM to make the sound.
 
 View user's profile Send private message  
Reply with quote Back to top
DO1THL
PostPosted: Nov 10, 2011 - 07:24 AM
Resident


Joined: Aug 29, 2002
Posts: 786
Location: Muenster, Germany

Two options come to mind immediately:

1) ask the guy from which you got the code.
2) have a look at the data sheet to learn how SPI works.

Sorry if i failed to understand your question correctly.
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Nov 10, 2011 - 09:28 AM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62345
Location: (using avr-gcc in) Finchingfield, Essex, England

Quote:

i have program to use SPI to read SDCARD

Tell us more about that. Is it the same FatFs that everyone else here uses or something else? If something else have you considered using FatFs as there's loads of people here who could help you with using it.

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
ibnukoel
PostPosted: Feb 05, 2012 - 12:38 PM
Newbie


Joined: Oct 29, 2011
Posts: 2


Code:

#include <avr/io.h>

#include <avr/iom16.h>

#include <avr/interrupt.h>

 

#define FOSC 6400000

#define DI 6                         // Port B bit 6 (pin7): data in (data from MMC)

#define DT 5                        // Port B bit 5 (pin6): data out (data to MMC)

#define CLK 7                     // Port B bit 7 (pin8): clock

#define CS 4                        // Port B bit 4 (pin5): chip select for MMC

void ini_SPI(void) {

     DDRB &= ~(_BV(DI));                     //input

     DDRB |= _BV(CLK);                     //outputs

     DDRB |= _BV(DT);                     //outputs

     DDRB |= _BV(CS);                     //outputs

     SPCR |= _BV(SPE);                     //SPI enable

     SPCR |= _BV(MSTR);                     //Master SPI mode

     SPCR &= ~(_BV(SPR1));                    //fosc/16

     SPCR |= _BV(SPR0);                    //fosc/16

     SPSR &= ~(_BV(SPI2X));                    //speed is not doubled

     PORTB &= ~(_BV(CS));                     //Enable CS pin for the SD card

}


char SPI_sendchar(char chr) {

     char receivedchar = 0;

     SPDR = chr;

     while(!(SPSR & (1<<SPIF)));

     receivedchar = SPDR;

     return (receivedchar);

}

char Command(char cmd, uint16_t ArgH, uint16_t ArgL, char crc ) {

     SPI_sendchar(0xFF);

     SPI_sendchar(cmd);

     SPI_sendchar((uint8_t)(ArgH >> 8));

     SPI_sendchar((uint8_t)ArgH);

     SPI_sendchar((uint8_t)(ArgL >> 8));

     SPI_sendchar((uint8_t)ArgL);

     SPI_sendchar(crc);

     SPI_sendchar(0xFF);

     return SPI_sendchar(0xFF);                // Returns the last byte received

}

void ini_SD(void) {

     char i;

     PORTB |= _BV(CS);                    //disable CS

     for(i=0; i < 10; i++)

       SPI_sendchar(0xFF);                // Send 10 * 8 = 80 clock pulses 400 kHz

     PORTB &= ~(_BV(CS));                 //enable CS

     for(i=0; i < 2; i++)

       SPI_sendchar(0xFF);                // Send 2 * 8 = 16 clock pulses 400 kHz

     Command(0x40,0,0,0x95);              // reset

idle_no:

     if (Command(0x41,0,0,0xFF) !=0)

     goto idle_no;                        //idle = L?

     SPCR &= ~(_BV(SPR0));                //fosc/4

}

int read(void) {

    int i;

    uint16_t ix;

    char r1 =  Command(0x51,0,512,0xFF);

    for (ix = 0; ix < 50000; ix++) {

        if (r1 == (char)0x00) break;

        r1 = SPI_sendchar(0xFF);

    }

    if (r1 != (char)0x00) {

    return 1;

    }

    //read from the card will start after the framework

    while(SPI_sendchar(0xFF) != (char)0xFE);

    for(i=0; i < 512; i++) {

        while(!(SPSR & (1<<SPIF)));

        chars[i] = SPDR;

        SPDR = SPI_sendchar(0xFF);

    }

    SPI_sendchar(0xFF);

    SPI_sendchar(0xFF);

    return 0;

}

char chars[512];

 

int main(void) {

    ini_SPI();

    ini_SD();

    sei();

   

    read();

 

    return 0;

}


that is the code with CVAVR

thx..
it using fat32 mmc
 
 View user's profile Send private message  
Reply with quote Back to top
haker_fox
PostPosted: Feb 05, 2012 - 11:17 PM
Resident


Joined: Oct 15, 2005
Posts: 530
Location: Russia, Far East Siberia, Irkutsk

ibnukoel wrote:
hey guys.. i have program to use SPI to read SDCARD..

Hello there!
I strongly recommend you to pay attention to FatFS as it's been mentioned earlier.
There are a lot of projects for AVR using the filesystem. It might help you in a short time.
Good luck!
 
 View user's profile Send private message  
Reply with quote Back to top
Display posts from previous:     
Jump to:  
All times are GMT + 1 Hour
Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Powered by PNphpBB2 © 2003-2006 The PNphpBB Group
Credits