target.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #include <Arduino.h>
  2. #include "target.h"
  3. #include <helpers/ArduinoHelpers.h>
  4. RAK4631Board board;
  5. RADIO_CLASS radio = new Module(P_LORA_NSS, P_LORA_DIO_1, P_LORA_RESET, P_LORA_BUSY, SPI);
  6. WRAPPER_CLASS radio_driver(radio, board);
  7. VolatileRTCClock fallback_clock;
  8. AutoDiscoverRTCClock rtc_clock(fallback_clock);
  9. SensorManager sensors;
  10. #ifdef DISPLAY_CLASS
  11. DISPLAY_CLASS display;
  12. #endif
  13. #ifndef LORA_CR
  14. #define LORA_CR 5
  15. #endif
  16. bool radio_init() {
  17. rtc_clock.begin(Wire);
  18. #ifdef SX126X_DIO3_TCXO_VOLTAGE
  19. float tcxo = SX126X_DIO3_TCXO_VOLTAGE;
  20. #else
  21. float tcxo = 1.6f;
  22. #endif
  23. SPI.setPins(P_LORA_MISO, P_LORA_SCLK, P_LORA_MOSI);
  24. SPI.begin();
  25. int status = radio.begin(LORA_FREQ, LORA_BW, LORA_SF, LORA_CR, RADIOLIB_SX126X_SYNC_WORD_PRIVATE, LORA_TX_POWER, 8, tcxo);
  26. if (status != RADIOLIB_ERR_NONE) {
  27. Serial.print("ERROR: radio init failed: ");
  28. Serial.println(status);
  29. return false; // fail
  30. }
  31. radio.setCRC(1);
  32. #ifdef SX126X_CURRENT_LIMIT
  33. radio.setCurrentLimit(SX126X_CURRENT_LIMIT);
  34. #endif
  35. #ifdef SX126X_DIO2_AS_RF_SWITCH
  36. radio.setDio2AsRfSwitch(SX126X_DIO2_AS_RF_SWITCH);
  37. #endif
  38. #ifdef SX126X_RX_BOOSTED_GAIN
  39. radio.setRxBoostedGainMode(SX126X_RX_BOOSTED_GAIN);
  40. #endif
  41. return true; // success
  42. }
  43. uint32_t radio_get_rng_seed() {
  44. return radio.random(0x7FFFFFFF);
  45. }
  46. void radio_set_params(float freq, float bw, uint8_t sf, uint8_t cr) {
  47. radio.setFrequency(freq);
  48. radio.setSpreadingFactor(sf);
  49. radio.setBandwidth(bw);
  50. radio.setCodingRate(cr);
  51. }
  52. void radio_set_tx_power(uint8_t dbm) {
  53. radio.setOutputPower(dbm);
  54. }
  55. mesh::LocalIdentity radio_new_identity() {
  56. RadioNoiseListener rng(radio);
  57. return mesh::LocalIdentity(&rng); // create new random identity
  58. }