From 46f4dfbdba53e7780f0e54035424b597d62c6f93 Mon Sep 17 00:00:00 2001 From: tychovrahe Date: Fri, 10 Jan 2025 13:03:33 +0100 Subject: [PATCH] feat(ble): power management [no changelog] --- west/trezor/trezor-ble/CMakeLists.txt | 2 + west/trezor/trezor-ble/prj.conf | 8 ++- west/trezor/trezor-ble/src/main.c | 3 + .../inc/power_management/power_management.h | 23 +++++++ .../src/power_management/power_management.c | 67 +++++++++++++++++++ .../src/trz_comm/inc/trz_comm/trz_comm.h | 1 + .../trezor/trezor-ble/src/trz_comm/trz_comm.c | 5 ++ 7 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 west/trezor/trezor-ble/src/power_management/inc/power_management/power_management.h create mode 100644 west/trezor/trezor-ble/src/power_management/power_management.c diff --git a/west/trezor/trezor-ble/CMakeLists.txt b/west/trezor/trezor-ble/CMakeLists.txt index 452e96d4ad..4446c38e84 100644 --- a/west/trezor/trezor-ble/CMakeLists.txt +++ b/west/trezor/trezor-ble/CMakeLists.txt @@ -18,6 +18,7 @@ target_sources(app PRIVATE src/ble/bonds.c src/ble/pairing.c src/ble/ble.c + src/power_management/power_management.c src/trz_comm/uart.c src/trz_comm/spi.c src/trz_comm/trz_comm.c @@ -27,6 +28,7 @@ target_sources(app PRIVATE include_directories(src/signals/inc) include_directories(src/trz_comm/inc) include_directories(src/ble/inc) +include_directories(src/power_management/inc) # NORDIC SDK APP END diff --git a/west/trezor/trezor-ble/prj.conf b/west/trezor/trezor-ble/prj.conf index d570620a4b..c9f8e998da 100644 --- a/west/trezor/trezor-ble/prj.conf +++ b/west/trezor/trezor-ble/prj.conf @@ -27,8 +27,12 @@ CONFIG_GPIO=y CONFIG_HEAP_MEM_POOL_SIZE=2048 - - +# Enable system-wide Power Management +CONFIG_PM=y +CONFIG_PM_DEVICE=y +# (Optional) Enable device power management at runtime +CONFIG_PM_DEVICE_RUNTIME=y +CONFIG_POWEROFF=y CONFIG_BT=y CONFIG_BT_PERIPHERAL=y diff --git a/west/trezor/trezor-ble/src/main.c b/west/trezor/trezor-ble/src/main.c index 2cc7acb88f..10ee4e1412 100644 --- a/west/trezor/trezor-ble/src/main.c +++ b/west/trezor/trezor-ble/src/main.c @@ -39,6 +39,7 @@ #include #include +#include #include #include @@ -54,6 +55,8 @@ int main(void) { ble_init(); + power_management_init(); + signals_fw_running(true); for (;;) { diff --git a/west/trezor/trezor-ble/src/power_management/inc/power_management/power_management.h b/west/trezor/trezor-ble/src/power_management/inc/power_management/power_management.h new file mode 100644 index 0000000000..492b4ad478 --- /dev/null +++ b/west/trezor/trezor-ble/src/power_management/inc/power_management/power_management.h @@ -0,0 +1,23 @@ +/* + * This file is part of the Trezor project, https://trezor.io/ + * + * Copyright (c) SatoshiLabs + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once + +// Initialize the power management module +void power_management_init(void); diff --git a/west/trezor/trezor-ble/src/power_management/power_management.c b/west/trezor/trezor-ble/src/power_management/power_management.c new file mode 100644 index 0000000000..a779eb2bba --- /dev/null +++ b/west/trezor/trezor-ble/src/power_management/power_management.c @@ -0,0 +1,67 @@ +/* + * This file is part of the Trezor project, https://trezor.io/ + * + * Copyright (c) SatoshiLabs + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include + +#include +#include + +#include +#include +#include + +#include + +#define LOG_MODULE_NAME power_manangement +LOG_MODULE_REGISTER(LOG_MODULE_NAME); + +static K_SEM_DEFINE(power_management_ok, 0, 1); + +typedef enum { + PWR_CMD_SYSTEM_OFF = 0x00, +} power_management_cmd_t; + +void power_management_init(void) { k_sem_give(&power_management_ok); } + +static void process_command(uint8_t *data, uint16_t len) { + uint8_t cmd = data[0]; + switch (cmd) { + case PWR_CMD_SYSTEM_OFF: + LOG_INF("System off"); + sys_poweroff(); + break; + default: + break; + } +} + +void power_management_thread(void) { + /* Don't go any further until BLE is initialized */ + k_sem_take(&power_management_ok, K_FOREVER); + + for (;;) { + trz_packet_t *buf = trz_comm_poll_data(NRF_SERVICE_POWER_MANAGEMENT); + process_command(buf->data, buf->len); + k_free(buf); + } +} + +K_THREAD_DEFINE(power_management_thread_id, CONFIG_DEFAULT_THREAD_STACK_SIZE, + power_management_thread, NULL, NULL, NULL, 7, 0, 0); diff --git a/west/trezor/trezor-ble/src/trz_comm/inc/trz_comm/trz_comm.h b/west/trezor/trezor-ble/src/trz_comm/inc/trz_comm/trz_comm.h index 2955f171c5..d2c9037ce7 100644 --- a/west/trezor/trezor-ble/src/trz_comm/inc/trz_comm/trz_comm.h +++ b/west/trezor/trezor-ble/src/trz_comm/inc/trz_comm/trz_comm.h @@ -27,6 +27,7 @@ typedef enum { NRF_SERVICE_BLE = 0, NRF_SERVICE_BLE_MANAGER = 1, + NRF_SERVICE_POWER_MANAGEMENT = 2, NRF_SERVICE_CNT // Number of services } nrf_service_id_t; diff --git a/west/trezor/trezor-ble/src/trz_comm/trz_comm.c b/west/trezor/trezor-ble/src/trz_comm/trz_comm.c index 20e59413b2..77837c89d3 100644 --- a/west/trezor/trezor-ble/src/trz_comm/trz_comm.c +++ b/west/trezor/trezor-ble/src/trz_comm/trz_comm.c @@ -34,6 +34,7 @@ LOG_MODULE_REGISTER(LOG_MODULE_NAME); static K_FIFO_DEFINE(fifo_uart_rx_ble); static K_FIFO_DEFINE(fifo_uart_rx_ble_manager); +static K_FIFO_DEFINE(fifo_uart_rx_power_management); void trz_comm_init(void) { spi_init(); @@ -65,6 +66,8 @@ void process_rx_msg(uint8_t service_id, uint8_t *data, uint32_t len) { k_fifo_put(&fifo_uart_rx_ble, buf); } else if (service_id == NRF_SERVICE_BLE_MANAGER) { k_fifo_put(&fifo_uart_rx_ble_manager, buf); + } else if (service_id == NRF_SERVICE_POWER_MANAGEMENT) { + k_fifo_put(&fifo_uart_rx_power_management, buf); } else { LOG_WRN("UART_RX unknown service"); k_free(buf); @@ -77,6 +80,8 @@ trz_packet_t *trz_comm_poll_data(nrf_service_id_t service) { return k_fifo_get(&fifo_uart_rx_ble, K_FOREVER); case NRF_SERVICE_BLE_MANAGER: return k_fifo_get(&fifo_uart_rx_ble_manager, K_FOREVER); + case NRF_SERVICE_POWER_MANAGEMENT: + return k_fifo_get(&fifo_uart_rx_power_management, K_FOREVER); default: LOG_WRN("UART_RX unknown service"); return NULL;