1 /**
2 * \file  hw_timer.c
3 *
4 * \brief Wrapper used by sw_timer utility using ASF timer api's
5 *
6 * Copyright (C) 2016 Atmel Corporation. All rights reserved.
7 *
8 * \asf_license_start
9 *
10 * \page License
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions are met:
14 *
15 * 1. Redistributions of source code must retain the above copyright notice,
16 *    this list of conditions and the following disclaimer.
17 *
18 * 2. Redistributions in binary form must reproduce the above copyright notice,
19 *    this list of conditions and the following disclaimer in the documentation
20 *    and/or other materials provided with the distribution.
21 *
22 * 3. The name of Atmel may not be used to endorse or promote products derived
23 *    from this software without specific prior written permission.
24 *
25 * 4. This software may only be redistributed and used in connection with an
26 *    Atmel microcontroller product.
27 *
28 * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
29 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
30 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
31 * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
32 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
36 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
37 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
39 *
40 * \asf_license_stop
41 *
42 * Modified for use with Atmel START hpl_rtc functions
43 */
44 
45 /**************************************** INCLUDES*****************************/
46 #include <stdint.h>
47 #include <stdbool.h>
48 #include <utils_assert.h>
49 #include <utils.h>
50 #include <hal_atomic.h>
51 #include <hpl_irq.h>
52 
53 #include "board-config.h"
54 #include "gpio.h"
55 #include "hw_timer.h"
56 
57 
58 /**************************************** MACROS*****************************/
59 //#define USE_HWTMR_DEBUG
60 
61 #define COMPARE_COUNT_MAX_VALUE ( uint32_t )( -1 )
62 
63 /**************************************** GLOBALS*****************************/
64 
65 HwTimerCallback_t HwTimerAlarmCallback = NULL;
66 HwTimerCallback_t HwTimerOverflowCallback = NULL;
67 
68 #if defined( USE_HWTMR_DEBUG )
69 Gpio_t DbgHwTmrPin;
70 #endif
71 
72 /************************************** IMPLEMENTATION************************/
73 
74 /**
75 * \brief Initializes the hw timer module
76 */
HwTimerInit(void)77 void HwTimerInit(void)
78 {
79 #if defined( USE_HWTMR_DEBUG )
80     GpioInit( &DbgHwTmrPin, HWTMR_DBG_PIN_0, PIN_OUTPUT, PIN_PUSH_PULL, PIN_NO_PULL, 0 );
81 #endif
82 
83     hri_mclk_set_APBAMASK_RTC_bit(MCLK);
84     hri_rtcmode0_write_CTRLA_reg(RTC, RTC_MODE0_CTRLA_SWRST);
85     hri_rtcmode0_wait_for_sync(RTC, RTC_MODE0_SYNCBUSY_SWRST);
86 
87     hri_rtcmode0_write_CTRLA_reg(RTC, RTC_MODE0_CTRLA_PRESCALER(0) |
88                                  RTC_MODE0_CTRLA_COUNTSYNC);
89     hri_rtcmode0_write_EVCTRL_reg(RTC, RTC_MODE0_EVCTRL_CMPEO0);
90     hri_rtcmode0_write_COMP_reg(RTC, 0, ( uint32_t )COMPARE_COUNT_MAX_VALUE);
91     hri_rtcmode0_set_INTEN_CMP0_bit(RTC);
92 
93     NVIC_EnableIRQ(RTC_IRQn);
94     hri_rtcmode0_write_COUNT_reg(RTC, 0);
95     hri_rtcmode0_wait_for_sync(RTC, RTC_MODE0_SYNCBUSY_COUNT);
96     hri_rtcmode0_set_CTRLA_ENABLE_bit(RTC);
97 }
98 
99 /**
100 * \brief This function is used to set the callback when the hw timer
101 * expires.
102 * \param callback Callback to be registered
103 */
HwTimerAlarmSetCallback(HwTimerCallback_t callback)104 void HwTimerAlarmSetCallback(HwTimerCallback_t callback)
105 {
106     HwTimerAlarmCallback = callback;
107 }
108 
109 /**
110 * \brief This function is used to set the callback when the hw timer
111 * overflows.
112 * \param callback Callback to be registered
113 */
HwTimerOverflowSetCallback(HwTimerCallback_t callback)114 void HwTimerOverflowSetCallback(HwTimerCallback_t callback)
115 {
116     HwTimerOverflowCallback = callback;
117 }
118 
119 /**
120 * \brief Loads the timeout in terms of ticks into the hardware
121 * \ticks Time value in terms of timer ticks
122 */
HwTimerLoadAbsoluteTicks(uint32_t ticks)123 bool HwTimerLoadAbsoluteTicks(uint32_t ticks)
124 {
125 #if defined( USE_HWTMR_DEBUG )
126     GpioWrite( &DbgHwTmrPin, 1 );
127 #endif
128 
129     RTC_CRITICAL_SECTION_ENTER();
130     hri_rtcmode0_write_COMP_reg(RTC, 0, ticks);
131     hri_rtcmode0_wait_for_sync(RTC, RTC_MODE0_SYNCBUSY_MASK);
132     uint32_t current = hri_rtcmode0_read_COUNT_reg(RTC);
133     RTC_CRITICAL_SECTION_LEAVE();
134 
135     if((ticks - current - 1) >= (COMPARE_COUNT_MAX_VALUE >> 1)) {
136         // if difference is more than half of max assume timer has passed
137         return false;
138     }
139     if((ticks - current) < 10) {
140         // if too close the matching interrupt does not trigger, so handle same as passed
141         return false;
142     }
143     return true;
144 }
145 
146 /**
147 * \brief Gets the absolute time value
148 * \retval Absolute time in ticks
149 */
HwTimerGetTime(void)150 uint32_t HwTimerGetTime(void)
151 {
152     hri_rtcmode0_wait_for_sync(RTC, RTC_MODE0_SYNCBUSY_COUNT);
153     return hri_rtcmode0_read_COUNT_reg(RTC);
154 }
155 
156 /**
157 * \brief Disables the hw timer module
158 */
HwTimerDisable(void)159 void HwTimerDisable(void)
160 {
161 
162 }
163 
164 /**
165 * \brief Rtc interrupt handler
166 */
RTC_Handler(void)167 void RTC_Handler(void)
168 {
169     /* Read and mask interrupt flag register */
170     uint16_t flag = hri_rtcmode0_read_INTFLAG_reg(RTC);
171 
172     if (flag & RTC_MODE0_INTFLAG_CMP0) {
173 #if defined( USE_HWTMR_DEBUG )
174         GpioWrite( &DbgHwTmrPin, 0 );
175 #endif
176         hri_rtcmode0_clear_interrupt_CMP0_bit(RTC);
177         if (HwTimerAlarmCallback != NULL) {
178             HwTimerAlarmCallback();
179         }
180         /* Clear interrupt flag */
181     }
182     else if ( flag & RTC_MODE0_INTFLAG_OVF) {
183         hri_rtcmode0_clear_interrupt_OVF_bit(RTC);
184         if (HwTimerOverflowCallback != NULL) {
185             HwTimerOverflowCallback();
186         }
187     }
188 
189 }
190 
191 /* eof hw_timer.c */
192