Skip to content

Commit

Permalink
🔖 (v1.0.1): Merge branch 'release/v1.0.1'
Browse files Browse the repository at this point in the history
* release/v1.0.1:
  ✨ (bootloader): Add log_stats() function
  🔥 (bootloader): Remove watchdog from bootloader
  • Loading branch information
ladislas committed Oct 2, 2022
2 parents bb108a4 + a29fdb3 commit ed7772a
Showing 1 changed file with 56 additions and 71 deletions.
127 changes: 56 additions & 71 deletions app/bootloader/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,97 +310,82 @@ namespace motors {

} // namespace motors

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

} // namespace stats
namespace leka::logger {

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

static auto start = rtos::Kernel::Clock::now();
static auto stop = rtos::Kernel::Clock::now();
static auto delta = static_cast<int>((stop - start).count());
auto cpu = mbed_stats_cpu_t {};
auto stack = mbed_stats_stack_t {};
auto heap = mbed_stats_heap_t {};

static auto ble_connected = uint8_t {};
static auto battery_level = uint8_t {};
static auto charging_status = uint8_t {};
auto kick_count = uint32_t {0};

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

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 {};
auto ble_connected = uint8_t {};
auto battery_level = uint8_t {};
auto charging_status = 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 {};
auto sleep_ratio = uint8_t {};
auto deep_sleep_ratio = uint8_t {};

while (true) {
internal::instance.kick();
++kick_count;
auto stack_used_delta = int32_t {};
auto stack_used_size = uint32_t {};
auto stack_reserved_size = uint32_t {};
auto stack_used_ratio = uint8_t {};

stop = rtos::Kernel::Clock::now();
delta = static_cast<int>((stop - start).count());
auto heap_used_delta = int32_t {};
auto heap_used_size = uint32_t {};
auto heap_reserved_size = uint32_t {};
auto heap_used_ratio = uint8_t {};

mbed_stats_cpu_get(&stats::cpu);
} // namespace stats

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

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

stack_used_delta = static_cast<int32_t>(stats::stack.max_size - stack_used_size);
stack_used_size = stats::stack.max_size;
stack_reserved_size = stats::stack.reserved_size;
stack_used_ratio = static_cast<uint8_t>((stack_used_size * 100) / stack_reserved_size);
stats::battery_level = battery::cells.level();
stats::charging_status = battery::cells.isCharging() ? 1 : 0;

mbed_stats_heap_get(&stats::heap);
mbed_stats_cpu_get(&stats::cpu);

heap_used_delta = static_cast<int32_t>(stats::heap.current_size - heap_used_size);
heap_used_size = stats::heap.current_size;
heap_reserved_size = stats::heap.reserved_size;
heap_used_ratio = static_cast<uint8_t>((heap_used_size * 100) / heap_reserved_size);
stats::sleep_ratio = static_cast<uint8_t>(((stats::cpu.sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000)));
stats::deep_sleep_ratio =
static_cast<uint8_t>(((stats::cpu.deep_sleep_time / 1000 * 100) / (stats::cpu.uptime / 1000)));

log_info(
"dt: %i, kck: %u, ble: %u, lvl: %u%%, chr: %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);
mbed_stats_stack_get(&stats::stack);

start = rtos::Kernel::Clock::now();
rtos::ThisThread::sleep_for(5s);
}
}
stats::stack_used_delta = static_cast<int32_t>(stats::stack.max_size - stats::stack_used_size);
stats::stack_used_size = stats::stack.max_size;
stats::stack_reserved_size = stats::stack.reserved_size;
stats::stack_used_ratio = static_cast<uint8_t>((stats::stack_used_size * 100) / stats::stack_reserved_size);

} // namespace internal
mbed_stats_heap_get(&stats::heap);

void start()
{
internal::instance.start(internal::timeout.count());
internal::thread.start(watchdog::internal::watchdog_kick);
}
stats::heap_used_delta = static_cast<int32_t>(stats::heap.current_size - stats::heap_used_size);
stats::heap_used_size = stats::heap.current_size;
stats::heap_reserved_size = stats::heap.reserved_size;
stats::heap_used_ratio = static_cast<uint8_t>((stats::heap_used_size * 100) / stats::heap_reserved_size);

} // namespace watchdog
log_info(
"dt: %i, kck: %u, ble: %u, lvl: %u%%, chr: %u, slp: %u%%, dsl: %u%%, sur: %u%% (%+i)[%u/"
"%u], hur: %u%% (%+i)[%u/%u]",
stats::delta, stats::kick_count, stats::ble_connected, stats::battery_level, stats::charging_status,
stats::sleep_ratio, stats::deep_sleep_ratio, stats::stack_used_ratio, stats::stack_used_delta,
stats::stack_used_size, stats::stack_reserved_size, stats::heap_used_ratio, stats::heap_used_delta,
stats::heap_used_size, stats::heap_reserved_size);
}

} // namespace
} // namespace leka::logger

//
// MARK: - main()
Expand All @@ -411,7 +396,6 @@ auto main() -> int
leds::turnOff();
motors::turnOff();

watchdog::start();
logger::init();

rtos::ThisThread::sleep_for(50ms);
Expand All @@ -435,6 +419,7 @@ auto main() -> int
leds::blink::highEnergy();
}

logger::log_stats();
rtos::ThisThread::sleep_for(4s);
}

Expand Down

0 comments on commit ed7772a

Please sign in to comment.