Linux Kernel training (2) Gpio

General Purpose Input Output (通用输入/输出)简称为GPIO,或总线扩展器,利用工业标准I2CSMBus™SPI™接口简化了I/O口的扩展。当微控制器或芯片组没有足够的 I/O端口,或当系统需要采用远端串行通信或控制时,GPIO产品能够提供额外的控制和监视功能


One Module driver example,

 #include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/gpio.h>
#include <linux/delay.h>

#define SNOWBALL_LED_GPIO_NR    142
#define LED_OFF            0
#define LED_ON            1

static struct delayed_work blinking_work;
static int i = 0;

static void blink_function(struct work_struct *work)
{
  gpio_set_value(SNOWBALL_LED_GPIO_NR, i++ % 2);
  schedule_delayed_work(&blinking_work, 1000);
}

static int start_blinking(void)
{
  int err = 0;

  INIT_DELAYED_WORK(&blinking_work, blink_function);

  printk(KERN_ERR "We're starting our module!\n");

  err = gpio_request(SNOWBALL_LED_GPIO_NR, "led_blinking");
  if (err) {
    printk(KERN_ERR "GPIO request failed %d\n", err);
    goto request_failed;
  }

  err = gpio_direction_output(SNOWBALL_LED_GPIO_NR, LED_OFF);
  if (err) {
    printk(KERN_ERR "gpio_direction_output failed %d\n", err);
    gpio_free(SNOWBALL_LED_GPIO_NR);
    goto request_failed;
  }

  schedule_delayed_work(&blinking_work, 1);

  request_failed:
  return err;
}

static void stop_blinking(void) {
  printk(KERN_ERR "Bye, cruel world\n");
  cancel_delayed_work_sync(&blinking_work);
  gpio_free(SNOWBALL_LED_GPIO_NR);
}

module_init(start_blinking);
module_exit(stop_blinking);

MODULE_LICENSE("GPL");


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值