提交 8bb65869 编写于 作者: wuyangyong's avatar wuyangyong

add test

git-svn-id: https://rt-thread.googlecode.com/svn/trunk@1334 bbd45198-f89e-11dd-88c7-29a3b14d5316
上级 b5fbd467
/*
* File : device_test.c
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2011, RT-Thread Development Team
*
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
* http://openlab.rt-thread.com/license/LICENSE.
*
* Change Logs:
* Date Author Notes
* 2011-01-01 aozima the first version
*/
#include <rtthread.h>
static rt_err_t _block_device_test(rt_device_t device)
{
rt_err_t result;
struct rt_device_blk_geometry geometry;
rt_uint8_t * read_buffer = RT_NULL;
rt_uint8_t * write_buffer = RT_NULL;
rt_kprintf("\r\n");
if( (device->flag & RT_DEVICE_FLAG_RDWR) == RT_DEVICE_FLAG_RDWR )
{
// device can read and write.
// step 1: open device
result = device->open(device,RT_DEVICE_FLAG_RDWR);
if( result == RT_EOK )
{
device->open_flag |= RT_DEVICE_OFLAG_RDWR | RT_DEVICE_OFLAG_OPEN;
}
else
{
return result;
}
// step 2: get device info
rt_memset(&geometry, 0, sizeof(geometry));
result = rt_device_control(device, RT_DEVICE_CTRL_BLK_GETGEOME, &geometry);
if( result != RT_EOK )
{
rt_kprintf("device : %s cmd RT_DEVICE_CTRL_BLK_GETGEOME failed.\r\n");
return result;
}
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);
if( read_buffer == RT_NULL )
{
rt_kprintf("no memory for read_buffer!\r\n");
goto __return;
}
write_buffer = rt_malloc(geometry.bytes_per_sector);
if( write_buffer == RT_NULL )
{
rt_kprintf("no memory for write_buffer!\r\n");
goto __return;
}
//step 3: I/O R/W test
{
rt_uint32_t i,err_count,sector_no;
rt_uint8_t * data_point;
// the first sector
sector_no = 0;
data_point = write_buffer;
*data_point++ = (rt_uint8_t)sector_no;
for(i=1; i<geometry.bytes_per_sector; i++)
{
*data_point++ = (rt_uint8_t)i;
}
i = device->write(device,sector_no,write_buffer,1);
if( i != 1 )
{
rt_kprintf("write device :%s ",device->parent.name);
rt_kprintf("the first sector failed.\r\n");
goto __return;
}
i = device->read(device,sector_no,read_buffer,1);
if( i != 1 )
{
rt_kprintf("read device :%s ",device->parent.name);
rt_kprintf("the first sector failed.\r\n");
goto __return;
}
err_count = 0;
data_point = read_buffer;
if( (*data_point++) != (rt_uint8_t)sector_no)
{
err_count++;
}
for(i=1; i<geometry.bytes_per_sector; i++)
{
if( (*data_point++) != (rt_uint8_t)i )
{
err_count++;
}
}
if( err_count > 0 )
{
rt_kprintf("verify device :%s ",device->parent.name);
rt_kprintf("the first sector failed.\r\n");
goto __return;
}
// the second sector
sector_no = 1;
data_point = write_buffer;
*data_point++ = (rt_uint8_t)sector_no;
for(i=1; i<geometry.bytes_per_sector; i++)
{
*data_point++ = (rt_uint8_t)i;
}
i = device->write(device,sector_no,write_buffer,1);
if( i != 1 )
{
rt_kprintf("write device :%s ",device->parent.name);
rt_kprintf("the second sector failed.\r\n");
goto __return;
}
i = device->read(device,sector_no,read_buffer,1);
if( i != 1 )
{
rt_kprintf("read device :%s ",device->parent.name);
rt_kprintf("the second sector failed.\r\n");
goto __return;
}
err_count = 0;
data_point = read_buffer;
if( (*data_point++) != (rt_uint8_t)sector_no)
{
err_count++;
}
for(i=1; i<geometry.bytes_per_sector; i++)
{
if( (*data_point++) != (rt_uint8_t)i )
{
err_count++;
}
}
if( err_count > 0 )
{
rt_kprintf("verify device :%s ",device->parent.name);
rt_kprintf("the second sector failed.\r\n");
goto __return;
}
// the end sector
sector_no = geometry.sector_count-1;
data_point = write_buffer;
*data_point++ = (rt_uint8_t)sector_no;
for(i=1; i<geometry.bytes_per_sector; i++)
{
*data_point++ = (rt_uint8_t)i;
}
i = device->write(device,sector_no,write_buffer,1);
if( i != 1 )
{
rt_kprintf("write device :%s ",device->parent.name);
rt_kprintf("the end sector failed.\r\n");
goto __return;
}
i = device->read(device,sector_no,read_buffer,1);
if( i != 1 )
{
rt_kprintf("read device :%s ",device->parent.name);
rt_kprintf("the end sector failed.\r\n");
goto __return;
}
err_count = 0;
data_point = read_buffer;
if( (*data_point++) != (rt_uint8_t)sector_no)
{
err_count++;
}
for(i=1; i<geometry.bytes_per_sector; i++)
{
if( (*data_point++) != (rt_uint8_t)i )
{
err_count++;
}
}
if( err_count > 0 )
{
rt_kprintf("verify device :%s ",device->parent.name);
rt_kprintf("the end sector failed.\r\n");
goto __return;
}
rt_kprintf("device I/O R/W test pass!\r\n");
}//step 3: I/O R/W test
// step 4: speed test
{
rt_uint32_t tick_start,tick_end;
rt_uint32_t i;
rt_kprintf("\r\n");
rt_kprintf("device I/O speed test.\r\n");
rt_kprintf("RT_TICK_PER_SECOND:%d\r\n",RT_TICK_PER_SECOND);
if( geometry.sector_count < 10 )
{
rt_kprintf("device sector_count < 10,speed test abort!\r\n");
}
else
{
// sign sector read
tick_start = rt_tick_get();
for(i=0; i<200; i++)
{
device->read(device,i%10,read_buffer,1);
}
tick_end = rt_tick_get();
rt_kprintf("read 200 sector from %d to %d,",tick_start,tick_end);
rt_kprintf("%d byte/s\r\n",(geometry.bytes_per_sector*200UL*RT_TICK_PER_SECOND)/(tick_end-tick_start) );
// sign sector write
tick_start = rt_tick_get();
for(i=0; i<200; i++)
{
device->write(device,i%10,read_buffer,1);
}
tick_end = rt_tick_get();
rt_kprintf("write 200 sector from %d to %d,",tick_start,tick_end);
rt_kprintf("%d byte/s\r\n",(geometry.bytes_per_sector*200UL*RT_TICK_PER_SECOND)/(tick_end-tick_start) );
}
}// step 4: speed test
return RT_EOK;
}// device can read and write.
else
{
// device read only
return RT_EOK;
}// device read only
__return:
if( read_buffer != RT_NULL )
{
rt_free(read_buffer);
}
if( write_buffer != RT_NULL )
{
rt_free(write_buffer);
}
return RT_ERROR;
}
int device_test(const char * device_name)
{
rt_device_t device = RT_NULL;
// step 1:find device
device = rt_device_find(device_name);
if( device == RT_NULL)
{
rt_kprintf("device %s: not found!\r\n");
return RT_ERROR;
}
// step 2:init device
if (!(device->flag & RT_DEVICE_FLAG_ACTIVATED))
{
rt_err_t result;
result = device->init(device);
if (result != RT_EOK)
{
rt_kprintf("To initialize device:%s failed. The error code is %d\r\n",
device->parent.name, result);
return result;
}
else
{
device->flag |= RT_DEVICE_FLAG_ACTIVATED;
}
}
// step 3: device test
switch( device->type )
{
case RT_Device_Class_Block :
rt_kprintf("block device!\r\n");
return _block_device_test(device);
default:
rt_kprintf("unkown device type : %02X",device->type);
return RT_ERROR;
}
}
#ifdef RT_USING_FINSH
#include <finsh.h>
FINSH_FUNCTION_EXPORT(device_test, e.g:device_test("sd0"));
#endif
/*
* File : fs_test.c
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2011, RT-Thread Development Team
*
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
* http://openlab.rt-thread.com/license/LICENSE.
*
* Change Logs:
* Date Author Notes
* 2011-01-02 aozima the first version.
* 2011-03-17 aozima fix some bug.
*/
#include <rtthread.h>
#include <dfs_posix.h>
static rt_uint32_t stop_flag = 0;
#define fsrw1_fn "/test1.dat"
#define fsrw1_data_len 120 /* Less than 256 */
static struct rt_thread fsrw1_thread;
static rt_uint32_t fsrw1_stack[1024/sizeof(rt_uint32_t)];
static void fsrw1_thread_entry(void* parameter)
{
int fd;
int index,length;
rt_uint32_t round;
rt_uint32_t tick_start,tick_end,read_speed,write_speed;
static rt_uint8_t write_data1[fsrw1_data_len];
static rt_uint8_t read_data1[fsrw1_data_len];
round = 1;
while(1)
{
if( stop_flag )
{
rt_kprintf("thread fsrw2 error,thread fsrw1 quit!\r\n");
return;
}
/* creat file */
fd = open(fsrw1_fn, O_WRONLY | O_CREAT | O_TRUNC, 0);
if (fd < 0)
{
rt_kprintf("fsrw1 open file for write failed\n");
stop_flag = 1;
return;
}
/* plan write data */
for (index = 0; index < fsrw1_data_len; index ++)
{
write_data1[index] = index;
}
/* write 8000 times */
tick_start = rt_tick_get();
for(index=0; index<8000; index++)
{
length = write(fd, write_data1, fsrw1_data_len);
if (length != fsrw1_data_len)
{
rt_kprintf("fsrw1 write data failed\n");
close(fd);
stop_flag = 1;
return;
}
}
tick_end = rt_tick_get();
write_speed = fsrw1_data_len*8000UL*RT_TICK_PER_SECOND/(tick_end-tick_start);
/* close file */
close(fd);
/* open file read only */
fd = open(fsrw1_fn, O_RDONLY, 0);
if (fd < 0)
{
rt_kprintf("fsrw1 open file for read failed\n");
stop_flag = 1;
return;
}
/* verify data */
tick_start = rt_tick_get();
for(index=0; index<8000; index++)
{
rt_uint32_t i;
length = read(fd, read_data1, fsrw1_data_len);
if (length != fsrw1_data_len)
{
rt_kprintf("fsrw1 read file failed\r\n");
close(fd);
stop_flag = 1;
return;
}
for(i=0; i<fsrw1_data_len; i++)
{
if( read_data1[i] != write_data1[i] )
{
rt_kprintf("fsrw1 data error!\r\n");
close(fd);
stop_flag = 1;
return;
}
}
}
tick_end = rt_tick_get();
read_speed = fsrw1_data_len*8000UL*RT_TICK_PER_SECOND/(tick_end-tick_start);
rt_kprintf("thread fsrw1 round %d ",round++);
rt_kprintf("rd:%dbyte/s,wr:%dbyte/s\r\n",read_speed,write_speed);
/* close file */
close(fd);
}
}
#define fsrw2_fn "/test2.dat"
#define fsrw2_data_len 180 /* Less than 256 */
static struct rt_thread fsrw2_thread;
static rt_uint32_t fsrw2_stack[1024/sizeof(rt_uint32_t)];
static void fsrw2_thread_entry(void* parameter)
{
int fd;
int index,length;
rt_uint32_t round;
rt_uint32_t tick_start,tick_end,read_speed,write_speed;
static rt_uint8_t write_data2[fsrw2_data_len];
static rt_uint8_t read_data2[fsrw2_data_len];
round = 1;
while(1)
{
if( stop_flag )
{
rt_kprintf("thread fsrw1 error,thread fsrw2 quit!\r\n");
return;
}
/* creat file */
fd = open(fsrw2_fn, O_WRONLY | O_CREAT | O_TRUNC, 0);
if (fd < 0)
{
rt_kprintf("fsrw2 open file for write failed\n");
stop_flag = 1;
return;
}
/* plan write data */
for (index = 0; index < fsrw2_data_len; index ++)
{
write_data2[index] = index;
}
/* write 5000 times */
tick_start = rt_tick_get();
for(index=0; index<5000; index++)
{
length = write(fd, write_data2, fsrw2_data_len);
if (length != fsrw2_data_len)
{
rt_kprintf("fsrw2 write data failed\n");
close(fd);
stop_flag = 1;
return;
}
}
tick_end = rt_tick_get();
write_speed = fsrw2_data_len*5000UL*RT_TICK_PER_SECOND/(tick_end-tick_start);
/* close file */
close(fd);
/* open file read only */
fd = open(fsrw2_fn, O_RDONLY, 0);
if (fd < 0)
{
rt_kprintf("fsrw2 open file for read failed\n");
stop_flag = 1;
return;
}
/* verify data */
tick_start = rt_tick_get();
for(index=0; index<5000; index++)
{
rt_uint32_t i;
length = read(fd, read_data2, fsrw2_data_len);
if (length != fsrw2_data_len)
{
rt_kprintf("fsrw2 read file failed\r\n");
close(fd);
stop_flag = 1;
return;
}
for(i=0; i<fsrw2_data_len; i++)
{
if( read_data2[i] != write_data2[i] )
{
rt_kprintf("fsrw2 data error!\r\n");
close(fd);
stop_flag = 1;
return;
}
}
}
tick_end = rt_tick_get();
read_speed = fsrw2_data_len*5000UL*RT_TICK_PER_SECOND/(tick_end-tick_start);
rt_kprintf("thread fsrw2 round %d ",round++);
rt_kprintf("rd:%dbyte/s,wr:%dbyte/s\r\n",read_speed,write_speed);
/* close file */
close(fd);
}
}
/** \brief startup filesystem read/write test(multi thread).
*
* \param arg rt_uint32_t [0]startup thread1,[1]startup thread2.
* \return void
*
*/
void fs_test(rt_uint32_t arg)
{
rt_err_t result;
rt_kprintf("arg is : 0x%02X",arg);
if(arg & 0x01)
{
/* init fsrw1 thread */
result = rt_thread_init(&fsrw1_thread,
"fsrw1",
fsrw1_thread_entry, RT_NULL,
(rt_uint8_t*)&fsrw1_stack[0],
sizeof(fsrw1_stack),
RT_THREAD_PRIORITY_MAX-2,
1);
if (result == RT_EOK)
{
rt_thread_startup(&fsrw1_thread);
}
}
if( arg & 0x02)
{
/* init fsrw2 thread */
result = rt_thread_init(&fsrw2_thread,
"fsrw2",
fsrw2_thread_entry, RT_NULL,
(rt_uint8_t*)&fsrw2_stack[0],
sizeof(fsrw2_stack),
RT_THREAD_PRIORITY_MAX-2,
1);
if (result == RT_EOK)
{
rt_thread_startup(&fsrw2_thread);
}
}
}
#ifdef RT_USING_FINSH
#include <finsh.h>
FINSH_FUNCTION_EXPORT(fs_test, file system R/W test. e.g: fs_test(3));
#endif
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册