Skip to content

Commit de7429e

Browse files
committed
pbio/drv: Add deinit exit point.
Allows graceful exit of selected drivers. As an example, stop the IMU driver loop. Fixes pybricks/support#2253
1 parent 4dcb363 commit de7429e

File tree

5 files changed

+29
-3
lines changed

5 files changed

+29
-3
lines changed

lib/pbio/drv/core.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,16 @@ void pbdrv_init(void) {
9090
#endif
9191
pbdrv_ioport_enable_vcc(true);
9292
}
93+
94+
/**
95+
* Deinitializes selected drivers that are not needed after soft-shutdown.
96+
*/
97+
void pbdrv_deinit(void) {
98+
99+
pbdrv_imu_deinit();
100+
101+
while (pbdrv_init_busy()) {
102+
pbio_os_run_processes_once();
103+
}
104+
105+
}

lib/pbio/drv/imu/imu.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111
#include <pbdrv/imu.h>
1212

1313
void pbdrv_imu_init(void);
14+
void pbdrv_imu_deinit(void);
1415

1516
#else // PBDRV_CONFIG_IMU
1617

1718
#define pbdrv_imu_init()
19+
#define pbdrv_imu_deinit()
1820

1921
#endif // PBDRV_CONFIG_IMU
2022

lib/pbio/drv/imu/imu_lsm6ds3tr_c_stm32.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,8 @@ static pbio_error_t pbdrv_imu_lsm6ds3tr_c_stm32_process_thread(pbio_os_state_t *
367367
// value each time we want to read new data. This saves CPU usage since
368368
// we have fewer interrupts per sample.
369369

370-
for (;;) {
370+
while (!(pbdrv_imu_lsm6ds3tr_c_stm32_process.request & PBIO_OS_PROCESS_REQUEST_TYPE_CANCEL)) {
371+
371372
PBIO_OS_AWAIT_UNTIL(state, atomic_exchange(&imu_dev->int1, false));
372373

373374
imu_dev->ctx.read_write_done = false;
@@ -403,8 +404,10 @@ static pbio_error_t pbdrv_imu_lsm6ds3tr_c_stm32_process_thread(pbio_os_state_t *
403404
}
404405
}
405406

406-
// Unreachable
407-
PBIO_OS_ASYNC_END(PBIO_ERROR_FAILED);
407+
// Cancellation complete.
408+
pbdrv_imu_lsm6ds3tr_c_stm32_i2c_reset(hi2c);
409+
pbdrv_init_busy_down();
410+
PBIO_OS_ASYNC_END(PBIO_ERROR_CANCELED);
408411
}
409412

410413
// internal driver interface implementation
@@ -414,6 +417,11 @@ void pbdrv_imu_init(void) {
414417
pbio_os_process_start(&pbdrv_imu_lsm6ds3tr_c_stm32_process, pbdrv_imu_lsm6ds3tr_c_stm32_process_thread, NULL);
415418
}
416419

420+
void pbdrv_imu_deinit(void) {
421+
pbdrv_init_busy_up();
422+
pbio_os_process_make_request(&pbdrv_imu_lsm6ds3tr_c_stm32_process, PBIO_OS_PROCESS_REQUEST_TYPE_CANCEL);
423+
}
424+
417425
// public driver interface implementation
418426

419427
pbio_error_t pbdrv_imu_get_imu(pbdrv_imu_dev_t **imu_dev, pbdrv_imu_config_t **config) {

lib/pbio/include/pbdrv/core.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#define _PBDRV_CORE_H_
1414

1515
void pbdrv_init(void);
16+
void pbdrv_deinit(void);
1617

1718
#endif // _PBDRV_CORE_H_
1819

lib/pbio/sys/main.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include <stdint.h>
99

10+
#include <pbdrv/core.h>
1011
#include <pbdrv/reset.h>
1112
#include <pbdrv/usb.h>
1213
#include <pbio/main.h>
@@ -140,6 +141,7 @@ int main(int argc, char **argv) {
140141
pbsys_deinit();
141142

142143
// Now lower-level processes may shutdown and/or power off.
144+
pbdrv_deinit();
143145
pbsys_status_set(PBIO_PYBRICKS_STATUS_SHUTDOWN);
144146

145147
// The power could be held on due to someone pressing the center button

0 commit comments

Comments
 (0)