add lidar lite v3 module 08/207508/3
authorJeonghoon Park <jh1979.park@samsung.com>
Wed, 5 Jun 2019 10:31:30 +0000 (19:31 +0900)
committerJeonghoon Park <jh1979.park@samsung.com>
Fri, 7 Jun 2019 05:27:49 +0000 (14:27 +0900)
Change-Id: I0b0e0b4ae1f2d52b1f5bea5b241b552d8d688a14

inc/resource.h
inc/resource/resource_lidar_v3.h [new file with mode: 0644]
inc/resource/resource_lidar_v3_internal.h [new file with mode: 0644]
inc/resource_internal.h
src/resource.c
src/resource/resource_lidar_v3.c [new file with mode: 0644]

index 951bdee..41624fc 100644 (file)
@@ -1,7 +1,5 @@
 /*
- * Copyright (c) 2019 G.camp,
- *
- * Contact: Jin Seog Bang <seog814@gmail.com>
+ * Copyright (c) 2018 Samsung Electronics Co., Ltd All Rights Reserved
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 
 #include <peripheral_io.h>
 
-#include "resource_internal.h"
+#include "resource/resource_lidar_v3.h"
 #include "resource/resource_lidar.h"
 #include "resource/resource_led.h"
+
+extern void resource_close_all(void);
+
 #endif /* __RESOURCE_H__ */
diff --git a/inc/resource/resource_lidar_v3.h b/inc/resource/resource_lidar_v3.h
new file mode 100644 (file)
index 0000000..88e5a73
--- /dev/null
@@ -0,0 +1,34 @@
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * 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 __RESOURCE_LIDAR_V3_H__
+#define __RESOURCE_LIDAR_V3_H__
+
+typedef enum {
+       LLV3_MODE_DEFAULT = 0, /* Default mode, balanced performance */
+       LLV3_MODE_SRHS, /* Short range, high speed */
+       LLV3_MODE_DR_HSSR, /* Default range, higher speed short range */
+       LLV3_MODE_MAXRANGE, /* Maximum range */
+       LLV3_MODE_HIGH_SENSE, /* High sensitivity detection */
+       LLV3_MODE_LOW_SENSE, /* Low sensitivity detection */
+} llv3_mode_e;
+
+int resource_lidar_v3_mode_set(llv3_mode_e mode); /* option */
+
+int resource_read_lidar_v3(unsigned int *out_value);
+
+#endif /* __RESOURCE_LIDAR_V3_H__ */
+
diff --git a/inc/resource/resource_lidar_v3_internal.h b/inc/resource/resource_lidar_v3_internal.h
new file mode 100644 (file)
index 0000000..b99d5aa
--- /dev/null
@@ -0,0 +1,23 @@
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * 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 __RESOURCE_LIDAR_V3_INTERNAL_H__
+#define __RESOURCE_LIDAR_V3_INTERNAL_H__
+
+void resource_close_lidar_v3(void);
+
+#endif /* __RESOURCE_LIDAR_SENSOR_INTERNAL_H__ */
+
index 327bb8e..0ac9f33 100644 (file)
@@ -1,7 +1,5 @@
 /*
- * Copyright (c) 2019 G.camp,
- *
- * Contact: Jin Seog Bang <seog814@gmail.com>
+ * Copyright (c) 2018 Samsung Electronics Co., Ltd All Rights Reserved
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -21,7 +19,9 @@
 
 #include <peripheral_io.h>
 
+#include "resource/resource_led_internal.h"
 #include "resource/resource_lidar_internal.h"
+#include "resource/resource_lidar_v3_internal.h"
 
 #define PIN_MAX 40
 
@@ -42,6 +42,5 @@ struct _resource_read_cb_s {
 typedef struct _resource_read_cb_s resource_read_s;
 
 extern resource_s *resource_get_info(int pin_num);
-extern void resource_close_all(void);
 
 #endif /* __RESOURCE_INTERNAL_H__ */
