-
Notifications
You must be signed in to change notification settings - Fork 87
/
stream_passthru.c
206 lines (155 loc) · 5.23 KB
/
stream_passthru.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
/*
stream_passthru.c - stream redirector for programming coprocessor
Part of grblHAL
Copyright (c) 2024 Terje Io
grblHAL 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.
grblHAL 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 grblHAL. If not, see <http://www.gnu.org/licenses/>.
*/
#include "hal.h"
#include "task.h"
#include "protocol.h"
//static xbar_t *dtr, *rts;
static uint8_t boot0_port = 0xFF, reset_port = 0xFF;
static io_stream_t dest;
static bool conn_ok = false;
static on_linestate_changed_ptr on_linestate_changed;
//static on_execute_realtime_ptr on_execute_realtime = NULL;
// Weak implementation of low level function to be provided by the driver
__attribute__((weak)) void stream_passthru_enter (void)
{
// The driver implementation calls stream_passthru_init() with start parameter set true after setting up for execution.
}
// ****
/*
static void onExecuteRealtime (uint_fast16_t state)
{
static bool lock = false;
int16_t c;
if(!lock) {
lock = true;
if((c = hal.stream.read()) != SERIAL_NO_DATA)
dest.write_char(c);
lock = false;
}
on_execute_realtime(state);
}
*/
ISR_CODE static bool ISR_FUNC(forward_usb_rx)(char c)
{
dest.write_char(c);
return true;
}
ISR_CODE static bool ISR_FUNC(sink_uart_rx)(char c)
{
return true;
}
static void forward_uart_rx (void *data)
{
static char buf[64];
static uint_fast8_t idx = 0;
int16_t c;
while((c = dest.read()) != SERIAL_NO_DATA) {
idx = 1;
*buf = c;
while(idx < sizeof(buf) && (c = dest.read()) != SERIAL_NO_DATA)
buf[idx++] = c;
hal.stream.write_n(buf, idx);
}
task_add_delayed(forward_uart_rx, NULL, 8);
}
static void onLinestateChanged (serial_linestate_t state)
{
/*
Auto program
DTR RTS-->EN IO0
1 1 1 1
0 0 1 1
1 0 0 1
0 1 1 0
*/
if(conn_ok) {
if(state.dtr == state.rts) {
hal.port.digital_out(boot0_port, 0);
hal.port.digital_out(reset_port, 1);
} else if(state.dtr) {
hal.port.digital_out(reset_port, 0);
hal.port.digital_out(boot0_port, 0);
} else {
hal.port.digital_out(boot0_port, 0);
hal.port.digital_out(reset_port, 1);
}
}
}
static void passthru_start2 (void *data)
{
conn_ok = true;
hal.port.digital_out(reset_port, 1);
task_add_delayed(forward_uart_rx, NULL, 8);
// on_execute_realtime = grbl.on_execute_realtime;
// grbl.on_execute_realtime = onExecuteRealtime;
dest.set_enqueue_rt_handler(stream_buffer_all);
dest.cancel_read_buffer();
}
static void passthru_start1 (void *data)
{
hal.port.digital_out(boot0_port, 0);
hal.port.digital_out(reset_port, 0);
on_linestate_changed = hal.stream.on_linestate_changed;
hal.stream.on_linestate_changed = onLinestateChanged;
task_add_delayed(passthru_start2, NULL, 1250); // delay a bit to allow the USB stack to start
}
static bool get_ports (xbar_t *properties, uint8_t port, void *data)
{
switch(properties->function) {
case Output_CoProc_Reset:
reset_port = port;
break;
case Output_CoProc_Boot0:
boot0_port = port;
break;
default:
break;
}
return boot0_port != 0xFF && reset_port != 0xFF;
}
static status_code_t passthru_enter (sys_state_t state, char *args)
{
report_message("Entering passthru mode", Message_Warning);
hal.delay_ms(100, NULL);
stream_passthru_enter();
return Status_OK;
}
void stream_passthru_init (uint8_t instance, uint32_t baud_rate, bool start)
{
static const sys_command_t passthru_command_list[] = {
{"PTRGH", passthru_enter, { .noargs = On }, { .str = "enter passthru mode" } }
};
static sys_commands_t passthru_commands = {
.n_commands = sizeof(passthru_command_list) / sizeof(sys_command_t),
.commands = passthru_command_list
};
if(hal.stream.type == StreamType_Serial &&
hal.stream.state.is_usb &&
hal.stream.state.linestate_event &&
ioports_enumerate(Port_Digital, Port_Output, (pin_cap_t){ .output = On }, get_ports, NULL)) {
if(start) {
io_stream_t const *stream = stream_open_instance(instance, baud_rate, NULL, "Passthru");
if(stream != NULL) {
protocol_enqueue_foreground_task(passthru_start1, NULL); // enter passthrouh mode after finished booting grblHAL
memcpy(&dest, stream, sizeof(io_stream_t));
dest.set_enqueue_rt_handler(sink_uart_rx);
hal.stream.set_enqueue_rt_handler(forward_usb_rx);
} else
report_message("Entering passthru mode failed!", Message_Warning);
} else
system_register_commands(&passthru_commands);
}
}