linux字元裝置驅動開發之裝置樹 2

2021-09-29 07:10:45 字數 3862 閱讀 5479

裝置樹描述了裝置的詳細資訊(比如reg->暫存器位址),編寫驅動的目的在於獲取這些資訊.

linux提供了一些of函式讀取這些資訊, /include/linux/of.h.

查詢節點相關函式

// 裝置節點

struct device_node

;// 通過節點名查詢指定的節點

// from : 節點, 若為null,則從根節點查起.

// name : 查詢節點名

// return : 找到的節點

struct device_node *

of_find_node_by_name

(struct device_node *from,

const

char

*name)

// 通過節點型別查詢指定節點

struct device_node *

of_find_node_by_type

(struct device_node *from,

const

char

*type)

// 通過指定節點型別(device_type)和相容(compatible)查詢節點

struct device_node *

of_find_compatible_node

(struct device_node *from,

const

char

*type,

const

char

*compat)

// 通過路徑查詢指定節點

// path : 全路徑節點名

struct device_node *

of_find_node_by_path

(const

char

*path)

查詢父/子節點相關函式

// 獲取指定節點的父節點

struct device_node *

of_get_parent

(const

struct device_node *node)

// 迭代查詢子節點

// node :父節點

// prev : 前乙個子節點, 設定null表示 從第乙個節點開始

struct device_node *

of_get_next_child

(const

struct device_node *node,

struct device_node *prev)

提起屬性的函式

// 屬性結構體

struct property

;// 查詢指定節點屬性

// name : 屬性名稱

//lenp : 屬性位元組數

struct property *

of_find_property

(const

struct device_node *np,

const

char

*name,

int*lenp)

// 獲取屬性中元素的數量

// proname :統計元素數量的屬性名稱

// elem_size : 元素長度

intof_property_count_elems_of_size

(const

struct device_node *np,

const

char

*propname,

int elem_size)

// 讀取屬性中 u8,u16,u32和u64型別陣列資料

intof_property_read_u8_array

(const

struct device_node *np,

const

char

*propname, u8 *out_values, size_t sz)

intof_property_read_u16_array

(const

struct device_node *np,

const

char

*propname, u16 *out_values, size_t sz)

intof_property_read_u32_array

(const

struct device_node *np,

const

char

*propname,u32 *out_values, size_t sz)

intof_property_read_u64_array

(const

struct device_node *np,

char

*propname,

*out_values, size_t sz)

// 讀取屬性中字串資料

intof_property_read_string

(struct device_node *np,

const

char

*propname,

const

char

**out_string)

//讀取資料整形資料

intof_property_read_u8

(const

struct device_node *np,

const

char

*propname,

u8 *out_value)

intof_property_read_u16

(const

struct device_node *np,

const

char

*propname,

u16 *out_value)

intof_property_read_u32

(const

struct device_node *np,

const

char

*propname,

u32 *out_value)

// 獲取節點 address-cells

intof_n_addr_cells

(struct device_node *np)

// 獲取節點size-cells

intof_n_size_cells

(struct device_node *np)

;

其他常用函式

// 檢查節點是否與name相容

intof_device_is_compatible

(const

struct device_node *device,

const

char

*name)

//of_address.h

// 轉換裝置樹讀取到的位址為實體地址

// addr 要轉換的位址

u64 of_translate_address

(struct device_node *np,

const __be32 *addr)

// 替代ioremap,of_iomap本質是將reg屬性中位址資訊轉化為虛擬位址,若為多段,使用index轉化

// index : reg屬性中記憶體對映段,一段設定為0

void __iomem *

of_iomap

(struct device_node *device,

int index)

;

Linux裝置驅動之字元裝置 1 建立字元裝置

建立乙個字元裝置並在 dev目錄下建立節點的基本步驟 include include include include include include include include include include include include include include define hell...

驅動 linux裝置驅動 字元裝置驅動開發

preface 前面對linux裝置驅動的相應知識點進行了總結,現在進入實踐階段!linux 裝置驅動入門篇 linux 裝置驅動掃盲篇 fedora下的字元裝置驅動開發 開發乙個基本的字元裝置驅動 在linux核心驅動中,字元裝置是最基本的裝置驅動。字元裝置包括了裝置最基本的操作,如開啟裝置 關閉...

Linux驅動開發之字元裝置驅動例項

1 驅動標頭檔案mem dev.h ifndef memdev h define memdev h ifndef memdev major define memdev major 238 預設的mem的主裝置號 endif ifndef memdev nr devs define memdev nr...