IkokaNanoNRFBoard.cpp 2.7 KB

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