BM1383AGLV sensor library

Dependents:   simple-sensor-client rohm-SensorShield-example

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers BM1383AGLV.cpp Source File

BM1383AGLV.cpp

00001 /* Copyright (c) 2015 ARM Ltd., MIT License
00002 *
00003 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00004 * and associated documentation files (the "Software"), to deal in the Software without
00005 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
00006 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
00007 * Software is furnished to do so, subject to the following conditions:
00008 *
00009 * The above copyright notice and this permission notice shall be included in all copies or
00010 * substantial portions of the Software.
00011 *
00012 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00013 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00014 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00015 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00016 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00017 *
00018 *
00019 *  BM1383AGLV Pressure sensor library
00020 *
00021 *  @author  Toyomasa Watarai
00022 *  @version 1.0
00023 *  @date    30-December-2015
00024 *
00025 *  Library for "BM1383AGLV Pressure sensor library"
00026 *    http://www.rohm.com/web/eu/news-detail?news-title=2014-12-11_ad_pressure&defaultGroupId=false
00027 *
00028 */
00029 
00030 #include "mbed.h"
00031 #include "BM1383AGLV.h"
00032 
00033 BM1383AGLV::BM1383AGLV(PinName sda, PinName scl, int addr)
00034     :
00035     i2c_p(new I2C(sda, scl)), 
00036     i2c(*i2c_p),
00037     m_addr(addr)
00038 {
00039     initialize();
00040 }
00041 
00042 BM1383AGLV::BM1383AGLV(I2C &i2c_obj, int addr)
00043     :
00044     i2c_p(NULL), 
00045     i2c(i2c_obj),
00046     m_addr(addr)
00047 {
00048     initialize();
00049 }
00050 
00051 BM1383AGLV::~BM1383AGLV()
00052 {
00053     if (NULL != i2c_p)
00054         delete  i2c_p;
00055 }
00056 
00057 void BM1383AGLV::initialize()
00058 {
00059     unsigned char buf1, buf2;
00060     unsigned char reg[2];
00061 
00062     readRegs(BM1383AGLV_ID1, &buf1, sizeof(buf1));
00063     readRegs(BM1383AGLV_ID2, &buf2, sizeof(buf2));
00064     if ((buf1 != BM1383AGLV_ID1_VAL) || (buf2 != BM1383AGLV_ID2_VAL)) {
00065         DEBUG_PRINT("BM1383AGLV initialization error. (%d, %d)\n", buf1, buf2);
00066         return;
00067     }
00068 
00069     reg[0] = BM1383AGLV_POWER_DOWN;
00070     reg[1] = 0x01;
00071     writeRegs(reg, 2);
00072     
00073     wait(1);
00074 
00075     reg[0] = BM1383AGLV_RESET;
00076     reg[1] = 0x01;
00077     writeRegs(reg, 2);
00078 
00079     reg[0] = BM1383AGLV_MODE_CONTROL;
00080     reg[1] = 0xC2;  // x32, continuous measurement
00081     writeRegs(reg, 2);
00082 }
00083 
00084 float BM1383AGLV::getPressure()
00085 {
00086     uint32_t rawPressure;
00087 
00088 #if BM1383AGLV_WAIT_READY_STATUS
00089     uint8_t ready_status = 0;
00090     do {
00091         wait_ms(6);
00092         readRegs(BM1383AGLV_STATUS, &ready_status, 1);
00093     } while(ready_status == 0);
00094 #else
00095     wait_ms(6);
00096 #endif
00097     readRegs(BM1383AGLV_PRESSURE_MSB, m_buf, 3);
00098     rawPressure = (((uint32_t)m_buf[0] << 16) | ((uint32_t)m_buf[1] << 8) | m_buf[2]&0xFC) >> 2;
00099     return (float)rawPressure / (2048);
00100 }
00101 
00102 float BM1383AGLV::getTemperature()
00103 {
00104     int32_t rawTemerature;
00105     
00106 #if BM1383AGLV_WAIT_READY_STATUS
00107     uint8_t ready_status = 0;
00108     do {
00109         wait_ms(6);
00110         readRegs(BM1383AGLV_STATUS, &ready_status, 1);
00111     } while(ready_status == 0);
00112 #else
00113     wait_ms(6);
00114 #endif
00115     readRegs(BM1383AGLV_TEMPERATURE_MSB, m_buf, 2);
00116     rawTemerature = ((int32_t)m_buf[0] << 8) | (m_buf[1]);
00117     return (float)rawTemerature / 32;
00118 }
00119 
00120 void BM1383AGLV::readRegs(int addr, uint8_t * data, int len)
00121 {
00122     char t[1] = {addr};
00123     i2c.write(m_addr, t, 1, true);
00124     i2c.read(m_addr, (char *)data, len);
00125 }
00126 
00127 void BM1383AGLV::writeRegs(uint8_t * data, int len)
00128 {
00129     i2c.write(m_addr, (char *)data, len);
00130 }