From 39c53a4b7115f77e51dc182105fc0659cba7c3d1 Mon Sep 17 00:00:00 2001 From: "bernard.xiong" Date: Fri, 19 Feb 2010 11:54:17 +0000 Subject: [PATCH] update sdcard driver. git-svn-id: https://rt-thread.googlecode.com/svn/trunk@423 bbd45198-f89e-11dd-88c7-29a3b14d5316 --- bsp/stm32_radio/mp3.c | 11 +++++- bsp/stm32_radio/sdcard.c | 85 ++++++++++++++++++++++++++-------------- 2 files changed, 65 insertions(+), 31 deletions(-) diff --git a/bsp/stm32_radio/mp3.c b/bsp/stm32_radio/mp3.c index 69070a5d47..16be5c2d68 100644 --- a/bsp/stm32_radio/mp3.c +++ b/bsp/stm32_radio/mp3.c @@ -158,7 +158,7 @@ int mp3_decoder_run(struct mp3_decoder* decoder) if (decoder->read_offset < 0) { /* discard this data */ - rt_kprintf("outof sync\n"); + rt_kprintf("outof sync, byte left: %d\n", decoder->bytes_left); decoder->bytes_left = 0; return 0; @@ -439,8 +439,14 @@ void mp3_get_info(const char* filename, struct tag_info* info) rt_size_t fd_fetch(void* parameter, rt_uint8_t *buffer, rt_size_t length) { int fd = (int)parameter; - return read(fd, (char*)buffer, length); + int read_bytes; + + read_bytes = read(fd, (char*)buffer, length); + if (read_bytes <= 0) return 0; + + return read_bytes; } + void mp3(char* filename) { int fd; @@ -492,6 +498,7 @@ rt_size_t http_data_fetch(void* parameter, rt_uint8_t *buffer, rt_size_t length) { return net_buf_read(buffer, length); } + void http_mp3(char* url) { struct http_session* session; diff --git a/bsp/stm32_radio/sdcard.c b/bsp/stm32_radio/sdcard.c index 84f788312b..2fd2db8d0e 100644 --- a/bsp/stm32_radio/sdcard.c +++ b/bsp/stm32_radio/sdcard.c @@ -175,7 +175,7 @@ SD_Error SD_Init(void) SDIO_InitStructure.SDIO_ClockBypass = SDIO_ClockBypass_Disable; SDIO_InitStructure.SDIO_ClockPowerSave = SDIO_ClockPowerSave_Disable; SDIO_InitStructure.SDIO_BusWide = SDIO_BusWide_1b; - SDIO_InitStructure.SDIO_HardwareFlowControl = SDIO_HardwareFlowControl_Disable; + SDIO_InitStructure.SDIO_HardwareFlowControl = SDIO_HardwareFlowControl_Enable; SDIO_Init(&SDIO_InitStructure); return(errorstatus); @@ -953,11 +953,21 @@ SD_Error SD_ReadBlock(uint32_t addr, uint32_t *readbuff, uint16_t BlockSize) } else if (DeviceMode == SD_DMA_MODE) { + rt_tick_t tick; + SDIO_ITConfig(SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND | SDIO_IT_RXOVERR | SDIO_IT_STBITERR, ENABLE); SDIO_DMACmd(ENABLE); + tick = rt_tick_get(); DMA_RxConfiguration(readbuff, BlockSize); while (DMA_GetFlagStatus(DMA2_FLAG_TC4) == RESET) - {} + { + if ((TransferError != SD_OK) || (rt_tick_get() - tick > 2)) + { + errorstatus = SD_ERROR; + // rt_kprintf("sd error\n"); + break; + } + } } return(errorstatus); } @@ -1151,11 +1161,21 @@ SD_Error SD_ReadMultiBlocks(uint32_t addr, uint32_t *readbuff, uint16_t BlockSiz } else if (DeviceMode == SD_DMA_MODE) { + rt_tick_t tick; + SDIO_ITConfig(SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND | SDIO_IT_RXOVERR | SDIO_IT_STBITERR, ENABLE); SDIO_DMACmd(ENABLE); + tick = rt_tick_get(); DMA_RxConfiguration(readbuff, (NumberOfBlocks * BlockSize)); while (DMA_GetFlagStatus(DMA2_FLAG_TC4) == RESET) - {} + { + if ((TransferError != SD_OK) || (rt_tick_get() - tick > 2)) + { + errorstatus = SD_ERROR; + // rt_kprintf("sd error\n"); + return errorstatus; + } + } while ((TransferEnd == 0) && (TransferError == SD_OK)) {} if (TransferError != SD_OK) @@ -3001,42 +3021,49 @@ 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; + rt_uint32_t nr = size / SECTOR_SIZE, retry; rt_sem_take(&sd_lock, RT_WAITING_FOREVER); - /* read all sectors */ - if (((rt_uint32_t)buffer % 4 != 0) || - ((rt_uint32_t)buffer > 0x20080000)) - { - rt_uint32_t index; - - /* which is not alignment with 4 or chip SRAM */ - for (index = 0; index < nr; index ++) + retry = 3; + while(retry) + { + /* read all sectors */ + if (((rt_uint32_t)buffer % 4 != 0) || + ((rt_uint32_t)buffer > 0x20080000)) { - status = SD_ReadBlock((part.offset + index) * SECTOR_SIZE + pos, - (uint32_t*)_sdcard_buffer, SECTOR_SIZE); + rt_uint32_t index; - if (status != SD_OK) break; + /* which is not alignment with 4 or chip SRAM */ + for (index = 0; index < nr; index ++) + { + status = SD_ReadBlock((part.offset + index) * SECTOR_SIZE + pos, + (uint32_t*)_sdcard_buffer, SECTOR_SIZE); - /* copy to the buffer */ - rt_memcpy(((rt_uint8_t*)buffer + index * SECTOR_SIZE), _sdcard_buffer, SECTOR_SIZE); - } - } - else - { - if (nr == 1) - { - status = SD_ReadBlock(part.offset * SECTOR_SIZE + pos, - (uint32_t*)buffer, SECTOR_SIZE); + if (status != SD_OK) break; + + /* copy to the buffer */ + rt_memcpy(((rt_uint8_t*)buffer + index * SECTOR_SIZE), _sdcard_buffer, SECTOR_SIZE); + } } else { - status = SD_ReadMultiBlocks(part.offset * SECTOR_SIZE + pos, - (uint32_t*)buffer, SECTOR_SIZE, nr); + if (nr == 1) + { + status = SD_ReadBlock(part.offset * SECTOR_SIZE + pos, + (uint32_t*)buffer, SECTOR_SIZE); + } + else + { + status = SD_ReadMultiBlocks(part.offset * SECTOR_SIZE + pos, + (uint32_t*)buffer, SECTOR_SIZE, nr); + } } - } + if (status == SD_OK) break; + + retry --; + } rt_sem_release(&sd_lock); if (status == SD_OK) return size; @@ -3110,7 +3137,7 @@ void rt_hw_sdcard_init() status = SD_SelectDeselect((u32) (SDCardInfo.RCA << 16)); if (status != SD_OK) goto __return; - SD_EnableWideBusOperation(SDIO_BusWide_4b); + SD_EnableWideBusOperation(SDIO_BusWide_1b); SD_SetDeviceMode(SD_DMA_MODE); /* get the first sector to read partition table */