-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
76 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
text data bss dec hex filename | ||
181721 51664 371173 604558 9398e build/template.elf | ||
182109 51744 371125 604978 93b32 build/template.elf |
Submodule libmcu
updated
4 files
+2 −1 | modules/common/include/libmcu/board.h | |
+2 −1 | modules/common/src/board.c | |
+145 −0 | ports/stm32/flash_g4.c | |
+0 −0 | ports/stm32/flash_h7.c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2024 Kyunghwan Kwon <k@libmcu.org> | ||
* | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
|
||
#include "libmcu/board.h" | ||
|
||
#include "freertos/FreeRTOS.h" | ||
#include "freertos/task.h" | ||
|
||
#define CPULOAD_CALC_INTERVAL_MS 1000 | ||
|
||
static struct cpuload { | ||
uint32_t idle_time_elapsed; | ||
uint32_t running_time_elapsed; | ||
TaskHandle_t prev_task; | ||
uint8_t cpuload; | ||
} cores[SOC_CPU_CORES_NUM]; | ||
|
||
void on_task_switch_in(void) | ||
{ | ||
static uint32_t t0; | ||
static uint32_t sum_elapsed; | ||
|
||
uint32_t t1 = board_get_time_since_boot_ms(); | ||
uint32_t elapsed = t1 - t0; | ||
|
||
/* NOTE: count at least 1 even if the task has run for much shorter time | ||
* as millisecond unit timer used here. For fine granularity, introduce | ||
* high-resolution timer. */ | ||
if (elapsed == 0) { | ||
elapsed = 1; | ||
} | ||
|
||
TaskHandle_t current = xTaskGetCurrentTaskHandle(); | ||
struct cpuload *core = &cores[xPortGetCoreID()]; | ||
|
||
if (current == xTaskGetIdleTaskHandle()) { | ||
if (current == core->prev_task) { /* idle to idle */ | ||
core->idle_time_elapsed += elapsed; | ||
} else { /* active to idle */ | ||
core->running_time_elapsed += elapsed; | ||
} | ||
} else { | ||
if (current == core->prev_task) { /* active to active */ | ||
core->running_time_elapsed += elapsed; | ||
} else { /* idle to active */ | ||
core->idle_time_elapsed += elapsed; | ||
} | ||
} | ||
|
||
sum_elapsed += elapsed; | ||
core->cpuload = (uint8_t)(core->running_time_elapsed * 100 / | ||
(core->running_time_elapsed + core->idle_time_elapsed)); | ||
|
||
if (sum_elapsed >= CPULOAD_CALC_INTERVAL_MS) { | ||
core->running_time_elapsed = core->idle_time_elapsed = 0; | ||
sum_elapsed = 0; | ||
} | ||
|
||
t0 = t1; | ||
core->prev_task = current; | ||
} | ||
|
||
uint8_t board_cpuload(int core_id) | ||
{ | ||
return cores[core_id].cpuload; | ||
} |