完成数码管和按键驱动后,继续驱动点东西吧。有点忘了platform形式驱动咋写了,网上又学习了一下大神的讲解
https://blog.csdn.net/fml1997/article/details/69659198
那我就开始移植过来吧。
硬件跳帽接法如下图,
驱动代码直接贴了
drv-leds.c
#include "drv-leds.h"
#define DRV_AUTHOR "rambo "
#define DRV_DESC "AP-283Demo LED driver"
/* Driver version*/
#define DRV_MAJOR_VER 1
#define DRV_MINOR_VER 0
#define DRV_REVER_VER 0
#define DEV_NAME DEV_LED_NAME
#ifndef DEV_MAJOR
#define DEV_MAJOR 0 /* dynamic major by default */
#endif
#define TIMER_TIMEOUT 40
static int debug = DISABLE;
static int dev_major = DEV_MAJOR;
static int dev_minor = 0;
/* ============================ Platform Device part ===============================*/
/*
LED 每个硬件参数
1.LED编号
2.LED使用对应的io引脚
3.LED使能方式
4.LED状态
5.LED闪烁
*/
struct imx28x_led_info
{
unsigned char num; /* The LED number */
unsigned char name[10];
unsigned int gpio; /* Which GPIO the LED used */
unsigned char active_level; /* The GPIO pin level(HIGHLEVEL or LOWLEVEL) to turn on or off */
unsigned char status; /* Current LED status: OFF/ON */
unsigned char blink; /* Blink or not */
};
/*
所有LED 平台参数
1.指向每个LED指针
2.总共LED数量
*/
struct imx28x_led_platform_data
{
struct imx28x_led_info *leds;
int nleds;
};
/*
初始化所有LED的硬件参数
*/
static struct imx28x_led_info imx28x_leds[] = {
[0] = {
.num = 1,
.name = "LED1",
.gpio = MXS_PIN_TO_GPIO(PINID_SAIF1_SDATA0),
.active_level = LOWLEVEL,
.status = OFF,
.blink = ENABLE,
},
[1] = {
.num = 2,
.name = "LED2",
.gpio = MXS_PIN_TO_GPIO(PINID_SAIF0_MCLK),
.active_level = LOWLEVEL,
.status = OFF,
.blink = DISABLE,
},
[2] = {
.num = 3,
.name = "LED3",
.gpio = MXS_PIN_TO_GPIO(PINID_LCD_D17),
.active_level = LOWLEVEL,
.status = OFF,
.blink = DISABLE,
},
[3] = {
.num = 4,
.name = "LED4",
.gpio = MXS_PIN_TO_GPIO(PINID_SSP0_DATA7),
.active_level = LOWLEVEL,
.status = OFF,
.blink = DISABLE,
},
};
/*
初始化LED 平台参数
*/
static struct imx28x_led_platform_data imx28x_led_data = {
.leds = imx28x_leds, //LED灯的每条信息
.nleds = ARRAY_SIZE(imx28x_leds), //灯数
};
/*
内核注册结构体,与硬件无关
*/
struct led_device
{
struct imx28x_led_platform_data *data;
struct cdev cdev;
struct class *dev_class;
struct timer_list blink_timer;
} led_device;
static void platform_led_release(struct device * dev)
{
int i;
struct imx28x_led_platform_data *pdata = dev->platform_data;
printk("%s():%d\n", __FUNCTION__,__LINE__);
/* Turn all LED off */
for(i=0; inleds; i++){ //rambo
// s3c2410_gpio_setpin(pdata->leds[i].gpio, ~pdata->leds[i].active_level);
}
}
static struct platform_device imx28x_led_device = { //定义设备结构体
.name = "imx28x_leds",
.id = 1,
.dev =
{
.platform_data = &imx28x_led_data,
.release = platform_led_release,
},
};
/* ===================== led device driver part ===========================*/
void led_timer_handler(unsigned long data)
{
int i;
struct imx28x_led_platform_data *pdata = (struct imx28x_led_platform_data *)data;
for(i=0; inleds; i++)
{
if(ON == pdata->leds[i].status) //rambo
{
gpio_set_value(pdata->leds[i].gpio, pdata->leds[i].active_level);
}
else
{
gpio_set_value(pdata->leds[i].gpio, ~pdata->leds[i].active_level);
}
if(ENABLE == pdata->leds[i].blink ) /* LED should blink */
{
/* Switch status between 0 and 1 to turn LED ON or off */
pdata->leds[i].status = pdata->leds[i].status ^ 0x01;
}
mod_timer(&(led_device.blink_timer), jiffies + TIMER_TIMEOUT); //闪烁时间设置
}
}
static int led_open(struct inode *inode, struct file *file) //LED开启函数
{
struct led_device *pdev ;
struct imx28x_led_platform_data *pdata;
pdev = container_of(inode->i_cdev,struct led_device, cdev);
pdata = pdev->data;
file->private_data = pdata;
return 0;
}
static int led_release(struct inode *inode, struct file *file) //关闭led
{
return 0;
}
static long led_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
struct imx28x_led_platform_data *pdata = file->private_data;
switch (cmd)
{
case LED_ON:
if(pdata->nleds <= arg)
{
printk("LED_ON: LED%ld doesn't exist\n", arg);
return -ENOTTY;
}
pdata->leds[arg].status = ON;
pdata->leds[arg].blink = DISABLE;
break;
case LED_OFF:
if(pdata->nleds <= arg)
{
printk("LED_OFF: LED%ld doesn't exist\n", arg);
return -ENOTTY;
}
pdata->leds[arg].status = OFF;
pdata->leds[arg].blink = DISABLE;
break;
case LED_BLINK:
if(pdata->nleds <= arg)
{
printk("LED_BLINK: LED%ld doesn't exist\n", arg);
return -ENOTTY;
}
pdata->leds[arg].blink = ENABLE;
pdata->leds[arg].status = ON;
break;
default:
printk("%s driver don't support ioctl command=%d\n", DEV_NAME, cmd);
//print_led_help();
return -EINVAL;
}
return 0;
}
static struct file_operations led_fops = {
.owner = THIS_MODULE,
.open = led_open,
.release = led_release,
.unlocked_ioctl = led_ioctl, /* compatible with kernel version >=2.6.38*/
};
//注册设备驱动
static int imx28x_led_probe(struct platform_device *dev)
{
struct imx28x_led_platform_data *pdata = dev->dev.platform_data;
int result = 0;
int i;
dev_t devno;
for(i=0; inleds; i++)
{ //rambo1992
gpio_free(pdata->leds[i].gpio);
result = gpio_request(pdata->leds[i].gpio, pdata->leds[i].name);
if (result != 0) {
printk("request %s failed \n",pdata->leds[i].name);
return -EBUSY;
}
if(ON == pdata->leds[i].status){
gpio_direction_output(pdata->leds[i].gpio, pdata->leds[i].active_level );
printk("%s value [%d] ON\n",pdata->leds[i].name,pdata->leds[i].active_level);
}else{
gpio_direction_output(pdata->leds[i].gpio, pdata->leds[i].active_level ^ 0x01);
printk("%s value [%d] OFF\n",pdata->leds[i].name,pdata->leds[i].active_level ^ 0x01);
}
}
if (0 != dev_major)
{
devno = MKDEV(dev_major, dev_minor);
result = register_chrdev_region(devno, 1, DEV_NAME);
}
else //动态分配主次设备号
{
result = alloc_chrdev_region(&devno, dev_minor, 1, DEV_NAME);
dev_major = MAJOR(devno);
}
if (result < 0)
{
printk("%s driver can't get major %d\n", DEV_NAME, dev_major);
return result;
}
printk("%s driver get major %d\n", DEV_NAME, dev_major);
memset(&led_device, 0, sizeof(led_device));
led_device.data = dev->dev.platform_data;
cdev_init (&(led_device.cdev), &led_fops);
led_device.cdev.owner = THIS_MODULE;
result = cdev_add (&(led_device.cdev), devno , 1);
if (result){
printk (KERN_NOTICE "error %d add %s device", result, DEV_NAME);
goto ERROR;
}
led_device.dev_class = class_create(THIS_MODULE, DEV_NAME);
if(IS_ERR(led_device.dev_class)){
printk("%s driver create class failture\n",DEV_NAME);
result = -ENOMEM;
goto ERROR;
}
device_create(led_device.dev_class, NULL, MKDEV(dev_major,0), NULL, DEV_NAME);
/* Initial the LED blink timer 初始化闪烁定时器 */
init_timer(&(led_device.blink_timer));
led_device.blink_timer.function = led_timer_handler;
led_device.blink_timer.data = (unsigned long)pdata;
led_device.blink_timer.expires = jiffies + TIMER_TIMEOUT;//jiffies为当前时间
add_timer(&(led_device.blink_timer));
printk("imx28x %s driver version %d.%d.%d initiliazed.\n", DEV_NAME, DRV_MAJOR_VER, DRV_MINOR_VER, DRV_REVER_VER);
return 0;
ERROR:
printk("imx28x %s driver version %d.%d.%d install failure.\n", DEV_NAME, DRV_MAJOR_VER, DRV_MINOR_VER, DRV_REVER_VER);
cdev_del(&(led_device.cdev));
unregister_chrdev_region(devno, 1);
return result;
}
//卸载驱动
static int imx28x_led_remove(struct platform_device *dev) //释放主次设备号给Linux内核
{
dev_t devno = MKDEV(dev_major, dev_minor);
del_timer(&(led_device.blink_timer));
cdev_del(&(led_device.cdev));
//for()
device_destroy(led_device.dev_class, devno);
class_destroy(led_device.dev_class);
unregister_chrdev_region(devno, 1);
printk("imx28x %s driver removed\n", DEV_NAME);
return 0;
}
static struct platform_driver imx28x_led_driver = {
.probe = imx28x_led_probe,
.remove = imx28x_led_remove,
.driver = {
.name = "imx28x_leds",
.owner = THIS_MODULE,
},
};
static int __init imx28x_led_init(void)
{
int ret = 0;
//注册LED设备
ret = platform_device_register(&imx28x_led_device);
if(ret){
printk(KERN_ERR "%s:%d: Can't register platform device %d\n", __FUNCTION__,__LINE__, ret);
goto fail_reg_plat_dev;
}
printk("Regist imx28x LED Platform Device successfully.\n");
//注册LED设备驱动
ret = platform_driver_register(&imx28x_led_driver);
if(ret){
printk(KERN_ERR "%s:%d: Can't register platform driver %d\n", __FUNCTION__,__LINE__, ret);
goto fail_reg_plat_drv;
}
printk("Regist imx28x LED Platform Driver successfully.\n");
return 0;
fail_reg_plat_drv:
platform_driver_unregister(&imx28x_led_driver);
fail_reg_plat_dev:
return ret;
}
static void imx28x_led_exit(void)
{
//卸载LED设备驱动
printk("%s():%d remove LED platform drvier\n", __FUNCTION__,__LINE__);
platform_driver_unregister(&imx28x_led_driver);
//卸载LED设备
printk("%s():%d remove LED platform device\n", __FUNCTION__,__LINE__);
platform_device_unregister(&imx28x_led_device);
}
module_init(imx28x_led_init);
module_exit(imx28x_led_exit);
drv-leds.h
#ifndef __IMX287_DRIVER_H
#define __IMX287_DRIVER_H
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include /* For sys_access*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "mach/../../mx28_pins.h"
#include
#include "mach/mx28.h"
#include
#include
#include
#include
#define LED_ON 1
#define LED_OFF 2
#define LED_BLINK 3
#define ENPULLUP 1
#define DISPULLUP 0
#define HIGHLEVEL 1
#define LOWLEVEL 0
#define INPUT 1
#define OUTPUT 0
#define OFF 0
#define ON 1
#define ENABLE 1
#define DISABLE 0
#define TRUE 1
#define FALSE 0
#define DEV_LED_NAME "rambo-led"
#define DEV_LED_MAJOR 203
#endif
测试代码
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LED_OFF 2
#define LED_ON 1
#define LED_BLINK 3
#define LED1 0
#define LED2 1
#define LED3 2
#define LED4 3
int main (int argc,char **argv)
{
int fd;
fd=open("/dev/rambo-led",O_RDWR);
ioctl(fd,LED_ON,LED1);
ioctl(fd,LED_BLINK,LED2);
ioctl(fd,LED_ON,LED3);
ioctl(fd,LED_BLINK,LED4);
close(fd);
return 0;
}