- 论坛徽章:
- 0
|
使用O_DIRECT打开设备文件,结果写入的数据和读出的数据不一致,davinci的板子,linux内核版本 2.6.18,代码如下:
#define _GNU_SOURCE
#define _USE_GNU 1
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <sys/times.h>
#include <time.h>
#define bufferSize 512
#define offset 512*10
#define CARDPATH "/dev/mmcblk0"
#define READ_SIZE 512
int main()
{
int bResult,lseek_error;
int i,j,p;
char *data_buf, *data_out;
unsigned short * lendata, len;
long tryTimes = 0;
int hDevice;
void *buf,*buf1;
//char filename[] = "/dev/sdd1";
if ((hDevice = open(CARDPATH, O_RDWR | O_DIRECT)) < 0)
{
printf("open error");
return -1;
}
printf("SD DEVICE OPEN SUCCESS!\r\n" );
buf = (void *)memalign(0x1000, READ_SIZE);
data_buf=(char *)buf;
buf1 = (void *)memalign(0x1000, READ_SIZE);
data_out=(char *)buf1;
memset( data_buf, 0, 512 );
memset( data_out, 0, 512 );
for(i=0; i<100; i++)
{
memset(data_buf,0,512);
//printf("\n");
//printf("****************************************\n");
for(p=0; p<512; p++)
{
data_buf[p] = rand() % 256;
/*printf("%2.2x ", data_buf[p]);
if((p+1) % 16 == 0)
printf("\n");*/
}
//lseek( hDevice,-512*2,SEEK_END);
lseek( hDevice,512*520,SEEK_SET);
bResult = write(hDevice, data_buf, 512);
sync();/*同步磁盘操作*/
if( bResult == -1 )
{
//lstError = GetLastError( );
return -1;
}
sleep(1);
//lseek_error=lseek( hDevice,-512*2,SEEK_END);
lseek_error=lseek( hDevice,512*520,SEEK_SET);
if(lseek_error==-1)
{
printf("\n lseek error!\n");
}
read(hDevice,data_out,512);
if(memcmp(data_buf, data_out, 512))
{
printf("error %d\n", i);
/*for(p=0; p<512; p++)
{
printf("%2.2x ", data_out[p]);
if((p+1) % 16 == 0)
printf("\n");
}*/
}
}
return 0;
}
请高手指教! |
|