enc28j60.c 28.4 KB
Newer Older
wuyangyong's avatar
wuyangyong 已提交
1 2
#include "enc28j60.h"

3 4 5
/* #define NET_TRACE */
/* #define ETH_RX_DUMP */
/* #define ETH_TX_DUMP */
wuyangyong's avatar
wuyangyong 已提交
6 7

#ifdef NET_TRACE
8
    #define NET_DEBUG         rt_kprintf
wuyangyong's avatar
wuyangyong 已提交
9
#else
10
    #define NET_DEBUG(...)
wuyangyong's avatar
wuyangyong 已提交
11 12 13 14
#endif /* #ifdef NET_TRACE */

struct enc28j60_tx_list_typedef
{
15 16
    struct enc28j60_tx_list_typedef *prev;
    struct enc28j60_tx_list_typedef *next;
wuyangyong's avatar
wuyangyong 已提交
17 18 19 20 21
    rt_uint32_t addr; /* pkt addr in buffer */
    rt_uint32_t len;  /* pkt len */
    volatile rt_bool_t free; /* 0:busy, 1:free */
};
static struct enc28j60_tx_list_typedef enc28j60_tx_list[2];
22 23
static volatile struct enc28j60_tx_list_typedef *tx_current;
static volatile struct enc28j60_tx_list_typedef *tx_ack;
wuyangyong's avatar
wuyangyong 已提交
24 25 26 27
static struct rt_event tx_event;

/* private enc28j60 define */
/* enc28j60 spi interface function */
28 29
static uint8_t spi_read_op(struct rt_spi_device *spi_device, uint8_t op, uint8_t address);
static void spi_write_op(struct rt_spi_device *spi_device, uint8_t op, uint8_t address, uint8_t data);
wuyangyong's avatar
wuyangyong 已提交
30

31 32
static uint8_t spi_read(struct rt_spi_device *spi_device, uint8_t address);
static void spi_write(struct rt_spi_device *spi_device, rt_uint8_t address, rt_uint8_t data);
wuyangyong's avatar
wuyangyong 已提交
33

34 35 36 37
static void enc28j60_clkout(struct rt_spi_device *spi_device, rt_uint8_t clk);
static void enc28j60_set_bank(struct rt_spi_device *spi_device, uint8_t address);
static uint32_t enc28j60_interrupt_disable(struct rt_spi_device *spi_device);
static void enc28j60_interrupt_enable(struct rt_spi_device *spi_device, uint32_t level);
wuyangyong's avatar
wuyangyong 已提交
38

39 40 41
static uint16_t enc28j60_phy_read(struct rt_spi_device *spi_device, rt_uint8_t address);
static void enc28j60_phy_write(struct rt_spi_device *spi_device, rt_uint8_t address, uint16_t data);
static rt_bool_t enc28j60_check_link_status(struct rt_spi_device *spi_device);
wuyangyong's avatar
wuyangyong 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54

#define enc28j60_lock(dev)      rt_mutex_take(&((struct net_device*)dev)->lock, RT_WAITING_FOREVER);
#define enc28j60_unlock(dev)    rt_mutex_release(&((struct net_device*)dev)->lock);

static struct net_device  enc28j60_dev;
static uint8_t  Enc28j60Bank;
//struct rt_spi_device * spi_device;
static uint16_t NextPacketPtr;

static void _delay_us(uint32_t us)
{
    volatile uint32_t len;
    for (; us > 0; us --)
55
        for (len = 0; len < 20; len++);
wuyangyong's avatar
wuyangyong 已提交
56 57 58
}

/* enc28j60 spi interface function */
59
static uint8_t spi_read_op(struct rt_spi_device *spi_device, uint8_t op, uint8_t address)
wuyangyong's avatar
wuyangyong 已提交
60 61 62 63 64 65 66 67 68
{
    uint8_t send_buffer[2];
    uint8_t recv_buffer[1];
    uint32_t send_size = 1;

    send_buffer[0] = op | (address & ADDR_MASK);
    send_buffer[1] = 0xFF;

    /* do dummy read if needed (for mac and mii, see datasheet page 29). */
69
    if (address & 0x80)
wuyangyong's avatar
wuyangyong 已提交
70 71 72 73 74 75 76 77
    {
        send_size = 2;
    }

    rt_spi_send_then_recv(spi_device, send_buffer, send_size, recv_buffer, 1);
    return (recv_buffer[0]);
}

78
static void spi_write_op(struct rt_spi_device *spi_device, uint8_t op, uint8_t address, uint8_t data)
wuyangyong's avatar
wuyangyong 已提交
79 80 81 82 83 84 85 86 87 88 89 90 91 92
{
    uint32_t level;
    uint8_t buffer[2];

    level = rt_hw_interrupt_disable();

    buffer[0] = op | (address & ADDR_MASK);
    buffer[1] = data;
    rt_spi_send(spi_device, buffer, 2);

    rt_hw_interrupt_enable(level);
}

/* enc28j60 function */
93
static void enc28j60_clkout(struct rt_spi_device *spi_device, rt_uint8_t clk)
wuyangyong's avatar
wuyangyong 已提交
94 95 96 97 98
{
    /* setup clkout: 2 is 12.5MHz: */
    spi_write(spi_device, ECOCON, clk & 0x7);
}

