-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUSS.cpp
More file actions
298 lines (239 loc) · 8.33 KB
/
USS.cpp
File metadata and controls
298 lines (239 loc) · 8.33 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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/**
* Copyright (c) 2020, Merlin Kr�mmel
* SPDX-License-Identifier: LGPL-3.0-or-later
*/
/**
* @section LICENSE
*
* This library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, version 3 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file USS.cpp
* @brief class definition for Siemens USS protocol, implements the low level
* functionality of communication protocol as baselayer for higher layers
* like inverters.
* @author Merlin Kr�mmel
* @date 22.07.2020
*/
#include "USS.h"
//extern USS uss;
template<size_t nrSlaves>
USS<nrSlaves>::USS() :
m_slaves{0},
m_actualSlave(0),
m_sendBuffer{0},
m_recvBuffer{0},
m_mainsetpoint{0},
m_mainactualvalue{0},
m_ctlword{0},
m_statusword{0},
m_paramValue{{0}, {0}},
m_nextSend(0),
m_period(0),
m_characterRuntime(0),
m_dePin(-1)
{
m_sendBuffer[0] = STX_BYTE_STX;
m_sendBuffer[1] = (PKW_LENGTH_CHARACTERS * PKW_ANZ) + (PZD_LENGTH_CHARACTERS * PZD_ANZ) + 2; // 2 for ADR and BCC bytes
}
template<size_t nrSlaves>
int USS<nrSlaves>::begin(const long speed, const byte slaves[])
{
int telegramRuntime;
if(slaves == nullptr)
return -1;
memcpy(m_slaves, slaves, nrSlaves);
m_characterRuntime = CHARACTER_RUNTIME_BASE_US / (speed / BAUDRATE_BASE);
telegramRuntime = USS_BUFFER_LENGTH * m_characterRuntime * 1.5f / 1000;
Serial1.begin(speed, SERIAL_8E1);
Serial1.setTimeout(telegramRuntime + MAX_RESP_DELAY_TIME_MS);
m_period = telegramRuntime * 2 + (START_DELAY_LENGTH_CHARACTERS * m_characterRuntime / 1000) + MAX_RESP_DELAY_TIME_MS + MASTER_COMPUTE_DELAY_MS;
return 0;
}
template<size_t nrSlaves>
int USS<nrSlaves>::begin(const long speed, const byte slaves[], const int dePin)
{
m_dePin = dePin;
pinMode(m_dePin,OUTPUT);
digitalWrite(m_dePin, HIGH);
return begin(speed, slaves);
}
template<size_t nrSlaves>
int USS<nrSlaves>::setParameter(const uint16_t param, const uint16_t value, const int slaveIndex)
{
int ret = 0;
if(slaveIndex >= nrSlaves)
return -1;
m_paramValue[0][slaveIndex] = (param & PKE_WORD_PARAM_MASK) | PKE_WORD_AK_CHW_PWE;
m_paramValue[1][slaveIndex] = 0;
m_paramValue[2][slaveIndex] = 0;
m_paramValue[3][slaveIndex] = value;
while(m_paramValue[0][slaveIndex] != PARAM_VALUE_EMPTY)
{
if(send())
{
ret = receive();
}
}
return ret;
}
template<size_t nrSlaves>
int USS<nrSlaves>::setParameter(const uint16_t param, const uint32_t value, const int slaveIndex)
{
int ret = 0;
if(slaveIndex >= nrSlaves)
return -1;
m_paramValue[0][slaveIndex] = (param & PKE_WORD_PARAM_MASK) | PKE_WORD_AK_CHD_PWE;
m_paramValue[1][slaveIndex] = 0;
// USS is Big-Endian
m_paramValue[2][slaveIndex] = (value >> 16) & 0xFFFF;
m_paramValue[3][slaveIndex] = value & 0xFFFF;
while(m_paramValue[0][slaveIndex] != PARAM_VALUE_EMPTY)
{
if(send())
{
ret = receive();
}
}
return ret;
}
template<size_t nrSlaves>
int USS<nrSlaves>::setParameter(const uint16_t param, const float value, const int slaveIndex)
{
uint32_t u32 = 0;;
memcpy(&u32, &value, sizeof(u32));
return setParameter(param, u32, slaveIndex);
}
template<size_t nrSlaves>
void USS<nrSlaves>::setMainsetpoint(const uint16_t value, const int slaveIndex)
{
if(slaveIndex >= nrSlaves)
return;
m_mainsetpoint[slaveIndex] = value;
}
template<size_t nrSlaves>
void USS<nrSlaves>::setCtlFlag(const uint16_t flags, const int slaveIndex)
{
if(slaveIndex >= nrSlaves)
return;
m_ctlword[slaveIndex] |= flags;
}
template<size_t nrSlaves>
void USS<nrSlaves>::clearCtlFlag(const uint16_t flags, const int slaveIndex)
{
if(slaveIndex >= nrSlaves)
return;
m_ctlword[slaveIndex] &= ~flags;
}
template<size_t nrSlaves>
uint16_t USS<nrSlaves>::getActualvalue(const int slaveIndex) const
{
if(slaveIndex >= nrSlaves)
return -1;
uint16_t ret;
ret = m_mainactualvalue[slaveIndex];
return ret;
}
template<size_t nrSlaves>
bool USS<nrSlaves>::checkStatusFlag(const uint16_t flag, const int slaveIndex) const
{
if(slaveIndex >= nrSlaves)
return false;
return (m_statusword[slaveIndex] & flag) != 0 ? true : false;
}
template<size_t nrSlaves>
byte USS<nrSlaves>::BCC(const byte buffer[], const int length) const
{
byte ret = 0;
for(int i = 0; i < length; i++)
ret = ret ^ buffer[i];
return ret;
}
template<size_t nrSlaves>
bool USS<nrSlaves>::send()
{
//while(!(millis() > m_nextSend && (millis() - m_nextSend) < 10000));
if((millis() - m_lastSend) < m_period)
{
return false;
}
m_lastSend = millis();
if(m_actualSlave == nrSlaves)
m_actualSlave = 0;
m_sendBuffer[2] = m_slaves[m_actualSlave] & ADDR_BYTE_ADDR_MASK;
if(m_paramValue[0][m_actualSlave] != PARAM_VALUE_EMPTY)
{
// USS is Big-Endian
m_sendBuffer[3] = (m_paramValue[0][m_actualSlave] >> 8) & 0xFF;
m_sendBuffer[4] = m_paramValue[0][m_actualSlave] & 0xFF;
m_sendBuffer[5] = (m_paramValue[1][m_actualSlave] >> 8) & 0xFF;
m_sendBuffer[6] = m_paramValue[1][m_actualSlave] & 0xFF;
m_sendBuffer[7] = (m_paramValue[2][m_actualSlave] >> 8) & 0xFF;
m_sendBuffer[8] = m_paramValue[2][m_actualSlave] & 0xFF;
m_sendBuffer[9] = (m_paramValue[3][m_actualSlave] >> 8) & 0xFF;
m_sendBuffer[10] = m_paramValue[3][m_actualSlave] & 0xFF;
}
else
{
memset(&m_sendBuffer[3], 0, 8);
}
// USS is Big-Endian
m_sendBuffer[PKW_LENGTH_CHARACTERS * PKW_ANZ + 3] = (m_ctlword[m_actualSlave] >> 8) & 0xFF;
m_sendBuffer[PKW_LENGTH_CHARACTERS * PKW_ANZ + 4] = m_ctlword[m_actualSlave] & 0xFF;
m_sendBuffer[PKW_LENGTH_CHARACTERS * PKW_ANZ + 5] = (m_mainsetpoint[m_actualSlave] >> 8) & 0xFF;
m_sendBuffer[PKW_LENGTH_CHARACTERS * PKW_ANZ + 6] = m_mainsetpoint[m_actualSlave] & 0xFF;
m_sendBuffer[USS_BUFFER_LENGTH - 1] = BCC(m_sendBuffer, USS_BUFFER_LENGTH - 1);
Serial1.write(m_sendBuffer, USS_BUFFER_LENGTH);
Serial1.flush();
delayMicroseconds(START_DELAY_LENGTH_CHARACTERS * m_characterRuntime);
if (m_dePin != -1)
digitalWrite(m_dePin, LOW);
return true;
}
template<size_t nrSlaves>
int USS<nrSlaves>::receive()
{
int ret = 0;
if(Serial1.readBytes(m_recvBuffer, USS_BUFFER_LENGTH) == USS_BUFFER_LENGTH &&
m_recvBuffer[0] == STX_BYTE_STX && (m_recvBuffer[2] & ADDR_BYTE_ADDR_MASK) == (m_slaves[m_actualSlave] & ADDR_BYTE_ADDR_MASK) &&
BCC(m_recvBuffer, USS_BUFFER_LENGTH - 1) == m_recvBuffer[USS_BUFFER_LENGTH - 1])
{
// USS is Big-Endian
m_statusword[m_actualSlave] = (m_recvBuffer[PKW_LENGTH_CHARACTERS * PKW_ANZ + 3] << 8) & 0xFF00;
m_statusword[m_actualSlave] |= m_recvBuffer[PKW_LENGTH_CHARACTERS * PKW_ANZ + 4] & 0xFF;
m_mainactualvalue[m_actualSlave] = (m_recvBuffer[PKW_LENGTH_CHARACTERS * PKW_ANZ + 5] << 8) & 0xFF00;
m_mainactualvalue[m_actualSlave] |= m_recvBuffer[PKW_LENGTH_CHARACTERS * PKW_ANZ + 6] & 0xFF;
if(m_paramValue[0][m_actualSlave] != PARAM_VALUE_EMPTY)
{
if(((m_recvBuffer[3] << 8) & PKE_WORD_AK_MASK) == PKE_WORD_AK_NO_RESP)
ret = -1;
if(((m_recvBuffer[3] << 8) & PKE_WORD_AK_MASK) == PKE_WORD_AK_NO_RIGHTS)
ret = -2;
if(((m_recvBuffer[3] << 8) & PKE_WORD_AK_MASK) == PKE_WORD_AK_CANT_EXECUTE)
{
ret = m_recvBuffer[10] | m_recvBuffer[9] << 8;
if(!ret)
ret = -3; // 0 is error code for illegal parameter number
}
m_paramValue[0][m_actualSlave] = PARAM_VALUE_EMPTY;
}
}
else
{
ret = -1;
}
if (m_dePin != -1)
digitalWrite(m_dePin, HIGH);
m_actualSlave++;
return ret;
}