这里同时支持4个LED
和前面不同之处只是在open/write函数稍作修改即可
static int led_open (struct inode *inode, struct file *file)
{
printk(KERN_INFO "drv open \n");
/* set GPM4_0 , GPM4_1, GPM4_2, GPM4_3 as output */
led_gpio->con |= (1 << 0*4) + (1<<1*4) + (1<<2*4) + (1<<3*4);
return 0;
}
static ssize_t led_write (struct file *file, const char __user *usrbuf, size_t len, loff_t *off)
{
int i = 0;
int cmd,opt;
int rev_buf[8] = {0};
printk(KERN_INFO "drv write \n");
if(copy_from_user(rev_buf,usrbuf,8))
return -EFAULT;
printk(KERN_INFO "data ");
for(i = 0; i < 8;i++)
{
printk(KERN_INFO "%d ", rev_buf[i]);
}
printk(KERN_INFO "\n");
cmd = rev_buf[0];
opt = rev_buf[1];
printk(KERN_NOTICE "cmd : %d opt : %d \n", cmd, opt);
if(cmd == 0)
{
// off
led_gpio->data |= (1<<opt);
}
else if(cmd == 1)
{
// on
led_gpio->data &= ~(1<<opt);
}
else
{
// do nothing.
}
return 0;
}
为什么因此要分开写呢,主要是这里有个比较奇怪的问题,仔细看write函数usrbuf是char, 而使用copy_from_user拷贝后的rev_buf是int型。
开始rev_buf我使用的char型,发现测试工具发下来的参数都收不到, 导致rev_buf一直为0.改成int后可以正常控制。
表示费解???