当前位置: 首页 > 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.hskmm.com/?act=detail&tid=35573

相关文章:

  • 2025年10月留香沐浴露排行:蓝蕨等五款香水级体验评测
  • 2025年10月防脱生发产品推荐榜:十款临床验证口碑对比
  • 2025 年国内优质不锈钢厂家最新推荐排行榜:含沈阳/东三省区域及水箱油罐等产品优质服务商楼梯/激光切割/桥梁杆/真空罐/扶手不锈钢厂家推荐
  • 2025 顶管源头厂家最新推荐榜单:F 型混凝土 / 水泥 / 电力 / 矩形 / 市政排水大口径优质供应商精选
  • 2025 年台车炉厂家最新推荐榜,技术实力与市场口碑深度解析,助力企业精准选型天然气/燃气/热处理/全纤维/翻转式台车炉厂家推荐
  • 2025 年淬火炉源头厂家最新推荐榜:聚焦技术创新与市场口碑深度解析,精选优质企业供采购参考
  • 2025 年贵阳家居品牌最新推荐榜,技术实力与市场口碑深度解析贵阳家居实木家具/贵阳家居布艺沙发/贵阳家居多功能沙发家居公司推荐
  • vim编辑器常用命令
  • ida pro 9.2 接入 ida-pro-mcp
  • 2025 年国际物流服务公司最新推荐排行榜:覆盖海运快递跨境专线,精选优质企业助力跨境电商商家高效选择合作伙伴
  • 跟着GPT5学习bert分类中[CLS]标记 hidden state的作用
  • 2025 年最新推荐立体画厂家权威榜单:涵盖 3D 光栅 / 装饰 / 三维等品类,助力精准选优质厂家
  • 2025年信息流代运营服务商权威推荐榜:专业投放策略与高转化效果深度解析
  • 实用指南:【办公类-116-01】20250929家长会PPT(Python快速批量制作16:9PPT相册,带文件名,照片横版和竖版)
  • 为什么String 创建的字符串存储在公共池中,而 new 创建的字符串对象在堆上?公共池和堆又是什么?
  • 2025年法兰保护罩厂家权威推荐榜:阀门保温罩/法兰罩/法兰防溅罩/法兰保护套,专业防护与耐用品质深度解析
  • 分组查询注意力(GQA)的Pytorch实现
  • 基于TV模型利用Bregman分裂算法迭代对图像进行滤波和复原处理
  • 2025 年大路灯品牌最新推荐榜,技术实力与市场口碑深度解析,精选优质源头厂家
  • LangChain4j 比 SolonAI 强在哪?弱在哪?
  • 2025.10.20__2023秋季联赛题解(第11题)
  • docker怎么更新版本
  • B树和B+树的解析应用
  • 2025 年快速退火炉优质厂家最新推荐榜单:真空 / 半导体 / 晶圆 / 高温 / 桌面等多类型设备企业权威评选
  • 2025年10月河南园区招商扶持公司推荐:五强对比评测榜
  • 2025 年广州心理疏导机构推荐:桥恩心理多维度服务满足不同人群心理健康需求
  • 2025 年深圳心理疏导机构推荐,桥恩心理:专业心理疏导服务的优质选择与全体系诊疗优势
  • 2025年10月手操器公司推荐:对比评测榜揭示工业诊断选型要点
  • OIFC NOI2023省队集训
  • 实战案例:职行力如何利用纷享销客CRM实现人效管理数字化突围?