#include "EthernetDriver.h" #include "utility/w5500.h" Wiznet5500 driver(10); #define SPI_ETHERNET_SETTINGS SPISettings(14000000, MSBFIRST, SPI_MODE0) static volatile bool busy = false; static uint8_t macAddress[MAC_ADDRESS_DIM] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEF }; static volatile bool linkIsUp = false; static uint8_t buffer[ETH_BUFF_DIM]; static volatile uint16_t length = 0; static EtherCallback_f frame_received = nullptr; static EtherCallback_f link_on = nullptr; static EtherCallback_f link_off = nullptr; void eth_set_mac_address(const uint8_t *mad) { memcpy(macAddress, mad, MAC_ADDRESS_DIM); } int eth_get_mac_address(uint8_t *mad) { memcpy(mad, macAddress, MAC_ADDRESS_DIM); return MAC_ADDRESS_DIM; } bool eth_init() { SPI.begin(); SPI.beginTransaction(SPI_ETHERNET_SETTINGS); bool ret = driver.begin(macAddress); SPI.endTransaction(); return ret; } void eth_execute_link_process() { static unsigned long previousMillis; if (busy) return; busy = true; if (!linkIsUp || millis() - previousMillis > 1000) { previousMillis = millis(); SPI.beginTransaction(SPI_ETHERNET_SETTINGS); bool s = driver.isLinked(); SPI.endTransaction(); if (linkIsUp != s) { linkIsUp = s; if (s) { link_on(); } else { link_off(); } } } if (linkIsUp) { SPI.beginTransaction(SPI_ETHERNET_SETTINGS); length = driver.readFrame(buffer, sizeof(buffer)); SPI.endTransaction(); if (length > 0) { frame_received(); } } busy = false; } uint8_t* eth_input(volatile uint32_t *dim) { *dim = length; return buffer; } bool eth_output(uint8_t *buf, uint16_t dim) { if (busy) return false; busy = true; SPI.beginTransaction(SPI_ETHERNET_SETTINGS); bool ret = driver.sendFrame(buf, dim) == dim; SPI.endTransaction(); busy = false; return ret; } bool eth_output_can_transimit() { return linkIsUp; } void eth_release_rx_buffer() { } uint8_t* eth_get_tx_buffer(uint16_t *size) { *size = sizeof(buffer); return buffer; } void eth_set_rx_frame_cbk(EtherCallback_f fn) { frame_received = fn; } void eth_set_link_on_cbk(EtherCallback_f fn) { link_on = fn; } void eth_set_link_off_cbk(EtherCallback_f fn) { link_off = fn; }