sdasd
source/rpr0521_driver.cpp@1:145f11782373, 2016-09-20 (annotated)
- Committer:
- MikkoZ
- Date:
- Tue Sep 20 11:15:09 2016 +0000
- Revision:
- 1:145f11782373
- Parent:
- 0:0bcc203c5c75
- Child:
- 2:56d144605997
- Child:
- 3:83c4958e4bdc
Refactoring and sensor detection improvements; ; - Added partID detection; - refactored indentation
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 | 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 | 1:145f11782373 | 26 | uint8_t id; |
MikkoZ | 1:145f11782373 | 27 | uint8_t read_bytes; |
MikkoZ | 1:145f11782373 | 28 | |
MikkoZ | 1:145f11782373 | 29 | read_bytes = read_register(SAD, RPR0521_MANUFACT, &id, 1); |
MikkoZ | 1:145f11782373 | 30 | if ( read_bytes > 0 ){ |
MikkoZ | 1:145f11782373 | 31 | uint8_t partid; |
MikkoZ | 0:0bcc203c5c75 | 32 | |
MikkoZ | 1:145f11782373 | 33 | DEBUG_printf("Manufacturer: %u\n\r", id); |
MikkoZ | 1:145f11782373 | 34 | read_bytes = read_register(SAD, RPR0521_SYSTEM_CONTROL, &partid, 1); |
MikkoZ | 1:145f11782373 | 35 | if ( read_bytes > 0 ){ |
MikkoZ | 1:145f11782373 | 36 | DEBUG_printf("Part ID: %u\n\r", (partid & 0b00111111) ); |
MikkoZ | 1:145f11782373 | 37 | return(partid); |
MikkoZ | 1:145f11782373 | 38 | } |
MikkoZ | 1:145f11782373 | 39 | else{ |
MikkoZ | 1:145f11782373 | 40 | DEBUG_print("Part ID read failed.\n\r"); |
MikkoZ | 1:145f11782373 | 41 | return 255; |
MikkoZ | 1:145f11782373 | 42 | } |
MikkoZ | 0:0bcc203c5c75 | 43 | } |
MikkoZ | 1:145f11782373 | 44 | else{ |
MikkoZ | 1:145f11782373 | 45 | DEBUG_print("Manufacturer read failed.\n\r"); |
MikkoZ | 1:145f11782373 | 46 | return 255; |
MikkoZ | 0:0bcc203c5c75 | 47 | } |
MikkoZ | 0:0bcc203c5c75 | 48 | } |
MikkoZ | 0:0bcc203c5c75 | 49 | |
MikkoZ | 0:0bcc203c5c75 | 50 | void rpr0521_wait_until_found(){ |
MikkoZ | 1:145f11782373 | 51 | uint8_t id; |
MikkoZ | 1:145f11782373 | 52 | |
MikkoZ | 0:0bcc203c5c75 | 53 | id = rpr0521_readId(); |
MikkoZ | 1:145f11782373 | 54 | while (id == 255){ |
MikkoZ | 1:145f11782373 | 55 | wait(100); |
MikkoZ | 1:145f11782373 | 56 | id = rpr0521_readId(); |
MikkoZ | 1:145f11782373 | 57 | } |
MikkoZ | 1:145f11782373 | 58 | return; |
MikkoZ | 1:145f11782373 | 59 | } |
MikkoZ | 0:0bcc203c5c75 | 60 | |
MikkoZ | 0:0bcc203c5c75 | 61 | void rpr0521_soft_reset(){ |
MikkoZ | 0:0bcc203c5c75 | 62 | write_register(SAD, RPR0521_SYSTEM_CONTROL, RPR0521_SYSTEM_CONTROL_SW_RESET_START); |
MikkoZ | 0:0bcc203c5c75 | 63 | } |
MikkoZ | 0:0bcc203c5c75 | 64 | |
MikkoZ | 0:0bcc203c5c75 | 65 | void rpr0521_clear_interrupt(){ |
MikkoZ | 0:0bcc203c5c75 | 66 | write_register(SAD, RPR0521_SYSTEM_CONTROL, RPR0521_SYSTEM_CONTROL_INT_PIN_HI_Z); |
MikkoZ | 0:0bcc203c5c75 | 67 | } |
MikkoZ | 0:0bcc203c5c75 | 68 | |
MikkoZ | 0:0bcc203c5c75 | 69 | void rpr0521_initial_setup(){ |
MikkoZ | 0:0bcc203c5c75 | 70 | write_register(SAD, RPR0521_ALS_PS_CONTROL, |
MikkoZ | 0:0bcc203c5c75 | 71 | (RPR0521_ALS_PS_CONTROL_ALS_DATA0_GAIN_X1 | |
MikkoZ | 0:0bcc203c5c75 | 72 | RPR0521_ALS_PS_CONTROL_ALS_DATA1_GAIN_X1 | |
MikkoZ | 0:0bcc203c5c75 | 73 | RPR0521_ALS_PS_CONTROL_LED_CURRENT_25MA) |
MikkoZ | 0:0bcc203c5c75 | 74 | ); |
MikkoZ | 0:0bcc203c5c75 | 75 | write_register(SAD, RPR0521_PS_CONTROL, |
MikkoZ | 0:0bcc203c5c75 | 76 | (RPR0521_PS_CONTROL_PS_GAIN_X1 | |
MikkoZ | 0:0bcc203c5c75 | 77 | RPR0521_PS_CONTROL_PERSISTENCE_DRDY ) |
MikkoZ | 0:0bcc203c5c75 | 78 | ); |
MikkoZ | 0:0bcc203c5c75 | 79 | write_register(SAD, RPR0521_MODE_CONTROL, |
MikkoZ | 0:0bcc203c5c75 | 80 | (RPR0521_MODE_CONTROL_ALS_EN_TRUE | RPR0521_MODE_CONTROL_PS_EN_TRUE | |
MikkoZ | 0:0bcc203c5c75 | 81 | RPR0521_MODE_CONTROL_PS_PULSE_200US | RPR0521_MODE_CONTROL_PS_OPERATING_MODE_NORMAL | |
MikkoZ | 0:0bcc203c5c75 | 82 | RPR0521_MODE_CONTROL_MEASUREMENT_TIME_100MS_100MS) |
MikkoZ | 0:0bcc203c5c75 | 83 | ); |
MikkoZ | 0:0bcc203c5c75 | 84 | } |
MikkoZ | 0:0bcc203c5c75 | 85 | |
MikkoZ | 0:0bcc203c5c75 | 86 | /* input param: data16, pointer to 3*16bit memory |
MikkoZ | 0:0bcc203c5c75 | 87 | return: error, true/false */ |
MikkoZ | 0:0bcc203c5c75 | 88 | bool rpr0521_read_data(uint16_t* data16){ |
MikkoZ | 1:145f11782373 | 89 | #define RPR0521_DATA_LEN 6 |
MikkoZ | 1:145f11782373 | 90 | |
MikkoZ | 1:145f11782373 | 91 | uint8_t data[RPR0521_DATA_LEN]; |
MikkoZ | 0:0bcc203c5c75 | 92 | uint8_t read_bytes; |
MikkoZ | 0:0bcc203c5c75 | 93 | |
MikkoZ | 1:145f11782373 | 94 | read_bytes = read_register(SAD, RPR0521_PS_DATA_LSBS, &data[0], RPR0521_DATA_LEN); |
MikkoZ | 1:145f11782373 | 95 | if (read_bytes == RPR0521_DATA_LEN){ |
MikkoZ | 0:0bcc203c5c75 | 96 | data16[0] = (data[0]) | (data[1] << 8); //ps_data |
MikkoZ | 0:0bcc203c5c75 | 97 | data16[1] = (data[2]) | (data[3] << 8); //als_data0 |
MikkoZ | 0:0bcc203c5c75 | 98 | data16[2] = (data[4]) | (data[5] << 8); //als_data1 |
MikkoZ | 0:0bcc203c5c75 | 99 | return false; |
MikkoZ | 0:0bcc203c5c75 | 100 | } |
MikkoZ | 0:0bcc203c5c75 | 101 | else{ |
MikkoZ | 0:0bcc203c5c75 | 102 | DEBUG_printf("Read error. Read %d bytes\n\r", read_bytes); |
MikkoZ | 0:0bcc203c5c75 | 103 | return true; |
MikkoZ | 0:0bcc203c5c75 | 104 | } |
MikkoZ | 0:0bcc203c5c75 | 105 | |
MikkoZ | 0:0bcc203c5c75 | 106 | } |