提交 bfc473eb 编写于 作者: B bernard.xiong

update sdcard driver according to stm32 radio project.

git-svn-id: https://rt-thread.googlecode.com/svn/trunk@509 bbd45198-f89e-11dd-88c7-29a3b14d5316
上级 a9d2a380
......@@ -2,8 +2,8 @@
******************************************************************************
* @file SDIO/sdcard.c
* @author MCD Application Team
* @version V3.1.0
* @date 06/19/2009
* @version V3.1.2
* @date 09/28/2009
* @brief This file provides all the SD Card driver firmware functions.
******************************************************************************
* @copy
......@@ -20,9 +20,6 @@
/* Includes ------------------------------------------------------------------*/
#include "sdcard.h"
#include <stm32f10x_dma.h>
#include <stm32f10x_sdio.h>
#include <rtthread.h>
/** @addtogroup STM32F10x_StdPeriph_Examples
......@@ -178,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);
......@@ -321,7 +318,6 @@ SD_Error SD_PowerON(void)
{
CardType = SDIO_HIGH_CAPACITY_SD_CARD;
}
}/* else MMC Card */
else
{
......@@ -330,7 +326,6 @@ SD_Error SD_PowerON(void)
/* Send CMD1 SEND_OP_COND with Argument 0x80FF8000 */
while ((!validvoltage) && (count < SD_MAX_VOLT_TRIAL))
{
/* SEND CMD55 APP_CMD with RCA as 0 */
SDIO_CmdInitStructure.SDIO_Argument = SD_VOLTAGE_WINDOW_MMC;
SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_SEND_OP_COND;
......@@ -958,26 +953,18 @@ SD_Error SD_ReadBlock(uint32_t addr, uint32_t *readbuff, uint16_t BlockSize)
}
else if (DeviceMode == SD_DMA_MODE)
{
int cnt = 0;
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)
{
cnt ++;
if (cnt > 10 * 50000)
if ((TransferError != SD_OK) || (rt_tick_get() - tick > 10))
{
rt_kprintf("DMA flag 0x%08x\n", DMA_GetFlagStatus(DMA2_FLAG_TC4));
/* Clear all DPSM configuration */
SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT;
SDIO_DataInitStructure.SDIO_DataLength = 0;
SDIO_DataInitStructure.SDIO_DataBlockSize = SDIO_DataBlockSize_1b;
SDIO_DataInitStructure.SDIO_TransferDir = SDIO_TransferDir_ToCard;
SDIO_DataInitStructure.SDIO_TransferMode = SDIO_TransferMode_Block;
SDIO_DataInitStructure.SDIO_DPSM = SDIO_DPSM_Disable;
SDIO_DataConfig(&SDIO_DataInitStructure);
SDIO_DMACmd(DISABLE);
errorstatus = SD_ERROR;
// rt_kprintf("sd error\n");
break;
}
}
......@@ -1174,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 > 10))
{
errorstatus = SD_ERROR;
// rt_kprintf("sd error\n");
return errorstatus;
}
}
while ((TransferEnd == 0) && (TransferError == SD_OK))
{}
if (TransferError != SD_OK)
......@@ -2983,10 +2980,14 @@ static void DMA_RxConfiguration(uint32_t *BufferDST, uint32_t BufferSize)
#include <rtthread.h>
#include <dfs_fs.h>
/* set sector size to 512 */
#define SECTOR_SIZE 512
static struct rt_device sdcard_device;
static SD_CardInfo SDCardInfo;
static struct dfs_partition part;
static struct rt_semaphore sd_lock;
static rt_uint8_t _sdcard_buffer[SECTOR_SIZE];
/* RT-Thread Device Driver Interface */
static rt_err_t rt_sdcard_init(rt_device_t dev)
......@@ -3009,7 +3010,6 @@ static rt_err_t rt_sdcard_init(rt_device_t dev)
static rt_err_t rt_sdcard_open(rt_device_t dev, rt_uint16_t oflag)
{
return RT_EOK;
}
......@@ -3018,32 +3018,52 @@ static rt_err_t rt_sdcard_close(rt_device_t dev)
return RT_EOK;
}
/* set sector size to 512 */
#define SECTOR_SIZE 512
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 i, retry;
// rt_kprintf("read: 0x%x, size %d\n", pos, size);
rt_uint32_t nr = size / SECTOR_SIZE, retry;
rt_sem_take(&sd_lock, RT_WAITING_FOREVER);
retry = 3;
while(retry)
{
/* read all sectors */
for (i = 0; i < size / SECTOR_SIZE; i ++)
if (((rt_uint32_t)buffer % 4 != 0) ||
((rt_uint32_t)buffer > 0x20080000))
{
__retry:
status = SD_ReadBlock((part.offset + i)* SECTOR_SIZE + pos,
(uint32_t*)((rt_uint8_t*)buffer + i * SECTOR_SIZE),
SECTOR_SIZE);
if (status != SD_OK)
rt_uint32_t index;
/* which is not alignment with 4 or chip SRAM */
for (index = 0; index < nr; index ++)
{
-- retry;
if (retry != 0) goto __retry;
else break;
status = SD_ReadBlock((part.offset + index) * SECTOR_SIZE + pos,
(uint32_t*)_sdcard_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
{
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;
......@@ -3055,26 +3075,44 @@ __retry:
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 i;
// rt_kprintf("write: 0x%x, size %d\n", pos, size);
rt_uint32_t nr = size / SECTOR_SIZE;
rt_sem_take(&sd_lock, RT_WAITING_FOREVER);
/* write all sectors */
for (i = 0; i < size / SECTOR_SIZE; i ++)
/* 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 ++)
{
/* 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,
(uint32_t*)_sdcard_buffer, SECTOR_SIZE);
if (status != SD_OK) break;
}
}
else
{
status = SD_WriteBlock((part.offset + i)* SECTOR_SIZE + pos,
(uint32_t*)((rt_uint8_t*)buffer + i * SECTOR_SIZE),
SECTOR_SIZE);
if (status != SD_OK)
if (nr == 1)
{
rt_kprintf("sd card write failed\n");
break;
status = SD_WriteBlock(part.offset * SECTOR_SIZE + pos,
(uint32_t*)buffer, SECTOR_SIZE);
}
else
{
status = SD_WriteMultiBlocks(part.offset * SECTOR_SIZE + pos,
(uint32_t*)buffer, SECTOR_SIZE, nr);
}
}
rt_sem_release(&sd_lock);
if (status == SD_OK) return size;
rt_kprintf("write failed: %d, buffer 0x%08x\n", status, buffer);
......@@ -3088,19 +3126,18 @@ static rt_err_t rt_sdcard_control(rt_device_t dev, rt_uint8_t cmd, void *args)
void rt_hw_sdcard_init()
{
if ( SD_Init() == SD_OK )
if (SD_Init() == SD_OK)
{
SD_Error status;
rt_uint8_t *sector;
SD_EnableWideBusOperation(SDIO_BusWide_1b);
status = SD_GetCardInfo(&SDCardInfo);
if (status != SD_OK) goto __return;
status = SD_SelectDeselect((u32) (SDCardInfo.RCA << 16));
if (status != SD_OK) goto __return;
SD_EnableWideBusOperation(SDIO_BusWide_1b);
SD_SetDeviceMode(SD_DMA_MODE);
/* get the first sector to read partition table */
......@@ -3140,7 +3177,7 @@ void rt_hw_sdcard_init()
sdcard_device.control = rt_sdcard_control;
/* no private */
sdcard_device.private = RT_NULL;
sdcard_device.private = &SDCardInfo;
rt_device_register(&sdcard_device, "sd0",
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_REMOVABLE | RT_DEVICE_FLAG_STANDALONE);
......
......@@ -2,8 +2,8 @@
******************************************************************************
* @file SDIO/sdcard.h
* @author MCD Application Team
* @version V3.1.0
* @date 06/19/2009
* @version V3.1.2
* @date 09/28/2009
* @brief This file contains all the functions prototypes for the SD Card
* driver firmware library.
******************************************************************************
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册