Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Power off improvements #265

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
- Fixed not able to connect to new Technic Move hub with `LWP3Device()`.
- Removed `gc_collect()` from `tools.run_task()` loop to fix unwanted delays.
- Fixed `await wait(0)` never yielding, so parallel tasks could lock up ([support#1429]).
- Fixed some sensors not powering off when the hub is turned off.

### Removed
- Removed `loop_time` argument to `pybricks.tools.run_task` as this wasn't
Expand Down
1 change: 0 additions & 1 deletion bricks/_common/sources.mk
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,6 @@ PBIO_SRC_C = $(addprefix lib/pbio/,\
sys/command.c \
sys/core.c \
sys/hmi.c \
sys/io_ports.c \
sys/light_matrix.c \
sys/light.c \
sys/main.c \
Expand Down
5 changes: 0 additions & 5 deletions lib/pbio/drv/ioport/ioport.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,11 @@

void pbdrv_ioport_init(void);

void pbdrv_ioport_deinit(void);

#else // PBDRV_CONFIG_IOPORT

static inline void pbdrv_ioport_init(void) {
}

static inline void pbdrv_ioport_deinit(void) {
}

#endif // PBDRV_CONFIG_IOPORT

#endif // _INTERNAL_PBDRV_IOPORT_H
28 changes: 23 additions & 5 deletions lib/pbio/drv/ioport/ioport_pup.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,31 @@ void pbdrv_ioport_init(void) {
#endif
}

void pbdrv_ioport_deinit(void) {
bool pbdrv_ioport_power_off(void) {
init_ports();

// Turn off power on pin 4 on all ports. This is set to input instead of
// low to avoid city/move hubs turning back on when button released.
// as soon as the user releases the power button
pbdrv_gpio_input(&pbdrv_ioport_pup_platform_data.port_vcc);
// Turn off power on pin 4 on all ports.
#if PBDRV_CONFIG_IOPORT_PUP_QUIRK_SHUTDOWN
if (pbdrv_ioport_needs_shutdown_quirk()) {
// Some hubs will turn themselves back on if VCC is off when power is
// turned off, so we have to turn VCC back on as a workaround.
pbdrv_ioport_enable_vcc(true);

return false;
}

// We want VCC off so that lights on sensors turn off while the power
// button is held down. But, this would cause the hub to turn back on
// immediately. So return true to indicate that it is safe to turn off
// power.
pbdrv_ioport_enable_vcc(false);

return true;
#else
pbdrv_ioport_enable_vcc(false);

return false;
#endif
}

#if PBDRV_CONFIG_IOPORT_PUP_QUIRK_POWER_CYCLE
Expand Down
5 changes: 5 additions & 0 deletions lib/pbio/drv/ioport/ioport_pup.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ typedef struct {

extern const pbdrv_ioport_pup_platform_data_t pbdrv_ioport_pup_platform_data;

#if PBDRV_CONFIG_IOPORT_PUP_QUIRK_SHUTDOWN
// this function must be defined in platform.c when quirk is enabled
bool pbdrv_ioport_needs_shutdown_quirk(void);
#endif

#endif // PBDRV_CONFIG_IOPORT_PUP

#endif // _INTERNAL_PBDRV_IOPORT_PUP_H_
15 changes: 15 additions & 0 deletions lib/pbio/include/pbdrv/ioport.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,26 @@
*/
void pbdrv_ioport_enable_vcc(bool enable);

/**
* Sets port I/O pins to default state and powers off VCC.
*
* Some hubs have a quirk where VCC can't be turned off without causing the
* hub to immediately power back on. The return value indicates if it is safe
* to attempt to power off the hub or not.
*
* @return true if the hub cannot be powered off, false otherwise.
*/
bool pbdrv_ioport_power_off(void);

#else // PBDRV_CONFIG_IOPORT

static inline void pbdrv_ioport_enable_vcc(bool enable) {
}

static inline bool pbdrv_ioport_power_off(void) {
return false;
}

#endif // PBDRV_CONFIG_IOPORT

#endif // PBDRV_IOPORT_H
Expand Down
15 changes: 15 additions & 0 deletions lib/pbio/include/pbdrv/motor_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,18 @@ pbio_error_t pbdrv_motor_driver_set_duty_cycle(pbdrv_motor_driver_dev_t *driver,
*/
pbio_error_t pbdrv_motor_driver_get_device_type_id(pbio_port_id_t port, pbdrv_legodev_type_id_t *type_id);

/**
* Powers off all motor outputs.
*/
static inline void pbdrv_motor_driver_power_off(void) {
for (uint8_t i = 0; i < PBDRV_CONFIG_MOTOR_DRIVER_NUM_DEV; i++) {
pbdrv_motor_driver_dev_t *driver;
if (pbdrv_motor_driver_get_dev(i, &driver) == PBIO_SUCCESS) {
pbdrv_motor_driver_coast(driver);
}
}
}

#else

static inline pbio_error_t pbdrv_motor_driver_get_dev(uint8_t id, pbdrv_motor_driver_dev_t **driver) {
Expand All @@ -94,6 +106,9 @@ static inline pbio_error_t pbdrv_motor_driver_get_device_type_id(pbio_port_id_t
return PBIO_ERROR_NOT_SUPPORTED;
}

static inline void pbdrv_motor_driver_power_off(void) {
}

#endif

#endif // _PBDRV_MOTOR_DRIVER_H_
Expand Down
1 change: 1 addition & 0 deletions lib/pbio/platform/city_hub/pbdrvconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#define PBDRV_CONFIG_IOPORT_PUP (1)
#define PBDRV_CONFIG_IOPORT_NUM_DEV (2)
#define PBDRV_CONFIG_IOPORT_PUP_QUIRK_POWER_CYCLE (1)
#define PBDRV_CONFIG_IOPORT_PUP_QUIRK_SHUTDOWN (1)
#define PBDRV_CONFIG_IOPORT_DEBUG_UART (0) // Doesn't work.

#define PBDRV_CONFIG_LED (1)
Expand Down
5 changes: 5 additions & 0 deletions lib/pbio/platform/city_hub/platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,11 @@ const pbdrv_ioport_pup_platform_data_t pbdrv_ioport_pup_platform_data = {
},
};

bool pbdrv_ioport_needs_shutdown_quirk(void) {
// quirk is not needed when button is pressed - button is active low
return !!(GPIOC->IDR & GPIO_IDR_13);
}

// LED

// The constants below are derived from the SunLED XZM2CRKM2DGFBB45SCCB
Expand Down
1 change: 1 addition & 0 deletions lib/pbio/platform/essential_hub/pbdrvconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
#define PBDRV_CONFIG_IOPORT_PUP (1)
#define PBDRV_CONFIG_IOPORT_NUM_DEV (2)
#define PBDRV_CONFIG_IOPORT_PUP_QUIRK_POWER_CYCLE (0)
#define PBDRV_CONFIG_IOPORT_PUP_QUIRK_SHUTDOWN (0)
#define PBDRV_CONFIG_IOPORT_DEBUG_UART (0)

#define PBDRV_CONFIG_LED (1)
Expand Down
1 change: 1 addition & 0 deletions lib/pbio/platform/move_hub/pbdrvconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#define PBDRV_CONFIG_IOPORT_PUP (1)
#define PBDRV_CONFIG_IOPORT_NUM_DEV (2)
#define PBDRV_CONFIG_IOPORT_PUP_QUIRK_POWER_CYCLE (0)
#define PBDRV_CONFIG_IOPORT_PUP_QUIRK_SHUTDOWN (1)
#define PBDRV_CONFIG_IOPORT_DEBUG_UART (0) // Doesn't work.

#define PBDRV_CONFIG_LED (1)
Expand Down
5 changes: 5 additions & 0 deletions lib/pbio/platform/move_hub/platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ const pbdrv_ioport_pup_platform_data_t pbdrv_ioport_pup_platform_data = {
}
};

bool pbdrv_ioport_needs_shutdown_quirk(void) {
// quirk is not needed when button is pressed - button is active low
return !!(GPIOC->IDR & GPIO_IDR_13);
}

// LED

// The constants below are derived from the SunLED XZM2CRKM2DGFBB45SCCB
Expand Down
1 change: 1 addition & 0 deletions lib/pbio/platform/prime_hub/pbdrvconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
#define PBDRV_CONFIG_IOPORT_PUP (1)
#define PBDRV_CONFIG_IOPORT_NUM_DEV (6)
#define PBDRV_CONFIG_IOPORT_PUP_QUIRK_POWER_CYCLE (0)
#define PBDRV_CONFIG_IOPORT_PUP_QUIRK_SHUTDOWN (0)
#define PBDRV_CONFIG_IOPORT_DEBUG_UART (0)

#define PBDRV_CONFIG_LED (1)
Expand Down
1 change: 1 addition & 0 deletions lib/pbio/platform/technic_hub/pbdrvconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
#define PBDRV_CONFIG_IOPORT_PUP (1)
#define PBDRV_CONFIG_IOPORT_NUM_DEV (4)
#define PBDRV_CONFIG_IOPORT_PUP_QUIRK_POWER_CYCLE (1)
#define PBDRV_CONFIG_IOPORT_PUP_QUIRK_SHUTDOWN (0)
#define PBDRV_CONFIG_IOPORT_DEBUG_UART (0)

#define PBDRV_CONFIG_LED (1)
Expand Down
2 changes: 0 additions & 2 deletions lib/pbio/sys/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

#include "core.h"
#include "hmi.h"
#include "io_ports.h"
#include "light.h"
#include "storage.h"
#include "supervisor.h"
Expand All @@ -36,7 +35,6 @@ PROCESS_THREAD(pbsys_system_process, ev, data) {
etimer_reset(&timer);
pbsys_battery_poll();
pbsys_hmi_poll();
pbsys_io_ports_poll();
pbsys_supervisor_poll();
pbsys_program_stop_poll();
}
Expand Down
14 changes: 0 additions & 14 deletions lib/pbio/sys/io_ports.c

This file was deleted.

9 changes: 0 additions & 9 deletions lib/pbio/sys/io_ports.h

This file was deleted.

12 changes: 12 additions & 0 deletions lib/pbio/sys/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

#include <stdint.h>

#include <pbdrv/ioport.h>
#include <pbdrv/motor_driver.h>
#include <pbdrv/reset.h>
#include <pbdrv/usb.h>
#include <pbio/main.h>
Expand Down Expand Up @@ -112,6 +114,16 @@ int main(int argc, char **argv) {
while (pbio_do_one_event()) {
}

pbdrv_motor_driver_power_off();

// Some hubs will turn themselves back on if I/O port VCC is off when
// we call pbdrv_reset_power_off(). So don't turn off power until the
// return value of pbdrv_ioport_power_off() tells us it is safe to do
// so.
if (pbdrv_ioport_power_off()) {
continue;
}

#if PBSYS_CONFIG_BATTERY_CHARGER
// On hubs with USB battery chargers, we can't turn off power while
// USB is connected, otherwise it disables the op-amp that provides
Expand Down
Loading