之前在RK3566上调试过VL53L0X,这次主要参照之前的记录,在X2000 linux上进行调试。
一、SDK中加入驱动
1、加入源码
不同于常规的方式,将驱动添加到kernel/driver下,君正的linux sdk,需要将驱动添加到module_driver下,可参照doc下的《模块驱动添加流程.pdf》文档。
将vl53L0X文件夹及内容添加到\module_driver\devices下,然后修改Makefile,内容如下:
#
# Makefile for the vl53L0X drivers.
#
# Each configuration option enables a list of files.
FEATURE_USE_CCI := false
#FEATURE_USE_CCI := true
ifeq ($(FEATURE_USE_CCI), true)
ccflags-y += -I$(DRIVERS_DIR)/devices/vl53L0X/inc -DCAMERA_CCI
else
ccflags-y += -I$(DRIVERS_DIR)/devices/vl53L0X/inc
ccflags-y += -I$(DRIVERS_DIR)/devices/vl53L0X
endif
include $(DRIVERS_DIR)/tools/common_module.mk
EXTRA_CFLAGS +=-Wno-error=date-time
EXTRA_CFLAGS +=-Wno-date-time
MODULE_NAME:= stmvl53l0x
module-y += stmvl53l0x_module.o
module-y += stmvl53l0x_module-i2c.o
module-y += stmvl53l0x_module-cci.o
module-y += src/vl53l0x_api_calibration.o
module-y += src/vl53l0x_api_core.o
module-y += src/vl53l0x_api_ranging.o
module-y += src/vl53l0x_api_strings.o
module-y += src/vl53l0x_api.o
module-y += src/vl53l0x_platform.o
module-y += src/vl53l0x_i2c_platform.o
module-y += src/vl53l0x_port_i2c.o
KBUILD_EXTRA_SYMBOLS := $(DRIVERS_DIR)/drivers/utils/Module.symvers
obj-m = $(MODULE_NAME).o
$(MODULE_NAME)-y = $(module-y)
2、添加配置文件
在\module_driver\package\devices\other路径下添加
vl53l0x\Config.in
vl53l0x\vl53l0x.mk
两个文件。
vl53l0x\Config.in的内容如下:
menuconfig MD_STMVL53L0X
bool "stmvl53l0x"
config MD_VL53L0X_IRQ_GPIO
string "gpio irq(vl53L0x 中断引脚)"
choice from SOC_GPIO_PINS
config MD_VL53L0X_I2C_BUSNUM
int "i2c bus num(VL53L0X挂接的i2c总线号)"
default -1
endmenu
用于配置所连接的I2C总线编号。
vl53l0x\vl53l0x.mk内容如下:
#-------------------------------------------------------
package_name = vl53l0x
package_depends = utils
package_module_src = devices/vl53L0X/
package_make_hook =
package_init_hook =
package_finalize_hook = vl53l0x_finalize_hook
package_clean_hook =
#-------------------------------------------------------
vl53l0x_init_file = output/vl53l0x.sh
define vl53l0x_finalize_hook
$(Q)cp devices/vl53L0X/stmvl53l0x.ko output/
$(Q)echo "insmod stmvl53l0x.ko \\" >> $(vl53l0x_init_file)
$(Q)echo " vl53l0x_i2c_bus_num=$(MD_VL53L0X_I2C_BUSNUM) \\" >> $(vl53l0x_init_file)
$(Q)echo " vl53l0x_irq_gpio=$(MD_VL53L0X_IRQ_GPIO) \\" >> $(vl53l0x_init_file)
$(Q)echo >> $(vl53l0x_init_file)
endef
用于将编译生成的驱动模块拷贝到\module_driver\output目录下,生成驱动的加载脚本。
修改other\Config.in,加入vl53L0X的配置:
menu "其它类型设备列表"
source /package/devices/other/pwm_battery/Config.in
source /package/devices/other/vl53l0x/Config.in
endmenu
修改other\other.mk,加入vl53L0x的mk:
package-$(MD_PWM_BATTERY) += package/devices/other/pwm_battery/pwm_battery.mk
package-$(MD_STMVL53L0X) += package/devices/other/vl53l0x/vl53l0x.mk
3、配置文件中选中vl53L0X
cd到tools/iconfigtool/IConfigToolApp/目录,执行./IConfigTool,选择要修改的配置文件
点击Open
依次点击 模块驱动->外设->其它类型驱动列表,选中stmvl53l0x。
然后点击File->Save。
二、修改驱动源码
由于st官方的驱动文件适用于常规方式,但用于君正平台,需要做些修改。
1、stmvl53l0x_module-i2c.c文件修改
包含头文件
#include <utils/i2c.h>
#include <utils/gpio.h>
stmvl53l0x_init_i2c()函数中添加代码:
++static u32 i2c_bus_num;
++struct i2c_client * i2c_connect_client = NULL;
++/* get data */
++module_param_named(vl53l0x_i2c_bus_num , i2c_bus_num, int, 0644);
++static struct i2c_board_info vl53l0x_info = {
++ .type = STMVL53L0X_DRV_NAME,
++ .addr = STMVL53L0X_SLAVE_ADDR,
++};
int stmvl53l0x_init_i2c(void)
{
int ret = 0;
#ifdef STM_TEST
struct i2c_client *client = NULL;
struct i2c_adapter *adapter;
struct i2c_board_info info = {
.type = "stmvl53l0",
.addr = STMVL53L0X_SLAVE_ADDR,
};
#endif
vl53l0x_dbgmsg("Enter\n");
/* register as a i2c client device */
ret = i2c_add_driver(&stmvl53l0x_driver);
if (ret < 0)
vl53l0x_errmsg("%d erro ret:%d\n", __LINE__, ret);
#ifdef STM_TEST
if (!ret) {
adapter = i2c_get_adapter(4);
if (!adapter)
ret = -EINVAL;
else
client = i2c_new_device(adapter, &info);
if (!client)
ret = -EINVAL;
}
#endif
++ i2c_connect_client = i2c_register_device(&vl53l0x_info, i2c_bus_num);
++ if (i2c_connect_client == NULL) {
++ vl53l0x_errmsg("failed to register i2c device\n");
++ i2c_del_driver(&stmvl53l0x_driver);
++ return -EINVAL;
++ }
vl53l0x_dbgmsg("End with rc:%d\n", ret);
return ret;
}
stmvl53l0x_power_up_i2c()内容修改:
int stmvl53l0x_power_up_i2c(void *i2c_object, unsigned int *preset_flag)
{
int ret = 0;
#ifndef STM_TEST
struct i2c_data *data = (struct i2c_data *)i2c_object;
#endif
vl53l0x_dbgmsg("Enter\n");
/* actual power on */
#ifndef STM_TEST
data->power_up = 1;
*preset_flag = 1;
#endif
vl53l0x_dbgmsg("End\n");
return ret;
}
stmvl53l0x_power_down_i2c()内容修改:
int stmvl53l0x_power_down_i2c(void *i2c_object)
{
int ret = 0;
#ifndef STM_TEST
struct i2c_data *data = (struct i2c_data *)i2c_object;
#endif
vl53l0x_dbgmsg("Enter\n");
#ifndef STM_TEST
data->power_up = 0;
#endif
vl53l0x_dbgmsg("End\n");
return ret;
}
2、stmvl53l0x_module.c文件修改
++#include <utils/gpio.h>
--#define IRQ_NUM
++int IRQ_NUM;
++//irq gpio info
++module_param_gpio_named(vl53l0x_irq_gpio, IRQ_NUM, 0644);
三、编译、运行
cd到build目录,执行
make x2100_nand_defconfig
make app_module_driver
make buildroot
编程成功后,会生成新的rootfs.squashfs,重新烧写rootfs.squashfs文件。
如果一切正常,启动后/dev目录下有stmvl53l0x_ranging设备。