99
static void enc28j60_set_bank(struct rt_spi_device *spi_device, uint8_t address)
wuyangyong's avatar
wuyangyong 已提交
100 101
{
    /* set the bank (if needed) .*/
102
    if ((address & BANK_MASK) != Enc28j60Bank)
wuyangyong's avatar
wuyangyong 已提交
103 104
    {
        /* set the bank. */
105 106
        spi_write_op(spi_device, ENC28J60_BIT_FIELD_CLR, ECON1, (ECON1_BSEL1 | ECON1_BSEL0));
        spi_write_op(spi_device, ENC28J60_BIT_FIELD_SET, ECON1, (address & BANK_MASK) >> 5);
wuyangyong's avatar
wuyangyong 已提交
107 108 109 110
        Enc28j60Bank = (address & BANK_MASK);
    }
}

111
static uint8_t spi_read(struct rt_spi_device *spi_device, uint8_t address)
wuyangyong's avatar
wuyangyong 已提交
112 113 114 115 116 117 118
{
    /* set the bank. */
    enc28j60_set_bank(spi_device, address);
    /* do the read. */
    return spi_read_op(spi_device, ENC28J60_READ_CTRL_REG, address);
}

119
static void spi_write(struct rt_spi_device *spi_device, rt_uint8_t address, rt_uint8_t data)
wuyangyong's avatar
wuyangyong 已提交
120 121 122 123 124 125 126
{
    /* set the bank. */
    enc28j60_set_bank(spi_device, address);
    /* do the write. */
    spi_write_op(spi_device, ENC28J60_WRITE_CTRL_REG, address, data);
}

127
static uint16_t enc28j60_phy_read(struct rt_spi_device *spi_device, rt_uint8_t address)
wuyangyong's avatar
wuyangyong 已提交
128 129 130 131 132 133 134 135 136 137
{
    uint16_t value;

    /* Set the right address and start the register read operation. */
    spi_write(spi_device, MIREGADR, address);
    spi_write(spi_device, MICMD, MICMD_MIIRD);

    _delay_us(15);

    /* wait until the PHY read completes. */
138
    while (spi_read(spi_device, MISTAT) & MISTAT_BUSY);
wuyangyong's avatar
wuyangyong 已提交
139 140 141 142

    /* reset reading bit */
    spi_write(spi_device, MICMD, 0x00);

143
    value = spi_read(spi_device, MIRDL) | spi_read(spi_device, MIRDH) << 8;
wuyangyong's avatar
wuyangyong 已提交
144 145 146 147

    return (value);
}

148
static void enc28j60_phy_write(struct rt_spi_device *spi_device, rt_uint8_t address, uint16_t data)
wuyangyong's avatar
wuyangyong 已提交
149 150 151 152 153 154
{
    /* set the PHY register address. */
    spi_write(spi_device, MIREGADR, address);

    /* write the PHY data. */
    spi_write(spi_device, MIWRL, data);
155
    spi_write(spi_device, MIWRH, data >> 8);
wuyangyong's avatar
wuyangyong 已提交
156 157

    /* wait until the PHY write completes. */
158
    while (spi_read(spi_device, MISTAT) & MISTAT_BUSY)
wuyangyong's avatar
wuyangyong 已提交
159 160 161 162 163
    {
        _delay_us(15);
    }
}

164
static uint32_t enc28j60_interrupt_disable(struct rt_spi_device *spi_device)
wuyangyong's avatar
wuyangyong 已提交
165 166 167 168 169 170 171 172 173 174 175 176 177 178
{
    uint32_t level;

    /* switch to bank 0 */
    enc28j60_set_bank(spi_device, EIE);

    /* get last interrupt level */
    level = spi_read(spi_device, EIE);
    /* disable interrutps */
    spi_write_op(spi_device, ENC28J60_BIT_FIELD_CLR, EIE, level);

    return level;
}

179
static void enc28j60_interrupt_enable(struct rt_spi_device *spi_device, uint32_t level)
wuyangyong's avatar
wuyangyong 已提交
180 181 182 183 184 185 186 187 188
{
    /* switch to bank 0 */
    enc28j60_set_bank(spi_device, EIE);
    spi_write_op(spi_device, ENC28J60_BIT_FIELD_SET, EIE, level);
}

/*
 * Access the PHY to determine link status
 */
189
static rt_bool_t enc28j60_check_link_status(struct rt_spi_device *spi_device)
wuyangyong's avatar
wuyangyong 已提交
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
{
    uint16_t reg;

    reg = enc28j60_phy_read(spi_device, PHSTAT2);

    if (reg & PHSTAT2_LSTAT)
    {
        /* on */
        return RT_TRUE;
    }
    else
    {
        /* off */
        return RT_FALSE;
    }
}

/************************* RT-Thread Device Interface *************************/
void enc28j60_isr(void)
{
    eth_device_ready(&enc28j60_dev.parent);
    NET_DEBUG("enc28j60_isr\r\n");
}

static void _tx_chain_init(void)
{
    enc28j60_tx_list[0].next = &enc28j60_tx_list[1];
    enc28j60_tx_list[1].next = &enc28j60_tx_list[0];

    enc28j60_tx_list[0].prev = &enc28j60_tx_list[1];
    enc28j60_tx_list[1].prev = &enc28j60_tx_list[0];

    enc28j60_tx_list[0].addr = TXSTART_INIT;
    enc28j60_tx_list[1].addr = TXSTART_INIT + MAX_TX_PACKAGE_SIZE;

    enc28j60_tx_list[0].free = RT_TRUE;
    enc28j60_tx_list[1].free = RT_TRUE;

    tx_current = &enc28j60_tx_list[0];
    tx_ack = tx_current;
}

