当前位置: 首页 > news >正文

如何在linux驱动程序保存文件

碰到问题:项目中发现ADC提取的数据,偶尔有个别好像是上次采集的数据,需要查找在哪一环节出错了,因此需要保存驱动中DMA刚完成时的数据和应用程序处理完发送包的数据。应用程序比较容易,直接保存文件就行了,驱动程序在内核层如何做呢?查找了一些资料,发现在内核中需要使用filep_open函数来获取struct file结构,然后针对struct file可以写文件,最后使用filp_close函数关闭打开的文件,初步整出以下代码:
static int kernel_file_delete(char *file_path1)
{
struct path path;

int ret = kern_path(file_path1, LOOKUP_FOLLOW, &path);
if (ret) return ret;ret = vfs_unlink(d_inode(path.dentry->d_parent), path.dentry, NULL);path_put(&path);
return ret;

}

static ssize_t kernel_file_write(unsigned char *buf,int count, struct file *target_file)
{
mm_segment_t old_fs;

old_fs = get_fs();
set_fs(KERNEL_DS);

// ret = vfs_write(target_file, buf, count, &target_file->f_pos);
loff_t pos = 0;
printk("targetfile=%lx,buf=%lx,count=%d\n",target_file,buf,count);
ssize_t ret = kernel_write(target_file, buf, count, &pos); if (ret < 0) {
printk("Write failed: %d\n", ret);
return ret;
}

set_fs(old_fs);
return ret;

}

static int kernel_file_close(struct file *target_file)
{
filp_close(target_file, NULL);
}
//====================================================================================
struct file *target_file;
unsigned char *buf=(unsigned char *)p_adc_dev->dsts[0];
buf+=0x1E84;// add 0.5S@3906 sample rate
sprintf(file_path,"/kernel_data%02d.bin",p_adc_dev->layout_index);
printk("--channel%02d--1 ",p_adc_dev->current_transfer_index);
kernel_file_delete(file_path);
printk("2 ");
target_file = filp_open(file_path, O_CREAT|O_WRONLY, 0644);
if (IS_ERR(target_file)) {
printk("Failed to open %s\n", file_path);
}
printk("3 ");
kernel_file_write(buf,(p_adc_dev->bytes_cur_transfer-0x1E84),target_file);
printk("4 ");
vfs_fsync(target_file,0);
printk("5 ");
kernel_file_close(target_file);
printk("6\n");

但是在实际使用中,发现每次运行到了都会内核崩溃,考虑可能是写文件的地方在中断的原因,因此引出第二个问题:如何把对文件的操作移到中断函数之外?
综合考虑,可以使用linux内核的工作队列方式,因此得出以下代码:
首先在设备结构体中加入work_struct,并且在probe函数中初始化work
struct axi_adc_dev
{
。。。
dev_t dev_node;
bool f_on; /* flag indicates this ADC is configured to ON */
ktime_t startTime;
int abort;
int aborted;
struct work_struct my_work;
};

