arduino 原始碼讀書會(iii) : arduino ide 解析 · pdf file在 \hardware...

Post on 13-Feb-2018

262 Views

Category:

Documents

20 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Arduino 原始碼讀書會(III) :

Arduino IDE 解析

講者介紹

王騰毅

瞻營全 Intern

元智大學電機工程學系

86Duino

第三方函式庫移植

講者介紹

郭宏澤

瞻營全 RD

◦ 等級1

◦ 打怪練級中

技能: 86Duino 未來 軟體維護與擴展

講者介紹

林恩州

瞻營全 RD

◦ 等級3

◦ 目前與郭宏澤一起 組隊打怪

技能: 86Duino 基礎 軟體開發

7:00 ~ 7:50 讀書會分享 I

7:50 ~ 8:00 問題討論&休息

8:00 ~ 8:50 讀書會分享 II

8:50 ~ 9:00 問題討論&結束

進行流程

Arduino IDE 的編譯

取得 Arduino Source Code

第一種方式:

◦ 使用 git 軟體

下載: http://git-scm.com/downloads

◦ 指令: git clone https://github.com/arduino/Arduino.git

取得 Arduino Source Code

第二種方式:

◦ 直接下載 source code

點選這裡可直接下載

● Arduino IDE Source Code https://github.com/arduino/Arduino

● Cygwin http://cygwin.com/install.html

● Sun Java JDK(Java SE Development Kit)

http://www.oracle.com/technetwork/java/javase/downloads/index.html

● Apache Ant Binary Distributions

http://ant.apache.org/bindownload.cgi

● git

http://windows.github.com/

Windows-下載相關應用程式

Setup steps(1)

安裝 Cygwin

安裝 Java JDK

解壓縮apache-ant-x.x.x-bin.zip至ProgrmFiles

Setup steps(2)

控制台\所有控制台項目\系統\進階系統設定\

進階\環境變數

Setup steps(3)

在使用者變數裡新增

變數名稱 變數值

ANT_HOME C:\Program Files\apache-ant-1.9.2 (apache ant資料夾路徑)

JAVA_HOME C:\Program Files\Java\jdk1.7.0_45 (Java JDK資料夾路徑)

Setup steps(4)

在系統變數中

◦ 將apache-ant中的bin資料夾路徑加入Path變數值中

Setup steps(5)

開啟cmd,移至Arduino底下的build

>ant

>ant run

Setup steps(6)

將…\build\windows中的jre.zip裡面的java

資料夾解壓縮到…\build\windows\work

Linux(Ubuntu)所需的應用程式

● Sun Java JDK $sudo add-apt-repository ppa:webupd8team/java

$sudo apt-get update $sudo apt-get install oracle-java8-installer

● Apache Ant $ sudo apt-get install ant

● avr-gcc, avr-g++, avr-libc $ sudo apt-get install arduino

● Make $ sudo apt-get install make

● (git) $ sudo apt-get install git

Setup steps

● $ git clone git://github.com/arduino/Arduino.git

● $ cd ./Arduino/build/

● $ ant

● $ ant run

Mac OSX所需要的應用程式 ● Java for OSX

http://support.apple.com/kb/dl1572

● (git)

– $ sudo port selfupdate

– $ sudo port install git-core

Setup steps

● $ git clone git://github.com/arduino/Arduino.git

● $ cd ./Arduino/build/

● $ ant

● $ ant run

開胃小菜:

Arduino IDE Hacking Tips

讓我們再次回到熟悉的地方

https://github.com/arduino/Arduino

切換到 1.5.x 分支

1.5.x 支援 Arduino Due, Yun, …

Arduino 原始碼根目錄

改過的 Processing IDE 原始碼 (Arduino 相關)

Processing IDE 原始碼

存放編譯結果的目錄

Arduino Bootloader, Standard API 原始碼

Arduino Library 原始碼

App →src →processing →app

Arduino 編譯、燒錄相關程式碼

前置處理的相關程式碼, 例如: 字串轉換…

Arduino IDE 選單、按鈕功能程式碼

處理Sketch.ino 相關程式碼

App →src →processing →app →preproc

處理字串問題程式碼

App→src →processing→app→ debug

Arduino 編譯相關程式碼

語言設定

如何客製化呢?

App →src →processing →app

語言設定檔的位置

中英說明文件 (修改後沒有效果)

英轉中 編碼轉換設定檔

修改中文顯示訊息

Resources_zh_tw.po Resources_zh_tw.properties

英文字串 Open… 替換成 開啟… (unicode 編碼)

修改範例

