|
1 | 1 | # Power module |
2 | 2 |
|
3 | | -What does this module do |
| 3 | +The power module manages power-related functionality for devices with nPM1300, like the Thingy:91 X, including the following: |
| 4 | +- Monitoring battery voltage and calculating remaining battery percentage. |
| 5 | +- Handling VBUS (USB) connect and disconnect events to enable or disable UART peripherals. |
| 6 | +- Publishing battery percentage updates via zbus messages. |
4 | 7 |
|
5 | 8 | ## Messages |
6 | 9 |
|
| 10 | +The Power module defines and communicates on the `POWER_CHAN` channel. |
| 11 | + |
| 12 | +### Input Messages |
| 13 | + |
| 14 | +- **POWER_BATTERY_PERCENTAGE_SAMPLE_REQUEST** |
| 15 | + Requests a battery percentage sample. |
| 16 | + |
| 17 | +### Output Messages |
| 18 | + |
| 19 | +- **POWER_BATTERY_PERCENTAGE_SAMPLE_RESPONSE** |
| 20 | + Contains the calculated battery percentage. |
| 21 | + |
| 22 | +The power message structure is defined in `power.h`: |
| 23 | + |
| 24 | +```c |
| 25 | +struct power_msg { |
| 26 | + enum power_msg_type type; |
| 27 | + |
| 28 | + /** Contains the current charge of the battery in percentage. */ |
| 29 | + double percentage; |
| 30 | +}; |
| 31 | +``` |
| 32 | + |
7 | 33 | ## Configurations |
8 | 34 |
|
9 | | -Kconfig and device tree |
| 35 | +The following Kconfig options control this module’s behavior: |
| 36 | + |
| 37 | +- **CONFIG_APP_POWER** |
| 38 | + Enables the Power module. |
| 39 | + |
| 40 | +- **CONFIG_APP_POWER_DISABLE_UART_ON_VBUS_REMOVED** |
| 41 | + If enabled, suspends UART devices when VBUS is removed. |
| 42 | + |
| 43 | +- **CONFIG_APP_POWER_THREAD_STACK_SIZE** |
| 44 | + Size of the Power module’s thread stack. |
| 45 | + |
| 46 | +- **CONFIG_APP_POWER_WATCHDOG_TIMEOUT_SECONDS** |
| 47 | + Defines the watchdog timeout for the module. Must be larger than the message processing timeout. |
| 48 | + |
| 49 | +- **CONFIG_APP_POWER_MSG_PROCESSING_TIMEOUT_SECONDS** |
| 50 | + Maximum time spent processing a single message. |
| 51 | + |
| 52 | +See the `Kconfig.power` file in the module's directory for more details on the available Kconfig options. |
| 53 | + |
| 54 | +## Kconfig and device tree |
| 55 | + |
| 56 | +- The Power module uses `npm1300_charger` as specified by device tree. |
| 57 | +- The two UART devices `uart0_dev` and `uart1_dev` defined in device tree will be enabled or disabled based on VBUS events. |
10 | 58 |
|
11 | 59 | ## State diagram |
| 60 | + |
| 61 | +The Power module uses a minimal state machine with a single state, **STATE_RUNNING**. In this state, the module: |
| 62 | +1. Initializes the charger and optional fuel gauge in the state entry function. |
| 63 | +2. Waits for messages to arrive via zbus. |
| 64 | +3. Handles battery sample requests and publishes results. |
| 65 | +4. Monitors for VBUS events to enable or disable UART devices. |
0 commit comments