Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lightweight thread refactoring #465

Merged
merged 12 commits into from
Oct 11, 2020
2 changes: 1 addition & 1 deletion bin/version.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@


export VERSION=1.1.2
export VERSION=1.1.3
2 changes: 1 addition & 1 deletion docs/software/power.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ From lower to higher power consumption.

- full on (ON) - Everything is on, can eventually timeout and lower to a lower power state
onEntry: setBluetoothOn(true), screen.setOn(true)
onExit: screen.setOn(false)
onExit: screen->setOn(false)

- has power (POWER) - Screen is on, device doesn't sleep, bluetooth on, will stay in this state as long as we have power
onEntry: setBluetooth off, screen on
Expand Down
4 changes: 3 additions & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ build_flags = -Wno-missing-field-initializers -Isrc -Isrc/mesh -Isrc/gps -Ilib/n
-DHW_VERSION_${sysenv.COUNTRY}
-DAPP_VERSION=${sysenv.APP_VERSION}
-DHW_VERSION=${sysenv.HW_VERSION}
-DUSE_THREAD_NAMES

; leave this commented out to avoid breaking Windows
;upload_port = /dev/ttyUSB0
Expand Down Expand Up @@ -61,13 +62,14 @@ lib_deps =
https://github.com/meshtastic/esp8266-oled-ssd1306.git ; ESP8266_SSD1306
1260 ; OneButton library for non-blocking button debounce
1202 ; CRC32, explicitly needed because dependency is missing in the ble ota update lib
https://github.com/meshtastic/arduino-fsm.git
https://github.com/meshtastic/arduino-fsm.git#2f106146071fc7bc620e1e8d4b88dc4e0266ce39
https://github.com/meshtastic/SparkFun_Ublox_Arduino_Library.git#31015a55e630a2df77d9d714669c621a5bf355ad
https://github.com/meshtastic/RadioLib.git#8657380241bce681c33aab46598bbf13b11f876c
https://github.com/meshtastic/TinyGPSPlus.git
https://github.com/meshtastic/AXP202X_Library.git#8404abb6d4b486748636bc6ad72d2a47baaf5460
Wire ; explicitly needed here because the AXP202 library forgets to add it
SPI
https://github.com/geeksville/ArduinoThread.git#333ffd09b596977c217ba25da4258f588b462ac6

; Common settings for conventional (non Portduino) Ardino targets
[arduino_base]
Expand Down
89 changes: 40 additions & 49 deletions src/Power.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ class AnalogBatteryLevel : public HasBatteryLevel
virtual bool isBatteryConnect() { return getBattVoltage() != -1; }
} analogLevel;

Power::Power() : OSThread("Power") {}

bool Power::analogInit()
{
#ifdef BATTERY_PIN
Expand All @@ -86,10 +88,7 @@ bool Power::setup()
if (!found) {
found = analogInit();
}
if (found) {
concurrency::PeriodicTask::setup(); // We don't start our periodic task unless we actually found the device
setPeriod(1);
}
enabled = found;

return found;
}
Expand Down Expand Up @@ -135,13 +134,46 @@ void Power::readPowerStatus()
}
}

void Power::doTask()
int32_t Power::runOnce()
{
readPowerStatus();

#ifdef PMU_IRQ
if (pmu_irq) {
pmu_irq = false;
axp.readIRQ();

DEBUG_MSG("pmu irq!\n");

if (axp.isChargingIRQ()) {
DEBUG_MSG("Battery start charging\n");
}
if (axp.isChargingDoneIRQ()) {
DEBUG_MSG("Battery fully charged\n");
}
if (axp.isVbusRemoveIRQ()) {
DEBUG_MSG("USB unplugged\n");
powerFSM.trigger(EVENT_POWER_DISCONNECTED);
}
if (axp.isVbusPlugInIRQ()) {
DEBUG_MSG("USB plugged In\n");
powerFSM.trigger(EVENT_POWER_CONNECTED);
}
if (axp.isBattPlugInIRQ()) {
DEBUG_MSG("Battery inserted\n");
}
if (axp.isBattRemoveIRQ()) {
DEBUG_MSG("Battery removed\n");
}
if (axp.isPEKShortPressIRQ()) {
DEBUG_MSG("PEK short button press\n");
}
axp.clearIRQ();
}
#endif

// Only read once every 20 seconds once the power status for the app has been initialized
if (statusHandler && statusHandler->isInitialized())
setPeriod(1000 * 20);
return (statusHandler && statusHandler->isInitialized()) ? (1000 * 20) : RUN_SAME;
}

