Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
18 changes: 18 additions & 0 deletions drivers/at86rf215/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,24 @@ menuconfig KCONFIG_USEMODULE_AT86RF215

if KCONFIG_USEMODULE_AT86RF215

menuconfig KCONFIG_USEMODULE_AT86RF215_BATMON
bool "AT86RF215 Battery Monitor"
depends on USEMODULE_AT86RF215
help
Configure the AT86RF215 battery monitor using Kconfig.

config AT86RF215_BATMON_THRESHOLD
int "Treshold voltage (in mV) of the battery monitor"
range 1700 3675
default 1800
depends on KCONFIG_USEMODULE_AT86RF215_BATMON
help
If the supply voltage falls below the configured threshold
a SYS_BUS_POWER_EVENT_LOW_VOLTAGE event is generated on the
SYS_BUS_POWER bus.

Battery Monitoring is disabled when the device is in Deep Sleep.

config AT86RF215_USE_CLOCK_OUTPUT
bool "Enable clock output"
help
Expand Down
4 changes: 4 additions & 0 deletions drivers/at86rf215/Makefile.dep
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ ifeq (,$(filter at86rf215_subghz at86rf215_24ghz,$(USEMODULE)))
DEFAULT_MODULE += at86rf215_24ghz
endif

ifneq (,$(filter at86rf215_batmon,$(USEMODULE)))
USEMODULE += sys_bus_power
endif

DEFAULT_MODULE += netdev_ieee802154_multimode

DEFAULT_MODULE += netdev_ieee802154_oqpsk
Expand Down
46 changes: 46 additions & 0 deletions drivers/at86rf215/at86rf215_getset.c
Original file line number Diff line number Diff line change
Expand Up @@ -396,3 +396,49 @@ bool at86rf215_set_idle_from_rx(at86rf215_t *dev, uint8_t state)

return false;
}

int at86rf215_enable_batmon(at86rf215_t *dev, unsigned voltage)
{
uint8_t bmdvc;

/* only configure BATMON on one interface */
if (!is_subGHz(dev) && dev->sibling != NULL) {
dev = dev->sibling;
}

/* ensure valid range */
if (voltage < 1700 || voltage > 3675) {
return -ERANGE;
}

if (voltage > 2500) {
/* high range */
bmdvc = (voltage - 2550 + 37) / 75;
DEBUG("[at86rf215] BATMON set to %u mV\n", 2550 + 75 * bmdvc);

bmdvc |= BMDVC_BMHR_MASK;
} else {
/* low range */
bmdvc = (voltage - 1700 + 25) / 50;
DEBUG("[at86rf215] BATMON set to %u mV\n", 1700 + 50 * bmdvc);
}

/* set batmon threshold */
at86rf215_reg_write(dev, RG_RF_BMDVC, bmdvc);

/* enable interrupt */
at86rf215_reg_or(dev, dev->RF->RG_IRQM, RF_IRQ_BATLOW);

return 0;
}

void at86rf215_disable_batmon(at86rf215_t *dev)
{
/* only configure BATMON on one interface */
if (!is_subGHz(dev) && dev->sibling != NULL) {
dev = dev->sibling;
}

/* disable interrupt */
at86rf215_reg_and(dev, dev->RF->RG_IRQM, ~RF_IRQ_BATLOW);
}
5 changes: 5 additions & 0 deletions drivers/at86rf215/at86rf215_internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ int at86rf215_hardware_reset(at86rf215_t *dev)
return -ENODEV;
}

/* enable battery monitor */
if (IS_ACTIVE(MODULE_AT86RF215_BATMON)) {
at86rf215_enable_batmon(dev, CONFIG_AT86RF215_BATMON_THRESHOLD);
}

/* clear interrupts */
at86rf215_reg_read(dev, RG_RF09_IRQS);
at86rf215_reg_read(dev, RG_RF24_IRQS);
Expand Down
26 changes: 26 additions & 0 deletions drivers/at86rf215/at86rf215_netdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
#include "net/netdev/ieee802154.h"
#include "net/gnrc/netif/internal.h"

#include "sys/bus.h"

#include "at86rf215.h"
#include "at86rf215_netdev.h"
#include "at86rf215_internal.h"
Expand Down Expand Up @@ -551,6 +553,22 @@ static int _set(netdev_t *netdev, netopt_t opt, const void *val, size_t len)
res = sizeof(netopt_enable_t);
break;

