add 09_UART/5a,05,06

This commit is contained in:
weidongshan
2021-07-19 11:17:00 +08:00
parent f5e121829f
commit 09ac57733b
14 changed files with 721 additions and 71 deletions

View File

@@ -97,7 +97,15 @@ export PATH=$PATH:/home/book/100ask_imx6ull-sdk/ToolChain/arm-buildroot-linux-gn
编译、执行程序
编译、执行程序:
```shell
1. Ubuntu上
arm-buildroot-linux-gnueabihf-gcc -o serial_send_recv serial_send_recv.c
2. 板子上
/mnt/serial_send_recv /dev/ttymxc5
```
@@ -113,7 +121,17 @@ export PATH=$PATH:/home/book/100ask_stm32mp157_pro-sdk/ToolChain/arm-buildroot-l
编译、执行程序
编译、执行程序:
```shell
1. Ubuntu上
arm-buildroot-linux-gnueabihf-gcc -o serial_send_recv serial_send_recv.c
2. 板子上
/mnt/serial_send_recv /dev/ttySTM3
```
@@ -211,7 +229,17 @@ export PATH=$PATH:/home/book/100ask_imx6ull-sdk/ToolChain/arm-buildroot-linux-gn
编译、执行程序
编译、执行程序:
```shell
1. Ubuntu上
arm-buildroot-linux-gnueabihf-gcc -o gps_read gps_read.c
2. 板子上
/mnt/gps_read /dev/ttymxc5
```
@@ -227,7 +255,17 @@ export PATH=$PATH:/home/book/100ask_stm32mp157_pro-sdk/ToolChain/arm-buildroot-l
编译、执行程序
编译、执行程序
```shell
1. Ubuntu上
arm-buildroot-linux-gnueabihf-gcc -o gps_read gps_read.c
2. 板子上
/mnt/gps_read /dev/ttySTM3
```

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 77 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 40 KiB

View File

@@ -0,0 +1,177 @@
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <stdlib.h>
/* set_opt(fd,115200,8,'N',1) */
int set_opt(int fd,int nSpeed, int nBits, char nEvent, int nStop)
{
struct termios newtio,oldtio;
if ( tcgetattr( fd,&oldtio) != 0) {
perror("SetupSerial 1");
return -1;
}
bzero( &newtio, sizeof( newtio ) );
newtio.c_cflag |= CLOCAL | CREAD;
newtio.c_cflag &= ~CSIZE;
newtio.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); /*Input*/
newtio.c_oflag &= ~OPOST; /*Output*/
switch( nBits )
{
case 7:
newtio.c_cflag |= CS7;
break;
case 8:
newtio.c_cflag |= CS8;
break;
}
switch( nEvent )
{
case 'O':
newtio.c_cflag |= PARENB;
newtio.c_cflag |= PARODD;
newtio.c_iflag |= (INPCK | ISTRIP);
break;
case 'E':
newtio.c_iflag |= (INPCK | ISTRIP);
newtio.c_cflag |= PARENB;
newtio.c_cflag &= ~PARODD;
break;
case 'N':
newtio.c_cflag &= ~PARENB;
break;
}
switch( nSpeed )
{
case 2400:
cfsetispeed(&newtio, B2400);
cfsetospeed(&newtio, B2400);
break;
case 4800:
cfsetispeed(&newtio, B4800);
cfsetospeed(&newtio, B4800);
break;
case 9600:
cfsetispeed(&newtio, B9600);
cfsetospeed(&newtio, B9600);
break;
case 115200:
cfsetispeed(&newtio, B115200);
cfsetospeed(&newtio, B115200);
break;
default:
cfsetispeed(&newtio, B9600);
cfsetospeed(&newtio, B9600);
break;
}
if( nStop == 1 )
newtio.c_cflag &= ~CSTOPB;
else if ( nStop == 2 )
newtio.c_cflag |= CSTOPB;
newtio.c_cc[VMIN] = 1; /* 读数据时的最小字节数: 没读到这些数据我就不返回! */
newtio.c_cc[VTIME] = 0; /* 等待第1个数据的时间:
* 比如VMIN设为10表示至少读到10个数据才返回,
* 但是没有数据总不能一直等吧? 可以设置VTIME(单位是10秒)
* 假设VTIME=1表示:
* 10秒内一个数据都没有的话就返回
* 如果10秒内至少读到了1个字节那就继续等待完全读到VMIN个数据再返回
*/
tcflush(fd,TCIFLUSH);
if((tcsetattr(fd,TCSANOW,&newtio))!=0)
{
perror("com set error");
return -1;
}
//printf("set done!\n");
return 0;
}
int open_port(char *com)
{
int fd;
//fd = open(com, O_RDWR|O_NOCTTY|O_NDELAY);
fd = open(com, O_RDWR|O_NOCTTY);
if (-1 == fd){
return(-1);
}
if(fcntl(fd, F_SETFL, 0)<0) /* 设置串口为阻塞状态*/
{
printf("fcntl failed!\n");
return -1;
}
return fd;
}
/*
* ./serial_send_recv <dev>
*/
int main(int argc, char **argv)
{
int fd;
int iRet;
char c;
/* 1. open */
/* 2. setup
* 115200,8N1
* RAW mode
* return data immediately
*/
/* 3. write and read */
if (argc != 2)
{
printf("Usage: \n");
printf("%s </dev/ttySAC1 or other>\n", argv[0]);
return -1;
}
fd = open_port(argv[1]);
if (fd < 0)
{
printf("open %s err!\n", argv[1]);
return -1;
}
iRet = set_opt(fd, 115200, 8, 'N', 1);
if (iRet)
{
printf("set port err!\n");
return -1;
}
printf("Enter a char: ");
while (1)
{
scanf("%c", &c);
iRet = write(fd, &c, 1);
iRet = read(fd, &c, 1);
if (iRet == 1)
printf("get: %02x %c\n", c, c);
else
printf("can not get data\n");
}
return 0;
}

