前面文章有写,我上手时采用了IDE,但后来实践中,我再也没打开过STM32CubeIDE,原因比较多吧,
当然后两点纯粹个人原因 哈哈
实际学习中,我基本上是采用的gcc+sublime text
或者gcc+vim
。因为我习惯这么用了,虽然用着谈不上方便,但是顺手。
可是,开始上手时,对于源码的不了解,加之GCC
下只有一个HelloWorld
,让我对TencentOS-tiny的探索之旅无所适从。所以,为了后人乘凉,我干脆就把我编写好的Makefile文件和当时修改文件的思路拿出来分享给大家。
首先,我们先看看标准Makefile到底长啥样子,这里,我们打开board/TencentOS_tiny_EVB_MX/GCC/hello_world/Makefile
文件,分析一下里面的结构,就发现其实大部分地方不需要改动,我们只用将我们的examples
需要编译的c文件,以及需要包含的头文件包含进去就可以了。
所以,找到待编译的文件,还有头文件,就是我们的目标。
现在转战到KEIL目录下,我们以farm_ai_demo
文件为例,使用文本编辑器打开board/TencentOS_tiny_EVB_MX/KEIL/farm_ai_demo/TencentOS_tiny.uvprojx
文件,就可以看到里面的文件结构,大致浏览一下,发现这就是个xml文件。
就在
标签之中,所有的头文件包含都在这里了。我们把这段复制出来,进行处理一下:
先将Windows下的路径分隔符\
转换为/
,然后使用换行替换掉;
再将../
转换为对应的路径,这样,就可以把这段代码添加到Makefile文件的头文件包含段了。我们对他们再进行一些模块划分,最终头文件包含的效果就是这样:
# C includes
KERNEL_INC = \
-I $(TOP_DIR)/kernel/core/include \
-I $(TOP_DIR)/kernel/pm/include \
-I $(TOP_DIR)/kernel/hal/include \
-I $(TOP_DIR)/arch/arm/arm-v7m/common/include \
-I $(TOP_DIR)/arch/arm/arm-v7m/cortex-m4/gcc
C_INCLUDES += $(KERNEL_INC)
CMSIS_INC = \
-I $(TOP_DIR)/osal/cmsis_os
C_INCLUDES += $(CMSIS_INC)
PLATFORM_INC = \
-I $(TOP_DIR)/platform/vendor_bsp/st/STM32L4xx_HAL_Driver/Inc \
-I $(TOP_DIR)/platform/vendor_bsp/st/STM32L4xx_HAL_Driver/Inc/Legacy \
-I $(TOP_DIR)/platform/vendor_bsp/st/CMSIS/Device/ST/STM32L4xx/Include \
-I $(TOP_DIR)/platform/vendor_bsp/st/CMSIS/Include
C_INCLUDES += $(PLATFORM_INC)
BOARD_INC = \
-I $(TOP_DIR)/board/TencentOS_tiny_EVB_MX/TOS-CONFIG \
-I $(TOP_DIR)/board/TencentOS_tiny_EVB_MX/BSP/Inc \
-I $(TOP_DIR)/board/TencentOS_tiny_EVB_MX/BSP/Hardware/DHT11 \
-I $(TOP_DIR)/board/TencentOS_tiny_EVB_MX/BSP/Hardware/BH1750 \
-I $(TOP_DIR)/board/TencentOS_tiny_EVB_MX/BSP/Hardware/E53_IA1 \
-I $(TOP_DIR)/board/TencentOS_tiny_EVB_MX/BSP/Hardware/OLED
C_INCLUDES += $(BOARD_INC)
NET_INC = \
-I $(TOP_DIR)/net/at/include \
-I $(TOP_DIR)/net/sal_module_wrapper \
-I $(TOP_DIR)/devices/esp8266
C_INCLUDES += $(NET_INC)
COMPONENTS_INC = \
-I $(TOP_DIR)/components/utils/JSON/include \
-I $(TOP_DIR)/components/connectivity/Eclipse-Paho-MQTT/wrapper/include \
-I $(TOP_DIR)/components/connectivity/Eclipse-Paho-MQTT/3rdparty/include
C_INCLUDES += $(COMPONENTS_INC)
EXAMPLES_INC = \
-I $(TOP_DIR)/examples/farm_ai_demo
C_INCLUDES += $(EXAMPLES_INC)
不过,跟刚才的头文件稍有不同,这里每个源码都是单独的标签,不再拥挤在一起,而且,路径也单独标识出来,看着好像不太好搞。不过,没关系,这点根本难不倒读文章到这里的你:
复制所有的FilePath
标签行到新文件,替换掉标签,并且把Windows下的路径分隔符\
转换为/
,操作跟之前都相似。
最终,我们得到了一个个热气腾腾的源码文件列表,把他们稍加分类,添加到我们的Makefile中:
# C sources
KERNEL_SRC = \
${wildcard $(TOP_DIR)/kernel/core/*.c}
C_SOURCES += $(KERNEL_SRC)
ARCH_SRC = \
${wildcard $(TOP_DIR)/arch/arm/arm-v7m/cortex-m4/gcc/*.c} \
${wildcard $(TOP_DIR)/arch/arm/arm-v7m/common/*.c}
C_SOURCES += $(ARCH_SRC)
CMSIS_SRC = \
${wildcard $(TOP_DIR)/osal/cmsis_os/*.c}
C_SOURCES += $(CMSIS_SRC)
PLATFORM_SRC = \
$(TOP_DIR)/platform/vendor_bsp/st/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_tim.c \
$(TOP_DIR)/platform/vendor_bsp/st/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_tim_ex.c \
$(TOP_DIR)/platform/vendor_bsp/st/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_uart.c \
$(TOP_DIR)/platform/vendor_bsp/st/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_uart_ex.c \
$(TOP_DIR)/platform/vendor_bsp/st/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal.c \
$(TOP_DIR)/platform/vendor_bsp/st/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_i2c.c \
$(TOP_DIR)/platform/vendor_bsp/st/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_i2c_ex.c \
$(TOP_DIR)/platform/vendor_bsp/st/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_rcc.c \
$(TOP_DIR)/platform/vendor_bsp/st/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_rcc_ex.c \
$(TOP_DIR)/platform/vendor_bsp/st/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_flash.c \
$(TOP_DIR)/platform/vendor_bsp/st/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_flash_ex.c \
$(TOP_DIR)/platform/vendor_bsp/st/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_flash_ramfunc.c \
$(TOP_DIR)/platform/vendor_bsp/st/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_gpio.c \
$(TOP_DIR)/platform/vendor_bsp/st/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_dma.c \
$(TOP_DIR)/platform/vendor_bsp/st/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_dma_ex.c \
$(TOP_DIR)/platform/vendor_bsp/st/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_pwr.c \
$(TOP_DIR)/platform/vendor_bsp/st/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_pwr_ex.c \
$(TOP_DIR)/platform/vendor_bsp/st/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_cortex.c \
$(TOP_DIR)/platform/vendor_bsp/st/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_adc_ex.c \
$(TOP_DIR)/platform/vendor_bsp/st/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_adc.c \
$(TOP_DIR)/platform/vendor_bsp/st/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_dac.c \
$(TOP_DIR)/platform/vendor_bsp/st/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_dac_ex.c \
$(TOP_DIR)/platform/vendor_bsp/st/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_spi.c \
$(TOP_DIR)/platform/vendor_bsp/st/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_spi_ex.c \
$(TOP_DIR)/platform/hal/st/stm32l4xx/src/tos_hal_uart.c
C_SOURCES += $(PLATFORM_SRC)
NET_SRC = \
$(TOP_DIR)/net/at/src/tos_at.c \
$(TOP_DIR)/net/at/src/tos_at_utils.c \
$(TOP_DIR)/net/sal_module_wrapper/sal_module_wrapper.c \
$(TOP_DIR)/devices/esp8266/esp8266.c
C_SOURCES += $(NET_SRC)
BSP_SRC = \
$(TOP_DIR)/board/TencentOS_tiny_EVB_MX/BSP/Src/gpio.c \
$(TOP_DIR)/board/TencentOS_tiny_EVB_MX/BSP/Src/main.c \
$(TOP_DIR)/board/TencentOS_tiny_EVB_MX/BSP/Src/mcu_init.c \
$(TOP_DIR)/board/TencentOS_tiny_EVB_MX/BSP/Src/stm32l4xx_hal_msp.c \
$(TOP_DIR)/board/TencentOS_tiny_EVB_MX/BSP/Src/stm32l4xx_it_module.c \
$(TOP_DIR)/board/TencentOS_tiny_EVB_MX/BSP/Src/usart.c \
$(TOP_DIR)/board/TencentOS_tiny_EVB_MX/BSP/Src/adc.c \
$(TOP_DIR)/board/TencentOS_tiny_EVB_MX/BSP/Src/dac.c \
$(TOP_DIR)/board/TencentOS_tiny_EVB_MX/BSP/Src/i2c.c \
$(TOP_DIR)/board/TencentOS_tiny_EVB_MX/BSP/Src/spi.c \
$(TOP_DIR)/board/TencentOS_tiny_EVB_MX/BSP/Src/system_stm32l4xx.c \
$(TOP_DIR)/board/TencentOS_tiny_EVB_MX/BSP/Hardware/DHT11/DHT11_BUS.c \
$(TOP_DIR)/board/TencentOS_tiny_EVB_MX/BSP/Hardware/OLED/oled.c \
$(TOP_DIR)/board/TencentOS_tiny_EVB_MX/BSP/Hardware/E53_IA1/E53_IA1.c
C_SOURCES += $(BSP_SRC)
COMPONENTS_SRC = \
$(TOP_DIR)/components/connectivity/Eclipse-Paho-MQTT/wrapper/src/mqtt_wrapper.c \
$(TOP_DIR)/components/connectivity/Eclipse-Paho-MQTT/wrapper/src/transport_wrapper.c \
$(TOP_DIR)/components/connectivity/Eclipse-Paho-MQTT/3rdparty/src/MQTTConnectClient.c \
$(TOP_DIR)/components/connectivity/Eclipse-Paho-MQTT/3rdparty/src/MQTTConnectServer.c \
$(TOP_DIR)/components/connectivity/Eclipse-Paho-MQTT/3rdparty/src/MQTTDeserializePublish.c \
$(TOP_DIR)/components/connectivity/Eclipse-Paho-MQTT/3rdparty/src/MQTTFormat.c \
$(TOP_DIR)/components/connectivity/Eclipse-Paho-MQTT/3rdparty/src/MQTTPacket.c \
$(TOP_DIR)/components/connectivity/Eclipse-Paho-MQTT/3rdparty/src/MQTTSerializePublish.c \
$(TOP_DIR)/components/connectivity/Eclipse-Paho-MQTT/3rdparty/src/MQTTSubscribeClient.c \
$(TOP_DIR)/components/connectivity/Eclipse-Paho-MQTT/3rdparty/src/MQTTSubscribeServer.c \
$(TOP_DIR)/components/connectivity/Eclipse-Paho-MQTT/3rdparty/src/MQTTUnsubscribeClient.c \
$(TOP_DIR)/components/connectivity/Eclipse-Paho-MQTT/3rdparty/src/MQTTUnsubscribeServer.c \
$(TOP_DIR)/components/utils/JSON/src/cJSON.c
C_SOURCES += $(COMPONENTS_SRC)
EXAMPLES_SRC = \
$(TOP_DIR)/examples/farm_ai_demo/farm_ai_demo.c
C_SOURCES += $(EXAMPLES_SRC)
# ASM sources
ASM_SOURCES = \
startup_stm32l431xx.s \
ASM_SOURCES_S = \
$(TOP_DIR)/arch/arm/arm-v7m/cortex-m4/gcc/port_s.S
就这样,一个崭新的Makefile诞生了!
在之前的文章中也有提到,默认的编译器,对浮点型的printf("%f")
是不支持的,这里我们改一下:
LDFLAGS = $(MCU) -u _printf_float -specs=nano.specs -T$(LDSCRIPT) $(LIBDIR) $(LIBS) \
-Wl,-Map=$(BUILD_DIR)/$(TARGET).map,--cref -Wl,--gc-sections
在这句中,我们添加了一个参数 -u _printf_float
, 这样,就可以顺利打印浮点型数据了。
其实这篇文章挺水的,都开始看操作系统源码以及应用了,不可能改个Makefile都不会。不过我对自己的认识,算是一个浇花匠吧,我不浇水,花儿也可以正常生长,但我浇了水,可以让花儿长的更好,不用再期待雨水的降临。所谓 锦上添花,大概如此吧。
万丈高楼平地起,虽不像那些大楼设计师、高级技师,可以建设栋梁,但现在这个阶段,TencentOS-tiny的文档和代码都不是很完善,就需要很多我这样的基层劳动者进行修修补补,搬砖添瓦。我相信我的工作是有意义的。
愿未来更好!