2012-11-04 13:52:14 +08:00
|
|
|
#include <rtthread.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <dfs_def.h>
|
|
|
|
|
2012-12-27 16:26:35 +08:00
|
|
|
// #define SD_TRACE rt_kprintf
|
2012-11-04 13:52:14 +08:00
|
|
|
#define SD_TRACE(...)
|
|
|
|
|
|
|
|
//#define SDCARD_SIM "F:\\Project\\tools\\SDCARD"
|
|
|
|
#define SDCARD_SIM "sd.bin"
|
|
|
|
#define SDCARD_SIZE (16*1024*1024) //16M
|
|
|
|
|
|
|
|
struct sdcard_device
|
|
|
|
{
|
2012-12-27 16:26:35 +08:00
|
|
|
struct rt_device parent;
|
|
|
|
FILE *file;
|
2012-11-04 13:52:14 +08:00
|
|
|
};
|
|
|
|
static struct sdcard_device _sdcard;
|
|
|
|
|
2012-12-27 16:26:35 +08:00
|
|
|
#define SDCARD_DEVICE(device) (( struct sdcard_device*)(device))
|
2012-11-04 13:52:14 +08:00
|
|
|
|
|
|
|
static rt_mutex_t lock;
|
|
|
|
|
|
|
|
/* RT-Thread device interface */
|
|
|
|
|
|
|
|
static rt_err_t rt_sdcard_init(rt_device_t dev)
|
|
|
|
{
|
2012-12-27 16:26:35 +08:00
|
|
|
return RT_EOK;
|
2012-11-04 13:52:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static rt_err_t rt_sdcard_open(rt_device_t dev, rt_uint16_t oflag)
|
|
|
|
{
|
2012-12-27 16:26:35 +08:00
|
|
|
return RT_EOK;
|
2012-11-04 13:52:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static rt_err_t rt_sdcard_close(rt_device_t dev)
|
|
|
|
{
|
2012-12-27 16:26:35 +08:00
|
|
|
return RT_EOK;
|
2012-11-04 13:52:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* position: block page address, not bytes address
|
|
|
|
* buffer:
|
|
|
|
* size : how many blocks
|
|
|
|
*/
|
2012-12-27 16:26:35 +08:00
|
|
|
static rt_size_t rt_sdcard_read(rt_device_t device, rt_off_t position, void *buffer, rt_size_t size)
|
2012-11-04 13:52:14 +08:00
|
|
|
{
|
2012-12-27 16:26:35 +08:00
|
|
|
struct sdcard_device *sd;
|
|
|
|
int result = 0;
|
2012-11-04 13:52:14 +08:00
|
|
|
|
2012-12-27 16:26:35 +08:00
|
|
|
SD_TRACE("sd read: pos %d, size %d\n", position, size);
|
2012-11-04 13:52:14 +08:00
|
|
|
|
2012-12-27 16:26:35 +08:00
|
|
|
rt_mutex_take(lock, RT_WAITING_FOREVER);
|
2012-11-04 13:52:14 +08:00
|
|
|
sd = SDCARD_DEVICE(device);
|
2012-12-27 16:26:35 +08:00
|
|
|
fseek(sd->file, position * SECTOR_SIZE, SEEK_SET);
|
2012-11-04 13:52:14 +08:00
|
|
|
|
2012-12-27 16:26:35 +08:00
|
|
|
result = fread(buffer, size * SECTOR_SIZE, 1, sd->file);
|
|
|
|
if (result < 0)
|
|
|
|
goto _err;
|
2012-11-04 13:52:14 +08:00
|
|
|
|
2012-12-27 16:26:35 +08:00
|
|
|
rt_mutex_release(lock);
|
2012-11-04 13:52:14 +08:00
|
|
|
return size;
|
|
|
|
|
|
|
|
_err:
|
2012-12-27 16:26:35 +08:00
|
|
|
SD_TRACE("sd read errors!\n");
|
|
|
|
rt_mutex_release(lock);
|
|
|
|
return 0;
|
2012-11-04 13:52:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* position: block page address, not bytes address
|
|
|
|
* buffer:
|
|
|
|
* size : how many blocks
|
|
|
|
*/
|
2012-12-27 16:26:35 +08:00
|
|
|
static rt_size_t rt_sdcard_write(rt_device_t device, rt_off_t position, const void *buffer, rt_size_t size)
|
2012-11-04 13:52:14 +08:00
|
|
|
{
|
2012-12-27 16:26:35 +08:00
|
|
|
struct sdcard_device *sd;
|
|
|
|
int result = 0;
|
2012-11-04 13:52:14 +08:00
|
|
|
|
2012-12-27 16:26:35 +08:00
|
|
|
SD_TRACE("sst write: pos %d, size %d\n", position, size);
|
2012-11-04 13:52:14 +08:00
|
|
|
|
2012-12-27 16:26:35 +08:00
|
|
|
rt_mutex_take(lock, RT_WAITING_FOREVER);
|
2012-11-04 13:52:14 +08:00
|
|
|
sd = SDCARD_DEVICE(device);
|
2012-12-27 16:26:35 +08:00
|
|
|
fseek(sd->file, position * SECTOR_SIZE, SEEK_SET);
|
2012-11-04 13:52:14 +08:00
|
|
|
|
2012-12-27 16:26:35 +08:00
|
|
|
result = fwrite(buffer, size * SECTOR_SIZE, 1, sd->file);
|
|
|
|
if (result < 0)
|
|
|
|
goto _err;
|
2012-11-04 13:52:14 +08:00
|
|
|
|
2012-12-27 16:26:35 +08:00
|
|
|
rt_mutex_release(lock);
|
2012-11-04 13:52:14 +08:00
|
|
|
return size;
|
|
|
|
|
|
|
|
_err:
|
2012-12-27 16:26:35 +08:00
|
|
|
SD_TRACE("sd write errors!\n");
|
|
|
|
rt_mutex_release(lock);
|
|
|
|
return 0;
|
2012-11-04 13:52:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static rt_err_t rt_sdcard_control(rt_device_t dev, rt_uint8_t cmd, void *args)
|
|
|
|
{
|
2012-12-27 16:26:35 +08:00
|
|
|
struct sdcard_device *sd;
|
|
|
|
unsigned int size;
|
2012-11-04 13:52:14 +08:00
|
|
|
|
|
|
|
RT_ASSERT(dev != RT_NULL);
|
|
|
|
|
|
|
|
sd = SDCARD_DEVICE(dev);
|
|
|
|
|
|
|
|
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 = SECTOR_SIZE;
|
|
|
|
geometry->block_size = SECTOR_SIZE;
|
|
|
|
|
2012-12-27 16:26:35 +08:00
|
|
|
fseek(sd->file, 0, SEEK_END);
|
|
|
|
size = ftell(sd->file);
|
2012-11-04 13:52:14 +08:00
|
|
|
|
2012-12-27 16:26:35 +08:00
|
|
|
geometry->sector_count = size / SECTOR_SIZE;
|
2012-11-04 13:52:14 +08:00
|
|
|
}
|
2012-12-27 16:26:35 +08:00
|
|
|
return RT_EOK;
|
2012-11-04 13:52:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-27 16:26:35 +08:00
|
|
|
rt_err_t rt_hw_sdcard_init(const char *spi_device_name)
|
2012-11-04 13:52:14 +08:00
|
|
|
{
|
2012-12-27 16:26:35 +08:00
|
|
|
int size;
|
2012-11-04 13:52:14 +08:00
|
|
|
rt_uint32_t id, total_block;
|
2012-12-27 16:26:35 +08:00
|
|
|
struct sdcard_device *sd;
|
|
|
|
struct rt_device *device;
|
2012-11-04 13:52:14 +08:00
|
|
|
|
|
|
|
sd = &_sdcard;
|
2012-12-27 16:26:35 +08:00
|
|
|
device = &(sd->parent);
|
|
|
|
|
|
|
|
lock = rt_mutex_create("lock", RT_IPC_FLAG_FIFO);
|
|
|
|
|
|
|
|
/* open sd card file, if not exist, then create it */
|
|
|
|
sd->file = fopen(SDCARD_SIM, "rb+");
|
|
|
|
if (sd->file == NULL)
|
|
|
|
{
|
|
|
|
/* create a file to simulate sd card */
|
|
|
|
sd->file = fopen(SDCARD_SIM, "wb+");
|
|
|
|
|
|
|
|
fseek(sd->file, 0, SEEK_END);
|
|
|
|
size = ftell(sd->file);
|
|
|
|
|
|
|
|
fseek(sd->file, 0, SEEK_SET);
|
|
|
|
if (size < SDCARD_SIZE)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
unsigned char *ptr;
|
|
|
|
|
|
|
|
ptr = (unsigned char *) malloc(1024 * 1024);
|
|
|
|
if (ptr == NULL)
|
|
|
|
{
|
|
|
|
SD_TRACE("malloc error, no memory!\n");
|
|
|
|
return RT_ERROR;
|
|
|
|
}
|
|
|
|
memset(ptr, 0x0, 1024 * 1024);
|
|
|
|
|
|
|
|
fseek(sd->file, 0, SEEK_SET);
|
|
|
|
|
|
|
|
for (i = 0; i < (SDCARD_SIZE / (1024 * 1024)); i++)
|
|
|
|
fwrite(ptr, 1024 * 1024, 1, sd->file);
|
|
|
|
|
|
|
|
free(ptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fseek(sd->file, 0, SEEK_SET);
|
|
|
|
|
|
|
|
device->type = RT_Device_Class_Block;
|
|
|
|
device->init = rt_sdcard_init;
|
|
|
|
device->open = rt_sdcard_open;
|
|
|
|
device->close = rt_sdcard_close;
|
|
|
|
device->read = rt_sdcard_read;
|
|
|
|
device->write = rt_sdcard_write;
|
|
|
|
device->control = rt_sdcard_control;
|
|
|
|
device->user_data = NULL;
|
|
|
|
|
|
|
|
rt_device_register(device, "sd0",
|
|
|
|
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_REMOVABLE | RT_DEVICE_FLAG_STANDALONE);
|
2012-11-04 13:52:14 +08:00
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef RT_USING_FINSH
|
|
|
|
#include <finsh.h>
|
|
|
|
void eraseall(void)
|
|
|
|
{
|
2012-12-27 16:26:35 +08:00
|
|
|
printf("had not implemented yet!\n");
|
2012-11-04 13:52:14 +08:00
|
|
|
}
|
|
|
|
FINSH_FUNCTION_EXPORT(eraseall, erase all block in SPI flash);
|
|
|
|
#endif
|