怎么传kernel文件到linux,linux kernel将buf保存到文件做法 很好的调试方法

下面有2个示例:

1:保存机器从开机到结束的VBATT;

2:保存uart接收到的数据到文件;

意义不多说了。

以下是代码:

#include

#include

#include

static struct file *fp =NULL;

int write_to_file (char *buf, int size)

{

int ret = 0;

struct file *fp;

mm_segment_t old_fs;

loff_t pos = 0;

int nwrite = 0;

static int offset = 0;

static int first_flag=0;

/* change to KERNEL_DS address limit */

old_fs = get_fs();

set_fs(KERNEL_DS);

if( first_flag==0){

first_flag=1;

/* open file to write */

fp = filp_open("/data/at_log1", O_WRONLY|O_CREAT, 0640);

if (!fp) {

printk("%s: open file error\n", __FUNCTION__);

ret = -1;

goto exit;

}

}

pos=(unsigned long)offset;

/* Write buf to file */

nwrite=vfs_write(fp, buf, size, &pos);

offset +=nwrite;

exit:

return ret;

}

int xxxx_exit(void){

if (fp)

filp_close(fp, NULL);

}

1:保存VBATT与SOC:

static int adjust_soc(struct pm8921_bms_chip *chip, int soc,

int batt_temp, int chargecycles,

int rbatt, int fcc_uah, int uuc_uah, int cc_uah)

{

------------

static char vbatt[100];

rc = pm8921_bms_get_simultaneous_battery_voltage_and_current(

&ibat_ua,

&vbat_uv);

if (rc < 0) {

pr_err("simultaneous vbat ibat failed err = %d\n", rc);

goto out;

}

chip->vbat_mv= vbat_uv/1000;

sprintf(vbatt,"%d %d\n",the_chip->vbat_mv,calculated_soc);

write_to_file(vbatt,strlen(vbatt));

----------

}

0818b9ca8b590ca3270a3433284dd417.png

2:将串口接收到的字符转化为16进制保存到文件中:

static void msm_serial_hs_rx_tlet(unsigned long tlet_ptr)

{

---------------

static char temp[1024];

rx_count = msm_hs_read(uport, UARTDM_RX_TOTAL_SNAP_ADDR);

/* order the read of rx.buffer */

rmb();

if (0 != (uport->read_status_mask & CREAD)) {

retval = tty_insert_flip_string(tty, msm_uport->rx.buffer,

rx_count);

if (retval != rx_count) {

msm_uport->rx.buffer_pending |= CHARS_NORMAL |

retval << 5 | (rx_count - retval) << 16;

}

if(rx_count<=512){

//memcpy(temp,msm_uport->rx.buffer,rx_count);

for(i=0;i

sprintf(temp+2*i,"%02x",msm_uport->rx.buffer[i]);

write_to_file(temp,strlen(temp));

}

}

----------------

}

0818b9ca8b590ca3270a3433284dd417.png

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值