-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathioports_analog.c
560 lines (447 loc) · 17.6 KB
/
ioports_analog.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
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
/*
ioports_analog.c - driver code for Texas Instruments MSP432P401R ARM processor
Part of grblHAL
Copyright (c) 2025 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 Licens
along with grblHAL. If not, see <http://www.gnu.org/licenses/>.
*/
#include "driver.h"
#if AUX_ANALOG
#ifdef AUXOUTPUT0_PWM_PORT
#define PWM_OUT0 1
#else
#define PWM_OUT0 0
#endif
#ifdef AUXOUTPUT1_PWM_PORT
#define PWM_OUT1 1
#else
#define PWM_OUT1 0
#endif
#define AUX_ANALOG_OUT (PWM_OUT0 + PWM_OUT1)
//#include "pwm.h"
#include "grbl/ioports.h"
/*
typedef struct {
GPIO_TypeDef *port;
uint8_t pin;
uint8_t alt;
ADC_TypeDef *adc;
uint32_t ch;
} adc_map_t;
static const adc_map_t adc_map[] = {
{ GPIOA, 0, 123, ADC1, ADC_CHANNEL_0 },
{ GPIOA, 1, 123, ADC1, ADC_CHANNEL_1 },
{ GPIOA, 2, 123, ADC1, ADC_CHANNEL_2 },
{ GPIOA, 3, 123, ADC1, ADC_CHANNEL_3 },
#ifdef ADC2
{ GPIOA, 4, 12, ADC2, ADC_CHANNEL_4 },
{ GPIOA, 5, 12, ADC2, ADC_CHANNEL_5 },
{ GPIOA, 6, 12, ADC2, ADC_CHANNEL_6 },
{ GPIOA, 7, 12, ADC2, ADC_CHANNEL_7 },
{ GPIOB, 0, 12, ADC2, ADC_CHANNEL_8 },
{ GPIOB, 1, 12, ADC2, ADC_CHANNEL_9 },
#endif
{ GPIOC, 0, 123, ADC1, ADC_CHANNEL_10 },
{ GPIOC, 1, 123, ADC1, ADC_CHANNEL_11 },
{ GPIOC, 2, 123, ADC1, ADC_CHANNEL_12 },
{ GPIOC, 3, 123, ADC1, ADC_CHANNEL_13 },
#ifdef ADC2
{ GPIOC, 4, 12, ADC2, ADC_CHANNEL_14 },
{ GPIOC, 5, 12, ADC2, ADC_CHANNEL_15 },
#endif
#ifdef ADC3
{ GPIOF, 3, 3, ADC3, ADC_CHANNEL_9 },
{ GPIOF, 4, 3, ADC3, ADC_CHANNEL_14 },
{ GPIOF, 5, 3, ADC3, ADC_CHANNEL_15 },
{ GPIOF, 6, 3, ADC3, ADC_CHANNEL_4 },
{ GPIOF, 7, 3, ADC3, ADC_CHANNEL_5 },
{ GPIOF, 8, 3, ADC3, ADC_CHANNEL_6 },
{ GPIOF, 9, 3, ADC3, ADC_CHANNEL_7 },
{ GPIOF, 10, 3, ADC3, ADC_CHANNEL_8 }
#endif
};
*/
static io_ports_data_t analog;
static input_signal_t *aux_in_analog;
static output_signal_t *aux_out_analog;
static set_pin_description_ptr set_pin_description_digital;
static get_pin_info_ptr get_pin_info_digital;
static claim_port_ptr claim_digital;
//static swap_pins_ptr swap_pins_digital;
//static wait_on_input_ptr wait_on_input_digital;
#ifdef MCP3221_ENABLE
#include "MCP3221.h"
static xbar_t mcp3221;
static enumerate_pins_ptr on_enumerate_pins;
static void enumerate_pins (bool low_level, pin_info_ptr pin_info, void *data)
{
on_enumerate_pins(low_level, pin_info, data);
pin_info(&mcp3221, data);
}
static float mcp3221_in_state (xbar_t *input)
{
return (float)MCP3221_read();
}
#endif // MCP3221_ENABLE
#if AUX_ANALOG_OUT
static float pwm_get_value (xbar_t *output)
{
return output->id < analog.out.n_ports ? aux_out_analog[output->id].pwm->value : -1.0f;
}
static void pwm_out (uint8_t port, float value)
{
if(port < analog.out.n_ports && aux_out_analog[port].pwm) {
uint_fast16_t pwm_value = ioports_compute_pwm_value(&aux_out_analog[port].pwm->data, value);
// const Timer_A_Type *pwm = aux_out_analog[port].pwm->port;
aux_out_analog[port].pwm->value = value;
if(pwm_value == aux_out_analog[port].pwm->data.off_value) {
if(aux_out_analog[port].pwm->data.always_on) {
AUX0_PWM_TIMER->CCR[2] = aux_out_analog[port].pwm->data.off_value;
AUX0_PWM_TIMER->CCTL[2] = aux_out_analog[port].pwm->data.invert_pwm ? TIMER_A_CCTLN_OUTMOD_2 : TIMER_A_CCTLN_OUTMOD_6;
} else
AUX0_PWM_TIMER->CCTL[2] = settings.pwm_spindle.invert.pwm ? TIMER_A_CCTLN_OUT : 0; // Set PWM output according to invert setting
} else {
AUX0_PWM_TIMER->CCR[2] = pwm_value;
AUX0_PWM_TIMER->CCTL[2] = aux_out_analog[port].pwm->data.invert_pwm ? TIMER_A_CCTLN_OUTMOD_2 : TIMER_A_CCTLN_OUTMOD_6;
}
}
}
static bool analog_out (uint8_t port, float value)
{
if(port < analog.out.n_ports)
pwm_out(ioports_map(analog.out, port), value);
return port < analog.out.n_ports;
}
static bool init_pwm (xbar_t *output, pwm_config_t *config, bool persistent)
{
bool ok;
if(aux_out_analog[output->id].pwm == NULL) {
pwm_out_t *pwm;
if((pwm = calloc(sizeof(pwm_out_t), 1))) {
aux_out_analog[output->id].pwm = pwm;
pwm->port = AUX0_PWM_TIMER;
}
}
if((ok = !!aux_out_analog[output->id].pwm)) {
int32_t prescaler = -1;
uint32_t psf[] = { 1, 2, 4, 8 };
do {
prescaler++;
ok = ioports_precompute_pwm_values(config, &aux_out_analog[output->id].pwm->data, 12000000UL / psf[prescaler]);
} while(ok && aux_out_analog[output->id].pwm->data.period > 65530 && prescaler <= 2);
if(ok) {
AUX0_PWM_TIMER->CTL &= ~TIMER_A_CTL_ID_MASK;
AUX0_PWM_TIMER->CTL |= (prescaler << TIMER_A_CTL_ID_OFS);
AUX0_PWM_TIMER->CCR[0] = aux_out_analog[output->id].pwm->data.period;
AUX0_PWM_TIMER->CCTL[2] = config->invert ? TIMER_A_CCTLN_OUT : 0;
AUX0_PWM_TIMER->CTL |= TIMER_A_CTL_CLR|TIMER_A_CTL_MC0;
aux_out_analog[output->id].mode.pwm = !config->servo_mode;
aux_out_analog[output->id].mode.servo_pwm = config->servo_mode;
}
}
if(!ok && !aux_out_analog[output->id].mode.claimed)
hal.port.claim(Port_Analog, Port_Output, &output->id, "N/A");
return ok;
}
#endif // AUX_ANALOG_OUT
/*
static float analog_in_state (xbar_t *input)
{
float value = -1.0f;
#ifdef MCP3221_ENABLE
if(input->id < analog.in.n_ports && input->id != mcp3221.id) {
#else
if(input->id < analog.in.n_ports) {
#endif
HAL_ADC_Start(aux_in_analog[input->id].adc);
if(HAL_ADC_PollForConversion(aux_in_analog[input->id].adc, 2) == HAL_OK)
value = HAL_ADC_GetValue(aux_in_analog[input->id].adc);
}
return value;
}
static int32_t wait_on_input_dummy (io_port_type_t type, uint8_t port, wait_mode_t wait_mode, float timeout)
{
return -1;
}
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)
return wait_on_input_digital(type, port, wait_mode, timeout);
port = ioports_map(analog.in, port);
#ifdef MCP3221_ENABLE
if(port == mcp3221.id)
value = (int32_t)MCP3221_read();
else
#endif
if(port < analog.in.n_ports && aux_in_analog[port].adc) {
HAL_ADC_Start(aux_in_analog[port].adc);
if(HAL_ADC_PollForConversion(aux_in_analog[port].adc, 2) == HAL_OK)
value = HAL_ADC_GetValue(aux_in_analog[port].adc);
}
return value;
}
*/
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)
return get_pin_info_digital ? get_pin_info_digital(type, dir, port) : NULL;
else {
memset(&pin, 0, sizeof(xbar_t));
switch(dir) {
/*
case Port_Input:
if(port < analog.in.n_ports) {
pin.id = ioports_map(analog.in, port);
#ifdef MCP3221_ENABLE
if(pin.id == mcp3221.id)
info = &mcp3221;
else
#endif
{
pin.mode = aux_in_analog[pin.id].mode;
pin.cap = aux_in_analog[pin.id].cap;
pin.function = aux_in_analog[pin.id].id;
pin.group = aux_in_analog[pin.id].group;
pin.pin = aux_in_analog[pin.id].pin;
pin.port = (void *)aux_in_analog[pin.id].port;
pin.description = aux_in_analog[pin.id].description;
pin.get_value = analog_in_state;
info = &pin;
}
}
break;
*/
case Port_Output:
#if AUX_ANALOG_OUT
if(port < analog.out.n_ports) {
pin.id = ioports_map(analog.out, port);
pin.port = aux_out_analog[pin.id].port;
pin.mode = aux_out_analog[pin.id].mode;
pin.mode.pwm = !pin.mode.servo_pwm; //?? for easy filtering
XBAR_SET_CAP(pin.cap, pin.mode);
pin.function = aux_out_analog[pin.id].id;
pin.group = aux_out_analog[pin.id].group;
pin.pin = aux_out_analog[pin.id].pin;
pin.port = (void *)aux_out_analog[pin.id].port;
pin.description = aux_out_analog[pin.id].description;
pin.get_value = pwm_get_value;
pin.config = init_pwm;
info = &pin;
}
#endif // AUX_ANALOG_OUT
break;
default: break;
}
}
return info;
}
static void set_pin_description (io_port_type_t type, io_port_direction_t dir, uint8_t port, const char *description)
{
if(type == Port_Analog) {
/* if(dir == Port_Input && port < analog.in.n_ports) {
port = ioports_map(analog.in, port);
#ifdef MCP3221_ENABLE
if(port == mcp3221.id)
mcp3221.description = description;
else
#endif
aux_in_analog[port].description = description;
} else*/ if(port < analog.out.n_ports)
aux_out_analog[ioports_map(analog.out, port)].description = description;
} else if(set_pin_description_digital)
set_pin_description_digital(type, dir, port, description);
}
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)
return claim_digital ? claim_digital(type, dir, port, description) : false;
else switch(dir) {
/*
case Port_Input:
if((ok = analog.in.map && *port < analog.in.n_ports && !(
#ifdef MCP3221_ENABLE
*port == mcp3221.id ? mcp3221.mode.claimed :
#endif
aux_in_analog[*port].mode.claimed))) {
uint8_t i;
hal.port.num_analog_in--;
for(i = ioports_map_reverse(&analog.in, *port); i < hal.port.num_analog_in; i++) {
analog.in.map[i] = analog.in.map[i + 1];
#ifdef MCP3221_ENABLE
if(mcp3221.id == analog.in.map[i])
mcp3221.description = iports_get_pnum(analog, i);
else
#endif
aux_in_analog[analog.in.map[i]].description = iports_get_pnum(analog, i);
}
#ifdef MCP3221_ENABLE
if(*port == mcp3221.id) {
mcp3221.mode.claimed = On;
mcp3221.description = description;
} else
#endif
{
aux_in_analog[*port].mode.claimed = On;
aux_in_analog[*port].description = description;
}
analog.in.map[hal.port.num_analog_in] = *port;
*port = hal.port.num_analog_in;
}
break;
*/
case Port_Output:
#if AUX_ANALOG_OUT
if((ok = analog.out.map && *port < analog.out.n_ports && !aux_out_analog[*port].mode.claimed)) {
uint_fast8_t i;
hal.port.num_analog_out--;
for(i = ioports_map_reverse(&analog.out, *port); i < hal.port.num_analog_out; i++) {
analog.out.map[i] = analog.out.map[i + 1];
aux_out_analog[analog.out.map[i]].description = iports_get_pnum(analog, i);
}
aux_out_analog[*port].mode.claimed = On;
aux_out_analog[*port].description = description;
analog.out.map[hal.port.num_analog_out] = *port;
*port = hal.port.num_analog_out;
}
#endif
break;
default: break;
}
return ok;
}
void ioports_init_analog (pin_group_pins_t *aux_inputs, pin_group_pins_t *aux_outputs)
{
// uint8_t p_pins = aux_inputs->n_pins;
aux_in_analog = aux_inputs->pins.inputs;
aux_out_analog = aux_outputs->pins.outputs;
set_pin_description_digital = hal.port.set_pin_description;
hal.port.set_pin_description = set_pin_description;
#ifdef MCP3221_ENABLE
pin_group_pins_t aux_in = {
.n_pins = 1
};
mcp3221.function = Input_Analog_Aux0 + (aux_inputs ? aux_inputs->n_pins : 0);
mcp3221.group = PinGroup_AuxInputAnalog;
mcp3221.id = aux_inputs ? aux_inputs->n_pins : 0;
mcp3221.port = "MCP3221:";
if((mcp3221.mode.analog = MCP3221_init())) {
if(aux_inputs)
aux_inputs->n_pins++;
else
aux_inputs = &aux_in;
mcp3221.get_value = mcp3221_in_state;
} else
mcp3221.description = "No power";
on_enumerate_pins = hal.enumerate_pins;
hal.enumerate_pins = enumerate_pins;
#endif // MCP3221_ENABLE
if(ioports_add(&analog, Port_Analog, aux_inputs->n_pins, aux_outputs->n_pins)) {
claim_digital = hal.port.claim;
hal.port.claim = claim;
get_pin_info_digital = hal.port.get_pin_info;
hal.port.get_pin_info = get_pin_info;
// swap_pins = hal.port.swap_pins;
// hal.port.swap_pins = swap_pins;
/*
if(p_pins) {
GPIO_InitTypeDef gpio_init = {
.Mode = GPIO_MODE_ANALOG,
.Pull = GPIO_NOPULL
};
ADC_ChannelConfTypeDef adc_config = {
.Rank = 1,
.SamplingTime = ADC_SAMPLETIME_3CYCLES
};
uint_fast8_t i;
for(i = 0; i < p_pins; i++) {
uint_fast8_t j = sizeof(adc_map) / sizeof(adc_map_t);
do {
j--;
if(adc_map[j].port == aux_inputs->pins.inputs[i].port && adc_map[j].pin == aux_inputs->pins.inputs[i].pin) {
ADC_HandleTypeDef *adc;
if((adc = calloc(sizeof(ADC_HandleTypeDef), 1))) {
#ifdef ADC3
if(adc_map[j].alt == 3)
__HAL_RCC_ADC3_CLK_ENABLE();
else
#endif
#ifdef ADC2
if(adc_map[j].alt == 12)
__HAL_RCC_ADC2_CLK_ENABLE();
else
#endif
__HAL_RCC_ADC1_CLK_ENABLE();
gpio_init.Pin = aux_inputs->pins.inputs[i].bit;
HAL_GPIO_Init(aux_inputs->pins.inputs[i].port, &gpio_init);
adc_config.Channel = adc_map[j].ch;
adc->Instance = adc_map[j].adc;
adc->Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
adc->Init.Resolution = ADC_RESOLUTION_12B;
adc->Init.ScanConvMode = DISABLE;
adc->Init.ContinuousConvMode = DISABLE;
adc->Init.DiscontinuousConvMode = DISABLE;
adc->Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
adc->Init.ExternalTrigConv = ADC_SOFTWARE_START;
adc->Init.DataAlign = ADC_DATAALIGN_RIGHT;
adc->Init.NbrOfConversion = 1;
adc->Init.DMAContinuousRequests = DISABLE;
adc->Init.EOCSelection = ADC_EOC_SINGLE_CONV;
if(HAL_ADC_Init(adc) == HAL_OK && HAL_ADC_ConfigChannel(adc, &adc_config) == HAL_OK)
aux_inputs->pins.inputs[i].adc = adc;
else
analog.in.n_ports--;
}
break;
}
} while(j);
}
}
if(analog.in.n_ports) {
if((wait_on_input_digital = hal.port.wait_on_input) == NULL)
wait_on_input_digital = wait_on_input_dummy;
hal.port.wait_on_input = wait_on_input;
}
*/
#if AUX_ANALOG_OUT
if(analog.out.n_ports) {
xbar_t *pin;
uint_fast8_t i;
pwm_config_t config = {
.freq_hz = 5000.0f,
.min = 0.0f,
.max = 100.0f,
.off_value = 0.0f,
.min_value = 0.0f,
.max_value = 100.0f,
.invert = Off
};
hal.port.analog_out = analog_out;
// P3MAP->PMAP_REGISTER5 = 25;
AUX0_PWM_TIMER->CTL = TIMER_A_CTL_SSEL__SMCLK;
AUX0_PWM_TIMER->EX0 = 0;
for(i = 0; i < analog.out.n_ports; i++) {
BITBAND_PERI(aux_outputs->pins.outputs[i].port->DIR, aux_outputs->pins.outputs[i].pin) = 1;
BITBAND_PERI(aux_outputs->pins.outputs[i].port->SEL0, aux_outputs->pins.outputs[i].pin) = 1;
BITBAND_PERI(aux_outputs->pins.outputs[i].port->SEL1, aux_outputs->pins.outputs[i].pin) = 0;
aux_out_analog[i].pwm->port = AUX0_PWM_TIMER;
if((pin = get_pin_info(Port_Analog, Port_Output, i)))
pin->config(pin, &config, false);
}
}
#endif // AUX_ANALOG_OUT
} else
hal.port.set_pin_description = set_pin_description_digital;
}
#endif