arduino-LoRa
arduino-LoRa copied to clipboard
ESP8266 SPI boot error (yes the correct SPI pins are being used)
A LoRa module is connected to ESP8266 via SPI (not the flash SPI but: MOSI GPIO13, MISO GPIO12, SCK GPIO14) to an `ESP8266. Scenario 1: When the ESP8266 is started up without the LoRa module on the SPI bus, then initialisation occurs, waiting for the LoRa module to be plugged in. When that is done, the program is further initialised and data reception occurs as normal. Scenario 2: However, when startup occurs with the LoRa module in place, then even setup is not performed.
A logic analyser shows proper data transfers in the first scenario. In scenario 2 errors are shown: "Settings mismatch" and "The initial (idle) state of the CLK line does not match the settings". Hardware and software are identical in the 2 scenarios.
Power is not the issue: a 3.3V 1A line powers the LoRa module separately, grounds connected.
EDIT: led on pin 14 interferes with SPI SCK. Moving to LED_BUILTIN (GPIO16) does not solve the issue.
EDIT #2: Solved. DIO0 is connected to GPIO0 on the ESP8266, which also serves as UART programming switch. For some reason DPIO0 is LOW at startup of the LoRa module, causing the ESP8266 to enter programming modus. Temporary solution: since GPIO0 is the only remaining available GPIO (GPIO16 is LED_BUILTIN, GPIO4 and GPIO5 are I2C; all of which I prefer to keep available for their purpose) a solution is to apply power to the LoRa module shortly after powering up the ESP8266.
QUESTION: is there any means to adapt this library so that DIO0 is set HIGH on start-up of the LoRa module?
See attachment: logic analyser output.
#include <SPI.h>
#include <LoRa.h>
#define led 14
const int csPin = 15; // LoRa chip select GPIO15(D8)->SS or chip select
const int resetPin = 2; // LoRa reset GPIO2(D4)->RESET OF Lora module
const int DIO0Pin = 0; // LoRa IRQ (Callback) GPIO0 (D3) -> IRQ or "Callback"
volatile bool doRead = false; // Flag set by callback to perform read process hand interrupt main loop
void setup() {
SPI.begin();
LoRa.setPins(csPin, resetPin, DIO0Pin); // set CS, reset, IRQ pin. Override the default CS, reset, and IRQ pins (optional)
Serial.begin(9600);
delay(1000);
while (!Serial);
Serial.println("LoRa Receiver IRQ");
Serial.flush();
while (!LoRa.begin(868E6)) {
Serial.print(".");
delay(500);
}
// LoRa.setSyncWord(0xF3); // syncword default or private: 0x12; LoRaWAN/TTN: 0x34
// LoRa.setTxPower(20);
Serial.println("LoRa Initializing OK!");
// trigger interrupt on pin 2 (INT0) (Arduino) or GPIO0 (D3) on ESP8266 (NodeMCU) from DIO0
// register the receive callback
LoRa.onReceive(onReceive);
// put the radio into receive mode
LoRa.receive();
}
void loop() {
while (!doRead) {
digitalWrite(led, HIGH);
delay(1550);
digitalWrite(led, LOW);
delay(1550);
// wait for a second
}
// yield();
}
void onReceive(int packetSize) {
if (packetSize == 0) {
return;
}
doRead = true;
// incomingPacketSize = packetSize;
// Read 2 bytes into a
uint16_t a;
char LoRaStringA[12];
for (int i = 0; i < sizeof(a); i++) {
*(((uint8_t*)&a) + i) = (uint8_t)LoRa.read();
// LoRaString = a+(char)LoRa.read();
// itoa(a, LoRaStringA, 12);
}
char a1 = a;
// Read 2 bytes into b
uint16_t b;
char LoRaStringB[12];
for (int i = 0; i < sizeof(a); i++) {
*(((uint8_t*)&b) + i) = (uint8_t)LoRa.read();
// LoRaString = b+(char)LoRa.read();
// itoa(b, LoRaStringB, 12);
}
char b1 = b;
// Read 2 bytes into counter
uint16_t counter;
for (int i = 0; i < sizeof(counter); i++) {
*(((uint8_t*)&counter) + i) = (uint8_t)LoRa.read();
}
// Read 4 bytes into x
float x;
for (int i = 0; i < sizeof(x); i++) {
*(((uint8_t*)&x) + i) = (uint8_t)LoRa.read();
}
// Read 4 bytes into z
float z;
for (int i = 0; i < sizeof(z); i++) {
*(((uint8_t*)&z) + i) = (uint8_t)LoRa.read();
}
// Display read values
Serial.print("Received: ");
Serial.print("a = ");
Serial.print(a1);
Serial.print(" b = ");
Serial.print(b1);
Serial.print(" counter = ");
Serial.print(counter);
Serial.print(" x = ");
Serial.print(x);
Serial.print(" z = ");
Serial.print(z);
Serial.print(" with RSSI ");
Serial.println(LoRa.packetRssi());
Serial.flush();
doRead = false; // Set flag back to false so next read will happen only after next ISR event
}
Solved: LoRa DIO0 is pulled low by LoRa at startup hence setting the ESP in programming mode. if connected to ESP DIO 0 or 2. Hence the only way to use DIO0 on ESP is to delay startup of the LoRa module by a few milliseconds (for exapmple using mosfet with LoRa on high side and gate delay circuit, ie resistor and cap circuit).
Should always avoid to use GPIO 0 and 2 for ESP board.