grblHAL core  20250412
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-2024 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 // Sanity checks
25 
26 #if PROBE_ENABLE && !defined(PROBE_PIN)
27 #error "Probe input is not supported in this configuration!"
28 #endif
29 
30 #if SAFETY_DOOR_ENABLE && !defined(SAFETY_DOOR_PIN)
31 #error "Safety door input is not supported in this configuration!"
32 #endif
33 
34 #if MOTOR_FAULT_ENABLE && !defined(MOTOR_FAULT_PIN)
35 #error "Motor fault input is not supported in this configuration!"
36 #endif
37 
38 #if MOTOR_WARNING_ENABLE && !defined(MOTOR_WARNING_PIN)
39 #error "Motor warning input is not supported in this configuration!"
40 #endif
41 
42 #if I2C_STROBE_ENABLE && !defined(I2C_STROBE_PIN)
43 #error "I2C keypad/strobe is not supported in this configuration!"
44 #endif
45 
46 #if MPG_ENABLE == 1 && !defined(MPG_MODE_PIN)
47 #error "MPG mode input is not supported in this configuration!"
48 #endif
49 
50 #if QEI_SELECT_ENABLE && !defined(QEI_SELECT_PIN)
51 #error "Encoder select input is not supported in this configuration!"
52 #endif
53 
54 #define EXPANDER_PORT 1
55 
56 // Control input signals
57 
58 // Define the CONTROL_PORT symbol as a shorthand in the *_map.h file if all control inputs share the same port.
59 #ifdef CONTROL_PORT
60 
61 #ifndef RESET_PORT
62 #define RESET_PORT CONTROL_PORT
63 #endif
64 #ifndef FEED_HOLD_PORT
65 #define FEED_HOLD_PORT CONTROL_PORT
66 #endif
67 #ifndef CYCLE_START_PORT
68 #define CYCLE_START_PORT CONTROL_PORT
69 #endif
70 #ifndef ESTOP_PORT
71 #define ESTOP_PORT CONTROL_PORT
72 #endif
73 #ifndef PROBE_DISCONNECT_PORT
74 #define PROBE_DISCONNECT_PORT CONTROL_PORT
75 #endif
76 #ifndef STOP_DISABLE_PORT
77 #define STOP_DISABLE_PORT CONTROL_PORT
78 #endif
79 #ifndef BLOCK_DELETE_PORT
80 #define BLOCK_DELETE_PORT CONTROL_PORT
81 #endif
82 #ifndef SINGLE_BLOCK_PORT
83 #define SINGLE_BLOCK_PORT CONTROL_PORT
84 #endif
85 #ifndef MOTOR_FAULT_PORT
86 #define MOTOR_FAULT_PORT CONTROL_PORT
87 #endif
88 #ifndef MOTOR_WARNING_PORT
89 #define MOTOR_WARNING_PORT CONTROL_PORT
90 #endif
91 #ifndef LIMITS_OVERRIDE_PORT
92 #define LIMITS_OVERRIDE_PORT CONTROL_PORT
93 #endif
94 #if SAFETY_DOOR_ENABLE && !defined(SAFETY_DOOR_PORT)
95 #define SAFETY_DOOR_PORT CONTROL_PORT
96 #endif
97 
98 #endif // CONTROL_PORT
99 
100 #ifndef RESET_BIT
101 #ifdef RESET_PIN
102 #define RESET_BIT (1<<RESET_PIN)
103 #else
104 #define RESET_BIT 0
105 #endif
106 #endif
107 
108 #ifndef FEED_HOLD_BIT
109 #ifdef FEED_HOLD_PIN
110 #define FEED_HOLD_BIT (1<<FEED_HOLD_PIN)
111 #else
112 #define FEED_HOLD_BIT 0
113 #endif
114 #endif
115 
116 #ifndef CYCLE_START_BIT
117 #ifdef CYCLE_START_PIN
118 #define CYCLE_START_BIT (1<<CYCLE_START_PIN)
119 #else
120 #define CYCLE_START_BIT 0
121 #endif
122 #endif
123 
124 #ifndef ESTOP_BIT
125 #ifdef ESTOP_PIN
126 #define ESTOP_BIT (1<<ESTOP_PIN)
127 #else
128 #define ESTOP_BIT 0
129 #endif
130 #endif
131 
132 #ifndef SD_DETECT_BIT
133 #ifdef SD_DETECT_PIN
134 #define SD_DETECT_BIT (1<<SD_DETECT_PIN)
135 #else
136 #define SD_DETECT_BIT 0
137 #endif
138 #endif
139 
140 // Optional control signals
141 
142 #ifndef SAFETY_DOOR_BIT
143 #if defined(SAFETY_DOOR_PIN) && !defined(AUX_DEVICES)
144 #define SAFETY_DOOR_BIT (1<<SAFETY_DOOR_PIN)
145 #else
146 #define SAFETY_DOOR_BIT 0
147 #endif
148 #endif
149 
150 // Optional control signals, assigned to auxiliary input pins
151 
152 #ifndef MOTOR_FAULT_BIT
153 #if defined(MOTOR_FAULT_PIN) && !MOTOR_FAULT_ENABLE
154 #define MOTOR_FAULT_BIT (1<<MOTOR_FAULT_PIN)
155 #else
156 #define MOTOR_FAULT_BIT 0
157 #endif
158 #endif
159 
160 #ifndef MOTOR_WARNING_BIT
161 #if defined(MOTOR_WARNING_PIN) && !MOTOR_WARNING_ENABLE
162 #define MOTOR_WARNING_BIT (1<<MOTOR_WARNING_PIN)
163 #else
164 #define MOTOR_WARNING_BIT 0
165 #endif
166 #endif
167 
168 #ifndef PROBE_DISCONNECT_BIT
169 #if defined(PROBE_DISCONNECT_PIN) && !PROBE_DISCONNECT_ENABLE
170 #define PROBE_DISCONNECT_BIT (1<<PROBE_DISCONNECT_PIN)
171 #else
172 #define PROBE_DISCONNECT_BIT 0
173 #endif
174 #endif
175 
176 #ifndef STOP_DISABLE_BIT
177 #if defined(STOP_DISABLE_PIN) && !STOP_DISABLE_ENABLE
178 #define STOP_DISABLE_BIT (1<<STOP_DISABLE_PIN)
179 #else
180 #define STOP_DISABLE_BIT 0
181 #endif
182 #endif
183 
184 #ifndef BLOCK_DELETE_BIT
185 #if defined(BLOCK_DELETE_PIN) && !BLOCK_DELETE_ENABLE
186 #define BLOCK_DELETE_BIT (1<<BLOCK_DELETE_PIN)
187 #else
188 #define BLOCK_DELETE_BIT 0
189 #endif
190 #endif
191 
192 #ifndef SINGLE_BLOCK_BIT
193 #if defined(SINGLE_BLOCK_PIN) && !SINGLE_BLOCK_ENABLE
194 #define SINGLE_BLOCK_BIT (1<<SINGLE_BLOCK_PIN)
195 #else
196 #define SINGLE_BLOCK_BIT 0
197 #endif
198 #endif
199 
200 #ifndef LIMITS_OVERRIDE_BIT
201 #if defined(LIMITS_OVERRIDE_PIN) && !LIMITS_OVERRIDE_ENABLE
202 #define LIMITS_OVERRIDE_BIT (1<<LIMITS_OVERRIDE_PIN)
203 #else
204 #define LIMITS_OVERRIDE_BIT 0
205 #endif
206 #endif
207 
208 #if SAFETY_DOOR_ENABLE || MOTOR_FAULT_ENABLE || MOTOR_WARNING_ENABLE || PROBE_DISCONNECT_ENABLE || \
209  STOP_DISABLE_ENABLE || BLOCK_DELETE_ENABLE || SINGLE_BLOCK_ENABLE || LIMITS_OVERRIDE_ENABLE || \
210  (defined(AUX_DEVICES) && (PROBE_ENABLE || I2C_STROBE_ENABLE || MPG_ENABLE == 1 || QEI_SELECT_ENABLE)) || defined __DOXYGEN__
211 
212 #define AUX_CONTROLS_ENABLED 1
213 
214 #if PROBE_DISCONNECT_ENABLE || STOP_DISABLE_ENABLE || BLOCK_DELETE_ENABLE || SINGLE_BLOCK_ENABLE || LIMITS_OVERRIDE_ENABLE
215 #define AUX_CONTROLS_SCAN 1
216 #else
217 #define AUX_CONTROLS_SCAN 0
218 #endif
219 
220 static aux_ctrl_t aux_ctrl[] = {
221 // The following pins are bound explicitly to aux input pins
222 #if PROBE_ENABLE && defined(PROBE_PIN) && defined(AUX_DEVICES)
223 #ifdef PROBE_PORT
224  { .function = Input_Probe, .aux_port = 0xFF, .irq_mode = (pin_irq_mode_t)(IRQ_Mode_Rising|IRQ_Mode_Falling), .cap = { .value = 0 }, .pin = PROBE_PIN, .port = (void *)PROBE_PORT },
225 #else
226  { .function = Input_Probe, .aux_port = 0xFF, .irq_mode = (pin_irq_mode_t)(IRQ_Mode_Rising|IRQ_Mode_Falling), .cap = { .value = 0 }, .pin = PROBE_PIN, .port = NULL },
227 #endif
228 #endif
229 #if SAFETY_DOOR_ENABLE && defined(SAFETY_DOOR_PIN)
230 #ifdef SAFETY_DOOR_PORT
231  { .function = Input_SafetyDoor, .aux_port = 0xFF, .irq_mode = (pin_irq_mode_t)(IRQ_Mode_Rising|IRQ_Mode_Falling), .cap = { .safety_door_ajar = On }, .pin = SAFETY_DOOR_PIN, .port = (void *)SAFETY_DOOR_PORT },
232 #else
233  { .function = Input_SafetyDoor, .aux_port = 0xFF, .irq_mode = (pin_irq_mode_t)(IRQ_Mode_Rising|IRQ_Mode_Falling), .cap = { .safety_door_ajar = On }, .pin = SAFETY_DOOR_PIN, .port = NULL },
234 #endif
235 #endif
236 #if MOTOR_FAULT_ENABLE && defined(MOTOR_FAULT_PIN)
237 #ifdef MOTOR_FAULT_PORT
238  { .function = Input_MotorFault, .aux_port = 0xFF, .irq_mode = (pin_irq_mode_t)(IRQ_Mode_Rising|IRQ_Mode_Falling), .cap = { .motor_fault = On }, .pin = MOTOR_FAULT_PIN, .port = (void *)MOTOR_FAULT_PORT },
239 #else
240  { .function = Input_MotorFault, .aux_port = 0xFF, .irq_mode = (pin_irq_mode_t)(IRQ_Mode_Rising|IRQ_Mode_Falling), .cap = { .motor_fault = On }, .pin = MOTOR_FAULT_PIN, .port = NULL },
241 #endif
242 #if MOTOR_WARNING_ENABLE && defined(MOTOR_WARNING_PIN)
243 #ifdef MOTOR_WARNING_PORT
244  { .function = Input_MotorWarning, .aux_port = 0xFF, .irq_mode = (pin_irq_mode_t)(IRQ_Mode_Rising|IRQ_Mode_Falling), .cap = { .motor_fault = On }, .pin = MOTOR_WARNING_PIN, .port = (void *)MOTOR_WARNING_PORT },
245 #else
246  { .function = Input_MotorWarning, .aux_port = 0xFF, .irq_mode = (pin_irq_mode_t)(IRQ_Mode_Rising|IRQ_Mode_Falling), .cap = { .motor_warning = On }, .pin = MOTOR_WARNING_PIN, .port = NULL },
247 #endif
248 #endif
249 #endif
250 #if I2C_STROBE_ENABLE && defined(I2C_STROBE_PIN) && defined(AUX_DEVICES)
251 #ifdef I2C_STROBE_PORT
252  { .function = Input_I2CStrobe, .aux_port = 0xFF, .irq_mode = (pin_irq_mode_t)(IRQ_Mode_Change), .cap = { .value = 0 }, .pin = I2C_STROBE_PIN, .port = (void *)I2C_STROBE_PORT },
253 #else
254  { .function = Input_I2CStrobe, .aux_port = 0xFF, .irq_mode = (pin_irq_mode_t)(IRQ_Mode_Change), .cap = { .value = 0 }, .pin = I2C_STROBE_PIN, .port = NULL },
255 #endif
256 #endif
257 #if MPG_ENABLE == 1 && defined(MPG_MODE_PIN) && defined(AUX_DEVICES)
258 #ifdef MPG_MODE_PORT
259  { .function = Input_MPGSelect, .aux_port = 0xFF, .irq_mode = (pin_irq_mode_t)(IRQ_Mode_Change), .cap = { .value = 0 }, .pin = MPG_MODE_PIN, .port = (void *)MPG_MODE_PORT },
260 #else
261  { .function = Input_MPGSelect, .aux_port = 0xFF, .irq_mode = (pin_irq_mode_t)(IRQ_Mode_Change), .cap = { .value = 0 }, .pin = MPG_MODE_PIN, .port = NULL },
262 #endif
263 #endif
264 #if QEI_SELECT_ENABLE && defined(QEI_SELECT_PIN) && defined(AUX_DEVICES)
265 #ifdef QEI_SELECT_PORT
266  { .function = Input_QEI_Select, .aux_port = 0xFF, .irq_mode = (pin_irq_mode_t)(IRQ_Mode_Rising|IRQ_Mode_Falling), .cap = { .value = 0 }, .pin = QEI_SELECT_PIN, .port = (void *)QEI_SELECT_PORT },
267 #else
268  { .function = Input_QEI_Select, .aux_port = 0xFF, .irq_mode = (pin_irq_mode_t)(IRQ_Mode_Rising|IRQ_Mode_Falling), .cap = { .value = 0 }, .pin = QEI_SELECT_PIN, .port = NULL },
269 #endif
270 #endif
271 // The following pins are allocated from remaining aux inputs pool
272 #if LIMITS_OVERRIDE_ENABLE
273  { .function = Input_LimitsOverride, .aux_port = 0xFF, .irq_mode = IRQ_Mode_None, .cap = { .limits_override = On }, .pin = 0xFF, .port = NULL },
274 #endif
275 #if STOP_DISABLE_ENABLE
276  { .function = Input_StopDisable, .aux_port = 0xFF, .irq_mode = IRQ_Mode_Change, .cap = { .stop_disable = On }, .pin = 0xFF, .port = NULL },
277 #endif
278 #if BLOCK_DELETE_ENABLE
279  { .function = Input_BlockDelete, .aux_port = 0xFF, .irq_mode = IRQ_Mode_Change, .cap = { .block_delete = On }, .pin = 0xFF, .port = NULL },
280 #endif
281 #if SINGLE_BLOCK_ENABLE
282  { .function = Input_SingleBlock, .aux_port = 0xFF, .irq_mode = IRQ_Mode_Change, .cap = { .single_block = On }, .pin = 0xFF, .port = NULL },
283 #endif
284 #if PROBE_DISCONNECT_ENABLE
285  { .function = Input_ProbeDisconnect, .aux_port = 0xFF, .irq_mode = (pin_irq_mode_t)(IRQ_Mode_Rising|IRQ_Mode_Falling), .cap = { .probe_disconnected = On }, .pin = 0xFF, .port = NULL },
286 #endif
287 };
288 
289 static inline aux_ctrl_t *aux_ctrl_remap_explicit (void *port, uint8_t pin, uint8_t aux_port, void *input)
290 {
291  aux_ctrl_t *ctrl_pin = NULL;
292 
293  uint_fast8_t idx = sizeof(aux_ctrl) / sizeof(aux_ctrl_t);
294 
295  do {
296  idx--;
297  if(aux_ctrl[idx].port == port && aux_ctrl[idx].pin == pin) {
298  ctrl_pin = &aux_ctrl[idx];
299  ctrl_pin->aux_port = aux_port;
300  ctrl_pin->input = input;
301  }
302  } while(idx && ctrl_pin == NULL);
303 
304  return ctrl_pin;
305 }
306 
307 static inline aux_ctrl_t *aux_ctrl_get_pin (uint8_t aux_port)
308 {
309  aux_ctrl_t *ctrl_pin = NULL;
310 
311  uint_fast8_t idx = sizeof(aux_ctrl) / sizeof(aux_ctrl_t);
312 
313  do {
314  if(aux_ctrl[--idx].aux_port == aux_port)
315  ctrl_pin = &aux_ctrl[idx];
316  } while(idx && ctrl_pin == NULL);
317 
318  return ctrl_pin;
319 }
320 
321 static inline void aux_ctrl_irq_enable (settings_t *settings, ioport_interrupt_callback_ptr aux_irq_handler)
322 {
323  uint_fast8_t idx = sizeof(aux_ctrl) / sizeof(aux_ctrl_t);
324 
325  if(idx) do {
326  if(aux_ctrl[--idx].aux_port != 0xFF) {
327 #if PROBE_ENABLE && defined(PROBE_PIN) && defined(AUX_DEVICES)
328  if(aux_ctrl[idx].function == Input_Probe) {
329  xbar_t *xbar;
330  if((xbar = hal.port.get_pin_info(Port_Digital, Port_Input, aux_ctrl[idx].aux_port))) {
331  gpio_in_config_t cfg;
333  cfg.debounce = xbar->mode.debounce;
335  xbar->config(xbar, &cfg, false);
336  }
337  } else
338 #endif
339  if(aux_ctrl[idx].irq_mode != IRQ_Mode_None) {
340  if(aux_ctrl[idx].irq_mode & (IRQ_Mode_Falling|IRQ_Mode_Rising))
341  aux_ctrl[idx].irq_mode = (settings->control_invert.mask & aux_ctrl[idx].cap.mask) ? IRQ_Mode_Falling : IRQ_Mode_Rising;
342  hal.port.register_interrupt_handler(aux_ctrl[idx].aux_port, aux_ctrl[idx].irq_mode, aux_irq_handler);
343  }
344  }
345  } while(idx);
346 }
347 
348 typedef bool (*aux_claim_explicit_ptr)(aux_ctrl_t *aux_ctrl);
349 
350 static bool aux_ctrl_claim_port (xbar_t *properties, uint8_t port, void *data)
351 {
352  if(ioport_claim(Port_Digital, Port_Input, &port, xbar_fn_to_pinname(((aux_ctrl_t *)data)->function)))
353  ((aux_ctrl_t *)data)->aux_port = port;
354 
355  return ((aux_ctrl_t *)data)->aux_port != 0xFF;
356 }
357 
358 static inline void aux_ctrl_claim_ports (aux_claim_explicit_ptr aux_claim_explicit, ioports_enumerate_callback_ptr aux_claim)
359 {
360  uint_fast8_t idx;
361 
362  if(aux_claim == NULL)
363  aux_claim = aux_ctrl_claim_port;
364 
365  for(idx = 0; idx < sizeof(aux_ctrl) / sizeof(aux_ctrl_t); idx++) {
366  if(aux_ctrl[idx].pin == 0xFF) {
367  if(ioports_enumerate(Port_Digital, Port_Input, (pin_cap_t){ .irq_mode = aux_ctrl[idx].irq_mode, .claimable = On }, aux_claim, (void *)&aux_ctrl[idx]))
368  hal.signals_cap.mask |= aux_ctrl[idx].cap.mask;
369  } else if(aux_ctrl[idx].aux_port != 0xFF)
370  aux_claim_explicit(&aux_ctrl[idx]);
371  }
372 }
373 
374 #if AUX_CONTROLS_SCAN
375 
376 static inline control_signals_t aux_ctrl_scan_status (control_signals_t signals)
377 {
378  uint_fast8_t idx = sizeof(aux_ctrl) / sizeof(aux_ctrl_t);
379 
380  if(idx) do {
381  if(aux_ctrl[--idx].pin != 0xFF)
382  break;
383  if(aux_ctrl[idx].aux_port != 0xFF) {
384  signals.mask &= ~aux_ctrl[idx].cap.mask;
385 #ifdef GRBL_ESP32 // Snowflake guru workaround
386  if(hal.port.wait_on_input(Port_Digital, aux_ctrl[idx].aux_port, WaitMode_Immediate, FZERO) == 1)
387  signals.mask |= aux_ctrl[idx].cap.mask;
388 #else
389  if(hal.port.wait_on_input(Port_Digital, aux_ctrl[idx].aux_port, WaitMode_Immediate, 0.0f) == 1)
390  signals.mask |= aux_ctrl[idx].cap.mask;
391 #endif
392  }
393  } while(idx);
394 
395  return signals;
396 }
397 
398 #endif
399 
400 #else
401 #define AUX_CONTROLS_ENABLED 0
402 #define AUX_CONTROLS_SCAN 0
403 #endif
404 
405 #if defined(AUX_CONTROLS_OUT) && !defined(AUX_CONTROLS)
406 #define AUX_CONTROLS AUX_CONTROL_SPINDLE
407 #elif !defined(AUX_CONTROLS)
408 #define AUX_CONTROLS 0
409 #endif
410 
411 #if AUX_CONTROLS
412 
413 // The following pins are bound explicitly to aux output pins
414 static aux_ctrl_out_t aux_ctrl_out[] = {
415 #if defined(ESP_PLATFORM) || defined(RP2040) // for now
416 #if defined(STEPPERS_ENABLE_PIN) && STEPPERS_ENABLE_PORT == EXPANDER_PORT
417  { .function = Output_StepperEnable, .aux_port = 0xFF, .pin = STEPPERS_ENABLE_PIN, .port = (void *)STEPPERS_ENABLE_PORT },
418 #endif
419 #if defined(X_ENABLE_PIN) && X_ENABLE_PORT == EXPANDER_PORT
420  { .function = Output_StepperEnableX, .aux_port = 0xFF, .pin = X_ENABLE_PIN, .port = (void *)X_ENABLE_PORT },
421 #endif
422 #if defined(Y_ENABLE_PIN) && Y_ENABLE_PORT == EXPANDER_PORT
423  { .function = Output_StepperEnableY, .aux_port = 0xFF, .pin = Y_ENABLE_PIN, .port = (void *)Y_ENABLE_PORT },
424 #endif
425 #if defined(XY_ENABLE_PIN) && XY_ENABLE_PORT == EXPANDER_PORT
426  { .function = Output_StepperEnableXY, .aux_port = 0xFF, .pin = XY_ENABLE_PIN, .port = (void *)XY_ENABLE_PORT },
427 #endif
428 #if defined(Z_ENABLE_PIN) && Z_ENABLE_PORT == EXPANDER_PORT
429  { .function = Output_StepperEnableZ, .aux_port = 0xFF, .pin = Z_ENABLE_PIN, .port = (void *)Z_ENABLE_PORT },
430 #endif
431 #endif
432 #if AUX_CONTROLS & AUX_CONTROL_SPINDLE
433 #ifdef SPINDLE_ENABLE_PIN
434  #ifndef SPINDLE_ENABLE_PORT
435  #define SPINDLE_ENABLE_PORT 0
436  #endif
437  { .function = Output_SpindleOn, .aux_port = 0xFF, .pin = SPINDLE_ENABLE_PIN, .port = (void *)SPINDLE_ENABLE_PORT },
438 #endif
439 #ifdef SPINDLE_PWM_PIN
440  #ifndef SPINDLE_PWM_PORT
441  #define SPINDLE_PWM_PORT 0
442  #endif
443  { .function = Output_SpindlePWM, .aux_port = 0xFF, .pin = SPINDLE_PWM_PIN, .port = (void *)SPINDLE_PWM_PORT },
444 #endif
445 #ifdef SPINDLE_DIRECTION_PIN
446  #ifndef SPINDLE_DIRECTION_PORT
447  #define SPINDLE_DIRECTION_PORT 0
448  #endif
449  { .function = Output_SpindleDir, .aux_port = 0xFF, .pin = SPINDLE_DIRECTION_PIN, .port = (void *)SPINDLE_DIRECTION_PORT },
450 #endif
451 
452 #ifdef SPINDLE1_ENABLE_PIN
453  #ifndef SPINDLE1_ENABLE_PORT
454  #define SPINDLE1_ENABLE_PORT 0
455  #endif
456  { .function = Output_Spindle1On, .aux_port = 0xFF, .pin = SPINDLE1_ENABLE_PIN, .port = (void *)SPINDLE1_ENABLE_PORT },
457 #endif
458 #ifdef SPINDLE1_PWM_PIN
459  #ifndef SPINDLE1_PWM_PORT
460  #define SPINDLE1_PWM_PORT 0
461  #endif
462  { .function = Output_Spindle1PWM, .aux_port = 0xFF, .pin = SPINDLE1_PWM_PIN, .port = (void *)SPINDLE1_PWM_PORT },
463 #endif
464 #ifdef SPINDLE1_DIRECTION_PIN
465  #ifndef SPINDLE1_DIRECTION_PORT
466  #define SPINDLE1_DIRECTION_PORT 0
467  #endif
468  { .function = Output_Spindle1Dir, .aux_port = 0xFF, .pin = SPINDLE1_DIRECTION_PIN, .port = (void *)SPINDLE1_DIRECTION_PORT },
469 #endif
470 #endif // SPINDLES
471 
472 #if AUX_CONTROLS & AUX_CONTROL_COOLANT
473 #ifdef COOLANT_FLOOD_PIN
474  #ifndef COOLANT_FLOOD_PORT
475  #define COOLANT_FLOOD_PORT 0
476  #endif
477  { .function = Output_CoolantFlood, .aux_port = 0xFF, .pin = COOLANT_FLOOD_PIN, .port = (void *)COOLANT_FLOOD_PORT },
478 #endif
479 #ifdef COOLANT_MIST_PIN
480  #ifndef COOLANT_MIST_PORT
481  #define COOLANT_MIST_PORT 0
482  #endif
483  { .function = Output_CoolantMist, .aux_port = 0xFF, .pin = COOLANT_MIST_PIN, .port = (void *)COOLANT_MIST_PORT },
484 #endif
485 #endif // COOLANT
486 
487 #ifdef COPROC_RESET_PIN
488  #ifndef COPROC_RESET_PORT
489  #define COPROC_RESET_PORT 0
490  #endif
491  { .function = Output_CoProc_Reset, .aux_port = 0xFF, .pin = COPROC_RESET_PIN, .port = (void *)COPROC_RESET_PORT },
492 #endif
493 #ifdef COPROC_BOOT0_PIN
494  #ifndef COPROC_BOOT0_PORT
495  #define COPROC_BOOT0_PORT 0
496  #endif
497  { .function = Output_CoProc_Boot0, .aux_port = 0xFF, .pin = COPROC_BOOT0_PIN, .port = (void *)COPROC_BOOT0_PORT },
498 #endif
499 #if defined(SPI_RST_PIN) && SPI_RST_PORT == EXPANDER_PORT && defined(RP2040)
500  { .function = Output_SPIRST, .aux_port = 0xFF, .pin = SPI_RST_PIN, .port = (void *)SPI_RST_PORT },
501 #endif
502 };
503 
504 static inline aux_ctrl_out_t *aux_out_remap_explicit (void *port, uint8_t pin, uint8_t aux_port, void *output)
505 {
506  aux_ctrl_out_t *ctrl_pin = NULL;
507 
508  uint_fast8_t idx = sizeof(aux_ctrl_out) / sizeof(aux_ctrl_out_t);
509 
510  if(idx) do {
511  idx--;
512  if(aux_ctrl_out[idx].port == port && aux_ctrl_out[idx].pin == pin) {
513  ctrl_pin = &aux_ctrl_out[idx];
514  ctrl_pin->aux_port = aux_port;
515  ctrl_pin->output = output;
516  }
517  } while(idx && ctrl_pin == NULL);
518 
519  return ctrl_pin;
520 }
521 
522 typedef bool (*aux_claim_explicit_out_ptr)(aux_ctrl_out_t *aux_ctrl);
523 
524 static bool aux_ctrl_claim_out_port (xbar_t *properties, uint8_t port, void *data)
525 {
526  if(((aux_ctrl_out_t *)data)->port == (void *)EXPANDER_PORT) {
527  if(((aux_ctrl_out_t *)data)->pin == properties->pin && properties->set_value)
528  ((aux_ctrl_out_t *)data)->aux_port = port;
529  } else if(ioport_claim(Port_Digital, Port_Output, &port, xbar_fn_to_pinname(((aux_ctrl_out_t *)data)->function)))
530  ((aux_ctrl_out_t *)data)->aux_port = port;
531 
532  return ((aux_ctrl_out_t *)data)->aux_port != 0xFF;
533 }
534 
535 static inline void aux_ctrl_claim_out_ports (aux_claim_explicit_out_ptr aux_claim_explicit, ioports_enumerate_callback_ptr aux_claim)
536 {
537  uint_fast8_t idx;
538 
539  if(aux_claim == NULL)
540  aux_claim = aux_ctrl_claim_out_port;
541 
542  for(idx = 0; idx < sizeof(aux_ctrl_out) / sizeof(aux_ctrl_out_t); idx++) {
543  if(aux_ctrl_out[idx].port == (void *)EXPANDER_PORT) {
544  if(ioports_enumerate(Port_Digital, Port_Output, (pin_cap_t){ .external = On, .claimable = On }, aux_claim, &aux_ctrl_out[idx])) {
545  if((aux_ctrl_out[idx].output = ioport_claim(Port_Digital, Port_Output, &aux_ctrl_out[idx].aux_port, NULL /*xbar_fn_to_pinname(aux_ctrl_out[idx].function)*/))) {
546  ioport_set_function((xbar_t *)aux_ctrl_out[idx].output, aux_ctrl_out[idx].function, NULL);
547  // TODO: else set description?
548  aux_claim_explicit(&aux_ctrl_out[idx]);
549  }
550  }
551  } else if(aux_ctrl_out[idx].pin == 0xFF) {
552  if(ioports_enumerate(Port_Digital, Port_Output, (pin_cap_t){ .claimable = On }, aux_claim, &aux_ctrl_out[idx]))
553  aux_claim_explicit(&aux_ctrl_out[idx]);
554  } else if(aux_ctrl_out[idx].aux_port != 0xFF)
555  aux_claim_explicit(&aux_ctrl_out[idx]);
556  }
557 }
558 
559 #endif // AUX_CONTROLS
560 
561 //
562 
563 #ifndef CONTROL_MASK
564 #if SAFETY_DOOR_ENABLE
565 #define CONTROL_MASK (RESET_BIT|FEED_HOLD_BIT|CYCLE_START_BIT|ESTOP_BIT|PROBE_DISCONNECT_BIT|STOP_DISABLE_BIT|BLOCK_DELETE_BIT|SINGLE_BLOCK_BIT|MOTOR_FAULT_BIT|MOTOR_WARNING_BIT|LIMITS_OVERRIDE_BIT|SAFETY_DOOR_BIT)
566 #define CONTROL_MASK_SUM (RESET_BIT+FEED_HOLD_BIT+CYCLE_START_BIT+ESTOP_BIT+PROBE_DISCONNECT_BIT+STOP_DISABLE_BIT+BLOCK_DELETE_BIT+SINGLE_BLOCK_BIT+MOTOR_FAULT_BIT+MOTOR_WARNING_BIT+LIMITS_OVERRIDE_BIT+SAFETY_DOOR_BIT)
567 #else
568 #define CONTROL_MASK (RESET_BIT|FEED_HOLD_BIT|CYCLE_START_BIT|ESTOP_BIT|PROBE_DISCONNECT_BIT|STOP_DISABLE_BIT|BLOCK_DELETE_BIT|SINGLE_BLOCK_BIT|MOTOR_FAULT_BIT|MOTOR_WARNING_BIT|LIMITS_OVERRIDE_BIT)
569 #define CONTROL_MASK_SUM (RESET_BIT+FEED_HOLD_BIT+CYCLE_START_BIT+ESTOP_BIT+PROBE_DISCONNECT_BIT+STOP_DISABLE_BIT+BLOCK_DELETE_BIT+SINGLE_BLOCK_BIT+MOTOR_FAULT_BIT+MOTOR_WARNING_BIT+LIMITS_OVERRIDE_BIT)
570 #endif
571 #endif
572 
573 // Output Signals
574 
575 #if defined(SPINDLE_ENABLE_PIN) && !defined(SPINDLE_ENABLE_BIT)
576 #define SPINDLE_ENABLE_BIT (1<<SPINDLE_ENABLE_PIN)
577 #endif
578 #if defined(SPINDLE_DIRECTION_PIN) && !defined(SPINDLE_DIRECTION_BIT)
579 #define SPINDLE_DIRECTION_BIT (1<<SPINDLE_DIRECTION_PIN)
580 #endif
581 
582 #if defined(SPINDLE1_ENABLE_PIN) && !defined(SPINDLE1_ENABLE_BIT)
583 #define SPINDLE1_ENABLE_BIT (1<<SPINDLE1_ENABLE_PIN)
584 #endif
585 #if defined(SPINDLE1_DIRECTION_PIN) && !defined(SPINDLE1_DIRECTION_BIT)
586 #define SPINDLE1_DIRECTION_BIT (1<<SPINDLE1_DIRECTION_PIN)
587 #endif
588 
589 #if defined(COOLANT_FLOOD_PIN) && !defined(COOLANT_FLOOD_BIT)
590 #define COOLANT_FLOOD_BIT (1<<COOLANT_FLOOD_PIN)
591 #endif
592 #if defined(COOLANT_MIST_PIN) && !defined(COOLANT_MIST_BIT)
593 #define COOLANT_MIST_BIT (1<<COOLANT_MIST_PIN)
594 #endif
595 
596 #if defined(RTS_PIN) && !defined(RTS_BIT)
597 #define RTS_BIT (1<<RTS_PIN)
598 #endif
599 
600 // IRQ enabled input singnals
601 
602 #ifndef AUX_DEVICES
603 
604 // IRQ capability for the probe input is optional
605 #if defined(PROBE_PIN) && !defined(PROBE_BIT)
606 #define PROBE_BIT (1<<PROBE_PIN)
607 #endif
608 
609 #if defined(MPG_MODE_PIN) && !defined(MPG_MODE_BIT)
610 #define MPG_MODE_BIT (1<<MPG_MODE_PIN)
611 #endif
612 
613 #if defined(I2C_STROBE_PIN) && !defined(I2C_STROBE_BIT)
614 #define I2C_STROBE_BIT (1<<I2C_STROBE_PIN)
615 #endif
616 
617 #if defined(QEI_SELECT_PIN) && !defined(QEI_SELECT_BIT)
618 #define QEI_SELECT_BIT (1<<QEI_SELECT_PIN)
619 #endif
620 
621 #endif // !AUX_DEVICES
622 
623 #if QEI_ENABLE
624 #ifndef QEI_A_BIT
625 #define QEI_A_BIT (1<<QEI_A_PIN)
626 #endif
627 #ifndef QEI_B_BIT
628 #define QEI_B_BIT (1<<QEI_B_PIN)
629 #endif
630 #else
631 #define QEI_A_BIT 0
632 #define QEI_B_BIT 0
633 #endif
634 
635 #ifndef QEI_SELECT_BIT
636 #define QEI_SELECT_BIT 0
637 #endif
638 #ifndef MPG_MODE_BIT
639 #define MPG_MODE_BIT 0
640 #endif
641 #ifndef I2C_STROBE_BIT
642 #define I2C_STROBE_BIT 0
643 #endif
644 
645 // Do NOT #define PROBE_BIT 0 here!
646 
647 #if SPINDLE_ENCODER_ENABLE
648 #ifndef SPINDLE_PULSE_PIN
649 #error "Spindle encoder requires at least SPINDLE_PULSE_PIN defined in the board map!"
650 #endif
651 #if !defined(SPINDLE_PULSE_BIT) && defined(SPINDLE_PULSE_PIN)
652 #define SPINDLE_PULSE_BIT (1<<SPINDLE_PULSE_PIN)
653 #endif
654 #if !defined(SPINDLE_INDEX_BIT) && defined(SPINDLE_INDEX_PIN)
655 #define SPINDLE_INDEX_BIT (1<<SPINDLE_INDEX_PIN)
656 #endif
657 #endif
658 
659 #ifndef SPINDLE_INDEX_BIT
660 #define SPINDLE_INDEX_BIT 0
661 #endif
662 #ifndef SPINDLE_PULSE_BIT
663 #define SPINDLE_PULSE_BIT 0
664 #endif
665 
666 #if SPINDLE_SYNC_ENABLE && (SPINDLE_INDEX_BIT + SPINDLE_PULSE_BIT) == 0
667 #error "Spindle sync requires SPINDLE_PULSE_PIN and SPINDLE_INDEX_PIN defined in the board map!"
668 #endif
669 
670 #ifndef SPI_IRQ_PIN
671 #define SPI_IRQ_BIT 0
672 #elif !defined(SPI_IRQ_BIT)
673 #define SPI_IRQ_BIT (1<<SPI_IRQ_PIN)
674 #endif
675 
676 #ifndef DEVICES_IRQ_MASK
677 #ifdef AUX_DEVICES
678 #define DEVICES_IRQ_MASK (SPI_IRQ_BIT|SPINDLE_INDEX_BIT|QEI_A_BIT|QEI_B_BIT|SD_DETECT_BIT)
679 #define DEVICES_IRQ_MASK_SUM (SPI_IRQ_BIT+SPINDLE_INDEX_BIT+QEI_A_BIT+QEI_B_BIT+SD_DETECT_BIT)
680 #else
681 #define DEVICES_IRQ_MASK (MPG_MODE_BIT|I2C_STROBE_BIT|QEI_SELECT_BIT|SPI_IRQ_BIT|SPINDLE_INDEX_BIT|QEI_A_BIT|QEI_B_BIT|SD_DETECT_BIT)
682 #define DEVICES_IRQ_MASK_SUM (MPG_MODE_BIT+I2C_STROBE_BIT+QEI_SELECT_BIT+SPI_IRQ_BIT+SPINDLE_INDEX_BIT+QEI_A_BIT+QEI_B_BIT+SD_DETECT_BIT)
683 #endif
684 #endif
685 
686 // Auxillary input signals
687 
688 #ifdef AUXINPUT0_PIN
689 #define AUXINPUT0_BIT (1<<AUXINPUT0_PIN)
690 #else
691 #define AUXINPUT0_BIT 0
692 #endif
693 #ifdef AUXINPUT1_PIN
694 #define AUXINPUT1_BIT (1<<AUXINPUT1_PIN)
695 #else
696 #define AUXINPUT1_BIT 0
697 #endif
698 #ifdef AUXINPUT2_PIN
699 #define AUXINPUT2_BIT (1<<AUXINPUT2_PIN)
700 #else
701 #define AUXINPUT2_BIT 0
702 #endif
703 #ifdef AUXINPUT3_PIN
704 #define AUXINPUT3_BIT (1<<AUXINPUT3_PIN)
705 #else
706 #define AUXINPUT3_BIT 0
707 #endif
708 #ifdef AUXINPUT4_PIN
709 #define AUXINPUT4_BIT (1<<AUXINPUT4_PIN)
710 #else
711 #define AUXINPUT4_BIT 0
712 #endif
713 #ifdef AUXINPUT5_PIN
714 #define AUXINPUT5_BIT (1<<AUXINPUT5_PIN)
715 #else
716 #define AUXINPUT5_BIT 0
717 #endif
718 #ifdef AUXINPUT6_PIN
719 #define AUXINPUT6_BIT (1<<AUXINPUT6_PIN)
720 #else
721 #define AUXINPUT6_BIT 0
722 #endif
723 #ifdef AUXINPUT7_PIN
724 #define AUXINPUT7_BIT (1<<AUXINPUT7_PIN)
725 #else
726 #define AUXINPUT7_BIT 0
727 #endif
728 #ifdef AUXINPUT8_PIN
729 #define AUXINPUT8_BIT (1<<AUXINPUT8_PIN)
730 #else
731 #define AUXINPUT8_BIT 0
732 #endif
733 #ifdef AUXINPUT9_PIN
734 #define AUXINPUT9_BIT (1<<AUXINPUT9_PIN)
735 #else
736 #define AUXINPUT9_BIT 0
737 #endif
738 #ifdef AUXINPUT10_PIN
739 #define AUXINPUT10_BIT (1<<AUXINPUT10_PIN)
740 #else
741 #define AUXINPUT10_BIT 0
742 #endif
743 #ifdef AUXINPUT11_PIN
744 #define AUXINPUT11_BIT (1<<AUXINPUT11_PIN)
745 #else
746 #define AUXINPUT11_BIT 0
747 #endif
748 #ifdef AUXINPUT12_PIN
749 #define AUXINPUT12_BIT (1<<AUXINPUT12_PIN)
750 #else
751 #define AUXINPUT12_BIT 0
752 #endif
753 #ifdef AUXINPUT13_PIN
754 #define AUXINPUT13_BIT (1<<AUXINPUT13_PIN)
755 #else
756 #define AUXINPUT13_BIT 0
757 #endif
758 #ifdef AUXINPUT14_PIN
759 #define AUXINPUT14_BIT (1<<AUXINPUT14_PIN)
760 #else
761 #define AUXINPUT14_BIT 0
762 #endif
763 #ifdef AUXINPUT15_PIN
764 #define AUXINPUT15_BIT (1<<AUXINPUT15_PIN)
765 #else
766 #define AUXINPUT15_BIT 0
767 #endif
768 
769 #ifndef AUXINPUT_MASK
770 #define AUXINPUT_MASK (AUXINPUT0_BIT|AUXINPUT1_BIT|AUXINPUT2_BIT|AUXINPUT3_BIT|AUXINPUT4_BIT|AUXINPUT5_BIT|AUXINPUT6_BIT|AUXINPUT7_BIT|\
771  AUXINPUT8_BIT|AUXINPUT9_BIT|AUXINPUT10_BIT|AUXINPUT11_BIT|AUXINPUT12_BIT|AUXINPUT13_BIT|AUXINPUT4_BIT|AUXINPUT15_BIT)
772 #define AUXINPUT_MASK_SUM (AUXINPUT0_BIT+AUXINPUT1_BIT+AUXINPUT2_BIT+AUXINPUT3_BIT+AUXINPUT4_BIT+AUXINPUT5_BIT+AUXINPUT6_BIT+AUXINPUT7_BIT+\
773  AUXINPUT8_BIT+AUXINPUT9_BIT+AUXINPUT10_BIT+AUXINPUT11_BIT+AUXINPUT12_BIT+AUXINPUT13_BIT+AUXINPUT4_BIT+AUXINPUT15_BIT)
774 #endif
775 
776 /*EOF*/
const char * xbar_fn_to_pinname(pin_function_t fn)
Definition: crossbar.c:142
@ PullMode_None
0b00 (0x00)
Definition: crossbar.h:583
@ PullMode_Up
0b01 (0x01)
Definition: crossbar.h:584
@ Output_CoProc_Boot0
Definition: crossbar.h:235
@ Input_Probe
Definition: crossbar.h:43
@ Output_StepperEnableX
Definition: crossbar.h:171
@ Input_MotorFault
Definition: crossbar.h:37
@ Input_MotorWarning
Definition: crossbar.h:38
@ Input_ProbeDisconnect
Definition: crossbar.h:36
@ Input_StopDisable
Definition: crossbar.h:34
@ Output_StepperEnableXY
Definition: crossbar.h:182
@ Input_LimitsOverride
Definition: crossbar.h:39
@ Output_CoProc_Reset
Definition: crossbar.h:234
@ Output_StepperEnableZ
Definition: crossbar.h:175
@ Input_QEI_Select
Definition: crossbar.h:258
@ Input_BlockDelete
Definition: crossbar.h:33
@ Output_StepperEnable
Definition: crossbar.h:169
@ Output_SpindleDir
Definition: crossbar.h:185
@ Output_StepperEnableY
Definition: crossbar.h:173
@ Input_SingleBlock
Definition: crossbar.h:40
@ Output_SPIRST
Definition: crossbar.h:245
@ Output_SpindleOn
Definition: crossbar.h:184
@ Input_I2CStrobe
Definition: crossbar.h:252
@ Output_Spindle1Dir
Definition: crossbar.h:188
@ Output_SpindlePWM
Definition: crossbar.h:186
@ Output_CoolantMist
Definition: crossbar.h:190
@ Input_MPGSelect
Definition: crossbar.h:58
@ Output_Spindle1On
Definition: crossbar.h:187
@ Input_SafetyDoor
Definition: crossbar.h:32
@ Output_CoolantFlood
Definition: crossbar.h:191
@ Output_Spindle1PWM
Definition: crossbar.h:189
pin_irq_mode_t
Pin interrupt modes, may be or'ed when reporting pin capability.
Definition: crossbar.h:556
@ IRQ_Mode_Rising
0b00001 (0x01)
Definition: crossbar.h:558
@ IRQ_Mode_None
0b00000 (0x00)
Definition: crossbar.h:557
@ IRQ_Mode_Change
0b00100 (0x04)
Definition: crossbar.h:561
@ IRQ_Mode_Falling
0b00010 (0x02)
Definition: crossbar.h:559
@ WaitMode_Immediate
0 - This is the only mode allowed for analog inputs
Definition: gcode.h:288
DCRAM grbl_hal_t hal
Global HAL struct.
Definition: grbllib.c:91
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:284
bool ioport_set_function(xbar_t *pin, pin_function_t function, driver_caps_t caps)
Set pin function.
Definition: ioports.c:345
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:415
@ 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
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
bool(* aux_claim_explicit_ptr)(aux_ctrl_t *aux_ctrl)
Definition: pin_bits_masks.h:348
#define EXPANDER_PORT
Definition: pin_bits_masks.h:54
settings_t settings
Definition: settings.c:43
Definition: crossbar.h:733
uint8_t aux_port
Definition: crossbar.h:735
void * output
Definition: crossbar.h:738
pin_function_t function
Definition: crossbar.h:734
Definition: crossbar.h:723
uint8_t aux_port
Definition: crossbar.h:725
control_signals_t cap
Definition: crossbar.h:727
pin_irq_mode_t irq_mode
Definition: crossbar.h:726
void * input
Definition: crossbar.h:730
pin_function_t function
Definition: crossbar.h:724
/a cfg_data argument to /a xbar_config_ptr for gpio input pins
Definition: crossbar.h:683
bool debounce
Definition: crossbar.h:685
bool inverted
Definition: crossbar.h:684
pull_mode_t pull_mode
Definition: crossbar.h:686
io_port_t port
Optional handlers for axuillary I/O (adds support for M62-M66).
Definition: hal.h:634
control_signals_t signals_cap
Control input signals supported by the driver.
Definition: hal.h:669
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
get_pin_info_ptr get_pin_info
Optional handler for getting information about an auxiliary pin.
Definition: ioports.h:173
Definition: settings.h:853
control_signals_t control_invert
Definition: settings.h:865
probeflags_t probe
Definition: settings.h:876
Definition: crossbar.h:741
pin_mode_t mode
Current pin configuration.
Definition: crossbar.h:750
xbar_config_ptr config
Optional pointer to function for configuring the port.
Definition: crossbar.h:751
xbar_set_value_ptr set_value
Optional pointer to function to set port value.
Definition: crossbar.h:753
uint_fast8_t pin
Pin number.
Definition: crossbar.h:748
Definition: system.h:162
uint16_t mask
Definition: system.h:164
Definition: crossbar.h:614
uint16_t debounce
Definition: crossbar.h:648
uint16_t invert_probe_pin
Definition: settings.h:602
uint16_t disable_probe_pullup
Definition: settings.h:603