[From QnA]I can't read or write registers via SPI


#1

I bought 3 your module wiznet810mj and I attempted work with it via SPI .
I use PIC18F452 which has hardewr SPI protocol. When I attempt read some register (example MR) I always get 0 from return same thing is when i write in some register.
I read all datasheet of w5100 and check out more time connected pins.
I first reset w5100 and wait some ms then enable SPI_EN and wait some ms and then send command in format f0adress (2B)data(1B).
I don’t know what can I do next???
Wiznet810mj which I got from you is fill with IP Subnet mask and enable PING. When I attempt fill MR register with 0 (because I want disable ping) I can’t.
A send my example code, maybe help you that tell me where I make wrong!

#include <18F452.h>
#DEVICE ADC=10
#use delay(clock=8000000)
#use rs232(baud=19200,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,stream=PC,errors)
#fuses HS,NOWDT,NOPROTECT,NOLVP,PUT,NOSTVREN

#include 
#include 
#include 

#bit SMP=0xFC7.7
#bit CKE=0xFC7.6
#bit CKP=0xFC6.4
#bit SSPM1=0xFC6.1
#bit SSPM0=0xFC6.0
#bit SSPEN=0xFC6.5
#bit CS=0xF82.2
#bit BF=0xFC7.0
#bit TRS3=0xF94.3
#bit TRS5=0xF94.5
#bit SSPIF = 0xF9E.3

void wiznet_init()
{

output_bit(PIN_C0,1); //PIN_CO connected to reset pin
delay_ms(10);
output_bit(PIN_C0,0);
delay_ms(10);
output_bit(PIN_C0,1);

delay_ms(10); 

SMP = 0; // input is valid in the middle of clock
CKE = 0; // rising edge is data capture
CKP = 0; // high value is passive state
SSPM0 = 0;
SSPM1 = 1; // speed f/64(312kHz), Master
SSPEN = 1; // enable SPI 

CS = 1; // disable wiznet

}

int SPI(int d) // send character over SPI
{
SPI_write(d); // send character
// while (!BF); // wait until sent
//return SPI_READ(); // and return the received character
}

int send_wiz_command()
{
int arg3,arg2,arg1,arg0;
int pom=255;

// command string - fill mr registar with 0
arg3=0xf0;// make8(cmd,3);
arg2=0x00;//make8(cmd,2);
arg1=0x00;//make8(cmd,1);
arg0=0x00;//make8(cmd,0);

CS = 0;
SPI(arg3); 
SPI(arg2);
SPI(arg1);
SPI(arg0);
CS = 1;

arg3=0x0f;// make8(cmd,3);
arg2=0x00;//make8(cmd,2);
arg1=0x00;//make8(cmd,1);
arg0=0x00;//make8(cmd,0);

CS = 0;

SPI(arg3);
SPI(arg2);
SPI(arg1);
SPI(arg0);

pom=spi_read(); 
CS = 1;

printf("rez2 = %d 
",pom);


}

void main()
{ 
int16 brojac=0;

output_bit(PIN_C1,0); // PIN_C1 - connected to SPI_EN

wiznet_init();

output_bit(PIN_C1,1); // PIN_C1 - connected to SPI_EN

delay_ms(5); 
send_wiz_command();
petlja : goto petlja ;

}

Help me PLEASE!


#2

Dear Sir,

For the SPI control, SPI_EN pin is asserted High and /SCS pin starts with Low and ends with High. Please refer to below code.

===============================================================
/**
@brief This function writes the data into W5100 registers.
*/
uint8 IINCHIP_WRITE(uint16 addr,uint8 data)
{
IINCHIP_ISR_DISABLE();
IINCHIP_SpiInit();

//SPI MODE I/F
IINCHIP_CSoff(); // SCS=0, SPI start
IINCHIP_SpiSendData(0xF0); //Write OP code
IINCHIP_SpiSendData((addr & 0xFF00) >> 8); //High Address
IINCHIP_SpiSendData(addr & 0x00FF); //Low Address
IINCHIP_SpiSendData(data); //Write Data
IINCHIP_CSon(); // SCS=1, SPI end
IINCHIP_ISR_ENABLE();
return 1;
}

/**
@brief This function reads the value from W5100 registers.
*/
uint8 IINCHIP_READ(uint16 addr)
{
uint8 data;

IINCHIP_ISR_ENABLE();

IINCHIP_ISR_DISABLE();
IINCHIP_SpiInit();
IINCHIP_CSoff(); // SCS=0, SPI start
IINCHIP_SpiSendData(0x0F); //Read OP code
IINCHIP_SpiSendData((addr & 0xFF00) >> 8); //High Address
IINCHIP_SpiSendData(addr & 0x00FF); //Low Address

IINCHIP_SpiSendData(0);
data = IINCHIP_SpiRecvData(); //Read Data
IINCHIP_CSon(); // SCS=1, SPI end
IINCHIP_ISR_ENABLE();
return data;
}