sdio_test.c 3.9 KB
Newer Older
T
tanek liang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 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 103 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
#include "rtthread.h"
#include <stdint.h>
#include "string.h"

#define __is_print(ch) ((unsigned int)((ch) - ' ') < 127u - ' ')
static void dump_hex(const rt_uint8_t *ptr, rt_size_t buflen)
{
    unsigned char *buf = (unsigned char*)ptr;
    int i, j;

    for (i=0; i<buflen; i+=16)
    {
        rt_kprintf("%08X: ", i);

        for (j=0; j<16; j++)
            if (i+j < buflen)
                rt_kprintf("%02X ", buf[i+j]);
            else
                rt_kprintf("   ");
        rt_kprintf(" ");

        for (j=0; j<16; j++)
            if (i+j < buflen)
                rt_kprintf("%c", __is_print(buf[i+j]) ? buf[i+j] : '.');
        rt_kprintf("\n");
    }
}


static void read_sd__(uint32_t addr, int length, int count)
{
    rt_device_t device = RT_NULL;
    rt_err_t result;
    struct rt_device_blk_geometry geometry;
    rt_uint8_t * read_buffer  = RT_NULL;

    device = rt_device_find("sd0");
    rt_device_init(device);
    rt_device_open(device,RT_DEVICE_FLAG_RDWR);

    rt_memset(&geometry, 0, sizeof(geometry));
    result = rt_device_control(device,
                               RT_DEVICE_CTRL_BLK_GETGEOME,
                               &geometry);                  
    rt_kprintf("device info:\r\n");
    rt_kprintf("sector  size : %d byte\r\n", geometry.bytes_per_sector);
    rt_kprintf("sector count : %d \r\n", geometry.sector_count);
    rt_kprintf("block   size : %d byte\r\n", geometry.block_size);
    
    rt_kprintf("\r\n");
    read_buffer = rt_malloc(geometry.bytes_per_sector*length);
    if( read_buffer == RT_NULL )
    {
        rt_kprintf("no memory for read_buffer!\r\n");
        goto __return;
    }
    
    memset(read_buffer,0x00,geometry.bytes_per_sector*length);
    // for(i = 0;i < count; i++)
    {
        result = rt_device_read(device,addr,read_buffer, length);
        
        dump_hex(read_buffer,geometry.bytes_per_sector * length);
        if(result != length)
        {
            rt_kprintf("read device :%s ", device->parent.name);
            rt_kprintf("the first sector failed.\r\n");
            goto __return;
        }
        rt_kprintf("\n");
    }
     
__return:
    if( read_buffer != RT_NULL )
    {
        rt_free(read_buffer);
    }  
}

static void write_sd__(uint32_t addr, int length, unsigned char data)
{
    int i;
    rt_device_t device = RT_NULL;
    rt_err_t result;
    struct rt_device_blk_geometry geometry;
    rt_uint8_t * write_buffer = RT_NULL;
    rt_uint8_t * data_point = RT_NULL;;
    
    device = rt_device_find("sd0");
    rt_device_init(device);
    rt_device_open(device,RT_DEVICE_FLAG_RDWR);

    rt_memset(&geometry, 0, sizeof(geometry));
    result = rt_device_control(device,
                               RT_DEVICE_CTRL_BLK_GETGEOME,
                               &geometry);                  
    rt_kprintf("device info:\r\n");
    rt_kprintf("sector  size : %d byte\r\n", geometry.bytes_per_sector);
    rt_kprintf("sector count : %d \r\n", geometry.sector_count);
    rt_kprintf("block   size : %d byte\r\n", geometry.block_size);
    rt_kprintf("\r\n");
    write_buffer = rt_malloc(geometry.bytes_per_sector);
    if( write_buffer == RT_NULL )
    {
        rt_kprintf("no memory for write_buffer!\r\n");
        goto __return;
    }
    data_point = write_buffer;
    
    for(i=data; i<geometry.bytes_per_sector; i++)
    {
        *data_point++ = (rt_uint8_t)i;
    }
    
    for(i = addr;i < (length + addr); i++)
    {
        rt_device_write(device, addr, write_buffer,1);
    }
    
__return:
    if( write_buffer != RT_NULL )
    {
        rt_free(write_buffer);
    }
}

int sdio_read(uint32_t addr, int length, int count)
{
    read_sd__(addr, length, count);
    
    return 0;
}

int sdio_write(uint32_t addr, int length, unsigned char data)
{
    write_sd__(addr, length, data);
    
    return 0;
}



#ifdef RT_USING_FINSH
#include <finsh.h>
FINSH_FUNCTION_EXPORT_ALIAS(sdio_read, sdior, sdio read test);
FINSH_FUNCTION_EXPORT_ALIAS(sdio_write, sdiow, sdio write test);
#endif