修改後重新編譯 IDE 即可看到結果

中文: 我愛Fablab (unicode)

在下拉式選單新增選項

新增 Burn Bootloader 子選單

新增 Arduino Bootloader 項目

選項動作 callback

修改 Editor.java 的 buildToolsMenu( )

Example:

新增 86Duino Bootloader 項目

修改結果:

在 \hardware 下新增資料夾。 Ex: 86Duino\x86

資料夾底下需要這幾個資料夾和檔案

以上這些檔案不需重新編譯 Arduino IDE,只需新增檔案並且重新開啟 Arduino IDE 即可看到效果。

添加 Arduino 相容板

bootloaders

libraries

編譯、上傳參數設定

standardAPI

Arduino 相容板子的各種參數

添加 Arduino 相容板

boards.txt platform.txt

設定板子名稱

設定板子相關參數

設定板子選單名稱

添加 Arduino 相容板

尚未添加板子前

boards 選單

添加後的 boards 選單

存放 UI 外觀圖檔及設定檔的路徑:\build\shared\lib\theme

修改 UI 外觀顯示

UI 設定檔

theme.txt 文件內容

更換 IDE 的啟動 logo

直接修改或更換此檔案,並且重新編譯Arduino IDE,編譯完成即可看到更改後圖案。

修改 IDE 顯示版本號

修改此字串並重新編譯, 即可更換 IDE 顯示的版本號

build.xml 內容

修改 IDE 顯示版本號

修改的版本號:~1.5.4^^

修改 IDE 視窗左上角小圖示

打開 Papplet.java 檔

修改 IDE 視窗左上角小圖示

把 ICON_IMAGE 陣列內容換成想換的小圖示, 格式必須為 GIF

修改 IDE 視窗左上角小圖示

IDE 視窗 Serial monitor 視窗

修改結果:

修改後重新編譯 IDE 即可看到效果

第一道主菜:

Sketch 編譯機制

Arduino 編譯流程

IDE 視窗 按下編譯按鈕主要做了三個動作:

1. 產生暫存資料夾

(Sketch.java)

2. 前處理 Sketck.ino 檔

(PdePreprocessor.java)

3. 編譯 Sketck.ino 檔

(Compiler.java)

App →src →processing →app

Arduino 編譯、燒錄相關程式碼

處理字串問題相關程式碼

Arduino IDE 選單、按鈕功能程式碼

處理Sketch.ino 相關程式碼

產生存放編譯結果的暫存資料夾

處理 Sketch.ino檔 並開始編譯

Editor.java – DefaultPresentHandler( )

App →src →processing →app

Arduino 編譯、燒錄相關程式碼

處理字串問題相關程式碼

Arduino IDE 選單、按鈕功能程式碼

處理Sketch.ino 相關程式碼

前處理 Sketch.ino檔案

找出 .ino 和 .pde 檔

在 .ino/.pde 檔中加入檔頭修正

(請參考第 1 次讀書會內容)

Sketch.java - preprocess( )

修改Sketch.ino 檔案

App →src →processing →app →preproc

Sketch 前處理的程式碼

前處理 Sketch.ino檔案 PdePreprocessor.java – writeProgram( )

在 .ino/.pde 檔中加入

#include “Arduino.h ”

加入函式原型宣告

加入行號修正

原始 skecth

被 IDE 改寫的 skecth

Sketch 處理前後之差異

找出 sketch 內呼叫的 libraries

找出 include 的 library

PdePreprocessor.java – writePrefix( )

App→src →processing→app→ debug

Arduino 編譯相關程式碼

Sketch 編譯流程重點講解

取得暫存資料夾路徑

取得libraries路徑

Compiler.java - compile( )

Sketch 編譯流程重點講解

編譯Sketch.ino

編譯libraries

編譯standardAPI

產生.elf檔

產生.eep檔

產生.hex 執行檔 並結束編譯

Sketch 編譯流程重點講解

Compiler.java - compileSketch( )

呼叫 CompileFiles 執行 sketch 編譯

Sketch 編譯流程重點講解

Compiler.java - compileLibraries( )

讀取所有被 include 的 libraries

在 tmp 建立 library 資料夾

在 library 資料夾 底下建立 utility 資料夾

編譯 library

編譯 library 底下 utility

Sketch 編譯流程重點講解 Compiler.java - compileCore( )

讀取 standaAPI 路徑

讀取 variant 資料夾路徑

編譯 standaAPI

編譯 variant 資料夾下的檔案

取得 .a 檔編譯 pattern

編譯 .a 檔

加入編譯 .a 檔參數

