Advertisement
Guest User

Untitled

a guest
Feb 19th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.15 KB | None | 0 0
  1. #include <linux/init.h>
  2. #include <linux/module.h>
  3. #include <linux/kernel.h>
  4. #include <linux/gpio.h>
  5. #include <linux/kthread.h>
  6. #include <linux/delay.h>
  7.  
  8. MODULE_LICENSE("GPL");
  9.  
  10. #define LED_ON 1
  11. #define LED_OFF 0
  12. #define DELAY_TIME 1000
  13.  
  14. static unsigned int gpioLED = 17;
  15. static bool ledOn = 0;
  16. static struct task_struct *task;
  17.  
  18. static int flash(void *arg){
  19.     while(!kthread_should_stop()){
  20.         set_current_state(TASK_RUNNING);
  21.         ledOn = !ledOn;
  22.         set_current_state(TASK_INTERRUPTIBLE);
  23.         msleep(DELAY_TIME);
  24.     }
  25.     return 0;
  26. }
  27.  
  28. static int __init gpio_blink_init(void){
  29.     if (!gpio_is_valid(gpioLED)){
  30.         printk(KERN_INFO "GPIO_TEST: invalid LED GPIO\n");
  31.         return -ENODEV;
  32.     }
  33.  
  34.     gpio_request(gpioLED, "stuff");
  35.     gpio_direction_output(gpioLED, LED_OFF);
  36.     gpio_export(gpioLED, false);
  37.  
  38.     task = kthread_run(flash, NULL, "LED_flash_thread");
  39.     return 0;
  40. }
  41.  
  42. static void __exit gpio_blink_exit(void){
  43.     gpio_set_value(gpioLED, LED_OFF);
  44.     gpio_unexport(gpioLED);
  45.     gpio_free(gpioLED);
  46.     kthread_stop(task);
  47. }
  48.  
  49. module_init(gpio_blink_init);
  50. module_exit(gpio_blink_exit);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement