2021-03-20 22:39:00 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2006-2021, RT-Thread Development Team
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
*/
|
2013-01-08 22:40:58 +08:00
|
|
|
#include <stdio.h>
|
2013-01-24 17:13:09 +08:00
|
|
|
#include <string.h>
|
2016-04-24 19:59:48 +08:00
|
|
|
#include <stdlib.h>
|
2017-10-22 17:07:10 +08:00
|
|
|
|
|
|
|
#include <rtthread.h>
|
|
|
|
#include <dfs.h>
|
2016-03-23 23:08:35 +08:00
|
|
|
|
2021-05-15 15:30:28 +08:00
|
|
|
#define DBG_TAG "sd.sim"
|
2022-04-09 11:33:34 +08:00
|
|
|
#define DBG_LVL DBG_WARNING
|
2021-05-15 15:30:28 +08:00
|
|
|
#include <rtdbg.h>
|
2013-01-08 22:40:58 +08:00
|
|
|
|
|
|
|
#define SDCARD_SIM "sd.bin"
|
|
|
|
#define SDCARD_SIZE (16*1024*1024) //16M
|
|
|
|
|
|
|
|
struct sdcard_device
|
|
|
|
{
|
|
|
|
struct rt_device parent;
|
|
|
|
FILE *file;
|
|
|
|
};
|
|
|
|
static struct sdcard_device _sdcard;
|
|
|
|
|
|
|
|
#define SDCARD_DEVICE(device) (( struct sdcard_device*)(device))
|
|
|
|
|
|
|
|
static rt_mutex_t lock;
|
|
|
|
|
|
|
|
/* RT-Thread device interface */
|
|
|
|
|
|
|
|
static rt_err_t rt_sdcard_init(rt_device_t dev)
|
|
|
|
{
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static rt_err_t rt_sdcard_open(rt_device_t dev, rt_uint16_t oflag)
|
|
|
|
{
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static rt_err_t rt_sdcard_close(rt_device_t dev)
|
|
|
|
{
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* position: block page address, not bytes address
|
|
|
|
* buffer:
|
|
|
|
* size : how many blocks
|
|
|
|
*/
|
2023-02-06 07:35:33 +08:00
|
|
|
static rt_ssize_t rt_sdcard_read(rt_device_t device, rt_off_t position, void *buffer, rt_size_t size)
|
2013-01-08 22:40:58 +08:00
|
|
|
{
|
|
|
|
struct sdcard_device *sd;
|
|
|
|
int result = 0;
|
|
|
|
|
2021-05-15 16:16:34 +08:00
|
|
|
LOG_I("sd read: pos %d, size %d", position, size);
|
2013-01-08 22:40:58 +08:00
|
|
|
|
|
|
|
rt_mutex_take(lock, RT_WAITING_FOREVER);
|
|
|
|
sd = SDCARD_DEVICE(device);
|
|
|
|
fseek(sd->file, position * SECTOR_SIZE, SEEK_SET);
|
|
|
|
|
|
|
|
result = fread(buffer, size * SECTOR_SIZE, 1, sd->file);
|
|
|
|
if (result < 0)
|
|
|
|
goto _err;
|
|
|
|
|
|
|
|
rt_mutex_release(lock);
|
|
|
|
return size;
|
|
|
|
|
|
|
|
_err:
|
2021-05-15 16:16:34 +08:00
|
|
|
LOG_E("sd read errors!");
|
2013-01-08 22:40:58 +08:00
|
|
|
rt_mutex_release(lock);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* position: block page address, not bytes address
|
|
|
|
* buffer:
|
|
|
|
* size : how many blocks
|
|
|
|
*/
|
2023-02-06 07:35:33 +08:00
|
|
|
static rt_ssize_t rt_sdcard_write(rt_device_t device, rt_off_t position, const void *buffer, rt_size_t size)
|
2013-01-08 22:40:58 +08:00
|
|
|
{
|
|
|
|
struct sdcard_device *sd;
|
|
|
|
int result = 0;
|
|
|
|
|
2021-05-15 16:16:34 +08:00
|
|
|
LOG_I("sst write: pos %d, size %d", position, size);
|
2013-01-08 22:40:58 +08:00
|
|
|
|
|
|
|
rt_mutex_take(lock, RT_WAITING_FOREVER);
|
|
|
|
sd = SDCARD_DEVICE(device);
|
|
|
|
fseek(sd->file, position * SECTOR_SIZE, SEEK_SET);
|
|
|
|
|
|
|
|
result = fwrite(buffer, size * SECTOR_SIZE, 1, sd->file);
|
|
|
|
if (result < 0)
|
|
|
|
goto _err;
|
|
|
|
|
|
|
|
rt_mutex_release(lock);
|
|
|
|
return size;
|
|
|
|
|
|
|
|
_err:
|
2021-05-15 16:16:34 +08:00
|
|
|
LOG_E("sd write errors!");
|
2013-01-08 22:40:58 +08:00
|
|
|
rt_mutex_release(lock);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-10-16 13:23:03 +08:00
|
|
|
static rt_err_t rt_sdcard_control(rt_device_t dev, int cmd, void *args)
|
2013-01-08 22:40:58 +08:00
|
|
|
{
|
|
|
|
struct sdcard_device *sd;
|
|
|
|
unsigned int size;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
fseek(sd->file, 0, SEEK_END);
|
|
|
|
size = ftell(sd->file);
|
|
|
|
|
|
|
|
geometry->sector_count = size / SECTOR_SIZE;
|
|
|
|
}
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
rt_err_t rt_hw_sdcard_init(const char *spi_device_name)
|
|
|
|
{
|
|
|
|
int size;
|
|
|
|
struct sdcard_device *sd;
|
|
|
|
struct rt_device *device;
|
|
|
|
|
|
|
|
sd = &_sdcard;
|
|
|
|
device = &(sd->parent);
|
|
|
|
|
2021-11-18 04:57:15 +08:00
|
|
|
lock = rt_mutex_create("lock", RT_IPC_FLAG_PRIO);
|
2021-05-15 15:35:45 +08:00
|
|
|
if (lock == RT_NULL)
|
2021-03-24 09:31:59 +08:00
|
|
|
{
|
|
|
|
LOG_E("Create mutex in rt_hw_sdcard_init failed!");
|
|
|
|
return -RT_ERROR;
|
|
|
|
}
|
2013-01-08 22:40:58 +08:00
|
|
|
|
|
|
|
/* 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)
|
|
|
|
{
|
2021-05-15 16:16:34 +08:00
|
|
|
LOG_E("malloc error, no memory!");
|
2023-03-17 12:12:16 +08:00
|
|
|
return -RT_ERROR;
|
2013-01-08 22:40:58 +08:00
|
|
|
}
|
|
|
|
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);
|
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef RT_USING_FINSH
|
|
|
|
#include <finsh.h>
|
2013-01-22 23:48:52 +08:00
|
|
|
int sd_erase(void)
|
2013-01-08 22:40:58 +08:00
|
|
|
{
|
2013-01-22 23:48:52 +08:00
|
|
|
rt_uint32_t index;
|
|
|
|
char * buffer;
|
|
|
|
struct rt_device *device;
|
|
|
|
device = &_sdcard.parent;
|
|
|
|
if ((buffer = rt_malloc(SECTOR_SIZE)) == RT_NULL)
|
|
|
|
{
|
|
|
|
rt_kprintf("out of memory\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(buffer, 0, SECTOR_SIZE);
|
|
|
|
/* just erase the MBR! */
|
|
|
|
for (index = 0; index < 2; index ++)
|
|
|
|
{
|
|
|
|
rt_sdcard_write(device, index, buffer, SECTOR_SIZE);
|
|
|
|
}
|
2013-01-22 23:57:10 +08:00
|
|
|
rt_free(buffer);
|
2013-01-22 23:48:52 +08:00
|
|
|
return 0;
|
2013-01-08 22:40:58 +08:00
|
|
|
}
|
2013-01-22 23:48:52 +08:00
|
|
|
FINSH_FUNCTION_EXPORT(sd_erase, erase all block in SPI flash);
|
2013-01-08 22:40:58 +08:00
|
|
|
#endif
|