Forum: Mikrocontroller und Digitale Elektronik SD Karte - CRC Berechnung


von Konstantin R. (kribel)


Lesenswert?

HI Leute!

Ich habe ein bisschen Probleme mit der Checksummenberechnung der 
SD/MMC-Speicherkarten.

Das Generatorpolynom ist: G(x) = x^7 + x^3 + 1
M(x) ist die zu übertragende Nachricht.

CRC = Rest( ( M(x) * x^7) / G(x) )

So steht es im HITACHI-Datenblatt.

Sendet man nun CMD0 an die Speicherkarte hängt man als CRC 1001010 an. 
Warum?

Eigentlich müsste es doch 0 sein, oder?

Und kann es sein, dass das Generatorpolynom falsch ist? Müsste es 
eigentlich nicht G(x) = x^6 + x^3 + x^2 + 1 heißen?

Gruß
Konstantin

von Obelix (Gast)


Lesenswert?

Das geht alles auch ohne CRC-Berechnung. Wenn du es doch mit machen 
möchtest, schaun noch ein weiters mal ins Datenblatt, oder nimm dir ein 
anderes Datenblatt zur Hand, oder schau dir die 100000 von Beispielen im 
Internet an.

von Obelix (Gast)


Lesenswert?

Noch was konstruktives:
1. Schau dir CMD59 an.
2. G(x) = x^7 + x^3 + 1 ist richtig.
3. Da CMD0 eine Konstante ist, kannst du als CRC auch eine Konstante 
nehmen, also brauchst du die CRC nicht berechnen.

von Obelix (Gast)


Lesenswert?

Noch was.

Die CRC ist 7 Bit lang. Diese 7 Bit werden in Bit 7 bis Bit 1 
gespeichert und Bit 0 ist immer 1. Steht aber alles im Datenblatt.

von Konstantin R. (kribel)


Lesenswert?

OK Danke.

Folgendes noch. Im Datenblatt steht:

M(x) = (first bit) * x^n + (second bit) * x^(n-1) + ... + (last bit) * 
x^0

müsste es nicht so heißen?:

M(x) = (last bit) * x^n + ... + (second bit) * x^1 + (first bit) * x^0

Gruß
Konstantin

von Hagen R. (hagen)


Lesenswert?

hier mal den Code den ich benutze
1
#ifdef MMC_CALC_CRC7
2
uint8_t mmcCRC7(uint8_t crc, uint8_t data) {
3
4
    crc ^= data;
5
    unsigned char poly = 0x89;
6
    for (unsigned char i = 8; i > 0; i--) {
7
      if ((crc & 0x80) != 0) crc ^= poly;
8
      crc <<= 1;
9
    }
10
    return(crc);
11
}
12
#endif

und hier in der Anwendung beim Senden eines Kommandos
1
uint8_t mmcCommandParam(uint8_t command, uint32_t address, uint16_t expected) {
2
3
    mmcDisable();
4
    mmcSend(data);
5
6
    uint8_t crc = 0;
7
#ifdef MMC_CALC_CRC7
8
    crc = mmcCRC7(crc, command);
9
    crc = mmcCRC7(crc, (uint8_t)(address >> 24));
10
    crc = mmcCRC7(crc, (uint8_t)(address >> 16));
11
    crc = mmcCRC7(crc, (uint8_t)(address >> 8));
12
    crc = mmcCRC7(crc, (uint8_t)(address));
13
    crc = crc | 1;
14
#else
15
    crc = 0x95;
16
#endif
17
18
    uint8_t result;
19
    uint8_t data = 0xFF;
20
21
    mmcWait();
22
    mmcEnable();
23
    for (uint8_t i = MMC_COMMAND_TRIALS; i > 0; i--) {
24
      mmcSend(command);
25
      mmcWait();
26
      mmcSend((uint8_t)(address >> 24));
27
      mmcWait();
28
      mmcSend((uint8_t)(address >> 16));
29
      mmcWait();
30
      mmcSend((uint8_t)(address >> 8));
31
      mmcWait();
32
      mmcSend((uint8_t)(address));
33
      mmcWait();
34
      mmcSend(crc);
35
      mmcWait();
36
37
      for (uint8_t j = MMC_RESPONSE_TRIALS; j > 0; j--) {
38
        mmcSend(data);
39
        mmcWait();
40
        result = mmcRead();
41
        if ((result & 0x80) == 0) break;
42
      }
43
44
      if ((result >= (uint8_t)(expected >> 8)) && (result <= (uint8_t)(expected))) break;
45
46
      mmcDisable();
47
      mmcSend(data);
48
      for (uint8_t j = MMC_COMMAND_DELAY -1; j > 0; j--) {
49
        mmcWait();
50
        mmcSend(data);
51
      }
52
      mmcWait();
53
      mmcEnable();
54
    }
55
    return(result);
56
}


Gruß Hagen

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.