Send data in network endianness Add information to README about the data packet format.
[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 "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
50 #define DEBUG 0
51 #if (DEBUG == 1)
52  #define debug_printf(...) \
53         gpio_clear(tx_en); \
54         uprintf(__VA_ARGS__); \
55         gpio_set(tx_en);
57  #define debug(cond, ...) \
58     if (cond) { \
59                 gpio_clear(tx_en); \
60                 uprintf(UART0, __VA_ARGS__ ); \
61                 gpio_set(tx_en); \
62         }
64 #else
65  #define debug_printf(...) ;
66  #define debug(...) ;
67 #endif
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..
74  */
75 const struct pio_config common_pins[] = {
76         /* UART 0 */
77         { LPC_GPIO_0_0, LPC_UART0_RX, 0 },
78         { LPC_GPIO_0_4, LPC_UART0_TX, 0 },
79         /* I2C 0 */
80         { LPC_I2C0_SCL_PIO_0_10, LPC_FIXED, 0 },
81         { LPC_I2C0_SDA_PIO_0_11, LPC_FIXED, 0 },
82         /* ADC */
83         { LPC_ADC_AD9_PIO_0_17, LPC_FIXED, 0 },
84         /* Timers */
85         { LPC_GPIO_0_9, LPC_SCT_POUT0, 0 },
86         /* GPIO */
87         { LPC_GPIO_0_2, LPC_GPIO, 0 },  /* Led */
88     { LPC_GPIO_0_3, LPC_GPIO, 0 },  /* Tx enable */
89         ARRAY_LAST_PIO,
90 };
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 },
96         ARRAY_LAST_PIO,
97 };
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 = {
114         .bus_num = I2C0,
115         .addr = BME280_ADDR,
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,
122 };
126 /***************************************************************************** */
127 /* Luminosity */
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 = {
132         .bus_num = I2C0,
133         .addr = TSL256x_ADDR,
134         .gain = TSL256x_LOW_GAIN,
135         .integration_time = TSL256x_INTEGRATION_100ms,
136         .package = TSL256x_PACKAGE_T,
137 };
142 /***************************************************************************** */
143 /* UV */
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 = {
149         .bus_num = I2C0,
150         .addr = VEML6070_ADDR,
151 };
155 /***************************************************************************** */
156 /* Soil humidity sensor  */
158 uint16_t raw_humidity = 0;
159 void soil_humidity_sample(void)
161         /* Get ADC values */
162         adc_get_value(&raw_humidity, LPC_ADC(9));
166 /* Set led */
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 /***************************************************************************** */
176 void system_init()
178         system_set_default_power_state();
179         clock_config(SELECTED_FREQ);
180         set_pins(common_pins);
181         gpio_on();
182         /* System tick timer MUST be configured and running in order to use the sleeping
183          * functions */
184         systick_timer_on(1); /* 1ms */
185         systick_start();
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.
192  */
193 void fault_info(const char* name, uint32_t len)
195         debug_printf(UART0, name);
196         while (1);
199 const struct lpc_timer_pwm_config pwm_conf = {
200         .nb_channels = 1,
201         .period = 30,
202         .outputs_initial_state = 0x01,
203         .match_values = { 10, },
204         .outputs = { 0, },
205 };
208 enum states {
209         IDLE = 0,
210         MSG_START,
211         ADDRESSSED,
212         NEED_DATA,
213 };
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;
225 /* serial_req */
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;
231         switch (state) {
232                 case IDLE:
233                         if (c == MSG_FIRST_BYTE) {
234                                 state = MSG_START;
235                         }
236                         break;
238                 case MSG_START:
239                         if ((c == address) || (c == 0xFF)) {
240                                 state = ADDRESSSED;
241                         } else {
242                                 state = IDLE;
243                         }
244                         break;
246                 case ADDRESSSED:
247                         if ((address == 0) & (addr_req == 1)) {
248                                 /* We received our address, store it */
249                                 address = c;
250                                 store_address = 1;
251                                 state = IDLE;
252                                 break;
253                         }
254                         switch (c) {
255                                 case 'a':
256                                         send_data = 1;
257                                         state = IDLE;
258                                         break;
259                                 case 'l':
260                                         req_need_data_len = 3;
261                                         tmp_req_cmd = 'l';
262                                         state = NEED_DATA;
263                                         break;
264                         }
265                         break;
267                 case NEED_DATA:
268                         req_data[req_data_len++] = c;
269                         if (req_data_len >= req_need_data_len) {
270                                 req_need_data_len = 0;
271                                 state = IDLE;
272                                 req_cmd = tmp_req_cmd;
273                                 tmp_req_cmd = 0;
274                         }
275                         break;
277                 default:
278                         state = IDLE;
279         }
282 /* Should check button "hold" duration and act accordingly
283  * When pressed with no addresss : should request address from host
284  */
285 volatile uint8_t need_config = 0;
286 void button_request(uint32_t gpio)
288         static uint32_t tick_press_date = 0;
289         int delta;
290         if (gpio_read(button) == 0) {
291                 tick_press_date = systick_get_tick_count();
292                 return;
293         }
294         delta = systick_get_tick_count() - tick_press_date;
295         if (delta < 0) {
296                 return; /* Timer wrapped while reading button hold duration ... let user press again. */
297         }
298         if (address == 0) {
299                 need_config = 1;
300                 return;
301         }
302         if (delta < 2000) {
303                 /* Send a sample .... */
304                 /* This should enter the "menu" loop */
305                 send_data = 1;
306         } else {
307                 address = 0;
308                 need_config = 1;
309         }
311 /***************************************************************************** */
312 int main(void)
314         uint8_t got_tsl = 1, got_veml = 1, got_bme = 1;
315         /* TSL2561 and VEML6070 */
316         uint16_t uv = 0, ir = 0;
317         uint32_t lux = 0;
318         /* BME280 */
319         uint32_t pressure = 0, temp = 0;
320         uint16_t humidity = 0;
321         int comp_temp = 0;
322         int ret = 0;
324         system_init();
326         /* Communication */
327         uart_on(UART0, 115200, serial_req);
328         config_gpio(&tx_en, 0, GPIO_DIR_OUT, 1);
330         /* ADC */
331         config_gpio(&cap_trig_gpio, 0, GPIO_DIR_OUT, 0);
332         config_gpio(&cap_adc_gpio, 0, GPIO_DIR_OUT, 0);
333         adc_on(NULL);
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");
341         /* Timer config */
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");
346         /* Start sampling */
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);
355         msleep(10);
357         /* Configure lux sensor */
358         ret = tsl256x_configure(&tsl256x_sensor);
359         if (ret != 0) {
360                 got_tsl = 0;
361                 debug_printf(UART0, "Lux config error: %d\n", ret);
362         }
363         msleep(10);
365         /* Configure uv sensor */
366         ret = veml6070_configure(&veml6070_sensor);
367         if (ret != 0) {
368                 got_veml = 0;
369                 debug_printf(UART0, "UV config error: %d\n", ret);
370         }
371         msleep(10);
373         /* Configure barometric + humidity + temp sensor */
374         ret = bme280_configure(&bme280_sensor);
375         if (ret != 0) {
376                 got_bme = 0;
377                 debug_printf(UART0, "Humidity config error: %d\n", ret);
378         }
379         msleep(10);
381         /* Read our address from Flash */
382         memcpy((void*)&address, (void*)(0x00003FC0), 1);
383         if (address == 0) {
384                 /* No sensor should have adrress 0, which is reserved for the "waiting for new address" state */
385                 address = 255;
386         }
387         debug_printf(UART0, "Address: %d\n", address);
389         send_data = 0;
390         store_address = 0;
391         req_cmd = 0;
392         need_config = 0;
394         while (1) {
396                 /* Enter sleep / low-power ? */
398                 /* Leave some time for the others */            
399                 msleep(50); /* Add some delay */
401                 if (store_address == 1) {
402                         char buf[64] = "";
403                         store_address = 0;
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);
414                         buf[0] = address;
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);
419                 }
421                 if (req_cmd != 0) {
422                         switch (req_cmd) {
423                                 case 'l':
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]); 
426                                         break;
428                                 default:
429                                         debug(1, "Unhandled command 0x%02x (%c)\n", req_cmd, req_cmd);
430                         }
431                         req_cmd = 0;
432                         req_data_len = 0;
433                 }
435                 /* Get hold of Tx line */
436                 if (need_config == 1) {
437                         /* Request a new address from host */
438                         gpio_clear(tx_en);
439                         if (serial_send_quickbyte(UART0, 'c') == 0) {
440                                 addr_req = 1;
441                                 need_config = 0;
442                         }
443                         gpio_set(tx_en);
444                 }
446                 if (send_data == 1) {
447                         send_data = 0;
448                         /* Take humidity sample */
449                         soil_humidity_sample();
451                         if (got_tsl == 1) {
452                                 ret = tsl256x_sensor_read(&tsl256x_sensor, NULL, &ir, &lux);
453                                 debug((ret != 0), "Lux read error: %d\n", ret);
454                                 msleep(2);
455                         }
456                         if (got_veml == 1) {
457                                 ret = veml6070_sensor_read(&veml6070_sensor, &uv);
458                                 debug((ret != 0), "UV read error: %d\n", ret);
459                                 msleep(2);
460                         }
461                         if (got_bme == 1) {
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;
468                         }
470                         /* Display all */
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",
474                                         pressure,
475                                         comp_temp / 10,  (comp_temp > 0) ? (comp_temp % 10) : ((-comp_temp) % 10),
476                                         humidity / 10, humidity % 10);
477         
478                         set_led(0, 0, ((raw_humidity >> 6) & 0xFF));
479         
480                         /* Send for control */
481                         if (1) {
482                                 char buff[20];
483                                 uint16_t* data = (uint16_t*)buff;
485                                 buff[0] = '#';
486                                 buff[1] = address | (got_tsl << 5) | (got_veml << 6) | (got_bme << 7);
487                                 data[1] = (uint16_t)htons(raw_humidity);
488                                 data[2] = (uint16_t)htons(lux);
489                                 data[3] = (uint16_t)htons(ir);
490                                 data[4] = (uint16_t)htons(uv);
491                                 data[5] = (uint16_t)htons(pressure);
492                                 data[6] = (uint16_t)htons(comp_temp);
493                                 data[7] = (uint16_t)htons(humidity);
495                                 gpio_clear(tx_en);
496                                 serial_write(UART0, buff, 20);
497                                 gpio_set(tx_en);
498                         }
499                 }
500         }
502         return 0;