1 /****************************************************************************
4 * TMP101, TSL2661 and VEML6070 I2C sensors example
6 * Copyright 2016 Nathael Pajani <nathael.pajani@ed3l.fr>
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 *************************************************************************** */
25 #include "lib/stdint.h"
26 #include "core/system.h"
27 #include "core/systick.h"
28 #include "core/watchdog.h"
30 #include "lib/stdio.h"
31 #include "drivers/i2c.h"
32 #include "drivers/serial.h"
33 #include "drivers/gpio.h"
35 #include "extdrv/tmp101_temp_sensor.h"
36 #include "extdrv/tsl256x_light_sensor.h"
37 #include "extdrv/veml6070_uv_sensor.h"
40 #define MODULE_VERSION 0x01
41 #define MODULE_NAME "E-Xanh Gardener"
43 #define SELECTED_FREQ FREQ_SEL_36MHz
47 /***************************************************************************** */
48 /* Pins configuration */
49 /* Pins blocks are passed to set_pins() for pins configuration.
50 * Unused pin blocks can be removed safely with the corresponding set_pins() call
51 * All pins blocks may be safelly merged in a single block for single set_pins() call..
53 const struct pio_config common_pins[] = {
55 { LPC_GPIO_0_0, LPC_UART0_RX, 0 },
56 { LPC_GPIO_0_4, LPC_UART0_TX, 0 },
58 { LPC_I2C0_SCL_PIO_0_10, LPC_FIXED, 0 },
59 { LPC_I2C0_SDA_PIO_0_11, LPC_FIXED, 0 },
65 /***************************************************************************** */
68 /* Note : These are 8bits address */
69 #define TSL256x_ADDR 0x52 /* Pin Addr Sel (pin2 of tsl256x) connected to GND */
70 struct tsl256x_sensor_config tsl256x_sensor = {
73 .gain = TSL256x_LOW_GAIN,
74 .integration_time = TSL256x_INTEGRATION_100ms,
75 .package = TSL256x_PACKAGE_T,
81 /***************************************************************************** */
84 /* The I2C UV light sensor is at addresses 0x70, 0x71 and 0x73 */
85 /* Note : These are 8bits address */
86 #define VEML6070_ADDR 0x70
87 struct veml6070_sensor_config veml6070_sensor = {
89 .addr = VEML6070_ADDR,
94 /***************************************************************************** */
97 #define TMP101_ADDR 0x94 /* Pin Addr0 (pin5 of tmp101) connected to VCC */
98 struct tmp101_sensor_config tmp101_sensor = {
101 .resolution = TMP_RES_ELEVEN_BITS,
107 /***************************************************************************** */
110 system_set_default_power_state();
111 clock_config(SELECTED_FREQ);
112 set_pins(common_pins);
114 /* System tick timer MUST be configured and running in order to use the sleeping
116 systick_timer_on(1); /* 1ms */
120 /* Define our fault handler. This one is not mandatory, the dummy fault handler
121 * will be used when it's not overridden here.
122 * Note : The default one does a simple infinite loop. If the watchdog is deactivated
123 * the system will hang.
125 void fault_info(const char* name, uint32_t len)
127 uprintf(UART0, name);
133 /***************************************************************************** */
136 uint16_t uv = 0, ir = 0;
138 int deci_degrees = 0;
142 uart_on(UART0, 115200, NULL);
145 i2c_on(I2C0, I2C_CLK_100KHz, I2C_MASTER);
149 /* Configure temp sensor */
150 ret = tmp101_sensor_config(&tmp101_sensor);
152 uprintf(UART0, "Temp config error: %d\n", ret);
155 /* Configure lux sensor */
156 ret = tsl256x_configure(&tsl256x_sensor);
158 uprintf(UART0, "Lux config error: %d\n", ret);
162 /* Configure uv sensor */
163 ret = veml6070_configure(&veml6070_sensor);
165 uprintf(UART0, "UV config error: %d\n", ret);
170 ret = tmp101_sensor_start_conversion(&tmp101_sensor);
172 uprintf(UART0, "Temp start conversion error: %d\n", ret);
175 msleep(460); /* Wait for the end of the conversion : 40ms min */
176 ret = tmp101_sensor_read(&tmp101_sensor, NULL, &deci_degrees);
178 uprintf(UART0, "Temp read error: %d\n", ret);
182 ret = tsl256x_sensor_read(&tsl256x_sensor, NULL, &ir, &lux);
184 uprintf(UART0, "Lux read error: %d\n", ret);
188 ret = veml6070_sensor_read(&veml6070_sensor, &uv);
190 uprintf(UART0, "UV read error: %d\n", ret);
195 uprintf(UART0, "Lux: %d, IR: %d, UV: %d, Temp: %d,%d\n", lux, ir, uv,
196 (deci_degrees/10), (deci_degrees%10));