1
1
//
2
2
// serial.c - serial (UART) port library
3
3
//
4
- // v1.0 / 2019-06-03 / Io Engineering / Terje
4
+ // v1.1 / 2021-07-15 / Io Engineering / Terje
5
5
//
6
6
//
7
7
8
8
/*
9
9
10
- Copyright (c) 2015-2019 , Terje Io
10
+ Copyright (c) 2015-2021 , Terje Io
11
11
All rights reserved.
12
12
13
13
Redistribution and use in source and binary forms, with or without modification,
@@ -43,11 +43,11 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43
43
44
44
#include "serial.h"
45
45
#include "grbl/hal.h"
46
-
47
- #define BUFCOUNT (head , tail , size ) ((head >= tail) ? (head - tail) : (size - tail + head))
46
+ #include "grbl/protocol.h"
48
47
49
48
static char txbuf [TX_BUFFER_SIZE ];
50
49
static char rxbuf [RX_BUFFER_SIZE ];
50
+ static enqueue_realtime_command_ptr enqueue_realtime_command = protocol_enqueue_realtime_command ;
51
51
52
52
const char eol [] = "\r\n" ;
53
53
static volatile uint16_t tx_head = 0 , tx_tail = 0 , rx_head = 0 , rx_tail = 0 , rx_overflow = 0 ;
@@ -56,63 +56,45 @@ static volatile uint16_t tx_head = 0, tx_tail = 0, rx_head = 0, rx_tail = 0, rx_
56
56
static volatile unsigned int rx_off = XONOK ;
57
57
#endif
58
58
59
- inline void setUCA1BR (uint16_t prescaler )
59
+ static inline void setUCA1BR (uint16_t prescaler )
60
60
{
61
61
UCA1BR0 = prescaler & 0xFF ; // LSB
62
62
UCA1BR1 = prescaler >> 8 ; // MSB
63
63
}
64
-
65
- void serialInit (void )
66
- {
67
- UCA1CTL1 = UCSWRST ;
68
- UCA1CTL1 |= UCSSEL_2 ; // Use SMCLK
69
- setUCA1BR (217 ); // Set baudrate to 115200 @ 25MHz SMCLK
70
- UCA1MCTL = 0 ; // Modulation UCBRSx=0, UCBRFx=0
71
- UCA1CTL0 = 0 ; // 8 bit, 1 stop bit, no parity
72
- SERIAL_SEL |= SERIAL_RXD |SERIAL_TXD ;
73
- UCA1CTL1 &= ~UCSWRST ; // Initialize USCI state machine
74
- UCA1IE |= UCRXIE ; // Enable USCI_A0 RX interrupt
75
-
76
- #ifndef XONXOFF
77
- SERIAL_RTS_PORT_DIR |= SERIAL_RTS_BIT ; // Enable RTS pin
78
- SERIAL_RTS_PORT_OUT &= ~SERIAL_RTS_BIT ; // and drive it low
79
- #endif
80
-
81
- }
82
-
83
- uint16_t serialTxCount (void )
64
+ /*
65
+ static uint16_t serialTxCount (void)
84
66
{
85
67
uint16_t tail = tx_tail;
86
68
return BUFCOUNT(tx_head, tail, TX_BUFFER_SIZE);
87
69
}
88
70
89
- uint16_t serialRxCount (void )
71
+ static uint16_t serialRxCount (void)
90
72
{
91
73
uint16_t tail = rx_tail, head = rx_head;
92
74
return BUFCOUNT(head, tail, RX_BUFFER_SIZE);
93
75
}
94
-
95
- uint16_t serialRxFree (void )
76
+ */
77
+ static uint16_t serialRxFree (void )
96
78
{
97
79
unsigned int tail = rx_tail , head = rx_head ;
98
80
return RX_BUFFER_SIZE - BUFCOUNT (head , tail , RX_BUFFER_SIZE );
99
81
}
100
82
101
- void serialRxFlush (void )
83
+ static void serialRxFlush (void )
102
84
{
103
85
rx_head = rx_tail = 0 ;
104
86
SERIAL_RTS_PORT_OUT &= ~SERIAL_RTS_BIT ;
105
87
}
106
88
107
- void serialRxCancel (void )
89
+ static void serialRxCancel (void )
108
90
{
109
91
rxbuf [rx_head ] = ASCII_CAN ;
110
92
rx_tail = rx_head ;
111
93
rx_head = (rx_tail + 1 ) & (RX_BUFFER_SIZE - 1 );
112
94
SERIAL_RTS_PORT_OUT &= ~SERIAL_RTS_BIT ;;
113
95
}
114
96
115
- bool serialPutC (const char data )
97
+ static bool serialPutC (const char data )
116
98
{
117
99
unsigned int next_head = tx_head ;
118
100
@@ -134,8 +116,8 @@ bool serialPutC(const char data)
134
116
return true;
135
117
}
136
118
137
- int16_t serialGetC (void ) {
138
-
119
+ static int16_t serialGetC (void )
120
+ {
139
121
if (rx_tail == rx_head )
140
122
return -1 ;
141
123
@@ -164,27 +146,69 @@ int16_t serialGetC (void) {
164
146
return (int16_t )data ;
165
147
}
166
148
167
- void serialWriteS (const char * data ) {
149
+ static void serialWriteS (const char * data )
150
+ {
168
151
169
152
char c , * ptr = (char * )data ;
170
153
171
154
while ((c = * ptr ++ ) != '\0' )
172
155
serialPutC (c );
173
-
174
156
}
175
-
176
- void serialWriteLn (const char * data ) {
157
+ /*
158
+ static void serialWriteLn (const char *data)
159
+ {
177
160
serialWriteS(data);
178
161
serialWriteS(eol);
179
162
}
180
163
181
- void serialWrite (const char * data , uint16_t length ) {
182
-
164
+ static void serialWrite (const char *data, uint16_t length)
165
+ {
183
166
char *ptr = (char *)data;
184
167
185
168
while(length--)
186
169
serialPutC(*ptr++);
170
+ }
171
+ */
172
+ static enqueue_realtime_command_ptr serialSetRtHandler (enqueue_realtime_command_ptr handler )
173
+ {
174
+ enqueue_realtime_command_ptr prev = enqueue_realtime_command ;
175
+
176
+ if (handler )
177
+ enqueue_realtime_command = handler ;
178
+
179
+ return prev ;
180
+ }
181
+
182
+ const io_stream_t * serialInit (void )
183
+ {
184
+ static const io_stream_t stream = {
185
+ .type = StreamType_Serial ,
186
+ .connected = true,
187
+ .read = serialGetC ,
188
+ .write = serialWriteS ,
189
+ .write_char = serialPutC ,
190
+ .write_all = serialWriteS ,
191
+ .get_rx_buffer_free = serialRxFree ,
192
+ .reset_read_buffer = serialRxFlush ,
193
+ .cancel_read_buffer = serialRxCancel ,
194
+ .set_enqueue_rt_handler = serialSetRtHandler
195
+ };
196
+
197
+ UCA1CTL1 = UCSWRST ;
198
+ UCA1CTL1 |= UCSSEL_2 ; // Use SMCLK
199
+ setUCA1BR (217 ); // Set baudrate to 115200 @ 25MHz SMCLK
200
+ UCA1MCTL = 0 ; // Modulation UCBRSx=0, UCBRFx=0
201
+ UCA1CTL0 = 0 ; // 8 bit, 1 stop bit, no parity
202
+ SERIAL_SEL |= SERIAL_RXD |SERIAL_TXD ;
203
+ UCA1CTL1 &= ~UCSWRST ; // Initialize USCI state machine
204
+ UCA1IE |= UCRXIE ; // Enable USCI_A0 RX interrupt
205
+
206
+ #ifndef XONXOFF
207
+ SERIAL_RTS_PORT_DIR |= SERIAL_RTS_BIT ; // Enable RTS pin
208
+ SERIAL_RTS_PORT_OUT &= ~SERIAL_RTS_BIT ; // and drive it low
209
+ #endif
187
210
211
+ return & stream ;
188
212
}
189
213
190
214
#pragma vector=USCI_A1_VECTOR
@@ -204,7 +228,7 @@ __interrupt void USCI1RX_ISR(void)
204
228
next_head = UCA1RXBUF ; // and do dummy read to clear interrupt
205
229
} else {
206
230
char data = UCA1RXBUF ;
207
- if (!hal . stream . enqueue_realtime_command (data )) {
231
+ if (!enqueue_realtime_command (data )) {
208
232
rxbuf [rx_head ] = data ; // Add data to buffer
209
233
rx_head = next_head ; // and update pointer
210
234
}
0 commit comments