Changeset 07109c2 in rtems


Ignore:
Timestamp:
Jun 21, 2010, 2:36:25 PM (9 years ago)
Author:
Sebastian Huber <sebastian.huber@…>
Branches:
4.11, master
Children:
167c6ce
Parents:
c5d8d2dc
Message:

2010-06-21 Arnout Vandecappelle <arnout@…>

PR 1569/misc

  • libchip/i2c/spi-sd-card.c: Added CRC checks.
Location:
c/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • c/src/ChangeLog

    rc5d8d2dc r07109c2  
     12010-06-21      Arnout Vandecappelle <arnout@mind.be>
     2
     3        PR 1569/misc
     4        * libchip/i2c/spi-sd-card.c: Added CRC checks.
     5
    162010-06-15      Joel Sherrill <joel.sherrill@oarcorp.com>
    27
  • c/src/libchip/i2c/spi-sd-card.c

    rc5d8d2dc r07109c2  
    310310
    311311/**
     312 * @name CRC functions
     313 *
     314 * Based on http://en.wikipedia.org/wiki/Computation_of_CRC
     315 *
     316 * @{
     317 */
     318
     319uint8_t sd_card_compute_crc7 (uint8_t *data, size_t len)
     320{
     321        uint8_t e, f, crc;
     322        size_t i;
     323
     324        crc = 0;
     325        for (i = 0; i < len; i++) {
     326                e   = crc ^ data[i];
     327                f   = e ^ (e >> 4) ^ (e >> 7);
     328                crc = (f << 1) ^ (f << 4);
     329        }
     330        return crc >> 1;
     331}
     332
     333uint16_t sd_card_compute_crc16 (uint8_t *data, size_t len)
     334{
     335        uint8_t s, t;
     336        uint16_t crc;
     337        size_t i;
     338
     339        crc = 0;
     340        for (i = 0; i < len; i++) {
     341                s = data[i] ^ (crc >> 8);
     342                t = s ^ (s >> 4);
     343                crc = (crc << 8) ^ t ^ (t << 5) ^ (t << 12);
     344        }
     345        return crc;
     346}
     347
     348/** @} */
     349
     350/**
    312351 * @name Communication Functions
    313352 * @{
     
    355394        };
    356395        int r = 0;
     396        uint8_t crc7;
    357397
    358398        SD_CARD_INVALIDATE_RESPONSE_INDEX( e);
     
    365405        SD_CARD_COMMAND_SET_COMMAND( e->command, command);
    366406        SD_CARD_COMMAND_SET_ARGUMENT( e->command, argument);
     407        crc7 = sd_card_compute_crc7( e->command + 1, 5);
     408        SD_CARD_COMMAND_SET_CRC7( e->command, crc7);
    367409        rv = rtems_libi2c_ioctl( e->bus, RTEMS_LIBI2C_IOCTL_READ_WRITE, &rw);
    368410        RTEMS_CHECK_RV( rv, "Write command and read response");
     
    405447{
    406448        int rv = 0;
     449        uint8_t crc7;
    407450
    408451        rv = sd_card_send_command( e, command, argument);
     
    418461        }
    419462
     463        crc7 = sd_card_compute_crc7( e->response + e->response_index, 5);
     464        if (crc7 != SD_CARD_COMMAND_GET_CRC7( e->response + e->response_index)) {
     465                RTEMS_SYSLOG_ERROR( "CRC check failed on register command\n");
     466                return -RTEMS_IO_ERROR;
     467        }
     468
    420469        *reg = sd_card_get_uint32( e->response + e->response_index + 1);
    421470
     
    471520        /* Data input index */
    472521        int i = 0;
     522
     523        /* CRC check of data */
     524        uint16_t crc16;
    473525
    474526        SD_CARD_INVALIDATE_RESPONSE_INDEX( e);
     
    528580        RTEMS_CHECK_RV( rv, "Read CRC 16");
    529581
     582        crc16 = sd_card_compute_crc16 (in, n);
     583        if ((e->response[0] != ((crc16 >> 8) & 0xff)) ||
     584            (e->response[1] != (crc16 & 0xff))) {
     585                RTEMS_SYSLOG_ERROR( "CRC check failed on read\n");
     586                return -RTEMS_IO_ERROR;
     587        }
     588
    530589        return i;
    531590}
     
    534593{
    535594        int rv = 0;
    536         uint8_t crc16 [2] = { 0, 0 };
     595        uint8_t crc16_bytes [2] = { 0, 0 };
     596        uint16_t crc16;
    537597
    538598        /* Data output index */
     
    552612
    553613        /* Write CRC 16 */
    554         rv = rtems_libi2c_write_bytes( e->bus, crc16, 2);
     614        crc16 = sd_card_compute_crc16(out, n);
     615        crc16_bytes[0] = (crc16>>8) & 0xff;
     616        crc16_bytes[1] = (crc16) & 0xff;
     617        rv = rtems_libi2c_write_bytes( e->bus, crc16_bytes, 2);
    555618        RTEMS_CHECK_RV( rv, "Write CRC 16");
    556619
     
    606669        uint8_t csd_structure = 0;
    607670        uint64_t capacity = 0;
     671        uint8_t crc7;
    608672
    609673        /* Assume first that we have a SD card and not a MMC card */
     
    672736         * voltage is right.  Some MMCs accept this command but will still fail
    673737         * on ACMD41.  SD 1.x cards will fails this command and do not support
    674          * HCS (> 2G capacity).  SD spec requires the correct CRC7 be sent even
    675          * when in SPI mode.  So this will just change the default CRC7 and
    676          * keep it there for all subsequent commands (which just require a do
    677          * not care CRC byte).
     738         * HCS (> 2G capacity).
    678739         */
    679         SD_CARD_COMMAND_SET_CRC7( e->command, 0x43U);
    680740        rv = sd_card_send_register_command( e, SD_CARD_CMD_SEND_IF_COND, if_cond_reg, &if_cond_reg);
    681741
     
    841901                RTEMS_SYSLOG( "Manufacturing date       : %" PRIu8 "\n", SD_CARD_CID_GET_MDT( block));
    842902                RTEMS_SYSLOG( "7-bit CRC checksum       : %" PRIu8 "\n",  SD_CARD_CID_GET_CRC7( block));
     903                crc7 = sd_card_compute_crc7( block, 15);
     904                if (crc7 != SD_CARD_CID_GET_CRC7( block))
     905                        RTEMS_SYSLOG( "  Failed! (computed %02" PRIx8 ")\n", crc7);
    843906        }
    844907
     
    851914        RTEMS_CLEANUP_RV_SC( rv, sc, sd_card_driver_init_cleanup, "Read: SD_CARD_CMD_SEND_CSD");
    852915
     916        crc7 = sd_card_compute_crc7( block, 15);
     917        if (crc7 != SD_CARD_CID_GET_CRC7( block)) {
     918                RTEMS_SYSLOG( "SD_CARD_CMD_SEND_CSD CRC failed\n");
     919                sc = RTEMS_IO_ERROR;
     920                goto sd_card_driver_init_cleanup;
     921        }
     922       
    853923        /* CSD Structure */
    854924        csd_structure = SD_CARD_CSD_GET_CSD_STRUCTURE( block);
Note: See TracChangeset for help on using the changeset viewer.