编译时找不到头文件问题
直接引入项目子目录头文件,总提示找不到文件
rc/main.c:5:10: fatal error: test.h: No such file or directory
在CMakeLists.txt里已经添加过include_directories,IDE没有报错,可以正确引用到,编译时就是找不到文件
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include/test)
查看终端执行命令
/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake --build /Users/shaun/CLionProjects/stmf103rc/cmake-build-genericstm32f103rc --target Production
[1/1] cd /Users/shaun/CLionProjects/stmf103rc && platformio -c clion run -egenericSTM32F103RC
FAILED: CMakeFiles/Production /Users/shaun/CLionProjects/stmf103rc/cmake-build-genericstm32f103rc/CMakeFiles/Production ....
调用cmake后,首先是执行platformio命令,会不会是platformio通过工具链执行编译时,没有把子目录include进去
通过查看platformio配置文档 https://docs.platformio.org/en/stable/projectconf/section_platformio.html#include-dir
默认只有include路径,添加在platformio.in里需要添加参数build_flags
[env:genericSTM32F103RC]
platform = ststm32
board = genericSTM32F103RC
framework = stm32cube
debug_tool = jlink
upload_protocol = jlink
build_flags = -I /path/to/dir
extra_scripts = post:extra_script.py
重复引用头文件问题
构建时报如下错误
error: unknown type name 'HAL_StatusTypeDef'; did you mean 'FLASH_TypeDef'
参考:https://electronics.stackexchange.com/questions/443018/cube-ide-error-unknown-type-name-hal-statustypedef
原因是代码里重复引用了头文件,删掉一个就可以,比如下面的hal_tim.h
#include <stm32f1xx_hal.h>
#include <stm32f1xx_hal_tim.h>
HAL_Delay事件不对问题
函数没响应
- 检查是否调用HAL_Init()函数
- 检查是否实现void SysTick_Handler(void)函数
int main()
{
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
}
void SysTick_Handler(void)
{
HAL_IncTick();
}
计时不对
HAL_Delay(1000)预期1秒,结果变成了2秒,没有正确配置时钟树参数
从STM32CubeMX配置好时钟树后,生成代码,将main.c里的SystemClock_Config()函数复制出来,在main()函数里调用即可
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
}