element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
WaRP7
  • Products
  • Dev Tools
  • Single-Board Computers
  • WaRP7
  • More
  • Cancel
WaRP7
Blog Warp7 Sensors mpl3115a2 demo
  • Blog
  • Forum
  • Documents
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join WaRP7 to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: tusharp
  • Date Created: 2 Nov 2016 9:10 AM Date Created
  • Views 1481 views
  • Likes 0 likes
  • Comments 6 comments
  • tusharp
  • mpl3115a2
  • nxp-warp7
  • warp7-demo
  • warp7-sensor
Related
Recommended

Warp7 Sensors mpl3115a2 demo

tusharp
tusharp
2 Nov 2016

Contents

1. Warp7 Yocto Part1

2. Warp7 Yocto Part2

3 Warp7 Sensors demo mpl3115a2 <-- You are here

4. Warp7 Sensors demo - fxos8700cq

5. Warp7 IoT demo

 

The warp7 comes with mutiple MEMS sensors.

One of the highly popular sensors is the mpl3115 sensor.

 

For mpl3115 sensor there is i2c driver support in kernel, but I was interested in the temperature part and thought to write a userspace driver myself. Honestly the i2c interface isn't that difficult. I will write about other warp7 drivers in subsequent blogs. The mpl3115 can operate in either altimeter or barometer mode depending on config register settings. In this blog we will be exploring the temperature part of the sensor. The high precision mpl3115 with the small form factor warp7 is a prefect fit for IoT and smartwatch like devices.

 

enough praising the sensor, lets write a simple bare-metal application to access the sensor.

 

We will use the linux header files i2c-dev.h and i2c.h to perform read/write on i2c bus.

We need to define the register write function.

 

static int write_register(int file,unsigned char address,unsigned char reg,unsigned char data)
{
   unsigned char output_buffer[2];
   struct i2c_rdwr_ioctl_data packets;
   struct i2c_msg messages[1];

    messages[0].addr  = address;
    messages[0].flags = 0;
    messages[0].len   = sizeof(output_buffer);
    messages[0].buf   = output_buffer;
    output_buffer[0] = reg;
    output_buffer[1] = data;
    packets.msgs  = messages;
    packets.nmsgs = 1;

    if(ioctl(file, I2C_RDWR, &packets) < 0) {
        perror("Error sending data");
        return 1;
    }
    return 0;
}

 

Similarly the register read function.

 

static int read_register(int file, unsigned char address, unsigned char reg, unsigned char *data)
{
    unsigned char input_buffer, output_buffer;
    struct i2c_rdwr_ioctl_data packets;
    struct i2c_msg messages[2];


    output_buffer = reg;
    messages[0].addr  = address;
    messages[0].flags = 0;
    messages[0].len   = sizeof(output_buffer);
    messages[0].buf   = &output_buffer;


    messages[1].addr  = address;
    messages[1].flags = I2C_M_RD;
    messages[1].len   = sizeof(input_buffer);
    messages[1].buf   = &input_buffer;


    packets.msgs      = messages;
    packets.nmsgs     = 2;
    if(ioctl(file, I2C_RDWR, &packets) < 0) {
        perror("Error sending data");
        return 1;
    }
    *data = input_buffer;


    return 0;
}

 

 

Probing...

probe the 0C(WHO_AM_I) register at i2c address 0x60.

Its should give a value hex value C4.

i2ctools package comes handy here, install it sudo apt-get install i2c-tools

 

$ i2cget -y 3 0x60 0x0C

0xc4

 

reading WHO_AMI_I register programatically using above function:

read_register(file, 0x60, 0x0C, <BUFFER_ADDRESS>)

 

For temperature calculation we are interested in three registers of the sensor:

26 – The Control register to put module in active mode and start measurement immediately.

05 – read temperature LSB  data (4 bits 0:3)

04 – read temperature MSB data (4 bits 4:11)

 

Now we will set the mpl3115 to active mode, this is done by switching the bit0(SYSB) in CTRL_REG1 from standby to active, i.e set the bit to 1.

