target.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #include <Arduino.h>
  2. #include "target.h"
  3. #include <helpers/sensors/MicroNMEALocationProvider.h>
  4. ThinkNodeM3Board 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. #ifdef ENV_INCLUDE_GPS
  10. MicroNMEALocationProvider nmea = MicroNMEALocationProvider(Serial1, &rtc_clock);
  11. EnvironmentSensorManager sensors = EnvironmentSensorManager(nmea);
  12. #else
  13. EnvironmentSensorManager sensors = EnvironmentSensorManager();
  14. #endif
  15. #ifdef DISPLAY_CLASS
  16. NullDisplayDriver display;
  17. #endif
  18. #ifndef LORA_CR
  19. #define LORA_CR 5
  20. #endif
  21. #ifdef RF_SWITCH_TABLE
  22. static const uint32_t rfswitch_dios[Module::RFSWITCH_MAX_PINS] = {
  23. RADIOLIB_LR11X0_DIO5,
  24. RADIOLIB_LR11X0_DIO6,
  25. RADIOLIB_NC,
  26. RADIOLIB_NC,
  27. RADIOLIB_NC
  28. };
  29. static const Module::RfSwitchMode_t rfswitch_table[] = {
  30. // mode DIO5 DIO6
  31. { LR11x0::MODE_STBY, {LOW , LOW }},
  32. { LR11x0::MODE_RX, {HIGH, LOW }},
  33. { LR11x0::MODE_TX, {HIGH, HIGH }},
  34. { LR11x0::MODE_TX_HP, {LOW , HIGH }},
  35. { LR11x0::MODE_TX_HF, {LOW , LOW }},
  36. { LR11x0::MODE_GNSS, {LOW , LOW }},
  37. { LR11x0::MODE_WIFI, {LOW , LOW }},
  38. END_OF_MODE_TABLE,
  39. };
  40. #endif
  41. bool radio_init() {
  42. rtc_clock.begin(Wire);
  43. #ifdef LR11X0_DIO3_TCXO_VOLTAGE
  44. float tcxo = LR11X0_DIO3_TCXO_VOLTAGE;
  45. #else
  46. float tcxo = 1.6f;
  47. #endif
  48. SPI.setPins(P_LORA_MISO, P_LORA_SCLK, P_LORA_MOSI);
  49. SPI.begin();
  50. int status = radio.begin(LORA_FREQ, LORA_BW, LORA_SF, LORA_CR, RADIOLIB_LR11X0_LORA_SYNC_WORD_PRIVATE, LORA_TX_POWER, 16, tcxo);
  51. if (status != RADIOLIB_ERR_NONE) {
  52. Serial.print("ERROR: radio init failed: ");
  53. Serial.println(status);
  54. return false; // fail
  55. }
  56. radio.setCRC(2);
  57. radio.explicitHeader();
  58. #ifdef RF_SWITCH_TABLE
  59. radio.setRfSwitchTable(rfswitch_dios, rfswitch_table);
  60. #endif
  61. #ifdef RX_BOOSTED_GAIN
  62. radio.setRxBoostedGainMode(RX_BOOSTED_GAIN);
  63. #endif
  64. return true; // success
  65. }
  66. uint32_t radio_get_rng_seed() {
  67. return radio.random(0x7FFFFFFF);
  68. }
  69. void radio_set_params(float freq, float bw, uint8_t sf, uint8_t cr) {
  70. radio.setFrequency(freq);
  71. radio.setSpreadingFactor(sf);
  72. radio.setBandwidth(bw);
  73. radio.setCodingRate(cr);
  74. }
  75. void radio_set_tx_power(int8_t dbm) {
  76. radio.setOutputPower(dbm);
  77. }
  78. mesh::LocalIdentity radio_new_identity() {
  79. RadioNoiseListener rng(radio);
  80. return mesh::LocalIdentity(&rng); // create new random identity
  81. }