lwip-eth

Fork of lwip-eth by mbed official

Committer:
agaikwad
Date:
Wed Sep 26 17:42:45 2018 +0000
Revision:
34:edd90272457b
Parent:
29:4380f0749039
TCP echo Server

Who changed what in which revision?

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