i.MX8MQ UART 串口不能通讯的问题

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

i.MX8MQ UART 串口不能通讯的问题

5,092 Views
lebeer
Contributor II

您好:

  在使用uart1-uart4时,发现的问题是串口只能发送数据i.MX8MQ不能收到从外部发来的数据。

开机时log:

[ 0.000000] Kernel command line: console=ttymxc0,115200 root=/dev/mmcblk0p2 rootwait rw
[ 0.316953] 30860000.serial: ttymxc0 at MMIO 0x30860000 (irq = 26, base_baud = 1562500) is a IMX
[ 1.264293] printk: console [ttymxc0] enabled
[ 1.269218] 30880000.serial: ttymxc2 at MMIO 0x30880000 (irq = 27, base_baud = 1562500) is a IMX
[ 1.278552] 30890000.serial: ttymxc1 at MMIO 0x30890000 (irq = 28, base_baud = 1562500) is a IMX
[ 1.287901] 30a60000.serial: ttymxc3 at MMIO 0x30a60000 (irq = 35, base_baud = 1562500) is a IMX

 

板:i.MX8MQ

内核版本:L5.4.47

系统:ubuntu

设备树:

pinctrl_uart1: uart1grp {
fsl,pins = <
MX8MQ_IOMUXC_UART1_RXD_UART1_DCE_RX 0x79
MX8MQ_IOMUXC_UART1_TXD_UART1_DCE_TX 0x79
>;
};
pinctrl_uart2: uart2grp {
fsl,pins = <
MX8MQ_IOMUXC_UART2_TXD_UART2_DCE_TX 0x49
MX8MQ_IOMUXC_UART2_RXD_UART2_DCE_RX 0x49
>;
};

pinctrl_uart3: uart3grp {
fsl,pins = <
MX8MQ_IOMUXC_UART3_TXD_UART3_DCE_TX 0x49
MX8MQ_IOMUXC_UART3_RXD_UART3_DCE_RX 0x49
>;
};

pinctrl_uart4: uart4grp {
fsl,pins = <
MX8MQ_IOMUXC_UART4_TXD_UART4_DCE_TX 0x49
MX8MQ_IOMUXC_UART4_RXD_UART4_DCE_RX 0x49
>;
};

&uart1 { /* console */
pinctrl-names = "default";
pinctrl-0 = <&pinctrl_uart1>;
assigned-clocks = <&clk IMX8MQ_CLK_UART1>;
assigned-clock-parents = <&clk IMX8MQ_CLK_25M>;
status = "okay";
};

&uart2 {
pinctrl-names = "default";
pinctrl-0 = <&pinctrl_uart2>;
assigned-clocks = <&clk IMX8MQ_CLK_UART2>;
assigned-clock-parents = <&clk IMX8MQ_SYS1_PLL_80M>;
fsl,uart-has-rtscts;
status = "okay";
};

&uart3 {
pinctrl-names = "default";
pinctrl-0 = <&pinctrl_uart3>;
assigned-clocks = <&clk IMX8MQ_CLK_UART3>;
assigned-clock-parents = <&clk IMX8MQ_SYS1_PLL_80M>;
fsl,uart-has-rtscts;
status = "okay";
};

&uart4 {
pinctrl-names = "default";
pinctrl-0 = <&pinctrl_uart4>;
assigned-clocks = <&clk IMX8MQ_CLK_UART4>;
assigned-clock-parents = <&clk IMX8MQ_SYS1_PLL_80M>;
fsl,uart-has-rtscts;
status = "okay";
};

打印输出:./a.out -d /dev/ttymxc3 -b 115200 -s 123
/dev/ttymxc3 RECV 512 total
/dev/ttymxc3 RECV:
/dev/ttymxc3 RECV 512 total
/dev/ttymxc3 RECV:
/dev/ttymxc3 RECV 512 total
/dev/ttymxc3 RECV:
/dev/ttymxc3 RECV 512 total
/dev/ttymxc3 RECV:
/dev/ttymxc3 RECV 512 total
/dev/ttymxc3 RECV:
/dev/ttymxc3 RECV 512 total
/dev/ttymxc3 RECV:
/dev/ttymxc3 RECV 512 total
/dev/ttymxc3 RECV:
/dev/ttymxc3 RECV 512 total
/dev/ttymxc3 RECV:

 

 

 

