在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进行集中处理。
源代码结构
其中,
- 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如下
编写main函数
我们在main函数中,写点代码来点亮一个led灯。关于_exit会在以后讲到的。
编译,下载
- 使用make all进行编译,链接。在完成之后,可以看见Makefile的目录中,多了blink.elf和blink.bin两个文件。
- 使用sudo make burn进行下载。由于st-flash需要superuser权限,所以需要在前面加上sudo。
- 在下载成功之后,可以看到灯以一定的频率闪烁。
make all之后可以看到程序的大小:
(这个6372是代码的字节数。点亮一个led灯却占用了6K的代码。如果你和keil mdk相比的话会发现这个数大的多。这并不是说明arm gcc比armcc弱。只是我们没有对代码进行优化。会在下面几篇中讲解如何优化)
make burn进行下载: