move nrf52 stuff to correct directory name

This commit is contained in:
geeksville
2020-04-27 07:39:50 -07:00
parent 038b7c9c91
commit 15cb599cd1
11 changed files with 18 additions and 15 deletions

3
src/nrf52/FS.h Normal file
View File

@@ -0,0 +1,3 @@
#pragma once
// FIXME - make a FS abstraction for NRF52

View File

@@ -0,0 +1,241 @@
#include "NRF52Bluetooth.h"
#include "configuration.h"
#include "main.h"
#include <bluefruit.h>
/* HRM Service Definitions
* Heart Rate Monitor Service: 0x180D
* Heart Rate Measurement Char: 0x2A37
* Body Sensor Location Char: 0x2A38
*/
BLEService hrms = BLEService(UUID16_SVC_HEART_RATE);
BLECharacteristic hrmc = BLECharacteristic(UUID16_CHR_HEART_RATE_MEASUREMENT);
BLECharacteristic bslc = BLECharacteristic(UUID16_CHR_BODY_SENSOR_LOCATION);
BLEDis bledis; // DIS (Device Information Service) helper class instance
BLEBas blebas; // BAS (Battery Service) helper class instance
BLEDfu bledfu; // DFU software update helper service
uint8_t bps = 0;
void connect_callback(uint16_t conn_handle)
{
// Get the reference to current connection
BLEConnection *connection = Bluefruit.Connection(conn_handle);
char central_name[32] = {0};
connection->getPeerName(central_name, sizeof(central_name));
DEBUG_MSG("Connected to %s\n", central_name);
}
/**
* Callback invoked when a connection is dropped
* @param conn_handle connection where this event happens
* @param reason is a BLE_HCI_STATUS_CODE which can be found in ble_hci.h
*/
void disconnect_callback(uint16_t conn_handle, uint8_t reason)
{
(void)conn_handle;
(void)reason;
DEBUG_MSG("Disconnected, reason = 0x%x\n", reason);
}
void cccd_callback(uint16_t conn_hdl, BLECharacteristic *chr, uint16_t cccd_value)
{
// Display the raw request packet
DEBUG_MSG("CCCD Updated: %u\n", cccd_value);
// Check the characteristic this CCCD update is associated with in case
// this handler is used for multiple CCCD records.
if (chr->uuid == hrmc.uuid) {
if (chr->notifyEnabled(conn_hdl)) {
DEBUG_MSG("Heart Rate Measurement 'Notify' enabled\n");
} else {
DEBUG_MSG("Heart Rate Measurement 'Notify' disabled\n");
}
}
}
void startAdv(void)
{
// Advertising packet
Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
Bluefruit.Advertising.addTxPower();
// Include HRM Service UUID
Bluefruit.Advertising.addService(hrms);
// Include Name
Bluefruit.Advertising.addName();
/* Start Advertising
* - Enable auto advertising if disconnected
* - Interval: fast mode = 20 ms, slow mode = 152.5 ms
* - Timeout for fast mode is 30 seconds
* - Start(timeout) with timeout = 0 will advertise forever (until connected)
*
* For recommended advertising interval
* https://developer.apple.com/library/content/qa/qa1931/_index.html
*/
Bluefruit.Advertising.restartOnDisconnect(true);
Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms
Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode
Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds
}
void setupHRM(void)
{
// Configure the Heart Rate Monitor service
// See: https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.service.heart_rate.xml
// Supported Characteristics:
// Name UUID Requirement Properties
// ---------------------------- ------ ----------- ----------
// Heart Rate Measurement 0x2A37 Mandatory Notify
// Body Sensor Location 0x2A38 Optional Read
// Heart Rate Control Point 0x2A39 Conditional Write <-- Not used here
hrms.begin();
// Note: You must call .begin() on the BLEService before calling .begin() on
// any characteristic(s) within that service definition.. Calling .begin() on
// a BLECharacteristic will cause it to be added to the last BLEService that
// was 'begin()'ed!
// Configure the Heart Rate Measurement characteristic
// See:
// https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.characteristic.heart_rate_measurement.xml
// Properties = Notify
// Min Len = 1
// Max Len = 8
// B0 = UINT8 - Flag (MANDATORY)
// b5:7 = Reserved
// b4 = RR-Internal (0 = Not present, 1 = Present)
// b3 = Energy expended status (0 = Not present, 1 = Present)
// b1:2 = Sensor contact status (0+1 = Not supported, 2 = Supported but contact not detected, 3 = Supported and
// detected) b0 = Value format (0 = UINT8, 1 = UINT16)
// B1 = UINT8 - 8-bit heart rate measurement value in BPM
// B2:3 = UINT16 - 16-bit heart rate measurement value in BPM
// B4:5 = UINT16 - Energy expended in joules
// B6:7 = UINT16 - RR Internal (1/1024 second resolution)
hrmc.setProperties(CHR_PROPS_NOTIFY);
hrmc.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS);
hrmc.setFixedLen(2);
hrmc.setCccdWriteCallback(cccd_callback); // Optionally capture CCCD updates
hrmc.begin();
uint8_t hrmdata[2] = {0b00000110, 0x40}; // Set the characteristic to use 8-bit values, with the sensor connected and detected
hrmc.write(hrmdata, 2);
// Configure the Body Sensor Location characteristic
// See:
// https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.characteristic.body_sensor_location.xml
// Properties = Read
// Min Len = 1
// Max Len = 1
// B0 = UINT8 - Body Sensor Location
// 0 = Other
// 1 = Chest
// 2 = Wrist
// 3 = Finger
// 4 = Hand
// 5 = Ear Lobe
// 6 = Foot
// 7:255 = Reserved
bslc.setProperties(CHR_PROPS_READ);
bslc.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS);
bslc.setFixedLen(1);
bslc.begin();
bslc.write8(2); // Set the characteristic to 'Wrist' (2)
}
void NRF52Bluetooth::setup()
{
// Initialise the Bluefruit module
DEBUG_MSG("Initialise the Bluefruit nRF52 module\n");
Bluefruit.begin();
// Set the advertised device name (keep it short!)
Bluefruit.setName(getDeviceName()); // FIXME
// Set the connect/disconnect callback handlers
Bluefruit.Periph.setConnectCallback(connect_callback);
Bluefruit.Periph.setDisconnectCallback(disconnect_callback);
// Configure and Start the Device Information Service
DEBUG_MSG("Configuring the Device Information Service\n");
bledis.setManufacturer("meshtastic.org");
bledis.setModel("NRF52-meshtastic"); // FIXME
bledis.begin();
// Start the BLE Battery Service and set it to 100%
DEBUG_MSG("Configuring the Battery Service\n");
blebas.begin();
blebas.write(42); // FIXME, report real power levels
bledfu.begin(); // Install the DFU helper
// Setup the Heart Rate Monitor service using
// BLEService and BLECharacteristic classes
DEBUG_MSG("Configuring the Heart Rate Monitor Service\n");
setupHRM();
// Setup the advertising packet(s)
DEBUG_MSG("Setting up the advertising payload(s)\n");
startAdv();
DEBUG_MSG("Advertising\n");
}
/*
void loop()
{
digitalToggle(LED_RED);
if ( Bluefruit.connected() ) {
uint8_t hrmdata[2] = { 0b00000110, bps++ }; // Sensor connected, increment BPS value
// Note: We use .notify instead of .write!
// If it is connected but CCCD is not enabled
// The characteristic's value is still updated although notification is not sent
if ( hrmc.notify(hrmdata, sizeof(hrmdata)) ){
Serial.print("Heart Rate Measurement updated to: "); Serial.println(bps);
}else{
Serial.println("ERROR: Notify not set in the CCCD or not connected!");
}
}
// Only send update once per second
delay(1000);
}
*/
/*
examples of advanced characteristics. use setReadAuthorizeCallback to prepare data for reads by others
err_t BLEDfu::begin(void)
{
// Invoke base class begin()
VERIFY_STATUS( BLEService::begin() );
// No need to keep packet & revision characteristics
BLECharacteristic chr_packet(UUID128_CHR_DFU_PACKET);
chr_packet.setTempMemory();
chr_packet.setProperties(CHR_PROPS_WRITE_WO_RESP);
chr_packet.setMaxLen(20);
VERIFY_STATUS( chr_packet.begin() );
_chr_control.setProperties(CHR_PROPS_WRITE | CHR_PROPS_NOTIFY);
_chr_control.setMaxLen(23);
_chr_control.setWriteAuthorizeCallback(bledfu_control_wr_authorize_cb);
VERIFY_STATUS( _chr_control.begin() );
BLECharacteristic chr_revision(UUID128_CHR_DFU_REVISON);
chr_revision.setTempMemory();
chr_revision.setProperties(CHR_PROPS_READ);
chr_revision.setFixedLen(2);
VERIFY_STATUS( chr_revision.begin());
chr_revision.write16(DFU_REV_APPMODE);
return ERROR_NONE;
}
*/

