欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

Ubuntu下使用PlatformIO开发STC89/STC12/Arduino

发布时间:2024/3/24 62 豆豆
生活随笔 收集整理的这篇文章主要介绍了 Ubuntu下使用PlatformIO开发STC89/STC12/Arduino 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

内容目录

  • 安装VSCode
  • 快捷键
    • 安装Idea键位插件
    • 修改快捷键
  • 安装PlatformIO
  • MCS51/STC51项目
    • 准备环境
    • 增加对STC其他型号的支持
      • STC89C516RD+
      • STC12C5A56S2
      • STC12C5A60S2
    • 新建项目
      • 项目目录结构
    • STC89/STC90/STC10/STC11项目
      • 将封装库添加到项目
      • 添加编译参数
      • 添加烧录参数
      • 使编辑器支持 `__sfr`
      • 编译
      • 烧录
    • STC12项目
  • Arduino项目
    • 提示Arduino.h找不到avr/pgmspace.h

安装VSCode

从 https://code.visualstudio.com/Download 下载最新的 deb 版, 通过命令行安装

sudo apt install ./code_1.59.0-1628120042_amd64.deb

快捷键

安装Idea键位插件

IntelliJ IDEA Keybindings: https://marketplace.visualstudio.com/items?itemName=k–kato.intellij-idea-keybindings
安装完

  • 可以使用IDEA风格的Ctrl+W选择键
  • 关闭标签页是Ctrl+F4
  • 使用Alt+o在C语言中切换源文件与头文件
  • 开关下方的消息栏Ctrl+j

修改快捷键

点击左下角的设置, 点击键盘快捷键, 在界面中设置

  • 前进 Go Back: 默认 Ctrl+Alt+-, 修改为Alt+,
  • 后退 Go Forward: 默认为 Ctrl+Alt++, 修改为Alt+.

安装PlatformIO

  • 直接在VSCode的插件中查找PlatformIO并安装,
  • 安装完成后, 还需要等待PlatformIO安装完PlatformIO Core,
  • Core安装完之后, 在底部状态栏就会出现一个Home图标
  • 点击图标会打开PIO Home,
    • 这时候左侧会出现PIO自己的菜单, 里面点Platforms
    • 会列出Installed, Embedded, Desktop, Frameworkds等

MCS51/STC51项目

准备环境

在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了

增加对STC其他型号的支持

需要增加自定义Board, 其配置文件位置为/home/milton/.platformio/platforms/intel_mcs51/boards, 在这里可以添加自定义的Board, 例如新建stc89c516rd.json, 写入下面的内容, 就添加了对stc89c516rd的支持.

STC89C516RD+

  • Flash: 61K
  • RAM: 256 + 1024
  • 与 HML_FwLib_STC89 一起使用时要将 config.h 中的 #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" }

STC12C5A56S2

  • Flash: 56K
  • RAM: 256 + 1024
{"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" }

STC12C5A60S2

  • Flash: 60K
  • RAM: 256 + 1024
{"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

其中要注意的几点:

  • c_cpp_properties.json 并不会随着platformio.ini的修改而更新, 而是每次在启动vscode,在打开PIO的Home界面时更新, 如果误删或误改此文件, 需要重新打开VSCode
    • 这个文件只会影响到界面上的渲染, 包括错误提示, 和编译无关. 可能这里报红但是编译OK, 也可能这里OK但是编译出错
  • 编译只与platformio.ini和lib//library.json有关, 这个修改完立即生效
  • 如果lib/这个项目, 其源文件和头文件都在根目录或者都在src目录下, 那么PIO会自动扫描并自动组织源文件编译
  • 如果不是上述的情况, 就要在lib/下编写一个library.json文件, 用来告诉PIO这个项目的源文件和头文件都在哪里, 这样PIO才会组织源文件编译

STC89/STC90/STC10/STC11项目

这些项目对应的可以添加HML_Fwib_STCxx封装库, 地址为

  • https://github.com/MCU-ZHISHAN-IoT/HML_FwLib_STC89
  • https://github.com/MCU-ZHISHAN-IoT/HML_FwLib_STC90
  • https://github.com/MCU-ZHISHAN-IoT/HML_FwLib_STC10
  • https://github.com/MCU-ZHISHAN-IoT/HML_FwLib_STC11

将封装库添加到项目

以添加 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

编译

  • 可以点击底部状态栏的勾号, 这个是编译
  • 也可以通过点左侧的PlatformIO图标, 然后点Build
  • 编译的时候会区分env, 这个要注意
  • 如果想看到详细的编译输出, 点开Advanced, 点击Verbose Build

正常输出

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: STC12C5A56S2Magic: D17CCode flash: 56.0 KBEEPROM flash: 6.0 KB Target frequency: 11.035 MHz Target BSL version: 6.6I Target options:reset_pin_enabled=Truelow_voltage_reset=Falseoscillator_stable_delay=32768por_reset_delay=longclock_gain=highclock_source=externalwatchdog_por_enabled=Falsewatchdog_stop_idle=Truewatchdog_prescale=256eeprom_erase_enabled=Falsebsl_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, 根据文档提示去下载放到这个目录下

STC12项目

STC12C5A60S2系列的MCU, 对应的封装库为 https://github.com/IOsetting/HML_FwLib_STC12 ,
项目中已经带了library.json, 可以直接集成至PlatformIO项目. 其他的方面和STC89-STC11是一样的

Arduino项目

新建项目, 选择项目目录, 在board里搜索nano 328 new bootloader, 点击创建后, 会自动下载需要的插件

提示Arduino.h找不到avr/pgmspace.h

这个头文件实际上在~/.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/

总结

以上是生活随笔为你收集整理的Ubuntu下使用PlatformIO开发STC89/STC12/Arduino的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。