乙個LED驅動的編寫

2021-08-02 23:34:46 字數 2569 閱讀 2557

#include 

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#define gpm4con 0x110002e0

volatile

unsigned

long *baseaddr = 0;

//位址必須要 volatile 修飾,volatile 是防止優化,就是避免某次計算中間值存放在暫存器中沒有存入flash,這會導致以後讀取的值不是想要的

#define rgpm4con (*((volatile unsigned long *)(baseaddr + 0)))

#define rgpm4dat (*((volatile unsigned long *)(baseaddr + 1)))

#define myled_major 0

#define myled_name "led"

#define myled_drvnum 1

#define myled_num 4

dev_t myled_devt; //裝置號

int myled_major;

struct

class *myled_class; //裝置結構體

struct cdev *myled; //裝置驅動核心結構

char led_status[myled_num]=;

loff_t led_lseek (struct file *fp, loff_t off, int whence)

if(newoff<0)

newoff = 0;

else

if(newoff>4)

newoff = 4;

fp->f_pos=newoff;

return newoff;

}ssize_t led_read (struct file *fp, char __user *buf, size_t size, loff_t *off)

ssize_t led_write (struct file *fp, const

char __user *buf, size_t size, loff_t *off)

*off = *off + size;

printk("led_write is called \n");

return0;}

int led_open (struct inode *node, struct file *fp)

int led_close (struct inode *node, struct file *fp)

struct file_operations led_fops=

;static

int __init myled_init(void) //形參不能為空

myled = cdev_alloc(); //分配裝置空間

/* 這裡多說兩句,每乙個裝置都是乙個結構體,這個結構體包括

struct kobject kobj; の,忘了幹嘛的

struct module *owner; 所屬的模組

const struct file_operations *ops; 所使用的檔案操作函式

struct list_head list; 鍊錶,向內和聯表新增

dev_t dev; 裝置號

unsigned int count; 裝置數目

*/cdev_init(myled,&led_fops); //初始化裝置

/* 這個初始化這是把檔案函式結構體新增到裝置結構體

*/if(cdev_add(myled,myled_devt,myled_drvnum)) //新增裝置空間

printk("註冊成功\n");

myled_class = class_create(this_module,myled_name); //產生乙個裝置類

for(i=0;i//新增裝置類進核心

device_create(myled_class,null,myled_devt+i,null,"myled%d",i);

baseaddr = ioremap(gpm4con, 8); //硬體位址對映到虛擬位址

return0;}

static

void __exit myled_exit(void) //形參不能為空,必須有個void

module_init(myled_init);

module_exit(myled_exit);

module_license("gpl");

驅動篇 乙個簡單的led驅動

1.構造裝置結構體 struct light dev cdev結構體 struct cdev 2.設定裝置資訊 struct light dev light devp 設定裝置結構體變數 int light major light major 設定主裝置號3.設定並填充file operations...

LED燈驅動的編寫

led燈驅動的編寫 一 原理圖 led2 gpx2 7 led3 gpx1 0 led4 gpf3 4 led5 gpf3 5 輸出 0滅 1亮 二 晶元手冊 gpx2con 0x11000c40 31 28 0x1 gpx2dat 0x11000c44 7 0 1 gpx1con 0x11000c...

乙個簡單的LED驅動 不基於驅動框架

驅動 include include include include include include include static int led major 1 static struct class led mark class null static struct device led mar...