View File

@@ -0,0 +1,8 @@
#pragma once
class NRF52Bluetooth
{
public:
void setup();
};

97
src/nrf52/PmuBQ25703A.cpp Normal file
View File

@@ -0,0 +1,97 @@
#include "PmuBQ25703A.h"
#include <assert.h>
// Default address for device. Note, it is without read/write bit. When read with analyser,
// this will appear 1 bit shifted to the left
#define BQ25703ADevaddr 0xD6
const byte Lorro_BQ25703A::BQ25703Aaddr = BQ25703ADevaddr;
void PmuBQ25703A::init()
{
// Set the watchdog timer to not have a timeout
regs.chargeOption0.set_WDTMR_ADJ(0);
assert(writeRegEx(regs.chargeOption0)); // FIXME, instead log a critical hw failure and reboot
delay(15); // FIXME, why are these delays required? - check datasheet
// Set the ADC on IBAT and PSYS to record values
// When changing bitfield values, call the writeRegEx function
// This is so you can change all the bits you want before sending out the byte.
regs.chargeOption1.set_EN_IBAT(1);
regs.chargeOption1.set_EN_PSYS(1);
assert(writeRegEx(regs.chargeOption1));
delay(15);
// Set ADC to make continuous readings. (uses more power)
regs.aDCOption.set_ADC_CONV(1);
// Set individual ADC registers to read. All have default off.
regs.aDCOption.set_EN_ADC_VBUS(1);
regs.aDCOption.set_EN_ADC_PSYS(1);
regs.aDCOption.set_EN_ADC_IDCHG(1);
regs.aDCOption.set_EN_ADC_ICHG(1);
regs.aDCOption.set_EN_ADC_VSYS(1);
regs.aDCOption.set_EN_ADC_VBAT(1);
// Once bits have been twiddled, send bytes to device
assert(writeRegEx(regs.aDCOption));
delay(15);
}
/*
//Initialise the device and library
Lorro_BQ25703A BQ25703A;
//Instantiate with reference to global set
extern Lorro_BQ25703A::Regt BQ25703Areg;
uint32_t previousMillis;
uint16_t loopInterval = 1000;
void setup() {
Serial.begin(115200);
}
void loop() {
uint32_t currentMillis = millis();
if( currentMillis - previousMillis > loopInterval ){
previousMillis = currentMillis;
Serial.print( "Voltage of VBUS: " );
Serial.print( BQ25703Areg.aDCVBUSPSYS.get_VBUS() );
Serial.println( "mV" );
delay( 15 );
Serial.print( "System power usage: " );
Serial.print( BQ25703Areg.aDCVBUSPSYS.get_sysPower() );
Serial.println( "W" );
delay( 15 );
Serial.print( "Voltage of VBAT: " );
Serial.print( BQ25703Areg.aDCVSYSVBAT.get_VBAT() );
Serial.println( "mV" );
delay( 15 );
Serial.print( "Voltage of VSYS: " );
Serial.print( BQ25703Areg.aDCVSYSVBAT.get_VSYS() );
Serial.println( "mV" );
delay( 15 );
Serial.print( "Charging current: " );
Serial.print( BQ25703Areg.aDCIBAT.get_ICHG() );
Serial.println( "mA" );
delay( 15 );
Serial.print( "Voltage of VSYS: " );
Serial.print( BQ25703Areg.aDCIBAT.get_IDCHG() );
Serial.println( "mA" );
delay( 15 );
}
}*/