To start getting the temp values set the bit1(OST) in CTRL_REG1 to 1.

 

write_register(file, 0x60, 0x26, 0x03)

 

with settings done, lets read data from 0x04 and 0x03 to a buffer using read_register function.

read_register(file, 0x60, 0x04, <BUFFER_ADDRESS>)

read_register(file, 0x60, 0x04, <BUFFER_ADDRESS>)

 

once data read we have to apply the shift operator couple of times to get the correct reading value .

 

The complete source code:

 

#include <stdio.h>
#include <stdint.h>
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <string.h>


static int write_register(int file,unsigned char address,unsigned char reg,unsigned char data)
{


   unsigned char output_buffer[2];
   struct i2c_rdwr_ioctl_data packets;
   struct i2c_msg messages[1];


    messages[0].addr  = address;
    messages[0].flags = 0;
    messages[0].len   = sizeof(output_buffer);
    messages[0].buf   = output_buffer;


    output_buffer[0] = reg;
    output_buffer[1] = data;


    packets.msgs  = messages;
    packets.nmsgs = 1;


    if(ioctl(file, I2C_RDWR, &packets) < 0) {
        perror("Error sending data");
        return 1;
    }


    return 0;
}




static int read_register(int file, unsigned char address, unsigned char reg, unsigned char *data)
{
    unsigned char input_buffer, output_buffer;
    struct i2c_rdwr_ioctl_data packets;
    struct i2c_msg messages[2];




    output_buffer = reg;
    messages[0].addr  = address;
    messages[0].flags = 0;
    messages[0].len   = sizeof(output_buffer);
    messages[0].buf   = &output_buffer;


    messages[1].addr  = address;
    messages[1].flags = I2C_M_RD;
    messages[1].len   = sizeof(input_buffer);
    messages[1].buf   = &input_buffer;




    packets.msgs      = messages;
    packets.nmsgs     = 2;
    if(ioctl(file, I2C_RDWR, &packets) < 0) {
        perror("Error sending data");
        return 1;
    }
    *data = input_buffer;


    return 0;
}




int main() {
    int file, i;
    unsigned char data;
    int val1;
    int val2;
    int t_m;
    int choice;
    int DBG_ENABLE = 0;
    char string[200];


printf("Enable Debugging [1-Enable/0-Disable] :");
scanf("%d",&choice);


    if (choice == 1)
        DBG_ENABLE = 1;
    else if (choice == 0)
        DBG_ENABLE = 0;
    else
        {
            printf("Unknown choice \n Exiting... \n");
            exit(1);
        }


if ((file = open("/dev/i2c-3", O_RDWR)) < 0)
{
    perror("Error openning file!");
    exit(1);
  }


if(DBG_ENABLE == 0)
printf("Debugging Disabled \n");
else if(DBG_ENABLE == 1)
printf("Debugging Enabled \n");
else
printf("No idea");




//0x0C whoami verify
   if(read_register(file, 0x60, 0x0C, &data))
    exit(1);
   else
   {
    if(DBG_ENABLE == 1)
     { printf("GET:Register[0x%02X]: 0x%02X\n" , 12 , data);
     }
   }
//0x26 ctrlreg1 get/set ACTIVE
   if(read_register(file, 0x60, 0x26, &data))
      exit(1);
   else
   {
    if(DBG_ENABLE == 1)
     { printf("GET:Register[0x%02X]: 0x%02X\n" , 38 , data);
     }
   }




   if(write_register(file, 0x60, 0x26, 0x03))
     exit(1);
   else
     {
        if(read_register(file, 0x60, 0x26, &data))
           exit(1);
        else
       {
    if(DBG_ENABLE == 1)
        {  printf("SET/GET:Register[0x%02X]: 0x%02X\n" , 38 , data);
        }
       }


     }


int counter=0;
while(counter<5)
{
//read temperature MSB/LSB
   if(read_register(file, 0x60, 0x04, &data))
     exit(1);
/*   else*/
/*    { if(DBG_ENABLE == 1)  */
/*        {   printf("Register[0x%02X]: 0x%02X\n" , 4 , data);*/
/*        }*/
/*    }*/


        val2 = (int)data;
        if(DBG_ENABLE == 1) printf("val2: initial :%d:\n",val2);
        val2 <<=8;
        if(DBG_ENABLE == 1) printf("val2: shift :%d:\n",val2);




   if(read_register(file, 0x60, 0x05, &data))
    exit(1);
   else
   {
     if(DBG_ENABLE == 1)
     printf("Register[0x%02X]: 0x%02X\n" , 5 , data);
   }


    val2 = val2+ (int)data;


    if(DBG_ENABLE == 1)
    { printf("val2 :%d:\n",val2);
    }


    t_m = (val2 >> 8) & 0xff;


   printf("temperature :%d: \n",t_m);


  system(string);
   sleep(1);
  counter++;
}
    close(file);
    return 0;
}

 

 