#ifdef MODULE_AT86RF215_BATMON
case NETOPT_BATMON:
assert(len <= sizeof(uint16_t));
{
uint16_t mV = *(const uint16_t *)val;
if (mV) {
res = at86rf215_enable_batmon(dev, mV);
res = (res == 0) ? (int)sizeof(uint16_t) : res;
} else {
at86rf215_disable_batmon(dev);
res = sizeof(uint16_t);
}
}
break;
#endif

case NETOPT_RETRANS:
assert(len <= sizeof(uint8_t));
dev->retries_max = *((const uint8_t *)val);
Expand Down Expand Up @@ -1052,6 +1070,14 @@ static void _isr(netdev_t *netdev)
}
}

/* Handle Low Battery IRQ */
#if MODULE_AT86RF215_BATMON
if ((rf_irq_mask & RF_IRQ_BATLOW)) {
msg_bus_t *bus = sys_bus_get(SYS_BUS_POWER);
msg_bus_post(bus, SYS_BUS_POWER_EVENT_LOW_VOLTAGE, NULL);
}
#endif

/* exit early if the interrupt was not for this interface */
if (!((bb_irq_mask & bb_irqs_enabled) ||
(rf_irq_mask & (RF_IRQ_EDC | RF_IRQ_TRXRDY)) || timeout)) {
Expand Down
28 changes: 28 additions & 0 deletions drivers/include/at86rf215.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,16 @@ enum {
#endif
/** @} */

/**
* @name Default Battery Monitor trigger threshold (in mV)
* if battery monitoring is enabled
* @{
*/
#ifndef CONFIG_AT86RF215_BATMON_THRESHOLD
#define CONFIG_AT86RF215_BATMON_THRESHOLD (1800)
#endif
/** @} */

/**
* @name Default PHY Mode
* @{
Expand Down Expand Up @@ -591,6 +601,24 @@ void at86rf215_tx_done(at86rf215_t *dev);
*/
bool at86rf215_cca(at86rf215_t *dev);

/**
* @brief Generate an interrupt if supply voltage drops below the configured
* threshold.
*
* @param[in] dev device to configure
* @param[in] voltage Threshold voltage in mV
*
* @return 0 on success, error otherwise
*/
int at86rf215_enable_batmon(at86rf215_t *dev, unsigned voltage);

/**
* @brief Disable the Battery Monitor interrupt.
*
* @param[in] dev device to configure
*/
void at86rf215_disable_batmon(at86rf215_t *dev);

#ifdef __cplusplus
}
#endif
Expand Down
1 change: 1 addition & 0 deletions makefiles/pseudomodules.inc.mk
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ PSEUDOMODULES += stm32_eth_auto
PSEUDOMODULES += stm32_eth_link_up
PSEUDOMODULES += suit_transport_%
PSEUDOMODULES += suit_storage_%
PSEUDOMODULES += sys_bus_%
PSEUDOMODULES += wakaama_objects_%
PSEUDOMODULES += wifi_enterprise
PSEUDOMODULES += xtimer_on_ztimer
Expand Down
3 changes: 3 additions & 0 deletions sys/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ endif
ifneq (,$(filter suit%,$(USEMODULE)))
DIRS += suit
endif
ifneq (,$(filter sys_bus,$(USEMODULE)))
DIRS += bus
endif
ifneq (,$(filter tcp,$(USEMODULE)))
DIRS += net/transport_layer/tcp
endif
Expand Down
5 changes: 5 additions & 0 deletions sys/Makefile.dep
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ ifneq (,$(filter crypto_%,$(USEMODULE)))
USEMODULE += crypto
endif

ifneq (,$(filter sys_bus_%,$(USEMODULE)))
USEMODULE += sys_bus
USEMODULE += core_msg_bus
endif

ifneq (,$(filter rtt_cmd,$(USEMODULE)))
FEATURES_REQUIRED += periph_rtt
endif
Expand Down
5 changes: 5 additions & 0 deletions sys/auto_init/auto_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ void auto_init(void)
extern void auto_init_event_thread(void);
auto_init_event_thread();
}
if (IS_USED(MODULE_SYS_BUS)) {
LOG_DEBUG("Auto init system buses.\n");
extern void auto_init_sys_bus(void);
auto_init_sys_bus();
}
if (IS_USED(MODULE_MCI)) {
LOG_DEBUG("Auto init mci.\n");
extern void mci_initialize(void);
Expand Down
3 changes: 3 additions & 0 deletions sys/bus/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
MODULE = sys_bus

include $(RIOTBASE)/Makefile.base
29 changes: 29 additions & 0 deletions sys/bus/sys_bus_init.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (C) 2020 ML!PA Consulting GmbH
*
* 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 sys_bus
* @{
*
* @file
* @brief System Buses
*
* @author Benjamin Valentin <benjamin.valentin@ml-pa.com>
* @}
*/

#include "sys/bus.h"

msg_bus_t _sys_bus[SYS_BUS_NUMOF];

void auto_init_sys_bus(void)
{
for (unsigned i = 0; i < SYS_BUS_NUMOF; ++i) {
msg_bus_init(&_sys_bus[i]);
}
}
10 changes: 10 additions & 0 deletions sys/include/net/netopt.h
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,16 @@ typedef enum {
*/
NETOPT_RSSI,

/**
* @brief (uint16_t) Set the battery monitor voltage (in mV).
*
* When set, a @ref SYS_BUS_POWER_EVENT_LOW_VOLTAGE event is generated
* on the SYS_BUS_POWER bus if the supply voltage falls below the set value.
*
* Set to 0 to disable battery monitoring.
*/
NETOPT_BATMON,

/**
* @brief (array of byte array) get link layer multicast groups as array
* of byte arrays (length of each byte array corresponds to the
Expand Down
77 changes: 77 additions & 0 deletions sys/include/sys/bus.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright (C) 2020 ML!PA Consulting GmbH
*
* 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 sys
* @defgroup sys_bus System Buses for common events
* @{
*
* @file
* @brief This provides System Buses for common events.
*
* @warning Bus Events will be lost if receiver message queue is full.
*
* @author Benjamin Valentin <benjamin.valentin@ml-pa.com>
*/

#ifndef SYS_BUS_H
#define SYS_BUS_H

#include <assert.h>
#include "msg_bus.h"

#ifdef __cplusplus
extern "C" {
#endif

/**
* @brief System Bus types
*/
typedef enum {
#if MODULE_SYS_BUS_POWER
SYS_BUS_POWER, /**< Events related to system power */
#endif
SYS_BUS_NUMOF /**< Number of enabled system buses */
} sys_bus_t;

/**
* @brief Power Bus Events
*/
typedef enum {
/**
* @brief Supply voltage fallen below threshold
*/
SYS_BUS_POWER_EVENT_LOW_VOLTAGE,

/* add more if needed, but not more than 32 */
} sys_bus_power_event_t;

/**
* @brief The System Bus array - do not use directly
*/
extern msg_bus_t _sys_bus[SYS_BUS_NUMOF];

/**
* @brief Get a System Bus for a category of events.
*
* @param[in] bus The event category of the the user
* is interested in
*
* @return The message bus for those events
*/
static inline msg_bus_t *sys_bus_get(sys_bus_t bus)
{
return &_sys_bus[bus];
}

#ifdef __cplusplus
}
#endif

#endif /* SYS_BUS_H */
/** @} */
1 change: 1 addition & 0 deletions sys/net/crosslayer/netopt/netopt.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ static const char *_netopt_strmap[] = {
[NETOPT_NUM_GATEWAYS] = "NETOPT_NUM_GATEWAYS",
[NETOPT_LINK_CHECK] = "NETOPT_LINK_CHECK",
[NETOPT_RSSI] = "NETOPT_RSSI",
[NETOPT_BATMON] = "NETOPT_BATMON",
[NETOPT_L2_GROUP] = "NETOPT_L2_GROUP",
[NETOPT_L2_GROUP_LEAVE] = "NETOPT_L2_GROUP_LEAVE",
[NETOPT_NUMOF] = "NETOPT_NUMOF",
Expand Down
1 change: 1 addition & 0 deletions tests/driver_at86rf215/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ BOARD ?= openmote-b

# the radio driver to test
USEMODULE += at86rf215
USEMODULE += at86rf215_batmon

include ../driver_netdev_common/Makefile.netdev.mk
1 change: 0 additions & 1 deletion tests/driver_at86rf215/main.c

This file was deleted.

Loading