IkokaNrf52Board.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #ifdef IKOKA_NRF52
  2. #include <Arduino.h>
  3. #include <Wire.h>
  4. #include <bluefruit.h>
  5. #include "IkokaNrf52Board.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 IkokaNrf52Board::begin() {
  17. // for future use, sub-classes SHOULD call this from their begin()
  18. startup_reason = BD_STARTUP_NORMAL;
  19. // ensure we have pull ups on the screen i2c, this isn't always available
  20. // in hardware and it should only be 20k ohms. Disable the pullups if we
  21. // are using the rotated lcd breakout board
  22. #if defined(DISPLAY_CLASS) && DISPLAY_ROTATION == 0
  23. pinMode(PIN_WIRE_SDA, INPUT_PULLUP);
  24. pinMode(PIN_WIRE_SCL, INPUT_PULLUP);
  25. #endif
  26. pinMode(PIN_VBAT, INPUT);
  27. pinMode(VBAT_ENABLE, OUTPUT);
  28. digitalWrite(VBAT_ENABLE, HIGH);
  29. // required button pullup is handled as part of button initilization
  30. // in target.cpp
  31. #if defined(PIN_WIRE_SDA) && defined(PIN_WIRE_SCL)
  32. Wire.setPins(PIN_WIRE_SDA, PIN_WIRE_SCL);
  33. #endif
  34. Wire.begin();
  35. #ifdef P_LORA_TX_LED
  36. pinMode(P_LORA_TX_LED, OUTPUT);
  37. digitalWrite(P_LORA_TX_LED, HIGH);
  38. #endif
  39. delay(10); // give sx1262 some time to power up
  40. }
  41. bool IkokaNrf52Board::startOTAUpdate(const char *id, char reply[]) {
  42. // Config the peripheral connection with maximum bandwidth
  43. // more SRAM required by SoftDevice
  44. // Note: All config***() function must be called before begin()
  45. Bluefruit.configPrphBandwidth(BANDWIDTH_MAX);
  46. Bluefruit.configPrphConn(92, BLE_GAP_EVENT_LENGTH_MIN, 16, 16);
  47. Bluefruit.begin(1, 0);
  48. // Set max power. Accepted values are: -40, -30, -20, -16, -12, -8, -4, 0, 4
  49. Bluefruit.setTxPower(4);
  50. // Set the BLE device name
  51. Bluefruit.setName("XIAO_NRF52_OTA");
  52. Bluefruit.Periph.setConnectCallback(connect_callback);
  53. Bluefruit.Periph.setDisconnectCallback(disconnect_callback);
  54. // To be consistent OTA DFU should be added first if it exists
  55. bledfu.begin();
  56. // Set up and start advertising
  57. // Advertising packet
  58. Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
  59. Bluefruit.Advertising.addTxPower();
  60. Bluefruit.Advertising.addName();
  61. /* Start Advertising
  62. - Enable auto advertising if disconnected
  63. - Interval: fast mode = 20 ms, slow mode = 152.5 ms
  64. - Timeout for fast mode is 30 seconds
  65. - Start(timeout) with timeout = 0 will advertise forever (until connected)
  66. For recommended advertising interval
  67. https://developer.apple.com/library/content/qa/qa1931/_index.html
  68. */
  69. Bluefruit.Advertising.restartOnDisconnect(true);
  70. Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms
  71. Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode
  72. Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds
  73. strcpy(reply, "OK - started");
  74. return true;
  75. }
  76. #endif