Skip to content

Commit 1a2ab80

Browse files
committed
Attempt other approaches of solving i2c issues
1 parent b78babd commit 1a2ab80

File tree

10 files changed

+118
-189
lines changed

10 files changed

+118
-189
lines changed

README.md

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,21 @@
11
# VL53L1X-C-API-Pico
22
VL53L1X Time-of-Flight distance sensor API for the Raspberry Pi Pico/Pico W
33

4-
## License
5-
This API uses the `core` code from STMicroelectronics, and implements the platform for Raspberry Pi Pico/Pico W. The implemntation, under `platform`, is licensed under the MIT License. Code provided by STMicroelectronics (in `core`) is provided under the BSD 3-clause "New" or "Revised" License.
4+
#### Notice: this library is in-progress
5+
- Please feel free to contribute to this library.
6+
- Major issues:
7+
- i2c communiucation with the VL53L1X hangs up in several steps (booting, starting ranging, etc.). This can be mitigated, albeit not solved, by adding delays.
8+
9+
## Usage
10+
See the examples for usage. To build the project, run the following:
11+
```
12+
mkdir build
13+
cd build
14+
cmake .. -DPICO_BOARD=pico_w
15+
make
16+
```
617

7-
## Assumptions
8-
- Using 7-bit I2C addresses
18+
The resulting uf2 files to upload to the Pico board reside in the build/examples directory.
19+
20+
## License
21+
This API uses the `core` code from STMicroelectronics, and implements the platform for Raspberry Pi Pico/Pico W. The implemntation, under `platform`, is licensed under the MIT License. Code provided by STMicroelectronics (in `core`) is provided under the BSD 3-clause "New" or "Revised" License.

VL53L1X_pico/OLD/VL53L1X_api.c renamed to VL53L1X_pico/VL53L1X_api.c

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ const uint8_t VL51L1X_DEFAULT_CONFIGURATION[] = {
197197
0x00, /* 0x84 : not user-modifiable */
198198
0x01, /* 0x85 : not user-modifiable */
199199
0x00, /* 0x86 : clear interrupt, use ClearInterrupt() */
200-
0x00 /* 0x87 : start ranging, use StartRanging() or StopRanging(), If you want an automatic start after VL53L1X_init() call, put 0x40 in location 0x87 */
200+
0x40 /* 0x87 : start ranging, use StartRanging() or StopRanging(), If you want an automatic start after VL53L1X_init() call, put 0x40 in location 0x87 */
201201
};
202202

