mirror of
https://github.com/eddyem/stm32samples.git
synced 2026-08-12 09:39:25 +03:00
.
This commit is contained in:
@@ -165,7 +165,7 @@ static errcodes_t cmd_testm(const char*, char *args){
|
||||
ok = false;
|
||||
}
|
||||
if(!ok) return ERR_BADVAL;
|
||||
SEND("TIME=");
|
||||
SEND("TIMEus=");
|
||||
SEND(u2str(elapsed));
|
||||
SEND("\n");
|
||||
return ERR_AMOUNT;
|
||||
@@ -193,7 +193,7 @@ static errcodes_t cmd_testc(const char*, char *args){
|
||||
ok = false;
|
||||
}
|
||||
if(!ok) return ERR_BADVAL;
|
||||
SEND("TIME=");
|
||||
SEND("TIMEus=");
|
||||
SEND(u2str(elapsed));
|
||||
SEND("\n");
|
||||
return ERR_AMOUNT;
|
||||
|
||||
Binary file not shown.
@@ -28,24 +28,24 @@ void sys_tick_handler(){
|
||||
}
|
||||
|
||||
static void timer_setup(){
|
||||
RCC->APB1ENR1 |= RCC_APB1ENR1_TIM2EN;
|
||||
RCC->APB1ENR1 |= RCC_APB1ENR1_TIM3EN;
|
||||
__DSB();
|
||||
TIM2->PSC = 84; // 85 MHz / 85 = 1 MHz
|
||||
TIM2->ARR = 0xFFFFFFFF; // 32-bit auto-reload (maximum)
|
||||
TIM2->CR1 = 0; // disable counter
|
||||
TIM3->CR1 = 0; // disable counter
|
||||
TIM3->PSC = 84; // 85 MHz / 85 = 1 MHz
|
||||
TIM3->ARR = 0xFFFFFFFF; // 32-bit auto-reload (maximum)
|
||||
}
|
||||
|
||||
void timer_start(){
|
||||
TIM2->CNT = 0;
|
||||
TIM2->CR1 |= TIM_CR1_CEN;
|
||||
TIM3->CNT = 0;
|
||||
TIM3->CR1 = TIM_CR1_CEN;
|
||||
}
|
||||
|
||||
void timer_stop(){
|
||||
TIM2->CR1 = 0;
|
||||
TIM3->CR1 = 0;
|
||||
}
|
||||
|
||||
uint32_t timer_read(){
|
||||
return TIM2->CNT;
|
||||
return TIM3->CNT;
|
||||
}
|
||||
|
||||
void gpio_setup(){
|
||||
@@ -62,7 +62,7 @@ void gpio_setup(){
|
||||
// count milliseconds
|
||||
SysTick_Config(SysFreq / 1000);
|
||||
|
||||
// Setup TIM2 for microsecond counting
|
||||
// Setup TIM3 for microsecond counting
|
||||
timer_setup();
|
||||
}
|
||||
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
|
||||
#include "commproto.h"
|
||||
#include "hardware.h"
|
||||
#include "strfunc.h"
|
||||
#include "usart.h"
|
||||
|
||||
// 40ms for debouncing
|
||||
@@ -33,6 +34,7 @@ int main(void){
|
||||
usart_setup(115200);
|
||||
bool pressed = false;
|
||||
uint32_t Tblink = 0, Tkey = 0; // blink and key pressed time
|
||||
uint32_t Tpressed = 0;
|
||||
while(1){
|
||||
USART_flags_t f = usart_process();
|
||||
if(f.rxovrfl) usart_sendstr("Rx buffer overflow!\n");
|
||||
@@ -50,12 +52,19 @@ int main(void){
|
||||
if(!pressed){ // new event
|
||||
pressed = true;
|
||||
usart_sendstr("Key pressed\n");
|
||||
timer_start();
|
||||
Tpressed = Tms;
|
||||
}
|
||||
Tkey = Tms;
|
||||
}else{ // key released
|
||||
if(pressed && (Tms - Tkey) > TDEBOUNCE){ // wait for debounce
|
||||
timer_stop();
|
||||
uint32_t micros = timer_read();
|
||||
uint32_t millis = Tms - Tpressed;
|
||||
pressed = false;
|
||||
usart_sendstr("Key released\n");
|
||||
usart_sendstr("Tms="); usart_sendstr(u2str(millis));
|
||||
usart_sendstr("\nTus="); usart_sendstr(u2str(micros)); usart_sendstr("\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,6 +27,9 @@
|
||||
// amount of iterations over test
|
||||
#define N_TESTS 1000
|
||||
|
||||
static float arr[N_TESTS];
|
||||
|
||||
|
||||
// RNG
|
||||
static uint32_t rand_state = 123456789;
|
||||
static uint32_t next_rand(void){
|
||||
@@ -35,35 +38,34 @@ static uint32_t next_rand(void){
|
||||
}
|
||||
|
||||
// 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){
|
||||
// angle from -pi to +pi
|
||||
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){
|
||||
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){
|
||||
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){
|
||||
arr[i] = (float)(next_rand() % 10000 + 1) / 100.0f; // [0.01, 100]
|
||||
}
|
||||
}
|
||||
|
||||
// main test template
|
||||
static uint32_t run_test(void (*gen)(float*), float (*func)(float)){
|
||||
float arr[N_TESTS];
|
||||
gen(arr);
|
||||
static uint32_t run_test(void (*gen)(), float (*func)(float)){
|
||||
gen();
|
||||
volatile float result = 0.0f; // don't let gcc to optimize this cycle
|
||||
timer_start();
|
||||
for(int i = 0; i < N_TESTS; ++i){
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
#define BUILD_NUMBER "6"
|
||||
#define BUILD_NUMBER "19"
|
||||
#define BUILD_DATE "2026-08-04"
|
||||
|
||||
Reference in New Issue
Block a user