linux嵌入式系統驅動程式的阻塞與非同步

2022-04-03 02:42:42 字數 1876 閱讀 9196

對於那些需要程序獨佔的裝置,需要使用linux提供的阻塞程式設計。步驟如下:

1.在裝置驅動程式中定義該裝置的程序等待列多,並將其初始化

static wait_queue_head_t wait_queue;

init_waitqueue_head(&wait_queue);

2.在裝置驅動程式的讀操作中,呼叫函式wait_event 實現阻塞訪問

int mixled_read(struct file * filp,char__user * buffer,size_t size,loff_t * ppos)

wait_event(wait_queue,elmixled_dev->full_flag!=0);

elmixled_dev->full_flag = 0;

if(copy_to_user(buffer,elmixled_dev->buf,read_lea))

printk(「copy to user err!」);

return lead_len;

在寫操作中,呼叫wake_up函式喚醒該裝置等待程序列隊上的程序

int mixled_write(struct file * filp, const char__user * buffer,size_t size,loff_t * ppos)

int write_len =size;

if(copy_from_user(elmixled_dev->buf,buffer,write_len))

printk(「copy from user err」);

elmixled_dev->full_flag = 1;

wake_up(&wait_queue);

return write_len;

對於非同步驅動程式設計,需要做的步驟如下:

1.定義裝置驅動程式的非同步通知列隊

struct fasync_struct * fasync_queue;

實現裝置驅動程式的非同步操作函式fasync,並在非同步操作函式fasync中呼叫fasync_helper函式將當前程序新增到裝置驅動程式的非同步通知列隊

int mixled_fasync(int fd,struct file * file ,int on)

printk(「enter mixled_fasync function」);

return fasync_helper(fd,file,on,&elmixled_dev->fasync_queue);

struct file_operations mixled_ops={

.fasync = mixled_fasync

3.在裝置驅動程式中,當檢測到裝置狀態資訊發生變化時,如資料到達或者按鍵被按下就需要通知應用程式,呼叫函式kill_fasync想應用程式傳送非同步通知訊息

irqeturn_t mixled_isr(int irq,void * dev_id)

printk(「key interrupt take place」);

if(elmixled_dev-》fasync_queue)

kill_fasync(&elmixled_dev->fasync_queue,sigio,pollmsg)

return irq_handled;

4.在裝置驅動的close函式中,呼叫mixled_fasync函式將當前程序從裝置的非同步通知列隊中移除

mixled_fasync(-1,file,0)

嵌入式linux的驅動程式

摘至嵌入式linux裝置驅動開發詳解 1.4 嵌入式linux驅動程式 1.4.1 嵌入式linux的核心空間和使用者空間 目前,各種處理器都能防止資源的未經授權訪問,包括嵌入式處理器。一般都是給cpu劃分不同的操作模式。不同的模式有不同的作用,某些操作不允許在低級別模式使用。核心態和使用者態是在硬...

嵌入式linux系統中裝置驅動程式

嵌入式linux系統中裝置驅動程式是核心的一部分,完成對裝置初始 讀寫操作和控制等功能。驅動程式隱藏了 硬體裝置的具體細節,對不同的裝置提供一致的介面,這些介面通過file operation結構來定義,設計驅動程式的大部分工作就是 根據硬體結構來 填寫 結構體中定義的函式。主要的函式包括open ...

linux 嵌入式驅動程式測試例項

include include include include include include include int main fd open dev fs o rdwr if fd 0 else printf open ok nwrite write fd,buf,strlen buf if n...