203203
static const uint8_t status_rtn[24] = { 255, 255, 255, 5, 2, 4, 1, 7, 3, 0,
@@ -227,20 +227,25 @@ VL53L1X_ERROR VL53L1X_SetI2CAddress(uint16_t dev, uint8_t new_address)
227227
VL53L1X_ERROR VL53L1X_SensorInit(uint16_t dev)
228228
{
229229
VL53L1X_ERROR status = 0;
230-
uint8_t Addr = 0x00, tmp;
230+
//uint8_t tmp;
231231

232-
for (Addr = 0x2D; Addr <= 0x87; Addr++){
233-
status |= VL53L1_WrByte(dev, Addr, VL51L1X_DEFAULT_CONFIGURATION[Addr - 0x2D]);
234-
sleep_ms(2);
235-
}
236-
status |= VL53L1X_StartRanging(dev);
237-
tmp = 0;
232+
VL53L1_WrByte(dev, 0x0000, 0x00);
233+
VL53L1_WaitMs(dev, 100);
234+
VL53L1_WrByte(dev, 0x0000, 0x01);
235+
VL53L1_WaitMs(dev, 1);
236+
237+
VL53L1_WrMulti(dev, 0x002d, VL51L1X_DEFAULT_CONFIGURATION, 91);
238+
239+
VL53L1_WaitMs(dev, 100);
240+
241+
//status |= VL53L1X_StartRanging(dev);
242+
/*tmp = 0;
238243
while(tmp==0){
239-
sleep_ms(100);
240244
status |= VL53L1X_CheckForDataReady(dev, &tmp);
241245
}
242246
status |= VL53L1X_ClearInterrupt(dev);
243-
status |= VL53L1X_StopRanging(dev);
247+
status |= VL53L1X_StopRanging(dev);*/
248+
244249
status |= VL53L1_WrByte(dev, VL53L1_VHV_CONFIG__TIMEOUT_MACROP_LOOP_BOUND, 0x09); /* two bounds VHV */
245250
status |= VL53L1_WrByte(dev, 0x0B, 0); /* start VHV from the previous temperature */
246251
return status;
@@ -297,12 +302,11 @@ VL53L1X_ERROR VL53L1X_CheckForDataReady(uint16_t dev, uint8_t *isDataReady)
297302
uint8_t Temp;
298303
uint8_t IntPol;
299304
VL53L1X_ERROR status = 0;
300-
uint64_t a = to_ms_since_boot(get_absolute_time());
305+
//uint64_t a = to_ms_since_boot(get_absolute_time());
301306
status |= VL53L1X_GetInterruptPolarity(dev, &IntPol);
302-
VL53L1_WaitMs(dev, 2000);
303-
//printf("gip %d&%d. ", status, IntPol);
304-
uint64_t b = to_ms_since_boot(get_absolute_time());
305-
printf("%d\n", (b-a));
307+
//printf("%d&%d. ", status, IntPol);
308+
//uint64_t b = to_ms_since_boot(get_absolute_time());
309+
//printf("time: %d\n", (b-a));
306310
status |= VL53L1_RdByte(dev, GPIO__TIO_HV_STATUS, &Temp);
307311
//printf("rb %d&%d\n", status, Temp);
308312
/* Read in the register to check if a new value is available */
@@ -537,8 +541,7 @@ VL53L1X_ERROR VL53L1X_BootState(uint16_t dev, uint8_t *state)
537541
VL53L1X_ERROR status = 0;
538542
uint8_t tmp = 0;
539543

540-
status |= VL53L1_RdByte(dev,VL53L1_FIRMWARE__SYSTEM_STATUS, &tmp);
541-
printf("%h\n", tmp);
544+
status |= VL53L1_RdByte(dev, VL53L1_FIRMWARE__SYSTEM_STATUS, &tmp);
542545
*state = tmp;
543546
return status;
544547
}
File renamed without changes.
File renamed without changes.
File renamed without changes.

VL53L1X_pico/OLD/VL53L1_platform.c renamed to VL53L1X_pico/VL53L1_platform.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,13 @@ int8_t VL53L1_WrDWord(uint16_t dev, uint16_t index, uint32_t data) {
8989
return i2c_write_blocking(i2c_default, (uint8_t)dev, d, 6, false) == PICO_ERROR_GENERIC;
9090
}
9191

92+
int8_t VL53L1_WrMulti(uint16_t dev, uint16_t index, uint8_t* data, uint8_t len) {
93+
uint8_t d[] = {index >> 8, index};
94+
if (i2c_write_blocking(i2c_default, (uint8_t)dev, d, 2, true) == PICO_ERROR_GENERIC)
95+
return 1;
96+
return i2c_write_blocking(i2c_default, (uint8_t)dev, data, len, false) == PICO_ERROR_GENERIC;
97+
}
98+
9299
int8_t VL53L1_RdByte(uint16_t dev, uint16_t index, uint8_t *data) {
93100
uint8_t buf[] = {index>>8, index};
94101
return i2c_write_blocking(i2c_default, dev, buf, 2, true) == PICO_ERROR_GENERIC

VL53L1X_pico/OLD/VL53L1_platform.h renamed to VL53L1X_pico/VL53L1_platform.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ int8_t VL53L1_WrDWord(
9090
uint16_t index,
9191
uint32_t data);
9292

93+
int8_t VL53L1_WrMulti(
94+
uint16_t dev,
95+
uint16_t index,
96+
uint8_t* data,
97+
uint8_t len);
98+
9399
int8_t VL53L1_RdByte(
94100
uint16_t dev,
95101
uint16_t index,

examples/pico_w/CMakeLists.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
add_executable(VL53L1X_example_pico_w
2-
VL53L1X_example_pico_w.c
1+
add_executable(example
2+
example.c
33
)
44

5-
target_link_libraries(VL53L1X_example_pico_w PUBLIC pico_stdlib VL53L1X_api pico_cyw43_arch_none)
5+
target_link_libraries(example PUBLIC pico_stdlib VL53L1X_api pico_cyw43_arch_none)
66

77
# Generate Pico executable
8-
pico_add_extra_outputs(VL53L1X_example_pico_w)
9-
pico_enable_stdio_usb(VL53L1X_example_pico_w 1)
8+
pico_add_extra_outputs(example)
9+
pico_enable_stdio_usb(example 1)

examples/pico_w/VL53L1X_example_pico_w.c

Lines changed: 0 additions & 163 deletions
This file was deleted.

examples/pico_w/example.c

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#include "pico/stdlib.h"
2+
#include "pico/cyw43_arch.h"
3+
#include "pico/binary_info.h"
4+
#include "hardware/i2c.h"
5+
6+
#include <stdio.h>
7+
#include <stdint.h>
8+
9+
#include "VL53L1X_api.h"
10+
11+
12+
int main() {
13+
stdio_init_all();
14+
15+
if (cyw43_arch_init()) {
16+
printf("failed to initialise\n");
17+
return 1;
18+
}
19+
20+
for (int i=0; i<25; i++) {
21+
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 1);
22+
sleep_ms(250);
23+
printf("Loading...\n");
24+
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 0);
25+
sleep_ms(250);
26+
}
27+
// This example will use I2C0 on the default SDA and SCL pins (GP4, GP5 on a Pico)
28+
i2c_init(i2c_default, 100 * 1000);
29+
gpio_set_function(PICO_DEFAULT_I2C_SDA_PIN, GPIO_FUNC_I2C);
30+
gpio_set_function(PICO_DEFAULT_I2C_SCL_PIN, GPIO_FUNC_I2C);
31+
gpio_pull_up(PICO_DEFAULT_I2C_SDA_PIN);
32+
gpio_pull_up(PICO_DEFAULT_I2C_SCL_PIN);
33+
// Make the I2C pins available to picotool
34+
bi_decl(bi_2pins_with_func(PICO_DEFAULT_I2C_SDA_PIN, PICO_DEFAULT_I2C_SCL_PIN, GPIO_FUNC_I2C));
35+
36+
uint16_t dev = 0x29;
37+
uint8_t state = 0;
38+
uint8_t status;
39+
40+
/*while (state == 0) {
41+
* status |= VL53L1X_BootState(dev, &state);
42+
}*/
43+
44+
status |= VL53L1X_SensorInit(dev);
45+
status |= VL53L1X_SetInterMeasurementInMs(dev, 100);
46+
status |= VL53L1X_StartRanging(dev);
47+
printf("status %d\n", status);
48+
uint8_t dr, rs;
49+
uint16_t dist;
50+
while (1) {
51+
dr = 0;
52+
while (!dr) {
53+
status = VL53L1X_CheckForDataReady(dev, &dr);
54+
}
55+
status = VL53L1X_GetRangeStatus(dev, &rs);
56+
status = VL53L1X_GetDistance(dev, &dist);
57+
status = VL53L1X_ClearInterrupt(dev);
58+
printf("Distance %d\n", dist);
59+
60+
sleep_ms(500);
61+
}
62+
63+
}

0 commit comments

Comments
 (0)