BM1383AGLV sensor library
Dependents: simple-sensor-client rohm-SensorShield-example
BM1383AGLV.cpp@1:997a1eb76a4f, 2020-11-20 (annotated)
- Committer:
- MACRUM
- Date:
- Fri Nov 20 14:55:11 2020 +0000
- Revision:
- 1:997a1eb76a4f
- Parent:
- 0:d505f3c67a62
Add ready-status check
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MACRUM | 0:d505f3c67a62 | 1 | /* Copyright (c) 2015 ARM Ltd., MIT License |
MACRUM | 0:d505f3c67a62 | 2 | * |
MACRUM | 0:d505f3c67a62 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
MACRUM | 0:d505f3c67a62 | 4 | * and associated documentation files (the "Software"), to deal in the Software without |
MACRUM | 0:d505f3c67a62 | 5 | * restriction, including without limitation the rights to use, copy, modify, merge, publish, |
MACRUM | 0:d505f3c67a62 | 6 | * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the |
MACRUM | 0:d505f3c67a62 | 7 | * Software is furnished to do so, subject to the following conditions: |
MACRUM | 0:d505f3c67a62 | 8 | * |
MACRUM | 0:d505f3c67a62 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
MACRUM | 0:d505f3c67a62 | 10 | * substantial portions of the Software. |
MACRUM | 0:d505f3c67a62 | 11 | * |
MACRUM | 0:d505f3c67a62 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
MACRUM | 0:d505f3c67a62 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
MACRUM | 0:d505f3c67a62 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
MACRUM | 0:d505f3c67a62 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
MACRUM | 0:d505f3c67a62 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
MACRUM | 0:d505f3c67a62 | 17 | * |
MACRUM | 0:d505f3c67a62 | 18 | * |
MACRUM | 0:d505f3c67a62 | 19 | * BM1383AGLV Pressure sensor library |
MACRUM | 0:d505f3c67a62 | 20 | * |
MACRUM | 0:d505f3c67a62 | 21 | * @author Toyomasa Watarai |
MACRUM | 0:d505f3c67a62 | 22 | * @version 1.0 |
MACRUM | 0:d505f3c67a62 | 23 | * @date 30-December-2015 |
MACRUM | 0:d505f3c67a62 | 24 | * |
MACRUM | 0:d505f3c67a62 | 25 | * Library for "BM1383AGLV Pressure sensor library" |
MACRUM | 0:d505f3c67a62 | 26 | * http://www.rohm.com/web/eu/news-detail?news-title=2014-12-11_ad_pressure&defaultGroupId=false |
MACRUM | 0:d505f3c67a62 | 27 | * |
MACRUM | 0:d505f3c67a62 | 28 | */ |
MACRUM | 0:d505f3c67a62 | 29 | |
MACRUM | 0:d505f3c67a62 | 30 | #include "mbed.h" |
MACRUM | 0:d505f3c67a62 | 31 | #include "BM1383AGLV.h" |
MACRUM | 0:d505f3c67a62 | 32 | |
MACRUM | 1:997a1eb76a4f | 33 | BM1383AGLV::BM1383AGLV(PinName sda, PinName scl, int addr) |
MACRUM | 1:997a1eb76a4f | 34 | : |
MACRUM | 1:997a1eb76a4f | 35 | i2c_p(new I2C(sda, scl)), |
MACRUM | 1:997a1eb76a4f | 36 | i2c(*i2c_p), |
MACRUM | 1:997a1eb76a4f | 37 | m_addr(addr) |
MACRUM | 0:d505f3c67a62 | 38 | { |
MACRUM | 0:d505f3c67a62 | 39 | initialize(); |
MACRUM | 0:d505f3c67a62 | 40 | } |
MACRUM | 0:d505f3c67a62 | 41 | |
MACRUM | 1:997a1eb76a4f | 42 | BM1383AGLV::BM1383AGLV(I2C &i2c_obj, int addr) |
MACRUM | 1:997a1eb76a4f | 43 | : |
MACRUM | 1:997a1eb76a4f | 44 | i2c_p(NULL), |
MACRUM | 1:997a1eb76a4f | 45 | i2c(i2c_obj), |
MACRUM | 1:997a1eb76a4f | 46 | m_addr(addr) |
MACRUM | 0:d505f3c67a62 | 47 | { |
MACRUM | 0:d505f3c67a62 | 48 | initialize(); |
MACRUM | 0:d505f3c67a62 | 49 | } |
MACRUM | 0:d505f3c67a62 | 50 | |
MACRUM | 0:d505f3c67a62 | 51 | BM1383AGLV::~BM1383AGLV() |
MACRUM | 0:d505f3c67a62 | 52 | { |
MACRUM | 1:997a1eb76a4f | 53 | if (NULL != i2c_p) |
MACRUM | 1:997a1eb76a4f | 54 | delete i2c_p; |
MACRUM | 0:d505f3c67a62 | 55 | } |
MACRUM | 0:d505f3c67a62 | 56 | |
MACRUM | 0:d505f3c67a62 | 57 | void BM1383AGLV::initialize() |
MACRUM | 0:d505f3c67a62 | 58 | { |
MACRUM | 0:d505f3c67a62 | 59 | unsigned char buf1, buf2; |
MACRUM | 0:d505f3c67a62 | 60 | unsigned char reg[2]; |
MACRUM | 0:d505f3c67a62 | 61 | |
MACRUM | 0:d505f3c67a62 | 62 | readRegs(BM1383AGLV_ID1, &buf1, sizeof(buf1)); |
MACRUM | 0:d505f3c67a62 | 63 | readRegs(BM1383AGLV_ID2, &buf2, sizeof(buf2)); |
MACRUM | 0:d505f3c67a62 | 64 | if ((buf1 != BM1383AGLV_ID1_VAL) || (buf2 != BM1383AGLV_ID2_VAL)) { |
MACRUM | 0:d505f3c67a62 | 65 | DEBUG_PRINT("BM1383AGLV initialization error. (%d, %d)\n", buf1, buf2); |
MACRUM | 0:d505f3c67a62 | 66 | return; |
MACRUM | 0:d505f3c67a62 | 67 | } |
MACRUM | 0:d505f3c67a62 | 68 | |
MACRUM | 0:d505f3c67a62 | 69 | reg[0] = BM1383AGLV_POWER_DOWN; |
MACRUM | 0:d505f3c67a62 | 70 | reg[1] = 0x01; |
MACRUM | 0:d505f3c67a62 | 71 | writeRegs(reg, 2); |
MACRUM | 0:d505f3c67a62 | 72 | |
MACRUM | 0:d505f3c67a62 | 73 | wait(1); |
MACRUM | 0:d505f3c67a62 | 74 | |
MACRUM | 0:d505f3c67a62 | 75 | reg[0] = BM1383AGLV_RESET; |
MACRUM | 0:d505f3c67a62 | 76 | reg[1] = 0x01; |
MACRUM | 0:d505f3c67a62 | 77 | writeRegs(reg, 2); |
MACRUM | 0:d505f3c67a62 | 78 | |
MACRUM | 0:d505f3c67a62 | 79 | reg[0] = BM1383AGLV_MODE_CONTROL; |
MACRUM | 0:d505f3c67a62 | 80 | reg[1] = 0xC2; // x32, continuous measurement |
MACRUM | 0:d505f3c67a62 | 81 | writeRegs(reg, 2); |
MACRUM | 0:d505f3c67a62 | 82 | } |
MACRUM | 0:d505f3c67a62 | 83 | |
MACRUM | 0:d505f3c67a62 | 84 | float BM1383AGLV::getPressure() |
MACRUM | 0:d505f3c67a62 | 85 | { |
MACRUM | 0:d505f3c67a62 | 86 | uint32_t rawPressure; |
MACRUM | 0:d505f3c67a62 | 87 | |
MACRUM | 1:997a1eb76a4f | 88 | #if BM1383AGLV_WAIT_READY_STATUS |
MACRUM | 1:997a1eb76a4f | 89 | uint8_t ready_status = 0; |
MACRUM | 1:997a1eb76a4f | 90 | do { |
MACRUM | 1:997a1eb76a4f | 91 | wait_ms(6); |
MACRUM | 1:997a1eb76a4f | 92 | readRegs(BM1383AGLV_STATUS, &ready_status, 1); |
MACRUM | 1:997a1eb76a4f | 93 | } while(ready_status == 0); |
MACRUM | 1:997a1eb76a4f | 94 | #else |
MACRUM | 1:997a1eb76a4f | 95 | wait_ms(6); |
MACRUM | 1:997a1eb76a4f | 96 | #endif |
MACRUM | 0:d505f3c67a62 | 97 | readRegs(BM1383AGLV_PRESSURE_MSB, m_buf, 3); |
MACRUM | 0:d505f3c67a62 | 98 | rawPressure = (((uint32_t)m_buf[0] << 16) | ((uint32_t)m_buf[1] << 8) | m_buf[2]&0xFC) >> 2; |
MACRUM | 0:d505f3c67a62 | 99 | return (float)rawPressure / (2048); |
MACRUM | 0:d505f3c67a62 | 100 | } |
MACRUM | 0:d505f3c67a62 | 101 | |
MACRUM | 0:d505f3c67a62 | 102 | float BM1383AGLV::getTemperature() |
MACRUM | 0:d505f3c67a62 | 103 | { |
MACRUM | 0:d505f3c67a62 | 104 | int32_t rawTemerature; |
MACRUM | 0:d505f3c67a62 | 105 | |
MACRUM | 1:997a1eb76a4f | 106 | #if BM1383AGLV_WAIT_READY_STATUS |
MACRUM | 1:997a1eb76a4f | 107 | uint8_t ready_status = 0; |
MACRUM | 1:997a1eb76a4f | 108 | do { |
MACRUM | 1:997a1eb76a4f | 109 | wait_ms(6); |
MACRUM | 1:997a1eb76a4f | 110 | readRegs(BM1383AGLV_STATUS, &ready_status, 1); |
MACRUM | 1:997a1eb76a4f | 111 | } while(ready_status == 0); |
MACRUM | 1:997a1eb76a4f | 112 | #else |
MACRUM | 1:997a1eb76a4f | 113 | wait_ms(6); |
MACRUM | 1:997a1eb76a4f | 114 | #endif |
MACRUM | 0:d505f3c67a62 | 115 | readRegs(BM1383AGLV_TEMPERATURE_MSB, m_buf, 2); |
MACRUM | 0:d505f3c67a62 | 116 | rawTemerature = ((int32_t)m_buf[0] << 8) | (m_buf[1]); |
MACRUM | 0:d505f3c67a62 | 117 | return (float)rawTemerature / 32; |
MACRUM | 0:d505f3c67a62 | 118 | } |
MACRUM | 0:d505f3c67a62 | 119 | |
MACRUM | 0:d505f3c67a62 | 120 | void BM1383AGLV::readRegs(int addr, uint8_t * data, int len) |
MACRUM | 0:d505f3c67a62 | 121 | { |
MACRUM | 0:d505f3c67a62 | 122 | char t[1] = {addr}; |
MACRUM | 1:997a1eb76a4f | 123 | i2c.write(m_addr, t, 1, true); |
MACRUM | 1:997a1eb76a4f | 124 | i2c.read(m_addr, (char *)data, len); |
MACRUM | 0:d505f3c67a62 | 125 | } |
MACRUM | 0:d505f3c67a62 | 126 | |
MACRUM | 0:d505f3c67a62 | 127 | void BM1383AGLV::writeRegs(uint8_t * data, int len) |
MACRUM | 0:d505f3c67a62 | 128 | { |
MACRUM | 1:997a1eb76a4f | 129 | i2c.write(m_addr, (char *)data, len); |
MACRUM | 0:d505f3c67a62 | 130 | } |