1.TQ2440按键资源
TXD1 -- GPH4
RXD1 -- GPH5
2.内核介绍
本驱动基于linux-tq2440,也即天嵌基于TQ2440移植的内核
3.原理
在移植的过程中,已经对2440的uart1做了初始化和tty相关的处理,并且在/dev目录下已经生成了对应的设备文件"ttySACx".
4.测试代码如下:
点击(
此处)折叠或打开
-
#include "stdio.h"
-
#include "string.h"
-
#include "malloc.h"
-
#include "sys/types.h"
-
#include "sys/stat.h"
-
#include "fcntl.h"
-
#include "unistd.h"
-
#include "termios.h"
-
-
-
#define BUFF_SIZE 100
-
-
int fd,s;
-
-
int main(void){
-
char rcvBuff[BUFF_SIZE],*rbuf;
-
char tmpDat;
-
char *msg = "uart init ready...\n";
-
int ret,i;
-
struct termios newtio;
-
struct termios oldtio;
-
-
fd = open("/dev/ttySAC1",O_RDWR|O_NOCTTY);
-
if(fd<0){
-
printf("open /dev/ttySAC1 error:%d\n",fd);
-
return -1;
-
}
-
-
printf("open /dev/ttySAC1 ok.\n");
-
-
if(tcgetattr(fd,&oldtio) != 0)
-
{
-
perror("SetupSerial 1");
-
return -1;
-
}
-
-
-
cfmakeraw(&newtio);
-
cfsetispeed(&newtio,B115200);
-
cfsetospeed(&newtio,B115200);
-
-
tcflush(fd,TCIOFLUSH);
-
-
if((tcsetattr(fd,TCSANOW,&newtio)) != 0)
-
{
-
perror("com set error");
-
return -1;
-
}
-
printf("set done!\n");
-
-
-
ret = write(fd,msg,strlen(msg));
-
if(ret<0){
-
printf("write error:%d",ret);
-
}
-
-
printf("uart ready for receiving data...\n");
-
-
i=0;
-
rbuf = rcvBuff;
-
tmpDat = 0;
-
memset(rcvBuff,0,sizeof(rcvBuff));
-
-
while(1){
-
-
ret = read(fd,&tmpDat,1);
-
if(ret<=0){
-
//printf("read error:%d\n",ret);
-
}
-
else{
-
rbuf[i++] = tmpDat;
-
if(tmpDat == '\r'){
-
printf("the data recvd completed.\n");
-
write(fd,rbuf,i);
-
break;
-
}
-
}
-
}
-
rbuf[i]=0;
-
if(*rbuf){
-
printf("read dat:%s\n",rbuf);
-
}
-
-
-
if((tcsetattr(fd,TCSANOW,&oldtio)) != 0)
-
{
-
perror("com set error");
-
return -1;
-
}
-
printf("set old config done!\n");
-
-
-
ret = close(fd);
-
if(ret < 0){
-
printf("close the device fail.\n");
-
}
-
return 0;
-
}
5.重要结构体
点击(此处)折叠或打开
-
struct termios {
-
tcflag_t c_iflag; /* input mode flags */
-
tcflag_t c_oflag; /* output mode flags */
-
tcflag_t c_cflag; /* control mode flags */
-
tcflag_t c_lflag; /* local mode flags */
-
cc_t c_line; /* line discipline */
-
cc_t c_cc[NCCS]; /* control characters */
-
};
该结构体用于设置串口属性。同时换配置有大量的操作函数以初始化串口(比如:cfsetispeed设置输入波特率),可自行翻阅相关文档
6.测试
(1)内核启动之后,查看/dev下的tty设备,如下:
可以看到dev下有三个串口:ttySAC0-ttySAC2,分别对应物理串口uart0-uart2,我们使用的是uart1,对应的设备文件为ttySAC1.
(2)运行结果
测试程序启动之后,串口打印字符“uart init ready...”,此时在串口工具的输入框内输入“hello world”,同时选择发送新行,也即字串末尾发送‘\r’.接收就认为是一个字串完成,具体可见程序。
点击发送之后,开发板接收到字串,同时将字串再次输出值串口工具!
阅读(5470) | 评论(0) | 转发(0) |