我一直在使用超声波传感器进行障碍物检测。计划是比较用户空间的响应时间和可加载的内核模块。 用户空间程序工作正常。但是当我插入LKM时,内核会冻结。我认为它是因为while循环但不知道如何处理它。有人能告诉我这个问题吗?
提前谢谢你。
#include<linux/module.h>
#include<linux/kernel.h>
#include<linux/time.h>
#include<linux/timer.h>
#include<linux/jiffies.h>
#include<linux/delay.h>
#include<linux/gpio.h>
#include<asm/div64.h>
# define LED 21
# define TRIG 11
# define ECHO 8
static int ultra_init(void){
int ret=0,flag;
unsigned long long delta, obj_time_start,distance ;
unsigned long long speed=34;
struct timeval start,end;
//s64 delta;
printk(KERN_ALERT "Module loaded");
ret=gpio_request_one(TRIG,GPIOF_OUT_INIT_LOW,"write_pin");
if(ret)
{
printk(KERN_ALERT "Pin %d could not be assigned",ret); //Priniting message in log file
flag=1;
}
ret=gpio_request_one(LED,GPIOF_OUT_INIT_LOW,"write_pin");
if(ret)
{
printk(KERN_ALERT "Pin %d could not be assigned",ret); //Priniting message in log file
flag=1;
}
ret=gpio_request_one(ECHO,GPIOF_IN,"read_pin"); //Defining GPIO 4 as an input
if(ret)
{
printk(KERN_ALERT "Pin %d could not be assigned",ret);
flag=1;
}
int i;
for(i=1;i<5;i++){
gpio_set_value(LED,0);
gpio_set_value(TRIG,0);
gpio_set_value(ECHO,0);
udelay(200);
gpio_set_value(TRIG,1);
udelay(10) ;// us
gpio_set_value(TRIG,0);
printk(KERN_ALERT "measuring the distance......");
printk(KERN_ALERT " ECHO STATE %d",gpio_get_value(ECHO));
udelay(300);
udelay(300);
udelay(300);
udelay(300);
udelay(300);
while(gpio_get_value(ECHO)==0){
do_gettimeofday(&start);
}
while(gpio_get_value(ECHO)==1){
printk(KERN_ALERT "ECHO 1");
do_gettimeofday(&end);}
obj_time_start=ktime_to_ns(ktime_get()); // Response time
//delta=ktime_to_ns(ktime_sub(end,start)); //ns
delta=(end.tv_sec-start.tv_sec)*1000.0;
delta+=(end.tv_usec-start.tv_usec)/1000.0;
//long long num=delta*17;
//printk(KERN_ALERT "Distance calculated is %llu cm \n ", delta);
do_div(speed,2000000);
printk(KERN_ALERT " %llu ",speed);
//distance=delta*0.000017; //do_div((delta*17),1000000);
//printk(KERN_ALERT "Distance calculated is %llu cm \n ", distance);
}
}
static void ultra_exit(void){
printk(KERN_ALERT "Good byee");
gpio_set_value(LED,0);
gpio_free(LED);
}
MODULE_LICENSE("GPL");
module_init(ultra_init);
module_exit(ultra_exit);