mstar 325 IO控制demo

头文件TestMstarGpio.h

///

#ifndef _MSTAR__GPIO_KLSDJFAJLKAJKAL_H_  
#define _MSTAR__GPIO_KLSDJFAJLKAJKAL_H_  

  
#define MSTAR_GPIO_MAJOR 224  
#define MSTAR_GPIO_MINOR 0  
#define Cmd_GPIO_Write 221
#define Cmd_GPIO_Read 222
#define Cmd_GPIO_DirectionOut 223
#define Cmd_GPIO_DirectionIn 224


typedef struct _mstar_gpio_data_struct {  
        int  num;
        int value;
    }mstar_gpio_data_struct;  
int  OpenGpio();

int WriteGpio(int fd,int  num,int value);

int ReadGpio(int fd,int num);

int SetGpioIn(int fd,int num);
#endif

/

源文件TestMstarGpio.c

///

#include <stdio.h>
#include <fcntl.h>
#include <sys/ioctl.h>

#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <sys/ioctl.h> 
#include "MstarGpio.h"


void HexPrintf(char * buf ,int len)
{
    int i;
    printf("\r\n");
    for(i=0;i<len;i++)
        {
            printf("0x%02x ",*(buf+i));
                if(15==(i%16))
            printf("\r\n");
        }
    printf("\r\n");

    
}
int  OpenGpio()
{
            int fd=0;
       /*打开设备文件*/
    fd = open("/dev/MstarGpio",O_RDWR);
    if (fd < 0)
    {
        printf("Open Dev Mem0 Error!\n");
        return -1;
    }
    return fd;
}
int WriteGpio(int fd,int  num,int value)
{

    mstar_gpio_data_struct Mymstar_gpio_data_struct;
    Mymstar_gpio_data_struct.num=num;
    Mymstar_gpio_data_struct.value=value;
 
    if (ioctl(fd, Cmd_GPIO_Write, &Mymstar_gpio_data_struct) < 0)
        {
            printf("Call cmd MEMDEV_IOCPRINT fail\n");
            return -1;
    }

    return 0;
}
int ReadGpio(int fd,int num)
{

    mstar_gpio_data_struct Mymstar_gpio_data_struct;
    Mymstar_gpio_data_struct.num=num;
  if (ioctl(fd, Cmd_GPIO_Read, &Mymstar_gpio_data_struct) < 0)
        {
            printf("Call cmd MEMDEV_IOCPRINT fail\n");
            return -1;
    }

    return Mymstar_gpio_data_struct.value;
}
int SetGpioIn(int fd,int num)
{

    mstar_gpio_data_struct Mymstar_gpio_data_struct;
 Mymstar_gpio_data_struct.num=num;
 
    if (ioctl(fd, Cmd_GPIO_DirectionIn, &Mymstar_gpio_data_struct) < 0)
        {
            printf("Call cmd MEMDEV_IOCPRINT fail\n");
            return -1;
    }

    return Mymstar_gpio_data_struct.value;
}
main(int argc,char *argv[])
{
	int num;
	int value;
	int i;
	int fd;
	int  ret=0;

		

	if((3!=argc)&&(2!=argc))
		{
			printf("usage \r\n read demo: ./TestMstarGpio 62 \r\n write demo: ./TestMstarGpio 62 1 \r\n");
			return 0;
		}
		num=atoi(argv[1]);
	fd=OpenGpio();
	if(argc==3)
		{
				printf("fd %d\r\n",fd);
				value=atoi(argv[2]);
			WriteGpio(fd,num,value);
			printf("WriteGpio %d\r\n",value);
		}
		
	if(argc==2)
		{
		//		SetGpioIn(fd,num);
		//	while(1)
			{
					ret=ReadGpio(fd,num);
				printf("ReadGpio %d\r\n",ret);
			//	usleep(2000);
			}
			
			
		}
	close(fd);
	//printf("argc %d %d\r\n",argc,cycle);
	return 0;
}

/

编译的Makefile

all:
    arm-buildroot-linux-uclibcgnueabihf-gcc -o TestMstarGpio TestMstarGpio.c
#    arm-linux-gnueabihf-gcc -o TestMstarGpio TestMstarGpio.c

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值