Trying to Connect BW16 to Wiz5500 using SPI

Hi all,

I am using the following code to read the VERSIONR Register in Wiz5500 ethernet chip.

/* Dated: 4th April 
 *  
 *  A sketch to read the VERSIONR Register using SPI in Arduino.
 *  Module is RTL8720 (BW16)
 *  Ameba Arduino SDK - 3.1.7
 *  
 */
 
#include <SPI.h>
#include "definitions.h"

#define SPI_MODE     'M'
    
    SPISettings mySPI(1000000, MSBFIRST, 0);// SPI setup, 1 MHz clock speed

    
void setup() {

    pinMode(9,OUTPUT); // CS/SS Pin See Section-2 of W5500 datasheet
    pinMode(10,OUTPUT); // CLK Pin
    pinMode(11,INPUT);  // MISO Pin
    pinMode(12,OUTPUT); // MOSI Pin
   
   Serial.begin(115200);
   Serial.println("VERSIONR Register Test!"); 
   
   SPI.begin(9);
   delay(1); 
    
}

void loop() {
  

// **************** 3-Phase Data Transfer: Address, Control & Data Phases*******************

      uint16_t address = SPI.transfer16(PA15, VERSIONR, SPI_CONTINUE); // First- Address Phase. See Section 2.2.1 of W5500 datasheet @ (https://docs.wiznet.io/Product/iEthernet/W5500/overview.
                                                               // For PHYCFGR, see lines 56-59 in "definitions.h"                                                            
      uint8_t control  = SPI.transfer(PA15, 0b00000000, SPI_CONTINUE);// Second- Control Phase.  See Section 2.2.2 of datasheet
 
      uint16_t reg_data = SPI.transfer(PA15, SPI_LAST);// Third: Data Phase.  See Section 2.2.3 of datasheet.

    
//*****************************************************************************************
 
  
  Serial.print("VERSIONR Address -- ");
  Serial.println (VERSIONR, HEX);
     
  Serial.print ("CONTROL -- ");
  Serial.println (control);

  Serial.print ("VERSIONR -- ");
  Serial.println (reg_data);
     
  Serial.println (); Serial.println ();
  
delay(1000);

}

I am only getting a ‘0’ in response.
What am I doing wrong?

Also please point out any superfluous/redundant code.

TIA and Regards
azhaque

PS:
uint16_t address = SPI.transfer16(PA15, VERSIONR, SPI_CONTINUE)
Should I replace SPI_CONTINUE with SPI_LAST?

Hi @azhaque,

To really debug this, the best way is to see from the waveform from a logic analyzer.

You can try putting in the setup loop first, to see if the data is transferred correctly on the SPI bus.

You can test with SPI_CONTINUE first, before testing with SPI_LAST.

You can take reference to AmebaILI9341.cpp to see how to use the transfer method for SPI.

I believe by default, the SPI.h shows it is using SPI_LAST.

Thank you.

Thanks for the quick response Kelvin.
On the Oscilloscope the CLK signal shows 8 bits and then stops. I don’t have access to a logic analyzer.
My Loopback test sketch (code copied below) worked fine.

/*
 * A sketch to perform a LOOPBACK Test using RTL8720 (BW16).
 * Uses the SPI.h library packaged with the Ameba Arduino SDK 3.1.7.
 * Loopback is performed by sending the data out byte by byte from MOSI. 
 * The same data is received back through MISO.
 * The received byte is converted to character and sent to the computer using Serial.print()

*/


#include <SPI.h>

char outt [40] = "THEQUICKBROWN_fox_jumpedover1234567890";  // string to be sent and received via SPI port

SPISettings my_spi(14000000, MSBFIRST, SPI_DATA_MODE0);// SPI setup

int caunter;
int inn;

void setup() {
    
    Serial.begin(115200);
    Serial.println("SPI Loopback Test!"); 
   
    SPI.begin();
    delay(100);  
}

void loop(){
  
  for (caunter = 0; caunter <= 45; caunter++) {
   
    inn = SPI.transfer(outt[caunter]);
  
     char c = inn;
     Serial.print (outt[caunter]);
     Serial.print(" -- ");
     Serial.println(c);
    
  delay(500);

   }
}

I have reduced the frequency in the 5500 code to 1MHz to reduce parasitic effects. I will speed it up once I get to the PCB stage.

As suggested I will try and report back results.
Thanks again.
azhaque

Failed test for transfer16 function.

Code given below. It is obvious that I am not using the transfer16() function correctly.

/*
 * A sketch to perform a LOOPBACK Test using RTL8720 (BW16).
 * Uses the SPI.h library packaged with the Ameba Arduino SDK 3.1.7.
 * Loopback is performed by sending the 16 bit data out from MOSI, using transfer16 function in SPI.h. 
 * The same data is received back through MISO.
 
*/


#include <SPI.h>

SPISettings my_spi(14000000, MSBFIRST, SPI_DATA_MODE0);// SPI setup

uint16_t caunter;

void setup() {
    
    Serial.begin(115200);
    Serial.println("SPI Loopback Test!"); 
   
    SPI.begin();
    delay(100);  
}

void loop(){
  
  for (caunter = 0; caunter <= 1000; caunter++) {
   
    uint16_t inn = SPI.transfer16(caunter);
  
     Serial.print (caunter, DEC);
     Serial.print(" -- ");
     Serial.println(inn, HEX);
    
 // delay(500);

   }
}

Will amend and test again.

Regards

Kelvin,

The following is an excerpt from SPI.h provided in the Arduino 3.1.7 SDK.

