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 09:53:15 2016 +0000
Revision:
0:0bcc203c5c75
Rohm rpr0521 driver library

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 0:0bcc203c5c75 18 #include "../rohm-rpr0521/rpr0521.h" //RPR0521_* register definitions
MikkoZ 0:0bcc203c5c75 19 #include "../rohm-rpr0521/rpr0521_driver.h"
MikkoZ 0:0bcc203c5c75 20 #define SAD 0x38
MikkoZ 0:0bcc203c5c75 21
MikkoZ 0:0bcc203c5c75 22
MikkoZ 0:0bcc203c5c75 23 /* rpr0521 driver*/
MikkoZ 0:0bcc203c5c75 24
MikkoZ 0:0bcc203c5c75 25 uint8_t rpr0521_readId(){
MikkoZ 0:0bcc203c5c75 26 uint8_t id;
MikkoZ 0:0bcc203c5c75 27 uint8_t read_bytes;
MikkoZ 0:0bcc203c5c75 28
MikkoZ 0:0bcc203c5c75 29 read_bytes = read_register(SAD, RPR0521_MANUFACT, &id, 1);
MikkoZ 0:0bcc203c5c75 30 if ( read_bytes > 0 ){
MikkoZ 0:0bcc203c5c75 31 DEBUG_printf("Manufacturer: %u\n\r", id);
MikkoZ 0:0bcc203c5c75 32 return(id);
MikkoZ 0:0bcc203c5c75 33 }
MikkoZ 0:0bcc203c5c75 34 else{
MikkoZ 0:0bcc203c5c75 35 DEBUG_print("Manufacturer read failed.\n\r");
MikkoZ 0:0bcc203c5c75 36 return 255;
MikkoZ 0:0bcc203c5c75 37 }
MikkoZ 0:0bcc203c5c75 38 }
MikkoZ 0:0bcc203c5c75 39
MikkoZ 0:0bcc203c5c75 40 void rpr0521_wait_until_found(){
MikkoZ 0:0bcc203c5c75 41 uint8_t id;
MikkoZ 0:0bcc203c5c75 42
MikkoZ 0:0bcc203c5c75 43 id = rpr0521_readId();
MikkoZ 0:0bcc203c5c75 44 while (id == 255){
MikkoZ 0:0bcc203c5c75 45 wait(100);
MikkoZ 0:0bcc203c5c75 46 id = rpr0521_readId();
MikkoZ 0:0bcc203c5c75 47 }
MikkoZ 0:0bcc203c5c75 48 return;
MikkoZ 0:0bcc203c5c75 49 }
MikkoZ 0:0bcc203c5c75 50
MikkoZ 0:0bcc203c5c75 51 void rpr0521_soft_reset(){
MikkoZ 0:0bcc203c5c75 52 write_register(SAD, RPR0521_SYSTEM_CONTROL, RPR0521_SYSTEM_CONTROL_SW_RESET_START);
MikkoZ 0:0bcc203c5c75 53 }
MikkoZ 0:0bcc203c5c75 54
MikkoZ 0:0bcc203c5c75 55 void rpr0521_clear_interrupt(){
MikkoZ 0:0bcc203c5c75 56 write_register(SAD, RPR0521_SYSTEM_CONTROL, RPR0521_SYSTEM_CONTROL_INT_PIN_HI_Z);
MikkoZ 0:0bcc203c5c75 57 }
MikkoZ 0:0bcc203c5c75 58
MikkoZ 0:0bcc203c5c75 59 void rpr0521_initial_setup(){
MikkoZ 0:0bcc203c5c75 60 write_register(SAD, RPR0521_ALS_PS_CONTROL,
MikkoZ 0:0bcc203c5c75 61 (RPR0521_ALS_PS_CONTROL_ALS_DATA0_GAIN_X1 |
MikkoZ 0:0bcc203c5c75 62 RPR0521_ALS_PS_CONTROL_ALS_DATA1_GAIN_X1 |
MikkoZ 0:0bcc203c5c75 63 RPR0521_ALS_PS_CONTROL_LED_CURRENT_25MA)
MikkoZ 0:0bcc203c5c75 64 );
MikkoZ 0:0bcc203c5c75 65 write_register(SAD, RPR0521_PS_CONTROL,
MikkoZ 0:0bcc203c5c75 66 (RPR0521_PS_CONTROL_PS_GAIN_X1 |
MikkoZ 0:0bcc203c5c75 67 RPR0521_PS_CONTROL_PERSISTENCE_DRDY )
MikkoZ 0:0bcc203c5c75 68 );
MikkoZ 0:0bcc203c5c75 69 write_register(SAD, RPR0521_MODE_CONTROL,
MikkoZ 0:0bcc203c5c75 70 (RPR0521_MODE_CONTROL_ALS_EN_TRUE | RPR0521_MODE_CONTROL_PS_EN_TRUE |
MikkoZ 0:0bcc203c5c75 71 RPR0521_MODE_CONTROL_PS_PULSE_200US | RPR0521_MODE_CONTROL_PS_OPERATING_MODE_NORMAL |
MikkoZ 0:0bcc203c5c75 72 RPR0521_MODE_CONTROL_MEASUREMENT_TIME_100MS_100MS)
MikkoZ 0:0bcc203c5c75 73 );
MikkoZ 0:0bcc203c5c75 74 }
MikkoZ 0:0bcc203c5c75 75
MikkoZ 0:0bcc203c5c75 76 /* input param: data16, pointer to 3*16bit memory
MikkoZ 0:0bcc203c5c75 77 return: error, true/false */
MikkoZ 0:0bcc203c5c75 78 bool rpr0521_read_data(uint16_t* data16){
MikkoZ 0:0bcc203c5c75 79 uint8_t data[6];
MikkoZ 0:0bcc203c5c75 80 uint8_t read_bytes;
MikkoZ 0:0bcc203c5c75 81
MikkoZ 0:0bcc203c5c75 82 read_bytes = read_register(SAD, RPR0521_PS_DATA_LSBS, &data[0], 6);
MikkoZ 0:0bcc203c5c75 83 if (read_bytes == 6){
MikkoZ 0:0bcc203c5c75 84 data16[0] = (data[0]) | (data[1] << 8); //ps_data
MikkoZ 0:0bcc203c5c75 85 data16[1] = (data[2]) | (data[3] << 8); //als_data0
MikkoZ 0:0bcc203c5c75 86 data16[2] = (data[4]) | (data[5] << 8); //als_data1
MikkoZ 0:0bcc203c5c75 87 return false;
MikkoZ 0:0bcc203c5c75 88 }
MikkoZ 0:0bcc203c5c75 89 else{
MikkoZ 0:0bcc203c5c75 90 DEBUG_printf("Read error. Read %d bytes\n\r", read_bytes);
MikkoZ 0:0bcc203c5c75 91 return true;
MikkoZ 0:0bcc203c5c75 92 }
MikkoZ 0:0bcc203c5c75 93
MikkoZ 0:0bcc203c5c75 94 }