Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[AVR] problem with mp3 code !!!!!!!!!!

Status
Not open for further replies.

ghoola

Member level 2
Joined
Aug 7, 2013
Messages
43
Helped
0
Reputation
0
Reaction score
0
Trophy points
6
Location
india
Activity points
352
Is there any problem with this code it doesn't work?


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*******************************************************************/
/*          SD diriver for  MP3 Player                             */
/*                                                                 */
/* Platform   : AVRStudio4.13 b528 + WinAVR20070525                */
/*              optimize -0s                                       */
/* Author     : bozai(Zhang Qibo)                                  */
/* E-mail     : [email]sudazqb@163.com[/email]                                    */
/* MSN        : [email]zhangqibo_1985@hotmail.com[/email]                         */
/* Date       : 2006-06-16                                         */
/*******************************************************************/
/*  2007-06-16: After reading the spec. in detail, I found some    */
/*              of the code don't meet the spec., that is after    */
/*              the last SPI transaction, it need an extra 8 CLK   */
/*              to finish it's work                                */
/*  2007-05-04: add read capacity function                         */
/*  2007-04-21:                                                    */
/*  Enable some code incase that when SD reset                     */
/*  faild program can't jump the loop                              */
/*******************************************************************/
 
 
 
 
#include <avr/io.h>
#include "MMC_SD.h"
 
  //spi low speed
void SPI_Low(void)
{
    SPCR =   _BV(SPE)|_BV(MSTR)|_BV(SPR1)|_BV(SPR0);
    SPSR &= ~_BV(SPI2X);
}
 
    //spi full speed
void SPI_High(void)
{
    SPCR =  _BV(SPE)|_BV(MSTR);
    SPSR |= _BV(SPI2X);
}
 
 //port initialize
void SPI_Init(void)
{
    DDR_INI();
    SPI_Low();
}
 
        //read and write one byte
uint8 SPI_WriteByte(uint8 val)
{
    SPDR = val;
    while(!(SPSR & _BV(SPIF)));
    return SPDR;
}
 
uint8 SPI_ReadByte(void)
{
    SPDR = 0xff;
    while(!(SPSR & _BV(SPIF)));
    return SPDR;
}
        //sd card initialize
void MMC_SD_Init(void)
{
    SPI_Init();
    SPI_CS_Deassert();
}
 
    //sd send command
uint8 MMC_SD_SendCommand(uint8 cmd, uint32 arg)
{
    uint8 r1;
    uint8 retry=0;
    
    SPI_WriteByte(0xff);
    SPI_CS_Assert();
    
    SPI_WriteByte(cmd | 0x40);  //send command
    SPI_WriteByte(arg>>24);
    SPI_WriteByte(arg>>16);
    SPI_WriteByte(arg>>8);
    SPI_WriteByte(arg);
    SPI_WriteByte(0x95);
    
    while((r1 = SPI_WriteByte(0xff)) == 0xff)   //wait response
        if(retry++ > 20) break;                 //time out error
 
    SPI_CS_Deassert();
    SPI_WriteByte(0xff);                // extra 8 CLK
 
    return r1;              //return state
}
 
    //reset sd card (software)
uint8 MMC_SD_Reset(void)
{
    uint8 i;
    uint8 retry;
    uint8 r1=0;
    retry = 0;
    SPI_Low();
    do
    {
        for(i=0;i<10;i++) SPI_WriteByte(0xff);
        r1 = MMC_SD_SendCommand(0, 0);  //send idle command
        retry++;
        if(retry>100) return 1;     //time out
    } while(r1 != 0x01);    
 
 
    retry = 0;
    do
    {
        r1 = MMC_SD_SendCommand(1, 0);  //send active command
        retry++;
        if(retry>254) return 1;     //time out
    } while(r1);
    SPI_High();
    r1 = MMC_SD_SendCommand(59, 0); //disable CRC
 
    r1 = MMC_SD_SendCommand(16, 512);   //set sector size to 512
    return 0;   //normal return
}
 
    //read one sector
