Merge from origin

This commit is contained in:
Vadim Furman
2021-03-14 19:42:57 -07:00
134 changed files with 5901 additions and 1456 deletions

View File

@@ -6,30 +6,13 @@
class NRF52CryptoEngine : public CryptoEngine
{
/// How many bytes in our key
uint8_t keySize = 0;
const uint8_t *keyBytes;
public:
NRF52CryptoEngine() {}
~NRF52CryptoEngine() {}
/**
* Set the key used for encrypt, decrypt.
*
* As a special case: If all bytes are zero, we assume _no encryption_ and send all data in cleartext.
*
* @param numBytes must be 16 (AES128), 32 (AES256) or 0 (no crypt)
* @param bytes a _static_ buffer that will remain valid for the life of this crypto instance (i.e. this class will cache the
* provided pointer)
*/
virtual void setKey(size_t numBytes, uint8_t *bytes)
{
keySize = numBytes;
keyBytes = bytes;
}
/**
* Encrypt a packet
*
@@ -39,11 +22,11 @@ class NRF52CryptoEngine : public CryptoEngine
{
// DEBUG_MSG("NRF52 encrypt!\n");
if (keySize != 0) {
if (key.length > 0) {
ocrypto_aes_ctr_ctx ctx;
initNonce(fromNode, packetNum);
ocrypto_aes_ctr_init(&ctx, keyBytes, keySize, nonce);
ocrypto_aes_ctr_init(&ctx, key.bytes, key.length, nonce);
ocrypto_aes_ctr_encrypt(&ctx, bytes, bytes, numBytes);
}
@@ -53,11 +36,11 @@ class NRF52CryptoEngine : public CryptoEngine
{
// DEBUG_MSG("NRF52 decrypt!\n");
if (keySize != 0) {
if (key.length > 0) {
ocrypto_aes_ctr_ctx ctx;
initNonce(fromNode, packetNum);
ocrypto_aes_ctr_init(&ctx, keyBytes, keySize, nonce);
ocrypto_aes_ctr_init(&ctx, key.bytes, key.length, nonce);
ocrypto_aes_ctr_decrypt(&ctx, bytes, bytes, numBytes);
}

View File

@@ -16,8 +16,14 @@ static void printUsageErrorMsg(uint32_t cfsr)
cfsr >>= SCB_CFSR_USGFAULTSR_Pos; // right shift to lsb
if ((cfsr & (1 << 9)) != 0)
FAULT_MSG("Divide by zero\n");
if ((cfsr & (1 << 8)) != 0)
else if ((cfsr & (1 << 8)) != 0)
FAULT_MSG("Unaligned\n");
else if ((cfsr & (1 << 1)) != 0)
FAULT_MSG("Invalid state\n");
else if ((cfsr & (1 << 0)) != 0)
FAULT_MSG("Invalid instruction\n");
else
FAULT_MSG("FIXME add to printUsageErrorMsg!\n");
}
static void printBusErrorMsg(uint32_t cfsr)
@@ -71,12 +77,39 @@ extern "C" void HardFault_Impl(uint32_t stack[])
FAULT_MSG("Done with fault report - Waiting to reboot\n");
asm volatile("bkpt #01"); // Enter the debugger if one is connected
while (1)
;
// Don't spin, so that the debugger will let the user step to next instruction
// while (1) ;
}
#ifndef INC_FREERTOS_H
// This is a generic cortex M entrypoint that doesn't assume freertos
extern "C" void HardFault_Handler(void)
{
asm volatile(" mrs r0,msp\n"
" b HardFault_Impl \n");
}
#else
/* The prototype shows it is a naked function - in effect this is just an
assembly function. */
extern "C" void HardFault_Handler( void ) __attribute__( ( naked ) );
/* The fault handler implementation calls a function called
prvGetRegistersFromStack(). */
extern "C" void HardFault_Handler(void)
{
__asm volatile
(
" tst lr, #4 \n"
" ite eq \n"
" mrseq r0, msp \n"
" mrsne r0, psp \n"
" ldr r1, [r0, #24] \n"
" ldr r2, handler2_address_const \n"
" bx r2 \n"
" handler2_address_const: .word HardFault_Impl \n"
);
}
#endif

View File

@@ -1,5 +1,6 @@
#include "NRF52Bluetooth.h"
#include "configuration.h"
#include "error.h"
#include <SPI.h>
#include <Wire.h>
#include <assert.h>
@@ -50,14 +51,14 @@ void getMacAddr(uint8_t *dmac)
NRF52Bluetooth *nrf52Bluetooth;
static bool bleOn = false;
static const bool enableBle = true; // Set to false for easier debugging
static const bool useSoftDevice = false; // Set to false for easier debugging
void setBluetoothEnable(bool on)
{
if (on != bleOn) {
if (on) {
if (!nrf52Bluetooth) {
if (!enableBle)
if (!useSoftDevice)
DEBUG_MSG("DISABLING NRF52 BLUETOOTH WHILE DEBUGGING\n");
else {
nrf52Bluetooth = new NRF52Bluetooth();
@@ -86,6 +87,49 @@ int printf(const char *fmt, ...)
#include "BQ25713.h"
void initBrownout()
{
auto vccthresh = POWER_POFCON_THRESHOLD_V28;
auto vcchthresh = POWER_POFCON_THRESHOLDVDDH_V27;
if (useSoftDevice) {
auto err_code = sd_power_pof_enable(POWER_POFCON_POF_Enabled);
assert(err_code == NRF_SUCCESS);
err_code = sd_power_pof_threshold_set(vccthresh);
assert(err_code == NRF_SUCCESS);
}
else {
NRF_POWER->POFCON = POWER_POFCON_POF_Msk | (vccthresh << POWER_POFCON_THRESHOLD_Pos) | (vcchthresh << POWER_POFCON_THRESHOLDVDDH_Pos);
}
}
void checkSDEvents()
{
if (useSoftDevice) {
uint32_t evt;
while (NRF_ERROR_NOT_FOUND == sd_evt_get(&evt)) {
switch (evt) {
case NRF_EVT_POWER_FAILURE_WARNING:
recordCriticalError(CriticalErrorCode_Brownout);
break;
default:
DEBUG_MSG("Unexpected SDevt %d\n", evt);
break;
}
}
} else {
if(NRF_POWER->EVENTS_POFWARN)
recordCriticalError(CriticalErrorCode_Brownout);
}
}
void nrf52Loop()
{
checkSDEvents();
}
void nrf52Setup()
{
@@ -111,6 +155,8 @@ void nrf52Setup()
// randomSeed(r);
DEBUG_MSG("FIXME, call randomSeed\n");
// ::printf("TESTING PRINTF\n");
initBrownout();
}
void cpuDeepSleep(uint64_t msecToWake)
@@ -132,7 +178,7 @@ void cpuDeepSleep(uint64_t msecToWake)
// https://devzone.nordicsemi.com/f/nordic-q-a/48919/ram-retention-settings-with-softdevice-enabled
auto ok = sd_power_system_off();
if(ok != NRF_SUCCESS) {
if (ok != NRF_SUCCESS) {
DEBUG_MSG("FIXME: Ignoring soft device (EasyDMA pending?) and forcing system-off!\n");
NRF_POWER->SYSTEMOFF = 1;
}