設備驅動和設備模型

Device Drivers and Device Model

設備驅動和設備模型

Introduction - 簡介

The Zephyr kernel supports a variety of device drivers. The specific set of device drivers available for an application’s board configuration varies according to the associated hardware components and device driver software.

Zephyr內核支持多種設備驅動。根據關聯的硬件組件和設備驅動軟件的不同,App使用的設備驅動集合通過單板配置來改變。

The Zephyr device model provides a consistent device model for configuring the drivers that are part of a system. The device model is responsible for initializing all the drivers configured into the system.

Zephyr設備模型提供設備模型來配置驅動。設備模型有責任完成所有配置的驅動的初始化。

Each type of driver (UART, SPI, I2C) is supported by a generic type API.

每種驅動都可以被通用API所支持。

In this model the driver fills in the pointer to the structure containing the function pointers to its API functions during driver initialization. These structures are placed into the RAM section in initialization level order.

在這個驅動模型填充的結構體指針中包含了執行API功能的函數指針。這些結構依照初始化級別順序存放在RAM中。

Standard Drivers - 標準驅動

Device drivers which are present on all supported board configurations are listed below.

  • Interrupt controller: This device driver is used by the kernel’s interrupt management subsystem.

  • - 中斷控制器。用於內核終端管理子程序。

  • Timer: This device driver is used by the kernel’s system clock and hardware clock subsystem.

  • - 定時器。用於內核系統時鐘和硬件時鐘子系統。

  • Serial communication: This device driver is used by the kernel’s system console subsystem.

  • - 串口通信。用於內核系統控制檯子系統。

  • Random number generator: This device driver provides a source of random numbers.

    Important

    Certain implementations of this device driver do not generate sequences of values that are truly random.

  • - 隨機數生成器。用於產生隨機數。實際上並不是完全隨機的。

Synchronous Calls - 同步調用

Zephyr provides a set of device drivers for multiple boards. Each driver should support an interrupt-based implementation, rather than polling, unless the specific hardware does not provide any interrupt.

除非某硬件不支持中斷,否則,所有的中斷都應該基於中斷實現,而不是輪詢。

High-level calls accessed through devices’s specific API, such as i2c.h or spi.h, are usually intended as synchronous. Thus, these calls should be blocking.

高層調用訪問設備通過制定API,一般都是嚴格同步的。因此,這些調用是阻塞式的。

Due to the fact that Zephyr provides two types of execution contexts (task and fiber) on a microkernel, drivers need to act accordingly. For example, a nanokernel semaphore cannot be used when the context is a task, so theinclude/device.h exposes a helper API to handle this case transparently; no other means ought to be used instead.

在微內核中,Zephyr提供了兩種上下文(task和fiber),因此,驅動需要分別實現兩種。比如,微內核信號量不能用於task上下文,所以include/device.h文件提供了幫助API來顯示處理這種情形;除此別無它法。

Zephyr API exposes 1 type and 3 inline functions to solve this issue. - Zephyr API使用1種類型和3個內聯函數來解決這一問題。

device_sync_call_t

This type provides a nanokernel semaphore, always present in both nanokernel and microkernel use cases. It is meant to be used within a fiber context. Then, and only on a microkernel type, it will provide a kernel semaphore meant to be used within a task context. A boolean marker is used to remember the caller’s context when running a microkernel.
這個類型提供了nanokernel信號量,在超微內核和微內核使用中,可以在fiber上下文中使用。如果被用於task上下文,那麼僅能用作微內核類型。當運行微內核時,使用布爾型標記來記住調用上下文。

device_sync_call_init()

This function initializes the device_sync_call_t type semaphores and the marker. This function should be used only once in the device driver’s instance lifetime. Thus, the driver’s initialization function is the best place for calling it.
初始化上述類型以及標識。在設備驅動實例化生命週期內,該函數僅能使用一次。因此,最好在驅動初始化函數中調用該函數。

device_sync_call_wait()

This function will block - that is, it will perform a “take wait” - on the relevant semaphore. The exposed driver’s API function can then be used as a blocking function until the relevant semaphore is released by a give. This is therefore used to start a synchronous call, and waits until being signaled for synchronization.
函數會被相關的信號量阻塞。該函數接口會被阻塞直到相關的信號量被give。可以被用於信號量同步。

device_sync_call_complete()

This function releases the relevant semaphore and thus will unlock the blocking function. Most frequently will it be called in the driver’s ISR handler. It is used to signal the completion of the synchronous call (error or success).
函數釋放相關的信號量。解鎖阻塞函數。通常在驅動中斷處理函數中調用。

Driver APIs - 驅動APIs

The following APIs for device drivers are provided by device.h. The APIs are intended for use in device drivers only and should not be used in applications.

驅動API放在device.h中。這些API僅僅用於設備驅動,請不要用於APP之中。

DEVICE_INIT()
create device object and set it up for boot time initialization.
創建設備對象,並且在啓動階段初始化設備對象。
DEVICE_AND_API_INIT()
Create device object and set it up for boot time initialization. This also takes a pointer to driver API struct for link time pointer assignment.
創建設備對象,並且在啓動階段初始化設備對象。該API生成一個指向驅動API對象的指針。
DEVICE_NAME_GET()
Expands to the full name of a global device object.
獲取全局設備對象的完整名稱。
DEVICE_GET()
Obtain a pointer to a device object by name.
通過設備名稱獲取設備指針。
DEVICE_DECLARE()
Declare a device object.
聲明設備對象
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章