1 /* ----------------------------------------------------------------------------
\r
2 * SAM Software Package License
\r
3 * ----------------------------------------------------------------------------
\r
4 * Copyright (c) 2015, Atmel Corporation
\r
6 * All rights reserved.
\r
8 * Redistribution and use in source and binary forms, with or without
\r
9 * modification, are permitted provided that the following conditions are met:
\r
11 * - Redistributions of source code must retain the above copyright notice,
\r
12 * this list of conditions and the disclaimer below.
\r
14 * Atmel's name may not be used to endorse or promote products derived from
\r
15 * this software without specific prior written permission.
\r
17 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
\r
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
\r
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
\r
20 * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
\r
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
\r
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
\r
23 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
\r
24 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
\r
25 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
\r
26 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\r
27 * ----------------------------------------------------------------------------
\r
32 * Implement simple PIT usage as system tick.
\r
35 /*----------------------------------------------------------------------------
\r
37 *----------------------------------------------------------------------------*/
\r
41 #include "peripherals/tc.h"
\r
42 #include "peripherals/pit.h"
\r
43 #include "peripherals/aic.h"
\r
44 #include "peripherals/pmc.h"
\r
46 /*----------------------------------------------------------------------------
\r
48 *----------------------------------------------------------------------------*/
\r
51 static volatile uint32_t _timer = 0;
\r
52 static uint32_t _resolution = 0;
\r
54 /*----------------------------------------------------------------------------
\r
55 * Exported Functions
\r
56 *----------------------------------------------------------------------------*/
\r
59 * \brief Handler for Sytem Tick interrupt.
\r
61 static void timer_increment(void)
\r
65 /* Read the PIT status register */
\r
66 status = pit_get_status() & PIT_SR_PITS;
\r
69 /* 1 = The Periodic Interval timer has reached PIV
\r
70 * since the last read of PIT_PIVR. Read the PIVR to
\r
71 * acknowledge interrupt and get number of ticks
\r
72 * Returns the number of occurrences of periodic
\r
73 * intervals since the last read of PIT_PIVR. */
\r
74 _timer += (pit_get_pivr() >> 20);
\r
78 uint32_t timer_configure(uint32_t resolution)
\r
82 resolution = BOARD_TIMER_RESOLUTION;
\r
84 pmc_enable_peripheral(ID_PIT);
\r
85 pit_init(resolution);
\r
86 aic_set_source_vector(ID_PIT, timer_increment);
\r
90 _resolution = resolution;
\r
94 uint32_t timer_get_resolution(void)
\r
99 uint32_t timer_get_interval(uint32_t start, uint32_t end)
\r
102 return (end - start);
\r
103 return (end + (0xFFFFFFFF - start) + 1);
\r
106 void timer_start_timeout(struct _timeout* timeout, uint32_t count)
\r
108 timeout->start = _timer;
\r
109 timeout->count = count;
\r
112 uint8_t timer_timeout_reached(struct _timeout* timeout)
\r
114 return timer_get_interval(timeout->start, _timer) >= timeout->count;
\r
117 void timer_wait(uint32_t count)
\r
119 uint32_t start, current;
\r
123 } while (timer_get_interval(start, current) < count);
\r
126 void timer_sleep(uint32_t count)
\r
128 uint32_t start, current;
\r
135 } while (timer_get_interval(start, current) < count);
\r
138 uint32_t timer_get_tick(void)
\r