-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12746 from benpicco/at25mac
drivers: add driver for the AT24MAC unique ID chip
- Loading branch information
Showing
10 changed files
with
388 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
include $(RIOTBASE)/Makefile.base |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 */ | ||
/** @} */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 */ | ||
/** @} */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
include ../Makefile.tests_common | ||
|
||
USEMODULE += at24mac | ||
|
||
include $(RIOTBASE)/Makefile.include |
Oops, something went wrong.