forked from grbl/grbl
-
Notifications
You must be signed in to change notification settings - Fork 4
/
serial.c
292 lines (244 loc) · 8.92 KB
/
serial.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
serial.c - Low level functions for sending and recieving bytes via the serial port
Part of Grbl
Copyright (c) 2009-2011 Simen Svale Skogsrud
Copyright (c) 2011-2012 Sungeun K. Jeon
Grbl is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Grbl is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Grbl. If not, see <http://www.gnu.org/licenses/>.
*/
/* This code was initially inspired by the wiring_serial module by David A. Mellis which
used to be a part of the Arduino project. */
#if defined( PART_LM4F120H5QR ) // code for ARM
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/sysctl.h"
#include "driverlib/uart.h"
#include "driverlib/interrupt.h"
#include "driverlib/pin_map.h"
#include "driverlib/gpio.h"
#include "inc/hw_ints.h"
#else // code for AVR
#include <avr/interrupt.h>
#endif
#include "serial.h"
#include "config.h"
#include "motion_control.h"
#include "protocol.h"
uint8_t rx_buffer[RX_BUFFER_SIZE];
volatile uint8_t rx_buffer_head;
volatile uint8_t rx_buffer_tail;
uint8_t tx_buffer[TX_BUFFER_SIZE];
volatile uint8_t tx_buffer_head;
volatile uint8_t tx_buffer_tail;
#ifdef ENABLE_XONXOFF
volatile uint8_t flow_ctrl = XON_SENT; // Flow control state variable
// Returns the number of bytes in the RX buffer. This replaces a typical byte counter to prevent
// the interrupt and main programs from writing to the counter at the same time.
static uint8_t get_rx_buffer_count()
{
if (rx_buffer_head == rx_buffer_tail) { return(0); }
if (rx_buffer_head < rx_buffer_tail) { return(rx_buffer_tail-rx_buffer_head); }
return (RX_BUFFER_SIZE - (rx_buffer_head-rx_buffer_tail));
}
#endif
inline uint8_t receive_buffer_empty() {
return rx_buffer_head == rx_buffer_head;
}
inline uint8_t transmit_buffer_empty() {
return tx_buffer_head == tx_buffer_tail;
}
#ifdef PART_LM4F120H5QR
//ARM code
void arm_uart_receive_data( void );
void arm_uart_send_data( void );
void arm_uart_interrupt_handler( void ) {
//clear interrupt flag
unsigned long ul = UARTIntStatus( UART0_BASE, true );
UARTIntClear( UART0_BASE, ul );
//receive chars if any
while ( UARTCharsAvail( UART0_BASE) ) arm_uart_receive_data();
//transmit characters if possible
while ( UARTSpaceAvail( UART0_BASE ) && !transmit_buffer_empty() ) arm_uart_send_data();
//if nothing to transmit, then switch off transmit interrupt, otherwise enable TX interrupt
if ( !UARTBusy( UART0_BASE ) && transmit_buffer_empty() ) {
UARTIntDisable( UART0_BASE, UART_INT_TX );
} else {
UARTIntEnable( UART0_BASE, UART_INT_TX );
}
}
#endif
void serial_init()
{
rx_buffer_head = rx_buffer_tail = 0;
tx_buffer_head = tx_buffer_tail = 0;
#ifdef PART_LM4F120H5QR
//code for ARM
SysCtlPeripheralEnable( SYSCTL_PERIPH_GPIOA ); //enable pins which correspond to RxD and TxD signals
SysCtlDelay( 26 ); // Delay 1usec for peripherial to start
GPIOPinConfigure( GPIO_PA0_U0RX ); //configure pin to be RxD of UART0
GPIOPinConfigure( GPIO_PA1_U0TX ); //configure pin to be TxD of UART0
GPIOPinTypeUART( GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1 ); //configure pins 0 and 1 of PORTA to be RxD and TxD
SysCtlPeripheralEnable( SYSCTL_PERIPH_UART0 ); // Enable the UART0 peripheral for use.
SysCtlDelay( 26 ); // Delay 1usec for peripherial to start
UARTConfigSetExpClk( UART0_BASE, SysCtlClockGet(), 115200, UART_CONFIG_WLEN_8 | UART_CONFIG_PAR_NONE | UART_CONFIG_STOP_ONE ); //115200 baud, 8-N-1
UARTFIFOLevelSet( UART0_BASE, UART_FIFO_TX1_8, UART_FIFO_RX1_8 ); //Interrupt if TX FIFO is almost empty or any character is received.
UARTIntDisable( UART0_BASE, 0xFFFFFFFF ); // Disable all interrupt sources for UART0 module
UARTIntEnable( UART0_BASE, UART_INT_RX | UART_INT_RT ); //Enable only receive interrupts
UARTIntRegister( UART0_BASE, arm_uart_interrupt_handler );
IntPrioritySet( INT_UART0, 64 ); // lowest priority for UART interrupts
IntEnable( INT_UART0 ); //Enable UART0 interrupts in the NVIC
UARTEnable( UART0_BASE ); //Enable UART0 to work
#else
//code for AVR
// Set baud rate
#if BAUD_RATE < 57600
uint16_t UBRR0_value = ((F_CPU / (8L * BAUD_RATE)) - 1)/2 ;
UCSR0A &= ~(1 << U2X0); // baud doubler off - Only needed on Uno XX
#else
uint16_t UBRR0_value = ((F_CPU / (4L * BAUD_RATE)) - 1)/2;
UCSR0A |= (1 << U2X0); // baud doubler on for high baud rates, i.e. 115200
#endif
UBRR0H = UBRR0_value >> 8;
UBRR0L = UBRR0_value;
// enable rx and tx
UCSR0B |= 1<<RXEN0;
UCSR0B |= 1<<TXEN0;
// enable interrupt on complete reception of a byte
UCSR0B |= 1<<RXCIE0;
// defaults to 8-bit, no parity, 1 stop bit
#endif //for ARM
}
void serial_write(uint8_t data) {
// Calculate next head
uint8_t next_head = tx_buffer_head + 1;
if (next_head == TX_BUFFER_SIZE) { next_head = 0; }
// Wait until there is space in the buffer
while (next_head == tx_buffer_tail) {
if (sys.execute & EXEC_RESET) { return; } // Only check for abort to avoid an endless loop.
}
// Store data and advance head
tx_buffer[tx_buffer_head] = data;
tx_buffer_head = next_head;
#ifdef PART_LM4F120H5QR // code for ARM
arm_uart_interrupt_handler();
#else // code for AVR
// Enable Data Register Empty Interrupt to make sure tx-streaming is running
UCSR0B |= (1 << UDRIE0);
#endif
}
// Data Register Empty Interrupt handler
#if defined( PART_LM4F120H5QR )
void arm_uart_send_data( void )
#elif defined( __AVR_ATmega644P__ )
ISR(USART0_UDRE_vect)
#else
ISR(USART_UDRE_vect)
#endif
{
if ( transmit_buffer_empty() ) {
#ifdef PART_LM4F120H5QR
UARTIntDisable( UART0_BASE, UART_INT_TX );
#else
// AVR code
// Turn off Data Register Empty Interrupt to stop tx-streaming if this concludes the transfer
if (tail == tx_buffer_head) { UCSR0B &= ~(1 << UDRIE0); }
#endif
return;
}
// Temporary tx_buffer_tail (to optimize for volatile)
uint8_t tail = tx_buffer_tail;
#ifdef ENABLE_XONXOFF
if (flow_ctrl == SEND_XOFF) {
UDR0 = XOFF_CHAR;
flow_ctrl = XOFF_SENT;
} else if (flow_ctrl == SEND_XON) {
UDR0 = XON_CHAR;
flow_ctrl = XON_SENT;
} else
#endif
{
// Send a byte from the buffer
#if defined( PART_LM4F120H5QR )
// ARM code
UARTCharPutNonBlocking( UART0_BASE, tx_buffer[ tail ] );
#else
// AVR code
UDR0 = tx_buffer[tail];
#endif
// Update tail position
tail++;
if (tail == TX_BUFFER_SIZE) { tail = 0; }
tx_buffer_tail = tail;
}
}
uint8_t serial_read()
{
if (rx_buffer_head == rx_buffer_tail) {
return SERIAL_NO_DATA;
} else {
uint8_t data = rx_buffer[rx_buffer_tail];
rx_buffer_tail++;
if (rx_buffer_tail == RX_BUFFER_SIZE) { rx_buffer_tail = 0; }
#ifdef ENABLE_XONXOFF
if ((get_rx_buffer_count() < RX_BUFFER_LOW) && flow_ctrl == XOFF_SENT) {
flow_ctrl = SEND_XON;
UCSR0B |= (1 << UDRIE0); // Force TX
}
#endif
return data;
}
}
// UART Receive Interrupt handler
#if defined( PART_LM4F120H5QR )
void arm_uart_receive_data( void )
#elif defined( __AVR_ATmega644P__ )
ISR(USART0_RX_vect)
#else
ISR(USART_RX_vect)
#endif
{
#if defined( PART_LM4F120H5QR ) // code for ARM
uint8_t data = (uint8_t)( UARTCharGetNonBlocking( UART0_BASE ) & 0xFF ); //read a char and remove control bits (highest)
serial_write( data ); //echo
#else // code for AVR
uint8_t data = UDR0;
#endif
uint8_t next_head;
// Pick off runtime command characters directly from the serial stream. These characters are
// not passed into the buffer, but these set system state flag bits for runtime execution.
switch (data) {
case CMD_STATUS_REPORT: sys.execute |= EXEC_STATUS_REPORT; break; // Set as true
case CMD_CYCLE_START: sys.execute |= EXEC_CYCLE_START; break; // Set as true
case CMD_FEED_HOLD: sys.execute |= EXEC_FEED_HOLD; break; // Set as true
case CMD_RESET: mc_reset(); break; // Call motion control reset routine.
default: // Write character to buffer
next_head = rx_buffer_head + 1;
if (next_head == RX_BUFFER_SIZE) { next_head = 0; }
// Write data to buffer unless it is full.
if (next_head != rx_buffer_tail) {
rx_buffer[rx_buffer_head] = data;
rx_buffer_head = next_head;
#ifdef ENABLE_XONXOFF
if ((get_rx_buffer_count() >= RX_BUFFER_FULL) && flow_ctrl == XON_SENT) {
flow_ctrl = SEND_XOFF;
UCSR0B |= (1 << UDRIE0); // Force TX
}
#endif
}
}
}
void serial_reset_read_buffer()
{
rx_buffer_tail = rx_buffer_head;
#ifdef ENABLE_XONXOFF
flow_ctrl = XON_SENT;
#endif
}