/* initialize the interface */
static rt_err_t enc28j60_init(rt_device_t dev)
{
235 236
    struct net_device *enc28j60 = (struct net_device *)dev;
    struct rt_spi_device *spi_device = enc28j60->spi_device;
wuyangyong's avatar
wuyangyong 已提交
237 238 239 240 241 242 243

    enc28j60_lock(dev);

    _tx_chain_init();

    // perform system reset
    spi_write_op(spi_device, ENC28J60_SOFT_RESET, 0, ENC28J60_SOFT_RESET);
244
    rt_thread_delay(RT_TICK_PER_SECOND / 50); /* delay 20ms */
wuyangyong's avatar
wuyangyong 已提交
245 246 247 248

    NextPacketPtr = RXSTART_INIT;

    // Rx start
249 250
    spi_write(spi_device, ERXSTL, RXSTART_INIT & 0xFF);
    spi_write(spi_device, ERXSTH, RXSTART_INIT >> 8);
wuyangyong's avatar
wuyangyong 已提交
251
    // set receive pointer address
252 253
    spi_write(spi_device, ERXRDPTL, RXSTOP_INIT & 0xFF);
    spi_write(spi_device, ERXRDPTH, RXSTOP_INIT >> 8);
wuyangyong's avatar
wuyangyong 已提交
254
    // RX end
255 256
    spi_write(spi_device, ERXNDL, RXSTOP_INIT & 0xFF);
    spi_write(spi_device, ERXNDH, RXSTOP_INIT >> 8);
wuyangyong's avatar
wuyangyong 已提交
257 258

    // TX start
259 260
    spi_write(spi_device, ETXSTL, TXSTART_INIT & 0xFF);
    spi_write(spi_device, ETXSTH, TXSTART_INIT >> 8);
wuyangyong's avatar
wuyangyong 已提交
261
    // set transmission pointer address
262 263
    spi_write(spi_device, EWRPTL, TXSTART_INIT & 0xFF);
    spi_write(spi_device, EWRPTH, TXSTART_INIT >> 8);
wuyangyong's avatar
wuyangyong 已提交
264
    // TX end
265 266
    spi_write(spi_device, ETXNDL, TXSTOP_INIT & 0xFF);
    spi_write(spi_device, ETXNDH, TXSTOP_INIT >> 8);
wuyangyong's avatar
wuyangyong 已提交
267 268 269 270 271 272 273 274 275 276 277

    // do bank 1 stuff, packet filter:
    // For broadcast packets we allow only ARP packtets
    // All other packets should be unicast only for our mac (MAADR)
    //
    // The pattern to match on is therefore
    // Type     ETH.DST
    // ARP      BROADCAST
    // 06 08 -- ff ff ff ff ff ff -> ip checksum for theses bytes=f7f9
    // in binary these poitions are:11 0000 0011 1111
    // This is hex 303F->EPMM0=0x3f,EPMM1=0x30
278
    spi_write(spi_device, ERXFCON, ERXFCON_UCEN | ERXFCON_CRCEN | ERXFCON_BCEN);
wuyangyong's avatar
wuyangyong 已提交
279 280 281

    // do bank 2 stuff
    // enable MAC receive
282
    spi_write(spi_device, MACON1, MACON1_MARXEN | MACON1_TXPAUS | MACON1_RXPAUS);
wuyangyong's avatar
wuyangyong 已提交
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
    // enable automatic padding to 60bytes and CRC operations
    // spi_write_op(ENC28J60_BIT_FIELD_SET, MACON3, MACON3_PADCFG0|MACON3_TXCRCEN|MACON3_FRMLNEN);
    spi_write_op(spi_device, ENC28J60_BIT_FIELD_SET, MACON3, MACON3_PADCFG0 | MACON3_TXCRCEN | MACON3_FRMLNEN | MACON3_FULDPX);
    // bring MAC out of reset

    // set inter-frame gap (back-to-back)
    // spi_write(MABBIPG, 0x12);
    spi_write(spi_device, MABBIPG, 0x15);

    spi_write(spi_device, MACON4, MACON4_DEFER);
    spi_write(spi_device, MACLCON2, 63);

    // set inter-frame gap (non-back-to-back)
    spi_write(spi_device, MAIPGL, 0x12);
    spi_write(spi_device, MAIPGH, 0x0C);

    // Set the maximum packet size which the controller will accept
    // Do not send packets longer than MAX_FRAMELEN:
301 302
    spi_write(spi_device, MAMXFLL, MAX_FRAMELEN & 0xFF);
    spi_write(spi_device, MAMXFLH, MAX_FRAMELEN >> 8);
wuyangyong's avatar
wuyangyong 已提交
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320

    // do bank 3 stuff
    // write MAC address
    // NOTE: MAC address in ENC28J60 is byte-backward
    spi_write(spi_device, MAADR0, enc28j60->dev_addr[5]);
    spi_write(spi_device, MAADR1, enc28j60->dev_addr[4]);
    spi_write(spi_device, MAADR2, enc28j60->dev_addr[3]);
    spi_write(spi_device, MAADR3, enc28j60->dev_addr[2]);
    spi_write(spi_device, MAADR4, enc28j60->dev_addr[1]);
    spi_write(spi_device, MAADR5, enc28j60->dev_addr[0]);

    /* output off */
    spi_write(spi_device, ECOCON, 0x00);

    // enc28j60_phy_write(PHCON1, 0x00);
    enc28j60_phy_write(spi_device, PHCON1, PHCON1_PDPXMD); // full duplex
    // no loopback of transmitted frames
    enc28j60_phy_write(spi_device, PHCON2, PHCON2_HDLDIS);
321 322
    /* enable PHY link changed interrupt. */
    enc28j60_phy_write(spi_device, PHIE, PHIE_PGEIE | PHIE_PLNKIE);
wuyangyong's avatar
wuyangyong 已提交
323 324 325 326 327 328 329 330 331 332 333 334 335 336

    enc28j60_set_bank(spi_device, ECON2);
    spi_write_op(spi_device, ENC28J60_BIT_FIELD_SET, ECON2, ECON2_AUTOINC);

    // switch to bank 0
    enc28j60_set_bank(spi_device, ECON1);
    // enable all interrutps
    spi_write_op(spi_device, ENC28J60_BIT_FIELD_SET, EIE, 0xFF);
    // enable packet reception
    spi_write_op(spi_device, ENC28J60_BIT_FIELD_SET, ECON1, ECON1_RXEN);

    /* clock out */
    enc28j60_clkout(spi_device, 2);

337 338
    enc28j60_phy_write(spi_device, PHLCON, 0xD76);  //0x476
    rt_thread_delay(RT_TICK_PER_SECOND / 50); /* delay 20ms */
wuyangyong's avatar
wuyangyong 已提交
339 340 341 342 343 344

    enc28j60_unlock(dev);
    return RT_EOK;
}

