Thanks, acpie360
I have two problems:
1- When using the command:
wiz_NetInfo gWIZNETINFO = { .mac = {0x00, 0x08, 0xdc,0x00, 0xab, 0xcd},
.ip = {192, 168, 0, 226},
.sn = {255,255,255,0},
.gw = {192, 168, 0, 1},
.dns = {0,0,0,0},
.dhcp = NETINFO_STATIC };
ctlnetwork(CN_SET_NETINFO, (void*)&gWIZNETINFO);
And then I want to read parameters above by:
ctlnetwork(CN_GET_NETINFO, (void*)&gWIZNETINFO);
I see a different amount in the COM port terminal for debug !!!
The picture below is at the 7.5MHz SPI clock:
The picture below is at the 30MHz SPI clock:
=======================================================================
2- When I use the command to open the port:
socket(0,Sn_MR_UDP,3000,0);
The program stops at the same line of code and does not go to the next step !!!
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
This is all my code:
#include <xc.h>
#define FCY 60000000UL
#include <libpic30.h>
#include <stdio.h>
#include <string.h>
#define WIZCHIP 5500
#include “wizchip_conf.h”
#include “socket.h”
#include “w5500.h”
#include “uart1.h”
// FICD
#pragma config ICS = PGD1 // ICD Communication Channel Select bits (Communicate on PGEC1 and PGED1)
#pragma config JTAGEN = OFF // JTAG Enable bit (JTAG is disabled)
// FPOR
#pragma config ALTI2C1 = OFF // Alternate I2C1 pins (I2C1 mapped to SDA1/SCL1 pins)
#pragma config ALTI2C2 = OFF // Alternate I2C2 pins (I2C2 mapped to SDA2/SCL2 pins)
// FWDT
#pragma config WDTPOST = PS32768 // Watchdog Timer Postscaler bits (1:32,768)
#pragma config WDTPRE = PR128 // Watchdog Timer Prescaler bit (1:128)
#pragma config PLLKEN = ON // PLL Lock Enable bit (Clock switch to PLL source will wait until the PLL lock signal is valid.)
#pragma config WINDIS = OFF // Watchdog Timer Window Enable bit (Watchdog Timer in Non-Window mode)
#pragma config FWDTEN = OFF // Watchdog Timer Enable bit (Watchdog timer enabled/disabled by user software)
// FOSC
#pragma config POSCMD = XT // Primary Oscillator Mode Select bits (XT Crystal Oscillator Mode)
#pragma config OSCIOFNC = OFF // OSC2 Pin Function bit (OSC2 is clock output)
#pragma config IOL1WAY = OFF // Peripheral pin select configuration (Allow multiple reconfigurations)
#pragma config FCKSM = CSECMD // Clock Switching Mode bits (Clock switching is enabled,Fail-safe Clock Monitor is disabled)
// FOSCSEL
#pragma config FNOSC = FRC // Oscillator Source Selection (Internal Fast RC (FRC))
#pragma config IESO = OFF // Two-speed Oscillator Start-up Enable bit (Start up with user-selected oscillator source)
// FGS
#pragma config GWRP = OFF // General Segment Write-Protect bit (General Segment may be written)
void Init_SPI ( void )
{
// setup the SPI peripheral
SPI1STAT = 0x0; // disable the SPI module (just in case)
SPI1CON1bits.DISSCK = 0;
SPI1CON1bits.DISSDO = 0;
SPI1CON1bits.MODE16 = 0;
SPI1CON1bits.SMP = 0;
SPI1CON1bits.CKE = 1;
SPI1CON1bits.SSEN = 0;
SPI1CON1bits.CKP = 0;
SPI1CON1bits.MSTEN = 1;
SPI1CON1bits.SPRE = 0b110;
/*
bit 4-2 SPRE<2:0>: Secondary Prescale bits (Master mode)
111 = Secondary prescale 1:1
110 = Secondary prescale 2:1
...
000 = Secondary prescale 8:1
*/
SPI1CON1bits.PPRE = 0b10;
/*
bit 1-0 PPRE<1:0>: Primary Prescale bits (Master mode)
11 = Primary prescale 1:1
10 = Primary prescale 4:1
01 = Primary prescale 16:1
00 = Primary prescale 64:1
*/
// SPI1CON1 = 0x0161; // FRAMEN = 0, SPIFSD = 0, DISSDO = 0, MODE16 = 0; SMP = 0; CKP = 1; CKE = 1; SSEN = 0; MSTEN = 1; SPRE = 0b000, PPRE = 0b01
// SPI1CON1bits.CKE = 0x00;
// SPI1CON1bits.CKP = 0x00;
SPI1STAT = 0x8000; // enable the SPI module
TRISGbits.TRISG0 = 1; // RG0 input - spi1 pin
RPINR20bits.SDI1R = 0b1110000; // assigned to spi1 function
TRISFbits.TRISF0 = 0; // RF0 output - SPI1CLK pin
RPOR7bits.RP96R = 0b000110; //RP96 as SPI1CLK
TRISGbits.TRISG1 = 0; // Rg1 output - SPI1 mosi pin
RPOR13bits.RP113R = 0b000101; //RP96 as SPI1CLK
}
void Init_URAT (void)
{
TRISDbits.TRISD5 = 0; //TX1RS232 - RP69 - RD5 - GRP13
TRISDbits.TRISD4 = 0; //TX2RS232 - RP68 - RD4 - GRP12
RPOR2bits.RP69R = 0b000001; //RPn tied to UART1 Transmit
RPOR2bits.RP68R = 0b000011; //RPn tied to UART2 Transmit
TRISDbits.TRISD12 = 1; //RX1RS232 - RPI76 - RD12 - GRPI7
TRISDbits.TRISD13 = 1; //RX2RS232 - RPI77 - RD13 - GRPI8
RPINR18bits.U1RXR = 0b1001100; // RPI76
RPINR19bits.U2RXR = 0b1001101; // RPI77
uart1_open(9600);
}
////////////////////////////////////////////////
// Shared Buffer Definition for LOOPBACK TEST //
////////////////////////////////////////////////
wiz_NetInfo gWIZNETINFO = { .mac = {0x00, 0x08, 0xdc,0x00, 0xab, 0xcd},
.ip = {192, 168, 0, 226},
.sn = {255,255,255,0},
.gw = {192, 168, 0, 1},
.dns = {0,0,0,0},
.dhcp = NETINFO_STATIC };
static void w5500_cs_select(void) {
TRISFbits.TRISF1 = 0;
PORTFbits.RF1 = 0;
}
static void w5500_cs_deselect(void) {
TRISFbits.TRISF1 = 0;
PORTFbits.RF1 = 1;
}
static void w5500_write(uint8_t wb) {
uint8_t temp;
SPI1BUF = wb; // write the data out to the SPI peripheral
// Wait for the TX buffer to empty
while(SPI1STATbits.SPITBF);
// Read the receive buffer
temp = SPI1BUF;
SPI1STATbits.SPIROV = 0;
}
static uint8_t w5500_read(void) {
uint8_t temp = 0;
SPI1BUF = temp; // write the data out to the SPI peripheral
// Wait for the TX buffer to empty
// while(SPI1STATbits.SPITBF);
// Wait for the RX buffer to be full
while(!SPI1STATbits.SPIRBF);
// Read the receive buffer
temp = SPI1BUF;
SPI1STATbits.SPIROV=0;
return temp;
}
static void w5500_read_burst(uint8_t *pBuf, uint16_t len) {
size_t for_i = 0;
for(for_i = 0 ; for_i < len ; for_i++)
{
pBuf[for_i] = w5500_read();
}
}
static void w5500_write_burst(uint8_t *pBuf, uint16_t len) {
size_t for_i = 0;
for(for_i = 0 ; for_i < len ; for_i++)
{
w5500_write(pBuf[for_i]);
}
}
void network_init(void)
{
uint8_t tmpstr[6];
ctlnetwork(CN_SET_NETINFO, (void*)&gWIZNETINFO);
// Display Network Information
ctlwizchip(CW_GET_ID,(void*)tmpstr);
printf("\r\n=== %s SET NET CONF ===\r\n",(char*)tmpstr);
printf("MAC: %02X:%02X:%02X:%02X:%02X:%02X\r\n",gWIZNETINFO.mac[0],gWIZNETINFO.mac[1],gWIZNETINFO.mac[2],
gWIZNETINFO.mac[3],gWIZNETINFO.mac[4],gWIZNETINFO.mac[5]);
printf("IP: %d.%d.%d.%d\r\n", gWIZNETINFO.ip[0],gWIZNETINFO.ip[1],gWIZNETINFO.ip[2],gWIZNETINFO.ip[3]);
printf("SUB: %d.%d.%d.%d\r\n", gWIZNETINFO.sn[0],gWIZNETINFO.sn[1],gWIZNETINFO.sn[2],gWIZNETINFO.sn[3]);
printf("GW: %d.%d.%d.%d\r\n", gWIZNETINFO.gw[0],gWIZNETINFO.gw[1],gWIZNETINFO.gw[2],gWIZNETINFO.gw[3]);
printf("DNS: %d.%d.%d.%d\r\n", gWIZNETINFO.dns[0],gWIZNETINFO.dns[1],gWIZNETINFO.dns[2],gWIZNETINFO.dns[3]);
printf("======================\r\n");
__delay_ms(300);
ctlnetwork(CN_GET_NETINFO, (void*)&gWIZNETINFO);
// Display Network Information
ctlwizchip(CW_GET_ID,(void*)tmpstr);
printf("\r\n=== %s GET NET CONF ===\r\n",(char*)tmpstr);
printf("MAC: %02X:%02X:%02X:%02X:%02X:%02X\r\n",gWIZNETINFO.mac[0],gWIZNETINFO.mac[1],gWIZNETINFO.mac[2],
gWIZNETINFO.mac[3],gWIZNETINFO.mac[4],gWIZNETINFO.mac[5]);
printf("IP: %d.%d.%d.%d\r\n", gWIZNETINFO.ip[0],gWIZNETINFO.ip[1],gWIZNETINFO.ip[2],gWIZNETINFO.ip[3]);
printf("SUB: %d.%d.%d.%d\r\n", gWIZNETINFO.sn[0],gWIZNETINFO.sn[1],gWIZNETINFO.sn[2],gWIZNETINFO.sn[3]);
printf("GW: %d.%d.%d.%d\r\n", gWIZNETINFO.gw[0],gWIZNETINFO.gw[1],gWIZNETINFO.gw[2],gWIZNETINFO.gw[3]);
printf("DNS: %d.%d.%d.%d\r\n", gWIZNETINFO.dns[0],gWIZNETINFO.dns[1],gWIZNETINFO.dns[2],gWIZNETINFO.dns[3]);
printf("======================\r\n");
}
int main(void)
{
// Configure Oscillator to operate the device at 60Mhz
// Fosc= FinM/(N1N2), Fcy=Fosc/2
// Fosc= 8M60/(22)=120Mhz for 8M input clock
PLLFBD = 58; // M=60
CLKDIVbits.PLLPOST = 0; // N1=2
CLKDIVbits.PLLPRE = 0; // N2=2
OSCTUN = 0; // Tune FRC oscillator, if FRC is used
// Disable Watch Dog Timer
RCONbits.SWDTEN = 0;
// Clock switching to incorporate PLL
__builtin_write_OSCCONH( 0x03 ); // Initiate Clock Switch to Primary
// Oscillator with PLL (NOSC=0b011)
__builtin_write_OSCCONL( OSCCON || 0x01 ); // Start clock switching
while( OSCCONbits.COSC != 0b011 );
// Wait for Clock switch to occur
// Wait for PLL to lock
while( OSCCONbits.LOCK != 1 )
{ };
Init_SPI();
Init_URAT();
/*
wiz_NetInfo netInfo = { .mac = {0x00, 0x08, 0xdc, 0xab, 0xcd, 0xef}, // MAC address
.ip = {10, 4, 33, 244}, // IP address
.sn = {255, 255, 255, 0}, // IP netmask
.gw = {10, 4, 33, 1}}; // IP gateway
*/
TRISCbits.TRISC13 = 0;
uint8_t memsize[2][8] = {{2,2,2,2,2,2,2,2},{2,2,2,2,2,2,2,2}};
uint8_t txsize[8] = {2,2,2,2,2,2,2,2};
uint8_t rxsize[8] = {2,2,2,2,2,2,2,2};
reg_wizchip_cs_cbfunc(w5500_cs_select, w5500_cs_deselect);
reg_wizchip_spiburst_cbfunc(w5500_read_burst, w5500_write_burst);
reg_wizchip_spi_cbfunc(w5500_read, w5500_write);
////////////////////////////////////////////////////////////////////////
/* WIZCHIP SOCKET Buffer initialize */
wizchip_init(txsize,rxsize);
/* Network initialization */
network_init();
printf("test\n");
socket(0, Sn_MR_UDP, 3000, SF_IO_NONBLOCK);
while( 1 )
{
// Write_SPI(0x07);
PORTCbits.RC13 = 1;
__delay_ms(500);
PORTCbits.RC13 = 0;
__delay_ms(500);
printf("Hello!\r\n");
// WIZCHIP_WRITE(0x0402 ,0xff);
// WIZCHIP_WRITE(uint32_t AddrSel, uint8_t wb );
// result = socket(0, Sn_MR_UDP, 3000, SF_IO_NONBLOCK);
// printf(“socket Result: %d\r\n”, result);
// sendto(0, testBuffer, strlen(testBuffer), address, 300);
// printf(“sendto Result: %d\r\n”, result);
// __delay_ms(1000);
//
// loopback_udps(1,testBuffer,3000);
// __delay_ms(1000);
}
}