From 0269956daa9d8bf2be57a7dc416cddbbbdaf78fa Mon Sep 17 00:00:00 2001 From: Nathael Pajani Date: Tue, 13 Sep 2016 18:30:32 +0200 Subject: [PATCH] VEML6070 I2C UV sensor example --- i2c_uv/Makefile | 12 ++++ i2c_uv/README | 27 +++++++++ i2c_uv/main.c | 149 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 188 insertions(+) create mode 100644 i2c_uv/Makefile create mode 100644 i2c_uv/README create mode 100644 i2c_uv/main.c diff --git a/i2c_uv/Makefile b/i2c_uv/Makefile new file mode 100644 index 0000000..097ff94 --- /dev/null +++ b/i2c_uv/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_uv/README b/i2c_uv/README new file mode 100644 index 0000000..8a49ba0 --- /dev/null +++ b/i2c_uv/README @@ -0,0 +1,27 @@ +VEML6070 I2C UV 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 VEML6070 I2C UV sensor. + +Periodic conversions (raw conversion value) are sent on UART0 using the +following frame format : +UV: 0x1b60 + diff --git a/i2c_uv/main.c b/i2c_uv/main.c new file mode 100644 index 0000000..add6bee --- /dev/null +++ b/i2c_uv/main.c @@ -0,0 +1,149 @@ +/**************************************************************************** + * i2c_uv/main.c + * + * VEML6070 I2C UV 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/veml6070_uv_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, +}; + + + + +/***************************************************************************** */ +/* UV */ + +/* The I2C UV light sensor is at addresses 0x70, 0x71 and 0x73 */ +/* Note : These are 8bits address */ +#define VEML6070_ADDR 0x70 +struct veml6070_sensor_config veml6070_sensor = { + .bus_num = I2C0, + .addr = VEML6070_ADDR, +}; + +void uv_config(int uart_num) +{ + int ret = 0; + + /* UV sensor */ + ret = veml6070_configure(&veml6070_sensor); + if (ret != 0) { + uprintf(uart_num, "UV config error: %d\n", ret); + } +} + +void uv_display(int uart_num) +{ + uint16_t uv_raw = 0; + int ret = 0; + + ret = veml6070_sensor_read(&veml6070_sensor, &uv_raw); + if (ret != 0) { + uprintf(uart_num, "UV read error\n"); + } else { + uprintf(uart_num, "UV: 0x%04x\n", uv_raw); + } +} + + +/***************************************************************************** */ +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 uv sensor */ + uv_config(UART0); + + while (1) { + /* VEML6070 uv sensor */ + msleep(200); + uv_display(UART0); + } + return 0; +} + -- 2.43.0