1 // Copyright 2020 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 #include <string.h>
15 #include <stdlib.h>
16 #include <sys/cdefs.h>
17 #include "driver/gpio.h"
18 #include "driver/spi_master.h"
19 #include "esp_attr.h"
20 #include "esp_log.h"
21 #include "esp_check.h"
22 #include "esp_eth.h"
23 #include "esp_system.h"
24 #include "esp_intr_alloc.h"
25 #include "esp_heap_caps.h"
26 #include "esp_rom_gpio.h"
27 #include "freertos/FreeRTOS.h"
28 #include "freertos/task.h"
29 #include "freertos/semphr.h"
30 #include "hal/cpu_hal.h"
31 #include "w5500.h"
32 #include "sdkconfig.h"
33 
34 static const char *TAG = "w5500.mac";
35 
36 #define W5500_SPI_LOCK_TIMEOUT_MS (50)
37 #define W5500_TX_MEM_SIZE (0x4000)
38 #define W5500_RX_MEM_SIZE (0x4000)
39 
40 typedef struct {
41     esp_eth_mac_t parent;
42     esp_eth_mediator_t *eth;
43     spi_device_handle_t spi_hdl;
44     SemaphoreHandle_t spi_lock;
45     TaskHandle_t rx_task_hdl;
46     uint32_t sw_reset_timeout_ms;
47     int int_gpio_num;
48     uint8_t addr[6];
49     bool packets_remain;
50 } emac_w5500_t;
51 
w5500_lock(emac_w5500_t * emac)52 static inline bool w5500_lock(emac_w5500_t *emac)
53 {
54     return xSemaphoreTake(emac->spi_lock, pdMS_TO_TICKS(W5500_SPI_LOCK_TIMEOUT_MS)) == pdTRUE;
55 }
56 
w5500_unlock(emac_w5500_t * emac)57 static inline bool w5500_unlock(emac_w5500_t *emac)
58 {
59     return xSemaphoreGive(emac->spi_lock) == pdTRUE;
60 }
61 
w5500_write(emac_w5500_t * emac,uint32_t address,const void * value,uint32_t len)62 static esp_err_t w5500_write(emac_w5500_t *emac, uint32_t address, const void *value, uint32_t len)
63 {
64     esp_err_t ret = ESP_OK;
65 
66     spi_transaction_t trans = {
67         .cmd = (address >> W5500_ADDR_OFFSET),
68         .addr = ((address & 0xFFFF) | (W5500_ACCESS_MODE_WRITE << W5500_RWB_OFFSET) | W5500_SPI_OP_MODE_VDM),
69         .length = 8 * len,
70         .tx_buffer = value
71     };
72     if (w5500_lock(emac)) {
73         if (spi_device_polling_transmit(emac->spi_hdl, &trans) != ESP_OK) {
74             ESP_LOGE(TAG, "%s(%d): spi transmit failed", __FUNCTION__, __LINE__);
75             ret = ESP_FAIL;
76         }
77         w5500_unlock(emac);
78     } else {
79         ret = ESP_ERR_TIMEOUT;
80     }
81     return ret;
82 }
83 
w5500_read(emac_w5500_t * emac,uint32_t address,void * value,uint32_t len)84 static esp_err_t w5500_read(emac_w5500_t *emac, uint32_t address, void *value, uint32_t len)
85 {
86     esp_err_t ret = ESP_OK;
87 
88     spi_transaction_t trans = {
89         .flags = len <= 4 ? SPI_TRANS_USE_RXDATA : 0, // use direct reads for registers to prevent overwrites by 4-byte boundary writes
90         .cmd = (address >> W5500_ADDR_OFFSET),
91         .addr = ((address & 0xFFFF) | (W5500_ACCESS_MODE_READ << W5500_RWB_OFFSET) | W5500_SPI_OP_MODE_VDM),
92         .length = 8 * len,
93         .rx_buffer = value
94     };
95     if (w5500_lock(emac)) {
96         if (spi_device_polling_transmit(emac->spi_hdl, &trans) != ESP_OK) {
97             ESP_LOGE(TAG, "%s(%d): spi transmit failed", __FUNCTION__, __LINE__);
98             ret = ESP_FAIL;
99         }
100         w5500_unlock(emac);
101     } else {
102         ret = ESP_ERR_TIMEOUT;
103     }
104     if ((trans.flags&SPI_TRANS_USE_RXDATA) && len <= 4) {
105         memcpy(value, trans.rx_data, len);  // copy register values to output
106     }
107     return ret;
108 }
109 
w5500_send_command(emac_w5500_t * emac,uint8_t command,uint32_t timeout_ms)110 static esp_err_t w5500_send_command(emac_w5500_t *emac, uint8_t command, uint32_t timeout_ms)
111 {
112     esp_err_t ret = ESP_OK;
113     ESP_GOTO_ON_ERROR(w5500_write(emac, W5500_REG_SOCK_CR(0), &command, sizeof(command)), err, TAG, "write SCR failed");
114     // after W5500 accepts the command, the command register will be cleared automatically
115     uint32_t to = 0;
116     for (to = 0; to < timeout_ms / 10; to++) {
117         ESP_GOTO_ON_ERROR(w5500_read(emac, W5500_REG_SOCK_CR(0), &command, sizeof(command)), err, TAG, "read SCR failed");
118         if (!command) {
119             break;
120         }
121         vTaskDelay(pdMS_TO_TICKS(10));
122     }
123     ESP_GOTO_ON_FALSE(to < timeout_ms / 10, ESP_ERR_TIMEOUT, err, TAG, "send command timeout");
124 
125 err:
126     return ret;
127 }
128 
w5500_get_tx_free_size(emac_w5500_t * emac,uint16_t * size)129 static esp_err_t w5500_get_tx_free_size(emac_w5500_t *emac, uint16_t *size)
130 {
131     esp_err_t ret = ESP_OK;
132     uint16_t free0, free1 = 0;
133     // read TX_FSR register more than once, until we get the same value
134     // this is a trick because we might be interrupted between reading the high/low part of the TX_FSR register (16 bits in length)
135     do {
136         ESP_GOTO_ON_ERROR(w5500_read(emac, W5500_REG_SOCK_TX_FSR(0), &free0, sizeof(free0)), err, TAG, "read TX FSR failed");
137         ESP_GOTO_ON_ERROR(w5500_read(emac, W5500_REG_SOCK_TX_FSR(0), &free1, sizeof(free1)), err, TAG, "read TX FSR failed");
138     } while (free0 != free1);
139 
140     *size = __builtin_bswap16(free0);
141 
142 err:
143     return ret;
144 }
145 
w5500_get_rx_received_size(emac_w5500_t * emac,uint16_t * size)146 static esp_err_t w5500_get_rx_received_size(emac_w5500_t *emac, uint16_t *size)
147 {
148     esp_err_t ret = ESP_OK;
149     uint16_t received0, received1 = 0;
150     do {
151         ESP_GOTO_ON_ERROR(w5500_read(emac, W5500_REG_SOCK_RX_RSR(0), &received0, sizeof(received0)), err, TAG, "read RX RSR failed");
152         ESP_GOTO_ON_ERROR(w5500_read(emac, W5500_REG_SOCK_RX_RSR(0), &received1, sizeof(received1)), err, TAG, "read RX RSR failed");
153     } while (received0 != received1);
154     *size = __builtin_bswap16(received0);
155 
156 err:
157     return ret;
158 }
159 
w5500_write_buffer(emac_w5500_t * emac,const void * buffer,uint32_t len,uint16_t offset)160 static esp_err_t w5500_write_buffer(emac_w5500_t *emac, const void *buffer, uint32_t len, uint16_t offset)
161 {
162     esp_err_t ret = ESP_OK;
163     uint32_t remain = len;
164     const uint8_t *buf = buffer;
165     offset %= W5500_TX_MEM_SIZE;
166     if (offset + len > W5500_TX_MEM_SIZE) {
167         remain = (offset + len) % W5500_TX_MEM_SIZE;
168         len = W5500_TX_MEM_SIZE - offset;
169         ESP_GOTO_ON_ERROR(w5500_write(emac, W5500_MEM_SOCK_TX(0, offset), buf, len), err, TAG, "write TX buffer failed");
170         offset += len;
171         buf += len;
172     }
173     ESP_GOTO_ON_ERROR(w5500_write(emac, W5500_MEM_SOCK_TX(0, offset), buf, remain), err, TAG, "write TX buffer failed");
174 
175 err:
176     return ret;
177 }
178 
w5500_read_buffer(emac_w5500_t * emac,void * buffer,uint32_t len,uint16_t offset)179 static esp_err_t w5500_read_buffer(emac_w5500_t *emac, void *buffer, uint32_t len, uint16_t offset)
180 {
181     esp_err_t ret = ESP_OK;
182     uint32_t remain = len;
183     uint8_t *buf = buffer;
184     offset %= W5500_RX_MEM_SIZE;
185     if (offset + len > W5500_RX_MEM_SIZE) {
186         remain = (offset + len) % W5500_RX_MEM_SIZE;
187         len = W5500_RX_MEM_SIZE - offset;
188         ESP_GOTO_ON_ERROR(w5500_read(emac, W5500_MEM_SOCK_RX(0, offset), buf, len), err, TAG, "read RX buffer failed");
189         offset += len;
190         buf += len;
191     }
192     ESP_GOTO_ON_ERROR(w5500_read(emac, W5500_MEM_SOCK_RX(0, offset), buf, remain), err, TAG, "read RX buffer failed");
193 
194 err:
195     return ret;
196 }
197 
w5500_set_mac_addr(emac_w5500_t * emac)198 static esp_err_t w5500_set_mac_addr(emac_w5500_t *emac)
199 {
200     esp_err_t ret = ESP_OK;
201     ESP_GOTO_ON_ERROR(w5500_write(emac, W5500_REG_MAC, emac->addr, 6), err, TAG, "write MAC address register failed");
202 
203 err:
204     return ret;
205 }
206 
w5500_reset(emac_w5500_t * emac)207 static esp_err_t w5500_reset(emac_w5500_t *emac)
208 {
209     esp_err_t ret = ESP_OK;
210     /* software reset */
211     uint8_t mr = W5500_MR_RST; // Set RST bit (auto clear)
212     ESP_GOTO_ON_ERROR(w5500_write(emac, W5500_REG_MR, &mr, sizeof(mr)), err, TAG, "write MR failed");
213     uint32_t to = 0;
214     for (to = 0; to < emac->sw_reset_timeout_ms / 10; to++) {
215         ESP_GOTO_ON_ERROR(w5500_read(emac, W5500_REG_MR, &mr, sizeof(mr)), err, TAG, "read MR failed");
216         if (!(mr & W5500_MR_RST)) {
217             break;
218         }
219         vTaskDelay(pdMS_TO_TICKS(10));
220     }
221     ESP_GOTO_ON_FALSE(to < emac->sw_reset_timeout_ms / 10, ESP_ERR_TIMEOUT, err, TAG, "reset timeout");
222 
223 err:
224     return ret;
225 }
226 
w5500_verify_id(emac_w5500_t * emac)227 static esp_err_t w5500_verify_id(emac_w5500_t *emac)
228 {
229     esp_err_t ret = ESP_OK;
230     uint8_t version = 0;
231     ESP_GOTO_ON_ERROR(w5500_read(emac, W5500_REG_VERSIONR, &version, sizeof(version)), err, TAG, "read VERSIONR failed");
232     // W5500 doesn't have chip ID, we just print the version number instead
233     ESP_LOGI(TAG, "version=%x", version);
234 
235 err:
236     return ret;
237 }
238 
w5500_setup_default(emac_w5500_t * emac)239 static esp_err_t w5500_setup_default(emac_w5500_t *emac)
240 {
241     esp_err_t ret = ESP_OK;
242     uint8_t reg_value = 16;
243 
244     // Only SOCK0 can be used as MAC RAW mode, so we give the whole buffer (16KB TX and 16KB RX) to SOCK0
245     ESP_GOTO_ON_ERROR(w5500_write(emac, W5500_REG_SOCK_RXBUF_SIZE(0), &reg_value, sizeof(reg_value)), err, TAG, "set rx buffer size failed");
246     ESP_GOTO_ON_ERROR(w5500_write(emac, W5500_REG_SOCK_TXBUF_SIZE(0), &reg_value, sizeof(reg_value)), err, TAG, "set tx buffer size failed");
247     reg_value = 0;
248     for (int i = 1; i < 8; i++) {
249         ESP_GOTO_ON_ERROR(w5500_write(emac, W5500_REG_SOCK_RXBUF_SIZE(i), &reg_value, sizeof(reg_value)), err, TAG, "set rx buffer size failed");
250         ESP_GOTO_ON_ERROR(w5500_write(emac, W5500_REG_SOCK_TXBUF_SIZE(i), &reg_value, sizeof(reg_value)), err, TAG, "set tx buffer size failed");
251     }
252 
253     /* Enable ping block, disable PPPoE, WOL */
254     reg_value = W5500_MR_PB;
255     ESP_GOTO_ON_ERROR(w5500_write(emac, W5500_REG_MR, &reg_value, sizeof(reg_value)), err, TAG, "write MR failed");
256     /* Disable interrupt for all sockets by default */
257     reg_value = 0;
258     ESP_GOTO_ON_ERROR(w5500_write(emac, W5500_REG_SIMR, &reg_value, sizeof(reg_value)), err, TAG, "write SIMR failed");
259     /* Enable MAC RAW mode for SOCK0, enable MAC filter, no blocking broadcast and multicast */
260     reg_value = W5500_SMR_MAC_RAW | W5500_SMR_MAC_FILTER;
261     ESP_GOTO_ON_ERROR(w5500_write(emac, W5500_REG_SOCK_MR(0), &reg_value, sizeof(reg_value)), err, TAG, "write SMR failed");
262     /* Enable receive event for SOCK0 */
263     reg_value = W5500_SIR_RECV;
264     ESP_GOTO_ON_ERROR(w5500_write(emac, W5500_REG_SOCK_IMR(0), &reg_value, sizeof(reg_value)), err, TAG, "write SOCK0 IMR failed");
265     /* Set the interrupt re-assert level to maximum (~1.5ms) to lower the chances of missing it */
266     uint16_t int_level = __builtin_bswap16(0xFFFF);
267     ESP_GOTO_ON_ERROR(w5500_write(emac, W5500_REG_INTLEVEL, &int_level, sizeof(int_level)), err, TAG, "write INTLEVEL failed");
268 
269 err:
270     return ret;
271 }
272 
emac_w5500_start(esp_eth_mac_t * mac)273 static esp_err_t emac_w5500_start(esp_eth_mac_t *mac)
274 {
275     esp_err_t ret = ESP_OK;
276     emac_w5500_t *emac = __containerof(mac, emac_w5500_t, parent);
277     uint8_t reg_value = 0;
278     /* open SOCK0 */
279     ESP_GOTO_ON_ERROR(w5500_send_command(emac, W5500_SCR_OPEN, 100), err, TAG, "issue OPEN command failed");
280     /* enable interrupt for SOCK0 */
281     reg_value = W5500_SIMR_SOCK0;
282     ESP_GOTO_ON_ERROR(w5500_write(emac, W5500_REG_SIMR, &reg_value, sizeof(reg_value)), err, TAG, "write SIMR failed");
283 
284 err:
285     return ret;
286 }
287 
emac_w5500_stop(esp_eth_mac_t * mac)288 static esp_err_t emac_w5500_stop(esp_eth_mac_t *mac)
289 {
290     esp_err_t ret = ESP_OK;
291     emac_w5500_t *emac = __containerof(mac, emac_w5500_t, parent);
292     uint8_t reg_value = 0;
293     /* disable interrupt */
294     ESP_GOTO_ON_ERROR(w5500_write(emac, W5500_REG_SIMR, &reg_value, sizeof(reg_value)), err, TAG, "write SIMR failed");
295     /* close SOCK0 */
296     ESP_GOTO_ON_ERROR(w5500_send_command(emac, W5500_SCR_CLOSE, 100), err, TAG, "issue CLOSE command failed");
297 
298 err:
299     return ret;
300 }
301 
w5500_isr_handler(void * arg)302 IRAM_ATTR static void w5500_isr_handler(void *arg)
303 {
304     emac_w5500_t *emac = (emac_w5500_t *)arg;
305     BaseType_t high_task_wakeup = pdFALSE;
306     /* notify w5500 task */
307     vTaskNotifyGiveFromISR(emac->rx_task_hdl, &high_task_wakeup);
308     if (high_task_wakeup != pdFALSE) {
309         portYIELD_FROM_ISR();
310     }
311 }
312 
emac_w5500_task(void * arg)313 static void emac_w5500_task(void *arg)
314 {
315     emac_w5500_t *emac = (emac_w5500_t *)arg;
316     uint8_t status = 0;
317     uint8_t *buffer = NULL;
318     uint32_t length = 0;
319     while (1) {
320         // check if the task receives any notification
321         if (ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(1000)) == 0 &&    // if no notification ...
322             gpio_get_level(emac->int_gpio_num) != 0) {               // ...and no interrupt asserted
323             continue;                                                // -> just continue to check again
324         }
325 
326         /* read interrupt status */
327         w5500_read(emac, W5500_REG_SOCK_IR(0), &status, sizeof(status));
328         /* packet received */
329         if (status & W5500_SIR_RECV) {
330             status = W5500_SIR_RECV;
331             // clear interrupt status
332             w5500_write(emac, W5500_REG_SOCK_IR(0), &status, sizeof(status));
333             do {
334                 length = ETH_MAX_PACKET_SIZE;
335                 buffer = heap_caps_malloc(length, MALLOC_CAP_DMA);
336                 if (!buffer) {
337                     ESP_LOGE(TAG, "no mem for receive buffer");
338                     break;
339                 } else if (emac->parent.receive(&emac->parent, buffer, &length) == ESP_OK) {
340                     /* pass the buffer to stack (e.g. TCP/IP layer) */
341                     if (length) {
342                         emac->eth->stack_input(emac->eth, buffer, length);
343                     } else {
344                         free(buffer);
345                     }
346                 } else {
347                     free(buffer);
348                 }
349             } while (emac->packets_remain);
350         }
351     }
352     vTaskDelete(NULL);
353 }
354 
emac_w5500_set_mediator(esp_eth_mac_t * mac,esp_eth_mediator_t * eth)355 static esp_err_t emac_w5500_set_mediator(esp_eth_mac_t *mac, esp_eth_mediator_t *eth)
356 {
357     esp_err_t ret = ESP_OK;
358     ESP_GOTO_ON_FALSE(eth, ESP_ERR_INVALID_ARG, err, TAG, "can't set mac's mediator to null");
359     emac_w5500_t *emac = __containerof(mac, emac_w5500_t, parent);
360     emac->eth = eth;
361     return ESP_OK;
362 err:
363     return ret;
364 }
365 
emac_w5500_write_phy_reg(esp_eth_mac_t * mac,uint32_t phy_addr,uint32_t phy_reg,uint32_t reg_value)366 static esp_err_t emac_w5500_write_phy_reg(esp_eth_mac_t *mac, uint32_t phy_addr, uint32_t phy_reg, uint32_t reg_value)
367 {
368     esp_err_t ret = ESP_OK;
369     emac_w5500_t *emac = __containerof(mac, emac_w5500_t, parent);
370     // PHY register and MAC registers are mixed together in W5500
371     // The only PHY register is PHYCFGR
372     ESP_GOTO_ON_FALSE(phy_reg == W5500_REG_PHYCFGR, ESP_FAIL, err, TAG, "wrong PHY register");
373     ESP_GOTO_ON_ERROR(w5500_write(emac, W5500_REG_PHYCFGR, &reg_value, sizeof(uint8_t)), err, TAG, "write PHY register failed");
374 
375 err:
376     return ret;
377 }
378 
emac_w5500_read_phy_reg(esp_eth_mac_t * mac,uint32_t phy_addr,uint32_t phy_reg,uint32_t * reg_value)379 static esp_err_t emac_w5500_read_phy_reg(esp_eth_mac_t *mac, uint32_t phy_addr, uint32_t phy_reg, uint32_t *reg_value)
380 {
381     esp_err_t ret = ESP_OK;
382     ESP_GOTO_ON_FALSE(reg_value, ESP_ERR_INVALID_ARG, err, TAG, "can't set reg_value to null");
383     emac_w5500_t *emac = __containerof(mac, emac_w5500_t, parent);
384     // PHY register and MAC registers are mixed together in W5500
385     // The only PHY register is PHYCFGR
386     ESP_GOTO_ON_FALSE(phy_reg == W5500_REG_PHYCFGR, ESP_FAIL, err, TAG, "wrong PHY register");
387     ESP_GOTO_ON_ERROR(w5500_read(emac, W5500_REG_PHYCFGR, reg_value, sizeof(uint8_t)), err, TAG, "read PHY register failed");
388 
389 err:
390     return ret;
391 }
392 
emac_w5500_set_addr(esp_eth_mac_t * mac,uint8_t * addr)393 static esp_err_t emac_w5500_set_addr(esp_eth_mac_t *mac, uint8_t *addr)
394 {
395     esp_err_t ret = ESP_OK;
396     ESP_GOTO_ON_FALSE(addr, ESP_ERR_INVALID_ARG, err, TAG, "invalid argument");
397     emac_w5500_t *emac = __containerof(mac, emac_w5500_t, parent);
398     memcpy(emac->addr, addr, 6);
399     ESP_GOTO_ON_ERROR(w5500_set_mac_addr(emac), err, TAG, "set mac address failed");
400 
401 err:
402     return ret;
403 }
404 
emac_w5500_get_addr(esp_eth_mac_t * mac,uint8_t * addr)405 static esp_err_t emac_w5500_get_addr(esp_eth_mac_t *mac, uint8_t *addr)
406 {
407     esp_err_t ret = ESP_OK;
408     ESP_GOTO_ON_FALSE(addr, ESP_ERR_INVALID_ARG, err, TAG, "invalid argument");
409     emac_w5500_t *emac = __containerof(mac, emac_w5500_t, parent);
410     memcpy(addr, emac->addr, 6);
411 
412 err:
413     return ret;
414 }
415 
emac_w5500_set_link(esp_eth_mac_t * mac,eth_link_t link)416 static esp_err_t emac_w5500_set_link(esp_eth_mac_t *mac, eth_link_t link)
417 {
418     esp_err_t ret = ESP_OK;
419     switch (link) {
420     case ETH_LINK_UP:
421         ESP_LOGD(TAG, "link is up");
422         ESP_GOTO_ON_ERROR(mac->start(mac), err, TAG, "w5500 start failed");
423         break;
424     case ETH_LINK_DOWN:
425         ESP_LOGD(TAG, "link is down");
426         ESP_GOTO_ON_ERROR(mac->stop(mac), err, TAG, "w5500 stop failed");
427         break;
428     default:
429         ESP_GOTO_ON_FALSE(false, ESP_ERR_INVALID_ARG, err, TAG, "unknown link status");
430         break;
431     }
432 
433 err:
434     return ret;
435 }
436 
emac_w5500_set_speed(esp_eth_mac_t * mac,eth_speed_t speed)437 static esp_err_t emac_w5500_set_speed(esp_eth_mac_t *mac, eth_speed_t speed)
438 {
439     esp_err_t ret = ESP_OK;
440     switch (speed) {
441     case ETH_SPEED_10M:
442         ESP_LOGD(TAG, "working in 10Mbps");
443         break;
444     case ETH_SPEED_100M:
445         ESP_LOGD(TAG, "working in 100Mbps");
446         break;
447     default:
448         ESP_GOTO_ON_FALSE(false, ESP_ERR_INVALID_ARG, err, TAG, "unknown speed");
449         break;
450     }
451 
452 err:
453     return ret;
454 }
455 
emac_w5500_set_duplex(esp_eth_mac_t * mac,eth_duplex_t duplex)456 static esp_err_t emac_w5500_set_duplex(esp_eth_mac_t *mac, eth_duplex_t duplex)
457 {
458     esp_err_t ret = ESP_OK;
459     switch (duplex) {
460     case ETH_DUPLEX_HALF:
461         ESP_LOGD(TAG, "working in half duplex");
462         break;
463     case ETH_DUPLEX_FULL:
464         ESP_LOGD(TAG, "working in full duplex");
465         break;
466     default:
467         ESP_GOTO_ON_FALSE(false, ESP_ERR_INVALID_ARG, err, TAG, "unknown duplex");
468         break;
469     }
470 
471 err:
472     return ret;
473 }
474 
emac_w5500_set_promiscuous(esp_eth_mac_t * mac,bool enable)475 static esp_err_t emac_w5500_set_promiscuous(esp_eth_mac_t *mac, bool enable)
476 {
477     esp_err_t ret = ESP_OK;
478     emac_w5500_t *emac = __containerof(mac, emac_w5500_t, parent);
479     uint8_t smr = 0;
480     ESP_GOTO_ON_ERROR(w5500_read(emac, W5500_REG_SOCK_MR(0), &smr, sizeof(smr)), err, TAG, "read SMR failed");
481     if (enable) {
482         smr &= ~W5500_SMR_MAC_FILTER;
483     } else {
484         smr |= W5500_SMR_MAC_FILTER;
485     }
486     ESP_GOTO_ON_ERROR(w5500_write(emac, W5500_REG_SOCK_MR(0), &smr, sizeof(smr)), err, TAG, "write SMR failed");
487 
488 err:
489     return ret;
490 }
491 
emac_w5500_enable_flow_ctrl(esp_eth_mac_t * mac,bool enable)492 static esp_err_t emac_w5500_enable_flow_ctrl(esp_eth_mac_t *mac, bool enable)
493 {
494     /* w5500 doesn't support flow control function, so accept any value */
495     return ESP_ERR_NOT_SUPPORTED;
496 }
497 
emac_w5500_set_peer_pause_ability(esp_eth_mac_t * mac,uint32_t ability)498 static esp_err_t emac_w5500_set_peer_pause_ability(esp_eth_mac_t *mac, uint32_t ability)
499 {
500     /* w5500 doesn't suppport PAUSE function, so accept any value */
501     return ESP_ERR_NOT_SUPPORTED;
502 }
503 
is_w5500_sane_for_rxtx(emac_w5500_t * emac)504 static inline bool is_w5500_sane_for_rxtx(emac_w5500_t *emac)
505 {
506     uint8_t phycfg;
507     /* phy is ok for rx and tx operations if bits RST and LNK are set (no link down, no reset) */
508     if (w5500_read(emac, W5500_REG_PHYCFGR, &phycfg, 1) == ESP_OK && (phycfg & 0x8001)) {
509         return true;
510     }
511    return false;
512 }
513 
emac_w5500_transmit(esp_eth_mac_t * mac,uint8_t * buf,uint32_t length)514 static esp_err_t emac_w5500_transmit(esp_eth_mac_t *mac, uint8_t *buf, uint32_t length)
515 {
516     esp_err_t ret = ESP_OK;
517     emac_w5500_t *emac = __containerof(mac, emac_w5500_t, parent);
518     uint16_t offset = 0;
519 
520     // check if there're free memory to store this packet
521     uint16_t free_size = 0;
522     ESP_GOTO_ON_ERROR(w5500_get_tx_free_size(emac, &free_size), err, TAG, "get free size failed");
523     ESP_GOTO_ON_FALSE(length <= free_size, ESP_ERR_NO_MEM, err, TAG, "free size (%d) < send length (%d)", length, free_size);
524     // get current write pointer
525     ESP_GOTO_ON_ERROR(w5500_read(emac, W5500_REG_SOCK_TX_WR(0), &offset, sizeof(offset)), err, TAG, "read TX WR failed");
526     offset = __builtin_bswap16(offset);
527     // copy data to tx memory
528     ESP_GOTO_ON_ERROR(w5500_write_buffer(emac, buf, length, offset), err, TAG, "write frame failed");
529     // update write pointer
530     offset += length;
531     offset = __builtin_bswap16(offset);
532     ESP_GOTO_ON_ERROR(w5500_write(emac, W5500_REG_SOCK_TX_WR(0), &offset, sizeof(offset)), err, TAG, "write TX WR failed");
533     // issue SEND command
534     ESP_GOTO_ON_ERROR(w5500_send_command(emac, W5500_SCR_SEND, 100), err, TAG, "issue SEND command failed");
535 
536     // pooling the TX done event
537     int retry = 0;
538     uint8_t status = 0;
539     while (!(status & W5500_SIR_SEND)) {
540         ESP_GOTO_ON_ERROR(w5500_read(emac, W5500_REG_SOCK_IR(0), &status, sizeof(status)), err, TAG, "read SOCK0 IR failed");
541         if ((retry++ > 3 && !is_w5500_sane_for_rxtx(emac)) || retry > 10) {
542             return ESP_FAIL;
543         }
544     }
545     // clear the event bit
546     status  = W5500_SIR_SEND;
547     ESP_GOTO_ON_ERROR(w5500_write(emac, W5500_REG_SOCK_IR(0), &status, sizeof(status)), err, TAG, "write SOCK0 IR failed");
548 
549 err:
550     return ret;
551 }
552 
emac_w5500_receive(esp_eth_mac_t * mac,uint8_t * buf,uint32_t * length)553 static esp_err_t emac_w5500_receive(esp_eth_mac_t *mac, uint8_t *buf, uint32_t *length)
554 {
555     esp_err_t ret = ESP_OK;
556     emac_w5500_t *emac = __containerof(mac, emac_w5500_t, parent);
557     uint16_t offset = 0;
558     uint16_t rx_len = 0;
559     uint16_t remain_bytes = 0;
560     emac->packets_remain = false;
561 
562     w5500_get_rx_received_size(emac, &remain_bytes);
563     if (remain_bytes) {
564         // get current read pointer
565         ESP_GOTO_ON_ERROR(w5500_read(emac, W5500_REG_SOCK_RX_RD(0), &offset, sizeof(offset)), err, TAG, "read RX RD failed");
566         offset = __builtin_bswap16(offset);
567         // read head first
568         ESP_GOTO_ON_ERROR(w5500_read_buffer(emac, &rx_len, sizeof(rx_len), offset), err, TAG, "read frame header failed");
569         rx_len = __builtin_bswap16(rx_len) - 2; // data size includes 2 bytes of header
570         offset += 2;
571         // read the payload
572         ESP_GOTO_ON_ERROR(w5500_read_buffer(emac, buf, rx_len, offset), err, TAG, "read payload failed, len=%d, offset=%d", rx_len, offset);
573         offset += rx_len;
574         // update read pointer
575         offset = __builtin_bswap16(offset);
576         ESP_GOTO_ON_ERROR(w5500_write(emac, W5500_REG_SOCK_RX_RD(0), &offset, sizeof(offset)), err, TAG, "write RX RD failed");
577         /* issue RECV command */
578         ESP_GOTO_ON_ERROR(w5500_send_command(emac, W5500_SCR_RECV, 100), err, TAG, "issue RECV command failed");
579         // check if there're more data need to process
580         remain_bytes -= rx_len + 2;
581         emac->packets_remain = remain_bytes > 0;
582     }
583 
584     *length = rx_len;
585 err:
586     return ret;
587 }
588 
emac_w5500_init(esp_eth_mac_t * mac)589 static esp_err_t emac_w5500_init(esp_eth_mac_t *mac)
590 {
591     esp_err_t ret = ESP_OK;
592     emac_w5500_t *emac = __containerof(mac, emac_w5500_t, parent);
593     esp_eth_mediator_t *eth = emac->eth;
594     esp_rom_gpio_pad_select_gpio(emac->int_gpio_num);
595     gpio_set_direction(emac->int_gpio_num, GPIO_MODE_INPUT);
596     gpio_set_pull_mode(emac->int_gpio_num, GPIO_PULLUP_ONLY);
597     gpio_set_intr_type(emac->int_gpio_num, GPIO_INTR_NEGEDGE); // active low
598     gpio_intr_enable(emac->int_gpio_num);
599     gpio_isr_handler_add(emac->int_gpio_num, w5500_isr_handler, emac);
600     ESP_GOTO_ON_ERROR(eth->on_state_changed(eth, ETH_STATE_LLINIT, NULL), err, TAG, "lowlevel init failed");
601     /* reset w5500 */
602     ESP_GOTO_ON_ERROR(w5500_reset(emac), err, TAG, "reset w5500 failed");
603     /* verify chip id */
604     ESP_GOTO_ON_ERROR(w5500_verify_id(emac), err, TAG, "vefiry chip ID failed");
605     /* default setup of internal registers */
606     ESP_GOTO_ON_ERROR(w5500_setup_default(emac), err, TAG, "w5500 default setup failed");
607     return ESP_OK;
608 err:
609     gpio_isr_handler_remove(emac->int_gpio_num);
610     gpio_reset_pin(emac->int_gpio_num);
611     eth->on_state_changed(eth, ETH_STATE_DEINIT, NULL);
612     return ret;
613 }
614 
emac_w5500_deinit(esp_eth_mac_t * mac)615 static esp_err_t emac_w5500_deinit(esp_eth_mac_t *mac)
616 {
617     emac_w5500_t *emac = __containerof(mac, emac_w5500_t, parent);
618     esp_eth_mediator_t *eth = emac->eth;
619     mac->stop(mac);
620     gpio_isr_handler_remove(emac->int_gpio_num);
621     gpio_reset_pin(emac->int_gpio_num);
622     eth->on_state_changed(eth, ETH_STATE_DEINIT, NULL);
623     return ESP_OK;
624 }
625 
emac_w5500_del(esp_eth_mac_t * mac)626 static esp_err_t emac_w5500_del(esp_eth_mac_t *mac)
627 {
628     emac_w5500_t *emac = __containerof(mac, emac_w5500_t, parent);
629     vTaskDelete(emac->rx_task_hdl);
630     vSemaphoreDelete(emac->spi_lock);
631     free(emac);
632     return ESP_OK;
633 }
634 
esp_eth_mac_new_w5500(const eth_w5500_config_t * w5500_config,const eth_mac_config_t * mac_config)635 esp_eth_mac_t *esp_eth_mac_new_w5500(const eth_w5500_config_t *w5500_config, const eth_mac_config_t *mac_config)
636 {
637     esp_eth_mac_t *ret = NULL;
638     emac_w5500_t *emac = NULL;
639     ESP_GOTO_ON_FALSE(w5500_config && mac_config, NULL, err, TAG, "invalid argument");
640     emac = calloc(1, sizeof(emac_w5500_t));
641     ESP_GOTO_ON_FALSE(emac, NULL, err, TAG, "no mem for MAC instance");
642     /* w5500 driver is interrupt driven */
643     ESP_GOTO_ON_FALSE(w5500_config->int_gpio_num >= 0, NULL, err, TAG, "invalid interrupt gpio number");
644     /* bind methods and attributes */
645     emac->sw_reset_timeout_ms = mac_config->sw_reset_timeout_ms;
646     emac->int_gpio_num = w5500_config->int_gpio_num;
647     emac->spi_hdl = w5500_config->spi_hdl;
648     emac->parent.set_mediator = emac_w5500_set_mediator;
649     emac->parent.init = emac_w5500_init;
650     emac->parent.deinit = emac_w5500_deinit;
651     emac->parent.start = emac_w5500_start;
652     emac->parent.stop = emac_w5500_stop;
653     emac->parent.del = emac_w5500_del;
654     emac->parent.write_phy_reg = emac_w5500_write_phy_reg;
655     emac->parent.read_phy_reg = emac_w5500_read_phy_reg;
656     emac->parent.set_addr = emac_w5500_set_addr;
657     emac->parent.get_addr = emac_w5500_get_addr;
658     emac->parent.set_speed = emac_w5500_set_speed;
659     emac->parent.set_duplex = emac_w5500_set_duplex;
660     emac->parent.set_link = emac_w5500_set_link;
661     emac->parent.set_promiscuous = emac_w5500_set_promiscuous;
662     emac->parent.set_peer_pause_ability = emac_w5500_set_peer_pause_ability;
663     emac->parent.enable_flow_ctrl = emac_w5500_enable_flow_ctrl;
664     emac->parent.transmit = emac_w5500_transmit;
665     emac->parent.receive = emac_w5500_receive;
666     /* create mutex */
667     emac->spi_lock = xSemaphoreCreateMutex();
668     ESP_GOTO_ON_FALSE(emac->spi_lock, NULL, err, TAG, "create lock failed");
669     /* create w5500 task */
670     BaseType_t core_num = tskNO_AFFINITY;
671     if (mac_config->flags & ETH_MAC_FLAG_PIN_TO_CORE) {
672         core_num = cpu_hal_get_core_id();
673     }
674     BaseType_t xReturned = xTaskCreatePinnedToCore(emac_w5500_task, "w5500_tsk", mac_config->rx_task_stack_size, emac,
675                            mac_config->rx_task_prio, &emac->rx_task_hdl, core_num);
676     ESP_GOTO_ON_FALSE(xReturned == pdPASS, NULL, err, TAG, "create w5500 task failed");
677     return &(emac->parent);
678 
679 err:
680     if (emac) {
681         if (emac->rx_task_hdl) {
682             vTaskDelete(emac->rx_task_hdl);
683         }
684         if (emac->spi_lock) {
685             vSemaphoreDelete(emac->spi_lock);
686         }
687         free(emac);
688     }
689     return ret;
690 }
691