aboutsummaryrefslogtreecommitdiff
path: root/stm32/src/tot.c
blob: 1fce495c94c6b6958d91aaafb34c82ad0a77fbcf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*!
 * Time-out timer.
 *
 * This is a simple time-out timer for ensuring a maximum transmission
 * time is observed.  The time-out timer is configured with a total time
 * in "ticks", which get counted down in an interrupt.
 *
 * When the "warning" level is reached, a flag is repeatedly set permit
 * triggering of LEDs/sounds to warn the user that time is nearly up.
 *
 * Upon timeout, a separate flag is set to indicate timeout has taken
 * place.
 *
 * Author Stuart Longland <[email protected]>
 * Copyright (C) 2015 FreeDV project.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 2.1,
 * as published by the Free Software Foundation.  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 Lesser General Public
 * License along with this program; if not, see
 * <http://www.gnu.org/licenses/>.
 */

#include "tot.h"

/*!
 * Reset the time-out timer.  This zeroes the counter and event flags.
 */
void tot_reset(struct tot_t * const tot)
{
	tot->event		= 0;
	tot->remaining		= 0;
	tot->warn_remain	= 0;
	tot->ticks		= 0;
}

/*!
 * Start the time-out timer ticking.
 */
void tot_start(struct tot_t * const tot, uint32_t tot_ticks,
		uint16_t warn_ticks)
{
	tot->event		= TOT_EVT_START;
	tot->warn_remain	= tot_ticks - warn_ticks;
	tot->remaining		= tot_ticks;
	tot->ticks		= tot->tick_period;
}

/*!
 * Update the time-out timer state.
 */
void tot_update(struct tot_t * const tot)
{
	if (!tot->event)
		/* We are not active */
		return;

	if (tot->event & TOT_EVT_DONE)
		/* We are done, do not process */
		return;

	if (tot->ticks)
		/* Wait for a tick to pass */
		return;

	/* One "tick" has passed */
	if (!tot->remaining) {
		/* Time-out reached, reset all flags except timeout */
		tot->event	|= TOT_EVT_TIMEOUT | TOT_EVT_DONE;
		return;
	} else {
		tot->remaining--;
	}

	if (!tot->warn_remain) {
		/* Warning period has passed */
		tot->event	|= TOT_EVT_WARN | TOT_EVT_WARN_NEXT;
		tot->warn_remain = tot->remain_warn_ticks;
	} else {
		tot->warn_remain--;
	}

	tot->ticks = tot->tick_period;
}