EMAC driver for the ENC28J60 Ethernet controller. This is a simplified fork of https://github.com/tobiasjaster/ENC28J60-EMAC-Driver published by Tobias Jaster.

Dependents:   MQTT_Hello MQTT_HelloENC28J60

EMAC driver for the ENC28J60 Ethernet controller

https://os.mbed.com/media/uploads/hudakz/enc28j60_module01.jpg

This is a fork (the INT and RST pins are not used) of the ENC28J60-EMAC driver published by Tobias Jaster at

https://github.com/tobiasjaster/ENC28J60-EMAC-Driver

Usage:

  • Connect the ENC28J60 module to the Mbed board as follows:

https://os.mbed.com/media/uploads/hudakz/enc28j60-emac.png

  • Import (add) this ENC28J60-EMAC library to your program.
  • Add a "mbed_app.json" file with the following content to the root directory of your program:

    {
        "target_overrides": {
            "*": {
                "platform.callback-nontrivial": true,
                "enc28j60-emac.mosi":  "D11",
                "enc28j60-emac.miso":  "D12",
                "enc28j60-emac.sck" :  "D13",
                "enc28j60-emac.cs"  :  "D10"
            }
        }
    }
  • Replace "D11", ..., "D10" with the actual pin names you selected on the Mbed board to be used for the SPI communication.
  • To set the MAC address define an array with the desired address bytes and call the "set_hwaddr(mac)" function before calling the network interface "connect" function.

