# ATK-STM32MP157D **Repository Path**: lituo666/atk-stm32-mp157-d ## Basic Information - **Project Name**: ATK-STM32MP157D - **Description**: ATK-STM32MP157D 学习例程 - **Primary Language**: C - **License**: GPL-3.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2022-04-05 - **Last Updated**: 2022-08-15 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # ATK-STM32MP157D ## 介绍 使用正点原子开发板[STM32MP157开发板](http://www.openedv.com/docs/boards/arm-linux/zdyzmp157.html),跟着教程写的驱动。 最好使用Ubuntu20.04,因为Ubuntu22.04的nfs服务uboot无法挂载成功,暂未找到解决办法。 ## 环境配置 ### 添加用户组 将`用户名`加入`dialout`和`plugdev`两个用户组内,这样普通用户就可以使用串口了。例如: ``` sudo usermod -a -G dialout lituo sudo usermod -a -G plugdev lituo ``` ### 安装依赖 ``` sudo apt install libc6-dev lib32stdc++6 bison flex libncurses5-dev lzop libssl-dev u-boot-tools lsb-core lib32stdc++6 ``` ### 交叉编译工具 下载[gcc-arm-9.2-2019.12-x86_64-arm-none-linux-gnueabihf.tar.xz](https://developer.arm.com/tools-and-software/open-source-software/developer-tools/gnu-toolchain/gnu-a/downloads) 依次执行下面命令 ``` sh sudo mkdir /usr/local/arm sudo cp gcc-arm-9.2-2019.12-x86_64-arm-none-linux-gnueabihf.tar.xz /usr/local/arm/ -f cd /usr/local/arm sudo tar -vxf gcc-arm-9.2-2019.12-x86_64-arm-none-linux-gnueabihf.tar.xz sudo vim ~/.zshrc export PATH=$PATH:/usr/local/arm/gcc-arm-9.2-2019.12-x86_64-arm-none-linux-gnueabihf/bin ``` ### stm32wrapper4dbg 工具安装 编译 TF-A 或者 Uboot 的时候需要用到 stm32wrapper4dbg 这个工具,源码的下载地址为:[stm32wrapper4dbg](https://github.com/STMicroelectronics/stm32wrapper4dbg) 下载后使用`unzip stm32wrapper4dbg-master.zip`解压,然后进入文件夹使用`make`命令编译,编译完成以后就会得到一个名为“stm32wrapper4dbg”的工具就是我们要的,然后使用命令`sudo cp stm32wrapper4dbg /usr/bin`拷贝到`/usr/bin` 目录下,最后使用命令`stm32wrapper4dbg -s`验证是否安装成功。 ### 安装NFS服务 ``` sudo apt-get install nfs-kernel-server rpcbind ``` 新建文件夹`nfs`,在 nfs 配置文件`/etc/exports`中最后添加 ``` /home/lituo/linux/nfs *(rw,sync,no_root_squash) ``` Ubuntu18 的 nfs 默认只支持 3 和 4 版本的 nfs, uboot 默认使用的是版本 2,所以需要修改 Ubuntu18 的 nfs 配置。打开 Ubuntu 下的/etc/default/nfs-kernel-server 文件,然后在最后面添加下面这一行: ``` RPCNFSDOPTS="--nfs-version 2,3,4 --debug --syslog" ``` 最后重启nfs服务 ``` sudo systemctl restart nfs-server.service ``` ### 安装tftp服务 ``` sudo apt-get install tftp-hpa tftpd-hpa xinetd ``` 和 NFS 一样, TFTP 也需要一个文件夹来存放文件,在用户目录下新建一个目录,命令如下: ``` mkdir /home/lituo/linux/tftpboot chmod 777 /home/lituo/linux/tftpboot ``` 这样我就在我的电脑上创建了一个名为 tftpboot 的目录(文件夹),路径为`/home/lituo/linux/tftpboot`。注意!我们要给 tftpboot 文件夹权限,否则的话 uboot 不能从 tftpboot 文件夹里面下载文件。 最后配置 tftp,新建文件/etc/xinetd.d/tftp,如果没有/etc/xinetd.d 目录的话自行创建,然后在里面输入如下内容: ``` server tftp { socket_type = dgram protocol = udp wait = yes user = root server = /usr/sbin/in.tftpd server_args = -s /home/zuozhongkai/linux/tftpboot/ disable = no per_source = 11 cps = 100 2 flags = IPv4 } ``` 完了以后启动 tftp 服务,命令如下: ``` sudo systemctl restart tftpd-hpa.service ``` 打开`/etc/default/tftpd-hpa`文件,将其修改为如下所示内容: ``` # /etc/default/tftpd-hpa TFTP_USERNAME="tftp" TFTP_DIRECTORY="/home/lituo/linux/tftpboot" TFTP_ADDRESS=":69" TFTP_OPTIONS="-l -c -s" ``` `TFTP_DIRECTORY` 就是我们上面创建的 tftp 文件夹目录,以后我们就将所有需要通过TFTP 传输的文件都放到这个文件夹里面,并且要给予这些文件相应的权限。 最后输入如下命令, 重启 tftp 服务器: `sudo systemctl restart tftpd-hpa.service` tftp 服务器已经搭建好了,接下来就是使用了。将 uImage 镜像文件拷贝到 tftpboot 文件夹中,并且给予 uImage 相应的权限,命令如下: ``` cp uImage /home/lituo/linux/tftpboot/ cd /home/lituo/linux/tftpboot/ chmod 777 uImage ``` 万事俱备,只剩验证了. ## 环境搭建中的问题 ### uboot 编译报错 ``` /usr/bin/ld: scripts/dtc/dtc-parser.tab.o:(.bss+0x10): multiple definition of `yylloc'; scripts/dtc/dtc-lexer.lex.o:(.bss+0x0): first defined here collect2: 错误:ld 返回 1 make[2]: *** [scripts/Makefile.host:106:scripts/dtc/dtc] 错误 1 make[1]: *** [scripts/Makefile.build:432:scripts/dtc] 错误 2 make: *** [Makefile:554:scripts] 错误 2 ``` 意思是`yylloc`首先在`scripts/dtc/dtc-lexer.lex.c`中定义,文件`scripts/dtc/dtc-parser.tab.c:`中重定义了, 解决办法是将`scripts/dtc/dtc-parser.tab.c`文件中大约1293行改为`extern YYLTYPE yylloc`即可 ## 常用命令 ``` setenv bootcmd 'tftp c2000000 uImage;tftp c4000000 stm32mp157d-atk.dtb;bootm c2000000 - c4000000' ``` ``` setenv bootargs 'console=ttySTM0,115200 root=/dev/nfs nfsroot=192.168.1.13:/home/lituo/linux/nfs/rootfs,proto=tcp rw ip=192.168.1.23:192.168.1.13:192.168.1.3:255.255.255.0::eth0:off' ``` ``` setenv bootcmd 'ext4load mmc 1:2 c2000000 uImage;ext4load mmc 1:2 c4000000 stm32mp157d-atk.dtb;bootm c2000000 - c4000000' ``` ``` setenv bootargs 'console=ttySTM0,115200 root=/dev/mmcblk1p3 rootwait rw' ``` ## 编译新的rootfs后的一些配置 ### 新建modules目录 ``` mkdir /lib/modules/5.4.31 -p ``` ### vsftpd 配置 打开 /etc/vsftpd.conf 文件,将下面两项前面的#删掉。 ``` local_enable=YES write_enable=YES ``` 修改/etc/vsftpd.conf 文件的所属用户。 ``` chown root:root /etc/vsftpd.conf ``` ### sshd 配置 如果 SSHD 启动失败并且提示“sshd: /var/empty must be owned by root and not group or world-writable.”,此时我们需要修改/var/empty 目录所属用户以及用户组,输入如下命令: ``` chown root:root /var/empty ``` 使用root用户登录,修改/etc/ssh/sshd_config文件。 ``` vim /etc/ssh/sshd_config ``` 将其中的`PermitRootLogin`修改为`yes`。 ``` PermitRootLogin yes ``` ### shell 命令前缀信息 在/etc/profile.d目录下新建文件myprofile.sh ``` touch /etc/profile.d/myprofile.sh ``` 添加以下内容 ``` #!/bin/sh if [ "`id -u`" -eq 0 ]; then export PS1='\n[\u@\h]:\w\n# ' else export PS1='\n[\u@\h]:\w\n$ ' fi ``` ### 创建自启动文件 只需要在 `/etc/init.d` 目录下新建以`S`开头的脚本即可。例如, ``` cd /etc/init.d/ //进入/etc/init.d 目录 touch Sautorun //使用 touch 命令创建 Sautorun 脚本 chmod 777 Sautorun //给予 Aautorun 脚本可执行权限 ``` Sautorun 内容一般为, ``` #/bin/sh mount -t debugfs none /sys/kernel/debug ``` ### 使能 sysfs debug 目录 后续调试驱动的时候我们可能要用到/sys/kernel/debug 目录,默认我们没有挂载 debugfs 文件系统,所以/sys/kernel/debug 目录下没有任何文件,挂载方法很简单。在自启动文件 Sautorun 中添加如下代码: ``` mount -t debugfs none /sys/kernel/debug ``` ## Manjaro 环境搭建 ### 添加用户组 将`用户名`加入`uucp`用户组内,这样普通用户就可以使用串口了。直接使用以下命令添加即可: ``` sudo usermod -aG uucp $USER ``` ### 安装NFS服务 ``` sudo pacman -Sy nfs-utils rpcbind ``` 新建文件夹`nfs`,在 nfs 配置文件`/etc/exports`中最后添加 ``` /home/lituo/linux/nfs *(rw,sync,no_root_squash) ``` 开机启动 ``` systemctl enable rpcbind.service systemctl enable nfs-server.service ``` 启动服务 ``` systemctl start rpcbind.service systemctl start nfs-server.service ``` 查看服务 ``` systemctl status rpcbind systemctl status nfs-server ``` 在一些嵌入式场景, 有的开发板还只支持nfsv2, 导致挂载失败。 查看支持的版本 `sudo cat /proc/fs/nfsd/versions`如下 `-2 +3 +4 +4.1 +4.2` 修改`sudo vim /etc/nfs.conf`文件,添加`vers2=y`,然后重启`sudo systemctl restart nfs-server.service` ### 安装tftp服务 ``` sudo pacman -Sy tftp-hpa ``` 和 NFS 一样, TFTP 也需要一个文件夹来存放文件,在用户目录下新建一个目录,命令如下: ``` mkdir /home/lituo/linux/tftpboot chmod 777 /home/lituo/linux/tftpboot ``` 这样我就在我的电脑上创建了一个名为 tftpboot 的目录(文件夹),路径为`/home/lituo/linux/tftpboot`。注意!我们要给 tftpboot 文件夹权限,否则的话 uboot 不能从 tftpboot 文件夹里面下载文件。 最后配置 tftp,新建文件`/etc/hosts.allow`,如果没有的话自行创建,然后在里面输入如下内容: ``` tftpd: ALL in.tftpd: ALL ``` 修改`sudo vim /etc/conf.d/tftpd`文件中的目录为实际目录 ``` TFTPD_ARGS="-l -c -s /home/lituo/linux/tftpboot" ``` 完了以后启动 tftp 服务,命令如下: ``` sudo systemctl enable tftpd.service sudo systemctl start tftpd.service sudo systemctl status tftpd.service sudo systemctl restart tftpd.service ``` 万事俱备,只剩验证了. ### 安装依赖 ``` sudo apt install libc6-dev lib32stdc++6 bison flex libncurses5-dev lzop libssl-dev lsb-core lib32stdc++6 sudo pacman -Sy uboot-tools cpio ``` ## ArchLinux KDE 环境搭建 ### 安装必备软件 ``` sudo pacman -S dtc unzip ``` ### 添加用户组 将`用户名`加入`uucp`用户组内,这样普通用户就可以使用串口了。直接使用以下命令添加即可: ``` sudo usermod -aG uucp $USER ``` ### 编译 buildroot 遇到到问题 #### 问题1: ``` c-stack.c:55:26: error: missing binary operator before token "(" 55 | #elif HAVE_LIBSIGSEGV && SIGSTKSZ < 16384 | ^~~~~~~~ ``` 下载补丁文件[0003-c-stack-stop-using-SIGSTKSZ.patch](https://git.buildroot.org/buildroot/tree/package/m4/0003-c-stack-stop-using-SIGSTKSZ.patch?id=5a9504831f3fa1ef3be334036c93da30150fde55),将网页中到内容保存到文件中,文件名为`0003-c-stack-stop-using-SIGSTKSZ.patch` 复制到buildroot目录下,然后再使用命令复制到: ``` sudo ./0003-c-stack-stop-using-SIGSTKSZ.patch ./output/build/host-m4-1.4.18 ``` 然后进行打补丁: ``` sudo patch -p1 < 0003-c-stack-stop-using-SIGSTKSZ.patch ``` 修改了这两个文件`lib/c-stack.c lib/c-stack.h` #### 问题2: ``` libfakeroot.c:99:40: '_STAT_VER' undeclared (first use inside this function) 99 | #define INT_NEXT_STAT(a,b) NEXT_STAT64(_STAT_VER,a,b) | ^~~~~~~~~ ``` 复制到 `/output/build/host-fakeroot-1.20.2` 目录,进行打补丁。 ``` https://git.buildroot.org/buildroot/tree/package/fakeroot/0002-libfakeroot.c-define-_STAT_VER-if-not-already-define.patch?id=f45925a951318e9e53bead80b363e004301adc6f https://git.buildroot.org/buildroot/tree/package/fakeroot/0003-libfakeroot.c-add-wrappers-for-new-glibc-2.33-symbol.patch?id=f45925a951318e9e53bead80b363e004301adc6f ``` ## Vscode 开发环境配置 ### 安装 安装必要软件 ``` sudo pacman -Sy clang bear ``` 安装 vscode 插件,搜索 `clangd` 安装。 ### 生成 compile_commands.json 文件 想要使用 clangd 就必须有 `compile_commands.json` 文件。 linux 源码编译时生成方法,依次使用以下命令会在根目录下生成: ``` make clean make menuconfig bear -- make ARCH=arm CROSS_COMPILE=arm-none-linux-gnueabihf- uImage dtbs LOADADDR=0XC2000040 -j16 ``` ### 配置 clangd 插件 如图,在插件中添加如下几项: ![](./images/Screenshot_20220809_090351.png) ``` --compile-commands-dir=/home/lituo/linux/atk-mp1/my-linux/linux-5.4.31 --background-index --completion-style=detailed --header-insertion=never -log=info ``` ~~然后使用vscode打开源码目录后,任意打开一个c文件,屏幕左下角即可看到正在建立索引,生成 `.cache` 文件夹。~~ ### 驱动开发 如果在上一步中 `compile-commands-dir` 直接指向 `compile_commands.json` 文件目录,就不需要软链接了。 可以使用ln命令将 `compile_commands.json` 文件软链接到独立到文件夹内,在独立文件夹内创建软链接如下: ``` ln -s /home/lituo/linux/atk-mp1/my-linux/linux-5.4.31/compile_commands.json compile_commands.json ``` 应为会创建 `.cache` 文件夹,所以需要在 `.gitignore` 文件中添加 `*.cache*` 来屏蔽多余文件。 在 vscode 设置中搜索 `editor.inlayHints` 设置为 `off`