/* control the interface */
B
bernard 已提交
345
static rt_err_t enc28j60_control(rt_device_t dev, int cmd, void *args)
wuyangyong's avatar
wuyangyong 已提交
346
{
347 348
    struct net_device *enc28j60 = (struct net_device *)dev;
    switch (cmd)
wuyangyong's avatar
wuyangyong 已提交
349 350 351
    {
    case NIOCTL_GADDR:
        /* get mac address */
352
        if (args) rt_memcpy(args, enc28j60->dev_addr, 6);
wuyangyong's avatar
wuyangyong 已提交
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
        else return -RT_ERROR;
        break;

    default :
        break;
    }

    return RT_EOK;
}

/* Open the ethernet interface */
static rt_err_t enc28j60_open(rt_device_t dev, uint16_t oflag)
{
    return RT_EOK;
}

/* Close the interface */
static rt_err_t enc28j60_close(rt_device_t dev)
{
    return RT_EOK;
}

/* Read */
376
static rt_size_t enc28j60_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
wuyangyong's avatar
wuyangyong 已提交
377 378 379 380 381 382
{
    rt_set_errno(-RT_ENOSYS);
    return RT_EOK;
}

/* Write */
383
static rt_size_t enc28j60_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
wuyangyong's avatar
wuyangyong 已提交
384 385 386 387 388 389 390
{
    rt_set_errno(-RT_ENOSYS);
    return 0;
}

/* ethernet device interface */
/* Transmit packet. */
391
static rt_err_t enc28j60_tx(rt_device_t dev, struct pbuf *p)
wuyangyong's avatar
wuyangyong 已提交
392
{
393 394 395
    struct net_device *enc28j60 = (struct net_device *)dev;
    struct rt_spi_device *spi_device = enc28j60->spi_device;
    struct pbuf *q;
wuyangyong's avatar
wuyangyong 已提交
396 397 398
    rt_uint32_t level;
#ifdef ETH_TX_DUMP
    rt_size_t dump_count = 0;
399
    rt_uint8_t *dump_ptr;
wuyangyong's avatar
wuyangyong 已提交
400 401 402
    rt_size_t dump_i;
#endif

403
    if (tx_current->free == RT_FALSE)
wuyangyong's avatar
wuyangyong 已提交
404 405
    {
        NET_DEBUG("[Tx] no empty buffer!\r\n");
406
        while (tx_current->free == RT_FALSE)
wuyangyong's avatar
wuyangyong 已提交
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
        {
            rt_err_t result;
            rt_uint32_t recved;

            /* there is no block yet, wait a flag */
            result = rt_event_recv(&tx_event, 0x01,
                                   RT_EVENT_FLAG_AND | RT_EVENT_FLAG_CLEAR, RT_WAITING_FOREVER, &recved);

            RT_ASSERT(result == RT_EOK);
        }
        NET_DEBUG("[Tx] wait empty buffer done!\r\n");
    }

    enc28j60_lock(dev);

    /* disable enc28j60 interrupt */
    level = enc28j60_interrupt_disable(spi_device);

    // Set the write pointer to start of transmit buffer area
//    spi_write(EWRPTL, TXSTART_INIT&0xFF);
//    spi_write(EWRPTH, TXSTART_INIT>>8);
428 429
    spi_write(spi_device, EWRPTL, (tx_current->addr) & 0xFF);
    spi_write(spi_device, EWRPTH, (tx_current->addr) >> 8);
wuyangyong's avatar
wuyangyong 已提交
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
    // Set the TXND pointer to correspond to the packet size given
    tx_current->len = p->tot_len;
//    spi_write(ETXNDL, (TXSTART_INIT+ p->tot_len + 1)&0xFF);
//    spi_write(ETXNDH, (TXSTART_INIT+ p->tot_len + 1)>>8);

    // write per-packet control byte (0x00 means use macon3 settings)
    spi_write_op(spi_device, ENC28J60_WRITE_BUF_MEM, 0, 0x00);

#ifdef ETH_TX_DUMP
    NET_DEBUG("tx_dump, size:%d\r\n", p->tot_len);
#endif
    for (q = p; q != NULL; q = q->next)
    {
        uint8_t cmd = ENC28J60_WRITE_BUF_MEM;
        rt_spi_send_then_send(enc28j60->spi_device, &cmd, 1, q->payload, q->len);
#ifdef ETH_RX_DUMP
        dump_ptr = q->payload;
447
        for (dump_i = 0; dump_i < q->len; dump_i++)
wuyangyong's avatar
wuyangyong 已提交
448 449
        {
            NET_DEBUG("%02x ", *dump_ptr);
450
            if (((dump_count + 1) % 8) == 0)
wuyangyong's avatar
wuyangyong 已提交
451 452 453
            {
                NET_DEBUG("  ");
            }
454
            if (((dump_count + 1) % 16) == 0)
wuyangyong's avatar
wuyangyong 已提交
455 456 457 458 459 460 461 462 463 464 465 466 467
            {
                NET_DEBUG("\r\n");
            }
            dump_count++;
            dump_ptr++;
        }
#endif
    }
#ifdef ETH_RX_DUMP
    NET_DEBUG("\r\n");
#endif

    // send the contents of the transmit buffer onto the network
468
    if (tx_current == tx_ack)
wuyangyong's avatar
wuyangyong 已提交
469 470 471
    {
        NET_DEBUG("[Tx] stop, restart!\r\n");
        // TX start
472 473
        spi_write(spi_device, ETXSTL, (tx_current->addr) & 0xFF);
        spi_write(spi_device, ETXSTH, (tx_current->addr) >> 8);
wuyangyong's avatar
wuyangyong 已提交
474
        // TX end
475 476
        spi_write(spi_device, ETXNDL, (tx_current->addr + tx_current->len) & 0xFF);
        spi_write(spi_device, ETXNDH, (tx_current->addr + tx_current->len) >> 8);
wuyangyong's avatar
wuyangyong 已提交
477 478 479 480 481 482 483 484 485 486 487 488

        spi_write_op(spi_device, ENC28J60_BIT_FIELD_SET, ECON1, ECON1_TXRTS);
    }
    else
    {
        NET_DEBUG("[Tx] busy, add to chain!\r\n");
    }

    tx_current->free = RT_FALSE;
    tx_current = tx_current->next;

    /* Reset the transmit logic problem. See Rev. B4 Silicon Errata point 12. */
489
    if ((spi_read(spi_device, EIR) & EIR_TXERIF))
wuyangyong's avatar
wuyangyong 已提交
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
    {
        spi_write_op(spi_device, ENC28J60_BIT_FIELD_CLR, ECON1, ECON1_TXRST);
    }

    /* enable enc28j60 interrupt */
    enc28j60_interrupt_enable(spi_device, level);

    enc28j60_unlock(dev);

    return RT_EOK;
}