For example:

    const uint8_t       MAC[6] = { 0, 1, 2, 3, 4, 5 };
    EthernetInterface   net;
 
    int main()
    {
        net.get_emac().set_hwaddr(MAC);             // set MAC address
        if (net.connect() != 0) {
            printf("Error: Unable to connect to the network.\n");
            return -1;
        }
     ...
Committer:
hudakz
Date:
Mon Mar 29 08:37:01 2021 +0000
Revision:
3:aa88808326b9
Parent:
0:b599e748252c
Mbed OS Ethernet MAC (EMAC) driver for the ENC28J60 Ethernet controller. This a simplified fork of https://github.com/tobiasjaster/ENC28J60-EMAC-Driver published by Tobias Jaster.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hudakz 0:b599e748252c 1 /*
hudakz 0:b599e748252c 2 * Copyright (c) 2019 Tobias Jaster
hudakz 0:b599e748252c 3 *
hudakz 0:b599e748252c 4 * Modified by Zoltan Hudak
hudakz 0:b599e748252c 5 *
hudakz 0:b599e748252c 6 * Licensed under the Apache License, Version 2.0 (the "License");
hudakz 0:b599e748252c 7 * you may not use this file except in compliance with the License.
hudakz 0:b599e748252c 8 * You may obtain a copy of the License at
hudakz 0:b599e748252c 9 *
hudakz 0:b599e748252c 10 * http://www.apache.org/licenses/LICENSE-2.0
hudakz 0:b599e748252c 11 *
hudakz 0:b599e748252c 12 * Unless required by applicable law or agreed to in writing, software
hudakz 0:b599e748252c 13 * distributed under the License is distributed on an "AS IS" BASIS,
hudakz 0:b599e748252c 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
hudakz 0:b599e748252c 15 * See the License for the specific language governing permissions and
hudakz 0:b599e748252c 16 * limitations under the License.
hudakz 0:b599e748252c 17 */
hudakz 0:b599e748252c 18 #ifndef ENC28J60_ETH_DRV_H_
hudakz 0:b599e748252c 19 #define ENC28J60_ETH_DRV_H_
hudakz 0:b599e748252c 20
hudakz 0:b599e748252c 21 #include "mbed.h"
hudakz 0:b599e748252c 22 #include "enc28j60_reg.h"
hudakz 0:b599e748252c 23 #include "enc28j60_emac_config.h"
hudakz 0:b599e748252c 24
hudakz 0:b599e748252c 25 #define ENC28J60_READWRITE 1
hudakz 0:b599e748252c 26
hudakz 0:b599e748252c 27 /**
hudakz 0:b599e748252c 28 * \brief Error code definitions
hudakz 0:b599e748252c 29 *
hudakz 0:b599e748252c 30 */
hudakz 0:b599e748252c 31 typedef struct
hudakz 0:b599e748252c 32 {
hudakz 0:b599e748252c 33 uint8_t* buf;
hudakz 0:b599e748252c 34 uint16_t len;
hudakz 0:b599e748252c 35 } payload_t;
hudakz 0:b599e748252c 36
hudakz 0:b599e748252c 37 typedef struct
hudakz 0:b599e748252c 38 {
hudakz 0:b599e748252c 39 uint32_t addr;
hudakz 0:b599e748252c 40 payload_t payload;
hudakz 0:b599e748252c 41 } packet_t;
hudakz 0:b599e748252c 42
hudakz 0:b599e748252c 43 /**
hudakz 0:b599e748252c 44 * \brief Error code definitions
hudakz 0:b599e748252c 45 *
hudakz 0:b599e748252c 46 */
hudakz 0:b599e748252c 47 typedef enum
hudakz 0:b599e748252c 48 {
hudakz 0:b599e748252c 49 ENC28J60_ERROR_OK = 0U, /*!< no error */
hudakz 0:b599e748252c 50 ENC28J60_ERROR_TIMEOUT = 1U, /*!< timeout */
hudakz 0:b599e748252c 51 ENC28J60_ERROR_BUSY = 2U, /*!< no error */
hudakz 0:b599e748252c 52 ENC28J60_ERROR_PARAM = 3U, /*!< invalid parameter */
hudakz 0:b599e748252c 53 ENC28J60_ERROR_INTERNAL = 4U, /*!< internal error */
hudakz 0:b599e748252c 54 ENC28J60_ERROR_WRONG_ID = 5U, /*!< internal error */
hudakz 0:b599e748252c 55 ENC28J60_ERROR_NOPACKET = 10U,
hudakz 0:b599e748252c 56 ENC28J60_ERROR_RECEIVE = 11U,
hudakz 0:b599e748252c 57 ENC28J60_ERROR_LASTPACKET = 12U,
hudakz 0:b599e748252c 58 ENC28J60_ERROR_POSITIONLENGTH = 13U, /*!< internal error */
hudakz 0:b599e748252c 59 ENC28J60_ERROR_SIZE = 20U, /*!< internal error */
hudakz 0:b599e748252c 60 ENC28J60_ERROR_FIFOFULL = 21U, /*!< internal error */
hudakz 0:b599e748252c 61 ENC28J60_ERROR_NEXTPACKET = 22U, /*!< internal error */
hudakz 0:b599e748252c 62 } enc28j60_error_t;
hudakz 0:b599e748252c 63
hudakz 0:b599e748252c 64 /**
hudakz 0:b599e748252c 65 * \brief Interrupt source definitions
hudakz 0:b599e748252c 66 *
hudakz 0:b599e748252c 67 */
hudakz 0:b599e748252c 68 typedef enum
hudakz 0:b599e748252c 69 {
hudakz 0:b599e748252c 70 ENC28J60_INTERRUPT_ENABLE = EIE_INTIE,
hudakz 0:b599e748252c 71 ENC28J60_INTERRUPT_RX_PENDING_ENABLE= EIE_PKTIE,
hudakz 0:b599e748252c 72 ENC28J60_INTERRUPT_DMA_ENABLE = EIE_DMAIE,
hudakz 0:b599e748252c 73 ENC28J60_INTERRUPT_LINK_STATE_ENABLE= EIE_LINKIE,
hudakz 0:b599e748252c 74 ENC28J60_INTERRUPT_TX_ENABLE = EIE_TXIE,
hudakz 0:b599e748252c 75 ENC28J60_INTERRUPT_TX_ERROR_ENABLE = EIE_TXERIE,
hudakz 0:b599e748252c 76 ENC28J60_INTERRUPT_RX_ERROR_ENABLE = EIE_RXERIE
hudakz 0:b599e748252c 77 } enc28j60_interrupt_source;
hudakz 0:b599e748252c 78
hudakz 0:b599e748252c 79 class ENC28J60
hudakz 0:b599e748252c 80 {
hudakz 0:b599e748252c 81 public:
hudakz 0:b599e748252c 82 ENC28J60(PinName mosi, PinName miso, PinName sclk, PinName cs);
hudakz 0:b599e748252c 83
hudakz 0:b599e748252c 84 ENC28J60(mbed::SPI * spi, PinName cs);
hudakz 0:b599e748252c 85
hudakz 0:b599e748252c 86 /**
hudakz 0:b599e748252c 87 * \brief Initializes ENC28J60 Ethernet controller to a known default state:
hudakz 0:b599e748252c 88 * - device ID is checked
hudakz 0:b599e748252c 89 * - global interrupt is enabled, but all irq sources are disabled
hudakz 0:b599e748252c 90 * - Establish link enabled
hudakz 0:b599e748252c 91 * - Rx enabled
hudakz 0:b599e748252c 92 * - Tx enabled
hudakz 0:b599e748252c 93 * Init should be called prior to any other process and
hudakz 0:b599e748252c 94 * it's the caller's responsibility to follow proper call order.
hudakz 0:b599e748252c 95 *
hudakz 0:b599e748252c 96 * \return error code /ref enc28j60_error_t
hudakz 0:b599e748252c 97 */
hudakz 0:b599e748252c 98 void init(void);
hudakz 0:b599e748252c 99
hudakz 0:b599e748252c 100 /** This returns a unique 6-byte MAC address, based on the device UID
hudakz 0:b599e748252c 101 * This function overrides hal/common/mbed_interface.c function
hudakz 0:b599e748252c 102 * @param mac A 6-byte array to write the MAC address
hudakz 0:b599e748252c 103 */
hudakz 0:b599e748252c 104 void mbed_mac_address(char* mac);
hudakz 0:b599e748252c 105 MBED_WEAK uint8_t mbed_otp_mac_address(char* mac);
hudakz 0:b599e748252c 106 void mbed_default_mac_address(char* mac);
hudakz 0:b599e748252c 107
hudakz 0:b599e748252c 108 /**
hudakz 0:b599e748252c 109 * \brief Initiates a soft reset, returns failure or success.
hudakz 0:b599e748252c 110 *
hudakz 0:b599e748252c 111 * \return error code /ref enc28j60_error_t
hudakz 0:b599e748252c 112 */
hudakz 0:b599e748252c 113 enc28j60_error_t softReset(void);
hudakz 0:b599e748252c 114
hudakz 0:b599e748252c 115 /**
hudakz 0:b599e748252c 116 * \brief Set maximum transition unit by Rx fifo size.
hudakz 0:b599e748252c 117 * Note: The MTU will be smaller by 512 bytes,
hudakz 0:b599e748252c 118 * because the status uses this fixed space.
hudakz 0:b599e748252c 119 *
hudakz 0:b599e748252c 120 * \param[in] val Size of the fifo in kbytes
hudakz 0:b599e748252c 121 */
hudakz 0:b599e748252c 122 void setRxBufSize(uint32_t val);
hudakz 0:b599e748252c 123
hudakz 0:b599e748252c 124 /**
hudakz 0:b599e748252c 125 * \brief Reset PHY
hudakz 0:b599e748252c 126 *
hudakz 0:b599e748252c 127 * \return error code /ref enc28j60_error_t
hudakz 0:b599e748252c 128 */
hudakz 0:b599e748252c 129 enc28j60_error_t resetPhy(void);
hudakz 0:b599e748252c 130
hudakz 0:b599e748252c 131 /**
hudakz 0:b599e748252c 132 * \brief Enable receive
hudakz 0:b599e748252c 133 */
hudakz 0:b599e748252c 134 void enableMacRecv(void);
hudakz 0:b599e748252c 135
hudakz 0:b599e748252c 136 /**
hudakz 0:b599e748252c 137 * \brief Disable receive
hudakz 0:b599e748252c 138 */
hudakz 0:b599e748252c 139 void disableMacRecv(void);
hudakz 0:b599e748252c 140
hudakz 0:b599e748252c 141 /**
hudakz 0:b599e748252c 142 * \brief Read MAC address from EEPROM.
hudakz 0:b599e748252c 143 *
hudakz 0:b599e748252c 144 * \param[in,out] mac array will include the read MAC address in
hudakz 0:b599e748252c 145 * 6 bytes hexadecimal format.
hudakz 0:b599e748252c 146 * It should be allocated by the caller to 6 bytes.
hudakz 0:b599e748252c 147 *
hudakz 0:b599e748252c 148 * \return error code /ref enc28j60_error_t
hudakz 0:b599e748252c 149 */
hudakz 0:b599e748252c 150 enc28j60_error_t readMacAddr(char* mac);
hudakz 0:b599e748252c 151
hudakz 0:b599e748252c 152 /**
hudakz 0:b599e748252c 153 * \brief Write MAC address to EEPROM.
hudakz 0:b599e748252c 154 *
hudakz 0:b599e748252c 155 * \param[in,out] mac array will include the write MAC address in
hudakz 0:b599e748252c 156 * 6 bytes hexadecimal format.
hudakz 0:b599e748252c 157 * It should be allocated by the caller to 6 bytes.
hudakz 0:b599e748252c 158 *
hudakz 0:b599e748252c 159 * \return error code /ref enc28j60_error_t
hudakz 0:b599e748252c 160 */
hudakz 0:b599e748252c 161 enc28j60_error_t writeMacAddr(char* mac);
hudakz 0:b599e748252c 162
hudakz 0:b599e748252c 163 /**
hudakz 0:b599e748252c 164 * \brief Check device ID.
hudakz 0:b599e748252c 165 *
hudakz 0:b599e748252c 166 * \return error code /ref enc28j60_error_t
hudakz 0:b599e748252c 167 */
hudakz 0:b599e748252c 168 bool check_id(void);
hudakz 0:b599e748252c 169
hudakz 0:b599e748252c 170 /**
hudakz 0:b599e748252c 171 * \brief Get the data size of the Rx buffer, aka Maximum Transition Unit
hudakz 0:b599e748252c 172 *
hudakz 0:b599e748252c 173 * \return Fifo data size in bytes
hudakz 0:b599e748252c 174 */
hudakz 0:b599e748252c 175 enc28j60_error_t setWritePrt(uint16_t position, uint16_t offset);
hudakz 0:b599e748252c 176 enc28j60_error_t transmitPacket(packet_t* packet);
hudakz 0:b599e748252c 177 enc28j60_error_t loadPacketInTxBuffer(packet_t* packet);
hudakz 0:b599e748252c 178
hudakz 0:b599e748252c 179 /**
hudakz 0:b599e748252c 180 * \brief Get the free space of Rx fifo in bytes.
hudakz 0:b599e748252c 181 *
hudakz 0:b599e748252c 182 * \param[in] dev Ethernet device structure \ref enc28j60_eth_dev_t
hudakz 0:b599e748252c 183 *
hudakz 0:b599e748252c 184 * \return Space available to store received data in bytes
hudakz 0:b599e748252c 185 */
hudakz 0:b599e748252c 186 uint32_t getRxBufFreeSpace(void);
hudakz 0:b599e748252c 187
hudakz 0:b599e748252c 188 /**
hudakz 0:b599e748252c 189 * \brief Get the size of next unread packet in Rx buffer, using the peak
hudakz 0:b599e748252c 190 * register, which is not destructive so can be read asynchronously.
hudakz 0:b599e748252c 191 * Warning: In case of heavy receiving load, it's possible this register
hudakz 0:b599e748252c 192 * is not perfectly in sync.
hudakz 0:b599e748252c 193 *
hudakz 0:b599e748252c 194 * \param[in] dev Ethernet device structure \ref enc28j60_eth_dev_t
hudakz 0:b599e748252c 195 *
hudakz 0:b599e748252c 196 * \return Size in bytes of the next packet can be read from Rx fifo, according
hudakz 0:b599e748252c 197 * to the peek register.
hudakz 0:b599e748252c 198 */
hudakz 0:b599e748252c 199 enc28j60_error_t setRxBufReadPtr(uint16_t position);
hudakz 0:b599e748252c 200 enc28j60_error_t getPacketInfo(packet_t* packet);
hudakz 0:b599e748252c 201 void readPacket(packet_t* packet);
hudakz 0:b599e748252c 202 void freeRxBuffer(void);
hudakz 0:b599e748252c 203 uint16_t getRecvPointer(void);
hudakz 0:b599e748252c 204 uint16_t getWritePointer(void);
hudakz 0:b599e748252c 205 void readBuf(uint8_t* data, uint16_t len);
hudakz 0:b599e748252c 206 void writeBuf(uint8_t* data, uint16_t len);
hudakz 0:b599e748252c 207 uint8_t readReg(uint8_t address);
hudakz 0:b599e748252c 208 uint16_t readRegPair(uint8_t address);
hudakz 0:b599e748252c 209 void writeReg(uint8_t address, uint8_t data);
hudakz 0:b599e748252c 210 void writeRegPair(uint8_t address, uint16_t data);
hudakz 0:b599e748252c 211 enc28j60_error_t phyRead(uint8_t address, uint16_t* data);
hudakz 0:b599e748252c 212 enc28j60_error_t phyWrite(uint8_t address, uint16_t data);
hudakz 0:b599e748252c 213 bool linkStatus(void);
hudakz 0:b599e748252c 214 uint8_t readOp(uint8_t op, uint8_t address);
hudakz 0:b599e748252c 215 void writeOp(uint8_t op, uint8_t address, uint8_t data);
hudakz 0:b599e748252c 216 private:
hudakz 0:b599e748252c 217 void _setBank(uint8_t address);
hudakz 0:b599e748252c 218 void _read(uint8_t cmd, uint8_t* buf, uint16_t len, bool blocking);
hudakz 0:b599e748252c 219 void _write(uint8_t cmd, uint8_t* buf, uint16_t len, bool blocking);
hudakz 0:b599e748252c 220 #ifdef ENC28J60_READWRITE
hudakz 0:b599e748252c 221 void _readwrite(uint8_t cmd, uint8_t* readbuf, uint8_t* writebuf, uint16_t len, bool blocking);
hudakz 0:b599e748252c 222 #endif
hudakz 0:b599e748252c 223 SPI* _spi;
hudakz 0:b599e748252c 224 Mutex _SPIMutex;
hudakz 0:b599e748252c 225 DigitalOut _cs;
hudakz 0:b599e748252c 226 uint8_t _bank;
hudakz 0:b599e748252c 227 bool _ready;
hudakz 0:b599e748252c 228 uint32_t _next;
hudakz 0:b599e748252c 229 };
hudakz 0:b599e748252c 230 #endif /* ENC28J60_ETH_DRV_H_ */