Hello world for Rohm BH1726 ALS dual channel sensor. Uses Rohm sensor hal and bh1726 driver.

Dependencies:   mbed rohm-bh1726 rohm-sensor-hal

Fork of rohm-bh1745-hello by Rohm

Committer:
MikkoZ
Date:
Mon Sep 12 09:22:17 2016 +0000
Revision:
2:edc916d1a86d
Initial version of (proximity-als-ir) rpr0521 hello application.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MikkoZ 2:edc916d1a86d 1 /* Copyright 2016 Rohm Semiconductor
MikkoZ 2:edc916d1a86d 2
MikkoZ 2:edc916d1a86d 3 Licensed under the Apache License, Version 2.0 (the "License");
MikkoZ 2:edc916d1a86d 4 you may not use this file except in compliance with the License.
MikkoZ 2:edc916d1a86d 5 You may obtain a copy of the License at
MikkoZ 2:edc916d1a86d 6
MikkoZ 2:edc916d1a86d 7 http://www.apache.org/licenses/LICENSE-2.0
MikkoZ 2:edc916d1a86d 8
MikkoZ 2:edc916d1a86d 9 Unless required by applicable law or agreed to in writing, software
MikkoZ 2:edc916d1a86d 10 distributed under the License is distributed on an "AS IS" BASIS,
MikkoZ 2:edc916d1a86d 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
MikkoZ 2:edc916d1a86d 12 See the License for the specific language governing permissions and
MikkoZ 2:edc916d1a86d 13 limitations under the License.
MikkoZ 2:edc916d1a86d 14 */
MikkoZ 2:edc916d1a86d 15 #include "../../rohm-sensor-hal/rohm-sensor-hal/rohm_hal.h" //types, DEBUG_print*
MikkoZ 2:edc916d1a86d 16 #include "../../rohm-sensor-hal/rohm-sensor-hal/I2CCommon.h" //read_register, write_register, change_bits
MikkoZ 2:edc916d1a86d 17
MikkoZ 2:edc916d1a86d 18 #include "../rohm-rpr0521/rpr0521.h" //RPR0521_* register definitions
MikkoZ 2:edc916d1a86d 19 #include "../rohm-rpr0521/rpr0521_driver.h"
MikkoZ 2:edc916d1a86d 20 #define SAD 0x38
MikkoZ 2:edc916d1a86d 21
MikkoZ 2:edc916d1a86d 22
MikkoZ 2:edc916d1a86d 23 /* rpr0521 driver*/
MikkoZ 2:edc916d1a86d 24
MikkoZ 2:edc916d1a86d 25 uint8_t rpr0521_readId(){
MikkoZ 2:edc916d1a86d 26 uint8_t id;
MikkoZ 2:edc916d1a86d 27 uint8_t read_bytes;
MikkoZ 2:edc916d1a86d 28
MikkoZ 2:edc916d1a86d 29 read_bytes = read_register(SAD, RPR0521_MANUFACT, &id, 1);
MikkoZ 2:edc916d1a86d 30 if ( read_bytes > 0 ){
MikkoZ 2:edc916d1a86d 31 DEBUG_printf("Manufacturer: %u\n\r", id);
MikkoZ 2:edc916d1a86d 32 return(id);
MikkoZ 2:edc916d1a86d 33 }
MikkoZ 2:edc916d1a86d 34 else{
MikkoZ 2:edc916d1a86d 35 DEBUG_print("Manufacturer read failed.\n\r");
MikkoZ 2:edc916d1a86d 36 return 255;
MikkoZ 2:edc916d1a86d 37 }
MikkoZ 2:edc916d1a86d 38 }
MikkoZ 2:edc916d1a86d 39
MikkoZ 2:edc916d1a86d 40 void rpr0521_wait_until_found(){
MikkoZ 2:edc916d1a86d 41 uint8_t id;
MikkoZ 2:edc916d1a86d 42
MikkoZ 2:edc916d1a86d 43 id = rpr0521_readId();
MikkoZ 2:edc916d1a86d 44 while (id == 255){
MikkoZ 2:edc916d1a86d 45 wait(100);
MikkoZ 2:edc916d1a86d 46 id = rpr0521_readId();
MikkoZ 2:edc916d1a86d 47 }
MikkoZ 2:edc916d1a86d 48 return;
MikkoZ 2:edc916d1a86d 49 }
MikkoZ 2:edc916d1a86d 50
MikkoZ 2:edc916d1a86d 51 void rpr0521_soft_reset(){
MikkoZ 2:edc916d1a86d 52 write_register(SAD, RPR0521_SYSTEM_CONTROL, RPR0521_SYSTEM_CONTROL_SW_RESET_START);
MikkoZ 2:edc916d1a86d 53 }
MikkoZ 2:edc916d1a86d 54
MikkoZ 2:edc916d1a86d 55 void rpr0521_clear_interrupt(){
MikkoZ 2:edc916d1a86d 56 write_register(SAD, RPR0521_SYSTEM_CONTROL, RPR0521_SYSTEM_CONTROL_INT_PIN_HI_Z);
MikkoZ 2:edc916d1a86d 57 }
MikkoZ 2:edc916d1a86d 58
MikkoZ 2:edc916d1a86d 59 void rpr0521_initial_setup(){
MikkoZ 2:edc916d1a86d 60 write_register(SAD, RPR0521_ALS_PS_CONTROL,
MikkoZ 2:edc916d1a86d 61 (RPR0521_ALS_PS_CONTROL_ALS_DATA0_GAIN_X1 |
MikkoZ 2:edc916d1a86d 62 RPR0521_ALS_PS_CONTROL_ALS_DATA1_GAIN_X1 |
MikkoZ 2:edc916d1a86d 63 RPR0521_ALS_PS_CONTROL_LED_CURRENT_25MA)
MikkoZ 2:edc916d1a86d 64 );
MikkoZ 2:edc916d1a86d 65 write_register(SAD, RPR0521_PS_CONTROL,
MikkoZ 2:edc916d1a86d 66 (RPR0521_PS_CONTROL_PS_GAIN_X1 |
MikkoZ 2:edc916d1a86d 67 RPR0521_PS_CONTROL_PERSISTENCE_DRDY )
MikkoZ 2:edc916d1a86d 68 );
MikkoZ 2:edc916d1a86d 69 write_register(SAD, RPR0521_MODE_CONTROL,
MikkoZ 2:edc916d1a86d 70 (RPR0521_MODE_CONTROL_ALS_EN_TRUE | RPR0521_MODE_CONTROL_PS_EN_TRUE |
MikkoZ 2:edc916d1a86d 71 RPR0521_MODE_CONTROL_PS_PULSE_200US | RPR0521_MODE_CONTROL_PS_OPERATING_MODE_NORMAL |
MikkoZ 2:edc916d1a86d 72 RPR0521_MODE_CONTROL_MEASUREMENT_TIME_100MS_100MS)
MikkoZ 2:edc916d1a86d 73 );
MikkoZ 2:edc916d1a86d 74 }
MikkoZ 2:edc916d1a86d 75
MikkoZ 2:edc916d1a86d 76 /* input param: data16, pointer to 3*16bit memory
MikkoZ 2:edc916d1a86d 77 return: error, true/false */
MikkoZ 2:edc916d1a86d 78 bool rpr0521_read_data(uint16_t* data16){
MikkoZ 2:edc916d1a86d 79 uint8_t data[6];
MikkoZ 2:edc916d1a86d 80 uint8_t read_bytes;
MikkoZ 2:edc916d1a86d 81
MikkoZ 2:edc916d1a86d 82 read_bytes = read_register(SAD, RPR0521_PS_DATA_LSBS, &data[0], 6);
MikkoZ 2:edc916d1a86d 83 if (read_bytes == 6){
MikkoZ 2:edc916d1a86d 84 data16[0] = (data[0]) | (data[1] << 8); //ps_data
MikkoZ 2:edc916d1a86d 85 data16[1] = (data[2]) | (data[3] << 8); //als_data0
MikkoZ 2:edc916d1a86d 86 data16[2] = (data[4]) | (data[5] << 8); //als_data1
MikkoZ 2:edc916d1a86d 87 return false;
MikkoZ 2:edc916d1a86d 88 }
MikkoZ 2:edc916d1a86d 89 else{
MikkoZ 2:edc916d1a86d 90 DEBUG_printf("Read error. Read %d bytes\n\r", read_bytes);
MikkoZ 2:edc916d1a86d 91 return true;
MikkoZ 2:edc916d1a86d 92 }
MikkoZ 2:edc916d1a86d 93
MikkoZ 2:edc916d1a86d 94 }