Browse Source

Make rx tail/head atomic

AnHardt 8 years ago
parent
commit
dc0f41868e
2 changed files with 8 additions and 7 deletions
  1. 2
    2
      Marlin/MarlinSerial.cpp
  2. 6
    5
      Marlin/MarlinSerial.h

+ 2
- 2
Marlin/MarlinSerial.cpp View File

33
 #endif
33
 #endif
34
 
34
 
35
 FORCE_INLINE void store_char(unsigned char c) {
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
   // if we should be storing the received character into the location
38
   // if we should be storing the received character into the location
39
   // just before the tail (meaning that the head would advance to the
39
   // just before the tail (meaning that the head would advance to the
116
   }
116
   }
117
   else {
117
   else {
118
     unsigned char c = rx_buffer.buffer[rx_buffer.tail];
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
     return c;
120
     return c;
121
   }
121
   }
122
 }
122
 }

+ 6
- 5
Marlin/MarlinSerial.h View File

69
 // using a ring buffer (I think), in which rx_buffer_head is the index of the
69
 // using a ring buffer (I think), in which rx_buffer_head is the index of the
70
 // location to which to write the next incoming character and rx_buffer_tail
70
 // location to which to write the next incoming character and rx_buffer_tail
71
 // is the index of the location from which to read.
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
 #define RX_BUFFER_SIZE 128
73
 #define RX_BUFFER_SIZE 128
73
 
74
 
74
 
75
 
75
 struct ring_buffer {
76
 struct ring_buffer {
76
   unsigned char buffer[RX_BUFFER_SIZE];
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
 #if UART_PRESENT(SERIAL_PORT)
82
 #if UART_PRESENT(SERIAL_PORT)
92
     int read(void);
93
     int read(void);
93
     void flush(void);
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
     FORCE_INLINE void write(uint8_t c) {
100
     FORCE_INLINE void write(uint8_t c) {
105
     FORCE_INLINE void checkRx(void) {
106
     FORCE_INLINE void checkRx(void) {
106
       if (TEST(M_UCSRxA, M_RXCx)) {
107
       if (TEST(M_UCSRxA, M_RXCx)) {
107
         unsigned char c  =  M_UDRx;
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
         // if we should be storing the received character into the location
111
         // if we should be storing the received character into the location
111
         // just before the tail (meaning that the head would advance to the
112
         // just before the tail (meaning that the head would advance to the

Loading…
Cancel
Save