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

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

问题描述

我必须将GSM模块与基于ARM9的AM1808接口。



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



我需要从User应用程序处理一些GPIO。



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



例如我已经分配了一个GPIO用于GSM模块的电源键。我需要通过应用程序更改引脚的含义(HIGH或LOW)。



确定我已经编写了一个以下代码来从用户空间访问它,

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

int fd; //内存设备描述符
unsigned long * pPWR;

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

void初始化GSMpwr(void)
{
#if定义__HOST_ARM
int page_size = getpagesize();
unsigned int MAP_addr;
unsigned int reg_addr;
unsigned char * pTemp; //指向GSMpwr寄存器

/ *
*打开内存并获取FPGA中的GSMpwr寄存器指针
* /

if((fd = open(/ dev / mem,O_RDWR | O_SYNC))< 0)
{
printf(无法打开/ 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(map to / dev / mem);
return;
}
else
{
printf(Memory Mapped at Address%p。\\\
,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映射);
}

我只能通过这段代码阅读该引脚,现在我想使用引脚作为输出,并希望以3秒的时间间隔进入高低。

解决方案

最简单的方法是使用GPIO支持 sysfs ,您可以在其中控制所有导出的GPIO。请参阅Linux 内核GPIO文档,特别是 sysfs (<$ c)之后启用GPIO支持后,



$ c> GPIO_SYSFS ),GPIO控件将会像以下一样简单:



示例

  GPIO = 22 

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

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

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

有文件设置方向并检索当前值。

  echoin>方向
cat值

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

  echoout>方向
回声1>值

示例取自 here


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

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.

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

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

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");
}

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.

解决方案

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.

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

Example

GPIO=22

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

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

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

echo "out" > direction
echo 1 > value

Example is taken from here.

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

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