View File

@@ -0,0 +1,257 @@
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <stdlib.h>
/* set_opt(fd,115200,8,'N',1) */
int set_opt(int fd,int nSpeed, int nBits, char nEvent, int nStop)
{
struct termios newtio,oldtio;
if ( tcgetattr( fd,&oldtio) != 0) {
perror("SetupSerial 1");
return -1;
}
bzero( &newtio, sizeof( newtio ) );
newtio.c_cflag |= CLOCAL | CREAD;
newtio.c_cflag &= ~CSIZE;
newtio.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); /*Input*/
newtio.c_oflag &= ~OPOST; /*Output*/
switch( nBits )
{
case 7:
newtio.c_cflag |= CS7;
break;
case 8:
newtio.c_cflag |= CS8;
break;
}
switch( nEvent )
{
case 'O':
newtio.c_cflag |= PARENB;
newtio.c_cflag |= PARODD;
newtio.c_iflag |= (INPCK | ISTRIP);
break;
case 'E':
newtio.c_iflag |= (INPCK | ISTRIP);
newtio.c_cflag |= PARENB;
newtio.c_cflag &= ~PARODD;
break;
case 'N':
newtio.c_cflag &= ~PARENB;
break;
}
switch( nSpeed )
{
case 2400:
cfsetispeed(&newtio, B2400);
cfsetospeed(&newtio, B2400);
break;
case 4800:
cfsetispeed(&newtio, B4800);
cfsetospeed(&newtio, B4800);
break;
case 9600:
cfsetispeed(&newtio, B9600);
cfsetospeed(&newtio, B9600);
break;
case 115200:
cfsetispeed(&newtio, B115200);
cfsetospeed(&newtio, B115200);
break;
default:
cfsetispeed(&newtio, B9600);
cfsetospeed(&newtio, B9600);
break;
}
if( nStop == 1 )
newtio.c_cflag &= ~CSTOPB;
else if ( nStop == 2 )
newtio.c_cflag |= CSTOPB;
newtio.c_cc[VMIN] = 1; /* 读数据时的最小字节数: 没读到这些数据我就不返回! */
newtio.c_cc[VTIME] = 0; /* 等待第1个数据的时间:
* 比如VMIN设为10表示至少读到10个数据才返回,
* 但是没有数据总不能一直等吧? 可以设置VTIME(单位是10秒)
* 假设VTIME=1表示:
* 10秒内一个数据都没有的话就返回
* 如果10秒内至少读到了1个字节那就继续等待完全读到VMIN个数据再返回
*/
tcflush(fd,TCIFLUSH);
if((tcsetattr(fd,TCSANOW,&newtio))!=0)
{
perror("com set error");
return -1;
}
//printf("set done!\n");
return 0;
}
int open_port(char *com)
{
int fd;
//fd = open(com, O_RDWR|O_NOCTTY|O_NDELAY);
fd = open(com, O_RDWR|O_NOCTTY);
if (-1 == fd){
return(-1);
}
if(fcntl(fd, F_SETFL, 0)<0) /* 设置串口为阻塞状态*/
{
printf("fcntl failed!\n");
return -1;
}
return fd;
}
int read_gps_raw_data(int fd, char *buf)
{
int i = 0;
int iRet;
char c;
int start = 0;
while (1)
{
iRet = read(fd, &c, 1);
if (iRet == 1)
{
if (c == '$')
start = 1;
if (start)
{
buf[i++] = c;
}
if (c == '\n' || c == '\r')
return 0;
}
else
{
return -1;
}
}
}
/* eg. $GPGGA,082559.00,4005.22599,N,11632.58234,E,1,04,3.08,14.6,M,-5.6,M,,*76"<CR><LF> */
int parse_gps_raw_data(char *buf, char *time, char *lat, char *ns, char *lng, char *ew)
{
char tmp[10];
if (buf[0] != '$')
return -1;
else if (strncmp(buf+3, "GGA", 3) != 0)
return -1;
else if (strstr(buf, ",,,,,"))
{
printf("Place the GPS to open area\n");
return -1;
}
else {
//printf("raw data: %s\n", buf);
sscanf(buf, "%[^,],%[^,],%[^,],%[^,],%[^,],%[^,]", tmp, time, lat, ns, lng, ew);
return 0;
}
}
/*
* ./serial_send_recv <dev>
*/
int main(int argc, char **argv)
{
int fd;
int iRet;
char c;
char buf[1000];
char time[100];
char Lat[100];
char ns[100];
char Lng[100];
char ew[100];
float fLat, fLng;
/* 1. open */
/* 2. setup
* 115200,8N1
* RAW mode
* return data immediately
*/
/* 3. write and read */
if (argc != 2)
{
printf("Usage: \n");
printf("%s </dev/ttySAC1 or other>\n", argv[0]);
return -1;
}
fd = open_port(argv[1]);
if (fd < 0)
{
printf("open %s err!\n", argv[1]);
return -1;
}
iRet = set_opt(fd, 9600, 8, 'N', 1);
if (iRet)
{
printf("set port err!\n");
return -1;
}
while (1)
{
/* eg. $GPGGA,082559.00,4005.22599,N,11632.58234,E,1,04,3.08,14.6,M,-5.6,M,,*76"<CR><LF>*/
/* read line */
iRet = read_gps_raw_data(fd, buf);
/* parse line */
if (iRet == 0)
{
iRet = parse_gps_raw_data(buf, time, Lat, ns, Lng, ew);
}
/* printf */
if (iRet == 0)
{
printf("Time : %s\n", time);
printf("ns : %s\n", ns);
printf("ew : %s\n", ew);
printf("Lat : %s\n", Lat);
printf("Lng : %s\n", Lng);
/* 纬度格式: ddmm.mmmm */
sscanf(Lat+2, "%f", &fLat);
fLat = fLat / 60;
fLat += (Lat[0] - '0')*10 + (Lat[1] - '0');
/* 经度格式: dddmm.mmmm */
sscanf(Lng+3, "%f", &fLng);
fLng = fLng / 60;
fLng += (Lng[0] - '0')*100 + (Lng[1] - '0')*10 + (Lng[2] - '0');
printf("Lng,Lat: %.06f,%.06f\n", fLng, fLat);
}
}
return 0;
}

