Linux GPIO控制

参考:https://developer.ridgerun.com/wiki/index.php/How_to_use_GPIO_signals


#!/bin/sh

show_usage()
{
    printf "\ngpio.sh <gpio pin number> [in|out [<value>]]\n"
}

if [ \( $# -eq 0 \) -o \( $# -gt 3 \) ] ; then
    show_usage
    printf "\n\nERROR: incorrect number of parameters\n"
    exit 255
fi

#doesn't hurt to export a gpio more than once
echo $1 > /sys/class/gpio/export

if [  $# -eq 1 ] ; then
   cat /sys/class/gpio/gpio$1/value
   exit 0
fi

if [ \( "$2" != "in" \) -a  \( "$2" != "out" \) ] ; then
    show_usage
    printf "\n\nERROR: second parameter must be 'in' or 'out'\n"
    exit 255
fi

echo $2 > /sys/class/gpio/gpio$1/direction

if [  $# -eq 2 ] ; then
   cat /sys/class/gpio/gpio$1/value
   exit 0
fi


VAL=$3

if [ $VAL -ne 0 ] ; then
    VAL=1
fi

echo $VAL > /sys/class/gpio/gpio$1/value

问题:1、GPIO value值设置无反应?

解:(1) Linux加打印查看值是否有设下去,打印出GPIO相关寄存器的值;

static int davinci_gpio_get(struct gpio_chip *chip, unsigned offset)
{
    int tmp;
	struct davinci_gpio *d = container_of(chip, struct davinci_gpio, chip);
	struct gpio_controller *__iomem g = d->regs;

	/* return (1 << offset) & __raw_readl(&g->in_data); */
    tmp = (1 << offset) & __raw_readl(&g->in_data);
    printk("##### GPIO_GET val[0x%08x] ####\n", tmp);
    printk("########set_data[0x%08x], out_data[0x%08x], dir[0x%08x], in_data[0x%08x], clr_data[0x%08x]####\n", 
           g->set_data,
           g->out_data,
           g->dir,
           g->in_data,
           g->clr_data
           );
    return tmp;
}

/*
 * Assuming the pin is muxed as a gpio output, set its output value.
 */
static void
davinci_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
{
	struct davinci_gpio *d = container_of(chip, struct davinci_gpio, chip);
	struct gpio_controller *__iomem g = d->regs;

    printk("########GPIO_SET Val[0x%08x] ####\n", value);
    printk("########set_data[0x%08x], out_data[0x%08x], dir[0x%08x], in_data[0x%08x], clr_data[0x%08x]####\n", 
           g->set_data,
           g->out_data,
           g->dir,
           g->in_data,
           g->clr_data
           );
	__raw_writel((1 << offset), value ? &g->set_data : &g->clr_data);
}


(2) 对应的GPIO PIN MUX是否设置正确;

(3) GPIO方向是否设置正确;



GPIO 参考:

DM365 GPIO
http://blog.163.com/laorenyuhai126@126/blog/static/193507792011328213983/

sys接口概述
http://processors.wiki.ti.com/index.php/Linux_PSP_GPIO_Driver_Guide
https://blackfin.uclinux.org/doku.php?id=linux-kernel:drivers:gpio-sysfs


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值