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

ladislas/feature/deep sleep investigation #1101

Draft
wants to merge 11 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions app/os/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Copyright 2020-2022 APF France handicap
// SPDX-License-Identifier: Apache-2.0

#include "mbed_retarget.h"
#include "mbed_stats.h"

#include "drivers/Watchdog.h"
Expand Down Expand Up @@ -71,6 +72,15 @@
using namespace leka;
using namespace std::chrono;

//
// MARK: - Mbed retarget
//

auto mbed::mbed_override_console([[maybe_unused]] int fd) -> mbed::FileHandle *
{
return leka::logger::internal::filehandle;
}

//
// MARK: - Global definitions
//
Expand Down Expand Up @@ -321,14 +331,14 @@ namespace firmware {

} // namespace firmware

namespace rfid {
namespace rrfid {

auto serial = CoreBufferedSerial(RFID_UART_TX, RFID_UART_RX, 57600);
auto reader = CoreRFIDReaderCR95HF(serial);

} // namespace rfid
} // namespace rrfid

auto rfidkit = RFIDKit(rfid::reader);
auto rfidkit = RFIDKit(rrfid::reader);

namespace mcuboot {

Expand Down Expand Up @@ -522,6 +532,8 @@ auto main() -> int

rtos::ThisThread::sleep_for(1s);

rrfid::serial.disable_input();

auto version = firmware::version();

log_info("\n\n");
Expand Down
3 changes: 2 additions & 1 deletion config/mbed_app.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"target.printf_lib": "std",
"target.features_add": [
"EXPERIMENTAL_API"
]
],
"platform.stdio-baud-rate": 115200
},
"LEKA_DISCO": {
"target_name": "\"LEKA_DISCO\""
Expand Down
3 changes: 3 additions & 0 deletions drivers/CoreBufferedSerial/include/CoreBufferedSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ class CoreBufferedSerial : public interface::BufferedSerial

auto readable() -> bool final;

void enable_input() final;
void disable_input() final;

void sigio(mbed::Callback<void()> func) final;

private:
Expand Down
10 changes: 10 additions & 0 deletions drivers/CoreBufferedSerial/source/CoreBufferedSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ auto CoreBufferedSerial::readable() -> bool
return _serial.readable();
}

void CoreBufferedSerial::enable_input()
{
_serial.enable_input(true);
}

void CoreBufferedSerial::disable_input()
{
_serial.enable_input(false);
}

void CoreBufferedSerial::sigio(mbed::Callback<void()> func)
{
_serial.sigio(func);
Expand Down
5 changes: 5 additions & 0 deletions drivers/CoreMotor/source/CoreMotor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ void CoreMotor::stop()
{
setDirections(0, 0);
setSpeed(0);
_speed.suspend();
}

void CoreMotor::setDirections(int dir_1, int dir_2)
Expand All @@ -33,13 +34,17 @@ void CoreMotor::setDirections(int dir_1, int dir_2)

void CoreMotor::setSpeed(float speed)
{
// TODO (@ladislas) - add tests
if (speed < 0.0F) {
_speed.write(0);
_speed.suspend();

} else if (speed > 1.0F) {
_speed.resume();
_speed.write(1.0F);

} else {
_speed.resume();
_speed.write(speed);
}
}
Expand Down
1 change: 1 addition & 0 deletions drivers/CoreMotor/tests/CoreMotor_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ TEST_F(CoreMotorTest, stop)
EXPECT_CALL(dir_1, write(0));
EXPECT_CALL(dir_2, write(0));
EXPECT_CALL(speed, write(0));
EXPECT_CALL(speed, suspend());

motor.stop();
}
Expand Down
3 changes: 3 additions & 0 deletions drivers/CorePwm/include/CorePwm.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ class CorePwm : public interface::PwmOut
auto read() -> float final;
void write(float value) final;

void suspend() final;
void resume() final;

private:
mbed::PwmOut _pwm;
};
Expand Down
10 changes: 10 additions & 0 deletions drivers/CorePwm/source/CorePwm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,13 @@ void CorePwm::write(float value)
{
_pwm.write(value);
}

void CorePwm::suspend()
{
_pwm.suspend();
}

