Rohm BH1745 red-green-blue-clear -color sensor driver
Dependents: rohm-bh1745-hello rohm-tileshield-6sensor-demo GR-PEACH_IoT_Platform_HTTP_sample
Fork of rohm-rpr0521 by
source/bh1745_driver.cpp@2:2b4a1b49998d, 2016-09-14 (annotated)
- Committer:
- MikkoZ
- Date:
- Wed Sep 14 07:04:40 2016 +0000
- Revision:
- 2:2b4a1b49998d
- Parent:
- 1:64629eee9eab
Added stricter part id checks to prevent SAD collision go unnoticed. Same SADs are used in RPR0521(0x38) and BH1726(0x39).
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 | 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 | 2:2b4a1b49998d | 34 | if (id == 0xe0){ |
MikkoZ | 2:2b4a1b49998d | 35 | read_bytes = read_register(SAD, BH1745_SYSTEM_CONTROL, &partid, 1); |
MikkoZ | 2:2b4a1b49998d | 36 | partid = partid & 0b00111111; |
MikkoZ | 2:2b4a1b49998d | 37 | if ( read_bytes > 0 ){ |
MikkoZ | 2:2b4a1b49998d | 38 | DEBUG_printf("Part ID: %u\n\r", partid); |
MikkoZ | 2:2b4a1b49998d | 39 | if (partid == 0x0b){//OK |
MikkoZ | 2:2b4a1b49998d | 40 | return(partid); |
MikkoZ | 2:2b4a1b49998d | 41 | } else { |
MikkoZ | 2:2b4a1b49998d | 42 | DEBUG_print("Wrong part ID for this driver.\n\r"); |
MikkoZ | 2:2b4a1b49998d | 43 | return 255; |
MikkoZ | 2:2b4a1b49998d | 44 | } |
MikkoZ | 2:2b4a1b49998d | 45 | } else { |
MikkoZ | 2:2b4a1b49998d | 46 | DEBUG_print("Part ID read failed.\n\r"); |
MikkoZ | 2:2b4a1b49998d | 47 | return 255; |
MikkoZ | 2:2b4a1b49998d | 48 | } |
MikkoZ | 2:2b4a1b49998d | 49 | } else { |
MikkoZ | 2:2b4a1b49998d | 50 | DEBUG_print("Wrong manufacturer Id for this driver.\n\r"); |
MikkoZ | 1:64629eee9eab | 51 | return 255; |
MikkoZ | 1:64629eee9eab | 52 | } |
MikkoZ | 2:2b4a1b49998d | 53 | } else { |
MikkoZ | 1:64629eee9eab | 54 | DEBUG_print("Manufacturer read failed.\n\r"); |
MikkoZ | 1:64629eee9eab | 55 | return 255; |
MikkoZ | 0:0bcc203c5c75 | 56 | } |
MikkoZ | 0:0bcc203c5c75 | 57 | } |
MikkoZ | 0:0bcc203c5c75 | 58 | |
MikkoZ | 1:64629eee9eab | 59 | void bh1745_wait_until_found(){ |
MikkoZ | 2:2b4a1b49998d | 60 | uint8_t id; |
MikkoZ | 0:0bcc203c5c75 | 61 | |
MikkoZ | 1:64629eee9eab | 62 | id = bh1745_readId(); |
MikkoZ | 2:2b4a1b49998d | 63 | while (id == 255){ |
MikkoZ | 2:2b4a1b49998d | 64 | wait(100); |
MikkoZ | 2:2b4a1b49998d | 65 | id = bh1745_readId(); |
MikkoZ | 0:0bcc203c5c75 | 66 | } |
MikkoZ | 2:2b4a1b49998d | 67 | return; |
MikkoZ | 2:2b4a1b49998d | 68 | } |
MikkoZ | 0:0bcc203c5c75 | 69 | |
MikkoZ | 1:64629eee9eab | 70 | void bh1745_soft_reset(){ |
MikkoZ | 1:64629eee9eab | 71 | change_bits(SAD, BH1745_MODE_CONTROL1, BH1745_SYSTEM_CONTROL_SW_RESET_MASK, BH1745_SYSTEM_CONTROL_SW_RESET_START); |
MikkoZ | 0:0bcc203c5c75 | 72 | } |
MikkoZ | 0:0bcc203c5c75 | 73 | |
MikkoZ | 1:64629eee9eab | 74 | void bh1745_clear_interrupt(){ |
MikkoZ | 1:64629eee9eab | 75 | uint8_t tmp; |
MikkoZ | 1:64629eee9eab | 76 | read_register(SAD, BH1745_INTERRUPT, &tmp, 1); |
MikkoZ | 0:0bcc203c5c75 | 77 | } |
MikkoZ | 0:0bcc203c5c75 | 78 | |
MikkoZ | 1:64629eee9eab | 79 | void bh1745_initial_setup(){ |
MikkoZ | 1:64629eee9eab | 80 | write_register(SAD, BH1745_INTERRUPT, |
MikkoZ | 1:64629eee9eab | 81 | (BH1745_INTERRUPT_STATUS_INACTIVE | |
MikkoZ | 1:64629eee9eab | 82 | BH1745_INTERRUPT_LATCH_ENABLE | |
MikkoZ | 1:64629eee9eab | 83 | BH1745_INTERRUPT_PIN_DISABLE) |
MikkoZ | 0:0bcc203c5c75 | 84 | ); |
MikkoZ | 1:64629eee9eab | 85 | write_register(SAD, BH1745_PERSISTENCE, |
MikkoZ | 1:64629eee9eab | 86 | (BH1745_PERSISTENCE_OF_INTERRUPT_STATUS_TOGGLE_AFTER_MEASUREMENT) |
MikkoZ | 0:0bcc203c5c75 | 87 | ); |
MikkoZ | 1:64629eee9eab | 88 | write_register(SAD, BH1745_MODE_CONTROL1, |
MikkoZ | 1:64629eee9eab | 89 | (BH1745_MODE_CONTROL1_MEASUREMENT_TIME_160MSEC) |
MikkoZ | 1:64629eee9eab | 90 | ); |
MikkoZ | 1:64629eee9eab | 91 | write_register(SAD, BH1745_MODE_CONTROL2, |
MikkoZ | 1:64629eee9eab | 92 | (BH1745_MODE_CONTROL2_RGBC_MEASUREMENT_ACTIVE | |
MikkoZ | 1:64629eee9eab | 93 | BH1745_MODE_CONTROL2_ADC_GAIN_1X) |
MikkoZ | 0:0bcc203c5c75 | 94 | ); |
MikkoZ | 0:0bcc203c5c75 | 95 | } |
MikkoZ | 0:0bcc203c5c75 | 96 | |
MikkoZ | 1:64629eee9eab | 97 | /* input param: data16, pointer to 4*16bit memory |
MikkoZ | 0:0bcc203c5c75 | 98 | return: error, true/false */ |
MikkoZ | 1:64629eee9eab | 99 | bool bh1745_read_data(uint16_t* data16){ |
MikkoZ | 1:64629eee9eab | 100 | #define BH1745_DATA_LEN 8 |
MikkoZ | 1:64629eee9eab | 101 | uint8_t data[BH1745_DATA_LEN]; |
MikkoZ | 0:0bcc203c5c75 | 102 | uint8_t read_bytes; |
MikkoZ | 0:0bcc203c5c75 | 103 | |
MikkoZ | 1:64629eee9eab | 104 | read_bytes = read_register(SAD, BH1745_RED_DATA_LSBS, &data[0], BH1745_DATA_LEN); |
MikkoZ | 1:64629eee9eab | 105 | if (read_bytes == BH1745_DATA_LEN){ |
MikkoZ | 1:64629eee9eab | 106 | data16[0] = (data[0]) | (data[1] << 8); //red |
MikkoZ | 1:64629eee9eab | 107 | data16[1] = (data[2]) | (data[3] << 8); //green |
MikkoZ | 1:64629eee9eab | 108 | data16[2] = (data[4]) | (data[5] << 8); //blue |
MikkoZ | 1:64629eee9eab | 109 | data16[3] = (data[6]) | (data[7] << 8); //clear |
MikkoZ | 0:0bcc203c5c75 | 110 | return false; |
MikkoZ | 2:2b4a1b49998d | 111 | } else { |
MikkoZ | 0:0bcc203c5c75 | 112 | DEBUG_printf("Read error. Read %d bytes\n\r", read_bytes); |
MikkoZ | 0:0bcc203c5c75 | 113 | return true; |
MikkoZ | 2:2b4a1b49998d | 114 | } |
MikkoZ | 0:0bcc203c5c75 | 115 | |
MikkoZ | 2:2b4a1b49998d | 116 | } |