/* recv packet. */
static struct pbuf *enc28j60_rx(rt_device_t dev)
{
505 506 507
    struct net_device *enc28j60 = (struct net_device *)dev;
    struct rt_spi_device *spi_device = enc28j60->spi_device;
    struct pbuf *p = RT_NULL;
wuyangyong's avatar
wuyangyong 已提交
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522

    uint8_t eir, eir_clr;
    uint32_t pk_counter;
    rt_uint32_t level;
    rt_uint32_t len;
    rt_uint16_t rxstat;

    enc28j60_lock(dev);

    /* disable enc28j60 interrupt */
    level = enc28j60_interrupt_disable(spi_device);

    /* get EIR */
    eir = spi_read(spi_device, EIR);

523
    while (eir & ~EIR_PKTIF)
wuyangyong's avatar
wuyangyong 已提交
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
    {
        eir_clr = 0;

        /* clear PKTIF */
        if (eir & EIR_PKTIF)
        {
            NET_DEBUG("EIR_PKTIF\r\n");

            /* switch to bank 0. */
            enc28j60_set_bank(spi_device, EIE);
            /* disable rx interrutps. */
            spi_write_op(spi_device, ENC28J60_BIT_FIELD_CLR, EIE, EIE_PKTIE);
            eir_clr |= EIR_PKTIF;
//            enc28j60_set_bank(spi_device, EIR);
//            spi_write_op(spi_device, ENC28J60_BIT_FIELD_CLR, EIR, EIR_PKTIF);
        }

        /* clear DMAIF */
        if (eir & EIR_DMAIF)
        {
            NET_DEBUG("EIR_DMAIF\r\n");
            eir_clr |= EIR_DMAIF;
//            enc28j60_set_bank(spi_device, EIR);
//            spi_write_op(spi_device, ENC28J60_BIT_FIELD_CLR, EIR, EIR_DMAIF);
        }

        /* LINK changed handler */
551
        if (eir & EIR_LINKIF)
wuyangyong's avatar
wuyangyong 已提交
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
        {
            rt_bool_t link_status;

            NET_DEBUG("EIR_LINKIF\r\n");
            link_status = enc28j60_check_link_status(spi_device);

            /* read PHIR to clear the flag */
            enc28j60_phy_read(spi_device, PHIR);
            eir_clr |= EIR_LINKIF;
//            enc28j60_set_bank(spi_device, EIR);
//            spi_write_op(spi_device, ENC28J60_BIT_FIELD_CLR, EIR, EIR_LINKIF);

            eth_device_linkchange(&(enc28j60->parent), link_status);
        }

        if (eir & EIR_TXIF)
        {
            /* A frame has been transmitted. */
            enc28j60_set_bank(spi_device, EIR);
            spi_write_op(spi_device, ENC28J60_BIT_FIELD_CLR, EIR, EIR_TXIF);

            tx_ack->free = RT_TRUE;
            tx_ack = tx_ack->next;
575
            if (tx_ack->free == RT_FALSE)
wuyangyong's avatar
wuyangyong 已提交
576 577 578
            {
                NET_DEBUG("[tx isr] Tx chain not empty, continue send the next pkt!\r\n");
                // TX start
579 580
                spi_write(spi_device, ETXSTL, (tx_ack->addr) & 0xFF);
                spi_write(spi_device, ETXSTH, (tx_ack->addr) >> 8);
wuyangyong's avatar
wuyangyong 已提交
581
                // TX end
582 583
                spi_write(spi_device, ETXNDL, (tx_ack->addr + tx_ack->len) & 0xFF);
                spi_write(spi_device, ETXNDH, (tx_ack->addr + tx_ack->len) >> 8);
wuyangyong's avatar
wuyangyong 已提交
584 585 586 587 588 589 590 591 592 593 594 595 596

                spi_write_op(spi_device, ENC28J60_BIT_FIELD_SET, ECON1, ECON1_TXRTS);
            }
            else
            {
                NET_DEBUG("[tx isr] Tx chain empty, stop!\r\n");
            }

            /* set event */
            rt_event_send(&tx_event, 0x01);
        }

        /* wake up handler */
597
        if (eir & EIR_WOLIF)
wuyangyong's avatar
wuyangyong 已提交
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
        {
            NET_DEBUG("EIR_WOLIF\r\n");
            eir_clr |= EIR_WOLIF;
//            enc28j60_set_bank(spi_device, EIR);
//            spi_write_op(spi_device, ENC28J60_BIT_FIELD_CLR, EIR, EIR_WOLIF);
        }

        /* TX Error handler */
        if ((eir & EIR_TXERIF) != 0)
        {
            NET_DEBUG("EIR_TXERIF re-start tx chain!\r\n");
            enc28j60_set_bank(spi_device, ECON1);
            spi_write_op(spi_device, ENC28J60_BIT_FIELD_SET, ECON1, ECON1_TXRST);
            spi_write_op(spi_device, ENC28J60_BIT_FIELD_CLR, ECON1, ECON1_TXRST);
            eir_clr |= EIR_TXERIF;
//            enc28j60_set_bank(spi_device, EIR);
//            spi_write_op(spi_device, ENC28J60_BIT_FIELD_CLR, EIR, EIR_TXERIF);

            /* re-init tx chain */
            _tx_chain_init();
        }

        /* RX Error handler */
        if ((eir & EIR_RXERIF) != 0)
        {
            NET_DEBUG("EIR_RXERIF re-start rx!\r\n");

            NextPacketPtr = RXSTART_INIT;
            enc28j60_set_bank(spi_device, ECON1);
            spi_write_op(spi_device, ENC28J60_BIT_FIELD_SET, ECON1, ECON1_RXRST);
            spi_write_op(spi_device, ENC28J60_BIT_FIELD_CLR, ECON1, ECON1_RXRST);
            /* switch to bank 0. */
            enc28j60_set_bank(spi_device, ECON1);
            /* enable packet reception. */
            spi_write_op(spi_device, ENC28J60_BIT_FIELD_SET, ECON1, ECON1_RXEN);
            eir_clr |= EIR_RXERIF;
//            enc28j60_set_bank(spi_device, EIR);
//            spi_write_op(spi_device, ENC28J60_BIT_FIELD_CLR, EIR, EIR_RXERIF);
        }

        enc28j60_set_bank(spi_device, EIR);
        spi_write_op(spi_device, ENC28J60_BIT_FIELD_CLR, EIR, eir_clr);

        eir = spi_read(spi_device, EIR);
    }

