驱动模板编程总结

驱动模板

  • xxx.c –> 应用层驱动封装函数
  • xxx.h –> 应用层驱动封装函数头文件
  • test.c –> 测试驱动所有的应用层代码
  • xxxDrv.c –> 内核层驱动实现文件
  • xxxDrv.h –> 内核层驱动实现文件函数头文件
  • Makefile –> 编译管理
  • 一个外设的驱动程序模板一般都需要这几个文件

具体展开讲:

  • xxxDrv.c –> 内核层驱动实现文件
/*设备驱动模块加载函数*/
static int __init key_init(void) 
{
    int ret;

    printk("key_init\n");
    devNo = MKDEV(251, 2); //通过主设备号(251)和次设备号(2)生成一个dev_t的设备号 
    ret = register_chrdev_region(devNo, 1, "key"); //用来向系统申请设备号
    if (ret < 0) {
        printk("Register char device faikey, ret = %d\n", ret);
        goto err;
    }

    cdev_init(&keyDev, &key_ops);  //用来初始化cdev的成员,并建立cdev和file_operations之间的连接
    keyDev.owner = THIS_MODULE;

    ret = cdev_add(&keyDev, devNo, 1);  //向系统添加一个cdev,完成字符设备的注册
    if (ret < 0) {
        printk("Add dev faikey\n");
        unregister_chrdev_region(devNo, 1);
        goto err;
    }
    printk("Hello device init success\n");
    //实现具体的硬件初始化!
    s3c2410_gpio_cfgpin(S3C2410_GPF(KEY0), S3C2410_GPIO_INPUT);
    s3c2410_gpio_cfgpin(S3C2410_GPF(KEY1), S3C2410_GPIO_INPUT);
    s3c2410_gpio_cfgpin(S3C2410_GPF(KEY2), S3C2410_GPIO_INPUT);
    s3c2410_gpio_cfgpin(S3C2410_GPF(KEY3), S3C2410_GPIO_INPUT);
err:
    return 0;
}

/*备驱动模块卸载函数*/
static void __exit key_exit(void) 
{
    printk("key_exit\n");
    cdev_del(&keyDev);     //从系统中删除一个cdev,完成字符设备的注销
    unregister_chrdev_region(devNo, 1);  //向系统申请,删除设备号,释放被占用的设备号
}

// 相关的几个设备驱动函数
static ssize_t key_read (struct file *filp, char __user *buf, size_t size, loff_t *offset)
{
    return size;
}

static ssize_t key_write (struct file *filp, const char __user *buf, size_t size, loff_t *offset)
{
    return 0;
}

static int key_open(struct inode *inode, struct file *filp)
{
    printk("Open driver\n");
    return 0;
}

static int key_release (struct inode *inode, struct file *filp)
{
    printk("Close driver\n");
    return 0;
}

static long key_ioctl (struct file *filp, unsigned int cmd, unsigned long param)
{
    printk("ioctl driver\n");
    return 0;
}

// 配置key_ops结构体!
static struct file_operations key_ops = {
    .owner   = THIS_MODULE,
    .read    = key_read,
    .write   = key_write,
    .open    = key_open,
    .release = key_release,
    .llseek  = key_lseek,
    .unlocked_ioctl = key_ioctl,
};

// 内核调用相关函数
module_init(key_init);
module_exit(key_exit);
  • xxx.c –> 应用层驱动封装函数
// 设备初始化
int keyInit(void)
{
    int ret;
    keyFd = open("/dev/key", O_RDWR, 0666);  // 打开相关设备文件
    if (keyFd < 0) {
        printf("Open key device faikey\n");
        return -1;
    }

    /* 执行相应的其他初始化操作 */

    return 0;
}

// 设备复位,和设备初始化相对应!
void keyDeInit(void)
{
    /* 一系列相关的反初始化操作 */
    close(keyFd);     // 关闭设备文件
}

