Post

JetBrains IDE Setup

整理 JetBrains IDE 的运行环境变量、代理设置,以及 CLion 配合 PlatformIO 开发 51 单片机的配置

JetBrains IDE Setup

通用设置

运行与调试环境变量

CLion 和 PyCharm 的环境变量入口相同:进入运行 -> 编辑配置,在对应运行配置的环境变量中添加项目所需变量。动态库路径、代理等仅对当前运行配置生效的内容,统一在此处维护

代理设置

在运行配置的环境变量中添加:

1
2
http_proxy=http://127.0.0.1:7897
https_proxy=http://127.0.0.1:7897

代理环境变量配置

CLion

51 单片机开发

安装插件

在 CLion 插件市场安装 PlatformIO 插件
通过https://raw.githubusercontent.com/platformio/platformio-core-installer/master/get-platformio.py 下载get-platformio.py文件后使用 Python 运行。随后在设置->语言和框架中设置 PlatformIO 的路径
Windows 和 Linux 稍有不同
Windows 在C:/Users/UserName/.platformio/penv/Scripts/platformio
Linux 在~/.platformio/penv/bin/platformio

官网链接:https://docs.platformio.org/en/latest/core/installation/methods/index.html

配置 Shell 命令

  • Windows 在系统环境变量 Path 附加 C:\Users\UserName.platformio\penv\Scripts
  • Linux 可通过在~/.profile 中添加
1
export PATH=$PATH:$HOME/.local/bin

然后

1
2
3
4
mkdir -p ~/.local/bin
ln -s ~/.platformio/penv/bin/platformio ~/.local/bin/platformio
ln -s ~/.platformio/penv/bin/pio ~/.local/bin/pio
ln -s ~/.platformio/penv/bin/piodebuggdb ~/.local/bin/piodebuggdb

或者直接

1
2
3
4
sudo mkdir -p /usr/local/bin
sudo ln -s ~/.platformio/penv/bin/platformio /usr/local/bin/platformio
sudo ln -s ~/.platformio/penv/bin/pio /usr/local/bin/pio
sudo ln -s ~/.platformio/penv/bin/piodebuggdb /usr/local/bin/piodebuggdb

官网链接:https://docs.platformio.org/en/latest/core/installation/shell-commands.html#piocore-install-shell-commands

编写程序

  • 选择芯片

新建工程时选择芯片型号
以STC89C52为例
待环境构建完成后(第一次需下载文件有点慢),PlatformIO 会在工程根目录生成CMakeListsPrivate.txt,其中包含了宏定义和头文件路径 PlatformIO 生成的 CMakeListsPrivate.txt
其中没有 SDCC 自带头文件的路径,CLion 无法识别8052.h等头文件。CMakeListsPrivate.txt由 PlatformIO 自动生成,重新初始化工程时会被覆盖,不要直接修改;在工程根目录新建CMakeListsUser.txt,该文件会被CMakeLists.txt自动引入

Windows 添加

1
2
include_directories("${ENV_HOME_PATH}/.platformio/packages/toolchain-sdcc/include")
include_directories("${ENV_HOME_PATH}/.platformio/packages/toolchain-sdcc/non-free/include")

Linux 添加

1
2
include_directories("$ENV{HOME}/.platformio/packages/toolchain-sdcc/share/sdcc/include")
include_directories("$ENV{HOME}/.platformio/packages/toolchain-sdcc/share/sdcc/non-free/include")

跳转到8052.h头文件中,将#include <8051.h>改为#include "8051.h"并添加#include "lint.h"
修改后的 8052.h
跳转到8051.h头文件中,在开头添加上#include "lint.h"
修改后的 8051.h
然后可正常编写程序。这两个头文件位于 PlatformIO 的工具链目录中,工具链更新后修改会被还原,需要重新修改
注意,位定义和中断与 Keil 不同!

1
2
3
4
5
6
7
8
#include <8052.h>

#define LED P1_0 // Keil中写作 sbit LED = P1^0;

void timer0_isr(void) __interrupt(1) // Keil中写作 void timer0_isr(void) interrupt 1
{
    LED = !LED;
}

烧录程序

PlatformIO 原生的烧录很慢,改为使用 stcgal 烧录
首先,安装 stcgal。新版系统的 Python 不允许直接用 pip 安装到系统环境,这里安装到 PlatformIO 自带的 Python 环境中,并链接到~/.local/bin

1
2
~/.platformio/penv/bin/pip install stcgal
ln -s ~/.platformio/penv/bin/stcgal ~/.local/bin/stcgal

然后在 platformio.ini 添加

1
2
3
4
5
6
upload_flags =
    -P
    stc89a
    -p
    $UPLOAD_PORT
upload_command = stcgal $UPLOAD_FLAGS $SOURCE

即可使用 stcgal 烧录程序

-P后面的参数为芯片使用的协议,需根据芯片型号选择,stcgal 支持的协议如下

1
stc89  stc89a  stc12a  stc12b  stc12  stc15a  stc15  stc8  stc8d  stc8g  usb15

不确定时可以使用-P auto自动检测

This post is licensed under CC BY 4.0 by the author.