    /* read pkt */
    pk_counter = spi_read(spi_device, EPKTCNT);
646
    if (pk_counter)
wuyangyong's avatar
wuyangyong 已提交
647 648 649
    {
        /* Set the read pointer to the start of the received packet. */
        spi_write(spi_device, ERDPTL, (NextPacketPtr));
650
        spi_write(spi_device, ERDPTH, (NextPacketPtr) >> 8);
wuyangyong's avatar
wuyangyong 已提交
651 652 653

        /* read the next packet pointer. */
        NextPacketPtr  = spi_read_op(spi_device, ENC28J60_READ_BUF_MEM, 0);
654
        NextPacketPtr |= spi_read_op(spi_device, ENC28J60_READ_BUF_MEM, 0) << 8;
wuyangyong's avatar
wuyangyong 已提交
655 656

        /* read the packet length (see datasheet page 43). */
657 658
        len  = spi_read_op(spi_device, ENC28J60_READ_BUF_MEM, 0);       //0x54
        len |= spi_read_op(spi_device, ENC28J60_READ_BUF_MEM, 0) << 8;  //5554
wuyangyong's avatar
wuyangyong 已提交
659

660
        len -= 4; //remove the CRC count
wuyangyong's avatar
wuyangyong 已提交
661 662 663

        // read the receive status (see datasheet page 43)
        rxstat  = spi_read_op(spi_device, ENC28J60_READ_BUF_MEM, 0);
664
        rxstat |= ((rt_uint16_t)spi_read_op(spi_device, ENC28J60_READ_BUF_MEM, 0)) << 8;
wuyangyong's avatar
wuyangyong 已提交
665 666 667 668

        // check CRC and symbol errors (see datasheet page 44, table 7-3):
        // The ERXFCON.CRCEN is set by default. Normally we should not
        // need to check this.
669
        if ((rxstat & 0x80) == 0)
wuyangyong's avatar
wuyangyong 已提交
670 671
        {
            // invalid
672
            len = 0;
wuyangyong's avatar
wuyangyong 已提交
673 674 675 676
        }
        else
        {
            /* allocation pbuf */
677
            p = pbuf_alloc(PBUF_LINK, len, PBUF_POOL);
wuyangyong's avatar
wuyangyong 已提交
678 679
            if (p != RT_NULL)
            {
680
                struct pbuf *q;
wuyangyong's avatar
wuyangyong 已提交
681 682
#ifdef ETH_RX_DUMP
                rt_size_t dump_count = 0;
683
                rt_uint8_t *dump_ptr;
wuyangyong's avatar
wuyangyong 已提交
684 685 686
                rt_size_t dump_i;
                NET_DEBUG("rx_dump, size:%d\r\n", len);
#endif
687
                for (q = p; q != RT_NULL; q = q->next)
wuyangyong's avatar
wuyangyong 已提交
688 689 690 691 692
                {
                    uint8_t cmd = ENC28J60_READ_BUF_MEM;
                    rt_spi_send_then_recv(spi_device, &cmd, 1, q->payload, q->len);
#ifdef ETH_RX_DUMP
                    dump_ptr = q->payload;
693
                    for (dump_i = 0; dump_i < q->len; dump_i++)
wuyangyong's avatar
wuyangyong 已提交
694 695
                    {
                        NET_DEBUG("%02x ", *dump_ptr);
696
                        if (((dump_count + 1) % 8) == 0)
wuyangyong's avatar
wuyangyong 已提交
697 698 699
                        {
                            NET_DEBUG("  ");
                        }
700
                        if (((dump_count + 1) % 16) == 0)
wuyangyong's avatar
wuyangyong 已提交
701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
                        {
                            NET_DEBUG("\r\n");
                        }
                        dump_count++;
                        dump_ptr++;
                    }
#endif
                }
#ifdef ETH_RX_DUMP
                NET_DEBUG("\r\n");
#endif
            }
        }

        /* Move the RX read pointer to the start of the next received packet. */
        /* This frees the memory we just read out. */
        spi_write(spi_device, ERXRDPTL, (NextPacketPtr));
718
        spi_write(spi_device, ERXRDPTH, (NextPacketPtr) >> 8);
wuyangyong's avatar
wuyangyong 已提交
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740

        /* decrement the packet counter indicate we are done with this packet. */
        spi_write_op(spi_device, ENC28J60_BIT_FIELD_SET, ECON2, ECON2_PKTDEC);
    }
    else
    {
        /* switch to bank 0. */
        enc28j60_set_bank(spi_device, ECON1);
        /* enable packet reception. */
        spi_write_op(spi_device, ENC28J60_BIT_FIELD_SET, ECON1, ECON1_RXEN);

        level |= EIE_PKTIE;
    }

