Rohm / rohm-bh1726

Dependents:   rohm-bh1726-hello rohm-tileshield-6sensor-demo

Fork of rohm-bh1745 by Rohm

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers bh1726_driver.cpp Source File

bh1726_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-bh1726/bh1726.h"      //bh1726_* register definitions
00019 #include "../rohm-bh1726/bh1726_driver.h"
00020 //Choose SAD according to setup
00021 #define SAD  0x29
00022 //#define SAD 0x39
00023 
00024 /* bh1726 driver*/
00025 uint8_t bh1726_readId(){
00026     uint8_t partid;
00027     uint8_t read_bytes;
00028 
00029     read_bytes = read_register(SAD, BH1726_ID, &partid, 1);
00030     if ( read_bytes > 0 ){
00031         DEBUG_printf("Part ID: %u\n\r", partid);
00032         return(partid);
00033         }
00034     else{
00035         DEBUG_print("Part ID read failed.\n\r");
00036         return 255;
00037         }
00038 }
00039 
00040 void bh1726_dumpregs(uint8_t dumpstart, uint8_t dumpend){
00041     uint8_t i;
00042     uint8_t value;
00043     uint8_t read_bytes;
00044     
00045     for(i = dumpstart;i < dumpend;i++){
00046         read_bytes = read_register(SAD, i, &value, 1);
00047         if ( read_bytes > 0 ){
00048             DEBUG_printf("Register [%3u]=[%3u]\n\r", i, value);
00049             }
00050         }
00051     return;
00052 }
00053 
00054 void bh1726_wait_until_found(){
00055   uint8_t id;
00056 
00057   DEBUG_printf("Hoping to find part ID: %u\n\r", BH1726_ID_NUMBER_FOR_PART);
00058   id = bh1726_readId();
00059   while (id != BH1726_ID_NUMBER_FOR_PART){
00060     wait_ms(100);
00061     id = bh1726_readId();
00062     }
00063   return;
00064   }
00065 
00066 void bh1726_soft_reset(){
00067     write_register(SAD, BH1726_SOFTWARE_RESET, 0);
00068 }
00069 
00070 void bh1726_clear_interrupt(){
00071     write_register(SAD, BH1726_INTERRUPT_OUTPUT_RESET, 0);
00072 }
00073 
00074 void bh1726_initial_setup(){
00075     write_register(SAD, BH1726_TIMING,
00076         (BH1726_TIMING_ITIME_38CYCLE)   //105ms measurement time
00077         );
00078     write_register(SAD, BH1726_INTERRUPT,
00079         (BH1726_INTERRUPT_INT_LATCH_YES |
00080         BH1726_INTERRUPT_INT_EN_FALSE |
00081         BH1726_INTERRUPT_PERSIST_DRDY)
00082         );
00083     write_register(SAD, BH1726_GAIN,
00084         (BH1726_GAIN_GAIN0_X1 |
00085          BH1726_GAIN_GAIN1_X1)
00086         );
00087     write_register(SAD, BH1726_WAIT,
00088         (BH1726_WAIT_WAIT_NO)
00089         );
00090     write_register(SAD, BH1726_CONTROL,
00091         (BH1726_CONTROL_ADC_EN_TRUE |
00092         BH1726_CONTROL_POWER_ON)
00093         );
00094 }
00095 
00096 /* input param: data16, pointer to 4*16bit memory 
00097    return: error, true/false */
00098 bool bh1726_read_data(uint16_t* data16){
00099     #define BH1726_DATA_LEN 4
00100     uint8_t data[BH1726_DATA_LEN];
00101     uint8_t read_bytes;
00102 
00103     read_bytes = read_register(SAD, BH1726_DATA0_LSBS, &data[0], BH1726_DATA_LEN);
00104     if (read_bytes == BH1726_DATA_LEN){
00105         data16[0] = (data[0]) | (data[1] << 8); //data0
00106         data16[1] = (data[2]) | (data[3] << 8); //data1
00107         return false;
00108         }
00109     else{
00110         DEBUG_printf("Read error. Read %d bytes\n\r", read_bytes);
00111         return true;
00112         }
00113    
00114     }