/* read count blocks into existing buf */ static int mmchs_host_read(struct sd_card *card, uint32_t blknr, uint32_t count, unsigned char *buf) { uint32_t i; i = count; for (i = 0; i < count; i++) { read_single_block(&card->regs, blknr + i, buf + (i * card->blk_size)); } return OK; }
// // Read Partial Sector // DRESULT disk_readp (BYTE* buff, // Pointer to the destination object DWORD sector, // Sector number (LBA) UINT offset, // Offset in the sector UINT count) { // Byte count (bit15:destination) unsigned char temp[0x1500]; unsigned char *pbuf = temp; DRESULT res; int i, result; if(offset > 511) return RES_PARERR; if(offset + count > 512) count = 512 - offset; result = read_single_block (&card, sector, pbuf); if(result) return RES_ERROR; for (i = 0; i < count; i++) { buff[i] = temp[i + offset]; } res = RES_OK; return res; }
int main(void) { struct sd_card card; int i; unsigned char buf[1024]; memset(buf, 0, 1024); if (mmchs_init(1)) { printf("Failed to initialize the host controller\n"); return 1; } if (card_goto_idle_state()) { printf("Failed to go into idle state\n"); return 1; } if (card_identification()) { printf("Failed to go card_identification\n"); return 1; } if (card_query_voltage_and_type(&card)) { printf("Failed to go card_query_voltage_and_type\n"); return 1; } if (card_identify(&card)) { printf("Failed to identify card\n"); return 1; } /* We have now initialized the hardware identified the card */ if (card_csd(&card)) { printf("failed to read csd (card specific data)\n"); return 1; } if (select_card(&card)) { printf("Failed to select card\n"); return 1; } if (read_single_block(&card, 0, buf)) { printf("Failed to read a single block\n"); return 1; } /* check signature */ if (!(buf[0x01fe] == 0x55 && buf[0x01ff] == 0xaa)) { printf("Failed to find MBR signature\n"); return 1; } for (i = 0; i < 512; i++) { buf[i] = i % 256; } if (read_single_block(&card, 0, buf)) { printf("Failed to read a single block\n"); return 1; } /* check signature */ if (!(buf[0x01fe] == 0x55 && buf[0x01ff] == 0xaa)) { printf("Failed to find MBR signature\n"); return 1; } /* DESCUCTIVE... */ for (i = 0; i < 512; i++) { buf[i] = i % 256; } if (write_single_block(&card, 0xfffff, buf)) { printf("Failed to write a single block\n"); return 1; } for (i = 0; i < 512; i++) { buf[i] = 0; } if (read_single_block(&card, 0xfffff, buf)) { printf("Failed to write a single block (check)\n"); return 1; } for (i = 0; i < 512; i++) { if (!buf[i] == i % 256) { printf("Failed to write a single block and read it again \n"); return 1; } } printf("Finished\n"); return 0; }