作業系統與硬體元件的驅動軟體開發法則 (operating systems and software design...

25
Note: The Copyrights of the referenced materials and photos go to its original authors. As a result, this slide is for internal reference only. For the contents created in this document, the Copyright belongs to William W.-Y. Liang. © 2005-2015 All Rights Reserved. Note: The Copyrights of the referenced materials and photos go to their original authors. As a result, this slide is for non-commercial reference only. For the contents created in this document, the Copyright belongs to William W.-Y. Liang. © 2005-2015 All Rights Reserved. 作業系統與硬體元件的驅動軟體開發法則 Operating Systems and Software Design Principles for Hardware Components William W.-Y. Liang (梁文耀), Ph.D. http: //www.ntut.edu.tw/~wyliang

Upload: william-liang

Post on 08-Jan-2017

3.748 views

Category:

Software


0 download

TRANSCRIPT

Page 1: 作業系統與硬體元件的驅動軟體開發法則 (Operating Systems and Software Design Principles for Hardware Components)

Note: The Copyrights of the referenced materials and photos go to its original authors. As a result, this slide is for internal reference only.

For the contents created in this document, the Copyright belongs to William W.-Y. Liang. © 2005-2015 All Rights Reserved.

Note: The Copyrights of the referenced materials and photos go to their original authors. As a result, this slide is for non-commercial reference only.

For the contents created in this document, the Copyright belongs to William W.-Y. Liang. © 2005-2015 All Rights Reserved.

作業系統與硬體元件的驅動軟體開發法則Operating Systems and Software Design Principles for Hardware Components

William W.-Y. Liang (梁文耀), Ph.D.http: //www.ntut.edu.tw/~wyliang

Page 2: 作業系統與硬體元件的驅動軟體開發法則 (Operating Systems and Software Design Principles for Hardware Components)

© 2015 William W.-Y. Liang, All Rights Reserved.

大綱

作業系統與驅動軟體架構

即時作業系統 (RTOS) 與一般用途作業系統

驅動程式與 RTOS, Linux, 及 Android硬體元件的驅動軟體開發

硬體元件與系統軟體銜接的準備工作

驅動程式於作業系統中所扮演的角色

驅動程式介面定義與實現方法

驅動程式的開源與閉源考量

開放與貢獻源碼的優點

如何兼顧開源標準與閉源優化

開源與閉源分界的設計原則

授權議題的處理原則與開源釋出準則2 OS vs. Hardware Components

Page 3: 作業系統與硬體元件的驅動軟體開發法則 (Operating Systems and Software Design Principles for Hardware Components)

© 2015 William W.-Y. Liang, All Rights Reserved.

嵌入式系統組成

HardwareSoftware development environmentTool chain and LibraryBoot loaderOS Kernel / RTOSMiddleware*Applications

3 OS vs. Hardware Components

*Example: In Android, the middleware is called the Application Framework.

Source: Qing Li and Caroline Yao, “real-time concepts for embedded systems”

Page 4: 作業系統與硬體元件的驅動軟體開發法則 (Operating Systems and Software Design Principles for Hardware Components)

© 2015 William W.-Y. Liang, All Rights Reserved.

週邊裝置控制

Most of the I/O today is achieved by Memory-mapped I/OI/O devices communicate with the processor through a set of registers in the I/O controller.

Control registerStatus registerData register

4 OS vs. Hardware Components

Page 5: 作業系統與硬體元件的驅動軟體開發法則 (Operating Systems and Software Design Principles for Hardware Components)

© 2015 William W.-Y. Liang, All Rights Reserved.

即時作業系統

特性: 具備基本多工能力, 較容易滿足即時系統需求; 硬體須自行控制

Examples: VxWorks, Nucleus, ThreadX, QNX, RTEMS, Symbian, WinCE, TRON, uC/OS II, eCOS, FreeRTOS, mbed RTOS

Application Development: Usually implemented as threads which share the same space with the kernel; MMU is usually not used; Real-time considerations (priority, timing constrains)

5 OS vs. Hardware Components

Page 6: 作業系統與硬體元件的驅動軟體開發法則 (Operating Systems and Software Design Principles for Hardware Components)

© 2015 William W.-Y. Liang, All Rights Reserved.

一般用途作業系統

特性: 提供完整軟硬體資源管理機制; 應用程式不須顧慮底層硬體運作方式; 可專注軟體系統設計, 發展具備完整架構之軟體堆疊; 較易發展出功能複雜多樣化的軟體與應用產品

Example: Linux (with many Distributions), Android, Chrome OS, Firefox OS, Brillo, uClinux, MacOS, iOS, Windows

Application developmentSystem independent application development: General applications, GUI/Window programming, Web programmingSystem dependent software development: System programming, Device drivers, Protocol software

6 OS vs. Hardware Components & Brillo

Page 7: 作業系統與硬體元件的驅動軟體開發法則 (Operating Systems and Software Design Principles for Hardware Components)

© 2015 William W.-Y. Liang, All Rights Reserved.

微控制器作業系統範例: mbed OS

7 OS vs. Hardware Components

Supports a wide range of ARM Cortex-M based hardware platforms from major MCU vendorsProvide a C++ Application Framework and component architectureAutomation of power managementConnectivity protocol stack support for BLE®, Cellular, Ethernet, Thread, Wi-fi®, Zigbee IP, Zigbee NAN, 6LoWPAN, TLS & DTLS end-to-end IP secure communication channels, OMA LWM2M protocol for device managementSoftware asset protection and secure firmware updates for device security & management

https: //developer.mbed.org/blog/entry/Announcing-our-plans-for-mbed-v30/

Page 8: 作業系統與硬體元件的驅動軟體開發法則 (Operating Systems and Software Design Principles for Hardware Components)

© 2015 William W.-Y. Liang, All Rights Reserved.

mbed RTOS 範例: Thread & ISR & I/O Control

8 OS vs. Hardware Components

Page 9: 作業系統與硬體元件的驅動軟體開發法則 (Operating Systems and Software Design Principles for Hardware Components)

© 2015 William W.-Y. Liang, All Rights Reserved.

驅動程式在一般用途作業系統的定位

Device drivers are usually treated as black boxes for the application developers.They resemble as a software layer lying between the applications and the actual devices.

9 OS vs. Hardware Components

http: //www.ni.com/tutorial/3789/en/

Page 10: 作業系統與硬體元件的驅動軟體開發法則 (Operating Systems and Software Design Principles for Hardware Components)

© 2015 William W.-Y. Liang, All Rights Reserved.

Linux 作業系統

Open source and freeMaintained by numerous volunteer experts and hackersRobust, efficient, and reliableModular, configurable, and scalableTons of applications availableLower hardware requirement and BOM cost for mid-range to low-end products.Also suitable for general purpose computers, especially for high performance servers

10 OS vs. Hardware Components

Page 11: 作業系統與硬體元件的驅動軟體開發法則 (Operating Systems and Software Design Principles for Hardware Components)

© 2015 William W.-Y. Liang, All Rights Reserved.

Linux 驅動程式架構

UNIX/Linux systems implement device files to enable users to employ familiar commands and functions such as open, read, write, and close when working with some peripherals.

11 OS vs. Hardware Components

Example file operations int open( char *pathname, int flags, … ); int read( int fd, void *buf, size_t count ); int write( int fd, void *buf, size_t count ); int close( int fd );

Page 12: 作業系統與硬體元件的驅動軟體開發法則 (Operating Systems and Software Design Principles for Hardware Components)

© 2015 William W.-Y. Liang, All Rights Reserved.

Android作業系統

Consistent user experience and good UI designEasy to be commercializedComplete software frameworkLower App development costAlready supported by millions AppsApplicable for m high-end to mid-range devicesHigh portability for Apps and the OS itselfSuitable for resource-limited handheld devices

12 OS vs. Hardware Components

Page 13: 作業系統與硬體元件的驅動軟體開發法則 (Operating Systems and Software Design Principles for Hardware Components)

© 2015 William W.-Y. Liang, All Rights Reserved.

Android 硬體抽象層 (HAL)

A hardware abstraction layer (HAL) is an abstraction layer, implemented in software, between the physical hardware of a computer and the software that runs on that computer. Typically seen in portable operating system design, such as Linux.Realized in user-space of the Android operating system, with the concept of user-space device driver.

13 OS vs. Hardware Components

Page 14: 作業系統與硬體元件的驅動軟體開發法則 (Operating Systems and Software Design Principles for Hardware Components)

© 2015 William W.-Y. Liang, All Rights Reserved.

大綱

作業系統與驅動軟體架構

即時作業系統 (RTOS) 與一般用途作業系統

驅動程式與 RTOS, Linux, 及 Android硬體元件的驅動軟體開發

硬體元件與系統軟體銜接的準備工作

驅動程式於作業系統中所扮演的角色

驅動程式介面定義與實現方法

驅動程式的開源與閉源考量

開放與貢獻源碼的優點

如何兼顧開源標準與閉源優化

開源與閉源分界的設計原則

授權議題的處理原則與開源釋出準則14 OS vs. Hardware Components

Page 15: 作業系統與硬體元件的驅動軟體開發法則 (Operating Systems and Software Design Principles for Hardware Components)

© 2015 William W.-Y. Liang, All Rights Reserved.

硬體元件與系統軟體銜接的準備工作

選定一個或兩個初步支持之作業系統

如 Linux, Android, Windows, mbed OS, QNX, etc. Linux 已為應用最廣泛的作業系統核心, 故為必要選項

研究既有作業系統對元件支持的程度與實現方式

由於 Linux 與 Android 為發展程度最高的開源專案, 目前多數已標準化的軟硬體幾乎皆以其為首要支持對象

已開源的部分可直接繼承來用: 藉以了解實作方式、避免重複實作, 同時縮短開發時間.

步驟

根據各作業系統平台決定驅動程式介面

實作驅動程式

15 OS vs. Hardware Components

Page 16: 作業系統與硬體元件的驅動軟體開發法則 (Operating Systems and Software Design Principles for Hardware Components)

© 2015 William W.-Y. Liang, All Rights Reserved.

驅動程式於作業系統中所扮演的角色

驅動程式與上下層的關係

對上: 符合作業系統運作流程複雜度較高

不同作業系統有不同的「遊戲規則」

對下: 操作硬體配合硬體流程

一般用途作業系統須考慮的問題

正確運作: 複雜的多執行緒運行環境 + 中斷造成複雜的資源共享與同步問題

運行效能: 為了讓多工環境有效運行, 應謹慎共用資源

耗電考量: 行動裝置與節能議題

16 OS vs. Hardware Components

Page 17: 作業系統與硬體元件的驅動軟體開發法則 (Operating Systems and Software Design Principles for Hardware Components)

© 2015 William W.-Y. Liang, All Rights Reserved.

驅動程式介面定義與實現方法

驅動程式即為一種硬體抽象層

Hardware Abstraction Layer (HAL)介面函數規格定義應包含:

輸入命令與資料

輸出資料與狀態

規範與限制

Device Driver (HAL) API 隨作業系統而異

元件介面 Vendor API 則隨硬體功能由廠商定義

包含基礎操作功能, 通常與特定平台無關、具可移植性

可參考硬體功能與既有系統已定義的共通介面來設計

Glue實作: 基於 Vendor API 以實現對應驅動程式17 OS vs. Hardware Components

Page 18: 作業系統與硬體元件的驅動軟體開發法則 (Operating Systems and Software Design Principles for Hardware Components)

© 2015 William W.-Y. Liang, All Rights Reserved.

驅動程式的開源與閉源考量

開源與閉源的原則

只保護最需要的部分

其他部分可考慮盡量開放沒有技術含量的部分

技術含量太低的部分(亦即別人也很容易實現的地方)

開源 vs. 閉源

不開源代表必須自行維護, 包括處理各式各樣客戶的需求 (會增加維護上的負擔與成本)開源可借力使力, 可透過客戶或其他開發者協助改善軟體品質

18 OS vs. Hardware Components

http: //m.eettaiwan.com/ART_8800697488_676964_NT_57e14b60_2.HTM

Page 19: 作業系統與硬體元件的驅動軟體開發法則 (Operating Systems and Software Design Principles for Hardware Components)

© 2015 William W.-Y. Liang, All Rights Reserved.

開放與貢獻源碼的優點

Upstream 源碼

將源碼貢獻回開源源碼庫

一旦開源社群審核並接受之後, 開發者將持續為該源碼進行維護

以 Linux Kernel 為例, 其優點包括: 較佳品質: 被維護的驅動程式可能被開發者 (Kernel Hackers) 改得更好

持續維護: 被接受的驅動程式可以持續被維護及更新 (特別是對新版本的跟進), 長期來看可降低維護成本, 亦可延長硬體銷售期. 提升公司能見度與 Credit

19 OS vs. Hardware Components

Page 20: 作業系統與硬體元件的驅動軟體開發法則 (Operating Systems and Software Design Principles for Hardware Components)

© 2015 William W.-Y. Liang, All Rights Reserved.

如何兼顧開源標準與閉源優化

設計原則: 盡量採開源以擴大適用場域, 亦可降低長期維護成本. 僅將最關鍵部分以閉源方式實作.韌體實現方式

作法: 以韌體運行於元件內建微控制器

優點: 完全隔絕授權議題

缺點: 可使用資源較受限

驅動軟體方式

作法: 以驅動程式實現所有工作

優點: 可運用主系統資源 (CPU & Memory)缺點: 需謹慎處理如 GPL 等授權問題

20 OS vs. Hardware Components

Page 21: 作業系統與硬體元件的驅動軟體開發法則 (Operating Systems and Software Design Principles for Hardware Components)

© 2015 William W.-Y. Liang, All Rights Reserved.

如何兼顧開源標準與閉源優化 (續)

中介服務方式

作法: 透過中介軟體服務元件操作

優點: 可隔絕授權相關問題

缺點: 大量 IPC 影響運行效能

混合實作模式

作法: 將不重要的程式邏輯開源, 關鍵操作保留於韌體於元件控制器或中介服務方式實現

優點: 可同時兼顧閉源與開源授權議題

缺點: 需良好的設計配套以降低溝通及資料傳輸延遲

21 OS vs. Hardware Components

Page 22: 作業系統與硬體元件的驅動軟體開發法則 (Operating Systems and Software Design Principles for Hardware Components)

© 2015 William W.-Y. Liang, All Rights Reserved.

開源與閉源分界的設計原則

如何訂定開源閉源的分界點是一種藝術, 如同手術一般, 漂亮的一刀是術後讓傷痕最小化的關鍵!考量重點: 保護的價值 vs. 維護成本 vs. 運行效能

若有 GPL 開源授權考量, 可考慮將閉源部分放在: 軟體服務元件 (如 Android 與其他 OS)雲端的伺服器軟體 (如雲端應用與 WebApp 做法)獨立處理器的韌體 (如微控制器韌體)

驅動軟體的設計通常可將閉源程式邏輯實現於微控制器韌體與上層軟體服務之中 (前述混合實作模式)

22 OS vs. Hardware Components

Page 23: 作業系統與硬體元件的驅動軟體開發法則 (Operating Systems and Software Design Principles for Hardware Components)

© 2015 William W.-Y. Liang, All Rights Reserved.

附錄: 授權議題的處理原則

GPL 相關部分必須開放

其他部分能開放盡量開放

閉源而以 binary 呈現的部分, 於不違反 GPL 的前提下, 應具備合理再散播之授權條款

如 MIT 或 BSD Licenses 或 NDA

23 OS vs. Hardware Components

認識 GPL 開源 ≠ GPL (General Public License) 強制開源的時機: GPL 軟體重新散佈(redistribution)時 GPL 的感染性

直接修改 GPL 源碼或靜態連結時必然發生 動態連結 (.so & .ko)則需由衍生作品角度來判定

正確運用 GPL: 適當拿捏開源與閉源可兼顧開放與保護的目的 GPL塑造了開源巨人, 讓我們可站在其肩膀向前大步邁進!

Page 24: 作業系統與硬體元件的驅動軟體開發法則 (Operating Systems and Software Design Principles for Hardware Components)

© 2015 William W.-Y. Liang, All Rights Reserved.

附錄: 源碼庫與開源釋出準則

目前採用最多的版本管理系統: git開源源碼庫: e.g. github, bitbucket最佳的開源方式: 貢獻至上游源碼庫(up-streaming)需符合上游源碼架構, 包括Coding style、source tree structure、data structure、implementation methods

24 OS vs. Hardware Components

Page 25: 作業系統與硬體元件的驅動軟體開發法則 (Operating Systems and Software Design Principles for Hardware Components)

© 2015 William W.-Y. Liang, All Rights Reserved.

Q&A

其他分享: http://goo.gl/6qxlSvHome: http://www.ntut.edu.tw/~wyliangFB: http://www.facebook.com/william.wyliangSlideshare: http://www.slideshare.net/williamwyliang/Email: [email protected]

關於講者:梁文耀 (William W.-Y. Liang)• 顧問 / 開源貢獻者 (2014.10~)• 鴻海科技集團創新數位系統事業群資深處長 (2013.01~2014.09)• 安佐立科技顧問公司技術總監 (2012.08~2013.07)• 國立台北科技大學資訊工程系專任助理教授 (2005.02~2012.07)• 先前經歷: 聚興科技研發處協理、晶慧資訊研發副總經理、

晶慧資訊研發部經理、晶慧資訊資深工程師、美商 Avant! 軟體工程師• 專長領域:作業系統、嵌入式系統、計算機結構、平行與分散式系統 Linux 系統軟體 (1993~Now) Android 與 Linux 嵌入式系統核心及軟硬整合開發 (2001~Now)

• 國立台灣大學資訊工程博士、國立清華大學資訊科學碩士

OS vs. Hardware Components25