forked from Expatria-Technologies/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathioports.c
More file actions
1633 lines (1284 loc) · 56.2 KB
/
ioports.c
File metadata and controls
1633 lines (1284 loc) · 56.2 KB
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
ioports.c - some wrapper functions for the ioports HAL API
Part of grblHAL
Copyright (c) 2021-2026 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/>.
*/
/**
* @file
*
* Some wrapper functions for the #io_port_t API.
* They perform the necessary checks for both availability of ports
* and advanced functionality simplifying plugin code that uses them.
*/
#include <math.h>
#include "hal.h"
#include "settings.h"
#define MAX_PORTS (Output_AuxMax - Output_Aux0 + 1)
typedef enum {
Port_AnalogIn = 0,
Port_AnalogOut,
Port_DigitalIn,
Port_DigitalOut
} ioport_type_xxx_t;
struct ioports_handle {
ioport_type_xxx_t type;
io_ports_detail_t *ports;
const char *pnum;
char port_names[8 * 6 + (MAX_PORTS - 8) * 7];
ioport_bus_t enabled;
ioport_bus_t claimed;
uint8_t last_claimed;
int16_t count;
int16_t free;
int16_t n_ports;
int16_t n_max;
pin_function_t min_fn;
pin_function_t max_fn;
uint8_t map[MAX_PORTS];
ioport_bus_t bus;
};
typedef struct ioports_handle io_ports_private_t;
typedef struct {
digital_out_ptr digital_out; //!< Optional handler for setting a digital output.
analog_out_ptr analog_out; //!< Optional handler for setting an analog output.
ll_wait_on_input_ptr wait_on_input; //!< Optional handler for reading a digital or analog input.
ll_set_pin_description_ptr set_pin_description; //!< Optional handler for setting a description of an auxiliary pin.
ll_get_pin_info_ptr get_pin_info; //!< Optional handler for getting information about an auxiliary pin.
ll_claim_port_ptr claim; //!< Optional handler for claiming an auxiliary pin for exclusive use.
ll_ioport_register_interrupt_handler_ptr register_interrupt_handler;
} ll_io_port_t;
typedef struct io_ports_list_t {
io_port_type_t type;
ll_io_port_t hal;
io_ports_data_t *ports_id;
struct io_ports_list_t *next;
} io_ports_list_t;
static io_ports_list_t *ports = NULL;
static driver_settings_load_ptr on_settings_loaded = NULL;
static setting_changed_ptr on_setting_changed = NULL;
static on_settings_changed_ptr on_settings_changed;
static io_ports_private_t ports_cfg[] = {
{
.type = Port_AnalogIn, .count = -1, .free = -1, .min_fn = Input_Analog_Aux0, .max_fn = Input_Analog_AuxMax,
.n_max = N_AUX_AIN_MAX, .last_claimed = N_AUX_AIN_MAX - 1
},
{
.type = Port_AnalogOut, .count = -1, .free = -1, .min_fn = Output_Analog_Aux0, .max_fn = Output_Analog_AuxMax,
.n_max = N_AUX_AOUT_MAX, .last_claimed = N_AUX_AOUT_MAX - 1
},
{
.type = Port_DigitalIn, .count = -1, .free = -1, .min_fn = Input_Aux0, .max_fn = Input_AuxMax,
.n_max = N_AUX_DIN_MAX, .last_claimed = N_AUX_DIN_MAX - 1
},
{
.type = Port_DigitalOut, .count = -1, .free = -1, .min_fn = Output_Aux0, .max_fn = Output_AuxMax,
.n_max = N_AUX_DOUT_MAX, .last_claimed = N_AUX_DOUT_MAX - 1
}
};
PROGMEM static const char apnum[] = "E0\0E1\0E2\0E3\0E4\0E5\0E6\0E7"
#if N_AUX_AIN > 8 || N_AUX_AOUT > 8
"\0E8\0E9\0E10\0E11\0E12\0E13\0E14\0E15";
#endif
;
PROGMEM static const char dpnum[] = "P0\0P1\0P2\0P3\0P4\0P5\0P6\0P7\0P8\0P9\0P10\0P11\0P12\0P13\0P14\0P15"
#if N_AUX_DIN > 16 || N_AUX_DOUT > 16
"\0P16\0P17\0P18\0P19\0P20\0P21\0P22\0P23"
#endif
#if N_AUX_DIN > 24 || N_AUX_DOUT > 24
"\0P24\0P25\0P26\0P27\0P28\0P29\0P30\0P31"
#endif
;
__STATIC_FORCEINLINE io_ports_private_t *get_port_data (io_port_type_t type, io_port_direction_t dir)
{
return &ports_cfg[(type << 1) | dir];
}
static uint8_t map_reverse (io_ports_private_t *p_data, uint8_t port)
{
uint_fast8_t idx = p_data->n_max;
do {
if(p_data->map[--idx] == port) {
port = idx;
break;
}
} while(idx);
return port;
}
__STATIC_FORCEINLINE uint8_t is_aux (io_ports_private_t *p_data, pin_function_t function)
{
return function >= p_data->min_fn && function <= p_data->max_fn;
}
// TODO: change to always use ioports_map_reverse()? add range check?
__STATIC_FORCEINLINE uint8_t resolve_portnum (io_ports_private_t *p_data, xbar_t *port)
{
return is_aux(p_data, port->function) ? (port->function - p_data->min_fn) : map_reverse(p_data, port->id);
}
FLASHMEM static uint8_t ioports_count (io_port_type_t type, io_port_direction_t dir, io_ports_private_t *p_data)
{
xbar_t *port;
uint8_t n_ports = 0, n_remapped = 0;
// determine how many ports, including claimed ports, that are available. remapped ports may be excluded.
if(hal.port.get_pin_info) do {
if((port = hal.port.get_pin_info(type, dir, n_ports))) {
n_ports++;
if(p_data && (port->function < p_data->min_fn || port->function > p_data->max_fn))
n_remapped++;
}
} while(port != NULL);
return n_ports - n_remapped;
}
/*! \brief Get number of digital or analog ports available.
\param type as an \a #io_port_type_t enum value.
\param dir as an \a #io_port_direction_t enum value.
\returns number of ports available excluding remapped ports but including claimed ports if the API implementation supports that.
*/
FLASHMEM uint8_t ioports_available (io_port_type_t type, io_port_direction_t dir)
{
io_ports_private_t *p_data = get_port_data(type, dir);
if(p_data->count == -1)
p_data->count = ioports_count(type, dir, get_port_data(type, dir));
return p_data->count;
}
/*! \brief Get number of unclaimed digital or analog ports available.
\param type as an \a #io_port_type_t enum value.
\param dir as an \a #io_port_direction_t enum value.
\returns number of ports available.
*/
FLASHMEM uint8_t ioports_unclaimed (io_port_type_t type, io_port_direction_t dir)
{
io_ports_private_t *p_data = get_port_data(type, dir);
if(p_data->free == -1) {
xbar_t *port;
uint8_t idx = 0;
p_data->free = 0;
if(hal.port.get_pin_info) do {
if((port = hal.port.get_pin_info(type, dir, idx++)) && !port->mode.claimed)
p_data->free++;
} while(port);
}
return p_data->free;
}
struct ff_data {
uint8_t port;
uint32_t max_port;
const char *description;
};
FLASHMEM static bool match_port (xbar_t *properties, uint8_t port, void *data)
{
bool ok;
struct ff_data *ff_data = (struct ff_data *)data;
if((ok = properties->id <= ff_data->max_port))
ff_data->port = port;
return ok;
}
FLASHMEM static bool match_description (xbar_t *properties, uint8_t port, void *data)
{
bool ok;
struct ff_data *ff_data = (struct ff_data *)data;
if((ok = properties->description && !strcmp(properties->description, ff_data->description)))
ff_data->port = port;
return ok;
}
/*! \brief find claimable or claimed analog or digital port. Search starts from the last port number.
\param type as an \a #io_port_type_t enum value.
\param dir as an \a #io_port_direction_t enum value.
\param description pointer to a \a char constant for the pin description of a previousely claimed port
or a port number to be used as the upper limit for the search, or \a NULL if searching for the first free port.
\returns the port number if successful, 0xFF (255) if not.
*/
FLASHMEM uint8_t ioport_find_free (io_port_type_t type, io_port_direction_t dir, pin_cap_t filter, const char *description)
{
struct ff_data ff_data = { .port = IOPORT_UNASSIGNED, .max_port = IOPORT_UNASSIGNED + 1 };
if((ff_data.description = (description && *description) ? description : NULL)) {
uint_fast8_t pos = 0;
read_uint(ff_data.description, &pos, &ff_data.max_port);
if(ff_data.max_port <= IOPORT_UNASSIGNED)
ff_data.description = NULL;
}
if(ff_data.description)
ioports_enumerate(type, dir, (pin_cap_t){ .claimable = On }, match_description, (void *)&ff_data);
else
ioports_enumerate(type, dir, filter, match_port, (void *)&ff_data);
return ff_data.port;
}
/*! \brief Return information about a digital or analog port.
\param type as an \a #io_port_type_t enum value.
\param dir as an \a #io_port_direction_t enum value.
\param port the port aux number.
\returns pointer to \a xbar_t struct if successful, \a NULL if not.
*/
FLASHMEM static xbar_t *get_info (io_port_type_t type, io_port_direction_t dir, uint8_t port, bool claim)
{
bool ok = false;
xbar_t *portinfo = NULL;
io_ports_private_t *p_data = get_port_data(type, dir);
uint8_t n_ports = p_data->ports ? p_data->n_ports : 0;
if(hal.port.get_pin_info && n_ports) do {
ok = (portinfo = hal.port.get_pin_info(type, dir, --n_ports)) && !(claim && portinfo->mode.claimed) && resolve_portnum(p_data, portinfo) == port;
} while(n_ports && !ok);
return ok ? portinfo : NULL;
}
/*! \brief Return information about a digital or analog port.
\param type as an \a #io_port_type_t enum value.
\param dir as an \a #io_port_direction_t enum value.
\param port the claimed port aux number.
\returns pointer to \a xbar_t struct if successful, \a NULL if not.
*/
FLASHMEM xbar_t *ioport_get_info (io_port_type_t type, io_port_direction_t dir, uint8_t port)
{
return hal.port.get_pin_info(type, dir, port);
}
/* code to keep deprecated data updated, to be removed */
static inline uint8_t get_hcount (io_port_type_t type, io_port_direction_t dir)
{
return type == Port_Digital
? (dir == Port_Input ? hal.port.num_digital_in : hal.port.num_digital_out)
: (dir == Port_Input ? hal.port.num_analog_in : hal.port.num_analog_out);
}
static inline void dec_hcount (io_port_type_t type, io_port_direction_t dir)
{
if(type == Port_Digital) {
if(dir == Port_Input)
hal.port.num_digital_in--;
else
hal.port.num_digital_out--;
} else if(dir == Port_Input)
hal.port.num_analog_in--;
else
hal.port.num_analog_out--;
}
/* end deprecated */
/*! \brief Claim a digital or analog port for exclusive use.
\param type as an \a #io_port_type_t enum value.
\param dir as an \a #io_port_direction_t enum value.
\param port pointer to a \a uint8_t holding the ports aux number, returns the actual port number to use if successful.
\param description pointer to a \a char constant for the pin description.
\returns pointer to a \a #xbar_t structure with details about the claimed port if successful, \a NULL if not.
*/
FLASHMEM xbar_t *ioport_claim (io_port_type_t type, io_port_direction_t dir, uint8_t *port, const char *description)
{
xbar_t *portinfo = NULL;
if(hal.port.claim) {
uint8_t hcnt = get_hcount(type, dir);
if((portinfo = get_info(type, dir, *port, true)) &&
// portinfo->cap.claimable && TODO: add?
!portinfo->mode.claimed &&
(portinfo->mode.claimed = hal.port.claim(type, dir, port, description))) {
get_port_data(type, dir)->free = -1;
if(get_hcount(type, dir) == hcnt)
dec_hcount(type, dir);
} else {
portinfo = NULL;
*port = IOPORT_UNASSIGNED;
}
}
return portinfo;
}
/*! \brief Check if a analog or digital port is available for exclusive use.
\param type as an \a #io_port_type_t enum value.
\param dir as an \a #io_port_direction_t enum value.
\param port a \a uint8_t holding the ports aux number.
\returns \a TRUE if available, \a FALSE if not.
*/
FLASHMEM bool ioport_claimable (io_port_type_t type, io_port_direction_t dir, uint8_t port)
{
xbar_t *portinfo = port == IOPORT_UNASSIGNED ? NULL : hal.port.get_pin_info(type, dir, map_reverse(get_port_data(type, dir), port));
return port == IOPORT_UNASSIGNED || (portinfo && portinfo->cap.claimable);
}
// Deprecated
FLASHMEM void ioport_assign_function (aux_ctrl_t *aux_ctrl, pin_function_t *function)
{
xbar_t *input;
if((input = hal.port.get_pin_info(Port_Digital, Port_Input, aux_ctrl->port))) {
*function = aux_ctrl->function;
ports_cfg[Port_DigitalIn].bus.mask &= ~(1 << input->id);
ports_cfg[Port_DigitalIn].count = ports_cfg[Port_DigitalIn].free = -1;
hal.signals_cap.mask |= aux_ctrl->signal.mask;
if(aux_ctrl->function == Input_Probe || xbar_fn_to_signals_mask(aux_ctrl->function).mask)
setting_remove_elements(Settings_IoPort_InvertIn, ports_cfg[Port_DigitalIn].bus.mask, false);
}
}
// Deprecated
FLASHMEM void ioport_assign_out_function (aux_ctrl_out_t *aux_ctrl, pin_function_t *function)
{
xbar_t *output;
if((output = hal.port.get_pin_info(Port_Digital, Port_Output, aux_ctrl->port))) {
*function = aux_ctrl->function;
ports_cfg[Port_DigitalOut].bus.mask &= ~(1UL << output->id);
ports_cfg[Port_DigitalOut].count = ports_cfg[Port_DigitalOut].free = -1;
setting_remove_elements(Settings_IoPort_InvertOut, ports_cfg[Port_DigitalOut].bus.mask, false);
}
}
/*! \brief Set pin function.
\param port pointer to a \a xbar_t structure.
\param function a \a #pin_function_t enum value.
\param caps pointer to \a #driver_caps_t capability flags.
*/
FLASHMEM bool ioport_set_function (xbar_t *pin, pin_function_t function, driver_caps_t caps)
{
bool ok = false;
io_ports_list_t *io_port = ports;
io_ports_private_t *cfg = get_port_data((io_port_type_t)!pin->mode.analog, (io_port_direction_t)pin->mode.output);
if(io_port) do {
if(io_port->ports_id == pin->ports_id && (ok = pin->set_function && pin->set_function(pin, function))) {
cfg->bus.mask &= ~(1 << (pin->id + io_port->ports_id->cfg[pin->mode.output].n_start));
cfg->count = cfg->free = -1;
switch(cfg->type) {
case Port_DigitalIn:
if(caps.control)
hal.signals_cap.mask |= caps.control->mask;
if(xbar_is_probe_in(function) || xbar_is_motor_fault_in(function) || xbar_fn_to_signals_mask(function).mask)
setting_remove_elements(Settings_IoPort_InvertIn, cfg->bus.mask, false);
break;
case Port_DigitalOut:
switch(function) {
case Output_CoolantMist:
hal.coolant_cap.mist = On;
break;
case Output_CoolantFlood:
hal.coolant_cap.flood = On;
break;
default: break;
}
setting_remove_elements(Settings_IoPort_InvertOut, cfg->bus.mask, false);
break;
default: break;
}
}
} while(!ok && (io_port = io_port->next));
return ok;
}
/*! \brief Get basic ioports capabilities.
\returns a \a #io_port_cando_t union.
*/
FLASHMEM io_port_cando_t ioports_can_do (void)
{
io_port_cando_t can_do = {};
if(hal.port.get_pin_info) {
can_do.claim_explicit = !!hal.port.claim;
can_do.analog_out = !!hal.port.analog_out;
can_do.digital_out = !!hal.port.digital_out;
can_do.wait_on_input = !!hal.port.wait_on_input;
}
return can_do;
}
// Deprecated
FLASHMEM bool ioport_can_claim_explicit (void)
{
return ioports_can_do().claim_explicit;
}
//
// Some helper functions for plugins using ports
//
static float _get_value (io_port_cfg_t *p, uint8_t port)
{
return port > p->port_max ? -1.0f : (float)port;
}
static status_code_t _set_value (io_port_cfg_t *p, uint8_t *port, pin_cap_t caps, float value)
{
status_code_t status;
if((status = isintf(value) ? Status_OK : Status_BadNumberFormat) == Status_OK) {
if(value >= 0.0f) {
xbar_t *portinfo = hal.port.get_pin_info(p->handle->type >> 1, p->handle->type & 1, map_reverse(&ports_cfg[p->handle->type], (uint8_t)value));
if(portinfo == NULL || !portinfo->cap.claimable)
status = Status_AuxiliaryPortUnavailable;
else if(!(caps.mask == 0 || (portinfo->cap.mask & caps.mask)))
status = Status_AuxiliaryPortUnusable;
else
*port = (uint8_t)value;
} else
*port = IOPORT_UNASSIGNED;
}
return status;
}
uint8_t _get_next (io_port_cfg_t *p, uint8_t port, const char *description, pin_cap_t caps)
{
uint8_t px = IOPORT_UNASSIGNED;
caps.claimable = On;
if(description && *description)
px = ioport_find_free(p->handle->type >> 1, p->handle->type & 1, (pin_cap_t){ .claimable = On }, description);
if(px == IOPORT_UNASSIGNED && !(port == 0 && port == p->port_max))
px = ioport_find_free(p->handle->type >> 1, p->handle->type & 1, caps, uitoa(port == IOPORT_UNASSIGNED ? p->port_max : (port > p->port_max ? p->port_max : port) - 1));
return px;
}
FLASHMEM static xbar_t *_claim (io_port_cfg_t *p, uint8_t *port, const char *description, pin_cap_t caps)
{
xbar_t *portinfo = *port <= p->port_max ? hal.port.get_pin_info(p->handle->type >> 1, p->handle->type & 1, map_reverse(&ports_cfg[p->handle->type], *port)) : NULL;
if(!portinfo)
*port = IOPORT_UNASSIGNED;
return portinfo && !portinfo->mode.claimed && (caps.mask == 0 || (portinfo->cap.mask & caps.mask) == caps.mask) && ioport_claim(p->handle->type >> 1, p->handle->type & 1, port, description)
? portinfo
: NULL;
}
/*! \brief Get data and pointers to helper functions for managing ports and port settings.
\param pp a pointer to a \a io_port_cfg_t struct to hold the data and pointers.
\param type as an \a #io_port_type_t enum value.
\param dir as an \a #io_port_direction_t enum value.
\returns the pointer to the \a io_port_cfg_t struct passed in the pp argument.
*/
FLASHMEM io_port_cfg_t *ioports_cfg (io_port_cfg_t *pp, io_port_type_t type, io_port_direction_t dir)
{
static io_port_cfg_t cfg[4] = {0};
io_port_cfg_t *p = &cfg[(type << 1) | dir];
if(p->n_ports == 0 && !!hal.port.claim) {
if((p->n_ports = ioports_available(type, dir))) {
p->handle = &ports_cfg[(type << 1) | dir];
p->port_max = ioport_find_free(type, dir, (pin_cap_t){ .claimable = On }, NULL);
p->get_value = _get_value;
p->set_value = _set_value;
p->get_next = _get_next;
p->claim = _claim;
strcpy((char *)p->port_maxs, uitoa(p->port_max));
}
}
memcpy(pp, p, sizeof(io_port_cfg_t));
return pp;
}
// ---
/*! \brief Enumerate ports.
\param type as an \a #io_port_type_t enum value.
\param dir as an \a #io_port_direction_t enum value.
\param filter a \a #pin_cap_t union with fields set that must match the port capabilities.
\param callback pointer to a \a #ioports_enumerate_callback_ptr function that will be called for each matching port.
If the function returns \a true the enumeration will end.
\param data a pointer to context data passed to the callback function.
*/
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)
{
bool ok = false;
io_ports_private_t *p_data = get_port_data(type, dir);
if(p_data->ports && p_data->n_ports && ioports_can_do().claim_explicit) {
xbar_t *portinfo;
uint_fast16_t n_ports;
io_ports_private_t *p_data = get_port_data(type, dir);
if(filter.mask) {
n_ports = p_data->n_ports;
do {
if((portinfo = hal.port.get_pin_info(type, dir, map_reverse(p_data, --n_ports))) && (portinfo->cap.mask & filter.mask) == filter.mask) {
if((ok = callback(portinfo, resolve_portnum(p_data, portinfo), data)))
break;
}
} while(n_ports);
} else for(n_ports = 0; n_ports < p_data->n_ports; n_ports++) {
if((portinfo = hal.port.get_pin_info(type, dir, map_reverse(p_data, n_ports)))) {
if((ok = callback(portinfo, resolve_portnum(p_data, portinfo), data)))
break;
}
}
}
return ok;
}
/*! \brief Set pin description.
\param type as an \a #io_port_type_t enum value.
\param dir as an \a #io_port_direction_t enum value.
\param port the port aux number.
\param description pointer to a \a char constant for the pin description.
*/
FLASHMEM bool ioport_set_description (io_port_type_t type, io_port_direction_t dir, uint8_t port, const char *description)
{
if(hal.port.set_pin_description)
hal.port.set_pin_description(type, dir, port, description);
return !!hal.port.set_pin_description;
}
bool ioport_analog_out (uint8_t port, float value)
{
return hal.port.analog_out && hal.port.analog_out(port, value);
}
bool ioport_digital_out (uint8_t port, uint32_t value)
{
if(hal.port.digital_out)
hal.port.digital_out(port, value != 0);
return !!hal.port.digital_out;
}
int32_t ioport_wait_on_input (io_port_type_t type, uint8_t port, wait_mode_t wait_mode, float timeout)
{
return hal.port.wait_on_input ? hal.port.wait_on_input(type, port, wait_mode, timeout) : -1;
}
FLASHMEM bool ioport_analog_out_config (uint8_t port, pwm_config_t *config)
{
xbar_t *pin;
bool ok = (pin = hal.port.get_pin_info(Port_Analog, Port_Output, port)) && pin->config;
return ok && pin->config(pin, config, false);
}
FLASHMEM bool ioport_digital_in_config (uint8_t port, gpio_in_config_t *config)
{
xbar_t *pin;
bool ok = (pin = hal.port.get_pin_info(Port_Digital, Port_Input, port)) && pin->config;
return ok && pin->config(pin, config, false);
}
FLASHMEM bool ioport_enable_irq (uint8_t port, pin_irq_mode_t irq_mode, ioport_interrupt_callback_ptr handler)
{
return hal.port.register_interrupt_handler && hal.port.register_interrupt_handler(port, irq_mode, handler);
}
FLASHMEM bool ioport_digital_out_config (uint8_t port, gpio_out_config_t *config)
{
xbar_t *pin;
bool ok = (pin = hal.port.get_pin_info(Port_Digital, Port_Output, port)) && pin->config && !(pin->mode.pwm || pin->mode.servo_pwm);
return ok && pin->config(pin, config, false);
}
FLASHMEM bool ioport_digital_pwm_config (uint8_t port, pwm_config_t *config)
{
xbar_t *pin;
bool ok = (pin = hal.port.get_pin_info(Port_Digital, Port_Output, port)) && pin->config && pin->mode.claimed && pin->cap.pwm;
return ok && pin->config(pin, config, false);
}
// HAL wrapper/veneers
__STATIC_FORCEINLINE bool is_match (io_ports_list_t *io_port, io_port_type_t type, io_port_direction_t dir, uint8_t port)
{
return io_port->type == type && port >= io_port->ports_id->cfg[dir].n_start && port <= io_port->ports_id->cfg[dir].idx_last;
}
__STATIC_FORCEINLINE const char *pnum_to_string (uint8_t port, const char *pnum)
{
return pnum ? (pnum + (port * 3) + (port > 9 ? port - 10 : 0)) : NULL;
}
FLASHMEM static xbar_t *io_get_pin_info (io_port_type_t type, io_port_direction_t dir, uint8_t port)
{
xbar_t *pin = NULL;
io_ports_list_t *io_port = ports;
io_ports_private_t *cfg = get_port_data(type, dir);
port = cfg->map[port];
do {
if(is_match(io_port, type, dir, port)) {
if((pin = io_port->hal.get_pin_info(dir, port - io_port->ports_id->cfg[dir].n_start))) {
pin->ports_id = io_port->ports_id;
pin->cap.claimable = is_aux(cfg, pin->function);
pin->mode.claimed = cfg->claimed.mask & (1UL << (pin->id + io_port->ports_id->cfg[dir].n_start));
}
}
} while(pin == NULL && (io_port = io_port->next));
return pin;
}
FLASHMEM static void io_set_pin_description (io_port_type_t type, io_port_direction_t dir, uint8_t port, const char *s)
{
io_ports_list_t *io_port = ports;
io_ports_private_t *cfg = get_port_data(type, dir);
port = cfg->map[port];
do {
if(is_match(io_port, type, dir, port)) {
io_port->hal.set_pin_description(dir, port - io_port->ports_id->cfg[dir].n_start, s);
break;
}
} while((io_port = io_port->next));
}
FLASHMEM static bool io_claim (io_port_type_t type, io_port_direction_t dir, uint8_t *port, const char *description)
{
bool ok = false;
io_ports_list_t *io_port = ports;
io_ports_private_t *cfg = get_port_data(type, dir);
if(!(cfg->claimed.mask & (1UL << *port))) do {
if(is_match(io_port, type, dir, *port)) {
xbar_t *pin;
if((ok = (pin = io_port->hal.get_pin_info(dir, *port - io_port->ports_id->cfg[dir].n_start))) && pin->cap.claimable) {
uint_fast8_t idx = 0;
cfg->claimed.mask |= (1UL << *port);
while(cfg->map[idx] != *port)
idx++;
for(; idx < cfg->last_claimed ; idx++) {
if((cfg->map[idx] = cfg->map[idx + 1]) != 255)
io_set_pin_description(type, dir, idx, pnum_to_string(idx, cfg->pnum));
}
io_port->hal.set_pin_description(dir, *port - io_port->ports_id->cfg[dir].n_start, description);
pin->ports_id = io_port->ports_id;
cfg->map[cfg->last_claimed] = *port;
*port = cfg->last_claimed--;
break;
}
}
} while((io_port = io_port->next));
return ok;
}
static bool io_analog_out (uint8_t port, float value)
{
io_ports_list_t *io_port = ports;
io_ports_private_t *cfg = get_port_data(Port_Analog, Port_Output);
uint8_t pn = cfg->map[port];
do {
if(io_port->hal.analog_out && is_match(io_port, Port_Analog, Port_Output, pn)) {
bool ok = io_port->hal.analog_out(pn - io_port->ports_id->cfg[Port_Output].n_start, value);
if(ok && grbl.on_port_out && !(cfg->claimed.mask & (1 << pn)))
grbl.on_port_out(port, Port_Analog, value);
return ok;
}
} while((io_port = io_port->next));
return false;
}
static void io_digital_out (uint8_t port, bool on)
{
io_ports_list_t *io_port = ports;
io_ports_private_t *cfg = get_port_data(Port_Digital, Port_Output);
uint8_t pn = cfg->map[port];
do {
if(io_port->hal.digital_out && is_match(io_port, Port_Digital, Port_Output, pn)) {
io_port->hal.digital_out(pn - io_port->ports_id->cfg[Port_Output].n_start, on);
if(grbl.on_port_out && !(cfg->claimed.mask & (1 << pn)))
grbl.on_port_out(port, Port_Digital, (float)on);
break;
}
} while((io_port = io_port->next));
}
static int32_t io_wait_on_input (io_port_type_t type, uint8_t port, wait_mode_t wait_mode, float timeout)
{
int32_t value = -1;
io_ports_list_t *io_port = ports;
io_ports_private_t *cfg = get_port_data(type, Port_Input);
port = cfg->map[port];
do {
if(io_port->hal.wait_on_input && is_match(io_port, type, Port_Input, port)) {
value = io_port->hal.wait_on_input(port - io_port->ports_id->cfg[Port_Input].n_start, wait_mode, timeout);
break;
}
} while((io_port = io_port->next));
return value;
}
FLASHMEM static bool io_register_interrupt_handler (uint8_t port, pin_irq_mode_t irq_mode, ioport_interrupt_callback_ptr interrupt_callback)
{
uint8_t user_port = port;
io_ports_list_t *io_port = ports;
io_ports_private_t *cfg = get_port_data(Port_Digital, Port_Input);
port = cfg->map[port];
do {
if(io_port->hal.register_interrupt_handler && is_match(io_port, Port_Digital, Port_Input, port))
return io_port->hal.register_interrupt_handler(port - io_port->ports_id->cfg[Port_Input].n_start, user_port, irq_mode, interrupt_callback);
} while((io_port = io_port->next));
return false;
}
/**/
FLASHMEM static io_ports_list_t *insert_ports (void)
{
io_ports_list_t *io_ports;
if((io_ports = calloc(sizeof(io_ports_list_t), 1))) {
if(ports == NULL)
ports = io_ports;
else {
io_ports_list_t *add = ports;
while(add->next)
add = add->next;
add->next = io_ports;
}
}
return io_ports;
}
FLASHMEM static bool claim_hal (void)
{
io_port_t empty = {};
if(!memcmp(&hal.port, &empty, sizeof(io_port_t))) {
hal.port.get_pin_info = io_get_pin_info;
hal.port.set_pin_description = io_set_pin_description;
hal.port.claim = io_claim;
hal.port.analog_out = io_analog_out;
hal.port.digital_out = io_digital_out;
hal.port.wait_on_input = io_wait_on_input;
hal.port.register_interrupt_handler = io_register_interrupt_handler;
}
return hal.port.get_pin_info == io_get_pin_info;
}
#ifdef IOPORTS_KEEP_DEPRECATED
ISR_CODE uint8_t ISR_FUNC(ioports_map_reverse)(io_ports_detail_t *type, uint8_t port)
{
if(type->map) {
uint_fast8_t idx = type->n_ports;
do {
if(type->map[--idx] == port) {
port = idx;
break;
}
} while(idx);
}
return port;
}
FLASHMEM static const char *get_pnum (io_ports_data_t *ports, uint8_t port)
{
return ports->pnum ? (ports->pnum + (port * 3) + (port > 9 ? port - 10 : 0)) : NULL;
}
#endif
FLASHMEM static uint8_t add_ports (io_ports_detail_t *ports, uint8_t *map, io_port_type_t type, io_port_direction_t dir, uint8_t n_ports)
{
io_ports_private_t *p_data = get_port_data(type, dir);
if(n_ports) {
ports->n_start = p_data->n_ports;
if(ports->n_start + n_ports > p_data->n_max)
n_ports = p_data->n_max - ports->n_start;
ports->idx_last = ports->n_start + n_ports - 1;
#ifdef IOPORTS_KEEP_DEPRECATED
ports->map = map;
#endif
if(p_data->ports == NULL)
p_data->ports = ports;
p_data->count = -1;
} else {
ports->n_start = 255;
ports->idx_last = 0;
}
p_data->n_ports += n_ports;
ports->n_ports = n_ports;
return n_ports;
}
FLASHMEM static bool _ioports_add (io_ports_data_t *ports, io_port_type_t type, uint8_t n_in, uint8_t n_out, set_pin_description_ptr set_description)
{
uint_fast8_t n_ports;
io_ports_private_t *cfg_in = get_port_data(type, Port_Input),
*cfg_out = get_port_data(type, Port_Output);
#ifdef IOPORTS_KEEP_DEPRECATED
ports->get_pnum = get_pnum;
#endif
if(type == Port_Digital) {
hal.port.num_digital_in += add_ports(&ports->in, cfg_in->map, type, Port_Input, n_in);
hal.port.num_digital_out += add_ports(&ports->out, cfg_out->map, type, Port_Output, n_out);
} else {
hal.port.num_analog_in += add_ports(&ports->in, cfg_in->map, type, Port_Input, n_in);
hal.port.num_analog_out += add_ports(&ports->out, cfg_out->map, type, Port_Output, n_out);
}
if((n_ports = max(cfg_in->n_ports, cfg_out->n_ports)) > 0) {
uint_fast8_t i = MAX_PORTS, idx_in = 0, idx_out = 0;
if(cfg_in->pnum) {
while(cfg_in->map[idx_in] != 255)
idx_in++;
while(cfg_out->map[idx_out] != 255)
idx_out++;
} else do {
i--;
cfg_in->map[i] = cfg_out->map[i] = 255;
} while(i);
cfg_in->pnum = cfg_out->pnum /* = ports->pnum*/ = type == Port_Digital ? dpnum : apnum;
for(i = 0; i <= n_ports; i++) {
if(ports->in.n_ports && i >= ports->in.n_start && i <= ports->in.idx_last) {
cfg_in->map[idx_in] = i;
if(set_description)
set_description(type, Port_Input, i - ports->in.n_start, pnum_to_string(idx_in, cfg_in->pnum));
idx_in++;
if(i < MAX_PORTS) {
cfg_in->bus.mask |= (1 << i);
strcat(cfg_in->port_names, i == 0 ? "Aux " : ",Aux ");
strcat(cfg_in->port_names, uitoa(i));
}
}
if(ports->out.n_ports && i >= ports->out.n_start && i <= ports->out.idx_last) {
cfg_out->map[idx_out] = i;
if(set_description)
set_description(type, Port_Output, i - ports->out.n_start, pnum_to_string(idx_out, cfg_out->pnum));
if(i < MAX_PORTS) {
cfg_out->bus.mask |= (1 << i);
strcat(cfg_out->port_names, i == 0 ? "Aux " : ",Aux ");
strcat(cfg_out->port_names, uitoa(i));
}
idx_out++;
}
}
cfg_in->enabled.mask = cfg_in->bus.mask;
cfg_out->enabled.mask = cfg_out->bus.mask;
}
return n_ports > 0;
}
/*! \brief Remap (virtual) port.
\param type as an \a #io_port_type_t enum value.
\param dir as an \a #io_port_direction_t enum value.
\param port_from the assigned port number.
\param port_to the remapped port number. The original port number will be swapped with \a port_from.