尧图网站建设 尧图网络
  • 首页
  • 关于我们
  • 服务项目
  • 案例展示
  • 建站流程
  • 资讯中心
  • 联系我们
首页/资讯中心/详情

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

如何在linux驱动程序保存文件
📅 发布时间:2026/6/22 7:45:35

碰到问题:项目中发现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完成时把数据以文件的形式保存在系统根目录/下

相关新闻

  • 2025 年最新推荐!高性价比自动售卖机厂家推荐榜:无人 / 智能 / 文创 / 盲盒 / 食品售货机优选品牌全解析
  • 2025 年火花机厂家最新推荐榜:涵盖新型 / 镜面 / 数控 / 五轴联动等多类型设备,优质厂家精选助力企业精准选购
  • 2025年10月抗老面霜产品推荐榜:五强横评选购指南

最新新闻

  • Linux proc-modules文件格式与m_show回调
  • 酒泉市金塔县2026年黄金回收本地靠谱门店 白银回收+铂金回收门店指南TOP5排行榜 优选门店汇总及电话地址推荐 - 大熊猫898989
  • Qwen3-VL架构深度解析:Interleaved-MRoPE与DeepStack技术原理
  • 终极B站视频解析指南:3分钟掌握免费获取高清视频地址的完整教程
  • 开源AI视频分析神器:5分钟自动看懂视频内容,解放你的双手
  • 告别Selenium:PyAutoGUI图像识别实现跨平台桌面自动化测试

日新闻

  • 2026速览惠州叛逆青少年学校前十大排名名单出炉 - 武汉中职最新信息发布
  • 2026上饶白蚁消杀哪家好?15年本土2大权威白蚁防治公司推荐(金盾虫控/青蚁卫士) - 我叫一
  • 天龙八部单机版终极数据管理工具:5个技巧快速掌握游戏数据编辑

周新闻

  • Visual C++运行库修复终极指南:5分钟快速解决Windows软件启动错误
  • 手把手教你构建统计局地区经济数据爬虫:从环境搭建到数据持久化全指南
  • 2026多Agent深度解析:用AI团队替代单一模型,四种架构实战落地

月新闻

  • 【总结】入门篇:50句话让你记住架构核心概念
  • WeChatMsg技术方案解析:实现Mac微信数据自主管理的完整解决方案
  • WeChatMsg:革新性微信数据备份方案,打造你的专属数字记忆库

关于尧图

  • 公司简介
  • 团队介绍
  • 企业文化
  • 荣誉资质

服务项目

  • 定制开发
  • 电商建站
  • UI 设计
  • 运维服务

快速链接

  • 案例展示
  • 建站流程
  • 常见问题
  • 资讯中心

联系方式

  • 📍北京市朝阳区互联网产业园 A 座 10 层
  • 📞400-888-8888
  • ✉️contact@rkmt.cn
  • 🕐周一至周日 9:00-21:00

© 2024 北京尧图网络科技有限公司 版权所有 | 京 ICP 备 XXXXXXXX 号