CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

W5500 Library
Goto page Previous  1, 2, 3
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
ressas



Joined: 15 Nov 2019
Posts: 135

View user's profile Send private message

PostPosted: Tue Jul 28, 2020 11:56 pm     Reply with quote

Last question(maybe)
Quote:

W5500 has Common Register, 8 Socket Register, TX/RX Buffer Block for each Socket.
(datasheet page 15)

What should I understand from 8 socket register expressions here.
their differences are not specified.

Is it a problem to use a single socket throughout the program?
I think I will use tx while writing, and rx when reading.
When I first read it, I understood that I could plug 8 different sockets into an integrated device.
https://lh3.googleusercontent.com/proxy/EDXUnW5sfh5Lw_susbi0RocW5QvbxgBsmd9xelYUlGy9ZWPeggCCmGXNVYOSvDYisEfmTxSTe1LBVXu4gsIvrdjTCZfXU_VCSkqMdKN4TrUtsyU4YE80aFO741_FSWkN_VuiWk_YirqejZfHrajQNBhTxYhH80sSBk6AByc_J0G-Q4gZFO08VBZ1cRNyB0E


AND
If I have something missing, please correct it.
Code:

unsigned int8 w5500_read(unsigned int16 address){
   unsigned int8 data;
   output_low(_pinSCS);
   spi_xfer(w5500,address,16);
   spi_xfer(w5500, BLOCK(0) | READ | VDMN,8);
   data = spi_xfer(w5500,0x0,8);
   output_high(_pinSCS);
   return data;
}
void w5500_write(unsigned int16 address,unsigned int8 data){
   output_low(_pinSCS);
   spi_xfer(w5500,address,16);
   spi_xfer(w5500, BLOCK(0) | WRITE | VDMN,8);
   spi_xfer(w5500,data,8);
   output_high(_pinSCS);
}


Last edited by ressas on Wed Jul 29, 2020 8:07 am; edited 1 time in total
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Wed Jul 29, 2020 12:46 am     Reply with quote

These are not physical 'sockets'. These are logical connections.
In networking, a 'socket;, is a logical connection.

<https://www.quora.com/In-simple-terms-what-is-a-socket-in-networking>

Each connection setup to/from the device, is a separate 'socket'.
Once you setup a TCP connection for example, this is a 'socket'.

Now a single socket may be possible, but for a lot of things 'no'. If
for example you have a TCP connection and use UDP, this will be
two sockets.
ressas



Joined: 15 Nov 2019
Posts: 135

View user's profile Send private message

PostPosted: Wed Jul 29, 2020 8:07 am     Reply with quote

One more thing.
The socket and Common registers provided in Datasheet have the same addresses. What should I do to separate them
Code:

#define MR    0x0000          //mode register

#define GAR1  0x0001          //Gateway IP Address 192.168.0.1
#define GAR2  0x0002
#define GAR3  0x0003
#define GAR4  0x0004


Code:

//////////////Socket Registers////////////////
#define Sn_MR    0x0000     //Socket n Mode
#define Sn_CR    0x0001     //Socket n Command
#define Sn_IR    0x0002     //Socket n Interrupt
#define Sn_SR    0x0003     //Socket n Status
#define Sn_PORT1 0x0004     //Socket n Source Port
#define Sn_PORT2 0x0005


Progress:
I proceed as shown on the page below. I am currently writing a value to a register and reading it. (A simple event.)
But I got stuck in the socket register section
https://wizwiki.net/wiki/doku.php/products:w5500:application:tcp_function
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Wed Jul 29, 2020 8:16 am     Reply with quote

Yes, they have the same address. They are in different blocks....

Look at section 2.2.2 'Control Phase'.
Block 0 accesses the common registers.
Block 1 Socket 0 registers
Block 5 Socket 1

etc. etc.

The block locations for the Socket N register block is (n*4)+1. The RX block
(n*4)+2, the TX block (n+4)+3
ressas



Joined: 15 Nov 2019
Posts: 135

View user's profile Send private message

PostPosted: Wed Aug 05, 2020 1:32 am     Reply with quote

Thank you Ttelmah. I look the control phase.
When I set Block select bits to 0x00, I access common registers. In other cases, I access socket registers. (As I understand).
By following the steps in the link below, I came to the "SOCKET Initialization" section.
I don't know how to write expressions like "OPEN" - "CLOSE" to the sn_CR register. And what path should I follow ?
Please look this link:
https://wizwiki.net/wiki/doku.php/products:w5500:application:tcp_function

Secondly, I'm not sure about "Set socket memory information".


wiznet.h
Code:

#define _pinCLK  pin_C3
#define _pinSDI  pin_C4
#define _pinSDO  pin_C5
#define _pinSCS  pin_D0

