-
Notifications
You must be signed in to change notification settings - Fork 1
/
ioports.c
339 lines (254 loc) · 11 KB
/
ioports.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
ioports.c - driver code for Texas Instruments MSP432P401R ARM processor
Part of grblHAL
Copyright (c) 2021-2024 Terje Io
grblHAL is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
grblHAL is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with grblHAL. If not, see <http://www.gnu.org/licenses/>.
*/
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include "driver.h"
#include "grbl/protocol.h"
static io_ports_data_t digital;
static input_signal_t *aux_in;
static output_signal_t *aux_out;
static volatile uint32_t event_bits;
static bool digital_out_cfg (xbar_t *output, gpio_out_config_t *config, bool persistent)
{
if(output->id < digital.out.n_ports && config->inverted != aux_out[output->id].mode.inverted) {
aux_out[output->id].mode.inverted = config->inverted;
BITBAND_PERI(aux_out[output->id].port->OUT, aux_out[output->id].pin) = !BITBAND_PERI(aux_out[output->id].port->IN, aux_out[output->id].pin);
if(persistent)
ioport_save_output_settings(output, config);
}
return aux_out->id < digital.out.n_ports;
}
static void digital_out (uint8_t port, bool on)
{
if(port < digital.out.n_ports) {
port = ioports_map(digital.out, port);
BITBAND_PERI(aux_out[port].port->OUT, aux_out[port].pin) = aux_out[port].mode.inverted ? !on : on;
}
}
static float digital_out_state (xbar_t *output)
{
float value = -1.0f;
if(output->id < digital.out.n_ports)
value = (float)(BITBAND_PERI(aux_out[output->id].port->OUT, aux_out[output->id].pin) ^ aux_out[output->id].mode.inverted);
return value;
}
static bool digital_in_cfg (xbar_t *input, gpio_in_config_t *config, bool persistent)
{
if(input->id < digital.in.n_ports && config->pull_mode != PullMode_UpDown) {
aux_in[input->id].mode.inverted = config->inverted;
aux_in[input->id].mode.pull_mode = config->pull_mode;
aux_in[input->id].mode.debounce = config->debounce;
BITBAND_PERI(aux_in[input->id].port->REN, aux_out[input->id].pin) = config->pull_mode == PullMode_Up;
BITBAND_PERI(aux_in[input->id].port->OUT, aux_out[input->id].pin) = config->pull_mode == PullMode_Up;
if(persistent)
ioport_save_input_settings(input, config);
}
return input->id < digital.in.n_ports;
}
static float digital_in_state (xbar_t *input)
{
float value = -1.0f;
if(input->id < digital.in.n_ports)
value = (float)(BITBAND_PERI(aux_in[input->id].port->IN, aux_in[input->id].pin) ^ aux_in[input->id].mode.inverted);
return value;
}
static void enable_irq (const input_signal_t *input, pin_irq_mode_t irq_mode)
{
BITBAND_PERI(input->port->IES, input->pin) = irq_mode == IRQ_Mode_Falling;
BITBAND_PERI(input->port->IFG, input->pin) = 0;
BITBAND_PERI(input->port->IE, input->pin) = irq_mode != IRQ_Mode_None;
}
inline static __attribute__((always_inline)) int32_t get_input (const input_signal_t *input, wait_mode_t wait_mode, float timeout)
{
if(wait_mode == WaitMode_Immediate)
return BITBAND_PERI(input->port->IN, input->pin) ^ input->mode.inverted;
int32_t value = -1;
uint_fast16_t delay = (uint_fast16_t)ceilf((1000.0f / 50.0f) * timeout) + 1;
if(wait_mode == WaitMode_Rise || wait_mode == WaitMode_Fall) {
pin_irq_mode_t irq_mode = wait_mode == WaitMode_Rise ? IRQ_Mode_Rising : IRQ_Mode_Falling;
if(input->cap.irq_mode & irq_mode) {
event_bits &= ~input->bit;
enable_irq(input, irq_mode);
do {
if(event_bits & input->bit) {
value = BITBAND_PERI(input->port->IN, input->pin) ^ input->mode.inverted;
break;
}
if(delay) {
protocol_execute_realtime();
hal.delay_ms(50, NULL);
} else
break;
} while(--delay && !sys.abort);
enable_irq(input, (pin_irq_mode_t)input->mode.irq_mode); // Restore pin interrupt status
}
} else {
bool wait_for = wait_mode != WaitMode_Low;
do {
if((BITBAND_PERI(input->port->IN, input->pin) ^ input->mode.inverted) == wait_for) {
value = BITBAND_PERI(input->port->IN, input->pin) ^ input->mode.inverted;
break;
}
if(delay) {
protocol_execute_realtime();
hal.delay_ms(50, NULL);
} else
break;
} while(--delay && !sys.abort);
}
return value;
}
void ioports_event (input_signal_t *input)
{
event_bits |= input->bit;
if(input->interrupt_callback)
input->interrupt_callback(input->user_port, BITBAND_PERI(input->port->IN, input->pin));
}
static int32_t wait_on_input (io_port_type_t type, uint8_t port, wait_mode_t wait_mode, float timeout)
{
int32_t value = -1;
if(type == Port_Digital && port < digital.in.n_ports) {
port = ioports_map(digital.in, port);
value = get_input(&aux_in[port], wait_mode, timeout);
}
return value;
}
static bool register_interrupt_handler (uint8_t port, pin_irq_mode_t irq_mode, ioport_interrupt_callback_ptr interrupt_callback)
{
bool ok;
port = ioports_map(digital.in, port);
if((ok = port < digital.in.n_ports && aux_in[port].cap.irq_mode != IRQ_Mode_None)) {
input_signal_t *input = &aux_in[port];
BITBAND_PERI(input->port->IE, input->pin) = 0; // Disable pin interrupt
if((ok = (irq_mode & aux_in[port].cap.irq_mode) == irq_mode && interrupt_callback != NULL)) {
input->mode.irq_mode = irq_mode;
input->interrupt_callback = interrupt_callback;
enable_irq(input, irq_mode);
}
if(irq_mode == IRQ_Mode_None || !ok) {
hal.irq_disable();
enable_irq(input, IRQ_Mode_None);
input->mode.irq_mode = IRQ_Mode_None;
input->interrupt_callback = NULL;
hal.irq_enable();
}
}
return ok;
}
static xbar_t *get_pin_info (io_port_type_t type, io_port_direction_t dir, uint8_t port)
{
static xbar_t pin;
xbar_t *info = NULL;
if(type == Port_Digital) {
if(dir == Port_Input && port < digital.in.n_ports) {
XBAR_SET_DIN_INFO(pin, ioports_map(digital.in, port), aux_in[pin.id], digital_in_cfg, digital_in_state);
info = &pin;
}
if(dir == Port_Output && port < digital.out.n_ports) {
XBAR_SET_DOUT_INFO(pin, ioports_map(digital.out, port), aux_out[pin.id], digital_out_cfg, digital_out_state);
info = &pin;
}
}
return info;
}
static void set_pin_description (io_port_type_t type, io_port_direction_t dir, uint8_t port, const char *s)
{
if(type == Port_Digital) {
if(dir == Port_Input && port < digital.in.n_ports)
aux_in[ioports_map(digital.in, port)].description = s;
if(dir == Port_Output && port < digital.out.n_ports)
aux_out[ioports_map(digital.out, port)].description = s;
}
}
static bool claim (io_port_type_t type, io_port_direction_t dir, uint8_t *port, const char *description)
{
bool ok = false;
if(type == Port_Digital) {
if(dir == Port_Input) {
if((ok = digital.in.map && *port < digital.in.n_ports && !aux_in[*port].mode.claimed)) {
uint8_t i;
hal.port.num_digital_in--;
for(i = ioports_map_reverse(&digital.in, *port); i < hal.port.num_digital_in ; i++) {
digital.in.map[i] = digital.in.map[i + 1];
aux_in[digital.in.map[i]].user_port = i;
aux_in[digital.in.map[i]].description = iports_get_pnum(digital, i);
}
aux_in[*port].mode.claimed = On;
aux_in[*port].user_port = hal.port.num_digital_in;
aux_in[*port].description = description;
digital.in.map[hal.port.num_digital_in] = *port;
*port = hal.port.num_digital_in;
}
} else if((ok = digital.out.map && *port < digital.out.n_ports && !aux_out[*port].mode.claimed)) {
uint8_t i;
hal.port.num_digital_out--;
for(i = ioports_map_reverse(&digital.out, *port); i < hal.port.num_digital_out; i++) {
digital.out.map[i] = digital.out.map[i + 1];
aux_out[digital.out.map[i]].description = iports_get_pnum(digital, i);
}
aux_out[*port].mode.claimed = On;
aux_out[*port].description = description;
digital.out.map[hal.port.num_digital_out] = *port;
*port = hal.port.num_digital_out;
}
}
return ok;
}
bool swap_pins (io_port_type_t type, io_port_direction_t dir, uint8_t port_a, uint8_t port_b)
{
bool ok = port_a == port_b;
if(!ok && type == Port_Digital) {
if((ok = dir == Port_Input && port_a < digital.in.n_ports && port_b < digital.in.n_ports &&
aux_in[port_a].interrupt_callback == NULL &&
aux_in[port_b].interrupt_callback == NULL)) {
input_signal_t tmp;
memcpy(&tmp, &aux_in[port_a], sizeof(input_signal_t));
memcpy(&aux_in[port_a], &aux_in[port_b], sizeof(input_signal_t));
aux_in[port_a].description = tmp.description;
tmp.description = aux_in[port_b].description;
memcpy(&aux_in[port_b], &tmp, sizeof(input_signal_t));
}
if((ok = dir == Port_Output && port_a < digital.out.n_ports && port_b < digital.out.n_ports)) {
output_signal_t tmp;
memcpy(&tmp, &aux_out[port_a], sizeof(output_signal_t));
memcpy(&aux_out[port_a], &aux_out[port_b], sizeof(output_signal_t));
aux_out[port_a].description = tmp.description;
tmp.description = aux_out[port_b].description;
memcpy(&aux_out[port_b], &tmp, sizeof(output_signal_t));
}
}
return ok;
}
void ioports_init (pin_group_pins_t *aux_inputs, pin_group_pins_t *aux_outputs)
{
aux_in = aux_inputs->pins.inputs;
aux_out = aux_outputs->pins.outputs;
hal.port.set_pin_description = set_pin_description;
if(ioports_add(&digital, Port_Digital, aux_inputs->n_pins, aux_outputs->n_pins)) {
if(digital.in.n_ports) {
hal.port.wait_on_input = wait_on_input;
hal.port.register_interrupt_handler = register_interrupt_handler;
}
if(digital.out.n_ports)
hal.port.digital_out = digital_out;
hal.port.claim = claim;
hal.port.swap_pins = swap_pins;
hal.port.get_pin_info = get_pin_info;
ioports_add_settings(NULL, NULL);
} else
hal.port.set_pin_description = NULL;
}