OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [freertos-6.1.1/] [Demo/] [Common/] [ethernet/] [lwIP_130/] [src/] [netif/] [stf91x_ethernetif.c] - Blame information for rev 606

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 606 jeremybenn
/*
2
 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without modification,
6
 * are permitted provided that the following conditions are met:
7
 *
8
 * 1. Redistributions of source code must retain the above copyright notice,
9
 *    this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright notice,
11
 *    this list of conditions and the following disclaimer in the documentation
12
 *    and/or other materials provided with the distribution.
13
 * 3. The name of the author may not be used to endorse or promote products
14
 *    derived from this software without specific prior written permission.
15
 *
16
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
19
 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
21
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
25
 * OF SUCH DAMAGE.
26
 *
27
 * This file is part of the lwIP TCP/IP stack.
28
 *
29
 * Author: Adam Dunkels <adam@sics.se>
30
 *
31
 */
32
 
33
/*
34
 * This file is a skeleton for developing Ethernet network interface
35
 * drivers for lwIP. Add code to the low_level functions and do a
36
 * search-and-replace for the word "ethernetif" to replace it with
37
 * something that better describes your network interface.
38
 */
39
 
40
#include "lwip/def.h"
41
#include "lwip/mem.h"
42
#include "lwip/pbuf.h"
43
#include "lwip/sys.h"
44
#include <lwip/stats.h>
45
#include <lwip/snmp.h>
46
#include "netif/etharp.h"
47
#include "91x_enet.h"
48
 
49
// Standard library include
50
#include <stdio.h>
51
#include <string.h>
52
 
53
#define netifMTU                                                                                                        ( 1500 )
54
#define netifINTERFACE_TASK_STACK_SIZE          ( 350 )
55
#define netifINTERFACE_TASK_PRIORITY                    ( configMAX_PRIORITIES - 1 )
56
#define netifBUFFER_WAIT_ATTEMPTS                                       10
57
#define netifBUFFER_WAIT_DELAY                                          (10 / portTICK_RATE_MS)
58
#define IFNAME0 'e'
59
#define IFNAME1 'n'
60
 
61
/* The time to block waiting for input. */
62
#define emacBLOCK_TIME_WAITING_FOR_INPUT        ( ( portTickType ) 100 )
63
 
64
/* Interrupt status bit definition. */
65
#define DMI_RX_CURRENT_DONE 0x8000
66
 
67
static u8_t s_rxBuff[1520];
68
 
69
/* The semaphore used by the ISR to wake the lwIP task. */
70
static xSemaphoreHandle s_xSemaphore = NULL;
71
 
72
struct ethernetif {
73
  struct eth_addr *ethaddr;
74
  /* Add whatever per-interface state that is needed here. */
75
};
76
 
77
/* Forward declarations. */
78
static void ethernetif_input( void *pParams );
79
u8 *pcGetNextBuffer( void );
80
 
81
/**
82
 * In this function, the hardware should be initialized.
83
 * Called from ethernetif_init().
84
 *
85
 * @param netif the already initialized lwip network interface structure
86
 *        for this ethernetif
87
 */
88
static void low_level_init(struct netif *netif)
89
{
90
  /* set MAC hardware address length */
91
  netif->hwaddr_len = ETHARP_HWADDR_LEN;
92
 
93
  /* set MAC hardware address */
94
  netif->hwaddr[0] = MAC_ADDR0;
95
  netif->hwaddr[1] = MAC_ADDR1;
96
  netif->hwaddr[2] = MAC_ADDR2;
97
  netif->hwaddr[3] = MAC_ADDR3;
98
  netif->hwaddr[4] = MAC_ADDR4;
99
  netif->hwaddr[5] = MAC_ADDR5;
100
 
101
  /* maximum transfer unit */
102
  netif->mtu = netifMTU;
103
 
104
  // device capabilities.
105
        // don't set NETIF_FLAG_ETHARP if this device is not an ethernet one
106
  netif->flags |= NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP;
107
 
108
  /* Do whatever else is needed to initialize interface. */
109
 
110
  if( s_xSemaphore == NULL )
111
  {
112
      vSemaphoreCreateBinary( s_xSemaphore );
113
      xSemaphoreTake( s_xSemaphore,  0);
114
  }
115
 
116
  /* Initialise the MAC. */
117
  ENET_InitClocksGPIO();
118
  ENET_Init(PHY_AUTO_NEGOTIATION);
119
  ENET_Start();
120
 
121
  portENTER_CRITICAL();
122
  {
123
      /*set MAC physical*/
124
      ENET_MAC->MAH = (MAC_ADDR5<<8) + MAC_ADDR4;
125
      ENET_MAC->MAL = (MAC_ADDR3<<24) + (MAC_ADDR2<<16) + (MAC_ADDR1<<8) + MAC_ADDR0;
126
 
127
      VIC_Config( ENET_ITLine, VIC_IRQ, 1 );
128
      VIC_ITCmd( ENET_ITLine, ENABLE );
129
      ENET_DMA->ISR = DMI_RX_CURRENT_DONE;
130
      ENET_DMA->IER = DMI_RX_CURRENT_DONE;
131
  }
132
  portEXIT_CRITICAL();
133
 
134
  netif->flags |= NETIF_FLAG_LINK_UP;
135
 
136
  /* Create the task that handles the EMAC. */
137
  xTaskCreate( ethernetif_input, ( signed portCHAR * ) "ETH_INT", netifINTERFACE_TASK_STACK_SIZE, (void *)netif, netifINTERFACE_TASK_PRIORITY, NULL );
138
}
139
 
