Tjoms99 / xiao_sense_nrf52840_battery_lib

A battery management library for the XIAO BLE and XIAO BLE Sense board using Zephyr.
Apache License 2.0
23 stars 4 forks source link
battery nrf52840 seeedstudio xiao-ble xiao-ble-sense zephyr

XIAO BLE Sense (nRF52840) Battery Management Library


GitHub Repo stars Hits

Overview

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:

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. The Nordic Semiconductor nRF Connect SDK tool with VSCode is a good alternative as it includes Zephyr (and much more).

Features

Battery Voltage Reading

uint16_t voltage;

int ret = battery_get_millivolt(&voltage);
if (ret == 0) {
    // Use the voltage value here
} else {
    // Handle error
}

Battery Percentage Calculation

uint8_t percentage;
uint16_t voltage;

battery_get_millivolt(&voltage);

int ret = battery_get_percentage(&percentage, voltage);
if (ret == 0) {
    // Use the percentage value here
} else {
    // Handle error
}

Charging Modes

Charging State Change Callback

void charging_state_changed(bool is_charging) {
    if (is_charging) {
        // Charging started
    } else {
        // Charging stopped
    }
}

int ret = battery_register_charging_callback(charging_state_changed);
if (ret != 0) {
    // Handle error
}

Battery Sample Ready Callback

void battery_sample_ready(uint16_t millivolt) {
    // Process the millivolt value
}

int ret = battery_register_sample_callback(battery_sample_ready);
if (ret != 0) {
    // Handle error
}

One-Shot Battery Sampling

int ret = battery_sample_once();
if (ret != 0) {
    // Handle error
}

Periodic Battery Sampling

Initialization

int ret = battery_init();
if (ret != 0) {
    // Handle error
}

Example Usage

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 callbacks for charging state changes and battery voltage samples, and starts periodic sampling of the battery voltage.

Programming

Adafruit nRF52 Bootloader

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.

Entering Bootloader Mode

  1. Use a USB-C cable to connect to the XIAO BLE to your computer
  2. 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.

Flashing the Firmware

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.

Serial Logging

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: