diff --git a/app/os/main.cpp b/app/os/main.cpp index 13f6636019..94190cc89b 100644 --- a/app/os/main.cpp +++ b/app/os/main.cpp @@ -463,6 +463,7 @@ namespace watchdog { static auto battery_level = uint8_t {}; static auto charging_status = uint8_t {}; + static auto idle_ratio = uint8_t {}; static auto sleep_ratio = uint8_t {}; static auto deep_sleep_ratio = uint8_t {}; @@ -489,6 +490,7 @@ namespace watchdog { mbed_stats_cpu_get(&stats::cpu); + idle_ratio = static_cast(((stats::cpu.idle_time / 1000 * 100) / (stats::cpu.uptime / 1000))); sleep_ratio = static_cast(((stats::cpu.sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); deep_sleep_ratio = static_cast(((stats::cpu.deep_sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); @@ -508,11 +510,11 @@ namespace watchdog { heap_used_ratio = static_cast((heap_used_size * 100) / heap_reserved_size); log_info( - "dt: %i, kck: %u, ble: %u, lvl: %u%%, chr: %u, slp: %u%%, dsl: %u%%, sur: %u%% (%+i)[%u/" + "dt: %i, kck: %u, ble: %u, lvl: %u%%, chr: %u, idl: %u%%, slp: %u%%, dsl: %u%%, sur: %u%% (%+i)[%u/" "%u], hur: %u%% (%+i)[%u/%u]", - delta, kick_count, ble_connected, battery_level, charging_status, 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); + delta, kick_count, ble_connected, battery_level, charging_status, idle_ratio, 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); diff --git a/libs/RobotKit/include/RobotController.h b/libs/RobotKit/include/RobotController.h index 86f5c04d51..865139f9ac 100644 --- a/libs/RobotKit/include/RobotController.h +++ b/libs/RobotKit/include/RobotController.h @@ -545,7 +545,7 @@ class RobotController : public interface::RobotController interface::Timeout &_timeout_state_internal; - std::chrono::seconds _sleep_timeout_duration {60}; + std::chrono::seconds _sleep_timeout_duration {5}; std::chrono::seconds _idle_timeout_duration {600}; std::chrono::seconds _deep_sleep_timeout_duration {600}; interface::Timeout &_timeout_state_transition; diff --git a/spikes/CMakeLists.txt b/spikes/CMakeLists.txt index cc3bd94479..ac661ab02d 100644 --- a/spikes/CMakeLists.txt +++ b/spikes/CMakeLists.txt @@ -22,6 +22,7 @@ add_subdirectory(${SPIKES_DIR}/lk_imu_kit) add_subdirectory(${SPIKES_DIR}/lk_lcd) add_subdirectory(${SPIKES_DIR}/lk_led_kit) add_subdirectory(${SPIKES_DIR}/lk_log_kit) +add_subdirectory(${SPIKES_DIR}/lk_logs) add_subdirectory(${SPIKES_DIR}/lk_motion_kit) add_subdirectory(${SPIKES_DIR}/lk_motors) add_subdirectory(${SPIKES_DIR}/lk_qdac) diff --git a/spikes/lk_activity_kit/main.cpp b/spikes/lk_activity_kit/main.cpp index 453d817223..5a152cf508 100644 --- a/spikes/lk_activity_kit/main.cpp +++ b/spikes/lk_activity_kit/main.cpp @@ -59,8 +59,99 @@ using namespace leka; using namespace std::chrono; +#include "mbed_stats.h" + +#include "drivers/Watchdog.h" namespace { +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((stop - start).count()); + + static auto idle_ratio = uint8_t {}; + 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((stop - start).count()); + + mbed_stats_cpu_get(&stats::cpu); + + idle_ratio = static_cast(((stats::cpu.idle_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + sleep_ratio = static_cast(((stats::cpu.sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + deep_sleep_ratio = + static_cast(((stats::cpu.deep_sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + + mbed_stats_stack_get(&stats::stack); + + stack_used_delta = static_cast(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((stack_used_size * 100) / stack_reserved_size); + + mbed_stats_heap_get(&stats::heap); + + heap_used_delta = static_cast(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((heap_used_size * 100) / heap_reserved_size); + + log_info( + "dt: %i, kck: %u, idl: %u%%, slp: %u%%, dsl: %u%%, sur: %u%% (%+i)[%u/" + "%u], hur: %u%% (%+i)[%u/%u]", + delta, kick_count, idle_ratio, 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 + namespace sd { namespace internal { @@ -262,6 +353,7 @@ auto activitykit = ActivityKit {videokit}; auto main() -> int { logger::init(); + watchdog::start(); log_info("Hello, World!\n\n"); @@ -293,8 +385,20 @@ auto main() -> int activitykit.start(card); }); - while (true) { + { log_info("Still alive"); rtos::ThisThread::sleep_for(10s); } + + activitykit.stop(); + rtos::ThisThread::sleep_for(1s); + + rfidkit.enableDeepSleep(); + display::internal::corelcd.enableDeepSleep(); + motors::left::motor.enableDeepSleep(); + motors::right::motor.enableDeepSleep(); + + while (true) { + rtos::ThisThread::sleep_for(10min); + } } diff --git a/spikes/lk_audio/main.cpp b/spikes/lk_audio/main.cpp index 3483f70263..134a53fe5a 100644 --- a/spikes/lk_audio/main.cpp +++ b/spikes/lk_audio/main.cpp @@ -19,6 +19,101 @@ using namespace leka; using namespace std::chrono_literals; +#include "mbed_stats.h" + +#include "drivers/Watchdog.h" +namespace { + +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((stop - start).count()); + + static auto idle_ratio = uint8_t {}; + 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((stop - start).count()); + + mbed_stats_cpu_get(&stats::cpu); + + idle_ratio = static_cast(((stats::cpu.idle_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + sleep_ratio = static_cast(((stats::cpu.sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + deep_sleep_ratio = + static_cast(((stats::cpu.deep_sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + + mbed_stats_stack_get(&stats::stack); + + stack_used_delta = static_cast(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((stack_used_size * 100) / stack_reserved_size); + + mbed_stats_heap_get(&stats::heap); + + heap_used_delta = static_cast(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((heap_used_size * 100) / heap_reserved_size); + + log_info( + "dt: %i, kck: %u, idl: %u%%, slp: %u%%, dsl: %u%%, sur: %u%% (%+i)[%u/" + "%u], hur: %u%% (%+i)[%u/%u]", + delta, kick_count, idle_ratio, 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 + +} // namespace + auto sd_bd = SDBlockDevice {SD_SPI_MOSI, SD_SPI_MISO, SD_SPI_SCK}; auto fatfs = FATFileSystem {"fs"}; @@ -68,6 +163,7 @@ void playSound() auto main() -> int { logger::init(); + watchdog::start(); log_info("Hello, World!\n\n"); @@ -77,11 +173,16 @@ auto main() -> int return 1; } - while (true) { + { file.open(sound_file_path); - playSound(); + // playSound(); + audio_output.write_u16(0xFFFF); file.close(); rtos::ThisThread::sleep_for(1s); } + + while (true) { + rtos::ThisThread::sleep_for(10min); + } } diff --git a/spikes/lk_behavior_kit/main.cpp b/spikes/lk_behavior_kit/main.cpp index 5eb6f7c0c8..54ad1e0981 100644 --- a/spikes/lk_behavior_kit/main.cpp +++ b/spikes/lk_behavior_kit/main.cpp @@ -41,8 +41,98 @@ using namespace std::chrono; // MARK: - Global definitions // +#include "mbed_stats.h" + +#include "drivers/Watchdog.h" namespace { +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((stop - start).count()); + + static auto idle_ratio = uint8_t {}; + 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((stop - start).count()); + + mbed_stats_cpu_get(&stats::cpu); + + idle_ratio = static_cast(((stats::cpu.idle_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + sleep_ratio = static_cast(((stats::cpu.sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + deep_sleep_ratio = + static_cast(((stats::cpu.deep_sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + + mbed_stats_stack_get(&stats::stack); + + stack_used_delta = static_cast(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((stack_used_size * 100) / stack_reserved_size); + + mbed_stats_heap_get(&stats::heap); + + heap_used_delta = static_cast(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((heap_used_size * 100) / heap_reserved_size); + + log_info( + "dt: %i, kck: %u, idl: %u%%, slp: %u%%, dsl: %u%%, sur: %u%% (%+i)[%u/" + "%u], hur: %u%% (%+i)[%u/%u]", + delta, kick_count, idle_ratio, 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 namespace sd { namespace internal { @@ -180,6 +270,7 @@ auto hello = HelloWorld {}; auto main() -> int { logger::init(); + watchdog::start(); leds::turnOff(); motors::turnOff(); @@ -193,7 +284,7 @@ auto main() -> int hello.start(); - while (true) { + { behaviorkit.launching(); rtos::ThisThread::sleep_for(10s); behaviorkit.stop(); @@ -204,4 +295,14 @@ auto main() -> int behaviorkit.stop(); rtos::ThisThread::sleep_for(3s); } + + behaviorkit.stop(); + + display::internal::corelcd.enableDeepSleep(); + motors::left::motor.enableDeepSleep(); + motors::right::motor.enableDeepSleep(); + + while (true) { + rtos::ThisThread::sleep_for(10min); + } } diff --git a/spikes/lk_ble/main.cpp b/spikes/lk_ble/main.cpp index 0f09b76f96..61b9e415ca 100644 --- a/spikes/lk_ble/main.cpp +++ b/spikes/lk_ble/main.cpp @@ -22,6 +22,101 @@ using namespace leka; using namespace std::chrono; +#include "mbed_stats.h" + +#include "drivers/Watchdog.h" +namespace { + +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((stop - start).count()); + + static auto idle_ratio = uint8_t {}; + 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((stop - start).count()); + + mbed_stats_cpu_get(&stats::cpu); + + idle_ratio = static_cast(((stats::cpu.idle_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + sleep_ratio = static_cast(((stats::cpu.sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + deep_sleep_ratio = + static_cast(((stats::cpu.deep_sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + + mbed_stats_stack_get(&stats::stack); + + stack_used_delta = static_cast(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((stack_used_size * 100) / stack_reserved_size); + + mbed_stats_heap_get(&stats::heap); + + heap_used_delta = static_cast(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((heap_used_size * 100) / heap_reserved_size); + + log_info( + "dt: %i, kck: %u, idl: %u%%, slp: %u%%, dsl: %u%%, sur: %u%% (%+i)[%u/" + "%u], hur: %u%% (%+i)[%u/%u]", + delta, kick_count, idle_ratio, 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 + +} // namespace + auto level = uint8_t {0}; auto charging_status = bool {false}; @@ -53,6 +148,7 @@ void initializeSD() auto main() -> int { logger::init(); + watchdog::start(); log_info("Hello, World!\n\n"); @@ -81,9 +177,9 @@ auto main() -> int service_file_exchange.onFileDataReceived( [](std::span buffer) { file_reception_handler.onPacketReceived(buffer); }); - while (true) { + { log_info("Main thread running..."); - rtos::ThisThread::sleep_for(5s); + rtos::ThisThread::sleep_for(1s); log_info("Is connected: %d", blekit.isConnected()); @@ -97,15 +193,9 @@ auto main() -> int auto version = service_update.getVersion(); log_info("Requested version: %d.%d.%d", version.major, version.minor, version.revision); + } - auto advertising_data = blekit.getAdvertisingData(); - advertising_data.name = "NewLeka"; - advertising_data.battery = level; - advertising_data.is_charging = charging_status; - advertising_data.version_major = uint8_t {0x01}; - advertising_data.version_minor = uint8_t {0x02}; - advertising_data.version_revision = uint16_t {0x0304}; - - blekit.setAdvertisingData(advertising_data); + while (true) { + rtos::ThisThread::sleep_for(10min); } } diff --git a/spikes/lk_bluetooth/main.cpp b/spikes/lk_bluetooth/main.cpp index 85c5fe8d33..95dda5c532 100644 --- a/spikes/lk_bluetooth/main.cpp +++ b/spikes/lk_bluetooth/main.cpp @@ -12,11 +12,107 @@ using namespace leka; using namespace std::chrono; +#include "mbed_stats.h" + +#include "drivers/Watchdog.h" +namespace { + +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((stop - start).count()); + + static auto idle_ratio = uint8_t {}; + 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((stop - start).count()); + + mbed_stats_cpu_get(&stats::cpu); + + idle_ratio = static_cast(((stats::cpu.idle_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + sleep_ratio = static_cast(((stats::cpu.sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + deep_sleep_ratio = + static_cast(((stats::cpu.deep_sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + + mbed_stats_stack_get(&stats::stack); + + stack_used_delta = static_cast(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((stack_used_size * 100) / stack_reserved_size); + + mbed_stats_heap_get(&stats::heap); + + heap_used_delta = static_cast(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((heap_used_size * 100) / heap_reserved_size); + + log_info( + "dt: %i, kck: %u, idl: %u%%, slp: %u%%, dsl: %u%%, sur: %u%% (%+i)[%u/" + "%u], hur: %u%% (%+i)[%u/%u]", + delta, kick_count, idle_ratio, 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 + +} // namespace + auto audio_enable = mbed::DigitalOut {SOUND_ENABLE, 1}; auto main() -> int { logger::init(); + watchdog::start(); log_info("Hello, World!\n\n"); @@ -30,6 +126,6 @@ auto main() -> int bluetooth.start(); while (true) { - rtos::ThisThread::sleep_for(10s); + rtos::ThisThread::sleep_for(10min); } } diff --git a/spikes/lk_cg_animations/main.cpp b/spikes/lk_cg_animations/main.cpp index cd75668b34..85ba5d60b2 100644 --- a/spikes/lk_cg_animations/main.cpp +++ b/spikes/lk_cg_animations/main.cpp @@ -34,6 +34,101 @@ using namespace leka; using namespace std::chrono; +#include "mbed_stats.h" + +#include "drivers/Watchdog.h" +namespace { + +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((stop - start).count()); + + static auto idle_ratio = uint8_t {}; + 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((stop - start).count()); + + mbed_stats_cpu_get(&stats::cpu); + + idle_ratio = static_cast(((stats::cpu.idle_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + sleep_ratio = static_cast(((stats::cpu.sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + deep_sleep_ratio = + static_cast(((stats::cpu.deep_sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + + mbed_stats_stack_get(&stats::stack); + + stack_used_delta = static_cast(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((stack_used_size * 100) / stack_reserved_size); + + mbed_stats_heap_get(&stats::heap); + + heap_used_delta = static_cast(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((heap_used_size * 100) / heap_reserved_size); + + log_info( + "dt: %i, kck: %u, idl: %u%%, slp: %u%%, dsl: %u%%, sur: %u%% (%+i)[%u/" + "%u], hur: %u%% (%+i)[%u/%u]", + delta, kick_count, idle_ratio, 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 + +} // namespace + HelloWorld hello; SDBlockDevice sd_blockdevice(SD_SPI_MOSI, SD_SPI_MISO, SD_SPI_SCK); @@ -65,6 +160,7 @@ UIAnimationKit animationkit(animation_thread, animation_event_queue); auto main() -> int { logger::init(); + watchdog::start(); log_info("Hello, World!\n\n"); @@ -76,7 +172,7 @@ auto main() -> int hello.start(); - while (true) { + { auto t = rtos::Kernel::Clock::now() - start; log_info("A message from your board %s --> \"%s\" at %i s\n", MBED_CONF_APP_TARGET_NAME, hello.world, int(t.count() / 1000)); @@ -87,4 +183,10 @@ auto main() -> int animationkit.stop(); rtos::ThisThread::sleep_for(1s); } + + corelcd.enableDeepSleep(); + + while (true) { + rtos::ThisThread::sleep_for(10min); + } } diff --git a/spikes/lk_color_kit/main.cpp b/spikes/lk_color_kit/main.cpp index bb6dbcb50c..7fd67c812e 100644 --- a/spikes/lk_color_kit/main.cpp +++ b/spikes/lk_color_kit/main.cpp @@ -21,8 +21,99 @@ using namespace std::chrono; // MARK: - Global definitions // +#include "mbed_stats.h" + +#include "drivers/Watchdog.h" namespace { +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((stop - start).count()); + + static auto idle_ratio = uint8_t {}; + 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((stop - start).count()); + + mbed_stats_cpu_get(&stats::cpu); + + idle_ratio = static_cast(((stats::cpu.idle_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + sleep_ratio = static_cast(((stats::cpu.sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + deep_sleep_ratio = + static_cast(((stats::cpu.deep_sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + + mbed_stats_stack_get(&stats::stack); + + stack_used_delta = static_cast(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((stack_used_size * 100) / stack_reserved_size); + + mbed_stats_heap_get(&stats::heap); + + heap_used_delta = static_cast(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((heap_used_size * 100) / heap_reserved_size); + + log_info( + "dt: %i, kck: %u, idl: %u%%, slp: %u%%, dsl: %u%%, sur: %u%% (%+i)[%u/" + "%u], hur: %u%% (%+i)[%u/%u]", + delta, kick_count, idle_ratio, 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 + namespace leds { namespace internal { @@ -80,6 +171,7 @@ auto hello = HelloWorld {}; auto main() -> int { logger::init(); + watchdog::start(); leds::turnOff(); hello.start(); @@ -87,10 +179,14 @@ auto main() -> int rtos::ThisThread::sleep_for(2s); - while (true) { + { leds::changeColors(); rtos::ThisThread::sleep_for(1s); leds::turnOff(); rtos::ThisThread::sleep_for(1s); } + + while (true) { + rtos::ThisThread::sleep_for(10min); + } } diff --git a/spikes/lk_command_kit/main.cpp b/spikes/lk_command_kit/main.cpp index 030837fa1f..6e09779989 100644 --- a/spikes/lk_command_kit/main.cpp +++ b/spikes/lk_command_kit/main.cpp @@ -48,6 +48,101 @@ using namespace leka; using namespace std::chrono; +#include "mbed_stats.h" + +#include "drivers/Watchdog.h" +namespace { + +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((stop - start).count()); + + static auto idle_ratio = uint8_t {}; + 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((stop - start).count()); + + mbed_stats_cpu_get(&stats::cpu); + + idle_ratio = static_cast(((stats::cpu.idle_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + sleep_ratio = static_cast(((stats::cpu.sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + deep_sleep_ratio = + static_cast(((stats::cpu.deep_sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + + mbed_stats_stack_get(&stats::stack); + + stack_used_delta = static_cast(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((stack_used_size * 100) / stack_reserved_size); + + mbed_stats_heap_get(&stats::heap); + + heap_used_delta = static_cast(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((heap_used_size * 100) / heap_reserved_size); + + log_info( + "dt: %i, kck: %u, idl: %u%%, slp: %u%%, dsl: %u%%, sur: %u%% (%+i)[%u/" + "%u], hur: %u%% (%+i)[%u/%u]", + delta, kick_count, idle_ratio, 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 + +} // namespace + namespace leds { namespace spi { @@ -246,6 +341,7 @@ HelloWorld hello; auto main() -> int { logger::init(); + watchdog::start(); initializeSD(); display::videokit.initializeScreen(); @@ -267,7 +363,7 @@ auto main() -> int hello.start(); - while (true) { + { log_debug("A message from your board %s --> \"%s\" at %ims", MBED_CONF_APP_TARGET_NAME, hello.world, int(rtos::Kernel::Clock::now().time_since_epoch().count())); @@ -278,4 +374,12 @@ auto main() -> int turnOff(); } } + + display::internal::corelcd.enableDeepSleep(); + motor::left.enableDeepSleep(); + motor::right.enableDeepSleep(); + + while (true) { + rtos::ThisThread::sleep_for(10min); + } } diff --git a/spikes/lk_config_kit/main.cpp b/spikes/lk_config_kit/main.cpp index aa16f78801..be3a31233d 100644 --- a/spikes/lk_config_kit/main.cpp +++ b/spikes/lk_config_kit/main.cpp @@ -19,6 +19,101 @@ using namespace leka; using namespace std::chrono_literals; +#include "mbed_stats.h" + +#include "drivers/Watchdog.h" +namespace { + +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((stop - start).count()); + + static auto idle_ratio = uint8_t {}; + 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((stop - start).count()); + + mbed_stats_cpu_get(&stats::cpu); + + idle_ratio = static_cast(((stats::cpu.idle_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + sleep_ratio = static_cast(((stats::cpu.sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + deep_sleep_ratio = + static_cast(((stats::cpu.deep_sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + + mbed_stats_stack_get(&stats::stack); + + stack_used_delta = static_cast(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((stack_used_size * 100) / stack_reserved_size); + + mbed_stats_heap_get(&stats::heap); + + heap_used_delta = static_cast(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((heap_used_size * 100) / heap_reserved_size); + + log_info( + "dt: %i, kck: %u, idl: %u%%, slp: %u%%, dsl: %u%%, sur: %u%% (%+i)[%u/" + "%u], hur: %u%% (%+i)[%u/%u]", + delta, kick_count, idle_ratio, 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 + +} // namespace + SDBlockDevice sd_blockdevice(SD_SPI_MOSI, SD_SPI_MISO, SD_SPI_SCK); FATFileSystem fatfs("fs"); @@ -38,6 +133,7 @@ void initializeSD() auto main() -> int { logger::init(); + watchdog::start(); log_info("Hello, World!\n\n"); @@ -55,7 +151,7 @@ auto main() -> int rtos::ThisThread::sleep_for(1s); - while (true) { + { auto t = rtos::Kernel::Clock::now() - start; log_info("A message from your board %s --> \"%s\" at %i s", MBED_CONF_APP_TARGET_NAME, hello.world, int(t.count() / 1000)); @@ -68,6 +164,10 @@ auto main() -> int auto config_value = configkit.read(config_to_edit); log_info("Config value : %d", config_value); - rtos::ThisThread::sleep_for(10s); + rtos::ThisThread::sleep_for(1s); + } + + while (true) { + rtos::ThisThread::sleep_for(10min); } } diff --git a/spikes/lk_core_touch_sensor/main.cpp b/spikes/lk_core_touch_sensor/main.cpp index fef65ebfb6..b29025fa47 100644 --- a/spikes/lk_core_touch_sensor/main.cpp +++ b/spikes/lk_core_touch_sensor/main.cpp @@ -21,6 +21,101 @@ using namespace leka; using namespace std::chrono; +#include "mbed_stats.h" + +#include "drivers/Watchdog.h" +namespace { + +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((stop - start).count()); + + static auto idle_ratio = uint8_t {}; + 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((stop - start).count()); + + mbed_stats_cpu_get(&stats::cpu); + + idle_ratio = static_cast(((stats::cpu.idle_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + sleep_ratio = static_cast(((stats::cpu.sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + deep_sleep_ratio = + static_cast(((stats::cpu.deep_sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + + mbed_stats_stack_get(&stats::stack); + + stack_used_delta = static_cast(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((stack_used_size * 100) / stack_reserved_size); + + mbed_stats_heap_get(&stats::heap); + + heap_used_delta = static_cast(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((heap_used_size * 100) / heap_reserved_size); + + log_info( + "dt: %i, kck: %u, idl: %u%%, slp: %u%%, dsl: %u%%, sur: %u%% (%+i)[%u/" + "%u], hur: %u%% (%+i)[%u/%u]", + delta, kick_count, idle_ratio, 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 + +} // namespace + namespace touch { auto corei2c = CoreI2C {PinName::SENSOR_PROXIMITY_MUX_I2C_SDA, PinName::SENSOR_PROXIMITY_MUX_I2C_SCL}; @@ -141,6 +236,7 @@ void calibration() auto main() -> int { logger::init(); + watchdog::start(); HelloWorld hello; hello.start(); @@ -162,7 +258,7 @@ auto main() -> int rtos::ThisThread::sleep_for(1s); - while (true) { + { if (auto is_touched = sensor.read(); is_touched) { log_info("Sensor touched"); } else { @@ -171,4 +267,8 @@ auto main() -> int rtos::ThisThread::sleep_for(100ms); } + + while (true) { + rtos::ThisThread::sleep_for(10min); + } } diff --git a/spikes/lk_coreled/main.cpp b/spikes/lk_coreled/main.cpp index 8606cc73d7..ea16b3d204 100644 --- a/spikes/lk_coreled/main.cpp +++ b/spikes/lk_coreled/main.cpp @@ -16,6 +16,101 @@ using namespace leka; using namespace std::chrono; +#include "mbed_stats.h" + +#include "drivers/Watchdog.h" +namespace { + +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((stop - start).count()); + + static auto idle_ratio = uint8_t {}; + 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((stop - start).count()); + + mbed_stats_cpu_get(&stats::cpu); + + idle_ratio = static_cast(((stats::cpu.idle_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + sleep_ratio = static_cast(((stats::cpu.sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + deep_sleep_ratio = + static_cast(((stats::cpu.deep_sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + + mbed_stats_stack_get(&stats::stack); + + stack_used_delta = static_cast(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((stack_used_size * 100) / stack_reserved_size); + + mbed_stats_heap_get(&stats::heap); + + heap_used_delta = static_cast(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((heap_used_size * 100) / heap_reserved_size); + + log_info( + "dt: %i, kck: %u, idl: %u%%, slp: %u%%, dsl: %u%%, sur: %u%% (%+i)[%u/" + "%u], hur: %u%% (%+i)[%u/%u]", + delta, kick_count, idle_ratio, 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 + +} // namespace + auto constexpr NUM_BELT_LEDS = 20; auto constexpr NUM_EARS_LEDS = 2; @@ -75,6 +170,7 @@ void changeColor(interface::LED &e, interface::LED &b) auto main() -> int { logger::init(); + watchdog::start(); HelloWorld hello; hello.start(); @@ -82,8 +178,12 @@ auto main() -> int rtos::ThisThread::sleep_for(2s); - while (true) { + { changeColor(ears, belt); rtos::ThisThread::sleep_for(1s); } + + while (true) { + rtos::ThisThread::sleep_for(10min); + } } diff --git a/spikes/lk_event_queue/main.cpp b/spikes/lk_event_queue/main.cpp index f58822480b..6fe247e03b 100644 --- a/spikes/lk_event_queue/main.cpp +++ b/spikes/lk_event_queue/main.cpp @@ -11,6 +11,101 @@ using namespace leka; using namespace std::chrono; +#include "mbed_stats.h" + +#include "drivers/Watchdog.h" +namespace { + +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((stop - start).count()); + + static auto idle_ratio = uint8_t {}; + 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((stop - start).count()); + + mbed_stats_cpu_get(&stats::cpu); + + idle_ratio = static_cast(((stats::cpu.idle_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + sleep_ratio = static_cast(((stats::cpu.sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + deep_sleep_ratio = + static_cast(((stats::cpu.deep_sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + + mbed_stats_stack_get(&stats::stack); + + stack_used_delta = static_cast(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((stack_used_size * 100) / stack_reserved_size); + + mbed_stats_heap_get(&stats::heap); + + heap_used_delta = static_cast(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((heap_used_size * 100) / heap_reserved_size); + + log_info( + "dt: %i, kck: %u, idl: %u%%, slp: %u%%, dsl: %u%%, sur: %u%% (%+i)[%u/" + "%u], hur: %u%% (%+i)[%u/%u]", + delta, kick_count, idle_ratio, 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 + +} // namespace + mbed::DigitalOut blinky(LED1, 0); class LEDManager @@ -90,12 +185,13 @@ CoreEventQueue equeue {}; auto main() -> int { logger::init(); + watchdog::start(); log_info("Hello, World!\n\n"); manager.init(); - while (true) { + { log_info("Turn on ref"); manager.turnOnRef(blinky); rtos::ThisThread::sleep_for(1s); @@ -132,4 +228,8 @@ auto main() -> int equeue.call(lambda); equeue.call(int_function_with_multiple_params, 42, std::string {"Hello, World"}, true); } + + while (true) { + rtos::ThisThread::sleep_for(10min); + } } diff --git a/spikes/lk_file_manager_kit/main.cpp b/spikes/lk_file_manager_kit/main.cpp index 83b8fe68f5..86690f904c 100644 --- a/spikes/lk_file_manager_kit/main.cpp +++ b/spikes/lk_file_manager_kit/main.cpp @@ -17,6 +17,101 @@ using namespace leka; using namespace std::chrono_literals; +#include "mbed_stats.h" + +#include "drivers/Watchdog.h" +namespace { + +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((stop - start).count()); + + static auto idle_ratio = uint8_t {}; + 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((stop - start).count()); + + mbed_stats_cpu_get(&stats::cpu); + + idle_ratio = static_cast(((stats::cpu.idle_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + sleep_ratio = static_cast(((stats::cpu.sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + deep_sleep_ratio = + static_cast(((stats::cpu.deep_sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + + mbed_stats_stack_get(&stats::stack); + + stack_used_delta = static_cast(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((stack_used_size * 100) / stack_reserved_size); + + mbed_stats_heap_get(&stats::heap); + + heap_used_delta = static_cast(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((heap_used_size * 100) / heap_reserved_size); + + log_info( + "dt: %i, kck: %u, idl: %u%%, slp: %u%%, dsl: %u%%, sur: %u%% (%+i)[%u/" + "%u], hur: %u%% (%+i)[%u/%u]", + delta, kick_count, idle_ratio, 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 + +} // namespace + auto file = FileManagerKit::File {}; const auto seek_temp = uint8_t {7}; const std::filesystem::path tmp_dir = "/fs/tmp/"; @@ -55,6 +150,7 @@ void printSHA256(std::span array) auto main() -> int { logger::init(); + watchdog::start(); log_info("Hello, World!\n\n"); @@ -76,45 +172,52 @@ auto main() -> int printSHA256(sha256); } - while (true) { + { auto t = rtos::Kernel::Clock::now() - start; log_info("A message from your board %s --> \"%s\" at %i s", MBED_CONF_APP_TARGET_NAME, hello.world, int(t.count() / 1000)); + rtos::ThisThread::sleep_for(1s); if (auto removed = std::filesystem::remove(test_filename); !removed) { log_error("The file or directory '%s' doesn't exist or fails to be removed", test_filename.c_str()); } else { log_info("File or directory '%s' removed", test_filename.c_str()); } + rtos::ThisThread::sleep_for(1s); if (auto removed = std::filesystem::remove(sub_test_dir); !removed) { log_error("The file or directory '%s' doesn't exist or fails to be removed", sub_test_dir.c_str()); } else { log_info("File or directory '%s' removed", sub_test_dir.c_str()); } + rtos::ThisThread::sleep_for(1s); if (auto removed = std::filesystem::remove(test_dir); !removed) { log_error("The file or directory '%s' doesn't exist or fails to be removed", test_dir.c_str()); } else { log_info("File or directory '%s' removed", test_dir.c_str()); } + rtos::ThisThread::sleep_for(1s); if (auto created = FileManagerKit::create_directory(test_dir); !created) { log_error("The directory '%s' already exists or fails to be created ", test_dir.c_str()); } else { log_info("Directory '%s' created", test_dir.c_str()); } + rtos::ThisThread::sleep_for(1s); if (auto created = FileManagerKit::create_directory(sub_test_dir); !created) { log_error("The directory '%s' already exists or fails to be created ", sub_test_dir.c_str()); } else { log_info("Directory '%s' created", sub_test_dir.c_str()); } + rtos::ThisThread::sleep_for(1s); if (auto open = file.open(test_filename, "w"); !open) { log_error("Fail to open file"); return EXIT_FAILURE; } + rtos::ThisThread::sleep_for(1s); log_info("File opened in mode 'w'"); @@ -124,6 +227,7 @@ auto main() -> int log_error("Fail to set buffer"); return EXIT_FAILURE; } + rtos::ThisThread::sleep_for(1s); log_info("Setting buffer"); @@ -134,6 +238,7 @@ auto main() -> int return EXIT_FAILURE; } log_info("File edited"); + rtos::ThisThread::sleep_for(1s); auto size = file.size(); if (size != std::size(input_data)) { @@ -141,6 +246,7 @@ auto main() -> int return EXIT_FAILURE; } log_info("Size : %d", size); + rtos::ThisThread::sleep_for(1s); auto output_data = std::array {}; @@ -149,6 +255,7 @@ auto main() -> int return EXIT_FAILURE; } log_info("File re-opened in mode 'r'"); + rtos::ThisThread::sleep_for(1s); file.rewind(); log_info("Reading..."); @@ -158,6 +265,7 @@ auto main() -> int } log_info("File read"); log_info("Data : %s", output_data.data()); + rtos::ThisThread::sleep_for(1s); file.seek(seek_temp); log_info("Position indicator set to %d", file.tell()); @@ -168,10 +276,15 @@ auto main() -> int } log_info("File read"); log_info("Data : %s", output_data.data()); + rtos::ThisThread::sleep_for(1s); file.close(); log_info("File closed"); log_info("\n\n"); rtos::ThisThread::sleep_for(10s); } + + while (true) { + rtos::ThisThread::sleep_for(10min); + } } diff --git a/spikes/lk_file_reception/main.cpp b/spikes/lk_file_reception/main.cpp index bea0090a60..6a997b30a0 100644 --- a/spikes/lk_file_reception/main.cpp +++ b/spikes/lk_file_reception/main.cpp @@ -16,6 +16,101 @@ using namespace leka; using namespace std::chrono; +#include "mbed_stats.h" + +#include "drivers/Watchdog.h" +namespace { + +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((stop - start).count()); + + static auto idle_ratio = uint8_t {}; + 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((stop - start).count()); + + mbed_stats_cpu_get(&stats::cpu); + + idle_ratio = static_cast(((stats::cpu.idle_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + sleep_ratio = static_cast(((stats::cpu.sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + deep_sleep_ratio = + static_cast(((stats::cpu.deep_sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + + mbed_stats_stack_get(&stats::stack); + + stack_used_delta = static_cast(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((stack_used_size * 100) / stack_reserved_size); + + mbed_stats_heap_get(&stats::heap); + + heap_used_delta = static_cast(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((heap_used_size * 100) / heap_reserved_size); + + log_info( + "dt: %i, kck: %u, idl: %u%%, slp: %u%%, dsl: %u%%, sur: %u%% (%+i)[%u/" + "%u], hur: %u%% (%+i)[%u/%u]", + delta, kick_count, idle_ratio, 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 + +} // namespace + auto web_access = CoreNetwork {}; auto web_file_handle = FileManagerKit::File {}; auto web_kit = WebKit(web_access, web_file_handle); @@ -74,10 +169,14 @@ auto main() -> int } } - while (true) { + { auto t = rtos::Kernel::Clock::now() - start; log_info("A message from your board %s --> \"%s\" at %i s", MBED_CONF_APP_TARGET_NAME, hello.world, int(t.count() / 1000)); - rtos::ThisThread::sleep_for(10s); + rtos::ThisThread::sleep_for(1s); + } + + while (true) { + rtos::ThisThread::sleep_for(10min); } } diff --git a/spikes/lk_flash_memory/main.cpp b/spikes/lk_flash_memory/main.cpp index 8de09affa5..a53b18f36d 100644 --- a/spikes/lk_flash_memory/main.cpp +++ b/spikes/lk_flash_memory/main.cpp @@ -16,9 +16,105 @@ using namespace leka; using namespace std::chrono; +#include "mbed_stats.h" + +#include "drivers/Watchdog.h" +namespace { + +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((stop - start).count()); + + static auto idle_ratio = uint8_t {}; + 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((stop - start).count()); + + mbed_stats_cpu_get(&stats::cpu); + + idle_ratio = static_cast(((stats::cpu.idle_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + sleep_ratio = static_cast(((stats::cpu.sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + deep_sleep_ratio = + static_cast(((stats::cpu.deep_sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + + mbed_stats_stack_get(&stats::stack); + + stack_used_delta = static_cast(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((stack_used_size * 100) / stack_reserved_size); + + mbed_stats_heap_get(&stats::heap); + + heap_used_delta = static_cast(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((heap_used_size * 100) / heap_reserved_size); + + log_info( + "dt: %i, kck: %u, idl: %u%%, slp: %u%%, dsl: %u%%, sur: %u%% (%+i)[%u/" + "%u], hur: %u%% (%+i)[%u/%u]", + delta, kick_count, idle_ratio, 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 + +} // namespace + auto main() -> int { logger::init(); + watchdog::start(); auto start = rtos::Kernel::Clock::now(); @@ -46,7 +142,7 @@ auto main() -> int coreis25lp.erase(); - while (true) { + { auto t = rtos::Kernel::Clock::now() - start; log_info("A message from your board %s --> \"%s\" at %i s\n", MBED_CONF_APP_TARGET_NAME, hello.world, int(t.count() / 1000)); @@ -64,4 +160,8 @@ auto main() -> int address = new_address; } } + + while (true) { + rtos::ThisThread::sleep_for(10min); + } } diff --git a/spikes/lk_fs/main.cpp b/spikes/lk_fs/main.cpp index 5d608c7a0f..96c5840688 100644 --- a/spikes/lk_fs/main.cpp +++ b/spikes/lk_fs/main.cpp @@ -35,6 +35,10 @@ using namespace leka; using namespace std::chrono; +#include "mbed_stats.h" + +#include "drivers/Watchdog.h" + namespace { namespace sd { @@ -90,8 +94,91 @@ auto hello = HelloWorld {}; auto com_utils_flag = rtos::EventFlags {}; auto com = ComUtils {com_utils_flag}; +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((stop - start).count()); + + static auto idle_ratio = uint8_t {}; + 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((stop - start).count()); + + mbed_stats_cpu_get(&stats::cpu); + + idle_ratio = static_cast(((stats::cpu.idle_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + sleep_ratio = static_cast(((stats::cpu.sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + deep_sleep_ratio = + static_cast(((stats::cpu.deep_sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + + mbed_stats_stack_get(&stats::stack); + + stack_used_delta = static_cast(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((stack_used_size * 100) / stack_reserved_size); + + mbed_stats_heap_get(&stats::heap); + + heap_used_delta = static_cast(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((heap_used_size * 100) / 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 { + watchdog::start(); + sd::init(); display::internal::corelcd.turnOn(); @@ -101,7 +188,7 @@ auto main() -> int hello.start(); - while (true) { + { com_utils_flag.wait_any(ComUtils::flags::data_available); auto path = com.getPath(); @@ -114,19 +201,19 @@ auto main() -> int if (auto is_missing = !(std::filesystem::exists(path)); is_missing) { com.write("NOK_MISSING:" + path.string()); - continue; + // continue; } if (auto is_empty = std::filesystem::is_empty(path); is_empty) { com.write("NOK_EMPTY:" + path.string()); - continue; + // continue; } if (path.string().ends_with(".jpg")) { videokit.displayImage(path); rtos::ThisThread::sleep_for(100ms); com.write("ACK_IMAGE:" + path.string()); - continue; + // continue; } if (path.string().ends_with(".avi")) { @@ -137,7 +224,7 @@ auto main() -> int videokit.stopVideo(); rtos::ThisThread::sleep_for(5ms); com.write("ACK_VIDEO:" + path.string()); - continue; + // continue; } if (auto *file = std::fopen(path.c_str(), "r"); file != nullptr) { @@ -148,13 +235,19 @@ auto main() -> int if (buf.empty()) { com.write("NOK_EMPTY:" + path.string()); - continue; + // continue; } com.write("ACK_FILE:" + path.string()); - continue; + // continue; } com.write("NOK_NOT_OPEN:" + path.string()); } + + display::internal::corelcd.enableDeepSleep(); + + while (true) { + rtos::ThisThread::sleep_for(10min); + } } diff --git a/spikes/lk_imu_kit/main.cpp b/spikes/lk_imu_kit/main.cpp index c93230c6ba..371f8ae798 100644 --- a/spikes/lk_imu_kit/main.cpp +++ b/spikes/lk_imu_kit/main.cpp @@ -13,8 +13,100 @@ using namespace std::chrono; using namespace leka; +#include "mbed_stats.h" + +#include "drivers/Watchdog.h" + namespace { +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((stop - start).count()); + + static auto idle_ratio = uint8_t {}; + 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((stop - start).count()); + + mbed_stats_cpu_get(&stats::cpu); + + idle_ratio = static_cast(((stats::cpu.idle_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + sleep_ratio = static_cast(((stats::cpu.sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + deep_sleep_ratio = + static_cast(((stats::cpu.deep_sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + + mbed_stats_stack_get(&stats::stack); + + stack_used_delta = static_cast(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((stack_used_size * 100) / stack_reserved_size); + + mbed_stats_heap_get(&stats::heap); + + heap_used_delta = static_cast(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((heap_used_size * 100) / heap_reserved_size); + + log_info( + "dt: %i, kck: %u, idl: %u%%, slp: %u%%, dsl: %u%%, sur: %u%% (%+i)[%u/" + "%u], hur: %u%% (%+i)[%u/%u]", + delta, kick_count, idle_ratio, 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 + namespace imu { namespace internal { @@ -36,6 +128,7 @@ auto main() -> int { rtos::ThisThread::sleep_for(140ms); logger::init(); + watchdog::start(); HelloWorld hello; hello.start(); @@ -46,9 +139,13 @@ auto main() -> int imukit.init(); imukit.start(); - while (true) { + { const auto [pitch, roll, yaw] = imukit.getEulerAngles(); log_info("Pitch : %7.2f, Roll : %7.2f Yaw : %7.2f", pitch, roll, yaw); rtos::ThisThread::sleep_for(140ms); } + + while (true) { + rtos::ThisThread::sleep_for(10min); + } } diff --git a/spikes/lk_lcd/main.cpp b/spikes/lk_lcd/main.cpp index 51c25ff1e8..e902e2767d 100644 --- a/spikes/lk_lcd/main.cpp +++ b/spikes/lk_lcd/main.cpp @@ -35,6 +35,101 @@ using namespace leka; using namespace std::chrono; +#include "mbed_stats.h" + +#include "drivers/Watchdog.h" +namespace { + +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((stop - start).count()); + + static auto idle_ratio = uint8_t {}; + 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((stop - start).count()); + + mbed_stats_cpu_get(&stats::cpu); + + idle_ratio = static_cast(((stats::cpu.idle_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + sleep_ratio = static_cast(((stats::cpu.sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + deep_sleep_ratio = + static_cast(((stats::cpu.deep_sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + + mbed_stats_stack_get(&stats::stack); + + stack_used_delta = static_cast(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((stack_used_size * 100) / stack_reserved_size); + + mbed_stats_heap_get(&stats::heap); + + heap_used_delta = static_cast(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((heap_used_size * 100) / heap_reserved_size); + + log_info( + "dt: %i, kck: %u, idl: %u%%, slp: %u%%, dsl: %u%%, sur: %u%% (%+i)[%u/" + "%u], hur: %u%% (%+i)[%u/%u]", + delta, kick_count, idle_ratio, 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 + +} // namespace + SDBlockDevice sd_blockdevice(SD_SPI_MOSI, SD_SPI_MISO, SD_SPI_SCK); FATFileSystem fatfs("fs"); @@ -81,6 +176,7 @@ auto main() -> int auto start = rtos::Kernel::Clock::now(); logger::init(); + watchdog::start(); log_info("Hello, World!\n\n"); @@ -96,36 +192,7 @@ auto main() -> int display::internal::corevideo.clearScreen(); rtos::ThisThread::sleep_for(1s); - static auto line = 1; - static CGColor foreground; - static CGColor background = CGColor::white; - - leka::logger::set_sink_function([](const char *str, std::size_t size) { - display::internal::corevideo.displayText(str, size, line, foreground, background); - }); - - for (int i = 1; i <= 10; i++) { - foreground = (i % 2 == 0) ? CGColor::black : CGColor::pure_red; - line = i * 2; - log_info("Line #%i", i); - rtos::ThisThread::sleep_for(100ms); - } - - rtos::ThisThread::sleep_for(500ms); - - leka::logger::set_sink_function([](const char *str, std::size_t size) { - display::internal::corevideo.displayText(str, size, 10, {0x00, 0x00, 0xFF}, CGColor::white); // write in blue - }); - - log_info( - "This sentence is supposed to be on multiple lines because it is too long to be displayed on " - "only one line of the screen."); - - rtos::ThisThread::sleep_for(1s); - - leka::logger::set_sink_function(logger::internal::default_sink_function); - - while (true) { + { auto t = rtos::Kernel::Clock::now() - start; log_info("A message from your board %s --> \"%s\" at %is", MBED_CONF_APP_TARGET_NAME, hello.world, int(t.count() / 1000)); @@ -149,4 +216,9 @@ auto main() -> int display::internal::corelcd.turnOff(); } + + display::internal::corelcd.enableDeepSleep(); + while (true) { + rtos::ThisThread::sleep_for(10min); + } } diff --git a/spikes/lk_led_kit/main.cpp b/spikes/lk_led_kit/main.cpp index 6e689fb360..81dd58fa6b 100644 --- a/spikes/lk_led_kit/main.cpp +++ b/spikes/lk_led_kit/main.cpp @@ -18,8 +18,99 @@ using namespace leka; using namespace std::chrono; +#include "mbed_stats.h" + +#include "drivers/Watchdog.h" namespace { +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((stop - start).count()); + + static auto idle_ratio = uint8_t {}; + 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((stop - start).count()); + + mbed_stats_cpu_get(&stats::cpu); + + idle_ratio = static_cast(((stats::cpu.idle_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + sleep_ratio = static_cast(((stats::cpu.sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + deep_sleep_ratio = + static_cast(((stats::cpu.deep_sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + + mbed_stats_stack_get(&stats::stack); + + stack_used_delta = static_cast(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((stack_used_size * 100) / stack_reserved_size); + + mbed_stats_heap_get(&stats::heap); + + heap_used_delta = static_cast(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((heap_used_size * 100) / heap_reserved_size); + + log_info( + "dt: %i, kck: %u, idl: %u%%, slp: %u%%, dsl: %u%%, sur: %u%% (%+i)[%u/" + "%u], hur: %u%% (%+i)[%u/%u]", + delta, kick_count, idle_ratio, 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 + namespace leds { namespace internal { @@ -67,6 +158,7 @@ auto ledkit = LedKit {leds::animations::event_loop, leds::ears, leds::belt}; auto main() -> int { logger::init(); + watchdog::start(); leds::turnOff(); log_info("Hello, World!\n\n"); @@ -75,113 +167,14 @@ auto main() -> int hello.start(); - while (true) { + { log_info("animation::ble_connection"); ledkit.start(&led::animation::ble_connection); rtos::ThisThread::sleep_for(2s); + } - log_info("animation::sleeping"); - ledkit.start(&led::animation::sleeping); - rtos::ThisThread::sleep_for(2s); - - log_info("animation::afraid_blue"); - ledkit.start(&led::animation::afraid_blue); - rtos::ThisThread::sleep_for(2s); - - log_info("animation::afraid_red"); - ledkit.start(&led::animation::afraid_red); - rtos::ThisThread::sleep_for(2s); - - log_info("animation::afraid_red_blue"); - ledkit.start(&led::animation::afraid_red_blue); - rtos::ThisThread::sleep_for(2s); - - log_info("animation::amazed"); - ledkit.start(&led::animation::amazed); - rtos::ThisThread::sleep_for(2s); - - log_info("animation::angry"); - ledkit.start(&led::animation::angry); - rtos::ThisThread::sleep_for(20s); - - log_info("animation::angry_short"); - ledkit.start(&led::animation::angry_short); - rtos::ThisThread::sleep_for(2s); - - log_info("animation::blink_green"); - ledkit.start(&led::animation::blink_green); - rtos::ThisThread::sleep_for(2s); - - log_info("animation::bubbles"); - ledkit.start(&led::animation::bubbles); - rtos::ThisThread::sleep_for(2s); - - log_info("animation::disgusted"); - ledkit.start(&led::animation::disgusted); - rtos::ThisThread::sleep_for(2s); - - log_info("animation::fire"); - ledkit.start(&led::animation::fire); - rtos::ThisThread::sleep_for(2s); - - log_info("animation::fly"); - ledkit.start(&led::animation::fly); - rtos::ThisThread::sleep_for(2s); - - log_info("animation::happy"); - ledkit.start(&led::animation::happy); - rtos::ThisThread::sleep_for(2s); - - log_info("animation::rainbow"); - ledkit.start(&led::animation::rainbow); - rtos::ThisThread::sleep_for(2s); - - log_info("animation::sad"); - ledkit.start(&led::animation::sad); - rtos::ThisThread::sleep_for(2s); - - log_info("animation::sad_cry"); - ledkit.start(&led::animation::sad_cry); - rtos::ThisThread::sleep_for(2s); - - log_info("animation::sick"); - ledkit.start(&led::animation::sick); - rtos::ThisThread::sleep_for(2s); - - log_info("animation::singing"); - ledkit.start(&led::animation::singing); - rtos::ThisThread::sleep_for(2s); - - log_info("animation::sleeping"); - ledkit.start(&led::animation::sleeping); - rtos::ThisThread::sleep_for(2s); - - log_info("animation::sneeze"); - ledkit.start(&led::animation::sneeze); - rtos::ThisThread::sleep_for(2s); - - log_info("animation::spin_blink"); - ledkit.start(&led::animation::spin_blink); - rtos::ThisThread::sleep_for(2s); - - log_info("animation::sprinkles"); - ledkit.start(&led::animation::sprinkles); - rtos::ThisThread::sleep_for(2s); - - log_info("animation::underwater"); - ledkit.start(&led::animation::underwater); - rtos::ThisThread::sleep_for(20s); - - log_info("animation::wake_up"); - ledkit.start(&led::animation::wake_up); - rtos::ThisThread::sleep_for(2s); - - log_info("animation::wink"); - ledkit.start(&led::animation::wink); - rtos::ThisThread::sleep_for(2s); - - log_info("animation::yawn"); - ledkit.start(&led::animation::yawn); - rtos::ThisThread::sleep_for(2s); + ledkit.stop(); + while (true) { + rtos::ThisThread::sleep_for(10min); } } diff --git a/spikes/lk_log_kit/main.cpp b/spikes/lk_log_kit/main.cpp index 48639e3ef8..7ca599d9e2 100644 --- a/spikes/lk_log_kit/main.cpp +++ b/spikes/lk_log_kit/main.cpp @@ -17,6 +17,102 @@ using namespace leka; using namespace mbed; using namespace std::chrono; +#include "mbed_stats.h" + +#include "drivers/Watchdog.h" + +namespace { + +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((stop - start).count()); + + static auto idle_ratio = uint8_t {}; + 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((stop - start).count()); + + mbed_stats_cpu_get(&stats::cpu); + + idle_ratio = static_cast(((stats::cpu.idle_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + sleep_ratio = static_cast(((stats::cpu.sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + deep_sleep_ratio = + static_cast(((stats::cpu.deep_sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + + mbed_stats_stack_get(&stats::stack); + + stack_used_delta = static_cast(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((stack_used_size * 100) / stack_reserved_size); + + mbed_stats_heap_get(&stats::heap); + + heap_used_delta = static_cast(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((heap_used_size * 100) / heap_reserved_size); + + log_info( + "dt: %i, kck: %u, idl: %u%%, slp: %u%%, dsl: %u%%, sur: %u%% (%+i)[%u/" + "%u], hur: %u%% (%+i)[%u/%u]", + delta, kick_count, idle_ratio, 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 + +} // namespace + auto hello = HelloWorld {}; auto thread_log_debug = rtos::Thread {}; @@ -57,7 +153,7 @@ auto main() -> int // ticker_log_from_isr.attach(log_isr_lambda, 5s); - while (true) { + { auto start = rtos::Kernel::Clock::now(); log_debug("A message from your board %s --> \"%s\" at %ims", MBED_CONF_APP_TARGET_NAME, hello.world, int(rtos::Kernel::Clock::now().time_since_epoch().count())); @@ -67,4 +163,8 @@ auto main() -> int rtos::ThisThread::sleep_for(3333ms); } + + while (true) { + rtos::ThisThread::sleep_for(10min); + } } diff --git a/spikes/lk_logs/CMakeLists.txt b/spikes/lk_logs/CMakeLists.txt new file mode 100644 index 0000000000..85b0323afe --- /dev/null +++ b/spikes/lk_logs/CMakeLists.txt @@ -0,0 +1,21 @@ +# Leka - LekaOS +# Copyright 2024 APF France handicap +# SPDX-License-Identifier: Apache-2.0 + +add_mbed_executable(spike_lk_logs) + +target_include_directories(spike_lk_logs + PRIVATE + . +) + +target_sources(spike_lk_logs + PRIVATE + main.cpp +) + +target_link_libraries(spike_lk_logs + CoreEventQueue +) + +target_link_custom_leka_targets(spike_lk_logs) diff --git a/spikes/lk_logs/main.cpp b/spikes/lk_logs/main.cpp new file mode 100644 index 0000000000..8590ca5fb9 --- /dev/null +++ b/spikes/lk_logs/main.cpp @@ -0,0 +1,124 @@ +// Leka - LekaOS +// Copyright 2024 APF France handicap +// SPDX-License-Identifier: Apache-2.0 + +#include "mbed_stats.h" + +#include "drivers/Watchdog.h" +#include "rtos/Kernel.h" +#include "rtos/ThisThread.h" + +#include "LogKit.h" + +using namespace leka; +using namespace std::chrono; + +// +// MARK: - Global definitions +// + +namespace { + +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((stop - start).count()); + + static auto idle_ratio = uint8_t {}; + 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((stop - start).count()); + + mbed_stats_cpu_get(&stats::cpu); + + idle_ratio = static_cast(((stats::cpu.idle_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + sleep_ratio = static_cast(((stats::cpu.sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + deep_sleep_ratio = + static_cast(((stats::cpu.deep_sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + + mbed_stats_stack_get(&stats::stack); + + stack_used_delta = static_cast(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((stack_used_size * 100) / stack_reserved_size); + + mbed_stats_heap_get(&stats::heap); + + heap_used_delta = static_cast(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((heap_used_size * 100) / heap_reserved_size); + + log_info( + "dt: %i, kck: %u, idl: %u%%, slp: %u%%, dsl: %u%%, sur: %u%% (%+i)[%u/" + "%u], hur: %u%% (%+i)[%u/%u]", + delta, kick_count, idle_ratio, 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 + +} // namespace + +auto main() -> int +{ + logger::init(); + watchdog::start(); + + rtos::ThisThread::sleep_for(1s); + + log_info("Hello, World!\n\n"); + + while (true) { + rtos::ThisThread::sleep_for(10min); + } +} diff --git a/spikes/lk_motion_kit/main.cpp b/spikes/lk_motion_kit/main.cpp index 26ec4622ca..d5e9179df4 100644 --- a/spikes/lk_motion_kit/main.cpp +++ b/spikes/lk_motion_kit/main.cpp @@ -21,8 +21,100 @@ using namespace std::chrono; using namespace leka; +#include "mbed_stats.h" + +#include "drivers/Watchdog.h" + namespace { +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((stop - start).count()); + + static auto idle_ratio = uint8_t {}; + 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((stop - start).count()); + + mbed_stats_cpu_get(&stats::cpu); + + idle_ratio = static_cast(((stats::cpu.idle_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + sleep_ratio = static_cast(((stats::cpu.sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + deep_sleep_ratio = + static_cast(((stats::cpu.deep_sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + + mbed_stats_stack_get(&stats::stack); + + stack_used_delta = static_cast(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((stack_used_size * 100) / stack_reserved_size); + + mbed_stats_heap_get(&stats::heap); + + heap_used_delta = static_cast(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((heap_used_size * 100) / heap_reserved_size); + + log_info( + "dt: %i, kck: %u, idl: %u%%, slp: %u%%, dsl: %u%%, sur: %u%% (%+i)[%u/" + "%u], hur: %u%% (%+i)[%u/%u]", + delta, kick_count, idle_ratio, 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 + namespace motors { namespace left { @@ -127,6 +219,7 @@ void onMagicCardAvailable(const MagicCard &card) auto main() -> int { logger::init(); + watchdog::start(); HelloWorld hello; hello.start(); @@ -137,7 +230,15 @@ auto main() -> int rfidkit.onTagActivated(onMagicCardAvailable); - while (true) { + { rtos::ThisThread::sleep_for(100ms); } + + rfidkit.enableDeepSleep(); + motors::left::motor.enableDeepSleep(); + motors::right::motor.enableDeepSleep(); + + while (true) { + rtos::ThisThread::sleep_for(10min); + } } diff --git a/spikes/lk_motors/main.cpp b/spikes/lk_motors/main.cpp index b824e5b375..c5f830891f 100644 --- a/spikes/lk_motors/main.cpp +++ b/spikes/lk_motors/main.cpp @@ -18,6 +18,101 @@ using namespace leka; using namespace std::chrono; +#include "mbed_stats.h" + +#include "drivers/Watchdog.h" +namespace { + +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((stop - start).count()); + + static auto idle_ratio = uint8_t {}; + 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((stop - start).count()); + + mbed_stats_cpu_get(&stats::cpu); + + idle_ratio = static_cast(((stats::cpu.idle_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + sleep_ratio = static_cast(((stats::cpu.sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + deep_sleep_ratio = + static_cast(((stats::cpu.deep_sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + + mbed_stats_stack_get(&stats::stack); + + stack_used_delta = static_cast(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((stack_used_size * 100) / stack_reserved_size); + + mbed_stats_heap_get(&stats::heap); + + heap_used_delta = static_cast(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((heap_used_size * 100) / heap_reserved_size); + + log_info( + "dt: %i, kck: %u, idl: %u%%, slp: %u%%, dsl: %u%%, sur: %u%% (%+i)[%u/" + "%u], hur: %u%% (%+i)[%u/%u]", + delta, kick_count, idle_ratio, 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 + +} // namespace + void spinLeft(interface::Motor &left, interface::Motor &right) { left.spin(Rotation::clockwise, 0.5F); @@ -39,6 +134,7 @@ void stop(interface::Motor &left, interface::Motor &right) auto main() -> int { logger::init(); + watchdog::start(); auto start = rtos::Kernel::Clock::now(); @@ -60,7 +156,7 @@ auto main() -> int auto motor_left = CoreMotor {motor_left_dir_1, motor_left_dir_2, motor_left_speed}; auto motor_right = CoreMotor {motor_right_dir_1, motor_right_dir_2, motor_right_speed}; - while (true) { + { auto t = rtos::Kernel::Clock::now() - start; log_info("A message from your board %s --> \"%s\" at %i s\n", MBED_CONF_APP_TARGET_NAME, hello.world, int(t.count() / 1000)); @@ -70,16 +166,16 @@ auto main() -> int log_info("Spin left..."); spinLeft(motor_left, motor_right); - rtos::ThisThread::sleep_for(5s); - - log_info("Spin right..."); - spinRight(motor_left, motor_right); - - rtos::ThisThread::sleep_for(5s); + rtos::ThisThread::sleep_for(1s); log_info("Spin stop..."); stop(motor_left, motor_right); + } - rtos::ThisThread::sleep_for(5s); + motor_left.enableDeepSleep(); + motor_right.enableDeepSleep(); + + while (true) { + rtos::ThisThread::sleep_for(10min); } } diff --git a/spikes/lk_qdac/main.cpp b/spikes/lk_qdac/main.cpp index f552690f5a..f19bcc62fd 100644 --- a/spikes/lk_qdac/main.cpp +++ b/spikes/lk_qdac/main.cpp @@ -19,6 +19,101 @@ using namespace std::chrono; // MARK: - Global definitions // +#include "mbed_stats.h" + +#include "drivers/Watchdog.h" +namespace { + +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((stop - start).count()); + + static auto idle_ratio = uint8_t {}; + 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((stop - start).count()); + + mbed_stats_cpu_get(&stats::cpu); + + idle_ratio = static_cast(((stats::cpu.idle_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + sleep_ratio = static_cast(((stats::cpu.sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + deep_sleep_ratio = + static_cast(((stats::cpu.deep_sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + + mbed_stats_stack_get(&stats::stack); + + stack_used_delta = static_cast(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((stack_used_size * 100) / stack_reserved_size); + + mbed_stats_heap_get(&stats::heap); + + heap_used_delta = static_cast(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((heap_used_size * 100) / heap_reserved_size); + + log_info( + "dt: %i, kck: %u, idl: %u%%, slp: %u%%, dsl: %u%%, sur: %u%% (%+i)[%u/" + "%u], hur: %u%% (%+i)[%u/%u]", + delta, kick_count, idle_ratio, 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 + +} // namespace + auto corei2c = CoreI2C {PinName::SENSOR_PROXIMITY_MUX_I2C_SDA, PinName::SENSOR_PROXIMITY_MUX_I2C_SCL}; auto dac = CoreQDACMCP4728 {corei2c, 0xC2}; @@ -39,6 +134,7 @@ void printInputRegisters() auto main() -> int { logger::init(); + watchdog::start(); log_info("Hello, World!\n\n"); @@ -73,4 +169,8 @@ auto main() -> int rtos::ThisThread::sleep_for(100ms); printInputRegisters(); rtos::ThisThread::sleep_for(1s); + + while (true) { + rtos::ThisThread::sleep_for(10min); + } } diff --git a/spikes/lk_reinforcer/main.cpp b/spikes/lk_reinforcer/main.cpp index 478f9000fa..3326b3c48b 100644 --- a/spikes/lk_reinforcer/main.cpp +++ b/spikes/lk_reinforcer/main.cpp @@ -45,8 +45,99 @@ using namespace std::chrono; // MARK: - Global definitions // +#include "mbed_stats.h" + +#include "drivers/Watchdog.h" namespace { +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((stop - start).count()); + + static auto idle_ratio = uint8_t {}; + 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((stop - start).count()); + + mbed_stats_cpu_get(&stats::cpu); + + idle_ratio = static_cast(((stats::cpu.idle_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + sleep_ratio = static_cast(((stats::cpu.sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + deep_sleep_ratio = + static_cast(((stats::cpu.deep_sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + + mbed_stats_stack_get(&stats::stack); + + stack_used_delta = static_cast(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((stack_used_size * 100) / stack_reserved_size); + + mbed_stats_heap_get(&stats::heap); + + heap_used_delta = static_cast(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((heap_used_size * 100) / heap_reserved_size); + + log_info( + "dt: %i, kck: %u, idl: %u%%, slp: %u%%, dsl: %u%%, sur: %u%% (%+i)[%u/" + "%u], hur: %u%% (%+i)[%u/%u]", + delta, kick_count, idle_ratio, 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 + namespace sd { namespace internal { @@ -190,6 +281,7 @@ auto hello = HelloWorld {}; auto main() -> int { logger::init(); + watchdog::start(); leds::turnOff(); log_info("Hello, World!\n\n"); @@ -207,20 +299,18 @@ auto main() -> int rtos::ThisThread::sleep_for(3s); - while (true) { - reinforcerkit.play(ReinforcerKit::Reinforcer::Fire); - rtos::ThisThread::sleep_for(10s); - - reinforcerkit.play(ReinforcerKit::Reinforcer::BlinkGreen); - rtos::ThisThread::sleep_for(10s); - + { reinforcerkit.play(ReinforcerKit::Reinforcer::SpinBlink); - rtos::ThisThread::sleep_for(10s); + rtos::ThisThread::sleep_for(1s); + } - reinforcerkit.play(ReinforcerKit::Reinforcer::Sprinkles); - rtos::ThisThread::sleep_for(5s); + reinforcerkit.stop(); - reinforcerkit.play(ReinforcerKit::Reinforcer::Rainbow); - rtos::ThisThread::sleep_for(5s); + display::internal::corelcd.enableDeepSleep(); + motor::left.enableDeepSleep(); + motor::right.enableDeepSleep(); + + while (true) { + rtos::ThisThread::sleep_for(10min); } } diff --git a/spikes/lk_rfid/main.cpp b/spikes/lk_rfid/main.cpp index 70cea240d0..21e7d6ee74 100644 --- a/spikes/lk_rfid/main.cpp +++ b/spikes/lk_rfid/main.cpp @@ -16,9 +16,105 @@ using namespace leka; using namespace std::chrono; +#include "mbed_stats.h" + +#include "drivers/Watchdog.h" +namespace { + +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((stop - start).count()); + + static auto idle_ratio = uint8_t {}; + 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((stop - start).count()); + + mbed_stats_cpu_get(&stats::cpu); + + idle_ratio = static_cast(((stats::cpu.idle_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + sleep_ratio = static_cast(((stats::cpu.sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + deep_sleep_ratio = + static_cast(((stats::cpu.deep_sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + + mbed_stats_stack_get(&stats::stack); + + stack_used_delta = static_cast(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((stack_used_size * 100) / stack_reserved_size); + + mbed_stats_heap_get(&stats::heap); + + heap_used_delta = static_cast(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((heap_used_size * 100) / heap_reserved_size); + + log_info( + "dt: %i, kck: %u, idl: %u%%, slp: %u%%, dsl: %u%%, sur: %u%% (%+i)[%u/" + "%u], hur: %u%% (%+i)[%u/%u]", + delta, kick_count, idle_ratio, 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 + +} // namespace + auto main() -> int { logger::init(); + watchdog::start(); log_info("Hello, World!\n\n"); @@ -26,7 +122,7 @@ auto main() -> int auto rfidreader = CoreRFIDReaderCR95HF(rfidserial); auto rfidkit = RFIDKit(rfidreader); - rtos::ThisThread::sleep_for(2s); + rtos::ThisThread::sleep_for(100ms); rfidkit.init(); rfidkit.onTagActivated([](const MagicCard &card) { log_debug("Card id: %i", card.getId()); }); @@ -34,7 +130,12 @@ auto main() -> int HelloWorld hello; hello.start(); + { + rtos::ThisThread::sleep_for(10ms); + } + + rfidkit.enableDeepSleep(); while (true) { - rtos::ThisThread::sleep_for(10s); + rtos::ThisThread::sleep_for(10min); } } diff --git a/spikes/lk_sensors_battery/main.cpp b/spikes/lk_sensors_battery/main.cpp index 3717f067cd..148951f0cb 100644 --- a/spikes/lk_sensors_battery/main.cpp +++ b/spikes/lk_sensors_battery/main.cpp @@ -14,6 +14,101 @@ using namespace leka; using namespace std::chrono; +#include "mbed_stats.h" + +#include "drivers/Watchdog.h" +namespace { + +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((stop - start).count()); + + static auto idle_ratio = uint8_t {}; + 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((stop - start).count()); + + mbed_stats_cpu_get(&stats::cpu); + + idle_ratio = static_cast(((stats::cpu.idle_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + sleep_ratio = static_cast(((stats::cpu.sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + deep_sleep_ratio = + static_cast(((stats::cpu.deep_sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + + mbed_stats_stack_get(&stats::stack); + + stack_used_delta = static_cast(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((stack_used_size * 100) / stack_reserved_size); + + mbed_stats_heap_get(&stats::heap); + + heap_used_delta = static_cast(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((heap_used_size * 100) / heap_reserved_size); + + log_info( + "dt: %i, kck: %u, idl: %u%%, slp: %u%%, dsl: %u%%, sur: %u%% (%+i)[%u/" + "%u], hur: %u%% (%+i)[%u/%u]", + delta, kick_count, idle_ratio, 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 + +} // namespace + auto charge_input = mbed::InterruptIn {PinName::BATTERY_CHARGE_STATUS}; auto corebattery = CoreBattery {PinName::BATTERY_VOLTAGE, charge_input}; auto batterykit = BatteryKit {corebattery}; @@ -31,6 +126,7 @@ void logBatteryNewLevel(uint8_t battery_new_level) auto main() -> int { logger::init(); + watchdog::start(); log_info("Hello, World!\n\n"); @@ -48,6 +144,6 @@ auto main() -> int batterykit.startEventHandler(); while (true) { - rtos::ThisThread::sleep_for(1s); + rtos::ThisThread::sleep_for(10min); } } diff --git a/spikes/lk_sensors_imu_lsm6dsox/main.cpp b/spikes/lk_sensors_imu_lsm6dsox/main.cpp index 554511ee4a..706d076f48 100644 --- a/spikes/lk_sensors_imu_lsm6dsox/main.cpp +++ b/spikes/lk_sensors_imu_lsm6dsox/main.cpp @@ -14,8 +14,100 @@ using namespace std::chrono; using namespace leka; +#include "mbed_stats.h" + +#include "drivers/Watchdog.h" + namespace { +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((stop - start).count()); + + static auto idle_ratio = uint8_t {}; + 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((stop - start).count()); + + mbed_stats_cpu_get(&stats::cpu); + + idle_ratio = static_cast(((stats::cpu.idle_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + sleep_ratio = static_cast(((stats::cpu.sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + deep_sleep_ratio = + static_cast(((stats::cpu.deep_sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + + mbed_stats_stack_get(&stats::stack); + + stack_used_delta = static_cast(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((stack_used_size * 100) / stack_reserved_size); + + mbed_stats_heap_get(&stats::heap); + + heap_used_delta = static_cast(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((heap_used_size * 100) / heap_reserved_size); + + log_info( + "dt: %i, kck: %u, idl: %u%%, slp: %u%%, dsl: %u%%, sur: %u%% (%+i)[%u/" + "%u], hur: %u%% (%+i)[%u/%u]", + delta, kick_count, idle_ratio, 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 + namespace imu { namespace internal { @@ -34,6 +126,7 @@ namespace imu { auto main() -> int { logger::init(); + watchdog::start(); HelloWorld hello; hello.start(); @@ -53,7 +146,7 @@ auto main() -> int imu::lsm6dsox.registerOnGyDataReadyCallback(callback); - while (true) { + { log_info("Setting normal power mode for 5s"); rtos::ThisThread::sleep_for(1s); imu::lsm6dsox.setPowerMode(CoreLSM6DSOX::PowerMode::Normal); @@ -66,4 +159,8 @@ auto main() -> int rtos::ThisThread::sleep_for(5s); } + + while (true) { + rtos::ThisThread::sleep_for(10min); + } } diff --git a/spikes/lk_sensors_imu_lsm6dsox_fusion_calibration/main.cpp b/spikes/lk_sensors_imu_lsm6dsox_fusion_calibration/main.cpp index f9aee3d5db..54f0303aac 100644 --- a/spikes/lk_sensors_imu_lsm6dsox_fusion_calibration/main.cpp +++ b/spikes/lk_sensors_imu_lsm6dsox_fusion_calibration/main.cpp @@ -20,8 +20,100 @@ using namespace std::chrono; using namespace leka; +#include "mbed_stats.h" + +#include "drivers/Watchdog.h" + namespace { +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((stop - start).count()); + + static auto idle_ratio = uint8_t {}; + 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((stop - start).count()); + + mbed_stats_cpu_get(&stats::cpu); + + idle_ratio = static_cast(((stats::cpu.idle_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + sleep_ratio = static_cast(((stats::cpu.sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + deep_sleep_ratio = + static_cast(((stats::cpu.deep_sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + + mbed_stats_stack_get(&stats::stack); + + stack_used_delta = static_cast(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((stack_used_size * 100) / stack_reserved_size); + + mbed_stats_heap_get(&stats::heap); + + heap_used_delta = static_cast(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((heap_used_size * 100) / heap_reserved_size); + + log_info( + "dt: %i, kck: %u, idl: %u%%, slp: %u%%, dsl: %u%%, sur: %u%% (%+i)[%u/" + "%u], hur: %u%% (%+i)[%u/%u]", + delta, kick_count, idle_ratio, 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 + namespace imu { namespace internal { @@ -116,6 +208,7 @@ namespace fusion { auto main() -> int { logger::init(); + watchdog::start(); rtos::ThisThread::sleep_for(1s); @@ -139,7 +232,11 @@ auto main() -> int imu::lsm6dsox.registerOnGyDataReadyCallback(fusion::callback); imu::lsm6dsox.setPowerMode(CoreLSM6DSOX::PowerMode::Normal); - while (true) { + { rtos::ThisThread::sleep_for(5s); } + + while (true) { + rtos::ThisThread::sleep_for(10min); + } } diff --git a/spikes/lk_sensors_light/main.cpp b/spikes/lk_sensors_light/main.cpp index c9360adff7..847cac902f 100644 --- a/spikes/lk_sensors_light/main.cpp +++ b/spikes/lk_sensors_light/main.cpp @@ -16,9 +16,106 @@ using namespace leka; using namespace std::chrono; +#include "mbed_stats.h" + +#include "drivers/Watchdog.h" + +namespace { + +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((stop - start).count()); + + static auto idle_ratio = uint8_t {}; + 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((stop - start).count()); + + mbed_stats_cpu_get(&stats::cpu); + + idle_ratio = static_cast(((stats::cpu.idle_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + sleep_ratio = static_cast(((stats::cpu.sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + deep_sleep_ratio = + static_cast(((stats::cpu.deep_sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + + mbed_stats_stack_get(&stats::stack); + + stack_used_delta = static_cast(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((stack_used_size * 100) / stack_reserved_size); + + mbed_stats_heap_get(&stats::heap); + + heap_used_delta = static_cast(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((heap_used_size * 100) / heap_reserved_size); + + log_info( + "dt: %i, kck: %u, idl: %u%%, slp: %u%%, dsl: %u%%, sur: %u%% (%+i)[%u/" + "%u], hur: %u%% (%+i)[%u/%u]", + delta, kick_count, idle_ratio, 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 + +} // namespace + auto main() -> int { logger::init(); + watchdog::start(); log_info("Hello, World!\n\n"); rtos::ThisThread::sleep_for(2s); @@ -28,8 +125,12 @@ auto main() -> int CoreLightSensor lightSensor(SENSOR_LIGHT_ADC_INPUT); - while (true) { + { log_info("Current luminosity: %.4f", lightSensor.readLuminosity()); rtos::ThisThread::sleep_for(33ms); } + + while (true) { + rtos::ThisThread::sleep_for(10min); + } } diff --git a/spikes/lk_sensors_microphone/main.cpp b/spikes/lk_sensors_microphone/main.cpp index f887aeed0b..a01e54ece5 100644 --- a/spikes/lk_sensors_microphone/main.cpp +++ b/spikes/lk_sensors_microphone/main.cpp @@ -16,6 +16,102 @@ using namespace leka; using namespace std::chrono; +#include "mbed_stats.h" + +#include "drivers/Watchdog.h" + +namespace { + +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((stop - start).count()); + + static auto idle_ratio = uint8_t {}; + 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((stop - start).count()); + + mbed_stats_cpu_get(&stats::cpu); + + idle_ratio = static_cast(((stats::cpu.idle_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + sleep_ratio = static_cast(((stats::cpu.sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + deep_sleep_ratio = + static_cast(((stats::cpu.deep_sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + + mbed_stats_stack_get(&stats::stack); + + stack_used_delta = static_cast(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((stack_used_size * 100) / stack_reserved_size); + + mbed_stats_heap_get(&stats::heap); + + heap_used_delta = static_cast(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((heap_used_size * 100) / heap_reserved_size); + + log_info( + "dt: %i, kck: %u, idl: %u%%, slp: %u%%, dsl: %u%%, sur: %u%% (%+i)[%u/" + "%u], hur: %u%% (%+i)[%u/%u]", + delta, kick_count, idle_ratio, 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 + +} // namespace + // SMA - Small Moving Average template class SMA @@ -68,6 +164,7 @@ auto RMS(std::deque &data, int newvalue) -> int auto main() -> int { logger::init(); + watchdog::start(); HelloWorld hello; hello.start(); @@ -82,7 +179,7 @@ auto main() -> int CoreMicrophone microphone(MCU_MIC_INPUT); - while (true) { + { auto rawValue = static_cast(1000 * microphone.readVolume()); if (rawValue <= 458) { @@ -99,4 +196,8 @@ auto main() -> int rtos::ThisThread::sleep_for(6ms); } } + + while (true) { + rtos::ThisThread::sleep_for(10min); + } } diff --git a/spikes/lk_sensors_temperature_humidity/main.cpp b/spikes/lk_sensors_temperature_humidity/main.cpp index 34904d8f3c..c7a433f09d 100644 --- a/spikes/lk_sensors_temperature_humidity/main.cpp +++ b/spikes/lk_sensors_temperature_humidity/main.cpp @@ -15,9 +15,106 @@ using namespace leka; using namespace std::chrono; +#include "mbed_stats.h" + +#include "drivers/Watchdog.h" + +namespace { + +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((stop - start).count()); + + static auto idle_ratio = uint8_t {}; + 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((stop - start).count()); + + mbed_stats_cpu_get(&stats::cpu); + + idle_ratio = static_cast(((stats::cpu.idle_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + sleep_ratio = static_cast(((stats::cpu.sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + deep_sleep_ratio = + static_cast(((stats::cpu.deep_sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + + mbed_stats_stack_get(&stats::stack); + + stack_used_delta = static_cast(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((stack_used_size * 100) / stack_reserved_size); + + mbed_stats_heap_get(&stats::heap); + + heap_used_delta = static_cast(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((heap_used_size * 100) / heap_reserved_size); + + log_info( + "dt: %i, kck: %u, idl: %u%%, slp: %u%%, dsl: %u%%, sur: %u%% (%+i)[%u/" + "%u], hur: %u%% (%+i)[%u/%u]", + delta, kick_count, idle_ratio, 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 + +} // namespace + auto main() -> int { logger::init(); + watchdog::start(); log_info("Hello, World!\n\n"); @@ -37,7 +134,7 @@ auto main() -> int log_info("Humidity calibration values: slope: %f, y-intercept: %f\n", coefficients_humidity.slope, coefficients_humidity.y_intercept); - while (true) { + { auto temperature = corehts.getTemperatureCelsius(); auto humidity = corehts.getRelativeHumidity(); @@ -45,4 +142,8 @@ auto main() -> int rtos::ThisThread::sleep_for(1s); } + + while (true) { + rtos::ThisThread::sleep_for(10min); + } } diff --git a/spikes/lk_serial_number/main.cpp b/spikes/lk_serial_number/main.cpp index 0d69f9b522..80f52f6d64 100644 --- a/spikes/lk_serial_number/main.cpp +++ b/spikes/lk_serial_number/main.cpp @@ -14,6 +14,102 @@ using namespace leka; using namespace std::chrono_literals; +#include "mbed_stats.h" + +#include "drivers/Watchdog.h" + +namespace { + +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((stop - start).count()); + + static auto idle_ratio = uint8_t {}; + 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((stop - start).count()); + + mbed_stats_cpu_get(&stats::cpu); + + idle_ratio = static_cast(((stats::cpu.idle_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + sleep_ratio = static_cast(((stats::cpu.sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + deep_sleep_ratio = + static_cast(((stats::cpu.deep_sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + + mbed_stats_stack_get(&stats::stack); + + stack_used_delta = static_cast(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((stack_used_size * 100) / stack_reserved_size); + + mbed_stats_heap_get(&stats::heap); + + heap_used_delta = static_cast(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((heap_used_size * 100) / heap_reserved_size); + + log_info( + "dt: %i, kck: %u, idl: %u%%, slp: %u%%, dsl: %u%%, sur: %u%% (%+i)[%u/" + "%u], hur: %u%% (%+i)[%u/%u]", + delta, kick_count, idle_ratio, 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 + +} // namespace + SDBlockDevice sd_blockdevice(SD_SPI_MOSI, SD_SPI_MISO, SD_SPI_SCK); FATFileSystem fatfs("fs"); @@ -33,6 +129,7 @@ void initializeSD() auto main() -> int { logger::init(); + watchdog::start(); log_info("Hello, World!\n\n"); @@ -57,10 +154,14 @@ auto main() -> int auto short_serial_number = serialnumberkit.getShortSerialNumber(); log_info("S/N (short): %s", short_serial_number.data()); - while (true) { + { auto t = rtos::Kernel::Clock::now() - start; log_info("A message from your board %s --> \"%s\" at %i s", MBED_CONF_APP_TARGET_NAME, hello.world, int(t.count() / 1000)); - rtos::ThisThread::sleep_for(10s); + rtos::ThisThread::sleep_for(1s); + } + + while (true) { + rtos::ThisThread::sleep_for(10min); } } diff --git a/spikes/lk_ticker_timeout/main.cpp b/spikes/lk_ticker_timeout/main.cpp index 9e8da6e748..72f6d8edd2 100644 --- a/spikes/lk_ticker_timeout/main.cpp +++ b/spikes/lk_ticker_timeout/main.cpp @@ -12,12 +12,109 @@ using namespace leka; using namespace std::chrono; +#include "mbed_stats.h" + +#include "drivers/Watchdog.h" + +namespace { + +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((stop - start).count()); + + static auto idle_ratio = uint8_t {}; + 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((stop - start).count()); + + mbed_stats_cpu_get(&stats::cpu); + + idle_ratio = static_cast(((stats::cpu.idle_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + sleep_ratio = static_cast(((stats::cpu.sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + deep_sleep_ratio = + static_cast(((stats::cpu.deep_sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + + mbed_stats_stack_get(&stats::stack); + + stack_used_delta = static_cast(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((stack_used_size * 100) / stack_reserved_size); + + mbed_stats_heap_get(&stats::heap); + + heap_used_delta = static_cast(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((heap_used_size * 100) / heap_reserved_size); + + log_info( + "dt: %i, kck: %u, idl: %u%%, slp: %u%%, dsl: %u%%, sur: %u%% (%+i)[%u/" + "%u], hur: %u%% (%+i)[%u/%u]", + delta, kick_count, idle_ratio, 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 + +} // namespace + auto ticker = CoreTicker {}; auto timeout = CoreTimeout {}; auto main() -> int { logger::init(); + watchdog::start(); log_info("Hello, World!\n\n"); @@ -49,8 +146,12 @@ auto main() -> int rtos::ThisThread::sleep_for(500ms); // lambda is not called timeout.stop(); - while (true) { + { log_info("Main thread running..."); rtos::ThisThread::sleep_for(5s); } + + while (true) { + rtos::ThisThread::sleep_for(10min); + } } diff --git a/spikes/lk_touch_sensor_kit/main.cpp b/spikes/lk_touch_sensor_kit/main.cpp index 2b1aa6ab61..12a1323b50 100644 --- a/spikes/lk_touch_sensor_kit/main.cpp +++ b/spikes/lk_touch_sensor_kit/main.cpp @@ -19,6 +19,102 @@ using namespace leka; using namespace std::chrono_literals; +#include "mbed_stats.h" + +#include "drivers/Watchdog.h" + +namespace { + +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((stop - start).count()); + + static auto idle_ratio = uint8_t {}; + 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((stop - start).count()); + + mbed_stats_cpu_get(&stats::cpu); + + idle_ratio = static_cast(((stats::cpu.idle_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + sleep_ratio = static_cast(((stats::cpu.sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + deep_sleep_ratio = + static_cast(((stats::cpu.deep_sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + + mbed_stats_stack_get(&stats::stack); + + stack_used_delta = static_cast(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((stack_used_size * 100) / stack_reserved_size); + + mbed_stats_heap_get(&stats::heap); + + heap_used_delta = static_cast(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((heap_used_size * 100) / heap_reserved_size); + + log_info( + "dt: %i, kck: %u, idl: %u%%, slp: %u%%, dsl: %u%%, sur: %u%% (%+i)[%u/" + "%u], hur: %u%% (%+i)[%u/%u]", + delta, kick_count, idle_ratio, 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 + +} // namespace + namespace touch { inline auto corei2c = CoreI2C {PinName::SENSOR_PROXIMITY_MUX_I2C_SDA, PinName::SENSOR_PROXIMITY_MUX_I2C_SCL}; @@ -143,6 +239,7 @@ void printSensorReleased(Position position) auto main() -> int { logger::init(); + watchdog::start(); HelloWorld hello {}; hello.start(); @@ -167,8 +264,12 @@ auto main() -> int log_info("Enable Touch"); touch_sensor_kit.enable(); - while (true) { + { log_info("Still alive"); rtos::ThisThread::sleep_for(5s); } + + while (true) { + rtos::ThisThread::sleep_for(10min); + } } diff --git a/spikes/lk_update_process_app_base/main.cpp b/spikes/lk_update_process_app_base/main.cpp index fd6713011d..6f0405196e 100644 --- a/spikes/lk_update_process_app_base/main.cpp +++ b/spikes/lk_update_process_app_base/main.cpp @@ -3,6 +3,7 @@ // SPDX-License-Identifier: Apache-2.0 #include "drivers/BufferedSerial.h" +#include "rtos/ThisThread.h" #include "CoreFlashIS25LP016D.h" #include "CoreFlashManagerIS25LP016D.h" @@ -20,6 +21,102 @@ using namespace leka; using namespace std::chrono; +#include "mbed_stats.h" + +#include "drivers/Watchdog.h" + +namespace { + +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((stop - start).count()); + + static auto idle_ratio = uint8_t {}; + 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((stop - start).count()); + + mbed_stats_cpu_get(&stats::cpu); + + idle_ratio = static_cast(((stats::cpu.idle_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + sleep_ratio = static_cast(((stats::cpu.sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + deep_sleep_ratio = + static_cast(((stats::cpu.deep_sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + + mbed_stats_stack_get(&stats::stack); + + stack_used_delta = static_cast(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((stack_used_size * 100) / stack_reserved_size); + + mbed_stats_heap_get(&stats::heap); + + heap_used_delta = static_cast(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((heap_used_size * 100) / heap_reserved_size); + + log_info( + "dt: %i, kck: %u, idl: %u%%, slp: %u%%, dsl: %u%%, sur: %u%% (%+i)[%u/" + "%u], hur: %u%% (%+i)[%u/%u]", + delta, kick_count, idle_ratio, 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 + +} // namespace + HelloWorld hello; SDBlockDevice sd_blockdevice(SD_SPI_MOSI, SD_SPI_MISO, SD_SPI_SCK); @@ -50,6 +147,7 @@ void initializeSD() auto main() -> int { logger::init(); + watchdog::start(); log_info("Hello, Application!\n"); @@ -74,4 +172,8 @@ auto main() -> int } else { log_error("Failed to set secondary image pending: %d", ret); } + + while (true) { + rtos::ThisThread::sleep_for(10min); + } } diff --git a/spikes/lk_update_process_app_update/main.cpp b/spikes/lk_update_process_app_update/main.cpp index 3dd8d3119f..0ecc01e50a 100644 --- a/spikes/lk_update_process_app_update/main.cpp +++ b/spikes/lk_update_process_app_update/main.cpp @@ -16,6 +16,102 @@ using namespace leka; using namespace std::chrono; +#include "mbed_stats.h" + +#include "drivers/Watchdog.h" + +namespace { + +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((stop - start).count()); + + static auto idle_ratio = uint8_t {}; + 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((stop - start).count()); + + mbed_stats_cpu_get(&stats::cpu); + + idle_ratio = static_cast(((stats::cpu.idle_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + sleep_ratio = static_cast(((stats::cpu.sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + deep_sleep_ratio = + static_cast(((stats::cpu.deep_sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + + mbed_stats_stack_get(&stats::stack); + + stack_used_delta = static_cast(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((stack_used_size * 100) / stack_reserved_size); + + mbed_stats_heap_get(&stats::heap); + + heap_used_delta = static_cast(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((heap_used_size * 100) / heap_reserved_size); + + log_info( + "dt: %i, kck: %u, idl: %u%%, slp: %u%%, dsl: %u%%, sur: %u%% (%+i)[%u/" + "%u], hur: %u%% (%+i)[%u/%u]", + delta, kick_count, idle_ratio, 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 + +} // namespace + auto get_secondary_bd() -> mbed::BlockDevice * { static auto _bd = QSPIFBlockDevice {}; @@ -28,6 +124,7 @@ auto get_secondary_bd() -> mbed::BlockDevice * auto main() -> int { logger::init(); + watchdog::start(); log_info("Hello, Update!\n"); @@ -45,10 +142,14 @@ auto main() -> int log_error("Failed to confirm boot: %d", ret); } - while (true) { + { auto t = rtos::Kernel::Clock::now() - start; log_info("A message from your board %s --> \"%s\" at %i s\n", MBED_CONF_APP_TARGET_NAME, hello.world, int(t.count() / 1000)); rtos::ThisThread::sleep_for(1s); } + + while (true) { + rtos::ThisThread::sleep_for(10min); + } } diff --git a/spikes/lk_watchdog_isr/main.cpp b/spikes/lk_watchdog_isr/main.cpp index ba45281e1e..851ae5fcff 100644 --- a/spikes/lk_watchdog_isr/main.cpp +++ b/spikes/lk_watchdog_isr/main.cpp @@ -187,6 +187,7 @@ auto main() -> int rtos::ThisThread::sleep_for(1s); + rfidkit.enableDeepSleep(); while (true) { // ? Un/Comment one or the other for testing log_info("Main thread still alive"); diff --git a/spikes/lk_wifi/main.cpp b/spikes/lk_wifi/main.cpp index 23e9f6af7f..a071df605f 100644 --- a/spikes/lk_wifi/main.cpp +++ b/spikes/lk_wifi/main.cpp @@ -14,6 +14,102 @@ using namespace leka; using namespace std::chrono; +#include "mbed_stats.h" + +#include "drivers/Watchdog.h" + +namespace { + +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((stop - start).count()); + + static auto idle_ratio = uint8_t {}; + 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((stop - start).count()); + + mbed_stats_cpu_get(&stats::cpu); + + idle_ratio = static_cast(((stats::cpu.idle_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + sleep_ratio = static_cast(((stats::cpu.sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + deep_sleep_ratio = + static_cast(((stats::cpu.deep_sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + + mbed_stats_stack_get(&stats::stack); + + stack_used_delta = static_cast(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((stack_used_size * 100) / stack_reserved_size); + + mbed_stats_heap_get(&stats::heap); + + heap_used_delta = static_cast(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((heap_used_size * 100) / heap_reserved_size); + + log_info( + "dt: %i, kck: %u, idl: %u%%, slp: %u%%, dsl: %u%%, sur: %u%% (%+i)[%u/" + "%u], hur: %u%% (%+i)[%u/%u]", + delta, kick_count, idle_ratio, 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 + +} // namespace + constexpr auto network_ssid {"USER_SSID"}; constexpr auto network_password {"USER_PASSWORD"}; @@ -22,6 +118,7 @@ void runExample(WiFiInterface &wifi_interface); auto main() -> int { logger::init(); + watchdog::start(); log_info("Hello, World!\n\n"); @@ -43,7 +140,7 @@ auto main() -> int hello.start(); - while (true) { + { auto t = rtos::Kernel::Clock::now() - start; log_info("A message from your board %s --> \"%s\" at %i s\n", MBED_CONF_APP_TARGET_NAME, hello.world, int(t.count() / 1000)); @@ -52,6 +149,10 @@ auto main() -> int runExample(corewifi); rtos::ThisThread::sleep_for(500ms); } + + while (true) { + rtos::ThisThread::sleep_for(10min); + } } void runExample(WiFiInterface &wifi_interface) diff --git a/spikes/mbed_blinky/main.cpp b/spikes/mbed_blinky/main.cpp index d166963b03..ae5c3a1c30 100644 --- a/spikes/mbed_blinky/main.cpp +++ b/spikes/mbed_blinky/main.cpp @@ -5,17 +5,121 @@ #include "drivers/DigitalOut.h" #include "rtos/ThisThread.h" +#include "LogKit.h" + using namespace std::chrono; +#include "mbed_stats.h" + +#include "drivers/Watchdog.h" + +namespace { + +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((stop - start).count()); + + static auto idle_ratio = uint8_t {}; + 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((stop - start).count()); + + mbed_stats_cpu_get(&stats::cpu); + + idle_ratio = static_cast(((stats::cpu.idle_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + sleep_ratio = static_cast(((stats::cpu.sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + deep_sleep_ratio = + static_cast(((stats::cpu.deep_sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + + mbed_stats_stack_get(&stats::stack); + + stack_used_delta = static_cast(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((stack_used_size * 100) / stack_reserved_size); + + mbed_stats_heap_get(&stats::heap); + + heap_used_delta = static_cast(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((heap_used_size * 100) / heap_reserved_size); + + log_info( + "dt: %i, kck: %u, idl: %u%%, slp: %u%%, dsl: %u%%, sur: %u%% (%+i)[%u/" + "%u], hur: %u%% (%+i)[%u/%u]", + delta, kick_count, idle_ratio, 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 + +} // namespace + auto main() -> int { + watchdog::start(); + // Initialise the digital pin LED1 as an output mbed::DigitalOut led(LED1); - while (true) { + { led = !led; rtos::ThisThread::sleep_for(500ms); led = !led; rtos::ThisThread::sleep_for(100ms); } + + while (true) { + rtos::ThisThread::sleep_for(10min); + } } diff --git a/spikes/mbed_watchdog_ticker_vs_thread/main.cpp b/spikes/mbed_watchdog_ticker_vs_thread/main.cpp index 9f3cd75bed..bbcde169e0 100644 --- a/spikes/mbed_watchdog_ticker_vs_thread/main.cpp +++ b/spikes/mbed_watchdog_ticker_vs_thread/main.cpp @@ -16,6 +16,102 @@ using namespace leka; using namespace std::chrono; +#include "mbed_stats.h" + +#include "drivers/Watchdog.h" + +namespace { + +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((stop - start).count()); + + static auto idle_ratio = uint8_t {}; + 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((stop - start).count()); + + mbed_stats_cpu_get(&stats::cpu); + + idle_ratio = static_cast(((stats::cpu.idle_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + sleep_ratio = static_cast(((stats::cpu.sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + deep_sleep_ratio = + static_cast(((stats::cpu.deep_sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + + mbed_stats_stack_get(&stats::stack); + + stack_used_delta = static_cast(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((stack_used_size * 100) / stack_reserved_size); + + mbed_stats_heap_get(&stats::heap); + + heap_used_delta = static_cast(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((heap_used_size * 100) / heap_reserved_size); + + log_info( + "dt: %i, kck: %u, idl: %u%%, slp: %u%%, dsl: %u%%, sur: %u%% (%+i)[%u/" + "%u], hur: %u%% (%+i)[%u/%u]", + delta, kick_count, idle_ratio, 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 + +} // namespace + /// /// @brief Uncomment the line you want to test. `USE_THREAD` will run the example with /// a thread, whereas `USE_TICKER` will run the example with a ticker. @@ -106,6 +202,7 @@ void initWatchdog() auto main() -> int { logger::init(); + watchdog::start(); log_info("Hello, World!\n\n"); @@ -135,7 +232,7 @@ auto main() -> int variables::button.rise(&buttonInterrupt); - while (true) { + { variables::led_main = !variables::led_main; rtos::ThisThread::sleep_for(100ms); @@ -154,4 +251,8 @@ auto main() -> int } } } + + while (true) { + rtos::ThisThread::sleep_for(10min); + } } diff --git a/spikes/stl_cxxsupport/main.cpp b/spikes/stl_cxxsupport/main.cpp index eff0ebd85a..4b757a4e69 100644 --- a/spikes/stl_cxxsupport/main.cpp +++ b/spikes/stl_cxxsupport/main.cpp @@ -14,6 +14,102 @@ using namespace leka; using namespace std::chrono; +#include "mbed_stats.h" + +#include "drivers/Watchdog.h" + +namespace { + +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((stop - start).count()); + + static auto idle_ratio = uint8_t {}; + 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((stop - start).count()); + + mbed_stats_cpu_get(&stats::cpu); + + idle_ratio = static_cast(((stats::cpu.idle_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + sleep_ratio = static_cast(((stats::cpu.sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + deep_sleep_ratio = + static_cast(((stats::cpu.deep_sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000))); + + mbed_stats_stack_get(&stats::stack); + + stack_used_delta = static_cast(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((stack_used_size * 100) / stack_reserved_size); + + mbed_stats_heap_get(&stats::heap); + + heap_used_delta = static_cast(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((heap_used_size * 100) / heap_reserved_size); + + log_info( + "dt: %i, kck: %u, idl: %u%%, slp: %u%%, dsl: %u%%, sur: %u%% (%+i)[%u/" + "%u], hur: %u%% (%+i)[%u/%u]", + delta, kick_count, idle_ratio, 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 + +} // namespace + void foo(std::span span) { log_info("from foo span size: %i", span.size()); @@ -25,6 +121,7 @@ void foo(std::span span) auto main() -> int { logger::init(); + watchdog::start(); log_info("Hello, World!\n"); @@ -94,5 +191,9 @@ auto main() -> int auto back = span0.back(); log_info("span0.back() = %i", back); + while (true) { + rtos::ThisThread::sleep_for(10min); + } + return 1; }