void CorePwm::resume()
{
_pwm.resume();
}
29 changes: 26 additions & 3 deletions drivers/CorePwm/tests/CorePwm_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ TEST(CorePwmTest, initialisation)
auto corepwm = CorePwm {NC};

ASSERT_NE(&corepwm, nullptr);
ASSERT_FALSE(spy_PwmOut_isSuspended());
}

TEST(CorePwmTest, write)
Expand All @@ -22,11 +23,11 @@ TEST(CorePwmTest, write)

corepwm.write(0.5f);

ASSERT_EQ(spy_PwmOut_value, 0.5f);
ASSERT_EQ(spy_PwmOut_getValue(), 0.5f);

corepwm.write(1.0f);

ASSERT_EQ(spy_PwmOut_value, 1.0f);
ASSERT_EQ(spy_PwmOut_getValue(), 1.0f);
}

TEST(CorePwmTest, read)
Expand All @@ -35,9 +36,31 @@ TEST(CorePwmTest, read)

corepwm.write(0.5f);

ASSERT_EQ(spy_PwmOut_value, 0.5f);
ASSERT_EQ(spy_PwmOut_getValue(), 0.5f);

auto val = corepwm.read();

ASSERT_EQ(val, 0.5f);
}

TEST(CorePwmTest, suspend)
{
auto corepwm = CorePwm {NC};

corepwm.suspend();

ASSERT_TRUE(spy_PwmOut_isSuspended());
}

TEST(CorePwmTest, suspendThenResume)
{
auto corepwm = CorePwm {NC};

corepwm.suspend();

ASSERT_TRUE(spy_PwmOut_isSuspended());

corepwm.resume();

ASSERT_FALSE(spy_PwmOut_isSuspended());
}
2 changes: 2 additions & 0 deletions drivers/CoreVideo/source/CoreLCDDriverOTM8009A.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ void CoreLCDDriverOTM8009A::turnOn()
{
_dsi.write(display::turn_on::array, std::size(display::turn_on::array));
setBrightness(_previous_brightness_value);
_backlight.resume();
}

void CoreLCDDriverOTM8009A::turnOff()
{
_dsi.write(display::turn_off::array, std::size(display::turn_off::array));
setBrightness(0.F);
_backlight.suspend();
}

void CoreLCDDriverOTM8009A::setBrightness(float value)
Expand Down
3 changes: 3 additions & 0 deletions include/interface/drivers/BufferedSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ class BufferedSerial

virtual auto readable() -> bool = 0;

virtual void disable_input() = 0;
virtual void enable_input() = 0;

virtual void sigio(mbed::Callback<void()> func) = 0; // TODO (@HPezz) replace mbed callback by std function
};

Expand Down
3 changes: 3 additions & 0 deletions include/interface/drivers/PwmOut.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ class PwmOut

virtual auto read() -> float = 0;
virtual void write(float value) = 0;

virtual void suspend() = 0;
virtual void resume() = 0;
};

} // namespace leka::interface
1 change: 1 addition & 0 deletions libs/LogKit/include/LogKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ inline void init(filehandle_ptr fh = &internal::default_serial,
const sink_function_t &sink = internal::default_sink_function)
{
set_filehandle_pointer(fh);
fh->enable_input(false);
set_sink_function(sink);
internal::start_event_queue();
}
Expand Down
2 changes: 2 additions & 0 deletions spikes/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ add_subdirectory(${SPIKES_DIR}/lk_update_process_app_base)
add_subdirectory(${SPIKES_DIR}/lk_update_process_app_update)

add_subdirectory(${SPIKES_DIR}/mbed_blinky)
add_subdirectory(${SPIKES_DIR}/mbed_deep_sleep)
add_subdirectory(${SPIKES_DIR}/mbed_watchdog_ticker_vs_thread)

