Linux device drivers学习笔记(6)——uart primary driver testing code

根据现阶段测试代码的反映来看,uart driver的性能还算可以。

#include <stdio.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/wait.h>
#define N 128
void Delay(unsigned int t);
int main(int argc, char **argv)
{
int fd,send_n,recv_n,pipe_fd[2];
pid_t pid;
char buffer[N+1];

fd = open("/dev/uart1",O_RDWR);  // open the device
if (fd < 0) 
{
printf("Can't open /dev/uart1\n");
return -1;
}
if (pipe(pipe_fd))
{
printf("Fail to create pipe!\n");
return -1;
}
pid = fork();
if(pid < 0)
{
close(fd);
printf("uart1 has been closed!\n");
}
else if (pid == 0) //child process---read process
{
close(pipe_fd[0]); //close read end point
while(1)
{
recv_n = read(fd,buffer,N);
if(recv_n)
{
send_n = write(pipe_fd[1],buffer,recv_n);
if(send_n != recv_n)
printf("child bug,send_n=%d,recv_n=%d\n",send_n,recv_n);
if(buffer[0] == 'q')
break;
}
}
}
else //parent process---write process
{
close(pipe_fd[1]); //close write end point
while(1)
{
recv_n = read(pipe_fd[0],buffer,N);
if(recv_n)
{
send_n = write(fd,buffer,recv_n);
Delay(100);
if(send_n != recv_n)
printf("parent bug,send_n=%d,recv_n=%d\n",send_n,recv_n);
if(send_n)
{
ioctl(fd,1); //waiting for a while is necessary.
}
if(buffer[0] == 'q')
break;
}
}
}
while(!ioctl(fd,2))
{
;
}
close(fd);
printf("uart1 has been closed!\n");
return 0;
}
void Delay(unsigned int t)
{
unsigned int i,j;
for(i = 0;i < t;i++)
{
for(j = 0;j < 100;j++)
{
;
}
}

}


附带makefile文件:

CROSS=arm-linux-
TARGET=uart1_test


$(TARGET):uart1_test.c
$(CROSS)gcc -o $@ $< -static


clean:
@rm -rf $(TARGET) *.o

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值