calculate cpu usage with a hardware timer

had to bump the display stack size to keep snprintf from crashing
This commit is contained in:
Andy Killorin 2025-04-12 18:40:26 -04:00
parent e8946f2a03
commit 281c0f68d5
2 changed files with 33 additions and 4 deletions

33
main.c
View file

@ -36,6 +36,8 @@ uint32_t gSystemClock = 120000000; // [Hz] system clock frequency
tContext sContext; tContext sContext;
uint32_t cputime_unloaded;
#define LOCAL_BUF_LEN 128 #define LOCAL_BUF_LEN 128
uint16_t adc_buffer_sample[LOCAL_BUF_LEN]; // copy of g adc buffer uint16_t adc_buffer_sample[LOCAL_BUF_LEN]; // copy of g adc buffer
uint8_t adc_buffer_processed[LOCAL_BUF_LEN]; // copy of g adc buffer uint8_t adc_buffer_processed[LOCAL_BUF_LEN]; // copy of g adc buffer
@ -100,6 +102,26 @@ void start_signal() {
PWMGenEnable(PWM0_BASE, PWM_GEN_1); PWMGenEnable(PWM0_BASE, PWM_GEN_1);
} }
uint32_t cpu_load_count(void)
{
uint32_t i = 0;
TimerIntClear(TIMER1_BASE, TIMER_TIMA_TIMEOUT);
TimerEnable(TIMER1_BASE, TIMER_A); // start one-shot timer
while (!(TimerIntStatus(TIMER1_BASE, false) & TIMER_TIMA_TIMEOUT))
i++;
return i;
}
void start_cputimer() {
// initialize timer 1 in one-shot mode for polled timing
SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER1);
TimerDisable(TIMER1_BASE, TIMER_BOTH);
TimerConfigure(TIMER1_BASE, TIMER_CFG_ONE_SHOT);
TimerLoadSet(TIMER1_BASE, TIMER_A, gSystemClock/100); // 10ms interval
// baseline load
cputime_unloaded = cpu_load_count();
}
/* /*
* ======== main ======== * ======== main ========
*/ */
@ -111,6 +133,7 @@ int main(void)
start_signal(); start_signal();
start_sampler(); start_sampler();
ButtonInit(); ButtonInit();
start_cputimer();
Crystalfontz128x128_Init(); // Initialize the LCD display driver Crystalfontz128x128_Init(); // Initialize the LCD display driver
Crystalfontz128x128_SetOrientation(LCD_ORIENTATION_UP); // set screen orientation Crystalfontz128x128_SetOrientation(LCD_ORIENTATION_UP); // set screen orientation
@ -215,6 +238,12 @@ void display_waveform(UArg arg1, UArg arg2)
while(1) { while(1) {
Semaphore_pend(display_sem, BIOS_WAIT_FOREVER); Semaphore_pend(display_sem, BIOS_WAIT_FOREVER);
// calculate cpu usage
uint32_t cputime_loaded;
cputime_loaded = cpu_load_count();
float usage_percent;
usage_percent = (1.0 - (float) cputime_loaded / (float) cputime_unloaded) * (float) 100.;
GrContextForegroundSet(&sContext, ClrBlack); GrContextForegroundSet(&sContext, ClrBlack);
GrRectFill(&sContext, &rectFullScreen); // fill screen with black GrRectFill(&sContext, &rectFullScreen); // fill screen with black
@ -235,8 +264,8 @@ void display_waveform(UArg arg1, UArg arg2)
GrContextForegroundSet(&sContext, ClrWheat); GrContextForegroundSet(&sContext, ClrWheat);
GrStringDraw(&sContext, gVoltageScaleStr[voltage_scale], /*length*/ -1, /*x*/ 0, /*y*/ 0, /*opaque*/ false); GrStringDraw(&sContext, gVoltageScaleStr[voltage_scale], /*length*/ -1, /*x*/ 0, /*y*/ 0, /*opaque*/ false);
GrStringDraw(&sContext, gTimeScaleStr[time_scale], /*length*/ -1, /*x*/ 60, /*y*/ 0, /*opaque*/ false); GrStringDraw(&sContext, gTimeScaleStr[time_scale], /*length*/ -1, /*x*/ 60, /*y*/ 0, /*opaque*/ false);
//snprintf(str, sizeof(str), "CPU Load %.1f%%", usage_percent); snprintf(str, sizeof(str), "CPU Load %.1f%%", usage_percent);
//GrStringDraw(&sContext, str, /*length*/ -1, /*x*/ 0, /*y*/ HEIGHT - 10, /*opaque*/ false); GrStringDraw(&sContext, str, /*length*/ -1, /*x*/ 0, /*y*/ HEIGHT - 10, /*opaque*/ false);
switch (trigger_mode) { switch (trigger_mode) {
case 1: case 1:

View file

@ -567,7 +567,7 @@ Program.global.processing = Task.create("&process_waveform", task2Params);
var task3Params = new Task.Params(); var task3Params = new Task.Params();
task3Params.instance.name = "display"; task3Params.instance.name = "display";
task3Params.priority = 5; task3Params.priority = 5;
task3Params.stackSize = 1024; task3Params.stackSize = 2048;
Program.global.display = Task.create("&display_waveform", task3Params); Program.global.display = Task.create("&display_waveform", task3Params);
var semaphore2Params0 = new Semaphore.Params(); var semaphore2Params0 = new Semaphore.Params();
semaphore2Params0.instance.name = "display_sem"; semaphore2Params0.instance.name = "display_sem";
@ -591,5 +591,5 @@ mailbox0Params.instance.name = "button_mailbox";
Program.global.button_mailbox = Mailbox.create(1, 9, mailbox0Params); Program.global.button_mailbox = Mailbox.create(1, 9, mailbox0Params);
var task5Params = new Task.Params(); var task5Params = new Task.Params();
task5Params.instance.name = "user_input"; task5Params.instance.name = "user_input";
task5Params.priority = 4; task5Params.priority = 6;
Program.global.user_input = Task.create("&handle_user_input", task5Params); Program.global.user_input = Task.create("&handle_user_input", task5Params);