View File

@@ -326,6 +326,13 @@ git clone https://e.coding.net/weidongshan/linux/doc_and_source_for_drivers.git
04_TTY驱动程序框架
```
* 2021.07.19 发布"UART子系统"
```shell
5a_在STM32MP157上做串口实验的准备工作
05_Linux串口应用编程_回环
06_Linux串口应用编程_GPS
```
## 6. 联系方式

View File

@@ -1,63 +0,0 @@
## 先修改STM32MP157的设备树以使用UART8
### 1. 只关注APP开发的话
把GIT仓库中这个dtb文件复制到开发板上去就可以
```shell
doc_and_source_for_drivers\STM32MP157\source\A7\09_UART\00_stm32mp157_devicetree_for_uart8\dtb
stm32mp157c-100ask-512d-lcd-v1.dtb
```
操作方法:
* Ubuntu上复制dtb文件到NFS目录
```shell
$ cp stm32mp157c-100ask-512d-lcd-v1.dtb ~/nfs_rootfs/
```
* 开发板上挂载NFS文件系统
* vmware使用NAT(假设windowsIP为192.168.1.100)
```shell
[root@100ask:~]# mount -t nfs -o nolock,vers=3,port=2049,mountport=9999
192.168.1.100:/home/book/nfs_rootfs /mnt
```
* vmware使用桥接或者不使用vmware而是直接使用服务器假设Ubuntu IP为192.168.1.137
```shell
[root@100ask:~]# mount -t nfs -o nolock,vers=3 192.168.1.137:/home/book/nfs_rootfs /mnt
```
* 更新设备树
```shell
[root@100ask:~]# mount /dev/mmcblk2p2 /boot
[root@100ask:~]# cp /mnt/stm32mp157c-100ask-512d-lcd-v1.dtb /boot
[root@100ask:~]# sync
```
* 重启开发板
* 在串口可以看到以下设备节点
```shell
ls /dev/ttySTM*
/dev/ttySTM0
/dev/ttySTM1
/dev/ttySTM3
```
### 2. 从头修改设备树文件

View File

@@ -97,7 +97,15 @@ export PATH=$PATH:/home/book/100ask_imx6ull-sdk/ToolChain/arm-buildroot-linux-gn
编译、执行程序
编译、执行程序:
```shell
1. Ubuntu上
arm-buildroot-linux-gnueabihf-gcc -o serial_send_recv serial_send_recv.c
2. 板子上
/mnt/serial_send_recv /dev/ttymxc5
```
@@ -113,7 +121,17 @@ export PATH=$PATH:/home/book/100ask_stm32mp157_pro-sdk/ToolChain/arm-buildroot-l
编译、执行程序
编译、执行程序:
```shell
1. Ubuntu上
arm-buildroot-linux-gnueabihf-gcc -o serial_send_recv serial_send_recv.c
2. 板子上
/mnt/serial_send_recv /dev/ttySTM3
```
@@ -211,7 +229,17 @@ export PATH=$PATH:/home/book/100ask_imx6ull-sdk/ToolChain/arm-buildroot-linux-gn
编译、执行程序
编译、执行程序:
```shell
1. Ubuntu上
arm-buildroot-linux-gnueabihf-gcc -o gps_read gps_read.c
2. 板子上
/mnt/gps_read /dev/ttymxc5
```
@@ -227,7 +255,17 @@ export PATH=$PATH:/home/book/100ask_stm32mp157_pro-sdk/ToolChain/arm-buildroot-l
编译、执行程序
编译、执行程序
```shell
1. Ubuntu上
arm-buildroot-linux-gnueabihf-gcc -o gps_read gps_read.c
2. 板子上
/mnt/gps_read /dev/ttySTM3
```

View File

@@ -0,0 +1,196 @@
## 在STM32MP157上做串口实验的准备工作
* 本节课程源码在GIT仓库里
```shell
doc_and_source_for_drivers\STM32MP157\source\A7\09_UART
00_stm32mp157_devicetree_for_uart8
```
### 1. 出厂系统不支持扩展板上的UART
![](../../../IMX6ULL/doc_pic/09_UART/pic/09_UART/18_gps_to_stm32mp157.png)
### 2. 只关注APP开发的话
把GIT仓库中这个dtb文件复制到开发板上去就可以
```shell
doc_and_source_for_drivers\STM32MP157\source\A7\09_UART\00_stm32mp157_devicetree_for_uart8\dtb
stm32mp157c-100ask-512d-lcd-v1.dtb
```
操作方法:
* Ubuntu上复制dtb文件到NFS目录
```shell
$ cp stm32mp157c-100ask-512d-lcd-v1.dtb ~/nfs_rootfs/
```
* 开发板上挂载NFS文件系统
* vmware使用NAT(假设windowsIP为193.168.1.100)
```shell
[root@100ask:~]# mount -t nfs -o nolock,vers=3,port=2049,mountport=9999
193.168.1.100:/home/book/nfs_rootfs /mnt
```
* vmware使用桥接或者不使用vmware而是直接使用服务器假设Ubuntu IP为193.168.1.137
```shell
[root@100ask:~]# mount -t nfs -o nolock,vers=3 193.168.1.137:/home/book/nfs_rootfs /mnt
```
* 更新设备树
```shell
[root@100ask:~]# mount /dev/mmcblk2p2 /boot
[root@100ask:~]# cp /mnt/stm32mp157c-100ask-512d-lcd-v1.dtb /boot
[root@100ask:~]# sync
```
* 重启开发板
* 在串口可以看到以下设备节点
```shell
ls /dev/ttySTM*
/dev/ttySTM0
/dev/ttySTM1
/dev/ttySTM3
```
### 3. 从头修改设备树文件
#### 3.1 确定硬件资源
观察STM32MP157的扩展板背后写着: UART8_TX, UART8_RX如下图
![image-20210719092503508](../../../IMX6ULL/doc_pic/09_UART/pic/09_UART/19_stm32mp157_extend_board_back.png)
打开STM32MP157底板原理图可知UART8_TX、UART8_RX使用引脚PE1、PE0如下图
![image-20210719092726986](../../../IMX6ULL/doc_pic/09_UART/pic/09_UART/20_stm32mp157_pins_of_uart8.png)
#### 3.2 使能设备树节点
在STM32MP157的内核设备树文件`arch/arm/boot/dts/stm32mp151.dtsi`中已经设置了uart8节点
![image-20210719092138173](C:/Users/weidongshan/AppData/Roaming/Typora/typora-user-images/image-20210719092138173.png)
我们需要使能这个节点,往下看。
#### 3.3 通过Pinctrl指定引脚
光使能UART8还不行还需要为它选择引脚。
修改`arch/arm/boot/dts/stm32mp15xx-100ask.dtsi`,如下:
```shell
&uart8 {
pinctrl-names = "default", "sleep";
pinctrl-0 = <&uart8_pins_mx>;
pinctrl-1 = <&uart8_sleep_pins_mx>;
status = "okay";
};
```
这些Pinctrl信息已经在`arm/boot/dts/stm32mp157-100ask-pinctrl.dtsi`中有了,无需我们添加:
![image-20210719092949939](../../../IMX6ULL/doc_pic/09_UART/pic/09_UART/21_stm32mp157_uart8_pinctrl.png)
#### 3.4 指定设备别名
UART8对应的设备节点是哪个它的驱动程序需要从"别名"里确定编号。
修改`arch/arm/boot/dts/stm32mp157c-100ask-512d-v1.dts`,如下:
```shell
aliases {
ethernet0 = &ethernet0;
serial0 = &uart4; //debug
serial1 = &usart6; //rs485
serial2 = &usart1; //bluetooth
serial3 = &uart8; // extend board uart, /dev/ttySTM3
};
```
#### 3.5 编译、使用
* 设置工具链
```shell
export ARCH=arm
export CROSS_COMPILE=arm-buildroot-linux-gnueabihf-
export PATH=$PATH:/home/book/100ask_stm32mp157_pro-sdk/ToolChain/arm-buildroot-linux-gnueabihf_sdk-buildroot/bin
```
* 编译设备树:
在Ubuntu的STM32MP157内核目录下执行如下命令,
得到设备树文件:`arch/arm/boot/dts/stm32mp157c-100ask-512d-lcd-v1.dtb`
```shell
make dtbs
```
* 复制到NFS目录
```shell
$ cp arch/arm/boot/dts/stm32mp157c-100ask-512d-lcd-v1.dtb ~/nfs_rootfs/
```
* 开发板上挂载NFS文件系统
* vmware使用NAT(假设windowsIP为193.168.1.100)
```shell
[root@100ask:~]# mount -t nfs -o nolock,vers=3,port=2049,mountport=9999
193.168.1.100:/home/book/nfs_rootfs /mnt
```
* vmware使用桥接或者不使用vmware而是直接使用服务器假设Ubuntu IP为193.168.1.137
```shell
[root@100ask:~]# mount -t nfs -o nolock,vers=3 193.168.1.137:/home/book/nfs_rootfs /mnt
```
* 更新设备树
```shell
[root@100ask:~]# mount /dev/mmcblk2p2 /boot
[root@100ask:~]# cp /mnt/stm32mp157c-100ask-512d-lcd-v1.dtb /boot
[root@100ask:~]# sync
```
* 重启开发板
* 在串口可以看到以下设备节点
```shell
ls /dev/ttySTM*
/dev/ttySTM0
/dev/ttySTM1
/dev/ttySTM3
```

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 77 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 40 KiB