VL53L0X communication with BeagleBone Black using c language

I have beagle bone black I have i2c communication code in c but I trouble communicating the VL53L0X sensor using the I2C Communication in C program.

Below is my I2c2 source code in beagle bone Black.

    #include<stdio.h>
    #include<rc/i2c.h>
    #include<rc/time.h>
    #include<rc/pinmux.h>
    #define I2C_BUS 2 //P9_19,P9_20
    #define Dev_Addr 4
    #define WAIT_US 100000*

    int main()
    {
    	int a,b,d,f;
    	uint8_t* e; 
    	rc_usleep(WAIT_US);
    	rc_usleep(WAIT_US);
    	a = rc_i2c_init (I2C_BUS , Dev_Addr);
    	printf("Intialising a bus %d. \n",a);
    	uint8_t c = 0;
    	while(1)
    	{
    //		b = rc_i2c_send_byte(I2C_BUS,c)	;
    //		printf("Writing a byte %d = %d. \t",c,b);
    		c += 1;
    		rc_usleep(WAIT_US);
    		f = rc_i2c_read_byte(I2C_BUS, Dev_Addr, e);
    		if(*e>=0)
    		{
    			printf("read data = %d ,flag = %d . \n", *e, f);
    		}
    	}
    	d = rc_i2c_close(I2C_BUS);
    	printf("Closing a bus %d. \n",d);
    	return 0;
    }

The above code is working

So no other way to gets a solution ?

Thank you.

very late response, buy why not use the driver. ya have to add the device to the device tree for the driver to load at boot time.

1 Like

Can you elaborate on your statement Please?

there is a driver that once loaded will provide interfaces to read the vl53 part.
how ever for the driver to work you have to add the part to the device tree. or create and load an overlay

the interfaces should be at these locations. ( this is from c-code i did on a raspberry pi for this device)
lots of learning if you move forward with the driver.
#define DEVICE_PATH “/sys/bus/iio/devices/iio:device”
#define DEVICE_NAME “vl53l0x”
#define DEV_PATH “/dev/iio:device”

this is an example that show how to compile the dts, not the best for i2c, and it your a beginner.

here is an overlay example, assuming your using i2c2 buss (don’t think the code formatting held)

/dts-v1/;
/plugin/;

/{
compatible = “ti,beaglebone”, “ti,beaglebone-black”;
part-number = “BBB-00A0”;
version = “00A0”;
fragment@0 {
target = <&i2c2>;
overlay {
#address-cells = <1>;
#size-cells = <0>;
status = “okay”;
lv53@29{
reg = <0x29>;
compatible = “st,vl53l0x”;
};
};
};
};

kernel version
Linux beaglebone 5.4.106-ti-rt-r31
/lib/modules/4.19.94-ti-r42/kernel/drivers/iio/proximity/vl53l0x-i2c.ko.xz

to help you a little more,
this all assumes that you connected your lv53 device to i2c2 buss (correctly)

edit /boot/uEnv.txt
add/change: uboot_overlay_addr4=/lib/firmware/BBB-VL53-00A0.dtbo
make sure the next line is not commented out.
enable_uboot_overlays=1

the overlay above is is named
BBB-VL53-00A0.dts
on the beaglebone black
dtc -O dtb -o BBB-VL53-00A0.dtbo -b 0 -@ BBB-VL53-00A0.dts
copy the dtbo to /lib/firmware

after rebooting, if all is good, this path should exist
cat /sys/bus/iio/devices/iio:device1/in_distance_raw

while booting, if you have a serial console connected, ya should see this during boot time.
uboot_overlays: [fdt_buffer=0x60000] …
uboot_overlays: loading /lib/firmware/BB-ADC-00A0.dtbo …
654 bytes read in 173 ms (2.9 KiB/s)
uboot_overlays: loading /lib/firmware/BBB-VL53-00A0.dtbo …

at command prompt, lsmod should have
vl53l0x_i2c, in the output.