ef4dae6efa52e4332a8c7b5399e48e92bc7affcb
[soft/lpc82x/exanh] / v04 / main.c
1 /****************************************************************************
2  *   sensors/main.c
3  *
4  * Exanh Gardener soil moisture sensor support.
5  *
6  * Copyright 2016-2017 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 "core/iap.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
51 #define DEBUG 1
52 #if (DEBUG == 1) /* Send only on prog output */
53  #define debug_printf(...) \
54         uprintf(__VA_ARGS__); \
56  #define debug(cond, ...) \
57     if (cond) { \
58                 uprintf(UART0, __VA_ARGS__ ); \
59         }
61 #elif (DEBUG == 2) /* Send on comm port */
62  #define debug_printf(...) \
63         gpio_clear(tx_en); \
64         uprintf(__VA_ARGS__); \
65         serial_flush(UART0); \
66         gpio_set(tx_en);
68  #define debug(cond, ...) \
69     if (cond) { \
70                 gpio_clear(tx_en); \
71                 uprintf(UART0, __VA_ARGS__ ); \
72                 serial_flush(UART0); \
73                 gpio_set(tx_en); \
74         }
75 #else
76  #define debug_printf(...) ;
77  #define debug(...) ;
78 #endif
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..
85  */
86 const struct pio_config common_pins[] = {
87         /* UART 0 */
88         { LPC_GPIO_0_0, LPC_UART0_RX, 0 },
89         { LPC_GPIO_0_4, LPC_UART0_TX, 0 },
90         /* I2C 0 */
91         { LPC_I2C0_SCL_PIO_0_10, LPC_FIXED, 0 },
92         { LPC_I2C0_SDA_PIO_0_11, LPC_FIXED, 0 },
93         /* ADC */
94         { LPC_ADC_AD9_PIO_0_17, LPC_FIXED, 0 },
95         /* Timers */
96         { LPC_GPIO_0_9, LPC_SCT_POUT0, 0 },
97         /* GPIO */
98         { LPC_GPIO_0_2, LPC_GPIO, 0 },  /* Led */
99     { LPC_GPIO_0_3, LPC_GPIO, 0 },  /* Tx enable */
100         ARRAY_LAST_PIO,
101 };
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 },
107         ARRAY_LAST_PIO,
108 };
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 = {
125         .bus_num = I2C0,
126         .addr = BME280_ADDR,
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,
133 };
137 /***************************************************************************** */
138 /* Luminosity */
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 = {
143         .bus_num = I2C0,
144         .addr = TSL256x_ADDR,
145         .gain = TSL256x_LOW_GAIN,
146         .integration_time = TSL256x_INTEGRATION_100ms,
147         .package = TSL256x_PACKAGE_T,
148 };
153 /***************************************************************************** */
154 /* UV */
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 = {
160         .bus_num = I2C0,
161         .addr = VEML6070_ADDR,
162 };
166 /***************************************************************************** */
167 /* Soil humidity sensor  */
169 uint16_t raw_humidity = 0;
170 void soil_humidity_sample(void)
172         /* Get ADC values */
173         adc_get_value(&raw_humidity, LPC_ADC(9));
177 /* Set led */
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 /***************************************************************************** */
187 void system_init()
189         system_set_default_power_state();
190         clock_config(SELECTED_FREQ);
191         set_pins(common_pins);
192         gpio_on();
193         /* System tick timer MUST be configured and running in order to use the sleeping
194          * functions */
195         systick_timer_on(1); /* 1ms */
196         systick_start();
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.
203  */
204 void fault_info(const char* name, uint32_t len)
206         debug_printf(UART0, name);
207         while (1);
210 const struct lpc_timer_pwm_config pwm_conf = {
211         .nb_channels = 1,
212         .period = 30,
213         .outputs_initial_state = 0x01,
214         .match_values = { 10, },
215         .outputs = { 0, },
216 };
219 enum states {
220         IDLE = 0,
221         MSG_START,
222         ADDRESSSED,
223         NEED_DATA,
224 };
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;
236 /* serial_req */
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;
242         switch (state) {
243                 case IDLE:
244                         if (c == MSG_FIRST_BYTE) {
245                                 state = MSG_START;
246                         }
247                         break;
249                 case MSG_START:
250                         if ((c == address) || (c == 0xFF)) {
251                                 state = ADDRESSSED;
252                         } else {
253                                 state = IDLE;
254                         }
255                         break;
257                 case ADDRESSSED:
258                         if ((address == 0) & (addr_req == 1)) {
259                                 /* We received our address, store it */
260                                 address = c;
261                                 store_address = 1;
262                                 state = IDLE;
263                                 break;
264                         }
265                         switch (c) {
266                                 case 'a':
267                                         send_data = 1;
268                                         state = IDLE;
269                                         break;
270                                 case 'l':
271                                         req_need_data_len = 3;
272                                         tmp_req_cmd = 'l';
273                                         state = NEED_DATA;
274                                         break;
275                         }
276                         break;
278                 case NEED_DATA:
279                         req_data[req_data_len++] = c;
280                         if (req_data_len >= req_need_data_len) {
281                                 req_need_data_len = 0;
282                                 state = IDLE;
283                                 req_cmd = tmp_req_cmd;
284                                 tmp_req_cmd = 0;
285                         }
286                         break;
288                 default:
289                         state = IDLE;
290         }
293 /* Should check button "hold" duration and act accordingly
294  * When pressed with no addresss : should request address from host
295  */
296 volatile uint8_t need_config = 0;
297 void button_request(uint32_t gpio)
299         static uint32_t tick_press_date = 0;
300         int delta;
301         if (gpio_read(button) == 0) {
302                 tick_press_date = systick_get_tick_count();
303                 return;
304         }
305         delta = systick_get_tick_count() - tick_press_date;
306         if (delta < 0) {
307                 return; /* Timer wrapped while reading button hold duration ... let user press again. */
308         }
309         if (address == 0) {
310                 need_config = 1;
311                 return;
312         }
313         if (delta < 2000) {
314                 /* Send a sample .... */
315                 /* This should enter the "menu" loop */
316                 send_data = 1;
317         } else {
318                 address = 0;
319                 need_config = 1;
320         }
322 /***************************************************************************** */
323 int main(void)
325         uint8_t got_tsl = 1, got_veml = 1, got_bme = 1;
326         /* TSL2561 and VEML6070 */
327         uint16_t uv = 0, ir = 0;
328         uint32_t lux = 0;
329         /* BME280 */
330         uint32_t pressure = 0, temp = 0;
331         uint16_t humidity = 0;
332         int comp_temp = 0;
333         int ret = 0;
335         system_init();
337         /* Communication */
338         uart_on(UART0, 115200, serial_req);
339         config_gpio(&tx_en, 0, GPIO_DIR_OUT, 1);
341         /* ADC */
342         config_gpio(&cap_trig_gpio, 0, GPIO_DIR_OUT, 0);
343         config_gpio(&cap_adc_gpio, 0, GPIO_DIR_OUT, 0);
344         adc_on(NULL);
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");
352         /* Timer config */
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");
357         /* Start sampling */
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);
366         msleep(10);
368         /* Configure lux sensor */
369         ret = tsl256x_configure(&tsl256x_sensor);
370         if (ret != 0) {
371                 got_tsl = 0;
372                 debug_printf(UART0, "Lux config error: %d\n", ret);
373         }
374         msleep(10);
376         /* Configure uv sensor */
377         ret = veml6070_configure(&veml6070_sensor);
378         if (ret != 0) {
379                 got_veml = 0;
380                 debug_printf(UART0, "UV config error: %d\n", ret);
381         }
382         msleep(10);
384         /* Configure barometric + humidity + temp sensor */
385         ret = bme280_configure(&bme280_sensor);
386         if (ret != 0) {
387                 got_bme = 0;
388                 debug_printf(UART0, "Humidity config error: %d\n", ret);
389         }
390         msleep(10);
392         /* Read our address from Flash */
393         memcpy((void*)&address, (void*)(0x00003FC0), 1);
394         if (address == 0) {
395                 /* No sensor should have adrress 0, which is reserved for the "waiting for new address" state */
396                 address = 255;
397         }
398         debug_printf(UART0, "Address: %d\n", address);
400         send_data = 0;
401         store_address = 0;
402         req_cmd = 0;
403         need_config = 0;
405         while (1) {
407                 /* Enter sleep / low-power ? */
409                 /* Leave some time for the others */            
410                 msleep(50); /* Add some delay */
412                 if (store_address == 1) {
413                         char buf[64] = "";
414                         store_address = 0;
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);
425                         buf[0] = address;
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);
430                 }
432                 if (req_cmd != 0) {
433                         switch (req_cmd) {
434                                 case 'l':
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]); 
437                                         break;
439                                 default:
440                                         debug(1, "Unhandled command 0x%02x (%c)\n", req_cmd, req_cmd);
441                         }
442                         req_cmd = 0;
443                         req_data_len = 0;
444                 }
446                 /* Get hold of Tx line */
447                 if (need_config == 1) {
448                         /* Request a new address from host */
449                         serial_flush(UART0);
450                         /* Do not place any of the following instructions before the flush end */
451                         isb();
452                         gpio_clear(tx_en);
453                         if (serial_write(UART0, "$c", 2) == 2) {
454                                 addr_req = 1;
455                                 need_config = 0;
456                         }
457                         /* And wait for fush end before relaesing the line */
458                         isb();
459                         serial_flush(UART0);
460                         isb();
461                         gpio_set(tx_en);
462                 }
464                 if (send_data == 1) {
465                         send_data = 0;
466                         /* Take humidity sample */
467                         soil_humidity_sample();
469                         if (got_tsl == 1) {
470                                 ret = tsl256x_sensor_read(&tsl256x_sensor, NULL, &ir, &lux);
471                                 debug((ret != 0), "Lux read error: %d\n", ret);
472                                 msleep(2);
473                         }
474                         if (got_veml == 1) {
475                                 ret = veml6070_sensor_read(&veml6070_sensor, &uv);
476                                 debug((ret != 0), "UV read error: %d\n", ret);
477                                 msleep(2);
478                         }
479                         if (got_bme == 1) {
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;
486                         }
488                         /* Display all */
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",
492                                         pressure,
493                                         comp_temp / 10,  (comp_temp > 0) ? (comp_temp % 10) : ((-comp_temp) % 10),
494                                         humidity / 10, humidity % 10);
495         
496                         set_led(0, 0, ((raw_humidity >> 6) & 0xFF));
497         
498                         /* Send for control */
499                         if (1) {
500                                 char buff[20];
501                                 uint16_t* data = (uint16_t*)buff;
502                                 memset(buff, 0, 20);
503                                 buff[0] = '#';
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);
513                                 serial_flush(UART0);
514                                 /* Do not place any of the following instructions before the flush end */
515                                 isb();
516                                 gpio_clear(tx_en);
517                                 serial_write(UART0, buff, 20);
518                                 /* And wait for fush end before relaesing the line */
519                                 isb();
520                                 serial_flush(UART0);
521                                 isb();
522                                 gpio_set(tx_en);
523                         }
524                 }
525         }
527         return 0;