从 https://code.visualstudio.com/Download 下载最新的 deb 版, 通过命令行安装
sudo apt install ./code_1.59.0-1628120042_amd64.deb
IntelliJ IDEA Keybindings: https://marketplace.visualstudio.com/items?itemName=k–kato.intellij-idea-keybindings
安装完
Ctrl
+W
选择键Ctrl
+F4
Alt
+o
在C语言中切换源文件与头文件Ctrl
+j
点击左下角的设置, 点击键盘快捷键, 在界面中设置
Ctrl
+Alt
+-
, 修改为Alt
+,
Ctrl
+Alt
++
, 修改为Alt
+.
在PIO Home里点击Platforms, 点击Embedded会列出可用的类型, 里面选择Intel MCS51, 点击Install, 这个下面会有STC系列的芯片
安装完之后会弹出提示, 实际的安装路径是 ~/.platformio/packages/ 下的toolchain-sdcc和tool-stcgal
Platform Manager: Installing intel_mcs51
Platform Manager: intel_mcs51 @ 1.2.3 has been installed!
Tool Manager: Installing platformio/toolchain-sdcc @ ~1.30804.10766
Downloading...
Tool Manager: toolchain-sdcc @ 1.30804.10766 has been installed!
Tool Manager: Installing platformio/tool-stcgal @ ~1.104.0
Tool Manager: tool-stcgal @ 1.104.0 has been installed!
The platform 'intel_mcs51' has been successfully installed!
The rest of the packages will be installed later depending on your build environment.
这里用的SDCC还是3.8.4, 比较旧. 最新的已经4.1.0了
需要增加自定义Board, 其配置文件位置为/home/milton/.platformio/platforms/intel_mcs51/boards
, 在这里可以添加自定义的Board, 例如新建stc89c516rd.json, 写入下面的内容, 就添加了对stc89c516rd的支持.
#define __CONF_COMPILE_ISP 1
设为0, 因为这个型号没有EEPROM, 所以ISP功能无效{
"build": {
"f_cpu": "11059200",
"size_iram": 256,
"size_xram": 1024,
"size_code": 62464,
"size_heap": 128,
"mcu": "stc89c516rd",
"cpu": "mcs51"
},
"frameworks": [],
"upload": {
"maximum_ram_size": 1280,
"maximum_size": 62464,
"protocol": "stcgal",
"stcgal_protocol": "stc89",
"protocols": [
"stcgal"
]
},
"name": "Generic STC89C516RD",
"url": "https://www.stcmicro.com/stc/STC89C516RD.html",
"vendor": "STC"
}
{
"build": {
"f_cpu": "11059200",
"size_iram": 256,
"size_xram": 1024,
"size_code": 57344,
"size_heap": 128,
"mcu": "stc12c5a56s2",
"cpu": "mcs51"
},
"frameworks": [],
"upload": {
"maximum_ram_size": 1280,
"maximum_size": 57344,
"protocol": "stcgal",
"stcgal_protocol": "stc12",
"protocols": [
"stcgal"
]
},
"name": "Generic STC12C5A56S2",
"url": "https://www.stcmicro.com/stc/stc12c5a32s2.html",
"vendor": "STC"
}
{
"build": {
"f_cpu": "11059200",
"size_iram": 256,
"size_xram": 1024,
"size_code": 61440,
"size_heap": 128,
"mcu": "stc12c5a60s2",
"cpu": "mcs51"
},
"frameworks": [],
"upload": {
"maximum_ram_size": 1280,
"maximum_size": 61440,
"protocol": "stcgal",
"stcgal_protocol": "stc12",
"protocols": [
"stcgal"
]
},
"name": "Generic STC12C5A60S2",
"url": "https://www.stcmicro.com/stc/stc12c5a32s2.html",
"vendor": "STC"
}
注意workspace如果不用默认的, 要自己选一下, 选择board的时候用stc关键词可以搜到STC相关的芯片.
默认的目录结构如下, 包含第三方库的结构
|--.pio
| |--build
| |--<...> 与项目同名的目录, 这里放的是编译产生的文件
|--.vscode
| |--c_cpp_properties.json 这个文件,在每次启动vscode打开项目的时候由PlatformIO更新
| |--...
|
|--include 这里放置项目的头文件
|
|--lib 这里放置其它lib, 默认情况下,lib的c文件和h文件必须在根目录或src目录下才会自动被扫描并包含和编译
| |
| |--Bar
| | |--docs
| | |--examples
| | |--src
| | |- Bar.c
| | |- Bar.h
| | |- library.json (如果c文件和h文件不在默认目录, 就需要这个文件) https://docs.platformio.org/page/librarymanager/config.html
| |
| |--Foo
| | |- Foo.c
| | |- Foo.h
|
|- platformio.ini 这个是项目的关键文件, 用来设置platform,board,build_flags等信息
|
|--src 这里放置项目的c文件
|- main.c
其中要注意的几点:
这些项目对应的可以添加HML_Fwib_STCxx封装库, 地址为
以添加 HML_FwLib_STC89 库为例, 有两种方式
方式一
简单的方法, 就是直接把头文件和源文件都合到src目录下, 在lib/HML_FWLib_STC89目录里, 将inc下的hml目录移动到项目src 目录下, 这样编译时就会自动编译下面的c文件.
方式二
将项目git clone到lib目录下, 在lib/HML_FWLib_STC89下建立library.json, 写入以下内容
{
"name": "hml_fwlib_stc89",
"version": "0.0.0",
"build": {
"srcDir": "src",
"includeDir": "inc"
}
}
这样也能让PIO识别这个库的头文件和源文件
因为 HML_FWLib_STC89 这个库编译时需要额外的参数, 可以通过配置 platformio.ini 添加. 增加build_flags. 因为没有Makefile, 所以与封装库文档中的flag不同, 这里的flag名称是最终作用到编译参数上的名称, 这样编译就不会有warning了.
[env:stc89c52rc]
platform = intel_mcs51
board = stc89c52rc
build_flags =
-D__CONF_FRE_CLKIN=11059200
-D__CONF_MCU_PRESCALER=12
-D__CONF_MCU_MODEL=MCU_MODEL_STC89C52RC
board设置的默认upload方式为stcgal, 默认的波特率是 19200, 如果想让烧录速度快一点, 可以添加upload参数
upload_speed = 115200
upload_flags =
-b$UPLOAD_SPEED
__sfr
默认配置下, 编辑器不能识别SDCC MCS51的__sfr关键字, 会提示错误, 这时候可以通过在main.c添加lint.h头文件解决
#include "lint.h"
参考 https://github.com/microsoft/vscode-cpptools/issues/2499
正常输出
Processing stc12c5a56s2 (platform: intel_mcs51; board: stc12c5a56s2)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/intel_mcs51/stc12c5a56s2.html
PLATFORM: Intel MCS-51 (8051) (1.2.3) > Generic STC12C5A56S2
HARDWARE: STC12C5A56S2 11MHz, 1.25KB RAM, 56KB Flash
PACKAGES:
- tool-stcgal 1.104.0 (1.4)
- toolchain-sdcc 1.30804.10766 (3.8.4)
LDF: Library Dependency Finder -> http://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 1 compatible libraries
Scanning dependencies...
Dependency Graph
|-- <hml_fwlib_stc11> 0.0.0
Building in release mode
Checking size .pio/build/stc12c5a56s2/firmware.hex
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
Flash: [ ] 4.6% (used 2655 bytes from 57344 bytes)
将USB2TTL连接好后, 点击底部状态栏的 右箭头 图标, 就会开始烧录, 同样需要手动操作开关的过程. 输出如下(这个是默认的19200波特率配置)
Configuring upload protocol...
AVAILABLE: stcgal
CURRENT: upload_protocol = stcgal
Looking for upload port...
Auto-detected: /dev/ttyUSB0
Uploading .pio/build/stc12c5a56s2/firmware.hex
Cycling power: done # 到这一步断电/加电
Waiting for MCU: done
Target model:
Name: STC12C5A56S2
Magic: D17C
Code flash: 56.0 KB
EEPROM flash: 6.0 KB
Target frequency: 11.035 MHz
Target BSL version: 6.6I
Target options:
reset_pin_enabled=True
low_voltage_reset=False
oscillator_stable_delay=32768
por_reset_delay=long
clock_gain=high
clock_source=external
watchdog_por_enabled=False
watchdog_stop_idle=True
watchdog_prescale=256
eeprom_erase_enabled=False
bsl_pindetect_enabled=False
Loading flash: 2655 bytes (Intel HEX)
Switching to 19200 baud: testing setting done
Erasing 12 blocks: done
Writing 3072 bytes: ........................ done
Finishing write: done
Setting options: done
Target UID: 000300E155111B
Disconnected!
========================================== [SUCCESS] Took 23.10 seconds ================
中间如果有提示
Warning! Please install `99-platformio-udev.rules`.
More details: https://docs.platformio.org/page/faq.html#platformio-udev-rules
说明没有在/etc/udev/rules.d/下放置这个rule, 根据文档提示去下载放到这个目录下
STC12C5A60S2系列的MCU, 对应的封装库为 https://github.com/IOsetting/HML_FwLib_STC12 ,
项目中已经带了library.json, 可以直接集成至PlatformIO项目. 其他的方面和STC89-STC11是一样的
新建项目, 选择项目目录, 在board里搜索nano 328 new bootloader
, 点击创建后, 会自动下载需要的插件
这个头文件实际上在~/.platformio/packages/toolchain-atmelavr/avr/include/
下面, 此时编译是可以正常编译的, 只是在VSCode里展示会提示错误. 要解决这个错误提示, 需要加到platformio.ini里面, 加上lib_deps
如下, 然后在项目文件浏览下的.pio/libdeps中就会出现这些头文件.
[env:nanoatmega328new]
platform = atmelavr
board = nanoatmega328new
framework = arduino
lib_deps =
/home/milton/.platformio/packages/toolchain-atmelavr/avr/include/