Rohm / rohm-bm1383-glv

Dependents:   rohm-bm1383-hello-mbedclassic

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers bm1383_driver.cpp Source File

bm1383_driver.cpp

00001 /*   Copyright 2016 Rohm Semiconductor
00002 
00003    Licensed under the Apache License, Version 2.0 (the "License");
00004    you may not use this file except in compliance with the License.
00005    You may obtain a copy of the License at
00006 
00007        http://www.apache.org/licenses/LICENSE-2.0
00008 
00009    Unless required by applicable law or agreed to in writing, software
00010    distributed under the License is distributed on an "AS IS" BASIS,
00011    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012    See the License for the specific language governing permissions and
00013    limitations under the License.
00014 */
00015 #include "../../rohm-sensor-hal/rohm-sensor-hal/rohm_hal.h"         //types, DEBUG_print*
00016 #include "../../rohm-sensor-hal/rohm-sensor-hal/I2CCommon.h"        //read_register, write_register, change_bits
00017 
00018 #include "../rohm-bm1383-glv/bm1383glv.h"      //BM1383_* register definitions
00019 #include "../rohm-bm1383-glv/bm1383_driver.h"
00020 #define SAD 0x5d
00021 
00022 
00023 /* bm1383 driver*/
00024 
00025 uint8_t bm1383_readId(){
00026   uint8_t id;
00027   uint8_t read_bytes;
00028 
00029   read_bytes = read_register(SAD, BM1383GLV_ID_REG, &id, 1);
00030   if ( read_bytes > 0 ){
00031     DEBUG_printf("Id: %d", id);
00032     return(id);
00033     }
00034   else{
00035     DEBUG_print("Sorry, ID read failed.");
00036     return 255;
00037     }
00038 }
00039 
00040 void bm1383_wait_until_found(){
00041   uint8_t id;
00042 
00043   id = bm1383_readId();
00044   while (id == 255){
00045     wait(100);
00046     id = bm1383_readId();
00047     }
00048   return;
00049   }
00050 
00051 // bm1383_mode_* -functions, modes: poweroff-reset(==sleep)-standby. Configuration can only be done in standby-mode.
00052 // For further details please refer specification diagram: "Operation mode transition"
00053 void bm1383_mode_poweroff2reset(){
00054   write_register(SAD, BM1383GLV_POWER_REG, BM1383GLV_POWER_REG_POWER_UP );
00055 }
00056 void bm1383_mode_reset2poweroff(){
00057   write_register(SAD, BM1383GLV_POWER_REG, BM1383GLV_POWER_REG_POWER_DOWN );
00058 }
00059 void bm1383_mode_reset2standby(){
00060   write_register(SAD, BM1383GLV_SLEEP_REG, BM1383GLV_SLEEP_REG_SLEEP_OFF );
00061 }
00062 void bm1383_mode_standby2reset(){
00063   write_register(SAD, BM1383GLV_SLEEP_REG, BM1383GLV_SLEEP_REG_SLEEP_ON );
00064 }
00065 
00066 bool bm1383_start_measurement_oneshot(){
00067   return change_bits(SAD, BM1383GLV_MODE_CONTROL_REG, BM1383GLV_MODE_CONTROL_REG_MODE_MASK, BM1383GLV_MODE_CONTROL_REG_MODE_ONE_SHOT);
00068 }
00069 
00070 /* input parameter continuous_mode: BM1383GLV_MODE_CONTROL_REG_MODE_X
00071    return: success true/fail */
00072 bool bm1383_start_measurement_continuous(uint8_t continuous_mode){
00073   if( (continuous_mode == BM1383GLV_MODE_CONTROL_REG_MODE_50MS) ||
00074       (continuous_mode == BM1383GLV_MODE_CONTROL_REG_MODE_100MS) ||
00075       (continuous_mode == BM1383GLV_MODE_CONTROL_REG_MODE_200MS) ){
00076     return change_bits(SAD, BM1383GLV_MODE_CONTROL_REG, BM1383GLV_MODE_CONTROL_REG_MODE_MASK, continuous_mode);
00077     }
00078   else{
00079     DEBUG_print("Wrong continuous mode.");
00080     return false;
00081     }
00082 }
00083 
00084 bool bm1383_stop_measurement(){   // return to standby mode
00085   return change_bits(SAD, BM1383GLV_MODE_CONTROL_REG, BM1383GLV_MODE_CONTROL_REG_MODE_MASK, BM1383GLV_MODE_CONTROL_REG_MODE_STANDBY);
00086 }
00087 
00088 float bm1383_read_pressure(){
00089     uint8_t incoming[3]= {0,0,0};
00090     float si_converted;
00091     uint8_t read_bytes;
00092 
00093     read_bytes = read_register(SAD, BM1383GLV_PRESSURE_OUT_MSB, &incoming[0], 3);
00094     if (read_bytes > 2){
00095         si_converted = bm1383_pressure_conversion(incoming[0], incoming[1], incoming[2]);
00096         }
00097     else {
00098         DEBUG_print("Not enough bytes from pressure registers.");
00099         si_converted = 0;
00100         }
00101     return si_converted;
00102 }
00103 
00104 float bm1383_pressure_conversion(uint8_t highB, uint8_t lowB, uint8_t leastB){
00105     float BM1383_Var = 0;
00106     float BM1383_Deci = 0;
00107     float BM1383_Pres_Conv_Out = 0;
00108 
00109     BM1383_Var  = (highB<<3) | (lowB >> 5);
00110     BM1383_Deci = ((lowB & 0x1f) << 6 | ((leastB >> 2)));
00111     BM1383_Deci = BM1383_Deci * 0.00048828125f;      //0.00048828125 = 2^-11
00112     BM1383_Pres_Conv_Out = (BM1383_Var + BM1383_Deci);
00113     return BM1383_Pres_Conv_Out;
00114     }
00115 
00116 /* return: true, when new data is available. */
00117 bool read_drdy_reg(){
00118     return false;
00119     }
00120 
00121 void bm1383_soft_reset(){
00122     write_register(SAD, BM1383GLV_RESET_CONTROL_REG, (BM1383GLV_RESET_CONTROL_REG_SW_RESET_EXECUTE | BM1383GLV_RESET_CONTROL_REG_INT_RESET_INACTIVE) );
00123 }
00124 
00125 void bm1383_set_high_treshold(uint16_t value){
00126     write_register(SAD, BM1383GLV_INT_HIGH_TRESHOLD_MSB, (value >> 8)   );
00127     write_register(SAD, BM1383GLV_INT_HIGH_TRESHOLD_LSB, (value & 0xff) );
00128     return;        //always succeed
00129 }
00130 
00131 void bm1383_set_low_treshold(uint16_t value){
00132     write_register(SAD, BM1383GLV_INT_LOW_TRESHOLD_MSB, (value >> 8)   );
00133     write_register(SAD, BM1383GLV_INT_LOW_TRESHOLD_LSB, (value & 0xff) );
00134     return;        //always succeed
00135 }
00136 
00137 bool bm1383_enable_treshold_interrupts(){
00138     return change_bits( SAD,
00139                         BM1383GLV_INT_CONTROL_REG,
00140                         (BM1383GLV_INT_CONTROL_REG_INT_HIGH_MASK | BM1383GLV_INT_CONTROL_REG_INT_LOW_MASK | BM1383GLV_INT_CONTROL_REG_INTERRUPT_MASK),
00141                         (BM1383GLV_INT_CONTROL_REG_INT_HIGH_ENABLE | BM1383GLV_INT_CONTROL_REG_INT_LOW_ENABLE | BM1383GLV_INT_CONTROL_REG_INTERRUPT_ENABLE)
00142                         );
00143 }
00144 
00145 bool bm1383_disable_treshold_interrupts(){
00146     return change_bits( SAD,
00147                         BM1383GLV_INT_CONTROL_REG,
00148                         (BM1383GLV_INT_CONTROL_REG_INT_HIGH_MASK | BM1383GLV_INT_CONTROL_REG_INT_LOW_MASK | BM1383GLV_INT_CONTROL_REG_INTERRUPT_MASK),
00149                         (BM1383GLV_INT_CONTROL_REG_INT_HIGH_DISABLE | BM1383GLV_INT_CONTROL_REG_INT_LOW_DISABLE | BM1383GLV_INT_CONTROL_REG_INTERRUPT_DISABLE)
00150                         );
00151 }
00152 
00153 bool bm1383_enable_interrupt_latching(){
00154     return change_bits(SAD, BM1383GLV_INT_CONTROL_REG, BM1383GLV_INT_CONTROL_REG_INTERRUPT_STATE_MASK, BM1383GLV_INT_CONTROL_REG_INTERRUPT_STATE_KEEP_UNTIL_CLEARED );
00155 }
00156 
00157 bool bm1383_disable_interrupt_latching(){
00158     return change_bits(SAD, BM1383GLV_INT_CONTROL_REG, BM1383GLV_INT_CONTROL_REG_INTERRUPT_STATE_MASK, BM1383GLV_INT_CONTROL_REG_INTERRUPT_STATE_CONTINUOUS_UPDATE );
00159 }
00160 
00161 bool bm1383_enable_interrupt_pullup(){
00162     return change_bits(SAD, BM1383GLV_INT_CONTROL_REG, BM1383GLV_INT_CONTROL_REG_INT_PULLUP_MASK, BM1383GLV_INT_CONTROL_REG_INT_PULLUP_ENABLE );
00163 }
00164 
00165 bool bm1383_disable_interrupt_pullup(){
00166     return change_bits(SAD, BM1383GLV_INT_CONTROL_REG, BM1383GLV_INT_CONTROL_REG_INT_PULLUP_MASK, BM1383GLV_INT_CONTROL_REG_INT_PULLUP_DISABLE );
00167 }
00168 
00169 void bm1383_clear_interrupt(){
00170     /* Clears interrupt when in latched int mode. No effect when latched mode is off. */
00171     write_register(SAD, BM1383GLV_RESET_CONTROL_REG, (BM1383GLV_RESET_CONTROL_REG_INT_RESET_INACTIVE | BM1383GLV_RESET_CONTROL_REG_SW_RESET_NONE) );
00172 }
00173 
00174 bool bm1383_is_treshold_high_crossed(){
00175     uint8_t value;
00176     read_register(SAD, BM1383GLV_INT_CONTROL_REG, &value, 1);
00177     return ( (value & BM1383GLV_INT_CONTROL_REG_TRESHOLD_HIGH_MASK) == BM1383GLV_INT_CONTROL_REG_TRESHOLD_HIGH_CROSSED );
00178 }
00179 
00180 bool bm1383_is_treshold_low_crossed(){
00181     uint8_t value;
00182     read_register(SAD, BM1383GLV_INT_CONTROL_REG, &value, 1);
00183     return ( (value & BM1383GLV_INT_CONTROL_REG_TRESHOLD_LOW_MASK) == BM1383GLV_INT_CONTROL_REG_TRESHOLD_LOW_CROSSED );
00184 }
00185 
00186