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 "lib/utils.h"
33 #include "drivers/i2c.h"
34 #include "drivers/adc.h"
35 #include "drivers/serial.h"
36 #include "drivers/gpio.h"
37 #include "drivers/timers.h"
39 #include "extdrv/ws2812.h"
40 #include "extdrv/tsl256x_light_sensor.h"
41 #include "extdrv/veml6070_uv_sensor.h"
42 #include "extdrv/bme280_humidity_sensor.h"
45 #define MODULE_VERSION 0x04
46 #define MODULE_NAME "E-Xanh Gardener"
48 #define SELECTED_FREQ FREQ_SEL_36MHz
52 #if (DEBUG == 1) /* Send only on prog output */
53 #define debug_printf(...) \
54 uprintf(__VA_ARGS__); \
56 #define debug(cond, ...) \
58 uprintf(UART0, __VA_ARGS__ ); \
61 #elif (DEBUG == 2) /* Send on comm port */
62 #define debug_printf(...) \
64 uprintf(__VA_ARGS__); \
65 serial_flush(UART0); \
68 #define debug(cond, ...) \
71 uprintf(UART0, __VA_ARGS__ ); \
72 serial_flush(UART0); \
76 #define debug_printf(...) ;
80 /***************************************************************************** */
81 /* Pins configuration */
82 /* Pins blocks are passed to set_pins() for pins configuration.
83 * Unused pin blocks can be removed safely with the corresponding set_pins() call
84 * All pins blocks may be safelly merged in a single block for single set_pins() call..
86 const struct pio_config common_pins[] = {
88 { LPC_GPIO_0_0, LPC_UART0_RX, 0 },
89 { LPC_GPIO_0_4, LPC_UART0_TX, 0 },
91 { LPC_I2C0_SCL_PIO_0_10, LPC_FIXED, 0 },
92 { LPC_I2C0_SDA_PIO_0_11, LPC_FIXED, 0 },
94 { LPC_ADC_AD9_PIO_0_17, LPC_FIXED, 0 },
96 { LPC_GPIO_0_9, LPC_SCT_POUT0, 0 },
98 { LPC_GPIO_0_2, LPC_GPIO, 0 }, /* Led */
99 { LPC_GPIO_0_3, LPC_GPIO, 0 }, /* Tx enable */
103 /* Configure pins as used for capacitance sensing */
104 const struct pio_config capacitance_pins[] = {
105 { LPC_GPIO_0_9, LPC_SCT_POUT0, 0 },
106 { LPC_ADC_AD9_PIO_0_17, LPC_FIXED, 0 },
111 const struct pio button = LPC_GPIO_0_13; /* Mode button */
112 const struct pio ws2812_data_out_pin = LPC_GPIO_0_2; /* Led control data pin */
113 const struct pio tx_en = LPC_GPIO_0_3; /* Enable comminication output */
115 const struct pio cap_trig_gpio = LPC_GPIO_0_9;
116 const struct pio cap_adc_gpio = LPC_GPIO_0_17;
119 /***************************************************************************** */
120 /* External Sensors */
122 /* Note : 8bits address */
123 #define BME280_ADDR 0xEC
124 struct bme280_sensor_config bme280_sensor = {
127 .humidity_oversampling = BME280_OS_x16,
128 .temp_oversampling = BME280_OS_x16,
129 .pressure_oversampling = BME280_OS_x16,
130 .mode = BME280_NORMAL,
131 .standby_len = BME280_SB_62ms,
132 .filter_coeff = BME280_FILT_OFF,
137 /***************************************************************************** */
140 /* Note : These are 8bits address */
141 #define TSL256x_ADDR 0x52 /* Pin Addr Sel (pin2 of tsl256x) connected to GND */
142 struct tsl256x_sensor_config tsl256x_sensor = {
144 .addr = TSL256x_ADDR,
145 .gain = TSL256x_LOW_GAIN,
146 .integration_time = TSL256x_INTEGRATION_100ms,
147 .package = TSL256x_PACKAGE_T,
153 /***************************************************************************** */
156 /* The I2C UV light sensor is at addresses 0x70, 0x71 and 0x73 */
157 /* Note : These are 8bits address */
158 #define VEML6070_ADDR 0x70
159 struct veml6070_sensor_config veml6070_sensor = {
161 .addr = VEML6070_ADDR,
166 /***************************************************************************** */
167 /* Soil humidity sensor */
169 uint16_t raw_humidity = 0;
170 void soil_humidity_sample(void)
173 adc_get_value(&raw_humidity, LPC_ADC(9));
178 void set_led(uint8_t red, uint8_t green, uint8_t blue)
180 ws2812_set_pixel(0, red, green, blue);
181 ws2812_send_frame(0);
186 /***************************************************************************** */
189 system_set_default_power_state();
190 clock_config(SELECTED_FREQ);
191 set_pins(common_pins);
193 /* System tick timer MUST be configured and running in order to use the sleeping
195 systick_timer_on(1); /* 1ms */
199 /* Define our fault handler. This one is not mandatory, the dummy fault handler
200 * will be used when it's not overridden here.
201 * Note : The default one does a simple infinite loop. If the watchdog is deactivated
202 * the system will hang.
204 void fault_info(const char* name, uint32_t len)
206 debug_printf(UART0, name);
210 const struct lpc_timer_pwm_config pwm_conf = {
213 .outputs_initial_state = 0x01,
214 .match_values = { 10, },
225 #define MSG_FIRST_BYTE '#'
226 volatile uint8_t send_data = 0;
227 volatile uint8_t req_cmd = 0;
228 volatile uint8_t req_data_len = 0;
229 #define DATA_LEN_MAX 8
230 volatile uint8_t req_data[DATA_LEN_MAX];
232 volatile uint8_t address = 0;
233 volatile uint8_t store_address = 0;
234 volatile uint8_t addr_req = 0;
237 void serial_req(uint8_t c)
239 static int state = IDLE;
240 static uint8_t req_need_data_len = 0;
241 static uint8_t tmp_req_cmd = 0;
244 if (c == MSG_FIRST_BYTE) {
250 if ((c == address) || (c == 0xFF)) {
258 if ((address == 0) & (addr_req == 1)) {
259 /* We received our address, store it */
271 req_need_data_len = 3;
279 req_data[req_data_len++] = c;
280 if (req_data_len >= req_need_data_len) {
281 req_need_data_len = 0;
283 req_cmd = tmp_req_cmd;
293 /* Should check button "hold" duration and act accordingly
294 * When pressed with no addresss : should request address from host
296 volatile uint8_t need_config = 0;
297 void button_request(uint32_t gpio)
299 static uint32_t tick_press_date = 0;
301 if (gpio_read(button) == 0) {
302 tick_press_date = systick_get_tick_count();
305 delta = systick_get_tick_count() - tick_press_date;
307 return; /* Timer wrapped while reading button hold duration ... let user press again. */
314 /* Send a sample .... */
315 /* This should enter the "menu" loop */
322 /***************************************************************************** */
325 uint8_t got_tsl = 1, got_veml = 1, got_bme = 1;
326 /* TSL2561 and VEML6070 */
327 uint16_t uv = 0, ir = 0;
330 uint32_t pressure = 0, temp = 0;
331 uint16_t humidity = 0;
338 uart_on(UART0, 115200, serial_req);
339 config_gpio(&tx_en, 0, GPIO_DIR_OUT, 1);
342 config_gpio(&cap_trig_gpio, 0, GPIO_DIR_OUT, 0);
343 config_gpio(&cap_adc_gpio, 0, GPIO_DIR_OUT, 0);
345 adc_start_burst_conversion(ADC_MCH(9), LPC_ADC_SEQA);
346 debug_printf(UART0, "ADC config done\n");
348 /* Led strip configuration */
349 ws2812_config(&ws2812_data_out_pin);
350 debug_printf(UART0, "Led config done\n");
353 timer_on(LPC_SCT, (10 * 1000 * 1000), NULL);
354 timer_pwm_config(LPC_SCT, &pwm_conf);
355 debug_printf(UART0, "Timer config done\n");
358 set_pins(capacitance_pins);
359 timer_start(LPC_SCT);
361 /* Register callback on button */
362 set_gpio_callback(button_request, &button, EDGES_BOTH);
364 i2c_on(I2C0, I2C_CLK_100KHz, I2C_MASTER);
368 /* Configure lux sensor */
369 ret = tsl256x_configure(&tsl256x_sensor);
372 debug_printf(UART0, "Lux config error: %d\n", ret);
376 /* Configure uv sensor */
377 ret = veml6070_configure(&veml6070_sensor);
380 debug_printf(UART0, "UV config error: %d\n", ret);
384 /* Configure barometric + humidity + temp sensor */
385 ret = bme280_configure(&bme280_sensor);
388 debug_printf(UART0, "Humidity config error: %d\n", ret);
392 /* Read our address from Flash */
393 memcpy((void*)&address, (void*)(0x00003FC0), 1);
395 /* No sensor should have adrress 0, which is reserved for the "waiting for new address" state */
398 debug_printf(UART0, "Address: %d\n", address);
407 /* Enter sleep / low-power ? */
409 /* Leave some time for the others */
410 msleep(50); /* Add some delay */
412 if (store_address == 1) {
416 ret = iap_prepare_flash(15, 15); /* Prepare sector 15 (16K flash size) for erase */
417 debug((ret != IAP_STATUS_CMD_SUCCESS), "Error preparing flash: %d\n", ret);
419 ret = iap_erase_flash_pages(255, 255); /* Erase the last page of the Flash */
420 debug((ret != IAP_STATUS_CMD_SUCCESS), "Error erasing page 255: %d\n", ret);
422 ret = iap_prepare_flash(15, 15); /* Prepare sector 15 (16K flash size) for write */
423 debug((ret != IAP_STATUS_CMD_SUCCESS), "Error preparing flash: %d\n", ret);
426 ret = iap_copy_ram_to_flash(0x00003FC0, (uint32_t)(&buf), 64);
427 debug((ret != IAP_STATUS_CMD_SUCCESS), "Error writting our number: %d\n", ret);
429 debug(1, "Changed our address to %d\n", address);
435 set_led(req_data[0], req_data[1], req_data[2]);
436 debug(1, "Set led to %d %d %d\n", req_data[0], req_data[1], req_data[2]);
440 debug(1, "Unhandled command 0x%02x (%c)\n", req_cmd, req_cmd);
446 /* Get hold of Tx line */
447 if (need_config == 1) {
448 /* Request a new address from host */
450 /* Do not place any of the following instructions before the flush end */
453 if (serial_write(UART0, "$c", 2) == 2) {
457 /* And wait for fush end before relaesing the line */
464 if (send_data == 1) {
466 /* Take humidity sample */
467 soil_humidity_sample();
470 ret = tsl256x_sensor_read(&tsl256x_sensor, NULL, &ir, &lux);
471 debug((ret != 0), "Lux read error: %d\n", ret);
475 ret = veml6070_sensor_read(&veml6070_sensor, &uv);
476 debug((ret != 0), "UV read error: %d\n", ret);
480 ret = bme280_sensor_read(&bme280_sensor, &pressure, &temp, &humidity);
481 debug((ret != 0), "Humidity read error: %d\n", ret);
483 comp_temp = bme280_compensate_temperature(&bme280_sensor, temp) / 10;
484 pressure = bme280_compensate_pressure(&bme280_sensor, pressure) / 100;
485 humidity = bme280_compensate_humidity(&bme280_sensor, humidity) / 10;
489 debug(1, "Sensor %d:\n\tSoil: %d\n", address, raw_humidity);
490 debug(1, "\tLux: %d, IR: %d, UV: %d\n", lux, ir, uv);
491 debug(1, "\tPatm: %d hPa, Temp: %d,%02d degC, Humidity: %d,%d rH\n\n",
493 comp_temp / 10, (comp_temp > 0) ? (comp_temp % 10) : ((-comp_temp) % 10),
494 humidity / 10, humidity % 10);
496 set_led(0, 0, ((raw_humidity >> 6) & 0xFF));
498 /* Send for control */
501 uint16_t* data = (uint16_t*)buff;
504 buff[1] = address | (got_tsl << 5) | (got_veml << 6) | (got_bme << 7);
505 data[1] = (uint16_t)htons(raw_humidity);
506 data[2] = (uint16_t)htons(lux);
507 data[3] = (uint16_t)htons(ir);
508 data[4] = (uint16_t)htons(uv);
509 data[5] = (uint16_t)htons(pressure);
510 data[6] = (uint16_t)htons(comp_temp);
511 data[7] = (uint16_t)htons(humidity);
514 /* Do not place any of the following instructions before the flush end */
517 serial_write(UART0, buff, 20);
518 /* And wait for fush end before relaesing the line */