常用linux内核和用户层进行数据交换的样例

记录一些内核驱动层与用户层的数据交互,方便后续忘记查看:
下面附加应用层测试代码:

#include "stdio.h"
#include "unistd.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "sys/ioctl.h"
#include "fcntl.h"
#include "stdlib.h"
#include "string.h"
#include <poll.h>
#include <sys/select.h>
#include <sys/time.h>
#include <signal.h>
#include <fcntl.h>
#include<math.h>

#define cmd 2

/*
 * @description		: main主程序
 * @param - argc 	: argv数组元素个数
 * @param - argv 	: 具体参数
 * @return 			: 0 成功;其他 失败
 */
 int main(int argc, char *argv[])
{
    
    
	int fd;
	char *filename;
   
	int ret = 0;
    int val = 1;
	if (argc != 2) {        
		printf("Error Usage!\r\n");
		return -1;
	}

	fd = open(argv[1], O_RDWR);			/* /dev/spidev */
	if(fd < 0) {   
		printf("can't open file %s\r\n", filename);
		return -1;
	}
/******************************************************************/
	// char data = 0xaa;
	// char value;
	
	// ret = write(fd,&data,1);		//void cmt_spi3_send(uint8_t data8)


	// ret = read(fd,&value,1);		//uint8_t cmt_spi3_recv(void)
	// printf("value = %x\n",value);
/*******************************************************************/
	// ret = ioctl(fd, 1, 0);
	// if (ret < 0) {
	// 	perror("IOCTL command failed");	 //void cmt_spi3_init(void)
	// 	exit(1);
	// }
	
/*****************************************************************/
	// uint8_t value[2] = {0xaa,0x11};	
	// ret = ioctl(fd, 3, value);
	// if (ret < 0) {
	// 	perror("IOCTL command failed");	 //void cmt_spi3_write(u8 addr, u8 dat)
	// 	exit(1);
	// }
/*********************************************************************/	
	// uint8_t data = 0xff;
	// ret = ioctl(fd, 4, &data);
	// if (ret < 0) {
	// 	perror("IOCTL command failed");  //cmt_spi3_read(addr,&value);
	// 	exit(1);
	// }
	// printf("data = %x\n",data);
/*********************************************************************/
/*********************************************************************/
	// struct data {
	// 	uint8_t buf[32];
	// 	int len;
	// };
	// struct data value;
	// value.buf[0] = 0xff;
	// value.buf[1] = 0xaa;
	// value.buf[2] = 0xee;
	// value.buf[3] = 0x11;
	// value.len = 4;

	// ret = ioctl(fd, 5, &value);
	// if (ret < 0) {
	// 	perror("IOCTL command failed");			//cmt_spi3_write_fifo(buf, len);
	// 	exit(1);
	// }

/*********************************************************************/	
	struct data {
		uint8_t buf[32];
		int len;
	};
	int i;
	struct data value;
	value.len = 5;
	ret = ioctl(fd, 6, &value);
	if (ret < 0) {
		perror("IOCTL command failed");	//cmt_spi3_read_fifo(buf, len);
		exit(1);
	}
	// printf("%s\n",user_data);
	for(i = 0;i<value.len;i++)
		printf("value.buf[%d] = %x",i,value.buf[i]);

/**********************************************************************/
close(fd);	/* 关闭文件 */	
	return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值