Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Periodic task in a Linux kernel module

Currently I am developing GPIO kernel module for friendlyarm Linux 2.6.32.2 (mini2440). I am from electronics background and new to Linux.

The kernel module loaded at start-up and the related device file is located in /dev as gpiofreq.

At first time writing to device file, GPIO pin toggles continuously at 50kHz. At second time writing it stop toggling. At third time, it starts again, and so on.

I have wrote separate kernel module to generate freq. but CPU freezes after writing device file at first time. The terminal prompt is shown but I can not run any command afterwards.

Here is the code-snippet:

//calling function which generates continuous freq at gpio

static int send_freq(void *arg)
{
    set_current_state(TASK_INTERRUPTIBLE);
    for(;;)
    {
        gpio_set_value(192,1);
        udelay(10);
        gpio_set_value(192,0);
        udelay(10);
    }
    return 0;
}

Here is the device write code, which start or stop with any data written to device file.

if(toggle==0)
{
       printk("Starting Freq.\n");
       task=kthread_run(&send_freq,(void *)freq,"START");
       toggle=1;
}
else
{
       printk("Operation Terminated.\n");
       i = kthread_stop(task);
       toggle=0;
}
like image 533
Jaydeep Dhrangdhariya Avatar asked Nov 29 '11 11:11

Jaydeep Dhrangdhariya


People also ask

How do Linux kernel modules work?

Linux Kernel Modules. Kernel modules are pieces of code that can be loaded and unloaded into the kernel upon demand. They extend the functionality of the kernel without the need to reboot the system. A module can be configured as built-in or loadable.

What are the components of kernel module?

A kernel module that supports one or more hardware components. There are two driver models: the monolithic driver model and the class/port driver model. A signal from a hardware component that eventually causes the interrupt handler in the appropriate driver to be called.

What is a Linux kernel module and how do you load a new module?

A kernel module is a program which can loaded into or unloaded from the kernel upon demand, without necessarily recompiling it (the kernel) or rebooting the system, and is intended to enhance the functionality of the kernel.

What is kernel explain the task it performs?

The kernel performs its tasks, such as running processes, managing hardware devices such as the hard disk, and handling interrupts, in this protected kernel space. In contrast, application programs such as browsers, word processors, or audio or video players use a separate area of memory, user space.


1 Answers

You are doing an infinite loop in a kernel thread, there is no room for anything else to happen, except IRQ and maybe other kernel thread.

What you could do is either

  • program a timer on your hardware and do your pin toggling in an interrupt

  • replace udelay with usleep_range

I suggest doing thing progressively, and starting in the kHz range with usleep_range, and eventually moving to cust om timer + ISR

in either case, you will probably have a lot of jitter, and doing such gpio toggling may be a good idea on a DSP or a PIC, but is a waste of resources on ARM + Linux, unless you are hardware assisted with pwm capable gpio engine.

like image 183
shodanex Avatar answered Sep 20 '22 19:09

shodanex