you will get result as below (with debug enabled):

$ ./mpl3115_temperature

image

 

The above code can be used in android/yocto platforms also.

 

NOTE: the above code access the /dev/i2c-3 interface, if MPL3115A2MPL3115A2 driver enabled in kernel, the code will fail due to device blocking by driver.

 

To enable supported drivers in kernel, change the values to "y" in .config (as show below) or edit in "menuconfig".

# CONFIG_SENSORS_FXOS8700 is not set

# CONFIG_SENSORS_FXAS2100X is not set

# CONFIG_INPUT_MPL3115 is not set

to

CONFIG_SENSORS_FXOS8700=y

CONFIG_SENSORS_FXAS2100X=y

CONFIG_INPUT_MPL3115=y

 

Happy Sensing.

Attachments:
mpl3115_temperature.zip
  • Sign in to reply

Top Comments

  • bheemarao
    bheemarao over 8 years ago in reply to tbsi +3
    Hi C code can be compiled in standalone or as a package builder, here it has been executed in standalone procedure: $ bitbake <image> -c populate_sdk will generates an SDK script to reproduce the rootfs…
  • clem57
    clem57 over 8 years ago +1
    Jan Cumps You may want to read this as it relates to your new toy! Hope you enjoy it. Clem
  • bheemarao
    bheemarao over 8 years ago in reply to bheemarao +1
    I have posted a blog on warp7 cross compile and its Toolchain click the link here WaRP7 : Regards Bheema Rao
  • bheemarao
    bheemarao over 8 years ago in reply to bheemarao

    I have posted a blog on warp7 cross compile and its Toolchain

    click the link here WaRP7 :

     

    Regards

    Bheema Rao

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • bheemarao
    bheemarao over 8 years ago in reply to tbsi

    Hi

    C code can be compiled in standalone or as a package builder, here it has been executed in standalone procedure:

     

    $ bitbake <image> -c populate_sdk


    will generates an SDK script to reproduce the rootfs and build environment for non-Yocto Project build systems.

    the compiler by default gets installed in /opt/poky/ folder version of the cross compiler.

     

    i have tested using below image

     

    $ bitbake fsl-image-machine-test -c populate_sdk

     

    i will be posting a detailed procedure on how to cross compile the helloworld.c for warp7 target board shortly.

     

    Regards

    Bheema Rao

    • Cancel
    • Vote Up +3 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • tbsi
    tbsi over 8 years ago in reply to kazzak

    Hi kazzak,

     

    have you any solution?

     

    Thanks.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Jan Cumps
    Jan Cumps over 8 years ago in reply to clem57

    Thanks Clem. I'm plundering e14 and the whole web at the moment for info and examples.

    At this moment, I'm checking if either a RPi3 or BB can serve as linux dev machine for the uboot + linux build.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • clem57
    clem57 over 8 years ago

    Jan Cumps

         You may want to read this as it relates to your new toy! Hope you enjoy it.

    Clem

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
>
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube