Skip to content

Commit

Permalink
Merge pull request #12746 from benpicco/at25mac
Browse files Browse the repository at this point in the history
drivers: add driver for the AT24MAC unique ID chip
  • Loading branch information
benpicco authored Feb 13, 2020
2 parents fc40b54 + 02e1c0a commit f9f222c
Show file tree
Hide file tree
Showing 10 changed files with 388 additions and 0 deletions.
7 changes: 7 additions & 0 deletions boards/avr-rss2/include/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@
extern "C" {
#endif

/**
* @name AT24MAC602 configuration
* @{
*/
#define AT24MAC_PARAM_I2C_DEV I2C_DEV(0)
#define AT24MAC_PARAM_TYPE AT24MAC6XX
/** @} */

/**
* @name LED pin definitions and handlers
Expand Down
9 changes: 9 additions & 0 deletions boards/same54-xpro/include/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@
extern "C" {
#endif

/**
* @name AT24MAC402 configuration
* @{
*/
#define AT24MAC_PARAM_I2C_DEV I2C_DEV(0)
#define AT24MAC_PARAM_I2C_ADDR (0x5E)
#define AT24MAC_PARAM_TYPE AT24MAC4XX
/** @} */

/**
* @name LED pin definitions and handlers
* @{
Expand Down
4 changes: 4 additions & 0 deletions drivers/Makefile.dep
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ ifneq (,$(filter at,$(USEMODULE)))
USEMODULE += isrpipe_read_timeout
endif

ifneq (,$(filter at24mac,$(USEMODULE)))
FEATURES_REQUIRED += periph_i2c
endif

ifneq (,$(filter at30tse75x,$(USEMODULE)))
USEMODULE += xtimer
FEATURES_REQUIRED += periph_i2c
Expand Down
4 changes: 4 additions & 0 deletions drivers/Makefile.include
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ ifneq (,$(filter apa102,$(USEMODULE)))
USEMODULE_INCLUDES += $(RIOTBASE)/drivers/apa102/include
endif

ifneq (,$(filter at24mac,$(USEMODULE)))
USEMODULE_INCLUDES += $(RIOTBASE)/drivers/at24mac/include
endif

ifneq (,$(filter at86rf2xx,$(USEMODULE)))
USEMODULE_INCLUDES += $(RIOTBASE)/drivers/at86rf2xx/include
endif
Expand Down
1 change: 1 addition & 0 deletions drivers/at24mac/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include $(RIOTBASE)/Makefile.base
91 changes: 91 additions & 0 deletions drivers/at24mac/at24mac.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Copyright (C) 2019 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_at24mac
*
* @{
* @file
* @brief Driver for AT24MAC unique ID chip.
*
* @author Benjamin Valentin <benjamin.valentin@ml-pa.com>
*
* @}
*/

#include <errno.h>

#include "at24mac.h"
#include "at24mac_params.h"

#define CMD_READ_EUI48 (0x9A)
#define CMD_READ_EUI64 (0x98)
#define CMD_READ_ID128 (0x80)

static bool _is_valid(at24mac_type_t type, uint8_t reg)
{
if (type == AT24MAC4XX && reg == CMD_READ_EUI64) {
return false;
}

if (type == AT24MAC6XX && reg == CMD_READ_EUI48) {
return false;
}

return true;
}

static int _read_reg(at24mac_t dev, uint8_t reg, void *dst, size_t size)
{
if (dev >= ARRAY_SIZE(at24mac_params)) {
return -ERANGE;
}

int res = 0;
const at24mac_params_t *params = &at24mac_params[dev];

if (!_is_valid(params->type, reg)) {
return -ENOTSUP;
}

res = i2c_acquire(params->i2c_dev);
if (res) {
return res;
}

res = i2c_read_regs(params->i2c_dev, params->i2c_addr,
reg, dst, size, 0);

i2c_release(params->i2c_dev);

return res;
}

int at24mac_get_eui48(at24mac_t dev, eui48_t *dst)
{
return _read_reg(dev, CMD_READ_EUI48, dst, sizeof(*dst));
}

int at24mac_get_eui64(at24mac_t dev, eui64_t *dst)
{
return _read_reg(dev, CMD_READ_EUI64, dst, sizeof(*dst));
}

int at24mac_get_id128(at24mac_t dev, void *dst)
{
return _read_reg(dev, CMD_READ_ID128, dst, AT24MAC_ID_LEN);
}

at24mac_type_t at24mac_get_type(at24mac_t dev)
{
if (dev >= ARRAY_SIZE(at24mac_params)) {
return -ERANGE;
}

return at24mac_params[dev].type;
}
63 changes: 63 additions & 0 deletions drivers/at24mac/include/at24mac_params.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright (C) 2019 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_at24mac
*
* @{
* @file
* @brief Default configuration for the AT24MAC chip
*
* @author Benjamin Valentin <benjamin.valentin@ml-pa.com>
*/

#ifndef AT24MAC_PARAMS_H
#define AT24MAC_PARAMS_H

#include "board.h"
#include "at24mac.h"

#ifdef __cplusplus
extern "C" {
#endif

/**
* @name Set default configuration parameters for AT24Mac driver
* @{
*/
#ifndef AT24MAC_PARAM_I2C_DEV
#define AT24MAC_PARAM_I2C_DEV I2C_DEV(0)
#endif
#ifndef AT24MAC_PARAM_I2C_ADDR
#define AT24MAC_PARAM_I2C_ADDR (0x58)
#endif
#ifndef AT24MAC_PARAM_TYPE
#define AT24MAC_PARAM_TYPE (AT24MAC6XX)
#endif

#ifndef AT24MAC_PARAMS
#define AT24MAC_PARAMS { .i2c_dev = AT24MAC_PARAM_I2C_DEV, \
.i2c_addr = AT24MAC_PARAM_I2C_ADDR,\
.type = AT24MAC_PARAM_TYPE }
#endif
/**@}*/

/**
* @brief Configuration for AT24MACs
*/
static const at24mac_params_t at24mac_params[] =
{
AT24MAC_PARAMS
};

#ifdef __cplusplus
}
#endif

#endif /* AT24MAC_PARAMS_H */
/** @} */
104 changes: 104 additions & 0 deletions drivers/include/at24mac.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* Copyright (C) 2019 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_at24mac unique ID chip
* @brief Device driver interface for the AT24MAC I2C chip
* @{
*
* @file
*
* @author Benjamin Valentin <benjamin.valentin@ml-pa.com>
*/

#ifndef AT24MAC_H
#define AT24MAC_H

#include <stdint.h>
#include "net/eui48.h"
#include "net/eui64.h"
#include "periph/i2c.h"

#ifdef __cplusplus
extern "C" {
#endif

/**
* @brief Device handle type for AT24Mac devices
*/
typedef uint_fast8_t at24mac_t;

#define AT24MAC_ID_LEN (16) /**< Length of ID128 */

/**
* @brief Type of the AT24Mac device
*/
typedef enum {
AT24MAC4XX, /**< provides EUI-48 */
AT24MAC6XX /**< provides EUI-64 */
} at24mac_type_t;

/**
* @brief struct holding all params needed for device communication
*/
typedef struct {
i2c_t i2c_dev; /**< I2C device */
uint8_t i2c_addr; /**< I2C address */
at24mac_type_t type; /**< Device type */
} at24mac_params_t;

/**
* @brief Get the unique EUI48 address from a AT24MAC4xx chip
*
* @param[in] dev Index of the AT24Mac chip in the at24mac_params
* array.
* @param[out] addr memory location to copy the address into.
*
* @return 0 on success, error otherwise.
*/
int at24mac_get_eui48(at24mac_t dev, eui48_t *addr);

/**
* @brief Get the unique EUI64 address from a AT24MAC6xx chip
*
* @param[in] dev Index of the AT24Mac chip in the at24mac_params
* array.
* @param[out] addr memory location to copy the address into.
*
* @return 0 on success, error otherwise.
*/
int at24mac_get_eui64(at24mac_t dev, eui64_t *addr);

/**
* @brief Get the unique ID from a AT24MACxxx chip
*
* @param[in] dev Index of the AT24MAC chip in the at24mac_params
* array.
* @param[out] dst memory location to copy the ID into.
* Must be able to hold at least @ref AT24MAC_ID_LEN bytes.
*
* @return 0 on success, error otherwise.
*/
int at24mac_get_id128(at24mac_t dev, void *dst);

/**
* @brief Get the type of a AT24MACxxx chip
*
* @param[in] dev Index of the AT24MAC chip in the at24mac_params
* array.
*
* @return The type of the device (AT24MAC4XX or AT24MAC6XX)
*/
at24mac_type_t at24mac_get_type(at24mac_t dev);

#ifdef __cplusplus
}
#endif

#endif /* AT24MAC_H */
/** @} */
5 changes: 5 additions & 0 deletions tests/driver_at24mac/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
include ../Makefile.tests_common

USEMODULE += at24mac

include $(RIOTBASE)/Makefile.include
Loading

0 comments on commit f9f222c

Please sign in to comment.