Skip to content

Commit e641518

Browse files
committed
OnTxDone
Add callback onTxDone
1 parent ed97efd commit e641518

File tree

4 files changed

+67
-14
lines changed

4 files changed

+67
-14
lines changed

API.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,24 @@ LoRa.endPacket(async);
117117

118118
Returns `1` on success, `0` on failure.
119119

120+
### Tx Done
121+
122+
**WARNING**: Not supported on the Arduino MKR WAN 1300 board!
123+
124+
### Register callback
125+
126+
Register a callback function for when a packet transmission finish.
127+
128+
```arduino
129+
LoRa.onTxDone(onTxDone);
130+
131+
void onTxDone() {
132+
// ...
133+
}
134+
```
135+
136+
* `onTxDone` - function to call when a packet transmission finish.
137+
120138
## Receiving data
121139

122140
### Parsing packet

keywords.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ peek KEYWORD2
3131
flush KEYWORD2
3232

3333
onReceive KEYWORD2
34+
onTxDone KEYWORD2
3435
receive KEYWORD2
3536
idle KEYWORD2
3637
sleep KEYWORD2

src/LoRa.cpp

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ LoRaClass::LoRaClass() :
7070
_frequency(0),
7171
_packetIndex(0),
7272
_implicitHeaderMode(0),
73-
_onReceive(NULL)
73+
_onReceive(NULL),
74+
_onTxDone(NULL)
7475
{
7576
// overide Stream timeout value
7677
setTimeout(0);
@@ -177,13 +178,14 @@ int LoRaClass::beginPacket(int implicitHeader)
177178

178179
int LoRaClass::endPacket(bool async)
179180
{
181+
182+
if ((async) && (_onTxDone))
183+
writeRegister(REG_DIO_MAPPING_1, 0x40); // DIO0 => TXDONE
184+
180185
// put in TX mode
181186
writeRegister(REG_OP_MODE, MODE_LONG_RANGE_MODE | MODE_TX);
182187

183-
if (async) {
184-
// grace time is required for the radio
185-
delayMicroseconds(150);
186-
} else {
188+
if (!async) {
187189
// wait for TX done
188190
while ((readRegister(REG_IRQ_FLAGS) & IRQ_TX_DONE_MASK) == 0) {
189191
yield();
@@ -353,8 +355,24 @@ void LoRaClass::onReceive(void(*callback)(int))
353355

354356
if (callback) {
355357
pinMode(_dio0, INPUT);
358+
#ifdef SPI_HAS_NOTUSINGINTERRUPT
359+
SPI.usingInterrupt(digitalPinToInterrupt(_dio0));
360+
#endif
361+
attachInterrupt(digitalPinToInterrupt(_dio0), LoRaClass::onDio0Rise, RISING);
362+
} else {
363+
detachInterrupt(digitalPinToInterrupt(_dio0));
364+
#ifdef SPI_HAS_NOTUSINGINTERRUPT
365+
SPI.notUsingInterrupt(digitalPinToInterrupt(_dio0));
366+
#endif
367+
}
368+
}
369+
370+
void LoRaClass::onTxDone(void(*callback)())
371+
{
372+
_onTxDone = callback;
356373

357-
writeRegister(REG_DIO_MAPPING_1, 0x00);
374+
if (callback) {
375+
pinMode(_dio0, INPUT);
358376
#ifdef SPI_HAS_NOTUSINGINTERRUPT
359377
SPI.usingInterrupt(digitalPinToInterrupt(_dio0));
360378
#endif
@@ -369,6 +387,9 @@ void LoRaClass::onReceive(void(*callback)(int))
369387

370388
void LoRaClass::receive(int size)
371389
{
390+
391+
writeRegister(REG_DIO_MAPPING_1, 0x00); // DIO0 => RXDONE
392+
372393
if (size > 0) {
373394
implicitHeaderMode();
374395

@@ -640,17 +661,28 @@ void LoRaClass::handleDio0Rise()
640661
writeRegister(REG_IRQ_FLAGS, irqFlags);
641662

642663
if ((irqFlags & IRQ_PAYLOAD_CRC_ERROR_MASK) == 0) {
643-
// received a packet
644-
_packetIndex = 0;
645664

646-
// read packet length
647-
int packetLength = _implicitHeaderMode ? readRegister(REG_PAYLOAD_LENGTH) : readRegister(REG_RX_NB_BYTES);
665+
if ((irqFlags & IRQ_RX_DONE_MASK) != 0) {
666+
// received a packet
667+
_packetIndex = 0;
648668

649-
// set FIFO address to current RX address
650-
writeRegister(REG_FIFO_ADDR_PTR, readRegister(REG_FIFO_RX_CURRENT_ADDR));
669+
// read packet length
670+
int packetLength = _implicitHeaderMode ? readRegister(REG_PAYLOAD_LENGTH) : readRegister(REG_RX_NB_BYTES);
651671

652-
if (_onReceive) {
653-
_onReceive(packetLength);
672+
// set FIFO address to current RX address
673+
writeRegister(REG_FIFO_ADDR_PTR, readRegister(REG_FIFO_RX_CURRENT_ADDR));
674+
675+
if (_onReceive) {
676+
_onReceive(packetLength);
677+
}
678+
679+
// reset FIFO address
680+
writeRegister(REG_FIFO_ADDR_PTR, 0);
681+
}
682+
else if ((irqFlags & IRQ_TX_DONE_MASK) != 0) {
683+
if (_onTxDone) {
684+
_onTxDone();
685+
}
654686
}
655687
}
656688
}

src/LoRa.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class LoRaClass : public Stream {
5757

5858
#ifndef ARDUINO_SAMD_MKRWAN1300
5959
void onReceive(void(*callback)(int));
60+
void onTxDone(void(*callback)());
6061

6162
void receive(int size = 0);
6263
#endif
@@ -117,6 +118,7 @@ class LoRaClass : public Stream {
117118
int _packetIndex;
118119
int _implicitHeaderMode;
119120
void (*_onReceive)(int);
121+
void (*_onTxDone)();
120122
};
121123

122124
extern LoRaClass LoRa;

0 commit comments

Comments
 (0)