Rohm BH1726 ALS sensor driver.
Dependents: rohm-bh1726-hello rohm-tileshield-6sensor-demo
Fork of rohm-bh1745 by
source/bh1726_driver.cpp@2:40307a303153, 2016-09-14 (annotated)
- Committer:
- MikkoZ
- Date:
- Wed Sep 14 07:14:36 2016 +0000
- Revision:
- 2:40307a303153
- Parent:
- source/bh1745_driver.cpp@1:64629eee9eab
- Child:
- 3:6eb91c7c6560
Rohm BH1726 dual channel ALS sensor driver for 400-700nm wavelength.
Who changed what in which revision?
User | Revision | Line number | New 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 | 2:40307a303153 | 18 | #include "../rohm-bh1726/bh1726.h" //bh1726_* register definitions |
MikkoZ | 2:40307a303153 | 19 | #include "../rohm-bh1726/bh1726_driver.h" |
MikkoZ | 1:64629eee9eab | 20 | //Choose SAD according to setup |
MikkoZ | 2:40307a303153 | 21 | //#define SAD 0x29 |
MikkoZ | 1:64629eee9eab | 22 | #define SAD 0x39 |
MikkoZ | 0:0bcc203c5c75 | 23 | |
MikkoZ | 2:40307a303153 | 24 | /* bh1726 driver*/ |
MikkoZ | 2:40307a303153 | 25 | uint8_t bh1726_readId(){ |
MikkoZ | 2:40307a303153 | 26 | uint8_t partid; |
MikkoZ | 1:64629eee9eab | 27 | uint8_t read_bytes; |
MikkoZ | 0:0bcc203c5c75 | 28 | |
MikkoZ | 2:40307a303153 | 29 | read_bytes = read_register(SAD, BH1726_ID, &partid, 1); |
MikkoZ | 1:64629eee9eab | 30 | if ( read_bytes > 0 ){ |
MikkoZ | 2:40307a303153 | 31 | DEBUG_printf("Part ID: %u\n\r", partid); |
MikkoZ | 1:64629eee9eab | 32 | return(partid); |
MikkoZ | 1:64629eee9eab | 33 | } |
MikkoZ | 2:40307a303153 | 34 | else{ |
MikkoZ | 2:40307a303153 | 35 | DEBUG_print("Part ID read failed.\n\r"); |
MikkoZ | 2:40307a303153 | 36 | return 255; |
MikkoZ | 1:64629eee9eab | 37 | } |
MikkoZ | 0:0bcc203c5c75 | 38 | } |
MikkoZ | 0:0bcc203c5c75 | 39 | |
MikkoZ | 2:40307a303153 | 40 | void bh1726_dumpregs(uint8_t dumpstart, uint8_t dumpend){ |
MikkoZ | 2:40307a303153 | 41 | uint8_t i; |
MikkoZ | 2:40307a303153 | 42 | uint8_t value; |
MikkoZ | 2:40307a303153 | 43 | uint8_t read_bytes; |
MikkoZ | 2:40307a303153 | 44 | |
MikkoZ | 2:40307a303153 | 45 | for(i = dumpstart;i < dumpend;i++){ |
MikkoZ | 2:40307a303153 | 46 | read_bytes = read_register(SAD, i, &value, 1); |
MikkoZ | 2:40307a303153 | 47 | if ( read_bytes > 0 ){ |
MikkoZ | 2:40307a303153 | 48 | DEBUG_printf("Register [%3u]=[%3u]\n\r", i, value); |
MikkoZ | 2:40307a303153 | 49 | } |
MikkoZ | 2:40307a303153 | 50 | } |
MikkoZ | 2:40307a303153 | 51 | return; |
MikkoZ | 2:40307a303153 | 52 | } |
MikkoZ | 2:40307a303153 | 53 | |
MikkoZ | 2:40307a303153 | 54 | void bh1726_wait_until_found(){ |
MikkoZ | 0:0bcc203c5c75 | 55 | uint8_t id; |
MikkoZ | 0:0bcc203c5c75 | 56 | |
MikkoZ | 2:40307a303153 | 57 | DEBUG_printf("Hoping to find part ID: %u\n\r", BH1726_ID_NUMBER_FOR_PART); |
MikkoZ | 2:40307a303153 | 58 | id = bh1726_readId(); |
MikkoZ | 2:40307a303153 | 59 | while (id != BH1726_ID_NUMBER_FOR_PART){ |
MikkoZ | 2:40307a303153 | 60 | wait_ms(100); |
MikkoZ | 2:40307a303153 | 61 | id = bh1726_readId(); |
MikkoZ | 0:0bcc203c5c75 | 62 | } |
MikkoZ | 0:0bcc203c5c75 | 63 | return; |
MikkoZ | 0:0bcc203c5c75 | 64 | } |
MikkoZ | 0:0bcc203c5c75 | 65 | |
MikkoZ | 2:40307a303153 | 66 | void bh1726_soft_reset(){ |
MikkoZ | 2:40307a303153 | 67 | write_register(SAD, BH1726_SOFTWARE_RESET, 0); |
MikkoZ | 0:0bcc203c5c75 | 68 | } |
MikkoZ | 0:0bcc203c5c75 | 69 | |
MikkoZ | 2:40307a303153 | 70 | void bh1726_clear_interrupt(){ |
MikkoZ | 2:40307a303153 | 71 | write_register(SAD, BH1726_INTERRUPT_OUTPUT_RESET, 0); |
MikkoZ | 0:0bcc203c5c75 | 72 | } |
MikkoZ | 0:0bcc203c5c75 | 73 | |
MikkoZ | 2:40307a303153 | 74 | void bh1726_initial_setup(){ |
MikkoZ | 2:40307a303153 | 75 | write_register(SAD, BH1726_TIMING, |
MikkoZ | 2:40307a303153 | 76 | (BH1726_TIMING_ITIME_38CYCLE) //105ms measurement time |
MikkoZ | 2:40307a303153 | 77 | ); |
MikkoZ | 2:40307a303153 | 78 | write_register(SAD, BH1726_INTERRUPT, |
MikkoZ | 2:40307a303153 | 79 | (BH1726_INTERRUPT_INT_LATCH_YES | |
MikkoZ | 2:40307a303153 | 80 | BH1726_INTERRUPT_INT_EN_FALSE | |
MikkoZ | 2:40307a303153 | 81 | BH1726_INTERRUPT_PERSIST_DRDY) |
MikkoZ | 0:0bcc203c5c75 | 82 | ); |
MikkoZ | 2:40307a303153 | 83 | write_register(SAD, BH1726_GAIN, |
MikkoZ | 2:40307a303153 | 84 | (BH1726_GAIN_GAIN0_X1 | |
MikkoZ | 2:40307a303153 | 85 | BH1726_GAIN_GAIN1_X1) |
MikkoZ | 0:0bcc203c5c75 | 86 | ); |
MikkoZ | 2:40307a303153 | 87 | write_register(SAD, BH1726_WAIT, |
MikkoZ | 2:40307a303153 | 88 | (BH1726_WAIT_WAIT_NO) |
MikkoZ | 1:64629eee9eab | 89 | ); |
MikkoZ | 2:40307a303153 | 90 | write_register(SAD, BH1726_CONTROL, |
MikkoZ | 2:40307a303153 | 91 | (BH1726_CONTROL_ADC_EN_TRUE | |
MikkoZ | 2:40307a303153 | 92 | BH1726_CONTROL_POWER_ON) |
MikkoZ | 0:0bcc203c5c75 | 93 | ); |
MikkoZ | 0:0bcc203c5c75 | 94 | } |
MikkoZ | 0:0bcc203c5c75 | 95 | |
MikkoZ | 1:64629eee9eab | 96 | /* input param: data16, pointer to 4*16bit memory |
MikkoZ | 0:0bcc203c5c75 | 97 | return: error, true/false */ |
MikkoZ | 2:40307a303153 | 98 | bool bh1726_read_data(uint16_t* data16){ |
MikkoZ | 2:40307a303153 | 99 | #define BH1726_DATA_LEN 4 |
MikkoZ | 2:40307a303153 | 100 | uint8_t data[BH1726_DATA_LEN]; |
MikkoZ | 0:0bcc203c5c75 | 101 | uint8_t read_bytes; |
MikkoZ | 0:0bcc203c5c75 | 102 | |
MikkoZ | 2:40307a303153 | 103 | read_bytes = read_register(SAD, BH1726_DATA0_LSBS, &data[0], BH1726_DATA_LEN); |
MikkoZ | 2:40307a303153 | 104 | if (read_bytes == BH1726_DATA_LEN){ |
MikkoZ | 2:40307a303153 | 105 | data16[0] = (data[0]) | (data[1] << 8); //data0 |
MikkoZ | 2:40307a303153 | 106 | data16[1] = (data[2]) | (data[3] << 8); //data1 |
MikkoZ | 0:0bcc203c5c75 | 107 | return false; |
MikkoZ | 0:0bcc203c5c75 | 108 | } |
MikkoZ | 0:0bcc203c5c75 | 109 | else{ |
MikkoZ | 0:0bcc203c5c75 | 110 | DEBUG_printf("Read error. Read %d bytes\n\r", read_bytes); |
MikkoZ | 0:0bcc203c5c75 | 111 | return true; |
MikkoZ | 0:0bcc203c5c75 | 112 | } |
MikkoZ | 0:0bcc203c5c75 | 113 | |
MikkoZ | 0:0bcc203c5c75 | 114 | } |