本帖最后由 gjianw217 于 2015-9-23 00:52 编辑
这篇主要进行OK210的串口测试,主要分为硬件分析和软件测试
(一)硬件分析:
串口0
和串口1
的对应引脚分别如图所示,即我们通过TTL
转USB
数据线和电脑相连,进行数据通信,或通过引线与MCU
进行串口通信。主要连接的是3引脚(TXD)和8引脚(RXD)。 (二)软件测试:
OK210开发板,飞凌提供的源码中,已经为用户写好了串口驱动,具体可参见源码目录:
android-kernel-samsung-dev/driver/serial/samsung.c和android-kernel-samsung-dev/driver/serial/s5pv210.c
开发板上电后,打开串口终端,可见如下信息,说明已经成功加载了4个串口的驱动,具体如下图所示,它们的序号与开发板上的序号一一对应。
编写串口测试程序,源码如下,其中硬件连接串口0
(TTL转USB串口线,要交叉相接,白色接3,绿色接8),即打开串口0,并每隔1s,发送一帧数据。
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include <time.h>
- int set_opt(int fd,int nSpeed, int nBits, char nEvent, int nStop)
- {
- struct termios newtio,oldtio;
- if ( tcgetattr( fd,&oldtio) != 0)
- {
- perror("SetupSerial 1");
- return -1;
- }
- bzero( &newtio, sizeof( newtio ) );
- newtio.c_cflag |= CLOCAL | CREAD;
- newtio.c_cflag &= ~CSIZE;
- switch( nBits )
- {
- case 7:
- newtio.c_cflag |= CS7;
- break;
- case 8:
- newtio.c_cflag |= CS8;
- break;
- }
- switch( nEvent )
- {
- case 'O': //奇校验
- newtio.c_cflag |= PARENB;
- newtio.c_cflag |= PARODD;
- newtio.c_iflag |= (INPCK | ISTRIP);
- break;
- case 'E': //偶校验
- newtio.c_iflag |= (INPCK | ISTRIP);
- newtio.c_cflag |= PARENB;
- newtio.c_cflag &= ~PARODD;
- break;
- case 'N': //无校验
- newtio.c_cflag &= ~PARENB;
- break;
- }
- switch( nSpeed )
- {
- case 2400:
- cfsetispeed(&newtio, B2400);
- cfsetospeed(&newtio, B2400);
- break;
- case 4800:
- cfsetispeed(&newtio, B4800);
- cfsetospeed(&newtio, B4800);
- break;
- case 9600:
- cfsetispeed(&newtio, B9600);
- cfsetospeed(&newtio, B9600);
- break;
- case 115200:
- cfsetispeed(&newtio, B115200);
- cfsetospeed(&newtio, B115200);
- break;
- default:
- cfsetispeed(&newtio, B9600);
- cfsetospeed(&newtio, B9600);
- break;
- }
- if( nStop == 1 )
- {
- newtio.c_cflag &= ~CSTOPB;
- }
- else if ( nStop == 2 )
- {
- newtio.c_cflag |= CSTOPB;
- }
- newtio.c_cc[VTIME] = 0;
- newtio.c_cc[VMIN] = 0;
- tcflush(fd,TCIFLUSH);
- if((tcsetattr(fd,TCSANOW,&newtio))!=0)
- {
- perror("com set error");
- return -1;
- }
- printf("set done!n");
- return 0;
- }
- int open_port(int fd,int comport)
- {
- /*定义串口设备文件*/
- char *dev[]={"/dev/ttySAC0","/dev/ttySAC1","/dev/ttySAC2","/dev/ttySAC3"};
- long vdisable;
- if (comport==1)
- { fd = open( dev[0], O_RDWR|O_NOCTTY|O_NDELAY);
- if (-1 == fd)
- {
- perror("Can't Open Serial Port 0");
- return(-1);
- }
- else
- {
- printf("open %sn",dev[0]);
- }
- }
- else if(comport==2)
- { fd = open( dev[1], O_RDWR|O_NOCTTY|O_NDELAY);
- if (-1 == fd)
- {
- perror("Can't Open Serial Port 1");
- return(-1);
- }
- else
- {
- printf("open %sn",dev[1]);
- }
- }
- else if (comport==3)
- {
- fd = open( dev[2], O_RDWR|O_NOCTTY|O_NDELAY);
- if (-1 == fd)
- {
- perror("Can't Open Serial Port 2");
- return(-1);
- }
- else
- {
- printf("open %sn",dev[2]);
- }
- }
- else if (comport==4)
- {
- fd = open( dev[3], O_RDWR|O_NOCTTY|O_NDELAY);
- if (-1 == fd)
- {
- perror("Can't Open Serial Port 3");
- return(-1);
- }
- else
- {
- printf("open %sn",dev[3]);
- }
- }
- if(fcntl(fd, F_SETFL, 0)<0)
- {
- printf("fcntl failed!n");
- }
- else
- {
- printf("fcntl=%dn",fcntl(fd, F_SETFL,0));
- }
- if(isatty(STDIN_FILENO)==0)
- {
- printf("standard input is not a terminal devicen");
- }
- else
- {
- printf("isatty success!n");
- }
- printf("fd-open=%dn",fd);
- return fd;
- }
- /*************************************************************/
- int main(void)
- {
- int fd;
- int nread,i,nwrite;
- char buff[]="Hellotn";
- if((fd=open_port(fd,1))<0)/*1表示打开串口UART0,如果打开UART1,则为2*/
- {
- perror("open_port error");
- return;
- }
- if((i=set_opt(fd,115200,8,'N',1))<0)
- {
- perror("set_opt error");
- return;
- }
- printf("fd=%dn",fd);
- while(1)
- {
- nwrite=write(fd,buff,8);
- printf("nread=%d,%sn",nwrite,buff);
- sleep(1);
- }
- close(fd);
- return;
- }
最后的结果如下所示,上面是电脑上通过ttl转u***串口接收数据,开始是字符显示,后面是16进制显示,
有简单的乱码,不知是驱动的问题,还是接线不稳定的原因,下面是串口发送数据的记录。