title: Periodic task in a Linux kernel module
body: <p>Currently I am developing GPIO kernel module for friendlyarm Linux 2.6.32.2 (mini2440). I am from electronics background and new to Linux.</p>
  
  <p>The kernel module loaded at start-up and the related device file is located in <code>/dev</code> as <code>gpiofreq</code>.</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>Here is the code-snippet:</p>
  
  <pre class="lang-c prettyprint-override"><code>//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;
  }
  </code></pre>
  
  <p>Here is the device write code,
  which start or stop with any data written to device file.</p>
  
  <pre class="lang-c prettyprint-override"><code>if(toggle==0)
  {
         printk("Starting Freq.\n");
         task=kthread_run(&amp;send_freq,(void *)freq,"START");
         toggle=1;
  }
  else
  {
         printk("Operation Terminated.\n");
         i = kthread_stop(task);
         toggle=0;
  }
  </code></pre>