adafruit / TinyLoRa

LoRaWAN Library
69 stars 38 forks source link

Channel and Datarate Selection #10

Closed brentru closed 6 years ago

brentru commented 6 years ago

Adding support for setting single channel (CH1, CH2, ...) and multi-channel (MULTI) using lora.setChannel()

Adding support for setting the data-rate (spreading factor, bandwidth, modem configuration) using lora.setDataRate()

Tested on Feather 32U4

Addressing: https://github.com/adafruit/TinyLoRa/issues/6

jerryneedell commented 6 years ago

I connected a 32u4 adalogger board to my RFM95 breakout so I can test thied code unmodified (onthe the pinouts chaunge I use (11,12) instest of (7,8) With this PR implmented, the hello_LoRa (multichannel) example works fine witht eh one thing I noted before. Only the first tramsmitted packet contains the initialized data since the loraData array is overwritten by the encrypted values each time the packet is transmitted .

The single channel example does not workk for me -- In the serial output, I see that it initializes and sends a packet, but it then appears to hang while sendig the packed.

Starting LoRa...
SPI Write ADDR: 1 DATA: 0
SPI Write ADDR: 1 DATA: 80
SPI Write ADDR: 9 DATA: FF
SPI Write ADDR: 1F DATA: 25
SPI Write ADDR: 20 DATA: 0
SPI Write ADDR: 21 DATA: 8
SPI Write ADDR: 26 DATA: C
SPI Write ADDR: 39 DATA: 34
SPI Write ADDR: 33 DATA: 27
SPI Write ADDR: 3B DATA: 1D
SPI Write ADDR: E DATA: 80
SPI Write ADDR: F DATA: 0
> RFM module initialized
Sending LoRa Data...
Package length: 20
SPI Write ADDR: 1 DATA: 81
SPI Write ADDR: 40 DATA: 40
SPI Write ADDR: 6 DATA: 59
SPI Write ADDR: 7 DATA: 13
SPI Write ADDR: 8 DATA: E2
SPI Write ADDR: 1E DATA: 74
SPI Write ADDR: 1D DATA: 72
SPI Write ADDR: 26 DATA: 4
SPI Write ADDR: 22 DATA: 18
SPI Write ADDR: D DATA: 80
SPI Write ADDR: 0 DATA: 40
SPI Write ADDR: 0 DATA: C7
SPI Write ADDR: 0 DATA: 19
SPI Write ADDR: 0 DATA: 2
SPI Write ADDR: 0 DATA: 26
SPI Write ADDR: 0 DATA: 0
SPI Write ADDR: 0 DATA: 0
SPI Write ADDR: 0 DATA: 0
SPI Write ADDR: 0 DATA: 1
SPI Write ADDR: 0 DATA: EF
SPI Write ADDR: 0 DATA: C3
SPI Write ADDR: 0 DATA: 81
SPI Write ADDR: 0 DATA: FF
SPI Write ADDR: 0 DATA: 64
SPI Write ADDR: 0 DATA: 3A
SPI Write ADDR: 0 DATA: 28
SPI Write ADDR: 0 DATA: BE
SPI Write ADDR: 0 DATA: E1
SPI Write ADDR: 0 DATA: 5D
SPI Write ADDR: 0 DATA: 8A
SPI Write ADDR: 0 DATA: C5
SPI Write ADDR: 0 DATA: 10
SPI Write ADDR: 0 DATA: 8A
SPI Write ADDR: 0 DATA: 50
SPI Write ADDR: 1 DATA: 83   hangs here

This was the same behavior I noted on the M0 but I wanted to try it on a 32U4 so I did not have to make any code changes.

jerryneedell commented 6 years ago

a bit of good news -- If I just remove the "static" from the static const ... PROGMEM definitions in TinyLora.cpp then the existing code compiles for both the 32U4 and the M0. The PROGMEM and pgm_read_byte calls are apparently implemented (and meaningless) on the M0 The change had not apparent impact on the 32U4 compilations or file size. The M0 file size i much larger 14K vs 7k for the 32U4....but the M0 has lots of room.

jerryneedell commented 6 years ago

I've narrowed down the issue in single channel mode -- For me , it is hanging at this line: https://github.com/adafruit/TinyLoRa/blob/293ae72ad112fc96eeb9a5c9069ef36052216154/TinyLoRa.cpp#L355

No idea why -- multi-channel mode works fine....

jerryneedell commented 6 years ago

also - shouldn't _irq be an INPUT ? https://github.com/adafruit/TinyLoRa/blob/293ae72ad112fc96eeb9a5c9069ef36052216154/TinyLoRa.cpp#L256

Actually it does not appear to make any difference but I think it should be an input.

jerryneedell commented 6 years ago

ah foundit: for the single channel definitions the byte orders are reversed: https://github.com/adafruit/TinyLoRa/blob/293ae72ad112fc96eeb9a5c9069ef36052216154/TinyLoRa.cpp#L185 ah found it: for the single channel definitions the byte orders are reversed:

    case CH2:
      _rfmLSB = pgm_read_byte(&(LoRa_Frequency[2][0]));
      _rfmMID = pgm_read_byte(&(LoRa_Frequency[2][1]));
      _rfmMSB = pgm_read_byte(&(LoRa_Frequency[2][2]));
      _isMultiChan = 0;
      break;

should be
    case CH2:
      _rfmMSB = pgm_read_byte(&(LoRa_Frequency[2][0]));
      _rfmMID = pgm_read_byte(&(LoRa_Frequency[2][1]));
      _rfmLSB = pgm_read_byte(&(LoRa_Frequency[2][2]));
      _isMultiChan = 0;
      break;

I made this chanel for CH2 and it now works. Multichannel mode is correct.

All working now.

jerryneedell commented 6 years ago

Tested on Feather M0 express -- Both single and Multichannel working OK. Theo only change I had to make was to remove the "static" from the "static const" definition in TinyLora.cpp

ladyada commented 6 years ago

thanks, ok brent lets make that change & wrap'er up :)

brentru commented 6 years ago

Tested single and multi. on Feather LoRa 32u4 and M0, looks ready to squash+merge