add_subdirectory(${SPIKES_DIR}/stl_cxxsupport)
Expand Down Expand Up @@ -76,6 +77,7 @@ add_dependencies(spikes_leka
add_custom_target(spikes_mbed)
add_dependencies(spikes_mbed
spike_mbed_blinky
spike_mbed_deep_sleep
spike_mbed_watchdog_ticker_vs_thread
)

Expand Down
92 changes: 92 additions & 0 deletions spikes/lk_rfid/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
#include <cstddef>
#include <cstdint>

#include "drivers/Watchdog.h"
#include "platform/mbed_power_mgmt.h"
#include "platform/mbed_stats.h"
#include "rtos/ThisThread.h"

#include "CoreBufferedSerial.h"
Expand All @@ -16,9 +19,95 @@
using namespace leka;
using namespace std::chrono;

namespace watchdog {

namespace internal {

auto &instance = mbed::Watchdog::get_instance();
constexpr auto timeout = 30000ms;
auto thread = rtos::Thread {osPriorityLow};

namespace stats {

auto cpu = mbed_stats_cpu_t {};
auto stack = mbed_stats_stack_t {};
auto heap = mbed_stats_heap_t {};

} // namespace stats

__attribute__((noreturn)) void watchdog_kick()
{
static auto kick_count = uint32_t {0};

static auto start = rtos::Kernel::Clock::now();
static auto stop = rtos::Kernel::Clock::now();
static auto delta = static_cast<int>((stop - start).count());

static auto sleep_ratio = uint8_t {};
static auto deep_sleep_ratio = uint8_t {};

static auto stack_used_delta = int32_t {};
static auto stack_used_size = uint32_t {};
static auto stack_reserved_size = uint32_t {};
static auto stack_used_ratio = uint8_t {};

static auto heap_used_delta = int32_t {};
static auto heap_used_size = uint32_t {};
static auto heap_reserved_size = uint32_t {};
static auto heap_used_ratio = uint8_t {};

while (true) {
internal::instance.kick();
++kick_count;

stop = rtos::Kernel::Clock::now();
delta = static_cast<int>((stop - start).count());

mbed_stats_cpu_get(&stats::cpu);

sleep_ratio = static_cast<uint8_t>(((stats::cpu.sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000)));
deep_sleep_ratio =
static_cast<uint8_t>(((stats::cpu.deep_sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000)));

mbed_stats_stack_get(&stats::stack);

stack_used_delta = static_cast<int32_t>(stats::stack.max_size - stack_used_size);
stack_used_size = stats::stack.max_size;
stack_reserved_size = stats::stack.reserved_size;
stack_used_ratio = static_cast<uint8_t>((stack_used_size * 100) / stack_reserved_size);

mbed_stats_heap_get(&stats::heap);

heap_used_delta = static_cast<int32_t>(stats::heap.current_size - heap_used_size);
heap_used_size = stats::heap.current_size;
heap_reserved_size = stats::heap.reserved_size;
heap_used_ratio = static_cast<uint8_t>((heap_used_size * 100) / heap_reserved_size);

log_info(
"dt: %i, kck: %u, slp: %u%%, dsl: %u%%, sur: %u%% (%+i)[%u/"
"%u], hur: %u%% (%+i)[%u/%u]",
delta, kick_count, sleep_ratio, deep_sleep_ratio, stack_used_ratio, stack_used_delta, stack_used_size,
stack_reserved_size, heap_used_ratio, heap_used_delta, heap_used_size, heap_reserved_size);

start = rtos::Kernel::Clock::now();
rtos::ThisThread::sleep_for(5s);
}
}

} // namespace internal

void start()
{
internal::instance.start(internal::timeout.count());
internal::thread.start(watchdog::internal::watchdog_kick);
}

} // namespace watchdog

auto main() -> int
{
logger::init();
watchdog::start();

log_info("Hello, World!\n\n");

Expand All @@ -34,6 +123,9 @@ auto main() -> int
HelloWorld hello;
hello.start();

// ? Uncomment to see the impact on deep sleep ratio
// rfidserial.disable_input();

while (true) {
rtos::ThisThread::sleep_for(10s);
}
Expand Down
17 changes: 17 additions & 0 deletions spikes/mbed_deep_sleep/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Leka - LekaOS
# Copyright 2020 APF France handicap
# SPDX-License-Identifier: Apache-2.0

add_mbed_executable(spike_mbed_deep_sleep)

target_include_directories(spike_mbed_deep_sleep
PRIVATE
.
)

target_sources(spike_mbed_deep_sleep
PRIVATE
main.cpp
)

target_link_custom_leka_targets(spike_mbed_deep_sleep)
Loading