<commit_before>#include "common.h"

#include <avr/io.h>
#include <avr/interrupt.h>

#include "timer.h"

/* useconds per clock tick */
#define US_PER_TICK 128
/* max useconds with max OCRA1 */
#define MAX_US ((uint32_t) 8388480)

/* max/current timer hits */
static volatile uint8_t maxhits, hits;
/* compare value for n-1, last hits */
static uint16_t count, lastcount;
/* accumulated time since last timeout/call to timerHit (us) */
static uint32_t time;
static bool oneshot = false;

ISR(TIMER1_COMPA_vect) {
	++hits;
	time += (uint32_t) OCR1A * (uint32_t) US_PER_TICK;
	if (hits == maxhits-1) {
		OCR1A = lastcount;
	} else if (hits >= maxhits) {
		enableWakeup (WAKE_TIMER);
	}
}

/*	Check if timer was hit, return time since last restart or 0 if not hit yet
 */
uint32_t timerHit () {
	uint32_t ret = 0;
	ATOMIC_BLOCK (ATOMIC_FORCEON) {
		if (shouldWakeup (WAKE_TIMER)) {
			disableWakeup (WAKE_TIMER);

			ret = time;
			if (oneshot) {
				timerStop ();
			} else {
				/* reset timer, start again */
				hits = 0;
				time = 0;
				OCR1A = count;
				TCNT1 = 0;
			}
		}
	}
	return ret;<commit_msg>timer: Fix one-shot mode<commit_after>#include "common.h"

#include <avr/io.h>
#include <avr/interrupt.h>

#include "timer.h"

/* useconds per clock tick */
#define US_PER_TICK 128
/* max useconds with max OCRA1 */
#define MAX_US ((uint32_t) 8388480)

/* max/current timer hits */
static volatile uint8_t maxhits, hits;
/* compare value for n-1, last hits */
static uint16_t count, lastcount;
/* accumulated time since last timeout/call to timerHit (us) */
static uint32_t time;
static bool oneshot = false;

ISR(TIMER1_COMPA_vect) {
	++hits;
	time += (uint32_t) OCR1A * (uint32_t) US_PER_TICK;
	if (hits == maxhits-1) {
		OCR1A = lastcount;
	} else if (hits >= maxhits) {
		enableWakeup (WAKE_TIMER);
		if (oneshot) {
			timerStop ();
		}
	}
}

/*	Check if timer was hit, return time since last restart or 0 if not hit yet
 */
uint32_t timerHit () {
	uint32_t ret = 0;
	ATOMIC_BLOCK (ATOMIC_FORCEON) {
		if (shouldWakeup (WAKE_TIMER)) {
			disableWakeup (WAKE_TIMER);

			ret = time;
			if (!oneshot) {
				/* reset timer, start again */
				hits = 0;
				time = 0;
				OCR1A = count;
				TCNT1 = 0;
			}
		}
	}
	return ret;
{'fraction_non_alphanumeric': 0.12869955156950671, 'fraction_numerical': 0.034080717488789235, 'mean_word_length': 3.357421875, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3117832', 'n_tokens_mistral': 1008, 'n_tokens_neox': 899, 'n_words': 249}