    /* enable enc28j60 interrupt */
    enc28j60_interrupt_enable(spi_device, level);

    enc28j60_unlock(dev);

    return p;
}

B
Bernard Xiong 已提交
741 742 743 744 745 746 747 748 749 750 751 752
#ifdef RT_USING_DEVICE_OPS
const static struct rt_device_ops enc28j60_ops = 
{
    enc28j60_init,
    enc28j60_open,
    enc28j60_close,
    enc28j60_read,
    enc28j60_write,
    enc28j60_control
};
#endif

753
rt_err_t enc28j60_attach(const char *spi_device_name)
wuyangyong's avatar
wuyangyong 已提交
754
{
755
    struct rt_spi_device *spi_device;
wuyangyong's avatar
wuyangyong 已提交
756 757

    spi_device = (struct rt_spi_device *)rt_device_find(spi_device_name);
758
    if (spi_device == RT_NULL)
wuyangyong's avatar
wuyangyong 已提交
759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787
    {
        NET_DEBUG("spi device %s not found!\r\n", spi_device_name);
        return -RT_ENOSYS;
    }

    /* config spi */
    {
        struct rt_spi_configuration cfg;
        cfg.data_width = 8;
        cfg.mode = RT_SPI_MODE_0 | RT_SPI_MSB; /* SPI Compatible Modes 0 */
        cfg.max_hz = 20 * 1000 * 1000; /* SPI Interface with Clock Speeds Up to 20 MHz */
        rt_spi_configure(spi_device, &cfg);
    } /* config spi */

    memset(&enc28j60_dev, 0, sizeof(enc28j60_dev));

    rt_event_init(&tx_event, "eth_tx", RT_IPC_FLAG_FIFO);
    enc28j60_dev.spi_device = spi_device;

    /* detect device */
    {
        uint16_t value;

        /* perform system reset. */
        spi_write_op(spi_device, ENC28J60_SOFT_RESET, 0, ENC28J60_SOFT_RESET);
        rt_thread_delay(1); /* delay 20ms */

        enc28j60_dev.emac_rev = spi_read(spi_device, EREVID);
        value = enc28j60_phy_read(spi_device, PHHID2);
788 789 790
        enc28j60_dev.phy_rev = value & 0x0F;
        enc28j60_dev.phy_pn = (value >> 4) & 0x3F;
        enc28j60_dev.phy_id = (enc28j60_phy_read(spi_device, PHHID1) | ((value >> 10) << 16)) << 3;
wuyangyong's avatar
wuyangyong 已提交
791

792
        if (enc28j60_dev.phy_id != 0x00280418)
wuyangyong's avatar
wuyangyong 已提交
793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813
        {
            NET_DEBUG("ENC28J60 PHY ID not correct!\r\n");
            NET_DEBUG("emac_rev:%d\r\n", enc28j60_dev.emac_rev);
            NET_DEBUG("phy_rev:%02X\r\n", enc28j60_dev.phy_rev);
            NET_DEBUG("phy_pn:%02X\r\n", enc28j60_dev.phy_pn);
            NET_DEBUG("phy_id:%08X\r\n", enc28j60_dev.phy_id);
            return RT_EIO;
        }
    }

    /* OUI 00-04-A3 (hex): Microchip Technology, Inc. */
    enc28j60_dev.dev_addr[0] = 0x00;
    enc28j60_dev.dev_addr[1] = 0x04;
    enc28j60_dev.dev_addr[2] = 0xA3;
    /* set MAC address, only for test */
    enc28j60_dev.dev_addr[3] = 0x12;
    enc28j60_dev.dev_addr[4] = 0x34;
    enc28j60_dev.dev_addr[5] = 0x56;

    /* init rt-thread device struct */
    enc28j60_dev.parent.parent.type    = RT_Device_Class_NetIf;
B
Bernard Xiong 已提交
814 815 816
#ifdef RT_USING_DEVICE_OPS
    enc28j60_dev.parent.parent.ops     = &enc28j60_ops;
#else
wuyangyong's avatar
wuyangyong 已提交
817 818 819 820 821 822
    enc28j60_dev.parent.parent.init    = enc28j60_init;
    enc28j60_dev.parent.parent.open    = enc28j60_open;
    enc28j60_dev.parent.parent.close   = enc28j60_close;
    enc28j60_dev.parent.parent.read    = enc28j60_read;
    enc28j60_dev.parent.parent.write   = enc28j60_write;
    enc28j60_dev.parent.parent.control = enc28j60_control;
B
Bernard Xiong 已提交
823
#endif
wuyangyong's avatar
wuyangyong 已提交
824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842

    /* init rt-thread ethernet device struct */
    enc28j60_dev.parent.eth_rx  = enc28j60_rx;
    enc28j60_dev.parent.eth_tx  = enc28j60_tx;

    rt_mutex_init(&enc28j60_dev.lock, "enc28j60", RT_IPC_FLAG_FIFO);

    eth_device_init(&(enc28j60_dev.parent), "e0");

    return RT_EOK;
}

