Linux Kernel---56.gpio control __linux

Source: Internet
Author: User
I. The GPIO control of MTK Platform
GPIO configuration in the 1.1 kernel
Set Gpio18 to Gpio mode
Mt_set_gpio_mode (GPIO18, Gpio_mode_gpio);
Set GPIO18 orientation to Out
Mt_set_gpio_dir (GPIO18, gpio_dir_out);
Set Gpio18 height
Mt_set_gpio_out (GPIO18, Gpio_out_one); Low is zero
1.2 Upper level application control 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 Application code
Gpiotest.rar (renamed to gpiotest.tar.gz after downloading)
1.4 View GPIO Status
/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 This is the mode in Code.dws.
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 two. Using SYS to control the GPIO port
2.1 Code as follows
#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]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.