Chinaunix首页 | 论坛 | 博客
  • 博客访问: 72905
  • 博文数量: 11
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 94
  • 用 户 组: 普通用户
  • 注册时间: 2015-12-11 15:27
个人简介

给自己一个城堡

文章分类
文章存档

2017年(3)

2016年(8)

我的朋友

分类: LINUX

2016-06-06 15:23:53

1.TQ2440按键资源
    TXD1  --  GPH4
    RXD1  --  GPH5
2.内核介绍
    本驱动基于linux-tq2440,也即天嵌基于TQ2440移植的内核
3.原理
   在移植的过程中,已经对2440的uart1做了初始化和tty相关的处理,并且在/dev目录下已经生成了对应的设备文件"ttySACx".
4.测试代码如下:
点击(此处)折叠或打开
  1. #include "stdio.h"
  2. #include "string.h"
  3. #include "malloc.h"
  4. #include "sys/types.h"
  5. #include "sys/stat.h"
  6. #include "fcntl.h"
  7. #include "unistd.h"
  8. #include "termios.h"


  9. #define BUFF_SIZE 100

  10. int fd,s;

  11. int main(void){
  12.     char rcvBuff[BUFF_SIZE],*rbuf;
  13.     char tmpDat;
  14.     char *msg = "uart init ready...\n";
  15.     int ret,i;
  16.     struct termios newtio;
  17.     struct termios oldtio;

  18.     fd = open("/dev/ttySAC1",O_RDWR|O_NOCTTY);
  19.     if(fd<0){
  20.         printf("open /dev/ttySAC1 error:%d\n",fd);
  21.         return -1;
  22.     }

  23.     printf("open /dev/ttySAC1 ok.\n");

  24.     if(tcgetattr(fd,&oldtio) != 0)
  25.     {
  26.         perror("SetupSerial 1");
  27.         return -1;
  28.     }

  29.     
  30.     cfmakeraw(&newtio);
  31.     cfsetispeed(&newtio,B115200);
  32.     cfsetospeed(&newtio,B115200);

  33.     tcflush(fd,TCIOFLUSH);

  34.     if((tcsetattr(fd,TCSANOW,&newtio)) != 0)
  35.     {
  36.         perror("com set error");
  37.         return -1;
  38.     }
  39.     printf("set done!\n");


  40.     ret = write(fd,msg,strlen(msg));
  41.     if(ret<0){
  42.         printf("write error:%d",ret);
  43.     }

  44.     printf("uart ready for receiving data...\n");

  45.     i=0;
  46.     rbuf = rcvBuff;
  47.     tmpDat = 0;
  48.     memset(rcvBuff,0,sizeof(rcvBuff));

  49.     while(1){

  50.         ret = read(fd,&tmpDat,1);
  51.         if(ret<=0){
  52.             //printf("read error:%d\n",ret);
  53.         }
  54.         else{
  55.             rbuf[i++] = tmpDat;
  56.             if(tmpDat == '\r'){
  57.                 printf("the data recvd completed.\n");
  58.                 write(fd,rbuf,i);
  59.                 break;
  60.             }
  61.         }
  62.     }
  63.     rbuf[i]=0;
  64.     if(*rbuf){
  65.         printf("read dat:%s\n",rbuf);
  66.     }


  67.     if((tcsetattr(fd,TCSANOW,&oldtio)) != 0)
  68.     {
  69.         perror("com set error");
  70.         return -1;
  71.     }
  72.     printf("set old config done!\n");

  73.   
  74.     ret = close(fd);
  75.     if(ret < 0){
  76.         printf("close the device fail.\n");
  77.     }
  78.     return 0;
  79. }
5.重要结构体

点击(此处)折叠或打开

  1. struct termios {
  2.     tcflag_t c_iflag;        /* input mode flags */
  3.     tcflag_t c_oflag;        /* output mode flags */
  4.     tcflag_t c_cflag;        /* control mode flags */
  5.     tcflag_t c_lflag;        /* local mode flags */
  6.     cc_t c_line;            /* line discipline */
  7.     cc_t c_cc[NCCS];        /* control characters */
  8. };
    该结构体用于设置串口属性。同时换配置有大量的操作函数以初始化串口(比如:cfsetispeed设置输入波特率),可自行翻阅相关文档
6.测试
(1)内核启动之后,查看/dev下的tty设备,如下:

    可以看到dev下有三个串口:ttySAC0-ttySAC2,分别对应物理串口uart0-uart2,我们使用的是uart1,对应的设备文件为ttySAC1.
(2)运行结果

    测试程序启动之后,串口打印字符“uart init ready...”,此时在串口工具的输入框内输入“hello world”,同时选择发送新行,也即字串末尾发送‘\r’.接收就认为是一个字串完成,具体可见程序。
    点击发送之后,开发板接收到字串,同时将字串再次输出值串口工具!

    



阅读(5392) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~