產生編譯.s檔案的 gcc 命令 執行avrgcc

Compiler.java - compileFiles( )

Sketch 編譯流程重點講解

Sketch 編譯流程重點講解

產生編譯 .c檔案的 gcc 命令

產生編譯 .cpp檔案的 gcc 命令

執行 avrgcc

執行 avrgcc

Sketch 編譯流程重點講解

將編譯結果顯示到 IDE 訊息框內

開新的 process 執行avrgcc 命令

Compiler.java - execAsynchronously( )

配菜:

加入 86Duino 程式編譯

對 IDE 所做的修改

86Duino 編譯系統

DOSBox + DJGPP

DOSBox 是一個跨平台的 DOS 模擬軟體

◦ 在 IDE 的路徑: \build\windows\work\DOSBox-0.74

DJGPP 是一個可在 DOS 下編譯程式的 GNU

gcc

◦ 在 IDE 的路徑: \build\windows\work\DJGPP

86Duino 編譯流程

86Duino原始碼重要細節

如果使用者選的板子是 86Duino, 則跳到

duinocompiler.java 編譯

Compiler.java - compile( )

86Duino原始碼重要細節

設定所需程式的路徑參數

DuinoCompiler.java - compile( )

86Duino原始碼重要細節

Makefile 檔案路徑、編譯出檔案路徑設定

DuinoCompiler.java - compile( )

86Duino原始碼重要細節

Dosbox config設定

Makefile 設定

設定 DOSBox 執行命令

將編譯訊息寫到MESSAGE.TXT

DuinoCompiler.java - compile( )

86Duino原始碼重要細節

Dosbox參數設定

Dosbox執行編譯參數設定

DuinoCompiler.java - writeDosboxconf( )

86Duino原始碼重要細節

設定編譯命令參數

將之前Makefile設定讀取進來並編譯

DuinoCompiler.java - writeMakefile( )

第二道主菜:

Arduino 程式的燒錄機制

實作燒錄的 IDE 原始碼:

app→src → processing → app → debug

本次解析目標

處理 Arduino 程式燒錄動作

處理 Arduino 程式燒錄動作

Arduino 程式燒錄設定檔

hardware→arduino→avr

分別簡要介紹

定義 boards 選單、編譯及燒錄參數

定義編譯及燒錄參數

本次解析目標

燒錄設定檔的用途

Arduino IDE 在燒錄程式之前, 會從燒錄設定檔讀取與板子相關的燒錄參數

這些設定檔中, 使用一種特定的格式, 記錄了每塊 Arduino 板子的差異, 例如:

◦ CPU時脈、燒錄的 protocol、燒錄檔案的最大 size

等等

只要推出一片新的板子, 依照指定的格式加入新參數, 就可以直接套用到目前的 IDE, 不需要重新編譯程式

boards.txt 格式: 以 Leonardo 為例

要在 Boards 選單上顯示的名稱

燒錄工具程式檔名

燒錄用的 protocol 允許燒錄的 binary 最大 size

燒錄用的 baudrate

板子名稱 燒錄 sketch 的參數設定

燒錄 bootloader 的參數設定

設定燒錄時 IDE 清空 serial data 值

設定燒錄時用 1200 baudrate 來 reset Arduino

設定 reset Arduino 後要等待 upload port 出現才可進行燒錄

這次讀書會不講燒錄

bootloader 部分

boards.txt 格式: 以 Leonardo 為例

板子名稱

編譯 sketch 的相關參數 Leonardo 的 CPU 型號(燒錄會用到的參數)

platform.txt 的格式: 以 Leonardo 為例

在 linux 下, 燒錄工具程式的位置以及燒錄 config 檔的位置

在 windows 和 Mac 下, 燒錄工具程式的位置以及燒錄 config 檔的位置

platform.txt 格式: 以 Leonardo 為例

IDE 燒錄程式時下 的命令列參數

燒錄過程中會被替換成正確參數

實際燒錄一次, 看看輸出訊息

燒錄程式 Avrdude config 檔

Leonardo CPU COM port 燒錄 baudrate

要燒錄的檔案

protocol

IDE 執行燒錄的機制

執行 avrdude 燒錄命令

取得 sketch binary 檔路徑

Reset Arduino

使用 serial bootloader 的板子, 例如: UNO …

使用 usb bootloader 的板子, 例如: Leonardo

詳細流程請參考第 2 次 Arduino 原始碼讀書會內容

設定正確的板子燒錄參數

BasicUploader.java 是否需要 reset

Arduino board

Uploader.java

等待 upload

