Go Down

Topic: Help intergrating some code i found (Ethernet Code).... (Read 2 times) previous topic - next topic

dxw00d


cjdelphi

it's really that convoluted to add 1 single function?


dxw00d

#7
Apr 07, 2012, 12:49 pm Last Edit: Apr 07, 2012, 12:59 pm by dxw00d Reason: 1
It can be, if that function has dependencies. Network communication is not a trivial thing to implement.

cjdelphi

Code: [Select]

#define Sn_PORT0(ch) (CH_BASE + ch * CH_SIZE + 0x0004)
/**
@brief Peer MAC register address
*/
#define Sn_DHAR0(ch) (CH_BASE + ch * CH_SIZE + 0x0006)
/**
@brief Peer IP register address
*/
#define Sn_DIPR0(ch) (CH_BASE + ch * CH_SIZE + 0x000C)
/**


So I need to add the address 0c

Code: [Select]

  __GP_REGISTER8 (MR,     0x0000);    // Mode
  __GP_REGISTER_N(GAR,    0x0001, 4); // Gateway IP address
  __GP_REGISTER_N(SUBR,   0x0005, 4); // Subnet mask address
  __GP_REGISTER_N(SHAR,   0x0009, 6); // Source MAC address
  __GP_REGISTER_N(SIPR,   0x000F, 4); // Source IP address
  __GP_REGISTER8 (IR,     0x0015);    // Interrupt
  __GP_REGISTER8 (IMR,    0x0016);    // Interrupt Mask
  __GP_REGISTER16(RTR,    0x0017);    // Timeout address
  __GP_REGISTER8 (RCR,    0x0019);    // Retry count
  __GP_REGISTER8 (RMSR,   0x001A);    // Receive memory size
  __GP_REGISTER8 (TMSR,   0x001B);    // Transmit memory size
  __GP_REGISTER8 (PATR,   0x001C);    // Authentication type address in PPPoE mode
  __GP_REGISTER8 (PTIMER, 0x0028);    // PPP LCP Request Timer
  __GP_REGISTER8 (PMAGIC, 0x0029);    // PPP LCP Magic Number
  __GP_REGISTER_N(UIPR,   0x002A, 4); // Unreachable IP address in UDP mode
  __GP_REGISTER16(UPORT,  0x002E);    // Unreachable Port address in UDP mode

//new one here?

    __GP_REGISTER_N(SIPR,   0x000C, 4); // Remote IP address  ??? now what






cjdelphi

I also found standalone code how to communicate over SPI.

Code: [Select]

I have written this code,trying to interface a PIC18F4550 with a wiznet811mj.The problem is that, when I'm trying to ping the wiz, the answer is destination unreachable.And those two leds(green and yellow are off).I've tried both..conecting with an utp cable to switch,and direct connection with crossover to my pc..the same problem.

#include <stdio.h>
#include "p18f4550.h"
#include <string.h>
#include <stdlib.h>


#pragma config FOSC   = INTOSC_HS
#pragma config WDT    = OFF
#pragma config MCLRE  = OFF
#pragma config LVP    = OFF
#pragma config PBADEN = OFF

#define WIZNET_WRITE_OPCODE 0xF0
#define WIZNET_READ_OPCODE 0x0F// Wiznet W5100 Register Addresses

#define MR   0x0000   // Mode Register
#define GAR  0x0001   // Gateway Address: 0x0001 to 0x0004
#define SUBR 0x0005   // Subnet mask Address: 0x0005 to 0x0008
#define SAR  0x0009   // Source Hardware Address (MAC): 0x0009 to 0x000E
#define SIPR 0x000F   // Source IP Address: 0x000F to 0x0012
#define RMSR 0x001A   // RX Memory Size Register
#define TMSR 0x001B   // TX Memory Size Register

#define DEL_1us          16
#define DEL_10ms  80000
#define DEL_1ms   8000

void InitializePIC(void);
void Delay(unsigned int N);
void SPI_Write(unsigned int addr,unsigned char data);
unsigned char SPI_Read(unsigned int addr);
void W5100_Init(void);

void InitializePIC(void)
{

  ADCON1 = 0X0F;
  TRISAbits.TRISA5 = 0; // NU STIU DACA E LEGAT ??

  TRISDbits.TRISD0=0;
  TRISDbits.TRISD1=0;
  TRISDbits.TRISD7=0;
  TRISCbits.TRISC0=0;//RESET

  PORTDbits.RD0=0;
  PORTDbits.RD1=0;
  PORTDbits.RD7=0;

  TRISCbits.TRISC7 = 0;  // SDO
  TRISBbits.TRISB0 = 1;  // SDI
  TRISBbits.TRISB1 = 0;  // Sck out in master mode

  SSPSTATbits.SMP = 0;//smp=0 AND CKE=1
  SSPSTATbits.CKE = 1;

  SSPCON1bits.SSPEN = 1;//enable SPI
  SSPCON1bits.WCOL  = 0;
  SSPCON1bits.SSPOV = 0;
  SSPCON1bits.CKP   = 0;

  SSPCON1bits.SSPM0 = 0;//Fosc/4
  SSPCON1bits.SSPM1 = 0;
  SSPCON1bits.SSPM2 = 0;
  SSPCON1bits.SSPM3 = 0;



  RCONbits.IPEN = 1;
  IPR1bits.SSPIP = 0;
  PIE1bits.SSPIE = 0;
  PIR1bits.SSPIF = 0;
  INTCON = 0xC0;
  OSCCON=0x72;// 8 mhz
}

void Delay(unsigned int N) {

  unsigned int i;
  for(i=0; i<N; i++);
   Nop();

}


void SPI_Write(unsigned int addr,unsigned char data)
{
  // Activate the CS pin
  PORTAbits.RA5 = 0;

// Start Wiznet W5100 Write OpCode transmission
  SSPBUF = WIZNET_WRITE_OPCODE;
  while(!PIR1bits.SSPIF);  // Start Wiznet W5100 Address High Bytes transmission
  PIR1bits.SSPIF = 0;

  SSPBUF = ((addr & 0xFF00) >> 8) ;  // Wait for transmission complete
  while(!PIR1bits.SSPIF);  // Start Wiznet W5100 Address Low Bytes transmission
  PIR1bits.SSPIF = 0;

  SSPBUF= addr & 0x00FF;  // Wait for transmission complete
  while(!PIR1bits.SSPIF); 
  PIR1bits.SSPIF = 0;

  SSPBUF = data;  // Wait for transmission complete
  while(!PIR1bits.SSPIF);
  PIR1bits.SSPIF = 0;

  // CS pin is not active

  PORTAbits.RA5 = 1;

}
unsigned char SPI_Read(unsigned int addr)
{
  // Activate the CS pin
PORTAbits.RA5 = 0;
  // Start Wiznet W5100 Read OpCode transmission

  SSPBUF = WIZNET_READ_OPCODE;  // Wait for transmission complete
  while(!PIR1bits.SSPIF);  // Start Wiznet W5100 Address High Bytes transmission
  PIR1bits.SSPIF = 0;

  SSPBUF = (addr & 0xFF00) >> 8;  // Wait for transmission complete
  while(!PIR1bits.SSPIF);  // Start Wiznet W5100 Address Low Bytes transmission
  PIR1bits.SSPIF = 0;

  SSPBUF = addr & 0x00FF;  // Wait for transmission complete
  while(!PIR1bits.SSPIF); 
  PIR1bits.SSPIF = 0;

// Send Dummy transmission for reading the data
  SSPBUF = 0x00;  // Wait for transmission complete
  while(!PIR1bits.SSPIF);
  PIR1bits.SSPIF = 0;
  // CS pin is not active

PORTAbits.RA5 = 1 ;

return(SSPBUF);
}



void W5100_Init(void)
{
  // Ethernet Setup
  unsigned char mac_addr[] = {0x00,0x08,0xDC,0x00,0x00,0x24};
  unsigned char ip_addr[] = {192,168,0,2};
  unsigned char sub_mask[] = {255,255,255,0};
  unsigned char gtw_addr[] = {192,168,0,1};

  PORTDbits.RD0=1;
  SPI_Write(MR,0x80);       

  SPI_Write(GAR + 0,gtw_addr[0]);
  SPI_Write(GAR + 1,gtw_addr[1]);
  SPI_Write(GAR + 2,gtw_addr[2]);
  SPI_Write(GAR + 3,gtw_addr[3]);
  Delay(DEL_1ms);

  SPI_Write(SAR + 0,mac_addr[0]);
  SPI_Write(SAR + 1,mac_addr[1]);
  SPI_Write(SAR + 2,mac_addr[2]);
  SPI_Write(SAR + 3,mac_addr[3]);
  SPI_Write(SAR + 4,mac_addr[4]);
  SPI_Write(SAR + 5,mac_addr[5]);
  Delay(DEL_1ms);

  SPI_Write(SUBR + 0,sub_mask[0]);
  SPI_Write(SUBR + 1,sub_mask[1]);
  SPI_Write(SUBR + 2,sub_mask[2]);
  SPI_Write(SUBR + 3,sub_mask[3]);
  Delay(DEL_1ms);

  SPI_Write(SIPR + 0,ip_addr[0]);
  SPI_Write(SIPR + 1,ip_addr[1]);
  SPI_Write(SIPR + 2,ip_addr[2]);
  SPI_Write(SIPR + 3,ip_addr[3]);
  Delay(DEL_1ms);


  // Setting the Wiznet W5100 RX and TX Memory Size, we use 2KB for Rx/Tx 4 channels
  SPI_Write(RMSR,0x55);
  SPI_Write(TMSR,0x55);
  PORTDbits.RD1=1;

}// Assign I/O stream to UART




////////////////


void main(void) {

  unsigned int i;
unsigned int gar0;

PORTCbits.RC0=0;
Delay(DEL_1us);
PORTCbits.RC0=1;
Delay(DEL_10ms);


InitializePIC();
  PORTAbits.RA5 = 1;

W5100_Init();



Bogdan Baluta





all i want is the Remote IP address (source what's the point, you set that yourserlf, your own local IP) i want the Remote IP :(

Thanks.

Go Up