串口应用:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <getopt.h>
#include <string.h>

#define FALSE 1
#define TRUE 0

char *recchr="We received:\"";

void print_usage();

int speed_arr[] = {
B921600, B460800, B230400, B115200, B57600, B38400, B19200,
B9600, B4800, B2400, B1200, B300,
};

int name_arr[] = {
921600, 460800, 230400, 115200, 57600, 38400, 19200,
9600, 4800, 2400, 1200, 300,
};

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);
}

if (i == 12){
printf("\tSorry, please set the correct baud rate!\n\n");
print_usage(stderr, 1);
}
}
/*
*@brief 设置串口数据位,停止位和效验位
*@param fd 类型 int 打开的串口文件句柄*
*@param databits 类型 int 数据位 取值 为 7 或者8*
*@param stopbits 类型 int 停止位 取值为 1 或者2*
*@param parity 类型 int 效验类型 取值为N,E,O,,S
*/
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; /* Clear parity enable */
options.c_iflag &= ~INPCK; /* Enable parity checking */
break;
case 'o':
case 'O':
options.c_cflag |= (PARODD | PARENB); /* 设置为奇效验*/
options.c_iflag |= INPCK; /* Disnable parity checking */
break;
case 'e':
case 'E':
options.c_cflag |= PARENB; /* Enable parity */
options.c_cflag &= ~PARODD; /* 转换为偶效验*/
options.c_iflag |= INPCK; /* Disnable parity checking */
break;
case 'S':
case 's': /*as no parity*/
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);
}
/* Set input parity option */
if (parity != 'n')
options.c_iflag |= INPCK;
options.c_cc[VTIME] = 150; // 15 seconds
options.c_cc[VMIN] = 0;

options.c_lflag &= ~(ECHO | ICANON);

tcflush(fd,TCIFLUSH); /* Update the options and do it NOW */
if (tcsetattr(fd,TCSANOW,&options) != 0) {
perror("SetupSerial 3");
return (FALSE);
}
return (TRUE);
}

/**
*@breif 打开串口
*/
int OpenDev(char *Dev)
{
int fd = open( Dev, O_RDWR ); //| O_NOCTTY | O_NDELAY
if (-1 == fd) { /*设置数据位数*/
perror("Can't Open Serial Port");
return -1;
} else
return fd;
}


/* The name of this program */
const char * program_name;

/* Prints usage information for this program to STREAM (typically
* stdout or stderr), and exit the program with EXIT_CODE. Does not
* return.
*/

void print_usage (FILE *stream, int exit_code)
{
fprintf(stream, "Usage: %s option [ dev... ] \n", program_name);
fprintf(stream,
"\t-h --help Display this usage information.\n"
"\t-d --device The device ttyS[0-3] or ttySCMA[0-1]\n"
"\t-b --baudrate Set the baud rate you can select\n"
"\t [230400, 115200, 57600, 38400, 19200, 9600, 4800, 2400, 1200, 300]\n"
"\t-s --string Write the device data\n");
exit(exit_code);
}

 

