1 /*******************************************************************************
2 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
3 * Author: Ismail H. Kose <[email protected]>
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
19 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * Except as contained in this notice, the name of Maxim Integrated
24 * Products, Inc. shall not be used except as stated in the Maxim Integrated
25 * Products, Inc. Branding Policy.
26 *
27 * The mere transfer of this software does not imply any licenses
28 * of trade secrets, proprietary technology, copyrights, patents,
29 * trademarks, maskwork rights, or any other form of intellectual
30 * property whatsoever. Maxim Integrated Products, Inc. retains all
31 * ownership rights.
32 *******************************************************************************
33 */
34
35 #include <stdio.h>
36 #include "board.h"
37
38 /***** Definitions *****/
39 #define USE_RTC_SYSTEM_CLK 0
40 #define SYSTICK_PERIOD_EXT_CLK 32767
41
42 /* Trigger interrupt every second */
43 static const uint32_t sysTicks = SYSTICK_PERIOD_EXT_CLK;
44 static volatile uint32_t sys_tick_sec = 0;
45
hal_tick_init(void)46 int32_t hal_tick_init(void)
47 {
48 uint32_t ret;
49 ret = SYS_SysTick_Config(sysTicks, USE_RTC_SYSTEM_CLK);
50 printf("SysTick Clock = %d Hz\n", SYS_SysTick_GetFreq());
51 if(ret != E_NO_ERROR) {
52 printf("ERROR: Ticks is not valid");
53 }
54
55 return ret;
56 }
57
SysTick_Handler(void)58 void SysTick_Handler(void)
59 {
60 sys_tick_sec++;
61 }
62
hal_get_tick(void)63 uint64_t hal_get_tick(void)
64 {
65 uint32_t usec_tick;
66 uint64_t tick_sec;
67 uint32_t systick_val = SysTick->VAL;
68 uint32_t _sys_tick_sec = sys_tick_sec;
69 uint32_t sys_freq = SYS_SysTick_GetFreq();
70
71 usec_tick = ((uint64_t)(sysTicks - systick_val) * 1000000) / sys_freq;
72 if (systick_val == 0) // to protect time overflow
73 _sys_tick_sec -= 1;
74 tick_sec = _sys_tick_sec * 1000000 + usec_tick;
75 return tick_sec;
76 }
77
hal_delay_ms(unsigned int ms)78 void hal_delay_ms(unsigned int ms)
79 {
80 uint64_t prev_tick = hal_get_tick();
81 uint64_t wait_time = ms * 1000;
82 uint64_t curr_tick;
83 int64_t diff;
84
85 while(1) {
86 curr_tick = hal_get_tick();
87 diff = curr_tick - prev_tick;
88 if (diff > wait_time) {
89 break;
90 }
91 }
92 }
93
hal_delay_us(unsigned int us)94 void hal_delay_us(unsigned int us)
95 {
96 uint64_t prev_tick = hal_get_tick();
97 uint64_t wait_time = us;
98 uint64_t curr_tick;
99 int64_t diff;
100
101 while(1) {
102 curr_tick = hal_get_tick();
103 diff = curr_tick - prev_tick;
104 if (diff > wait_time) {
105 break;
106 }
107 }
108 }
109
hal_get_time_ms(void)110 uint32_t hal_get_time_ms(void)
111 {
112 uint32_t usec_tick;
113 uint64_t tick_sec;
114 uint32_t systick_val = SysTick->VAL;
115 uint32_t _sys_tick_sec = sys_tick_sec;
116 uint32_t sys_freq = SYS_SysTick_GetFreq();
117
118 usec_tick = ((uint64_t)(sysTicks - systick_val) * 1000) / sys_freq;
119 if (systick_val == 0) // to protect time overflow
120 _sys_tick_sec -= 1;
121 tick_sec = _sys_tick_sec * 1000 + usec_tick;
122 return tick_sec;
123 }
124
hal_time_ms(void)125 uint32_t hal_time_ms(void)
126 {
127 return hal_get_time_ms();
128 }
129