Ubuntu下使用PlatformIO開發STC89/STC12/Arduino


安裝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封裝庫, 地址為

將封裝庫添加到項目

以添加 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: 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, 根據文檔提示去下載放到這個目錄下

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/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM