2007年9月17日 星期一

MDD與PDD

分層Driver的架構分成MDD與 PDD兩部份,前者和具體的設備綁定在一起,是針對特定設備特定型號的,後者是同一類設備驅動中公用的部分,主要由CE的系統支持提供,通常不需要修改。(意思就是說MDD是MS寫好的,PDD則要vendor自己來implement)

以Audio driver舉例:
若我們要implement一個stream interface driver,我們可以使用Microsoft提供的MDD(model device driver) library, Wavemdd.lib, 透過audio device driver service-provider interface(DDSI) function來實現stream interface function.
使用Wavemdd.lib,那我們就要create 一個對應的PDD(platform-dependent driver) library來implement audio DDSI function.
PDD library 通常會被稱為Wavepdd.lib,僅管它可能是不需要的, 但這兩個library會從audio driver被link起來 成為一個.dll的檔案,Wavedev.dll。
如圖所示:

2007年8月30日 星期四

I2C Bus

概觀
  • I2C 為 Inter-Integrated Circuit的縮寫,I2C Bus顧名思義就是IC之間溝通的匯流排
  • I2C Bus為飛利浦(Philips)在1980年代所發展的雙向二線式的串列匯流排(Serial Bus)標準
    • 資料線:Serial data line (SDA)
    • 時脈線:Serial clock line (SCL)
  • I2C Unit允許Processor透過I2C Bus來服務master和slave 裝置
  • 資料在I2C Bus上可以用三種速度被傳送
    • Standard-mode: 100 kbit/s
    • Fast-mode: 400 kbit/s
    • High-speed mode: 3.4 Mbit/s
I2C Bus的優點
  • 相較傳統平行匯流排(Parallel Bus)
    • 架構簡單,只需要二條線就能傳送資料,可靠度及安全性佳
    • 省去平行匯流排所須的解碼電路
    • 降低平行匯流排可能因接線太多而造成的電磁干擾和靜電放電的副作用
I2C Bus適用的情況
  • 系統包含μC及其他周邊
  • 希望將連接的成本降至最低
  • 系統不需要很快的傳輸速度
  • 希望應用於Multi-Master系統
I2C Bus的硬體特性
  • I2C Bus介面本身為Open Drain或是Open Collector構造,因此應用時需外加電源及提升電阻才能運作

I2C Bus功能說明
  • I2C Bus定義一個由序列資料/位址線(SDA) 和序列時脈線(SCL)組成的序列協定給I2C Bus上的處理器來處理通過的資訊
  • I2C Bus上的每個裝置是由一個獨特的7bits address來識別而且可以在master或slave模式下當成傳送端或接收端來操作


  • for example

    • 當Processor 在I2C Bus上作用為master時,它定址一個EEPROM為slave來接收資料。當Processor定址EEPROM時,它是一個master傳送端而EEPROM是一個slave接收端。當Processor讀取資料時,它是一個master接收端而EEPROM是一個slave傳送端。不管它是傳送端或是接收端,master會產生時脈信號,起始傳送,還有終止傳送。
  • I2C Bus是一種同步傳輸協定,其資料傳送形態包含
    • 開始(Start)
    • 位址(Address)
    • 讀/寫(R/nW)
    • 資料(Data)
    • 確認(Acknowledge)
    • 停止(Stop)
I2C Bus信號變化原則

  • SDA若要變化,只有在SCL為低電位時才可以改變,而SCL在高電位時,其對應同時SDA的狀態(高電位或低電位)就是其傳送的位元(1 or 0)
I2C Bus資料傳送形態
  • 開始(Start)
    • Master必須送出”start”信號才能取得I2C Bus的控制權
    • 當I2C Bus沒有動作時,SCL和SDA都保持在高電位
    • I2C Bus動作時,master先在SDA送出低電位,經過一小段時間後,再將SCL變成低電位

  • 停止(Stop)
    • 和”start”信號的動作相反
    • Master完成和slave的動作後,先將SCL釋放至高電位,經一小段時間在將SDA放至高電位

  • 位址(Address)
    • 每一個送到Bus上的資料都必須是8bit
    • 以standard mode為例,Address有7個bit,master會先傳送MSB到Bus上,再依次傳出其他bit
  • 讀/寫(R/nW)
    • 緊接著Address,只佔一個bit 為LSB,高電位時為Read,低電位時為Write

  • 確認(Acknowledge)
    • Slave正確地收到資料後,會發出”ACK”信號向master表示收到
    • Master會將SDA釋放成高電位,slave確認時會將SDA拉至低電位
    • Slave未能正確地收到資料,slave則不動作,SDA會維持在高電位

  • 資料(Data)
    • 和Address及R/nW一樣,只不過資料可能由master或slave送出,由master或slave接收,而Address及R/nW則是由master送出,slave接收
    • 資料的意義隨著裝置的不同而不同

I2C Bus的操作
  • 簡單的溝通原理
  • Uc要對memory寫入資料
  1. Uc發出start訊號告訴其他device它要動作了,接著發出”device address”(每個deivce都有一個唯一的address)和讀寫訊號
  2. Memory收到uc要它做讀寫動作,便發出ack告訴uc我收到了
  3. 然後uc傳送欲寫入的address(這裡是指memory address)給memroy
  4. Memory再發出ack給uc
  5. Uc再將欲寫入的資料送給memory
  6. 寫入完畢後,memroy又發出ack給uc
  7. 接著uc送出stop告訴bus上的所有device我的事做完了

  • Master接收Slave傳送 / 重複START / Master傳送Slave接收

Multi-Master
  • Single-Master: 只有一個μC發號施令,主控整個系統
  • Multi-Master: 系統有二個以上的μC發號施令,其他的μC(slave)只能受主控的μC (master)控制,在不對外界送出命令時,master本身又可以變成slave

  • 就是說有兩個以上的μC時,只有一個主控的master μc,其他的μc均當成slave
  • Multi-master就只靠scl和sda這兩條線來完成,在兩條線上有很多個master傳送資料,那不是很容易發生衝突嗎? 靠著open collector (drain)的特性,scl和sda均可作wired_and動作,這些問題都可以解決。達成arbitration 和 synchronization的功能以確保bus正常運作。
Arbitration
  • Multi-master功能需要I2C Bus arbitration(調停),當二或多個master在最短的停留時間產生START信號時調停將發生
  • 使用wired-AND硬體線路,因此多個master發出同樣狀態的信號,傳輸不會遺失資料
  • 假如包含的address和R/nW或是data不一樣時,master會發出高電位狀態來表示調停失敗並關閉它的SDA,然後回到slave模式

  • 當兩個以上的master送出不同的位址時,低位址會擁有優先權
Synchronization
  • 每個I2C Bus上的master為了在SCL上的資料傳輸會產生它自己的時脈
  • 不同頻率的時脈連接到SCL,當時脈是在高電位期間時資料是有效的
  • Master擁有最長的時脈週期者會將SCL維持在低電位
  • 當兩個master發出的信號頻率不同時,SCL的頻率會以最低的為標準
Clock synchronizing mechanism
  • Service an internal interrupt
    • If a slave can’treceive or transmit another complete byte of data until ithas performed some other function, for example servicing an internal interrupt, it can hold the clock line SCL LOW to force the master into a wait state. Data transfer then continues when the slave is ready for another byte of data and releases clock line SCL.
  • Handshake
    • The clock synchronization mechanism can be used to enable receivers to cope with fast data transfers, on either a byte level or a bit level. On the byte level, a device may be able to receive bytes of data at a fast rate, but needs more time to store a received byte or prepare another byte to be transmitted. Slaves can then hold the SCL line LOW after reception and acknowledgment of a byte to force the master into a wait state until the slave is ready for the next byte transfer in a type of handshake procedure.


A good web for I2C BUS protocol
http://www.esacademy.com/faq/i2c/

2007年7月20日 星期五

How to load a driver into user mode

在CE6.0中一般driver default都是run在kernel mode
要如何將driver load 到 user mode?
其實很簡單
只需要設定platform.reg中Flags的value為 DEVFLAGS_LOAD_AS_USERPROC(0x10)
n

及platform.bib即可。

以上次建出來的Driver為範例。

platform.reg
//===============================
[HKEY_LOCAL_MACHINE\Drivers\BuiltIn\Cin]
"Dll" = "CIN.dll"
"Prefix" ="CIN"
"Index"= dword:1
"Order"= dword:0
"FriendlyName" = "CIN Demo Driver"
"Ioctl" = dword:0
"Flags" = dword:10
; Flags==0x10 is DEVFLAGS_LOAD_AS_USERPROC

//===============================

platform.bib
//===============================
CIN.dll $(_FLATRELEASEDIR)\CIN.dll NK SH
//===============================


run 出來的結果


千萬要注意哦
bib檔的SHK要改成SH哦,沒改的話會出現錯誤。
run出來的結果

How to create a CE 6.0 driver

其實建CE 6.0 的driver與5.0的雷同,先前也曾介紹過。
1.首先在BSP底下建你要的driver folder。

\WINCE600\PLATFORM\DEVICEEMULATOR\SRC\DRIVERS


2.接著建立所需的makefile、.def、sources、.c(or .cpp)檔。
內容設定如下:簡略說明,可參閱stream driver文章。
D:\WINCE600\PLATFORM\DEVICEEMULATOR\SRC\DRIVERS\CINTEST


.def檔

//=========================================

LIBRARY CINTEST // driver folder 名稱

EXPORTS
// 要export 的function
XXX_Init

XXX_Deinit
XXX_Open
XXX_Close
XXX_IOControl
XXX_PowerUp
XXX_PowerDown
XXX_Read
XXX_Write
XXX_Seek

//=========================================


source檔
//=========================================
SYNCHRONIZE_DRAIN=1

TARGETNAME=CIN //.dll的名稱
RELEASETYPE=PLATFORM
TARGETTYPE=DYNLINK

TARGETLIBS= \
$(_SYSGENSDKROOT)\lib\$(_CPUINDPATH)\coredll.lib \ //會呼叫到的library

SOURCES= \
cin.c \\用到的.c檔
//=========================================


3.修改platform.reg和platform.bib檔
\WINCE600\PLATFORM\DEVICEEMULATOR\FILES

platform.reg
//=========================================
[HKEY_LOCAL_MACHINE\Drivers\BuiltIn\Cin]
"Dll" = "CIN.dll"
"Prefix" ="CIN"
"Index"= dword:1
"Order"= dword:0
"FriendlyName" = "CIN Demo Driver"
"Ioctl" = dword:0

//=========================================


platform.bib
//=========================================
CIN.dll $(_FLATRELEASEDIR)\CIN.dll NK SHK
//=========================================

2007年7月13日 星期五

system calls

In compupting, a system call is the mechanism used by an application program to request service from the operating system.
簡言之就是應用程式與作業系統的介面

2007年7月4日 星期三

User Mode Driver Framework

User Mode Driver Framework Development Concepts
  • allows an intermediate driver to load in user mode
  • cannot directly access hardware
  • some drivers will be more stable in user mode
  • reflector in kernel makes a user mode driver work as if it was a kernel mode driver
User Mode Driver Framework Architecture



The user mode driver framework is divided into two physical components.
  • the user mode driver reflector, which reside inside the device manager.
    • manages the user mode driver host processor.
    • forwards the requests that are issued by the device manager.
    • provides services for a user mode driver.
  • the user mode driver host, which is a user mode application that is launched and managed bye the user mode driver reflector.
    • it is used at the kernel's request to launch and to manage a user mode driver.
    • it is responsible for mounting the API of a volume in order to receive forwarded requests from the user mode driver reflector.
Registry
  • stores configuration information for applications, drivers, and the OS.
  • OS uses registry to locate and load device drivers.
  • Same basic setup as the desktop windows OS registry.
  • space is limited, so keep any new entries short.
Device Manager
  • manages devices and their interfaces.
  • decides which drivers to load.
  • uses registry to find drivers.
  • runs continuously.

Kernel Mode Drviers VS. User Mode Drivers

The development team at Microsoft has made a number of changes to the architecture of Windows CE in version 6.0. One of these is a new model for loading device drivers that can provide for increased security, robustness or performance.

Prior to CE6, device drivers primarily resided in the device.exe process.
The new kernel architecture provides more flexibility by providing two different driver models. The old device.exe functionality has been moved into the kernel, and a new user device process (udevice.exe) has been introduced. This change results in two different driver models, kernel mode drivers and user mode drivers. BSP developers must choose how each of their drivers will be implemented on a particular platform.

Kernel Mode Drivers

Kernel mode drivers are loaded by device.dll inside of the kernel. This is the default behavior, and most closely resembles the old device.exe model. The big win here is performance – the expensive interprocess calls (thunks) have been eliminated. There is no need for the user mode process initiating a driver access to be switched out, because the user mode process coexists with the kernel. The kernel includes the file system and device drivers so everything is resident in memory at the same time.

This new performance benefit comes at a price. Kernel mode drivers are now a part of the kernel, and they have full privileges for the entire kernel address space. A driver failure that results in memory corruption could easily bring down the kernel and with it the entire system. Therefore it’s critical that kernel mode drivers be robust.


User Mode Drivers

CE6 provides a new mechanism to load drivers into individual user mode processes (called udevice.exe) instead of into the kernel. A driver that loads in this fashion is called a user mode driver, and it gives up a level of performance in return for increased system robustness and security. In addition, since user mode drivers run inside a user mode process they are restricted in their use of certain APIs . This security feature restricts drivers loading in user mode from arbitrarily accessing any hardware resource in the system.

Microsoft has designed the user mode driver model to have a very high level of compatibility with kernel mode drivers. There are very few differences between the two models, and a driver written for user mode can be loaded into kernel mode with no changes. A new kernel component called the user mode driver reflector handles the interface between user mode applications and the user mode driver. This means that applications do not need to be aware of how a particular driver was loaded.

It’s a simple matter to load a driver into user mode instead of the default kernel mode. There is a new bit defined in the Flags registry key that if set tells the kernel to load the corresponding driver into user mode. That’s it – nothing more to it. The user mode driver will run in its own process isolated from the kernel and the rest of the system. If the driver fails for some reason only its copy of udevice.exe is affected, the rest of the system should remain intact.

BSP developers now have the option of choosing between a high performance kernel mode driver and a more protected user mode driver. Untrusted third party drivers can be loaded into user mode to increase system security and robustness. Unstable or otherwise questionable drivers can start life in user mode and then be moved to kernel mode as they become proven.


參考來源:http://www.bsquare.com/blog/default-aug_21.asp