Code for Link Up... SAM4E-EK w/no OS, LWIP RAW example

Discussion around product based on ARM Cortex M4 core.

Moderators: nferre, ncollot

scowell
Posts: 29
Joined: Fri Feb 13, 2015 11:00 pm

Code for Link Up... SAM4E-EK w/no OS, LWIP RAW example

Tue Aug 04, 2015 11:20 pm

I needed to know when my Ethernet connection was unplugged... this code has some gotchas, in that if you don't have the cable plugged in at first, there's a timeout, then no connection is available.

First, the power-up connection... easiest is to convert the timeout into an infinite wait, here's some code from sam4e_gmac.c/gmac_low_level_init(), at the end of the routine:

Code: Select all

	/* Init MAC PHY driver. */
	if (ethernet_phy_init(GMAC, BOARD_GMAC_PHY_ADDR, sysclk_get_cpu_hz()) != GMAC_OK) {
		LWIP_DEBUGF(NETIF_DEBUG, ("gmac_low_level_init: PHY init ERROR!\n"));
		return;
	}

	/* Auto Negotiate, work in RMII mode. */
	while (ethernet_phy_auto_negotiate(GMAC, BOARD_GMAC_PHY_ADDR) != GMAC_OK) {
		LWIP_DEBUGF(NETIF_DEBUG, ("gmac_low_level_init: auto negotiate ERROR!\n"));// slc added while so this would stall until connect... was 'if'
		;//return;// semicolon so it wouldn't optimize away... comment out the return to make infinite
	}

	/* Establish ethernet link. */
	while (ethernet_phy_set_link(GMAC, BOARD_GMAC_PHY_ADDR, 1) != GMAC_OK) {
		LWIP_DEBUGF(NETIF_DEBUG, ("gmac_low_level_init: set link ERROR!\n"));
		;//return;// same here... infinite wait
	}
That took care of initialization... and if the plug was disconnected, it would re-connect no problem when re-plugged... but I have a fancy status LED that I wanted to change if this happened... so I put this in the main while(1) loop:

Code: Select all

	while (1) 
	{		
		/* Check for input packet and process it. */
		ethernet_task();	

		sys_check_timeouts();// includes sntp stuff
		
		loopCounter++;
		if (!(loopCounter%0x00080000))
		{			
			gmac_enable_management(GMAC, true);	
			uc_rc = gmac_phy_read(GMAC, BOARD_GMAC_PHY_ADDR, GMII_BMSR, &ul_stat1);// get status from PHY
		
			if ((ul_stat1 & GMII_LINK_STATUS) == 0) // change status LED for no link
			{
				// do something here so LED is different
             LED_Bad();				
			}
			else
				LED_OK();// normal status

			gmac_enable_management(GMAC, false);				
		}
	
	}
Works great... the mod loop hits every five seconds or so.... plenty for what I want. I'd be interested in better solutions, especially for the main loop hack. HTH someone!

Return to “SAM4 Cortex-M4 MCU”

Who is online

Users browsing this forum: No registered users and 2 guests