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
30 /** \addtogroup pit_module Working with PIT
\r
32 * The PIT driver provides the Interface for configuration the Periodic
\r
33 * Interval Timer (PIT) peripheral.
\r
37 * <li> Initialize the PIT with the desired period using pit_init().
\r
38 * Alternatively, the Periodic Interval Value (PIV) can be configured
\r
39 * manually using pit_set_piv(). </li>
\r
40 * <li> Start the PIT counting using pit_enable().
\r
41 * <li> Enable & disable the PIT interrupt using pit_enable_it() and
\r
42 * pit_disable_it(). </li>
\r
43 * <li> Retrieve the current status of the PIT using pit_get_status(). </li>
\r
44 * <li> To get the current value of the internal counter and the number of ticks
\r
45 * that have occurred, use either pit_get_pivr() or pit_get_piir() depending
\r
46 * on whether you want the values to be cleared or not. </li>
\r
49 * For more accurate information, please look at the PIT section of the
\r
62 * Implementation of PIT (Periodic Interval Timer) controller.
\r
65 /*------------------------------------------------------------------------------
\r
67 *------------------------------------------------------------------------------*/
\r
70 #include "peripherals/pit.h"
\r
71 #include "peripherals/pmc.h"
\r
73 /*------------------------------------------------------------------------------
\r
74 * Exported functions
\r
75 *------------------------------------------------------------------------------*/
\r
77 void pit_init(uint32_t period)
\r
79 uint32_t pit_frequency = pmc_get_peripheral_clock(ID_PIT) / 1000000;
\r
80 PIT->PIT_MR = period ? (period * pit_frequency + 8) >> 4 : 0;
\r
81 PIT->PIT_MR |= PIT_MR_PITEN;
\r
84 void pit_set_piv(uint32_t piv)
\r
86 uint32_t dwMr = PIT->PIT_MR & (~PIT_MR_PIV_Msk);
\r
87 PIT->PIT_MR = dwMr | PIT_MR_PIV(piv);
\r
90 void pit_enable(void)
\r
92 PIT->PIT_MR |= PIT_MR_PITEN;
\r
95 void pit_disable(void)
\r
97 PIT->PIT_MR &= ~PIT_MR_PITEN;
\r
100 void pit_enable_it(void)
\r
102 PIT->PIT_MR |= PIT_MR_PITIEN;
\r
105 void pit_disable_it(void)
\r
107 PIT->PIT_MR &= ~PIT_MR_PITIEN;
\r
110 uint32_t pit_get_mode(void)
\r
112 return PIT->PIT_MR;
\r
115 uint32_t pit_get_status(void)
\r
117 return PIT->PIT_SR;
\r
120 uint32_t pit_get_piir(void)
\r
122 return PIT->PIT_PIIR;
\r
125 uint32_t pit_get_pivr(void)
\r
127 return PIT->PIT_PIVR;
\r