uint8 MMC_SD_ReadSingleBlock(uint32 sector, uint8* buffer)
{
    uint8 r1;
    uint16 i;
    uint16 retry=0;
 
    r1 = MMC_SD_SendCommand(17, sector<<9); //read command
    
    if(r1 != 0x00)
        return r1;
 
    SPI_CS_Assert();
        //wait to start recieve data
    while(SPI_WriteByte(0xff) != 0xfe)if(retry++ > 1000){SPI_CS_Deassert();return 1;}
 
    for(i=0; i<512; i++)    //read 512 bytes
    {
        *buffer++ = SPI_WriteByte(0xff);
    }
 
    SPI_WriteByte(0xff);//αcrc    //dummy crc
    SPI_WriteByte(0xff);
    
    SPI_CS_Deassert();
    SPI_WriteByte(0xff);// extra 8 CLK
 
    return 0;
}
 
 
    //wirite one sector //not used in this application
uint8 MMC_SD_WriteSingleBlock(uint32 sector, uint8* buffer)
{
    uint8 r1;
    uint16 i;
    uint16 retry=0;
 
    r1 = MMC_SD_SendCommand(24, sector<<9); //send command
    if(r1 != 0x00)
        return r1;
 
    SPI_CS_Assert();
    
    SPI_WriteByte(0xff);
    SPI_WriteByte(0xff);
    SPI_WriteByte(0xff);
 
    SPI_WriteByte(0xfe);            //send start byte "token"
    
    for(i=0; i<512; i++)        //send 512 bytes data
    {
        SPI_WriteByte(*buffer++);
    }
    
    SPI_WriteByte(0xff);            //dummy crc
    SPI_WriteByte(0xff);
    
    r1 = SPI_WriteByte(0xff);
    
    if( (r1&0x1f) != 0x05)  //judge if it successful
    {
        SPI_CS_Deassert();
        return r1;
    }
        //wait no busy
    while(!SPI_WriteByte(0xff))if(retry++ > 2000){SPI_CS_Deassert();return 1;}
 
    SPI_CS_Deassert();
    SPI_WriteByte(0xff);// extra 8 CLK
 
    return 0;
}
 
 
 
uint32 MMC_SD_ReadCapacity()
{
    uint8 r1;
    uint16 i;
    uint16 temp;
    uint8 buffer[16];
    uint32 Capacity;
    //uint8 retry=0;
 
    r1 = MMC_SD_SendCommand(9, 0);  //send command  //READ CSD
    if(r1 != 0x00)
        return r1;
 
    SPI_CS_Assert();
    while(SPI_WriteByte(0xff) != 0xfe);
    
    for(i=0;i<16;i++)
    {
        buffer[i]=SPI_WriteByte(0xff);
    }   
 
    SPI_WriteByte(0xff);
    SPI_WriteByte(0xff);
    
    SPI_WriteByte(0xff);
    
    SPI_CS_Deassert();
 
    SPI_WriteByte(0xff);// extra 8 CLK
 
/*********************************/
//  C_SIZE
    i = buffer[6]&0x03;
    i<<=8;
    i += buffer[7];
    i<<=2;
    i += ((buffer[8]&0xc0)>>6);
 
/**********************************/
//  C_SIZE_MULT
 
    r1 = buffer[9]&0x03;
    r1<<=1;
    r1 += ((buffer[10]&0x80)>>7);
 
 
/**********************************/
// BLOCKNR
 
    r1+=2;
 
    temp = 1;
    while(r1)
    {
        temp*=2;
        r1--;
    }
    
    Capacity = ((uint32)(i+1))*((uint32)temp);
 
/////////////////////////
// READ_BL_LEN
 
    i = buffer[5]&0x0f;
 
/*************************/
//BLOCK_LEN
 
    temp = 1;
    while(i)
    {
        temp*=2;
        i--;
    }
/************************/
 
 
/************** formula of the capacity ******************/
//
//  memory capacity = BLOCKNR * BLOCK_LEN
//  
//  BLOCKNR = (C_SIZE + 1)* MULT
//
//           C_SIZE_MULT+2
//  MULT = 2
//
//               READ_BL_LEN
//  BLOCK_LEN = 2
/**********************************************/
 
//The final result
    
    Capacity *= (uint32)temp;    
    return Capacity;        
}

 
Last edited by a moderator:

From reading the program,I get that you are using serial peripheral interface(SPI) to access a audio file in an SD card..
But specifically,what problem is occurring?Is the code not getting compiled/even if it is getting compiled,the system is behaving differently,not as it should behave..?
Also can you upload the circuit diagram of the system?
 

the diagram is
123.png
 

It is good that you uploaded the circuit diagram...and you are saying..
Is there any problem with this code it doesn't work?
How..do you know that the code is not working?What exactly is the problem you are facing?please specify.....
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top