lightranger10 2.0.0.0
lightranger10.h
Go to the documentation of this file.
1/****************************************************************************
2** Copyright (C) 2020 MikroElektronika d.o.o.
3** Contact: https://www.mikroe.com/contact
4**
5** Permission is hereby granted, free of charge, to any person obtaining a copy
6** of this software and associated documentation files (the "Software"), to deal
7** in the Software without restriction, including without limitation the rights
8** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9** copies of the Software, and to permit persons to whom the Software is
10** furnished to do so, subject to the following conditions:
11** The above copyright notice and this permission notice shall be
12** included in all copies or substantial portions of the Software.
13**
14** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
16** OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18** DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
19** OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20** USE OR OTHER DEALINGS IN THE SOFTWARE.
21****************************************************************************/
22
28#ifndef LIGHTRANGER10_H
29#define LIGHTRANGER10_H
30
31#ifdef __cplusplus
32extern "C"{
33#endif
34
39#ifdef PREINIT_SUPPORTED
40#include "preinit.h"
41#endif
42
43#ifdef MikroCCoreVersion
44 #if MikroCCoreVersion >= 1
45 #include "delays.h"
46 #endif
47#endif
48
49#include "drv_digital_out.h"
50#include "drv_digital_in.h"
51#include "drv_i2c_master.h"
52
73#define LIGHTRANGER10_REG_SOFT_RESET 0x0000
74#define LIGHTRANGER10_REG_ALGO_PTP_RANGE_OFFSET_MM 0x001E
75#define LIGHTRANGER10_REG_MM_CFG_INNER_OFFSET_MM 0x0020
76#define LIGHTRANGER10_REG_MM_CFG_OUTER_OFFSET_MM 0x0022
77#define LIGHTRANGER10_REG_RANGE_CFG_VCSEL_PERIOD_A 0x0060
78#define LIGHTRANGER10_REG_RANGE_CFG_VCSEL_PERIOD_B 0x0063
79#define LIGHTRANGER10_REG_RANGE_CFG_VALID_PHASE_HIGH 0x0069
80#define LIGHTRANGER10_REG_SD_CFG_WOI_SD0 0x0078
81#define LIGHTRANGER10_REG_SD_CFG_WOI_SD1 0x0079
82#define LIGHTRANGER10_REG_SD_CFG_INIT_PHASE_SD0 0x007A
83#define LIGHTRANGER10_REG_SD_CFG_INIT_PHASE_SD1 0x007B
84#define LIGHTRANGER10_REG_SYSTEM_INT_MEAS_PERIOD 0x006C
85#define LIGHTRANGER10_REG_SYSTEM_INTERRUPT_CLEAR 0x0086
86#define LIGHTRANGER10_REG_SYSTEM_MODE_START 0x0087
87#define LIGHTRANGER10_REG_RESULT_RANGE_MM_SD0 0x0096
88#define LIGHTRANGER10_REG_RESULT_OSC_CALIBRATE_VAL 0x00DE
89#define LIGHTRANGER10_REG_MODEL_ID 0x010F
90#define LIGHTRANGER10_REG_MODEL_TYPE 0x0110
91
92 // lightranger10_reg
93
108#define LIGHTRANGER10_SOFT_RESET_SET 0x00
109#define LIGHTRANGER10_SOFT_RESET_CLEAR 0x01
110
115#define LIGHTRANGER10_INTERRUPT_CLEAR 0x01
116
121#define LIGHTRANGER10_RANGING_ENABLE 0x40
122#define LIGHTRANGER10_RANGING_DISABLE 0x00
123
128#define LIGHTRANGER10_DISTANCE_MODE_MEDIUM 0x00
129#define LIGHTRANGER10_DISTANCE_MODE_LONG 0x01
130
135#define LIGHTRANGER10_DISTANCE_MEDIUM_RANGE_PERIOD_A 0x0B
136#define LIGHTRANGER10_DISTANCE_MEDIUM_RANGE_PERIOD_B 0x09
137#define LIGHTRANGER10_DISTANCE_MEDIUM_RANGE_VAL_PH_H 0x78
138#define LIGHTRANGER10_DISTANCE_MEDIUM_SD_WOI_SD0 0x0B
139#define LIGHTRANGER10_DISTANCE_MEDIUM_SD_WOI_SD1 0x09
140#define LIGHTRANGER10_DISTANCE_MEDIUM_SD_IN_PH_SD0 0x0A
141#define LIGHTRANGER10_DISTANCE_MEDIUM_SD_IN_PH_SD1 0x0A
142#define LIGHTRANGER10_DISTANCE_LONG_RANGE_PERIOD_A 0x0F
143#define LIGHTRANGER10_DISTANCE_LONG_RANGE_PERIOD_B 0x0D
144#define LIGHTRANGER10_DISTANCE_LONG_RANGE_VAL_PH_H 0xB8
145#define LIGHTRANGER10_DISTANCE_LONG_SD_WOI_SD0 0x0F
146#define LIGHTRANGER10_DISTANCE_LONG_SD_WOI_SD1 0x0D
147#define LIGHTRANGER10_DISTANCE_LONG_SD_IN_PH_SD0 0x0E
148#define LIGHTRANGER10_DISTANCE_LONG_SD_IN_PH_SD1 0x0E
149
154#define LIGHTRANGER10_ALGO_PTP_RANGE_OFFSET_MM_MULT 4
155#define LIGHTRANGER10_ALGO_PTP_RANGE_OFFSET_MM_RESET 0
156#define LIGHTRANGER10_MM_CFG_INNER_OFFSET_MM_RESET 0
157#define LIGHTRANGER10_MM_CFG_OUTER_OFFSET_MM_RESET 0
158#define LIGHTRANGER10_CALIBRATE_DISTANCE_AVERAGE_NUM 20
159
164#define LIGHTRANGER10_DEFAULT_IM_PERIOD_MS 200
165#define LIGHTRANGER10_OSC_CALIB_IM_PERIOD_OFFSET 4
166
171#define LIGHTRANGER10_MODEL_ID 0xEB
172#define LIGHTRANGER10_MODEL_TYPE 0xAA
173
179#define LIGHTRANGER10_DEVICE_ADDRESS 0x29
180
181 // lightranger10_set
182
197#define LIGHTRANGER10_MAP_MIKROBUS( cfg, mikrobus ) \
198 cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
199 cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
200 cfg.en = MIKROBUS( mikrobus, MIKROBUS_CS ); \
201 cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
202
203 // lightranger10_map
204 // lightranger10
205
210typedef struct
211{
212 // Output pins
213 digital_out_t en;
215 // Input pins
216 digital_in_t int_pin;
218 // Modules
219 i2c_master_t i2c;
221 // I2C slave address
225
230typedef struct
231{
232 pin_name_t scl;
233 pin_name_t sda;
235 pin_name_t en;
236 pin_name_t int_pin;
238 uint32_t i2c_speed;
239 uint8_t i2c_address;
242
253
270
285
299
314err_t lightranger10_generic_write ( lightranger10_t *ctx, uint16_t reg, uint8_t *data_in, uint8_t len );
315
330err_t lightranger10_generic_read ( lightranger10_t *ctx, uint16_t reg, uint8_t *data_out, uint8_t len );
331
344err_t lightranger10_write_byte ( lightranger10_t *ctx, uint16_t reg, uint8_t data_in );
345
358err_t lightranger10_read_byte ( lightranger10_t *ctx, uint16_t reg, uint8_t *data_out );
359
373err_t lightranger10_write_word ( lightranger10_t *ctx, uint16_t reg, uint16_t data_in );
374
388err_t lightranger10_read_word ( lightranger10_t *ctx, uint16_t reg, uint16_t *data_out );
389
403err_t lightranger10_write_dword ( lightranger10_t *ctx, uint16_t reg, uint32_t data_in );
404
418err_t lightranger10_read_dword ( lightranger10_t *ctx, uint16_t reg, uint16_t *data_out );
419
431
441
451
461
471
483
495
508
520err_t lightranger10_set_distance_offset ( lightranger10_t *ctx, int16_t offset_mm );
521
533err_t lightranger10_calibrate_distance ( lightranger10_t *ctx, uint16_t target_distance_mm );
534
547err_t lightranger10_set_distance_mode ( lightranger10_t *ctx, uint8_t distance_mode );
548
560
572
584err_t lightranger10_get_distance ( lightranger10_t *ctx, uint16_t *distance_mm );
585
586#ifdef __cplusplus
587}
588#endif
589#endif // LIGHTRANGER10_H
590
591 // lightranger10
592
593// ------------------------------------------------------------------------ END
err_t lightranger10_generic_read(lightranger10_t *ctx, uint16_t reg, uint8_t *data_out, uint8_t len)
LightRanger 10 I2C reading function.
uint8_t lightranger10_get_int_pin(lightranger10_t *ctx)
LightRanger 10 get int pin function.
err_t lightranger10_read_word(lightranger10_t *ctx, uint16_t reg, uint16_t *data_out)
LightRanger 10 read word function.
err_t lightranger10_calibrate_distance(lightranger10_t *ctx, uint16_t target_distance_mm)
LightRanger 10 calibrate distance function.
err_t lightranger10_check_communication(lightranger10_t *ctx)
LightRanger 10 check communication function.
err_t lightranger10_set_distance_offset(lightranger10_t *ctx, int16_t offset_mm)
LightRanger 10 set distance offset function.
err_t lightranger10_init(lightranger10_t *ctx, lightranger10_cfg_t *cfg)
LightRanger 10 initialization function.
err_t lightranger10_default_cfg(lightranger10_t *ctx)
LightRanger 10 default configuration function.
err_t lightranger10_clear_interrupts(lightranger10_t *ctx)
LightRanger 10 clear interrupts function.
err_t lightranger10_set_distance_mode(lightranger10_t *ctx, uint8_t distance_mode)
LightRanger 10 set distance mode function.
void lightranger10_cfg_setup(lightranger10_cfg_t *cfg)
LightRanger 10 configuration object setup function.
void lightranger10_enable_device(lightranger10_t *ctx)
LightRanger 10 enable device function.
err_t lightranger10_start_measurement(lightranger10_t *ctx)
LightRanger 10 start measurement function.
err_t lightranger10_read_byte(lightranger10_t *ctx, uint16_t reg, uint8_t *data_out)
LightRanger 10 read byte function.
err_t lightranger10_soft_reset(lightranger10_t *ctx)
LightRanger 10 soft reset function.
void lightranger10_disable_device(lightranger10_t *ctx)
LightRanger 10 disable device function.
err_t lightranger10_stop_measurement(lightranger10_t *ctx)
LightRanger 10 stop measurement function.
err_t lightranger10_read_dword(lightranger10_t *ctx, uint16_t reg, uint16_t *data_out)
LightRanger 10 read dword function.
err_t lightranger10_write_byte(lightranger10_t *ctx, uint16_t reg, uint8_t data_in)
LightRanger 10 write byte function.
err_t lightranger10_generic_write(lightranger10_t *ctx, uint16_t reg, uint8_t *data_in, uint8_t len)
LightRanger 10 I2C writing function.
err_t lightranger10_get_distance(lightranger10_t *ctx, uint16_t *distance_mm)
LightRanger 10 get distance function.
void lightranger10_reset_device(lightranger10_t *ctx)
LightRanger 10 reset device function.
err_t lightranger10_write_dword(lightranger10_t *ctx, uint16_t reg, uint32_t data_in)
LightRanger 10 write dword function.
err_t lightranger10_set_inter_measurement_period_ms(lightranger10_t *ctx, uint32_t period_ms)
LightRanger 10 set inter measurement period ms function.
err_t lightranger10_write_word(lightranger10_t *ctx, uint16_t reg, uint16_t data_in)
LightRanger 10 write word function.
lightranger10_return_value_t
LightRanger 10 Click return value data.
Definition lightranger10.h:248
@ LIGHTRANGER10_OK
Definition lightranger10.h:249
@ LIGHTRANGER10_ERROR
Definition lightranger10.h:250
LightRanger 10 Click configuration object.
Definition lightranger10.h:231
uint32_t i2c_speed
Definition lightranger10.h:238
pin_name_t scl
Definition lightranger10.h:232
pin_name_t en
Definition lightranger10.h:235
pin_name_t int_pin
Definition lightranger10.h:236
pin_name_t sda
Definition lightranger10.h:233
uint8_t i2c_address
Definition lightranger10.h:239
LightRanger 10 Click context object.
Definition lightranger10.h:211
digital_in_t int_pin
Definition lightranger10.h:216
i2c_master_t i2c
Definition lightranger10.h:219
digital_out_t en
Definition lightranger10.h:213
uint8_t slave_address
Definition lightranger10.h:222