X-Git-Url: https://codewiz.org/gitweb?a=blobdiff_plain;f=bertos%2Fdrv%2Fsd.c;h=c2e412e5bbc5b270314c6f774e69bd98e0ce6cda;hb=53716ae692a6352c1b230a568f4dbb2a779bda85;hp=e4f9c17f4da9b8348a67443e2a1eac88d2ca33f4;hpb=9ef4a00f57e1fa3cb605317df9cb617b976a1a2d;p=bertos.git diff --git a/bertos/drv/sd.c b/bertos/drv/sd.c index e4f9c17f..c2e412e5 100644 --- a/bertos/drv/sd.c +++ b/bertos/drv/sd.c @@ -413,7 +413,7 @@ static const KBlockVTable sd_buffered_vt = #define SD_SEND_OP_COND_CRC 0xF9 #define SD_START_DELAY 10 -#define SD_INIT_TIMEOUT 1000 +#define SD_INIT_TIMEOUT ms_to_ticks(2000) #define SD_IDLE_RETRIES 4 static bool sd_blockInit(Sd *sd, KFile *ch) @@ -500,7 +500,7 @@ static bool sd_blockInit(Sd *sd, KFile *ch) return true; } -#ifdef CPU_CM3_SAM3X8 +#if CPU_CM3_SAM3X8 #include @@ -574,6 +574,8 @@ static bool sd_blockInit(Sd *sd, KFile *ch) }) +#define SD_ADDR_TO_RCA(addr) (uint32_t)(((addr) << 16) & 0xFFFF0000) + #define BCD_TO_INT_32BIT(bcd) ((uint32_t )((bcd) & 0xf) * 1 + \ (((bcd) >> 4) & 0xf) * 10 + \ (((bcd) >> 8) & 0xf) * 100 + \ @@ -583,18 +585,29 @@ static bool sd_blockInit(Sd *sd, KFile *ch) (((bcd) >> 24) & 0xf) * 1000000 + \ (((bcd) >> 28) & 0xf) * 10000000) \ - -static void dump(uint32_t *r, size_t len) +LOG_INFOB( +static void dump(const char *label, uint32_t *r, size_t len) { ASSERT(r); - kputs("r [ "); - for (size_t i = 0; i < len; i++) - kprintf("%lx ", r[i]); - kputs("]\n"); + size_t i; + int j = 0; + kprintf("\n%s [\n", label); + for (i = 0; i < len; i++) + { + if (j == 5) + { + kputs("\n"); + j = 0; + } + kprintf("%08lx ", r[i]); + j++; + } + kprintf("\n] len=%d\n\n", i); } +) -int sd_decode_csd(SDcsd *csd, uint32_t *resp, size_t len) +static int sd_decodeCsd(SDcsd *csd, uint32_t *resp, size_t len) { ASSERT(csd); ASSERT(resp); @@ -603,12 +616,19 @@ int sd_decode_csd(SDcsd *csd, uint32_t *resp, size_t len) csd->structure = UNSTUFF_BITS(resp, 126, 2); csd->ccc = UNSTUFF_BITS(resp, 84, 12); + + /* + * CSD structure: + * - 0: + * - Version 1.01-1.10 + * - Version 2.00/Standard Capacity + * - 1: + * - Version 2.00/High Capacity + * - >1: not defined. + */ + if (csd->structure == 0) { - kprintf("csize %ld\n", UNSTUFF_BITS(resp, 62, 12)); - kprintf("csize_mul %ld\n", UNSTUFF_BITS(resp, 47, 3)); - kprintf("bit read %ld %ld\n", csd->read_blk_bits = UNSTUFF_BITS(resp, 80, 4), csd->read_blk_bits); - // (C_size + 1) x 2^(C_SIZE_MUL+2) csd->blk_num = (1 + UNSTUFF_BITS(resp, 62, 12)) << (UNSTUFF_BITS(resp, 47, 3) + 2); @@ -665,74 +685,54 @@ int sd_decode_csd(SDcsd *csd, uint32_t *resp, size_t len) } -void sd_dump_csd(SDcsd *csd) +void sd_dumpCsd(Sd *sd) { - ASSERT(csd); + ASSERT(sd); - kprintf("VERSION: %d.0\n", csd->structure ? 2 : 1); - kprintf("CARD COMMAND CLASS: %d\n", csd->ccc); - kprintf("WRITE BLK LEN BITS: %ld\n", csd->write_blk_bits); - kprintf("READ BLK LEN BITS: %ld\n", csd->read_blk_bits); - kprintf("ERASE SIZE: %ld\n", csd->erase_size); - kprintf("BLK NUM: %ld\n", csd->blk_num); - kprintf("BLK LEN: %ld\n", csd->blk_len); - kprintf("CAPACITY %ld\n", csd->capacity); - kprintf("FLAG Write: WP %d, W MISALIGN %d\n", csd->write_partial, csd->write_misalign); - kprintf("FLAG Read: RP %d, R MISALIGN %d\n", csd->read_partial, csd->read_misalign); + LOG_INFO("VERSION: %d.0\n", sd->csd.structure ? 2 : 1); + LOG_INFO("CARD COMMAND CLASS: %d\n", sd->csd.ccc); + LOG_INFO("WRITE BLK LEN BITS: %ld\n", sd->csd.write_blk_bits); + LOG_INFO("READ BLK LEN BITS: %ld\n", sd->csd.read_blk_bits); + LOG_INFO("ERASE SIZE: %ld\n", sd->csd.erase_size); + LOG_INFO("BLK NUM: %ld\n", sd->csd.blk_num); + LOG_INFO("BLK LEN: %ld\n", sd->csd.blk_len); + LOG_INFO("CAPACITY %ld\n", sd->csd.capacity); + LOG_INFO("FLAG Write: WP %d, W MISALIGN %d\n", sd->csd.write_partial, sd->csd.write_misalign); + LOG_INFO("FLAG Read: RP %d, R MISALIGN %d\n", sd->csd.read_partial, sd->csd.read_misalign); } - -void sd_decode_cid(SDcid *cid, uint32_t *resp, size_t len) +void sd_dumpCid(Sd *sd) { - ASSERT(cid); - ASSERT(resp); - ASSERT(len >= 4); - - cid->manfid = UNSTUFF_BITS(resp, 120, 8); - cid->oemid = UNSTUFF_BITS(resp, 104, 16); - cid->prod_name[0] = UNSTUFF_BITS(resp, 96, 8); - cid->prod_name[1] = UNSTUFF_BITS(resp, 88, 8); - cid->prod_name[2] = UNSTUFF_BITS(resp, 80, 8); - cid->prod_name[3] = UNSTUFF_BITS(resp, 72, 8); - cid->prod_name[4] = UNSTUFF_BITS(resp, 64, 8); - cid->m_rev = UNSTUFF_BITS(resp, 60, 4); - cid->l_rev = UNSTUFF_BITS(resp, 56, 4); - cid->serial = UNSTUFF_BITS(resp, 24, 32); - cid->year_off = UNSTUFF_BITS(resp, 8, 12); -} - -void sd_dump_cid(SDcid *cid) -{ - ASSERT(cid); + ASSERT(sd); - kprintf("MANFID: %d\n", cid->manfid); - kprintf("OEMID: %d\n", cid->oemid); - kprintf("SERIAL: %ld\n", cid->serial); - kprintf("PROD_NAME: %s\n", cid->prod_name); - kprintf("REV: %d.%d\n", cid->m_rev, cid->l_rev); - kprintf("OFF,Y,M: %lx, %ld\n", cid->year_off, BCD_TO_INT_32BIT(cid->year_off)); + LOG_INFO("MANFID: %d\n", sd->cid.manfid); + LOG_INFO("OEMID: %d\n", sd->cid.oemid); + LOG_INFO("SERIAL: %ld\n", sd->cid.serial); + LOG_INFO("PROD_NAME: %s\n", sd->cid.prod_name); + LOG_INFO("REV: %d.%d\n", sd->cid.m_rev, sd->cid.l_rev); + LOG_INFO("OFF,Y,M: %lx, %ld %ld\n", sd->cid.year_off, (BCD_TO_INT_32BIT(sd->cid.year_off) / 12) + 2000, + (BCD_TO_INT_32BIT(sd->cid.year_off) % 12)); } -void sd_send_init(void) +void sd_sendInit(void) { if (hsmci_sendCmd(0, 0, HSMCI_CMDR_SPCMD_INIT | HSMCI_CMDR_RSPTYP_NORESP)) - kprintf("INIT Errore %lx\n", HSMCI_SR); + LOG_ERR("INIT: %lx\n", HSMCI_SR); } -void sd_go_idle(void) +void sd_goIdle(void) { - for (int i = 0; i < 10; i++) - if (hsmci_sendCmd(0, 0, HSMCI_CMDR_RSPTYP_NORESP)) - kprintf("0 Errore %lx\n", HSMCI_SR); + if (hsmci_sendCmd(0, 0, HSMCI_CMDR_RSPTYP_NORESP)) + LOG_ERR("GO_IDLE: %lx\n", HSMCI_SR); } -int sd_send_if_cond(void) +int sd_sendIfCond(void) { if (hsmci_sendCmd(8, CMD8_V_RANGE_27V_36V, HSMCI_CMDR_RSPTYP_48_BIT)) { - kprintf("8 Errore %lx\n", HSMCI_SR); + LOG_ERR("IF_COND %lx\n", HSMCI_SR); return -1; } else @@ -740,100 +740,283 @@ int sd_send_if_cond(void) uint32_t r = HSMCI_RSPR; if ((r & 0xFFF) == CMD8_V_RANGE_27V_36V) { - kprintf("8 ok: %lx\n", r); + LOG_INFO("IF_COND: %lx\n", r); return 0; } - kprintf("8 ko: %lx\n", r); + LOG_ERR("IF_COND: %lx\n", r); } return -1; } -int sd_send_app_op_cond(void) +int sd_sendAppOpCond(void) { - for (int i = 0; i < 10; i++) + if (hsmci_sendCmd(55, 0, HSMCI_CMDR_RSPTYP_48_BIT)) { + LOG_ERR("APP_CMD %lx\n", HSMCI_SR); + return -1; + } + else + { + LOG_INFO("APP_CMD %lx\n", HSMCI_RSPR); + } - if (hsmci_sendCmd(55, 0, HSMCI_CMDR_RSPTYP_48_BIT)) - { - kprintf("55 Errore %lx\n", HSMCI_SR); - return -1; - } - else - { - kprintf("55 Risposta %lx\n", HSMCI_RSPR); - } - - if (hsmci_sendCmd(41, SD_HOST_VOLTAGE_RANGE | SD_OCR_CCS, HSMCI_CMDR_RSPTYP_48_BIT))// se cmd 8 va ok. - //if (hsmci_sendCmd(41, SD_HOST_VOLTAGE_RANGE, HSMCI_CMDR_RSPTYP_48_BIT)) - { - kprintf("41 Errore %lx\n", HSMCI_SR); - return -1; - } - else + if (hsmci_sendCmd(41, SD_HOST_VOLTAGE_RANGE | SD_OCR_CCS, HSMCI_CMDR_RSPTYP_48_BIT))// se cmd 8 va ok. + { + LOG_ERR("APP_OP_COND %lx\n", HSMCI_SR); + return -1; + } + else + { + uint32_t status = HSMCI_RSPR; + if (status & SD_OCR_BUSY) { - if (HSMCI_RSPR & SD_OCR_BUSY) - { - kputs("sd power up!\n"); - return 0; - } - - kputs("sd not ready.\n"); - continue; + LOG_INFO("SD power up! Hight Capability [%d]\n", (bool)(status & SD_OCR_CCS)); + return 0; } - timer_delay(10); + LOG_ERR("sd not ready.\n"); } + return -1; } -int sd_get_cid(uint32_t *resp, size_t len) + +int sd_getCid(Sd *sd, uint32_t addr, uint8_t flag) { - if (hsmci_sendCmd(2, 0, HSMCI_CMDR_RSPTYP_136_BIT)) + ASSERT(sd); + memset(&(sd->cid), 0, sizeof(SDcid)); + + uint8_t idx = 9; // CMD9 get cid from gived sd address (RCA) + if (flag & SD_SEND_ALL_CID) + idx = 2; + + + if (hsmci_sendCmd(idx, SD_ADDR_TO_RCA(addr), HSMCI_CMDR_RSPTYP_136_BIT)) { - kprintf("2 Errore %lx\n", HSMCI_SR); + LOG_ERR("GET_CID %lx\n", HSMCI_SR); return -1; } else { - hsmci_readResp(resp, len); - dump(resp, len); + uint32_t resp[4]; + hsmci_readResp(resp, 4); + LOG_INFOB(dump("CID", resp, 4);); + + sd->cid.manfid = UNSTUFF_BITS(resp, 120, 8); + sd->cid.oemid = UNSTUFF_BITS(resp, 104, 16); + sd->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8); + sd->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8); + sd->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8); + sd->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8); + sd->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8); + sd->cid.m_rev = UNSTUFF_BITS(resp, 60, 4); + sd->cid.l_rev = UNSTUFF_BITS(resp, 56, 4); + sd->cid.serial = (uint32_t)UNSTUFF_BITS(resp, 24, 32); + sd->cid.year_off = UNSTUFF_BITS(resp, 8, 12); } return 0; } -int sd_get_csd(uint32_t *resp, size_t len) +int sd_getCsd(Sd *sd) { - if (hsmci_sendCmd(9, 0, HSMCI_CMDR_RSPTYP_136_BIT)) + ASSERT(sd); + memset(&(sd->csd), 0, sizeof(SDcsd)); + + LOG_INFO("Send to RCA: %lx\n", SD_ADDR_TO_RCA(sd->addr)); + if (hsmci_sendCmd(9, SD_ADDR_TO_RCA(sd->addr), HSMCI_CMDR_RSPTYP_136_BIT)) { - kprintf("9 Errore %lx\n", HSMCI_SR); + LOG_ERR("GET_CSD %lx\n", HSMCI_SR); return -1; } else { - hsmci_readResp(resp, len); - dump(resp, len); + uint32_t resp[4]; + hsmci_readResp(resp, 4); + LOG_INFOB(dump("CSD", resp, 4);); + sd_decodeCsd(&(sd->csd), resp, 4); } + return 0; } -int sd_app_status(void) +int sd_getRelativeAddr(Sd *sd) { - if (hsmci_sendCmd(13, 0, HSMCI_CMDR_RSPTYP_48_BIT)) + ASSERT(sd); + if (hsmci_sendCmd(3, 0, HSMCI_CMDR_RSPTYP_48_BIT)) { - kprintf("13 Errore %lx\n", HSMCI_SR); + LOG_ERR("RCA: %lx\n", HSMCI_SR); return -1; } else { - uint32_t status = HSMCI_RSPR; - kprintf("13 r: %lx\n", status); - if (status & SD_OCR_BUSY) - return 1; + hsmci_readResp(&sd->addr, 1); + LOG_INFOB(dump("RCA", &sd->addr, 1);); + + sd->addr = sd->addr >> 16; } + return 0; } +#define SD_STATUS_APP_CMD BV(5) +#define SD_STATUS_READY BV(8) +#define SD_STATUS_CURR_MASK 0x1E00 +#define SD_STATUS_CURR_SHIFT 9 + +#define SD_GET_STATE(status) (uint8_t)(((status) & SD_STATUS_CURR_MASK) >> SD_STATUS_CURR_SHIFT) + +int sd_appStatus(Sd *sd) +{ + ASSERT(sd); + LOG_INFO("Send to RCA: %lx\n", SD_ADDR_TO_RCA(sd->addr)); + if (hsmci_sendCmd(13, SD_ADDR_TO_RCA(sd->addr), HSMCI_CMDR_RSPTYP_48_BIT)) + { + LOG_ERR("STATUS: %lx\n", HSMCI_SR); + return -1; + } + + hsmci_readResp(&(sd->status), 1); + LOG_INFOB(dump("STATUS", &(sd->status), 1);); + + LOG_INFO("State[%d]\n", SD_GET_STATE(sd->status)); + + if (sd->status & SD_STATUS_READY) + return 0; + + return -1; +} + + +INLINE int sd_cardSelection(Sd *sd, size_t rca) +{ + ASSERT(sd); + LOG_INFO("Select RCA: %lx\n", SD_ADDR_TO_RCA(sd->addr)); + if (hsmci_sendCmd(7, rca, HSMCI_CMDR_RSPTYP_R1B)) + { + LOG_ERR("SELECT_SD: %lx\n", HSMCI_SR); + return -1; + } + + HSMCI_CHECK_BUSY(); + hsmci_readResp(&(sd->status), 1); + LOG_INFOB(dump("SELECT_SD", &(sd->status), 1);); + + LOG_INFO("State[%d]\n", SD_GET_STATE(sd->status)); + + if (sd->status & SD_STATUS_READY) + return 0; + + return -1; +} + +int sd_selectCard(Sd *sd) +{ + return sd_cardSelection(sd, SD_ADDR_TO_RCA(sd->addr)); +} + +int sd_deSelectCard(Sd *sd) +{ + uint32_t rca = 0; + if (!sd->addr) + rca = SD_ADDR_TO_RCA(sd->addr + 1); + + return sd_cardSelection(sd, rca); +} + + +int sd_setBusWidth(Sd *sd, size_t len) +{ + ASSERT(sd); + + if (hsmci_sendCmd(55, SD_ADDR_TO_RCA(sd->addr), HSMCI_CMDR_RSPTYP_48_BIT)) + { + LOG_ERR("APP_CMD %lx\n", HSMCI_SR); + return -1; + } + + uint32_t status = HSMCI_RSPR; + if (status & (SD_STATUS_APP_CMD | SD_STATUS_READY)) + { + hsmci_setBusWidth(len); + + if (hsmci_sendCmd(6, len, HSMCI_CMDR_RSPTYP_48_BIT)) + { + LOG_ERR("SET_BUS_WIDTH CMD: %lx\n", HSMCI_SR); + return -1; + } + + hsmci_readResp(&(sd->status), 1); + HSMCI_CHECK_BUSY(); + + LOG_INFOB(dump("SET_BUS_WIDTH", &(sd->status), 1);); + LOG_INFO("State[%d]\n", SD_GET_STATE(sd->status)); + + if (sd->status & SD_STATUS_READY) + return 0; + } + + LOG_ERR("SET_BUS_WIDTH REP %lx\n", status); + return -1; +} + + +int sd_set_BlockLen(Sd *sd, size_t len) +{ + ASSERT(sd); + + if (hsmci_sendCmd(16, len, HSMCI_CMDR_RSPTYP_48_BIT)) + { + LOG_ERR("SET_BLK_LEN: %lx\n", HSMCI_SR); + return -1; + } + + hsmci_readResp(&(sd->status), 1); + HSMCI_CHECK_BUSY(); + + LOG_INFOB(dump("SET_BLK_LEN", &(sd->status), 1);); + LOG_INFO("State[%d]\n", SD_GET_STATE(sd->status)); + + sd->csd.blk_len = len; + + if (sd->status & SD_STATUS_READY) + return 0; + + return -1; +} + +int sd_readSingleBlock(Sd *sd, size_t index, void *_buf, size_t len) +{ + ASSERT(sd); + ASSERT(_buf); + ASSERT(!(len % sd->csd.blk_len)); + + uint32_t *buf = (uint32_t *)_buf; + + hsmci_setBlkSize(sd->csd.blk_len); + + if (hsmci_sendCmd(17, index * sd->csd.blk_len, HSMCI_CMDR_RSPTYP_48_BIT)) + { + LOG_ERR("SIGLE_BLK_READ: %lx\n", HSMCI_SR); + return -1; + } + hsmci_readResp(&(sd->status), 1); + HSMCI_CHECK_BUSY(); + + LOG_INFOB(dump("SIGLE_BLK_READ", &(sd->status), 1);); + LOG_INFO("State[%d]\n", SD_GET_STATE(sd->status)); + + if (sd->status & SD_STATUS_READY) + { + hsmci_read(buf, len / sizeof(uint32_t)); + LOG_INFOB(dump("BLK", buf, 8);); + return len; + } + + return -1; +} + + #endif