20240611在飞凌OK3588-C核心板的Linux系统下打通UART0和UART3
2024/6/11 7:45
Z:\OK3588_Linux_fs\kernel\arch\arm64\boot\dts\rockchip\OK3588-C-common.dtsi
&pinctrl {
m2 {
net_m2_rst_gpio: net_m2_rst_gpio {
rockchip,pins = <1 RK_PC6 RK_FUNC_GPIO &pcfg_pull_none>;
};
};
bt {
bluetooth_wake_pin: bluetooth-wake-pin {
rockchip,pins = <1 RK_PA4 RK_FUNC_GPIO &pcfg_pull_none>;
};
};
tf {
sdmmc_pwren_gpio: sdmmc-pwren-gpio {
rockchip,pins = <2 RK_PC4 RK_FUNC_GPIO &pcfg_pull_none>;
};
};
lcd {
mipi0_enable_gpio: mipi0-enable-gpio {
rockchip,pins = <3 RK_PB2 RK_FUNC_GPIO &pcfg_pull_none>;
};
mipi1_enable_gpio: mipi1-enable-gpio {
rockchip,pins = <3 RK_PC6 RK_FUNC_GPIO &pcfg_pull_none>;
};
};
tp_int {
ft5x06_dsi1_gpio: ft5x06-dsi1-gpio {
rockchip,pins = <3 RK_PC0 RK_FUNC_GPIO &pcfg_pull_none>;
};
ft5x06_dsi2_gpio: ft5x06-dsi2-gpio {
rockchip,pins = <3 RK_PD3 RK_FUNC_GPIO &pcfg_pull_none>;
};
gt911_dsi1_gpio:gt911-dsi0-gpio {
rockchip,pins = <3 RK_PB7 RK_FUNC_GPIO &pcfg_pull_none>,
<3 RK_PC0 RK_FUNC_GPIO &pcfg_pull_none>;
};
gt911_dsi2_gpio:gt911-dsi1-gpio {
rockchip,pins = <3 RK_PD2 RK_FUNC_GPIO &pcfg_pull_none>,
<3 RK_PD3 RK_FUNC_GPIO &pcfg_pull_none>;
};
};
hdmirxdet {
hdmirx_det_gpio: hdmirx-det-gpio {
rockchip,pins = <1 RK_PD5 RK_FUNC_GPIO &pcfg_pull_up>;
};
};
// extio {
// extio_int_gpio: extio-int-gpio {
// rockchip,pins = <1 RK_PA4 RK_FUNC_GPIO &pcfg_pull_up>;
// };
// };
usb {
vcc5v0_host_en: vcc5v0-host-en {
rockchip,pins = <4 RK_PB0 RK_FUNC_GPIO &pcfg_pull_none>;
};
};
usb-typec {
usbc0_int: usbc0-int {
rockchip,pins = <1 RK_PB0 RK_FUNC_GPIO &pcfg_pull_up>;
};
typec5v_pwren0: typec5v-pwren0 {
rockchip,pins = <0 RK_PD0 RK_FUNC_GPIO &pcfg_pull_none>;
};
usbc1_int: usbc1-int {
rockchip,pins = <1 RK_PB3 RK_FUNC_GPIO &pcfg_pull_up>;
};
typec5v_pwren1: typec5v-pwren1 {
rockchip,pins = <0 RK_PD3 RK_FUNC_GPIO &pcfg_pull_none>;
};
};
headphone {
hp_det: hp-det {
rockchip,pins = <1 RK_PB2 RK_FUNC_GPIO &pcfg_pull_none>;
};
};
};
&uart0 {
status = "okay";
pinctrl-names = "default";
pinctrl-0 = <&uart0m2_xfer>;
};
&uart3 {
status = "okay";
pinctrl-names = "default";
pinctrl-0 = <&uart3m2_xfer>;
};
&uart4 {
pinctrl-names = "default";
pinctrl-0 = <&uart4m0_xfer>;
status = "okay";
};
&uart6 {
pinctrl-0 = <&uart6m1_xfer>, <&uart6m1_ctsn>, <&uart6m1_rtsn>;
status = "okay";
};
&uart9 {
pinctrl-names = "default";
pinctrl-0 = <&uart9m2_xfer>;
status = "okay";
};
&can1 {
pinctrl-0 = <&can1m1_pins>;
status = "okay";
assigned-clocks = <&cru CLK_CAN1>;
assigned-clock-rates = <200000000>;
};
为了防止GPIO的复用干扰,可选关闭:
GPIO3A3
GPIO3A4
GPIO3A5
GPIO3A6
Z:\OK3588_Linux_fs\kernel\arch\arm64\boot\dts\rockchip\rk3588s.dtsi
//i2s1_8ch: i2s@fe480000 {
// compatible = "rockchip,rk3588-i2s-tdm";
// reg = <0x0 0xfe480000 0x0 0x1000>;
// interrupts = <GIC_SPI 181 IRQ_TYPE_LEVEL_HIGH>;
// clocks = <&cru MCLK_I2S1_8CH_TX>, <&cru MCLK_I2S1_8CH_RX>, <&cru HCLK_I2S1_8CH>;
// clock-names = "mclk_tx", "mclk_rx", "hclk";
// dmas = <&dmac0 2>, <&dmac0 3>;
// dma-names = "tx", "rx";
// resets = <&cru SRST_M_I2S1_8CH_TX>, <&cru SRST_M_I2S1_8CH_RX>;
// reset-names = "tx-m", "rx-m";
// rockchip,clk-trcm = <1>;
// pinctrl-names = "default";
// pinctrl-0 = <&i2s1m0_lrck
// &i2s1m0_sclk
// &i2s1m0_sdi0
// &i2s1m0_sdi1
// &i2s1m0_sdi2
// &i2s1m0_sdi3
// &i2s1m0_sdo0
// &i2s1m0_sdo1
// &i2s1m0_sdo2
// &i2s1m0_sdo3>;
// #sound-dai-cells = <0>;
// status = "disabled";
//};
为了防止GPIO的复用干扰,可选关闭:
Z:\OK3588_Linux_fs\kernel\arch\arm64\boot\dts\rockchip\rk3588s-pinctrl.dtsi
【还有很多!】
bt1120 {
/omit-if-no-ref/
bt1120_pins: bt1120-pins {
rockchip,pins =
/* bt1120_clkout */
<4 RK_PB0 2 &pcfg_pull_none>,
/* bt1120_d0 */
<4 RK_PA0 2 &pcfg_pull_none>,
/* bt1120_d1 */
<4 RK_PA1 2 &pcfg_pull_none>,
/* bt1120_d2 */
<4 RK_PA2 2 &pcfg_pull_none>,
/* bt1120_d3 */
//<4 RK_PA3 2 &pcfg_pull_none>,
/* bt1120_d4 */
//<4 RK_PA4 2 &pcfg_pull_none>,
/* bt1120_d5 */
//<4 RK_PA5 2 &pcfg_pull_none>,
/* bt1120_d6 */
//<4 RK_PA6 2 &pcfg_pull_none>,
/* bt1120_d7 */
<4 RK_PA7 2 &pcfg_pull_none>,
/* bt1120_d8 */
<4 RK_PB2 2 &pcfg_pull_none>,
/* bt1120_d9 */
<4 RK_PB3 2 &pcfg_pull_none>,
/* bt1120_d10 */
<4 RK_PB4 2 &pcfg_pull_none>,
/* bt1120_d11 */
<4 RK_PB5 2 &pcfg_pull_none>,
/* bt1120_d12 */
<4 RK_PB6 2 &pcfg_pull_none>,
/* bt1120_d13 */
<4 RK_PB7 2 &pcfg_pull_none>,
/* bt1120_d14 */
<4 RK_PC0 2 &pcfg_pull_none>,
/* bt1120_d15 */
<4 RK_PC1 2 &pcfg_pull_none>;
};
};
参考资料:
调试工具:
cat /proc/tty/driver/serial
root@ok3588:/#
root@ok3588:/# cat proc/tty/driver/serial
serinfo:1.0 driver revision:
0: uart:16550A mmio:0xFD890000 irq:28 tx:0 rx:0
3: uart:16550A mmio:0xFEB60000 irq:99 tx:138 rx:233 fe:91 brk:3 RTS|DTR
4: uart:16550A mmio:0xFEB70000 irq:100 tx:0 rx:0
6: uart:16550A mmio:0xFEB90000 irq:101 tx:0 rx:0
9: uart:16550A mmio:0xFEBC0000 irq:102 tx:0 rx:0
root@ok3588:/#
root@ok3588:/#
cat /sys/kernel/debug/pinctrl/pinctrl-rockchip-pinctrl/pinmux-pins
pin 131 (gpio4-3): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 132 (gpio4-4): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 133 (gpio4-5): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 134 (gpio4-6): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 131 (gpio4-3): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 132 (gpio4-4): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 133 (gpio4-5): (MUX UNCLAIMED) gpio4:133
pin 134 (gpio4-6): (MUX UNCLAIMED) gpio4:134
修改之后:
pin 130 (gpio4-2): (MUX UNCLAIMED) gpio4:130
pin 131 (gpio4-3): fd890000.serial (GPIO UNCLAIMED) function uart0 group uart0m2-xfer
pin 132 (gpio4-4): fd890000.serial (GPIO UNCLAIMED) function uart0 group uart0m2-xfer
pin 133 (gpio4-5): feb60000.serial (GPIO UNCLAIMED) function uart3 group uart3m2-xfer
pin 134 (gpio4-6): feb60000.serial (GPIO UNCLAIMED) function uart3 group uart3m2-xfer
pin 135 (gpio4-7): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 136 (gpio4-8): (MUX UNCLAIMED) gpio4:136
# 设置波特率
stty -F /dev/ttyS3 115200
# 写入数据到串口
echo "Hello, serial port!" > /dev/ttyS3
cat /dev/ttyS3
root@ok3588:/mnt# fltest_uarttest -d /dev/ttyS3
Welcome to uart test
Send test data:
forlinx_uart_test.1234567890...
343
Read Test Data finished,Read:
t?????????????????
root@ok3588:/mnt# 343
sh: 343: command not found
root@ok3588:/mnt# fltest_uarttest -d /dev/ttyS3
Welcome to uart test
Send test data:
forlinx_uart_test.1234567890...
Read Test Data finished,Read:
t?????????????????
root@ok3588:/mnt#
https://zhuanlan.zhihu.com/p/664344987
Banana Pi BPI-W3(Armsom W3)RK3588开当板之调试UART
/home/forlinx/aarch64-buildroot-linux-gnu_sdk-buildroot/bin/aarch64-buildroot-linux-gnu-gcc
viewpro@viewpro-ThinkBook-16-G5-IRH:~$
viewpro@viewpro-ThinkBook-16-G5-IRH:~$ find . -name aarch64-buildroot-linux-gnu-gcc
find: ‘./fuse/sd-fuse_h3/01该乱了rootfs/var/lib/apt/lists/partial’: Permission denied
find: ‘./fuse/sd-fuse_h3/rootfs/var/lib/apt/lists/partial’: Permission denied
./28四次该乱了repo_RK3588_Buildroot20240508/buildroot/output/rockchip_rk3588/host/bin/aarch64-buildroot-linux-gnu-gcc
./28四次该乱了repo_RK3588_Buildroot20240508/buildroot/output/rockchip_rk3588_recovery/host/bin/aarch64-buildroot-linux-gnu-gcc
./25改乱了repo_RK3588_Buildroot20240508/buildroot/output/rockchip_rk3588/host/bin/aarch64-buildroot-linux-gnu-gcc
^C
viewpro@viewpro-ThinkBook-16-G5-IRH:~$ cd repo_RK3588_Buildroot20240508/
viewpro@viewpro-ThinkBook-16-G5-IRH:~/repo_RK3588_Buildroot20240508$
viewpro@viewpro-ThinkBook-16-G5-IRH:~/repo_RK3588_Buildroot20240508$
viewpro@viewpro-ThinkBook-16-G5-IRH:~/repo_RK3588_Buildroot20240508$ find . -name aarch64-buildroot-linux-gnu-gcc
./buildroot/output/rockchip_rk3588/host/bin/aarch64-buildroot-linux-gnu-gcc
./buildroot/output/rockchip_rk3588_recovery/host/bin/aarch64-buildroot-linux-gnu-gcc
viewpro@viewpro-ThinkBook-16-G5-IRH:~/repo_RK3588_Buildroot20240508$ ll *.c
-rwx------ 1 viewpro viewpro 7263 6月 11 10:48 uart_test.c*
viewpro@viewpro-ThinkBook-16-G5-IRH:~/repo_RK3588_Buildroot20240508$
viewpro@viewpro-ThinkBook-16-G5-IRH:~/repo_RK3588_Buildroot20240508$
viewpro@viewpro-ThinkBook-16-G5-IRH:~/repo_RK3588_Buildroot20240508$ ./buildroot/output/rockchip_rk3588/host/bin/aarch64-buildroot-linux-gnu-gcc uart_test.c
viewpro@viewpro-ThinkBook-16-G5-IRH:~/repo_RK3588_Buildroot20240508$ ll
total 2834224
drwxrwxr-x 16 viewpro viewpro 4096 6月 11 10:53 ./
drwxr-xr-x 39 viewpro viewpro 4096 6月 11 10:48 ../
-rw-rw-r-- 1 viewpro viewpro 967377482 6月 1 16:39 01原始的update.img
-rw-rw-r-- 1 viewpro viewpro 967377482 6月 7 18:28 02配置UART03了update.img
-rw-rw-r-- 1 viewpro viewpro 967377482 6月 7 19:25 08打开UART03了在核心板update.img
-rwxrwxr-x 1 viewpro viewpro 14512 6月 11 10:53 a.out*
drwxrwxr-x 4 viewpro viewpro 4096 5月 20 16:26 app/
drwxrwxr-x 18 viewpro viewpro 4096 6月 1 16:20 buildroot/
lrwxrwxrwx 1 viewpro viewpro 39 5月 20 16:26 build.sh -> device/rockchip/common/scripts/build.sh*
lrwxrwxrwx 1 viewpro viewpro 22 6月 1 15:24 common -> device/rockchip/common/
drwxrwxr-x 9 viewpro viewpro 4096 5月 20 16:26 debian/
drwxrwxr-x 3 viewpro viewpro 4096 5月 20 16:26 device/
drwxrwxr-x 6 viewpro viewpro 4096 5月 20 16:26 docs/
drwxrwxr-x 27 viewpro viewpro 4096 5月 20 16:27 external/
drwxrwxr-x 26 viewpro viewpro 4096 6月 7 19:24 kernel/
lrwxrwxrwx 1 viewpro viewpro 31 5月 20 16:26 Makefile -> device/rockchip/common/Makefile
drwxrwxr-x 9 viewpro viewpro 4096 6月 7 19:25 output/
drwxrwxr-x 3 viewpro viewpro 4096 5月 20 16:26 prebuilts/
lrwxrwxrwx 1 viewpro viewpro 32 6月 1 15:24 README.md -> device/rockchip/common/README.md
drwxrwxr-x 9 viewpro viewpro 4096 6月 1 15:25 rkbin/
lrwxrwxrwx 1 viewpro viewpro 41 5月 20 16:26 rkflash.sh -> device/rockchip/common/scripts/rkflash.sh*
lrwxrwxrwx 1 viewpro viewpro 15 6月 1 16:39 rockdev -> output/firmware/
drwxrwxr-x 5 viewpro viewpro 4096 5月 20 16:27 tools/
-rwx------ 1 viewpro viewpro 7263 6月 11 10:48 uart_test.c*
drwxrwxr-x 27 viewpro viewpro 4096 6月 1 15:25 u-boot/
drwxrwxr-x 5 viewpro viewpro 4096 5月 20 16:27 uefi/
drwxrwxr-x 9 viewpro viewpro 4096 5月 20 16:27 yocto/
viewpro@viewpro-ThinkBook-16-G5-IRH:~/repo_RK3588_Buildroot20240508$
viewpro@viewpro-ThinkBook-16-G5-IRH:~/repo_RK3588_Buildroot20240508$ ./buildroot/output/rockchip_rk3588/host/bin/aarch64-buildroot-linux-gnu-gcc uart_test.c -o uart_test
viewpro@viewpro-ThinkBook-16-G5-IRH:~/repo_RK3588_Buildroot20240508$ ll
total 2834240
drwxrwxr-x 16 viewpro viewpro 4096 6月 11 10:54 ./
drwxr-xr-x 39 viewpro viewpro 4096 6月 11 10:48 ../
-rw-rw-r-- 1 viewpro viewpro 967377482 6月 1 16:39 01原始的update.img
-rw-rw-r-- 1 viewpro viewpro 967377482 6月 7 18:28 02配置UART03了update.img
-rw-rw-r-- 1 viewpro viewpro 967377482 6月 7 19:25 08打开UART03了在核心板update.img
-rwxrwxr-x 1 viewpro viewpro 14512 6月 11 10:53 a.out*
drwxrwxr-x 4 viewpro viewpro 4096 5月 20 16:26 app/
drwxrwxr-x 18 viewpro viewpro 4096 6月 1 16:20 buildroot/
lrwxrwxrwx 1 viewpro viewpro 39 5月 20 16:26 build.sh -> device/rockchip/common/scripts/build.sh*
lrwxrwxrwx 1 viewpro viewpro 22 6月 1 15:24 common -> device/rockchip/common/
drwxrwxr-x 9 viewpro viewpro 4096 5月 20 16:26 debian/
drwxrwxr-x 3 viewpro viewpro 4096 5月 20 16:26 device/
drwxrwxr-x 6 viewpro viewpro 4096 5月 20 16:26 docs/
drwxrwxr-x 27 viewpro viewpro 4096 5月 20 16:27 external/
drwxrwxr-x 26 viewpro viewpro 4096 6月 7 19:24 kernel/
lrwxrwxrwx 1 viewpro viewpro 31 5月 20 16:26 Makefile -> device/rockchip/common/Makefile
drwxrwxr-x 9 viewpro viewpro 4096 6月 7 19:25 output/
drwxrwxr-x 3 viewpro viewpro 4096 5月 20 16:26 prebuilts/
lrwxrwxrwx 1 viewpro viewpro 32 6月 1 15:24 README.md -> device/rockchip/common/README.md
drwxrwxr-x 9 viewpro viewpro 4096 6月 1 15:25 rkbin/
lrwxrwxrwx 1 viewpro viewpro 41 5月 20 16:26 rkflash.sh -> device/rockchip/common/scripts/rkflash.sh*
lrwxrwxrwx 1 viewpro viewpro 15 6月 1 16:39 rockdev -> output/firmware/
drwxrwxr-x 5 viewpro viewpro 4096 5月 20 16:27 tools/
-rwxrwxr-x 1 viewpro viewpro 14512 6月 11 10:54 uart_test*
-rwx------ 1 viewpro viewpro 7263 6月 11 10:48 uart_test.c*
drwxrwxr-x 27 viewpro viewpro 4096 6月 1 15:25 u-boot/
drwxrwxr-x 5 viewpro viewpro 4096 5月 20 16:27 uefi/
drwxrwxr-x 9 viewpro viewpro 4096 5月 20 16:27 yocto/
viewpro@viewpro-ThinkBook-16-G5-IRH:~/repo_RK3588_Buildroot20240508$ file uart_test
uart_test: ELF 64-bit LSB shared object, ARM aarch64, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-aarch64.so.1, for GNU/Linux 3.7.0, not stripped
viewpro@viewpro-ThinkBook-16-G5-IRH:~/repo_RK3588_Buildroot20240508$ ll *.c
-rwx------ 1 viewpro viewpro 7263 6月 11 10:48 uart_test.c*
viewpro@viewpro-ThinkBook-16-G5-IRH:~/repo_RK3588_Buildroot20240508$
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <termio.h>
#include <time.h>
#include <pthread.h>
int read_data(int fd, void *buf, int len);
int write_data(int fd, void *buf, int len);
int setup_port(int fd, int baud, int databits, int parity, int stopbits);
void print_usage(char *program_name);
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t data_ready = PTHREAD_COND_INITIALIZER;
int data_available = 0;
void *read_thread(void *arg) {
int fd = *(int *)arg;
char buffer[1024]; // 存储读取的数据
while (1) {
int bytes_read = read_data(fd, buffer, sizeof(buffer));
if (bytes_read > 0) {
//printf("Read Thread: Read %d bytes: %s\n", bytes_read, buffer);
//printf("Read Thread: Read %d bytes: 0x%0x\n", bytes_read, buffer);
//printf("Read Thread: Read %d bytes: 0x%0x, 0x%0x, 0x%0x, 0x%0x, 0x%0x, \n", bytes_read, buffer[0], buffer[1], buffer[2], buffer[3], buffer[4], buffer[5] );
printf("Read Thread: Read %d bytes: 0x%0x, 0x%0x, 0x%0x, 0x%0x, 0x%0x, \n", bytes_read, buffer[0], buffer[1], buffer[2], buffer[3], buffer[4] );
buffer[0] = 0x55;
buffer[1] = 0xAA;
buffer[2] = 0x55;
buffer[3] = 0xAA;
buffer[4] = 0x55;
} else {
// 处理读取错误或设备关闭的情况
break;
}
}
pthread_exit(NULL);
}
void *write_thread(void *arg) {
int fd = *(int *)arg;
char input[1024]; // 存储用户输入的数据
while (1) {
printf("Enter data to write (or 'q' to quit): ");
fgets(input, sizeof(input), stdin);
if (strcmp(input, "q\n") == 0 || strcmp(input, "Q\n") == 0) {
// 用户输入 'q' 或 'Q',退出循环
break;
}
int len = strlen(input);
int bytes_written = write_data(fd, input, len);
if (bytes_written > 0) {
printf("Write Thread: Wrote %d bytes: %s\n", bytes_written, input);
}
}
pthread_exit(NULL);
}
int main(int argc, char *argv[]) //./a.out /dev/ttyS4 115200 8 0 1
{
int fd;
int baud;
int len;
int count;
int i;
int databits;
int stopbits;
int parity;
if (argc != 6) {
print_usage(argv[0]);
return 1;
}
baud = atoi(argv[2]);
if ((baud < 0) || (baud > 921600)) {
fprintf(stderr, "Invalid baudrate!\n");
return 1;
}
databits = atoi(argv[3]);
if ((databits < 5) || (databits > 8)) {
fprintf(stderr, "Invalid databits!\n");
return 1;
}
parity = atoi(argv[4]);
if ((parity < 0) || (parity > 2)) {
fprintf(stderr, "Invalid parity!\n");
return 1;
}
stopbits = atoi(argv[5]);
if ((stopbits < 1) || (stopbits > 2)) {
fprintf(stderr, "Invalid stopbits!\n");
return 1;
}
fd = open(argv[1], O_RDWR, 0);
if (fd < 0) {
fprintf(stderr, "open <%s> error %s\n", argv[1], strerror(errno));
return 1;
}
if (setup_port(fd, baud, databits, parity, stopbits)) {
fprintf(stderr, "setup_port error %s\n", strerror(errno));
close(fd);
return 1;
}
pthread_t read_tid, write_tid;
int ret;
// 创建读取线程
ret = pthread_create(&read_tid, NULL, read_thread, &fd);
if (ret != 0) {
fprintf(stderr, "Failed to create read thread\n");
return 1;
}
// 创建写入线程
ret = pthread_create(&write_tid, NULL, write_thread, &fd);
if (ret != 0) {
fprintf(stderr, "Failed to create write thread\n");
return 1;
}
// 等待读取线程和写入线程结束
pthread_join(read_tid, NULL);
pthread_join(write_tid, NULL);
close(fd);
return 0;
}
static int baudflag_arr[] = {
B921600, B460800, B230400, B115200, B57600, B38400,
B19200, B9600, B4800, B2400, B1800, B1200,
B600, B300, B150, B110, B75, B50
};
static int speed_arr[] = {
921600, 460800, 230400, 115200, 57600, 38400,
19200, 9600, 4800, 2400, 1800, 1200,
600, 300, 150, 110, 75, 50
};
int speed_to_flag(int speed)
{
int i;
for (i = 0; i < sizeof(speed_arr)/sizeof(int); i++) {
if (speed == speed_arr[i]) {
return baudflag_arr[i];
}
}
fprintf(stderr, "Unsupported baudrate, use 9600 instead!\n");
return B9600;
}
static struct termio oterm_attr;
int setup_port(int fd, int baud, int databits, int parity, int stopbits)
{
struct termio term_attr;
if (ioctl(fd, TCGETA, &term_attr) < 0) {
return -1;
}
memcpy(&oterm_attr, &term_attr, sizeof(struct termio));
term_attr.c_iflag &= ~(INLCR | IGNCR | ICRNL | ISTRIP);
term_attr.c_oflag &= ~(OPOST | ONLCR | OCRNL);
term_attr.c_lflag &= ~(ISIG | ECHO | ICANON | NOFLSH);
term_attr.c_cflag &= ~CBAUD;
term_attr.c_cflag |= CREAD | speed_to_flag(baud);
term_attr.c_cflag &= ~(CSIZE);
switch (databits) {
case 5:
term_attr.c_cflag |= CS5;
break;
case 6:
term_attr.c_cflag |= CS6;
break;
case 7:
term_attr.c_cflag |= CS7;
break;
case 8:
default:
term_attr.c_cflag |= CS8;
break;
}
switch (parity) {
case 1:
term_attr.c_cflag |= (PARENB | PARODD);
break;
case 2:
term_attr.c_cflag |= PARENB;
term_attr.c_cflag &= ~(PARODD);
break;
case 0:
default:
term_attr.c_cflag &= ~(PARENB);
break;
}
switch (stopbits) {
case 2:
term_attr.c_cflag |= CSTOPB;
break;
case 1:
default:
term_attr.c_cflag &= ~CSTOPB;
break;
}
term_attr.c_cc[VMIN] = 1;
term_attr.c_cc[VTIME] = 0;
if (ioctl(fd, TCSETAW, &term_attr) < 0) {
return -1;
}
if (ioctl(fd, TCFLSH, 2) < 0) {
return -1;
}
return 0;
}
int read_data(int fd, void *buf, int len)
{
int count;
int ret;
ret = 0;
count = 0;
//while (len > 0) {
ret = read(fd, (char*)buf + count, len);
if (ret < 1) {
fprintf(stderr, "Read error %s\n", strerror(errno));
//break;
}
count += ret;
len = len - ret;
//}
*((char*)buf + count) = 0;
return count;
}
int write_data(int fd, void *buf, int len)
{
int count;
int ret;
ret = 0;
count = 0;
while (len > 0) {
ret = write(fd, (char*)buf + count, len);
if (ret < 1) {
fprintf(stderr, "Write error %s\n", strerror(errno));
break;
}
count += ret;
len = len - ret;
}
return count;
}
void print_usage(char *program_name)
{
fprintf(stderr,
"*************************************\n"
" A Simple Serial Port Test Utility\n"
"*************************************\n\n"
"Usage:\n %s <device> <baud> <databits> <parity> <stopbits> \n"
" databits: 5, 6, 7, 8\n"
" parity: 0(None), 1(Odd), 2(Even)\n"
" stopbits: 1, 2\n"
"Example:\n %s /dev/ttyS4 115200 8 0 1\n\n",
program_name, program_name
);
}
异常情况:
飞凌OK3588-C核心板(RK3588)可以发送,但是不能接收!
Enter data to write (or 'q' to quit): Read Thread: Read 3 bytes: 0x1f, 0xfa, 0xfc, 0x0, 0x0,
飞凌建议:可以先tx接rx自回环测试下
后来将串口板短接之后直接接电脑测试,正常。
接核心板异常。换了一条线就解决了!