Skip to content

Commit 8821de6

Browse files
committed
pbio/drv/ioport: Clean up pin setting.
Split out setting defaults to a separate function so we don't have to call it recursively. Also make the comments about the quadrature mode never changing more explicit.
1 parent 5a25e4b commit 8821de6

File tree

2 files changed

+42
-36
lines changed

2 files changed

+42
-36
lines changed

lib/pbio/drv/ioport/ioport.c

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,42 @@
1010
#include <pbdrv/ioport.h>
1111
#include <pbdrv/uart.h>
1212

13-
pbio_error_t pbdrv_ioport_p5p6_set_mode(const pbdrv_ioport_pins_t *pins, pbdrv_ioport_p5p6_mode_t mode) {
13+
/**
14+
* Resets pins to the default state, which is input with the buffer disabled.
15+
*/
16+
static pbio_error_t pbdrv_ioport_p5p6_pin_reset(const pbdrv_ioport_pins_t *pins) {
1417

15-
if (mode == PBDRV_IOPORT_P5P6_MODE_GPIO_ADC) {
18+
if (!pins) {
19+
return PBIO_ERROR_NOT_SUPPORTED;
20+
}
1621

17-
// Reset pins if this port has GPIO pins.
18-
if (!pins) {
19-
return PBIO_ERROR_NOT_SUPPORTED;
20-
}
22+
pbdrv_gpio_input(&pins->p5);
23+
pbdrv_gpio_input(&pins->p6);
24+
pbdrv_gpio_input(&pins->uart_tx);
25+
pbdrv_gpio_input(&pins->uart_rx);
26+
pbdrv_gpio_out_high(&pins->uart_buf);
2127

22-
pbdrv_gpio_input(&pins->p5);
23-
pbdrv_gpio_input(&pins->p6);
24-
pbdrv_gpio_input(&pins->uart_tx);
25-
pbdrv_gpio_input(&pins->uart_rx);
26-
pbdrv_gpio_out_high(&pins->uart_buf);
28+
// These should be set by default already, but it seems that the
29+
// bootloader on the Technic hub changes these and causes wrong
30+
// detection if we don't make sure pull is disabled.
31+
pbdrv_gpio_set_pull(&pins->p5, PBDRV_GPIO_PULL_NONE);
32+
pbdrv_gpio_set_pull(&pins->p6, PBDRV_GPIO_PULL_NONE);
33+
pbdrv_gpio_set_pull(&pins->uart_buf, PBDRV_GPIO_PULL_NONE);
34+
pbdrv_gpio_set_pull(&pins->uart_tx, PBDRV_GPIO_PULL_NONE);
35+
pbdrv_gpio_set_pull(&pins->uart_rx, PBDRV_GPIO_PULL_NONE);
2736

28-
// These should be set by default already, but it seems that the
29-
// bootloader on the Technic hub changes these and causes wrong
30-
// detection if we don't make sure pull is disabled.
31-
pbdrv_gpio_set_pull(&pins->p5, PBDRV_GPIO_PULL_NONE);
32-
pbdrv_gpio_set_pull(&pins->p6, PBDRV_GPIO_PULL_NONE);
33-
pbdrv_gpio_set_pull(&pins->uart_buf, PBDRV_GPIO_PULL_NONE);
34-
pbdrv_gpio_set_pull(&pins->uart_tx, PBDRV_GPIO_PULL_NONE);
35-
pbdrv_gpio_set_pull(&pins->uart_rx, PBDRV_GPIO_PULL_NONE);
37+
return PBIO_SUCCESS;
38+
}
3639

37-
return PBIO_SUCCESS;
40+
pbio_error_t pbdrv_ioport_p5p6_set_mode(const pbdrv_ioport_pins_t *pins, pbdrv_ioport_p5p6_mode_t mode) {
41+
42+
pbio_error_t err;
43+
44+
if (mode == PBDRV_IOPORT_P5P6_MODE_GPIO_ADC) {
45+
// This is the same as the default mode.
46+
return pbdrv_ioport_p5p6_pin_reset(pins);
3847
} else if (mode == PBDRV_IOPORT_P5P6_MODE_UART) {
39-
// First reset all pins to inputs by going to GPIO mode recursively.
40-
pbio_error_t err = pbdrv_ioport_p5p6_set_mode(pins, PBDRV_IOPORT_P5P6_MODE_GPIO_ADC);
48+
err = pbdrv_ioport_p5p6_pin_reset(pins);
4149
if (err != PBIO_SUCCESS) {
4250
return err;
4351
}
@@ -47,22 +55,22 @@ pbio_error_t pbdrv_ioport_p5p6_set_mode(const pbdrv_ioport_pins_t *pins, pbdrv_i
4755
pbdrv_gpio_out_low(&pins->uart_buf);
4856
return PBIO_SUCCESS;
4957
} else if (mode == PBDRV_IOPORT_P5P6_MODE_I2C) {
50-
// First reset all pins to inputs by going to GPIO mode recursively.
51-
pbio_error_t err = pbdrv_ioport_p5p6_set_mode(pins, PBDRV_IOPORT_P5P6_MODE_GPIO_ADC);
58+
err = pbdrv_ioport_p5p6_pin_reset(pins);
5259
if (err != PBIO_SUCCESS) {
5360
return err;
5461
}
62+
// Required for EV3 I2C implementation.
5563
pbdrv_gpio_out_low(&pins->p5);
5664
pbdrv_gpio_input(&pins->p5);
5765
pbdrv_gpio_out_low(&pins->p6);
5866
pbdrv_gpio_input(&pins->p6);
5967
return PBIO_SUCCESS;
6068
} else if (mode == PBDRV_IOPORT_P5P6_MODE_QUADRATURE) {
61-
// In PoweredUP, this is only used for two motors in boost. Its counter
62-
// driver does all the required setup. Its mode can never change. The
63-
// initial driver init does not check errors for default modes since
64-
// they are supported by definition. We can return an error for all
65-
// other ports.
69+
// Ports with this mode support only this mode and nothing else. The
70+
// counter drivers are automatically started on boot. This mode is only
71+
// set at port init when default ports are set, for which the return
72+
// value is not checked. This mode should never change at runtime
73+
// either, so always just return an error.
6674
return PBIO_ERROR_NOT_SUPPORTED;
6775
}
6876

lib/pbio/src/port.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -419,8 +419,8 @@ static void pbio_port_init_one_port(pbio_port_t *port) {
419419
// Optionally used by some ports to get angle information.
420420
pbdrv_counter_get_dev(port->pdata->counter_driver_index, &port->counter);
421421

422-
// Configure basic quadrature-only ports such as BOOST A&B or NXT A&B&C
423-
// without device kind and type id detection.
422+
// Configure basic quadrature-only ports such as BOOST A&B or NXT/EV3 motor
423+
// ports. May also support device detection in their counter driver.
424424
if (port->pdata->supported_modes == PBIO_PORT_MODE_QUADRATURE) {
425425
pbio_port_set_mode(port, PBIO_PORT_MODE_QUADRATURE);
426426
return;
@@ -560,14 +560,12 @@ pbio_error_t pbio_port_set_mode(pbio_port_t *port, pbio_port_mode_t mode) {
560560
case PBIO_PORT_MODE_UART:
561561
// Enable UART on the port. No process needed here. User can
562562
// access UART from their own event loop.
563-
pbdrv_ioport_p5p6_set_mode(port->pdata->pins, PBDRV_IOPORT_P5P6_MODE_UART);
564-
return PBIO_SUCCESS;
563+
return pbdrv_ioport_p5p6_set_mode(port->pdata->pins, PBDRV_IOPORT_P5P6_MODE_UART);
565564
case PBIO_PORT_MODE_I2C:
566565
// Enable I2C on the port. User controlled; no process needed here.
567-
pbdrv_ioport_p5p6_set_mode(port->pdata->pins, PBDRV_IOPORT_P5P6_MODE_I2C);
568-
return PBIO_SUCCESS;
566+
return pbdrv_ioport_p5p6_set_mode(port->pdata->pins, PBDRV_IOPORT_P5P6_MODE_I2C);
569567
case PBIO_PORT_MODE_QUADRATURE:
570-
return PBIO_SUCCESS;
568+
return pbdrv_ioport_p5p6_set_mode(port->pdata->pins, PBDRV_IOPORT_P5P6_MODE_QUADRATURE);
571569
default:
572570
return PBIO_ERROR_NOT_SUPPORTED;
573571
}

0 commit comments

Comments
 (0)