AM3358 >> C/C++

LED(Register)


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>

#define GPIO0                 0x44E07000
#define GPIO1                 0x4804C000
#define GPIO2                 0x481AC000
#define GPIO3                 0x481AE000 

#define GPIO_REVISION         0x00
#define GPIO_SYSCONFIG        0x10
#define GPIO_EOI              0x20
#define GPIO_IRQSTATUS_RAW_0  0x24
#define GPIO_IRQSTATUS_RAW_1  0x28
#define GPIO_IRQSTATUS_0      0x2c
#define GPIO_IRQSTATUS_1      0x30
#define GPIO_IRQSTATUS_SET_0  0x34
#define GPIO_IRQSTATUS_SET_1  0x38
#define GPIO_IRQSTATUS_CLR_0  0x3c
#define GPIO_IRQSTATUS_CLR_1  0x40
#define GPIO_IRQWAKEN_0       0x44
#define GPIO_IRQWAKEN_1       0x48
#define GPIO_SYSSTATUS        0x114
#define GPIO_CTRL             0x130
#define GPIO_OE               0x134
#define GPIO_DATAIN           0x138
#define GPIO_DATAOUT          0x13c
#define GPIO_LEVELDETECT0     0x140
#define GPIO_LEVELDETECT1     0x144
#define GPIO_RISINGDETECT     0x148
#define GPIO_FALLINGDETECT    0x14c
#define GPIO_DEBOUNCENABLE    0x150
#define GPIO_DEBOUNCINGTIME   0x154
#define GPIO_CLEARDATAOUT     0x190
#define GPIO_SETDATAOUT       0x194

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;
static volatile unsigned long *gpio1_dataout;
 
void blink_handler(unsigned long unused)
{
  static int on = 1;
     
  on = on ? 0 : 1;
  *gpio1_dataout = (on << 21) | (on << 22) | (on << 23) | (on << 24);
  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);
  }

  gpio1_dataout = ioremap(GPIO1 + GPIO_DATAOUT, 4);
  printk("gpio1_dataout: 0x%x\n", (unsigned int)gpio1_dataout);
 
  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);
  iounmap(gpio1_dataout);
}
  
module_init(main_init);
module_exit(main_exit);

完成


返回上一頁