1 
2 /**
3  * \file
4  *
5  * \brief SysTick related functionality implementation.
6  *
7  * Copyright (C) 2014 Atmel Corporation. All rights reserved.
8  *
9  * \asf_license_start
10  *
11  * \page License
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions are met:
15  *
16  * 1. Redistributions of source code must retain the above copyright notice,
17  *    this list of conditions and the following disclaimer.
18  *
19  * 2. Redistributions in binary form must reproduce the above copyright notice,
20  *    this list of conditions and the following disclaimer in the documentation
21  *    and/or other materials provided with the distribution.
22  *
23  * 3. The name of Atmel may not be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * 4. This software may only be redistributed and used in connection with an
27  *    Atmel microcontroller product.
28  *
29  * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
30  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
31  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
32  * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
33  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
38  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39  * POSSIBILITY OF SUCH DAMAGE.
40  *
41  * \asf_license_stop
42  *
43  */
44 
45 #include <hpl_time_measure.h>
46 #include <hpl_systick_config.h>
47 
48 /**
49  * \brief Initialize system time module
50  */
_system_time_init(void * const hw)51 void _system_time_init(void *const hw)
52 {
53 	(void)hw;
54 	SysTick->LOAD = (0xFFFFFF << SysTick_LOAD_RELOAD_Pos);
55 	SysTick->CTRL = (1 << SysTick_CTRL_ENABLE_Pos) | (CONF_SYSTICK_TICKINT << SysTick_CTRL_TICKINT_Pos)
56 	                | (1 << SysTick_CTRL_CLKSOURCE_Pos);
57 }
58 /**
59  * \brief Initialize delay functionality
60  */
_delay_init(void * const hw)61 void _delay_init(void *const hw)
62 {
63 	_system_time_init(hw);
64 }
65 
66 /**
67  * \brief De-initialize system time module
68  */
_system_time_deinit(void * const hw)69 void _system_time_deinit(void *const hw)
70 {
71 	(void)hw;
72 	SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;
73 }
74 
75 /**
76  * \brief Get system time
77  */
_system_time_get(const void * const hw)78 system_time_t _system_time_get(const void *const hw)
79 {
80 	(void)hw;
81 	return (system_time_t)SysTick->VAL;
82 }
83 
84 /**
85  * \brief Get maximum possible system time
86  */
_system_time_get_max_time_value(const void * const hw)87 system_time_t _system_time_get_max_time_value(const void *const hw)
88 {
89 	(void)hw;
90 	return 0xFFFFFF;
91 }
92 /**
93  * \brief Delay loop to delay n number of cycles
94  */
_delay_cycles(void * const hw,uint32_t cycles)95 void _delay_cycles(void *const hw, uint32_t cycles)
96 {
97 	(void)hw;
98 	uint8_t  n   = cycles >> 24;
99 	uint32_t buf = cycles;
100 
101 	while (n--) {
102 		SysTick->LOAD = 0xFFFFFF;
103 		SysTick->VAL  = 0xFFFFFF;
104 		while (!(SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk))
105 			;
106 		buf -= 0xFFFFFF;
107 	}
108 
109 	SysTick->LOAD = buf;
110 	SysTick->VAL  = buf;
111 	while (!(SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk))
112 		;
113 }
114