若该文为原创文章,转载请注明原文出处
因项目需要使用串口和单片机通信,所以专门调试了一下串口,记录下过程。
一、硬件和开发环境
2、开发环境: ubuntu16.04-64bit
3、编译器:mips-gcc540-glibc222-32bit-r3.3.0.tar.gz
二、UART0测试
1、引脚定义
通过T31手册可以看出,引脚70和71为UART0,74和73为UART1, UART1用于DEBUG,
主要测试UART0
2、测试
通过命令ls /dev/ttyS0可以查看到UART0,所以使用的设备节点为/dev/ttyS0
测试方法有两种,板子固件默认的UART0波特轨是9200。
方法一:
短接串口的收发引脚
在shell终端输入:
# cat /dev/ttyS0 & # echo helloworld > /dev/ttyS0
讯享网
就可以在终端下看到输出信息了。

方法二:
使用TTL模块,把TTL的TX接引脚71,TTL的RX接引脚70,GND和板子共地。
打开调试串口软件,这里使用的是SSCOM,设置如下:

三、代码测试
创建comm_test.c文件
由于代码比较简单,直接附代码
讯享网/*! * * * Copyright ? 2017-2018 yifeng. All Rights Reserved. * * \file comm_test.c * \author yifeng * \version 1.0 * \date 2022年11月28日 * \brief sample-Ai * VX: *---------------------------------------------------------------------------- * \attention * * * */ /* change history: 1.date : 2022年11月28日 author: yifeng change: create file */ /*========================================================================================== 本源程序包括的头文件 建议:包含本项目的文件使用 #include "文件名.扩展名" , 包含系统库的文件使用 #include <文件名.扩展名> 。 ==========================================================================================*/ #include <unistd.h> #include <sys/ioctl.h> #include <sys/prctl.h> #include <string.h> #include <stdio.h> #include <stdlib.h> #include <math.h> #include <stdint.h> #include <errno.h> #include <pthread.h> #include <semaphore.h> #include <fcntl.h> #include <sys/time.h> #include "faac/faac.h" #include <imp/imp_audio.h> #include <imp/imp_log.h> #include <stdio.h> #include <stdlib.h> #include <strings.h> #include <string.h> #include <pthread.h> #include <unistd.h> #include <fcntl.h> #include <stdlib.h> #include <error.h> #include <termios.h> #include <malloc.h> #include <sys/types.h> #include <sys/stat.h> typedef struct termios termios_t; typedef struct serial_data{ char databuf[100];//发送/接受数据 int serfd;//串口文件描述符 }ser_Data; void *sersend(void *arg); void *serrecv(void *arg); int main(int argc,char *argv[]) { pthread_t pid1,pid2; pthread_attr_t *pthread_arr1,*pthread_arr2; pthread_arr1 = NULL; pthread_arr2 = NULL; int serport1fd; printf("==> main\r\n"); /* 进行串口参数设置 */ termios_t *ter_s = malloc(sizeof(*ter_s)); serport1fd = open("/dev/ttyS0",O_RDWR | O_NOCTTY | O_NDELAY);//不成为控制终端程序,不受其他程序输出输出影响 if(serport1fd < 0){ printf("%s open faild\r\n",argv[1]); return -1; } printf("open /dev/ttyS0 succeed\n"); bzero(ter_s,sizeof(*ter_s)); ter_s->c_cflag |= CLOCAL | CREAD; //激活本地连接与接受使能 ter_s->c_cflag &= ~CSIZE;//失能数据位屏蔽 ter_s->c_cflag |= CS8;//8位数据位 ter_s->c_cflag &= ~CSTOPB;//1位停止位 ter_s->c_cflag &= ~PARENB;//无校验位 ter_s->c_cc[VTIME] = 0; ter_s->c_cc[VMIN] = 0; /*1 VMIN> 0 && VTIME> 0 VMIN为最少读取的字符数,当读取到一个字符后,会启动一个定时器,在定时器超时事前,如果已经读取到了VMIN个字符,则read返回VMIN个字符。如果在接收到VMIN个字符之前,定时器已经超时,则read返回已读取到的字符,注意这个定时器会在每次读取到一个字符后重新启用,即重新开始计时,而且是读取到第一个字节后才启用,也就是说超时的情况下,至少读取到一个字节数据。 2 VMIN > 0 && VTIME== 0 在只有读取到VMIN个字符时,read才返回,可能造成read被永久阻塞。 3 VMIN == 0 && VTIME> 0 和第一种情况稍有不同,在接收到一个字节时或者定时器超时时,read返回。如果是超时这种情况,read返回值是0。 4 VMIN == 0 && VTIME== 0 这种情况下read总是立即就返回,即不会被阻塞。----by 解释粘贴自博客园 */ cfsetispeed(ter_s,B);//设置输入波特率 cfsetospeed(ter_s,B);//设置输出波特率 printf("==> cfsetospeed\r\n"); tcflush(serport1fd,TCIFLUSH);//刷清未处理的输入和/或输出 printf("==> tcflush\r\n"); if(tcsetattr(serport1fd,TCSANOW,ter_s) != 0){ printf("com set error!\r\n"); } printf("==> tcsetattr\r\n"); char buffer[] = {"hello my world!\r\n"}; char recvbuf[100] = {}; ser_Data snd_data; ser_Data rec_data; snd_data.serfd = serport1fd; rec_data.serfd = serport1fd; memcpy(snd_data.databuf,buffer,strlen(buffer));//拷贝发送数据 pthread_create(&pid1,pthread_arr1,sersend,(void *)&snd_data); pthread_create(&pid2,pthread_arr2,serrecv,(void *)&rec_data); ssize_t sizec; while(1){ usleep(); } pthread_join(pid1,NULL); pthread_join(pid2,NULL); free(ter_s); return 0; } void *sersend(void *arg)//串口发送线程函数 { ser_Data *snd = (ser_Data *)arg ; int ret; printf("==> sersend\r\n"); while(1){ printf("write\r\n"); ret = write(snd->serfd,snd->databuf,strlen(snd->databuf)); if(ret > 0){ printf("send success, data is %s\r\n",snd->databuf); }else{ printf("send error!\r\n"); } usleep(1000*1000*3); /* if(发生中断) break;//退出 */ } } void *serrecv(void *arg)//串口发送线程函数 { ser_Data *rec= (ser_Data *)arg ; int ret; printf("==> serrecv\r\n"); while(1){ ret = read(rec->serfd,rec->databuf,1024); if(ret > 0){ printf("recv success,recv size is %d \r\n",ret ); printf("recv databuf: %s\r\n", rec->databuf); }else{ /* 什么也不做 */ } usleep(1000); /* if(发生中断) break;//退出 */ } }
先设置串口参数,波特率为,然后创建接收和发送线程,发送线程每3秒发送一次数据,接收线程一直等待接收数据。
这里使用测试方法二,把串口0接到TTL模块上,TTL在接到电脑上。
打开SSCOM软件,修改波特率为。

四、总结
1、相对于海思,君正T31的串口使用还是挺方便的,途中没遇到要修改的,固件直接可以使用。
2、串口的使用相当常见,测试只是为了功能更快的集成。
如有侵权,请及时联系博主删除。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/104723.html