问题如下:使用pc端串口调试助手调试am335x的uart4,am335x向外发送数据,PC端可接收到,但是通过PC端调试助手发送给am335x,am335x一直读取数据失败。而且串口调试助手本身会直接收到已发送的数据。
测试代码如下:
#include <stdio.h>
#include <stdlib.h> /*标准函数库定义*/
#include <unistd.h> /*Unix标准函数定义*/
#include <sys/types.h> /**/
#include <sys/stat.h> /**/
#include <fcntl.h> /*文件控制定义*/
#include <termios.h> /*PPSIX终端控制定义*/
#include <errno.h> /*错误号定义*/
int fd = -1;
int open_port(char port[])
{
fd = open( port, O_RDWR | O_NONBLOCK );
if (fd<0) {
printf("open serialport failed!\n");
return -1;
}
struct termios tio;
tcgetattr(fd, &tio);
//bzero(&tio, sizeof(tio));
// these must be set first, don't know why
tio.c_cflag |= CLOCAL;
tio.c_cflag |= CREAD;
tio.c_cflag &= ~CSIZE;
// baud rate 115200
if (cfsetispeed(&tio, B115200) != 0) {
printf("cfsetispeed failed\n");
return -1;
}
if (cfsetospeed(&tio, B115200) != 0)
{
printf("cfsetospeed failed\n");
return -1;
}
// parity NONE
tio.c_cflag &= ~PARENB;
tio.c_iflag &= ~INPCK;
tio.c_iflag |= IGNBRK;
// data bit 8
tio.c_cflag |= CS8;
// stop bit 1
tio.c_cflag &= ~CSTOPB;
// others
tio.c_cc[VTIME] = 0;
tio.c_cc[VMIN] = 0;
// flush settings
if(tcflush(fd, TCIOFLUSH) != 0)
{
printf("open serialport: Flushing %s ERROR!\n\n", port);
return -1;
}
if(tcsetattr(fd, TCSANOW, &tio) != 0)
{
printf("open serialport: Setting %s ERROR!\n\n", port);
return -1;
}
return 0;
}
int main(int argc, char* argv[])
{
char port_num[10] = {0,0,0,0,0,0,0,0,0,0};
char wr_num[2] = {1,2};
int ret;
if(argc != 2)
{
printf("%s SerialPortDevFile\n", argv[0]);
return -1;
}
if(open_port(argv[1]) != 0)
{
printf("Fail to open %s!\n", argv[1]);
return -2;
}
else
{
while(1)
{
printf( "start read %s\n", argv[1] );
ret = read( fd, (void *)port_num, 9 );
if( ret == -1 )
printf( "ret = -1\n" );
else
{
printf("ret = %d\n", ret);
printf( "%s received : %s\n", argv[1], port_num );
}
printf( "start write %s\n", argv[1] );
ret = write( fd, (void *)wr_num, 2 );
sleep(5);
}
close_com();
return 0;
}
}
很简单的程序,只是循环接收发送。dts中配置uart4如下:
&uart4 {
pinctrl-names = "default", "sleep";
pinctrl-0 = <&uart4_pins_default>;
pinctrl-1 = <&uart4_pins_sleep>;
status = "okay";
};
uart4_pins_default: pinmux_uart4_pins_default {
pinctrl-single,pins = <
0x168 (PIN_INPUT_PULLUP | MUX_MODE1) /* uart0_ctsn.uart4_rxd */
0x16C (PIN_OUTPUT_PULLDOWN | MUX_MODE1) /* uart0_rtsn.uart4_txd */
>;
};
uart4_pins_sleep: pinmux_uart4_pins_sleep {
pinctrl-single,pins = <
0x168 (PIN_INPUT_PULLDOWN | MUX_MODE7)
0x16C (PIN_INPUT_PULLDOWN | MUX_MODE7)
>;
};
硬件上,uart4的收发信号线 直接 接到 PC端 usb转串口(232)线,信号线上没有连接任何上下拉等。