Rohm BH1726 ALS sensor driver.

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

Fork of rohm-bh1745 by Rohm

Committer:
MikkoZ
Date:
Mon Sep 12 12:31:40 2016 +0000
Revision:
1:64629eee9eab
Parent:
source/rpr0521_driver.cpp@0:0bcc203c5c75
Initial version of bh1745 driver

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MikkoZ 0:0bcc203c5c75 1 /* Copyright 2016 Rohm Semiconductor
MikkoZ 0:0bcc203c5c75 2
MikkoZ 0:0bcc203c5c75 3 Licensed under the Apache License, Version 2.0 (the "License");
MikkoZ 0:0bcc203c5c75 4 you may not use this file except in compliance with the License.
MikkoZ 0:0bcc203c5c75 5 You may obtain a copy of the License at
MikkoZ 0:0bcc203c5c75 6
MikkoZ 0:0bcc203c5c75 7 http://www.apache.org/licenses/LICENSE-2.0
MikkoZ 0:0bcc203c5c75 8
MikkoZ 0:0bcc203c5c75 9 Unless required by applicable law or agreed to in writing, software
MikkoZ 0:0bcc203c5c75 10 distributed under the License is distributed on an "AS IS" BASIS,
MikkoZ 0:0bcc203c5c75 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
MikkoZ 0:0bcc203c5c75 12 See the License for the specific language governing permissions and
MikkoZ 0:0bcc203c5c75 13 limitations under the License.
MikkoZ 0:0bcc203c5c75 14 */
MikkoZ 0:0bcc203c5c75 15 #include "../../rohm-sensor-hal/rohm-sensor-hal/rohm_hal.h" //types, DEBUG_print*
MikkoZ 0:0bcc203c5c75 16 #include "../../rohm-sensor-hal/rohm-sensor-hal/I2CCommon.h" //read_register, write_register, change_bits
MikkoZ 0:0bcc203c5c75 17
MikkoZ 1:64629eee9eab 18 #include "../rohm-bh1745/bh1745.h" //bh1745_* register definitions
MikkoZ 1:64629eee9eab 19 #include "../rohm-bh1745/bh1745_driver.h"
MikkoZ 1:64629eee9eab 20 //Choose SAD according to setup
MikkoZ 1:64629eee9eab 21 //#define SAD 0x38
MikkoZ 1:64629eee9eab 22 #define SAD 0x39
MikkoZ 0:0bcc203c5c75 23
MikkoZ 1:64629eee9eab 24 /* bh1745 driver*/
MikkoZ 1:64629eee9eab 25 uint8_t bh1745_readId(){
MikkoZ 1:64629eee9eab 26 uint8_t id;
MikkoZ 1:64629eee9eab 27 uint8_t read_bytes;
MikkoZ 0:0bcc203c5c75 28
MikkoZ 1:64629eee9eab 29 read_bytes = read_register(SAD, BH1745_ID_REG, &id, 1);
MikkoZ 1:64629eee9eab 30 if ( read_bytes > 0 ){
MikkoZ 1:64629eee9eab 31 uint8_t partid;
MikkoZ 1:64629eee9eab 32
MikkoZ 1:64629eee9eab 33 DEBUG_printf("Manufacturer: %u\n\r", id);
MikkoZ 1:64629eee9eab 34 read_bytes = read_register(SAD, BH1745_SYSTEM_CONTROL, &partid, 1);
MikkoZ 1:64629eee9eab 35 if ( read_bytes > 0 ){
MikkoZ 1:64629eee9eab 36 DEBUG_printf("Part ID: %u\n\r", (partid & 0b00111111) );
MikkoZ 1:64629eee9eab 37 return(partid);
MikkoZ 1:64629eee9eab 38 }
MikkoZ 1:64629eee9eab 39 else{
MikkoZ 1:64629eee9eab 40 DEBUG_print("Part ID read failed.\n\r");
MikkoZ 1:64629eee9eab 41 return 255;
MikkoZ 1:64629eee9eab 42 }
MikkoZ 0:0bcc203c5c75 43 }
MikkoZ 1:64629eee9eab 44 else{
MikkoZ 1:64629eee9eab 45 DEBUG_print("Manufacturer read failed.\n\r");
MikkoZ 1:64629eee9eab 46 return 255;
MikkoZ 0:0bcc203c5c75 47 }
MikkoZ 0:0bcc203c5c75 48 }
MikkoZ 0:0bcc203c5c75 49
MikkoZ 1:64629eee9eab 50 void bh1745_wait_until_found(){
MikkoZ 0:0bcc203c5c75 51 uint8_t id;
MikkoZ 0:0bcc203c5c75 52
MikkoZ 1:64629eee9eab 53 id = bh1745_readId();
MikkoZ 0:0bcc203c5c75 54 while (id == 255){
MikkoZ 0:0bcc203c5c75 55 wait(100);
MikkoZ 1:64629eee9eab 56 id = bh1745_readId();
MikkoZ 0:0bcc203c5c75 57 }
MikkoZ 0:0bcc203c5c75 58 return;
MikkoZ 0:0bcc203c5c75 59 }
MikkoZ 0:0bcc203c5c75 60
MikkoZ 1:64629eee9eab 61 void bh1745_soft_reset(){
MikkoZ 1:64629eee9eab 62 change_bits(SAD, BH1745_MODE_CONTROL1, BH1745_SYSTEM_CONTROL_SW_RESET_MASK, BH1745_SYSTEM_CONTROL_SW_RESET_START);
MikkoZ 0:0bcc203c5c75 63 }
MikkoZ 0:0bcc203c5c75 64
MikkoZ 1:64629eee9eab 65 void bh1745_clear_interrupt(){
MikkoZ 1:64629eee9eab 66 uint8_t tmp;
MikkoZ 1:64629eee9eab 67 read_register(SAD, BH1745_INTERRUPT, &tmp, 1);
MikkoZ 0:0bcc203c5c75 68 }
MikkoZ 0:0bcc203c5c75 69
MikkoZ 1:64629eee9eab 70 void bh1745_initial_setup(){
MikkoZ 1:64629eee9eab 71 write_register(SAD, BH1745_INTERRUPT,
MikkoZ 1:64629eee9eab 72 (BH1745_INTERRUPT_STATUS_INACTIVE |
MikkoZ 1:64629eee9eab 73 BH1745_INTERRUPT_LATCH_ENABLE |
MikkoZ 1:64629eee9eab 74 BH1745_INTERRUPT_PIN_DISABLE)
MikkoZ 0:0bcc203c5c75 75 );
MikkoZ 1:64629eee9eab 76 write_register(SAD, BH1745_PERSISTENCE,
MikkoZ 1:64629eee9eab 77 (BH1745_PERSISTENCE_OF_INTERRUPT_STATUS_TOGGLE_AFTER_MEASUREMENT)
MikkoZ 0:0bcc203c5c75 78 );
MikkoZ 1:64629eee9eab 79 write_register(SAD, BH1745_MODE_CONTROL1,
MikkoZ 1:64629eee9eab 80 (BH1745_MODE_CONTROL1_MEASUREMENT_TIME_160MSEC)
MikkoZ 1:64629eee9eab 81 );
MikkoZ 1:64629eee9eab 82 write_register(SAD, BH1745_MODE_CONTROL2,
MikkoZ 1:64629eee9eab 83 (BH1745_MODE_CONTROL2_RGBC_MEASUREMENT_ACTIVE |
MikkoZ 1:64629eee9eab 84 BH1745_MODE_CONTROL2_ADC_GAIN_1X)
MikkoZ 0:0bcc203c5c75 85 );
MikkoZ 0:0bcc203c5c75 86 }
MikkoZ 0:0bcc203c5c75 87
MikkoZ 1:64629eee9eab 88 /* input param: data16, pointer to 4*16bit memory
MikkoZ 0:0bcc203c5c75 89 return: error, true/false */
MikkoZ 1:64629eee9eab 90 bool bh1745_read_data(uint16_t* data16){
MikkoZ 1:64629eee9eab 91 #define BH1745_DATA_LEN 8
MikkoZ 1:64629eee9eab 92 uint8_t data[BH1745_DATA_LEN];
MikkoZ 0:0bcc203c5c75 93 uint8_t read_bytes;
MikkoZ 0:0bcc203c5c75 94
MikkoZ 1:64629eee9eab 95 read_bytes = read_register(SAD, BH1745_RED_DATA_LSBS, &data[0], BH1745_DATA_LEN);
MikkoZ 1:64629eee9eab 96 if (read_bytes == BH1745_DATA_LEN){
MikkoZ 1:64629eee9eab 97 data16[0] = (data[0]) | (data[1] << 8); //red
MikkoZ 1:64629eee9eab 98 data16[1] = (data[2]) | (data[3] << 8); //green
MikkoZ 1:64629eee9eab 99 data16[2] = (data[4]) | (data[5] << 8); //blue
MikkoZ 1:64629eee9eab 100 data16[3] = (data[6]) | (data[7] << 8); //clear
MikkoZ 0:0bcc203c5c75 101 return false;
MikkoZ 0:0bcc203c5c75 102 }
MikkoZ 0:0bcc203c5c75 103 else{
MikkoZ 0:0bcc203c5c75 104 DEBUG_printf("Read error. Read %d bytes\n\r", read_bytes);
MikkoZ 0:0bcc203c5c75 105 return true;
MikkoZ 0:0bcc203c5c75 106 }
MikkoZ 0:0bcc203c5c75 107
MikkoZ 0:0bcc203c5c75 108 }