Skip to content

Commit 6da712a

Browse files
committed
pbio/drv/ioport: Allow GPIO 2 to be disabled.
We can't detect everything this way, but crucially we can still detect the NXT light sensor and NXT color sensor in order to drive their active background processes.
1 parent 7c42024 commit 6da712a

File tree

4 files changed

+54
-4
lines changed

4 files changed

+54
-4
lines changed

lib/pbio/include/pbdrv/ioport.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,19 @@ typedef struct {
8686
* ADC channel for pin 6,
8787
*/
8888
uint8_t adc_p6;
89+
#endif // PBDRV_CONFIG_IOPORT_HAS_ADC
90+
#if PBDRV_CONFIG_IOPORT_HAS_GPIO_P1
8991
/**
9092
* Pin 1 I/O,
9193
*/
9294
pbdrv_gpio_t p1;
95+
#endif // PBDRV_CONFIG_IOPORT_HAS_GPIO_P1
96+
#if PBDRV_CONFIG_IOPORT_HAS_GPIO_P2
9397
/**
9498
* Pin 2 I/O.
9599
*/
96100
pbdrv_gpio_t p2;
97-
#endif // PBDRV_CONFIG_IOPORT_HAS_ADC
101+
#endif // PBDRV_CONFIG_IOPORT_HAS_GPIO_P2
98102
} pbdrv_ioport_pins_t;
99103

100104
#define PBDRV_IOPORT_INDEX_NOT_AVAILABLE (0xFF)

lib/pbio/platform/ev3/pbdrvconfig.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737

3838
#define PBDRV_CONFIG_IOPORT (1)
3939
#define PBDRV_CONFIG_IOPORT_HAS_ADC (1)
40+
#define PBDRV_CONFIG_IOPORT_HAS_GPIO_P1 (1)
41+
#define PBDRV_CONFIG_IOPORT_HAS_GPIO_P2 (1)
4042
#define PBDRV_CONFIG_IOPORT_NUM_DEV (8)
4143

4244
#define PBDRV_CONFIG_I2C (1)

