gnea\grbl-Mega  1.0f
Source Code Documentation ( Internal Workings )
serial.c
Go to the documentation of this file.
1 /*
2  serial.c - Low level functions for sending and recieving bytes via the serial port
3  Part of Grbl
4 
5  Copyright (c) 2011-2016 Sungeun K. Jeon for Gnea Research LLC
6  Copyright (c) 2009-2011 Simen Svale Skogsrud
7 
8  Grbl is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  Grbl is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with Grbl. If not, see <http://www.gnu.org/licenses/>.
20 */
21 
22 #include "grbl.h"
23 
24 #define RX_RING_BUFFER (RX_BUFFER_SIZE+1)
25 #define TX_RING_BUFFER (TX_BUFFER_SIZE+1)
26 
29 volatile uint8_t serial_rx_buffer_tail = 0;
30 
33 volatile uint8_t serial_tx_buffer_tail = 0;
34 
37 {
38  uint8_t rtail = serial_rx_buffer_tail;
39  if (serial_rx_buffer_head >= rtail) { return(RX_BUFFER_SIZE - (serial_rx_buffer_head-rtail)); }
40  return((rtail-serial_rx_buffer_head-1));
41 }
42 
44 //
47 {
48  uint8_t rtail = serial_rx_buffer_tail;
49  if (serial_rx_buffer_head >= rtail) { return(serial_rx_buffer_head-rtail); }
50  return (RX_BUFFER_SIZE - (rtail-serial_rx_buffer_head));
51 }
52 
54 //
57 {
58  uint8_t ttail = serial_tx_buffer_tail;
59  if (serial_tx_buffer_head >= ttail) { return(serial_tx_buffer_head-ttail); }
60  return (TX_RING_BUFFER - (ttail-serial_tx_buffer_head));
61 }
62 
63 
65 {
66 // Set baud rate
67  #if BAUD_RATE < 57600
68  uint16_t UBRR0_value = ((F_CPU / (8L * BAUD_RATE)) - 1)/2 ;
69  UCSR0A &= ~(1 << U2X0);
70  #else
71  uint16_t UBRR0_value = ((F_CPU / (4L * BAUD_RATE)) - 1)/2;
72  UCSR0A |= (1 << U2X0);
73  #endif
74  UBRR0H = UBRR0_value >> 8;
75  UBRR0L = UBRR0_value;
76 
77 // enable rx, tx, and interrupt on complete reception of a byte
78  UCSR0B |= (1<<RXEN0 | 1<<TXEN0 | 1<<RXCIE0);
79 
80 // defaults to 8-bit, no parity, 1 stop bit
81 }
82 
84 void serial_write(uint8_t data) {
85 // Calculate next head
86  uint8_t next_head = serial_tx_buffer_head + 1;
87  if (next_head == TX_RING_BUFFER) { next_head = 0; }
88 
89 // Wait until there is space in the buffer
90  while (next_head == serial_tx_buffer_tail) {
92  if (sys_rt_exec_state & EXEC_RESET) { return; }
93  }
94 
95 // Store data and advance head
97  serial_tx_buffer_head = next_head;
98 
99 // Enable Data Register Empty Interrupt to make sure tx-streaming is running
100  UCSR0B |= (1 << UDRIE0);
101 }
102 
104 ISR(SERIAL_UDRE)
105 {
106  uint8_t tail = serial_tx_buffer_tail;
107 
108 // Send a byte from the buffer
109  UDR0 = serial_tx_buffer[tail];
110 
111 // Update tail position
112  tail++;
113  if (tail == TX_RING_BUFFER) { tail = 0; }
114 
115  serial_tx_buffer_tail = tail;
116 
117 // Turn off Data Register Empty Interrupt to stop tx-streaming if this concludes the transfer
118  if (tail == serial_tx_buffer_head) { UCSR0B &= ~(1 << UDRIE0); }
119 }
120 
122 uint8_t serial_read()
123 {
124  uint8_t tail = serial_rx_buffer_tail;
125  if (serial_rx_buffer_head == tail) {
126  return SERIAL_NO_DATA;
127  } else {
128  uint8_t data = serial_rx_buffer[tail];
129 
130  tail++;
131  if (tail == RX_RING_BUFFER) { tail = 0; }
132  serial_rx_buffer_tail = tail;
133 
134  return data;
135  }
136 }
137 
138 
139 ISR(SERIAL_RX)
140 {
141  uint8_t data = UDR0;
142  uint8_t next_head;
143 
144 // Pick off realtime command characters directly from the serial stream. These characters are
145 // not passed into the main buffer, but these set system state flag bits for realtime execution.
146  switch (data) {
147  case CMD_RESET: mc_reset(); break;
151  default :
152  if (data > 0x7F) {
153  switch(data) {
155  case CMD_JOG_CANCEL:
156  if (sys.state & STATE_JOG) {
158  }
159  break;
160  #ifdef DEBUG
161  case CMD_DEBUG_REPORT: {uint8_t sreg = SREG; cli(); bit_true(sys_rt_exec_debug,EXEC_DEBUG_REPORT); SREG = sreg;} break;
162  #endif
179  }
180 // Throw away any unfound extended-ASCII character by not passing it to the serial buffer.
181  } else {
182  next_head = serial_rx_buffer_head + 1;
183  if (next_head == RX_RING_BUFFER) { next_head = 0; }
184 
185 // Write data to buffer unless it is full.
186  if (next_head != serial_rx_buffer_tail) {
188  serial_rx_buffer_head = next_head;
189  }
190  }
191  }
192 }
193 
194 
196 {
198 }
uint8_t serial_tx_buffer_head
Definition: serial.c:32
#define CMD_SPINDLE_OVR_COARSE_MINUS
Definition: config.h:75
#define CMD_DEBUG_REPORT
Only when DEBUG enabled, sends debug report in '{}' braces.
Definition: config.h:63
#define EXEC_RAPID_OVR_RESET
Definition: system.h:56
uint8_t serial_get_tx_buffer_count()
Returns the number of bytes used in the TX serial buffer.
Definition: serial.c:56
#define EXEC_FEED_OVR_COARSE_PLUS
Definition: system.h:52
uint8_t serial_read()
Fetches the first byte in the serial read buffer. Called by main program.
Definition: serial.c:122
#define EXEC_RAPID_OVR_MEDIUM
Definition: system.h:57
#define CMD_RESET
Define realtime command special characters. These characters are 'picked-off' directly from the...
Definition: config.h:48
#define bit_true(x, mask)
Definition: nuts_bolts.h:58
void system_set_exec_state_flag(uint8_t mask)
Special handlers for setting and clearing Grbl's real-time execution flags.
Definition: system.c:340
#define CMD_FEED_OVR_FINE_MINUS
Definition: config.h:68
void serial_reset_read_buffer()
Reset and empty data in read buffer. Used by e-stop and reset.
Definition: serial.c:195
#define CMD_FEED_OVR_FINE_PLUS
Definition: config.h:67
uint8_t state
Tracks the current system state of Grbl.
Definition: system.h:112
void system_set_exec_motion_override_flag(uint8_t mask)
Definition: system.c:368
#define EXEC_RESET
bitmask 00010000
Definition: system.h:35
#define TX_RING_BUFFER
Definition: serial.c:25
#define CMD_SPINDLE_OVR_COARSE_PLUS
Definition: config.h:74
#define CMD_FEED_HOLD
Definition: config.h:51
#define CMD_FEED_OVR_RESET
Restores feed override value to 100%.
Definition: config.h:64
#define RX_RING_BUFFER
Definition: serial.c:24
#define CMD_SPINDLE_OVR_FINE_PLUS
Definition: config.h:76
#define EXEC_FEED_OVR_COARSE_MINUS
Definition: system.h:53
#define EXEC_SPINDLE_OVR_STOP
Definition: system.h:66
#define EXEC_SAFETY_DOOR
bitmask 00100000
Definition: system.h:36
#define CMD_FEED_OVR_COARSE_PLUS
Definition: config.h:65
#define CMD_SAFETY_DOOR
NOTE: All override realtime commands must be in the extended ASCII character set, starting...
Definition: config.h:61
void serial_write(uint8_t data)
Writes one byte to the TX serial buffer. Called by main program.
Definition: serial.c:84
#define CMD_SPINDLE_OVR_FINE_MINUS
Definition: config.h:77
#define CMD_CYCLE_START
Definition: config.h:50
#define RX_BUFFER_SIZE
Definition: serial.h:27
volatile uint8_t serial_tx_buffer_tail
Definition: serial.c:33
#define EXEC_CYCLE_START
bitmask 00000010
Definition: system.h:32
system_t sys
Declare system global variable structure.
Definition: main.c:25
#define EXEC_COOLANT_MIST_OVR_TOGGLE
Definition: system.h:68
#define EXEC_SPINDLE_OVR_FINE_PLUS
Definition: system.h:64
#define EXEC_SPINDLE_OVR_RESET
Definition: system.h:61
#define EXEC_FEED_OVR_FINE_PLUS
Definition: system.h:54
#define CMD_SPINDLE_OVR_RESET
Restores spindle override value to 100%.
Definition: config.h:73
#define EXEC_COOLANT_FLOOD_OVR_TOGGLE
Definition: system.h:67
#define EXEC_MOTION_CANCEL
bitmask 01000000
Definition: system.h:37
uint8_t serial_get_rx_buffer_count()
Returns the number of bytes used in the RX serial buffer.
Definition: serial.c:46
#define EXEC_SPINDLE_OVR_COARSE_MINUS
Definition: system.h:63
volatile uint8_t sys_rt_exec_state
Global realtime executor bitflag variable for state management. See EXEC bitmasks.
Definition: system.h:134
void serial_init()
Definition: serial.c:64
volatile uint8_t serial_rx_buffer_tail
Definition: serial.c:29
#define EXEC_FEED_HOLD
bitmask 00001000
Definition: system.h:34
uint8_t serial_get_rx_buffer_available()
Returns the number of bytes available in the RX serial buffer.
Definition: serial.c:36
#define CMD_RAPID_OVR_MEDIUM
Definition: config.h:70
void system_set_exec_accessory_override_flag(uint8_t mask)
Definition: system.c:375
#define EXEC_SPINDLE_OVR_FINE_MINUS
Definition: system.h:65
#define CMD_JOG_CANCEL
Definition: config.h:62
uint8_t serial_tx_buffer[TX_RING_BUFFER]
Definition: serial.c:31
#define EXEC_RAPID_OVR_LOW
Definition: system.h:58
#define CMD_STATUS_REPORT
Definition: config.h:49
#define CMD_COOLANT_FLOOD_OVR_TOGGLE
Definition: config.h:79
#define CMD_RAPID_OVR_LOW
Definition: config.h:71
#define CMD_RAPID_OVR_RESET
Restores rapid override value to 100%.
Definition: config.h:69
#define STATE_JOG
Jogging mode.
Definition: system.h:78
#define SERIAL_NO_DATA
Definition: serial.h:33
void mc_reset()
Method to ready the system to reset by setting the realtime reset command and killing any...
#define CMD_COOLANT_MIST_OVR_TOGGLE
Definition: config.h:80
#define CMD_SPINDLE_OVR_STOP
Definition: config.h:78
#define CMD_FEED_OVR_COARSE_MINUS
Definition: config.h:66
#define EXEC_STATUS_REPORT
Define system executor bit map. Used internally by realtime protocol as realtime command flags...
Definition: system.h:31
#define EXEC_FEED_OVR_RESET
Override bit maps. Realtime bitflags to control feed, rapid, spindle, and coolant overrides...
Definition: system.h:51
uint8_t serial_rx_buffer[RX_RING_BUFFER]
Definition: serial.c:27
#define EXEC_FEED_OVR_FINE_MINUS
Definition: system.h:55
#define BAUD_RATE
Serial baud rate.
Definition: config.h:39
ISR(SERIAL_UDRE)
Data Register Empty Interrupt handler.
Definition: serial.c:104
uint8_t serial_rx_buffer_head
Definition: serial.c:28
#define EXEC_SPINDLE_OVR_COARSE_PLUS
Definition: system.h:62