1 /****************************************************************************
4 * Exanh Gardener soil moisture sensor support.
6 * Copyright 2016-2017 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"
31 #include "lib/stdio.h"
32 #include "drivers/i2c.h"
33 #include "drivers/adc.h"
34 #include "drivers/serial.h"
35 #include "drivers/gpio.h"
36 #include "drivers/timers.h"
38 #include "extdrv/ws2812.h"
39 #include "extdrv/tsl256x_light_sensor.h"
40 #include "extdrv/veml6070_uv_sensor.h"
41 #include "extdrv/bme280_humidity_sensor.h"
44 #define MODULE_VERSION 0x04
45 #define MODULE_NAME "E-Xanh Gardener"
47 #define SELECTED_FREQ FREQ_SEL_36MHz
52 #define debug_printf(...) \
54 uprintf(__VA_ARGS__); \
57 #define debug(cond, ...) \
60 uprintf(UART0, __VA_ARGS__ ); \
65 #define debug_printf(...) ;
69 /***************************************************************************** */
70 /* Pins configuration */
71 /* Pins blocks are passed to set_pins() for pins configuration.
72 * Unused pin blocks can be removed safely with the corresponding set_pins() call
73 * All pins blocks may be safelly merged in a single block for single set_pins() call..
75 const struct pio_config common_pins[] = {
77 { LPC_GPIO_0_0, LPC_UART0_RX, 0 },
78 { LPC_GPIO_0_4, LPC_UART0_TX, 0 },
80 { LPC_I2C0_SCL_PIO_0_10, LPC_FIXED, 0 },
81 { LPC_I2C0_SDA_PIO_0_11, LPC_FIXED, 0 },
83 { LPC_ADC_AD9_PIO_0_17, LPC_FIXED, 0 },
85 { LPC_GPIO_0_9, LPC_SCT_POUT0, 0 },
87 { LPC_GPIO_0_2, LPC_GPIO, 0 }, /* Led */
88 { LPC_GPIO_0_3, LPC_GPIO, 0 }, /* Tx enable */
92 /* Configure pins as used for capacitance sensing */
93 const struct pio_config capacitance_pins[] = {
94 { LPC_GPIO_0_9, LPC_SCT_POUT0, 0 },
95 { LPC_ADC_AD9_PIO_0_17, LPC_FIXED, 0 },
100 const struct pio button = LPC_GPIO_0_13; /* Mode button */
101 const struct pio ws2812_data_out_pin = LPC_GPIO_0_2; /* Led control data pin */
102 const struct pio tx_en = LPC_GPIO_0_3; /* Enable comminication output */
104 const struct pio cap_trig_gpio = LPC_GPIO_0_9;
105 const struct pio cap_adc_gpio = LPC_GPIO_0_17;
108 /***************************************************************************** */
109 /* External Sensors */
111 /* Note : 8bits address */
112 #define BME280_ADDR 0xEC
113 struct bme280_sensor_config bme280_sensor = {
116 .humidity_oversampling = BME280_OS_x16,
117 .temp_oversampling = BME280_OS_x16,
118 .pressure_oversampling = BME280_OS_x16,
119 .mode = BME280_NORMAL,
120 .standby_len = BME280_SB_62ms,
121 .filter_coeff = BME280_FILT_OFF,
126 /***************************************************************************** */
129 /* Note : These are 8bits address */
130 #define TSL256x_ADDR 0x52 /* Pin Addr Sel (pin2 of tsl256x) connected to GND */
131 struct tsl256x_sensor_config tsl256x_sensor = {
133 .addr = TSL256x_ADDR,
134 .gain = TSL256x_LOW_GAIN,
135 .integration_time = TSL256x_INTEGRATION_100ms,
136 .package = TSL256x_PACKAGE_T,
142 /***************************************************************************** */
145 /* The I2C UV light sensor is at addresses 0x70, 0x71 and 0x73 */
146 /* Note : These are 8bits address */
147 #define VEML6070_ADDR 0x70
148 struct veml6070_sensor_config veml6070_sensor = {
150 .addr = VEML6070_ADDR,
155 /***************************************************************************** */
156 /* Soil humidity sensor */
158 uint16_t raw_humidity = 0;
159 void soil_humidity_sample(void)
162 adc_get_value(&raw_humidity, LPC_ADC(9));
167 void set_led(uint8_t red, uint8_t green, uint8_t blue)
169 ws2812_set_pixel(0, red, green, blue);
170 ws2812_send_frame(0);
175 /***************************************************************************** */
178 system_set_default_power_state();
179 clock_config(SELECTED_FREQ);
180 set_pins(common_pins);
182 /* System tick timer MUST be configured and running in order to use the sleeping
184 systick_timer_on(1); /* 1ms */
188 /* Define our fault handler. This one is not mandatory, the dummy fault handler
189 * will be used when it's not overridden here.
190 * Note : The default one does a simple infinite loop. If the watchdog is deactivated
191 * the system will hang.
193 void fault_info(const char* name, uint32_t len)
195 debug_printf(UART0, name);
199 const struct lpc_timer_pwm_config pwm_conf = {
202 .outputs_initial_state = 0x01,
203 .match_values = { 10, },
214 #define MSG_FIRST_BYTE '#'
215 volatile uint8_t send_data = 0;
216 volatile uint8_t req_cmd = 0;
217 volatile uint8_t req_data_len = 0;
218 #define DATA_LEN_MAX 8
219 volatile uint8_t req_data[DATA_LEN_MAX];
221 volatile uint8_t address = 0;
222 volatile uint8_t store_address = 0;
223 volatile uint8_t addr_req = 0;
226 void serial_req(uint8_t c)
228 static int state = IDLE;
229 static uint8_t req_need_data_len = 0;
230 static uint8_t tmp_req_cmd = 0;
233 if (c == MSG_FIRST_BYTE) {
239 if ((c == address) || (c == 0xFF)) {
247 if ((address == 0) & (addr_req == 1)) {
248 /* We received our address, store it */
260 req_need_data_len = 3;
268 req_data[req_data_len++] = c;
269 if (req_data_len >= req_need_data_len) {
270 req_need_data_len = 0;
272 req_cmd = tmp_req_cmd;
282 /* Should check button "hold" duration and act accordingly
283 * When pressed with no addresss : should request address from host
285 volatile uint8_t need_config = 0;
286 void button_request(uint32_t gpio)
288 static uint32_t tick_press_date = 0;
290 if (gpio_read(button) == 0) {
291 tick_press_date = systick_get_tick_count();
294 delta = systick_get_tick_count() - tick_press_date;
296 return; /* Timer wrapped while reading button hold duration ... let user press again. */
303 /* Send a sample .... */
304 /* This should enter the "menu" loop */
311 /***************************************************************************** */
314 uint8_t got_tsl = 1, got_veml = 1, got_bme = 1;
315 /* TSL2561 and VEML6070 */
316 uint16_t uv = 0, ir = 0;
319 uint32_t pressure = 0, temp = 0;
320 uint16_t humidity = 0;
327 uart_on(UART0, 115200, serial_req);
328 config_gpio(&tx_en, 0, GPIO_DIR_OUT, 1);
331 config_gpio(&cap_trig_gpio, 0, GPIO_DIR_OUT, 0);
332 config_gpio(&cap_adc_gpio, 0, GPIO_DIR_OUT, 0);
334 adc_start_burst_conversion(ADC_MCH(9), LPC_ADC_SEQA);
335 debug_printf(UART0, "ADC config done\n");
337 /* Led strip configuration */
338 ws2812_config(&ws2812_data_out_pin);
339 debug_printf(UART0, "Led config done\n");
342 timer_on(LPC_SCT, (10 * 1000 * 1000), NULL);
343 timer_pwm_config(LPC_SCT, &pwm_conf);
344 debug_printf(UART0, "Timer config done\n");
347 set_pins(capacitance_pins);
348 timer_start(LPC_SCT);
350 /* Register callback on button */
351 set_gpio_callback(button_request, &button, EDGES_BOTH);
353 i2c_on(I2C0, I2C_CLK_100KHz, I2C_MASTER);
357 /* Configure lux sensor */
358 ret = tsl256x_configure(&tsl256x_sensor);
361 debug_printf(UART0, "Lux config error: %d\n", ret);
365 /* Configure uv sensor */
366 ret = veml6070_configure(&veml6070_sensor);
369 debug_printf(UART0, "UV config error: %d\n", ret);
373 /* Configure barometric + humidity + temp sensor */
374 ret = bme280_configure(&bme280_sensor);
377 debug_printf(UART0, "Humidity config error: %d\n", ret);
381 /* Read our address from Flash */
382 memcpy((void*)&address, (void*)(0x00003FC0), 1);
384 /* No sensor should have adrress 0, which is reserved for the "waiting for new address" state */
387 debug_printf(UART0, "Address: %d\n", address);
396 /* Enter sleep / low-power ? */
398 /* Leave some time for the others */
399 msleep(50); /* Add some delay */
401 if (store_address == 1) {
405 ret = iap_prepare_flash(15, 15); /* Prepare sector 15 (16K flash size) for erase */
406 debug((ret != IAP_STATUS_CMD_SUCCESS), "Error preparing flash: %d\n", ret);
408 ret = iap_erase_flash_pages(255, 255); /* Erase the last page of the Flash */
409 debug((ret != IAP_STATUS_CMD_SUCCESS), "Error erasing page 255: %d\n", ret);
411 ret = iap_prepare_flash(15, 15); /* Prepare sector 15 (16K flash size) for write */
412 debug((ret != IAP_STATUS_CMD_SUCCESS), "Error preparing flash: %d\n", ret);
415 ret = iap_copy_ram_to_flash(0x00003FC0, (uint32_t)(&buf), 64);
416 debug((ret != IAP_STATUS_CMD_SUCCESS), "Error writting our number: %d\n", ret);
418 debug(1, "Changed our address to %d\n", address);
424 set_led(req_data[0], req_data[1], req_data[2]);
425 debug(1, "Set led to %d %d %d\n", req_data[0], req_data[1], req_data[2]);
429 debug(1, "Unhandled command 0x%02x (%c)\n", req_cmd, req_cmd);
435 /* Get hold of Tx line */
436 if (need_config == 1) {
437 /* Request a new address from host */
439 if (serial_send_quickbyte(UART0, 'c') == 0) {
446 if (send_data == 1) {
448 /* Take humidity sample */
449 soil_humidity_sample();
452 ret = tsl256x_sensor_read(&tsl256x_sensor, NULL, &ir, &lux);
453 debug((ret != 0), "Lux read error: %d\n", ret);
457 ret = veml6070_sensor_read(&veml6070_sensor, &uv);
458 debug((ret != 0), "UV read error: %d\n", ret);
462 ret = bme280_sensor_read(&bme280_sensor, &pressure, &temp, &humidity);
463 debug((ret != 0), "Humidity read error: %d\n", ret);
465 comp_temp = bme280_compensate_temperature(&bme280_sensor, temp) / 10;
466 pressure = bme280_compensate_pressure(&bme280_sensor, pressure) / 100;
467 humidity = bme280_compensate_humidity(&bme280_sensor, humidity) / 10;
471 debug(1, "Sensor %d:\n\tSoil: %d\n", address, raw_humidity);
472 debug(1, "\tLux: %d, IR: %d, UV: %d\n", lux, ir, uv);
473 debug(1, "\tPatm: %d hPa, Temp: %d,%02d degC, Humidity: %d,%d rH\n\n",
475 comp_temp / 10, (comp_temp > 0) ? (comp_temp % 10) : ((-comp_temp) % 10),
476 humidity / 10, humidity % 10);
478 set_led(0, 0, ((raw_humidity >> 6) & 0xFF));
480 /* Send for control */
483 uint16_t* data = (uint16_t*)buff;
486 buff[1] = address | (got_tsl << 5) | (got_veml << 6) | (got_bme << 7);
487 data[1] = (uint16_t)raw_humidity;
488 data[2] = (uint16_t)lux;
489 data[3] = (uint16_t)ir;
490 data[4] = (uint16_t)uv;
491 data[5] = (uint16_t)pressure;
492 data[6] = (uint16_t)comp_temp;
493 data[7] = (uint16_t)humidity;
496 serial_write(UART0, buff, 20);