add calculate_speed_print for device_test

git-svn-id: https://rt-thread.googlecode.com/svn/trunk@1747 bbd45198-f89e-11dd-88c7-29a3b14d5316
This commit is contained in:
wuyangyong 2011-10-09 16:14:45 +00:00
parent b2f9217686
commit 7af3914228
1 changed files with 327 additions and 301 deletions

View File

@ -1,301 +1,327 @@
/* /*
* File : device_test.c * File : device_test.c
* This file is part of RT-Thread RTOS * This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2011, RT-Thread Development Team * COPYRIGHT (C) 2011, RT-Thread Development Team
* *
* The license and distribution terms for this file may be * The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at * found in the file LICENSE in this distribution or at
* http://openlab.rt-thread.com/license/LICENSE. * http://openlab.rt-thread.com/license/LICENSE.
* *
* Change Logs: * Change Logs:
* Date Author Notes * Date Author Notes
* 2011-01-01 aozima the first version * 2011-01-01 aozima the first version
*/ */
#include <rtthread.h> #include <rtthread.h>
static rt_err_t _block_device_test(rt_device_t device) /* calculate speed */
{ static void calculate_speed_print(rt_uint32_t speed)
rt_err_t result; {
struct rt_device_blk_geometry geometry; rt_uint32_t k,m;
rt_uint8_t * read_buffer = RT_NULL;
rt_uint8_t * write_buffer = RT_NULL; k = speed/1024UL;
if( k )
rt_kprintf("\r\n"); {
m = k/1024UL;
if( (device->flag & RT_DEVICE_FLAG_RDWR) == RT_DEVICE_FLAG_RDWR ) if( m )
{ {
// device can read and write. rt_kprintf("%d.%dMbyte/s",m,k%1024UL*100/1024UL);
// step 1: open device }
result = device->open(device,RT_DEVICE_FLAG_RDWR); else
if( result == RT_EOK ) {
{ rt_kprintf("%d.%dKbyte/s",k,speed%1024UL*100/1024UL);
device->open_flag |= RT_DEVICE_OFLAG_RDWR | RT_DEVICE_OFLAG_OPEN; }
} }
else else
{ {
return result; rt_kprintf("%dbyte/s",speed);
} }
}
// step 2: get device info
rt_memset(&geometry, 0, sizeof(geometry)); static rt_err_t _block_device_test(rt_device_t device)
result = rt_device_control(device, RT_DEVICE_CTRL_BLK_GETGEOME, &geometry); {
if( result != RT_EOK ) rt_err_t result;
{ struct rt_device_blk_geometry geometry;
rt_kprintf("device : %s cmd RT_DEVICE_CTRL_BLK_GETGEOME failed.\r\n"); rt_uint8_t * read_buffer = RT_NULL;
return result; rt_uint8_t * write_buffer = RT_NULL;
}
rt_kprintf("device info:\r\n"); rt_kprintf("\r\n");
rt_kprintf("sector size : %d byte\r\n",geometry.bytes_per_sector);
rt_kprintf("sector count : %d \r\n",geometry.sector_count); if( (device->flag & RT_DEVICE_FLAG_RDWR) == RT_DEVICE_FLAG_RDWR )
rt_kprintf("block size : %d byte\r\n",geometry.block_size); {
// device can read and write.
rt_kprintf("\r\n"); // step 1: open device
read_buffer = rt_malloc(geometry.bytes_per_sector); result = device->open(device,RT_DEVICE_FLAG_RDWR);
if( read_buffer == RT_NULL ) if( result == RT_EOK )
{ {
rt_kprintf("no memory for read_buffer!\r\n"); device->open_flag |= RT_DEVICE_OFLAG_RDWR | RT_DEVICE_OFLAG_OPEN;
goto __return; }
} else
write_buffer = rt_malloc(geometry.bytes_per_sector); {
if( write_buffer == RT_NULL ) return result;
{ }
rt_kprintf("no memory for write_buffer!\r\n");
goto __return; // step 2: get device info
} rt_memset(&geometry, 0, sizeof(geometry));
result = rt_device_control(device, RT_DEVICE_CTRL_BLK_GETGEOME, &geometry);
//step 3: I/O R/W test if( result != RT_EOK )
{ {
rt_uint32_t i,err_count,sector_no; rt_kprintf("device : %s cmd RT_DEVICE_CTRL_BLK_GETGEOME failed.\r\n");
rt_uint8_t * data_point; return result;
}
// the first sector rt_kprintf("device info:\r\n");
sector_no = 0; rt_kprintf("sector size : %d byte\r\n",geometry.bytes_per_sector);
data_point = write_buffer; rt_kprintf("sector count : %d \r\n",geometry.sector_count);
*data_point++ = (rt_uint8_t)sector_no; rt_kprintf("block size : %d byte\r\n",geometry.block_size);
for(i=1; i<geometry.bytes_per_sector; i++)
{ rt_kprintf("\r\n");
*data_point++ = (rt_uint8_t)i; read_buffer = rt_malloc(geometry.bytes_per_sector);
} if( read_buffer == RT_NULL )
i = device->write(device,sector_no,write_buffer,1); {
if( i != 1 ) rt_kprintf("no memory for read_buffer!\r\n");
{ goto __return;
rt_kprintf("write device :%s ",device->parent.name); }
rt_kprintf("the first sector failed.\r\n"); write_buffer = rt_malloc(geometry.bytes_per_sector);
goto __return; if( write_buffer == RT_NULL )
} {
i = device->read(device,sector_no,read_buffer,1); rt_kprintf("no memory for write_buffer!\r\n");
if( i != 1 ) goto __return;
{ }
rt_kprintf("read device :%s ",device->parent.name);
rt_kprintf("the first sector failed.\r\n"); //step 3: I/O R/W test
goto __return; {
} rt_uint32_t i,err_count,sector_no;
err_count = 0; rt_uint8_t * data_point;
data_point = read_buffer;
if( (*data_point++) != (rt_uint8_t)sector_no) // the first sector
{ sector_no = 0;
err_count++; data_point = write_buffer;
} *data_point++ = (rt_uint8_t)sector_no;
for(i=1; i<geometry.bytes_per_sector; i++) for(i=1; i<geometry.bytes_per_sector; i++)
{ {
if( (*data_point++) != (rt_uint8_t)i ) *data_point++ = (rt_uint8_t)i;
{ }
err_count++; i = device->write(device,sector_no,write_buffer,1);
} if( i != 1 )
} {
if( err_count > 0 ) rt_kprintf("write device :%s ",device->parent.name);
{ rt_kprintf("the first sector failed.\r\n");
rt_kprintf("verify device :%s ",device->parent.name); goto __return;
rt_kprintf("the first sector failed.\r\n"); }
goto __return; i = device->read(device,sector_no,read_buffer,1);
} if( i != 1 )
// the second sector {
sector_no = 1; rt_kprintf("read device :%s ",device->parent.name);
data_point = write_buffer; rt_kprintf("the first sector failed.\r\n");
*data_point++ = (rt_uint8_t)sector_no; goto __return;
for(i=1; i<geometry.bytes_per_sector; i++) }
{ err_count = 0;
*data_point++ = (rt_uint8_t)i; data_point = read_buffer;
} if( (*data_point++) != (rt_uint8_t)sector_no)
i = device->write(device,sector_no,write_buffer,1); {
if( i != 1 ) err_count++;
{ }
rt_kprintf("write device :%s ",device->parent.name); for(i=1; i<geometry.bytes_per_sector; i++)
rt_kprintf("the second sector failed.\r\n"); {
goto __return; if( (*data_point++) != (rt_uint8_t)i )
} {
i = device->read(device,sector_no,read_buffer,1); err_count++;
if( i != 1 ) }
{ }
rt_kprintf("read device :%s ",device->parent.name); if( err_count > 0 )
rt_kprintf("the second sector failed.\r\n"); {
goto __return; rt_kprintf("verify device :%s ",device->parent.name);
} rt_kprintf("the first sector failed.\r\n");
err_count = 0; goto __return;
data_point = read_buffer; }
if( (*data_point++) != (rt_uint8_t)sector_no) // the second sector
{ sector_no = 1;
err_count++; data_point = write_buffer;
} *data_point++ = (rt_uint8_t)sector_no;
for(i=1; i<geometry.bytes_per_sector; i++) for(i=1; i<geometry.bytes_per_sector; i++)
{ {
if( (*data_point++) != (rt_uint8_t)i ) *data_point++ = (rt_uint8_t)i;
{ }
err_count++; i = device->write(device,sector_no,write_buffer,1);
} if( i != 1 )
} {
if( err_count > 0 ) rt_kprintf("write device :%s ",device->parent.name);
{ rt_kprintf("the second sector failed.\r\n");
rt_kprintf("verify device :%s ",device->parent.name); goto __return;
rt_kprintf("the second sector failed.\r\n"); }
goto __return; i = device->read(device,sector_no,read_buffer,1);
} if( i != 1 )
// the end sector {
sector_no = geometry.sector_count-1; rt_kprintf("read device :%s ",device->parent.name);
data_point = write_buffer; rt_kprintf("the second sector failed.\r\n");
*data_point++ = (rt_uint8_t)sector_no; goto __return;
for(i=1; i<geometry.bytes_per_sector; i++) }
{ err_count = 0;
*data_point++ = (rt_uint8_t)i; data_point = read_buffer;
} if( (*data_point++) != (rt_uint8_t)sector_no)
i = device->write(device,sector_no,write_buffer,1); {
if( i != 1 ) err_count++;
{ }
rt_kprintf("write device :%s ",device->parent.name); for(i=1; i<geometry.bytes_per_sector; i++)
rt_kprintf("the end sector failed.\r\n"); {
goto __return; if( (*data_point++) != (rt_uint8_t)i )
} {
i = device->read(device,sector_no,read_buffer,1); err_count++;
if( i != 1 ) }
{ }
rt_kprintf("read device :%s ",device->parent.name); if( err_count > 0 )
rt_kprintf("the end sector failed.\r\n"); {
goto __return; rt_kprintf("verify device :%s ",device->parent.name);
} rt_kprintf("the second sector failed.\r\n");
err_count = 0; goto __return;
data_point = read_buffer; }
if( (*data_point++) != (rt_uint8_t)sector_no) // the end sector
{ sector_no = geometry.sector_count-1;
err_count++; data_point = write_buffer;
} *data_point++ = (rt_uint8_t)sector_no;
for(i=1; i<geometry.bytes_per_sector; i++) for(i=1; i<geometry.bytes_per_sector; i++)
{ {
if( (*data_point++) != (rt_uint8_t)i ) *data_point++ = (rt_uint8_t)i;
{ }
err_count++; i = device->write(device,sector_no,write_buffer,1);
} if( i != 1 )
} {
if( err_count > 0 ) rt_kprintf("write device :%s ",device->parent.name);
{ rt_kprintf("the end sector failed.\r\n");
rt_kprintf("verify device :%s ",device->parent.name); goto __return;
rt_kprintf("the end sector failed.\r\n"); }
goto __return; i = device->read(device,sector_no,read_buffer,1);
} if( i != 1 )
rt_kprintf("device I/O R/W test pass!\r\n"); {
rt_kprintf("read device :%s ",device->parent.name);
}//step 3: I/O R/W test rt_kprintf("the end sector failed.\r\n");
goto __return;
// step 4: speed test }
{ err_count = 0;
rt_uint32_t tick_start,tick_end; data_point = read_buffer;
rt_uint32_t i; if( (*data_point++) != (rt_uint8_t)sector_no)
{
rt_kprintf("\r\n"); err_count++;
rt_kprintf("device I/O speed test.\r\n"); }
rt_kprintf("RT_TICK_PER_SECOND:%d\r\n",RT_TICK_PER_SECOND); for(i=1; i<geometry.bytes_per_sector; i++)
{
if( geometry.sector_count < 10 ) if( (*data_point++) != (rt_uint8_t)i )
{ {
rt_kprintf("device sector_count < 10,speed test abort!\r\n"); err_count++;
} }
else }
{ if( err_count > 0 )
// sign sector read {
tick_start = rt_tick_get(); rt_kprintf("verify device :%s ",device->parent.name);
for(i=0; i<200; i++) rt_kprintf("the end sector failed.\r\n");
{ goto __return;
device->read(device,i%10,read_buffer,1); }
} rt_kprintf("device I/O R/W test pass!\r\n");
tick_end = rt_tick_get();
rt_kprintf("read 200 sector from %d to %d,",tick_start,tick_end); }//step 3: I/O R/W test
rt_kprintf("%d byte/s\r\n",(geometry.bytes_per_sector*200UL*RT_TICK_PER_SECOND)/(tick_end-tick_start) );
// step 4: speed test
// sign sector write {
tick_start = rt_tick_get(); rt_uint32_t tick_start,tick_end;
for(i=0; i<200; i++) rt_uint32_t i;
{
device->write(device,i%10,read_buffer,1); rt_kprintf("\r\n");
} rt_kprintf("device I/O speed test.\r\n");
tick_end = rt_tick_get(); rt_kprintf("RT_TICK_PER_SECOND:%d\r\n",RT_TICK_PER_SECOND);
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) ); if( geometry.sector_count < 10 )
} {
}// step 4: speed test rt_kprintf("device sector_count < 10,speed test abort!\r\n");
}
return RT_EOK; else
}// device can read and write. {
else // sign sector read
{ tick_start = rt_tick_get();
// device read only for(i=0; i<200; i++)
return RT_EOK; {
}// device read only device->read(device,i%10,read_buffer,1);
}
__return: tick_end = rt_tick_get();
if( read_buffer != RT_NULL ) rt_kprintf("read 200 sector from %d to %d, ",tick_start,tick_end);
{ calculate_speed_print( (geometry.bytes_per_sector*200UL*RT_TICK_PER_SECOND)/(tick_end-tick_start) );
rt_free(read_buffer); rt_kprintf("\r\n");
}
if( write_buffer != RT_NULL ) // sign sector write
{ tick_start = rt_tick_get();
rt_free(write_buffer); for(i=0; i<200; i++)
} {
return RT_ERROR; device->write(device,i%10,read_buffer,1);
} }
tick_end = rt_tick_get();
int device_test(const char * device_name) rt_kprintf("write 200 sector from %d to %d, ",tick_start,tick_end);
{ calculate_speed_print( (geometry.bytes_per_sector*200UL*RT_TICK_PER_SECOND)/(tick_end-tick_start) );
rt_device_t device = RT_NULL; rt_kprintf("\r\n");
}
// step 1:find device }// step 4: speed test
device = rt_device_find(device_name);
if( device == RT_NULL) return RT_EOK;
{ }// device can read and write.
rt_kprintf("device %s: not found!\r\n"); else
return RT_ERROR; {
} // device read only
return RT_EOK;
// step 2:init device }// device read only
if (!(device->flag & RT_DEVICE_FLAG_ACTIVATED))
{ __return:
rt_err_t result; if( read_buffer != RT_NULL )
result = device->init(device); {
if (result != RT_EOK) rt_free(read_buffer);
{ }
rt_kprintf("To initialize device:%s failed. The error code is %d\r\n", if( write_buffer != RT_NULL )
device->parent.name, result); {
return result; rt_free(write_buffer);
} }
else return RT_ERROR;
{ }
device->flag |= RT_DEVICE_FLAG_ACTIVATED;
} int device_test(const char * device_name)
} {
rt_device_t device = RT_NULL;
// step 3: device test
switch( device->type ) // step 1:find device
{ device = rt_device_find(device_name);
case RT_Device_Class_Block : if( device == RT_NULL)
rt_kprintf("block device!\r\n"); {
return _block_device_test(device); rt_kprintf("device %s: not found!\r\n");
default: return RT_ERROR;
rt_kprintf("unkown device type : %02X",device->type); }
return RT_ERROR;
} // step 2:init device
} if (!(device->flag & RT_DEVICE_FLAG_ACTIVATED))
{
#ifdef RT_USING_FINSH rt_err_t result;
#include <finsh.h> result = device->init(device);
FINSH_FUNCTION_EXPORT(device_test, e.g:device_test("sd0")); if (result != RT_EOK)
#endif {
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