140
 
141
/**
142
 * This function should do the actual transmission of the packet. The packet is
143
 * contained in the pbuf that is passed to the function. This pbuf
144
 * might be chained.
145
 *
146
 * @param netif the lwip network interface structure for this ethernetif
147
 * @param p the MAC packet to send (e.g. IP packet including MAC addresses and type)
148
 * @return ERR_OK if the packet could be sent
149
 *         an err_t value if the packet couldn't be sent
150
 *
151
 * @note Returning ERR_MEM here if a DMA queue of your MAC is full can lead to
152
 *       strange results. You might consider waiting for space in the DMA queue
153
 *       to become availale since the stack doesn't retry to send a packet
154
 *       dropped because of memory failure (except for the TCP timers).
155
 */
156
static err_t low_level_output(struct netif *netif, struct pbuf *p)
157
{
158
  struct pbuf *q;
159
  u32_t l = 0;
160
        unsigned portCHAR *pcTxData;
161
 
162
#if ETH_PAD_SIZE
163
  pbuf_header(p, -ETH_PAD_SIZE);                        /* drop the padding word */
164
#endif
165
 
166
        /* Get a DMA buffer into which we can write the data to send. */
167
        for( int i = 0; i < netifBUFFER_WAIT_ATTEMPTS; i++ )
168
        {
169
                pcTxData = pcGetNextBuffer();
170
 
171
                if( pcTxData ){
172
                        break;
173
                } else {
174
                        vTaskDelay( netifBUFFER_WAIT_DELAY );
175
                }
176
        }
177
 
178
        if (pcTxData == NULL) {
179
                portNOP();
180
 
181
                return ERR_BUF;
182
        }
183
        else {
184
 
185
                for(q = p; q != NULL; q = q->next) {
186
                        /* Send the data from the pbuf to the interface, one pbuf at a
187
                                 time. The size of the data in each pbuf is kept in the ->len
188
                                 variable. */
189
 
190
                        vTaskSuspendAll();
191
                        memcpy(&pcTxData[l], (u8_t*)q->payload, q->len);
192
                        xTaskResumeAll();
193
                        l += q->len;
194
                }
195
        }
196
 
197
        ENET_TxPkt( &pcTxData, l );
198
 
199
        #if ETH_PAD_SIZE
200
                pbuf_header(p, ETH_PAD_SIZE);                   /* reclaim the padding word */
201
        #endif
202
 
203
                LINK_STATS_INC(link.xmit);
204
 
205
  return ERR_OK;
206
}
207
 
208
/**
209
 * Should allocate a pbuf and transfer the bytes of the incoming
210
 * packet from the interface into the pbuf.
211
 *
212
 * @param netif the lwip network interface structure for this ethernetif
213
 * @return a pbuf filled with the received packet (including MAC header)
214
 *         NULL on memory error
215
 */
216
static struct pbuf *low_level_input(struct netif *netif)
217
{
218
  struct pbuf *p, *q;
219
  u16_t len, l;
220
 
221
  l = 0;
222
  p = NULL;
223
 
224
 
225
        /* Obtain the size of the packet and put it into the "len"
226
                 variable. */
227
        len = ENET_HandleRxPkt(s_rxBuff);
228
 
229
        if(len)
230
        {
231
        #if ETH_PAD_SIZE
232
                len += ETH_PAD_SIZE;                                            /* allow room for Ethernet padding */
233
        #endif
234
 
235
                /* We allocate a pbuf chain of pbufs from the pool. */
236
                p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL);
237
 
238
                if (p != NULL) {
239
 
240
        #if ETH_PAD_SIZE
241
                        pbuf_header(p, -ETH_PAD_SIZE);                  /* drop the padding word */
242
        #endif
243
 
244
                        /* We iterate over the pbuf chain until we have read the entire
245
                         * packet into the pbuf. */
246
                        for(q = p; q != NULL; q = q->next) {
247
                                /* Read enough bytes to fill this pbuf in the chain. The
248
                                 * available data in the pbuf is given by the q->len
249
                                 * variable. */
250
                                vTaskSuspendAll();
251
                                memcpy((u8_t*)q->payload, &s_rxBuff[l], q->len);
252
                                xTaskResumeAll();
253
                                l = l + q->len;
254
                        }
255
 
256
 
257
        #if ETH_PAD_SIZE
258
                        pbuf_header(p, ETH_PAD_SIZE);                   /* reclaim the padding word */
259
        #endif
260
 
261
                        LINK_STATS_INC(link.recv);
262
 
263
                } else {
264
 
265
                        LINK_STATS_INC(link.memerr);
266
                        LINK_STATS_INC(link.drop);
267
 
268
                } /* End else */
269
        } /* End if */
