Battery charger module

The battery charger module is responsible for battery charging control.

Module events

Source Module

Input Event

This Module

Output Event

Sink Module

Power manager module

power_down_event

battery_charger

Source modules for module_state_event

module_state_event

USB state module

usb_state_event

Bluetooth LE advertising module

wake_up_event

Buttons module

HID forward module

Motion module

Power manager module

Wheel module

battery_state_event

LED state module

module_state_event

Sink modules for module_state_event

Note

See the Overview: Firmware architecture for more information about the event-based communication in the nRF Desktop application and about how to read this table.

Configuration

The module implemented in battery_charger.c uses Zephyr’s GPIO driver to control and monitor battery charging. For this reason, you should set CONFIG_GPIO option.

By default, the module is disabled and the CONFIG_DESKTOP_BATTERY_CHARGER_NONE option is selected. Set the option CONFIG_DESKTOP_BATTERY_CHARGER_DISCRETE to enable the module.

The module requires a node labeled battery_charger with a battery-charger compatible set in Devicetree. The charge status output (CSO) GPIO spec, the Enable GPIO spec, and the CSO switching frequency properties are also required. See the following snippet for an example:

battery_charger: battery-charger {
    compatible = "battery-charger";
    cso-gpios = <&gpio0 0 GPIO_ACTIVE_HIGH>;
    enable-gpios = <&gpio0 1 GPIO_ACTIVE_HIGH>;
    cso-switching-freq = <1000>;
};

Implementation details

As required by the USB specification, the module disables the battery charging when USB is suspended to reduce the power consumption. In addition, it also disables charging when USB is disconnected. The module enables charging when USB is powered or active.

Note

Make sure that the hardware configuration enables charging by default (for example, when the pin to enable charging is not configured). Otherwise, it will be impossible to enable charging when the battery is empty.

The module checks the battery state using k_work_delayable. The check is based on the edge interrupt count on the CSO pin in a period of time defined in ERROR_CHECK_TIMEOUT_MS, and on the CSO pin state while the work is processed.

On a battery state change, the new state is sent using battery_state_event. The battery state can have one of the following values:

  • BATTERY_STATE_IDLE - Battery is not being charged (CSO pin set to logical high).

  • BATTERY_STATE_CHARGING - Battery is being charged (CSO pin set to logical low).

  • BATTERY_STATE_ERROR - Battery charger reported an error (a signal with the CONFIG_DESKTOP_BATTERY_CHARGER_CSO_FREQ frequency and a 50% duty cycle on the CSO pin).