This commit is contained in:
Edward Emelianov
2026-08-04 19:35:32 +03:00
parent 970d73dc38
commit 40d0605218
6 changed files with 30 additions and 19 deletions

View File

@@ -165,7 +165,7 @@ static errcodes_t cmd_testm(const char*, char *args){
ok = false; ok = false;
} }
if(!ok) return ERR_BADVAL; if(!ok) return ERR_BADVAL;
SEND("TIME="); SEND("TIMEus=");
SEND(u2str(elapsed)); SEND(u2str(elapsed));
SEND("\n"); SEND("\n");
return ERR_AMOUNT; return ERR_AMOUNT;
@@ -193,7 +193,7 @@ static errcodes_t cmd_testc(const char*, char *args){
ok = false; ok = false;
} }
if(!ok) return ERR_BADVAL; if(!ok) return ERR_BADVAL;
SEND("TIME="); SEND("TIMEus=");
SEND(u2str(elapsed)); SEND(u2str(elapsed));
SEND("\n"); SEND("\n");
return ERR_AMOUNT; return ERR_AMOUNT;

Binary file not shown.

View File

@@ -28,24 +28,24 @@ void sys_tick_handler(){
} }
static void timer_setup(){ static void timer_setup(){
RCC->APB1ENR1 |= RCC_APB1ENR1_TIM2EN; RCC->APB1ENR1 |= RCC_APB1ENR1_TIM3EN;
__DSB(); __DSB();
TIM2->PSC = 84; // 85 MHz / 85 = 1 MHz TIM3->CR1 = 0; // disable counter
TIM2->ARR = 0xFFFFFFFF; // 32-bit auto-reload (maximum) TIM3->PSC = 84; // 85 MHz / 85 = 1 MHz
TIM2->CR1 = 0; // disable counter TIM3->ARR = 0xFFFFFFFF; // 32-bit auto-reload (maximum)
} }
void timer_start(){ void timer_start(){
TIM2->CNT = 0; TIM3->CNT = 0;
TIM2->CR1 |= TIM_CR1_CEN; TIM3->CR1 = TIM_CR1_CEN;
} }
void timer_stop(){ void timer_stop(){
TIM2->CR1 = 0; TIM3->CR1 = 0;
} }
uint32_t timer_read(){ uint32_t timer_read(){
return TIM2->CNT; return TIM3->CNT;
} }
void gpio_setup(){ void gpio_setup(){
@@ -62,7 +62,7 @@ void gpio_setup(){
// count milliseconds // count milliseconds
SysTick_Config(SysFreq / 1000); SysTick_Config(SysFreq / 1000);
// Setup TIM2 for microsecond counting // Setup TIM3 for microsecond counting
timer_setup(); timer_setup();
} }

View File

@@ -21,6 +21,7 @@
#include "commproto.h" #include "commproto.h"
#include "hardware.h" #include "hardware.h"
#include "strfunc.h"
#include "usart.h" #include "usart.h"
// 40ms for debouncing // 40ms for debouncing
@@ -33,6 +34,7 @@ int main(void){
usart_setup(115200); usart_setup(115200);
bool pressed = false; bool pressed = false;
uint32_t Tblink = 0, Tkey = 0; // blink and key pressed time uint32_t Tblink = 0, Tkey = 0; // blink and key pressed time
uint32_t Tpressed = 0;
while(1){ while(1){
USART_flags_t f = usart_process(); USART_flags_t f = usart_process();
if(f.rxovrfl) usart_sendstr("Rx buffer overflow!\n"); if(f.rxovrfl) usart_sendstr("Rx buffer overflow!\n");
@@ -50,12 +52,19 @@ int main(void){
if(!pressed){ // new event if(!pressed){ // new event
pressed = true; pressed = true;
usart_sendstr("Key pressed\n"); usart_sendstr("Key pressed\n");
timer_start();
Tpressed = Tms;
} }
Tkey = Tms; Tkey = Tms;
}else{ // key released }else{ // key released
if(pressed && (Tms - Tkey) > TDEBOUNCE){ // wait for debounce if(pressed && (Tms - Tkey) > TDEBOUNCE){ // wait for debounce
timer_stop();
uint32_t micros = timer_read();
uint32_t millis = Tms - Tpressed;
pressed = false; pressed = false;
usart_sendstr("Key released\n"); usart_sendstr("Key released\n");
usart_sendstr("Tms="); usart_sendstr(u2str(millis));
usart_sendstr("\nTus="); usart_sendstr(u2str(micros)); usart_sendstr("\n");
} }
} }
} }

View File

@@ -27,6 +27,9 @@
// amount of iterations over test // amount of iterations over test
#define N_TESTS 1000 #define N_TESTS 1000
static float arr[N_TESTS];
// RNG // RNG
static uint32_t rand_state = 123456789; static uint32_t rand_state = 123456789;
static uint32_t next_rand(void){ static uint32_t next_rand(void){
@@ -35,35 +38,34 @@ static uint32_t next_rand(void){
} }
// fill array with random angles // fill array with random angles
static void fill_random_sin_cos(float *arr){ static void fill_random_sin_cos(){
for(int i = 0; i < N_TESTS; ++i){ for(int i = 0; i < N_TESTS; ++i){
// angle from -pi to +pi // angle from -pi to +pi
arr[i] = (float)(next_rand() % 62831853) / 10000000.0f - 3.14159265f; arr[i] = (float)(next_rand() % 62831853) / 10000000.0f - 3.14159265f;
} }
} }
static void fill_random_atan(float *arr){ static void fill_random_atan(){
for(int i = 0; i < N_TESTS; ++i){ for(int i = 0; i < N_TESTS; ++i){
arr[i] = (float)(next_rand() % 2000000) / 1e6f - 1e6f; // [-1,1] arr[i] = (float)(next_rand() % 2000000) / 1e6f - 1e6f; // [-1,1]
} }
} }
static void fill_random_sqrt(float *arr){ static void fill_random_sqrt(){
for(int i = 0; i < N_TESTS; ++i){ for(int i = 0; i < N_TESTS; ++i){
arr[i] = (float)(next_rand() % 10000) / 100.0f; // [0,100] arr[i] = (float)(next_rand() % 10000) / 100.0f; // [0,100]
} }
} }
static void fill_random_log(float *arr){ static void fill_random_log(){
for(int i = 0; i < N_TESTS; ++i){ for(int i = 0; i < N_TESTS; ++i){
arr[i] = (float)(next_rand() % 10000 + 1) / 100.0f; // [0.01, 100] arr[i] = (float)(next_rand() % 10000 + 1) / 100.0f; // [0.01, 100]
} }
} }
// main test template // main test template
static uint32_t run_test(void (*gen)(float*), float (*func)(float)){ static uint32_t run_test(void (*gen)(), float (*func)(float)){
float arr[N_TESTS]; gen();
gen(arr);
volatile float result = 0.0f; // don't let gcc to optimize this cycle volatile float result = 0.0f; // don't let gcc to optimize this cycle
timer_start(); timer_start();
for(int i = 0; i < N_TESTS; ++i){ for(int i = 0; i < N_TESTS; ++i){

View File

@@ -1,2 +1,2 @@
#define BUILD_NUMBER "6" #define BUILD_NUMBER "19"
#define BUILD_DATE "2026-08-04" #define BUILD_DATE "2026-08-04"