forked from atmelcorp/atmel-software-package
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer.h
130 lines (112 loc) · 4.25 KB
/
timer.h
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/* ----------------------------------------------------------------------------
* SAM Software Package License
* ----------------------------------------------------------------------------
* Copyright (c) 2015, Atmel Corporation
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the disclaimer below.
*
* Atmel's name may not be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* ----------------------------------------------------------------------------
*/
#ifndef TIMER_H_
#define TIMER_H_
/*----------------------------------------------------------------------------
* Headers
*----------------------------------------------------------------------------*/
#include <stdint.h>
#include "board.h"
/*----------------------------------------------------------------------------
* Type definitions
*----------------------------------------------------------------------------*/
struct _timeout
{
uint64_t start;
uint64_t count;
};
/*----------------------------------------------------------------------------
* Global functions
*----------------------------------------------------------------------------*/
/**
* \brief Configures the timer and reset tick counter.
*
* If CONFIG_TIMER_POLLING is not defined, the TC interrupt will be used to
* increment the tick counter. If CONFIG_TIMER_POLLING is defined, the tick
* counter will be updated from the TC when requested.
*
* \note TC is enabled automatically in this function.
* \warning If interrupts are used, this function also reconfigures the TC
* handler in AIC.
*
* \param timer Pointer to a struct _timer instance
*/
extern void timer_configure(Tc* tc, uint8_t channel, uint32_t clock_source);
/**
* \brief Wait for count times the timer resolution
*
* If interrupts are enabled, the waiting loop will use WFI instructions to put
* the core to sleep between timer ticks. Otherwise, if polling is enabled, a
* busy-loop is used to poll the TC counter value.
*/
extern void timer_sleep(uint64_t count);
/**
* \brief Initialize a timeout
*/
extern void timer_start_timeout(struct _timeout* timeout, uint64_t count);
/**
* \brief Reset the starting time of a timeout structure
*/
extern void timer_reset_timeout(struct _timeout* timeout);
/**
* \brief Tells if the timeout as been reached
*/
extern uint8_t timer_timeout_reached(struct _timeout* timeout);
/**
* \brief Compute elapsed number of ticks between start and end with
* taking overlaps into account
*
* \param start Start tick point.
* \param end End tick point.
*/
extern uint64_t timer_get_interval(uint64_t start, uint64_t end);
/**
* \brief Returns the current number of ticks
*/
extern uint64_t timer_get_tick(void);
/**
* \brief Wait for at least count seconds.
*/
extern void sleep(uint32_t count);
/**
* \brief Wait for at least count milliseconds
*/
extern void msleep(uint32_t count);
/**
* \brief Wait for at least count microseconds
*
* Notes:
* - interrupts are disabled during the wait
* - if count is <2, it is assumed to be 2
* - maximum wait interval depends on the effective TC frequency and the TC
* channel resolution (it is expected that this function is called with
* count<1000)
*/
extern void usleep(uint32_t count);
#endif /* TIMER_H_ */