-
Notifications
You must be signed in to change notification settings - Fork 4
/
mmc.c
355 lines (290 loc) · 8.63 KB
/
mmc.c
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*! \file mmc.c \brief MultiMedia and SD Flash Card Interface. */
//*****************************************************************************
//
// File Name : 'mmc.c'
// Title : MultiMedia and SD Flash Card Interface
// Author : Pascal Stang - Copyright (C) 2004
// Created : 2004.09.22
// Revised : 2004.09.22
// Version : 0.1
// Target MCU : Atmel AVR Series
// Editor Tabs : 4
//
// NOTE: This code is currently below version 1.0, and therefore is considered
// to be lacking in some functionality or documentation, or may not be fully
// tested. Nonetheless, you can expect most functions to work.
//
// ----------------------------------------------------------------------------
// 17.8.2008
// Bob!k & Raster, C.P.U.
// Original code was modified especially for the SDrive device.
// Some parts of code have been added, removed, rewrited or optimized due to
// lack of MCU AVR Atmega8 memory.
// ----------------------------------------------------------------------------
//
// This code is distributed under the GNU Public License
// which can be found at http://www.gnu.org/licenses/gpl.txt
//
//*****************************************************************************
//----- Include Files ---------------------------------------------------------
#include <avr/io.h> // include I/O definitions (port names, pin names, etc)
#include <avr/signal.h> // include "signal" names (interrupt names)
#include <avr/interrupt.h> // include interrupt support
#include "avrlibdefs.h" // global AVRLIB defines
#include "avrlibtypes.h" // global AVRLIB types definitions
#include "spi.h" // include spi bus support
//#define spiTransferTwoFF() { spiTransferFF(); spiTransferFF(); }
#include "mmc.h"
// include project-specific hardware configuration
#include "mmcconf.h"
extern unsigned char mmc_sector_buffer[512];
void mmcInit(void)
{
// initialize SPI interface
spiInit();
// release chip select
sbi(MMC_CS_DDR, MMC_CS_PIN);
sbi(MMC_CS_PORT,MMC_CS_PIN);
}
extern u32 n_actual_mmc_sector;
extern u08 n_actual_mmc_sector_needswrite;
u08 SDHC;
u08 mmcReset(void)
{
u16 retry;
u08 r1;
u08 i;
n_actual_mmc_sector=0xFFFFFFFF; //!!! pridano dovnitr
n_actual_mmc_sector_needswrite=0;
SDHC=0;
//
i=0xff; //255x
do { spiTransferFF(); i--; } while(i); //255x
//
r1 = mmcSendCommand(MMC_GO_IDLE_STATE, 0);
if (r1!=1) return -1;
// tady by mela byt asi dana smycka, ale mame malo mista ve flash, tak verime, ze jednou staci (popr. rucne reset)
// retry=0xffff; //65535x
// do
{
//r1 = mmcSendCommand(MMC_SEND_IF_COND, 0x1aa); // nastav 2.7-3.6V + 0xAA echo pattern
//set_display(0);
//r1 = mmcCommandToBufferResponse(MMC_SEND_IF_COND, 0x1aa, 4);
mmcCommandToBufferResponse(MMC_SEND_IF_COND, 0x1aa, 4);
/*
if (r1==0) //spravne vraci karta 1! protoze je stale v init rezimu 120911
{
set_display(3);
SendToUART();
// nasledujici odpoved ignorujeme. pokud se s nama karta nebude chtit bavit,
//tak se nebude a vysledek je na stejno => nepouzitelna karta (karta se tim chrani pri spatnem rozsahu)
// kvuli mistu pocitame s tim, ze se tyto 4 byty doctou az pripadne na zacatku dalsiho mmcSendCommand
spiTransferFF(); // dohromady 5bytu, prvni je precten uz v mmcSendCommand
spiTransferFF(); // voltage
spiTransferFF(); // echo back pattern
spiTransferFF(); //crc
// break;
}
*/
// retry--;
}
// while(retry);
//set_display(4);
// mmcSendCommand(MMC_CRC_ON_OFF, 0);
retry=0xffff; //65535x
do
{
r1 = mmcSendCommand(55, 0);
if (r1!=1) break; //MMC // 120911 opravdu ma vracet 1, kdyz vsechno ok
//if (r1==5) break; //MMC <--- kontrolovat takhle rekl Bob 27.6.2008
//Strana 120 ve specifikaci.
//bit 0 - in idle state, bit 2 - illegal command
r1 = mmcSendCommand(41, (0x40000000)); // umime i SDHC/SDXC
if (r1==0) // 120911 dokud neni r1 nulove, tak znovu cely init (CMD55 i CMD41)
{
// set_display(1);
//r1 = mmcSendCommand(58, (0x40000000)); // kontrola, zda karta je/neni SDHC/SDXC (potreba kvuli
// adresaci bytu/sektoru u MMC_READ_SINGLE_BLOCK a MMC_WRITE_SINGLE_BLOCK
//r1 = mmcSendCommand(9, (0x0)); // kontrola, zda karta je/neni SDHC/SDXC (potreba kvuli
r1 = mmcCommandToBufferResponse(58,0,5);
if(r1 == 0) // if command succeeded
{
if((mmc_sector_buffer[1]&0xC0)==0xC0)
SDHC = 1;
else
SDHC = 0;
}
else
{
return -1; //unknown;
}
break; //SD/SDHC
}
// else
// {
// Delay1000us();
// }
retry--;
}
while(retry);
// initializing card for operation
r1 = mmcSendCommand(MMC_SEND_OP_COND, 0);
// SDkarty vraceji 0
// MMCkarta vraci 1
//if (r1>=2) return -1; //problem (MMC taky nefunguje)
if (r1!=0) return -1;
//kdyz jsem to tady nechal jit s MMC kartou dal (s r1=1)
//tak to na nekterem z dalsich povelu zustalo zasekle
//a po vytazeni MMC a vlozeni jine SD se muset mackat Reset :-(
/*
if( r1!=0 && r1!=5)
{
u08 v;
v=inb(PINC)&0xf0;
v|=(0x0f ^ (r1 & 0x0f) );
PORTC=v;
while(1); //nekonecna smycka
return -1;
}
*/
// turn off CRC checking to simplify communication
//r1 =
mmcSendCommand(MMC_CRC_ON_OFF, 0);
// set block length to 512 bytes
//r1 =
mmcSendCommand(MMC_SET_BLOCKLEN, 512);
// return success
return 0;
}
u08 mmcSendCommand(u08 cmd, u32 arg)
{
u08 r1;
// assert chip select
cbi(MMC_CS_PORT,MMC_CS_PIN);
//
//spiTransferFF(); //pribude 34 bajtu kodu (!)
// issue the command
r1 = mmcCommand(cmd, arg);
//
//spiTransferFF();
// release chip select
sbi(MMC_CS_PORT,MMC_CS_PIN);
return r1;
}
u08 mmcRead(u32 sector)
{
u08 r1;
u16 i;
u08 *buffer=mmc_sector_buffer; //natvrdo!
// assert chip select
cbi(MMC_CS_PORT,MMC_CS_PIN);
// issue command
r1 = mmcCommand(MMC_READ_SINGLE_BLOCK, (SDHC)?sector:(sector<<9));
// check for valid response
if(r1 != 0x00) return r1;
// wait for block start
while(spiTransferFF() != MMC_STARTBLOCK_READ);
//zacatek bloku
//nacti 512 bytu
i=0x200; //512
do { *buffer++ = spiTransferFF(); i--; } while(i);
// read 16-bit CRC
//2x FF:
spiTransferFF();
spiTransferFF();
//// + 1x FF navic pred releasnutim chip selectu
//spiTransferFF();
//
// release chip select
sbi(MMC_CS_PORT,MMC_CS_PIN);
//
return 0; //success
}
u08 mmcWrite(u32 sector)
{
u08 r1;
u16 i;
u08 *buffer=mmc_sector_buffer; //natvrdo!
// assert chip select
cbi(MMC_CS_PORT,MMC_CS_PIN);
// issue command
r1 = mmcCommand(MMC_WRITE_BLOCK, (SDHC)?sector:(sector<<9));
// check for valid response
if(r1 != 0x00)
return r1;
// send dummy
spiTransferFF();
// send data start token
spiTransferByte(MMC_STARTBLOCK_WRITE);
// write data (512 bytes)
i=0x200;
do { spiTransferByte(*buffer++); i--; } while(i);
// write 16-bit CRC (dummy values)
//2x FF:
spiTransferFF();
spiTransferFF();
// read data response token
r1 = spiTransferFF();
if( (r1&MMC_DR_MASK) != MMC_DR_ACCEPT)
return r1;
// wait until card not busy
while(!spiTransferFF());
// release chip select
sbi(MMC_CS_PORT,MMC_CS_PIN);
// return success
return 0;
}
u08 mmcCommand(u08 cmd, u32 arg)
{
u08 r1;
u08 retry=0xff; //255x
//
spiTransferFF();
spiTransferFF(); //pridano navic! 27.6.2008 doporucil Bob!k
spiTransferFF(); //pridano navic! 27.6.2008 doporucil Bob!k
spiTransferFF(); //pridano navic! 27.6.2008 doporucil Bob!k
// send command
spiTransferByte(cmd | 0x40);
//
spiTransferByte(arg>>24);
spiTransferByte(arg>>16);
spiTransferByte(arg>>8);
spiTransferByte(arg);
//Alternativni zpusob pres ((u08*)&arg)[] je delsi.
if(cmd==MMC_GO_IDLE_STATE)
spiTransferByte(0x95); // crc valid only for MMC_GO_IDLE_STATE
else
spiTransferByte(0x86); // crc valid for MMC_SEND_IF_COND
// end command
// wait for response
// if more than 255 retries, card has timed-out
// return the received 0xFF
do
{
r1 = spiTransferFF();
retry--;
}
//while(retry && (r1 == 0xFF) );
while(retry && (r1 & 0x80) ); // valid answer to command has 7bit null => must be less 0x80
// return response
return r1;
}
u08 mmcCommandToBufferResponse(u08 cmd, u32 arg, u08 len)
{
u08 r1;
u16 i;
u08 *buffer=mmc_sector_buffer; //natvrdo!
// assert chip select
cbi(MMC_CS_PORT,MMC_CS_PIN);
// issue command
r1 = mmcCommand(cmd, arg);
*buffer++ = r1;
//nacti 'len' bytu
i=len; //0x200; //512
do { *buffer++ = spiTransferFF(); i--; } while(i);
// release chip select
sbi(MMC_CS_PORT,MMC_CS_PIN);
//
//return 0; //success
return mmc_sector_buffer[0];
}