forked from kbr-net/sdrive-max
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmmc.c
380 lines (334 loc) · 9.17 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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*! \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.
// ----------------------------------------------------------------------------
// 03.05.2014
// enhanced by kbr
//
// 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
#include "global.h"
#include "mmc.h"
#include "fat.h"
#include "display.h"
// include project-specific hardware configuration
#include "mmcconf.h"
u32 n_actual_mmc_sector;
u08 n_actual_mmc_sector_needswrite;
extern unsigned char mmc_sector_buffer[512];
struct flags SDFlags;
unsigned char crc7 (unsigned char crc, unsigned char *pc, unsigned int len) {
//unsigned int i;
unsigned char ibit;
unsigned char c;
//for (i = 0; i < len; i++, pc++) {
while (len--) { // reverse byteorder!
c = *(pc+len);
for (ibit = 0; ibit < 8; ibit++) {
crc <<= 1;
if ((c ^ crc) & 0x80)
crc ^= 0x09;
c <<= 1;
}
crc &= 0x7F;
}
return crc;
}
void mmcInit(void)
{
// initialize SPI interface
spiInit();
//// allready done in spiInit()
// release chip select
//sbi(MMC_CS_PORT,MMC_CS_PIN); //high
}
u08 mmcReset(void)
{
u16 retry;
u08 r1;
u08 i;
u08 *buffer=mmc_sector_buffer;
u08 *b=mmc_sector_buffer;
n_actual_mmc_sector=0xFFFFFFFF; //!!! pridano dovnitr
n_actual_mmc_sector_needswrite=0;
SDFlags.SDHC = 0; // reset SDHC-Flag
//
//i=0xff; //255x
i=10; //10x8bit=80clocks (74 lt. Spec.)
do { spiTransferFF(); i--; } while(i);
//
i=10; //try more times, a Kingston 8GB SDHC returns 0x3f at first time
do {
r1 = mmcSendCommand(MMC_GO_IDLE_STATE, 0); // CMD0
if(r1 == 1) break;
}
while(--i);
//if (r1!=1) {
if (!i) {
*b = r1; // debug only
return 1;
}
i=10; //Transcent 32GB needs a bit more tries on first start
do {
cbi(MMC_CS_PORT,MMC_CS_PIN);
r1 = mmcCommand(MMC_SEND_IF_COND, 0b000110101010); // CMD8
if (r1==1) { // get the response
i=4;
do { *b++=spiTransferFF(); i--; } while(i);
// check return values
if (buffer[2] != 1 || buffer[3] != 0xaa) r1 = 6; // fake error
}
sbi(MMC_CS_PORT,MMC_CS_PIN);
spiTransferFF(); // send 8 clocks at end
//exit on correct response or invalid command
if(r1 == 1 || r1 == 5) break;
} while(i--);
if (r1>5) {
*b = r1; // debug only
return(2);
}
buffer += 4; // inc the buffer for next cmd
b = buffer;
// sundisk 64MB braucht 48 Zyklen
retry=0x3ff; // give card time to init
do
{
r1 = mmcSendCommand(MMC_APP_CMD, 0); // CMD55(APP_CMD)
if (r1>1) break; //MMC
//bit 0 - in idle state, bit 2 - illegal command
r1 = mmcSendCommand(MMC_SD_SEND_OP_COND, 1L<<30); // 0x29(ACMD41) HCS-bit set
if (r1==0) { //SD
cbi(MMC_CS_PORT,MMC_CS_PIN);
r1 = mmcCommand(MMC_READ_OCR, 0); // CMD58
i=4;
do { *b++=spiTransferFF(); i--; } while(i);
sbi(MMC_CS_PORT,MMC_CS_PIN);
spiTransferFF(); // send 8 clocks at end
//mark SDHC
SDFlags.SDHC = (buffer[0] & 0x40) ? 1 : 0;
break; //and out
}
retry--;
}
while(retry);
//return(r1); //only for testing
//return(0xff - retry); //only for testing
if(retry == 0) return(3);
if(r1 == 5) { // try for MMC
r1 = mmcSendCommand(MMC_SEND_OP_COND, 0); // CMD1
if (r1!=0) return 4;
}
//// now we have a crc routine \o/
// turn off CRC checking to simplify communication
//r1 =
//mmcSendCommand(MMC_CRC_ON_OFF, 0); // CMD59
// set block length to 512 bytes
//r1 =
r1 = mmcSendCommand(MMC_SET_BLOCKLEN, 512); // CMD16
if (r1!=0) return 5;
// return success
return 0;
}
u08 mmcSendCommand(u08 cmd, u32 arg)
{
u08 r1;
// assert chip select
cbi(MMC_CS_PORT,MMC_CS_PIN);
// issue the command
r1 = mmcCommand(cmd, arg);
// release chip select
sbi(MMC_CS_PORT,MMC_CS_PIN);
spiTransferFF(); // send 8 clocks at end
return r1;
}
u08 mmcRead(u32 sector)
{
u08 r1;
u16 i;
u08 *buffer=mmc_sector_buffer; //natvrdo!
//too expensive for atx support!
//Draw_Circle(15,5,3,1,Green);
// assert chip select
cbi(MMC_CS_PORT,MMC_CS_PIN);
// issue command
if (!SDFlags.SDHC) sector<<=9;
r1 = mmcCommand(MMC_READ_SINGLE_BLOCK, sector);
// 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();
// release chip select
sbi(MMC_CS_PORT,MMC_CS_PIN);
spiTransferFF(); // send 8 clocks at end
//
//Draw_Circle(15,5,3,1,Black);
return 0; //success
}
u08 mmcWrite(u32 sector)
{
u08 r1;
u16 i;
u08 *buffer=mmc_sector_buffer; //natvrdo!
//LED_RED_ON; //TODO
//Draw_Circle(15,5,3,1,Red);
// assert chip select
cbi(MMC_CS_PORT,MMC_CS_PIN);
// issue command
if (!SDFlags.SDHC) sector<<=9;
r1 = mmcCommand(MMC_WRITE_BLOCK, sector);
// 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);
spiTransferFF(); // send 8 clocks at end
//LED_RED_OFF; //TODO
//Draw_Circle(15,5,3,1,Black);
// return success
return 0;
}
u08 mmcCommand(u08 cmd, u32 arg)
{
u08 r1;
u08 retry=0xff; //255x
unsigned char crc;
//
/* what?
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
*/
cmd |= 0x40;
// send command
spiTransferByte(cmd);
//
spiTransferByte(arg>>24);
spiTransferByte(arg>>16);
spiTransferByte(arg>>8);
spiTransferByte(arg);
//Alternativni zpusob pres ((u08*)&arg)[] je delsi.
//spiTransferByte(0x95); // crc valid only for MMC_GO_IDLE_STATE
// calculate crc
crc = crc7(0, &cmd, 1);
crc = crc7(crc, (u08*) &arg, 4)<<1 | 0x01;
// and send it
spiTransferByte(crc);
// 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) );
// return response
return r1;
}
u08 mmcReadCached(u32 sector)
{
if(sector==n_actual_mmc_sector) return(-1);
u08 ret,retry;
//save cache before read another sector
mmcWriteCachedFlush();
//from now on
retry=0; //maximal 256x tries
do
{
ret = mmcRead(sector); //returns 0 if ok
retry--;
} while (ret && retry);
if(ret) // exit on error
return(-1);
n_actual_mmc_sector=sector;
return(0);
}
u08 mmcWriteCached(unsigned char force)
{
//if ( get_readonly() ) return 0xff; //zakazany zapis
//LED_RED_ON; //signal cache is not written yet
Draw_Circle(15,5,3,1,Red);
if (force)
{
u08 ret,retry;
retry=16; //maximal 16x tries
do
{
ret = mmcWrite(n_actual_mmc_sector); //returns 0 if ok
retry--;
} while (ret && retry);
while(ret); //and if it did not work, SDrive blocks it!
n_actual_mmc_sector_needswrite = 0;
//LED_RED_OFF;
Draw_Circle(15,5,3,1,Black);
}
else
{
n_actual_mmc_sector_needswrite=1;
}
return 0; //return 0 if ok
}
void mmcWriteCachedFlush()
{
if (n_actual_mmc_sector_needswrite)
{
while (mmcWriteCached(1)); //if it's forbidden to write, SDrive blocks it!
//until the write is not allowed (you are hanging something in the cache)
}
}