grblHAL core  20241107
ioports.h
Go to the documentation of this file.
1 /*
2  ioports.h - typedefs, API structure and functions for auxiliary I/O
3 
4  Part of grblHAL
5 
6  Copyright (c) 2021-2024 Terje Io
7 
8  grblHAL is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  grblHAL is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with grblHAL. If not, see <http://www.gnu.org/licenses/>.
20 */
21 
22 #pragma once
23 
24 typedef enum {
26  Port_Digital = 1
28 
29 typedef enum {
30  Port_Input = 0,
31  Port_Output = 1
33 
38 typedef void (*digital_out_ptr)(uint8_t port, bool on);
39 
45 typedef bool (*analog_out_ptr)(uint8_t port, float value);
46 
57 typedef int32_t (*wait_on_input_ptr)(io_port_type_t type, uint8_t port, wait_mode_t wait_mode, float timeout);
58 
65 typedef void (*set_pin_description_ptr)(io_port_type_t type, io_port_direction_t dir, uint8_t port, const char *s);
66 
74 typedef xbar_t *(*get_pin_info_ptr)(io_port_type_t type, io_port_direction_t dir, uint8_t port);
75 
83 typedef bool (*claim_port_ptr)(io_port_type_t type, io_port_direction_t dir, uint8_t *port, const char *description);
84 
92 typedef bool (*swap_pins_ptr)(io_port_type_t type, io_port_direction_t dir, uint8_t port_from, uint8_t port_to);
93 
98 typedef void (*ioport_interrupt_callback_ptr)(uint8_t port, bool state);
99 
106 typedef bool (*ioport_register_interrupt_handler_ptr)(uint8_t port, pin_irq_mode_t irq_mode, ioport_interrupt_callback_ptr interrupt_callback);
107 
108 typedef bool (*ioports_enumerate_callback_ptr)(xbar_t *properties, uint8_t port, void *data);
109 
111 typedef struct {
112  uint8_t num_digital_in;
113  uint8_t num_digital_out;
114  uint8_t num_analog_in;
115  uint8_t num_analog_out;
124 } io_port_t;
125 
127 bool ioport_claim (io_port_type_t type, io_port_direction_t dir, uint8_t *port, const char *description);
128 bool ioport_can_claim_explicit (void);
129 uint8_t ioport_find_free (io_port_type_t type, io_port_direction_t dir, const char *description);
131 void ioport_assign_function (aux_ctrl_t *aux_ctrl, pin_function_t *function);
132 void ioport_assign_out_function (aux_ctrl_out_t *aux_ctrl, pin_function_t *function);
133 bool ioport_analog_out_config (uint8_t port, pwm_config_t *config);
134 bool ioport_digital_pwm_config (uint8_t port, pwm_config_t *config);
135 bool ioport_digital_in_config (uint8_t port, gpio_in_config_t *config);
136 bool ioport_enable_irq (uint8_t port, pin_irq_mode_t irq_mode, ioport_interrupt_callback_ptr handler);
137 bool ioport_digital_out_config (uint8_t port, gpio_out_config_t *config);
138 
139 //
140 
141 struct io_ports_data;
142 
143 typedef struct {
144  uint8_t n_ports;
145  uint8_t n_start;
146  uint8_t *map;
148 
149 typedef struct io_ports_data {
150  char *pnum;
153  char *(*get_pnum)(struct io_ports_data *data, uint8_t port);
155 
157 typedef struct {
158  uint32_t f_clock;
159  uint_fast16_t period;
160  uint_fast16_t off_value;
161  uint_fast16_t min_value;
162  uint_fast16_t max_value;
163  float min;
165  bool invert_pwm;
166  bool always_on;
167 } ioports_pwm_t;
168 
169 bool ioports_add (io_ports_data_t *ports, io_port_type_t type, uint8_t n_in, uint8_t n_out);
170 void ioports_add_settings (driver_settings_load_ptr settings_loaded, setting_changed_ptr setting_changed);
174 #define iports_get_pnum(type, port) type.get_pnum(&type, port)
175 #define ioports_map(type, port) ( type.map ? type.map[port] : port )
176 uint8_t ioports_map_reverse (io_ports_detail_t *type, uint8_t port);
177 bool ioports_precompute_pwm_values (pwm_config_t *config, ioports_pwm_t *pwm_data, uint32_t clock_hz);
178 uint_fast16_t ioports_compute_pwm_value (ioports_pwm_t *pwm_data, float value);
179 
180 /*EOF*/
pin_function_t
Definition: crossbar.h:27
pin_irq_mode_t
Pin interrupt modes, may be or'ed when reporting pin capability.
Definition: crossbar.h:458
wait_mode_t
M66 Allowed L-parameter values.
Definition: gcode.h:278
bool(* analog_out_ptr)(uint8_t port, float value)
Pointer to function for setting an analog output.
Definition: ioports.h:45
uint8_t ioports_available(io_port_type_t type, io_port_direction_t dir)
Get number of digital or analog ports available.
Definition: ioports.c:71
bool ioports_add(io_ports_data_t *ports, io_port_type_t type, uint8_t n_in, uint8_t n_out)
Definition: ioports.c:274
void ioports_add_settings(driver_settings_load_ptr settings_loaded, setting_changed_ptr setting_changed)
Definition: ioports.c:819
io_port_direction_t
Definition: ioports.h:29
@ Port_Input
0
Definition: ioports.h:30
@ Port_Output
1
Definition: ioports.h:31
bool(* swap_pins_ptr)(io_port_type_t type, io_port_direction_t dir, uint8_t port_from, uint8_t port_to)
Pointer to function for swapping two digital or analog ports.
Definition: ioports.h:92
bool(* ioports_enumerate_callback_ptr)(xbar_t *properties, uint8_t port, void *data)
Definition: ioports.h:108
bool ioport_digital_in_config(uint8_t port, gpio_in_config_t *config)
Definition: ioports.c:238
io_port_type_t
Definition: ioports.h:24
@ Port_Analog
0
Definition: ioports.h:25
@ Port_Digital
1
Definition: ioports.h:26
void ioport_assign_out_function(aux_ctrl_out_t *aux_ctrl, pin_function_t *function)
Definition: ioports.c:194
bool ioport_analog_out_config(uint8_t port, pwm_config_t *config)
Definition: ioports.c:230
void ioport_assign_function(aux_ctrl_t *aux_ctrl, pin_function_t *function)
Reassign pin function.
Definition: ioports.c:179
void ioport_save_output_settings(xbar_t *xbar, gpio_out_config_t *config)
Definition: ioports.c:469
uint8_t ioport_find_free(io_port_type_t type, io_port_direction_t dir, const char *description)
find first free or claimed digital or analog port.
Definition: ioports.c:104
bool ioport_digital_out_config(uint8_t port, gpio_out_config_t *config)
Definition: ioports.c:251
uint_fast16_t ioports_compute_pwm_value(ioports_pwm_t *pwm_data, float value)
Analog value to PWM conversion.
Definition: ioports.c:425
xbar_t *(* get_pin_info_ptr)(io_port_type_t type, io_port_direction_t dir, uint8_t port)
Pointer to function for getting information about a digital or analog port. NOTE: The port informati...
Definition: ioports.h:74
void ioport_setting_changed(setting_id_t id)
Definition: ioports.c:747
uint8_t ioports_map_reverse(io_ports_detail_t *type, uint8_t port)
Definition: ioports.c:365
bool(* ioport_register_interrupt_handler_ptr)(uint8_t port, pin_irq_mode_t irq_mode, ioport_interrupt_callback_ptr interrupt_callback)
Pointer to function for registering or deregistering an interrupt handler for a digital input port.
Definition: ioports.h:106
struct io_ports_data io_ports_data_t
bool ioport_can_claim_explicit(void)
Check if ports can be claimed by aux number or not.
Definition: ioports.c:210
bool 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:140
bool ioport_enable_irq(uint8_t port, pin_irq_mode_t irq_mode, ioport_interrupt_callback_ptr handler)
Definition: ioports.c:246
int32_t(* wait_on_input_ptr)(io_port_type_t type, uint8_t port, wait_mode_t wait_mode, float timeout)
Pointer to function for reading a digital or analog input.
Definition: ioports.h:57
void(* ioport_interrupt_callback_ptr)(uint8_t port, bool state)
Pointer to callback function for input port interrupt events.
Definition: ioports.h:98
bool ioports_precompute_pwm_values(pwm_config_t *config, ioports_pwm_t *pwm_data, uint32_t clock_hz)
Precompute PWM values for faster conversion.
Definition: ioports.c:396
bool(* claim_port_ptr)(io_port_type_t type, io_port_direction_t dir, uint8_t *port, const char *description)
Pointer to function for claiming a digital or analog port for exclusive use.
Definition: ioports.h:83
void(* set_pin_description_ptr)(io_port_type_t type, io_port_direction_t dir, uint8_t port, const char *s)
Pointer to function for setting pin description for a digital or analog port.
Definition: ioports.h:65
bool ioports_enumerate(io_port_type_t type, io_port_direction_t dir, pin_cap_t filter, ioports_enumerate_callback_ptr callback, void *data)
Definition: ioports.c:215
void ioport_save_input_settings(xbar_t *xbar, gpio_in_config_t *config)
Definition: ioports.c:445
bool ioport_digital_pwm_config(uint8_t port, pwm_config_t *config)
Definition: ioports.c:259
void(* digital_out_ptr)(uint8_t port, bool on)
Pointer to function for setting a digital output.
Definition: ioports.h:38
void(* setting_changed_ptr)(setting_id_t id)
Definition: settings.h:995
setting_id_t
Definition: settings.h:46
void(* driver_settings_load_ptr)(void)
Definition: settings.h:1003
Definition: crossbar.h:616
Definition: crossbar.h:606
/a cfg_data argument to /a xbar_config_ptr for gpio input pins
Definition: crossbar.h:567
/a cfg_data argument to /a xbar_config_ptr for gpio output pins
Definition: crossbar.h:574
Properties and handlers for auxiliary digital and analog I/O.
Definition: ioports.h:111
claim_port_ptr claim
Optional handler for claiming an auxiliary pin for exclusive use.
Definition: ioports.h:121
wait_on_input_ptr wait_on_input
Optional handler for reading a digital or analog input.
Definition: ioports.h:118
uint8_t num_digital_in
Number of digital inputs available.
Definition: ioports.h:112
swap_pins_ptr swap_pins
Optional handler for swapping pins.
Definition: ioports.h:122
uint8_t num_analog_out
Number of analog outputs available.
Definition: ioports.h:115
set_pin_description_ptr set_pin_description
Optional handler for setting a description of an auxiliary pin.
Definition: ioports.h:119
analog_out_ptr analog_out
Optional handler for setting an analog output.
Definition: ioports.h:117
uint8_t num_digital_out
Number of digital outputs available.
Definition: ioports.h:113
digital_out_ptr digital_out
Optional handler for setting a digital output.
Definition: ioports.h:116
ioport_register_interrupt_handler_ptr register_interrupt_handler
Definition: ioports.h:123
uint8_t num_analog_in
Number of analog inputs available.
Definition: ioports.h:114
get_pin_info_ptr get_pin_info
Optional handler for getting information about an auxiliary pin.
Definition: ioports.h:120
Definition: ioports.h:149
io_ports_detail_t out
Definition: ioports.h:152
io_ports_detail_t in
Definition: ioports.h:151
char * pnum
Definition: ioports.h:150
Definition: ioports.h:143
uint8_t * map
Definition: ioports.h:146
uint8_t n_start
Definition: ioports.h:145
uint8_t n_ports
Definition: ioports.h:144
Precalculated values that may be set/used by HAL driver to speed up analog input to PWM conversions....
Definition: ioports.h:157
uint_fast16_t period
Definition: ioports.h:159
uint_fast16_t off_value
NOTE: this value holds the inverted version if software PWM inversion is enabled by the driver.
Definition: ioports.h:160
uint32_t f_clock
Definition: ioports.h:158
bool invert_pwm
NOTE: set (by driver) when inversion is done in code.
Definition: ioports.h:165
uint_fast16_t min_value
Definition: ioports.h:161
float min
Minimum analog input value.
Definition: ioports.h:163
bool always_on
Definition: ioports.h:166
uint_fast16_t max_value
Definition: ioports.h:162
float pwm_gradient
Definition: ioports.h:164
/a cfg_data argument to /a xbar_config_ptr for PWM pins
Definition: crossbar.h:581
Definition: crossbar.h:624
Definition: crossbar.h:502