Rohm RPR0521 proximity-als-ir -sensor driver

Dependents:   rohm-rpr0521-hello rohm-tileshield-6sensor-demo rohm-SensorShield-example

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers rpr0521_driver.cpp Source File

rpr0521_driver.cpp

00001 /*   Copyright 2016 Rohm Semiconductor
00002 
00003    Licensed under the Apache License, Version 2.0 (the "License");
00004    you may not use this file except in compliance with the License.
00005    You may obtain a copy of the License at
00006 
00007        http://www.apache.org/licenses/LICENSE-2.0
00008 
00009    Unless required by applicable law or agreed to in writing, software
00010    distributed under the License is distributed on an "AS IS" BASIS,
00011    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012    See the License for the specific language governing permissions and
00013    limitations under the License.
00014 */
00015 #include "../../rohm-sensor-hal/rohm-sensor-hal/rohm_hal.h"         //types, DEBUG_print*
00016 #include "../../rohm-sensor-hal/rohm-sensor-hal/I2CCommon.h"        //read_register, write_register, change_bits
00017 
00018 #include "../rohm-rpr0521/rpr0521.h"      //RPR0521_* register definitions
00019 #include "../rohm-rpr0521/rpr0521_driver.h"
00020 #define SAD 0x38
00021 
00022 
00023 /* rpr0521 driver*/
00024 
00025 uint8_t rpr0521_readId(){
00026     uint8_t id;
00027     uint8_t read_bytes;
00028 
00029     read_bytes = read_register(SAD, RPR0521_MANUFACT, &id, 1);
00030     if ( read_bytes > 0 ){
00031         uint8_t partid;
00032 
00033         DEBUG_printf("Manufacturer: %u\n\r", id);
00034         read_bytes = read_register(SAD, RPR0521_SYSTEM_CONTROL, &partid, 1);
00035         if ( read_bytes > 0 ){
00036             DEBUG_printf("Part ID: %u\n\r", (partid & 0x3f) );
00037         return(partid);
00038         }
00039         else{
00040             DEBUG_print("Part ID read failed.\n\r");
00041             return 255;
00042         }
00043     }
00044     else{
00045         DEBUG_print("Manufacturer read failed.\n\r");
00046         return 255;
00047     }
00048 }
00049 
00050 void rpr0521_wait_until_found(){
00051     uint8_t id;
00052     
00053     id = rpr0521_readId();
00054     while (id == 255){
00055         wait(100);
00056         id = rpr0521_readId();
00057         }
00058     return;
00059 }
00060 
00061 void rpr0521_soft_reset(){
00062     write_register(SAD, RPR0521_SYSTEM_CONTROL, RPR0521_SYSTEM_CONTROL_SW_RESET_START);
00063 }
00064 
00065 void rpr0521_clear_interrupt(){
00066     write_register(SAD, RPR0521_SYSTEM_CONTROL, RPR0521_SYSTEM_CONTROL_INT_PIN_HI_Z);
00067 }
00068 
00069 void rpr0521_initial_setup(){
00070     write_register(SAD, RPR0521_ALS_PS_CONTROL,
00071         (RPR0521_ALS_PS_CONTROL_ALS_DATA0_GAIN_X1 | 
00072          RPR0521_ALS_PS_CONTROL_ALS_DATA1_GAIN_X1 | 
00073          RPR0521_ALS_PS_CONTROL_LED_CURRENT_25MA)
00074         );
00075     write_register(SAD, RPR0521_PS_CONTROL,
00076         (RPR0521_PS_CONTROL_PS_GAIN_X1 |
00077          RPR0521_PS_CONTROL_PERSISTENCE_DRDY )
00078         );
00079     write_register(SAD, RPR0521_MODE_CONTROL,
00080         (RPR0521_MODE_CONTROL_ALS_EN_TRUE | RPR0521_MODE_CONTROL_PS_EN_TRUE |
00081          RPR0521_MODE_CONTROL_PS_PULSE_200US | RPR0521_MODE_CONTROL_PS_OPERATING_MODE_NORMAL |
00082          RPR0521_MODE_CONTROL_MEASUREMENT_TIME_100MS_100MS)
00083         );
00084 }
00085 
00086 /* input param: data16, pointer to 3*16bit memory 
00087    return: error, true/false */
00088 bool rpr0521_read_data(uint16_t* data16){
00089     #define RPR0521_DATA_LEN 6
00090 
00091     uint8_t data[RPR0521_DATA_LEN];
00092     uint8_t read_bytes;
00093 
00094     read_bytes = read_register(SAD, RPR0521_PS_DATA_LSBS, &data[0], RPR0521_DATA_LEN);
00095     if (read_bytes == RPR0521_DATA_LEN){
00096         data16[0] = (data[0]) | (data[1] << 8); //ps_data
00097         data16[1] = (data[2]) | (data[3] << 8); //als_data0
00098         data16[2] = (data[4]) | (data[5] << 8); //als_data1
00099         return false;
00100         }
00101     else{
00102         DEBUG_printf("Read error. Read %d bytes\n\r", read_bytes);
00103         return true;
00104         }
00105    
00106     }