diff --git a/Configuration.h b/Configuration.h index 6b2774c..e956748 100644 --- a/Configuration.h +++ b/Configuration.h @@ -24,8 +24,8 @@ Licence: GPL #define CONFIGURATION_H #define NAME "RepRapFirmware" -#define VERSION "0.58alpha1-dc42" -#define DATE "2014-04-18" +#define VERSION "0.58-dc42" +#define DATE "2014-04-19" #define LAST_AUTHOR "dc42" // Other firmware that we might switch to be compatible with. diff --git a/GCodes.cpp b/GCodes.cpp index c91e855..ac48b7c 100644 --- a/GCodes.cpp +++ b/GCodes.cpp @@ -1315,68 +1315,119 @@ void GCodes::HandleReply(bool error, bool fromLine, const char* reply, char gMOr } } -void GCodes::SetHeaterParameters(GCodeBuffer *gb, size_t heater, char reply[STRING_LENGTH]) +// Set PID parameters (M301 or M303 command). 'heater' is the defeault heater number to use. +void GCodes::SetPidParameters(GCodeBuffer *gb, int heater, char reply[STRING_LENGTH]) { - PidParameters pp = platform->GetPidParameters(heater); - bool seen = false; - if (gb->Seen('P')) - { - pp.kP = gb->GetFValue(); - seen = true; - } - if (gb->Seen('I')) - { - pp.kI = gb->GetFValue(); - seen = true; - } - if (gb->Seen('D')) - { - pp.kD = gb->GetFValue(); - seen = true; - } - - // We also allow the 25C resistance and beta to be set. We must set these together. - float r25, beta; - if (gb->Seen('R')) - { - r25 = gb->GetFValue(); - seen = true; - } - else - { - r25 = pp.GetThermistorR25(); - } - - if (gb->Seen('B')) - { - beta = gb->GetFValue(); - seen = true; - } - else - { - beta = pp.GetBeta(); - } - - if (gb->Seen('L')) - { - pp.adcLowOffset = gb->GetFValue(); - seen = true; - } if (gb->Seen('H')) { - pp.adcHighOffset = gb->GetFValue(); - seen = true; + heater = gb->GetIValue(); } - if (seen) + if (heater >= 0 && heater < HEATERS) { - pp.SetThermistorR25AndBeta(r25, beta); // recalculate Rinf - platform->SetPidParameters(heater, pp); + PidParameters pp = platform->GetPidParameters(heater); + bool seen = false; + if (gb->Seen('P')) + { + pp.kP = gb->GetFValue(); + seen = true; + } + if (gb->Seen('I')) + { + pp.kI = gb->GetFValue(); + seen = true; + } + if (gb->Seen('D')) + { + pp.kD = gb->GetFValue(); + seen = true; + } + if (gb->Seen('W')) + { + pp.pidMax = gb->GetFValue(); + seen = true; + } + if (gb->Seen('B')) + { + pp.fullBand = gb->GetFValue(); + seen = true; + } + + if (seen) + { + platform->SetPidParameters(heater, pp); + } + else + { + snprintf(reply, STRING_LENGTH, "P:%.2f I:%.3f D:%.2f W:%.1f B:%.1f\n", pp.kP, pp.kI, pp.kD, pp.pidMax, pp.fullBand); + } } - else +} + +void GCodes::SetHeaterParameters(GCodeBuffer *gb, char reply[STRING_LENGTH]) +{ + if (gb->Seen('P')) { - snprintf(reply, STRING_LENGTH, "P:%.2f I:%.3f D: %.2f R: %.1f B: %.1f L: %.1f H: %.1f\n", - pp.kP, pp.kI, pp.kD, r25, beta, pp.adcLowOffset, pp.adcHighOffset); + int heater = gb->GetIValue(); + if (heater >= 0 && heater < HEATERS) + { + PidParameters pp = platform->GetPidParameters(heater); + bool seen = false; + + // We must set the 25C resistance and beta together in order to calculate Rinf. Check for these first. + float r25, beta; + if (gb->Seen('T')) + { + r25 = gb->GetFValue(); + seen = true; + } + else + { + r25 = pp.GetThermistorR25(); + } + + if (gb->Seen('B')) + { + beta = gb->GetFValue(); + seen = true; + } + else + { + beta = pp.GetBeta(); + } + + if (seen) // if see R25 or Beta or both + { + pp.SetThermistorR25AndBeta(r25, beta); // recalculate Rinf + } + + // Now do the other parameters + if (gb->Seen('R')) + { + pp.thermistorSeriesR = gb->GetFValue(); + seen = true; + } + if (gb->Seen('L')) + { + pp.adcLowOffset = gb->GetFValue(); + seen = true; + } + if (gb->Seen('H')) + { + pp.adcHighOffset = gb->GetFValue(); + seen = true; + } + + if (seen) + { + platform->SetPidParameters(heater, pp); + } + else + { + snprintf(reply, STRING_LENGTH, "T:%.1f B:%.1f R:%.1f L:%.1f H:%.1f\n", + r25, beta, pp.thermistorSeriesR, pp.adcLowOffset, pp.adcHighOffset); + } + } } } @@ -1801,14 +1852,18 @@ bool GCodes::ActOnGcode(GCodeBuffer *gb) break; case 301: // Set hot end PID values - SetHeaterParameters(gb, 1, reply); + SetPidParameters(gb, 1, reply); break; case 302: // Allow cold extrudes break; case 304: // Set heated bed parameters - SetHeaterParameters(gb, 0, reply); + SetPidParameters(gb, 0, reply); + break; + + case 305: + SetHeaterParameters(gb, reply); break; case 503: // list variable settings diff --git a/GCodes.h b/GCodes.h index 01c1f3a..467ee27 100644 --- a/GCodes.h +++ b/GCodes.h @@ -116,7 +116,8 @@ class GCodes bool SendConfigToLine(); void WriteHTMLToFile(char b, GCodeBuffer *gb); bool OffsetAxes(GCodeBuffer *gb); - void SetHeaterParameters(GCodeBuffer *gb, size_t heater, char reply[STRING_LENGTH]); + void SetPidParameters(GCodeBuffer *gb, int heater, char reply[STRING_LENGTH]); + void SetHeaterParameters(GCodeBuffer *gb, char reply[STRING_LENGTH]); int8_t Heater(int8_t head) const; Platform* platform; diff --git a/Heat.cpp b/Heat.cpp index 2a9296c..364cc1b 100644 --- a/Heat.cpp +++ b/Heat.cpp @@ -104,7 +104,6 @@ void PID::Init() standbyTemperature = ABS_ZERO; lastTemperature = temperature; temp_iState = 0.0; - temp_dState = 0.0; badTemperatureCount = 0; temperatureFault = false; active = false; @@ -163,11 +162,10 @@ void PID::Spin() temp_iState += error * pp.kI; - if (temp_iState < pp.iMin) temp_iState = pp.iMin; - else if (temp_iState > pp.iMax) temp_iState = pp.iMax; + if (temp_iState < pp.pidMin) temp_iState = pp.pidMin; + else if (temp_iState > pp.pidMax) temp_iState = pp.pidMax; - temp_dState = pp.kD * (temperature - lastTemperature); - + float temp_dState = pp.kD * (temperature - lastTemperature); float result = pp.kP * error + temp_iState - temp_dState; lastTemperature = temperature; @@ -177,11 +175,9 @@ void PID::Spin() result = result/255.0; if(!temperatureFault) + { platform->SetHeater(heater, result); + } -#if 0 // debug - char buffer[100]; - snprintf(buffer, ARRAY_SIZE(buffer), "Heat: e=%f, P=%f, I=%f, d=%f, r=%f\n", error, platform->PidKp(heater)*error, temp_iState, temp_dState, result); - platform->Message(HOST_MESSAGE, buffer); -#endif + //debugPrintf("Heat: e=%f, P=%f, I=%f, d=%f, r=%f\n", error, platform->PidKp(heater)*error, temp_iState, temp_dState, result); } diff --git a/Heat.h b/Heat.h index 5b1127e..ea01add 100644 --- a/Heat.h +++ b/Heat.h @@ -46,7 +46,6 @@ class PID float temperature; float lastTemperature; float temp_iState; - float temp_dState; bool active; int8_t heater; int8_t badTemperatureCount; diff --git a/Libraries/EMAC/ethernet_phy.c b/Libraries/EMAC/ethernet_phy.c index c315782..9d6207c 100644 --- a/Libraries/EMAC/ethernet_phy.c +++ b/Libraries/EMAC/ethernet_phy.c @@ -288,11 +288,12 @@ uint8_t ethernet_phy_set_link(Emac *p_emac, uint8_t uc_phy_addr, * \param p_emac Pointer to the EMAC instance. * \param uc_phy_addr PHY address. * - * Return EMAC_OK if successfully, EMAC_TIMEOUT if timeout. + * Return EMAC_OK if successfully, EMAC_TIMEOUT if timeout (in which case we can keep calling this). */ uint8_t ethernet_phy_auto_negotiate(Emac *p_emac, uint8_t uc_phy_addr) { - uint32_t ul_retry_max = ETH_PHY_RETRY_MAX; + static bool negotiationInProgress = false; + uint32_t ul_value; uint32_t ul_phy_anar; uint32_t ul_phy_analpar; @@ -301,63 +302,69 @@ uint8_t ethernet_phy_auto_negotiate(Emac *p_emac, uint8_t uc_phy_addr) uint8_t uc_speed = 0; uint8_t uc_rc = EMAC_TIMEOUT; - emac_enable_management(p_emac, true); - - - /* Set up control register */ - uc_rc = emac_phy_read(p_emac, uc_phy_addr, MII_BMCR, &ul_value); - if (uc_rc != EMAC_OK) { - emac_enable_management(p_emac, false); - return uc_rc; - } + if (!negotiationInProgress) + { + emac_enable_management(p_emac, true); - ul_value &= ~MII_AUTONEG; /* Remove auto-negotiation enable */ - ul_value &= ~(MII_LOOPBACK | MII_POWER_DOWN); - ul_value |= MII_ISOLATE; /* Electrically isolate PHY */ - uc_rc = emac_phy_write(p_emac, uc_phy_addr, MII_BMCR, ul_value); - if (uc_rc != EMAC_OK) { - emac_enable_management(p_emac, false); - return uc_rc; + /* Set up control register */ + uc_rc = emac_phy_read(p_emac, uc_phy_addr, MII_BMCR, &ul_value); + if (uc_rc != EMAC_OK) { + emac_enable_management(p_emac, false); + return uc_rc; + } + + ul_value &= ~MII_AUTONEG; /* Remove auto-negotiation enable */ + ul_value &= ~(MII_LOOPBACK | MII_POWER_DOWN); + ul_value |= MII_ISOLATE; /* Electrically isolate PHY */ + uc_rc = emac_phy_write(p_emac, uc_phy_addr, MII_BMCR, ul_value); + if (uc_rc != EMAC_OK) { + emac_enable_management(p_emac, false); + return uc_rc; + } + + /* + * Set the Auto_negotiation Advertisement Register. + * MII advertising for Next page. + * 100BaseTxFD and HD, 10BaseTFD and HD, IEEE 802.3. + */ + ul_phy_anar = MII_TX_FDX | MII_TX_HDX | MII_10_FDX | MII_10_HDX | + MII_AN_IEEE_802_3; + uc_rc = emac_phy_write(p_emac, uc_phy_addr, MII_ANAR, ul_phy_anar); + if (uc_rc != EMAC_OK) { + emac_enable_management(p_emac, false); + return uc_rc; + } + + /* Read & modify control register */ + uc_rc = emac_phy_read(p_emac, uc_phy_addr, MII_BMCR, &ul_value); + if (uc_rc != EMAC_OK) { + emac_enable_management(p_emac, false); + return uc_rc; + } + ul_value |= MII_SPEED_SELECT | MII_AUTONEG | MII_DUPLEX_MODE; + uc_rc = emac_phy_write(p_emac, uc_phy_addr, MII_BMCR, ul_value); + if (uc_rc != EMAC_OK) { + emac_enable_management(p_emac, false); + return uc_rc; + } + /* Restart auto negotiation */ + ul_value |= MII_RESTART_AUTONEG; + ul_value &= ~MII_ISOLATE; + uc_rc = emac_phy_write(p_emac, uc_phy_addr, MII_BMCR, ul_value); + if (uc_rc != EMAC_OK) { + emac_enable_management(p_emac, false); + return uc_rc; + } } - /* - * Set the Auto_negotiation Advertisement Register. - * MII advertising for Next page. - * 100BaseTxFD and HD, 10BaseTFD and HD, IEEE 802.3. - */ - ul_phy_anar = MII_TX_FDX | MII_TX_HDX | MII_10_FDX | MII_10_HDX | - MII_AN_IEEE_802_3; - uc_rc = emac_phy_write(p_emac, uc_phy_addr, MII_ANAR, ul_phy_anar); - if (uc_rc != EMAC_OK) { - emac_enable_management(p_emac, false); - return uc_rc; - } + negotiationInProgress = true; // auto negotiation is in progress - /* Read & modify control register */ - uc_rc = emac_phy_read(p_emac, uc_phy_addr, MII_BMCR, &ul_value); - if (uc_rc != EMAC_OK) { - emac_enable_management(p_emac, false); - return uc_rc; - } - ul_value |= MII_SPEED_SELECT | MII_AUTONEG | MII_DUPLEX_MODE; - uc_rc = emac_phy_write(p_emac, uc_phy_addr, MII_BMCR, ul_value); - if (uc_rc != EMAC_OK) { - emac_enable_management(p_emac, false); - return uc_rc; - } - /* Restart auto negotiation */ - ul_value |= MII_RESTART_AUTONEG; - ul_value &= ~MII_ISOLATE; - uc_rc = emac_phy_write(p_emac, uc_phy_addr, MII_BMCR, ul_value); - if (uc_rc != EMAC_OK) { - emac_enable_management(p_emac, false); - return uc_rc; - } /* Check if auto negotiation is completed */ while (1) { uc_rc = emac_phy_read(p_emac, uc_phy_addr, MII_BMSR, &ul_value); if (uc_rc != EMAC_OK) { emac_enable_management(p_emac, false); + negotiationInProgress = false; return uc_rc; } /* Done successfully */ @@ -366,13 +373,13 @@ uint8_t ethernet_phy_auto_negotiate(Emac *p_emac, uint8_t uc_phy_addr) } /* Timeout check */ - if (ul_retry_max) { - if (++ul_retry_count >= ul_retry_max) { - return EMAC_TIMEOUT; - } + if (++ul_retry_count >= 20) { + return EMAC_TIMEOUT; } } + negotiationInProgress = false; + /* Get the auto negotiate link partner base page */ uc_rc = emac_phy_read(p_emac, uc_phy_addr, MII_ANLPAR, &ul_phy_analpar); if (uc_rc != EMAC_OK) { diff --git a/Libraries/Lwip/lwip/src/core/dhcp.c b/Libraries/Lwip/lwip/src/core/dhcp.c index 0bacf2a..d4cf012 100644 --- a/Libraries/Lwip/lwip/src/core/dhcp.c +++ b/Libraries/Lwip/lwip/src/core/dhcp.c @@ -73,12 +73,12 @@ #include "lwip/src/include/lwip/stats.h" #include "lwip/src/include/lwip/mem.h" #include "lwip/src/include/lwip/udp.h" -#include "lwip/src/include/lwip/ip_addr.h" +#include "lwip/src/include/ipv4/lwip/ip_addr.h" #include "lwip/src/include/lwip/netif.h" #include "lwip/src/include/lwip/def.h" #include "lwip/src/include/lwip/sys.h" #include "lwip/src/include/lwip/dhcp.h" -#include "lwip/src/include/lwip/autoip.h" +#include "lwip/src/include/ipv4/lwip/autoip.h" #include "lwip/src/include/lwip/dns.h" #include "lwip/src/include/netif/etharp.h" diff --git a/Libraries/Lwip/lwip/src/sam/include/netif/ethernetif.h b/Libraries/Lwip/lwip/src/sam/include/netif/ethernetif.h index b722a9c..ba0eaab 100644 --- a/Libraries/Lwip/lwip/src/sam/include/netif/ethernetif.h +++ b/Libraries/Lwip/lwip/src/sam/include/netif/ethernetif.h @@ -57,5 +57,6 @@ err_t ethernetif_init(struct netif *netif); void ethernetif_input(void *pv_parameters); void ethernet_hardware_init(void); +bool ethernet_establish_link(void); #endif /* ETHERNETIF_H_INCLUDED */ diff --git a/Libraries/Lwip/lwip/src/sam/netif/ethernetif.c b/Libraries/Lwip/lwip/src/sam/netif/ethernetif.c index a4bf076..309af09 100644 --- a/Libraries/Lwip/lwip/src/sam/netif/ethernetif.c +++ b/Libraries/Lwip/lwip/src/sam/netif/ethernetif.c @@ -250,21 +250,24 @@ void ethernet_hardware_init(void) NVIC_EnableIRQ(EMAC_IRQn); /* Init MAC PHY driver */ - if (ethernet_phy_init(EMAC, BOARD_EMAC_PHY_ADDR, - SystemCoreClock) != EMAC_OK) { + if (ethernet_phy_init(EMAC, BOARD_EMAC_PHY_ADDR, SystemCoreClock) != EMAC_OK) { LWIP_DEBUGF(LWIP_DBG_TRACE, ("PHY Initialize ERROR!\r")); return; } +} +bool ethernet_establish_link(void) +{ /* Auto Negotiate, work in RMII mode */ if (ethernet_phy_auto_negotiate(EMAC, BOARD_EMAC_PHY_ADDR) != EMAC_OK) { LWIP_DEBUGF(LWIP_DBG_TRACE, ("Auto Negotiate ERROR!\r")); - return; + return false; } /* Establish ethernet link */ - while (ethernet_phy_set_link(EMAC, BOARD_EMAC_PHY_ADDR, 1) != EMAC_OK) { + if (ethernet_phy_set_link(EMAC, BOARD_EMAC_PHY_ADDR, 1) != EMAC_OK) { LWIP_DEBUGF(LWIP_DBG_TRACE,("Set link ERROR!\r")); + return false; } /**@todo debug*/ @@ -277,6 +280,7 @@ void ethernet_hardware_init(void) netifINTERFACE_TASK_STACK_SIZE, netifINTERFACE_TASK_PRIORITY ); #endif + return true; } /** diff --git a/Network.cpp b/Network.cpp index 53debb8..2655c29 100644 --- a/Network.cpp +++ b/Network.cpp @@ -56,8 +56,9 @@ const uint8_t windowedSendPackets = 2; const int httpStateSize = MEMP_NUM_TCP_PCB + 1; // the +1 is in case of recovering from network errors // Called to put out a message via the RepRap firmware. +// Can be called from C as well as C++ -void RepRapNetworkMessage(const char* s) +extern "C" void RepRapNetworkMessage(const char* s) { reprap.GetPlatform()->Message(HOST_MESSAGE, s); } @@ -237,15 +238,16 @@ static err_t http_accept(void *arg, struct tcp_pcb *pcb, err_t err) return ERR_OK; } +} // end extern "C" + /*-----------------------------------------------------------------------------------*/ -// This function (is)x should be called only once at the start. +// This function (is) should be called only once at the start. -void httpd_init(void) +void httpd_init() { static int initCount = 0; - initCount++; if (initCount > 1) { @@ -258,8 +260,6 @@ void httpd_init(void) tcp_accept(pcb, http_accept); } -} // end extern "C" - /*-----------------------------------------------------------------------------------*/ static void close_conn(struct tcp_pcb *pcb, HttpState *hs) @@ -346,9 +346,7 @@ void Network::AppendTransaction(RequestState** list, RequestState *r) void Network::Init() { - init_ethernet(reprap.GetPlatform()->IPAddress(), reprap.GetPlatform()->NetMask(), reprap.GetPlatform()->GateWay()); - start_ethernet(); - active = true; + init_ethernet(); } void Network::Spin() @@ -386,10 +384,12 @@ void Network::Spin() } } } -// else -// { -// active = establish_ethernet_link(); -// } + else if (establish_ethernet_link()) + { + start_ethernet(reprap.GetPlatform()->IPAddress(), reprap.GetPlatform()->NetMask(), reprap.GetPlatform()->GateWay()); + httpd_init(); + active = true; + } } bool Network::HaveData() const diff --git a/Platform.cpp b/Platform.cpp index 50fee7e..db18692 100644 --- a/Platform.cpp +++ b/Platform.cpp @@ -82,8 +82,8 @@ void PidParameters::SetThermistorR25AndBeta(float r25, float beta) bool PidParameters::operator==(const PidParameters& other) const { - return kI == other.kI && kD == other.kD && kP == other.kP && fullBand == other.fullBand && iMin == other.iMin - && iMax == other.iMax && thermistorBeta == other.thermistorBeta && thermistorInfR == other.thermistorInfR + return kI == other.kI && kD == other.kD && kP == other.kP && fullBand == other.fullBand && pidMin == other.pidMin + && pidMax == other.pidMax && thermistorBeta == other.thermistorBeta && thermistorInfR == other.thermistorInfR && thermistorSeriesR == other.thermistorSeriesR && adcLowOffset == other.adcLowOffset && adcHighOffset == other.adcHighOffset; } @@ -136,8 +136,8 @@ void Platform::Init() pp.kD = defaultPidKds[i]; pp.kP = defaultPidKps[i]; pp.fullBand = defaultFullBand[i]; - pp.iMin = defaultIMin[i]; - pp.iMax = defaultIMax[i]; + pp.pidMin = defaultPidMin[i]; + pp.pidMax = defaultPidMax[i]; pp.adcLowOffset = pp.adcHighOffset = 0.0; } diff --git a/Platform.h b/Platform.h index a4b04fb..a55c093 100644 --- a/Platform.h +++ b/Platform.h @@ -159,8 +159,8 @@ const float defaultPidKps[HEATERS] = {-1, 20.0}; const float defaultFullBand[HEATERS] = {5.0, 150.0}; // errors larger than this cause heater to be on or off and I-term set to zero #endif -const float defaultIMin[HEATERS] = {0.0, 0.0}; // minimum value of I-term -const float defaultIMax[HEATERS] = {255, 180}; // maximum value of I-term, must be high enough to reach 245C for ABS printing +const float defaultPidMin[HEATERS] = {0.0, 0.0}; // minimum value of I-term +const float defaultPidMax[HEATERS] = {255, 180}; // maximum value of I-term, must be high enough to reach 245C for ABS printing #define STANDBY_TEMPERATURES {ABS_ZERO, ABS_ZERO} // We specify one for the bed, though it's not needed #define ACTIVE_TEMPERATURES {ABS_ZERO, ABS_ZERO} @@ -403,7 +403,7 @@ private: public: float kI, kD, kP; - float fullBand, iMin, iMax; + float fullBand, pidMin, pidMax; float thermistorSeriesR; float adcLowOffset, adcHighOffset; diff --git a/Release/RepRapFirmware-057zb-dc42.bin b/Release/RepRapFirmware-058-dc42.bin similarity index 60% rename from Release/RepRapFirmware-057zb-dc42.bin rename to Release/RepRapFirmware-058-dc42.bin index 936f789..30e1982 100644 Binary files a/Release/RepRapFirmware-057zb-dc42.bin and b/Release/RepRapFirmware-058-dc42.bin differ diff --git a/network/ethernet_sam.c b/network/ethernet_sam.c index 36d0c88..c881b09 100644 --- a/network/ethernet_sam.c +++ b/network/ethernet_sam.c @@ -71,6 +71,8 @@ #include "lwip_test.h" +extern void RepRapNetworkMessage(const char*); + /* Global variable containing MAC Config (hw addr, IP, GW, ...) */ struct netif gs_net_if; @@ -124,11 +126,7 @@ static void timers_update(void) timers_info_t *p_tmr_inf; ul_cur_time = sys_get_ms(); - if (ul_cur_time >= ul_last_time) { - ul_time_diff = ul_cur_time - ul_last_time; - } else { - ul_time_diff = 0xFFFFFFFF - ul_last_time + ul_cur_time; - } + ul_time_diff = ul_cur_time - ul_last_time; // we're using unsigned arithmetic, so this handles wrap around if (ul_time_diff) { ul_last_time = ul_cur_time; @@ -148,71 +146,6 @@ static void timers_update(void) } } -/** - * \brief Set ethernet config. - */ -//err_t ethernetif_init_(struct netif *netif){return ERR_OK;}; -//err_t ethernet_input_(struct pbuf *p, struct netif *netif){return ERR_OK;}; - -//static void ethernet_configure_interface(void) -//{ -// struct ip_addr x_ip_addr, x_net_mask, x_gateway; -// extern err_t ethernetif_init(struct netif *netif); -// -//#if defined(DHCP_USED) -// x_ip_addr.addr = 0; -// x_net_mask.addr = 0; -//#else -// /* Default ip addr */ -// IP4_ADDR(&x_ip_addr, ETHERNET_CONF_IPADDR0, ETHERNET_CONF_IPADDR1, ETHERNET_CONF_IPADDR2, ETHERNET_CONF_IPADDR3); -// -// /* Default subnet mask */ -// IP4_ADDR(&x_net_mask, ETHERNET_CONF_NET_MASK0, ETHERNET_CONF_NET_MASK1, ETHERNET_CONF_NET_MASK2, ETHERNET_CONF_NET_MASK3); -// -// /* Default gateway addr */ -// IP4_ADDR(&x_gateway, ETHERNET_CONF_GATEWAY_ADDR0, ETHERNET_CONF_GATEWAY_ADDR1, ETHERNET_CONF_GATEWAY_ADDR2, ETHERNET_CONF_GATEWAY_ADDR3); -//#endif -// -// /* Add data to netif */ -// netif_add(&gs_net_if, &x_ip_addr, &x_net_mask, &x_gateway, NULL, -// ethernetif_init, ethernet_input); -// -// /* Make it the default interface */ -// netif_set_default(&gs_net_if); -// -// /* Setup callback function for netif status change */ -// netif_set_status_callback(&gs_net_if, status_callback); -// -// /* Bring it up */ -//#if defined(DHCP_USED) -// printf("LwIP: DHCP Started"); -// dhcp_start(&gs_net_if); -//#else -//// printf("LwIP: Static IP Address Assigned\r\n"); -// netif_set_up(&gs_net_if); -//#endif -//} -// -///** \brief Create ethernet task, for ethernet management. -// * -// */ -//void init_ethernet(void) -//{ -// /* Initialize lwIP */ -// lwip_init(); -// -// /* Set hw and IP parameters, initialize MAC too */ -// ethernet_configure_interface(); -// -// /* Init timer service */ -// sys_init_timing(); -// -//#if defined(HTTP_RAW_USED) -// /* Bring up the web server */ -// httpd_init(); -//#endif -//} - //************************************************************************************************************ @@ -223,25 +156,18 @@ static void ethernet_configure_interface(unsigned char ipAddress[], unsigned cha struct ip_addr x_ip_addr, x_net_mask, x_gateway; extern err_t ethernetif_init(struct netif *netif); -#if defined(DHCP_USED) - x_ip_addr.addr = 0; - x_net_mask.addr = 0; -#else - /* Default ip addr */ - //IP4_ADDR(&x_ip_addr, ETHERNET_CONF_IPADDR0, ETHERNET_CONF_IPADDR1, ETHERNET_CONF_IPADDR2, ETHERNET_CONF_IPADDR3); + IP4_ADDR(&x_ip_addr, ipAddress[0], ipAddress[1], ipAddress[2], ipAddress[3]); // set IP address - IP4_ADDR(&x_ip_addr, ipAddress[0], ipAddress[1], ipAddress[2], ipAddress[3]); + if (x_ip_addr.addr == 0) + { + x_net_mask.addr = 0; // not sure this is needed, but the demo program does it + } + else + { + IP4_ADDR(&x_net_mask, netMask[0], netMask[1], netMask[2], netMask[3]); // set network mask + } - /* Default subnet mask */ - //IP4_ADDR(&x_net_mask, ETHERNET_CONF_NET_MASK0, ETHERNET_CONF_NET_MASK1, ETHERNET_CONF_NET_MASK2, ETHERNET_CONF_NET_MASK3); - - IP4_ADDR(&x_net_mask, netMask[0], netMask[1], netMask[2], netMask[3]); - - /* Default gateway addr */ - //IP4_ADDR(&x_gateway, ETHERNET_CONF_GATEWAY_ADDR0, ETHERNET_CONF_GATEWAY_ADDR1, ETHERNET_CONF_GATEWAY_ADDR2, ETHERNET_CONF_GATEWAY_ADDR3); - - IP4_ADDR(&x_gateway, gateWay[0], gateWay[1], gateWay[2], gateWay[3]); -#endif + IP4_ADDR(&x_gateway, gateWay[0], gateWay[1], gateWay[2], gateWay[3]); // set gateway /* Add data to netif */ netif_add(&gs_net_if, &x_ip_addr, &x_net_mask, &x_gateway, NULL, ethernetif_init, ethernet_input); @@ -253,38 +179,45 @@ static void ethernet_configure_interface(unsigned char ipAddress[], unsigned cha netif_set_status_callback(&gs_net_if, status_callback); /* Bring it up */ -#if defined(DHCP_USED) - printf("LwIP: DHCP Started"); - dhcp_start(&gs_net_if); -#else -// printf("LwIP: Static IP Address Assigned\r\n"); - netif_set_up(&gs_net_if); -#endif + if (x_ip_addr.addr == 0) + { + RepRapNetworkMessage("Starting DHCP\n"); + dhcp_start(&gs_net_if); + } + else + { + RepRapNetworkMessage("Starting network\n"); + netif_set_up(&gs_net_if); + } +} + +/** \brief Initialize the Ethernet subsystem. + * + */ +void init_ethernet(void) +{ + lwip_init(); + ethernet_hardware_init(); +} + +/** \brief Try to establish a physical link at, returning true if successful. + * + */ +bool establish_ethernet_link(void) +{ + return ethernet_establish_link(); // this is the one that takes a long time } /** \brief Create ethernet task, for ethernet management. * */ -void init_ethernet(const unsigned char ipAddress[], const unsigned char netMask[], const unsigned char gateWay[]) +void start_ethernet(const unsigned char ipAddress[], const unsigned char netMask[], const unsigned char gateWay[]) { - /* Initialize lwIP */ - lwip_init(); - - ethernet_hardware_init(); - /* Set hw and IP parameters, initialize MAC too */ ethernet_configure_interface(ipAddress, netMask, gateWay); -} -void start_ethernet(void) -{ /* Init timer service */ sys_init_timing(); - -#if defined(HTTP_RAW_USED) - /* Bring up the web server */ - httpd_init(); -#endif } @@ -297,16 +230,18 @@ void start_ethernet(void) */ void status_callback(struct netif *netif) { - int8_t c_mess[25]; - if (netif_is_up(netif)) { -// printf("Network up\r\n"); -// strcpy((char*)c_mess, "IP="); -// strcat((char*)c_mess, inet_ntoa(*(struct in_addr *)&(netif->ip_addr))); -// printf((char const*)c_mess); -// printf("-----------------\r\n"); - netif->flags |=NETIF_FLAG_LINK_UP; - } else { -// printf("Network down\r\n"); + char c_mess[20]; // 15 for IP address, 1 for \n, 1 for null, so 3 spare + if (netif_is_up(netif)) + { + RepRapNetworkMessage("Network up, IP="); + ipaddr_ntoa_r(&(netif->ip_addr), c_mess, sizeof(c_mess)); + strncat(c_mess, sizeof(c_mess) - 1, "\n"); + RepRapNetworkMessage(c_mess); + netif->flags |= NETIF_FLAG_LINK_UP; + } + else + { + RepRapNetworkMessage("Network down\n"); } } diff --git a/network/ethernet_sam.h b/network/ethernet_sam.h index 101bf97..c69cebe 100644 --- a/network/ethernet_sam.h +++ b/network/ethernet_sam.h @@ -62,9 +62,9 @@ bool status_link_up();//*****************************AB */ //void init_ethernet(void); -void init_ethernet(const unsigned char ipAddress[], const unsigned char netMask[], const unsigned char gateWay[]); - -void start_ethernet(void); +void init_ethernet(void); +bool establish_ethernet_link(void); +void start_ethernet(const unsigned char ipAddress[], const unsigned char netMask[], const unsigned char gateWay[]); struct netif* GetConfiguration(); diff --git a/network/lwipopts.h b/network/lwipopts.h index a61f995..eb86264 100644 --- a/network/lwipopts.h +++ b/network/lwipopts.h @@ -61,10 +61,10 @@ #define LWIP_NETIF_STATUS_CALLBACK 1 /* These options can be configured by the user in the standalone demo default demo */ -#define HTTP_RAW_USED +//#define HTTP_RAW_USED // #undef HTTP_RAW_USED -//#define DHCP_USED - #undef DHCP_USED +#define DHCP_USED 1 +// #undef DHCP_USED /* These are not available when using "NO_SYS" */