-
Notifications
You must be signed in to change notification settings - Fork 51
/
pico_cnc.c
450 lines (334 loc) · 12.6 KB
/
pico_cnc.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/*
pico_cnc.c - driver code for RP2040 ARM processors
Part of grblHAL
Copyright (c) 2021-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 <math.h>
#include <stdlib.h>
#include <string.h>
#include "driver.h"
#if defined(BOARD_PICO_CNC)
#include "hardware/pio.h"
#include "MCP3221.h"
#include "driverPIO.pio.h"
#include "grbl/protocol.h"
static output_sr_t *sr;
static bool state[AUX_N_OUT];
static volatile uint32_t event_bits;
static io_ports_data_t digital;
static input_signal_t *aux_in;
static output_signal_t *aux_out;
#if MCP3221_ENABLE
static xbar_t analog_in;
static uint_fast8_t analog_n_in;
static enumerate_pins_ptr on_enumerate_pins;
#endif
static bool digital_out_cfg (xbar_t *output, gpio_out_config_t *config, bool persistent)
{
if(output->id < digital.out.n_ports) {
if(config->inverted != aux_out[output->id].mode.inverted) {
aux_out[output->id].mode.inverted = config->inverted;
state[output->id] = !state[output->id];
out_sr16_write(pio1, 1, sr->value);
}
// Open drain not supported
if(persistent)
ioport_save_output_settings(output, config);
}
return aux_out->id < digital.out.n_ports;
}
static void digital_out (uint8_t port, bool on)
{
if(port < digital.out.n_ports) {
port = ioports_map(digital.out, port);
on = ((settings.ioport.invert_out.mask >> port) & 0x01) ? !on : on;
state[port] = on;
switch(port)
{
case 0:
sr->aux0_out = on;
break;
case 1:
sr->aux1_out = on;
break;
case 2:
sr->aux2_out = on;
break;
case 3:
sr->aux3_out = on;
break;
case 4:
sr->aux4_out = on;
break;
case 5:
sr->aux5_out = on;
break;
case 6:
sr->aux6_out = on;
break;
#ifdef AUXOUTPUT7_PIN // TX enable
case 7:
DIGITAL_OUT(1 << AUXOUTPUT7_PIN, on);
break;
#endif
default:
break;
}
out_sr16_write(pio1, 1, sr->value);
}
}
static float digital_out_state (xbar_t *output)
{
float value = -1.0f;
if(output->id < digital.out.n_ports)
value = (float)state[output->id];
return value;
}
static bool digital_in_cfg (xbar_t *input, gpio_in_config_t *config, bool persistent)
{
if(input->id < digital.in.n_ports && config->pull_mode != PullMode_UpDown) {
aux_in[input->id].mode.inverted = config->inverted;
aux_in[input->id].mode.debounce = config->debounce;
aux_in[input->id].mode.pull_mode = config->pull_mode;
gpio_set_inover(input->pin, config->inverted ? GPIO_OVERRIDE_INVERT : GPIO_OVERRIDE_NORMAL);
gpio_set_pulls(input->pin, config->pull_mode == PullMode_Up, config->pull_mode == PullMode_Down);
if(persistent)
ioport_save_input_settings(input, config);
}
return input->id < digital.in.n_ports;
}
static float digital_in_state (xbar_t *input)
{
float value = -1.0f;
if(input->id < digital.in.n_ports)
value = (float)(DIGITAL_IN(aux_in[input->id].bit));
return value;
}
inline static __attribute__((always_inline)) int32_t get_input (const input_signal_t *input, wait_mode_t wait_mode, float timeout)
{
if(wait_mode == WaitMode_Immediate)
return DIGITAL_IN(input->bit);
int32_t value = -1;
uint_fast16_t delay = (uint_fast16_t)ceilf((1000.0f / 50.0f) * timeout) + 1;
if(wait_mode == WaitMode_Rise || wait_mode == WaitMode_Fall) {
pin_irq_mode_t irq_mode = wait_mode == WaitMode_Rise ? IRQ_Mode_Rising : IRQ_Mode_Falling;
if(input->cap.irq_mode & irq_mode) {
event_bits &= ~input->bit;
pinEnableIRQ(input, irq_mode);
do {
if(event_bits & input->bit) {
value = DIGITAL_IN(input->bit);
break;
}
if(delay) {
protocol_execute_realtime();
hal.delay_ms(50, NULL);
} else
break;
} while(--delay && !sys.abort);
pinEnableIRQ(input, IRQ_Mode_None); // Restore pin interrupt status
}
} else {
bool wait_for = wait_mode != WaitMode_Low;
do {
if((DIGITAL_IN(input->bit)) == wait_for) {
value = DIGITAL_IN(input->bit);
break;
}
if(delay) {
protocol_execute_realtime();
hal.delay_ms(50, NULL);
} else
break;
} while(--delay && !sys.abort);
}
return value;
}
void ioports_event (input_signal_t *input)
{
event_bits |= input->bit;
if(input->interrupt_callback)
input->interrupt_callback(input->user_port, DIGITAL_IN(input->bit) ^ input->mode.inverted);
}
static int32_t wait_on_input (io_port_type_t type, uint8_t port, wait_mode_t wait_mode, float timeout)
{
int32_t value = -1;
if(type == Port_Digital && port < digital.in.n_ports) {
port = ioports_map(digital.in, port);
value = get_input(&aux_in[port], wait_mode, timeout);
}
#if MCP3221_ENABLE
else if(port < analog_n_in)
value = (int32_t)MCP3221_read();
#endif
// else if(port == 0)
// value = analogRead(41);
return value;
}
static bool register_interrupt_handler (uint8_t port, pin_irq_mode_t irq_mode, ioport_interrupt_callback_ptr interrupt_callback)
{
bool ok;
port = ioports_map(digital.in, port);
if((ok = port < digital.in.n_ports && aux_in[port].cap.irq_mode != IRQ_Mode_None)) {
input_signal_t *input = &aux_in[port];
if(irq_mode != IRQ_Mode_None && (ok = interrupt_callback != NULL)) {
input->mode.irq_mode = irq_mode;
input->interrupt_callback = interrupt_callback;
pinEnableIRQ(input, irq_mode);
}
if(irq_mode == IRQ_Mode_None || !ok) {
hal.irq_disable();
pinEnableIRQ(input, IRQ_Mode_None);
input->mode.irq_mode = IRQ_Mode_None;
input->interrupt_callback = NULL;
hal.irq_enable();
}
}
return ok;
}
static xbar_t *get_pin_info (io_port_type_t type, io_port_direction_t dir, uint8_t port)
{
static xbar_t pin;
xbar_t *info = NULL;
if(type == Port_Digital) {
if(dir == Port_Input && port < digital.in.n_ports) {
XBAR_SET_DIN_INFO(pin, ioports_map(digital.in, port), aux_in[pin.id], digital_in_cfg, digital_in_state);
info = &pin;
}
if(dir == Port_Output && port < digital.out.n_ports) {
XBAR_SET_DOUT_INFO(pin, ioports_map(digital.out, port), aux_out[pin.id], digital_out_cfg, digital_out_state);
info = &pin;
}
}
#if MCP3221_ENABLE
else if(dir == Port_Input && port == 0)
info = &analog_in;
#endif
return info;
}
static void set_pin_description (io_port_type_t type, io_port_direction_t dir, uint8_t port, const char *s)
{
if(type == Port_Digital) {
if(dir == Port_Input && port < digital.in.n_ports)
aux_in[ioports_map(digital.in, port)].description = s;
if(dir == Port_Output && port < digital.out.n_ports)
aux_out[ioports_map(digital.out, port)].description = s;
}
}
static bool claim (io_port_type_t type, io_port_direction_t dir, uint8_t *port, const char *description)
{
bool ok = false;
if(type == Port_Digital) {
if(dir == Port_Input) {
if((ok = digital.in.map && *port < digital.in.n_ports && !aux_in[*port].mode.claimed)) {
uint8_t i;
hal.port.num_digital_in--;
for(i = ioports_map_reverse(&digital.in, *port); i < hal.port.num_digital_in ; i++) {
digital.in.map[i] = digital.in.map[i + 1];
aux_in[digital.in.map[i]].user_port = i;
aux_in[digital.in.map[i]].description = iports_get_pnum(digital, i);
}
aux_in[*port].mode.claimed = On;
aux_in[*port].user_port = hal.port.num_digital_in;
aux_in[*port].description = description;
digital.in.map[hal.port.num_digital_in] = *port;
*port = hal.port.num_digital_in;
}
} else if((ok = digital.out.map && *port < digital.out.n_ports && !aux_out[*port].mode.claimed)) {
uint8_t i;
hal.port.num_digital_out--;
for(i = ioports_map_reverse(&digital.out, *port); i < hal.port.num_digital_out; i++) {
digital.out.map[i] = digital.out.map[i + 1];
aux_out[digital.out.map[i]].description = iports_get_pnum(digital, i);
}
aux_out[*port].mode.claimed = On;
aux_out[*port].description = description;
digital.out.map[hal.port.num_digital_out] = *port;
*port = hal.port.num_digital_out;
}
}
#if MCP3221_ENABLE
else if(dir == Port_Input && (ok = *port == 0 && analog_in.mode.analog && !analog_in.mode.claimed)) {
hal.port.num_analog_in--;
analog_in.mode.claimed = On;
analog_in.description = description;
}
#endif
return ok;
}
bool swap_pins (io_port_type_t type, io_port_direction_t dir, uint8_t port_a, uint8_t port_b)
{
bool ok = port_a == port_b;
if(!ok && type == Port_Digital) {
if((ok = dir == Port_Input && port_a < digital.in.n_ports && port_b < digital.in.n_ports &&
aux_in[port_a].interrupt_callback == NULL &&
aux_in[port_b].interrupt_callback == NULL)) {
input_signal_t tmp;
memcpy(&tmp, &aux_in[port_a], sizeof(input_signal_t));
memcpy(&aux_in[port_a], &aux_in[port_b], sizeof(input_signal_t));
aux_in[port_a].description = tmp.description;
tmp.description = aux_in[port_b].description;
memcpy(&aux_in[port_b], &tmp, sizeof(input_signal_t));
}
if((ok = dir == Port_Output && port_a < digital.out.n_ports && port_b < digital.out.n_ports)) {
output_signal_t tmp;
memcpy(&tmp, &aux_out[port_a], sizeof(output_signal_t));
memcpy(&aux_out[port_a], &aux_out[port_b], sizeof(output_signal_t));
aux_out[port_a].description = tmp.description;
tmp.description = aux_out[port_b].description;
memcpy(&aux_out[port_b], &tmp, sizeof(output_signal_t));
}
}
return ok;
}
#if MCP3221_ENABLE
static void enumerate_pins (bool low_level, pin_info_ptr pin_info, void *data)
{
on_enumerate_pins(low_level, pin_info, data);
pin_info(&analog_in, data);
}
#endif
void board_init (pin_group_pins_t *aux_inputs, pin_group_pins_t *aux_outputs, output_sr_t *reg)
{
aux_in = aux_inputs->pins.inputs;
aux_out = aux_outputs->pins.outputs;
hal.port.set_pin_description = set_pin_description;
if(ioports_add(&digital, Port_Digital, aux_inputs->n_pins, aux_outputs->n_pins)) {
sr = reg;
hal.port.claim = claim;
hal.port.swap_pins = swap_pins;
hal.port.get_pin_info = get_pin_info;
if(digital.in.n_ports) {
hal.port.wait_on_input = wait_on_input;
hal.port.register_interrupt_handler = register_interrupt_handler;
}
if(digital.out.n_ports)
hal.port.digital_out = digital_out;
ioports_add_settings(NULL, NULL);
}
#if MCP3221_ENABLE
analog_in.function = Input_Analog_Aux0;
analog_in.group = PinGroup_AuxInput;
analog_in.pin = 0;
analog_in.port = "MCP3221:";
if(MCP3221_init()) {
analog_in.mode.analog = On;
hal.port.num_analog_in = analog_n_in = 1;
hal.port.wait_on_input = wait_on_input;
};
analog_in.description = analog_in.mode.analog ? "E0" : "No power";
on_enumerate_pins = hal.enumerate_pins;
hal.enumerate_pins = enumerate_pins;
#endif
}
#endif