#include #include "wm_hal.h" #define X_MAX 79 #define Y_MAX 25 uint8_t result[Y_MAX][X_MAX]; TIM_HandleTypeDef TimerDef; void display_result(void) { int x, y; const char c[] = "0123456789ABCDEF "; for (y = 0; y < Y_MAX; y++) { for (x = 0; x < X_MAX; x++) { putchar(c[result[y][x]]); } putchar('\n'); } } void benchmark(void) { int x, y, i; float a, b, ca, cb, t; for (y = -12; y <= 12; y++) { for (x = -39; x <= 39; x++) { a = ca = x * 0.0458; b = cb = y * 0.08333; for (i = 0; i <= 15; i++) { t = (a * a) - (b * b) + ca; b = (2 * a * b) + cb; a = t; if ((a * a + b * b) > 4) break; } result[y + 12][x + 39] = i; } } return; } void benchmark_main(uint32_t clk) { uint32_t t, before, after; SystemClock_Config(clk); /* SystemClock_Config() enables SysTick interrupt */ HAL_NVIC_DisableIRQ(SYS_TICK_IRQn); /* HAL_TIM_Base_Init() should be called after SystemClock_Config() */ TimerDef.Instance = TIM0; TimerDef.Init.Unit = TIM_UNIT_US; TimerDef.Init.Period = ~0; // 0 <= TIM0_CNT <= TimerDef.Init.Period TimerDef.Init.AutoReload = TIM_AUTORELOAD_PRELOAD_ENABLE; if (HAL_TIM_Base_Init(&TimerDef) == HAL_OK) HAL_TIM_Base_Start(&TimerDef); t = before = TIM->TIM0_CNT; while ((before = TIM->TIM0_CNT) == t); benchmark(); after = TIM->TIM0_CNT; /* HAL_TIM_Base_Stop/DeInit is required to restart timer next time */ HAL_TIM_Base_Stop(&TimerDef); HAL_TIM_Base_DeInit(&TimerDef); printf("%ld: %lu - %lu = %lu\n", clk, after, before, after - before); } int main(void) { printf("enter main\n"); /* PLL is 480MHz, CPU clock should be multiply of APB clock (40MHz) */ benchmark_main(2); // CPU_CLK_240M benchmark_main(3); // CPU_CLK_160M benchmark_main(4); // 120MHz (secret?) benchmark_main(6); // CPU_CLK_80M benchmark_main(12); // CPU_CLK_40M display_result(); printf("exit main\n"); while (1); } /* called from wm_tim.c:HAL_TIM_Base_Init() */ void HAL_TIM_Base_MspInit(TIM_HandleTypeDef *htim_base) { __HAL_RCC_TIM_CLK_ENABLE(); } /* called from wm_tim.c:HAL_TIM_Base_DeInit() */ void HAL_TIM_BASE_MspDeInit(TIM_HandleTypeDef *htim_base) { __HAL_RCC_TIM_CLK_DISABLE(); } /* SystemClock_Config() requires this handler, do not forget -mistack */ __attribute__((isr)) void CORET_IRQHandler(void) { (void)*(volatile unsigned int *)0xe000e010; HAL_IncTick(); }