Skip to content

Commit fb5ca35

Browse files
authored
Merge pull request #1215 from dhalbert/soft-restart-device-reset
reset I2C and SPI on ctrl-D
2 parents f09537b + 48a3aaf commit fb5ca35

File tree

5 files changed

+23
-4
lines changed

5 files changed

+23
-4
lines changed

ports/nrf/common-hal/busio/I2C.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@ STATIC twim_peripheral_t twim_peripherals[] = {
5454
#endif
5555
};
5656

57-
#define INST_NO 0
57+
void i2c_reset(void) {
58+
for (size_t i = 0 ; i < MP_ARRAY_SIZE(twim_peripherals); i++) {
59+
twim_peripherals[i].in_use = false;
60+
}
61+
}
5862

5963
static uint8_t twi_error_to_mp(const nrfx_err_t err) {
6064
switch (err) {

ports/nrf/common-hal/busio/I2C.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,6 @@ typedef struct {
4545
uint8_t sda_pin_number;
4646
} busio_i2c_obj_t;
4747

48+
void i2c_reset(void);
49+
4850
#endif // MICROPY_INCLUDED_NRF_COMMON_HAL_BUSIO_I2C_H

ports/nrf/common-hal/busio/SPI.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ STATIC spim_peripheral_t spim_peripherals[] = {
5959
#endif
6060
};
6161

62+
void spi_reset(void) {
63+
for (size_t i = 0 ; i < MP_ARRAY_SIZE(spim_peripherals); i++) {
64+
nrfx_spim_uninit(&spim_peripherals[i].spim);
65+
}
66+
}
67+
6268
// Convert frequency to clock-speed-dependent value
6369
static nrf_spim_frequency_t baudrate_to_spim_frequency(const uint32_t baudrate) {
6470
if (baudrate <= 125000) {

ports/nrf/common-hal/busio/SPI.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,6 @@ typedef struct {
4545
uint8_t MISO_pin_number;
4646
} busio_spi_obj_t;
4747

48+
void spi_reset(void);
49+
4850
#endif // MICROPY_INCLUDED_NRF_COMMON_HAL_BUSIO_SPI_H

ports/nrf/supervisor/port.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434

3535
#include "shared-module/gamepad/__init__.h"
3636
#include "common-hal/microcontroller/Pin.h"
37+
#include "common-hal/busio/I2C.h"
38+
#include "common-hal/busio/SPI.h"
3739
#include "common-hal/pulseio/PWMOut.h"
3840
#include "tick.h"
3941

@@ -72,11 +74,14 @@ safe_mode_t port_init(void) {
7274
}
7375

7476
void reset_port(void) {
75-
#ifdef CIRCUITPY_GAMEPAD_TICKS
76-
gamepad_reset();
77-
#endif
77+
#ifdef CIRCUITPY_GAMEPAD_TICKS
78+
gamepad_reset();
79+
#endif
7880

81+
i2c_reset();
82+
spi_reset();
7983
pwmout_reset();
84+
8085
reset_all_pins();
8186
}
8287

0 commit comments

Comments
 (0)