aboutsummaryrefslogtreecommitdiff
path: root/protocol/serial_uart.c
diff options
context:
space:
mode:
Diffstat (limited to 'protocol/serial_uart.c')
-rw-r--r--protocol/serial_uart.c21
1 files changed, 20 insertions, 1 deletions
diff --git a/protocol/serial_uart.c b/protocol/serial_uart.c
index 6c0af8817..35df27fd6 100644
--- a/protocol/serial_uart.c
+++ b/protocol/serial_uart.c
@@ -41,13 +41,29 @@ POSSIBILITY OF SUCH DAMAGE.
41#include "serial.h" 41#include "serial.h"
42 42
43 43
44#if defined(SERIAL_UART_RTS_LO) && defined(SERIAL_UART_RTS_HI)
45 // Buffer state
46 // Empty: RBUF_SPACE == RBUF_SIZE(head==tail)
47 // Last 1 space: RBUF_SPACE == 2
48 // Full: RBUF_SPACE == 1(last cell of rbuf be never used.)
49 #define RBUF_SPACE() (rbuf_head < rbuf_tail ? (rbuf_tail - rbuf_head) : (RBUF_SIZE - rbuf_head + rbuf_tail))
50 // allow to send
51 #define rbuf_check_rts_lo() do { if (RBUF_SPACE() > 2) SERIAL_UART_RTS_LO(); } while (0)
52 // prohibit to send
53 #define rbuf_check_rts_hi() do { if (RBUF_SPACE() <= 2) SERIAL_UART_RTS_HI(); } while (0)
54#else
55 #define rbuf_check_rts_lo()
56 #define rbuf_check_rts_hi()
57#endif
58
59
44void serial_init(void) 60void serial_init(void)
45{ 61{
46 SERIAL_UART_INIT(); 62 SERIAL_UART_INIT();
47} 63}
48 64
49// RX ring buffer 65// RX ring buffer
50#define RBUF_SIZE 8 66#define RBUF_SIZE 256
51static uint8_t rbuf[RBUF_SIZE]; 67static uint8_t rbuf[RBUF_SIZE];
52static uint8_t rbuf_head = 0; 68static uint8_t rbuf_head = 0;
53static uint8_t rbuf_tail = 0; 69static uint8_t rbuf_tail = 0;
@@ -61,6 +77,7 @@ uint8_t serial_recv(void)
61 77
62 data = rbuf[rbuf_tail]; 78 data = rbuf[rbuf_tail];
63 rbuf_tail = (rbuf_tail + 1) % RBUF_SIZE; 79 rbuf_tail = (rbuf_tail + 1) % RBUF_SIZE;
80 rbuf_check_rts_lo();
64 return data; 81 return data;
65} 82}
66 83
@@ -73,6 +90,7 @@ int16_t serial_recv2(void)
73 90
74 data = rbuf[rbuf_tail]; 91 data = rbuf[rbuf_tail];
75 rbuf_tail = (rbuf_tail + 1) % RBUF_SIZE; 92 rbuf_tail = (rbuf_tail + 1) % RBUF_SIZE;
93 rbuf_check_rts_lo();
76 return data; 94 return data;
77} 95}
78 96
@@ -90,4 +108,5 @@ ISR(SERIAL_UART_RXD_VECT)
90 rbuf[rbuf_head] = SERIAL_UART_DATA; 108 rbuf[rbuf_head] = SERIAL_UART_DATA;
91 rbuf_head = next; 109 rbuf_head = next;
92 } 110 }
111 rbuf_check_rts_hi();
93} 112}