You can use pd_havelink() to test the cable and close the connection if no cable is detected:
/*******************************************************************************
CABLE_DETECT.C
Puck Curtis
May 18, 2007
This is an easy way to test whether an Ethernet cable is connected.
1. Run the program.
2. Ping the device to validate the connection.
3. Remove the Ethernet cable from the Rabbit.
4. The Ethernet connection will close and then attempt to reopen.
5. Plug the Ethernet cable back in.
6. The connection should come back up and you can ping it.
*******************************************************************************/
/*
* NOTE: Since we are manually configuring the device, we are
* intentionally NOT using the TCP_CONFIG.LIB setup. This means that
* you will need to set your default network configuration in the LOCAL_*
* macros below.
*/
#define TCPCONFIG 0
/*
* USE_ETHERNET must be defined. This definition simply means that we are
* using the first (and probably only) physical Ethernet interface.
*/
#define USE_ETHERNET 0x01
/*
* The following macros define the network setup of the physical Ethernet
* interface. The virtual Ethernet interfaces will be the subsequent IP
* addresses (e.g., "10.10.6.113", "10.10.6.114", "10.10.6.115").
*/
#define LOCAL_IP "192.168.1.197"
#define LOCAL_NETMASK "255.255.255.0"
#define LOCAL_GATEWAY "192.168.1.1"
#memmap xmem
#use "dcrtcp.lib"
void main(void)
{
char Connected_Flag; //Flag variable wsed to store the state of the cable
// Initialize the TCP/IP stack
sock_init();
while (1)
{
// Perform network configuration on the Ethernet ineterface
printf("Bringing up Main Interface \n");
ifconfig(IF_ETH0,
IFS_IPADDR, aton(LOCAL_IP),
IFS_NETMASK, aton(LOCAL_NETMASK),
IFS_ROUTER_SET,aton(LOCAL_GATEWAY),
IFS_UP,
IFS_END);
// Wait for the interface to come up
while (ifpending(IF_ETH0) == IF_COMING_UP) {
tcp_tick(NULL);
}
printf("Interface is up!!\n");
Connected_Flag = 1;
while(Connected_Flag) //Loop while the cable is plugged in
{
costate
{
tcp_tick(NULL); //Drive the TCP stack
}
costate
{
// Test the cable and close the connection if no cable is detected
if (pd_havelink(IF_ETH0) == 0)
{
Connected_Flag = 0;
printf("Cable unplugged!!\n");
}
}
}
// Bring down the main Ethernet interface
printf("Bringing down Main Interface!!\n");
ifconfig(IF_ETH0,
IFS_DOWN,
IFS_END);
// Wait for the interface to come down
while (ifpending(IF_ETH0) != IF_DOWN) {
tcp_tick(NULL);
}
printf("Interface is down!!\n\n");
}
}