diff options
author | David Crocker <dcrocker@eschertech.com> | 2020-01-04 17:32:40 +0300 |
---|---|---|
committer | David Crocker <dcrocker@eschertech.com> | 2020-01-04 17:32:40 +0300 |
commit | 8ded9143fa9d07dcddd525683403980c42881f1a (patch) | |
tree | e3ee49f1ee85bdf7cd9d4ba01533634603af1e7e /src/Networking/LwipEthernet | |
parent | 06a753856ed5e59aab098384920423d759743ced (diff) |
Networking and conditional GCode changes
Removed LWIP_GMAC_TASK definition. We now always use a separate task for
the GMAC.
Added loop counter constant 'iterations'
Fixed some expressoin evaluation bugs
Diffstat (limited to 'src/Networking/LwipEthernet')
-rw-r--r-- | src/Networking/LwipEthernet/GMAC/ethernet_sam.cpp | 19 | ||||
-rw-r--r-- | src/Networking/LwipEthernet/GMAC/ethernet_sam.h | 5 | ||||
-rw-r--r-- | src/Networking/LwipEthernet/GMAC/same70_gmac.cpp | 50 | ||||
-rw-r--r-- | src/Networking/LwipEthernet/GMAC/same70_gmac.h | 5 | ||||
-rw-r--r-- | src/Networking/LwipEthernet/LwipEthernetInterface.cpp | 247 | ||||
-rw-r--r-- | src/Networking/LwipEthernet/LwipEthernetInterface.h | 2 | ||||
-rw-r--r-- | src/Networking/LwipEthernet/LwipSocket.cpp | 46 |
7 files changed, 83 insertions, 291 deletions
diff --git a/src/Networking/LwipEthernet/GMAC/ethernet_sam.cpp b/src/Networking/LwipEthernet/GMAC/ethernet_sam.cpp index d4301e93..da87c94f 100644 --- a/src/Networking/LwipEthernet/GMAC/ethernet_sam.cpp +++ b/src/Networking/LwipEthernet/GMAC/ethernet_sam.cpp @@ -212,29 +212,10 @@ bool ethernet_link_established() noexcept */ void ethernet_task() noexcept { -#if !LWIP_GMAC_TASK - /* Run polling tasks */ - while (ethernetif_input(&gs_net_if)) { } -#endif - /* Run periodic tasks */ ethernet_timers_update(); } -#if !LWIP_GMAC_TASK -/* - * \brief Sets the EMAC RX callback. It will be called when a new packet - * can be processed and should be called with a NULL parameter inside - * the actual callback. - * - * \param callback The callback to be called when a new packet is ready - */ -void ethernet_set_rx_callback(gmac_dev_tx_cb_t callback) noexcept -{ - ethernetif_set_rx_callback(callback); -} -#endif - /* * \brief Returns the current IP address */ diff --git a/src/Networking/LwipEthernet/GMAC/ethernet_sam.h b/src/Networking/LwipEthernet/GMAC/ethernet_sam.h index c1948803..41350fb2 100644 --- a/src/Networking/LwipEthernet/GMAC/ethernet_sam.h +++ b/src/Networking/LwipEthernet/GMAC/ethernet_sam.h @@ -75,11 +75,6 @@ void ethernet_timers_update() noexcept; // Reads all stored network packets and processes them void ethernet_task() noexcept; -#if !LWIP_GMAC_TASK -// Set the RX callback for incoming network packets -void ethernet_set_rx_callback(gmac_dev_tx_cb_t callback) noexcept; -#endif - // Returns the network interface's current IPv4 address void ethernet_get_ipaddress(IPAddress& ipAddress, IPAddress& netMask, IPAddress& gateWay) noexcept; diff --git a/src/Networking/LwipEthernet/GMAC/same70_gmac.cpp b/src/Networking/LwipEthernet/GMAC/same70_gmac.cpp index 2565fe79..79704484 100644 --- a/src/Networking/LwipEthernet/GMAC/same70_gmac.cpp +++ b/src/Networking/LwipEthernet/GMAC/same70_gmac.cpp @@ -52,8 +52,6 @@ extern "C" { extern void delay(uint32_t ms); -#if LWIP_GMAC_TASK - // We can't #include RepRapFirmware.h here because that leads to a duplicate definition of ERR_TIMEOUT #include <RTOSIface/RTOSIface.h> #include <TaskPriorities.h> @@ -63,8 +61,6 @@ extern Mutex lwipMutex; constexpr size_t EthernetTaskStackWords = 250; static Task<EthernetTaskStackWords> ethernetTask; -#endif - // Error counters unsigned int rxErrorCount; unsigned int rxBuffersNotFullyPopulatedCount; @@ -82,7 +78,6 @@ unsigned int txBufferTooShortCount; /** Network link speed. */ #define NET_LINK_SPEED 100000000 -#if LWIP_GMAC_TASK /* Interrupt priorities. (lowest value = highest priority) */ /* ISRs using FreeRTOS *FromISR APIs must have priorities below or equal to */ /* configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY. */ @@ -95,16 +90,6 @@ unsigned int txBufferTooShortCount; /** The GMAC RX errors to handle */ #define GMAC_RX_ERRORS (GMAC_RSR_RXOVR | GMAC_RSR_HNO) -#elif 1 // chrishamm -#define GMAC_INT_GROUP (GMAC_ISR_RCOMP | GMAC_ISR_ROVR) // call ISR only when a new frame has arrived -#define GMAC_TX_ERRORS 0 -#define GMAC_RX_ERRORS 0 -#else -#define INT_PRIORITY_GMAC 0 -#define GMAC_INT_GROUP 0 -#define GMAC_TX_ERRORS 0 -#define GMAC_RX_ERRORS 0 -#endif /** TX descriptor lists */ COMPILER_ALIGNED(8) @@ -169,16 +154,11 @@ uint32_t lwip_tx_rate = 0; uint32_t lwip_rx_rate = 0; #endif -#if !LWIP_GMAC_TASK -static gmac_dev_tx_cb_t gmac_rx_cb = nullptr; -#endif - /** * \brief GMAC interrupt handler. */ extern "C" void GMAC_Handler() noexcept { -#if LWIP_GMAC_TASK /* Get interrupt status. */ const uint32_t ul_isr = gmac_get_interrupt_status(GMAC); @@ -187,22 +167,6 @@ extern "C" void GMAC_Handler() noexcept { ethernetTask.GiveFromISR(); } -#else -# if 1 - volatile uint32_t ul_isr; - - /* Get interrupt status. */ - ul_isr = gmac_get_interrupt_status(GMAC); - - /* RX interrupts. */ - if ((ul_isr & GMAC_INT_GROUP) != 0 && gmac_rx_cb != nullptr) - { - gmac_rx_cb(ul_isr); - } -# else - NVIC_DisableIRQ(GMAC_IRQn); -# endif -#endif } /** @@ -557,7 +521,6 @@ static pbuf *gmac_low_level_input(struct netif *netif) noexcept return p; } -#if LWIP_GMAC_TASK /** * \brief GMAC task function. This function waits for the notification * semaphore from the interrupt, processes the incoming packet and then @@ -582,7 +545,6 @@ extern "C" [[noreturn]] void gmac_task(void *pvParameters) noexcept TaskBase::Take((rxPbufsFullyPopulated) ? 1000 : 20); } } -#endif /** * \brief This function should be called when a packet is ready to be @@ -676,10 +638,7 @@ err_t ethernetif_init(struct netif *netif) noexcept /* Initialize the hardware */ gmac_low_level_init(netif); -#if LWIP_GMAC_TASK ethernetTask.Create(gmac_task, "ETHERNET", &gs_gmac_dev, TaskPriority::EthernetPriority); -#endif - return ERR_OK; } @@ -809,13 +768,6 @@ bool ethernetif_link_established() noexcept return true; } -#if !LWIP_GMAC_TASK -void ethernetif_set_rx_callback(gmac_dev_tx_cb_t callback) noexcept -{ - gmac_rx_cb = callback; -} -#endif - void ethernetif_set_mac_address(const uint8_t macAddress[]) noexcept { // This function must be called once before low_level_init(), because that is where the @@ -830,9 +782,7 @@ void ethernetif_set_mac_address(const uint8_t macAddress[]) noexcept void ethernetif_terminate() noexcept { NVIC_DisableIRQ(GMAC_IRQn); -#if LWIP_GMAC_TASK ethernetTask.TerminateAndUnlink(); -#endif } extern "C" u32_t millis() noexcept; diff --git a/src/Networking/LwipEthernet/GMAC/same70_gmac.h b/src/Networking/LwipEthernet/GMAC/same70_gmac.h index 8d6bd595..dfdaca6d 100644 --- a/src/Networking/LwipEthernet/GMAC/same70_gmac.h +++ b/src/Networking/LwipEthernet/GMAC/same70_gmac.h @@ -69,11 +69,6 @@ bool ethernetif_establish_link() noexcept; // attempts to establish link and bool ethernetif_link_established() noexcept; // asks the PHY if the link is still up -#if !LWIP_GMAC_TASK -typedef void (*gmac_dev_tx_cb_t) (uint32_t ul_status) noexcept; // copied from gmac_raw.h -void ethernetif_set_rx_callback(gmac_dev_tx_cb_t callback) noexcept; -#endif - void ethernetif_set_mac_address(const uint8_t macAddress[]) noexcept; #endif diff --git a/src/Networking/LwipEthernet/LwipEthernetInterface.cpp b/src/Networking/LwipEthernet/LwipEthernetInterface.cpp index 88c42a90..0a46d850 100644 --- a/src/Networking/LwipEthernet/LwipEthernetInterface.cpp +++ b/src/Networking/LwipEthernet/LwipEthernetInterface.cpp @@ -46,71 +46,17 @@ const unsigned int MdnsTtl = 10 * 60; // same value as on the Duet 0.6/0.8.5 static LwipEthernetInterface *ethernetInterface; -#if LWIP_GMAC_TASK - # include <RTOSIface/RTOSIface.h> Mutex lwipMutex; -#endif - extern "C" { -#if !LWIP_GMAC_TASK - - static volatile bool lwipLocked = false; - static volatile bool resetCallback = false; - - // Lock functions for LwIP (LwIP isn't thread-safe when working with the raw API) - bool LockLWIP() - { - if (lwipLocked) - { - return false; - } - - lwipLocked = true; - return true; - } - - void UnlockLWIP() - { - lwipLocked = false; - } - - // Callback functions for the GMAC driver and for LwIP - - // Called from ISR - static void ethernet_rx_callback(uint32_t ul_status) - { - // Because the LWIP stack can become corrupted if we work with it in parallel, - // we may have to wait for the next Spin() call to read the next packet. - if (LockLWIP()) - { - ethernet_task(); - UnlockLWIP(); - } - else - { - ethernet_set_rx_callback(nullptr); - resetCallback = true; - } - } - - #endif // Task function to keep the GMAC and LwIP running void DoEthernetTask() { ethernet_task(); - -#if !LWIP_GMAC_TASK - if (resetCallback) - { - resetCallback = false; - ethernet_set_rx_callback(ðernet_rx_callback); - } -#endif } // Callback functions for LWIP (may be called from ISR) @@ -180,9 +126,7 @@ void LwipEthernetInterface::Init() noexcept interfaceMutex.Create("LwipIface"); //TODO we don't yet use this mutex anywhere! -#if LWIP_GMAC_TASK lwipMutex.Create("LwipCore"); -#endif // Clear the PCBs for (size_t i = 0; i < NumTcpPorts; ++i) @@ -405,9 +349,6 @@ void LwipEthernetInterface::Start() noexcept initialised = true; } -#if !LWIP_GMAC_TASK - resetCallback = true; // reset EMAC RX callback on next Spin call -#endif state = NetworkState::establishingLink; } @@ -417,10 +358,6 @@ void LwipEthernetInterface::Stop() noexcept if (state != NetworkState::disabled) { netif_set_down(&gs_net_if); -#if !LWIP_GMAC_TASK - resetCallback = false; - ethernet_set_rx_callback(nullptr); -#endif #if defined(DUET3) pinMode(PhyResetPin, OUTPUT_LOW); // hold the Ethernet Phy chip in reset @@ -432,125 +369,106 @@ void LwipEthernetInterface::Stop() noexcept // Main spin loop. If 'full' is true then we are being called from the main spin loop. If false then we are being called during HSMCI idle time. void LwipEthernetInterface::Spin() noexcept { -#if LWIP_GMAC_TASK MutexLocker lock(lwipMutex); -#else - if (LockLWIP()) // basically we can't do anything if we can't interact with LWIP + + switch(state) { -#endif - switch(state) + case NetworkState::enabled: + case NetworkState::disabled: + default: + // Nothing to do + break; + + case NetworkState::establishingLink: + if (ethernet_establish_link()) { - case NetworkState::enabled: - case NetworkState::disabled: - default: - // Nothing to do - break; - - case NetworkState::establishingLink: - if (ethernet_establish_link()) + usingDhcp = platform.GetIPAddress().IsNull(); + if (usingDhcp) { - usingDhcp = platform.GetIPAddress().IsNull(); - if (usingDhcp) - { - // IP address is all zeros, so use DHCP - state = NetworkState::obtainingIP; + // IP address is all zeros, so use DHCP + state = NetworkState::obtainingIP; // debugPrintf("Link established, getting IP address\n"); - IPAddress nullAddress; - ethernet_set_configuration(nullAddress, nullAddress, nullAddress); - dhcp_start(&gs_net_if); - } - else - { - // Using static IP address - state = NetworkState::connected; -// debugPrintf("Link established, network running\n"); - ethernet_set_configuration(platform.GetIPAddress(), platform.NetMask(), platform.GateWay()); - } - } - break; - - case NetworkState::obtainingIP: - if (ethernet_link_established()) - { - // Check for incoming packets - DoEthernetTask(); - - // Have we obtained an IP address yet? - ethernet_get_ipaddress(ipAddress, netmask, gateway); - if (!ipAddress.IsNull()) - { - // Notify the mDNS responder about this - state = NetworkState::connected; -// debugPrintf("IP address obtained, network running\n"); - } + IPAddress nullAddress; + ethernet_set_configuration(nullAddress, nullAddress, nullAddress); + dhcp_start(&gs_net_if); } else { -// debugPrintf("Lost phy link\n"); - TerminateSockets(); - state = NetworkState::establishingLink; + // Using static IP address + state = NetworkState::connected; +// debugPrintf("Link established, network running\n"); + ethernet_set_configuration(platform.GetIPAddress(), platform.NetMask(), platform.GateWay()); } - break; + } + break; - case NetworkState::connected: - if (usingDhcp) + case NetworkState::obtainingIP: + if (ethernet_link_established()) + { + // Check for incoming packets + DoEthernetTask(); + + // Have we obtained an IP address yet? + ethernet_get_ipaddress(ipAddress, netmask, gateway); + if (!ipAddress.IsNull()) { - dhcp_stop(&gs_net_if); + // Notify the mDNS responder about this + state = NetworkState::connected; +// debugPrintf("IP address obtained, network running\n"); } + } + else + { +// debugPrintf("Lost phy link\n"); + TerminateSockets(); + state = NetworkState::establishingLink; + } + break; - InitSockets(); - RebuildMdnsServices(); - ethernet_get_ipaddress(ipAddress, netmask, gateway); - platform.MessageF(NetworkInfoMessage, "Ethernet running, IP address = %s\n", IP4String(ipAddress).c_str()); - state = NetworkState::active; - break; + case NetworkState::connected: + if (usingDhcp) + { + dhcp_stop(&gs_net_if); + } - case NetworkState::active: - // Check that the link is still up - if (ethernet_link_established()) - { - // Check for incoming packets - DoEthernetTask(); + InitSockets(); + RebuildMdnsServices(); + ethernet_get_ipaddress(ipAddress, netmask, gateway); + platform.MessageF(NetworkInfoMessage, "Ethernet running, IP address = %s\n", IP4String(ipAddress).c_str()); + state = NetworkState::active; + break; - // Poll the next TCP socket - sockets[nextSocketToPoll]->Poll(); + case NetworkState::active: + // Check that the link is still up + if (ethernet_link_established()) + { + // Check for incoming packets + DoEthernetTask(); - // Move on to the next TCP socket for next time - ++nextSocketToPoll; - if (nextSocketToPoll == NumEthernetSockets) - { - nextSocketToPoll = 0; - } + // Poll the next TCP socket + sockets[nextSocketToPoll]->Poll(); - // Check if the data port needs to be closed - if (closeDataPort && !sockets[FtpDataSocketNumber]->IsClosing()) - { - TerminateDataPort(); - } + // Move on to the next TCP socket for next time + ++nextSocketToPoll; + if (nextSocketToPoll == NumEthernetSockets) + { + nextSocketToPoll = 0; } - else + + // Check if the data port needs to be closed + if (closeDataPort && !sockets[FtpDataSocketNumber]->IsClosing()) { -// debugPrintf("Lost phy link\n"); - TerminateSockets(); - state = NetworkState::establishingLink; + TerminateDataPort(); } - break; } -#if !LWIP_GMAC_TASK - UnlockLWIP(); - } -#endif -} - -void LwipEthernetInterface::Interrupt() noexcept -{ -#if !LWIP_GMAC_TASK - if (initialised && LockLWIP()) - { - ethernet_timers_update(); - UnlockLWIP(); + else + { +// debugPrintf("Lost phy link\n"); + TerminateSockets(); + state = NetworkState::establishingLink; + } + break; } -#endif } void LwipEthernetInterface::Diagnostics(MessageType mtype) noexcept @@ -603,15 +521,6 @@ int LwipEthernetInterface::EnableState() const noexcept return (state == NetworkState::disabled) ? 0 : 1; } -bool LwipEthernetInterface::InNetworkStack() const noexcept -{ -#if LWIP_GMAC_TASK - return false; -#else - return lwipLocked; -#endif -} - bool LwipEthernetInterface::ConnectionEstablished(tcp_pcb *pcb) noexcept { for (LwipSocket *s : sockets) diff --git a/src/Networking/LwipEthernet/LwipEthernetInterface.h b/src/Networking/LwipEthernet/LwipEthernetInterface.h index 3a4a6f42..d4269cd3 100644 --- a/src/Networking/LwipEthernet/LwipEthernetInterface.h +++ b/src/Networking/LwipEthernet/LwipEthernetInterface.h @@ -34,7 +34,6 @@ public: void Activate() noexcept override; void Exit() noexcept override; void Spin() noexcept override; - void Interrupt() noexcept override; void Diagnostics(MessageType mtype) noexcept override; GCodeResult EnableInterface(int mode, const StringRef& ssid, const StringRef& reply) noexcept override; // enable or disable the network @@ -44,7 +43,6 @@ public: GCodeResult GetNetworkState(const StringRef& reply) noexcept override; int EnableState() const noexcept override; - bool InNetworkStack() const noexcept override; bool IsWiFiInterface() const noexcept override { return false; } void UpdateHostname(const char *hostname) noexcept override; diff --git a/src/Networking/LwipEthernet/LwipSocket.cpp b/src/Networking/LwipEthernet/LwipSocket.cpp index 52c5288c..a057bb69 100644 --- a/src/Networking/LwipEthernet/LwipSocket.cpp +++ b/src/Networking/LwipEthernet/LwipSocket.cpp @@ -12,9 +12,7 @@ #include "Networking/NetworkBuffer.h" #include "RepRap.h" -#if LWIP_GMAC_TASK extern Mutex lwipMutex; -#endif //*************************************************************************************************** @@ -66,12 +64,7 @@ static err_t conn_sent(void *arg, tcp_pcb *pcb, u16_t len) return ERR_OK; } -#if !LWIP_GMAC_TASK -extern bool LockLWIP(); -extern void UnlockLWIP(); -#endif - -} +} // end extern "C" //*************************************************************************************************** @@ -208,9 +201,7 @@ void LwipSocket::Close() noexcept { if (state != SocketState::disabled && state != SocketState::listening) { -#if LWIP_GMAC_TASK MutexLocker lock(lwipMutex); -#endif DiscardReceivedData(); state = SocketState::closing; whenClosed = millis(); @@ -227,9 +218,7 @@ void LwipSocket::Terminate() noexcept { if (state != SocketState::disabled) { -#if LWIP_GMAC_TASK MutexLocker lock(lwipMutex); -#endif if (connectionPcb != nullptr) { tcp_err(connectionPcb, nullptr); @@ -267,10 +256,9 @@ bool LwipSocket::ReadChar(char& c) noexcept if (readIndex >= receivedData->len) { -#if LWIP_GMAC_TASK - MutexLocker lock(lwipMutex); -#endif // We've processed one more pbuf + MutexLocker lock(lwipMutex); + if (connectionPcb != nullptr) { tcp_recved(connectionPcb, receivedData->len); @@ -313,10 +301,9 @@ void LwipSocket::Taken(size_t len) noexcept readIndex += len; if (readIndex >= receivedData->len) { -#if LWIP_GMAC_TASK - MutexLocker lock(lwipMutex); -#endif // Notify LwIP + MutexLocker lock(lwipMutex); + if (connectionPcb != nullptr) { tcp_recved(connectionPcb, receivedData->len); @@ -412,19 +399,11 @@ void LwipSocket::DiscardReceivedData() noexcept // Send the data, returning the length buffered size_t LwipSocket::Send(const uint8_t *data, size_t length) noexcept { -#if LWIP_GMAC_TASK MutexLocker lock(lwipMutex); -#else - // This is always called outside the EthernetInterface::Spin method. Wait for pending ISRs to finish - while (!LockLWIP()) { } -#endif if (!CanSend()) { // Don't bother if we cannot send anything at all+ -#if !LWIP_GMAC_TASK - UnlockLWIP(); -#endif return 0; } @@ -446,9 +425,6 @@ size_t LwipSocket::Send(const uint8_t *data, size_t length) noexcept if (ERR_IS_FATAL(err)) { Terminate(); -#if !LWIP_GMAC_TASK - UnlockLWIP(); -#endif return 0; } else if (err == ERR_MEM) @@ -457,9 +433,6 @@ size_t LwipSocket::Send(const uint8_t *data, size_t length) noexcept { // The buffers are full - try again later tcp_output(connectionPcb); -#if !LWIP_GMAC_TASK - UnlockLWIP(); -#endif return 0; } bytesToSend /= 2; @@ -471,9 +444,6 @@ size_t LwipSocket::Send(const uint8_t *data, size_t length) noexcept if (ERR_IS_FATAL(tcp_output(connectionPcb))) { Terminate(); -#if !LWIP_GMAC_TASK - UnlockLWIP(); -#endif return 0; } @@ -481,15 +451,9 @@ size_t LwipSocket::Send(const uint8_t *data, size_t length) noexcept whenWritten = millis(); unAcked += bytesToSend; -#if !LWIP_GMAC_TASK - UnlockLWIP(); -#endif return bytesToSend; } -#if !LWIP_GMAC_TASK - UnlockLWIP(); -#endif return 0; } |