Linux核心---56.gpio控制__Linux

來源:互聯網
上載者:User
一.mtk平台的gpio控制
1.1 核心中的gpio配置
//設定gpio18為GPIO模式
mt_set_gpio_mode(GPIO18, GPIO_MODE_GPIO);  
//設定gpio18方向為out
mt_set_gpio_dir(GPIO18, GPIO_DIR_OUT);
//設定gpio18高
mt_set_gpio_out(GPIO18, GPIO_OUT_ONE);  //低是ZERO
1.2 上層應用控制gpio的高低
cong@msi:/work/mtk/temp/gpiotest$ cat gpiotest.c 
#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <dirent.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
#include <linux/ioctl.h>

#define dbmsg(fmt, args ...) printf("%s:%s[%d]: "fmt"\n", __FILE__,__FUNCTION__, __LINE__,##args)

#define GPIO_IOC_MAGIC 0x90
#define GPIO_IOCQMODE _IOR(GPIO_IOC_MAGIC, 0x01, uint32_t)
#define GPIO_IOCTMODE0 _IOW(GPIO_IOC_MAGIC, 0x02, uint32_t)
#define GPIO_IOCTMODE1 _IOW(GPIO_IOC_MAGIC, 0x03, uint32_t)
#define GPIO_IOCTMODE2 _IOW(GPIO_IOC_MAGIC, 0x04, uint32_t)
#define GPIO_IOCTMODE3 _IOW(GPIO_IOC_MAGIC, 0x05, uint32_t)
#define GPIO_IOCQDIR _IOR(GPIO_IOC_MAGIC, 0x06, uint32_t)
#define GPIO_IOCSDIRIN _IOW(GPIO_IOC_MAGIC, 0x07, uint32_t)
#define GPIO_IOCSDIROUT _IOW(GPIO_IOC_MAGIC, 0x08, uint32_t)
#define GPIO_IOCQPULLEN _IOR(GPIO_IOC_MAGIC, 0x09, uint32_t)
#define GPIO_IOCSPULLENABLE _IOW(GPIO_IOC_MAGIC, 0x0A, uint32_t)
#define GPIO_IOCSPULLDISABLE _IOW(GPIO_IOC_MAGIC, 0x0B, uint32_t)
#define GPIO_IOCQPULL _IOR(GPIO_IOC_MAGIC, 0x0C, uint32_t)
#define GPIO_IOCSPULLDOWN _IOW(GPIO_IOC_MAGIC, 0x0D, uint32_t)
#define GPIO_IOCSPULLUP _IOW(GPIO_IOC_MAGIC, 0x0E, uint32_t)
#define GPIO_IOCQINV _IOR(GPIO_IOC_MAGIC, 0x0F, uint32_t)
#define GPIO_IOCSINVENABLE _IOW(GPIO_IOC_MAGIC, 0x10, uint32_t)
#define GPIO_IOCSINVDISABLE _IOW(GPIO_IOC_MAGIC, 0x11, uint32_t)
#define GPIO_IOCQDATAIN _IOR(GPIO_IOC_MAGIC, 0x12, uint32_t)
#define GPIO_IOCQDATAOUT _IOR(GPIO_IOC_MAGIC, 0x13, uint32_t)
#define GPIO_IOCSDATALOW _IOW(GPIO_IOC_MAGIC, 0x14, uint32_t)
#define GPIO_IOCSDATAHIGH _IOW(GPIO_IOC_MAGIC, 0x15, uint32_t)
    
int main ( int argc, char *argv[] )
{
    int fd_gpio;
    int ret;
    
    fd_gpio = open("/dev/mtgpio",O_RDWR);
    if(fd_gpio < 0)
    {
     printf("fd_gpio open error\n");
        return -1;
    }

    while(1)
    {
        ret = ioctl(fd_gpio, GPIO_IOCSDATAHIGH, 18);
        sleep(1);
        ret = ioctl(fd_gpio, GPIO_IOCSDATAHIGH, 18);
        sleep(1);
    }

    close(fd_gpio);
    return 0;
} 1.3 應用代碼
  gpiotest.rar  (下載後改名為gpiotest.tar.gz)
1.4 查看gpio狀態
/sys/class/misc/mtgpio/pin
130|root@78P01:/ # cat /sys/class/misc/mtgpio/pin 
PIN: [MODE] [PULL_SEL] [DIN] [DOUT] [PULL EN] [DIR] [IES]
3: 3 0 0 0 0 0 1 MI                  -->MODE這一項就是在code.dws中配的MODE
4: 3 0 0 0 0 1 1 MO
5: 3 0 1 0 0 1 1 CS
6: 3 0 0 0 0 1 1 CLK 二.利用sys控制gpio口
2.1 代碼如下
#include <linux/init.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/ioport.h>
#include <linux/errno.h>
#include <linux/workqueue.h>
#include <linux/platform_device.h>
#include <linux/types.h>
#include <linux/io.h>
#include <mach/mt_gpio.h>

#define GPIO_POWER_EN GPIO123 //

typedef struct {
    int gpio_power;
}hello_priv;

static ssize_t hello_set_power(struct device *dev,struct device_attribute *attr,const char *buf, size_t count)
{
    hello_priv* prv = dev_get_drvdata(dev);
    if ('0' == buf[0]) //shutdown
    { //process: pin low-->high
     mt_set_gpio_out(prv->gpio_power, GPIO_OUT_ZERO);
      msleep(1500);
      mt_set_gpio_out(prv->gpio_power, GPIO_OUT_ONE);
      printk(KERN_NOTICE "cong: %s:%s[%d]: buf=%s", __FILE__,__FUNCTION__, __LINE__,buf); 
    }else if ('1' == buf[0]
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.