grblHAL core  20260225
pin_bits_masks.h
Go to the documentation of this file.
1 /*
2  pin_bits_masks.h - for adding bit definitions and masks
3 
4  NOTE: This file is not used by the core, it may be used by drivers
5 
6  Part of grblHAL
7 
8  Copyright (c) 2021-2026 Terje Io
9 
10  grblHAL is free software: you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14 
15  grblHAL is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with grblHAL. If not, see <http://www.gnu.org/licenses/>.
22 */
23 
24 #include "platform.h"
25 
26 // Sanity checks
27 
28 #if PROBE_ENABLE && !defined(PROBE_PIN)
29 #error "Probe input is not supported in this configuration!"
30 #endif
31 
32 #if SAFETY_DOOR_ENABLE && !defined(SAFETY_DOOR_PIN)
33 #error "Safety door input is not supported in this configuration!"
34 #endif
35 
36 #if MOTOR_FAULT_ENABLE && !defined(MOTOR_FAULT_PIN)
37 #error "Motor fault input is not supported in this configuration!"
38 #endif
39 
40 #if MOTOR_WARNING_ENABLE && !defined(MOTOR_WARNING_PIN)
41 #error "Motor warning input is not supported in this configuration!"
42 #endif
43 
44 #if I2C_STROBE_ENABLE && !defined(I2C_STROBE_PIN)
45 #error "I2C keypad/strobe is not supported in this configuration!"
46 #endif
47 
48 #if MPG_ENABLE == 1 && !defined(MPG_MODE_PIN)
49 #error "MPG mode input is not supported in this configuration!"
50 #endif
51 
52 #if (ENCODER_ENABLE & 1) && !defined(QEI_SELECT_PIN)
53 #error "Encoder select input is not supported in this configuration!"
54 #endif
55 
56 #define EXPANDER_PORT 1
57 
58 // Control input signals
59 
60 // Define the CONTROL_PORT symbol as a shorthand in the *_map.h file if all control inputs share the same port.
61 #ifdef CONTROL_PORT
62 
63 #ifndef RESET_PORT
64 #define RESET_PORT CONTROL_PORT
65 #endif
66 #ifndef FEED_HOLD_PORT
67 #define FEED_HOLD_PORT CONTROL_PORT
68 #endif
69 #ifndef CYCLE_START_PORT
70 #define CYCLE_START_PORT CONTROL_PORT
71 #endif
72 #ifndef ESTOP_PORT
73 #define ESTOP_PORT CONTROL_PORT
74 #endif
75 #ifndef PROBE_DISCONNECT_PORT
76 #define PROBE_DISCONNECT_PORT CONTROL_PORT
77 #endif
78 #ifndef STOP_DISABLE_PORT
79 #define STOP_DISABLE_PORT CONTROL_PORT
80 #endif
81 #ifndef BLOCK_DELETE_PORT
82 #define BLOCK_DELETE_PORT CONTROL_PORT
83 #endif
84 #ifndef SINGLE_BLOCK_PORT
85 #define SINGLE_BLOCK_PORT CONTROL_PORT
86 #endif
87 #ifndef MOTOR_FAULT_PORT
88 #define MOTOR_FAULT_PORT CONTROL_PORT
89 #endif
90 #ifndef MOTOR_WARNING_PORT
91 #define MOTOR_WARNING_PORT CONTROL_PORT
92 #endif
93 #ifndef LIMITS_OVERRIDE_PORT
94 #define LIMITS_OVERRIDE_PORT CONTROL_PORT
95 #endif
96 #if SAFETY_DOOR_ENABLE && !defined(SAFETY_DOOR_PORT)
97 #define SAFETY_DOOR_PORT CONTROL_PORT
98 #endif
99 
100 #endif // CONTROL_PORT
101 
102 #ifndef SD_DETECT_BIT
103 #ifdef SD_DETECT_PIN
104 #define SD_DETECT_BIT (1<<SD_DETECT_PIN)
105 #else
106 #define SD_DETECT_BIT 0
107 #endif
108 #endif
109 
110 #ifndef CONTROL_ENABLE
111 #define CONTROL_ENABLE 0
112 #endif
113 
114 #define a_cap(pin) .cap.pin
115 
116 #if defined(ESP_PLATFORM) || defined(RP2040) || defined(__IMXRT1062__)
117 #define add_aux_input(fn, aux, irq, signal_bit) { .function = fn, .irq_mode = irq, .signal.value = signal_bit, .port = IOPORT_UNASSIGNED, .gpio.pin = aux##_PIN },
118 #else
119 #define add_aux_input(fn, aux, irq, signal_bit) { .function = fn, .irq_mode = irq, .signal.value = signal_bit, .port = IOPORT_UNASSIGNED, .gpio.port = (void *)aux##_PORT, .gpio.pin = aux##_PIN },
120 #endif
121 #if defined(__IMXRT1062__) || defined(ESP_PLATFORM)
122 #define add_aux_output(fn, aux) { .function = fn, .port = IOPORT_UNASSIGNED, .gpio.pin = aux##_PIN },
123 #else
124 #define add_aux_output(fn, aux) { .function = fn, .port = IOPORT_UNASSIGNED, .gpio.port = (void *)aux##_PORT, .gpio.pin = aux##_PIN },
125 #endif
126 #define add_aux_input_scan(fn, irq, signal_bit) { .function = fn, .irq_mode = irq, .signal.value = signal_bit, .port = IOPORT_UNASSIGNED, .gpio.pin = 0xFF, .scan = On },
127 #define add_aux_input_no_signal(fn, irq) { .function = fn, .irq_mode = irq, .port = IOPORT_UNASSIGNED, .gpio.pin = 0xFE },
128 #define add_aux_output_exp(fn, aux) { .function = fn, .port = IOPORT_UNASSIGNED, .gpio.port = (void *)aux##_PORT, .gpio.pin = aux##_PIN },
129 
130 static aux_ctrl_t aux_ctrl[] = {
131 // The following pins are bound explicitly to aux input pins.
132 #ifdef RESET_PIN
133  #if (CONTROL_ENABLE & CONTROL_ESTOP)
134  add_aux_input(Input_EStop, RESET, IRQ_Mode_RisingFalling, SIGNALS_ESTOP_BIT)
135  #elif (CONTROL_ENABLE & CONTROL_RESET)
136  add_aux_input(Input_Reset, RESET, IRQ_Mode_RisingFalling, SIGNALS_RESET_BIT)
137  #endif
138 #endif
139 #if (CONTROL_ENABLE & CONTROL_FEED_HOLD) && defined(FEED_HOLD_PIN)
140  add_aux_input(Input_FeedHold, FEED_HOLD, IRQ_Mode_RisingFalling, SIGNALS_FEEDHOLD_BIT)
141 #endif
142 #if (CONTROL_ENABLE & CONTROL_CYCLE_START) && defined(CYCLE_START_PIN)
143  add_aux_input(Input_CycleStart, CYCLE_START, IRQ_Mode_RisingFalling, SIGNALS_CYCLESTART_BIT)
144 #endif
145 #if SAFETY_DOOR_ENABLE && defined(SAFETY_DOOR_PIN)
146  add_aux_input(Input_SafetyDoor, SAFETY_DOOR, IRQ_Mode_RisingFalling, SIGNALS_SAFETYDOOR_BIT)
147 #endif
148 #if MOTOR_FAULT_ENABLE && defined(MOTOR_FAULT_PIN)
149  add_aux_input(Input_MotorFault, MOTOR_FAULT, IRQ_Mode_RisingFalling, SIGNALS_MOTOR_FAULT_BIT)
150 #endif
151 #if MOTOR_WARNING_ENABLE && defined(MOTOR_WARNING_PIN)
152  add_aux_input(Input_MotorWarning, MOTOR_WARNING, IRQ_Mode_RisingFalling, SIGNALS_MOTOR_WARNING_BIT)
153 #endif
154 #if I2C_STROBE_ENABLE && defined(I2C_STROBE_PIN)
156 #endif
157 #if MPG_ENABLE == 1 && defined(MPG_MODE_PIN)
159 #endif
160 #if QEI_ENABLE && defined(QEI_A_PIN) && defined(QEI_B_PIN)
163 #endif
164 #if (ENCODER_ENABLE & 1) && defined(QEI_SELECT_PIN)
166 #endif
167 // Probe pins can be bound explicitly and can be "degraded" to not interrupt capable.
168 #if PROBE_ENABLE && defined(PROBE_PIN)
170 #endif
171 #if PROBE2_ENABLE && defined(PROBE2_PIN)
173 #endif
174 #if TOOLSETTER_ENABLE && defined(TOOLSETTER_PIN)
176 #endif
177 
178 // The following pins are allocated from remaining aux inputs pool
179 #if TOOLSETTER_ENABLE && !defined(TOOLSETTER_PIN)
181 #endif
182 #if PROBE2_ENABLE && !defined(PROBE2_PIN)
184 #endif
185 #if TLS_OVERTRAVEL_ENABLE
187 #endif
188 #if LIMITS_OVERRIDE_ENABLE
189  add_aux_input_scan(Input_LimitsOverride, IRQ_Mode_None, SIGNALS_LIMITS_OVERRIDE_BIT)
190 #endif
191 #if STOP_DISABLE_ENABLE
192  add_aux_input_scan(Input_StopDisable, IRQ_Mode_Change, SIGNALS_STOPDISABLE_BIT)
193 #endif
194 #if BLOCK_DELETE_ENABLE
195  add_aux_input_scan(Input_BlockDelete, IRQ_Mode_Change, SIGNALS_BLOCKDELETE_BIT)
196 #endif
197 #if SINGLE_BLOCK_ENABLE
198  add_aux_input_scan(Input_SingleBlock, IRQ_Mode_Change, SIGNALS_SINGLE_BLOCK_BIT)
199 #endif
200 #if PROBE_DISCONNECT_ENABLE
201  add_aux_input_scan(Input_ProbeDisconnect, IRQ_Mode_Change, SIGNALS_PROBE_CONNECTED_BIT)
202 #endif
203 };
204 
205 
206 // General inputs
207 
208 static inline bool aux_ctrl_is_probe (pin_function_t function)
209 {
210  return function == Input_Probe || function == Input_Probe2 || function == Input_Toolsetter;
211 }
212 
213 static inline bool aux_ctrl_is_encoder (pin_function_t function)
214 {
215  return function == Input_QEI_A || function == Input_QEI_B || function == Input_QEI_Select;
216 }
217 
218 #ifdef STM32_PLATFORM
219 
220 static inline aux_ctrl_t *aux_ctrl_get_fn (aux_gpio_t gpio)
221 {
222  aux_ctrl_t *ctrl_pin = NULL;
223 
224  if(sizeof(aux_ctrl) / sizeof(aux_ctrl_t)) {
225  uint_fast8_t idx;
226  for(idx = 0; ctrl_pin == NULL && aux_ctrl[idx].gpio.pin != 0xFF && idx < sizeof(aux_ctrl) / sizeof(aux_ctrl_t); idx++) {
227  if(aux_ctrl[idx].gpio.pin == gpio.pin && aux_ctrl[idx].gpio.port == gpio.port)
228  ctrl_pin = &aux_ctrl[idx];
229  }
230  }
231 
232  return ctrl_pin;
233 }
234 
235 #endif // STM32_PLATFORM
236 
237 static inline xbar_t *aux_ctrl_claim_port (aux_ctrl_t *aux_ctrl)
238 {
239  xbar_t *pin = NULL;
240 
241  if(aux_ctrl) {
242  if(aux_ctrl->port != IOPORT_UNASSIGNED && (pin = ioport_claim(Port_Digital, Port_Input, &aux_ctrl->port, NULL))) {
243  aux_ctrl->gpio.port = pin->port;
244  aux_ctrl->gpio.pin = pin->pin;
245  if(ioport_set_function(pin, aux_ctrl->function, &aux_ctrl->signal))
246  pin->function = aux_ctrl->function;
247  } else
248  aux_ctrl->port = IOPORT_UNASSIGNED;
249  }
250 
251  return pin;
252 }
253 
254 static inline aux_ctrl_t *aux_ctrl_remap_explicit (aux_gpio_t gpio, uint8_t port, void *input)
255 {
256  int_fast8_t idx;
257  aux_ctrl_t *ctrl_pin = NULL;
258 
259  if(sizeof(aux_ctrl) / sizeof(aux_ctrl_t)) {
260  for(idx = 0; ctrl_pin == NULL && idx < sizeof(aux_ctrl) / sizeof(aux_ctrl_t) && aux_ctrl[idx].gpio.pin != 0xFF; idx++) {
261  if(aux_ctrl[idx].gpio.pin == gpio.pin && aux_ctrl[idx].gpio.port == gpio.port) {
262  ctrl_pin = &aux_ctrl[idx];
263  ctrl_pin->port = port;
264  ctrl_pin->input = input;
265  break;
266  }
267  }
268  }
269 
270  return ctrl_pin;
271 }
272 
273 static inline aux_ctrl_t *aux_ctrl_in_get (uint8_t port)
274 {
275  aux_ctrl_t *ctrl_pin = NULL;
276 
277  uint_fast8_t idx = sizeof(aux_ctrl) / sizeof(aux_ctrl_t);
278 
279  if(idx) do {
280  if(aux_ctrl[--idx].port == port)
281  ctrl_pin = &aux_ctrl[idx];
282  } while(idx && ctrl_pin == NULL);
283 
284  return ctrl_pin;
285 }
286 
287 static inline void aux_ctrl_irq_enable (settings_t *settings, ioport_interrupt_callback_ptr aux_irq_handler)
288 {
289  uint_fast8_t idx = sizeof(aux_ctrl) / sizeof(aux_ctrl_t);
290 
291  if(idx) do {
292  if(aux_ctrl[--idx].port != 0xFF && aux_ctrl[idx].irq_mode != IRQ_Mode_None) {
293  if(!(aux_ctrl_is_probe(aux_ctrl[idx].function) || aux_ctrl_is_encoder(aux_ctrl[idx].function))) {
294  pin_irq_mode_t irq_mode;
295  if((irq_mode = aux_ctrl[idx].irq_mode) & IRQ_Mode_RisingFalling)
296  irq_mode = (settings->control_invert.mask & aux_ctrl[idx].signal.mask) ? IRQ_Mode_Falling : IRQ_Mode_Rising;
297  hal.port.register_interrupt_handler(aux_ctrl[idx].port, irq_mode, aux_irq_handler);
298  }
299  }
300  } while(idx);
301 }
302 
303 typedef bool (*aux_claim_explicit_ptr)(aux_ctrl_t *aux_ctrl);
304 
305 // Default/internal functions for aux_ctrl_claim_ports()
306 
307 static bool __claim_in_port (xbar_t *properties, uint8_t port, void *data)
308 {
309  if(ioport_claim(Port_Digital, Port_Input, &port, NULL)) {
310  ((aux_ctrl_t *)data)->port = port;
311  ((aux_ctrl_t *)data)->gpio.port = properties->port;
312  ((aux_ctrl_t *)data)->gpio.pin = properties->pin;
313  ioport_set_function(properties, ((aux_ctrl_t *)data)->function, &((aux_ctrl_t *)data)->signal);
314  }
315 
316  return ((aux_ctrl_t *)data)->port != IOPORT_UNASSIGNED;
317 }
318 
319 static bool __find_in_port (xbar_t *properties, uint8_t port, void *data)
320 {
321  ((aux_ctrl_t *)data)->port = port;
322 
323  return true;
324 }
325 
326 // --
327 
328 static inline void aux_ctrl_claim_ports (aux_claim_explicit_ptr aux_claim_explicit, ioports_enumerate_callback_ptr aux_claim)
329 {
330  uint_fast8_t idx;
331 
332  if(aux_claim == NULL)
333  aux_claim = __claim_in_port;
334 
335  if(sizeof(aux_ctrl)) for(idx = 0; idx < sizeof(aux_ctrl) / sizeof(aux_ctrl_t); idx++) {
336 
337  if(aux_ctrl[idx].port != IOPORT_UNASSIGNED)
338  aux_claim_explicit(&aux_ctrl[idx]);
339 
340  else {
341 
342  pin_cap_t cap = { .irq_mode = aux_ctrl[idx].irq_mode, .claimable = On };
343 
344  // Toolsetter and Probe2
345  if(aux_ctrl[idx].gpio.pin == 0xFE && ioports_enumerate(Port_Digital, Port_Input, cap, __find_in_port, (void *)&aux_ctrl[idx]))
346  aux_claim_explicit(&aux_ctrl[idx]);
347 
348 #ifdef STM32_PLATFORM
349  if(aux_ctrl[idx].irq_mode == IRQ_Mode_None && !(aux_ctrl_is_probe(aux_ctrl[idx].function) || aux_ctrl[idx].function == Input_LimitsOverride))
350  continue;
351 #endif
352  if(aux_ctrl[idx].gpio.pin == 0xFF) {
353  if(ioports_enumerate(Port_Digital, Port_Input, cap, aux_claim, (void *)&aux_ctrl[idx]))
354  hal.signals_cap.mask |= aux_ctrl[idx].signal.mask;
355  }
356  }
357  }
358 }
359 
360 static inline control_signals_t aux_ctrl_scan_status (control_signals_t signals)
361 {
362 #if PROBE_DISCONNECT_ENABLE || STOP_DISABLE_ENABLE || BLOCK_DELETE_ENABLE || SINGLE_BLOCK_ENABLE || LIMITS_OVERRIDE_ENABLE
363 
364  uint_fast8_t idx = sizeof(aux_ctrl) / sizeof(aux_ctrl_t);
365 
366  if(idx) do {
367  if(!aux_ctrl[--idx].scan)
368  break;
369  signals.mask &= ~aux_ctrl[idx].signal.mask;
370  if(aux_ctrl[idx].port != IOPORT_UNASSIGNED) {
371  #ifdef GRBL_ESP32 // Snowflake guru workaround
372  if(hal.port.wait_on_input(Port_Digital, aux_ctrl[idx].port, WaitMode_Immediate, FZERO) == 1)
373  signals.mask |= aux_ctrl[idx].signal.mask;
374  #else
375  if(hal.port.wait_on_input(Port_Digital, aux_ctrl[idx].port, WaitMode_Immediate, 0.0f) == 1)
376  signals.mask |= aux_ctrl[idx].signal.mask;
377  #endif
378  }
379  } while(idx);
380 
381 #endif
382 
383  return signals;
384 }
385 
386 // The following pins are bound explicitly to aux output pins
387 static aux_ctrl_out_t aux_ctrl_out[] = {
388 #if defined(ESP_PLATFORM) || defined(RP2040) // for now
389 #if defined(STEPPERS_ENABLE_PIN) && STEPPERS_ENABLE_PORT == EXPANDER_PORT
390  add_aux_output_exp(Output_StepperEnable, STEPPERS_ENABLE)
391 #endif
392 #if defined(X_ENABLE_PIN) && X_ENABLE_PORT == EXPANDER_PORT
394 #endif
395 #if defined(X2_ENABLE_PIN) && X2_ENABLE_PORT == EXPANDER_PORT
397 #endif
398 #if defined(Y_ENABLE_PIN) && Y_ENABLE_PORT == EXPANDER_PORT
400 #endif
401 #if defined(Y2_ENABLE_PIN) && Y2_ENABLE_PORT == EXPANDER_PORT
403 #endif
404 #if defined(XY_ENABLE_PIN) && XY_ENABLE_PORT == EXPANDER_PORT
406 #endif
407 #if defined(Z_ENABLE_PIN) && Z_ENABLE_PORT == EXPANDER_PORT
409 #endif
410 #if defined(Z2_ENABLE_PIN) && Z2_ENABLE_PORT == EXPANDER_PORT
412 #endif
413 #if defined(A_ENABLE_PIN) && A_ENABLE_PORT == EXPANDER_PORT
415 #endif
416 #if defined(B_ENABLE_PIN) && B_ENABLE_PORT == EXPANDER_PORT
418 #endif
419 #if defined(C_ENABLE_PIN) && C_ENABLE_PORT == EXPANDER_PORT
421 #endif
422 #if defined(U_ENABLE_PIN) && U_ENABLE_PORT == EXPANDER_PORT
424 #endif
425 #if defined(V_ENABLE_PIN) && V_ENABLE_PORT == EXPANDER_PORT
427 #endif
428 #endif //
429 
430 #ifdef SPINDLE_ENABLE_PIN
432 #endif
433 #ifdef SPINDLE_PWM_PIN
435 #endif
436 #ifdef SPINDLE_DIRECTION_PIN
437  add_aux_output(Output_SpindleDir, SPINDLE_DIRECTION)
438 #endif
439 #ifdef SPINDLE1_ENABLE_PIN
441 #endif
442 #ifdef SPINDLE1_DIRECTION_PIN
443  add_aux_output(Output_Spindle1Dir, SPINDLE1_DIRECTION)
444 #endif
445 #ifdef SPINDLE1_PWM_PIN
446  add_aux_output(Output_Spindle1PWM, SPINDLE1_PWM)
447 #endif
448 #ifdef COOLANT_FLOOD_PIN
450 #endif
451 #ifdef COOLANT_MIST_PIN
453 #endif
454 #ifdef COPROC_RESET_PIN
455  add_aux_output(Output_CoProc_Reset, COPROC_RESET)
456 #endif
457 #ifdef COPROC_BOOT0_PIN
458  add_aux_output(Output_CoProc_Boot0, COPROC_BOOT0)
459 #endif
460 #if defined(SPI_RST_PIN) && defined(RP2040)
461  #ifndef SPI_RST_PORT
462  #define SPI_RST_PORT 0
463  #endif
464  add_aux_output(Output_SPIRST, SPI_RST)
465 #endif
466 };
467 
468 static inline aux_ctrl_out_t *aux_out_remap_explicit (aux_gpio_t gpio, uint8_t port, void *output)
469 {
470  aux_ctrl_out_t *ctrl_pin = NULL;
471 
472  uint_fast8_t idx = sizeof(aux_ctrl_out) / sizeof(aux_ctrl_out_t);
473 
474  if(idx) do {
475  idx--;
476  if(aux_ctrl_out[idx].gpio.port == gpio.port && aux_ctrl_out[idx].gpio.pin == gpio.pin) {
477  ctrl_pin = &aux_ctrl_out[idx];
478  ctrl_pin->port = port;
479  ctrl_pin->output = output;
480  }
481  } while(idx && ctrl_pin == NULL);
482 
483  return ctrl_pin;
484 }
485 
486 typedef bool (*aux_claim_explicit_out_ptr)(aux_ctrl_out_t *aux_ctrl);
487 
488 // Default functions for aux_ctrl_claim_out_ports()
489 
490 static bool __claim_out_port (xbar_t *properties, uint8_t port, void *data)
491 {
492  if(((aux_ctrl_out_t *)data)->gpio.port == (void *)EXPANDER_PORT) {
493  if(((aux_ctrl_out_t *)data)->gpio.pin == properties->pin && properties->set_value)
494  ((aux_ctrl_out_t *)data)->port = port;
495  } else if(ioport_claim(Port_Digital, Port_Output, &port, xbar_fn_to_pinname(((aux_ctrl_out_t *)data)->function)))
496  ((aux_ctrl_out_t *)data)->port = port;
497 
498  return ((aux_ctrl_out_t *)data)->port != IOPORT_UNASSIGNED;
499 }
500 
501 static bool ___claim_out_port_explicit (aux_ctrl_out_t *aux_ctrl)
502 {
503  xbar_t *pin;
504 
505  if((pin = ioport_claim(Port_Digital, Port_Output, &aux_ctrl->port, NULL)))
506  ioport_set_function(pin, aux_ctrl->function, NULL);
507  else
508  aux_ctrl->port = IOPORT_UNASSIGNED;
509 
510  return aux_ctrl->port != IOPORT_UNASSIGNED;
511 }
512 
513 //
514 
515 static inline void aux_ctrl_claim_out_ports (aux_claim_explicit_out_ptr aux_claim_explicit, ioports_enumerate_callback_ptr aux_claim)
516 {
517  uint_fast8_t idx;
518 
519  if(aux_claim == NULL)
520  aux_claim = __claim_out_port;
521 
522  if(aux_claim_explicit == NULL)
523  aux_claim_explicit = ___claim_out_port_explicit;
524 
525  if(sizeof(aux_ctrl_out)) for(idx = 0; idx < sizeof(aux_ctrl_out) / sizeof(aux_ctrl_out_t); idx++) {
526  if(aux_ctrl_out[idx].gpio.port == (void *)EXPANDER_PORT) {
527  if(ioports_enumerate(Port_Digital, Port_Output, (pin_cap_t){ .external = On, .claimable = On }, aux_claim, &aux_ctrl_out[idx])) {
528  if((aux_ctrl_out[idx].output = ioport_claim(Port_Digital, Port_Output, &aux_ctrl_out[idx].port, NULL /*xbar_fn_to_pinname(aux_ctrl_out[idx].function)*/))) {
529  ioport_set_function((xbar_t *)aux_ctrl_out[idx].output, aux_ctrl_out[idx].function, NULL);
530  // TODO: else set description?
531  aux_claim_explicit(&aux_ctrl_out[idx]);
532  }
533  }
534  } else if(aux_ctrl_out[idx].gpio.pin == 0xFF) {
535  if(ioports_enumerate(Port_Digital, Port_Output, (pin_cap_t){ .claimable = On }, aux_claim, &aux_ctrl_out[idx]))
536  aux_claim_explicit(&aux_ctrl_out[idx]);
537  } else if(aux_ctrl_out[idx].port != IOPORT_UNASSIGNED)
538  aux_claim_explicit(&aux_ctrl_out[idx]);
539  }
540 }
541 
542 // Output Signals
543 
544 #if defined(SPINDLE_ENABLE_PIN) && !defined(SPINDLE_ENABLE_BIT)
545 #define SPINDLE_ENABLE_BIT (1<<SPINDLE_ENABLE_PIN)
546 #endif
547 #if defined(SPINDLE_DIRECTION_PIN) && !defined(SPINDLE_DIRECTION_BIT)
548 #define SPINDLE_DIRECTION_BIT (1<<SPINDLE_DIRECTION_PIN)
549 #endif
550 
551 #if defined(SPINDLE1_ENABLE_PIN) && !defined(SPINDLE1_ENABLE_BIT)
552 #define SPINDLE1_ENABLE_BIT (1<<SPINDLE1_ENABLE_PIN)
553 #endif
554 #if defined(SPINDLE1_DIRECTION_PIN) && !defined(SPINDLE1_DIRECTION_BIT)
555 #define SPINDLE1_DIRECTION_BIT (1<<SPINDLE1_DIRECTION_PIN)
556 #endif
557 
558 #if defined(COOLANT_FLOOD_PIN) && !defined(COOLANT_FLOOD_BIT)
559 #define COOLANT_FLOOD_BIT (1<<COOLANT_FLOOD_PIN)
560 #endif
561 #if defined(COOLANT_MIST_PIN) && !defined(COOLANT_MIST_BIT)
562 #define COOLANT_MIST_BIT (1<<COOLANT_MIST_PIN)
563 #endif
564 
565 #if defined(RTS_PIN) && !defined(RTS_BIT)
566 #define RTS_BIT (1<<RTS_PIN)
567 #endif
568 
569 #if defined(RS485_DIR_PIN) && !defined(RS485_DIR_BIT)
570 #define RS485_DIR_BIT (1<<RS485_DIR_PIN)
571 #endif
572 
573 // IRQ enabled input singnals
574 /*
575 #if QEI_ENABLE
576 #ifndef QEI_A_BIT
577 #define QEI_A_BIT (1<<QEI_A_PIN)
578 #endif
579 #ifndef QEI_B_BIT
580 #define QEI_B_BIT (1<<QEI_B_PIN)
581 #endif
582 #else*/
583 #define QEI_A_BIT 0
584 #define QEI_B_BIT 0
585 //#endif
586 
587 #ifndef QEI_SELECT_BIT
588 #define QEI_SELECT_BIT 0
589 #endif
590 #ifndef MPG_MODE_BIT
591 #define MPG_MODE_BIT 0
592 #endif
593 #ifndef I2C_STROBE_BIT
594 #define I2C_STROBE_BIT 0
595 #endif
596 
597 // Do NOT #define PROBE_BIT 0 here!
598 
599 #if SPINDLE_ENCODER_ENABLE
600 #ifndef SPINDLE_PULSE_PIN
601 #error "Spindle encoder requires at least SPINDLE_PULSE_PIN defined in the board map!"
602 #endif
603 #if !defined(SPINDLE_PULSE_BIT) && defined(SPINDLE_PULSE_PIN)
604 #define SPINDLE_PULSE_BIT (1<<SPINDLE_PULSE_PIN)
605 #endif
606 #if !defined(SPINDLE_INDEX_BIT) && defined(SPINDLE_INDEX_PIN)
607 #define SPINDLE_INDEX_BIT (1<<SPINDLE_INDEX_PIN)
608 #endif
609 #endif
610 
611 #ifndef SPINDLE_INDEX_BIT
612 #define SPINDLE_INDEX_BIT 0
613 #endif
614 #ifndef SPINDLE_PULSE_BIT
615 #define SPINDLE_PULSE_BIT 0
616 #endif
617 
618 #if SPINDLE_ENCODER_ENABLE && (SPINDLE_INDEX_BIT + SPINDLE_PULSE_BIT) == 0
619 #error "Spindle encoder requires SPINDLE_PULSE_PIN and SPINDLE_INDEX_PIN defined in the board map!"
620 #endif
621 
622 #ifndef SPI_IRQ_PIN
623 #define SPI_IRQ_BIT 0
624 #elif !defined(SPI_IRQ_BIT)
625 #define SPI_IRQ_BIT (1<<SPI_IRQ_PIN)
626 #endif
627 
628 #ifndef DEVICES_IRQ_MASK
629 #define DEVICES_IRQ_MASK (SPI_IRQ_BIT|SPINDLE_INDEX_BIT|QEI_A_BIT|QEI_B_BIT|SD_DETECT_BIT)
630 #define DEVICES_IRQ_MASK_SUM (SPI_IRQ_BIT+SPINDLE_INDEX_BIT+QEI_A_BIT+QEI_B_BIT+SD_DETECT_BIT)
631 #endif
632 
633 #ifdef STM32_PLATFORM
634 
635 // Used for validating pins that requires IRQ capabilities
636 
637 #ifdef RESET_PIN
638 #define RESET_BIT (1<<RESET_PIN)
639 #else
640 #define RESET_BIT 0
641 #endif
642 
643 #ifdef FEED_HOLD_PIN
644 #define FEED_HOLD_BIT (1<<FEED_HOLD_PIN)
645 #else
646 #define FEED_HOLD_BIT 0
647 #endif
648 
649 #ifdef CYCLE_START_PIN
650 #define CYCLE_START_BIT (1<<CYCLE_START_PIN)
651 #else
652 #define CYCLE_START_BIT 0
653 #endif
654 
655 #ifdef PROBE_DISCONNECT_PIN
656 #define PROBE_DISCONNECT_BIT (1<<PROBE_DISCONNECT_PIN)
657 #else
658 #define PROBE_DISCONNECT_BIT 0
659 #endif
660 
661 #ifdef STOP_DISABLE_PIN
662 #define STOP_DISABLE_BIT (1<<STOP_DISABLE_PIN)
663 #else
664 #define STOP_DISABLE_BIT 0
665 #endif
666 
667 #ifdef BLOCK_DELETE_PIN
668 #define BLOCK_DELETE_BIT (1<<BLOCK_DELETE_PIN)
669 #else
670 #define BLOCK_DELETE_BIT 0
671 #endif
672 
673 #ifdef SINGLE_BLOCK_PIN
674 #define SINGLE_BLOCK_BIT (1<<SINGLE_BLOCK_PIN)
675 #else
676 #define SINGLE_BLOCK_BIT 0
677 #endif
678 
679 #ifdef MOTOR_FAULT_PIN
680 #define MOTOR_FAULT_BIT (1<<MOTOR_FAULT_PIN)
681 #else
682 #define MOTOR_FAULT_BIT 0
683 #endif
684 
685 #ifdef MOTOR_WARNING_PIN
686 #define MOTOR_WARNING_BIT (1<<MOTOR_WARNING_PIN)
687 #else
688 #define MOTOR_WARNING_BIT 0
689 #endif
690 
691 #ifndef CONTROL_MASK
692 #define CONTROL_MASK (RESET_BIT|FEED_HOLD_BIT|CYCLE_START_BIT|PROBE_DISCONNECT_BIT|STOP_DISABLE_BIT|BLOCK_DELETE_BIT|SINGLE_BLOCK_BIT|MOTOR_FAULT_BIT|MOTOR_WARNING_BIT)
693 #define CONTROL_MASK_SUM (RESET_BIT+FEED_HOLD_BIT+CYCLE_START_BIT+PROBE_DISCONNECT_BIT+STOP_DISABLE_BIT+BLOCK_DELETE_BIT+SINGLE_BLOCK_BIT+MOTOR_FAULT_BIT+MOTOR_WARNING_BIT)
694 #endif
695 
696 #endif // STM32_PLATFORM
697 
698 // Auxiliary input signals
699 
700 #ifdef AUXINPUT0_PIN
701 #define AUXINPUT0_BIT (1<<AUXINPUT0_PIN)
702 #else
703 #define AUXINPUT0_BIT 0
704 #endif
705 #ifdef AUXINPUT1_PIN
706 #define AUXINPUT1_BIT (1<<AUXINPUT1_PIN)
707 #else
708 #define AUXINPUT1_BIT 0
709 #endif
710 #ifdef AUXINPUT2_PIN
711 #define AUXINPUT2_BIT (1<<AUXINPUT2_PIN)
712 #else
713 #define AUXINPUT2_BIT 0
714 #endif
715 #ifdef AUXINPUT3_PIN
716 #define AUXINPUT3_BIT (1<<AUXINPUT3_PIN)
717 #else
718 #define AUXINPUT3_BIT 0
719 #endif
720 #ifdef AUXINPUT4_PIN
721 #define AUXINPUT4_BIT (1<<AUXINPUT4_PIN)
722 #else
723 #define AUXINPUT4_BIT 0
724 #endif
725 #ifdef AUXINPUT5_PIN
726 #define AUXINPUT5_BIT (1<<AUXINPUT5_PIN)
727 #else
728 #define AUXINPUT5_BIT 0
729 #endif
730 #ifdef AUXINPUT6_PIN
731 #define AUXINPUT6_BIT (1<<AUXINPUT6_PIN)
732 #else
733 #define AUXINPUT6_BIT 0
734 #endif
735 #ifdef AUXINPUT7_PIN
736 #define AUXINPUT7_BIT (1<<AUXINPUT7_PIN)
737 #else
738 #define AUXINPUT7_BIT 0
739 #endif
740 #ifdef AUXINPUT8_PIN
741 #define AUXINPUT8_BIT (1<<AUXINPUT8_PIN)
742 #else
743 #define AUXINPUT8_BIT 0
744 #endif
745 #ifdef AUXINPUT9_PIN
746 #define AUXINPUT9_BIT (1<<AUXINPUT9_PIN)
747 #else
748 #define AUXINPUT9_BIT 0
749 #endif
750 #ifdef AUXINPUT10_PIN
751 #define AUXINPUT10_BIT (1<<AUXINPUT10_PIN)
752 #else
753 #define AUXINPUT10_BIT 0
754 #endif
755 #ifdef AUXINPUT11_PIN
756 #define AUXINPUT11_BIT (1<<AUXINPUT11_PIN)
757 #else
758 #define AUXINPUT11_BIT 0
759 #endif
760 #ifdef AUXINPUT12_PIN
761 #define AUXINPUT12_BIT (1<<AUXINPUT12_PIN)
762 #else
763 #define AUXINPUT12_BIT 0
764 #endif
765 #ifdef AUXINPUT13_PIN
766 #define AUXINPUT13_BIT (1<<AUXINPUT13_PIN)
767 #else
768 #define AUXINPUT13_BIT 0
769 #endif
770 #ifdef AUXINPUT14_PIN
771 #define AUXINPUT14_BIT (1<<AUXINPUT14_PIN)
772 #else
773 #define AUXINPUT14_BIT 0
774 #endif
775 #ifdef AUXINPUT15_PIN
776 #define AUXINPUT15_BIT (1<<AUXINPUT15_PIN)
777 #else
778 #define AUXINPUT15_BIT 0
779 #endif
780 
781 #ifndef AUXINPUT_MASK
782 #define AUXINPUT_MASK (AUXINPUT0_BIT|AUXINPUT1_BIT|AUXINPUT2_BIT|AUXINPUT3_BIT|AUXINPUT4_BIT|AUXINPUT5_BIT|AUXINPUT6_BIT|AUXINPUT7_BIT|\
783  AUXINPUT8_BIT|AUXINPUT9_BIT|AUXINPUT10_BIT|AUXINPUT11_BIT|AUXINPUT12_BIT|AUXINPUT13_BIT|AUXINPUT4_BIT|AUXINPUT15_BIT)
784 #define AUXINPUT_MASK_SUM (AUXINPUT0_BIT+AUXINPUT1_BIT+AUXINPUT2_BIT+AUXINPUT3_BIT+AUXINPUT4_BIT+AUXINPUT5_BIT+AUXINPUT6_BIT+AUXINPUT7_BIT+\
785  AUXINPUT8_BIT+AUXINPUT9_BIT+AUXINPUT10_BIT+AUXINPUT11_BIT+AUXINPUT12_BIT+AUXINPUT13_BIT+AUXINPUT4_BIT+AUXINPUT15_BIT)
786 #endif
787 
788 /*EOF*/
const char * xbar_fn_to_pinname(pin_function_t fn)
Definition: crossbar.c:177
pin_function_t
Definition: crossbar.h:27
@ Input_QEI_A
Definition: crossbar.h:265
@ Input_FeedHold
Definition: crossbar.h:30
@ Input_Toolsetter
Definition: crossbar.h:59
@ Output_StepperEnableV
Definition: crossbar.h:188
@ Input_Reset
Definition: crossbar.h:29
@ Output_CoProc_Boot0
Definition: crossbar.h:243
@ Input_Probe
Definition: crossbar.h:43
@ Output_StepperEnableX
Definition: crossbar.h:178
@ Input_MotorFault
Definition: crossbar.h:37
@ Input_Probe2
Definition: crossbar.h:57
@ Input_MotorWarning
Definition: crossbar.h:38
@ Input_ToolsetterOvertravel
Definition: crossbar.h:41
@ Input_ProbeDisconnect
Definition: crossbar.h:36
@ Input_EStop
Definition: crossbar.h:35
@ Input_StopDisable
Definition: crossbar.h:34
@ Output_StepperEnableXY
Definition: crossbar.h:190
@ Input_LimitsOverride
Definition: crossbar.h:39
@ Output_CoProc_Reset
Definition: crossbar.h:242
@ Output_StepperEnableZ
Definition: crossbar.h:182
@ Input_QEI_Select
Definition: crossbar.h:267
@ Output_StepperEnableU
Definition: crossbar.h:187
@ Output_StepperEnableA
Definition: crossbar.h:184
@ Output_StepperEnableY2
Definition: crossbar.h:181
@ Input_BlockDelete
Definition: crossbar.h:33
@ Output_StepperEnable
Definition: crossbar.h:176
@ Output_SpindleDir
Definition: crossbar.h:193
@ Output_StepperEnableY
Definition: crossbar.h:180
@ Output_StepperEnableC
Definition: crossbar.h:186
@ Input_SingleBlock
Definition: crossbar.h:40
@ Output_SPIRST
Definition: crossbar.h:253
@ Output_SpindleOn
Definition: crossbar.h:192
@ Input_I2CStrobe
Definition: crossbar.h:260
@ Output_Spindle1Dir
Definition: crossbar.h:196
@ Output_SpindlePWM
Definition: crossbar.h:194
@ Output_CoolantMist
Definition: crossbar.h:198
@ Input_MPGSelect
Definition: crossbar.h:60
@ Output_Spindle1On
Definition: crossbar.h:195
@ Input_QEI_B
Definition: crossbar.h:266
@ Output_StepperEnableB
Definition: crossbar.h:185
@ Input_CycleStart
Definition: crossbar.h:31
@ Input_SafetyDoor
Definition: crossbar.h:32
@ Output_CoolantFlood
Definition: crossbar.h:199
@ Output_StepperEnableX2
Definition: crossbar.h:179
@ Output_StepperEnableZ2
Definition: crossbar.h:183
@ Output_Spindle1PWM
Definition: crossbar.h:197
pin_irq_mode_t
Pin interrupt modes, may be or'ed when reporting pin capability.
Definition: crossbar.h:573
@ IRQ_Mode_Rising
0b00001 (0x01)
Definition: crossbar.h:575
@ IRQ_Mode_RisingFalling
0b00011 (0x03) - only used to report port capability.
Definition: crossbar.h:577
@ IRQ_Mode_None
0b00000 (0x00)
Definition: crossbar.h:574
@ IRQ_Mode_Change
0b00100 (0x04)
Definition: crossbar.h:578
@ IRQ_Mode_Falling
0b00010 (0x02)
Definition: crossbar.h:576
#define SPINDLE_ENABLE
Definition: driver_opts.h:345
#define SPINDLE1_ENABLE
Definition: driver_opts.h:317
#define COOLANT_FLOOD
Definition: driver_opts.h:300
#define COOLANT_MIST
Definition: driver_opts.h:301
#define SPINDLE_PWM
Definition: driver_opts.h:309
@ WaitMode_Immediate
0 - This is the only mode allowed for analog inputs
Definition: gcode.h:311
DCRAM grbl_hal_t hal
Global HAL struct.
Definition: grbllib.c:91
FLASHMEM xbar_t * ioport_claim(io_port_type_t type, io_port_direction_t dir, uint8_t *port, const char *description)
Claim a digital or analog port for exclusive use.
Definition: ioports.c:310
FLASHMEM bool ioport_set_function(xbar_t *pin, pin_function_t function, driver_caps_t caps)
Set pin function.
Definition: ioports.c:386
FLASHMEM bool ioports_enumerate(io_port_type_t type, io_port_direction_t dir, pin_cap_t filter, ioports_enumerate_callback_ptr callback, void *data)
Enumerate ports.
Definition: ioports.c:551
@ Port_Input
0
Definition: ioports.h:34
@ Port_Output
1
Definition: ioports.h:35
bool(* ioports_enumerate_callback_ptr)(xbar_t *properties, uint8_t port, void *data)
Definition: ioports.h:161
@ Port_Digital
1
Definition: ioports.h:30
#define IOPORT_UNASSIGNED
Definition: ioports.h:26
void(* ioport_interrupt_callback_ptr)(uint8_t port, bool state)
Pointer to callback function for input port interrupt events.
Definition: ioports.h:142
#define On
Definition: nuts_bolts.h:36
#define add_aux_input_scan(fn, irq, signal_bit)
Definition: pin_bits_masks.h:126
#define add_aux_input(fn, aux, irq, signal_bit)
Definition: pin_bits_masks.h:119
bool(* aux_claim_explicit_ptr)(aux_ctrl_t *aux_ctrl)
Definition: pin_bits_masks.h:303
#define add_aux_output(fn, aux)
Definition: pin_bits_masks.h:124
bool(* aux_claim_explicit_out_ptr)(aux_ctrl_out_t *aux_ctrl)
Definition: pin_bits_masks.h:486
#define add_aux_input_no_signal(fn, irq)
Definition: pin_bits_masks.h:127
#define add_aux_output_exp(fn, aux)
Definition: pin_bits_masks.h:128
#define EXPANDER_PORT
Definition: pin_bits_masks.h:56
settings_t settings
Definition: settings.c:47
Definition: crossbar.h:756
uint8_t port
Auxiliary port number, post claimed.
Definition: crossbar.h:758
void * output
Pointer to the driver input array entry for the pin.
Definition: crossbar.h:760
Definition: crossbar.h:746
pin_irq_mode_t irq_mode
Required IRQ mode for the input.
Definition: crossbar.h:749
control_signals_t signal
Set to the pin the input maps to, 0 if none.
Definition: crossbar.h:750
uint8_t port
Auxiliary port number, post claimed.
Definition: crossbar.h:748
void * input
Pointer to the driver input array entry for the pin.
Definition: crossbar.h:752
aux_gpio_t gpio
MCU port base address (may be NULL) and pin number.
Definition: crossbar.h:751
pin_function_t function
Pin function.
Definition: crossbar.h:747
Definition: crossbar.h:741
uint8_t pin
MCU pin number.
Definition: crossbar.h:743
void * port
MCU port address (may be NULL).
Definition: crossbar.h:742
io_port_t port
Optional handlers for axuillary I/O (adds support for M62-M66).
Definition: hal.h:655
control_signals_t signals_cap
Control input signals supported by the driver.
Definition: hal.h:690
wait_on_input_ptr wait_on_input
Optional handler for reading a digital or analog input.
Definition: ioports.h:171
ioport_register_interrupt_handler_ptr register_interrupt_handler
Definition: ioports.h:176
Definition: settings.h:881
control_signals_t control_invert
Definition: settings.h:893
Definition: crossbar.h:763
xbar_set_value_ptr set_value
Optional pointer to function to set port value.
Definition: crossbar.h:775
uint_fast8_t pin
Pin number.
Definition: crossbar.h:770
pin_function_t function
Pin function.
Definition: crossbar.h:766
void * port
Optional pointer to the underlying peripheral or pin specific data.
Definition: crossbar.h:768
Definition: nuts_bolts.h:416
uint16_t mask
Definition: nuts_bolts.h:418
Definition: crossbar.h:631
uint32_t irq_mode
pin_irq_mode_t - IRQ modes
Definition: crossbar.h:638