void SD_INIT_FAT(void){ unsigned int f**k =0; unsigned char fontshit[5]; unsigned char f****r =0; SD_INIT(); errCode = f_mount(0, &filesystem); Fat_FS_Error(errCode,"f_mount"); printf("\n Mounted File System: Type SD"); Write_Hello_World(); }
DSTATUS disk_initialize ( BYTE pdrv /* Physical drive nmuber to identify the drive */ ) { DSTATUS stat = 0; if(SD_INIT()){ return stat; } // sd card 初始化 return STA_NOINIT; }
/*----------------------------------------------------------------------------*/ int diskio_detect_devices() { struct mbr mbr; int dev_num = 0; int i = 0, index = 0; memset(devices, 0, DISKIO_MAX_DEVICES * sizeof (struct diskio_device_info)); #ifdef FLASH_INIT if (FLASH_INIT() == 0) { devices[index].type = DISKIO_DEVICE_TYPE_GENERIC_FLASH; devices[index].number = dev_num; /* This Flash has 4096 Pages */ devices[index].num_sectors = 4096; /* A Page is 528 Bytes long, but for easier acces we use only 512 Byte*/ devices[index].sector_size = 512; devices[index].first_sector = 0; index += 1; } #endif #ifdef SD_INIT if (SD_INIT() == 0) { devices[index].type = DISKIO_DEVICE_TYPE_SD_CARD; devices[index].number = dev_num; devices[index].num_sectors = microSD_get_block_num(); devices[index].sector_size = microSD_get_block_size(); devices[index].first_sector = 0; if (devices[index].sector_size > DISKIO_MAX_SECTOR_SIZE) { goto end_of_function; } mbr_init(&mbr); mbr_read(&devices[index], &mbr); index += 1; for (i = 0; i < 4; ++i) { if (mbr_hasPartition(&mbr, i + 1) != 0) { devices[index].type = DISKIO_DEVICE_TYPE_SD_CARD | DISKIO_DEVICE_TYPE_PARTITION; devices[index].number = dev_num; devices[index].partition = i + 1; devices[index].num_sectors = mbr.partition[i].lba_num_sectors; devices[index].sector_size = devices[dev_num].sector_size; devices[index].first_sector = mbr.partition[i].lba_first_sector; index += 1; } } dev_num += 1; index += 1; } #endif end_of_function: if (index == 0) { return DISKIO_FAILURE; } return DISKIO_SUCCESS; }
/*----------------------------------------------------------------------------*/ int diskio_detect_devices() { struct mbr mbr; int dev_num = 0; int i = 0, index = 0; memset(devices, 0, DISKIO_MAX_DEVICES * sizeof (struct diskio_device_info)); /** @todo Place definitions at proper position */ #ifndef FLASH_ARCH_NUM_SECTORS /* This Flash has 4096 Pages */ #define FLASH_ARCH_NUM_SECTORS 4096 #endif #ifndef FLASH_ARCH_SECTOR_SIZE /* A Page is 528 Bytes long, but for easier access we use only 512 Byte*/ #define FLASH_ARCH_SECTOR_SIZE 512 #endif #ifdef FLASH_INIT if (FLASH_INIT() == 0) { devices[index].type = DISKIO_DEVICE_TYPE_GENERIC_FLASH; devices[index].number = dev_num; devices[index].num_sectors = FLASH_ARCH_NUM_SECTORS; devices[index].sector_size = FLASH_ARCH_SECTOR_SIZE; devices[index].first_sector = 0; index += 1; } #endif /* FLASH_INIT */ #ifdef SD_INIT if (SD_INIT() == 0) { devices[index].type = DISKIO_DEVICE_TYPE_SD_CARD; devices[index].number = dev_num; devices[index].num_sectors = SD_GET_BLOCK_NUM(); devices[index].sector_size = SD_GET_BLOCK_SIZE(); devices[index].first_sector = 0; if (devices[index].sector_size > DISKIO_MAX_SECTOR_SIZE) { goto end_of_function; } mbr_init(&mbr); mbr_read(&devices[index], &mbr); index += 1; // test for max 5 partitions for (i = 0; i < 4; ++i) { if (mbr_hasPartition(&mbr, i + 1) != 0) { devices[index].type = DISKIO_DEVICE_TYPE_SD_CARD | DISKIO_DEVICE_TYPE_PARTITION; devices[index].number = dev_num; devices[index].partition = i + 1; devices[index].num_sectors = mbr.partition[i].lba_num_sectors; devices[index].sector_size = devices[dev_num].sector_size; devices[index].first_sector = mbr.partition[i].lba_first_sector; index += 1; } } dev_num += 1; index += 1; } #endif /* SD_INIT */ end_of_function: if (index == 0) { return DISKIO_FAILURE; } return DISKIO_SUCCESS; }
/*----------------------------------------------------------------------------*/ static int diskio_rw_op(struct diskio_device_info *dev, uint32_t block_start_address, uint32_t num_blocks, uint8_t *buffer, uint8_t op) { static uint32_t multi_block_nr = 0; if (dev == NULL) { if (default_device == 0) { PRINTF("\nNo default device"); return DISKIO_ERROR_NO_DEVICE_SELECTED; } dev = default_device; } block_start_address += dev->first_sector; uint8_t ret_code = 0; uint8_t tries = 0, reinit = 0; switch (dev->type & DISKIO_DEVICE_TYPE_MASK) { #ifdef SD_INIT case DISKIO_DEVICE_TYPE_SD_CARD: switch (op) { case DISKIO_OP_READ_BLOCK: #ifndef DISKIO_OLD_STYLE for (tries = 0; tries < DISKIO_RW_RETRIES; tries++) { ret_code = SD_READ_BLOCK(block_start_address, buffer); if (ret_code == 0) { return DISKIO_SUCCESS; } else { //PRINTF("\nret_code: %u", ret_code); } #ifdef FAT_COOPERATIVE if (!coop_step_allowed) { next_step_type = READ; coop_switch_sp(); } else { coop_step_allowed = 0; } #else /* FAT_COOPERATIVE */ _delay_ms(DISKIO_RW_DELAY_MS); #endif /* FAT_COOPERATIVE */ /* Try once to reinit sd card if access failed. */ if ((reinit == 0) && (tries == DISKIO_RW_RETRIES - 1)) { PRINTF("\nReinit"); tries = 0; reinit = 1; SD_INIT(); } } PRINTF("\ndiskion_rw_op(): Unrecoverable Read Error!"); return DISKIO_ERROR_INTERNAL_ERROR; #else /* !DISKIO_OLD_STYLE */ if (SD_READ_BLOCK(block_start_address, buffer) == 0) { return DISKIO_SUCCESS; } return DISKIO_ERROR_TRY_AGAIN; #endif /* !DISKIO_OLD_STYLE */ break; case DISKIO_OP_READ_BLOCKS: return DISKIO_ERROR_TO_BE_IMPLEMENTED; break; case DISKIO_OP_WRITE_BLOCK: #ifndef DISKIO_OLD_STYLE for (tries = 0; tries < DISKIO_RW_RETRIES; tries++) { ret_code = SD_WRITE_BLOCK(block_start_address, buffer); if (ret_code == 0) { return DISKIO_SUCCESS; } #ifdef FAT_COOPERATIVE if (!coop_step_allowed) { next_step_type = WRITE; coop_switch_sp(); } else { coop_step_allowed = 0; } #else /* FAT_COOPERATIVE */ _delay_ms(DISKIO_RW_DELAY_MS); #endif /* FAT_COOPERATIVE */ if ((reinit == 0) && (tries == DISKIO_RW_RETRIES - 1)) { PRINTF("\nReinit"); tries = 0; reinit = 1; SD_INIT(); } } PRINTF("\ndiskion_rw_op(): Unrecoverable Write Error!"); return DISKIO_ERROR_INTERNAL_ERROR; #else /* !DISKIO_OLD_STYLE */ if (SD_WRITE_BLOCK(block_start_address, buffer) == 0) { return DISKIO_SUCCESS; } return DISKIO_ERROR_TRY_AGAIN; #endif /* !DISKIO_OLD_STYLE */ break; case DISKIO_OP_WRITE_BLOCKS_START: ret_code = SD_WRITE_BLOCKS_START(block_start_address, num_blocks); if (ret_code == 0) { return DISKIO_SUCCESS; } else { return DISKIO_ERROR_INTERNAL_ERROR; } break; case DISKIO_OP_WRITE_BLOCKS_NEXT: ret_code = SD_WRITE_BLOCKS_NEXT(buffer); if (ret_code == 0) { return DISKIO_SUCCESS; } else { return DISKIO_ERROR_INTERNAL_ERROR; } break; case DISKIO_OP_WRITE_BLOCKS_DONE: ret_code = SD_WRITE_BLOCKS_DONE(); if (ret_code == 0) { return DISKIO_SUCCESS; } else { return DISKIO_ERROR_INTERNAL_ERROR; } break; default: return DISKIO_ERROR_OPERATION_NOT_SUPPORTED; break; } break; #endif /* SD_INIT */ #ifdef FLASH_INIT case DISKIO_DEVICE_TYPE_GENERIC_FLASH: switch (op) { case DISKIO_OP_READ_BLOCK: FLASH_READ_BLOCK(block_start_address, 0, buffer, 512); return DISKIO_SUCCESS; break; case DISKIO_OP_READ_BLOCKS: return DISKIO_ERROR_TO_BE_IMPLEMENTED; break; case DISKIO_OP_WRITE_BLOCK: FLASH_WRITE_BLOCK(block_start_address, 0, buffer, 512); return DISKIO_SUCCESS; break; // fake multi block write case DISKIO_OP_WRITE_BLOCKS_START: if (multi_block_nr != 0) { return DISKIO_ERROR_INTERNAL_ERROR; } multi_block_nr = block_start_address; return DISKIO_SUCCESS; break; case DISKIO_OP_WRITE_BLOCKS_NEXT: FLASH_WRITE_BLOCK(multi_block_nr, 0, buffer, 512); multi_block_nr++; return DISKIO_SUCCESS; case DISKIO_OP_WRITE_BLOCKS_DONE: multi_block_nr = 0; return DISKIO_SUCCESS; break; default: return DISKIO_ERROR_OPERATION_NOT_SUPPORTED; break; } break; #endif /* FLASH_INIT */ case DISKIO_DEVICE_TYPE_NOT_RECOGNIZED: default: return DISKIO_ERROR_NO_DEVICE_SELECTED; } return DISKIO_SUCCESS; }