1,vivado工程如图,已上传下载路径:(后续补充)
最多可以扩展16个axi_uart 232串口,约束到PL端的GPIO。
2,添加管脚和电平约束,生成顶层,编译生成.bit文件,然后导入到sdk,新建hellow测试工程
set_property SEVERITY {Warning} [get_drc_checks UCIO-1]
set_property SEVERITY {Warning} [get_drc_checks NSTD-1]
set_property PACKAGE_PIN Y11 [get_ports uart_rtl_0_rxd]
set_property PACKAGE_PIN U11 [get_ports uart_rtl_0_txd]
set_property PACKAGE_PIN Y10 [get_ports uart_rtl_1_rxd]
set_property PACKAGE_PIN AA9 [get_ports uart_rtl_1_txd]
set_property PACKAGE_PIN AB6 [get_ports uart_rtl_2_rxd]
set_property PACKAGE_PIN AB7 [get_ports uart_rtl_2_txd]
set_property PACKAGE_PIN AB17 [get_ports uart_rtl_3_rxd]
set_property PACKAGE_PIN AB9 [get_ports uart_rtl_3_txd]
set_property PACKAGE_PIN AB11 [get_ports uart_rtl_4_rxd]
set_property PACKAGE_PIN W10 [get_ports uart_rtl_4_txd]
set_property PACKAGE_PIN AB10 [get_ports uart_rtl_5_rxd]
set_property PACKAGE_PIN W22 [get_ports uart_rtl_5_txd]
3,点击system.mss ->Modify this BSP's Settings
4, 选择在sdk要测试的串口,然后run as下载运行,接上串口助手看打印。
5,添加内核驱动:按下图设置,保存退出
Device Drivers->Character Devices->Serial driver->
<*>xilinx uartlite serial port support ->
<*>support for console on xilinx uartlite serial port
6,交叉编译如下串口程序,arm-linux-gnueabihf-gcc uart_test.c -pthread -o uart_test,
将启动文件和可执行程序放入sd卡,启动起来以后,可以在/dev/下看到ttyUL*,就是扩展的串口
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <pthread.h>
#include <sys/ioctl.h>
#define FALSE 1
#define TRUE 0
int fd=-1;
char buff[512];
int speed_arr[] = { B115200, B57600, B38400, B19200, B9600, B4800,B2400, B1200};
int name_arr[] = {115200, 57600, 38400, 19200, 9600, 4800, 2400, 1200};
void set_speed(int fd, int speed)
{
int i;
int status;
struct termios Opt;
tcgetattr(fd, &Opt);
for ( i= 0; i < sizeof(speed_arr) / sizeof(int); i++)
{
if (speed == name_arr[i])
{
tcflush(fd, TCIOFLUSH);
cfsetispeed(&Opt, speed_arr[i]);
cfsetospeed(&Opt, speed_arr[i]);
status = tcsetattr(fd, TCSANOW, &Opt);
if (status != 0)
perror("tcsetattr fd1");
return;
}
tcflush(fd,TCIOFLUSH);
}
}
int set_Parity(int fd,int databits,int stopbits,int parity)
{
struct termios options;
if ( tcgetattr( fd,&options) != 0)
{
perror("SetupSerial 1");
return(FALSE);
}
options.c_cflag &= ~CSIZE;
switch (databits)
{
case 7:
options.c_cflag |= CS7;
break;
case 8:
options.c_cflag |= CS8;
break;
default:
fprintf(stderr,"Unsupported data size\n");
return (FALSE);
}
switch (parity)
{
case 'n':
case 'N':
options.c_cflag &= ~PARENB;
options.c_iflag &= ~INPCK;
break;
case 'o':
case 'O':
options.c_cflag |= (PARODD | PARENB);
options.c_iflag |= INPCK;
break;
case 'e':
case 'E':
options.c_cflag |= PARENB;
options.c_cflag &= ~PARODD;
options.c_iflag |= INPCK;
break;
case 'S':
case 's':
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
break;
default:
fprintf(stderr,"Unsupported parity\n");
return (FALSE);
}
switch (stopbits)
{
case 1:
options.c_cflag &= ~CSTOPB;
break;
case 2:
options.c_cflag |= CSTOPB;
break;
default:
fprintf(stderr,"Unsupported stop bits\n");
return (FALSE);
}
options.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
options.c_oflag &= ~OPOST;
options.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
/* Set input parity option */
if (parity != 'n')
options.c_iflag |= INPCK;
options.c_cc[VTIME] = 150; // 15 seconds
options.c_cc[VMIN] = 0;
tcflush(fd,TCIFLUSH); /* Update the options and do it NOW */
if (tcsetattr(fd,TCSANOW,&options) != 0)
{
perror("SetupSerial 3");
return (FALSE);
}
return (TRUE);
}
void receivethread(void)
{
int nread;
while(1)
{
if((nread = read(fd,buff,100))>0) //接收数据
{
printf("[RECEIVE] Len is %d,content is :\n",nread);
buff[nread]='\0';
printf("%s\n",buff);
}
usleep(100/**1000*/);
}
return;
}
int main(int argc, char *argv[])
{
char str[500];
pthread_t receiveid;
int c, ctrlbits;
/*
参数个数小于1则返回,按如下方式执行:
./uart_test /dev/ttyAT1
*/
if (argc < 2)
{
printf("Useage: %s dev\n", argv[0]);
exit(0);
}
printf("test\n");
fd = open(argv[1], O_RDWR);
if (fd < 0)
{
printf("open device %s faild\n", argv[1]);
exit(0);
}
set_speed(fd,115200); //设置串口波特率
set_Parity(fd,8,1,'N'); //设置8位数据位,1位停止位,无校验等其他设置。
pthread_create(&receiveid,NULL,(void*)receivethread,NULL);//创建接收线程
while(1)
{
printf("Please Input string to send to %s\n:",argv[1]);
scanf("%s", str);
if(strlen(str)>0)//发送数据
{
write(fd, str, strlen(str));
write(fd, "\n", strlen("\n"));
usleep(200*1000);
}
}
close(fd);
exit(0);
}