lib/pbio/platform/ev3/platform.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,9 @@ const pbdrv_ioport_platform_data_t pbdrv_ioport_platform_data[PBDRV_CONFIG_IOPOR
303303
.counter_driver_index = PBDRV_IOPORT_INDEX_NOT_AVAILABLE,
304304
.pins = &(pbdrv_ioport_pins_t) {
305305
.p1 = PBDRV_GPIO_EV3_PIN(18, 31, 28, 8, 10),
306+
#if PBDRV_CONFIG_IOPORT_HAS_GPIO_P2
306307
.p2 = PBDRV_GPIO_EV3_PIN(6, 23, 20, 2, 2),
308+
#endif
307309
.p5 = PBDRV_GPIO_EV3_PIN(1, 23, 20, 0, 2),
308310
.p6 = PBDRV_GPIO_EV3_PIN(0, 3, 0, 0, 15),
309311
.uart_buf = PBDRV_GPIO_EV3_PIN(18, 27, 24, 8, 11),
@@ -329,7 +331,9 @@ const pbdrv_ioport_platform_data_t pbdrv_ioport_platform_data[PBDRV_CONFIG_IOPOR
329331
.counter_driver_index = PBDRV_IOPORT_INDEX_NOT_AVAILABLE,
330332
.pins = &(pbdrv_ioport_pins_t) {
331333
.p1 = PBDRV_GPIO_EV3_PIN(18, 23, 20, 8, 12),
334+
#if PBDRV_CONFIG_IOPORT_HAS_GPIO_P2
332335
.p2 = PBDRV_GPIO_EV3_PIN(18, 11, 8, 8, 15),
336+
#endif
333337
.p5 = PBDRV_GPIO_EV3_PIN(0, 7, 4, 0, 14),
334338
.p6 = PBDRV_GPIO_EV3_PIN(0, 11, 8, 0, 13), // DIGI
335339
.uart_buf = PBDRV_GPIO_EV3_PIN(18, 15, 12, 8, 14),
@@ -340,7 +344,7 @@ const pbdrv_ioport_platform_data_t pbdrv_ioport_platform_data[PBDRV_CONFIG_IOPOR
340344
.adc_p1 = 8,
341345
.adc_p6 = 7,
342346
},
343-
.supported_modes = PBIO_PORT_MODE_UART | PBIO_PORT_MODE_I2C | PBIO_PORT_MODE_GPIO_ADC | PBIO_PORT_MODE_LEGO_DCM,
347+
.supported_modes = PBIO_PORT_MODE_UART,
344348
},
345349
{
346350
.port_id = PBIO_PORT_ID_3,
@@ -351,7 +355,9 @@ const pbdrv_ioport_platform_data_t pbdrv_ioport_platform_data[PBDRV_CONFIG_IOPOR
351355
.counter_driver_index = PBDRV_IOPORT_INDEX_NOT_AVAILABLE,
352356
.pins = &(pbdrv_ioport_pins_t) {
353357
.p1 = PBDRV_GPIO_EV3_PIN(19, 3, 0, 8, 9),
358+
#if PBDRV_CONFIG_IOPORT_HAS_GPIO_P2
354359
.p2 = PBDRV_GPIO_EV3_PIN(16, 27, 24, 7, 11),
360+
#endif
355361
.p5 = PBDRV_GPIO_EV3_PIN(0, 15, 12, 0, 12),
356362
.p6 = PBDRV_GPIO_EV3_PIN(2, 7, 4, 1, 14),
357363
.uart_buf = PBDRV_GPIO_EV3_PIN(17, 3, 0, 7, 9),
@@ -362,7 +368,7 @@ const pbdrv_ioport_platform_data_t pbdrv_ioport_platform_data[PBDRV_CONFIG_IOPOR
362368
.adc_p1 = 10,
363369
.adc_p6 = 9,
364370
},
365-
.supported_modes = PBIO_PORT_MODE_UART | PBIO_PORT_MODE_I2C | PBIO_PORT_MODE_GPIO_ADC | PBIO_PORT_MODE_LEGO_DCM,
371+
.supported_modes = PBIO_PORT_MODE_UART,
366372
},
367373
{
368374
.port_id = PBIO_PORT_ID_4,
@@ -373,7 +379,9 @@ const pbdrv_ioport_platform_data_t pbdrv_ioport_platform_data[PBDRV_CONFIG_IOPOR
373379
.counter_driver_index = PBDRV_IOPORT_INDEX_NOT_AVAILABLE,
374380
.pins = &(pbdrv_ioport_pins_t) {
375381
.p1 = PBDRV_GPIO_EV3_PIN(19, 11, 8, 6, 4),
382+
#if PBDRV_CONFIG_IOPORT_HAS_GPIO_P2
376383
.p2 = PBDRV_GPIO_EV3_PIN(17, 7, 4, 7, 8),
384+
#endif
377385
.p5 = PBDRV_GPIO_EV3_PIN(1, 27, 24, 0, 1),
378386
.p6 = PBDRV_GPIO_EV3_PIN(2, 3, 0, 1, 15),
379387
.uart_buf = PBDRV_GPIO_EV3_PIN(16, 31, 28, 7, 10),

lib/pbio/src/port_dcm_ev3.c

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ typedef enum {
4646
/**
4747
* Pin 2 GPIO is high.
4848
*/
49+
#if PBDRV_CONFIG_IOPORT_HAS_GPIO_P2
4950
PIN_STATE_P2_HIGH = 1 << 4,
51+
#else
52+
PIN_STATE_P2_HIGH = 0,
53+
#endif
5054
/**
5155
* Pin 5 GPIO is high.
5256
*/
@@ -70,6 +74,7 @@ typedef enum {
7074
* only one known device.
7175
*/
7276
typedef enum {
77+
#if PBDRV_CONFIG_IOPORT_HAS_GPIO_P2
7378
/**
7479
* Device category is EV3 UART sensor. P6 and ADC6 are arbitrary (can be set by sensor TX).
7580
*/
@@ -78,6 +83,7 @@ typedef enum {
7883
* Device category is EV3 analog sensor.
7984
*/
8085
DCM_CATEGORY_EV3_ANALOG = PIN_STATE_ADC1_100_to_3100 | PIN_STATE_P2_HIGH,
86+
#endif // PBDRV_CONFIG_IOPORT_HAS_GPIO_P2
8187
/**
8288
* No device is connected.
8389
*/
@@ -148,22 +154,30 @@ static uint32_t pbio_port_dcm_get_mv(const pbdrv_ioport_pins_t *pins, uint8_t pi
148154
*/
149155
static pbio_port_dcm_category_t pbio_port_dcm_get_category(pbio_port_dcm_pin_state_t state) {
150156

157+
#if PBDRV_CONFIG_IOPORT_HAS_GPIO_P2
151158
if ((state | PIN_STATE_MASK_P6) == DCM_CATEGORY_LUMP) {
152159
return DCM_CATEGORY_LUMP;
153160
}
161+
#endif
154162

155163
if ((state | PIN_STATE_MASK_P6) == DCM_CATEGORY_NXT_COLOR) {
156164
return DCM_CATEGORY_NXT_COLOR;
157165
}
158166

167+
#if PBDRV_CONFIG_IOPORT_HAS_GPIO_P2
159168
if ((state | PIN_STATE_MASK_P1) == DCM_CATEGORY_NXT_ANALOG_OTHER) {
160169
return DCM_CATEGORY_NXT_ANALOG_OTHER;
161170
}
171+
#endif
162172

163173
if ((state | PIN_STATE_MASK_P1) == DCM_CATEGORY_NXT_LIGHT) {
164174
return DCM_CATEGORY_NXT_LIGHT;
165175
}
166176

177+
#if !PBDRV_CONFIG_IOPORT_HAS_GPIO_P2
178+
return DCM_CATEGORY_NONE;
179+
#endif
180+
167181
// All other can be tested for equality.
168182
return (pbio_port_dcm_category_t)state;
169183
}
@@ -191,17 +205,19 @@ static pbio_port_dcm_pin_state_t pbio_port_dcm_get_state(const pbdrv_ioport_pins
191205
}
192206

193207
// Get the GPIO state for pins 2, 5, and 6.
208+
#if PBDRV_CONFIG_IOPORT_HAS_GPIO_P2
194209
if (pbdrv_gpio_input(&pins->p2)) {
195210
state |= PIN_STATE_P2_HIGH;
196211
}
212+
#endif
197213
if (pbdrv_gpio_input(&pins->p5)) {
198214
state |= PIN_STATE_P5_HIGH;
199215
}
200216
if (pbdrv_gpio_input(&pins->p6)) {
201217
state |= PIN_STATE_P6_HIGH;
202218
}
203219

204-
#if DEBUG == 2
220+
#if DEBUG == 2 && PBDRV_CONFIG_IOPORT_HAS_GPIO_P2
205221
debug_pr("%d::: p1: %dmv p2:%d p5:%d p6:%d (%d mv)\n",
206222
pbio_port_dcm_get_category(state),
207223
adc1,
@@ -350,6 +366,7 @@ pbio_error_t pbio_port_dcm_thread(pbio_os_state_t *state, pbio_os_timer_t *timer
350366
// Now run processes for devices that require a process, and otherwise
351367
// wait for disconnection.
352368

369+
#if PBDRV_CONFIG_IOPORT_HAS_GPIO_P2
353370
if (dcm->category == DCM_CATEGORY_LUMP) {
354371
debug_pr("Continue as LUMP process\n");
355372
// Exit EV3 device manager, letting LUMP manager take over.
@@ -365,6 +382,7 @@ pbio_error_t pbio_port_dcm_thread(pbio_os_state_t *state, pbio_os_timer_t *timer
365382
debug_pr("Stopped NXT temperature sensor process.\n");
366383
return PBIO_SUCCESS;
367384
}
385+
#endif
368386

369387
if (dcm->category == DCM_CATEGORY_NXT_LIGHT) {
370388
debug_pr("Reading NXT Light Sensor until disconnected.\n");
@@ -427,9 +445,11 @@ pbio_error_t pbio_port_dcm_thread(pbio_os_state_t *state, pbio_os_timer_t *timer
427445
dcm->nxt_rgba.b = pbio_port_dcm_get_mv(pins, 6);
428446
}
429447
pbdrv_gpio_out_low(&pins->p5);
448+
debug_pr("Color Sensor disconnected.\n");
430449
return PBIO_SUCCESS;
431450
}
432451

452+
#if PBDRV_CONFIG_IOPORT_HAS_GPIO_P2
433453
// For everything else, disconnection is detected by just one pin going
434454
// high rather than all pins going back to the none state. This is
435455
// because other pins are used for data transfer and may vary between
@@ -443,6 +463,7 @@ pbio_error_t pbio_port_dcm_thread(pbio_os_state_t *state, pbio_os_timer_t *timer
443463
PBIO_OS_AWAIT_MS(state, timer, DCM_LOOP_TIME_MS);
444464
}
445465
debug_pr("Device disconnected\n");
466+
#endif // PBDRV_CONFIG_IOPORT_HAS_GPIO_P2
446467

447468
PBIO_OS_ASYNC_END(PBIO_SUCCESS);
448469
}
@@ -480,6 +501,7 @@ pbio_error_t pbio_port_dcm_assert_type_id(pbio_port_dcm_t *dcm, lego_device_type
480501
}
481502

482503
switch (*expected_type_id) {
504+
#if PBDRV_CONFIG_IOPORT_HAS_GPIO_P2
483505
case LEGO_DEVICE_TYPE_ID_ANY_LUMP_UART:
484506
return matches_category(dcm, DCM_CATEGORY_LUMP);
485507
case LEGO_DEVICE_TYPE_ID_EV3_TOUCH_SENSOR:
@@ -503,15 +525,29 @@ pbio_error_t pbio_port_dcm_assert_type_id(pbio_port_dcm_t *dcm, lego_device_type
503525
PBIO_SUCCESS : PBIO_ERROR_NO_DEV;
504526
default:
505527
return PBIO_ERROR_NO_DEV;
528+
#else
529+
// On NXT without GPIO2, we can only definitively assert the color
530+
// sensor and the light sensor.
531+
case LEGO_DEVICE_TYPE_ID_ANY_LUMP_UART:
532+
return PBIO_ERROR_NO_DEV;
533+
case LEGO_DEVICE_TYPE_ID_NXT_COLOR_SENSOR:
534+
return matches_category(dcm, DCM_CATEGORY_NXT_COLOR);
535+
case LEGO_DEVICE_TYPE_ID_NXT_LIGHT_SENSOR:
536+
return matches_category(dcm, DCM_CATEGORY_NXT_LIGHT);
537+
default:
538+
return PBIO_SUCCESS;
539+
#endif // PBDRV_CONFIG_IOPORT_HAS_GPIO_P2
506540
}
507541
}
508542

509543
uint32_t pbio_port_dcm_get_analog_value(pbio_port_dcm_t *dcm, const pbdrv_ioport_pins_t *pins, bool active) {
510544

545+
#if PBDRV_CONFIG_IOPORT_HAS_GPIO_P2
511546
// This category measures analog on pin 6.
512547
if (dcm->category == DCM_CATEGORY_EV3_ANALOG) {
513548
return pbio_port_dcm_get_mv(pins, 6);
514549
}
550+
#endif
515551

516552
// Some NXT sensors have an active mode by setting P5 high.
517553
if (active) {

0 commit comments

Comments
 (0)