Skip to content

Commit 95de294

Browse files
committed
Align external target detection to board detection
1 parent 084b83e commit 95de294

File tree

1 file changed

+18
-21
lines changed

1 file changed

+18
-21
lines changed

source/board/nrf5x_dk_ext.c

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,7 @@ unsigned long pin_swdio = PIN_OB_SWDIO; // GPIO msak for SWDIO signal
6969
static void nrf_prerun_board_config(void)
7070
{
7171
// Work around for setting the correct board id based on GPIOs
72-
uint8_t bit1;
73-
uint8_t bit2;
74-
uint8_t bit3;
75-
uint8_t gpio_id;
72+
uint8_t bit1, bit2, bit3, gpio_id;
7673

7774
PIOB->PIO_PER = (1 << 1); // Enable PIO pin PB1
7875
PIOB->PIO_PER = (1 << 2); // Enable PIO pin PB2
@@ -137,37 +134,37 @@ static void nrf_prerun_board_config(void)
137134
// External target detection:
138135
// supports nRF51-DK, nRF52-DK, nRF52840-DK
139136

140-
// EXT_VTG (high if external target is powered)
137+
// Configure GPIO for detection
138+
// - EXT_VTG (high if external target is powered)
141139
PIOB->PIO_PUDR = (1 << 6); // pull-up disable
142140
PIOB->PIO_ODR = (1 << 6); // input
143141
PIOB->PIO_PER = (1 << 6); // GPIO control
144-
bit1 = (PIOB->PIO_PDSR >> 6) & 1; // Read PB6
145-
146-
// EXT_GND_DETECT (low if external target is connected)
142+
// - EXT_GND_DETECT (low if external target is connected)
147143
PIOB->PIO_PUER = (1 << 18); // pull-up enable
148144
PIOB->PIO_ODR = (1 << 18); // input
149145
PIOB->PIO_PER = (1 << 18); // GPIO control
150-
bit2 = (PIOB->PIO_PDSR >> 18) & 1; // Read PB18
151-
PIOB->PIO_PUDR = (1 << 18); // pull-up disable
152-
153-
// nonzero if external target is detected
154-
target_ext = bit1 & !bit2;
155-
156-
// SH_VTG (high if shield-mounted target is powered)
146+
// - SH_VTG (high if shield-mounted target is powered)
157147
PIOB->PIO_PUDR = (1 << 5); // pull-up disable
158148
PIOB->PIO_ODR = (1 << 5); // input
159149
PIOB->PIO_PER = (1 << 5); // GPIO control
160-
bit1 = (PIOB->PIO_PDSR >> 5) & 1; // Read PB5
161-
162-
// SH_GND_DETECT (low if shield-mounted target is connected)
150+
// - SH_GND_DETECT (low if shield-mounted target is connected)
163151
PIOB->PIO_PUER = (1 << 23); // pull-up enable
164152
PIOB->PIO_ODR = (1 << 23); // input
165153
PIOB->PIO_PER = (1 << 23); // GPIO control
166-
bit2 = (PIOB->PIO_PDSR >> 23) & 1; // Read PB23
167-
PIOB->PIO_PUDR = (1 << 23); // pull-up disable
154+
155+
// nonzero if external target is detected
156+
bit1 = (PIOB->PIO_PDSR >> 6) & 1; // Read PB6
157+
bit2 = (PIOB->PIO_PDSR >> 18) & 1; // Read PB18
158+
target_ext = bit1 & !bit2;
168159

169160
// nonzero if shield-mounted target is detected
170-
target_shield = bit1 & !bit2;
161+
bit1 = (PIOB->PIO_PDSR >> 5) & 1; // Read PB5
162+
bit2 = (PIOB->PIO_PDSR >> 23) & 1; // Read PB23
163+
target_shield = bit1 & !bit2;
164+
165+
// Disable pull-ups for detection
166+
PIOB->PIO_PUDR = (1 << 18); // pull-up disable
167+
PIOB->PIO_PUDR = (1 << 23); // pull-up disable
171168

172169
// if external/shield target is detected, re-route SWD signals
173170
if (target_ext) {

0 commit comments

Comments
 (0)