Skip to content

Commit

Permalink
common/particle-mesh: add support for saul_bat_voltage
Browse files Browse the repository at this point in the history
  • Loading branch information
miri64 committed Nov 22, 2024
1 parent 7698aca commit d892c2c
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 0 deletions.
1 change: 1 addition & 0 deletions boards/common/particle-mesh/Makefile.dep
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
ifneq (,$(filter saul_default,$(USEMODULE)))
USEMODULE += saul_gpio
USEMODULE += saul_bat_voltage
USEMODULE += saul_pwm
endif

Expand Down
1 change: 1 addition & 0 deletions boards/common/particle-mesh/Makefile.features
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
CPU_MODEL = nrf52840xxaa

# Put defined MCU peripherals here (in alphabetical order)
FEATURES_PROVIDED += board_bat_voltage
FEATURES_PROVIDED += periph_i2c
FEATURES_PROVIDED += periph_pwm
FEATURES_PROVIDED += periph_spi
Expand Down
31 changes: 31 additions & 0 deletions boards/common/particle-mesh/bat_voltage.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (C) 2024 TU Dresden
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/

/**
* @ingroup boards_feather-nrf52840
* @ingroup saul_bat_voltage
* @{
* @file
* @brief Implementation of battery voltage convert function
*
* @author Martine S. Lenders <[email protected]>
*
* @}
*/

#ifdef MODULE_SAUL_BAT_VOLTAGE
#include "saul/bat_voltage.h"

int16_t saul_bat_voltage_convert(int32_t adc_sample)
{
/* See https://community.particle.io/t/can-argon-or-xenon-read-the-battery-state/45554/6
* and https://docs.particle.io/assets/images/xenon/schematic-main.png */
return (int16_t)((adc_sample * 33L * 1403L) / 10000L);
}

#endif /* MODULE_SAUL_BAT_VOLTAGE */
56 changes: 56 additions & 0 deletions boards/common/particle-mesh/include/bat_voltage_params.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (C) 2024 TU Dresden
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/

/**
* @ingroup boards_common_particle-mesh
* @{
*
* @file
* @brief Configuration of SAUL mapped battery voltage information
*
* @author Martine S. Lenders <[email protected]>
*/

#ifndef BAT_VOLTAGE_PARAMS_H
#define BAT_VOLTAGE_PARAMS_H

#include "saul/bat_voltage.h"

#ifdef __cplusplus
extern "C" {
#endif

/**
* @brief Conversion function to convert ADC sample to battery voltage
*
* @param[in] adc_sample The raw ADC sample.
*
* @return Voltage value for phydat.
*/
int16_t saul_bat_voltage_convert(int32_t adc_sample);

/**
* @brief Battery voltage configuration
*/
static const saul_bat_voltage_params_t saul_bat_voltage_params[] =
{
{
.name = "BAT",
.phydat_scale = -3,
.line = ADC_LINE(3),
.res = ADC_RES_10BIT,
.convert = saul_bat_voltage_convert,
},
};

#ifdef __cplusplus
}
#endif

#endif /* BAT_VOLTAGE_PARAMS_H */
/** @} */

0 comments on commit d892c2c

Please sign in to comment.