LAT Hologramm-Software 2.0
Loading...
Searching...
No Matches
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
29volatile uint8_t serial_rx_buffer_tail = 0;
30
33volatile uint8_t serial_tx_buffer_tail = 0;
34
35
36// Returns the number of bytes available in the RX serial buffer.
38{
39 uint8_t rtail = serial_rx_buffer_tail; // Copy to limit multiple calls to volatile
40 if (serial_rx_buffer_head >= rtail) { return(RX_BUFFER_SIZE - (serial_rx_buffer_head-rtail)); }
41 return((rtail-serial_rx_buffer_head-1));
42}
43
44
45// Returns the number of bytes used in the RX serial buffer.
46// NOTE: Deprecated. Not used unless classic status reports are enabled in config.h.
48{
49 uint8_t rtail = serial_rx_buffer_tail; // Copy to limit multiple calls to volatile
50 if (serial_rx_buffer_head >= rtail) { return(serial_rx_buffer_head-rtail); }
51 return (RX_BUFFER_SIZE - (rtail-serial_rx_buffer_head));
52}
53
54
55// Returns the number of bytes used in the TX serial buffer.
56// NOTE: Not used except for debugging and ensuring no TX bottlenecks.
58{
59 uint8_t ttail = serial_tx_buffer_tail; // Copy to limit multiple calls to volatile
60 if (serial_tx_buffer_head >= ttail) { return(serial_tx_buffer_head-ttail); }
61 return (TX_RING_BUFFER - (ttail-serial_tx_buffer_head));
62}
63
64
66{
67 // Set baud rate
68 #if BAUD_RATE < 57600
69 uint16_t UBRR0_value = ((F_CPU / (8L * BAUD_RATE)) - 1)/2 ;
70 UCSR0A &= ~(1 << U2X0); // baud doubler off - Only needed on Uno XXX
71 #else
72 uint16_t UBRR0_value = ((F_CPU / (4L * BAUD_RATE)) - 1)/2;
73 UCSR0A |= (1 << U2X0); // baud doubler on for high baud rates, i.e. 115200
74 #endif
75 UBRR0H = UBRR0_value >> 8;
76 UBRR0L = UBRR0_value;
77
78 // enable rx, tx, and interrupt on complete reception of a byte
79 UCSR0B |= (1<<RXEN0 | 1<<TXEN0 | 1<<RXCIE0);
80
81 // defaults to 8-bit, no parity, 1 stop bit
82}
83
84
85// Writes one byte to the TX serial buffer. Called by main program.
86void serial_write(uint8_t data) {
87 // Calculate next head
88 uint8_t next_head = serial_tx_buffer_head + 1;
89 if (next_head == TX_RING_BUFFER) { next_head = 0; }
90
91 // Wait until there is space in the buffer
92 while (next_head == serial_tx_buffer_tail) {
93 // TODO: Restructure st_prep_buffer() calls to be executed here during a long print.
94 if (sys_rt_exec_state & EXEC_RESET) { return; } // Only check for abort to avoid an endless loop.
95 }
96
97 // Store data and advance head
99 serial_tx_buffer_head = next_head;
100
101 // Enable Data Register Empty Interrupt to make sure tx-streaming is running
102 UCSR0B |= (1 << UDRIE0);
103}
104
105
106// Data Register Empty Interrupt handler
107ISR(SERIAL_UDRE)
108{
109 uint8_t tail = serial_tx_buffer_tail; // Temporary serial_tx_buffer_tail (to optimize for volatile)
110
111 // Send a byte from the buffer
112 UDR0 = serial_tx_buffer[tail];
113
114 // Update tail position
115 tail++;
116 if (tail == TX_RING_BUFFER) { tail = 0; }
117
119
120 // Turn off Data Register Empty Interrupt to stop tx-streaming if this concludes the transfer
121 if (tail == serial_tx_buffer_head) { UCSR0B &= ~(1 << UDRIE0); }
122}
123
124
125// Fetches the first byte in the serial read buffer. Called by main program.
126uint8_t serial_read()
127{
128 uint8_t tail = serial_rx_buffer_tail; // Temporary serial_rx_buffer_tail (to optimize for volatile)
129 if (serial_rx_buffer_head == tail) {
130 return SERIAL_NO_DATA;
131 } else {
132 uint8_t data = serial_rx_buffer[tail];
133
134 tail++;
135 if (tail == RX_RING_BUFFER) { tail = 0; }
137
138 return data;
139 }
140}
141
142
143ISR(SERIAL_RX)
144{
145 uint8_t data = UDR0;
146 uint8_t next_head;
147
148 // Pick off realtime command characters directly from the serial stream. These characters are
149 // not passed into the main buffer, but these set system state flag bits for realtime execution.
150 switch (data) {
151 case CMD_RESET: mc_reset(); break; // Call motion control reset routine.
154 case CMD_FEED_HOLD: system_set_exec_state_flag(EXEC_FEED_HOLD); break; // Set as true
155 default :
156 if (data > 0x7F) { // Real-time control characters are extended ACSII only.
157 switch(data) {
159 case CMD_JOG_CANCEL:
160 if (sys.state & STATE_JOG) { // Block all other states from invoking motion cancel.
162 }
163 break;
164 #ifdef DEBUG
165 case CMD_DEBUG_REPORT: {uint8_t sreg = SREG; cli(); bit_true(sys_rt_exec_debug,EXEC_DEBUG_REPORT); SREG = sreg;} break;
166 #endif
182 #ifdef ENABLE_M7
184 #endif
185 }
186 // Throw away any unfound extended-ASCII character by not passing it to the serial buffer.
187 } else { // Write character to buffer
188 next_head = serial_rx_buffer_head + 1;
189 if (next_head == RX_RING_BUFFER) { next_head = 0; }
190
191 // Write data to buffer unless it is full.
192 if (next_head != serial_rx_buffer_tail) {
194 serial_rx_buffer_head = next_head;
195 }
196 }
197 }
198}
199
200
202{
204}
#define CMD_FEED_HOLD
Definition: config.h:54
#define CMD_SPINDLE_OVR_COARSE_MINUS
Definition: config.h:78
#define CMD_SPINDLE_OVR_RESET
Definition: config.h:76
#define CMD_DEBUG_REPORT
Definition: config.h:66
#define CMD_CYCLE_START
Definition: config.h:53
#define CMD_STATUS_REPORT
Definition: config.h:52
#define CMD_RAPID_OVR_RESET
Definition: config.h:72
#define CMD_SPINDLE_OVR_FINE_MINUS
Definition: config.h:80
#define CMD_SPINDLE_OVR_STOP
Definition: config.h:81
#define CMD_FEED_OVR_COARSE_MINUS
Definition: config.h:69
#define CMD_JOG_CANCEL
Definition: config.h:65
#define CMD_FEED_OVR_FINE_MINUS
Definition: config.h:71
#define CMD_COOLANT_FLOOD_OVR_TOGGLE
Definition: config.h:82
#define CMD_SPINDLE_OVR_COARSE_PLUS
Definition: config.h:77
#define CMD_RAPID_OVR_MEDIUM
Definition: config.h:73
#define CMD_COOLANT_MIST_OVR_TOGGLE
Definition: config.h:83
#define CMD_FEED_OVR_FINE_PLUS
Definition: config.h:70
#define CMD_FEED_OVR_COARSE_PLUS
Definition: config.h:68
#define CMD_SAFETY_DOOR
Definition: config.h:64
#define CMD_SPINDLE_OVR_FINE_PLUS
Definition: config.h:79
#define BAUD_RATE
Definition: config.h:42
#define CMD_FEED_OVR_RESET
Definition: config.h:67
#define CMD_RAPID_OVR_LOW
Definition: config.h:74
#define CMD_RESET
Definition: config.h:51
system_t sys
Definition: main.c:26
volatile uint8_t sys_rt_exec_state
Definition: main.c:30
void mc_reset()
#define bit_true(x, mask)
Definition: nuts_bolts.h:62
#define TX_RING_BUFFER
Definition: serial.c:25
uint8_t serial_rx_buffer[RX_RING_BUFFER]
Definition: serial.c:27
uint8_t serial_tx_buffer_head
Definition: serial.c:32
uint8_t serial_get_rx_buffer_available()
Definition: serial.c:37
void serial_reset_read_buffer()
Definition: serial.c:201
uint8_t serial_rx_buffer_head
Definition: serial.c:28
#define RX_RING_BUFFER
Definition: serial.c:24
volatile uint8_t serial_tx_buffer_tail
Definition: serial.c:33
void serial_write(uint8_t data)
Definition: serial.c:86
volatile uint8_t serial_rx_buffer_tail
Definition: serial.c:29
uint8_t serial_get_tx_buffer_count()
Definition: serial.c:57
void serial_init()
Definition: serial.c:65
uint8_t serial_read()
Definition: serial.c:126
uint8_t serial_get_rx_buffer_count()
Definition: serial.c:47
uint8_t serial_tx_buffer[TX_RING_BUFFER]
Definition: serial.c:31
ISR(SERIAL_UDRE)
Definition: serial.c:107
#define RX_BUFFER_SIZE
Definition: serial.h:27
#define SERIAL_NO_DATA
Definition: serial.h:37
uint8_t state
Definition: system.h:128
void system_set_exec_state_flag(uint8_t mask)
Definition: system.c:356
void system_set_exec_accessory_override_flag(uint8_t mask)
Definition: system.c:391
void system_set_exec_motion_override_flag(uint8_t mask)
Definition: system.c:384
#define EXEC_FEED_OVR_RESET
Definition: system.h:54
#define EXEC_FEED_OVR_COARSE_MINUS
Definition: system.h:56
#define EXEC_SPINDLE_OVR_FINE_MINUS
Definition: system.h:68
#define EXEC_SPINDLE_OVR_COARSE_MINUS
Definition: system.h:66
#define EXEC_FEED_OVR_FINE_PLUS
Definition: system.h:57
#define EXEC_RAPID_OVR_LOW
Definition: system.h:61
#define EXEC_FEED_OVR_COARSE_PLUS
Definition: system.h:55
#define EXEC_SPINDLE_OVR_COARSE_PLUS
Definition: system.h:65
#define EXEC_STATUS_REPORT
Definition: system.h:31
#define EXEC_COOLANT_FLOOD_OVR_TOGGLE
Definition: system.h:70
#define EXEC_SPINDLE_OVR_STOP
Definition: system.h:69
#define STATE_JOG
Definition: system.h:82
#define EXEC_RESET
Definition: system.h:35
#define EXEC_RAPID_OVR_MEDIUM
Definition: system.h:60
#define EXEC_FEED_HOLD
Definition: system.h:34
#define EXEC_MOTION_CANCEL
Definition: system.h:37
#define EXEC_COOLANT_MIST_OVR_TOGGLE
Definition: system.h:71
#define EXEC_SPINDLE_OVR_FINE_PLUS
Definition: system.h:67
#define EXEC_SAFETY_DOOR
Definition: system.h:36
#define EXEC_SPINDLE_OVR_RESET
Definition: system.h:64
#define EXEC_RAPID_OVR_RESET
Definition: system.h:59
#define EXEC_CYCLE_START
Definition: system.h:32
#define EXEC_FEED_OVR_FINE_MINUS
Definition: system.h:58