linux驅動程序混合架構,嵌入式系統最小驅動框架(類似linux驅動程序架構)(示例代碼)...

2010年就打算把linux里的驅動框架核心代碼摳出來的,但是由于懶而且linux代碼量大,一直下不了手。最近調試的intel curie里驅動架構也類似linux,代碼就少多了,由于工作需要不得不梳理一下這一堆代碼,今天花了一下午,把整個BSP部分的驅動核心抽離出來了,并且做了幾個小sample。

最小驅動框架核心代碼

1、設備管理

device.c

#include #include#include#include#include#include"../../bsp/soc/soc_config.h"#include"../../bsp/soc/device.h"

static struct td_device **all_devices =NULL;static uint32_t all_devices_count = 0;void init_devices(struct td_device **_all_devices, uint32_t _all_devices_count)

{if (all_devices !=NULL)/*Devices already init*/

return;/*Link array with root device*/all_devices=_all_devices;

all_devices_count=_all_devices_count;

uint32_t i;int ret = 0;for (i = 0; i < all_devices_count; ++i)

{struct td_device *dev =all_devices[i];if (dev->driver->init && (ret = dev->driver->init(dev)))

{

dev->powerstate =PM_NOT_INIT;

printf("dev(%d) is not init",dev->id);

}

dev->powerstate =PM_RUNNING;

}

}static voidresume_devices_from_index(uint32_t i)

{int ret = 0;struct td_device *dev =NULL;for (; i < all_devices_count; ++i)

{

dev=all_devices[i];

printf("resume device %d", dev->id);if (dev->powerstate <=PM_SHUTDOWN)

{

ret= -EINVAL;gotoerr_resume_device;

}if (dev->powerstate ==PM_RUNNING)/*Device already running*/

continue;if (dev->driver->resume && (ret = dev->driver->resume(dev)))gotoerr_resume_device;/*Current device resumed*/dev->powerstate =PM_RUNNING;

}return;

err_resume_device:

printf("failed to resume device %d (%d)", dev->id,ret);

}void resume_devices(void)

{

resume_devices_from_index(0);

}intsuspend_devices(PM_POWERSTATE state)

{

int32_t i;int ret = 0;/*Use the reverse order used for init, i.e. we suspend bus devices first,

* then buses, then top level devices*/

for (i = all_devices_count - 1; i >= 0; --i)

{struct td_device *dev =all_devices[i];//device already suspended

if (dev->powerstate <=state)continue;

printf("suspend dev %d", dev->id);if (!dev->driver->suspend)

{

dev->powerstate =state;continue;

}

ret= dev->driver->suspend(dev, state);if (!ret)

{

dev->powerstate =state;continue;

}break;

}if (!ret)return 0;/*Suspend aborted, resume all devices starting from where we had

* an issue*/

if (state >PM_SHUTDOWN)

resume_devices_from_index(i+ 1);return -1;

}

device.h

#ifndef __DEVICE_H_#define __DEVICE_H_#includetypedefenum{

PM_NOT_INIT= 0,

PM_SHUTDOWN,

PM_SUSPENDED,

PM_RUNNING,

PM_COUNT

} PM_POWERSTATE;structtd_device;structdriver;//struct __packed __aligned(4) td_device

structtd_device

{void *priv;struct driver *driver;

PM_POWERSTATE powerstate :8;

uint8_t id;

};structdriver

{int (*init)(struct td_device *dev);int (*suspend)(struct td_device *dev, PM_POWERSTATE state);int (*resume)(struct td_device *dev);

};intsuspend_devices(PM_POWERSTATE state);void resume_devices(void);void init_devices(struct td_device **all_devices, uint32_t all_devices_count);void init_all_devices(void);#endif

2、驅動程序配置文件,我這里配置了WDT , CLK , TEST 三個簡單的驅動程序。

soc_config.c

#include #include#include#include#include"../soc/soc_config.h"#include"../soc/device.h"#include"../driver/wdt/wdt.h"#include"../driver/clk/clk.h"#include"../driver/test/test.h"typedefenum{

WDT_ID= 0,

CLK_ID=1,

TEST_ID=2,

} DEVICE_ID;struct td_device pf_device_wdt ={

.id=WDT_ID,

.driver= &watchdog_driver,

.priv= &(structwdt_pm_data){

.a= 1,

.b=2,

},

};struct td_device pf_device_clk ={

.id=CLK_ID,

.driver= &clk_driver,

.priv= &(structclk_data){

.a=5,

.b=6,

},

};struct td_device pf_device_test ={

.id=TEST_ID,

.driver= &test_driver,

.priv= &(structtest_data){

.a=3,

.b=4,

},

};static struct td_device *qrk_platform_devices[] ={&pf_device_wdt,&pf_device_clk,&pf_device_test,

};#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))

void init_all_devices(void)

