Hi there,
For a project I try to connect a PIC24FJ256GA704 MCU via SPI1 with the w550io module.
I want the module to be setup as a TCP server. The module can be pinged but not connected via the AX1 loopback test program or similar.
I am using the wizIO library and SPI1 is setup with, Communication width 8Bit, Clock Polarity = Idle:Low, Active:High & Clock Edge: Active to Idle → SPI Mode 0. Input Data is sampled at the Middle.
Can you please check if there is any problem with my code below ?
#include "w5500/w5500_spi.h"
#include "w5500/wizchip_conf.h"
#include "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];
volatile wiz_NetInfo gWIZNETINFO =
{
.mac = {0x00, 0x14, 0xA3, 0x72, 0x17, 0x3f}, // Source Mac Address
.ip = {192, 168, 1, 107}, // Source IP Address
.sn = {255, 255, 255, 0}, // Subnet Mask
.gw = {192, 168, 1, 1}, // Gateway IP Address
.dns = {8, 8, 8, 8}, // DNS server IP Address
.dhcp = 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;
// brief Call back function for WIZCHIP select.
void CB_ChipSelect(void)
{
SS1OUT_SetLow();// = 0;
}
// brief Call back function for WIZCHIP deselect.
void CB_ChipDeselect(void)
{
SS1OUT_SetHigh();// = 1;
}
// brief Callback function to read byte usig SPI.
uint8_t CB_SpiRead(void)
{
return SPI1_Exchange(0xAA);
}
// brief Callback function to write byte usig SPI.
void CB_SpiWrite(uint8_t wb)
{
SPI1_Exchange(wb);
}
// brief Handle TCP socket state.
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;
}
static void W5500_Init(void)
{
// Set Tx and Rx buffer size to 2KB
uint8_t buffsize[8] = { 2, 2, 2, 2, 2, 2, 2, 2 };
CB_ChipDeselect(); // Deselect module
// Reset module
WIZRESET_SetLow(); // = 0;
__delay_ms(1);
WIZRESET_SetHigh(); // = 1;
__delay_ms(1);
SPI1_Initialize();
// Wizchip initialize
wizchip_init(buffsize, buffsize);//, 0, 0, CB_ChipSelect, CB_ChipDeselect, 0, 0, CB_SpiRead, CB_SpiWrite);
reg_wizchip_cs_cbfunc(CB_ChipSelect, CB_ChipDeselect);
reg_wizchip_spi_cbfunc(CB_SpiRead, CB_SpiWrite);
// Wizchip netconf
ctlnetwork(CN_SET_NETINFO, (void*) &gWIZNETINFO);
ctlwizchip(CW_SET_PHYCONF, (void*) &phyConf);
}
int main(void)
{
SPI1_Initialize();
W5500_Init();
wizchip_getnetinfo(&pnetinfo);
}
while (1)
{
// TCP Server
TCP_Server();
}
DEBUGGING:
1.)Serial print via UART
printf(getSn_SR(SOCK_ID_UDP)) returns 3, i can’t find what it means ?
2.) Logic Analyser:
I get signals on MOSI and MISO constantly. There seem to be no errors but i can’t tell if it is working properly. Any advice how I could check from the SPI Signals ?
Do the SPI signals look correct ?
I can ping the module on IP 192.168.1.2, but shouldn’t I be able to ping it on the source IP set in volatile wiz_NetInfo gWIZNETINFO ??
Because I get no ping on 192.168.1.107. I can’t connect on both addresses via the AX1 testing program on PORT 5000.
Am I missing something ?
Any help would be much appreciated!
Best regards