avr-crypto-lib/test_src/performance_test.c

96 lines
2.1 KiB
C
Raw Normal View History

2008-05-26 19:13:21 +00:00
/* performance_test.c */
/*
2009-01-30 23:00:04 +00:00
This file is part of the AVR-Crypto-Lib.
2015-02-06 02:43:31 +00:00
Copyright (C) 2006-2015 Daniel Otte (bg@nerilex.org)
2008-05-26 19:13:21 +00:00
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
2008-04-11 17:54:24 +00:00
* author: Daniel Otte
2015-02-06 02:43:31 +00:00
* email: bg@nerilex.org
2008-04-11 17:54:24 +00:00
* license: GPLv3
2009-12-10 17:33:12 +00:00
*
*
2008-04-11 17:54:24 +00:00
**/
2009-12-10 17:33:12 +00:00
2008-04-11 17:54:24 +00:00
#include "config.h"
2008-04-20 02:36:13 +00:00
#include <string.h>
#include <stdint.h>
#include <stdlib.h>
#include <avr/io.h>
#include <avr/interrupt.h>
2008-04-20 02:36:13 +00:00
#include <avr/pgmspace.h>
#include <stdio.h>
#include "performance_test.h"
2008-04-11 17:54:24 +00:00
#ifdef ATMEGA644
#define TIMSK TIMSK1
#endif
2009-12-10 17:33:12 +00:00
static volatile uint32_t ovfcounter;
static uint16_t const_overhead = 0;
static uint16_t int_overhead = 0;
ISR(TIMER1_OVF_vect){
++ovfcounter;
}
void calibrateTimer(void){
startTimer(1);
stopTimer();
const_overhead = TCNT1;
startTimer(1);
TCNT1 = 0xFFFE;
asm("nop");
stopTimer();
int_overhead = TCNT1;
}
void startTimer(uint8_t granularity){
TCCR1B = 0; /* stop timer */
TCNT1 = 0;
ovfcounter = 0;
TCCR1A = 0x00;
TIMSK = _BV(TOIE1); /* enable TOIE1 */
TCCR1B = granularity & 0x7; /* start timer */
}
uint64_t stopTimer(void){
TCCR1B = 0; /* stop timer */
uint64_t ret;
2009-12-10 17:33:12 +00:00
ret = (((uint64_t)ovfcounter)<<16) | TCNT1;
ret -= const_overhead;
ret -= ovfcounter * int_overhead;
return ret;
}
void getOverhead(uint16_t *constoh, uint16_t *intoh){
*constoh = const_overhead;
2009-12-10 17:33:12 +00:00
*intoh = int_overhead;
}
void print_time_P(PGM_P s, uint32_t t){
printf_P(PSTR("%S%11"PRIu32), t);
2008-04-20 02:36:13 +00:00
}
2008-04-20 02:36:13 +00:00
void print_overhead(void){
printf_P(PSTR("\n=== benchmark ===\n\tconst overhead: %7"PRIu16"\n\tinterrupt overhead: %7"PRIu16"\n"), const_overhead, int_overhead);
2008-04-20 02:36:13 +00:00
}