T114Board.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #include "T114Board.h"
  2. #include <Arduino.h>
  3. #include <Wire.h>
  4. #include <bluefruit.h>
  5. static BLEDfu bledfu;
  6. static void connect_callback(uint16_t conn_handle) {
  7. (void)conn_handle;
  8. MESH_DEBUG_PRINTLN("BLE client connected");
  9. }
  10. static void disconnect_callback(uint16_t conn_handle, uint8_t reason) {
  11. (void)conn_handle;
  12. (void)reason;
  13. MESH_DEBUG_PRINTLN("BLE client disconnected");
  14. }
  15. void T114Board::begin() {
  16. // for future use, sub-classes SHOULD call this from their begin()
  17. startup_reason = BD_STARTUP_NORMAL;
  18. pinMode(PIN_VBAT_READ, INPUT);
  19. // Enable SoftDevice low-power mode
  20. sd_power_mode_set(NRF_POWER_MODE_LOWPWR);
  21. // Enable DC/DC converter for better efficiency (REG1 stage)
  22. NRF_POWER->DCDCEN = 1;
  23. // Power down unused communication peripherals
  24. // UART1 - Not used on T114
  25. NRF_UARTE1->ENABLE = 0;
  26. // SPIM2/SPIS2 - Not used (SPI is on SPIM0)
  27. NRF_SPIM2->ENABLE = 0;
  28. NRF_SPIS2->ENABLE = 0;
  29. // TWI1 (I2C1) - Not used (I2C is on TWI0)
  30. NRF_TWIM1->ENABLE = 0;
  31. NRF_TWIS1->ENABLE = 0;
  32. // PWM modules - Not used for standard T114 functions
  33. NRF_PWM1->ENABLE = 0;
  34. NRF_PWM2->ENABLE = 0;
  35. NRF_PWM3->ENABLE = 0;
  36. // PDM (Digital Microphone Interface) - Not used
  37. NRF_PDM->ENABLE = 0;
  38. // I2S - Not used
  39. NRF_I2S->ENABLE = 0;
  40. // QSPI - Not used (no external flash)
  41. NRF_QSPI->ENABLE = 0;
  42. // Disable unused analog peripherals
  43. // SAADC channels - only keep what's needed for battery monitoring
  44. NRF_SAADC->ENABLE = 0; // Re-enable only when needed for measurements
  45. // COMP - Comparator not used
  46. NRF_COMP->ENABLE = 0;
  47. #if defined(PIN_BOARD_SDA) && defined(PIN_BOARD_SCL)
  48. Wire.setPins(PIN_BOARD_SDA, PIN_BOARD_SCL);
  49. #endif
  50. Wire.begin();
  51. #ifdef P_LORA_TX_LED
  52. pinMode(P_LORA_TX_LED, OUTPUT);
  53. digitalWrite(P_LORA_TX_LED, HIGH);
  54. #endif
  55. pinMode(SX126X_POWER_EN, OUTPUT);
  56. digitalWrite(SX126X_POWER_EN, HIGH);
  57. delay(10); // give sx1262 some time to power up
  58. }
  59. bool T114Board::startOTAUpdate(const char *id, char reply[]) {
  60. // Config the peripheral connection with maximum bandwidth
  61. // more SRAM required by SoftDevice
  62. // Note: All config***() function must be called before begin()
  63. Bluefruit.configPrphBandwidth(BANDWIDTH_MAX);
  64. Bluefruit.configPrphConn(92, BLE_GAP_EVENT_LENGTH_MIN, 16, 16);
  65. Bluefruit.begin(1, 0);
  66. // Set max power. Accepted values are: -40, -30, -20, -16, -12, -8, -4, 0, 4
  67. Bluefruit.setTxPower(4);
  68. // Set the BLE device name
  69. Bluefruit.setName("T114_OTA");
  70. Bluefruit.Periph.setConnectCallback(connect_callback);
  71. Bluefruit.Periph.setDisconnectCallback(disconnect_callback);
  72. // To be consistent OTA DFU should be added first if it exists
  73. bledfu.begin();
  74. // Set up and start advertising
  75. // Advertising packet
  76. Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
  77. Bluefruit.Advertising.addTxPower();
  78. Bluefruit.Advertising.addName();
  79. /* Start Advertising
  80. - Enable auto advertising if disconnected
  81. - Interval: fast mode = 20 ms, slow mode = 152.5 ms
  82. - Timeout for fast mode is 30 seconds
  83. - Start(timeout) with timeout = 0 will advertise forever (until connected)
  84. For recommended advertising interval
  85. https://developer.apple.com/library/content/qa/qa1931/_index.html
  86. */
  87. Bluefruit.Advertising.restartOnDisconnect(true);
  88. Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms
  89. Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode
  90. Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds
  91. strcpy(reply, "OK - started");
  92. return true;
  93. }