void rt_hw_sd_mmc_init() { if (sd_mmc_spi_internal_init()) { rt_uint8_t *sector = rt_malloc(MMC_SECTOR_SIZE); sd_mmc_spi_read_open(0); if (sd_mmc_spi_read_sector_to_ram(sector)) { if (dfs_filesystem_get_partition(&sd_mmc_partition, sector, 0) != RT_EOK) { sd_mmc_partition.offset = 0; sd_mmc_partition.size = 0; } } else { sd_mmc_partition.offset = 0; sd_mmc_partition.size = 0; } sd_mmc_spi_read_close(); rt_free(sector); rt_device_register(&sd_mmc_device, "sd0", RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_REMOVABLE | RT_DEVICE_FLAG_STANDALONE); } else { rt_kprintf("Failed to initialize SD/MMC card.\n"); } }
void rt_hw_spiflash_init() { unsigned char buf[4]; rt_uint8_t *sector; rt_uint8_t status; sf_read_jedec_id(buf, sizeof(buf)); if (0xbf==buf[0] && 0x25==buf[1] && 0x41==buf[2]) { rt_kprintf("spi flash is sst25vf016b\n"); } else if (0xef==buf[0] && 0x30==buf[1] && 0x13==buf[2]) { rt_kprintf("spi flash is w25x40bv\n"); } else { rt_kprintf("not support this spi flash. ID:0x%x,0x%x,0x%x\n", buf[0], buf[1], buf[2]); } /* register spi-flash device */ spiflash_device.type = RT_Device_Class_Block; spiflash_device.init = rt_spiflash_init; spiflash_device.open = rt_spiflash_open; spiflash_device.close = rt_spiflash_close; spiflash_device.read = rt_spiflash_read; spiflash_device.write = rt_spiflash_write; spiflash_device.control = rt_spiflash_control; spiflash_device.user_data = RT_NULL; /* no private */ /* get the first sector to read partition table */ sector = (rt_uint8_t*) rt_malloc (512); if (sector == RT_NULL) { rt_kprintf("allocate partition sector buffer failed\n"); return; } sf_read_data(0, sector, 512, 0); /* get the first partition */ status = dfs_filesystem_get_partition(&part, sector, 0); if (status != RT_EOK) { /* there is no partition table */ part.offset = 0; part.size = 0; } rt_free(sector); rt_device_register(&spiflash_device, "sf0", RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_REMOVABLE | RT_DEVICE_FLAG_STANDALONE); return; }
void rt_hw_sdcard_init() { rt_uint8_t i, status; rt_uint8_t *sector; char dname[4]; char sname[8]; /* Enable PCLK into SDI Block */ CLKCON |= 1 << 9; /* Setup GPIO as SD and SDCMD, SDDAT[3:0] Pull up En */ GPEUP = GPEUP & (~(0x3f << 5)) | (0x01 << 5); GPECON = GPECON & (~(0xfff << 10)) | (0xaaa << 10); RCA = 0; if (sd_init() == RT_EOK) { /* get the first sector to read partition table */ sector = (rt_uint8_t*) rt_malloc (512); if (sector == RT_NULL) { rt_kprintf("allocate partition sector buffer failed\n"); return; } status = sd_readblock(0, sector); if (status == RT_EOK) { for(i=0; i<4; i++) { /* get the first partition */ status = dfs_filesystem_get_partition(&part[i], sector, i); if (status == RT_EOK) { rt_snprintf(dname, 4, "sd%d", i); rt_snprintf(sname, 8, "sem_sd%d", i); part[i].lock = rt_sem_create(sname, 1, RT_IPC_FLAG_FIFO); /* register sdcard device */ sdcard_device[i].type = RT_Device_Class_Block; sdcard_device[i].init = rt_sdcard_init; sdcard_device[i].open = rt_sdcard_open; sdcard_device[i].close = rt_sdcard_close; sdcard_device[i].read = rt_sdcard_read; sdcard_device[i].write = rt_sdcard_write; sdcard_device[i].control = rt_sdcard_control; sdcard_device[i].user_data = &part[i]; rt_device_register(&sdcard_device[i], dname, RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_REMOVABLE | RT_DEVICE_FLAG_STANDALONE); } else { if(i == 0) { /* there is no partition table */ part[0].offset = 0; part[0].size = 0; part[0].lock = rt_sem_create("sem_sd0", 1, RT_IPC_FLAG_FIFO); /* register sdcard device */ sdcard_device[0].type = RT_Device_Class_Block; sdcard_device[0].init = rt_sdcard_init; sdcard_device[0].open = rt_sdcard_open; sdcard_device[0].close = rt_sdcard_close; sdcard_device[0].read = rt_sdcard_read; sdcard_device[0].write = rt_sdcard_write; sdcard_device[0].control = rt_sdcard_control; sdcard_device[0].user_data = &part[0]; rt_device_register(&sdcard_device[0], "sd0", RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_REMOVABLE | RT_DEVICE_FLAG_STANDALONE); break; } } } } else { rt_kprintf("read sdcard first sector failed\n"); } /* release sector buffer */ rt_free(sector); return; } else { rt_kprintf("sdcard init failed\n"); } }