Skip to content

Commit

Permalink
Merge pull request #12 from ricaun/update
Browse files Browse the repository at this point in the history
Fix the ISR not in IRAM chashes
  • Loading branch information
ricaun authored Dec 10, 2019
2 parents c647129 + c57ebe1 commit 9017fdc
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 36 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ This is the payload format, this protocol don't have any kind of encryption.
## Installation

* Install the library by [Using the Library Manager](https://www.arduino.cc/en/Guide/Libraries#toc3)
* **OR** by [Importing the .zip library](https://www.arduino.cc/en/Guide/Libraries#toc4) using either the [master](https://github.com/ricaun/LoRaNow/archive/1.0.4.zip) or one of the [releases](https://github.com/ricaun/LoRaNow/releases) ZIP files.
* **OR** by [Importing the .zip library](https://www.arduino.cc/en/Guide/Libraries#toc4) using either the [master](https://github.com/ricaun/LoRaNow/archive/1.0.5.zip) or one of the [releases](https://github.com/ricaun/LoRaNow/releases) ZIP files.

## Examples

Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=LoRaNow
version=1.0.4
version=1.0.5
author=Luiz Henrique Cassettari
maintainer=Luiz Henrique Cassettari <[email protected]>
sentence=LoRaNow Library is a simple LoRa Node <> Gateway communication protocol.
Expand Down
57 changes: 28 additions & 29 deletions src/LoRaNow.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// ---------------------------------------------------- //
// LoRaNow.cpp
// ---------------------------------------------------- //
// 28/08/2019 - esp32 interrupt fatal erro add ICACHE_RAM_ATTR
// 27/04/2019 - Add setPinsSPI to help esp32 boards
// 24/04/2019 - Fix LoRaNow board mosfet
// 23/04/2019 - Add onSleep callback
Expand Down Expand Up @@ -36,6 +37,12 @@
#endif
#endif

#ifdef ESP8266 || ESP32
#define ISR_PREFIX ICACHE_RAM_ATTR
#else
#define ISR_PREFIX
#endif

LoRaNowClass::LoRaNowClass()
{
setTimeout(0);
Expand All @@ -61,6 +68,7 @@ byte LoRaNowClass::begin()
randomSeed(seed);
now_id = makeId();
}

LORANOW_DEBUG_PRINTLN("[ln] Begin");
LoRa.onReceive(LoRaNowClass::onReceive);
LoRa.onTxDone(LoRaNowClass::onTxDone);
Expand Down Expand Up @@ -141,21 +149,32 @@ void LoRaNowClass::state_do(byte _state)
state_change(LORA_STATE_RX1_DONE, rxwindow);
break;
case LORA_STATE_RX1_DONE:
//state_change(LORA_STATE_END);
state_change(LORA_STATE_SLEEP);
break;
case LORA_STATE_RX1_WAIT:
case LORA_STATE_SLEEP:
sleep();
break;
case LORA_STATE_RECEIVE:
if (LoRaNow.messageCallback)
LoRaNow.beginDecode();
while (LoRa.available())
{
LoRaNow.write(LoRa.read());
}
if (LoRaNow.endDecode())
{
LoRaNow.messageCallback((uint8_t *)LoRaNow.buffer(), LoRaNow.available());
if (LoRaNow.messageCallback)
{
LoRaNow.messageCallback((uint8_t *)LoRaNow.buffer(), LoRaNow.available());
}
}
else
{
LoRa.receive();
}
LoRaNow.clear();
if (state != LORA_STATE_TX_WAIT)
state_change(LORA_STATE_SLEEP);
state_change(LORA_STATE_SLEEP);
break;
}
}
Expand Down Expand Up @@ -269,7 +288,6 @@ void LoRaNowClass::gateway(bool gateway)
{
_gateway = gateway;
rxwindow = 0;
//state_change(LORA_STATE_RX1);
}

void LoRaNowClass::setId(uint32_t _id)
Expand Down Expand Up @@ -510,13 +528,6 @@ int LoRaNowClass::endPacket()
return 0;
}