/*
*@breif main()
*/
int main(int argc, char *argv[])
{
int fd, next_option, havearg = 0;
char *device;
int i=0,j=0;
int nread; /* Read the counts of data */
char buff[512]; /* Recvice data buffer */
pid_t pid;
char *xmit = "1234567890"; /* Default send data */
int speed ;
const char *const short_options = "hd:s:b:";

const struct option long_options[] = {
{ "help", 0, NULL, 'h'},
{ "device", 1, NULL, 'd'},
{ "string", 1, NULL, 's'},
{ "baudrate", 1, NULL, 'b'},
{ NULL, 0, NULL, 0 }
};

program_name = argv[0];

do {
next_option = getopt_long (argc, argv, short_options, long_options, NULL);
switch (next_option) {
case 'h':
print_usage (stdout, 0);
case 'd':
device = optarg;
havearg = 1;
break;
case 'b':
speed = atoi(optarg);
break;

case 's':
xmit = optarg;
havearg = 1;
break;
case -1:
if (havearg) break;
case '?':
print_usage (stderr, 1);
default:
abort ();
}
}while(next_option != -1);

sleep(1);
fd = OpenDev(device);

if (fd > 0) {
set_speed(fd, speed);
} else {
fprintf(stderr, "Error opening %s: %s\n", device, strerror(errno));
exit(1);
}

if (set_Parity(fd,8,1,'N')== FALSE) {
fprintf(stderr, "Set Parity Error\n");
close(fd);
exit(1);
}

pid = fork();

if (pid < 0) {
fprintf(stderr, "Error in fork!\n");
} else if (pid == 0){
while(1) {
// printf("%s SEND: %s\n",device, xmit);
write(fd, xmit, strlen(xmit));
sleep(1);
i++;
}
exit(0);
} else {
while(1) {
nread = read(fd, buff, sizeof(buff));
if (nread > 0) {
buff[nread] = '\0';
printf("%s RECV %d total\n", device, nread);
printf("%s RECV: %s\n", device, buff);
}
}
}
close(fd);
exit(0);
}

 

13 Replies

3,707 Views
ZeroLee
Contributor I

您好,我也遇到了相同的问题。uart无法接收数据,发送正常。

sdma这边,做成.ko之后,也能顺利加载,但是串口仍然无法接收。

0 Kudos
Reply

4,994 Views
lebeer
Contributor II

./mxc_uart_test.out /dev/ttymxc3

---- Running < ./mxc_uart_test.out > test ----

/dev/ttymxc3 opened
Attributes set
Test: IOCTL Set
Data Written= Test
*** buffer overflow detected ***: ./mxc_uart_test.out terminated
Aborted

0 Kudos
Reply

5,008 Views
lebeer
Contributor II

Hi:

您好,去掉"fsl,uart-has-rtscts",也是一样的情况

0 Kudos
Reply

5,042 Views
jimmychan
NXP TechSupport
NXP TechSupport

please remove "fsl,uart-has-rtscts;" in your device tree uart setting

0 Kudos
Reply

4,964 Views
lebeer
Contributor II

您好:

  remove 

文件系统:

 

1、使用imx-image-multimedia-imx8mqevk.tar.bz2,uart正常通讯,收发正常。

 

2、使用自制的 ubuntu16.04.7 文件系统,只能发送,不能收到外部发来的数据。

0 Kudos
Reply

5,036 Views
lebeer
Contributor II

Hi:

您好,去掉"fsl,uart-has-rtscts",也是一样的情况

0 Kudos
Reply

5,025 Views
lebeer
Contributor II

lebeer_0-1642397016635.png

 

ubuntu 16.04.7 系统

0 Kudos
Reply

5,054 Views
lebeer
Contributor II

dmesg | grep "sdma"
[ 0.299378] imx-sdma 302c0000.sdma: Direct firmware load for imx/sdma/sdma-imx7d.bin failed with error -2
[ 0.299393] imx-sdma 302c0000.sdma: Falling back to sysfs fallback for: imx/sdma/sdma-imx7d.bin
[ 5.563325] imx-sdma 30bd0000.sdma: external firmware not found, using ROM firmware
[ 5.569269] imx-sdma 302c0000.sdma: loaded firmware 4.5

0 Kudos
Reply

5,056 Views
lebeer
Contributor II

您好:

 NXP Employee

是和这个链接有关系吗?

imx8mq evk sdma firmware not ready? - NXP Community

0 Kudos
Reply

5,086 Views
josephzhou1
Contributor V

good

Best Regards,
Joseph Zhou Jianhui / Senior Embedded Software Engineer, Singapore
0 Kudos
Reply

5,074 Views
lebeer
Contributor II

您知道是什么问题吗?

0 Kudos
Reply

3,682 Views
ZeroLee
Contributor I

我这边也出现了同样的问题,解决的方法如下:

1. sdam做成.ko进行加载

2. 使用对应版本yocto所使用的sdma-imx7d.bin

0 Kudos
Reply