fork of lwip-eth for publication

Fork of lwip-eth by mbed official

Committer:
urvishpatel12
Date:
Mon Apr 30 00:06:06 2018 +0000
Revision:
34:3fd06fad5020
Parent:
30:da4b2487ee7b
Add missing definition

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 30:da4b2487ee7b 1 #include "lwip/opt.h"
mbed_official 30:da4b2487ee7b 2 #include "lwip/tcpip.h"
mbed_official 30:da4b2487ee7b 3 #include "netif/etharp.h"
mbed_official 30:da4b2487ee7b 4 #include "mbed_interface.h"
mbed_official 30:da4b2487ee7b 5 #include "ethernet_api.h"
mbed_official 30:da4b2487ee7b 6 #include "ethernetext_api.h"
mbed_official 30:da4b2487ee7b 7
mbed_official 30:da4b2487ee7b 8 #define RECV_TASK_PRI (osPriorityNormal)
mbed_official 30:da4b2487ee7b 9 #define PHY_TASK_PRI (osPriorityNormal)
mbed_official 30:da4b2487ee7b 10 #define PHY_TASK_WAIT (200)
mbed_official 30:da4b2487ee7b 11
mbed_official 30:da4b2487ee7b 12 /* memory */
mbed_official 30:da4b2487ee7b 13 static sys_sem_t recv_ready_sem; /* receive ready semaphore */
mbed_official 30:da4b2487ee7b 14
mbed_official 30:da4b2487ee7b 15 /* function */
mbed_official 30:da4b2487ee7b 16 static void rza1_recv_task(void *arg);
mbed_official 30:da4b2487ee7b 17 static void rza1_phy_task(void *arg);
mbed_official 30:da4b2487ee7b 18 static err_t rza1_etharp_output(struct netif *netif, struct pbuf *q, ip_addr_t *ipaddr);
mbed_official 30:da4b2487ee7b 19 static err_t rza1_low_level_output(struct netif *netif, struct pbuf *p);
mbed_official 30:da4b2487ee7b 20 static void rza1_recv_callback(void);
mbed_official 30:da4b2487ee7b 21
mbed_official 30:da4b2487ee7b 22 static void rza1_recv_task(void *arg) {
mbed_official 30:da4b2487ee7b 23 struct netif *netif = (struct netif*)arg;
mbed_official 30:da4b2487ee7b 24 struct eth_hdr *ethhdr;
mbed_official 30:da4b2487ee7b 25 u16_t recv_size;
mbed_official 30:da4b2487ee7b 26 struct pbuf *p;
mbed_official 30:da4b2487ee7b 27 int cnt;
mbed_official 30:da4b2487ee7b 28
mbed_official 30:da4b2487ee7b 29 while (1) {
mbed_official 30:da4b2487ee7b 30 sys_arch_sem_wait(&recv_ready_sem, 0);
mbed_official 30:da4b2487ee7b 31 for (cnt = 0; cnt < 16; cnt++) {
mbed_official 30:da4b2487ee7b 32 recv_size = ethernet_receive();
mbed_official 30:da4b2487ee7b 33 if (recv_size != 0) {
mbed_official 30:da4b2487ee7b 34 p = pbuf_alloc(PBUF_RAW, recv_size, PBUF_RAM);
mbed_official 30:da4b2487ee7b 35 if (p != NULL) {
mbed_official 30:da4b2487ee7b 36 (void)ethernet_read((char *)p->payload, p->len);
mbed_official 30:da4b2487ee7b 37 ethhdr = p->payload;
mbed_official 30:da4b2487ee7b 38 switch (htons(ethhdr->type)) {
mbed_official 30:da4b2487ee7b 39 case ETHTYPE_IP:
mbed_official 30:da4b2487ee7b 40 case ETHTYPE_ARP:
mbed_official 30:da4b2487ee7b 41 #if PPPOE_SUPPORT
mbed_official 30:da4b2487ee7b 42 case ETHTYPE_PPPOEDISC:
mbed_official 30:da4b2487ee7b 43 case ETHTYPE_PPPOE:
mbed_official 30:da4b2487ee7b 44 #endif /* PPPOE_SUPPORT */
mbed_official 30:da4b2487ee7b 45 /* full packet send to tcpip_thread to process */
mbed_official 30:da4b2487ee7b 46 if (netif->input(p, netif) != ERR_OK) {
mbed_official 30:da4b2487ee7b 47 /* Free buffer */
mbed_official 30:da4b2487ee7b 48 pbuf_free(p);
mbed_official 30:da4b2487ee7b 49 }
mbed_official 30:da4b2487ee7b 50 break;
mbed_official 30:da4b2487ee7b 51 default:
mbed_official 30:da4b2487ee7b 52 /* Return buffer */
mbed_official 30:da4b2487ee7b 53 pbuf_free(p);
mbed_official 30:da4b2487ee7b 54 break;
mbed_official 30:da4b2487ee7b 55 }
mbed_official 30:da4b2487ee7b 56 }
mbed_official 30:da4b2487ee7b 57 } else {
mbed_official 30:da4b2487ee7b 58 break;
mbed_official 30:da4b2487ee7b 59 }
mbed_official 30:da4b2487ee7b 60 }
mbed_official 30:da4b2487ee7b 61 }
mbed_official 30:da4b2487ee7b 62 }
mbed_official 30:da4b2487ee7b 63
mbed_official 30:da4b2487ee7b 64 static void rza1_phy_task(void *arg) {
mbed_official 30:da4b2487ee7b 65 struct netif *netif = (struct netif*)arg;
mbed_official 30:da4b2487ee7b 66 s32_t connect_sts = 0; /* 0: disconnect, 1:connect */
mbed_official 30:da4b2487ee7b 67 s32_t link_sts;
mbed_official 30:da4b2487ee7b 68 s32_t link_mode_new = NEGO_FAIL;
mbed_official 30:da4b2487ee7b 69 s32_t link_mode_old = NEGO_FAIL;
mbed_official 30:da4b2487ee7b 70
mbed_official 30:da4b2487ee7b 71 while (1) {
mbed_official 30:da4b2487ee7b 72 link_sts = ethernet_link();
mbed_official 30:da4b2487ee7b 73 if (link_sts == 1) {
mbed_official 30:da4b2487ee7b 74 link_mode_new = ethernetext_chk_link_mode();
mbed_official 30:da4b2487ee7b 75 if (link_mode_new != link_mode_old) {
mbed_official 30:da4b2487ee7b 76 if (connect_sts == 1) {
mbed_official 30:da4b2487ee7b 77 tcpip_callback_with_block((tcpip_callback_fn)netif_set_link_down, (void*) netif, 1);
mbed_official 30:da4b2487ee7b 78 }
mbed_official 30:da4b2487ee7b 79 if (link_mode_new != NEGO_FAIL) {
mbed_official 30:da4b2487ee7b 80 ethernetext_set_link_mode(link_mode_new);
mbed_official 30:da4b2487ee7b 81 tcpip_callback_with_block((tcpip_callback_fn)netif_set_link_up, (void*) netif, 1);
mbed_official 30:da4b2487ee7b 82 connect_sts = 1;
mbed_official 30:da4b2487ee7b 83 }
mbed_official 30:da4b2487ee7b 84 }
mbed_official 30:da4b2487ee7b 85 } else {
mbed_official 30:da4b2487ee7b 86 if (connect_sts != 0) {
mbed_official 30:da4b2487ee7b 87 tcpip_callback_with_block((tcpip_callback_fn)netif_set_link_down, (void*) netif, 1);
mbed_official 30:da4b2487ee7b 88 link_mode_new = NEGO_FAIL;
mbed_official 30:da4b2487ee7b 89 connect_sts = 0;
mbed_official 30:da4b2487ee7b 90 }
mbed_official 30:da4b2487ee7b 91 }
mbed_official 30:da4b2487ee7b 92 link_mode_old = link_mode_new;
mbed_official 30:da4b2487ee7b 93 osDelay(PHY_TASK_WAIT);
mbed_official 30:da4b2487ee7b 94 }
mbed_official 30:da4b2487ee7b 95 }
mbed_official 30:da4b2487ee7b 96
mbed_official 30:da4b2487ee7b 97 static err_t rza1_etharp_output(struct netif *netif, struct pbuf *q, ip_addr_t *ipaddr) {
mbed_official 30:da4b2487ee7b 98 /* Only send packet is link is up */
mbed_official 30:da4b2487ee7b 99 if (netif->flags & NETIF_FLAG_LINK_UP) {
mbed_official 30:da4b2487ee7b 100 return etharp_output(netif, q, ipaddr);
mbed_official 30:da4b2487ee7b 101 }
mbed_official 30:da4b2487ee7b 102
mbed_official 30:da4b2487ee7b 103 return ERR_CONN;
mbed_official 30:da4b2487ee7b 104 }
mbed_official 30:da4b2487ee7b 105
mbed_official 30:da4b2487ee7b 106 static err_t rza1_low_level_output(struct netif *netif, struct pbuf *p) {
mbed_official 30:da4b2487ee7b 107 struct pbuf *q;
mbed_official 30:da4b2487ee7b 108 s32_t cnt;
mbed_official 30:da4b2487ee7b 109 err_t err = ERR_MEM;
mbed_official 30:da4b2487ee7b 110 s32_t write_size = 0;
mbed_official 30:da4b2487ee7b 111
mbed_official 30:da4b2487ee7b 112 if ((p->payload != NULL) && (p->len != 0)) {
mbed_official 30:da4b2487ee7b 113 /* If the first data can't be written, transmit descriptor is full. */
mbed_official 30:da4b2487ee7b 114 for (cnt = 0; cnt < 100; cnt++) {
mbed_official 30:da4b2487ee7b 115 write_size = ethernet_write((char *)p->payload, p->len);
mbed_official 30:da4b2487ee7b 116 if (write_size != 0) {
mbed_official 30:da4b2487ee7b 117 break;
mbed_official 30:da4b2487ee7b 118 }
mbed_official 30:da4b2487ee7b 119 osDelay(1);
mbed_official 30:da4b2487ee7b 120 }
mbed_official 30:da4b2487ee7b 121 if (write_size != 0) {
mbed_official 30:da4b2487ee7b 122 for (q = p->next; q != NULL; q = q->next) {
mbed_official 30:da4b2487ee7b 123 (void)ethernet_write((char *)q->payload, q->len);
mbed_official 30:da4b2487ee7b 124 }
mbed_official 30:da4b2487ee7b 125 if (ethernet_send() == 1) {
mbed_official 30:da4b2487ee7b 126 err = ERR_OK;
mbed_official 30:da4b2487ee7b 127 }
mbed_official 30:da4b2487ee7b 128 }
mbed_official 30:da4b2487ee7b 129 }
mbed_official 30:da4b2487ee7b 130
mbed_official 30:da4b2487ee7b 131 return err;
mbed_official 30:da4b2487ee7b 132 }
mbed_official 30:da4b2487ee7b 133
mbed_official 30:da4b2487ee7b 134 static void rza1_recv_callback(void) {
mbed_official 30:da4b2487ee7b 135 sys_sem_signal(&recv_ready_sem);
mbed_official 30:da4b2487ee7b 136 }
mbed_official 30:da4b2487ee7b 137
mbed_official 30:da4b2487ee7b 138 err_t eth_arch_enetif_init(struct netif *netif)
mbed_official 30:da4b2487ee7b 139 {
mbed_official 30:da4b2487ee7b 140 ethernet_cfg_t ethcfg;
mbed_official 30:da4b2487ee7b 141
mbed_official 30:da4b2487ee7b 142 /* set MAC hardware address */
mbed_official 30:da4b2487ee7b 143 #if (MBED_MAC_ADDRESS_SUM != MBED_MAC_ADDR_INTERFACE)
mbed_official 30:da4b2487ee7b 144 netif->hwaddr[0] = MBED_MAC_ADDR_0;
mbed_official 30:da4b2487ee7b 145 netif->hwaddr[1] = MBED_MAC_ADDR_1;
mbed_official 30:da4b2487ee7b 146 netif->hwaddr[2] = MBED_MAC_ADDR_2;
mbed_official 30:da4b2487ee7b 147 netif->hwaddr[3] = MBED_MAC_ADDR_3;
mbed_official 30:da4b2487ee7b 148 netif->hwaddr[4] = MBED_MAC_ADDR_4;
mbed_official 30:da4b2487ee7b 149 netif->hwaddr[5] = MBED_MAC_ADDR_5;
mbed_official 30:da4b2487ee7b 150 #else
mbed_official 30:da4b2487ee7b 151 mbed_mac_address((char *)netif->hwaddr);
mbed_official 30:da4b2487ee7b 152 #endif
mbed_official 30:da4b2487ee7b 153 netif->hwaddr_len = ETHARP_HWADDR_LEN;
mbed_official 30:da4b2487ee7b 154
mbed_official 30:da4b2487ee7b 155 /* maximum transfer unit */
mbed_official 30:da4b2487ee7b 156 netif->mtu = 1500;
mbed_official 30:da4b2487ee7b 157
mbed_official 30:da4b2487ee7b 158 /* device capabilities */
mbed_official 30:da4b2487ee7b 159 netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET | NETIF_FLAG_IGMP;
mbed_official 30:da4b2487ee7b 160
mbed_official 30:da4b2487ee7b 161 #if LWIP_NETIF_HOSTNAME
mbed_official 30:da4b2487ee7b 162 /* Initialize interface hostname */
mbed_official 30:da4b2487ee7b 163 netif->hostname = "lwiprza1";
mbed_official 30:da4b2487ee7b 164 #endif /* LWIP_NETIF_HOSTNAME */
mbed_official 30:da4b2487ee7b 165
mbed_official 30:da4b2487ee7b 166 netif->name[0] = 'e';
mbed_official 30:da4b2487ee7b 167 netif->name[1] = 'n';
mbed_official 30:da4b2487ee7b 168
mbed_official 30:da4b2487ee7b 169 netif->output = rza1_etharp_output;
mbed_official 30:da4b2487ee7b 170 netif->linkoutput = rza1_low_level_output;
mbed_official 30:da4b2487ee7b 171
mbed_official 30:da4b2487ee7b 172 /* Initialize the hardware */
mbed_official 30:da4b2487ee7b 173 ethcfg.int_priority = 6;
mbed_official 30:da4b2487ee7b 174 ethcfg.recv_cb = &rza1_recv_callback;
mbed_official 30:da4b2487ee7b 175 ethcfg.ether_mac = (char *)netif->hwaddr;
mbed_official 30:da4b2487ee7b 176 ethernetext_init(&ethcfg);
mbed_official 30:da4b2487ee7b 177
mbed_official 30:da4b2487ee7b 178 /* semaphore */
mbed_official 30:da4b2487ee7b 179 sys_sem_new(&recv_ready_sem, 0);
mbed_official 30:da4b2487ee7b 180
mbed_official 30:da4b2487ee7b 181 /* task */
mbed_official 30:da4b2487ee7b 182 sys_thread_new("rza1_recv_task", rza1_recv_task, netif, DEFAULT_THREAD_STACKSIZE, RECV_TASK_PRI);
mbed_official 30:da4b2487ee7b 183 sys_thread_new("rza1_phy_task", rza1_phy_task, netif, DEFAULT_THREAD_STACKSIZE, PHY_TASK_PRI);
mbed_official 30:da4b2487ee7b 184
mbed_official 30:da4b2487ee7b 185 return ERR_OK;
mbed_official 30:da4b2487ee7b 186 }
mbed_official 30:da4b2487ee7b 187
mbed_official 30:da4b2487ee7b 188 void eth_arch_enable_interrupts(void) {
mbed_official 30:da4b2487ee7b 189 ethernetext_start_stop(1);
mbed_official 30:da4b2487ee7b 190 }
mbed_official 30:da4b2487ee7b 191
mbed_official 30:da4b2487ee7b 192 void eth_arch_disable_interrupts(void) {
mbed_official 30:da4b2487ee7b 193 ethernetext_start_stop(0);
mbed_official 30:da4b2487ee7b 194 }