#define BLOCK(x) (x<<3)
#define WRITE 4
#define READ  0
#define VDMN  0
#define FDL1  1
#define FDL2  2
#define FDL4  3

// Common Register

#use SPI(MASTER, DO=_pinSDO, DI = _pinSDI, CLK=_pinCLK, baud=1000000, bits=16, MODE=2, stream=w5500)

#define MR    0x0000          //mode register

#define GAR1  0x0001          //Gateway IP Address 192.168.0.1
#define GAR2  0x0002
#define GAR3  0x0003
#define GAR4  0x0004

#define SUBR1 0x0005          //Subnet Mask 255.255.255.0
#define SUBR2 0x0006
#define SUBR3 0x0007
#define SUBR4 0x0008

#define SHAR1 0x0009          //Source Hardware Address 00.08.DC.01.02.03
#define SHAR2 0x000A
#define SHAR3 0x000B
#define SHAR4 0x000C
#define SHAR5 0x000D
#define SHAR6 0x000E

#define SIPR1 0x000F       //Source IP Address 192.168.0.2
#define SIPR2 0x0010
#define SIPR3 0x0011
#define SIPR4 0x0012
 
#define INTLEVEL1 0x0013   //Interrupt Low Level Timer
#define INTLEVEL2 0x0014

#define IR    0x0015      //Interrupt

#define IMR   0x0016      //Interrupt Mask
#define SIR   0x0017      //Socket Intterrupt
#define SIMR  0x0018      //Socket Interrupt Mask
#define RTR1  0x0019      //Retry Time-value
#define RTR2  0x001A
#define RCR   0x001B     //Retry Count
#define PTIMER 0x001C    //PPP Link Control Protocol Request Timer
#define PMAGIC 0x001D    //PPP Link Control Protocol Magic number

#define PHAR1 0x001E     //Destination Hardware Address Register in PPPoE mode)
#define PHAR2 0x001F     //00:08:DC:12:34:56
#define PHAR3 0x0020
#define PHAR4 0x0021
#define PHAR5 0x0022
#define PHAR6 0x0023

#define PSID1 0x0024      //Session ID Register in PPPoE mode)
#define PSID2 0x0025      //
#define PMRU1 0x0026        //Maximum Receive Unit in PPPoE mode
#define PMRU2 0x0027

#define UIPR1 0x0028        //Unreachable IP Address
#define UIPR2 0x0029
#define UIPR3 0x002A
#define UIPR4 0x002B

#define UPORTR1 0x002C    //Unreachable Port
#define UPORTR2 0x002D

#define PHYCFGR  0x002E    //W5500 PHY Configuration
#define VERSIONR 0x0039    //W5500 Chip Version

//////////////Socket Registers////////////////
#define Sn_MR    0x0000     //Socket n Mode
#define Sn_CR    0x0001     //Socket n Command
#define Sn_IR    0x0002     //Socket n Interrupt
#define Sn_SR    0x0003     //Socket n Status
#define Sn_PORT1 0x0004     //Socket n Source Port
#define Sn_PORT2 0x0005

#define Sn_DHAR1 0x0006 //Socket n Destination Hardware Address
#define Sn_DHAR2 0x0007
#define Sn_DHAR3 0x0008
#define Sn_DHAR4 0x0009
#define Sn_DHAR5 0x000A
#define Sn_DHAR6 0x000B

#define Sn_DIPR1 0x000C      //Socket n Destination IP Address
#define Sn_DIPR2 0x000D
#define Sn_DIPR3 0x000E
#define Sn_DIPR4 0x000F

#define Sn_DPORT1 0x0010     //Socket n Destination Port
#define Sn_DPORT2 0x0011

#define Sn_MSSR1 0x0012      //Socket n Maximum Segment Size
#define Sn_MSSR2 0x0013
#define Sn_TOS   0x0015      //Socket n IP Type of Service
#define Sn_TTL   0x0016      //Socket n TTL

#define Sn_RXBUF_SIZE 0x001E //Socket n RX Buffer Size
#define Sn_TXBUF_SIZE 0x001F //Socket n TX Buffer Size

#define Sn_TX_FSR1 0x0020     //Socket n TX Free Size
#define Sn_TX_FSR2 0x0021
#define Sn_TX_RD1  0x0022
#define Sn_TX_RD2  0x0023
#define Sn_TX_WR1  0x0024    //Socket n TX Write Pointer
#define Sn_TX_WR2  0x0025

#define Sn_RX_RSR1 0x0026    //Socket n Received Size
#define Sn_RX_RSR2 0x0027
#define Sn_RX_RD1  0x0028    //Socket n RX Read Data Pointer
#define Sn_RX_RD2  0x0029
#define Sn_RX_WR1  0x002A
#define Sn_RX_WR2  0X002B

#define Sn_IMR     0x002C      //Socket n Interrupt Mask
#define Sn_FRAG1   0x002D      //Socket n Frafment
#define Sn_FRAG2   0x002E
#define Sn_KPALVTR 0x002F       //Socket n Keep Alive Time


 unsigned int8 w5500_read(unsigned int16 address, unsigned int8 blockData);
 
 void w5500_write(unsigned int16 address,unsigned int8 data, unsigned int8 blockData);
 
 void start_setting();
 
 void basic_setting();
 
 void network_setting();
 
 void socket_setting();


wiznet.c

Code:

#include "w5500.h"

unsigned int8 w5500_read(unsigned int16 address,unsigned int8 blockData){
   unsigned int8 data;
   output_low(_pinSCS);
   delay_cycles(1);
   spi_xfer(w5500,address,16);
   spi_xfer(w5500, BLOCK(blockData) | READ | VDMN,8);
   data = spi_xfer(w5500,0x0,8);
   output_high(_pinSCS);
   return data;
}
void w5500_write(unsigned int16 address,unsigned int8 data, unsigned int8 blockData){
   output_low(_pinSCS);
   delay_cycles(1);
   spi_xfer(w5500,address,16);
   spi_xfer(w5500, BLOCK(blockData) | WRITE | VDMN,8);
   spi_xfer(w5500,data,8);
   output_high(_pinSCS);
}

void start_setting(){
     //start setting
     w5500_write(PHYCFGR,0xB8,0x00); //step 1  PHYCFGR config
}

void basic_setting(){
     //basic setting
     w5500_write(MR  , 0x00 , 0x00); //step 2 MR  config
     w5500_write(IMR , 0x00 , 0x00); //step 3 IMR config
     w5500_write(RTR1, 0x0F , 0x00); //step 4 RCR config
     w5500_write(RTR2, 0xA0 , 0x00);
}

void network_setting(){
     //Network Information setting
     w5500_write(SHAR1,0x00,0x00); //step 5 SHAR config
     w5500_write(SHAR2,0X08,0x00);
     w5500_write(SHAR3,0xDC,0x00);
     w5500_write(SHAR4,0x01,0x00);
     w5500_write(SHAR5,0x02,0x00);
     w5500_write(SHAR6,0x03,0x00);
     
     w5500_write(GAR1,0xC0,0x00); // 192 . 168 . 0 . 1
     w5500_write(GAR2,0xA8,0x00); // 168
     w5500_write(GAR3,0x00,0x00); // 0
     w5500_write(GAR4,0x01,0x00); // 1
   
     w5500_write(SUBR1,0xFF,0x00); // 255 . 255 . 255 . 0
     w5500_write(SUBR2,0xFF,0x00); // 255
     w5500_write(SUBR3,0xFF,0x00); // 255
     w5500_write(SUBR4,0x00,0x00); // 0
   
     w5500_write(SIPR1,0xC0,0x00); // 192 . 168 . 0 . 2
     w5500_write(SIPR2,0xA8,0x00); // 168
     w5500_write(SIPR3,0x00,0x00); // 0
     w5500_write(SIPR4,0x02,0x00); // 2
}

void socket_setting(){
//do{  //Sn_CR = CLOSE;
     start:
     w5500_write(Sn_MR ,  0x01,0x00); //
     w5500_write(Sn_PORT1,0xAB,0x00); //
     w5500_write(Sn_PORT2,0x00,0x00); //
     w5500_write(Sn_CR ,  0x01,0x00); //
     if(Sn_SR != "SOCK_INIT"){
     //Sn_CR = "CLOSE";
     goto start;
     }
//}while(Sn_SR != "SOCK_INIT");
}



wiznet_main.c

Code:

#include <18F46k22.h>
#device  ADC=12
#fuses   NOWDT,NOBROWNOUT,PUT,NOWRT,NODEBUG,INTRC_IO, NOMCLR, NOPROTECT, NOWDT, NOLVP,PLLEN                 
                 
#use     delay(internal=64000000)

#use     rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
 
#include "w5500.c"
/*
unsigned int16 S0_RXMEM_SIZE = 0;
unsigned int16 S0_TXMEM_SIZE = 0;

#locate S0_RXMEM_SIZE = 0x80
#locate S0_TXMEM_SIZE = 0x82
*/
void main()
{
   setup_oscillator(OSC_64MHZ , OSC_PLL_ON);
   setup_adc_ports(NO_ANALOGS,VSS_VDD);
   output_high(_pinSCS);
   
   start_setting();   //start setting
   
   basic_setting();   //basic setting
   
   network_setting(); //Network Information setting   
 
   //socket_setting();  //SOCKET REGISTER
   
   
   
   unsigned int16 r1,r2,r3,r4;
   while(true)
   {
   
   r1 = w5500_read(GAR1,0x00); // 192 . 168 . 0 . 1
   delay_us(20);
   r2 = w5500_read(GAR2,0x00); // 168
   delay_us(20);
   r3 = w5500_read(GAR3,0x00); // 0
   delay_us(20);
   r4 = w5500_read(GAR4,0x00); // 1
   delay_us(20);
   
   printf("V: %ld %ld %ld %ld\n",r1,r2,r3,r4);
   printf("%ld\n",Sn_SR);
   delay_ms (2000);
    }

  }



