From 452ed69b6c095a55be5ea290518b2b23e71526cc Mon Sep 17 00:00:00 2001 From: arighi Date: Mon, 25 Oct 2010 11:09:25 +0000 Subject: [PATCH] network: add generic ethernet module. git-svn-id: https://src.develer.com/svnoss/bertos/trunk@4459 38d2e660-2303-0410-9eaa-f027e97ec537 --- bertos/cfg/cfg_eth.h | 57 +++++++++++++++++++ bertos/drv/eth.h | 128 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 185 insertions(+) create mode 100644 bertos/cfg/cfg_eth.h create mode 100644 bertos/drv/eth.h diff --git a/bertos/cfg/cfg_eth.h b/bertos/cfg/cfg_eth.h new file mode 100644 index 00000000..a3788081 --- /dev/null +++ b/bertos/cfg/cfg_eth.h @@ -0,0 +1,57 @@ +/** + * \file + * + * + * \author Andrea Righi + * + * \brief Configuration file for the generic ethernet driver module + */ + +#ifndef CFG_ETH_H +#define CFG_ETH_H + +/** + * Module logging level. + * + * $WIZ$ type = "enum" + * $WIZ$ value_list = "log_level" + */ +#define ETH_LOG_LEVEL LOG_LVL_WARN + +/** + * module logging format. + * + * $WIZ$ type = "enum" + * $WIZ$ value_list = "log_format" + */ +#define ETH_LOG_FORMAT LOG_FMT_TERSE + +#endif /* CFG_ETH_H */ diff --git a/bertos/drv/eth.h b/bertos/drv/eth.h new file mode 100644 index 00000000..a5f0c2d5 --- /dev/null +++ b/bertos/drv/eth.h @@ -0,0 +1,128 @@ +/** + * \file + * + * + * \author Andrea Righi + * + * \brief Ethernet standard descriptors + * + * $WIZ$ module_name = "eth" + * $WIZ$ module_configuration = "bertos/cfg/cfg_eth.h" + * $WIZ$ module_supports = "at91" + */ + +#ifndef DRV_ETH_H +#define DRV_ETH_H + +#include + +#define ETH_ADDR_LEN 6 +#define ETH_HEAD_LEN 14 +#define ETH_DATA_LEN 1500 +#define ETH_FRAME_LEN (ETH_HEAD_LEN + ETH_DATA_LEN) + +#define ETH_TYPE_IP 0x0800 + +typedef union Ethernet +{ + struct + { + uint8_t dst[ETH_ADDR_LEN]; + uint8_t src[ETH_ADDR_LEN]; + uint16_t type; + uint8_t data[0]; + }; + uint8_t raw[ETH_FRAME_LEN]; +} PACKED Ethernet; + +/** + * Determine if ethernet address \a addr is a all zero. + */ +INLINE int eth_addrIsZero(const uint8_t *addr) +{ + return !(addr[0] | addr[1] | addr[2] | + addr[3] | addr[4] | addr[5]); +} + +/** + * Determine if ethernet address \a addr is a multicast address. + */ +INLINE int eth_addrIsMcast(const uint8_t *addr) +{ + return (0x01 & addr[0]); +} + +/** + * Determine if ethernet address \a addr is locally-assigned (IEEE 802). + */ +INLINE int eth_addrIsLocal(const uint8_t *addr) +{ + return (0x02 & addr[0]); +} + +/** + * Determine if ethernet address \a addr is broadcast. + */ +INLINE bool eth_addrIsBcast(const uint8_t *addr) +{ + return (addr[0] & addr[1] & addr[2] & + addr[3] & addr[4] & addr[5]) == 0xff; +} + +/** + * Check if the ethernet address \a addr is not all zero, is not a multicast + * address, and is not broadcast. + */ +INLINE bool eth_addrIsValid(const uint8_t *addr) +{ + return !eth_addrIsMcast(addr) && !eth_addrIsZero(addr); +} + +/** + * Compare two ethernet addresses: \a addr1 and \a addr2, returns 0 if equal. + */ +INLINE bool eth_addrCmp(const uint8_t *addr1, const uint8_t *addr2) +{ + return !!((addr1[0] ^ addr2[0]) | + (addr1[1] ^ addr2[1]) | + (addr1[2] ^ addr2[2]) | + (addr1[3] ^ addr2[3]) | + (addr1[4] ^ addr2[4]) | + (addr1[5] ^ addr2[5])); +} + +ssize_t eth_send(const uint8_t *buf, size_t len); +ssize_t eth_recv(uint8_t *buf, size_t len); +int eth_init(void); + +extern uint8_t mac_addr[ETH_ADDR_LEN]; + +#endif /* DRV_ETH_H */ -- 2.25.1