-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfreemodbusSerial.cpp
117 lines (90 loc) · 4.05 KB
/
freemodbusSerial.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
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
/**
* \file
* \brief Definitions of FreeMODBUS functions related to serial port
*
* \author Copyright (C) 2019-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 "freemodbusSerialPoll.hpp"
#include "FreemodbusInstance.hpp"
#include "mbport.h"
#include "distortos/devices/communication/SerialPort.hpp"
#include <cassert>
/*---------------------------------------------------------------------------------------------------------------------+
| global functions
+---------------------------------------------------------------------------------------------------------------------*/
void freemodbusSerialPoll(FreemodbusInstance& instance, const distortos::TickClock::time_point deadline)
{
assert(instance.serialPort != nullptr);
while (instance.serialMode == FreemodbusInstance::SerialMode::receiver)
{
const auto ret = instance.serialPort->tryReadUntil(deadline, instance.frameBuffer,
sizeof(instance.frameBuffer));
if (ret.second == 0)
return;
instance.bytesInBuffer = ret.second;
instance.rxPosition = {};
for (size_t i {}; i < ret.second; ++i)
instance.rawInstance.pxMBFrameCBByteReceived(&instance.rawInstance);
}
if (instance.serialMode == FreemodbusInstance::SerialMode::transmiter)
{
while (instance.serialMode == FreemodbusInstance::SerialMode::transmiter)
instance.rawInstance.pxMBFrameCBTransmitterEmpty(&instance.rawInstance);
instance.serialPort->write(instance.frameBuffer, instance.txPosition);
}
}
extern "C" void vMBPortSerialEnable(xMBInstance* const instance, const bool rxEnable, const bool txEnable)
{
assert(instance != nullptr);
assert(rxEnable == false || txEnable == false); // no more than one function may be enabled
auto& freemodbusInstance = *reinterpret_cast<FreemodbusInstance*>(instance);
freemodbusInstance.serialMode = rxEnable == true ? FreemodbusInstance::SerialMode::receiver :
txEnable == true ? FreemodbusInstance::SerialMode::transmiter : FreemodbusInstance::SerialMode::disabled;
if (rxEnable == true)
freemodbusInstance.rxPosition = {};
if (txEnable == true)
freemodbusInstance.txPosition = {};
}
extern "C" void xMBPortSerialClose(xMBInstance* const instance)
{
assert(instance != nullptr);
xMBPortTimersClose(instance);
auto& freemodbusInstance = *reinterpret_cast<FreemodbusInstance*>(instance);
freemodbusInstance.serialMode = FreemodbusInstance::SerialMode::disabled;
assert(freemodbusInstance.serialPort != nullptr);
const auto ret = freemodbusInstance.serialPort->close();
assert(ret == 0);
}
extern "C" bool xMBPortSerialGetByte(xMBInstance* const instance, uint8_t* const byte)
{
assert(instance != nullptr);
auto& freemodbusInstance = *reinterpret_cast<FreemodbusInstance*>(instance);
if (freemodbusInstance.rxPosition >= freemodbusInstance.bytesInBuffer)
return false;
*byte = freemodbusInstance.frameBuffer[freemodbusInstance.rxPosition++];
return true;
}
extern "C" bool xMBPortSerialInit(xMBInstance* const instance, uint8_t, const uint32_t baudRate, const uint8_t dataBits,
const eMBParity parity)
{
assert(instance != nullptr);
auto& freemodbusInstance = *reinterpret_cast<FreemodbusInstance*>(instance);
const auto uartParity = parity == MB_PAR_ODD ? distortos::devices::UartParity::odd :
parity == MB_PAR_EVEN ? distortos::devices::UartParity::even : distortos::devices::UartParity::none;
assert(freemodbusInstance.serialPort != nullptr);
const auto ret = freemodbusInstance.serialPort->open(baudRate, dataBits, uartParity, false);
return ret == 0;
}
extern "C" bool xMBPortSerialPutByte(xMBInstance* const instance, const uint8_t byte)
{
assert(instance != nullptr);
auto& freemodbusInstance = *reinterpret_cast<FreemodbusInstance*>(instance);
if (freemodbusInstance.txPosition >= sizeof(freemodbusInstance.frameBuffer) - 1)
return false;
freemodbusInstance.frameBuffer[freemodbusInstance.txPosition++] = byte;
return true;
}