2020-04-24 01:02:28 +00:00
|
|
|
#include "NRF52Bluetooth.h"
|
|
|
|
#include "configuration.h"
|
2020-09-26 16:40:48 +00:00
|
|
|
#include "graphics/TFTDisplay.h"
|
2020-04-23 19:47:41 +00:00
|
|
|
#include <assert.h>
|
2020-04-23 23:55:25 +00:00
|
|
|
#include <ble_gap.h>
|
|
|
|
#include <memory.h>
|
2020-07-17 16:14:23 +00:00
|
|
|
#include <stdio.h>
|
2020-07-10 04:27:34 +00:00
|
|
|
|
|
|
|
#ifdef NRF52840_XXAA
|
|
|
|
// #include <nrf52840.h>
|
|
|
|
#endif
|
2020-04-23 23:55:25 +00:00
|
|
|
|
|
|
|
// #define USE_SOFTDEVICE
|
2020-04-23 19:47:41 +00:00
|
|
|
|
|
|
|
static inline void debugger_break(void)
|
|
|
|
{
|
|
|
|
__asm volatile("bkpt #0x01\n\t"
|
|
|
|
"mov pc, lr\n\t");
|
|
|
|
}
|
|
|
|
|
|
|
|
// handle standard gcc assert failures
|
|
|
|
void __attribute__((noreturn)) __assert_func(const char *file, int line, const char *func, const char *failedexpr)
|
|
|
|
{
|
2020-04-24 16:33:45 +00:00
|
|
|
DEBUG_MSG("assert failed %s: %d, %s, test=%s\n", file, line, func, failedexpr);
|
2020-04-30 17:00:40 +00:00
|
|
|
// debugger_break(); FIXME doesn't work, possibly not for segger
|
2020-04-23 23:55:25 +00:00
|
|
|
while (1)
|
2020-04-24 16:33:45 +00:00
|
|
|
; // FIXME, reboot!
|
2020-04-23 23:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2020-04-24 01:06:46 +00:00
|
|
|
const uint8_t *src = (const uint8_t *)NRF_FICR->DEVICEADDR;
|
|
|
|
dmac[5] = src[0];
|
|
|
|
dmac[4] = src[1];
|
|
|
|
dmac[3] = src[2];
|
|
|
|
dmac[2] = src[3];
|
|
|
|
dmac[1] = src[4];
|
|
|
|
dmac[0] = src[5] | 0xc0; // MSB high two bits get set elsewhere in the bluetooth stack
|
2020-04-23 23:55:25 +00:00
|
|
|
#endif
|
2020-04-24 01:02:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NRF52Bluetooth *nrf52Bluetooth;
|
|
|
|
|
|
|
|
static bool bleOn = false;
|
2020-09-24 23:36:07 +00:00
|
|
|
static const bool enableBle = false; // Set to false for easier debugging
|
|
|
|
|
2020-04-24 01:02:28 +00:00
|
|
|
void setBluetoothEnable(bool on)
|
|
|
|
{
|
|
|
|
if (on != bleOn) {
|
|
|
|
if (on) {
|
2020-05-26 00:16:09 +00:00
|
|
|
if (!nrf52Bluetooth) {
|
2020-09-24 23:36:07 +00:00
|
|
|
if (!enableBle)
|
|
|
|
DEBUG_MSG("DISABLING NRF52 BLUETOOTH WHILE DEBUGGING\n");
|
|
|
|
else {
|
|
|
|
nrf52Bluetooth = new NRF52Bluetooth();
|
|
|
|
nrf52Bluetooth->setup();
|
|
|
|
}
|
2020-04-24 01:02:28 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
DEBUG_MSG("FIXME: implement BLE disable\n");
|
|
|
|
}
|
|
|
|
bleOn = on;
|
|
|
|
}
|
|
|
|
}
|
2020-04-24 16:33:45 +00:00
|
|
|
|
2020-07-17 16:14:23 +00:00
|
|
|
/**
|
|
|
|
* Override printf to use the SEGGER output library
|
|
|
|
*/
|
|
|
|
int printf(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
|
|
|
auto res = SEGGER_RTT_vprintf(0, fmt, &args);
|
|
|
|
va_end(args);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2020-04-24 16:33:45 +00:00
|
|
|
void nrf52Setup()
|
|
|
|
{
|
2020-05-24 23:20:21 +00:00
|
|
|
|
|
|
|
auto why = NRF_POWER->RESETREAS;
|
2020-05-24 23:34:18 +00:00
|
|
|
// per https://infocenter.nordicsemi.com/index.jsp?topic=%2Fcom.nordic.infocenter.nrf52832.ps.v1.1%2Fpower.html
|
2020-05-24 23:20:21 +00:00
|
|
|
DEBUG_MSG("Reset reason: 0x%x\n", why);
|
|
|
|
|
2020-05-25 22:56:06 +00:00
|
|
|
// Per https://devzone.nordicsemi.com/nordic/nordic-blog/b/blog/posts/monitor-mode-debugging-with-j-link-and-gdbeclipse
|
|
|
|
// This is the recommended setting for Monitor Mode Debugging
|
|
|
|
NVIC_SetPriority(DebugMonitor_IRQn, 6UL);
|
|
|
|
|
2020-09-24 23:36:07 +00:00
|
|
|
#ifdef PIN_PWR_ON
|
|
|
|
digitalWrite(PIN_PWR_ON, HIGH); // If we need to assert a pin to power external peripherals
|
|
|
|
pinMode(PIN_PWR_ON, OUTPUT);
|
|
|
|
#endif
|
|
|
|
|
2020-04-24 16:33:45 +00:00
|
|
|
// Not yet on board
|
|
|
|
// pmu.init();
|
2020-05-27 22:31:32 +00:00
|
|
|
|
|
|
|
// Init random seed
|
|
|
|
// FIXME - use this to get random numbers
|
|
|
|
// #include "nrf_rng.h"
|
|
|
|
// uint32_t r;
|
|
|
|
// ble_controller_rand_vector_get_blocking(&r, sizeof(r));
|
|
|
|
// randomSeed(r);
|
|
|
|
DEBUG_MSG("FIXME, call randomSeed\n");
|
2020-07-17 16:14:23 +00:00
|
|
|
// ::printf("TESTING PRINTF\n");
|
2020-04-24 16:33:45 +00:00
|
|
|
}
|