Add flushes and instruction barriers so that GCC does what we need with the Tx line...
authorNathael Pajani <nathael.pajani@ed3l.fr>
Wed, 24 May 2017 12:44:21 +0000 (14:44 +0200)
committerNathael Pajani <nathael.pajani@ed3l.fr>
Sat, 11 Feb 2023 04:09:54 +0000 (05:09 +0100)
v04/main.c

index be8d52f..ef4dae6 100644 (file)
@@ -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"
 #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);
                        }
                }