Rohm RPR0521 proximity-als-ir -sensor hello world application. Uses rpr0521-driver and rohm-sensor-hal -libraries.
Dependencies: mbed rohm-rpr0521 rohm-sensor-hal
Fork of KX022_Hello by
Revision 2:edc916d1a86d, committed 2016-09-12
- Comitter:
- MikkoZ
- Date:
- Mon Sep 12 09:22:17 2016 +0000
- Parent:
- 1:a531b03fca97
- Child:
- 3:e22f64655c63
- Commit message:
- Initial version of (proximity-als-ir) rpr0521 hello application.
Changed in this revision
--- a/KX022.lib Wed Dec 30 16:18:11 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -https://developer.mbed.org/teams/Rohm/code/KX022/#e642dd732c4b
--- a/main.cpp Wed Dec 30 16:18:11 2015 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,15 +0,0 @@
-
-#include "mbed.h"
-#include "KX022.h"
-
-Serial pc(USBTX, USBRX);
-KX022 acc(I2C_SDA, I2C_SCL);
-
-int main() {
- pc.printf("\nKX022 Accelerometer library test program.\n");
-
- while(1) {
- wait(0.5);
- pc.printf("x=%7.4f, y=%7.4f, z=%7.4f\n", acc.getAccX(), acc.getAccY(), acc.getAccZ());
- }
-}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main_classic.cpp Mon Sep 12 09:22:17 2016 +0000
@@ -0,0 +1,52 @@
+/* Copyright 2016 Rohm Semiconductor
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+*/
+
+#include "rohm-sensor-hal/rohm-sensor-hal/rohm_hal.h" //mbed.h, types, DEBUG_print*
+#include "rohm-sensor-hal/rohm-sensor-hal/I2CCommon.h"
+
+#include "rohm-rpr0521/rohm-rpr0521/rpr0521_driver.h"
+#include "rohm-rpr0521/rohm-rpr0521/rpr0521.h"
+
+Serial pc(USBTX, USBRX);
+
+void rpr0521_print_raw_values(){
+ pc.printf("\nRPR0521 library test program.\n\r");
+ I2CCommonBegin();
+
+ rpr0521_wait_until_found();
+ pc.printf("\nSensor found.\n\r");
+ rpr0521_initial_setup();
+ wait(1);
+
+ while(1) {
+ bool error;
+ uint16_t data[3];
+
+ error = rpr0521_read_data(&data[0]);
+ if (!error) {
+ pc.printf("PS[%4u], Als0[%4u], Als1[%4u]\n\r", data[0], data[1], data[2]);
+ }
+ else {
+ pc.printf("\n\r");
+ }
+ wait(0.1);
+ }
+}
+
+
+int main() {
+ rpr0521_print_raw_values();
+ }
+
--- a/mbed.bld Wed Dec 30 16:18:11 2015 +0000 +++ b/mbed.bld Mon Sep 12 09:22:17 2016 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/mbed/builds/4336505e4b1c \ No newline at end of file +http://mbed.org/users/mbed_official/code/mbed/builds/2e9cc70d1897 \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rohm-rpr0521/rohm-rpr0521/rpr0521.h Mon Sep 12 09:22:17 2016 +0000 @@ -0,0 +1,167 @@ +/* Copyright 2016 Rohm Semiconductor + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ +#ifndef RPR0521_H +#define RPR0521_H + +/* registers */ +#define RPR0521_REGISTER_DUMP_START 0x40 +#define RPR0521_SYSTEM_CONTROL 0x40 +#define RPR0521_MODE_CONTROL 0x41 +#define RPR0521_ALS_PS_CONTROL 0x42 +#define RPR0521_PS_CONTROL 0x43 +// low 8bit +#define RPR0521_PS_DATA_LSBS 0x44 +// high 4bit +#define RPR0521_PS_DATA_MSBS 0x45 +// low 8bit +#define RPR0521_ALS_DATA0_LSBS 0x46 +// high 8bit +#define RPR0521_ALS_DATA0_MSBS 0x47 +// low 8bit +#define RPR0521_ALS_DATA1_LSBS 0x48 +// high 8bit +#define RPR0521_ALS_DATA1_MSBS 0x49 +#define RPR0521_INTERRUPT 0x4A +// low 8bit +#define RPR0521_PS_TH_LSBS 0x4B +// high 4bit +#define RPR0521_PS_TH_MSBS 0x4C +// low 8bit +#define RPR0521_PS_TL_LSBS 0x4D +// high 4bit +#define RPR0521_PS_TL_MSBS 0x4E +// low 8bit +#define RPR0521_ALS_DATA0_TH_LSBS 0x4F +// high 8bit +#define RPR0521_ALS_DATA0_TH_MSBS 0x50 +// low 8bit +#define RPR0521_ALS_DATA0_TL_LSBS 0x51 +// high 8bit +#define RPR0521_ALS_DATA0_TL_MSBS 0x52 +// low 8bit +#define RPR0521_PS_OFFSET_LSBS 0x53 +// high 2bit +#define RPR0521_PS_OFFSET_MSBS 0x54 +#define RPR0521_MANUFACT 0x92 +#define RPR0521_MANUFACT_VALUE 0xE0 +#define RPR0521_REGISTER_DUMP_END 0x54 +/* registers bits */ +#define RPR0521_SYSTEM_CONTROL_SW_RESET_NOT_STARTED (0x00 << 7) +#define RPR0521_SYSTEM_CONTROL_SW_RESET_START (0x01 << 7) +#define RPR0521_SYSTEM_CONTROL_INT_PIN_NO_INIT (0x00 << 6) +#define RPR0521_SYSTEM_CONTROL_INT_PIN_HI_Z (0x01 << 6) +#define RPR0521_SYSTEM_CONTROL_PART_ID (0x0A << 0) +#define RPR0521_MODE_CONTROL_ALS_EN_FALSE (0x00 << 7) +#define RPR0521_MODE_CONTROL_ALS_EN_TRUE (0x01 << 7) +#define RPR0521_MODE_CONTROL_PS_EN_FALSE (0x00 << 6) +#define RPR0521_MODE_CONTROL_PS_EN_TRUE (0x01 << 6) +#define RPR0521_MODE_CONTROL_PS_PULSE_200US (0x00 << 5) +#define RPR0521_MODE_CONTROL_PS_PULSE_330US (0x01 << 5) +#define RPR0521_MODE_CONTROL_PS_OPERATING_MODE_NORMAL (0x00 << 4) +#define RPR0521_MODE_CONTROL_PS_OPERATING_MODE_DOUBLE_MEASUREMENT (0x01 << 4) +#define RPR0521_MODE_CONTROL_MEASUREMENT_TIME_OFF_OFF (0x00 << 0) +#define RPR0521_MODE_CONTROL_MEASUREMENT_TIME_OFF_10MS (0x01 << 0) +#define RPR0521_MODE_CONTROL_MEASUREMENT_TIME_OFF_40MS (0x02 << 0) +#define RPR0521_MODE_CONTROL_MEASUREMENT_TIME_OFF_100MS (0x03 << 0) +#define RPR0521_MODE_CONTROL_MEASUREMENT_TIME_OFF_400MS (0x04 << 0) +#define RPR0521_MODE_CONTROL_MEASUREMENT_TIME_100MS_50MS (0x05 << 0) +#define RPR0521_MODE_CONTROL_MEASUREMENT_TIME_100MS_100MS (0x06 << 0) +#define RPR0521_MODE_CONTROL_MEASUREMENT_TIME_100MS_400MS (0x07 << 0) +// ALS measurement time is 100ms, sleep time is 300ms +#define RPR0521_MODE_CONTROL_MEASUREMENT_TIME_400MS_50MS (0x08 << 0) +// ALS measurement time is 100ms, sleep time is 300ms +#define RPR0521_MODE_CONTROL_MEASUREMENT_TIME_400MS_100MS (0x09 << 0) +// Measurement time 400ms, high sensitivity mode. +#define RPR0521_MODE_CONTROL_MEASUREMENT_TIME_400MS_OFF (0x0A << 0) +#define RPR0521_MODE_CONTROL_MEASUREMENT_TIME_400MS_400MS (0x0B << 0) +// Additional sw process is necessary. Check P.18 +#define RPR0521_MODE_CONTROL_MEASUREMENT_TIME_50MS_50MS (0x0C << 0) +#define RPR0521_ALS_PS_CONTROL_RESERVED67_WRITE_00 (0x00 << 6) +#define RPR0521_ALS_PS_CONTROL_ALS_DATA0_GAIN_X1 (0x00 << 4) +#define RPR0521_ALS_PS_CONTROL_ALS_DATA0_GAIN_X2 (0x01 << 4) +#define RPR0521_ALS_PS_CONTROL_ALS_DATA0_GAIN_X64 (0x02 << 4) +#define RPR0521_ALS_PS_CONTROL_ALS_DATA0_GAIN_X128 (0x03 << 4) +#define RPR0521_ALS_PS_CONTROL_ALS_DATA1_GAIN_X1 (0x00 << 2) +#define RPR0521_ALS_PS_CONTROL_ALS_DATA1_GAIN_X2 (0x01 << 2) +#define RPR0521_ALS_PS_CONTROL_ALS_DATA1_GAIN_X64 (0x02 << 2) +#define RPR0521_ALS_PS_CONTROL_ALS_DATA1_GAIN_X128 (0x03 << 2) +#define RPR0521_ALS_PS_CONTROL_LED_CURRENT_25MA (0x00 << 0) +#define RPR0521_ALS_PS_CONTROL_LED_CURRENT_50MA (0x01 << 0) +#define RPR0521_ALS_PS_CONTROL_LED_CURRENT_100MA (0x02 << 0) +#define RPR0521_ALS_PS_CONTROL_LED_CURRENT_200MA (0x03 << 0) +#define RPR0521_PS_CONTROL_AMBIENT_IR_FLAG_LOW (0x00 << 6) +#define RPR0521_PS_CONTROL_AMBIENT_IR_FLAG_HIGH (0x01 << 6) +#define RPR0521_PS_CONTROL_AMBIENT_IR_FLAG_TOO_HIGH (0x02 << 6) +#define RPR0521_PS_CONTROL_PS_GAIN_X1 (0x00 << 4) +#define RPR0521_PS_CONTROL_PS_GAIN_X2 (0x01 << 4) +#define RPR0521_PS_CONTROL_PS_GAIN_X4 (0x02 << 4) +#define RPR0521_PS_CONTROL_PERSISTENCE_DRDY (0x00 << 0) +#define RPR0521_PS_CONTROL_PERSISTENCE_CONSECUTIVE_1 (0x01 << 0) +#define RPR0521_PS_CONTROL_PERSISTENCE_CONSECUTIVE_2 (0x02 << 0) +#define RPR0521_PS_CONTROL_PERSISTENCE_CONSECUTIVE_3 (0x03 << 0) +#define RPR0521_PS_CONTROL_PERSISTENCE_CONSECUTIVE_4 (0x04 << 0) +#define RPR0521_PS_CONTROL_PERSISTENCE_CONSECUTIVE_5 (0x05 << 0) +#define RPR0521_PS_CONTROL_PERSISTENCE_CONSECUTIVE_6 (0x06 << 0) +#define RPR0521_PS_CONTROL_PERSISTENCE_CONSECUTIVE_7 (0x07 << 0) +#define RPR0521_PS_CONTROL_PERSISTENCE_CONSECUTIVE_8 (0x08 << 0) +#define RPR0521_PS_CONTROL_PERSISTENCE_CONSECUTIVE_9 (0x09 << 0) +#define RPR0521_PS_CONTROL_PERSISTENCE_CONSECUTIVE_10 (0x0A << 0) +#define RPR0521_PS_CONTROL_PERSISTENCE_CONSECUTIVE_11 (0x0B << 0) +#define RPR0521_PS_CONTROL_PERSISTENCE_CONSECUTIVE_12 (0x0C << 0) +#define RPR0521_PS_CONTROL_PERSISTENCE_CONSECUTIVE_13 (0x0D << 0) +#define RPR0521_PS_CONTROL_PERSISTENCE_CONSECUTIVE_14 (0x0E << 0) +#define RPR0521_PS_CONTROL_PERSISTENCE_CONSECUTIVE_15 (0x0F << 0) +#define RPR0521_INTERRUPT_PS_INT_STATUS_INACTIVE (0x00 << 7) +#define RPR0521_INTERRUPT_PS_INT_STATUS_ACTIVE (0x01 << 7) +#define RPR0521_INTERRUPT_ALS_INT_STATUS_INACTIVE (0x00 << 6) +#define RPR0521_INTERRUPT_ALS_INT_STATUS_ACTIVE (0x01 << 6) +#define RPR0521_INTERRUPT_INT_MODE_PS_TH_H_ACTIVE (0x00 << 4) +#define RPR0521_INTERRUPT_INT_MODE_PS_TH_HYSTERESIS (0x01 << 4) +#define RPR0521_INTERRUPT_INT_MODE_PS_TH_OUTSIDE_DETECTION (0x02 << 4) +#define RPR0521_INTERRUPT_INT_ASSERT_STABLE (0x00 << 3) +#define RPR0521_INTERRUPT_INT_ASSERT_REINT (0x01 << 3) +#define RPR0521_INTERRUPT_INT_LATCH_ENABLED (0x00 << 2) +#define RPR0521_INTERRUPT_INT_LATCH_DISABLED (0x01 << 2) +#define RPR0521_INTERRUPT_INT_TRIG_INACTIVE (0x00 << 0) +#define RPR0521_INTERRUPT_INT_TRIG_BY_PS (0x01 << 0) +#define RPR0521_INTERRUPT_INT_TRIG_BY_ALS (0x02 << 0) +#define RPR0521_INTERRUPT_INT_TRIG_BY_BOTH (0x03 << 0) +#define RPR0521_MANUFACT_ID_E0H (0xE0 << 0) + /*registers bit masks */ +#define RPR0521_SYSTEM_CONTROL_SW_RESET_MASK 0x80 +#define RPR0521_SYSTEM_CONTROL_INT_PIN_MASK 0x40 +#define RPR0521_SYSTEM_CONTROL_PART_MASK 0x3F +#define RPR0521_MODE_CONTROL_ALS_EN_MASK 0x80 +#define RPR0521_MODE_CONTROL_PS_EN_MASK 0x40 +#define RPR0521_MODE_CONTROL_PS_PULSE_MASK 0x20 +#define RPR0521_MODE_CONTROL_PS_OPERATING_MODE_MASK 0x10 +#define RPR0521_MODE_CONTROL_MEASUREMENT_TIME_MASK 0x0F + +#define RPR0521_ALS_PS_CONTROL_RESERVED67_MASK 0xC0 +#define RPR0521_ALS_PS_CONTROL_ALS_DATA0_GAIN_MASK 0x30 +#define RPR0521_ALS_PS_CONTROL_ALS_DATA1_GAIN_MASK 0x0C +#define RPR0521_ALS_PS_CONTROL_LED_CURRENT_MASK 0x03 +#define RPR0521_PS_CONTROL_AMBIENT_IR_FLAG_MASK 0xC0 +#define RPR0521_PS_CONTROL_PS_GAIN_MASK 0x30 +#define RPR0521_PS_CONTROL_PERSISTENCE_MASK 0x0F +#define RPR0521_INTERRUPT_PS_INT_STATUS_MASK 0x80 +#define RPR0521_INTERRUPT_ALS_INT_STATUS_MASK 0x40 +#define RPR0521_INTERRUPT_INT_MODE_MASK 0x30 +#define RPR0521_INTERRUPT_INT_ASSERT_MASK 0x08 +#define RPR0521_INTERRUPT_INT_LATCH_MASK 0x04 +#define RPR0521_INTERRUPT_INT_TRIG_MASK 0x03 +#define RPR0521_MANUFACT_ID_MASK 0xFF + +#endif /* RPR0521_H */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rohm-rpr0521/rohm-rpr0521/rpr0521_driver.h Mon Sep 12 09:22:17 2016 +0000 @@ -0,0 +1,29 @@ +/* Copyright 2016 Rohm Semiconductor + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ +#ifndef rpr0521_driver_h +#define rpr0521_driver_h + +#include "../../rohm-sensor-hal/rohm-sensor-hal/rohm_hal.h" //types + +/* rpr0521 driver*/ +uint8_t rpr0521_readId(); +void rpr0521_wait_until_found(); +void rpr0521_soft_reset(); +bool rpr0521_read_data(uint16_t* data16); +void rpr0521_initial_setup(); +void rpr0521_clear_interrupt(); + +#endif +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/rohm-rpr0521/source/rpr0521_driver.cpp Mon Sep 12 09:22:17 2016 +0000
@@ -0,0 +1,94 @@
+/* Copyright 2016 Rohm Semiconductor
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+*/
+#include "../../rohm-sensor-hal/rohm-sensor-hal/rohm_hal.h" //types, DEBUG_print*
+#include "../../rohm-sensor-hal/rohm-sensor-hal/I2CCommon.h" //read_register, write_register, change_bits
+
+#include "../rohm-rpr0521/rpr0521.h" //RPR0521_* register definitions
+#include "../rohm-rpr0521/rpr0521_driver.h"
+#define SAD 0x38
+
+
+/* rpr0521 driver*/
+
+uint8_t rpr0521_readId(){
+ uint8_t id;
+ uint8_t read_bytes;
+
+ read_bytes = read_register(SAD, RPR0521_MANUFACT, &id, 1);
+ if ( read_bytes > 0 ){
+ DEBUG_printf("Manufacturer: %u\n\r", id);
+ return(id);
+ }
+ else{
+ DEBUG_print("Manufacturer read failed.\n\r");
+ return 255;
+ }
+}
+
+void rpr0521_wait_until_found(){
+ uint8_t id;
+
+ id = rpr0521_readId();
+ while (id == 255){
+ wait(100);
+ id = rpr0521_readId();
+ }
+ return;
+ }
+
+void rpr0521_soft_reset(){
+ write_register(SAD, RPR0521_SYSTEM_CONTROL, RPR0521_SYSTEM_CONTROL_SW_RESET_START);
+}
+
+void rpr0521_clear_interrupt(){
+ write_register(SAD, RPR0521_SYSTEM_CONTROL, RPR0521_SYSTEM_CONTROL_INT_PIN_HI_Z);
+}
+
+void rpr0521_initial_setup(){
+ write_register(SAD, RPR0521_ALS_PS_CONTROL,
+ (RPR0521_ALS_PS_CONTROL_ALS_DATA0_GAIN_X1 |
+ RPR0521_ALS_PS_CONTROL_ALS_DATA1_GAIN_X1 |
+ RPR0521_ALS_PS_CONTROL_LED_CURRENT_25MA)
+ );
+ write_register(SAD, RPR0521_PS_CONTROL,
+ (RPR0521_PS_CONTROL_PS_GAIN_X1 |
+ RPR0521_PS_CONTROL_PERSISTENCE_DRDY )
+ );
+ write_register(SAD, RPR0521_MODE_CONTROL,
+ (RPR0521_MODE_CONTROL_ALS_EN_TRUE | RPR0521_MODE_CONTROL_PS_EN_TRUE |
+ RPR0521_MODE_CONTROL_PS_PULSE_200US | RPR0521_MODE_CONTROL_PS_OPERATING_MODE_NORMAL |
+ RPR0521_MODE_CONTROL_MEASUREMENT_TIME_100MS_100MS)
+ );
+}
+
+/* input param: data16, pointer to 3*16bit memory
+ return: error, true/false */
+bool rpr0521_read_data(uint16_t* data16){
+ uint8_t data[6];
+ uint8_t read_bytes;
+
+ read_bytes = read_register(SAD, RPR0521_PS_DATA_LSBS, &data[0], 6);
+ if (read_bytes == 6){
+ data16[0] = (data[0]) | (data[1] << 8); //ps_data
+ data16[1] = (data[2]) | (data[3] << 8); //als_data0
+ data16[2] = (data[4]) | (data[5] << 8); //als_data1
+ return false;
+ }
+ else{
+ DEBUG_printf("Read error. Read %d bytes\n\r", read_bytes);
+ return true;
+ }
+
+ }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rohm-sensor-hal.lib Mon Sep 12 09:22:17 2016 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/teams/Rohm/code/rohm-sensor-hal/#38c206f19bb7

Rohm RPR-0521RS | Light & Proximity Sensor