From ef9139a98cc45ccbfacb779d03b10d84d1fa4b44 Mon Sep 17 00:00:00 2001 From: Nathael Pajani Date: Wed, 24 May 2017 14:44:21 +0200 Subject: [PATCH] Add flushes and instruction barriers so that GCC does what we need with the Tx line on the communication port. --- v04/main.c | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/v04/main.c b/v04/main.c index be8d52f..ef4dae6 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(...) ; @@ -435,11 +446,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); } @@ -481,7 +499,7 @@ int main(void) if (1) { char buff[20]; uint16_t* data = (uint16_t*)buff; - + 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); @@ -492,8 +510,15 @@ int main(void) data[6] = (uint16_t)htons(comp_temp); data[7] = (uint16_t)htons(humidity); + serial_flush(UART0); + /* Do not place any of the following instructions before the flush end */ + isb(); gpio_clear(tx_en); serial_write(UART0, buff, 20); + /* And wait for fush end before relaesing the line */ + isb(); + serial_flush(UART0); + isb(); gpio_set(tx_en); } } -- 2.43.0