blob: 46c7b0f2bc6f537ade37043bcd9a3393a7079026 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
/*
* This file is part of the libopencm3_cpp_extensions project.
* hosted at http://github.com/thirdpin/libopencm3_cpp_extensions
*
* Copyright (C) 2016 Third Pin LLC
* Written by Anastasiia Lazareva <a.lazareva@thirdpin.ru>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
SYSTICK implementation, public interface
*/
#include "systick_ext.h"
#if ENABLE_CUSTOM_SYSTICK_SOURCE == 1
extern "C" {
void SYS_TICK_INT_FUNC(void);
}
#include "libopencm3/stm32/timer.h"
#endif
volatile uint32_t counter_ms;
void SYS_TICK_INT_FUNC(void)
{
#if ENABLE_CUSTOM_SYSTICK_SOURCE == 1
if (timer_get_flag(INT_SOURCE, TIM_SR_UIF)) {
timer_clear_flag(INT_SOURCE, TIM_SR_UIF);
counter_ms++;
}
#else
counter_ms++;
#endif
}
void delay_nop(uint32_t count)
{
while(count--) {
__asm("nop");
}
}
void delay_ms(uint32_t ms)
{
uint32_t time = counter_ms;
while((counter_ms - time) < ms);
}
//by default sets up a timer to create 1ms ticks (div = 1000)
//at system clock 120mhz.
void systick_init(uint32_t div)
{
counter_ms = 0;
systick_set_reload(SYSTEM_CORE_CLOCK / div);
systick_set_clocksource(STK_CSR_CLKSOURCE_AHB);
systick_counter_enable();
systick_interrupt_enable();
}
uint32_t get_counter_ms()
{
return counter_ms;
}
TimerMs::TimerMs(TimerMode mode, uint32_t period_ms)
{
_mode = mode;
_saved_ms = get_counter_ms();
_period_ms = period_ms;
switch (_mode)
{
case CYCLE:
_is_active = true;
break;
case ONE_SHOT:
_is_active = false;
break;
}
}
bool TimerMs::timeout()
{
if (_is_active && ((counter_ms - _saved_ms) >= _period_ms)) {
switch (_mode)
{
case CYCLE:
_saved_ms = counter_ms;
return true;
case ONE_SHOT:
return true;
}
}
return false;
}
bool TimerMs::start()
{
if(_mode == CYCLE)
return false;
_saved_ms = counter_ms;
_is_active = true;
return true;
}
bool TimerMs::stop()
{
if(_mode == CYCLE)
return false;
_is_active = false;
return true;
}
|