// SPI_HAS_EXTENDED_CS_PIN_HANDLING means SPI has automatic 
// CS pin handling and provides the following methods:
//   - begin(pin)
//   - end(pin)
//   - setBitOrder(pin, bitorder)
//   - setDataMode(pin, datamode)
//   - setClockDivider(pin, clockdiv)
//   - transfer(pin, data, SPI_LAST/SPI_CONTINUE)
//   - beginTransaction(pin, SPISettings settings) (if transactions are available)
#define SPI_HAS_EXTENDED_CS_PIN_HANDLING 1

I note that transfer16() function IS NOT MENTIONED in this list, ALTHOUGH my sketch (containing the transfer16() function) compiles without any issues.

The excerpt below is from SPI.cpp provided in the Arduino 3.1.7 SDK.

class SPIClass {
    public:
        SPIClass(void *pSpiObj, int mosi, int miso, int clk, int ss);

        void transfer(byte _pin, void *_buf, size_t _count, SPITransferMode _mode = SPI_LAST);
        void transfer(void *_buf, size_t _count, SPITransferMode _mode = SPI_LAST);

        **uint16_t transfer16(byte _pin, uint16_t _data, SPITransferMode _mode = SPI_LAST);**
**        uint16_t transfer16(uint16_t _data, SPITransferMode _mode = SPI_LAST);**

I assume that because of these lines the sketch compiles OK. However I have no means of knowing whether the SDK handles it, which is why I am only getting 0s in the output. As against that, using transfer() in the loopback sketch works OK

As per your advise I looked at AmebaILI9341.cpp in detail. I noted that that AmebaILI9341.cpp ONLY uses transfer(). transfer16() isn’t used anywhere in the AmebaILI9341.cpp library.

Could you please comment.

Regards

Hi @azhaque,

As you can see from the SPI.h, you can see that transfer16 takes in 2 parameters. The first parameter is the 16 bit data, while the second parameter is default set as SPI_LAST. Take reference to AmebaILI9341.cpp. I will illustrate how to use the transfer16 method.

Best Regards,
Kelvin

Good morning Kelvin.
First of all many thanks for the effort you made in modifying the AmebaILI9341.cpp library to help me. As per your ealier advice, I DID have a look into the 9341 library. But I didn’t find anything BECAUSE I WAS EXPECTING a transfer16() instance. But there is none. The code uses 2 transfer() instructions to send out the 16 bit data.
However today I will now examine and test your latest input on my test setup which consists of a BW16, an ESP32 and a wiz5500 module (right-most module in the photo).

Another point that I would like to put on record for the benefit of other makers is as to why the transfer() apparently works IN LOOPBACK but transfer16() doesn’t in my setup with w5500, pictured above.
I think that the assumption that since transfer() works on wiz5500, transfer16() should work too- is not correct. To test it I will do a physical loopback today i.e. connect MOSI an MISO pins of the bw16 together.

Finally one question. Is the BW16 clock speed 200MHz? I could not find a direct answer to this except that the cpu used is 200 MHz.

Thanks again

Kelvin,

The code that I used is as per your instruction in your last post (the first method)

[code]

#include <SPI.h>

#define SPI_HAS_EXTENDED_CS_PIN_HANDLING 1
SPISettings my_spi(14000000, MSBFIRST, 0);// SPI setup
#define SPI_MODE = 'M'

uint16_t caunter;
uint16_t inn;
 
void setup() {
    
    Serial.begin(115200);
    Serial.println("SPI Loopback Test!"); 
   
    SPI.begin();
    delay(10);  
}

void loop(){
  
  for (caunter = 0; caunter <= 1000; caunter++) {
   
    **inn = SPI.transfer16(caunter,SPI_LAST);**
  
     Serial.print (caunter, DEC);
     Serial.print(" -- ");
     Serial.println(inn, HEX);
    
 // delay(500);

   }
}
[/code]

inn = SPI.transfer16(caunter, SPI_LAST);
The above code line of my sketch is equivalent to (_spi.transfer16(color[i], SPI_LAST). And it doesn’t work. I will test it with the other two i.e:

  • inn = SPI.transfer16(caunter);

  • inn = SPI.transfer16(caunter,SPI_CONTINUE);

and report back results.

Regards

tested with both instances of code. No effect.

Regards

SUCCESS AT LAST

Physically shorting the MOSI and MISO pins on the BW16 did the trick; at least for transfer().

Result video. Shows both the data sent and received

Will now test for transfer16().

SUCCESS with transfer() with MOSI and MISO pins on the BW16 connected together.

Output being shown in HEX on the right-most column.

Code used given below

[code]
/*
 * A sketch to perform a LOOPBACK Test using RTL8720 (BW16).
 * Uses the SPI.h library packaged with the Ameba Arduino SDK 3.1.7.
 * Loopback is performed by sending the 16 bit data out from MOSI, using transfer16 function in SPI.h. 
 * The same data is received back through MISO.
 
*/


#include <SPI.h>

#define SPI_HAS_EXTENDED_CS_PIN_HANDLING 1
SPISettings my_spi(14000000, MSBFIRST, 0);// SPI setup
#define SPI_MODE = 'M'


uint16_t caunter;
uint16_t inn;
 
void setup() {
    
    Serial.begin(115200);
    Serial.println("SPI Loopback Test!"); 
   
    SPI.begin();
    delay(10);  
}

void loop(){
  
  for (caunter = 0; caunter <= 1000; caunter++) {
   
    inn = SPI.transfer16(caunter,SPI_CONTINUE);
  
     Serial.print (caunter, DEC);
     Serial.print(" -- ");
     Serial.println(inn, HEX);
    
delay(500);

   }
}
[/code]

Thanks are due to Kelvin for this.

What next?

azhaque

Hi @azhaque,

Glad that your loopback test is working. You can then check to see if you can read the versionr register in wiz5500 ethernet chip, now that you know how to use both the transfer() and transfer16() method of SPI.

Thank you.

Best Regards,
Kelvin