1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-01-24 06:11:06 +00:00

feat(ble): power management

[no changelog]
This commit is contained in:
tychovrahe 2025-01-10 13:03:33 +01:00
parent d1a0bc16c2
commit 46f4dfbdba
7 changed files with 107 additions and 2 deletions

View File

@ -18,6 +18,7 @@ target_sources(app PRIVATE
src/ble/bonds.c src/ble/bonds.c
src/ble/pairing.c src/ble/pairing.c
src/ble/ble.c src/ble/ble.c
src/power_management/power_management.c
src/trz_comm/uart.c src/trz_comm/uart.c
src/trz_comm/spi.c src/trz_comm/spi.c
src/trz_comm/trz_comm.c src/trz_comm/trz_comm.c
@ -27,6 +28,7 @@ target_sources(app PRIVATE
include_directories(src/signals/inc) include_directories(src/signals/inc)
include_directories(src/trz_comm/inc) include_directories(src/trz_comm/inc)
include_directories(src/ble/inc) include_directories(src/ble/inc)
include_directories(src/power_management/inc)
# NORDIC SDK APP END # NORDIC SDK APP END

View File

@ -27,8 +27,12 @@ CONFIG_GPIO=y
CONFIG_HEAP_MEM_POOL_SIZE=2048 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=y
CONFIG_BT_PERIPHERAL=y CONFIG_BT_PERIPHERAL=y

View File

@ -39,6 +39,7 @@
#include <zephyr/logging/log.h> #include <zephyr/logging/log.h>
#include <ble/ble.h> #include <ble/ble.h>
#include <power_management/power_management.h>
#include <signals/signals.h> #include <signals/signals.h>
#include <trz_comm/trz_comm.h> #include <trz_comm/trz_comm.h>
@ -54,6 +55,8 @@ int main(void) {
ble_init(); ble_init();
power_management_init();
signals_fw_running(true); signals_fw_running(true);
for (;;) { for (;;) {

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#pragma once
// Initialize the power management module
void power_management_init(void);

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#include <stdbool.h>
#include <stdint.h>
#include <zephyr/kernel.h>
#include <zephyr/types.h>
#include <zephyr/logging/log.h>
#include <zephyr/sys/crc.h>
#include <zephyr/sys/poweroff.h>
#include <trz_comm/trz_comm.h>
#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);

View File

@ -27,6 +27,7 @@
typedef enum { typedef enum {
NRF_SERVICE_BLE = 0, NRF_SERVICE_BLE = 0,
NRF_SERVICE_BLE_MANAGER = 1, NRF_SERVICE_BLE_MANAGER = 1,
NRF_SERVICE_POWER_MANAGEMENT = 2,
NRF_SERVICE_CNT // Number of services NRF_SERVICE_CNT // Number of services
} nrf_service_id_t; } nrf_service_id_t;

View File

@ -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);
static K_FIFO_DEFINE(fifo_uart_rx_ble_manager); static K_FIFO_DEFINE(fifo_uart_rx_ble_manager);
static K_FIFO_DEFINE(fifo_uart_rx_power_management);
void trz_comm_init(void) { void trz_comm_init(void) {
spi_init(); 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); k_fifo_put(&fifo_uart_rx_ble, buf);
} else if (service_id == NRF_SERVICE_BLE_MANAGER) { } else if (service_id == NRF_SERVICE_BLE_MANAGER) {
k_fifo_put(&fifo_uart_rx_ble_manager, buf); 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 { } else {
LOG_WRN("UART_RX unknown service"); LOG_WRN("UART_RX unknown service");
k_free(buf); 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); return k_fifo_get(&fifo_uart_rx_ble, K_FOREVER);
case NRF_SERVICE_BLE_MANAGER: case NRF_SERVICE_BLE_MANAGER:
return k_fifo_get(&fifo_uart_rx_ble_manager, K_FOREVER); 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: default:
LOG_WRN("UART_RX unknown service"); LOG_WRN("UART_RX unknown service");
return NULL; return NULL;