22
src/nrf52/PmuBQ25703A.h Normal file
View File

@@ -0,0 +1,22 @@
#pragma once
#include <Lorro_BQ25703A.h>
class PmuBQ25703A : private Lorro_BQ25703A
{
Lorro_BQ25703A::Regt regs;
public:
/**
* Configure the PMU for our board
*/
void init();
// Methods to have a common API with AXP192
bool isBatteryConnect() { return true; } // FIXME
bool isVBUSPlug() { return true; }
bool isChargeing() { return true; } // FIXME, intentional misspelling
/// battery voltage in mV
int getBattVoltage() { return 3200; }
};

3
src/nrf52/SPIFFS.h Normal file
View File

@@ -0,0 +1,3 @@
#pragma once
// FIXME - make a FS abstraction for NRF52

2
src/nrf52/main-bare.cpp Normal file
View File

@@ -0,0 +1,2 @@
#include "target_specific.h"

70
src/nrf52/main-nrf52.cpp Normal file
View File

@@ -0,0 +1,70 @@
#include "NRF52Bluetooth.h"
#include "configuration.h"
#include <assert.h>
#include <ble_gap.h>
#include <memory.h>
#include <nrf52840.h>
// #define USE_SOFTDEVICE
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)
{
DEBUG_MSG("assert failed %s: %d, %s, test=%s\n", file, line, func, failedexpr);
debugger_break();
while (1)
; // FIXME, reboot!
}
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
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
#endif
}
NRF52Bluetooth *nrf52Bluetooth;
static bool bleOn = false;
void setBluetoothEnable(bool on)
{
if (on != bleOn) {
if (on) {
if (!nrf52Bluetooth) {
nrf52Bluetooth = new NRF52Bluetooth();
nrf52Bluetooth->setup();
}
} else {
DEBUG_MSG("FIXME: implement BLE disable\n");
}
bleOn = on;
}
}
#include "PmuBQ25703A.h"
PmuBQ25703A pmu;
void nrf52Setup()
{
// Not yet on board
// pmu.init();
}