在用户空间处理 GPIO ARM9 Embedded Linux AM1808 [英] Handle GPIO in User Space ARM9 Embedded Linux AM1808

查看:17
本文介绍了在用户空间处理 GPIO ARM9 Embedded Linux AM1808的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须将我的 GSM 模块与基于 ARM9 的 AM1808 连接起来.

I have to interface my GSM module with the AM1808 based on ARM9.

我已将所有 GPIO 引脚分配给 Da850.c 以及 mux.h 文件.我成功创建了一个 uImage 并将该图像插入到我的闪存中.

I have assigned all the GPIO pins to the Da850.c as well as mux.h files. I successfully created a uImage and inserted that image in my flash.

我需要处理来自用户应用程序的一些 GPIO.

I need to handle some of that GPIO from User application.

我知道我们可以从 Kerel 空间处理 GPIO,但我需要从用户空间处理.

I know that we can handle the GPIO from the Kerel space but i need to handle from the user space.

例如,我为 GSM 模块分配了一个用于电源键的 GPIO.我需要通过应用程序更改引脚方式(高或低).

As for example I have assigned a GPIO for power key to GSM module. I need to change the pin means (HIGH or LOW) through application.

好的,我已经编写了以下代码来从用户空间访问它,

Ok i have written a following code to access it from the User Space,

#include <stdio.h>
#include <time.h>
#include <signal.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <stdlib.h>
#include <unistd.h>
#include "GSMpwr.h"
#define BS_GSM_PWR_REGISTER 0x01E26014
#define BS_DCDS_MASK    0x00000004

int fd;  // Memory device descriptor
unsigned long *pPWR;

unsigned short GetGSMpwr(void)
{
    #if defined __HOST_ARM
    unsigned long dcd_value = *pPWR;
    return (pwr_value >> 7) & 0x01;
    #endif
}

void InitializeGSMpwr(void) 
{
    #if defined __HOST_ARM
    int page_size = getpagesize();
    unsigned int MAP_addr;
    unsigned int reg_addr;
    unsigned char *pTemp;             // Pointer to GSMpwr register

/*
 * Open memory and get pointer to GSMpwr register in the FPGA
 */

if((fd = open("/dev/mem", O_RDWR | O_SYNC)) < 0)
{
        printf("failed to open /dev/mem");
        return;
}
else
{
    MAP_addr = (BS_GSM_PWR_REGISTER & ~(page_size - 1));
    pTemp = (unsigned char *)mmap(NULL, page_size,(PROT_READ |      PROT_WRITE),MAP_SHARED,fd,MAP_addr);

    if((pTemp == MAP_FAILED) || (pTemp == NULL))
    {
        printf("failed to map /dev/mem");
        return;
    }
    else
    {
    printf("Memory Mapped at Address %p. \n",pTemp);
    }

virt_addr = map_base + (control & MAP_MASK);



    reg_addr = (BS_GSM_PWR_REGISTER & (page_size - 1));
    pPWR = (unsigned long*)(pTemp + reg_addr);
    printf("GSM PWR PIN mapped in Application\n");
}

我只能通过这段代码读取该引脚,现在我想将该引脚用作输出,并希望以 3 秒的时间间隔变高和变低.

I can only read that pin through this code, Now i want to use that pin as an output and want to go high and low with the time interval of 3sec.

推荐答案

最简单的方法是利用 sysfs 中的 GPIO 支持,您可以在其中控制所有导出的 GPIO.请查看 Linux 内核 GPIO 文档,特别是 用户空间的 Sysfs 接口部分.

The easiest way is to utilize GPIO support in sysfs, where you could control all the exported GPIO's. Please have a look at the Linux kernel GPIO documentation, in particular, Sysfs Interface for Userspace part.

sysfs(GPIO_SYSFS)中启用GPIO支持后,GPIO控制将变得如此简单:

After you have enabled GPIO support in sysfs (GPIO_SYSFS), the GPIO control would be as easy as:

示例

GPIO=22

cd /sys/class/gpio
ls
echo $GPIO > /sys/class/gpio/export
ls

请注意第一个 ls 上 gpio22 不存在,但在您将 GPIO 22 导出到用户空间后存在.

Notice on the first ls that gpio22 doesn't exist, but does after you export GPIO 22 to user space.

cd /sys/class/gpio/gpio$GPIO
ls

有用于设置方向和检索当前值的文件.

There are files to set the direction and retrieve the current value.

echo "in" > direction
cat value

您可以为输出配置 GPIO 并设置值.

You can configure the GPIO for output and set the value as well.

echo "out" > direction
echo 1 > value

示例取自此处.

这篇关于在用户空间处理 GPIO ARM9 Embedded Linux AM1808的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