Browse Source

option to block on uart buffer overrun

Thomas Buck 1 year ago
parent
commit
3b40fe0f43
3 changed files with 26 additions and 2 deletions
  1. 2
    0
      include/config.h
  2. 1
    0
      include/ring.h
  3. 23
    2
      src/serial.c

+ 2
- 0
include/config.h View File

@@ -25,6 +25,8 @@
25 25
 //#define DISABLE_CDC_DTR_CHECK
26 26
 #define DEBOUNCE_DELAY_MS 5
27 27
 
28
+#define SERIAL_WRITES_BLOCK_WHEN_BUFFER_FULL
29
+
28 30
 #define DEBUG_DISK_WRITE_SOURCES
29 31
 
30 32
 #define DISK_BLOCK_SIZE 512

+ 1
- 0
include/ring.h View File

@@ -33,6 +33,7 @@ struct ring_buffer {
33 33
 
34 34
 void rb_add(struct ring_buffer *rb, const uint8_t *data, size_t length);
35 35
 size_t rb_len(struct ring_buffer *rb);
36
+#define rb_space(rb) ((rb)->size - rb_len(rb))
36 37
 void rb_dump(struct ring_buffer *rb, void (*write)(const uint8_t *, size_t));
37 38
 uint8_t rb_pop(struct ring_buffer *rb);
38 39
 

+ 23
- 2
src/serial.c View File

@@ -76,8 +76,10 @@ static void serial_irq(void) {
76 76
 
77 77
 void serial_init(void) {
78 78
     uart_init(UART_ID, BAUD_RATE);
79
+
79 80
     gpio_set_function(UART_TX_PIN, GPIO_FUNC_UART);
80 81
     gpio_set_function(UART_RX_PIN, GPIO_FUNC_UART);
82
+
81 83
     uart_set_hw_flow(UART_ID, false, false);
82 84
     uart_set_format(UART_ID, DATA_BITS, STOP_BITS, PARITY);
83 85
     uart_set_fifo_enabled(UART_ID, true);
@@ -85,11 +87,30 @@ void serial_init(void) {
85 87
     irq_set_exclusive_handler(UART_IRQ, serial_irq);
86 88
     irq_set_enabled(UART_IRQ, true);
87 89
 
88
-    uart_set_irq_enables(UART_ID, true, false);
90
+    uart_set_irq_enables(UART_ID, true, true);
89 91
 }
90 92
 
91 93
 void serial_write(const uint8_t *buf, size_t count) {
92
-    rb_add(&tx, buf, count);
94
+    uart_set_irq_enables(UART_ID, true, false);
95
+
96
+    size_t off = 0;
97
+
98
+#ifdef SERIAL_WRITES_BLOCK_WHEN_BUFFER_FULL
99
+    while (count > rb_space(&tx)) {
100
+        size_t space = rb_space(&tx);
101
+        rb_add(&tx, buf + off, space);
102
+        count -= space;
103
+        off += space;
104
+
105
+        uart_set_irq_enables(UART_ID, true, true);
106
+        sleep_ms(1);
107
+        uart_set_irq_enables(UART_ID, true, false);
108
+    }
109
+#endif // SERIAL_WRITES_BLOCK_WHEN_BUFFER_FULL
110
+
111
+    rb_add(&tx, buf + off, count);
112
+
113
+    uart_set_irq_enables(UART_ID, true, true);
93 114
 }
94 115
 
95 116
 void serial_set_reroute(bool reroute) {

Loading…
Cancel
Save