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
0 commit comments