Skip to content

Commit 9c1f17b

Browse files
committed
sys/main: Simplify power off routine.
We can turn off sensor lights as soon as poweroff begins to indicate that the user may release the button. We can avoid platform specific poweroff quirks to turn VCC back on in some cases. This can be done the poweroff function which is already platform-specific.
1 parent 100a9ad commit 9c1f17b

File tree

7 files changed

+30
-69
lines changed

7 files changed

+30
-69
lines changed

lib/pbio/drv/ioport/ioport.c

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -64,21 +64,11 @@ pbio_error_t pbdrv_ioport_p5p6_set_mode(const pbdrv_ioport_pins_t *pins, pbdrv_u
6464
return PBIO_ERROR_NOT_SUPPORTED;
6565
}
6666

67-
void pbdrv_ioport_enable_vcc(pbdrv_ioport_vcc_mode_t mode) {
68-
switch (mode) {
69-
case PBDRV_IOPORT_VCC_OFF:
70-
pbdrv_gpio_out_low(&pbdrv_ioport_platform_data_vcc_pin);
71-
return;
72-
case PBDRV_IOPORT_VCC_ON:
73-
pbdrv_gpio_out_high(&pbdrv_ioport_platform_data_vcc_pin);
74-
return;
75-
default:
76-
case PBDRV_IOPORT_VCC_SHUTDOWN:
77-
// Turn off power on pin 4 on all ports. This is set to input instead of
78-
// low to avoid city/move hubs turning back on when button released.
79-
// as soon as the user releases the power button
80-
pbdrv_gpio_input(&pbdrv_ioport_platform_data_vcc_pin);
81-
return;
67+
void pbdrv_ioport_enable_vcc(bool enable) {
68+
if (enable) {
69+
pbdrv_gpio_out_high(&pbdrv_ioport_platform_data_vcc_pin);
70+
} else {
71+
pbdrv_gpio_out_low(&pbdrv_ioport_platform_data_vcc_pin);
8272
}
8373
}
8474

lib/pbio/include/pbdrv/ioport.h

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,6 @@
1717

1818
#include <stdbool.h>
1919

20-
/**
21-
* Modes of the common vcc for the I/O port.
22-
*/
23-
typedef enum {
24-
/**
25-
* Power to the ports is off.
26-
*/
27-
PBDRV_IOPORT_VCC_OFF,
28-
/**
29-
* Power to the ports is on.
30-
*/
31-
PBDRV_IOPORT_VCC_ON,
32-
/**
33-
* Power is set to the platform-specific needs during shutdown.
34-
*/
35-
PBDRV_IOPORT_VCC_SHUTDOWN,
36-
} pbdrv_ioport_vcc_mode_t;
37-
3820
/**
3921
* Modes of one I/O port. Governs the behavior of the P5 and P6 pins.
4022
*/
@@ -115,9 +97,9 @@ typedef struct {
11597
/**
11698
* Enables or disables VCC on pin 4 of all ioports.
11799
*
118-
* @param [in] mode The power state to set.
100+
* @param [in] enable Choose true to enable VCC, false to disable.
119101
*/
120-
void pbdrv_ioport_enable_vcc(pbdrv_ioport_vcc_mode_t mode);
102+
void pbdrv_ioport_enable_vcc(bool enable);
121103

122104
/**
123105
* Sets the mode of the P5P6 pins on this port.

lib/pbio/include/pbio/port_interface.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ typedef enum {
5757

5858
void pbio_port_init(void);
5959

60-
bool pbio_port_poweroff_is_ready(void);
60+
void pbio_port_power_off(void);
6161

6262
void pbio_port_stop_user_actions(bool reset);
6363

@@ -88,8 +88,7 @@ pbio_error_t pbio_port_set_mode(pbio_port_t *port, pbio_port_mode_t mode);
8888
static inline void pbio_port_init(void) {
8989
}
9090

91-
static inline bool pbio_port_poweroff_is_ready(void) {
92-
return true;
91+
static inline void pbio_port_power_off(void) {
9392
}
9493

9594
static inline void pbio_port_stop_user_actions(bool reset) {

lib/pbio/platform/city_hub/platform.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,10 @@ const pbdrv_pwm_stm32_tim_platform_data_t
330330
// RESET
331331

332332
void pbdrv_reset_power_off(void) {
333-
// setting PB11 low cuts the power
333+
// This hub turns itself back on if VCC is off during power off.
334+
pbdrv_ioport_enable_vcc(true);
335+
336+
// Setting PB11 low cuts the power.
334337
GPIOB->BRR = GPIO_BRR_BR_11;
335338
}
336339

lib/pbio/platform/move_hub/platform.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,9 @@ const pbdrv_pwm_stm32_tim_platform_data_t
293293
// RESET
294294

295295
void pbdrv_reset_power_off(void) {
296+
// This hub turns itself back on if VCC is off during power off.
297+
pbdrv_ioport_enable_vcc(true);
298+
296299
// setting PB11 low cuts the power
297300
GPIOB->BRR = GPIO_BRR_BR_11;
298301
}

lib/pbio/src/port.c

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -484,22 +484,19 @@ void pbio_port_stop_user_actions(bool reset) {
484484
* @return Whether the system is ready to power off.
485485
*/
486486

487-
bool pbio_port_poweroff_is_ready(void) {
487+
void pbio_port_power_off(void) {
488488
// Stops motors if active, ignoring sensors that need permanent power.
489489
pbio_port_stop_user_actions(true);
490490

491-
// We also want to power off sensors that need power now and stop their
492-
// processes from triggering any further actions.
491+
// We also want to turn off sensors powered through the motor terminals
492+
// and stop their processes from triggering any further actions.
493493
for (uint8_t i = 0; i < PBIO_CONFIG_PORT_NUM_PORTS; i++) {
494494
pbio_port_t *port = &ports[i];
495495
pbio_port_set_mode(port, PBIO_PORT_MODE_NONE);
496496
}
497497

498-
// Use ioport platform-specific needs for power off.
499-
pbdrv_ioport_enable_vcc(PBDRV_IOPORT_VCC_SHUTDOWN);
500-
501-
// REVISIT: Test button quirk here.
502-
return true;
498+
// This turns off any sensor lights that run on VCC.
499+
pbdrv_ioport_enable_vcc(false);
503500
}
504501

505502
void pbio_port_process_poll(void *port) {

lib/pbio/sys/main.c

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ int main(int argc, char **argv) {
123123
program.start_request_type = PBSYS_MAIN_PROGRAM_START_REQUEST_TYPE_NONE;
124124
}
125125

126+
// Power off sensors and motors, including the ones that are always powered.
127+
// This also makes it easier to see that users can let go of the button.
128+
pbio_port_power_off();
129+
126130
// Stop system processes and save user data before we shutdown.
127131
pbsys_deinit();
128132

@@ -132,29 +136,12 @@ int main(int argc, char **argv) {
132136
// The power could be held on due to someone pressing the center button
133137
// or USB being plugged in, so we have this loop to keep pumping events
134138
// to turn off most of the peripherals and keep the battery charger running.
135-
for (;;) {
136-
// We must handle all pending events before turning the power off the
137-
// first time, otherwise the city hub turns itself back on sometimes.
138-
while (pbio_do_one_event()) {
139-
}
140-
141-
// Prepare ports for power off but don't turn off power yet if
142-
// ports are not ready.
143-
if (!pbio_port_poweroff_is_ready()) {
144-
continue;
145-
}
146-
147-
#if PBSYS_CONFIG_BATTERY_CHARGER
148-
// On hubs with USB battery chargers, we can't turn off power while
149-
// USB is connected, otherwise it disables the op-amp that provides
150-
// the battery voltage to the ADC.
151-
if (pbdrv_usb_get_bcd() != PBDRV_USB_BCD_NONE) {
152-
continue;
153-
}
154-
#endif
155-
156-
pbdrv_reset_power_off();
139+
while (pbsys_status_test(PBIO_PYBRICKS_STATUS_POWER_BUTTON_PRESSED) || pbdrv_usb_get_bcd() != PBDRV_USB_BCD_NONE) {
140+
pbio_do_one_event();
157141
}
142+
143+
// Platform-specific power off.
144+
pbdrv_reset_power_off();
158145
}
159146

160147
#endif // PBSYS_CONFIG_MAIN

0 commit comments

Comments
 (0)