lower brownout threshold to 1.7V

This commit is contained in:
Kevin Hester 2021-04-11 13:52:39 +08:00
parent 328b24537f
commit f9a58b9dd1

View File

@ -13,25 +13,23 @@
#include "BQ25713.h" #include "BQ25713.h"
#endif #endif
static inline void debugger_break(void)
static inline void debugger_break(void) { {
__asm volatile( __asm volatile("bkpt #0x01\n\t"
"bkpt #0x01\n\t"
"mov pc, lr\n\t"); "mov pc, lr\n\t");
} }
// handle standard gcc assert failures // handle standard gcc assert failures
void __attribute__((noreturn)) void __attribute__((noreturn)) __assert_func(const char *file, int line, const char *func, const char *failedexpr)
__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);
DEBUG_MSG("assert failed %s: %d, %s, test=%s\n", file, line, func,
failedexpr);
// debugger_break(); FIXME doesn't work, possibly not for segger // debugger_break(); FIXME doesn't work, possibly not for segger
while (1) while (1)
; // FIXME, reboot! ; // FIXME, reboot!
} }
void getMacAddr(uint8_t *dmac) { void getMacAddr(uint8_t *dmac)
{
ble_gap_addr_t addr; ble_gap_addr_t addr;
if (sd_ble_gap_addr_get(&addr) == NRF_SUCCESS) { if (sd_ble_gap_addr_get(&addr) == NRF_SUCCESS) {
memcpy(dmac, addr.addr, 6); memcpy(dmac, addr.addr, 6);
@ -42,17 +40,17 @@ void getMacAddr(uint8_t *dmac) {
dmac[3] = src[2]; dmac[3] = src[2];
dmac[2] = src[3]; dmac[2] = src[3];
dmac[1] = src[4]; dmac[1] = src[4];
dmac[0] = src[5] | dmac[0] = src[5] | 0xc0; // MSB high two bits get set elsewhere in the bluetooth stack
0xc0; // MSB high two bits get set elsewhere in the bluetooth stack
} }
} }
NRF52Bluetooth *nrf52Bluetooth; NRF52Bluetooth *nrf52Bluetooth;
static bool bleOn = false; static bool bleOn = false;
static const bool useSoftDevice = true; // Set to false for easier debugging static const bool useSoftDevice = false; // Set to false for easier debugging
void setBluetoothEnable(bool on) { void setBluetoothEnable(bool on)
{
if (on != bleOn) { if (on != bleOn) {
if (on) { if (on) {
if (!nrf52Bluetooth) { if (!nrf52Bluetooth) {
@ -64,7 +62,8 @@ void setBluetoothEnable(bool on) {
} }
} }
} else { } else {
if (nrf52Bluetooth) nrf52Bluetooth->shutdown(); if (nrf52Bluetooth)
nrf52Bluetooth->shutdown();
} }
bleOn = on; bleOn = on;
} }
@ -73,7 +72,8 @@ void setBluetoothEnable(bool on) {
/** /**
* Override printf to use the SEGGER output library * Override printf to use the SEGGER output library
*/ */
int printf(const char *fmt, ...) { int printf(const char *fmt, ...)
{
va_list args; va_list args;
va_start(args, fmt); va_start(args, fmt);
auto res = SEGGER_RTT_vprintf(0, fmt, &args); auto res = SEGGER_RTT_vprintf(0, fmt, &args);
@ -81,8 +81,9 @@ int printf(const char *fmt, ...) {
return res; return res;
} }
void initBrownout() { void initBrownout()
auto vccthresh = POWER_POFCON_THRESHOLD_V28; {
auto vccthresh = POWER_POFCON_THRESHOLD_V17;
if (useSoftDevice) { if (useSoftDevice) {
auto err_code = sd_power_pof_enable(POWER_POFCON_POF_Enabled); auto err_code = sd_power_pof_enable(POWER_POFCON_POF_Enabled);
@ -90,19 +91,12 @@ void initBrownout() {
err_code = sd_power_pof_threshold_set(vccthresh); err_code = sd_power_pof_threshold_set(vccthresh);
assert(err_code == NRF_SUCCESS); assert(err_code == NRF_SUCCESS);
} else {
uint32_t pof_flags = POWER_POFCON_POF_Enabled | (vccthresh << POWER_POFCON_THRESHOLD_Pos);
#ifdef POWER_POFCON_THRESHOLDVDDH_Msk
auto vcchthresh = POWER_POFCON_THRESHOLDVDDH_V27;
pof_flags |= (vcchthresh << POWER_POFCON_THRESHOLDVDDH_Pos);
#endif
NRF_POWER->POFCON = pof_flags;
} }
// We don't bother with setting up brownout if soft device is disabled - because during production we always use softdevice
} }
void checkSDEvents() { void checkSDEvents()
{
if (useSoftDevice) { if (useSoftDevice) {
uint32_t evt; uint32_t evt;
while (NRF_SUCCESS == sd_evt_get(&evt)) { while (NRF_SUCCESS == sd_evt_get(&evt)) {
@ -122,9 +116,13 @@ void checkSDEvents() {
} }
} }
void nrf52Loop() { checkSDEvents(); } void nrf52Loop()
{
checkSDEvents();
}
void nrf52Setup() { void nrf52Setup()
{
auto why = NRF_POWER->RESETREAS; auto why = NRF_POWER->RESETREAS;
// per // per
// https://infocenter.nordicsemi.com/index.jsp?topic=%2Fcom.nordic.infocenter.nrf52832.ps.v1.1%2Fpower.html // https://infocenter.nordicsemi.com/index.jsp?topic=%2Fcom.nordic.infocenter.nrf52832.ps.v1.1%2Fpower.html
@ -137,7 +135,8 @@ void nrf52Setup() {
#ifdef BQ25703A_ADDR #ifdef BQ25703A_ADDR
auto *bq = new BQ25713(); auto *bq = new BQ25713();
if (!bq->setup()) DEBUG_MSG("ERROR! Charge controller init failed\n"); if (!bq->setup())
DEBUG_MSG("ERROR! Charge controller init failed\n");
#endif #endif
// Init random seed // Init random seed
@ -152,7 +151,8 @@ void nrf52Setup() {
initBrownout(); initBrownout();
} }
void cpuDeepSleep(uint64_t msecToWake) { void cpuDeepSleep(uint64_t msecToWake)
{
// FIXME, configure RTC or button press to wake us // FIXME, configure RTC or button press to wake us
// FIXME, power down SPI, I2C, RAMs // FIXME, power down SPI, I2C, RAMs
#ifndef NO_WIRE #ifndef NO_WIRE
@ -172,8 +172,7 @@ void cpuDeepSleep(uint64_t msecToWake) {
auto ok = sd_power_system_off(); auto ok = sd_power_system_off();
if (ok != NRF_SUCCESS) { if (ok != NRF_SUCCESS) {
DEBUG_MSG( DEBUG_MSG("FIXME: Ignoring soft device (EasyDMA pending?) and forcing "
"FIXME: Ignoring soft device (EasyDMA pending?) and forcing "
"system-off!\n"); "system-off!\n");
NRF_POWER->SYSTEMOFF = 1; NRF_POWER->SYSTEMOFF = 1;
} }