size_t LoRaNowClass::sendPacket(const uint8_t *buffer, size_t size)
{
LoRaNow.beginPacket();
LoRaNow.write(buffer, size);
return LoRaNow.endPacket();
}

byte LoRaNowClass::calcCheckSum(byte *packege, byte length)
{
byte sum = 0;
Expand All @@ -529,6 +540,7 @@ byte LoRaNowClass::calcCheckSum(byte *packege, byte length)

void LoRaNowClass::txMode()
{
LORANOW_DEBUG_PRINTLN("[ln] txMode");
LoRa.idle();
LoRa.setFrequency(frequency);
LoRa.setSpreadingFactor(sf);
Expand Down Expand Up @@ -578,31 +590,18 @@ void LoRaNowClass::onSleep(void (*cb)())
// LoRa
// ---------------------------------------------------- //

void LoRaNowClass::onReceive(int packetSize)
ISR_PREFIX void LoRaNowClass::onReceive(int packetSize)
{
LORANOW_DEBUG_PRINTLN("[ln] Receive");
LoRaNow.time = millis();
LoRaNow.beginDecode();
while (LoRa.available())
{
LoRaNow.write(LoRa.read());
}
if (LoRaNow.endDecode())
{
LoRaNow.state_change(LORA_STATE_RECEIVE, LORANOW_WAIT_RECEIVE);
}
else
{
LoRa.receive();
LoRaNow.clear();
}
LoRaNow.state_change(LORA_STATE_RECEIVE, LORANOW_WAIT_RECEIVE);
}

void LoRaNowClass::onTxDone()
ISR_PREFIX void LoRaNowClass::onTxDone()
{
LORANOW_DEBUG_PRINTLN("[ln] txDone");
LoRaNow.time = millis();
LoRaNow.state_change(LORA_STATE_TX_DONE);
LoRaNow.state_change(LORA_STATE_TX_DONE, LORANOW_WAIT_TXDONE);
}

LoRaNowClass LoRaNow;
5 changes: 2 additions & 3 deletions src/LoRaNow.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#define LORANOW_BUF_SIZE 128
#define LORANOW_WAIT_READY 2000
#define LORANOW_WAIT_RECEIVE 100
#define LORANOW_WAIT_TXDONE 10

#if defined(ARDUINO_ARCH_ESP32)
#define LORANOW_DEFAULT_SS_PIN 18
Expand Down Expand Up @@ -188,9 +189,7 @@ class LoRaNowClass : public Stream

int beginPacket();
int endPacket();

size_t sendPacket(const uint8_t *buffer, size_t size);


uint32_t makeId();

// ----------------------------------------------- //
Expand Down
11 changes: 9 additions & 2 deletions src/utility/LoRa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@
#define MAP_DIO3_LORA_NOP 0x03


#ifdef ESP8266 || ESP32
#define ISR_PREFIX ICACHE_RAM_ATTR
#else
#define ISR_PREFIX
#endif


LoRaClass::LoRaClass() :
_spiSettings(LORA_DEFAULT_SPI_FREQUENCY, MSBFIRST, SPI_MODE0),
_spi(&LORA_DEFAULT_SPI),
Expand Down Expand Up @@ -762,7 +769,7 @@ uint8_t LoRaClass::singleTransfer(uint8_t address, uint8_t value)
return response;
}

void LoRaClass::onDio0Rise()
ISR_PREFIX void LoRaClass::onDio0Rise()
{
LoRa.handleDio0Rise();
}
Expand Down Expand Up @@ -806,7 +813,7 @@ void LoRaClass::receiveCAD()
writeRegister(REG_OP_MODE, MODE_LONG_RANGE_MODE | MODE_CAD);
}

void LoRaClass::onCADRise()
ISR_PREFIX void LoRaClass::onCADRise()
{
LoRa.handleCADRise();
}
Expand Down

0 comments on commit 9017fdc

Please sign in to comment.