#ifdef RT_USING_FINSH
#include <finsh.h>
/*
 * Debug routine to dump useful register contents
 */
static void enc28j60(void)
{
843
    struct rt_spi_device *spi_device = enc28j60_dev.spi_device;
wuyangyong's avatar
wuyangyong 已提交
844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891
    enc28j60_lock(&enc28j60_dev);

    rt_kprintf("-- enc28j60 registers:\n");
    rt_kprintf("HwRevID: 0x%02X\n", spi_read(spi_device, EREVID));

    rt_kprintf("Cntrl: ECON1 ECON2 ESTAT  EIR  EIE\n");
    rt_kprintf("       0x%02X  0x%02X  0x%02X  0x%02X  0x%02X\n",
               spi_read(spi_device, ECON1),
               spi_read(spi_device, ECON2),
               spi_read(spi_device, ESTAT),
               spi_read(spi_device, EIR),
               spi_read(spi_device, EIE));

    rt_kprintf("MAC  : MACON1 MACON3 MACON4\n");
    rt_kprintf("       0x%02X   0x%02X   0x%02X\n",
               spi_read(spi_device, MACON1),
               spi_read(spi_device, MACON3),
               spi_read(spi_device, MACON4));

    rt_kprintf("Rx   : ERXST  ERXND  ERXWRPT ERXRDPT ERXFCON EPKTCNT MAMXFL\n");
    rt_kprintf("       0x%04X 0x%04X 0x%04X  0x%04X  ",
               (spi_read(spi_device, ERXSTH) << 8) | spi_read(spi_device, ERXSTL),
               (spi_read(spi_device, ERXNDH) << 8) | spi_read(spi_device, ERXNDL),
               (spi_read(spi_device, ERXWRPTH) << 8) | spi_read(spi_device, ERXWRPTL),
               (spi_read(spi_device, ERXRDPTH) << 8) | spi_read(spi_device, ERXRDPTL));

    rt_kprintf("0x%02X    0x%02X    0x%04X\n",
               spi_read(spi_device, ERXFCON),
               spi_read(spi_device, EPKTCNT),
               (spi_read(spi_device, MAMXFLH) << 8) | spi_read(spi_device, MAMXFLL));

    rt_kprintf("Tx   : ETXST  ETXND  MACLCON1 MACLCON2 MAPHSUP\n");
    rt_kprintf("       0x%04X 0x%04X 0x%02X     0x%02X     0x%02X\n",
               (spi_read(spi_device, ETXSTH) << 8) | spi_read(spi_device, ETXSTL),
               (spi_read(spi_device, ETXNDH) << 8) | spi_read(spi_device, ETXNDL),
               spi_read(spi_device, MACLCON1),
               spi_read(spi_device, MACLCON2),
               spi_read(spi_device, MAPHSUP));

    rt_kprintf("PHY   : PHCON1 PHSTAT1\r\n");
    rt_kprintf("        0x%04X 0x%04X\r\n",
               enc28j60_phy_read(spi_device, PHCON1),
               enc28j60_phy_read(spi_device, PHSTAT1));

    enc28j60_unlock(&enc28j60_dev);
}
FINSH_FUNCTION_EXPORT(enc28j60, dump enc28j60 registers);
#endif