diff --git a/drivers/Makefile.dep b/drivers/Makefile.dep index 7a833f4bc7..f59e4eeed4 100644 --- a/drivers/Makefile.dep +++ b/drivers/Makefile.dep @@ -241,6 +241,10 @@ ifneq (,$(filter dynamixel,$(USEMODULE))) USEMODULE += uart_half_duplex endif +ifneq (,$(filter edbg_eui,$(USEMODULE))) + FEATURES_REQUIRED += periph_i2c +endif + ifneq (,$(filter enc28j60,$(USEMODULE))) FEATURES_REQUIRED += periph_gpio FEATURES_REQUIRED += periph_gpio_irq diff --git a/drivers/edbg_eui/Makefile b/drivers/edbg_eui/Makefile new file mode 100644 index 0000000000..48422e909a --- /dev/null +++ b/drivers/edbg_eui/Makefile @@ -0,0 +1 @@ +include $(RIOTBASE)/Makefile.base diff --git a/drivers/edbg_eui/edbg_eui.c b/drivers/edbg_eui/edbg_eui.c new file mode 100644 index 0000000000..fdfc73ee3d --- /dev/null +++ b/drivers/edbg_eui/edbg_eui.c @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2020 Benjamin Valentin + * + * 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_edbg_eui + * + * @{ + * @file + * @brief Driver for getting a unique ID from the Atmel Embedded Debugger. + * + * @author Benjamin Valentin + * + * @} + */ + +#include "edbg_eui.h" +#include "periph/i2c.h" +#include "net/l2util.h" + +#ifndef EDBG_I2C_DEV +#define EDBG_I2C_DEV I2C_DEV(0) +#endif + +#define EDBG_ADDR (0x28) +#define TOKEN_EUI64 (0xD2) +#define TOKEN_EXT (0xE1) + +int edbg_get_eui64(eui64_t *addr) +{ + int res = 0; + i2c_acquire(EDBG_I2C_DEV); + + /* dummy read for another token - following edbg user manual, section 4.3.1 + * only returns the EUI-64 once, this work-around works reliable though. + */ + if ((res = i2c_read_regs(EDBG_I2C_DEV, EDBG_ADDR, TOKEN_EXT, addr, 2, 0))) { + goto out; + } + + /* EDBG provides an EUI-64 for the internal radio */ + if ((res = i2c_read_regs(EDBG_I2C_DEV, EDBG_ADDR, TOKEN_EUI64, addr, sizeof(*addr), 0))) { + goto out; + } + +out: + i2c_release(EDBG_I2C_DEV); + return res; +} diff --git a/drivers/include/edbg_eui.h b/drivers/include/edbg_eui.h new file mode 100644 index 0000000000..94da042b5b --- /dev/null +++ b/drivers/include/edbg_eui.h @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2020 Benjamin Valentin + * + * 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. + */ + +/** + * @defgroup drivers_edbg_eui Driver for getting MAC address out of Atmel EDBG + * @ingroup drivers_misc + * @brief Device driver interface for the AT24MAC I2C chip + * @{ + * + * @file + * + * @author Benjamin Valentin + */ + +#ifndef EDBG_EUI_H +#define EDBG_EUI_H + +#include +#include "net/eui64.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Get the unique EUI64 address from a Atmel EDBG debugger + * + * @param[out] addr memory location to copy the address into. + * + * @return 0 on success, error otherwise. + */ +int edbg_get_eui64(eui64_t *addr); + +#ifdef __cplusplus +} +#endif + +#endif /* EDBG_EUI_H */ +/** @} */ diff --git a/tests/driver_edbg_eui/Makefile b/tests/driver_edbg_eui/Makefile new file mode 100644 index 0000000000..6b31ffe8ba --- /dev/null +++ b/tests/driver_edbg_eui/Makefile @@ -0,0 +1,8 @@ +BOARD ?= samr21-xpro +BOARD_WHITELIST = samr21-xpro + +include ../Makefile.tests_common + +USEMODULE += edbg_eui + +include $(RIOTBASE)/Makefile.include diff --git a/tests/driver_edbg_eui/main.c b/tests/driver_edbg_eui/main.c new file mode 100644 index 0000000000..eb9402e675 --- /dev/null +++ b/tests/driver_edbg_eui/main.c @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2020 Benjamin Valentin + * + * 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 tests + * @{ + * + * @file + * @brief Test application for the EDBG EUI driver + * + * @author Benjamin Valentin + * + * @} + */ + +#include +#include "edbg_eui.h" + +static int test_get_eui64(void) +{ + eui64_t e64; + if (edbg_get_eui64(&e64) != 0) { + puts("[FAILED]"); + return 1; + } + + printf("EUI-64:"); + for (unsigned i = 0; i < sizeof(e64.uint8); ++i) { + printf(" %02x", e64.uint8[i]); + } + puts(""); + + return 0; +} + +int main(void) +{ + if (test_get_eui64()) { + return -1; + } + + puts("[SUCCESS]"); + + return 0; +}