index 45f644b..77bbdbc 100644 (file)
@@ -1,7 +1,5 @@
 /*
- * Copyright (c) 2019 G.camp,
- *
- * Contact: Jin Seog Bang <seog814@gmail.com>
+ * Copyright (c) 2018 Samsung Electronics Co., Ltd All Rights Reserved
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -19,7 +17,7 @@
 #include <peripheral_io.h>
 
 #include "log.h"
-#include "resource.h"
+#include "resource_internal.h"
 
 static resource_s resource_info[PIN_MAX] = { {0, NULL, NULL}, };
 
@@ -40,4 +38,5 @@ void resource_close_all(void)
        }
 
        resource_close_lidar_sensor();
+       resource_close_lidar_v3();
 }
diff --git a/src/resource/resource_lidar_v3.c b/src/resource/resource_lidar_v3.c
new file mode 100644 (file)
index 0000000..e0aa3b3
--- /dev/null
@@ -0,0 +1,184 @@
+/*
+ * Copyright (c) 2018 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * 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 <stdlib.h>
+#include <unistd.h>
+#include <peripheral_io.h>
+#include <sys/time.h>
+
+#include "log.h"
+#include "resource/resource_lidar_v3.h"
+
+/* http://static.garmin.com/pumac/LIDAR_Lite_v3_Operation_Manual_and_Technical_Specifications.pdf */
+
+#define I2C_BUS_DEF (1)
+#define LLV3_I2C_ADDR_DEF (0x62)
+
+// Control Registers
+#define LLV3_ACQ_CMD (0x00)
+#define LLV3_STATUS (0x01)
+#define LLV3_SIG_CNT_VAL (0x02)
+#define LLV3_ACQ_CONFIG (0x04)
+#define LLV3_DISTANCE (0x8f)
+#define LLv3_DISTANCE_HIGH (0x0f)
+#define LLv3_DISTANCE_LOW (0x10)
+#define LLV3_REF_CNT_VAL (0x12)
+#define LLV3_THRESH_BYPASS (0x1c)
+
+// ACQ commands
+#define CMD_RESET (0x00)
+#define CMD_WITHOUT_BIAS_CORRECTION (0x03)
+#define CMD_WITH_BIAS_CORRECTION (0x04)
+
+#define BUSY_TIMEMOUT_CNT (9999)
+
+static peripheral_i2c_h g_sensor_h;
+static llv3_mode_e g_mode;
+
+static int __lidar_v3_mode_set(llv3_mode_e mode)
+{
+       unsigned char sig_cnt_max = 0x80;
+       unsigned char acq_conf_reg = 0x08;
+       unsigned char ref_cnt_max = 0x05;
+       unsigned char threadhold = 0x00;
+       int ret = 0;
+
+       switch (mode)
+       {
+               case LLV3_MODE_SRHS:
+                       sig_cnt_max = 0x1d;
+                       ref_cnt_max = 0x03;
+                       break;
+               case LLV3_MODE_DR_HSSR:
+                       acq_conf_reg = 0x00;
+                       ref_cnt_max = 0x03;
+                       break;
+               case LLV3_MODE_MAXRANGE:
+                       sig_cnt_max = 0xff;
+                       break;
+               case LLV3_MODE_HIGH_SENSE:
+                       threadhold = 0x80;
+                       break;
+               case LLV3_MODE_LOW_SENSE:
+                       threadhold = 0xb0;
+                       break;
+               case LLV3_MODE_DEFAULT:
+               default:
+                       break;
+       }
+
+       ret = peripheral_i2c_write_register_byte(g_sensor_h, LLV3_SIG_CNT_VAL, sig_cnt_max);
+       retv_if(ret, -1);
+
+       ret = peripheral_i2c_write_register_byte(g_sensor_h, LLV3_ACQ_CONFIG, acq_conf_reg);
+       retv_if(ret, -1);
+
+       ret = peripheral_i2c_write_register_byte(g_sensor_h, LLV3_REF_CNT_VAL, ref_cnt_max);
+       retv_if(ret, -1);
+
+       ret = peripheral_i2c_write_register_byte(g_sensor_h, LLV3_THRESH_BYPASS, threadhold);
+       retv_if(ret, -1);
+
+       return 0;
+}
+
+static int __lidar_v3_i2c_open(int i2c_bus)
+{
+       int ret = 0;
+
+       if (g_sensor_h)
+               return 0;
+
+       ret = peripheral_i2c_open(i2c_bus, LLV3_I2C_ADDR_DEF, &g_sensor_h);
+       retv_if(ret, -1);
+
+       ret = __lidar_v3_mode_set(g_mode);
+       if (ret) {
+               _E("failed to set mode : %d", g_mode);
+               peripheral_i2c_close(g_sensor_h);
+               g_sensor_h = NULL;
+               return -1;
+       }
+       return 0;
+}
+
+static int __wait_busy_flag(void)
+{
+       int cnt = 0;
+       int busy = 0;
+
+       do {
+               unsigned char state = 0;
+               peripheral_i2c_read_register_byte(g_sensor_h, LLV3_STATUS, &state);
+               busy = state & 0x01;
+               cnt++;
+       } while (busy && (cnt <= BUSY_TIMEMOUT_CNT));
+
+       if (cnt > BUSY_TIMEMOUT_CNT) {
+               _E("TIMEOUT : The lidar sensor is busy now");
+               return -1;
+       }
+
+       return 0;
+}
+
+void resource_close_lidar_v3(void)
+{
+       g_mode = LLV3_MODE_DEFAULT;
+
+       if (!g_sensor_h)
+               return;
+
+       peripheral_i2c_close(g_sensor_h);
+       g_sensor_h = NULL;
+}
+
+int resource_lidar_v3_mode_set(llv3_mode_e mode)
+{
+       g_mode = mode;
+       if (!g_sensor_h)
+               return 0;
+
+       return __lidar_v3_mode_set(mode);
+}
+
+int resource_read_lidar_v3(unsigned int *out_value)
+{
+       int ret = 0;
+       unsigned char val_h = 0;
+       unsigned char val_l = 0;
+
+       retv_if(!out_value, -1);
+
+       ret = __lidar_v3_i2c_open(I2C_BUS_DEF);
+       retv_if(ret, -1);
+
+       ret = __wait_busy_flag();
+       retv_if(ret, -1);
+
+       ret = peripheral_i2c_write_register_byte(g_sensor_h, LLV3_ACQ_CMD, CMD_WITH_BIAS_CORRECTION);
+       retv_if(ret, -1);
+
+       ret = peripheral_i2c_read_register_byte(g_sensor_h, LLv3_DISTANCE_HIGH, &val_h);
+       retv_if(ret, -1);
+
+       ret = peripheral_i2c_read_register_byte(g_sensor_h, LLv3_DISTANCE_LOW, &val_l);
+       retv_if(ret, -1);
+
+       *out_value = ((val_h << 8) | val_l);
+
+       return 0;
+}