2 #include "lwip/tcpip.h"
3 #include "netif/etharp.h"
4 #include "mbed_interface.h"
5 #include "ethernet_api.h"
6 #include "ethernetext_api.h"
8 #define RECV_TASK_PRI (osPriorityNormal)
9 #define PHY_TASK_PRI (osPriorityNormal)
10 #define PHY_TASK_WAIT (200)
13 static sys_sem_t recv_ready_sem; /* receive ready semaphore */
16 static void rza1_recv_task(void *arg);
17 static void rza1_phy_task(void *arg);
18 static err_t rza1_etharp_output(struct netif *netif, struct pbuf *q, ip_addr_t *ipaddr);
19 static err_t rza1_low_level_output(struct netif *netif, struct pbuf *p);
20 static void rza1_recv_callback(void);
22 static void rza1_recv_task(void *arg) {
23 struct netif *netif = (struct netif*)arg;
24 struct eth_hdr *ethhdr;
30 sys_arch_sem_wait(&recv_ready_sem, 0);
31 recv_size = ethernet_receive();
33 p = pbuf_alloc(PBUF_RAW, recv_size, PBUF_POOL);
35 for (q = p; q != NULL; q = q->next) {
36 (void)ethernet_read((char *)q->payload, q->len);
39 switch (htons(ethhdr->type)) {
43 case ETHTYPE_PPPOEDISC:
45 #endif /* PPPOE_SUPPORT */
46 /* full packet send to tcpip_thread to process */
47 if (netif->input(p, netif) != ERR_OK) {
62 static void rza1_phy_task(void *arg) {
63 struct netif *netif = (struct netif*)arg;
64 s32_t connect_sts = 0; /* 0: disconnect, 1:connect */
66 s32_t link_mode_new = NEGO_FAIL;
67 s32_t link_mode_old = NEGO_FAIL;
70 link_sts = ethernet_link();
72 link_mode_new = ethernetext_chk_link_mode();
73 if (link_mode_new != link_mode_old) {
74 if (connect_sts == 1) {
75 tcpip_callback_with_block((tcpip_callback_fn)netif_set_link_down, (void*) netif, 1);
77 if (link_mode_new != NEGO_FAIL) {
78 ethernetext_set_link_mode(link_mode_new);
79 tcpip_callback_with_block((tcpip_callback_fn)netif_set_link_up, (void*) netif, 1);
84 if (connect_sts != 0) {
85 tcpip_callback_with_block((tcpip_callback_fn)netif_set_link_down, (void*) netif, 1);
86 link_mode_new = NEGO_FAIL;
90 link_mode_old = link_mode_new;
91 osDelay(PHY_TASK_WAIT);
95 static err_t rza1_etharp_output(struct netif *netif, struct pbuf *q, ip_addr_t *ipaddr) {
96 /* Only send packet is link is up */
97 if (netif->flags & NETIF_FLAG_LINK_UP) {
98 return etharp_output(netif, q, ipaddr);
104 static err_t rza1_low_level_output(struct netif *netif, struct pbuf *p) {
108 s32_t write_size = 0;
110 if ((p->payload != NULL) && (p->len != 0)) {
111 /* If the first data can't be written, transmit descriptor is full. */
112 for (cnt = 0; cnt < 100; cnt++) {
113 write_size = ethernet_write((char *)p->payload, p->len);
114 if (write_size != 0) {
119 if (write_size != 0) {
120 for (q = p->next; q != NULL; q = q->next) {
121 (void)ethernet_write((char *)q->payload, q->len);
123 if (ethernet_send() == 1) {
132 static void rza1_recv_callback(void) {
133 sys_sem_signal(&recv_ready_sem);
136 err_t eth_arch_enetif_init(struct netif *netif)
138 ethernet_cfg_t ethcfg;
140 /* set MAC hardware address */
141 #if (MBED_MAC_ADDRESS_SUM != MBED_MAC_ADDR_INTERFACE)
142 netif->hwaddr[0] = MBED_MAC_ADDR_0;
143 netif->hwaddr[1] = MBED_MAC_ADDR_1;
144 netif->hwaddr[2] = MBED_MAC_ADDR_2;
145 netif->hwaddr[3] = MBED_MAC_ADDR_3;
146 netif->hwaddr[4] = MBED_MAC_ADDR_4;
147 netif->hwaddr[5] = MBED_MAC_ADDR_5;
149 mbed_mac_address((char *)netif->hwaddr);
151 netif->hwaddr_len = ETHARP_HWADDR_LEN;
153 /* maximum transfer unit */
156 /* device capabilities */
157 netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET | NETIF_FLAG_IGMP;
159 #if LWIP_NETIF_HOSTNAME
160 /* Initialize interface hostname */
161 netif->hostname = "lwiprza1";
162 #endif /* LWIP_NETIF_HOSTNAME */
164 netif->name[0] = 'e';
165 netif->name[1] = 'n';
167 netif->output = rza1_etharp_output;
168 netif->linkoutput = rza1_low_level_output;
170 /* Initialize the hardware */
171 ethcfg.int_priority = 6;
172 ethcfg.recv_cb = &rza1_recv_callback;
173 ethcfg.ether_mac = (char *)netif->hwaddr;
174 ethernetext_init(ðcfg);
177 sys_sem_new(&recv_ready_sem, 0);
180 sys_thread_new("rza1_recv_task", rza1_recv_task, netif, DEFAULT_THREAD_STACKSIZE, RECV_TASK_PRI);
181 sys_thread_new("rza1_phy_task", rza1_phy_task, netif, DEFAULT_THREAD_STACKSIZE, PHY_TASK_PRI);
186 void eth_arch_enable_interrupts(void) {
187 ethernetext_start_stop(1);
190 void eth_arch_disable_interrupts(void) {
191 ethernetext_start_stop(0);