From b669237ae494a9b93a9564d6f4393c515a267058 Mon Sep 17 00:00:00 2001 From: "bernard.xiong@gmail.com" Date: Wed, 14 Jul 2010 23:32:01 +0000 Subject: [PATCH] the unit of read/write offset and buffer size is changed to the block size in block device driver read/write interface. git-svn-id: https://rt-thread.googlecode.com/svn/trunk@794 bbd45198-f89e-11dd-88c7-29a3b14d5316 --- bsp/stm3210/msd.c | 15 +++---- bsp/stm3210/sdcard.c | 81 +++++++++++++++++++++++++++----------- bsp/stm3210/stm32f10x_it.c | 8 ++-- 3 files changed, 69 insertions(+), 35 deletions(-) diff --git a/bsp/stm3210/msd.c b/bsp/stm3210/msd.c index f5febf5c0c..fc41f6d695 100644 --- a/bsp/stm3210/msd.c +++ b/bsp/stm3210/msd.c @@ -791,20 +791,21 @@ void SPI_Config(void) /* * RT-Thread SD Card Driver - * 20090417 Bernard + * 2009-04-17 Bernard first version + * 2010-07-15 Modify read/write according new block driver interface */ #include #include static struct rt_device sdcard_device; static struct dfs_partition part; + #define SECTOR_SIZE 512 /* RT-Thread Device Driver Interface */ static rt_err_t rt_msd_init(rt_device_t dev) { sMSD_CSD MSD_csd; - MSD_GetCSDRegister(&MSD_csd); return RT_EOK; @@ -829,11 +830,10 @@ static rt_size_t rt_msd_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_siz // rt_kprintf("read: 0x%x, size %d\n", pos, size); /* read all sectors */ - for (i = 0; i < size / SECTOR_SIZE; i ++) + for (i = 0; i < size; i ++) { status = MSD_ReadBlock((rt_uint8_t*)((rt_uint8_t*)buffer + i * SECTOR_SIZE), - (part.offset + i)* SECTOR_SIZE + pos, - SECTOR_SIZE); + (part.offset + pos + i)* SECTOR_SIZE, SECTOR_SIZE); if (status != MSD_RESPONSE_NO_ERROR) { rt_kprintf("sd card read failed\n"); @@ -859,8 +859,7 @@ static rt_size_t rt_msd_write (rt_device_t dev, rt_off_t pos, const void* buffer for (i = 0; i < size / SECTOR_SIZE; i ++) { status = MSD_WriteBuffer((rt_uint8_t*)((rt_uint8_t*)buffer + i * SECTOR_SIZE), - (part.offset + i)* SECTOR_SIZE + pos, - SECTOR_SIZE); + (part.offset + pos + i)* SECTOR_SIZE, SECTOR_SIZE); if (status != MSD_RESPONSE_NO_ERROR) { rt_kprintf("sd card write failed\n"); @@ -876,6 +875,8 @@ static rt_size_t rt_msd_write (rt_device_t dev, rt_off_t pos, const void* buffer static rt_err_t rt_msd_control(rt_device_t dev, rt_uint8_t cmd, void *args) { + RT_ASSERT(dev != RT_NULL); + return RT_EOK; } diff --git a/bsp/stm3210/sdcard.c b/bsp/stm3210/sdcard.c index c09bc201b5..1825436067 100644 --- a/bsp/stm3210/sdcard.c +++ b/bsp/stm3210/sdcard.c @@ -192,7 +192,7 @@ SD_Error SD_Init(void) SD_Error SD_PowerON(void) { SD_Error errorstatus = SD_OK; - uint32_t response = 0, count = 0; + uint32_t response = 0, count = 0, i = 0; bool validvoltage = FALSE; uint32_t SDType = SD_STD_CAPACITY; @@ -219,9 +219,12 @@ SD_Error SD_PowerON(void) SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_No; SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable; - SDIO_SendCommand(&SDIO_CmdInitStructure); - errorstatus = CmdError(); + for(i = 0;i < 74; i++) + { + SDIO_SendCommand(&SDIO_CmdInitStructure); + errorstatus = CmdError(); + } if (errorstatus != SD_OK) { @@ -841,7 +844,7 @@ SD_Error SD_ReadBlock(uint32_t addr, uint32_t *readbuff, uint16_t BlockSize) if (CardType == SDIO_HIGH_CAPACITY_SD_CARD) { BlockSize = 512; - addr /= 512; + // addr /= 512; } if ((BlockSize > 0) && (BlockSize <= 2048) && ((BlockSize & (BlockSize - 1)) == 0)) { @@ -1018,7 +1021,7 @@ SD_Error SD_ReadMultiBlocks(uint32_t addr, uint32_t *readbuff, uint16_t BlockSiz if (CardType == SDIO_HIGH_CAPACITY_SD_CARD) { BlockSize = 512; - addr /= 512; + // addr /= 512; } if ((BlockSize > 0) && (BlockSize <= 2048) && (0 == (BlockSize & (BlockSize - 1)))) @@ -1234,7 +1237,7 @@ SD_Error SD_WriteBlock(uint32_t addr, uint32_t *writebuff, uint16_t BlockSize) if (CardType == SDIO_HIGH_CAPACITY_SD_CARD) { BlockSize = 512; - addr /= 512; + // addr /= 512; } /* Set the block size, both on controller and card */ @@ -1469,7 +1472,7 @@ SD_Error SD_WriteMultiBlocks(uint32_t addr, uint32_t *writebuff, uint16_t BlockS if (CardType == SDIO_HIGH_CAPACITY_SD_CARD) { BlockSize = 512; - addr /= 512; + // addr /= 512; } /* Set the block size, both on controller and card */ @@ -2798,6 +2801,11 @@ static SD_Error FindSCR(uint16_t rca, uint32_t *pscr) SDIO_DataInitStructure.SDIO_DPSM = SDIO_DPSM_Enable; SDIO_DataConfig(&SDIO_DataInitStructure); + /* make a delay */ + { + volatile uint32_t delay; + for(delay = 0; delay < 20; delay++); + } /* Send ACMD51 SD_APP_SEND_SCR with argument as 0 */ SDIO_CmdInitStructure.SDIO_Argument = 0x0; @@ -2977,7 +2985,7 @@ static void DMA_RxConfiguration(uint32_t *BufferDST, uint32_t BufferSize) /* * RT-Thread SD Card Driver - * 20090417 Bernard + * 20100715 Bernard support SDHC card great than 4G. */ #include #include @@ -3023,7 +3031,11 @@ static rt_err_t rt_sdcard_close(rt_device_t dev) static rt_size_t rt_sdcard_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size) { SD_Error status; - rt_uint32_t nr = size / SECTOR_SIZE, retry; + rt_uint32_t retry; + rt_uint32_t factor; + + if (CardType == SDIO_HIGH_CAPACITY_SD_CARD) factor = 1; + else factor = SECTOR_SIZE; rt_sem_take(&sd_lock, RT_WAITING_FOREVER); @@ -3037,9 +3049,9 @@ static rt_size_t rt_sdcard_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_ rt_uint32_t index; /* which is not alignment with 4 or chip SRAM */ - for (index = 0; index < nr; index ++) + for (index = 0; index < size; index ++) { - status = SD_ReadBlock((part.offset + index) * SECTOR_SIZE + pos, + status = SD_ReadBlock((part.offset + index + pos) * factor, (uint32_t*)_sdcard_buffer, SECTOR_SIZE); if (status != SD_OK) break; @@ -3050,15 +3062,15 @@ static rt_size_t rt_sdcard_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_ } else { - if (nr == 1) + if (size == 1) { - status = SD_ReadBlock(part.offset * SECTOR_SIZE + pos, + status = SD_ReadBlock((part.offset + pos) * factor, (uint32_t*)buffer, SECTOR_SIZE); } else { - status = SD_ReadMultiBlocks(part.offset * SECTOR_SIZE + pos, - (uint32_t*)buffer, SECTOR_SIZE, nr); + status = SD_ReadMultiBlocks((part.offset + pos) * factor, + (uint32_t*)buffer, SECTOR_SIZE, size); } } @@ -3077,7 +3089,10 @@ static rt_size_t rt_sdcard_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_ static rt_size_t rt_sdcard_write (rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size) { SD_Error status; - rt_uint32_t nr = size / SECTOR_SIZE; + rt_uint32_t factor; + + if (CardType == SDIO_HIGH_CAPACITY_SD_CARD) factor = 1; + else factor = SECTOR_SIZE; rt_sem_take(&sd_lock, RT_WAITING_FOREVER); @@ -3087,13 +3102,13 @@ static rt_size_t rt_sdcard_write (rt_device_t dev, rt_off_t pos, const void* buf { rt_uint32_t index; - /* which is not alignment with 4 or chip SRAM */ - for (index = 0; index < nr; index ++) + /* which is not alignment with 4 or not chip SRAM */ + for (index = 0; index < size; index ++) { /* copy to the buffer */ rt_memcpy(_sdcard_buffer, ((rt_uint8_t*)buffer + index * SECTOR_SIZE), SECTOR_SIZE); - status = SD_WriteBlock((part.offset + index) * SECTOR_SIZE + pos, + status = SD_WriteBlock((part.offset + index + pos) * factor, (uint32_t*)_sdcard_buffer, SECTOR_SIZE); if (status != SD_OK) break; @@ -3101,15 +3116,15 @@ static rt_size_t rt_sdcard_write (rt_device_t dev, rt_off_t pos, const void* buf } else { - if (nr == 1) + if (size == 1) { - status = SD_WriteBlock(part.offset * SECTOR_SIZE + pos, + status = SD_WriteBlock((part.offset + pos) * factor, (uint32_t*)buffer, SECTOR_SIZE); } else { - status = SD_WriteMultiBlocks(part.offset * SECTOR_SIZE + pos, - (uint32_t*)buffer, SECTOR_SIZE, nr); + status = SD_WriteMultiBlocks((part.offset + pos) * factor, + (uint32_t*)buffer, SECTOR_SIZE, size); } } @@ -3123,6 +3138,23 @@ static rt_size_t rt_sdcard_write (rt_device_t dev, rt_off_t pos, const void* buf static rt_err_t rt_sdcard_control(rt_device_t dev, rt_uint8_t cmd, void *args) { + RT_ASSERT(dev != RT_NULL); + + if (cmd == RT_DEVICE_CTRL_BLK_GETGEOME) + { + struct rt_device_blk_geometry *geometry; + + geometry = (struct rt_device_blk_geometry *)args; + if (geometry == RT_NULL) return -RT_ERROR; + + geometry->bytes_per_sector = 512; + geometry->block_size = SDCardInfo.CardBlockSize; + if (CardType == SDIO_HIGH_CAPACITY_SD_CARD) + geometry->sector_count = (SDCardInfo.SD_csd.DeviceSize + 1) * 1024; + else + geometry->sector_count = SDCardInfo.CardCapacity/SDCardInfo.CardBlockSize; + } + return RT_EOK; } @@ -3139,7 +3171,7 @@ void rt_hw_sdcard_init() status = SD_SelectDeselect((u32) (SDCardInfo.RCA << 16)); if (status != SD_OK) goto __return; - SD_EnableWideBusOperation(SDIO_BusWide_1b); + SD_EnableWideBusOperation(SDIO_BusWide_4b); SD_SetDeviceMode(SD_DMA_MODE); /* get the first sector to read partition table */ @@ -3171,6 +3203,7 @@ void rt_hw_sdcard_init() rt_free(sector); /* register sdcard device */ + sdcard_device.type = RT_Device_Class_Block; sdcard_device.init = rt_sdcard_init; sdcard_device.open = rt_sdcard_open; sdcard_device.close = rt_sdcard_close; diff --git a/bsp/stm3210/stm32f10x_it.c b/bsp/stm3210/stm32f10x_it.c index 8411987ae9..558afa9c1a 100644 --- a/bsp/stm3210/stm32f10x_it.c +++ b/bsp/stm3210/stm32f10x_it.c @@ -285,11 +285,11 @@ void EXTI0_IRQHandler(void) /* enter interrupt */ rt_interrupt_enter(); - enc28j60_isr(); - /* Clear the Key Button EXTI line pending bit */ EXTI_ClearITPendingBit(EXTI_Line0); + enc28j60_isr(); + /* leave interrupt */ rt_interrupt_leave(); } @@ -310,11 +310,11 @@ void EXTI9_5_IRQHandler(void) /* enter interrupt */ rt_interrupt_enter(); - rt_dm9000_isr(); - /* Clear the Key Button EXTI line pending bit */ EXTI_ClearITPendingBit(EXTI_Line7); + rt_dm9000_isr(); + /* leave interrupt */ rt_interrupt_leave(); }