sample c application to test the mpl3115A2 driver in linux

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

sample c application to test the mpl3115A2 driver in linux

跳至解决方案
2,649 次查看
chiranjeevikinn
Contributor III


Hi all,

I have integrated the mpl3115A2 linux driver which is present in the NXP website : https://www.nxp.com/webapp/sps/download/preDownload.jsp . The driver is successfully registered and I am able to enable and disable it. Can anyone share a sample application to read the Altitude and Pressure?

Thanks,

Chiranjeevi

标签 (1)
0 项奖励
回复
1 解答
1,787 次查看
TomasVaverka
NXP TechSupport
NXP TechSupport

Hi Chiranjeevi,

I am not a Linux expert, so I have forwarded your question to our Linux specialists, here is their response:

"We do not have a Linux application for this, but since the device use standard input device node, customer shall be able to use application tools like Evtest (google from web) to get the input data. The driver also use sysfs device attribute interface, which can be accessed with “echo” and “cat” from the device node at /sys, refer to readme file in other NXP driver package like FXAS2100x."

Anyway, I looked at the mpl3115.c file and found the following functions to put the sensor into barometric mode, activate it and read the pressure data:

static void mpl3115_device_init(struct i2c_client *client)

{

   u8 val[8];

   val[0] = 0x28;

   mpl3115_i2c_write(client,MPL3115_CTRL_REG1,val,1);

}

static int mpl3115_start_chip(struct i2c_client *client )

{

   u8 val;

   int ret;

   mpl3115_i2c_read(client,MPL3115_CTRL_REG1,&val,1);

   val |= MPLL_ACTIVE_MASK;

   ret = mpl3115_i2c_write(client,MPL3115_CTRL_REG1, &val,1);

   return 0;

}

static int mpl3115_read_data(struct i2c_client *client,int *pres, short *temp)

{

   u8 tmp[5];

   mpl3115_i2c_read(client,MPL3115_PRESSURE_DATA,tmp,5);

   *pres = (DATA_SHIFT_BIT(tmp[0],24) | DATA_SHIFT_BIT(tmp[1],16)  | DATA_SHIFT_BIT(tmp[2],8) )>>12;

   *temp = (DATA_SHIFT_BIT(tmp[3],8) | DATA_SHIFT_BIT(tmp[4],0))>>4;

   return 0;

}

To read the altitude, I would recommend using something like this:

static void mpl3115_device_altitude_init(struct i2c_client *client)

{

   u8 val[8];

   val[0] = 0xA8;          // Altimeter mode, OSR = 32

   mpl3115_i2c_write(client,MPL3115_CTRL_REG1,val,1);

}

static int mpl3115_start_chip(struct i2c_client *client )

{

   u8 val;

   int ret;

   mpl3115_i2c_read(client,MPL3115_CTRL_REG1,&val,1);

   val |= MPLL_ACTIVE_MASK;

   ret = mpl3115_i2c_write(client,MPL3115_CTRL_REG1, &val,1);

   return 0;

}

static int mpl3115_read_altitude_data(struct i2c_client *client,int *altitude, short *temp)

{

   u8 tmp[5];

   mpl3115_i2c_read(client,MPL3115_PRESSURE_DATA,tmp,5);

   *altitude = (DATA_SHIFT_BIT(tmp[0],24) | DATA_SHIFT_BIT(tmp[1],16)  | DATA_SHIFT_BIT(tmp[2],8) )>>12;

   *temp = (DATA_SHIFT_BIT(tmp[3],8) | DATA_SHIFT_BIT(tmp[4],0))>>4;

   return 0;

}

You might find useful my simple bare-metal example code that is available here.

I hope it helps.

Regards,

Tomas

PS: If my answer helps to solve your question, please mark it as "Correct". Thank you.

在原帖中查看解决方案

1 回复
1,788 次查看
TomasVaverka
NXP TechSupport
NXP TechSupport

Hi Chiranjeevi,

I am not a Linux expert, so I have forwarded your question to our Linux specialists, here is their response:

"We do not have a Linux application for this, but since the device use standard input device node, customer shall be able to use application tools like Evtest (google from web) to get the input data. The driver also use sysfs device attribute interface, which can be accessed with “echo” and “cat” from the device node at /sys, refer to readme file in other NXP driver package like FXAS2100x."

Anyway, I looked at the mpl3115.c file and found the following functions to put the sensor into barometric mode, activate it and read the pressure data:

static void mpl3115_device_init(struct i2c_client *client)

{

   u8 val[8];

   val[0] = 0x28;

   mpl3115_i2c_write(client,MPL3115_CTRL_REG1,val,1);

}

static int mpl3115_start_chip(struct i2c_client *client )

{

   u8 val;

   int ret;

   mpl3115_i2c_read(client,MPL3115_CTRL_REG1,&val,1);

   val |= MPLL_ACTIVE_MASK;

   ret = mpl3115_i2c_write(client,MPL3115_CTRL_REG1, &val,1);

   return 0;

}

static int mpl3115_read_data(struct i2c_client *client,int *pres, short *temp)

{

   u8 tmp[5];

   mpl3115_i2c_read(client,MPL3115_PRESSURE_DATA,tmp,5);

   *pres = (DATA_SHIFT_BIT(tmp[0],24) | DATA_SHIFT_BIT(tmp[1],16)  | DATA_SHIFT_BIT(tmp[2],8) )>>12;

   *temp = (DATA_SHIFT_BIT(tmp[3],8) | DATA_SHIFT_BIT(tmp[4],0))>>4;

   return 0;

}

To read the altitude, I would recommend using something like this:

static void mpl3115_device_altitude_init(struct i2c_client *client)

{

   u8 val[8];

   val[0] = 0xA8;          // Altimeter mode, OSR = 32

   mpl3115_i2c_write(client,MPL3115_CTRL_REG1,val,1);

}

static int mpl3115_start_chip(struct i2c_client *client )

{

   u8 val;

   int ret;

   mpl3115_i2c_read(client,MPL3115_CTRL_REG1,&val,1);

   val |= MPLL_ACTIVE_MASK;

   ret = mpl3115_i2c_write(client,MPL3115_CTRL_REG1, &val,1);

   return 0;

}

static int mpl3115_read_altitude_data(struct i2c_client *client,int *altitude, short *temp)

{

   u8 tmp[5];

   mpl3115_i2c_read(client,MPL3115_PRESSURE_DATA,tmp,5);

   *altitude = (DATA_SHIFT_BIT(tmp[0],24) | DATA_SHIFT_BIT(tmp[1],16)  | DATA_SHIFT_BIT(tmp[2],8) )>>12;

   *temp = (DATA_SHIFT_BIT(tmp[3],8) | DATA_SHIFT_BIT(tmp[4],0))>>4;

   return 0;

}

You might find useful my simple bare-metal example code that is available here.

I hope it helps.

Regards,

Tomas

PS: If my answer helps to solve your question, please mark it as "Correct". Thank you.