Lichee Pi Zero >> C/C++

LED


電路圖


main.c

#include <linux/init.h>
#include <linux/device.h>
#include <linux/module.h>
#include <linux/delay.h>
#include <linux/timer.h>
#include <linux/gpio.h>
 
MODULE_AUTHOR("Steward_Fu");
MODULE_DESCRIPTION("LED driver for Lichee Pi Zero");
MODULE_LICENSE("GPL");

#define LED_G ((32 * 6) + 0)
#define LED_B ((32 * 6) + 1)
#define LED_R ((32 * 6) + 2)
 
static int g_blink_period=300;
static struct timer_list g_blink_timer;
 
void blink_handler(unsigned long unused)
{
  static int index=0;

  gpio_set_value(LED_R, 1);
  gpio_set_value(LED_G, 1);
  gpio_set_value(LED_B, 1);
  switch(index){
  case 0:
    gpio_set_value(LED_R, 0);
    break;
  case 1:
    gpio_set_value(LED_G, 0);
    break;
  case 2:
    gpio_set_value(LED_B, 0);
    break;
  default:
    break;
  }
  index+= 1;
  if(index > 3){
    index = 0;
  }
  mod_timer(&g_blink_timer, jiffies + msecs_to_jiffies(g_blink_period));
}
 
static int __init main_init(void)
{
  printk("LED driver for Lichee Pi Zero\n");

  gpio_request(LED_R, "led_r"); gpio_direction_output(LED_R, 1); gpio_set_value(LED_R, 1);
  gpio_request(LED_G, "led_g"); gpio_direction_output(LED_G, 1); gpio_set_value(LED_G, 1);
  gpio_request(LED_B, "led_b"); gpio_direction_output(LED_B, 1); gpio_set_value(LED_B, 1);
  setup_timer(&g_blink_timer, blink_handler, 0);
  mod_timer(&g_blink_timer, jiffies + msecs_to_jiffies(g_blink_period));
  return 0;
}
  
static void __exit main_exit(void)
{
  del_timer(&g_blink_timer);
  gpio_free(LED_R);
  gpio_free(LED_G);
  gpio_free(LED_B);
  printk("Unload it!\n");
}
  
module_init(main_init);
module_exit(main_exit);

Makefile

obj-m += main.o
all:
  make -C /home/steward/Github/v3s_kernel M=$(PWD) modules

clean:
  make -C /home/steward/Github/v3s_kernel M=$(PWD) clean

編譯

$ make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- clean
$ make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf-

完成


返回上一頁