云计算百科
云计算领域专业知识百科平台

CLion + STM32环境配置,亲测有效(2025.06.19记)

CLion + STM32环境配置

遇到的问题描述:

> "moniton" command not supported by this target.
> You can't do that when your target is exec
> 上传完成,但存在问题
> monitor reset
> "monitor" command not supported by this target.

在这里插入图片描述

GitHub链接

首先感谢偶像稚晖君

稚晖君大神配置Clion的教程:https://zhuanlan.zhihu.com/p/145801160

我在进行配置的时候遇到了一些问题,以下是我的修改版本:

前面的的可以参照稚晖君大神的配置也可以不安装MinGW,直接使用STM32CubeCLT,这里面是STM官方给我们配置好的各种编译环境

可以不安装MinGW,直接使用STM32CubeCLT相对容易一些

可以不安装MinGW,直接使用STM32CubeCLT相对容易一些

可以不安装MinGW,直接使用STM32CubeCLT相对容易一些

我的环境

CLion 2024.1

OpenOCD-20240916-0.12.0

STM32CubeCLT_1.18.0

相关资源在source文件夹内

GitHub上传大文件有点麻烦,给一个迅雷的下载链接:https://pan.xunlei.com/s/VOT6D5Qu-LTGNvuMcZoCLACRA1?pwd=iykg# 复制这段内容后打开迅雷,查看更方便

在这里插入图片描述

如果网盘下载的慢的话也可以直接去对应官网下载软件

OpenOCD-20240916-0.12.0版本的下载链接:https://www.st.com/en/development-tools/stm32cubeclt.html

STM32CubeCLT_1.18.0版本的下载链接:https://sysprogs.com/getfile/2285/openocd-20240916.7z

Clion2024.1版本的下载链接:https://download.jetbrains.com/cpp/CLion-2024.1.6.exe

STM32CubeMX 下载链接:https://www.st.com/en/development-tools/stm32cubemx.html

我的配置相设置如下

编译工具链

在这里插入图片描述

Cmake

在这里插入图片描述

嵌入式开发设置

在这里插入图片描述

运行调试配置

在这里插入图片描述

编译错误问题

  • 如果移动了工程文件夹的话,最好打开.ioc文件重新Generate一下再编译,可以解决很多错误。
  • 遇到任何CMake相关的报错,一般是由于缓存没有更新引起的,可以在CLion中选Tools-CMake-Reset Cache and Reload Project即可解决。
  • printf重定向问题

    步骤一:

    在Keil里面为了使用printf函数我们需要重定向fputc函数:

    int fputc (int ch, FILE *f)
    {
    (void)HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 1000);
    return ch;
    }

    其中的FILE定义在stdio.h头文件中,所以需要在项目中包含这个头文件,但是经过测试发现,Keil里面包含的是MDK\\ARM\\ARMCC\\include这个目录下的stdio.h,而在Clion中是不会链接到这个文件的。因此如果在Clion中也按之前的方法进行重定向,会发现printf没有任何输出。

    在Clion中链接的是GNU-Tools-ARM-Embedded\\arm-none-eabi\\include里面的stdio.h,如果仍然想使用printf函数功能,则需要进行如下操作:

    新建一个retarget.h文件内容如下:

    #ifndef __RETARGET_H__
    #define __RETARGET_H__

    #include "main.h"
    #include "usart.h"
    #include <sys/stat.h>
    #include <stdio.h>

    void RetargetInit(UART_HandleTypeDef *huart);

    int _isatty(int fd);

    int _write(int fd, char *ptr, int len);

    int _close(int fd);

    int _lseek(int fd, int ptr, int dir);

    int _read(int fd, char *ptr, int len);

    int _fstat(int fd, struct stat *st);

    #endif //#ifndef _RETARGET_H__

    再新建一个retarget.c文件内容如下:

    #include <_ansi.h>
    #include <_syslist.h>
    #include <errno.h>
    #include <sys/time.h>
    #include <sys/times.h>
    #include "retarget.h"
    #include <stdint.h>

    #if !defined(OS_USE_SEMIHOSTING)

    #define STDIN_FILENO 0
    #define STDOUT_FILENO 1
    #define STDERR_FILENO 2

    UART_HandleTypeDef *gHuart;

    void RetargetInit(UART_HandleTypeDef *huart)
    {
    gHuart = huart;

    /* Disable I/O buffering for STDOUT stream, so that
    * chars are sent out as soon as they are printed. */
    setvbuf(stdout, NULL, _IONBF, 0);
    }

    int _isatty(int fd)
    {
    if (fd >= STDIN_FILENO && fd <= STDERR_FILENO)
    return 1;

    errno = EBADF;
    return 0;
    }

    int _write(int fd, char *ptr, int len)
    {
    HAL_StatusTypeDef hstatus;

    if (fd == STDOUT_FILENO || fd == STDERR_FILENO)
    {
    hstatus = HAL_UART_Transmit(gHuart, (uint8_t *) ptr, len, HAL_MAX_DELAY);
    if (hstatus == HAL_OK)
    return len;
    else
    return EIO;
    }
    errno = EBADF;
    return -1;
    }

    int _close(int fd)
    {
    if (fd >= STDIN_FILENO && fd <= STDERR_FILENO)
    return 0;

    errno = EBADF;
    return -1;
    }

    int _lseek(int fd, int ptr, int dir)
    {
    (void) fd;
    (void) ptr;
    (void) dir;

    errno = EBADF;
    return -1;
    }

    int _read(int fd, char *ptr, int len)
    {
    HAL_StatusTypeDef hstatus;

    if (fd == STDIN_FILENO)
    {
    hstatus = HAL_UART_Receive(gHuart, (uint8_t *) ptr, 1, HAL_MAX_DELAY);
    if (hstatus == HAL_OK)
    return 1;
    else
    return EIO;
    }
    errno = EBADF;
    return -1;
    }

    int _fstat(int fd, struct stat *st)
    {
    if (fd >= STDIN_FILENO && fd <= STDERR_FILENO)
    {
    st->st_mode = S_IFCHR;
    return 0;
    }

    errno = EBADF;
    return 0;
    }

    #endif //#if !defined(OS_USE_SEMIHOSTING)

    添加这两个文件到工程,更新CMake,编译之后会发现,有几个系统函数重复定义了,被重复定义的函数位于Src目录的syscalls.c文件中,我们把里面重复的几个函数删掉即可。

    如图所示:

    在这里插入图片描述

    在main函数的初始化代码中添加对头文件的引用并注册重定向的串口号:

    #include "retarget.h"

    RetargetInit(&huart1);

    然后就可以愉快地使用printf和scanf啦:

    char buf[100];

    printf("\\r\\nYour name: ");
    scanf("%s", buf);
    printf("\\r\\nHello, %s!\\r\\n", buf);

    步骤二:

    上面的修改完成之后可能会发现无法正常读取浮点数,这里修改CMakeList.txt,加入下述编译选项

    set(COMMON_FLAGS "-specs=nosys.specs -specs=nano.specs -u _printf_float -u _scanf_float")
    name: ");
    scanf("%s", buf);
    printf("\\r\\nHello, %s!\\r\\n", buf);

    步骤二:

    上面的修改完成之后可能会发现无法正常读取浮点数,这里修改CMakeList.txt,加入下述编译选项

    set(COMMON_FLAGS "-specs=nosys.specs -specs=nano.specs -u _printf_float -u _scanf_float")

    赞(0)
    未经允许不得转载:网硕互联帮助中心 » CLion + STM32环境配置,亲测有效(2025.06.19记)
    分享到: 更多 (0)

    评论 抢沙发

    评论前必须登录!