瀏覽代碼

Make rx tail/head atomic

AnHardt 8 年之前
父節點
當前提交
dc0f41868e
共有 2 個檔案被更改,包括 8 行新增7 行删除
  1. 2
    2
      Marlin/MarlinSerial.cpp
  2. 6
    5
      Marlin/MarlinSerial.h

+ 2
- 2
Marlin/MarlinSerial.cpp 查看文件

@@ -33,7 +33,7 @@
33 33
 #endif
34 34
 
35 35
 FORCE_INLINE void store_char(unsigned char c) {
36
-  int i = (unsigned int)(rx_buffer.head + 1) % RX_BUFFER_SIZE;
36
+  uint8_t i = (uint8_t)(rx_buffer.head + 1) % RX_BUFFER_SIZE;
37 37
 
38 38
   // if we should be storing the received character into the location
39 39
   // just before the tail (meaning that the head would advance to the
@@ -116,7 +116,7 @@ int MarlinSerial::read(void) {
116 116
   }
117 117
   else {
118 118
     unsigned char c = rx_buffer.buffer[rx_buffer.tail];
119
-    rx_buffer.tail = (unsigned int)(rx_buffer.tail + 1) % RX_BUFFER_SIZE;
119
+    rx_buffer.tail = (uint8_t)(rx_buffer.tail + 1) % RX_BUFFER_SIZE;
120 120
     return c;
121 121
   }
122 122
 }

+ 6
- 5
Marlin/MarlinSerial.h 查看文件

@@ -69,13 +69,14 @@
69 69
 // using a ring buffer (I think), in which rx_buffer_head is the index of the
70 70
 // location to which to write the next incoming character and rx_buffer_tail
71 71
 // is the index of the location from which to read.
72
+// 256 is the max limit due to uint8_t head and tail. Thats needed to make them atomic.
72 73
 #define RX_BUFFER_SIZE 128
73 74
 
74 75
 
75 76
 struct ring_buffer {
76 77
   unsigned char buffer[RX_BUFFER_SIZE];
77
-  int head;
78
-  int tail;
78
+  volatile uint8_t head;
79
+  volatile uint8_t tail;
79 80
 };
80 81
 
81 82
 #if UART_PRESENT(SERIAL_PORT)
@@ -92,8 +93,8 @@ class MarlinSerial { //: public Stream
92 93
     int read(void);
93 94
     void flush(void);
94 95
 
95
-    FORCE_INLINE int available(void) {
96
-      return (unsigned int)(RX_BUFFER_SIZE + rx_buffer.head - rx_buffer.tail) % RX_BUFFER_SIZE;
96
+    FORCE_INLINE uint8_t available(void) {
97
+      return (uint8_t)(RX_BUFFER_SIZE + rx_buffer.head - rx_buffer.tail) % RX_BUFFER_SIZE;
97 98
     }
98 99
 
99 100
     FORCE_INLINE void write(uint8_t c) {
@@ -105,7 +106,7 @@ class MarlinSerial { //: public Stream
105 106
     FORCE_INLINE void checkRx(void) {
106 107
       if (TEST(M_UCSRxA, M_RXCx)) {
107 108
         unsigned char c  =  M_UDRx;
108
-        int i = (unsigned int)(rx_buffer.head + 1) % RX_BUFFER_SIZE;
109
+        uint8_t i = (uint8_t)(rx_buffer.head + 1) % RX_BUFFER_SIZE;
109 110
 
110 111
         // if we should be storing the received character into the location
111 112
         // just before the tail (meaning that the head would advance to the

Loading…
取消
儲存