diff --git a/bsp/mini4020/drivers/sdcard.c b/bsp/mini4020/drivers/sdcard.c index 77983c36b9..78c1306a0d 100644 --- a/bsp/mini4020/drivers/sdcard.c +++ b/bsp/mini4020/drivers/sdcard.c @@ -14,15 +14,14 @@ * 2010-10-13 Wangmeng Added sep4020 support */ -#include "sdcard.h" -//#include +#include -volatile rt_int32_t RCA; +#include "sdcard.h" #ifdef RT_USING_DFS +volatile rt_int32_t RCA; /* RT-Thread Device Driver Interface */ -#include #include /*GLOBAL SD DEVICE PONITER*/ @@ -41,30 +40,32 @@ static rt_uint8_t gsec_buf[SECTOR_SIZE]; */ static void sd_pwr(int sd_ctl) { - if (sd_ctl) - { - *(RP)GPIO_PORTA_SEL |= 0x0200; - *(RP)GPIO_PORTA_DIR &= (~0x0200); - *(RP)GPIO_PORTA_DATA |= 0x0200; - } - else - { - - *(RP)GPIO_PORTA_SEL |= 0x0200; - *(RP)GPIO_PORTA_DIR &= (~0x0200); - *(RP)GPIO_PORTA_DATA &= (~0x0200); - } + if (sd_ctl) + { + *(RP)GPIO_PORTA_SEL |= 0x0200; + *(RP)GPIO_PORTA_DIR &= (~0x0200); + *(RP)GPIO_PORTA_DATA |= 0x0200; + } + else + { + + *(RP)GPIO_PORTA_SEL |= 0x0200; + *(RP)GPIO_PORTA_DIR &= (~0x0200); + *(RP)GPIO_PORTA_DATA &= (~0x0200); + } } /*a nop operation to delay*/ -static void delay (U32 j) -{ - U32 i; - - for (i=0;ibytes_per_sector = 512; geometry->block_size = 0x200000; - //if (CardType == SDIO_HIGH_CAPACITY_SD_CARD) - // geometry->sector_count = (SDCardInfo.SD_csd.DeviceSize + 1) * 1024; - //else - geometry->sector_count = 0x200000;//SDCardInfo.CardCapacity/SDCardInfo.CardBlockSize; + //if (CardType == SDIO_HIGH_CAPACITY_SD_CARD) + // geometry->sector_count = (SDCardInfo.SD_csd.DeviceSize + 1) * 1024; + //else + geometry->sector_count = 0x200000;//SDCardInfo.CardCapacity/SDCardInfo.CardBlockSize; } return RT_EOK; @@ -532,47 +526,55 @@ static rt_err_t rt_sdcard_control(rt_device_t dev, int cmd, void *args) * * @param hook the hook function */ -static rt_size_t rt_sdcard_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size) +static rt_size_t rt_sdcard_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size) { - rt_uint32_t retry = 3; - rt_uint8_t status; - rt_uint32_t index; + rt_uint32_t retry = 3; + rt_uint8_t status; + rt_uint32_t index; - ///*take the semaphore - struct dfs_partition *part = (struct dfs_partition *)dev->user_data; - rt_sem_take(part->lock, RT_WAITING_FOREVER); - while(retry--) + struct dfs_partition *part; + + if (dev == RT_NULL) { - if (((rt_uint32_t)buffer % 4 != 0) || - ((rt_uint32_t)buffer > 0x20080000)) - { - for(index = 0;index < size;index++) - { - status = sd_readblock((part->offset + pos) * SECTOR_SIZE,ptr_sddev->sec_buf); - if(status != RT_EOK) - break; - - rt_memcpy((rt_uint8_t *)buffer + (index * SECTOR_SIZE),ptr_sddev->sec_buf,SECTOR_SIZE); - } - } - else - { - for(index = 0;indexlock); + part = (struct dfs_partition *)dev->user_data; + // take the semaphore + rt_sem_take(part->lock, RT_WAITING_FOREVER); + while (retry--) + { + if (((rt_uint32_t)buffer % 4 != 0) || + ((rt_uint32_t)buffer > 0x20080000)) + { + for (index = 0; index < size; index++) + { + status = sd_readblock((part->offset + pos) * SECTOR_SIZE, ptr_sddev->sec_buf); + if (status != RT_EOK) + break; - if (status == RT_EOK) - return size; + rt_memcpy((rt_uint8_t *)buffer + (index * SECTOR_SIZE), ptr_sddev->sec_buf, SECTOR_SIZE); + } + } + else + { + for (index = 0; index < size; index++) + { + status = sd_readblock((pos) * SECTOR_SIZE, (rt_uint8_t *)buffer + index * SECTOR_SIZE); + if (status != RT_EOK) + break; + } + } - rt_kprintf("read failed: %d, buffer 0x%08x\n", status, buffer); - return 0; + } + rt_sem_release(part->lock); + + if (status == RT_EOK) + return size; + + rt_kprintf("read failed: %d, buffer 0x%08x\n", status, buffer); + return 0; } @@ -582,63 +584,64 @@ static rt_size_t rt_sdcard_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_ * * @param hook the hook function */ -static rt_size_t rt_sdcard_write (rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size) +static rt_size_t rt_sdcard_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size) { - int i; - rt_uint8_t status; + int i; + rt_uint8_t status; + struct dfs_partition *part; - struct dfs_partition *part = (struct dfs_partition *)dev->user_data; + if (dev == RT_NULL) + { + rt_set_errno(-DFS_STATUS_EINVAL); + return 0; + } - if ( dev == RT_NULL ) - { - rt_set_errno(-DFS_STATUS_EINVAL); - return 0; - } + part = (struct dfs_partition *)dev->user_data; - rt_sem_take(part->lock, RT_WAITING_FOREVER); + rt_sem_take(part->lock, RT_WAITING_FOREVER); if (((rt_uint32_t)buffer % 4 != 0) || - ((rt_uint32_t)buffer > 0x20080000)) - { - rt_uint32_t index; + ((rt_uint32_t)buffer > 0x20080000)) + { + rt_uint32_t index; - for(index=0;indexsec_buf, ((rt_uint8_t*)buffer + index * SECTOR_SIZE), SECTOR_SIZE); - status = sd_writeblock((part->offset + index + pos)*SECTOR_SIZE,ptr_sddev->sec_buf); - } - } - else - { + for (index = 0; index < size; index++) + { + rt_memcpy(ptr_sddev->sec_buf, ((rt_uint8_t *)buffer + index * SECTOR_SIZE), SECTOR_SIZE); + status = sd_writeblock((part->offset + index + pos) * SECTOR_SIZE, ptr_sddev->sec_buf); + } + } + else + { - for(i=0;ioffset + i + pos)*SECTOR_SIZE, - (rt_uint8_t*)((rt_uint8_t*)buffer + i * SECTOR_SIZE)); - if (status != RT_EOK) break; - } - } + for (i = 0; i < size; i++) + { + status = sd_writeblock((part->offset + i + pos) * SECTOR_SIZE, + (rt_uint8_t *)((rt_uint8_t *)buffer + i * SECTOR_SIZE)); + if (status != RT_EOK) break; + } + } - rt_sem_release(part->lock); + rt_sem_release(part->lock); - if (status == RT_EOK) - return size; + if (status == RT_EOK) + return size; - rt_kprintf("read failed: %d, buffer 0x%08x\n", status, buffer); - return 0; + rt_kprintf("read failed: %d, buffer 0x%08x\n", status, buffer); + return 0; } rt_err_t rt_hw_sdcard_exit() { - if(ptr_sddev->device != RT_NULL) - rt_free(ptr_sddev->device); - if(ptr_sddev->part != RT_NULL) - rt_free(ptr_sddev->part); - if(ptr_sddev != RT_NULL) - rt_free(ptr_sddev); + if (ptr_sddev->device != RT_NULL) + rt_free(ptr_sddev->device); + if (ptr_sddev->part != RT_NULL) + rt_free(ptr_sddev->part); + if (ptr_sddev != RT_NULL) + rt_free(ptr_sddev); - return RT_EOK; + return RT_EOK; } /** @@ -648,143 +651,141 @@ rt_err_t rt_hw_sdcard_exit() */ rt_err_t rt_hw_sdcard_init() { - /*For test*/ - rt_err_t err; - rt_int32_t i; + /*For test*/ + rt_err_t err; + rt_int32_t i; - char dname[4]; - char sname[8]; + char dname[4]; + char sname[8]; - /*Initialize structure*/ + /*Initialize structure*/ - ptr_sddev = (struct sd_device*)rt_malloc(sizeof(struct sd_device)); - if(ptr_sddev == RT_NULL) - { - EOUT("Failed to allocate sdcard device structure\n"); - return RT_ENOMEM; - } + ptr_sddev = (struct sd_device *)rt_malloc(sizeof(struct sd_device)); + if (ptr_sddev == RT_NULL) + { + EOUT("Failed to allocate sdcard device structure\n"); + return RT_ENOMEM; + } - /*sdcard intialize*/ - err = sd_init(); - if(err != RT_EOK) - goto FAIL2; + /*sdcard intialize*/ + err = sd_init(); + if (err != RT_EOK) + goto FAIL2; - /*set sector buffer*/ - ptr_sddev->sec_buf = gsec_buf; - ptr_sddev->buf_size = SECTOR_SIZE; - ptr_sddev->sdc = (struct sd_c*)SD_BASE; + /*set sector buffer*/ + ptr_sddev->sec_buf = gsec_buf; + ptr_sddev->buf_size = SECTOR_SIZE; + ptr_sddev->sdc = (struct sd_c *)SD_BASE; - //DBOUT("allocate partition sector buffer OK!"); + //DBOUT("allocate partition sector buffer OK!"); - err = sd_readblock(0,ptr_sddev->sec_buf); - if(err != RT_EOK) - { - EOUT("read first block error\n"); - goto FAIL2; - } + err = sd_readblock(0, ptr_sddev->sec_buf); + if (err != RT_EOK) + { + EOUT("read first block error\n"); + goto FAIL2; + } - /*sdcard driver initialize*/ - ptr_sddev->part = (struct dfs_partition*)rt_malloc(4 * sizeof(struct dfs_partition)); - if(ptr_sddev->part == RT_NULL) - { - EOUT("allocate partition failed\n"); - err = RT_ENOMEM; - goto FAIL2; - } + /*sdcard driver initialize*/ + ptr_sddev->part = (struct dfs_partition *)rt_malloc(4 * sizeof(struct dfs_partition)); + if (ptr_sddev->part == RT_NULL) + { + EOUT("allocate partition failed\n"); + err = RT_ENOMEM; + goto FAIL2; + } - /*alloc device buffer*/ - ptr_sddev->device = (struct rt_device*)rt_malloc(4 * sizeof(struct rt_device)); - if(ptr_sddev->device == RT_NULL) - { - EOUT("allocate device failed\n"); - err = RT_ENOMEM; - goto FAIL1; - } + /*alloc device buffer*/ + ptr_sddev->device = (struct rt_device *)rt_malloc(4 * sizeof(struct rt_device)); + if (ptr_sddev->device == RT_NULL) + { + EOUT("allocate device failed\n"); + err = RT_ENOMEM; + goto FAIL1; + } - ptr_sddev->part_num = 0; + ptr_sddev->part_num = 0; - err = sd_readblock(0,ptr_sddev->sec_buf); + err = sd_readblock(0, ptr_sddev->sec_buf); - if(err != RT_EOK) - { - EOUT("Read block 0 to initialize ERROR\n"); - goto FAIL1; - } + if (err != RT_EOK) + { + EOUT("Read block 0 to initialize ERROR\n"); + goto FAIL1; + } - for(i=0; i<4; i++) - { - /* get the first partition */ - err = dfs_filesystem_get_partition(&(ptr_sddev->part[i]), ptr_sddev->sec_buf, i); - if (err == RT_EOK) - { - rt_snprintf(dname, 4, "sd%d", i); - rt_snprintf(sname, 8, "sem_sd%d", i); - ptr_sddev->part[i].lock = rt_sem_create(sname, 1, RT_IPC_FLAG_FIFO); + for (i = 0; i < 4; i++) + { + /* get the first partition */ + err = dfs_filesystem_get_partition(&(ptr_sddev->part[i]), ptr_sddev->sec_buf, i); + if (err == RT_EOK) + { + rt_snprintf(dname, 4, "sd%d", i); + rt_snprintf(sname, 8, "sem_sd%d", i); + ptr_sddev->part[i].lock = rt_sem_create(sname, 1, RT_IPC_FLAG_FIFO); - /* register sdcard device */ - ptr_sddev->device[i].init = rt_sdcard_init; - ptr_sddev->device[i].open = rt_sdcard_open; - ptr_sddev->device[i].close = rt_sdcard_close; - ptr_sddev->device[i].read = rt_sdcard_read; - ptr_sddev->device[i].write = rt_sdcard_write; - ptr_sddev->device[i].control = rt_sdcard_control; - ptr_sddev->device[i].user_data= &ptr_sddev->part[i]; + /* register sdcard device */ + ptr_sddev->device[i].init = rt_sdcard_init; + ptr_sddev->device[i].open = rt_sdcard_open; + ptr_sddev->device[i].close = rt_sdcard_close; + ptr_sddev->device[i].read = rt_sdcard_read; + ptr_sddev->device[i].write = rt_sdcard_write; + ptr_sddev->device[i].control = rt_sdcard_control; + ptr_sddev->device[i].user_data = &ptr_sddev->part[i]; - err = rt_device_register(&ptr_sddev->device[i], dname, - RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_REMOVABLE | RT_DEVICE_FLAG_STANDALONE); + err = rt_device_register(&ptr_sddev->device[i], dname, + RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_REMOVABLE | RT_DEVICE_FLAG_STANDALONE); - if(err == RT_EOK) - ptr_sddev->part_num++; - } - else - { - if(i == 0) - { - /* there is no partition table */ - ptr_sddev->part[0].offset = 0; - ptr_sddev->part[0].size = 0; - ptr_sddev->part[0].lock = rt_sem_create("sem_sd0", 1, RT_IPC_FLAG_FIFO); + if (err == RT_EOK) + ptr_sddev->part_num++; + } + else + { + if (i == 0) + { + /* there is no partition table */ + ptr_sddev->part[0].offset = 0; + ptr_sddev->part[0].size = 0; + ptr_sddev->part[0].lock = rt_sem_create("sem_sd0", 1, RT_IPC_FLAG_FIFO); - /* register sdcard device */ - ptr_sddev->device[0].init = rt_sdcard_init; - ptr_sddev->device[0].open = rt_sdcard_open; - ptr_sddev->device[0].close = rt_sdcard_close; - ptr_sddev->device[0].read = rt_sdcard_read; - ptr_sddev->device[0].write = rt_sdcard_write; - ptr_sddev->device[0].control = rt_sdcard_control; - ptr_sddev->device[0].user_data= &ptr_sddev->part[0]; + /* register sdcard device */ + ptr_sddev->device[0].init = rt_sdcard_init; + ptr_sddev->device[0].open = rt_sdcard_open; + ptr_sddev->device[0].close = rt_sdcard_close; + ptr_sddev->device[0].read = rt_sdcard_read; + ptr_sddev->device[0].write = rt_sdcard_write; + ptr_sddev->device[0].control = rt_sdcard_control; + ptr_sddev->device[0].user_data = &ptr_sddev->part[0]; - err = rt_device_register(&ptr_sddev->device[0], "sd0", - RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_REMOVABLE | RT_DEVICE_FLAG_STANDALONE); + err = rt_device_register(&ptr_sddev->device[0], "sd0", + RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_REMOVABLE | RT_DEVICE_FLAG_STANDALONE); - if(err == RT_EOK) - ptr_sddev->part_num++; + if (err == RT_EOK) + ptr_sddev->part_num++; - break; - } - } - } + break; + } + } + } - if(ptr_sddev->part_num == 0) - goto FAIL0; - - return err; + if (ptr_sddev->part_num == 0) + goto FAIL0; - FAIL0: - rt_free(ptr_sddev->device); - ptr_sddev->device = RT_NULL; + return err; - FAIL1: - rt_free(ptr_sddev->part); - ptr_sddev->part = RT_NULL; +FAIL0: + rt_free(ptr_sddev->device); + ptr_sddev->device = RT_NULL; - FAIL2: - rt_free(ptr_sddev); - ptr_sddev = RT_NULL; - - return err; +FAIL1: + rt_free(ptr_sddev->part); + ptr_sddev->part = RT_NULL; +FAIL2: + rt_free(ptr_sddev); + ptr_sddev = RT_NULL; + return err; } #endif diff --git a/bsp/mini4020/drivers/sdcard.h b/bsp/mini4020/drivers/sdcard.h index ce19f8f48c..c8a4acf1a1 100644 --- a/bsp/mini4020/drivers/sdcard.h +++ b/bsp/mini4020/drivers/sdcard.h @@ -17,43 +17,43 @@ #include #ifdef RT_USING_DFS - + #include -#define INICLK 300000 -#define SDCLK 24000000 //PCLK=49.392MHz -#define MMCCLK 15000000 //PCLK=49.392MHz +#define INICLK 300000 +#define SDCLK 24000000 //PCLK=49.392MHz +#define MMCCLK 15000000 //PCLK=49.392MHz /*struct of the SDC*/ -struct sd_c +struct sd_c { - __IO rt_uint32_t clk_ctl; - __IO rt_uint32_t soft_rst; - __IO rt_uint32_t arg; - __IO rt_uint32_t cmd; - __IO rt_uint32_t blk_sz; - __IO rt_uint32_t blk_cnt; - __IO rt_uint32_t trans_mode; - __O rt_uint32_t response0; - __O rt_uint32_t response1; - __O rt_uint32_t response2; - __O rt_uint32_t response3; - __IO rt_uint32_t rd_to_ctl; - __IO rt_uint32_t int_stat; - __IO rt_uint32_t int_stat_mask; - __O rt_uint32_t rx_fifo; - __I rt_uint32_t tx_fifo; + __IO rt_uint32_t clk_ctl; + __IO rt_uint32_t soft_rst; + __IO rt_uint32_t arg; + __IO rt_uint32_t cmd; + __IO rt_uint32_t blk_sz; + __IO rt_uint32_t blk_cnt; + __IO rt_uint32_t trans_mode; + __O rt_uint32_t response0; + __O rt_uint32_t response1; + __O rt_uint32_t response2; + __O rt_uint32_t response3; + __IO rt_uint32_t rd_to_ctl; + __IO rt_uint32_t int_stat; + __IO rt_uint32_t int_stat_mask; + __O rt_uint32_t rx_fifo; + __I rt_uint32_t tx_fifo; }; /*sdcard driver structure*/ -struct sd_device +struct sd_device { - struct rt_device *device; /*rt_device*/ - struct sd_c *sdc; /*SDCARD register*/ - struct dfs_partition *part; /*dfs partitions*/ - rt_uint8_t *sec_buf; /*sdcard buffer*/ - rt_uint32_t part_num; /*partiont numbers*/ - rt_uint32_t buf_size; /*buffer size*/ + struct rt_device *device; /*rt_device*/ + struct sd_c *sdc; /*SDCARD register*/ + struct dfs_partition *part; /*dfs partitions*/ + rt_uint8_t *sec_buf; /*sdcard buffer*/ + rt_uint32_t part_num; /*partiont numbers*/ + rt_uint32_t buf_size; /*buffer size*/ }; #endif