Test application for UV, IR, Light and Temperature sensors
[soft/lpc82x/exanh] / sensors / main.c
1 /****************************************************************************
2  *   sensors/main.c
3  *
4  * TMP101, TSL2661 and VEML6070 I2C sensors example
5  *
6  * Copyright 2016 Nathael Pajani <nathael.pajani@ed3l.fr>
7  *
8  *
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.
13  *
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.
18  *
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/>.
21  *
22  *************************************************************************** */
25 #include "lib/stdint.h"
26 #include "core/system.h"
27 #include "core/systick.h"
28 #include "core/watchdog.h"
29 #include "core/pio.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..
52  */
53 const struct pio_config common_pins[] = {
54         /* UART 0 */
55         { LPC_GPIO_0_0, LPC_UART0_RX, 0 },
56         { LPC_GPIO_0_4, LPC_UART0_TX, 0 },
57         /* I2C 0 */
58         { LPC_I2C0_SCL_PIO_0_10, LPC_FIXED, 0 },
59         { LPC_I2C0_SDA_PIO_0_11, LPC_FIXED, 0 },
60         ARRAY_LAST_PIO,
61 };
65 /***************************************************************************** */
66 /* Luminosity */
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 = {
71         .bus_num = I2C0,
72         .addr = TSL256x_ADDR,
73         .gain = TSL256x_LOW_GAIN,
74         .integration_time = TSL256x_INTEGRATION_100ms,
75         .package = TSL256x_PACKAGE_T,
76 };
81 /***************************************************************************** */
82 /* UV */
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 = {
88         .bus_num = I2C0,
89         .addr = VEML6070_ADDR,
90 };
94 /***************************************************************************** */
95 /* Temperature */
97 #define TMP101_ADDR  0x94 /* Pin Addr0 (pin5 of tmp101) connected to VCC */
98 struct tmp101_sensor_config tmp101_sensor = {
99         .bus_num = I2C0,
100         .addr = TMP101_ADDR,
101         .resolution = TMP_RES_ELEVEN_BITS,
102 };
107 /***************************************************************************** */
108 void system_init()
110         system_set_default_power_state();
111         clock_config(SELECTED_FREQ);
112         set_pins(common_pins);
113         gpio_on();
114         /* System tick timer MUST be configured and running in order to use the sleeping
115          * functions */
116         systick_timer_on(1); /* 1ms */
117         systick_start();
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.
124  */
125 void fault_info(const char* name, uint32_t len)
127         uprintf(UART0, name);
128         while (1);
133 /***************************************************************************** */
134 int main(void)
136         uint16_t uv = 0, ir = 0;
137         uint32_t lux = 0;
138         int deci_degrees = 0;
139         int ret = 0;
141         system_init();
142         uart_on(UART0, 115200, NULL);
144         while (1) {
145                 i2c_on(I2C0, I2C_CLK_100KHz, I2C_MASTER);
147                 msleep(10);
149                 /* Configure temp sensor */
150                 ret = tmp101_sensor_config(&tmp101_sensor);
151                 if (ret != 0) {
152                         uprintf(UART0, "Temp config error: %d\n", ret);
153                 }
154                 msleep(10);
155                 /* Configure lux sensor */
156                 ret = tsl256x_configure(&tsl256x_sensor);
157                 if (ret != 0) {
158                         uprintf(UART0, "Lux config error: %d\n", ret);
159                 }
160                 msleep(10);
162                 /* Configure uv sensor */
163                 ret = veml6070_configure(&veml6070_sensor);
164                 if (ret != 0) {
165                         uprintf(UART0, "UV config error: %d\n", ret);
166                 }
167                 msleep(10);
169                 while (1) {
170                         ret = tmp101_sensor_start_conversion(&tmp101_sensor);
171                         if (ret != 0) {
172                                 uprintf(UART0, "Temp start conversion error: %d\n", ret);
173                                 break;
174                         }
175                         msleep(460); /* Wait for the end of the conversion : 40ms min */
176                         ret = tmp101_sensor_read(&tmp101_sensor, NULL, &deci_degrees);
177                         if (ret != 0) {
178                                 uprintf(UART0, "Temp read error: %d\n", ret);
179                                 break;
180                         }
181                         msleep(10);
182                         ret = tsl256x_sensor_read(&tsl256x_sensor, NULL, &ir, &lux);
183                         if (ret != 0) {
184                                 uprintf(UART0, "Lux read error: %d\n", ret);
185                                 break;
186                         }
187                         msleep(10);
188                         ret = veml6070_sensor_read(&veml6070_sensor, &uv);
189                         if (ret != 0) {
190                                 uprintf(UART0, "UV read error: %d\n", ret);
191                                 break;
192                         }
194                         /* Display all */
195                         uprintf(UART0, "Lux: %d, IR: %d, UV: %d, Temp: %d,%d\n", lux, ir, uv,
196                                         (deci_degrees/10), (deci_degrees%10));
197                 }
199                 i2c_off(I2C0);
200                 msleep(500);
201         }
203         return 0;