X-Git-Url: http://git.techno-innov.fr/?a=blobdiff_plain;f=v04%2Fmain.c;h=ff226cbf52f71d9e02cad1060b3141fb36de9f14;hb=2a3b309eae02d3d7b01562ebfafcb814c3b56b30;hp=be8d52f1b4001fc346e24f2cbcdd5767854ad659;hpb=6450d3e8a64b04f7fd929d1d1253cc5216b61590;p=soft%2Flpc82x%2Fexanh diff --git a/v04/main.c b/v04/main.c index be8d52f..ff226cb 100644 --- a/v04/main.c +++ b/v04/main.c @@ -29,6 +29,7 @@ #include "core/pio.h" #include "core/iap.h" #include "lib/stdio.h" +#include "lib/utils.h" #include "drivers/i2c.h" #include "drivers/adc.h" #include "drivers/serial.h" @@ -47,20 +48,30 @@ #define SELECTED_FREQ FREQ_SEL_36MHz -#define DEBUG 0 -#if (DEBUG == 1) +#define DEBUG 1 +#if (DEBUG == 1) /* Send only on prog output */ + #define debug_printf(...) \ + uprintf(__VA_ARGS__); \ + + #define debug(cond, ...) \ + if (cond) { \ + uprintf(UART0, __VA_ARGS__ ); \ + } + +#elif (DEBUG == 2) /* Send on comm port */ #define debug_printf(...) \ gpio_clear(tx_en); \ uprintf(__VA_ARGS__); \ + serial_flush(UART0); \ gpio_set(tx_en); #define debug(cond, ...) \ if (cond) { \ gpio_clear(tx_en); \ uprintf(UART0, __VA_ARGS__ ); \ + serial_flush(UART0); \ gpio_set(tx_en); \ } - #else #define debug_printf(...) ; #define debug(...) ; @@ -244,7 +255,7 @@ void serial_req(uint8_t c) break; case ADDRESSSED: - if ((address == 0) & (addr_req == 1)) { + if ((address == 0) && (addr_req == 1)) { /* We received our address, store it */ address = c; store_address = 1; @@ -308,6 +319,8 @@ void button_request(uint32_t gpio) need_config = 1; } } + + /***************************************************************************** */ int main(void) { @@ -435,11 +448,18 @@ int main(void) /* Get hold of Tx line */ if (need_config == 1) { /* Request a new address from host */ + serial_flush(UART0); + /* Do not place any of the following instructions before the flush end */ + isb(); gpio_clear(tx_en); - if (serial_send_quickbyte(UART0, 'c') == 0) { + if (serial_write(UART0, "$c", 2) == 2) { addr_req = 1; need_config = 0; } + /* And wait for fush end before relaesing the line */ + isb(); + serial_flush(UART0); + isb(); gpio_set(tx_en); } @@ -467,21 +487,12 @@ int main(void) humidity = bme280_compensate_humidity(&bme280_sensor, humidity) / 10; } - /* Display all */ - debug(1, "Sensor %d:\n\tSoil: %d\n", address, raw_humidity); - debug(1, "\tLux: %d, IR: %d, UV: %d\n", lux, ir, uv); - debug(1, "\tPatm: %d hPa, Temp: %d,%02d degC, Humidity: %d,%d rH\n\n", - pressure, - comp_temp / 10, (comp_temp > 0) ? (comp_temp % 10) : ((-comp_temp) % 10), - humidity / 10, humidity % 10); - - set_led(0, 0, ((raw_humidity >> 6) & 0xFF)); - /* Send for control */ if (1) { char buff[20]; uint16_t* data = (uint16_t*)buff; - + int i = 0, sum = 0; + memset(buff, 0, 20); buff[0] = '#'; buff[1] = address | (got_tsl << 5) | (got_veml << 6) | (got_bme << 7); data[1] = (uint16_t)htons(raw_humidity); @@ -491,11 +502,35 @@ int main(void) data[5] = (uint16_t)htons(pressure); data[6] = (uint16_t)htons(comp_temp); data[7] = (uint16_t)htons(humidity); - + /* Compute checksum */ + for (i = 0; i < 19; i++) { + sum += buff[i]; + } + buff[19] = (uint8_t)(sum & 0xFF); + serial_flush(UART0); + /* Do not place any of the following instructions before the flush end */ + isb(); gpio_clear(tx_en); + msleep(3); serial_write(UART0, buff, 20); + /* And wait for fush end before relaesing the line */ + isb(); + serial_flush(UART0); + isb(); gpio_set(tx_en); } + + /* Display all */ + if(0) { + debug(1, "Sensor %d:\n\tSoil: %d\n", address, raw_humidity); + debug(1, "\tLux: %d, IR: %d, UV: %d\n", lux, ir, uv); + debug(1, "\tPatm: %d hPa, Temp: %d,%02d degC, Humidity: %d,%d rH\n\n", + pressure, + comp_temp / 10, (comp_temp > 0) ? (comp_temp % 10) : ((-comp_temp) % 10), + humidity / 10, humidity % 10); + } + set_led(0, 0, ((raw_humidity >> 6) & 0xFF)); + } }