summaryrefslogtreecommitdiff
path: root/digital/ucoolib/ucoolib/utils/delay.arm.cc
blob: 8410c8d1707d4502279082807ab7a1fa05143414 (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
// ucoolib - Microcontroller object oriented library. {{{
//
// Copyright (C) 2012 Nicolas Schodet
//
// APBTeam:
//        Web: http://apbteam.org/
//      Email: team AT apbteam DOT org
//
// 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 2 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, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
// }}}
#include "delay.arm.hh"

#include <algorithm>

#include <libopencm3/stm32/systick.h>
#include <libopencm3/stm32/f4/rcc.h>

namespace ucoo {

void
delay_us (int us)
{
    // Horrible hack: there is no record of the current systick speed, make
    // guesses based on APB2 clock. Also, suppose that frequency is a multiple
    // of 1 MHz (to avoid 64 bit division).
    int systick_mhz = rcc_ppre2_frequency / (4 * 1000000);
    int cycles = systick_mhz * us;
    STK_CTRL = 0;
    // Loop several times if cycles is too big for the systick timer. Some
    // nanoseconds are lost every second, I can live with that, it simplifies
    // code.
    while (cycles)
    {
        int loop_cycles = std::min (1 << 24, cycles);
        STK_LOAD = loop_cycles - 1;
        STK_VAL = 0;
        STK_CTRL = STK_CTRL_ENABLE;
        while (!(STK_CTRL & STK_CTRL_COUNTFLAG))
            ;
        STK_CTRL = 0;
        cycles -= loop_cycles;
    }
}

void
delay_ns (int ns)
{
    // Horrible hack: there is no record of the current hclock speed, make
    // guesses based on APB2 clock. Also, suppose that frequency is a multiple
    // of 1 MHz (to avoid 64 bit division).
    int hclock_mhz = rcc_ppre2_frequency / (1000000 / 2);
    int cycles = (hclock_mhz * ns + 999) / 1000;
    STK_CTRL = 0;
    // Loop once, ns is supposed to be small.
    STK_LOAD = cycles - 1;
    STK_VAL = 0;
    STK_CTRL = STK_CTRL_CLKSOURCE_AHB | STK_CTRL_ENABLE;
    while (!(STK_CTRL & STK_CTRL_COUNTFLAG))
        ;
    STK_CTRL = 0;
}

} // namespace ucoo