]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/net/eth/lwip-eth/arch/TARGET_RZ_A1H/rza1_emac.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / net / eth / lwip-eth / arch / TARGET_RZ_A1H / rza1_emac.c
1 #include "lwip/opt.h"
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"
7
8 #define RECV_TASK_PRI           (osPriorityNormal)
9 #define PHY_TASK_PRI            (osPriorityNormal)
10 #define PHY_TASK_WAIT           (200)
11
12 /* memory */
13 static sys_sem_t recv_ready_sem;    /* receive ready semaphore */
14
15 /* function */
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);
21
22 static void rza1_recv_task(void *arg) {
23     struct netif   *netif = (struct netif*)arg;
24     struct eth_hdr *ethhdr;
25     u16_t          recv_size;
26     struct pbuf    *p;
27     struct pbuf    *q;
28
29     while (1) {
30         sys_arch_sem_wait(&recv_ready_sem, 0);
31         recv_size = ethernet_receive();
32         if (recv_size != 0) {
33             p = pbuf_alloc(PBUF_RAW, recv_size, PBUF_POOL);
34             if (p != NULL) {
35                 for (q = p; q != NULL; q = q->next) {
36                     (void)ethernet_read((char *)q->payload, q->len);
37                 }
38                 ethhdr = p->payload;
39                 switch (htons(ethhdr->type)) {
40                     case ETHTYPE_IP:
41                     case ETHTYPE_ARP:
42 #if PPPOE_SUPPORT
43                     case ETHTYPE_PPPOEDISC:
44                     case ETHTYPE_PPPOE:
45 #endif /* PPPOE_SUPPORT */
46                         /* full packet send to tcpip_thread to process */
47                         if (netif->input(p, netif) != ERR_OK) {
48                             /* Free buffer */
49                             pbuf_free(p);
50                         }
51                         break;
52                     default:
53                         /* Return buffer */
54                         pbuf_free(p);
55                         break;
56                 }
57             }
58         }
59     }
60 }
61
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 */
65     s32_t        link_sts;
66     s32_t        link_mode_new = NEGO_FAIL;
67     s32_t        link_mode_old = NEGO_FAIL;
68
69     while (1) {
70         link_sts = ethernet_link();
71         if (link_sts == 1) {
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);
76                 }
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);
80                     connect_sts = 1;
81                 }
82             }
83         } else {
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;
87                 connect_sts   = 0;
88             }
89         }
90         link_mode_old = link_mode_new;
91         osDelay(PHY_TASK_WAIT);
92     }
93 }
94
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);
99     }
100
101     return ERR_CONN;
102 }
103
104 static err_t rza1_low_level_output(struct netif *netif, struct pbuf *p) {
105     struct pbuf *q;
106     s32_t       cnt;
107     err_t       err        = ERR_MEM;
108     s32_t       write_size = 0;
109
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) {
115                 break;
116             }
117             osDelay(1);
118         }
119         if (write_size != 0) {
120             for (q = p->next; q != NULL; q = q->next) {
121                 (void)ethernet_write((char *)q->payload, q->len);
122             }
123             if (ethernet_send() == 1) {
124                 err = ERR_OK;
125             }
126         }
127     }
128
129     return err;
130 }
131
132 static void rza1_recv_callback(void) {
133     sys_sem_signal(&recv_ready_sem);
134 }
135
136 err_t eth_arch_enetif_init(struct netif *netif)
137 {
138     ethernet_cfg_t ethcfg;
139
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;
148 #else
149     mbed_mac_address((char *)netif->hwaddr);
150 #endif
151     netif->hwaddr_len = ETHARP_HWADDR_LEN;
152
153     /* maximum transfer unit */
154     netif->mtu = 1500;
155
156     /* device capabilities */
157     netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET | NETIF_FLAG_IGMP;
158
159 #if LWIP_NETIF_HOSTNAME
160     /* Initialize interface hostname */
161     netif->hostname = "lwiprza1";
162 #endif /* LWIP_NETIF_HOSTNAME */
163
164     netif->name[0] = 'e';
165     netif->name[1] = 'n';
166
167     netif->output     = rza1_etharp_output;
168     netif->linkoutput = rza1_low_level_output;
169
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(&ethcfg);
175
176     /* semaphore */
177     sys_sem_new(&recv_ready_sem, 0);
178
179     /* task */
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);
182
183     return ERR_OK;
184 }
185
186 void eth_arch_enable_interrupts(void) {
187     ethernetext_start_stop(1);
188 }
189
190 void eth_arch_disable_interrupts(void) {
191     ethernetext_start_stop(0);
192 }