/**
Expand Down Expand Up @@ -208,8 +240,7 @@ bool Power::axp192Init()
// no battery also it could cause inadvertent waking from light sleep just because the battery filled
// we don't look for AXP202_BATT_REMOVED_IRQ because it occurs repeatedly while no battery installed
// we don't look at AXP202_VBUS_REMOVED_IRQ because we don't have anything hooked to vbus
axp.enableIRQ(AXP202_BATT_CONNECT_IRQ | AXP202_VBUS_CONNECT_IRQ | AXP202_PEK_SHORTPRESS_IRQ,
1);
axp.enableIRQ(AXP202_BATT_CONNECT_IRQ | AXP202_VBUS_CONNECT_IRQ | AXP202_PEK_SHORTPRESS_IRQ, 1);

axp.clearIRQ();
#endif
Expand All @@ -226,43 +257,3 @@ bool Power::axp192Init()
return false;
#endif
}

void Power::loop()
{
#ifdef PMU_IRQ
if (pmu_irq) {
pmu_irq = false;
axp.readIRQ();

DEBUG_MSG("pmu irq!\n");

if (axp.isChargingIRQ()) {
DEBUG_MSG("Battery start charging\n");
}
if (axp.isChargingDoneIRQ()) {
DEBUG_MSG("Battery fully charged\n");
}
if (axp.isVbusRemoveIRQ()) {
DEBUG_MSG("USB unplugged\n");
powerFSM.trigger(EVENT_POWER_DISCONNECTED);
}
if (axp.isVbusPlugInIRQ()) {
DEBUG_MSG("USB plugged In\n");
powerFSM.trigger(EVENT_POWER_CONNECTED);
}
if (axp.isBattPlugInIRQ()) {
DEBUG_MSG("Battery inserted\n");
}
if (axp.isBattRemoveIRQ()) {
DEBUG_MSG("Battery removed\n");
}
if (axp.isPEKShortPressIRQ()) {
DEBUG_MSG("PEK short button press\n");
}

readPowerStatus();
axp.clearIRQ();
}

#endif
}
14 changes: 7 additions & 7 deletions src/PowerFSM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ static uint32_t secsSlept;
static void lsEnter()
{
DEBUG_MSG("lsEnter begin, ls_secs=%u\n", getPref_ls_secs());
screen.setOn(false);
screen->setOn(false);
secsSlept = 0; // How long have we been sleeping this time

DEBUG_MSG("lsEnter end\n");
Expand Down Expand Up @@ -102,7 +102,7 @@ static void lsExit()

static void nbEnter()
{
screen.setOn(false);
screen->setOn(false);
setBluetoothEnable(false);

// FIXME - check if we already have packets for phone and immediately trigger EVENT_PACKETS_FOR_PHONE
Expand All @@ -111,24 +111,24 @@ static void nbEnter()
static void darkEnter()
{
setBluetoothEnable(true);
screen.setOn(false);
screen->setOn(false);
}

static void serialEnter()
{
setBluetoothEnable(false);
screen.setOn(true);
screen->setOn(true);
}

static void powerEnter()
{
screen.setOn(true);
screen->setOn(true);
setBluetoothEnable(true);
}

static void onEnter()
{
screen.setOn(true);
screen->setOn(true);
setBluetoothEnable(true);

static uint32_t lastPingMs;
Expand All @@ -144,7 +144,7 @@ static void onEnter()

static void screenPress()
{
screen.onPress();
screen->onPress();
}

static void bootEnter() {}
Expand Down
1 change: 1 addition & 0 deletions src/PowerFSM.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@
#define EVENT_POWER_DISCONNECTED 14

extern Fsm powerFSM;
extern State statePOWER;

void PowerFSM_setup();
44 changes: 0 additions & 44 deletions src/concurrency/BaseNotifiedWorkerThread.h

This file was deleted.

12 changes: 0 additions & 12 deletions src/concurrency/BaseThread.cpp

This file was deleted.

47 changes: 0 additions & 47 deletions src/concurrency/BaseThread.h

This file was deleted.

39 changes: 39 additions & 0 deletions src/concurrency/BinarySemaphoreFreeRTOS.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include "concurrency/BinarySemaphoreFreeRTOS.h"
#include "configuration.h"

#ifdef HAS_FREE_RTOS

namespace concurrency
{

BinarySemaphoreFreeRTOS::BinarySemaphoreFreeRTOS()
{
semaphore = xSemaphoreCreateBinary();
}

BinarySemaphoreFreeRTOS::~BinarySemaphoreFreeRTOS()
{
vSemaphoreDelete(semaphore);
}

/**
* Returns false if we were interrupted
*/
bool BinarySemaphoreFreeRTOS::take(uint32_t msec)
{
return xSemaphoreTake(semaphore, pdMS_TO_TICKS(msec));
}

void BinarySemaphoreFreeRTOS::give()
{
xSemaphoreGive(semaphore);
}

IRAM_ATTR void BinarySemaphoreFreeRTOS::giveFromISR(BaseType_t *pxHigherPriorityTaskWoken)
{
xSemaphoreGiveFromISR(semaphore, pxHigherPriorityTaskWoken);
}

} // namespace concurrency

#endif
Loading