Author | shaniadolphin |
---|---|
349948204@qq.com |
搭建交叉編譯環(huán)境
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
檢查環(huán)境變量,測試是否可用:
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)
設(shè)置STM32CubeMx
打開應(yīng)用創(chuàng)建工程:
創(chuàng)建工程
設(shè)置輸出為Makefie:
設(shè)置輸出
即可通過STM32CUBEMX輸出工程代碼及對應(yīng)的Makefile。
輸出工程
如果后續(xù)要添加自己的代碼兼都,就需要修改這里的Makefile文件泄伪。
用Makefile生成工程的好處就是方便管理自己添加的文件失都。
在Makefile中打開硬件浮點(diǎn)后控,
# float-abi
FLOAT-ABI = -mfloat-abi=hard
鏈接時(shí)會報(bào)錯(cuò):
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
原因是庫版本不匹配庙曙,詳細(xì)參考
可以下載下面的庫文件,用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前部增加:
# 在調(diào)用工程時(shí)輸入 make V=1 來顯示編譯過程捌朴,否則默認(rèn)不顯示編譯時(shí)的指令
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的代碼時(shí)洼怔,可能會有錯(cuò)誤提示,可能和編譯順序有關(guān):
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取消左驾,生成工程镣隶,可以編譯成功,這時(shí)再增加freertos選項(xiàng)诡右,即可編譯通過安岂。
參考文檔
# | 鏈接地址 | 文檔名稱 |
---|---|---|
1 | https://blog.csdn.net/zhouxuanyuye/article/details/81143423 |
卷積神經(jīng)網(wǎng)絡(luò)中卷積的OpenCL實(shí)現(xiàn) |
2 | https://www.cnblogs.com/Reyzal/p/7389993.html |
Intel核心顯卡OpenCL環(huán)境搭建 |
3 | https://github.com/shaniadolphin/ |
github源碼 |