Welcome to mirror list, hosted at ThFree Co, Russian Federation.

cm3cpp_systick.cpp - github.com/thirdpin/libopencm3_cpp_extensions.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3aca6676f1f705400b7a3995054d9446ee68e333 (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
 * 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 "cm3cpp_systick.h"

#define __EXPAND2(a, b) a##b
#define __EXPAND3(a, b, c) a##b##c

#define __INT_SOURCE(n) __EXPAND2(TIM, n)
#define __RCC(n) __EXPAND2(rcc_periph_clken::RCC_TIM, n)
#define __SYSTICK_INT_FUNC(n) __EXPAND3(tim, n, _isr)

#define CM3CPP_INT_SOURCE __INT_SOURCE(CM3CPP_TIMER_N)
#define CM3CPP_INT_SOURCE_RCC __RCC(CM3CPP_TIMER_N)

#if CM3CPP_ENABLE_CUSTOM_SYSTICK_SOURCE == 1
#define CM3CPP_SYSTICK_INT_FUNC __SYSTICK_INT_FUNC(CM3CPP_TIMER_N)
extern "C" {
void CM3CPP_SYSTICK_INT_FUNC(void);
}
#include "irq/cm3cpp_irq.h"
#include <libopencm3/stm32/rcc.h>
#include <libopencm3/stm32/timer.h>
#else
#define CM3CPP_SYSTICK_INT_FUNC sys_tick_handler
#endif

static volatile uint32_t counter;

#if CM3CPP_ENABLE_CUSTOM_SYSTICK_SOURCE != 1
void CM3CPP_SYSTICK_INT_FUNC(void)
{
    counter++;
}
#endif

void delay_nop(uint32_t count)
{
    while (count--) {
        __asm("nop");
    }
}

namespace cm3cpp {

namespace systick {

void delay_systick(uint32_t ms)
{
    uint32_t time = get_counter();
    while ((get_counter() - time) < ms) {
        __asm("nop");
    }
}

void init(uint32_t clock_div)
{
#if CM3CPP_ENABLE_CUSTOM_SYSTICK_SOURCE == 1
    rcc_periph_clock_enable(CM3CPP_INT_SOURCE_RCC);
    timer_set_mode(CM3CPP_INT_SOURCE, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE,
                   TIM_CR1_DIR_UP);
    timer_set_prescaler(CM3CPP_INT_SOURCE,
                        (CM3CPP_SYSTICK_CLOCK / clock_div) - 1);
    timer_disable_preload(CM3CPP_INT_SOURCE);
    timer_continuous_mode(CM3CPP_INT_SOURCE);
    timer_set_period(CM3CPP_INT_SOURCE, CM3CPP_SYSTICK_PERIOD);
    timer_enable_counter(CM3CPP_INT_SOURCE);
#else
    counter = 0;
    systick_set_reload(CM3CPP_SYSTEM_CORE_CLOCK / clock_div);
    systick_set_clocksource(STK_CSR_CLKSOURCE_AHB);
    systick_counter_enable();
    systick_interrupt_enable();
#endif
}

void deinit()
{
#if CM3CPP_ENABLE_CUSTOM_SYSTICK_SOURCE == 1
    timer_disable_counter(CM3CPP_INT_SOURCE);
#else
    systick_interrupt_disable();
    systick_counter_disable();
#endif
}

uint32_t get_counter()
{
#if CM3CPP_ENABLE_CUSTOM_SYSTICK_SOURCE == 1
    return timer_get_counter(CM3CPP_INT_SOURCE);
#else
    return counter;
#endif
}

Counter::Counter(Mode mode, uint32_t period)
{
    init(mode, period);
}

void Counter::init(Mode mode, uint32_t period)
{
    _mode = mode;
    _saved = get_counter();
    _period = period;

    switch (_mode) {
        case CYCLE:
            _is_active = true;
            break;
        case ONE_SHOT:
            _is_active = false;
            break;
    }
}

bool Counter::timeout()
{
    if (_is_active && ((get_counter() - _saved) >= _period)) {
        switch (_mode) {
            case CYCLE:
                _saved = get_counter();
                return true;
            case ONE_SHOT:
                return true;
        }
    }

    return false;
}

bool Counter::start()
{
    if (_mode == CYCLE)
        return false;
    _saved = get_counter();
    _is_active = true;
    return true;
}

bool Counter::stop()
{
    if (_mode == CYCLE)
        return false;
    _is_active = false;
    return true;
}

}  // namespace systick

}  // namespace cm3cpp