XiaoNrf52Board.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #ifdef XIAO_NRF52
  2. #include <Arduino.h>
  3. #include "XiaoNrf52Board.h"
  4. #include <bluefruit.h>
  5. #include <Wire.h>
  6. static BLEDfu bledfu;
  7. static void connect_callback(uint16_t conn_handle)
  8. {
  9. (void)conn_handle;
  10. MESH_DEBUG_PRINTLN("BLE client connected");
  11. }
  12. static void disconnect_callback(uint16_t conn_handle, uint8_t reason)
  13. {
  14. (void)conn_handle;
  15. (void)reason;
  16. MESH_DEBUG_PRINTLN("BLE client disconnected");
  17. }
  18. void XiaoNrf52Board::begin() {
  19. // for future use, sub-classes SHOULD call this from their begin()
  20. startup_reason = BD_STARTUP_NORMAL;
  21. pinMode(PIN_VBAT, INPUT);
  22. pinMode(VBAT_ENABLE, OUTPUT);
  23. digitalWrite(VBAT_ENABLE, HIGH);
  24. #if defined(PIN_WIRE_SDA) && defined(PIN_WIRE_SCL)
  25. Wire.setPins(PIN_WIRE_SDA, PIN_WIRE_SCL);
  26. #endif
  27. Wire.begin();
  28. #ifdef P_LORA_TX_LED
  29. pinMode(P_LORA_TX_LED, OUTPUT);
  30. digitalWrite(P_LORA_TX_LED, HIGH);
  31. #endif
  32. // pinMode(SX126X_POWER_EN, OUTPUT);
  33. // digitalWrite(SX126X_POWER_EN, HIGH);
  34. delay(10); // give sx1262 some time to power up
  35. }
  36. bool XiaoNrf52Board::startOTAUpdate(const char* id, char reply[]) {
  37. // Config the peripheral connection with maximum bandwidth
  38. // more SRAM required by SoftDevice
  39. // Note: All config***() function must be called before begin()
  40. Bluefruit.configPrphBandwidth(BANDWIDTH_MAX);
  41. Bluefruit.configPrphConn(92, BLE_GAP_EVENT_LENGTH_MIN, 16, 16);
  42. Bluefruit.begin(1, 0);
  43. // Set max power. Accepted values are: -40, -30, -20, -16, -12, -8, -4, 0, 4
  44. Bluefruit.setTxPower(4);
  45. // Set the BLE device name
  46. Bluefruit.setName("XIAO_NRF52_OTA");
  47. Bluefruit.Periph.setConnectCallback(connect_callback);
  48. Bluefruit.Periph.setDisconnectCallback(disconnect_callback);
  49. // To be consistent OTA DFU should be added first if it exists
  50. bledfu.begin();
  51. // Set up and start advertising
  52. // Advertising packet
  53. Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
  54. Bluefruit.Advertising.addTxPower();
  55. Bluefruit.Advertising.addName();
  56. /* Start Advertising
  57. - Enable auto advertising if disconnected
  58. - Interval: fast mode = 20 ms, slow mode = 152.5 ms
  59. - Timeout for fast mode is 30 seconds
  60. - Start(timeout) with timeout = 0 will advertise forever (until connected)
  61. For recommended advertising interval
  62. https://developer.apple.com/library/content/qa/qa1931/_index.html
  63. */
  64. Bluefruit.Advertising.restartOnDisconnect(true);
  65. Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms
  66. Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode
  67. Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds
  68. strcpy(reply, "OK - started");
  69. return true;
  70. return false;
  71. }
  72. #endif