Skip to content

Commit e5fef45

Browse files
committed
probe: check whether we need to actually load or remove probe SMs
Calling DAP_Connect multiple times without calls to DAP_Disconnect would cause a panic.
1 parent 13f18e2 commit e5fef45

File tree

1 file changed

+31
-25
lines changed

1 file changed

+31
-25
lines changed

src/probe.c

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ struct _probe {
6363

6464
// PIO offset
6565
uint offset;
66+
uint initted;
6667
};
6768

6869
static struct _probe probe;
@@ -150,36 +151,38 @@ void probe_init() {
150151
gpio_pull_up(PROBE_PIN_RESET);
151152
// gpio_init will leave the pin cleared and set as input
152153
gpio_init(PROBE_PIN_RESET);
154+
if (!probe.initted) {
155+
uint offset = pio_add_program(pio0, &probe_program);
156+
probe.offset = offset;
153157

154-
uint offset = pio_add_program(pio0, &probe_program);
155-
probe.offset = offset;
158+
pio_sm_config sm_config = probe_program_get_default_config(offset);
156159

157-
pio_sm_config sm_config = probe_program_get_default_config(offset);
160+
// Set SWCLK as a sideset pin
161+
sm_config_set_sideset_pins(&sm_config, PROBE_PIN_SWCLK);
158162

159-
// Set SWCLK as a sideset pin
160-
sm_config_set_sideset_pins(&sm_config, PROBE_PIN_SWCLK);
163+
// Set SWDIO offset
164+
sm_config_set_out_pins(&sm_config, PROBE_PIN_SWDIO, 1);
165+
sm_config_set_set_pins(&sm_config, PROBE_PIN_SWDIO, 1);
166+
sm_config_set_in_pins(&sm_config, PROBE_PIN_SWDIO);
161167

162-
// Set SWDIO offset
163-
sm_config_set_out_pins(&sm_config, PROBE_PIN_SWDIO, 1);
164-
sm_config_set_set_pins(&sm_config, PROBE_PIN_SWDIO, 1);
165-
sm_config_set_in_pins(&sm_config, PROBE_PIN_SWDIO);
168+
// Set SWD and SWDIO pins as output to start. This will be set in the sm
169+
pio_sm_set_consecutive_pindirs(pio0, PROBE_SM, PROBE_PIN_OFFSET, 2, true);
166170

167-
// Set SWD and SWDIO pins as output to start. This will be set in the sm
168-
pio_sm_set_consecutive_pindirs(pio0, PROBE_SM, PROBE_PIN_OFFSET, 2, true);
171+
// shift output right, autopull off, autopull threshold
172+
sm_config_set_out_shift(&sm_config, true, false, 0);
173+
// shift input right as swd data is lsb first, autopush off
174+
sm_config_set_in_shift(&sm_config, true, false, 0);
169175

170-
// shift output right, autopull off, autopull threshold
171-
sm_config_set_out_shift(&sm_config, true, false, 0);
172-
// shift input right as swd data is lsb first, autopush off
173-
sm_config_set_in_shift(&sm_config, true, false, 0);
176+
// Init SM with config
177+
pio_sm_init(pio0, PROBE_SM, offset, &sm_config);
174178

175-
// Init SM with config
176-
pio_sm_init(pio0, PROBE_SM, offset, &sm_config);
179+
// Set up divisor
180+
probe_set_swclk_freq(1000);
177181

178-
// Set up divisor
179-
probe_set_swclk_freq(1000);
180-
181-
// Enable SM
182-
pio_sm_set_enabled(pio0, PROBE_SM, 1);
182+
// Enable SM
183+
pio_sm_set_enabled(pio0, PROBE_SM, 1);
184+
probe.initted = 1;
185+
}
183186

184187
// Jump to write program
185188
probe_write_mode();
@@ -188,8 +191,11 @@ void probe_init() {
188191
void probe_deinit(void)
189192
{
190193
probe_read_mode();
191-
pio_sm_set_enabled(pio0, PROBE_SM, 0);
192-
pio_remove_program(pio0, &probe_program, probe.offset);
194+
if (probe.initted) {
195+
pio_sm_set_enabled(pio0, PROBE_SM, 0);
196+
pio_remove_program(pio0, &probe_program, probe.offset);
197+
probe.initted = 0;
198+
}
193199
}
194200

195201
void probe_handle_read(uint total_bits) {
@@ -306,4 +312,4 @@ void probe_task(void) {
306312
probe_handle_pkt();
307313
}
308314
}
309-
}
315+
}

0 commit comments

Comments
 (0)