Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

saul: initial import of saul_bat_voltage module #21018

Merged
merged 6 commits into from
Nov 30, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 */
/** @} */
1 change: 1 addition & 0 deletions boards/feather-nrf52840-sense/Makefile.dep
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ ifneq (,$(filter saul_default,$(USEMODULE)))
USEMODULE += lis3mdl
USEMODULE += lsm6dsxx
USEMODULE += saul_gpio
USEMODULE += saul_bat_voltage
USEMODULE += sht3x
USEMODULE += ws281x
endif
Expand Down
1 change: 1 addition & 0 deletions boards/feather-nrf52840-sense/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_spi
FEATURES_PROVIDED += periph_uart
Expand Down
35 changes: 35 additions & 0 deletions boards/feather-nrf52840-sense/bat_voltage.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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://learn.adafruit.com/introducing-the-adafruit-nrf52840-feather/power-management-2
*
* but the reference voltage is actually 3.3V (determined empirically)...
* we return in millivolt so we set the reference voltage to 3300
* instead of 3.3 */
return (int16_t)((adc_sample * 2L * 3300L) / 1024L);
}

#endif /* MODULE_SAUL_BAT_VOLTAGE */
56 changes: 56 additions & 0 deletions boards/feather-nrf52840-sense/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_feather-nrf52840-sense
* @{
*
* @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(5),
.res = ADC_RES_10BIT,
.convert = saul_bat_voltage_convert,
},
};

#ifdef __cplusplus
}
#endif

#endif /* BAT_VOLTAGE_PARAMS_H */
/** @} */
1 change: 1 addition & 0 deletions boards/feather-nrf52840/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 += ws281x
endif

Expand Down
1 change: 1 addition & 0 deletions boards/feather-nrf52840/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_spi
FEATURES_PROVIDED += periph_uart
Expand Down
35 changes: 35 additions & 0 deletions boards/feather-nrf52840/bat_voltage.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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://learn.adafruit.com/introducing-the-adafruit-nrf52840-feather/power-management-2
*
* but the reference voltage is actually 3.3V (determined empirically)...
* we return in millivolt so we set the reference voltage to 3300
* instead of 3.3 */
return (int16_t)((adc_sample * 2L * 3300L) / 1024L);
}

#endif /* MODULE_SAUL_BAT_VOLTAGE */
56 changes: 56 additions & 0 deletions boards/feather-nrf52840/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_feather-nrf52840
* @{
*
* @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(5),
.res = ADC_RES_10BIT,
.convert = saul_bat_voltage_convert,
},
};

#ifdef __cplusplus
}
#endif

#endif /* BAT_VOLTAGE_PARAMS_H */
/** @} */
5 changes: 5 additions & 0 deletions drivers/Makefile.dep
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,11 @@ ifneq (,$(filter ws281x_%,$(USEMODULE)))
USEMODULE += ws281x
endif

ifneq (,$(filter saul_bat_voltage,$(USEMODULE)))
FEATURES_REQUIRED += periph_adc
FEATURES_REQUIRED += board_bat_voltage
endif

ifneq (,$(filter saul_adc,$(USEMODULE)))
FEATURES_REQUIRED += periph_adc
endif
Expand Down
53 changes: 53 additions & 0 deletions drivers/include/saul/bat_voltage.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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 drivers_saul
* @{
*
* @file
* @brief Parameter definitions for mapping battery voltage to SAUL
*
* @author Martine S. Lenders <[email protected]>
*/

#ifndef SAUL_BAT_VOLTAGE_H
#define SAUL_BAT_VOLTAGE_H

#include <stdint.h>

#include "periph/adc.h"

#ifdef __cplusplus
extern "C" {
#endif

/**
* @brief SAUL battery voltage configuration values
*/
typedef struct {
const char *name; /**< name of the device connected to this pin */
int8_t phydat_scale; /**< Phydat scale of the resulting voltage */
adc_t line; /**< ADC line to initialize and expose */
adc_res_t res; /**< ADC resolution */
/**
* @brief Conversion function to convert raw ADC data to voltage
*
* @param[in] adc_sample The raw ADC sample.
*
* @return Voltage value for phydat.
*/
int16_t (*convert)(int32_t adc_sample);
} saul_bat_voltage_params_t;

#ifdef __cplusplus
}
#endif

#endif /* SAUL_BAT_VOLTAGE_H */
/** @} */
3 changes: 3 additions & 0 deletions drivers/saul/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ SRC = saul.c saul_str.c
ifneq (,$(filter saul_gpio,$(USEMODULE)))
SRC += gpio_saul.c
endif
ifneq (,$(filter saul_bat_voltage,$(USEMODULE)))
SRC += bat_voltage_saul.c
endif
ifneq (,$(filter saul_adc,$(USEMODULE)))
SRC += adc_saul.c
endif
Expand Down
Loading
Loading