ressas



Joined: 15 Nov 2019
Posts: 135

View user's profile Send private message

PostPosted: Wed Aug 05, 2020 8:35 pm     Reply with quote

any answers Question
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Thu Aug 06, 2020 1:31 am     Reply with quote

Seriously, this is called programming.

You are going to have to get the existing library from the Wiznet site, and
modify this for the PIC:
<https://github.com/Wiznet/ioLibrary_Driver/tree/master/Ethernet>
The code is 99% directly portable, just the hardware comms part needs
to change.
The .chm file provides a complete manual to the functions provided.
You need the stuff that is in the root directory there, and then the W5500
drivers in the sub-directory.
These give complete API functions to actually open a socket, handle data
I/O using this etc. etc..

Then in the directory above:
<https://github.com/Wiznet/ioLibrary_Driver/tree/master/Application>

There are examples showing these drivers being used for a loopback
and multicast.
ressas



Joined: 15 Nov 2019
Posts: 135

View user's profile Send private message

PostPosted: Sat Aug 22, 2020 2:51 am     Reply with quote

Well alright.
I downloaded the Wiznet libraries from the place you said.
It would take me time to adapt to CCS c. But after a few minor fixes for Mplab IDE it was fixed. (Compiled without an error)

Later I found a code adapted for ARM.

https://www.carminenoviello.com/2015/08/28/adding-ethernet-connectivity-stm32-nucleo/

I watched a video showing the code working fine:

https://www.youtube.com/watch?v=zsLQm6SgL_c

Code:

//*****************************************
  void cs_sel() {
   CS_SetLow(); //CS LOW
}
 
void cs_desel() {
   CS_SetHigh(); //CS HIGH
}
uint8_t spi_readb(void) {
   uint8_t rbuf;
   rbuf = SPI1_ExchangeByte(0x00);
   return rbuf;
}

void spi_writeb(uint8_t b) {
   SPI1_ExchangeByte(b);
}
//****************************************
void main(void)
{
    // Initialize the device
    SYSTEM_Initialize();
    SPI1_Open(0);
    uint8_t retVal, sockStatus;
    int16_t rcvLen;
    uint8_t rcvBuf[20];
    uint8_t bufSize[] = {2, 2, 2, 2};


  wizchip_init(bufSize, bufSize);
                              //80-A5-89-62-0A-19
  wiz_NetInfo netInfo = { .mac    = {0x00, 0x08, 0xdc, 0xab, 0xcd, 0xef},   // Mac address
                          .ip    = {192, 168, 1, 50},               // IP address
                          .sn    = {255, 255, 255, 0},               // Subnet mask
                          .gw    = {192, 168, 2, 1}};               // Gateway address
  wizchip_setnetinfo(&netInfo);
  wizchip_getnetinfo(&netInfo);
 // 0x00, 0x08, 0xdc, 0xab, 0xcd, 0xef
  //PRINT_NETINFO(netInfo);

  reg_wizchip_cs_cbfunc (cs_sel, cs_desel);
  reg_wizchip_spi_cbfunc(spi_readb, spi_writeb);
while(1);
}


Quote:

void cs_sel() {
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_RESET); //CS LOW
}

void cs_desel() {
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_SET); //CS HIGH
}

uint8_t spi_rb(void) {
uint8_t rbuf;
HAL_SPI_Receive(&hspi2, &rbuf, 1, 0xFFFFFFFF);
return rbuf;
}

void spi_wb(uint8_t b) {
HAL_SPI_Transmit(&hspi2, &b, 1, 0xFFFFFFFF);
}


The part I show as "QUOTE" is the part given in the link. I guess I couldn't adapt that expression properly in my code.
SPR_writeByte // SPI_ExchangeByte // SPI_ExchangeBlock
I'm not sure which one of the expressions I should use.

Why am I asking for an edit of MPLAB IDE in CCS C block?
Because I rely on your deep knowledge. Laughing
And I don't like MPLAB's Blog page.

If you can help me with this part, I will share all the codes here in case the code works. And I was already sharing my other apps.

I'm waiting for your answer
and I'm tired
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2, 3
Page 3 of 3

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group