-
Notifications
You must be signed in to change notification settings - Fork 76
/
serial.c
78 lines (62 loc) · 1.99 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
/*
serial.c - replacement for the modul of the same name in grbl
Make sure the simulator reads from stdin and writes to stdout.
Also print info about the last buffered block.
Part of Grbl Simulator
Copyright (c) 2012 Jens Geisler
Copyright (c) 2014 Adam Shelly
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/>.
*/
#include <stdio.h>
#include "simulator.h"
#include "avr/io.h"
#include "avr/interrupt.h"
#include "../serial.h"
//prototypes for overridden functions
uint8_t orig_serial_read();
//used to inject a sleep in grbl main loop,
// ensures hardware simulator gets some cycles in "parallel"
uint8_t serial_read() {
platform_sleep(0);
return orig_serial_read();
}
void simulate_write_interrupt(){
while (UCSR0B & (1<<UDRIE0)){
interrupt_SERIAL_UDRE();
sim_serial_out(UDR0);
}
}
void simulate_read_interrupt(){
uint8_t char_in = platform_poll_stdin();
if (char_in) {
UDR0 = char_in;
//EOF or CTRL-F to exit
if (UDR0 == EOF || UDR0 == 0xFF || UDR0 == 0x06 ) {
sim.exit = exit_REQ;
}
//debugging
if (UDR0 == '%') {
printf("%ld %f\n",sim.masterclock,(double)sim.sim_time);
}
interrupt_SERIAL_RX();
}
}
extern volatile uint8_t serial_rx_buffer_head;
extern volatile uint8_t serial_rx_buffer_tail;
void simulate_serial(){
simulate_write_interrupt();
uint8_t head = serial_rx_buffer_head+1;
if (head==RX_BUFFER_SIZE) { head = 0; }
if (head!=serial_rx_buffer_tail) {
simulate_read_interrupt();
}
}