Skip to content

Commit ca5dcf2

Browse files
authored
Merge pull request #911 from bplein/ikoka-nano-support
Ikoka Nano Variant
2 parents 86ecf97 + b588e3f commit ca5dcf2

File tree

7 files changed

+773
-0
lines changed

7 files changed

+773
-0
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#ifdef XIAO_NRF52
2+
3+
#include <Arduino.h>
4+
#include "IkokaNanoNRFBoard.h"
5+
6+
#include <bluefruit.h>
7+
#include <Wire.h>
8+
9+
static BLEDfu bledfu;
10+
11+
static void connect_callback(uint16_t conn_handle) {
12+
(void)conn_handle;
13+
MESH_DEBUG_PRINTLN("BLE client connected");
14+
}
15+
16+
static void disconnect_callback(uint16_t conn_handle, uint8_t reason) {
17+
(void)conn_handle;
18+
(void)reason;
19+
20+
MESH_DEBUG_PRINTLN("BLE client disconnected");
21+
}
22+
23+
void IkokaNanoNRFBoard::begin() {
24+
// for future use, sub-classes SHOULD call this from their begin()
25+
startup_reason = BD_STARTUP_NORMAL;
26+
27+
pinMode(PIN_VBAT, INPUT);
28+
pinMode(VBAT_ENABLE, OUTPUT);
29+
digitalWrite(VBAT_ENABLE, HIGH);
30+
31+
#ifdef PIN_USER_BTN
32+
pinMode(PIN_USER_BTN, INPUT_PULLUP);
33+
#endif
34+
35+
#if defined(PIN_WIRE_SDA) && defined(PIN_WIRE_SCL)
36+
Wire.setPins(PIN_WIRE_SDA, PIN_WIRE_SCL);
37+
#endif
38+
39+
Wire.begin();
40+
41+
#ifdef P_LORA_TX_LED
42+
pinMode(P_LORA_TX_LED, OUTPUT);
43+
digitalWrite(P_LORA_TX_LED, HIGH);
44+
#endif
45+
46+
// pinMode(SX126X_POWER_EN, OUTPUT);
47+
// digitalWrite(SX126X_POWER_EN, HIGH);
48+
delay(10); // give sx1262 some time to power up
49+
}
50+
51+
bool IkokaNanoNRFBoard::startOTAUpdate(const char *id, char reply[]) {
52+
// Config the peripheral connection with maximum bandwidth
53+
// more SRAM required by SoftDevice
54+
// Note: All config***() function must be called before begin()
55+
Bluefruit.configPrphBandwidth(BANDWIDTH_MAX);
56+
Bluefruit.configPrphConn(92, BLE_GAP_EVENT_LENGTH_MIN, 16, 16);
57+
58+
Bluefruit.begin(1, 0);
59+
// Set max power. Accepted values are: -40, -30, -20, -16, -12, -8, -4, 0, 4
60+
Bluefruit.setTxPower(4);
61+
// Set the BLE device name
62+
Bluefruit.setName("XIAO_NRF52_OTA");
63+
64+
Bluefruit.Periph.setConnectCallback(connect_callback);
65+
Bluefruit.Periph.setDisconnectCallback(disconnect_callback);
66+
67+
// To be consistent OTA DFU should be added first if it exists
68+
bledfu.begin();
69+
70+
// Set up and start advertising
71+
// Advertising packet
72+
Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
73+
Bluefruit.Advertising.addTxPower();
74+
Bluefruit.Advertising.addName();
75+
76+
/* Start Advertising
77+
- Enable auto advertising if disconnected
78+
- Interval: fast mode = 20 ms, slow mode = 152.5 ms
79+
- Timeout for fast mode is 30 seconds
80+
- Start(timeout) with timeout = 0 will advertise forever (until connected)
81+
82+
For recommended advertising interval
83+
https://developer.apple.com/library/content/qa/qa1931/_index.html
84+
*/
85+
Bluefruit.Advertising.restartOnDisconnect(true);
86+
Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms
87+
Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode
88+
Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds
89+
90+
strcpy(reply, "OK - started");
91+
return true;
92+
}
93+
94+
#endif
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#pragma once
2+
3+
#include <MeshCore.h>
4+
#include <Arduino.h>
5+
6+
#ifdef XIAO_NRF52
7+
8+
class IkokaNanoNRFBoard : public mesh::MainBoard {
9+
protected:
10+
uint8_t startup_reason;
11+
12+
public:
13+
void begin();
14+
uint8_t getStartupReason() const override { return startup_reason; }
15+
16+
#if defined(P_LORA_TX_LED)
17+
void onBeforeTransmit() override {
18+
digitalWrite(P_LORA_TX_LED, LOW); // turn TX LED on
19+
#if defined(LED_BLUE)
20+
// turn off that annoying blue LED before transmitting
21+
digitalWrite(LED_BLUE, HIGH);
22+
#endif
23+
}
24+
void onAfterTransmit() override {
25+
digitalWrite(P_LORA_TX_LED, HIGH); // turn TX LED off
26+
#if defined(LED_BLUE)
27+
// do it after transmitting too, just in case
28+
digitalWrite(LED_BLUE, HIGH);
29+
#endif
30+
}
31+
#endif
32+
33+
uint16_t getBattMilliVolts() override {
34+
// Please read befor going further ;)
35+
// https://wiki.seeedstudio.com/XIAO_BLE#q3-what-are-the-considerations-when-using-xiao-nrf52840-sense-for-battery-charging
36+
37+
// We can't drive VBAT_ENABLE to HIGH as long
38+
// as we don't know wether we are charging or not ...
39+
// this is a 3mA loss (4/1500)
40+
digitalWrite(VBAT_ENABLE, LOW);
41+
int adcvalue = 0;
42+
analogReadResolution(12);
43+
analogReference(AR_INTERNAL_3_0);
44+
delay(10);
45+
adcvalue = analogRead(PIN_VBAT);
46+
return (adcvalue * ADC_MULTIPLIER * AREF_VOLTAGE) / 4.096;
47+
}
48+
49+
const char *getManufacturerName() const override {
50+
return MANUFACTURER_STRING;
51+
}
52+
53+
void reboot() override {
54+
NVIC_SystemReset();
55+
}
56+
57+
bool startOTAUpdate(const char *id, char reply[]) override;
58+
};
59+
60+
#endif

0 commit comments

Comments
 (0)