using SPI1 and SPI2 Simultaneous for w5500 and ILI9341
Posted: Wed Mar 23, 2022 4:08 pm
Hello everybody. i create one pcb board for use ILI9341 and W5500 Simultaneously.i want show analog value on lcd and send it with w5500 udp.
MCU: STM32F103C8
ILI9341
LIB: "Adafruit_GFX.h" "Adafruit_ILI9341.h"
wiring: SPI2 >> PB15,PB14,PB13
TFT_BL PB5
TFT_CS PB12
TFT_DC PB1
TFT_RST PB0
w5500
LIB: <Ethernet.h> <EthernetUdp.h>
wiring: SPI1 >> PA7,PA6,PA5
CS PA4
The problem is that whatever I do, I can not use both at the same time. In the setup section, after installing one, when initializing the second, the first one fails. If I change their place during initializing, the first one will fail again during the second initialization. The point is that when I comment on the last initialization, the first part alone works fine.
I read and used all the following texts. But the problem remains.
((in setup() call SPI.setModule(2); before doing any other SPI activities.
SPI.setModule(2);
e.g.
SPI.setModule(2);
SPI.setClockDivider(SPI_CLOCK_DIV32);
Then the SPI pins for hardware SPI2 will be used by "SPI"
A better way to use more than one SPI device at the same time is to have a separate variable for the second SPI device
e.g.
SPIClass SecondSPI(2);
SecondSPI.setClockDivider(SPI_CLOCK_DIV32);
// etc etc
Unfortunately, you cant use the variable SPI2, as SPI1, SPI2 and SPI3 are internal variables in the core related to the hardware SPI devices. But you can use SPI_2 , SPI_3 .))
my code for test is:
MCU: STM32F103C8
ILI9341
LIB: "Adafruit_GFX.h" "Adafruit_ILI9341.h"
wiring: SPI2 >> PB15,PB14,PB13
TFT_BL PB5
TFT_CS PB12
TFT_DC PB1
TFT_RST PB0
w5500
LIB: <Ethernet.h> <EthernetUdp.h>
wiring: SPI1 >> PA7,PA6,PA5
CS PA4
The problem is that whatever I do, I can not use both at the same time. In the setup section, after installing one, when initializing the second, the first one fails. If I change their place during initializing, the first one will fail again during the second initialization. The point is that when I comment on the last initialization, the first part alone works fine.
I read and used all the following texts. But the problem remains.
((in setup() call SPI.setModule(2); before doing any other SPI activities.
SPI.setModule(2);
e.g.
SPI.setModule(2);
SPI.setClockDivider(SPI_CLOCK_DIV32);
Then the SPI pins for hardware SPI2 will be used by "SPI"
A better way to use more than one SPI device at the same time is to have a separate variable for the second SPI device
e.g.
SPIClass SecondSPI(2);
SecondSPI.setClockDivider(SPI_CLOCK_DIV32);
// etc etc
Unfortunately, you cant use the variable SPI2, as SPI1, SPI2 and SPI3 are internal variables in the core related to the hardware SPI devices. But you can use SPI_2 , SPI_3 .))
my code for test is:
Code: Select all
#include <SPI.h> // needed for Arduino versions later than 0018
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include <Ethernet.h>
#include <EthernetUdp.h> // UDP library from: bjoern@cs.stanford.edu 12/30/2008
//---------------------------------------- Ethernet -------------------------------------------
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; // W5500 MAC Address
unsigned int localPort = 8888; // W5500 port
IPAddress remote_ip(192, 168, 0, 103);
unsigned int remote_Port = 9999;
// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;
//------------------------------------------ ILI9341 --------------------------------------------------------
boolean TFT_BL = false ;
// For the Adafruit shield, these are the default
#define TFT_BL PB5
#define TFT_CS PB12
#define TFT_DC PB1
#define TFT_RST PB0
// Only use Hardware SPI (Declare RST is important)
//Adafruit_ILI9341 tft = Adafruit_ILI9341 (TFT_CS, TFT_DC, TFT_RST); // Use hardware SPI
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST); // Use hardware SPI
void setup() {
pinMode(TFT_BL, OUTPUT);
digitalWrite(TFT_BL, HIGH); //Turn Backlight ON
Serial.begin(9600);
delay(1000);
Serial.print("START ...");
//------------------------------------ Initial ILI9341 --------------------------------
SPI.setModule(2); // use SPI2
tft.begin();
tft.fillScreen(ILI9341_BLACK);
tft.fillScreen(ILI9341_RED);
tft.fillScreen(ILI9341_GREEN);
tft.fillScreen(ILI9341_BLUE);
tft.fillScreen(ILI9341_BLACK);
//------------------------------------ Initial Ethernet --------------------------------
SPI.setModule(1); // use SPI1
Ethernet.init(PA4);
Ethernet.begin(mac);
// start UDP
Udp.begin(localPort);
// print your local IP address:
Serial.print("Ethernet.localIP: ");
Serial.println(Ethernet.localIP());
Serial.print("Ethernet.subnetMask: ");
Serial.println(Ethernet.subnetMask());
Serial.print("Ethernet.gatewayIP: ");
Serial.println(Ethernet.gatewayIP());
Serial.print("Ethernet.dnsServerIP: ");
Serial.println(Ethernet.dnsServerIP());
Serial.print("Ethernet.localPort: ");
Serial.println(localPort);
Serial.println("-------------------------------------------------");
}
void loop(void) {
// print your local IP address for check
Serial.print("Ethernet.localIP: ");
Serial.println(Ethernet.localIP());
Serial.print("Ethernet.subnetMask: ");
Serial.println(Ethernet.subnetMask());
Serial.print("Ethernet.gatewayIP: ");
Serial.println(Ethernet.gatewayIP());
Serial.print("Ethernet.dnsServerIP: ");
Serial.println(Ethernet.dnsServerIP());
Serial.print("Ethernet.localPort: ");
Serial.println(localPort);
Serial.println("-------------------------------------------------");
//for check lcd
tft.fillScreen(ILI9341_BLACK);
tft.fillScreen(ILI9341_RED);
tft.fillScreen(ILI9341_GREEN);
tft.fillScreen(ILI9341_BLUE);
tft.fillScreen(ILI9341_BLACK);
}