2 caoai3033 caoai3033 于 2016.09.07 18:59 提问

linux gpio 通过创建节点控制led,echo问题

/*timer handle*/
static void change_status(unsigned long time)
{
printk(KERN_INFO "---------handle---------------\nWangyf: Enter timer handle!\n");
printk(KERN_INFO "Wangyf: gpio_get_value = %d\n",gpio_get_value(GPIO_NUM));

if(gpio_get_value(GPIO_NUM) == 1) {
    printk(KERN_INFO "Wangyf: led down!\n");
    gpio_set_value(GPIO_NUM,0);
}
else if(gpio_get_value(GPIO_NUM) == 0) {
    printk(KERN_INFO "Wangyf: led up!\n");
    gpio_set_value(GPIO_NUM,1);
}

if(blink_flag){
    printk(KERN_INFO "Wangyf: Add timer again!\n----------------------------\n");
    my_timer.expires = jiffies + HZ;                                                                                                                                                                     
    add_timer(&my_timer);
}   
return;

}


/*set blink*/
static ssize_t blink_gpio(struct device *dev,
struct device_attribute *attr,
char *buf,size_t size)
{
int blink_status;

printk(KERN_INFO "Wangyf: blink get order from echo!\n");                                                                                                                                                

sscanf(buf,"%du",&blink_status);

printk(KERN_INFO "Wangyf: blink_status = %d\n",blink_status);

if(blink_status){
    printk(KERN_INFO "Wangyf: Open blink!\n");
    if(!blink_flag){
        my_timer.expires = jiffies + HZ; 
        add_timer(&my_timer);
    }   
    blink_flag = true;
} else {
    printk(KERN_INFO "Wangyf: Close blink!\n");
    del_timer(&my_timer);
    blink_flag = false;
}
return blink_status;

}


DEVICE_ATTR(blink,0666,NULL,blink_gpio);

static struct attribute *my_led_attribute[] = {
&dev_attr_blink.attr,
NULL
};


生成blink文件节点,使用命令
echo 1 > blink
灯会闪烁,但是会有输出:
bash: echo: write error: Success

使用命令
echo 0 > blink
会进入不停的循环,dmesg结果:

[ 2309.131709] Wangyf: blink get order from echo!
[ 2309.131715] Wangyf: blink_status = 0
[ 2309.131720] Wangyf: Close blink!
[ 2309.131726] Wangyf: blink get order from echo!
[ 2309.131732] Wangyf: blink_status = 0
[ 2309.131737] Wangyf: Close blink!
[ 2309.131744] Wangyf: blink get order from echo!
[ 2309.131750] Wangyf: blink_status = 0
[ 2309.131755] Wangyf: Close blink!
[ 2309.131762] Wangyf: blink get order from echo!
[ 2309.131768] Wangyf: blink_status = 0
[ 2309.131774] Wangyf: Close blink!
[ 2309.131781] Wangyf: blink get order from echo!
[ 2309.131786] Wangyf: blink_status = 0
[ 2309.131791] Wangyf: Close blink!
[ 2309.131797] Wangyf: blink get order from echo!
[ 2309.131802] Wangyf: blink_status = 0
[ 2309.131806] Wangyf: Close blink!
[ 2309.131811] Wangyf: blink get order from echo!
[ 2309.131816] Wangyf: blink_status = 0
[ 2309.131820] Wangyf: Close blink!
[ 2309.131826] Wangyf: blink get order from echo!
[ 2309.131830] Wangyf: blink_status = 0


probe函数中:
init_timer(&my_timer);
my_timer.function = change_status;
my_timer.expires = jiffies + HZ;

如果设置 2 为关闭blink就正常,这是为什么呢?

1个回答

devmiao
devmiao   Ds   Rxr 2016.09.07 23:02
caoai3033
caoai3033 谢谢!不过已经弄清楚了,由于疏忽DEVICE_ATTR中的_show和_store函数,也就是我程序中的blink_gpio函数的返回值应该是size,当返回值是blink_status时,echo 1是正常的,但是echo 0就会返回0,就会一直循环读取指令。这一点还没有具体弄清楚为什么,推测是因为系统会认为返回0是还未读取到消息。
一年多之前 回复
caoai3033
caoai3033 谢谢!不过已经弄清楚了,由于疏忽DEVICE
一年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!