This library is designed to manage the battery charging functionality of the XIAO BLE and XIAO BLE Sense board. It supports the following features for a 3.7V LiPo battery:
- Reading battery voltage.
- Setting charging modes (fast or slow).
- Calculating battery capacity in percentage.
- Registering callbacks for charging state changes.
The library is built on the Zephyr Real-Time Operating System (RTOS). For comprehensive details on Zephyr and how to get started, visit the Zephyr Getting Started Guide.
- Function:
battery_get_millivolt(uint16_t *battery_millivolt)
- Description: Calculates the battery voltage using the ADC and stores the value in the provided pointer.
- Usage:
uint16_t voltage;
int ret = battery_get_millivolt(&voltage);
if (ret == 0) {
// Use the voltage value here
}
- Set Fast Charge (Default)
- Function:
battery_set_fast_charge(void)
- Description: Sets the battery charging current to fast charge mode (100mA).
- Usage:
- Function:
battery_set_fast_charge();
- Set Slow Charge
- Function:
battery_set_slow_charge(void)
- Description: Sets the battery charging current to slow charge mode (50mA).
- Usage:
- Function:
battery_set_fast_charge();
- Function:
battery_get_percentage(uint8_t *battery_percentage, uint16_t battery_millivolt)
- Description: Calculates the battery percentage based on the voltage and stores it in the provided pointer.
- Usage:
uint8_t percentage;
uint16_t voltage;
battery_get_millivolt(&voltage);
battery_get_percentage(&percentage, voltage);
- Function:
battery_register_charging_changed_callback(battery_charging_changed_callback_t callback)
- Description: Registers a callback function that is executed whenever the charging state changes.
- Usage:
void charging_state_changed(bool is_charging) {
if (is_charging) {
// Charging started
} else {
// Charging stopped
}
}
battery_register_charging_changed_callback(charging_state_changed);
- Function:
battery_init(void)
- Description: Initializes the battery charging circuit. Must be called before using other battery functions.
- Usage:
battery_init();
There is an example in the main.c
file that demonstrates how to use the battery management library. This example initializes the battery management library, registers a callback to log charging state changes, and periodically reads and logs the battery voltage and capacity.
The XIAO BLE Sense is equipped with the Adafruit nRF52 Bootloader, which supports UF2 flashing—a simple drag-and-drop method to program your device.
- Use a USB-C cable to connect to the XIAO BLE to your computer
- Double-Click the Reset button (located to the left of the USB connector) quickly. The device should enter bootloader mode and appear as a mass storage device named XIAO on your computer. If the device doesn't appear, ensure your USB cable supports data transfer (some cables are charge-only), and check your computer's device manager or disk utility for new devices.
- Navigate to the 'build/zephyr/' directory and locate the 'zephyr.uf2' file. If you can not find the build folder, build or rebuild the project.
- Drag and drop the zephyr.uf2 file into the XIAO drive that appeared when the device entered bootloader mode or copy the file using your command line.
After the UF2 file transfer is complete, the XIAO BLE will automatically reset and launch the new application.
For additional information on the flashing process and the XIAO BLE Sense board, refer to the Zephyr Board Documentation for XIAO BLE.
Connect to the device's serial port to view log messages from the device. Use a serial terminal application (e.g., PuTTY, Tera Term, Minicom) with the following settings:
- Baud Rate: 115200
- Data Bits: 8
- Parity: None
- Stop Bits: 1