uint8_t mmc_init(void) { unsigned int i,retval; digitalWrite(SDSS,HIGH) ; for(i=0;i<10;i++) // send 80 clocks while card power stabilizes (void)spixfer(0xff); mmc_send_command(0,0,0); // send CMD0 - reset card retval=mmc_get(); if (retval != 1) // if no valid response code { printf("\nmmc init cmd 0 failed with code %d (should have been 1)\n"); mmc_clock_and_release(); return 1; // card cannot be detected } // // send CMD1 until we get a 0 back, indicating card is done initializing // i = 0xff; // max timeout while ((spixfer(0xff) != 0) && (--i)) // wait for it { mmc_send_command(1,0,0); // send CMD1 - activate card init } mmc_clock_and_release(); // clean up if (i == 0){ // if we timed out above printf("\nmmc cmd 1 failed\n"); return 2; // return failure code } return 0; }
int mmc_readsector(uint32_t lba, uint8_t *buffer) { uint16_t i,retval; printf("\nreadsector\n"); // send read command and logical sector address mmc_send_command(17,(lba>>7) & 0xffff, (lba<<9) & 0xffff); //mmc_send_command(17,(lba>>16) & 0xffff, (lba) & 0xffff); //wjr retval=mmc_get(); printf("\ngot rc %d\n",retval); if (mmc_datatoken() != 0xfe) // if no valid token { mmc_clock_and_release(); // cleanup and return -1; // return error code } spiReceiveN(buffer,512); // for (i=0;i<512;i++) // read sector data // *buffer++ = spixfer(0xff); (void)spixfer(0xff); // ignore dummy checksum (void)spixfer(0xff); // ignore dummy checksum mmc_clock_and_release(); // cleanup return 0; // return success }
/** Init MMC/SD card. Initialize I/O ports for the MMC/SD interface and send init commands to the MMC/SD card \return 0 on success, other values on error */ uint8_t mmc_init(void) { //Run configure_spi() to setup the SPI port before initializing MMC. int i; for(i=0;i<10;i++) // send 8 clocks while card power stabilizes spi_byte(0xff); mmc_send_command(0,0,0); // send CMD0 - reset card if (mmc_get() != 1) // if no valid response code { mmc_clock_and_release(); return 1; // card cannot be detected } // // send CMD1 until we get a 0 back, indicating card is done initializing // i = 0xffff; // max timeout while ((spi_byte(0xff) != 0) && (--i)) // wait for it { mmc_send_command(1,0,0); // send CMD1 - activate card init } mmc_clock_and_release(); // clean up if (i == 0) // if we timed out above return 2; // return failure code return 0; }
//Write MMC/SD sector int mmc_writesector(uint32_t lba, uint8_t *buffer) { uint16_t i; // send write command and logical sector address mmc_send_command(24,(lba>>7) & 0xffff, (lba<<9) & 0xffff); spi_byte(0xfe); //Send data token for (i=0;i<512;i++) //Send sector data spi_byte(*buffer++); spi_byte(0xff); //Send dummy 16-bit checksum spi_byte(0xff); if ((mmc_get() & 0x0f) != 0x05) { //Receive response token mmc_clock_and_release(); return -1; //Write error } while (spi_byte(0xff) == 0x00) { //Wait for the card to finish writing, this can take //a very long time, i.e. several hundred milliseconds } mmc_clock_and_release(); //Cleanup return 0; //Return success }
int file_get_info_by_id(mdb_conn *conn, int id, char *url, int pid, file_t **file) { char mmckey[LEN_MMC_KEY]; file_t *fl; size_t datalen; char *buf; int ret; if (id < 0 && (url == NULL || pid < 0)) return RET_RBTOP_INPUTE; if (id <= 0) snprintf(mmckey, LEN_MMC_KEY, "%s.%d.%s", PRE_MMC_FILE, pid, url); else snprintf(mmckey, LEN_MMC_KEY, "%s.%d", PRE_MMC_FILE, id); buf = mmc_get(mmckey, &datalen, 0); if (buf == NULL || datalen < sizeof(file_t)) { if (buf != NULL && datalen < sizeof(file_t)) { mtc_warn("get %d %d.%s info error from mmc %d", id, pid, url, datalen); } if (mdb_get_errcode(conn) != MDB_ERR_NONE) { mtc_err("conn err %s", mdb_get_errmsg(conn)); return RET_RBTOP_INPUTE; } fl = file_new(); if (fl == NULL) return RET_RBTOP_MEMALLOCE; if (id <= 0) { LDB_QUERY_RAW(conn, "fileinfo", FILE_QUERY_COL, "pid=%d AND name=$1", "s", pid, url); } else { LDB_QUERY_RAW(conn, "fileinfo", FILE_QUERY_COL, "id=%d", NULL, id); } ret = FILE_GET_RAW(conn, fl); if (ret != MDB_ERR_NONE) { mtc_err("get %d %d.%s info failure from db %s", id, pid, url, mdb_get_errmsg(conn)); if (ret == MDB_ERR_NORESULT) return RET_RBTOP_NEXIST; return RET_RBTOP_SELECTE; } else { file_pack(fl, &buf, &datalen); mmc_store(MMC_OP_SET, mmckey, (void*)buf, datalen, ONE_HOUR, 0); } } else { ret = file_unpack(buf, datalen, &fl, NULL); if (ret != RET_RBTOP_OK) { mtc_err("assembly file from mmc error"); return RET_RBTOP_MMCERR; } } free(buf); *file = fl; return RET_RBTOP_OK; }
/* Initialize a mmc/sd card */ uint8_t mmc_init(void) { int i; // setup I/O ports PORTB &= ~((1 << MMC_SCK) | (1 << MMC_MOSI)); PORTB |= (1 << MMC_MISO); DDRB |= (1<<MMC_SCK) | (1<<MMC_MOSI); PORTB |= (1 << MMC_CS); DDRB |= (1 << MMC_CS); SPCR = (1<<MSTR)|(1<<SPE)|2; // enable SPI interface SPSR = 0; // set/disable double speed for(i=0;i<10;i++) // send 80 clocks spi_byte(0xff); mmc_send_command(GO_IDLE_STATE,0); // reset card if (mmc_get() != 1) // error if bad/no response code { mmc_release(); return 1; } // wait for initialization to finish (gets a 0 back) i = 0xffff; while ((spi_byte(0xff) != 0) && (--i)) { mmc_send_command(SEND_OP_COND,0); // init card } if (!i) return 2; // timed out above mmc_send_command(SET_BLOCK_LEN, 512); //set block size to 512 mmc_release(); // increase SPI clock to (Fosc/2) SPCR &= ~3; SPSR = 1; return 0; }