-
Notifications
You must be signed in to change notification settings - Fork 168
Expand file tree
/
Copy pathdelay_impl.hpp.in
More file actions
67 lines (61 loc) · 1.77 KB
/
Copy pathdelay_impl.hpp.in
File metadata and controls
67 lines (61 loc) · 1.77 KB
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
/*
* Copyright (c) 2020, Niklas Hauser
*
* This file is part of the modm project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
// ----------------------------------------------------------------------------
#pragma once
/// @cond
#include <util/delay.h>
#include <modm/architecture/utils.hpp>
#define MODM_DELAY_NS_IS_ACCURATE 0
namespace modm
{
modm_always_inline void
delay_ns(uint32_t ns)
{
__builtin_constant_p(ns) ? ({
// ns is a constant value, we can let the compiler do the work
const uint32_t cycles = uint32_t(ceil(fabs((F_CPU * double(ns)) / 1e9)));
__builtin_avr_delay_cycles(cycles);
}) : ({
const uint16_t loops = ns >> {{ shift }};
if (loops) _delay_loop_2(loops);
});
}
modm_always_inline void
delay_us(uint32_t us)
{
__builtin_constant_p(us) ? ({
// ns is a constant value, we can let the compiler do the work
const uint32_t cycles = uint32_t(ceil(fabs((F_CPU * double(us)) / 1e6)));
__builtin_avr_delay_cycles(cycles);
}) : ({
%% if f_cpu <= 6000000
constexpr int32_t cycles = (F_CPU / 1e6);
for (int32_t _us = us; _us > 0; _us -= cycles)
__builtin_avr_delay_cycles(1);
%% else
constexpr int32_t cycles = (F_CPU / 1e6) - 6;
for (uint32_t _us = us; _us; _us--)
__builtin_avr_delay_cycles(cycles);
%% endif
});
}
modm_always_inline void
delay_ms(uint32_t ms)
{
__builtin_constant_p(ms) ? ({
// ns is a constant value, we can let the compiler do the work
const uint32_t cycles = uint32_t(ceil(fabs((F_CPU * double(ms)) / 1e3)));
__builtin_avr_delay_cycles(cycles);
}) : ({
while(ms--) __builtin_avr_delay_cycles((F_CPU / 1e3) - 10);
});
}
} // namespace modm
/// @endcond