ubuntu开发stm32单片机-环境搭建
1、首先安装串口助手(ch340驱动)首先查看操作系统中自带的串口驱动
arvin@arvin-virtual-machine:/lib/modules/5.19.0-43-generic/kernel/drivers/usb/serial$ ls aircable.ko f81232.ko iuu_phoenix.ko mxuport.ko quatech2.ko usbserial.ko ark3116.ko f81534.ko keyspan.ko navman.ko safe_serial.ko usb-serial-simple.ko belkin_sa.ko ftdi_sio.ko keyspan_pda.ko omninet.ko sierra.ko usb_wwan.ko ch341.ko garmin_gps.ko kl5kusb105.ko opticon.ko spcp8x5.ko visor.ko cp210x.ko io_edgeport.ko kobil_sct.ko option.ko ssu100.ko whiteheat.ko cyberjack.ko io_ti.ko mct_u232.ko oti6858.ko symbolserial.ko wishbone-serial.ko cypress_m8.ko ipaq.ko metro-usb.ko pl2303.ko ti_usb_3410_5052.ko xr_serial.ko digi_acceleport.ko ipw.ko mos7720.ko qcaux.ko upd78f0730.ko xsens_mt.ko empeg.ko ir-usb.ko mos7840.ko qcserial.ko usb_debug.ko arvin@arvin-virtual-machine:/lib/modules/5.19.0-43-generic/kernel/drivers/usb/serial$
路径是根据操作系统的版本进行选择的
arvin@arvin-virtual-machine:/lib/modules$ ls 5.19.0-32-generic 5.19.0-43-generic arvin@arvin-virtual-machine:/lib/modules$ uname -a Linux arvin-virtual-machine 5.19.0-43-generic #44~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Mon May 22 13:39:36 UTC 2 x86_64 x86_64 x86_64 GNU/Linux arvin@arvin-virtual-machine:/lib/modules$
查看操作系统中本来是带有串口驱动对应的文件(ch341.ko)文件,但是这个版本是比较旧的,需要更新成新的,首先删除系统中自带的驱动文件。
arvin@arvin-virtual-machine:/lib/modules/5.19.0-43-generic/kernel/drivers/usb/serial$ ls aircable.ko f81232.ko iuu_phoenix.ko mxuport.ko quatech2.ko usbserial.ko ark3116.ko f81534.ko keyspan.ko navman.ko safe_serial.ko usb-serial-simple.ko belkin_sa.ko ftdi_sio.ko keyspan_pda.ko omninet.ko sierra.ko usb_wwan.ko ch341.ko garmin_gps.ko kl5kusb105.ko opticon.ko spcp8x5.ko visor.ko cp210x.ko io_edgeport.ko kobil_sct.ko option.ko ssu100.ko whiteheat.ko cyberjack.ko io_ti.ko mct_u232.ko oti6858.ko symbolserial.ko wishbone-serial.ko cypress_m8.ko ipaq.ko metro-usb.ko pl2303.ko ti_usb_3410_5052.ko xr_serial.ko digi_acceleport.ko ipw.ko mos7720.ko qcaux.ko upd78f0730.ko xsens_mt.ko empeg.ko ir-usb.ko mos7840.ko qcserial.ko usb_debug.ko arvin@arvin-virtual-machine:/lib/modules/5.19.0-43-generic/kernel/drivers/usb/serial$ sudo rm -rf ch341.ko [sudo] arvin 的密码: arvin@arvin-virtual-machine:/lib/modules/5.19.0-43-generic/kernel/drivers/usb/serial$ ls aircable.ko f81534.ko keyspan.ko navman.ko safe_serial.ko usb-serial-simple.ko ark3116.ko ftdi_sio.ko keyspan_pda.ko omninet.ko sierra.ko usb_wwan.ko belkin_sa.ko garmin_gps.ko kl5kusb105.ko opticon.ko spcp8x5.ko visor.ko cp210x.ko io_edgeport.ko kobil_sct.ko option.ko ssu100.ko whiteheat.ko cyberjack.ko io_ti.ko mct_u232.ko oti6858.ko symbolserial.ko wishbone-serial.ko cypress_m8.ko ipaq.ko metro-usb.ko pl2303.ko ti_usb_3410_5052.ko xr_serial.ko digi_acceleport.ko ipw.ko mos7720.ko qcaux.ko upd78f0730.ko xsens_mt.ko empeg.ko ir-usb.ko mos7840.ko qcserial.ko usb_debug.ko f81232.ko iuu_phoenix.ko mxuport.ko quatech2.ko usbserial.ko arvin@arvin-virtual-machine:/lib/modules/5.19.0-43-generic/kernel/drivers/usb/serial$
下载ch34x驱动源文件:CH341SER_LINUX.ZIP - 南京沁恒微电子股份有限公司 (wch.cn)
下载完成后解压如下所示:
进行源码的更新,源码对应的各个版本地址:https://elixir.bootlin.com/linux/v5.19/source/drivers/usb/serial/ch341.c 将对应文件内容拷贝到ch341.c文件中,然后进行源码编译。
将ch341.ko文件放置在linux系统的驱动位置
rvin@arvin-virtual-machine:/lib/modules/5.19.0-43-generic/kernel/drivers/usb/serial$ cd ~ arvin@arvin-virtual-machine:~$ ls 公共的 模板 视频 图片 文档 下载 音乐 桌面 dev snap arvin@arvin-virtual-machine:~$ cd 下载 arvin@arvin-virtual-machine:~/下载$ cd CH341SER_LINUX/driver/ arvin@arvin-virtual-machine:~/下载/CH341SER_LINUX/driver$ ls ch341.c ch341.ko ch341.mod.c ch341.o modules.order ch341.h ch341.mod ch341.mod.o Makefile Module.symvers arvin@arvin-virtual-machine:~/下载/CH341SER_LINUX/driver$ scp ch341. ch341.c ch341.h ch341.ko ch341.mod ch341.mod.c ch341.mod.o ch341.o arvin@arvin-virtual-machine:~/下载/CH341SER_LINUX/driver$ scp ch341.ko /lib/modules/5.19.0-43-generic/kernel/drivers/usb/serial/ cp: 无法创建普通文件 '/lib/modules/5.19.0-43-generic/kernel/drivers/usb/serial/ch341.ko': 权限不够 arvin@arvin-virtual-machine:~/下载/CH341SER_LINUX/driver$ sudo scp ch341.ko /lib/modules/5.19.0-43-generic/kernel/drivers/usb/serial/ [sudo] arvin 的密码: arvin@arvin-virtual-machine:~/下载/CH341SER_LINUX/driver$ ls ch341.c ch341.ko ch341.mod.c ch341.o modules.order ch341.h ch341.mod ch341.mod.o Makefile Module.symvers arvin@arvin-virtual-machine:~/下载/CH341SER_LINUX/driver$ ^C arvin@arvin-virtual-machine:~/下载/CH341SER_LINUX/driver$ cd /lib/modules/5.19.0-43-generic/kernel/drivers/usb/serial arvin@arvin-virtual-machine:/lib/modules/5.19.0-43-generic/kernel/drivers/usb/serial$ ls aircable.ko f81232.ko iuu_phoenix.ko mxuport.ko quatech2.ko usbserial.ko ark3116.ko f81534.ko keyspan.ko navman.ko safe_serial.ko usb-serial-simple.ko belkin_sa.ko ftdi_sio.ko keyspan_pda.ko omninet.ko sierra.ko usb_wwan.ko ch341.ko garmin_gps.ko kl5kusb105.ko opticon.ko spcp8x5.ko visor.ko cp210x.ko io_edgeport.ko kobil_sct.ko option.ko ssu100.ko whiteheat.ko cyberjack.ko io_ti.ko mct_u232.ko oti6858.ko symbolserial.ko wishbone-serial.ko cypress_m8.ko ipaq.ko metro-usb.ko pl2303.ko ti_usb_3410_5052.ko xr_serial.ko digi_acceleport.ko ipw.ko mos7720.ko qcaux.ko upd78f0730.ko xsens_mt.ko empeg.ko ir-usb.ko mos7840.ko qcserial.ko usb_debug.ko arvin@arvin-virtual-machine:/lib/modules/5.19.0-43-generic/kernel/drivers/usb/serial$
安装完成之后删除系统中的brltty,然后重启下系统。
arvin@arvin-virtual-machine:~/桌面$ sudo apt remove brltty [sudo] arvin 的密码: 正在读取软件包列表... 完成 正在分析软件包的依赖关系树... 完成 正在读取状态信息... 完成 下列软件包是自动安装的并且现在不需要了: libpcre2-32-0 使用'sudo apt autoremove'来卸载它(它们)。 下列软件包将被【卸载】: brltty 升级了 0 个软件包,新安装了 0 个软件包,要卸载 1 个软件包,有 8 个软件包未被升级。 解压缩后将会空出 8,294 kB 的空间。 您希望继续执行吗? [Y/n] y (正在读取数据库 ... 系统当前共安装有 220045 个文件和目录。) 正在卸载 brltty (6.4-4ubuntu3) ... 正在处理用于 man-db (2.10.2-1) 的触发器 ... arvin@arvin-virtual-machine:~/桌面$
为了每次系统重启之后都可以直接使用串口,所以进行如下配置,在dev目录下执行
arvin@arvin-virtual-machine:/dev$ sudo chmod 777 ttyUSB0 arvin@arvin-virtual-machine:/dev$
然后添加配置文件
arvin@arvin-virtual-machine:/dev$ sudo gedit /etc/udev/rules.d/70-ttyusb.rules
文件内容为:
KERNEL=="ttyUSB[0-9]*", MODE="0666" KERNEL=="ttyS[0-9]*", MODE="0666"
最后给配置文件添加执行权限
arvin@arvin-virtual-machine:/etc/udev/rules.d$ sudo chmod -R 777 70-ttyusb.rules arvin@arvin-virtual-machine:/etc/udev/rules.d$
到此串口驱动安装完成,开始安装串口助手野火多功能调试助手上位机 — 野火产品资料下载中心 文档 (embedfire.com)
安装完成之后打开软件连接后可以发送数据的串口开始验证是否安装正确
安装完成。
安装交叉编译器,在官网下载最新的版本:Downloads | GNU Arm Embedded Toolchain Downloads – Arm Developer
解压过后把bin文件路径添加到环境变量中:/etc/profile中
然后在终端查看是否安装成功
arvin@arvin-virtual-machine:~/桌面$ arm-none-eabi- arm-none-eabi-addr2line arm-none-eabi-gdb arm-none-eabi-ar arm-none-eabi-gdb-add-index arm-none-eabi-as arm-none-eabi-gdb-add-index-py arm-none-eabi-c++ arm-none-eabi-gdb-py arm-none-eabi-c++filt arm-none-eabi-gprof arm-none-eabi-cpp arm-none-eabi-ld arm-none-eabi-elfedit arm-none-eabi-ld.bfd arm-none-eabi-g++ arm-none-eabi-lto-dump arm-none-eabi-gcc arm-none-eabi-nm arm-none-eabi-gcc-10.3.1 arm-none-eabi-objcopy arm-none-eabi-gcc-ar arm-none-eabi-objdump arm-none-eabi-gcc-nm arm-none-eabi-ranlib arm-none-eabi-gcc-ranlib arm-none-eabi-readelf arm-none-eabi-gcov arm-none-eabi-size arm-none-eabi-gcov-dump arm-none-eabi-strings arm-none-eabi-gcov-tool arm-none-eabi-strip arvin@arvin-virtual-machine:~/桌面$
安装openocd直接通过终端安装:
arvin@arvin-virtual-machine:~/桌面$ sudo apt install openocd [sudo] arvin 的密码: 正在读取软件包列表... 完成 正在分析软件包的依赖关系树... 完成 正在读取状态信息... 完成 下列软件包是自动安装的并且现在不需要了: binutils-arm-none-eabi libnewlib-arm-none-eabi libnewlib-dev libpcre2-32-0 使用'sudo apt autoremove'来卸载它(它们)。 将会同时安装下列软件: libcapstone4 libgpiod2 libhidapi-hidraw0 libjaylink0 libjim0.79 libusb-0.1-4 下列【新】软件包将被安装: libcapstone4 libgpiod2 libhidapi-hidraw0 libjaylink0 libjim0.79 libusb-0.1-4 openocd 升级了 0 个软件包,新安装了 7 个软件包,要卸载 0 个软件包,有 8 个软件包未被升级。 需要下载 3,780 kB 的归档。 解压缩后会消耗 15.2 MB 的额外空间。 您希望继续执行吗? [Y/n] y
通过命令查看安装的位置
arvin@arvin-virtual-machine:~/桌面$ whereis openocd openocd: /usr/bin/openocd /usr/share/openocd /usr/share/man/man1/openocd.1.gz /usr/share/info/openocd.info-1.gz /usr/share/info/openocd.info-2.gz /usr/share/info/openocd.info.gz arvin@arvin-virtual-machine:~/桌面$
安装完成之后找一个stm32的makefile工程
直接make编译
可以正常编译,下面安装代码下载工具https://www.st.com/zh/development-tools/stm32-software-development-tools.html
安装软件
打开软件
链接开发板无法链接,是因为没有安装stlinK驱动
下载驱动:https://www.st.com/zh/development-tools/st-link-server.html#get-software
下载后解压安装
安装按照提示来
安装完成之后电脑重启后重新打开STM32CubeProgrammer即可正常下载代码