-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfreemodbusTimers.cpp
78 lines (61 loc) · 2.65 KB
/
freemodbusTimers.cpp
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
/**
* \file
* \brief Definitions of timers-related functions for FreeMODBUS
*
* \author Copyright (C) 2019 Aleksander Szczygiel https://distortec.com https://freddiechopin.info
* \author Copyright (C) 2021-2022 Kamil Szczygiel https://distortec.com https://freddiechopin.info
*
* \par License
* 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/.
*/
#include "freemodbusTimersPoll.hpp"
#include "FreemodbusInstance.hpp"
#include "mbport.h"
#include "distortos/ThisThread.hpp"
#include <cassert>
/*---------------------------------------------------------------------------------------------------------------------+
| global functions
+---------------------------------------------------------------------------------------------------------------------*/
distortos::TickClock::time_point freemodbusTimersPoll(FreemodbusInstance& instance)
{
if (instance.timerDeadline != decltype(instance.timerDeadline)::max() &&
distortos::TickClock::now() >= instance.timerDeadline)
{
instance.timerDeadline = decltype(instance.timerDeadline)::max();
instance.rawInstance.pxMBPortCBTimerExpired(&instance.rawInstance);
}
return instance.timerDeadline;
}
extern "C" void vMBPortTimersDelay(xMBInstance*, const uint16_t timeoutMs)
{
distortos::ThisThread::sleepFor(std::chrono::milliseconds{timeoutMs});
}
extern "C" void vMBPortTimersDisable(xMBInstance* const instance)
{
assert(instance != nullptr);
auto& freemodbusInstance = *reinterpret_cast<FreemodbusInstance*>(instance);
freemodbusInstance.timerDeadline = decltype(freemodbusInstance.timerDeadline)::max();
}
extern "C" void vMBPortTimersEnable(xMBInstance* const instance)
{
assert(instance != nullptr);
auto& freemodbusInstance = *reinterpret_cast<FreemodbusInstance*>(instance);
freemodbusInstance.timerDeadline = distortos::TickClock::now() + freemodbusInstance.timerDuration;
}
extern "C" void xMBPortTimersClose(xMBInstance*)
{
// Does not use any hardware resources
}
extern "C" bool xMBPortTimersInit(xMBInstance* const instance, const uint16_t timeout50us)
{
assert(instance != nullptr);
assert(timeout50us != 0);
auto& freemodbusInstance = *reinterpret_cast<FreemodbusInstance*>(instance);
const auto duration = std::chrono::microseconds{timeout50us * 50};
freemodbusInstance.timerDuration = std::chrono::duration_cast<decltype(freemodbusInstance.timerDuration)>(duration);
if (freemodbusInstance.timerDuration < duration)
++freemodbusInstance.timerDuration;
freemodbusInstance.timerDeadline = decltype(freemodbusInstance.timerDeadline)::max();
return true;
}