Guest User

Untitled

a guest
Sep 12th, 2015
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.59 KB | None | 0 0
  1. #include <linux/interrupt.h>
  2. #include <linux/err.h>
  3. #include <linux/irq.h>
  4. #include <linux/clk.h>
  5. #include <linux/list.h>
  6. #include <linux/kthread.h>
  7. #include <linux/init_task.h>
  8. #include <linux/sched.h>
  9. #include <linux/rtmutex.h>
  10. #include <linux/hrtimer.h>
  11. #include <linux/delay.h>
  12. #include <linux/gpio.h>
  13.  
  14. MODULE_LICENSE( "GPL" );
  15. MODULE_AUTHOR( "Anon <[email protected]>" );
  16.  
  17. static struct task_struct *thread_10ms;
  18.  
  19. static int bus_rt_timer_thread(void *arg) {
  20.     bool led_state = false;
  21.     if (gpio_request(62, "LED"))
  22.     {
  23.         printk( "+ error requesting LED GPIO" );
  24.         return -1;
  25.     }
  26.     gpio_direction_output(62, 0);
  27.     ktime_t timeout = ktime_get();
  28.     while(!kthread_should_stop()) {
  29.         led_state = !led_state;
  30.         gpio_set_value(62, led_state);
  31.  
  32.         timeout = ktime_add_us(timeout, 250000);
  33.         __set_current_state(TASK_UNINTERRUPTIBLE);
  34.         schedule_hrtimeout_range(&timeout, 100, HRTIMER_MODE_ABS);
  35.     }
  36.     gpio_direction_output(62, 0);
  37.     gpio_free(62);
  38.     return 0;  
  39. }
  40.  
  41. int __init bus_timer_interrupt_init(void) {
  42.     struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 };
  43.    
  44.     thread_10ms = kthread_create(bus_rt_timer_thread, NULL, "bus_10ms");
  45.     if (IS_ERR(thread_10ms)) {
  46.         printk(KERN_ERR "RT Failed to create RT thread\n");
  47.         return -ESRCH;
  48.     }
  49.     sched_setscheduler(thread_10ms, SCHED_FIFO, &param);
  50.     wake_up_process(thread_10ms);
  51.     return 0;
  52. }
  53.  
  54. void __exit bus_timer_interrupt_exit(void) {
  55.     kthread_stop(thread_10ms);
  56.     printk(KERN_INFO "RT Thread removed.\n");
  57. }
  58.  
  59. module_init( bus_timer_interrupt_init );
  60. module_exit( bus_timer_interrupt_exit );
Advertisement
Add Comment
Please, Sign In to add comment