port 出現

燒錄工具程式 Avrdude 簡介

普遍用來燒錄 Atmel AVR 的工具程式

跨多種平台, Windows, FreeBSD, linux, UNIX …

使用命令列來完成燒錄動作

參考資料

◦ 馬大的 Avrdude GUI 教學

http://www.coopermaa2nd.blogspot.tw/2011_06_01_archive.h

tml

◦ 詳細的 avrdude 命令, 可見 AVR Tutorial:

http://www.ladyada.net/learn/avr/avrdude.html

◦ Avrdude 原始碼

https://github.com/arduino/avrdude

Avrdude 程式放在哪?

Windows/Mac : hardware\tools\avr\bin

Avrdude 程式放在哪?

Linux : hardware\tools

Arduino 燒錄機制原始碼重點講解

BasicUploader.java → Class BasicUploader

→ uploadUsingPreferences()

取得目前 Serial USB port

對 upload port 設定 1200 baud 再關閉 (soft-reset Arduino)

如果設定要等待 upload port 出現

如果設定 1200 baudrate reset → 執行 USB bootloader 燒錄流程

取得 user 設定的 upload port

等待 Arduino reset 完畢, 重新 取得 upload port (見下頁)

BasicUploader.java → Class BasicUploader

→ waitForUploadPort()

Timeout 時間: 20 秒

找出 Arduino reset 後, 重新連線的 upload port

如果找到 upload port

如果沒有找到 upload port, delay 250ms 再重新尋找

如果超過時限未找到新 upload port (win: 10 秒, 其他: 500ms), 且 user 選擇的 upload port 並未消失, 則回傳 user 選擇的 upload port

將找到的 upload port 回傳

回到 BasicUploader.java → Class

BasicUploader → uploadUsingPreferences()

取得燒錄程式用的命令列pattern

設定正確的命令列參數

執行 avrdude 燒錄工具程式(見下頁)

Uploader.java → Class Uploader→

executeUploadCommand ()

開新的 process 執行燒錄命令

將編譯結果顯示到 IDE 訊息框內

等待燒錄 process 執行完畢

檢查燒錄是否燒錄成功

回到 BasicUploader.java → Class

BasicUploader → uploadUsingPreferences()

如果設定要等待 upload port 出現

檢查 upload

port 是否出現,

timeout = 2s

如果找到 upload port

將 upload port 設定回 9600 baudrate

點心:

加入 86Duino 程式燒錄

對 IDE 所做的修改

加入 86Duino 板子的資料夾結構

建立 boards.txt

建立 platform.txt

修改 BasicUploader.java

加入板子判斷

根據 86Duino 修改燒錄參數

將燒錄工具程式放到 IDE 指定資料夾下

燒錄工具程式必須依不同的平台而放在不同的目錄(下頁說明)

在 build IDE source code 的過程, 燒錄工具程式會自動被移到 IDE 規定的執行時期位置 (

詳見 build.xml 中的設定)

Case 1: Windows XP/7/8

將 window 版的 86Duino 燒錄工具壓縮至 avr_tool.zip 裡面

Case 2: Mac OS X

將 MAC 版的 86Duino 燒錄工具壓縮至 avr_tool.zip 裡面

Case 3: Linux

將 Linux 版的 86Duino 燒錄工具複製到此資料夾下

燒錄工具程式的一點開發經驗

在 ubuntu 遇到的問題

◦ 在 ubuntu 11.04 版本及之後的版本, 內建的 modem

manager 會干擾 USB CDC 裝置的傳輸

◦ https://bugs.launchpad.net/ubuntu/+source/modemmanager/+bug

/1153632/+activity

◦ 這會造成燒錄程序被干擾而失敗

◦ 解決方式: 將 USB CDC 裝置的 PID 和 VID 加入

modem manager 的忽略清單

燒錄工具程式的一點開發經驗

在 Mac OS X 遇到的問題 ◦ USB CDC 裝置的 Call Management Functional Descriptor 中的

最後一個 data 必須為 0x01, 否則 Mac 會認不到 USB CDC 裝置

◦ http://stackoverflow.com/questions/5009593/acessing-

a-serial-to-usb-device-with-i-o-kit

燒錄工具程式的一點開發經驗

在 Mac OS X 遇到的問題 (cont.)

◦ USB CDC 裝置的 Configuration Descriptor 中不可宣告 remote wakeup 功能, 否則會大大延長 Mac 辨識此 USB CDC 裝置的時間

Thank You 感謝大家來參與 Arduino 原始碼讀書會

接下來問題討論時間

top related