Author | shaniadolphin |
---|---|
349948204@qq.com |
搭建交叉编译环境
stm32 属于arm cortex-m系列thumb指令集,使用的编译工具是arm-none-eabi,在ubuntu下的安装可以参考以下方式:
sudo add-apt-repository ppa:team-gcc-arm-embedded/ppa
sudo apt-get update
sudo apt-get install gcc-arm-embedded
检查环境变量,测试是否可用:
dolphin@DESKTOP-DR5OKOG:/mnt/e/WORK/shaniadolphin/stm32/test_gcc$ echo $PATH | grep "gcc"
dolphin@DESKTOP-DR5OKOG:/mnt/e/WORK/shaniadolphin/stm32/test_gcc$ arm-none-eabi-gcc -v
Using built-in specs.
COLLECT_GCC=arm-none-eabi-gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/arm-none-eabi/6.3.1/lto-wrapper
Target: arm-none-eabi
gcc version 6.3.1 20170620 (15:6.3.1+svn253039-1build1)
设置STM32CubeMx
打开应用创建工程:
创建工程
设置输出为Makefie:
设置输出
即可通过STM32CUBEMX输出工程代码及对应的Makefile。
输出工程
如果后续要添加自己的代码,就需要修改这里的Makefile文件。
用Makefile生成工程的好处就是方便管理自己添加的文件。
在Makefile中打开硬件浮点,
# float-abi
FLOAT-ABI = -mfloat-abi=hard
链接时会报错:
LD stm32f405_ledscreen_freertos.elf
/usr/lib/gcc/arm-none-eabi/6.3.1/../../../arm-none-eabi/bin/ld: error: /usr/lib/gcc/arm-none-eabi/6.3.1/../../../arm-none-eabi/lib/crt0.o: Conflicting CPU architectures 13/1
/usr/lib/gcc/arm-none-eabi/6.3.1/../../../arm-none-eabi/bin/ld: failed to merge target specific data of file /usr/lib/gcc/arm-none-eabi/6.3.1/../../../arm-none-eabi/lib/crt0.o
collect2: error: ld returned 1 exit status
Makefile:199: recipe for target 'build/stm32f405_ledscreen_freertos.elf' failed
make: *** [build/stm32f405_ledscreen_freertos.elf] Error 1
原因是库版本不匹配,详细参考
可以下载下面的库文件,用dpkg的命令安装这样deb包,以安装匹配的库:
libnewlib-dev_3.0.0.20180802-2_all.deb
libnewlib-arm-none-eabi_3.0.0.20180802-2_all.deb
sudo dpkg -i libnewlib-dev_3.0.0.20180802-2_all.deb
sudo dpkg -i libnewlib-arm-none-eabi_3.0.0.20180802-2_all.deb
修改Makefile
在Makefile前部增加:
# 在调用工程时输入 make V=1 来显示编译过程,否则默认不显示编译时的指令
ifneq ($(V),1)
Q := @
NULL := 2>/dev/null
endif
修改编译打印:
#######################################
# build the application
#######################################
# list of objects
OBJECTS = $(addprefix $(BUILD_DIR)/,$(notdir $(C_SOURCES:.c=.o)))
vpath %.c $(sort $(dir $(C_SOURCES)))
# list of ASM program objects
OBJECTS += $(addprefix $(BUILD_DIR)/,$(notdir $(ASM_SOURCES:.s=.o)))
vpath %.s $(sort $(dir $(ASM_SOURCES)))
$(BUILD_DIR)/%.o: %.c Makefile | $(BUILD_DIR)
@printf " CC $(*).c\n"
$(Q) $(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@
$(BUILD_DIR)/%.o: %.s Makefile | $(BUILD_DIR)
@printf " AS $(*).s\n"
$(Q) $(AS) -c $(CFLAGS) $< -o $@
$(BUILD_DIR)/$(TARGET).elf: $(OBJECTS) Makefile
@printf " LD $(TARGET).elf\n"
$(Q) $(CC) $(OBJECTS) $(LDFLAGS) -o $@
@printf " SZ $(TARGET).elf\n"
$(Q) $(SZ) $@
$(BUILD_DIR)/%.hex: $(BUILD_DIR)/%.elf | $(BUILD_DIR)
$(HEX) $< $@
$(BUILD_DIR)/%.bin: $(BUILD_DIR)/%.elf | $(BUILD_DIR)
$(BIN) $< $@
$(BUILD_DIR):
mkdir $@
使打印像如图所示的效果,
make -j
CC main.c
CC stm32l0xx_it.c
CC stm32l0xx_hal_msp.c
CC stm32l0xx_hal_adc.c
CC stm32l0xx_hal_adc_ex.c
CC stm32l0xx_hal_i2c.c
CC stm32l0xx_hal_i2c_ex.c
CC stm32l0xx_hal_rtc.c
CC stm32l0xx_hal_tim.c
CC stm32l0xx_hal_rtc_ex.c
CC stm32l0xx_hal_tim_ex.c
CC stm32l0xx_hal.c
CC stm32l0xx_hal_flash_ex.c
CC stm32l0xx_hal_pwr.c
CC stm32l0xx_hal_flash_ramfunc.c
CC stm32l0xx_hal_pwr_ex.c
CC stm32l0xx_hal_dma.c
CC system_stm32l0xx.c
CC stm32l0xx_hal_gpio.c
CC stm32l0xx_hal_rcc.c
AS startup_stm32l011xx.s
CC stm32l0xx_hal_rcc_ex.c
CC stm32l0xx_hal_cortex.c
CC stm32l0xx_hal_flash.c
LD firmware.elf
SZ firmware.elf
text data bss dec hex filename
10152 20 1836 12008 2ee8 build/firmware.elf
arm-none-eabi-objcopy -O ihex build/firmware.elf build/firmware.hex
arm-none-eabi-objcopy -O binary -S build/firmware.elf build/firmware.bin
否则会显示完整的编译指令,让命令窗非常凌乱:
dolphin@DESKTOP-DR5OKOG:/mnt/e/WORK/shaniadolphin/stm32/test_gcc$ make
mkdir build
arm-none-eabi-gcc -c -mcpu=cortex-m3 -mthumb -DUSE_HAL_DRIVER -DSTM32F103xB -IInc -IDrivers/STM32F1xx_HAL_Driver/Inc -IDrivers/STM32F1xx_HAL_Driver/Inc/Legacy -IMiddlewares/ST/STM32_USB_Device_Library/Core/Inc -IMiddlewares/ST/STM32_USB_Device_Library/Class/CDC/Inc -IDrivers/CMSIS/Device/ST/STM32F1xx/Include -IDrivers/CMSIS/Include -IMiddlewares/Third_Party/FreeRTOS/Source/portable/GCC/ARM_CM3 -IMiddlewares/Third_Party/FreeRTOS/Source/include -IMiddlewares/Third_Party/FreeRTOS/Source/CMSIS_RTOS -O3 -Wall -fdata-sections -ffunction-sections -g -gdwarf-2 -MMD -MP -MF"build/main.d" -Wa,-a,-ad,-alms=build/main.lst Src/main.c -o build/main.o
arm-none-eabi-gcc build/main.o build/usb_device.o build/usbd_conf.o build/usbd_desc.o build/usbd_cdc_if.o build/stm32f1xx_it.o build/stm32f1xx_hal_msp.o build/stm32f1xx_hal_timebase_TIM.o build/stm32f1xx_hal_gpio_ex.o build/stm32f1xx_hal_pcd.o build/stm32f1xx_hal_pcd_ex.o build/stm32f1xx_ll_usb.o build/stm32f1xx_hal_adc.o build/stm32f1xx_hal_adc_ex.o build/stm32f1xx_hal_spi.o build/stm32f1xx_hal_spi_ex.o build/stm32f1xx_hal_tim.o build/stm32f1xx_hal_tim_ex.o build/stm32f1xx_hal_uart.o build/stm32f1xx_hal.o build/stm32f1xx_hal_rcc.o build/stm32f1xx_hal_rcc_ex.o build/stm32f1xx_hal_gpio.o build/stm32f1xx_hal_dma.o build/stm32f1xx_hal_cortex.o build/stm32f1xx_hal_pwr.o build/stm32f1xx_hal_flash.o build/stm32f1xx_hal_flash_ex.o build/system_stm32f1xx.o build/usbd_core.o build/usbd_ctlreq.o build/usbd_ioreq.o build/usbd_cdc.o build/freertos.o build/heap_4.o build/port.o build/croutine.o build/event_groups.o build/list.o build/queue.o build/tasks.o build/timers.o build/cmsis_os.o build/startup_stm32f103xb.o -mcpu=cortex-m3 -mthumb -specs=nano.specs -TSTM32F103C8Tx_FLASH.ld -lc -lm -lnosys -Wl,-Map=build/oled_test.map,--cref -Wl,--gc-sections -o build/oled_test.elf
arm-none-eabi-size build/oled_test.elf
text data bss dec hex filename
19500 360 11064 30924 78cc build/oled_test.elf
arm-none-eabi-objcopy -O ihex build/oled_test.elf build/oled_test.hex
arm-none-eabi-objcopy -O binary -S build/oled_test.elf build/oled_test.bin
需要注意编译freertos的代码时,可能会有错误提示,可能和编译顺序有关:
dolphin@DESKTOP-DR5OKOG:/mnt/e/WORK/shaniadolphin/stm32/test_gcc$ make
mkdir build
arm-none-eabi-gcc -c -mcpu=cortex-m3 -mthumb -DUSE_HAL_DRIVER -DSTM32F103xB -IInc -IDrivers/STM32F1xx_HAL_Driver/Inc -IDrivers/STM32F1xx_HAL_Driver/Inc/Legacy -IMiddlewares/Third_Party/FreeRTOS/Source/portable/GCC/ARM_CM3 -IMiddlewares/ST/STM32_USB_Device_Library/Core/Inc -IMiddlewares/ST/STM32_USB_Device_Library/Class/CDC/Inc -IDrivers/CMSIS/Device/ST/STM32F1xx/Include -IMiddlewares/Third_Party/FreeRTOS/Source/include -IMiddlewares/Third_Party/FreeRTOS/Source/CMSIS_RTOS -IDrivers/CMSIS/Include -O3 -Wall -fdata-sections -ffunction-sections -g -gdwarf-2 -MMD -MP -MF"build/main.d" -Wa,-a,-ad,-alms=build/main.lst Src/main.c -o build/main.o
arm-none-eabi-gcc -c -mcpu=cortex-m3 -mthumb -DUSE_HAL_DRIVER -DSTM32F103xB -IInc -IDrivers/STM32F1xx_HAL_Driver/Inc -IDrivers/STM32F1xx_HAL_Driver/Inc/Legacy -IMiddlewares/Third_Party/FreeRTOS/Source/portable/GCC/ARM_CM3 -IMiddlewares/ST/STM32_USB_Device_Library/Core/Inc -IMiddlewares/ST/STM32_USB_Device_Library/Class/CDC/Inc -IDrivers/CMSIS/Device/ST/STM32F1xx/Include -IMiddlewares/Third_Party/FreeRTOS/Source/include -IMiddlewares/Third_Party/FreeRTOS/Source/CMSIS_RTOS -IDrivers/CMSIS/Include -O3 -Wall -fdata-sections -ffunction-sections -g -gdwarf-2 -MMD -MP -MF"build/freertos.d" -Wa,-a,-ad,-alms=build/freertos.lst Src/freertos.c -o build/freertos.o
Src/freertos.c:91:8: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'void'
__weak void PreSleepProcessing(uint32_t *ulExpectedIdleTime)
^~~~
Src/freertos.c:96:8: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'void'
__weak void PostSleepProcessing(uint32_t *ulExpectedIdleTime)
^~~~
Makefile:191: recipe for target 'build/freertos.o' failed
make: *** [build/freertos.o] Error 1
将freertos取消,生成工程,可以编译成功,这时再增加freertos选项,即可编译通过。
参考文档
# | 链接地址 | 文档名称 |
---|---|---|
1 | https://blog.csdn.net/zhouxuanyuye/article/details/81143423 |
卷积神经网络中卷积的OpenCL实现 |
2 | https://www.cnblogs.com/Reyzal/p/7389993.html |
Intel核心显卡OpenCL环境搭建 |
3 | https://github.com/shaniadolphin/ |
github源码 |