From 8f3b33c84c06ec0c9ae0e576b87263fc78ff7ae6 Mon Sep 17 00:00:00 2001 From: geeksville Date: Thu, 23 Apr 2020 16:55:25 -0700 Subject: [PATCH] use a real macaddr on the nrf52 --- docs/software/nrf52-TODO.md | 54 ++++++++++++++++++++++++++++++++++++- src/bare/main-bare.cpp | 3 ++- src/bare/main-nrf52.cpp | 21 +++++++++++++++ src/esp32/main-esp32.cpp | 5 ++++ src/main.cpp | 16 +---------- src/main.h | 2 ++ 6 files changed, 84 insertions(+), 17 deletions(-) diff --git a/docs/software/nrf52-TODO.md b/docs/software/nrf52-TODO.md index 2606684c..88720e0f 100644 --- a/docs/software/nrf52-TODO.md +++ b/docs/software/nrf52-TODO.md @@ -4,4 +4,56 @@ #define PIN_SPI_MOSI (45) #define PIN_SPI_SCK (47) #define PIN_WIRE_SDA (26) -#define PIN_WIRE_SCL (27) \ No newline at end of file +#define PIN_WIRE_SCL (27) + + +/* +per +https://docs.platformio.org/en/latest/tutorials/nordicnrf52/arduino_debugging_unit_testing.html + +ardunino github is here https://github.com/sandeepmistry/arduino-nRF5 +devboard hw docs here: +https://infocenter.nordicsemi.com/topic/ug_nrf52840_dk/UG/nrf52840_DK/hw_buttons_leds.html?cp=4_0_4_7_6 + +https://docs.platformio.org/en/latest/boards/nordicnrf52/nrf52840_dk_adafruit.html + +must install adafruit bootloader first! +https://learn.adafruit.com/circuitpython-on-the-nrf52/nrf52840-bootloader +see link above and turn off jlink filesystem if we see unreliable serial comms +over USBCDC + +adafruit bootloader install commands (from their readme) +kevinh@kevin-server:~/.platformio/packages/framework-arduinoadafruitnrf52/bootloader$ +nrfjprog -e -f nrf52 Erasing user available code and UICR flash areas. Applying +system reset. +kevinh@kevin-server:~/.platformio/packages/framework-arduinoadafruitnrf52/bootloader$ +nrfjprog --program pca10056/pca10056_bootloader-0.3.2_s140_6.1.1.hex -f nrf52 +Parsing hex file. +Reading flash area to program to guarantee it is erased. +Checking that the area to write is not protected. +Programming device. +kevinh@kevin-server:~/.platformio/packages/framework-arduinoadafruitnrf52/bootloader$ +nrfjprog --reset -f nrf52 Applying system reset. Run. + +install jlink tools from here: +https://www.segger.com/downloads/jlink#J-LinkSoftwareAndDocumentationPack + +install nrf tools from here: +https://www.nordicsemi.com/Software-and-tools/Development-Tools/nRF-Command-Line-Tools/Download#infotabs + +examples of turning off the loop call to save power: +https://learn.adafruit.com/bluefruit-nrf52-feather-learning-guide/advertising-beacon + +example of a more complex BLE service: +https://learn.adafruit.com/bluefruit-nrf52-feather-learning-guide/custom-hrm +*/ + +// See g_ADigitalPinMap to see how arduino maps to the real gpio#s - and all in +// P0 +#define LED1 14 +#define LED2 13 + +/* +good led ble demo: +https://github.com/adafruit/Adafruit_nRF52_Arduino/blob/master/libraries/Bluefruit52Lib/examples/Peripheral/nrf_blinky/nrf_blinky.ino +*/ \ No newline at end of file diff --git a/src/bare/main-bare.cpp b/src/bare/main-bare.cpp index 29532a63..d327c238 100644 --- a/src/bare/main-bare.cpp +++ b/src/bare/main-bare.cpp @@ -3,4 +3,5 @@ void setBluetoothEnable(bool on) { // Do nothing -} \ No newline at end of file +} + diff --git a/src/bare/main-nrf52.cpp b/src/bare/main-nrf52.cpp index 1ccb7961..2c4bcbc1 100644 --- a/src/bare/main-nrf52.cpp +++ b/src/bare/main-nrf52.cpp @@ -1,4 +1,9 @@ #include +#include +#include +#include + +// #define USE_SOFTDEVICE static inline void debugger_break(void) { @@ -10,4 +15,20 @@ static inline void debugger_break(void) void __attribute__((noreturn)) __assert_func(const char *file, int line, const char *func, const char *failedexpr) { debugger_break(); + while (1) + ; +} + +void getMacAddr(uint8_t *dmac) +{ + ble_gap_addr_t addr; + +#ifdef USE_SOFTDEVICE + uint32_t res = sd_ble_gap_addr_get(&addr); + assert(res == NRF_SUCCESS); + memcpy(dmac, addr.addr, 6); +#else + // FIXME - byte order might be wrong and high bits might be wrong + memcpy(dmac, (const void *)NRF_FICR->DEVICEADDR, 6); +#endif } \ No newline at end of file diff --git a/src/esp32/main-esp32.cpp b/src/esp32/main-esp32.cpp index baf0d85b..e0423156 100644 --- a/src/esp32/main-esp32.cpp +++ b/src/esp32/main-esp32.cpp @@ -50,4 +50,9 @@ void setBluetoothEnable(bool on) // heap_trace_dump(); } } +} + +void getMacAddr(uint8_t *dmac) +{ + assert(esp_efuse_mac_get_default(dmac) == ESP_OK); } \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index c25b2a9c..37bf0b14 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -32,6 +32,7 @@ #include "power.h" // #include "rom/rtc.h" #include "FloodingRouter.h" +#include "main.h" #include "screen.h" #include "sleep.h" #include @@ -192,20 +193,6 @@ void axp192Init() #endif } -void getMacAddr(uint8_t *dmac) -{ -#ifndef NO_ESP32 - assert(esp_efuse_mac_get_default(dmac) == ESP_OK); -#else - dmac[0] = 0xde; - dmac[1] = 0xad; - dmac[2] = 0xbe; - dmac[3] = 0xef; - dmac[4] = 0x01; - dmac[5] = 0x02; // FIXME, macaddr stuff needed for NRF52 -#endif -} - const char *getDeviceName() { uint8_t dmac[6]; @@ -248,7 +235,6 @@ void setup() #endif scanI2Cdevice(); - // Buttons & LED #ifdef BUTTON_PIN pinMode(BUTTON_PIN, INPUT_PULLUP); diff --git a/src/main.h b/src/main.h index c8c37971..2a29da59 100644 --- a/src/main.h +++ b/src/main.h @@ -12,3 +12,5 @@ extern meshtastic::Screen screen; // Return a human readable string of the form "Meshtastic_ab13" const char *getDeviceName(); + +void getMacAddr(uint8_t *dmac); \ No newline at end of file