/*
* 其他的一系列相关应用层函数
*/
  • test.c –> 测试驱动所有的应用层代码
int main(void)
{
    /*
    *   相关的具体功能逻辑实现
    */
}

C++ 模板编程编译错误

09-13

请教高手rn我在学习C++的模板,练习一个例子程序时编译总报错如下错误,不知是什么回事:rn1>Queue.cpprn1>d:\dev\mytest\opclass\queue.cpp(10) : error C2995: “void Queue::destroy(void)”: 函数模板已经定义rn1> d:\dev\mytest\include\queue.h(57) : 参见“Queue::destroy”的声明rn1>d:\dev\mytest\opclass\queue.cpp(18) : error C2995: “void Queue::pop(void)”: 函数模板已经定义rn1> d:\dev\mytest\include\queue.h(44) : 参见“Queue::pop”的声明rn1>d:\dev\mytest\opclass\queue.cpp(34) : error C2995: “void Queue::push(const Type &)”: 函数模板已经定义rn1> d:\dev\mytest\include\queue.h(43) : 参见“Queue::push”的声明rn1>d:\dev\mytest\opclass\queue.cpp(44) : error C2995: “void Queue::copy_item(const Queue &)”: 函数模板已经定义rn1> d:\dev\mytest\include\queue.h(56) : 参见“Queue::copy_item”的声明rn1>d:\dev\mytest\opclass\queue.cpp(60) : error C2995: “void Queue::print(void)”: 函数模板已经定义rn1> d:\dev\mytest\include\queue.h(50) : 参见“Queue::print”的声明rnrnrn[color=#FF0000][b]Queue.h文件:[/b][/color]rn#ifndef _QUEUE_H_rn#define _QUEUE_H_rnrntemplate class Queue;rnrntemplate rnclass QueueItemrnrn friend class Queue;rn QueueItem( const Type& t ):value(t),next(0) rn Type value;rn QueueItem* next;rn;rnrntemplate rnclass Queuernrnpublic:rn Queue():head(0),tail(0) rnrn Queue( const Queue& Q ):head(0),tail(0)rn rn copy_item(Q);rn rnrn ~Queue()rn rn destroy();rn rn rn Type& front()rn rn return head->value;rn rnrn const Type& front() constrn rn return head->value;rn rnrn void push( const Type& );rn void pop();rn bool empty() constrn rn return head == 0;rn rnrn void print();rnrnprotected:rnprivate:rn QueueItem* head;rn QueueItem* tail;rn void copy_item( const Queue& );rn void destroy();rn;rn#include "Queue.cpp"rn#endifrnrnrn[color=#FF0000][b]Queue.cpp文件:[/b][/color]rn#include "Queue.h"rnrntemplate rnvoid Queue::destroy()rnrn while ( !empty() )rn rn pop();rn rnrnrntemplate rnvoid Queue::pop()rnrn QueueItem *ptr = head;rn head = head->next;rn delete ptr;rnrnrntemplate rnvoid Queue::push( const Type& t )rnrn QueueItem *ptr = new QueueItem(t);rnrn if ( empty() )rn rn head = tail = ptr;rn rn elsern rn tail->next = ptr;rn tail = tail->next;rn rnrnrntemplate rnvoid Queue::copy_item( const Queue& Q )rnrn QueueItem *ptr = Q.head;rn for ( ; ptr ; ptr++ )rn rn push( ptr->value );rn rnrnrntemplate rnvoid Queue::print()rnrn while( !empty() )rn rn cout << head->value << endl;rn rnrn

模板编程初学者的问题

11-24

想写一个类似于list的链表,编译运行都没有问题,可是排序那个地方有问题,没有起到排序的效果,帮帮忙看一下:代码有点长,但是还是很清晰的rn测试用的类Arnclass Arnrnpublic:rnA(int i=0)rnrn m_int=i;rnrnint GetMember() constrnrn return m_int;rnrnvoid SetMember()(int i)rnrn m_int=i;rnrnbool operator >=(const A& c)rnrn return m_int >= c.GetMember();rnrnbool operator <= (const A& c)rnrn return m_int <= c.GetMember();rnrn rnprotected:rnint m_int;rn;rnrntemplate class List;rnrntemplaternclass ListNodernrn friend class List;rnpublic:rn ListNode();rn ListNode(const NodeType& node);rn void operator = (NodeType& newnode);rn NodeType GetData()const;rnprivate:rn NodeType m_data;rn ListNode* next;rn;rnrntemplaternListNode::ListNode():next(NULL)rnrnrntemplaternListNode::ListNode(const NodeType& node):m_data(node),next(NULL)rnrnrntemplaternNodeType ListNode::GetData()constrnrn return m_data;rnrntemplaternvoid ListNode::operator =(NodeType& newnode)//问题处在这里??rnrn m_data=newnode.GetData();rn if(NULL!=next)rn delete next;rn memcpy(next,&newnode,sizeof(NodeType));rnrntemplaternclass Listrnrnpublic:rn List();rn ~List();rn void AddFromHead(const NodeType& node);rn void AddFromTail(const NodeType& node);rn bool DeleteFromHead();rn bool DeleteFromTail();rn void PrintElement();rn void Sort(bool b=true);rnprivate:rn bool IsEmpty();rn int count;rn ListNode* first;rn ListNode* last;rn ListNode* GetNewElement(const NodeType& node);rn;rnrntemplaternList::List():first(NULL),last(NULL)rnrn count=0;rnrntemplaternList::~List()rnrn if(!IsEmpty())rn rn ListNode* temp1=first;rn ListNode* temp2;rn for(int i=0;inext;rn delete temp2; rn rn cout<<"Destructor Over"<rnvoid List::AddFromHead(const NodeType& node)rnrn ListNode* temp=GetNewElement(node);rn if(IsEmpty())rn rn first=last=temp;rn rn elsern rn temp->next=first;rn first=temp;rn rn ++count;rnrntemplaternvoid List::AddFromTail(const NodeType& node)rnrn ListNode* temp=GetNewElement(node);rn if(IsEmpty())rn rn first=last=temp;rn rn elsern rn temp->next=last;rn last=temp;rn rn ++count;rnrntemplaternbool List::DeleteFromHead()rnrn if(IsEmpty())rn rn cout<<"List is Empty"<* temp;rn temp=first->next;rn delete first;rn first=temp;rn rn --count;rn return true;rn rnrntemplaternbool List::DeleteFromTail()rnrn if(IsEmpty())rn rn cout<<"List is Empty"<* temp;rn temp=last->next;rn delete last;rn last=temp;rn rn --count;rn return true;rn rnrntemplaternvoid List::Sort(bool b)//或者问题应该处在这里rnrn ListNode* temp1=first;rn ListNode* temp2;rn int i,j;rn if(b)rn rn for(i=0;iGetData() >= temp1->next->GetData())rn rn temp2=temp1;rn temp1=temp1->next;rn temp1->next=temp2;rn rn temp1=temp1->next;rn rn rn rn elsern rn for(i=0;iGetData() <= temp1->next->GetData())rn rn temp2=temp1;rn temp1=temp1->next;rn temp1->next=temp2;rn rn rnrntemplaternListNode* List::GetNewElement(const NodeType& node)rnrn ListNode* temp=new ListNode(node);rn return temp;rnrntemplaternbool List::IsEmpty()rnrn return first==NULL;rnrntemplaternvoid List::PrintElement()rnrn if(IsEmpty())rn rn cout<<"List is Empty"<* temp=first;rn while(km_data;rn temp=temp->next;rn ++k;rn rn rnrntemplaternvoid TestList(List& list1,const char* type)rnrn T t;rn if(type=="A")rn rn cout<<"now A list is initializing:"<list1;rn TestList(list1,"A");rn cout<<"Before sort A list:"<

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试