From 4ffb0952ed0a3a3da1a3b3c02f22e1a631b3f0f2 Mon Sep 17 00:00:00 2001 From: Nathael Pajani Date: Thu, 15 Sep 2016 15:49:07 +0200 Subject: [PATCH] VCNL4040 I2C Light / Distance sensor example --- i2c_vcnl4040/Makefile | 12 ++++ i2c_vcnl4040/README | 29 ++++++++ i2c_vcnl4040/main.c | 154 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 195 insertions(+) create mode 100644 i2c_vcnl4040/Makefile create mode 100644 i2c_vcnl4040/README create mode 100644 i2c_vcnl4040/main.c diff --git a/i2c_vcnl4040/Makefile b/i2c_vcnl4040/Makefile new file mode 100644 index 0000000..097ff94 --- /dev/null +++ b/i2c_vcnl4040/Makefile @@ -0,0 +1,12 @@ +# Makefile for example apps + +MODULE = $(shell basename $(shell cd .. && pwd && cd -)) +NAME = $(shell basename $(CURDIR)) + +.PHONY: $(NAME).bin +$(NAME).bin: + @make -C ../../.. --no-print-directory NAME=$(NAME) MODULE=$(MODULE) apps/$(MODULE)/$(NAME)/$@ + +clean mrproper: + @make -C ../../.. --no-print-directory $@ + diff --git a/i2c_vcnl4040/README b/i2c_vcnl4040/README new file mode 100644 index 0000000..62275f8 --- /dev/null +++ b/i2c_vcnl4040/README @@ -0,0 +1,29 @@ +VCNL4040 I2C Light / Distance sensor example + +Copyright 2016 Nathael Pajani + + +/* **************************************************************************** + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + *************************************************************************** */ + +This example shows the support of the VCNL6070 I2C Light / Distancesensor. + +Periodic conversions are sent on UART0 using the following frame format : +Dist: 180, AmbL: 26, White: 308 + +Note that for proximity sensor, values are not proportional to distance, and +lower values mean further objet. + diff --git a/i2c_vcnl4040/main.c b/i2c_vcnl4040/main.c new file mode 100644 index 0000000..5856d9a --- /dev/null +++ b/i2c_vcnl4040/main.c @@ -0,0 +1,154 @@ +/**************************************************************************** + * i2c_vcnl4040/main.c + * + * VCNL4040 I2C Light / Distance sensor example + * + * Copyright 2016 Nathael Pajani + * + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + *************************************************************************** */ + + +#include "lib/stdint.h" +#include "core/system.h" +#include "core/systick.h" +#include "core/watchdog.h" +#include "core/pio.h" +#include "lib/stdio.h" +#include "lib/errno.h" +#include "drivers/i2c.h" +#include "drivers/serial.h" +#include "drivers/gpio.h" + +#include "extdrv/vcnl4040_sensor.h" + + +#define MODULE_VERSION 0x01 +#define MODULE_NAME "E-Xanh Gardener" + + +#define SELECTED_FREQ FREQ_SEL_36MHz + + + + + +/***************************************************************************** */ +/* Pins configuration */ +/* Pins blocks are passed to set_pins() for pins configuration. + * Unused pin blocks can be removed safely with the corresponding set_pins() call + * All pins blocks may be safelly merged in a single block for single set_pins() call.. + */ +const struct pio_config common_pins[] = { + /* UART 0 */ + { LPC_GPIO_0_0, LPC_UART0_RX, 0 }, + { LPC_GPIO_0_4, LPC_UART0_TX, 0 }, + /* I2C 0 */ + { LPC_I2C0_SCL_PIO_0_10, LPC_FIXED, 0 }, + { LPC_I2C0_SDA_PIO_0_11, LPC_FIXED, 0 }, + ARRAY_LAST_PIO, +}; + + + + +/***************************************************************************** */ +/* Sensor */ + +/* Note : These are 8bits address */ +#define VCNL4040_ADDR 0xC0 +struct vcnl4040_sensor_config vcnl4040_sensor = { + .bus_num = I2C0, + .addr = VCNL4040_ADDR, + /* Sensors config */ + .als_integration = VCNL4040_ALS_INTEGR_80ms, + .ps_irled_duty = VCNL4040_PS_DUTY_1_320, + .ps_integration = VCNL4040_PS_INTEGR_8_0T, + .ps_definition = VCNL4040_PS_HD_16bits, + .ps_int_type = VCNL4040_PS_INT_DISABLE, + .ps_ms_led_current = VCNL4040_ILED_200mA, +}; + +void sensor_config(int uart_num) +{ + int ret = 0; + + ret = vcnl4040_configure(&vcnl4040_sensor); + if (ret != 0) { + uprintf(uart_num, "Sensor config error: %d\n", ret); + } +} + +void sensor_display(int uart_num) +{ + uint16_t prox = 0, amb = 0, white = 0; + int ret = 0; + + ret = vcnl4040_sensor_read(&vcnl4040_sensor, &prox, &amb, &white); + if (ret != 0) { + uprintf(uart_num, "Sensor read error: %d\n", ret); + } else { + uprintf(uart_num, "Dist: %d, AmbL: %d, White: %d\n", prox, amb / 10, white); + } +} + + +/***************************************************************************** */ +void system_init() +{ + system_set_default_power_state(); + clock_config(SELECTED_FREQ); + set_pins(common_pins); + gpio_on(); + /* System tick timer MUST be configured and running in order to use the sleeping + * functions */ + systick_timer_on(1); /* 1ms */ + systick_start(); +} + +/* Define our fault handler. This one is not mandatory, the dummy fault handler + * will be used when it's not overridden here. + * Note : The default one does a simple infinite loop. If the watchdog is deactivated + * the system will hang. + */ +void fault_info(const char* name, uint32_t len) +{ + uprintf(UART0, name); + while (1); +} + + + +/***************************************************************************** */ +int main(void) +{ + system_init(); + uart_on(UART0, 115200, NULL); + + + i2c_on(I2C0, I2C_CLK_100KHz, I2C_MASTER); + + /* Configure sensor */ + sensor_config(UART0); + + while (1) { + /* VCNL4040 sensor */ + msleep(200); + sensor_display(UART0); + } + return 0; +} + -- 2.43.0