lightranger5 2.0.0.0
lightranger5.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 LIGHTRANGER5_H
29#define LIGHTRANGER5_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 LIGHTRANGER5_REG_APPID 0x00
74#define LIGHTRANGER5_REG_APPREQID 0x02
75#define LIGHTRANGER5_REG_APPREV_MAJOR 0x01
76#define LIGHTRANGER5_REG_APPREV_MINOR 0x12
77#define LIGHTRANGER5_REG_APPREV_PATCH 0x13
78#define LIGHTRANGER5_REG_CMD_DATA9 0x06
79#define LIGHTRANGER5_REG_CMD_DATA8 0x07
80#define LIGHTRANGER5_REG_CMD_DATA7 0x08
81#define LIGHTRANGER5_REG_CMD_DATA6 0x09
82#define LIGHTRANGER5_REG_CMD_DATA5 0x0A
83#define LIGHTRANGER5_REG_CMD_DATA4 0x0B
84#define LIGHTRANGER5_REG_CMD_DATA3 0x0C
85#define LIGHTRANGER5_REG_CMD_DATA2 0x0D
86#define LIGHTRANGER5_REG_CMD_DATA1 0x0E
87#define LIGHTRANGER5_REG_CMD_DATA0 0x0F
88#define LIGHTRANGER5_REG_COMMAND 0x10
89#define LIGHTRANGER5_REG_PREVIOUS 0x11
90#define LIGHTRANGER5_REG_STATUS 0x1D
91#define LIGHTRANGER5_REG_REGISTER_CONTENTS 0x1E
92#define LIGHTRANGER5_REG_TID 0x1F
93#define LIGHTRANGER5_REG_RESULT_NUMBER 0x20
94#define LIGHTRANGER5_REG_RESULT_INFO 0x21
95#define LIGHTRANGER5_REG_DISTANCE_PEAK_0 0x22
96#define LIGHTRANGER5_REG_DISTANCE_PEAK_1 0x23
97#define LIGHTRANGER5_REG_SYS_CLOCK_0 0x24
98#define LIGHTRANGER5_REG_SYS_CLOCK_1 0x25
99#define LIGHTRANGER5_REG_SYS_CLOCK_2 0x26
100#define LIGHTRANGER5_REG_SYS_CLOCK_3 0x27
101#define LIGHTRANGER5_REG_STATE_DATA_0 0x28
102#define LIGHTRANGER5_REG_STATE_DATA_1 0x29
103#define LIGHTRANGER5_REG_STATE_DATA_2 0x2A
104#define LIGHTRANGER5_REG_STATE_DATA_3 0x2B
105#define LIGHTRANGER5_REG_STATE_DATA_4 0x2C
106#define LIGHTRANGER5_REG_STATE_DATA_5 0x2D
107#define LIGHTRANGER5_REG_STATE_DATA_6 0x2E
108#define LIGHTRANGER5_REG_STATE_DATA_7 0x2F
109#define LIGHTRANGER5_REG_STATE_DATA_8_XTALK_MSB 0x30
110#define LIGHTRANGER5_REG_STATE_DATA_9_XTALK_LSB 0x31
111#define LIGHTRANGER5_REG_STATE_DATA_10_TJ 0x32
112#define LIGHTRANGER5_REG_REFERENCE_HITS_0 0x33
113#define LIGHTRANGER5_REG_REFERENCE_HITS_1 0x34
114#define LIGHTRANGER5_REG_REFERENCE_HITS_2 0x35
115#define LIGHTRANGER5_REG_REFERENCE_HITS_3 0x36
116#define LIGHTRANGER5_REG_OBJECT_HITS_0 0x37
117#define LIGHTRANGER5_REG_OBJECT_HITS_1 0x38
118#define LIGHTRANGER5_REG_OBJECT_HITS_2 0x39
119#define LIGHTRANGER5_REG_OBJECT_HITS_3 0x3A
120#define LIGHTRANGER5_REG_FACTORY_CALIB_0 0x20
121#define LIGHTRANGER5_REG_STATE_DATA_WR_0 0x2E
122#define LIGHTRANGER5_REG_ENABLE 0xE0
123#define LIGHTRANGER5_REG_INT_STATUS 0xE1
124#define LIGHTRANGER5_REG_INT_ENAB 0xE2
125#define LIGHTRANGER5_REG_DEVICE_ID 0xE3
126#define LIGHTRANGER5_REG_REVID 0xE4
127
128 // lightranger5_reg
129
150#define LIGHTRANGER5_CMD_NO_COMMAND 0x00
151#define LIGHTRANGER5_CMD_DISTANCE_MEASURE_MODE_1 0x02
152#define LIGHTRANGER5_CMD_FACTORY_CALIB 0x0A
153#define LIGHTRANGER5_CMD_DL_CALIB_AND_STATE 0x0B
154#define LIGHTRANGER5_CMD_RESULT 0x55
155
161#define LIGHTRANGER5_APPID_MEASUREMENT 0xC0
162#define LIGHTRANGER5_APPID_BOOTLOADER 0x80
163#define LIGHTRANGER5_EXPECTED_ID 0x07
164#define LIGHTRANGER5_CALIB_DATA 0x0A
165#define LIGHTRANGER5_SERIAL_NUMBER 0x47
166#define LIGHTRANGER5_BIT_CPU_RDY 0x40
167#define LIGHTRANGER5_ENABLE_RESET 0x80
168
174#define LIGHTRANGER5_SET_DEV_ADDR 0x41
175
176 // lightranger5_set
177
187#define LIGHTRANGER5_STATUS_IDLE 0x00
188#define LIGHTRANGER5_STATUS_DIAGNOSTIC 0x01
189#define LIGHTRANGER5_STATUS_START 0x02
190#define LIGHTRANGER5_STATUS_CALIBRATION 0x03
191#define LIGHTRANGER5_STATUS_LIGHTCOL 0x04
192#define LIGHTRANGER5_STATUS_ALGORITHM 0x05
193#define LIGHTRANGER5_STATUS_STARTUP 0x06
194#define LIGHTRANGER5_STATUS_OK 0x0F
195#define LIGHTRANGER5_STATUS_VCSEL_PWR_FAIL 0x10
196#define LIGHTRANGER5_STATUS_VCSEL_LED_A_FAIL 0x11
197#define LIGHTRANGER5_STATUS_VCSEL_LED_K_FAIL 0x12
198#define LIGHTRANGER5_STATUS_INVALID_PARAM 0x18
199#define LIGHTRANGER5_STATUS_INVALID_DEVICE 0x19
200#define LIGHTRANGER5_STATUS_CALIB_ERROR 0x1B
201#define LIGHTRANGER5_STATUS_INVALID_COMMAND 0x1C
202#define LIGHTRANGER5_STATUS_INVALID_STATE 0x1D
203#define LIGHTRANGER5_STATUS_ERR_ALGORITHM 0x1F
204
205
206 // lightranger5_get
207
222#define LIGHTRANGER5_MAP_MIKROBUS( cfg, mikrobus ) \
223 cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
224 cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
225 cfg.en = MIKROBUS( mikrobus, MIKROBUS_CS ); \
226 cfg.io0 = MIKROBUS( mikrobus, MIKROBUS_RST ); \
227 cfg.io1 = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
228 cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
229
230 // lightranger5_map
231 // lightranger5
232
237typedef struct
238{
239 // Output pins
240
241 digital_out_t en;
242 digital_out_t io0;
243 digital_out_t io1;
245 // Input pins
246
247 digital_in_t int_pin;
249 // Modules
250
251 i2c_master_t i2c;
253 // I2C slave address
254
258
263typedef struct
264{
265 pin_name_t scl;
266 pin_name_t sda;
268 pin_name_t en;
269 pin_name_t io0;
270 pin_name_t io1;
271 pin_name_t int_pin;
273 uint32_t i2c_speed;
274 uint8_t i2c_address;
277
288
299
318
336
353
371err_t lightranger5_generic_write ( lightranger5_t *ctx, uint8_t reg, uint8_t *tx_buf, uint8_t tx_len );
372
390err_t lightranger5_generic_read ( lightranger5_t *ctx, uint8_t reg, uint8_t *rx_buf, uint8_t rx_len );
391
392
408
424
442err_t lightranger5_set_pin_state_io0 ( lightranger5_t *ctx, uint8_t io0_state );
443
461err_t lightranger5_set_pin_state_io1 ( lightranger5_t *ctx, uint8_t io1_state );
462
479
496
512
529
545err_t lightranger5_get_status ( lightranger5_t *ctx, uint8_t *status );
546
562err_t lightranger5_set_command ( lightranger5_t *ctx, uint8_t cmd );
563
580err_t lightranger5_set_factory_calib_data ( lightranger5_t *ctx, uint8_t *factory_calib_data );
581
598err_t lightranger5_get_factory_calib_data ( lightranger5_t *ctx, uint8_t *factory_calib_data );
599
617err_t lightranger5_set_algorithm_state_data ( lightranger5_t *ctx, uint8_t *alg_state_data );
618
634err_t lightranger5_set_command_data ( lightranger5_t *ctx, uint8_t *cmd_data );
635
654err_t lightranger5_check_previous_command ( lightranger5_t *ctx, uint8_t *previous_cmd );
655
673err_t lightranger5_get_currently_run_app ( lightranger5_t *ctx, uint8_t *appid_data );
674
690
705
721
722#ifdef __cplusplus
723}
724#endif
725#endif // LIGHTRANGER5_H
726
727 // lightranger5
728
729// ------------------------------------------------------------------------ END
err_t lightranger5_init(lightranger5_t *ctx, lightranger5_cfg_t *cfg)
LightRanger 5 initialization function.
err_t lightranger5_set_algorithm_state_data(lightranger5_t *ctx, uint8_t *alg_state_data)
LightRanger 5 set algorithm state data function.
err_t lightranger5_enable_device(lightranger5_t *ctx)
LightRanger 5 enable device function.
err_t lightranger5_start_calib_cmd(lightranger5_t *ctx)
LightRanger 5 start calibration function.
err_t lightranger5_check_factory_calibration(lightranger5_t *ctx)
LightRanger 5 check factory calibration function.
err_t lightranger5_generic_read(lightranger5_t *ctx, uint8_t reg, uint8_t *rx_buf, uint8_t rx_len)
LightRanger 5 I2C reading function.
err_t lightranger5_device_reset(lightranger5_t *ctx)
LightRanger 5 device reset function.
err_t lightranger5_default_cfg(lightranger5_t *ctx)
LightRanger 5 default configuration function.
err_t lightranger5_check_previous_command(lightranger5_t *ctx, uint8_t *previous_cmd)
LightRanger 5 check previous command function.
uint16_t lightranger5_measure_distance(lightranger5_t *ctx)
LightRanger 5 measure distance function.
err_t lightranger5_set_command(lightranger5_t *ctx, uint8_t cmd)
LightRanger 5 set command function.
err_t lightranger5_set_pin_state_io1(lightranger5_t *ctx, uint8_t io1_state)
LightRanger 5 set IO1 pin state function.
err_t lightranger5_set_factory_calib_data(lightranger5_t *ctx, uint8_t *factory_calib_data)
LightRanger 5 set factory calib data function.
err_t lightranger5_get_factory_calib_data(lightranger5_t *ctx, uint8_t *factory_calib_data)
LightRanger 5 get factory calib data function.
err_t lightranger5_load_app(lightranger5_t *ctx)
LightRanger 5 load app function.
uint8_t lightranger5_check_int(lightranger5_t *ctx)
LightRanger 5 check interrupt state function.
void lightranger5_cfg_setup(lightranger5_cfg_t *cfg)
LightRanger 5 configuration object setup function.
err_t lightranger5_generic_write(lightranger5_t *ctx, uint8_t reg, uint8_t *tx_buf, uint8_t tx_len)
LightRanger 5 I2C writing function.
err_t lightranger5_set_command_data(lightranger5_t *ctx, uint8_t *cmd_data)
LightRanger 5 set command data function.
err_t lightranger5_get_status(lightranger5_t *ctx, uint8_t *status)
LightRanger 5 get status function.
lightranger5_return_data_ready_t lightranger5_check_data_ready(lightranger5_t *ctx)
LightRanger 5 check data ready function.
err_t lightranger5_set_pin_state_io0(lightranger5_t *ctx, uint8_t io0_state)
LightRanger 5 set IO0 pin state function.
err_t lightranger5_get_currently_run_app(lightranger5_t *ctx, uint8_t *appid_data)
LightRanger 5 get currently running application function.
err_t lightranger5_disable_device(lightranger5_t *ctx)
LightRanger 5 disable device function.
lightranger5_return_value_t
LightRanger 5 Click return value data.
Definition lightranger5.h:283
@ LIGHTRANGER5_OK
Definition lightranger5.h:284
@ LIGHTRANGER5_ERROR
Definition lightranger5.h:285
lightranger5_return_data_ready_t
LightRanger 5 Click return data ready.
Definition lightranger5.h:294
@ LIGHTRANGER5_DATA_IS_READY
Definition lightranger5.h:296
@ LIGHTRANGER5_DATA_NOT_READY
Definition lightranger5.h:295
LightRanger 5 Click configuration object.
Definition lightranger5.h:264
pin_name_t io1
Definition lightranger5.h:270
pin_name_t io0
Definition lightranger5.h:269
uint32_t i2c_speed
Definition lightranger5.h:273
pin_name_t scl
Definition lightranger5.h:265
pin_name_t en
Definition lightranger5.h:268
pin_name_t int_pin
Definition lightranger5.h:271
pin_name_t sda
Definition lightranger5.h:266
uint8_t i2c_address
Definition lightranger5.h:274
LightRanger 5 Click context object.
Definition lightranger5.h:238
digital_out_t io0
Definition lightranger5.h:242
digital_in_t int_pin
Definition lightranger5.h:247
i2c_master_t i2c
Definition lightranger5.h:251
digital_out_t en
Definition lightranger5.h:241
uint8_t slave_address
Definition lightranger5.h:255
digital_out_t io1
Definition lightranger5.h:243