03.Ubuntu下开发STM32--灯,等灯等灯

16 篇文章 1 订阅

在Ubuntu下开发STM32并不简单。这里说的不简单是上手困难。不太适合新手。对一个刚开始学习STM32的人来说,如果一开始就使用Ubuntu开发,我觉得并不是一个好的注意。对于初学者来说,如果你的代码出现问题,你既要考虑是不是固件库的问题,又要考虑是不是Makefile的问题。还要考虑newlib(arm-none-eabi-gcc使用的库)系统调用的问题。这么多的问题加在一块,足够你想放弃学习了。

因此,对于初学者来说,就不要瞎折腾了。还是老老实实的使用keil mdk,iar等一些IDE,从0开始学习固件库。等你的固件库学习到一定的水平之后,在来折腾一下也是不错的。对于那些老手来说,编译器什么的已经不重要了。可以尝试一下。当你学会了使用Ubuntu开发STM32的时候,你自己做一个包,放到Arduino IDE下面。就可以尽情的使用Arduino来开发STM32了。

若是你一直使用盗版的keil,由于商业使用导致的版权问题,那么也可以考虑一下使用arm-none-eabi-gcc。使用arm-none-eabi-gcc并不要求你的系统是linux,是Ubuntu。而在windows平台下,也有arm-none-eabi-gcc。和Ubuntu下的是同一个公司开发的。一样好用。

固件库准备

对于ST的固件库来说,大部分是使用C进行编写的。不管是用什么环境开发,都是没有问题的。有问题的是汇编文件。armcc和gcc对于汇编的语法要求不同。因此之前使用mdk的汇编文件在这里就不要使用了。我们应该使用TrueStudio(在固件库的模板文件中)下的汇编文件。这个文件在ST的固件库中有。

除了汇编文件,armgcc还需要一份链接文件(*.ld)。这个文件在固件库中也可以找到。同样,还是需要使用TrueSTUDIO文件夹下的版本。如果你足够熟悉固件库的话。这些文件还是很容易找到的。

如果你找不到或者懒得找的话,也没关系。我会上传一个压缩包。里面会有我们这次使用的所有文件。
这次我们使用Makefile进行集中处理。

源代码结构

img

其中,

  • flash.ld是从TrueStudio文件夹下拷贝的。我把它改名为flash.ld。
  • 将启动文件的后缀从*.s改为*.S。

编写Makefile:

  • 由于Makefile文件和其他的源文件,头文件不再同一个文件夹中。为了能让make找到这些文件,需要使用vpath。

​ vpath %.S stlib
​ vpath %.c stlib stlib/src
​ vpath %.h stlib stlib/cminc stlib/inc

  • 使用vpath只是为了让make程序能够找到头文件。为了能够让arm-none-eabi-gcc找到头文件,需要在编译的时候使用-Ixxx指定头文件。
  • 在编译的时候,由于使用assert_param的原因,需要定义宏USE_STDPERIPH_DRIVER。使用-Dxxx指定宏。
  • 为了让arm gcc知道我们使用的是什么类型的单片机,需要指定参数-mcpu=cortex-m4 -mthumb。
  • 链接完成之后,生成的文件是*.elf格式的。而我们使用st-flash下载使用的文件是bin格式的。因此我们需要使用arm-none-eabi-objcopy来将elf转换成为bin格式。
  • st-flash下载的时候需要指定下载的地址。我们使用0x0800_0000这个地址。(在flash.ld中,作为flash的起始地址)
完整的Makefile如下

img

编写main函数

我们在main函数中,写点代码来点亮一个led灯。关于_exit会在以后讲到的。

img

编译,下载
  • 使用make all进行编译,链接。在完成之后,可以看见Makefile的目录中,多了blink.elf和blink.bin两个文件。
  • 使用sudo make burn进行下载。由于st-flash需要superuser权限,所以需要在前面加上sudo。
  • 在下载成功之后,可以看到灯以一定的频率闪烁。

make all之后可以看到程序的大小:

img

(这个6372是代码的字节数。点亮一个led灯却占用了6K的代码。如果你和keil mdk相比的话会发现这个数大的多。这并不是说明arm gcc比armcc弱。只是我们没有对代码进行优化。会在下面几篇中讲解如何优化)

make burn进行下载:

img

W: http://archive.ubuntu.com/ubuntu/dists/jammy/InRelease: The key(s) in the keyring /etc/apt/trusted.gpg.d/ubuntu-keyring-2012-cdimage.gpg are ignored as the file is not readable by user '_apt' executing apt-key. W: http://archive.ubuntu.com/ubuntu/dists/jammy/InRelease: The key(s) in the keyring /etc/apt/trusted.gpg.d/ubuntu-keyring-2018-archive.gpg are ignored as the file is not readable by user '_apt' executing apt-key. W: GPG error: http://archive.ubuntu.com/ubuntu jammy InRelease: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 871920D1991BC93C E: The repository 'http://archive.ubuntu.com/ubuntu jammy InRelease' is not signed. W: http://security.ubuntu.com/ubuntu/dists/jammy-security/InRelease: The key(s) in the keyring /etc/apt/trusted.gpg.d/ubuntu-keyring-2012-cdimage.gpg are ignored as the file is not readable by user '_apt' executing apt-key. W: http://security.ubuntu.com/ubuntu/dists/jammy-security/InRelease: The key(s) in the keyring /etc/apt/trusted.gpg.d/ubuntu-keyring-2018-archive.gpg are ignored as the file is not readable by user '_apt' executing apt-key. W: GPG error: http://security.ubuntu.com/ubuntu jammy-security InRelease: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 871920D1991BC93C E: The repository 'http://security.ubuntu.com/ubuntu jammy-security InRelease' is not signed. W: http://archive.ubuntu.com/ubuntu/dists/jammy-updates/InRelease: The key(s) in the keyring /etc/apt/trusted.gpg.d/ubuntu-keyring-2012-cdimage.gpg are ignored as the file is not readable by user '_apt' executing apt-key. W: http://archive.ubuntu.com/ubuntu/dists/jammy-updates/InRelease: The key(s) in the keyring /etc/apt/trusted.gpg.d/ubuntu-keyring-2018-archive.gpg are ignored as the file is not readable by user '_apt' executing apt-key. W: GPG error: http://archive.ubuntu.com/ubuntu jammy-updates InRelease: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 871920D1991BC93C E: The repository 'http://archive.ubuntu.com/ubuntu jammy-updates InRelease' is not signed. W: http://archive.ubuntu.com/ubuntu/dists/jammy-backports/InRelease: The key(s) in the keyring /etc/apt/trusted.gpg.d/ubuntu-keyring-2012-cdimage.gpg are ignored as the file is not readable by user '_apt' executing apt-key. W: http://archive.ubuntu.com/ubuntu/dists/jammy-backports/InRelease: The key(s) in the keyring /etc/apt/trusted.gpg.d/ubuntu-keyring-2018-archive.gpg are ignored as the file is not readable by user '_apt' executing apt-key. W: GPG error: http://archive.ubuntu.com/ubuntu jammy-backports InRelease: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 871920D1991BC93C E: The repository 'http://archive.ubuntu.com/ubuntu jammy-backports InRelease' is not signed. E: Problem executing scripts APT::Update::Post-Invoke 'rm -f /var/cache/apt/archives/*.deb /var/cache/apt/archives/partial/*.deb /var/cache/apt/*.bin || true' E: Sub-process returned an error code
06-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值