{/*Init plateform devices and buses*/init_devices(qrk_platform_devices, ARRAY_SIZE(qrk_platform_devices));

}

soc_config.h

#ifndef __SOC_CONFIG_H_#define __SOC_CONFIG_H_

extern structtd_device pf_device_wdt;extern structtd_device pf_device_clk;extern structtd_device pf_device_test;#endif

3、以上就是驅動架構的最小系統,下面添加一個驅動程序例子test_driver

test.c

#include #include#include"../../soc/soc_config.h"#include"../../soc/device.h"#include"../../driver/test/test.h"

int test_init(struct td_device *dev)

{return 0;

}static int test_suspend(struct td_device *dev, PM_POWERSTATE state)

{return 0;

}static int test_resume(struct td_device *dev)

{return 0;

}struct driver test_driver ={

.init=test_init,

.suspend=test_suspend,

.resume=test_resume

};

test.h

#ifndef _TEST_H_#define _TEST_H_#include

extern structdriver test_driver;structtest_data

{

uint32_t a;

uint32_t b;

};#endif

5、再寫個驅動程序調用實例

main.c

#include #include"../bsp/soc/device.h"#include"../bsp/soc/soc_config.h"#include"../bsp/driver/test/test.h"

intmain()

{//driver framework test!

init_all_devices();//driver struct test!

struct td_device *test_device =(struct td_device *)&pf_device_test;

printf("\r\n===test device(%d) ok!===\r\n",test_device->id);//driver api test!

struct driver *test_driver = (struct driver *)test_device->driver;if(test_driver->init(wdt_device)==0) printf("test init ok!\n");//driver data test!

struct test_data *data = (struct test_data *)test_device->priv;

printf("test_data a:%d,b:%d!\n",data->a,data->b);return 0;

}

用code::blocks可以直接編譯運行。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/373177.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/373177.shtml
英文地址,請注明出處:http://en.pswp.cn/news/373177.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

MyBaits 錯誤分析

錯誤原因&#xff1a;在DAO的映射文件中&#xff0c;在映射標簽中的type類型寫成DAO類了&#xff0c;應該寫成javaBean轉載于:https://www.cnblogs.com/shuaiandjun/p/5428847.html

超越JUnit –測試框架的替代方案

JUnit是事實上的Java單元測試框架&#xff0c;但是可能有一些新的&#xff08;不是那么新的&#xff09;框架可以用于Web開發。 在采用之前可能要問自己的問題&#xff1a; 它們是否快速&#xff0c;容易開發&#xff0c;因此成本低廉&#xff1f; 他們運行快并因此鼓勵采用嗎…

tensorflow mnist read_data_sets fails

下載處理mnist數據時出現如下錯誤 VisibleDeprecationWarning: converting an array with ndim > 0 to an index will result in an error in the future 解決方法&#xff1a; 在input_data.py文件中return numpy.frombuffer(bytestream.read(4), dtypedt) 后添加[0] retur…

斑馬打印機linux驅動安裝教程,linux-Zebra軟件包的基本安裝與配置

Zebra是一個路由軟件包&#xff0c;提供基于TCP/IP路由服務&#xff0c;支持RIPv1, RIPv2, RIPng, OSPFv2, OSPFv3, BGP- 4,和 BGP-4等眾多路由協議。Zebra還支持BGP特性路由反射器(Route Reflector)。除了傳統的 IPv4路由協議&#xff0c;Zebra也支持IPv6路由協議。如果運行的…

iOS 改變App狀態欄顏色為白色

默認狀態欄為黑色&#xff0c;對于某些App不是很美觀&#xff0c;變成白色很簡單&#xff0c;只需要兩個步驟。 1.在Info.plist中添加新項目&#xff0c;View controller-based status bar appearance&#xff0c;Boolean值為No. 2.在AppDelegate的- (BOOL)application:(UIAppl…

Java 7對抑制異常的支持

在JDK 7中 &#xff0c;向Throwable類&#xff08; Exception和Error類的父類&#xff09;添加了一個新的構造函數和兩個新方法。 添加了新的構造函數和兩個新方法以支持“抑制的異常”&#xff08;不要與吞咽或忽略異常的不良做法相混淆&#xff09;。 在本文中&#xff0c;我…

linux 如何做共享磁盤陣列,在Linux上玩轉磁盤陣列分享

大部分用戶都會擔心&#xff0c;萬一硬盤發生故障&#xff0c;一、使用磁盤陣列可以帶來哪些好處?在具體如何配置磁盤陣列之前&#xff0c;筆者要先給大家介紹一下利用磁盤陣列的好處。先給大家一點動力&#xff0c;讓大家能夠繼續看下面的內容。第一個好處是磁盤陣列可以提高…

my-innodb-heavy-4g.cnf