270
 
271
  return p;
272
}
273
 
274
/**
275
 * This function should be called when a packet is ready to be read
276
 * from the interface. It uses the function low_level_input() that
277
 * should handle the actual reception of bytes from the network
278
 * interface.Then the type of the received packet is determined and
279
 * the appropriate input function is called.
280
 *
281
 * @param netif the lwip network interface structure for this ethernetif
282
 */
283
 
284
static void ethernetif_input( void *pParams )
285
{
286
        struct netif *netif;
287
  struct ethernetif *ethernetif;
288
  struct eth_hdr *ethhdr;
289
  struct pbuf *p;
290
 
291
        netif = (struct netif*) pParams;
292
        ethernetif = netif->state;
293
 
294
        for( ;; )
295
        {
296
                do
297
                {
298
 
299
                        /* move received packet into a new pbuf */
300
                        p = low_level_input( netif );
301
 
302
                        if( p == NULL )
303
                        {
304
                                /* No packet could be read.  Wait a for an interrupt to tell us
305
                                there is more data available. */
306
                                xSemaphoreTake( s_xSemaphore, emacBLOCK_TIME_WAITING_FOR_INPUT );
307
                        }
308
 
309
                } while( p == NULL );
310
 
311
                /* points to packet payload, which starts with an Ethernet header */
312
                ethhdr = p->payload;
313
 
314
                switch (htons(ethhdr->type)) {
315
                        /* IP or ARP packet? */
316
 
317
                case ETHTYPE_IP:
318
 
319
                        /* full packet send to tcpip_thread to process */
320
                        if (netif->input(p, netif) != ERR_OK)
321
                        {
322
                                LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: IP input error\n"));
323
                                pbuf_free(p);
324
                                p = NULL;
325
                        }
326
                        break;
327
 
328
                case ETHTYPE_ARP:
329
 
330
#if ETHARP_TRUST_IP_MAC
331
                        etharp_ip_input(netif, p);
332
#endif
333
 
334
                        etharp_arp_input(netif, ethernetif->ethaddr, p);
335
                        break;
336
 
337
                default:
338
                        pbuf_free(p);
339
                        p = NULL;
340
                        break;
341
                }
342
        }
343
}
344
 
345
/**
346
 * Should be called at the beginning of the program to set up the
347
 * network interface. It calls the function low_level_init() to do the
348
 * actual setup of the hardware.
349
 *
350
 * This function should be passed as a parameter to netif_add().
351
 *
352
 * @param netif the lwip network interface structure for this ethernetif
353
 * @return ERR_OK if the loopif is initialized
354
 *         ERR_MEM if private data couldn't be allocated
355
 *         any other err_t on error
356
 */
357
err_t
358
ethernetif_init(struct netif *netif)
359
{
360
  struct ethernetif *ethernetif;
361
 
362
        LWIP_ASSERT("netif != NULL", (netif != NULL));
363
 
364
  ethernetif = mem_malloc(sizeof(struct ethernetif));
365
 
366
  if (ethernetif == NULL)
367
  {
368
        LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_init: out of memory\n"));
369
        return ERR_MEM;
370
  }
371
 
372
#if LWIP_NETIF_HOSTNAME
373
  /* Initialize interface hostname */
374
  netif->hostname = "lwip";
375
#endif /* LWIP_NETIF_HOSTNAME */
376
 
377
  /*
378
         * Initialize the snmp variables and counters inside the struct netif.
379
         * The last argument should be replaced with your link speed, in units
380
         * of bits per second.
381
         */
382
        NETIF_INIT_SNMP(netif, snmp_ifType_ethernet_csmacd, 100);
383
 
384
  netif->state = ethernetif;
385
  netif->name[0] = IFNAME0;
386
  netif->name[1] = IFNAME1;
387
  /* We directly use etharp_output() here to save a function call.
388
         * You can instead declare your own function an call etharp_output()
389
         * from it if you have to do some checks before sending (e.g. if link
390
         * is available...)
391
         */
392
  netif->output = etharp_output;
393
  netif->linkoutput = low_level_output;
394
 
395
  ethernetif->ethaddr = (struct eth_addr *)&(netif->hwaddr[0]);
396
 
397
  low_level_init(netif);
398
 
399
  return ERR_OK;
400
}
401
/*-----------------------------------------------------------*/
402
 
403
void ENET_IRQHandler(void)
404
{
405
portBASE_TYPE xSwitchRequired;
406
 
407
        /* Give the semaphore in case the lwIP task needs waking. */
408
        xSwitchRequired = xSemaphoreGiveFromISR( s_xSemaphore, &xSwitchRequired );
409
 
410
        /* Clear the interrupt. */
411
        ENET_DMA->ISR = DMI_RX_CURRENT_DONE;
412
 
413
        /* Switch tasks if necessary. */
414
        portEND_SWITCHING_ISR( xSwitchRequired );
415
}
416
/*-----------------------------------------------------------*/

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.