sd_sim.c 4.8 KB
Newer Older
Y
yygg_you 已提交
1 2 3
#include <rtthread.h>
#include <dfs.h>

M
Ming, Bai 已提交
4
#include <stdio.h>
5
#include <string.h>
6 7 8
#ifdef _WIN32
#include <stdlib.h>
#endif
B
Bernard Xiong 已提交
9

Y
yygg_you 已提交
10 11 12 13 14
#ifdef DEBUG
#  define SD_TRACE     rt_kprintf
#else
#  define SD_TRACE(...)
#endif
M
Ming, Bai 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102

#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
 */
static rt_size_t rt_sdcard_read(rt_device_t device, rt_off_t position, void *buffer, rt_size_t size)
{
    struct sdcard_device *sd;
    int result = 0;

    SD_TRACE("sd read: pos %d, size %d\n", position, size);

    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:
    SD_TRACE("sd read errors!\n");
    rt_mutex_release(lock);
    return 0;
}

/* position: block page address, not bytes address
 * buffer:
 * size  : how many blocks
 */
static rt_size_t rt_sdcard_write(rt_device_t device, rt_off_t position, const void *buffer, rt_size_t size)
{
    struct sdcard_device *sd;
    int result = 0;

    SD_TRACE("sst write: pos %d, size %d\n", position, size);

    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:
    SD_TRACE("sd write errors!\n");
    rt_mutex_release(lock);
    return 0;
}

B
bernard 已提交
103
static rt_err_t rt_sdcard_control(rt_device_t dev, int cmd, void *args)
M
Ming, Bai 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
{
    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);

    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);

    return RT_EOK;
}

#ifdef RT_USING_FINSH
#include <finsh.h>
193
int sd_erase(void)
M
Ming, Bai 已提交
194
{
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
    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);
    }
211
    rt_free(buffer);
212
    return 0;
M
Ming, Bai 已提交
213
}
214
FINSH_FUNCTION_EXPORT(sd_erase, erase all block in SPI flash);
M
Ming, Bai 已提交
215
#endif