最近开始研究了一下stm32cube,发现挺好用的,就是bug太多,经常生成的工程不能编译.
我一般选择生成makefile
,这样对自动集成比较友好.方便后续迭代.
cube配置

没有硬件,电路在不断改设计(这个是我随便画得,不一定能运行,没看手册),暂定这么分配管脚.这次主要是测试编译和接口,为以后开发踩下坑
芯片支持usbfs和fdcan,可以做个canfd数据分析,省下周立功两千多大洋(感觉周立功的东西去个0差不多),vector这种级别的比不了还是买吧.
而且支持双向USBPD,可以做快充,或者大功率耗电.看了下st的pd视频,估计未来的st芯片系列都会带有这个模块.
cube坑和注意事项
- pd快充的硬件模块
ucpd
必须要手动开启中断和DMA,增加USBPD
中间件并不会自动配置. - GUI_INTERFACE启用后可以对PD进行debug,ST有一个上位机PD工具
- USBPD开启TRACE_EMB,并且TRACE_EMB模块打开可以在串口debug,也可以在GUI看到.
生成代码的坑
这坑就大了…
Makefile上
IDrivers/CMSIS/Include
这里多一行,生成工程经常遇到,老错误了- 下面这段
LIBS = -lc -lm -lnosys \
-lUSBPDCORE_PD3_FULL_CM4_wc32.a
LIBDIR = \
-LMiddlewares/ST/STM32_USBPD_Library/Core/lib
把链接文件当库用了…应当是下面这样:
LIBS = -lc -lm -lnosys \
Middlewares/ST/STM32_USBPD_Library/Core/lib/USBPDCORE_PD3_FULL_CM4_wc32.a
LIBDIR =
3. 修改下面编译阶段,加入ccache可以加快编译,毕竟整个库几乎不用改.不然每次修改makefile都会全部重新构建.
4. cube配置后可能有重复的文件加进去,导致一堆符号重复,删除重复即可
库BUG
经过上面反复折腾,如果你在cube配置时选择双向(dual
)PD你会看到下面错误:
/usr/bin/ccache arm-none-eabi-gcc -c -mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=hard -DUSE_FULL_LL_DRIVER -DUSBPD_PORT_COUNT=1 -D_RTOS -D_DRP -D_TRACE -D_GUI_INTERFACE -DUSBPDCORE_LIB_PD3_FULL -DUSE_HAL_DRIVER -DSTM32G431xx -IInc -IDrivers/STM32G4xx_HAL_Driver/Inc -IDrivers/STM32G4xx_HAL_Driver/Inc/Legacy -IUtilities/GUI_INTERFACE -IUtilities/TRACER_EMB -IMiddlewares/Third_Party/FreeRTOS/Source/include -IMiddlewares/Third_Party/FreeRTOS/Source/CMSIS_RTOS_V2 -IMiddlewares/Third_Party/FreeRTOS/Source/portable/GCC/ARM_CM4F -IMiddlewares/ST/STM32_USBPD_Library/Core/inc -IMiddlewares/ST/STM32_USBPD_Library/Devices/STM32G4XX/inc -IMiddlewares/ST/STM32_USBPD_Library/../../../Drivers/STM32G4xx_HAL_Driver/Inc -IMiddlewares/ST/STM32_USB_Device_Library/Core/Inc -IMiddlewares/ST/STM32_USB_Device_Library/Class/CDC/Inc -IDrivers/CMSIS/Device/ST/STM32G4xx/Include -IDrivers/CMSIS/Include -Og -Wall -fdata-sections -ffunction-sections -g -gdwarf-2 -MMD -MP -MF"build/usbpd_cad_hw_if.d" -Wa,-a,-ad,-alms=build/usbpd_cad_hw_if.lst Middlewares/ST/STM32_USBPD_Library/Devices/STM32G4XX/src/usbpd_cad_hw_if.c -o build/usbpd_cad_hw_if.o
Middlewares/ST/STM32_USBPD_Library/Devices/STM32G4XX/src/usbpd_cad_hw_if.c:167:17: warning: 'ManageStateDetached_SNK' used but never defined
167 | static uint32_t ManageStateDetached_SNK(uint8_t PortNum);
| ^~~~~~~~~~~~~~~~~~~~~~~
arm-none-eabi-gcc build/main.o build/app_freertos.o build/usbpd.o build/usbpd_dpm_user.o build/usbpd_pwr_user.o build/usbpd_pwr_if.o build/usbpd_vdm_user.o build/usbpd_dpm_core.o build/usb_device.o build/usbd_conf.o build/usbd_desc.o build/usbd_cdc_if.o build/stm32g4xx_it.o build/stm32g4xx_hal_msp.o build/stm32g4xx_hal_timebase_tim.o build/stm32g4xx_ll_utils.o build/stm32g4xx_ll_exti.o build/stm32g4xx_hal_gpio.o build/stm32g4xx_hal_pcd.o build/stm32g4xx_hal_pcd_ex.o build/stm32g4xx_ll_usb.o build/stm32g4xx_hal.o build/stm32g4xx_hal_rcc.o build/stm32g4xx_hal_rcc_ex.o build/stm32g4xx_hal_flash.o build/stm32g4xx_hal_flash_ex.o build/stm32g4xx_hal_flash_ramfunc.o build/stm32g4xx_hal_exti.o build/stm32g4xx_hal_dma.o build/stm32g4xx_hal_dma_ex.o build/stm32g4xx_hal_pwr.o build/stm32g4xx_hal_pwr_ex.o build/stm32g4xx_hal_cortex.o build/stm32g4xx_hal_adc.o build/stm32g4xx_hal_adc_ex.o build/stm32g4xx_ll_adc.o build/stm32g4xx_hal_crc.o build/stm32g4xx_hal_crc_ex.o build/stm32g4xx_hal_dac.o build/stm32g4xx_hal_dac_ex.o build/stm32g4xx_hal_fdcan.o build/stm32g4xx_hal_i2c.o build/stm32g4xx_hal_i2c_ex.o build/stm32g4xx_hal_smbus.o build/stm32g4xx_hal_i2s.o build/stm32g4xx_hal_iwdg.o build/stm32g4xx_ll_lpuart.o build/stm32g4xx_ll_gpio.o build/stm32g4xx_ll_dma.o build/stm32g4xx_hal_uart.o build/stmle32g4xx_hal_uart_ex.o build/stm32g4xx_hal_rng.o build/stm32g4xx_hal_rtc.o build/stm32g4xx_hal_rtc_ex.o build/stm32g4xx_hal_spi.o build/stm32g4xx_hal_spi_ex.o build/stm32g4xx_hal_tim.o build/stm32g4xx_hal_tim_ex.o build/stm32g4xx_ll_pwr.o build/stm32g4xx_ll_usart.o build/stm32g4xx_ll_ucpd.o build/system_stm32g4xx.o build/croutine.o build/event_groups.o build/list.o build/queue.o build/stream_buffer.o build/tasks.o build/timers.o build/cmsis_os2.o build/heap_4.o build/port.o build/usbpd_trace.o build/usbpd_cad_hw_if.o build/usbpd_hw.o build/usbpd_hw_if_it.o build/usbpd_phy.o build/usbpd_phy_hw_if.o build/usbpd_pwr_hw_if.o build/usbpd_timersserver.o build/stm32g4xx_ll_tim.o build/usbd_core.o build/usbd_ctlreq.o build/usbd_ioreq.o build/usbd_cdc.o build/data_struct_tlv.o build/bsp_gui.o build/gui_api.o build/tracer_emb.o build/tracer_emb_hw.o build/stm32g4xx_ll_rcc.o build/startup_stm32g431xx.o -mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=hard -specs=nano.specs -TSTM32G431RBTx_FLASH.ld -lc -lm -lnosys Middlewares/ST/STM32_USBPD_Library/Core/lib/USBPDCORE_PD3_FULL_CM4_wc32.a -Wl,-Map=build/sthub.map,--cref -Wl,--gc-sections -o build/sthub.elf
/usr/lib/gcc/arm-none-eabi/9.3.0/../../../../arm-none-eabi/bin/ld: build/usbpd_cad_hw_if.o: in function `ManageStateDetached_DRP':
/home/feilong/workspace/sthub/prj/stm32g431rbtx/Middlewares/ST/STM32_USBPD_Library/Devices/STM32G4XX/src/usbpd_cad_hw_if.c:1137: undefined reference to `ManageStateDetached_SNK'
collect2: error: ld returned 1 exit status
make: *** [Makefile:260:build/sthub.elf] 错误 1
这个问题我猜了好久,因为之前的链接库错误,我一直在找这个库在那个lib里面,就差对全系统文件进行objdump
了,后来突然发现这个ManageStateDetached_SNK
函数就在报错的那个文件里面.不过是这么写的
#if defined(_SNK)
static uint32_t ManageStateDetached_SNK(uint8_t PortNum)
{
看来ST是认为这个函数只有在sink
时候才应该编译,但按照名字来看双向接口应该也有这个功能.于是修改成下面这样,编译之.
#if defined(_DRP) || defined(_SNK)
static uint32_t ManageStateDetached_SNK(uint8_t PortNum)
{
然后编译成功了.
不过至于能不能正常工作,这个就需要之后硬件完成后在测试了.