AM3358 >> C/C++

LED(GPIO)


LED位置
D2: GPIO1-21
D3: GPIO1-22
D4: GPIO1-23
D5: GPIO1-24

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>
#include <asm/io.h>
 
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Steward_Fu");
MODULE_DESCRIPTION("BBGW LED Driver");

static int g_blink_period=300;
static struct timer_list g_blink_timer;

void blink_handler(unsigned long unused)
{
  static bool on = false;
    
  on = !on;
  gpio_set_value(21+32, on);
  gpio_set_value(22+32, on);
  gpio_set_value(23+32, on);
  gpio_set_value(24+32, on);
  mod_timer(&g_blink_timer, jiffies + msecs_to_jiffies(g_blink_period));
}

static int __init main_init(void)
{
  if(gpio_request(21+32, "gpio1-21") < 0){
    printk("failed to request D2\n");
  }
  else{
    printk("request successfully for D2\n");
    gpio_direction_output(21+32, 1);
  }

  if(gpio_request(22+32, "gpio1-22") < 0){
    printk("failed to request D3\n");
  }
  else{
    printk("request successfully for D3\n");
    gpio_direction_output(22+32, 1);
  }
  
  if(gpio_request(23+32, "gpio1-23") < 0){
    printk("failed to request D4\n");
  }
  else{
    printk("request successfully for D4\n");
    gpio_direction_output(23+32, 1);
  }
  
  if(gpio_request(24+32, "gpio1-24") < 0){
    printk("failed to request D5\n");
  }
  else{
    printk("request successfully for D5\n");
    gpio_direction_output(24+32, 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(21+32);
  gpio_free(22+32);
  gpio_free(23+32);
  gpio_free(24+32);
}
 
module_init(main_init);
module_exit(main_exit);

完成


返回上一頁