static int axi_adc_probe(struct platform_device *pdev)
{
。。。。
INIT_WORK(&aad->my_work,writefile_work_func);

if (dev_index == 0)
{。。。。

}

然后在中断处理程序的上下文中调度这个work,以便对文件的操作放在工作队列的线程环境执行:
static void dma_rx_callback(void *completion)
{
//中断处理程序的上下文中
。。。
schedule_work(&p_adc_dev->my_work);//调度my_work
。。。
}

最后是my_work的工作函数writefile_work_func
static void writefile_work_func(struct work_struct *work)
{
char file_path[19];

 struct axi_adc_dev *p_adc_dev = container_of(work, struct axi_adc_dev, my_work);struct file *target_file;unsigned char *buf=(unsigned char *)p_adc_dev->dsts[0];
buf+=0x1E84;// add 0.5S@3906 sample ratesprintf(file_path,"/kernel_data%02d.bin",p_adc_dev->layout_index);
printk("--channel%02d--1 ",p_adc_dev->current_transfer_index);kernel_file_delete(file_path);
printk("2 ");
target_file = filp_open(file_path, O_CREAT|O_WRONLY, 0644);
if (IS_ERR(target_file)) {printk("Failed to open %s\n", file_path);
}
printk("3 ");kernel_file_write(buf,(p_adc_dev->bytes_cur_transfer-0x1E84),target_file);
printk("4 ");vfs_fsync(target_file,0);
printk("5 ");kernel_file_close(target_file);
printk("6\n");

}
这次再执行就没什么问题了,没次adc采集的DMA完成时把数据以文件的形式保存在系统根目录/下

http://www.jsqmd.com/news/18212/

相关文章:

  • 2025年10月留香沐浴露排行:蓝蕨等五款香水级体验评测
  • 2025 年最新推荐!高性价比自动售卖机厂家推荐榜:无人 / 智能 / 文创 / 盲盒 / 食品售货机优选品牌全解析
  • 使用rabbitmq 进行任务调度
  • byte[](字节数组)
  • 2025 年火花机厂家最新推荐榜:涵盖新型 / 镜面 / 数控 / 五轴联动等多类型设备,优质厂家精选助力企业精准选购
  • 2025年10月抗老面霜产品推荐榜:五强横评选购指南
  • 2025 年工业连接器厂家最新推荐榜单:聚焦 M8/M12 / 防水 / 重载 / 以太网品类,精选优质国产企业助力高效采购
  • Java设计模式之工厂模式 - 实践
  • 【转】[C#] 要从接口取时间,单个订单查询和批量查询,写一个接口还是两个接口合适?
  • 2025 年药包材辅导公司最新推荐榜:GMP 验证 / 质量体系 / 实验室装修等服务优质机构权威评选
  • 2025年10月防脱生发产品推荐榜:十款临床验证口碑对比
  • 2025 年国内优质不锈钢厂家最新推荐排行榜:含沈阳/东三省区域及水箱油罐等产品优质服务商楼梯/激光切割/桥梁杆/真空罐/扶手不锈钢厂家推荐
  • 界面控件DevExpress WPF v25.2新功能预览 - 聚焦AI功能提升
  • CSS 预处理器:Sass的基本用法、核心特性 - 详解
  • 2025 顶管源头厂家最新推荐榜单:F 型混凝土 / 水泥 / 电力 / 矩形 / 市政排水大口径优质供应商精选
  • 使用DMA和PWM驱动16组WS2812 LED的STM32实现
  • 2025年GEO品牌推荐榜:云视GEO以全栈技术引领行业变革
  • 【开题答辩实录分享】以《 Python基于大数据的四川旅游景点数据分析与可视化》为例进行答辩实录分享 - 实践
  • linux 程序 启动命令
  • 2025 年台车炉厂家最新推荐榜,技术实力与市场口碑深度解析,助力企业精准选型天然气/燃气/热处理/全纤维/翻转式台车炉厂家推荐
  • ida pro 9.2 接入 ida-pro-mcp
  • 2025 年最新高低温试验箱厂家排行榜:精选优质供应商,专业推荐助您精准选购合适设备恒温恒湿试验箱/高低温试验箱厂家推荐
  • 2025 年淬火炉源头厂家最新推荐榜:聚焦技术创新与市场口碑深度解析,精选优质企业供采购参考
  • 2025 年国际物流服务公司最新推荐排行榜:覆盖海运快递跨境专线,精选优质企业助力跨境电商商家高效选择合作伙伴
  • 跟着GPT5学习bert分类中[CLS]标记 hidden state的作用
  • 2025 年最新推荐立体画厂家权威榜单:涵盖 3D 光栅 / 装饰 / 三维等品类,助力精准选优质厂家
  • WSL1升级为WSL2
  • 详细介绍:关于容器Docker
  • 完整教程:计算机视觉进阶教学之Mediapipe库(一)
  • 实用指南:【办公类-116-01】20250929家长会PPT(Python快速批量制作16:9PPT相册,带文件名,照片横版和竖版)