my-innodb-heavy-4g.cnf轉載于:https://www.cnblogs.com/xiluhua/p/6231834.html

易于使用的單位和集成代碼

此示例說明如何使用Maven和Sonar生成單元測試和集成測試的覆蓋率。 它使用非常簡單的技術&#xff0c;只需10-15分鐘即可在任何現有的Maven構建中運行。 它可用于單元&#xff0c;集成&#xff0c;ATDD或任何其他類型的測試套件。 覆蓋率結果顯示在Sonar中。 有什么事嗎&#x…

Dij的堆優化

#include<algorithm> #include<iostream> #include<cstdio> #include<cstring> #include<queue> #define M 100000 #define pa pair<int,int>//優先比較第一個元素 using namespace std; int d[M],n,m,cnt,head[M],next[M],u[M],dis[M],n…

linux db2sysc 內存,db2sysc進程占用linux內存持續增長,請各位指點。

該服務器近期做過的變更情況&#xff1a;變更前&#xff0c;使用 sar -r 1 3 看內存使用率服務器內存使用率一直是70%該服務器原為獨立物理服務器&#xff0c;經過虛擬化遷移到EXS上成為虛擬服務器。遷移后發現swap無法啟動。原因是原物理服務器硬盤控制器為cciss。/etc/fstab …

k8s的探針

一、探針原理 分布式系統和微服務體系結構的挑戰之一是自動檢測不正常的應用程序&#xff0c;并將請求&#xff08;request&#xff09;重新路由到其他可用系統&#xff0c;恢復損壞的組件。健康檢查是應對該挑戰的一種可靠方法。使用 Kubernetes&#xff0c;可以通過探針配置運…

第一百三十節,JavaScript,封裝庫--連綴

JavaScript&#xff0c;封裝庫--連綴 學習要點&#xff1a; 1.連綴介紹 2.改寫庫對象 本章我們重點來介紹&#xff0c;在調用庫的時候&#xff0c;我們需要能夠在前臺調用的時候可以同時設置多個操作&#xff0c;比如設置CSS&#xff0c;設置innerHTML&#xff0c;設置click事件…

Spring3:類型安全依賴項注入

在從Spring跳到類型安全依賴注入之前&#xff0c;我想討論一下我們之前所做的方式。 我們一直在借助Spring的Autowired注釋按類型使用依賴項注入。 像這樣的東西會注入Spring Bean。 Autowired private StudentDao studentDao; // Autowires by type. Injects the instance who…

userData IE

蠻討厭IE的&#xff0c;因為他常常需要特別照顧&#xff0c;就像DOM Storage(sessionStorage和localStorage)只能支持IE8&#xff0c;對于以下的只能使用userData。 原理&#xff1a;通過在document元素后面附加一個專屬的“DHTML行為”來實現客戶端存儲&#xff0c; var memor…

context元素大概解說

Context元素代表一個web應用&#xff0c;運行在某個特定的虛擬主機上。如Servlet Specification 2.2或以后版本中描述的那樣&#xff0c;每個web應用基于一個Web Application Archive(WAR)文件&#xff0c;或者是一個目錄&#xff0c;包含WAR文件解壓后的內容。有關Web Applica…

全新的Play模塊資料庫

去年11月&#xff0c;我曾與Play框架的 Nicolas Leroux談過創建模塊存儲庫的問題。 他同意這將是一個好主意&#xff0c;但是時間不足使我無法開始。 在上周Google Play小組發生了暴風雨之后&#xff0c;我決定將其優先處理。 可以在幾周內提供可工作的原型。 概述&#xff1a;…

Ubuntu 16.04 安裝 VMware-Workstation-12

以前一直使用 Ubuntu Virtaulbox &#xff0c;最近測試了 VMware-Workstation-9,性能超過 Virtaulbox-4.2.x,下面是詳細步驟:1 首先準備一個Ubuntu 系統 lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 16.04 LTS Release: 16.04 …

Linux的md64進程,在Linux上安裝Elasticsearch Kibaba.md(示例代碼)

在Linux上安裝Elasticsearch KibabaKibana是一個開源為elasticsearch 引擎提供數據和數據分析1、下載安裝切換到root賬戶&#xff0c;按順序依次執行以下命令rpm包安裝$wget -c https://artifacts.elastic.co/downloads/kibana/kibana-5.5.3-x86_64.rpm$sha1sum kibana-5.3.2-x…

SSH實戰 · 唯唯樂購項目(中)

用戶模塊三&#xff1a;一級分類的查詢創建一級分類表并導入基本數據CREATE TABLE category (cid int(11) NOT NULL AUTO_INCREMENT,cname varchar(255) DEFAULT NULL,PRIMARY KEY (cid)) ENGINEInnoDB AUTO_INCREMENT11 DEFAULT CHARSETutf8;建包及相應的類:com.weiwei.shoppi…