Hello,
I am trying to put together some simple firmware together to allow for communication with my microcontroller over ethernet. Right now I am using a direct connection. Laptop -> thunderbolt hub -> thunderbolt hub ethernet -> W55000 (mikroc eth click).
I am using a PIC18F47Q10 and believe I've pieced enough together from the Mikroc example and the ioLibrary_Driver library to get something working but I am unable to connect to the device via telnet. Also, my router does not recognize the device either. Would someone be able to take a look at my main.c and let me know, from a top-level, if there is something I am missing?
I am trying to put together some simple firmware together to allow for communication with my microcontroller over ethernet. Right now I am using a direct connection. Laptop -> thunderbolt hub -> thunderbolt hub ethernet -> W55000 (mikroc eth click).
I am using a PIC18F47Q10 and believe I've pieced enough together from the Mikroc example and the ioLibrary_Driver library to get something working but I am unable to connect to the device via telnet. Also, my router does not recognize the device either. Would someone be able to take a look at my main.c and let me know, from a top-level, if there is something I am missing?
main.c:
#include "mcc_generated_files/mcc.h"
#define _WIZCHIP_ 5500
#include "../ioLibrary_Driver-master/ioLibrary_Driver-master/Ethernet/wizchip_conf.h"
#include "../ioLibrary_Driver-master/ioLibrary_Driver-master/Ethernet/W5500/w5500.h"
#include "../ioLibrary_Driver-master/ioLibrary_Driver-master/Ethernet/W5500/../socket.h"
// Socket & Port number definition
#define SOCK_ID_TCP 0
#define SOCK_ID_UDP 1
#define PORT_TCP 5000
#define PORT_UDP 10001
#define DATA_BUF_SIZE 1024
uint8_t gDATABUF[DATA_BUF_SIZE];
// Configuring Ethernet stuff..
volatile wiz_NetInfo gWIZNETINFO =
{
{0x00, 0x14, 0xA3, 0x72, 0x17, 0x3f}, // Source Mac Address
{10, 101, 14, 53}, // Source IP Address
{255, 255, 255, 0}, // Subnet Mask
{192, 168, 0, 1}, // Gateway IP Address
{10, 101, 14, 99}, // DNS server IP Address
NETINFO_STATIC
};
volatile wiz_PhyConf phyConf =
{
PHY_CONFBY_HW, // PHY_CONFBY_SW
PHY_MODE_MANUAL, // PHY_MODE_AUTONEGO
PHY_SPEED_10, // PHY_SPEED_100
PHY_DUPLEX_FULL, // PHY_DUPLEX_HALF
};
volatile wiz_NetInfo pnetinfo;
void TCP_Server(void)
{
int32_t ret;
uint16_t size = 0, sentsize = 0;
// Get status of socket
switch(getSn_SR(SOCK_ID_TCP))
{
// Connection established
case SOCK_ESTABLISHED :
{
// Check interrupt: connection with peer is successful
if(getSn_IR(SOCK_ID_TCP) & Sn_IR_CON)
{
// Clear corresponding bit
setSn_IR(SOCK_ID_TCP,Sn_IR_CON);
}
// Get received data size
if((size = getSn_RX_RSR(SOCK_ID_TCP)) > 0)
{
// Cut data to size of data buffer
if(size > DATA_BUF_SIZE)
{
size = DATA_BUF_SIZE;
}
// Get received data
ret = recv(SOCK_ID_TCP, gDATABUF, size);
// Check for error
if(ret <= 0)
{
return;
}
// Send echo to remote
sentsize = 0;
while(size != sentsize)
{
ret = send(SOCK_ID_TCP, gDATABUF + sentsize, size - sentsize);
// Check if remote close socket
if(ret < 0)
{
close(SOCK_ID_TCP);
return;
}
// Update number of sent bytes
sentsize += ret;
}
}
break;
}
// Socket received the disconnect-request (FIN packet) from the connected peer
case SOCK_CLOSE_WAIT :
{
// Disconnect socket
if((ret = disconnect(SOCK_ID_TCP)) != SOCK_OK)
{
return;
}
break;
}
// Socket is opened with TCP mode
case SOCK_INIT :
{
// Listen to connection request
if( (ret = listen(SOCK_ID_TCP)) != SOCK_OK)
{
return;
}
break;
}
// Socket is released
case SOCK_CLOSED:
{
// Open TCP socket
if((ret = socket(SOCK_ID_TCP, Sn_MR_TCP, PORT_TCP, 0x00)) != SOCK_ID_TCP)
{
return;
}
break;
}
default:
{
break;
}
}
}
// brief Handle UDP socket state.
void UDP_Server(void)
{
int32_t ret;
uint16_t size, sentsize;
uint8_t destip[4];
uint16_t destport;
// Get status of socket
switch(getSn_SR(SOCK_ID_UDP))
{
// Socket is opened in UDP mode
case SOCK_UDP:
{
// Get received data size
if((size = getSn_RX_RSR(SOCK_ID_UDP)) > 0)
{
// Cut data to size of data buffer
if(size > DATA_BUF_SIZE)
{
size = DATA_BUF_SIZE;
}
// Get received data
ret = recvfrom(SOCK_ID_UDP, gDATABUF, size, destip, (uint16_t*)&destport);
// Check for error
if(ret <= 0)
{
return;
}
// Send echo to remote
size = (uint16_t) ret;
sentsize = 0;
while(sentsize != size)
{
ret = sendto(SOCK_ID_UDP, gDATABUF + sentsize, size - sentsize, destip, destport);
if(ret < 0)
{
return;
}
// Update number of sent bytes
sentsize += ret;
}
}
break;
}
// Socket is not opened
case SOCK_CLOSED:
{
// Open UDP socket
if((ret=socket(SOCK_ID_UDP, Sn_MR_UDP, PORT_UDP, 0x00)) != SOCK_ID_UDP)
{
return;
}
break;
}
default :
{
break;
}
}
}
/*
Main application
*/
void main(void)
{
// Initialize the device
SYSTEM_Initialize();
SPI2_Open(SPI2_DEFAULT);
ETH_CS_UNSELECTED();
// Setting up callbacks
reg_wizchip_cs_cbfunc(ETH_CS_SELECTED, ETH_CS_UNSELECTED);
reg_wizchip_spi_cbfunc(SPI2_ReadByte,SPI2_ExchangeByte);
reg_wizchip_spiburst_cbfunc(NULL ,NULL);
ETH_RS_SetHigh();
__delay_ms(1);
wizchip_init(NULL, NULL);
ctlnetwork(CN_SET_NETINFO, (void*) &gWIZNETINFO);
ctlwizchip(CW_SET_PHYCONF, (void*) &phyConf);
HEARTBEAT_SetLow();
// If using interrupts in PIC18 High/Low Priority Mode you need to enable the Global High and Low Interrupts
// If using interrupts in PIC Mid-Range Compatibility Mode you need to enable the Global and Peripheral Interrupts
// Use the following macros to:
// Enable the Global Interrupts
//INTERRUPT_GlobalInterruptEnable();
// Disable the Global Interrupts
//INTERRUPT_GlobalInterruptDisable();
// Enable the Peripheral Interrupts
//INTERRUPT_PeripheralInterruptEnable();
// Disable the Peripheral Interrupts
//INTERRUPT_PeripheralInterruptDisable();
wizchip_getnetinfo(&pnetinfo);
while (1)
{
// Add your application code
TCP_Server();
}
}
/**
End of File
*/