1 /*
2 * Copyright (c) 2020 PHYTEC Messtechnik GmbH
3 * Copyright (c) 2021 Nordic Semiconductor ASA
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8 #include <zephyr/logging/log.h>
9 LOG_MODULE_REGISTER(modbus, CONFIG_MODBUS_LOG_LEVEL);
10
11 #include <zephyr/kernel.h>
12 #include <string.h>
13 #include <zephyr/sys/byteorder.h>
14 #include <modbus_internal.h>
15
16 #define DT_DRV_COMPAT zephyr_modbus_serial
17
18 #define MB_RTU_DEFINE_GPIO_CFG(inst, prop) \
19 static struct gpio_dt_spec prop##_cfg_##inst = { \
20 .port = DEVICE_DT_GET(DT_INST_PHANDLE(inst, prop)), \
21 .pin = DT_INST_GPIO_PIN(inst, prop), \
22 .dt_flags = DT_INST_GPIO_FLAGS(inst, prop), \
23 };
24
25 #define MB_RTU_DEFINE_GPIO_CFGS(inst) \
26 COND_CODE_1(DT_INST_NODE_HAS_PROP(inst, de_gpios), \
27 (MB_RTU_DEFINE_GPIO_CFG(inst, de_gpios)), ()) \
28 COND_CODE_1(DT_INST_NODE_HAS_PROP(inst, re_gpios), \
29 (MB_RTU_DEFINE_GPIO_CFG(inst, re_gpios)), ())
30
31 DT_INST_FOREACH_STATUS_OKAY(MB_RTU_DEFINE_GPIO_CFGS)
32
33 #define MB_RTU_ASSIGN_GPIO_CFG(inst, prop) \
34 COND_CODE_1(DT_INST_NODE_HAS_PROP(inst, prop), \
35 (&prop##_cfg_##inst), (NULL))
36
37 #define MODBUS_DT_GET_SERIAL_DEV(inst) { \
38 .dev = DEVICE_DT_GET(DT_INST_PARENT(inst)), \
39 .de = MB_RTU_ASSIGN_GPIO_CFG(inst, de_gpios), \
40 .re = MB_RTU_ASSIGN_GPIO_CFG(inst, re_gpios), \
41 },
42
43 #ifdef CONFIG_MODBUS_SERIAL
44 static struct modbus_serial_config modbus_serial_cfg[] = {
45 DT_INST_FOREACH_STATUS_OKAY(MODBUS_DT_GET_SERIAL_DEV)
46 };
47 #endif
48
49 #define MODBUS_DT_GET_DEV(inst) { \
50 .iface_name = DEVICE_DT_NAME(DT_DRV_INST(inst)),\
51 .cfg = &modbus_serial_cfg[inst], \
52 },
53
54 #define DEFINE_MODBUS_RAW_ADU(x, _) { \
55 .iface_name = "RAW_"#x, \
56 .rawcb.raw_tx_cb = NULL, \
57 .mode = MODBUS_MODE_RAW, \
58 }
59
60
61 static struct modbus_context mb_ctx_tbl[] = {
62 DT_INST_FOREACH_STATUS_OKAY(MODBUS_DT_GET_DEV)
63 #ifdef CONFIG_MODBUS_RAW_ADU
64 LISTIFY(CONFIG_MODBUS_NUMOF_RAW_ADU, DEFINE_MODBUS_RAW_ADU, (,), _)
65 #endif
66 };
67
modbus_rx_handler(struct k_work * item)68 static void modbus_rx_handler(struct k_work *item)
69 {
70 struct modbus_context *ctx;
71
72 ctx = CONTAINER_OF(item, struct modbus_context, server_work);
73
74 switch (ctx->mode) {
75 case MODBUS_MODE_RTU:
76 case MODBUS_MODE_ASCII:
77 if (IS_ENABLED(CONFIG_MODBUS_SERIAL)) {
78 modbus_serial_rx_disable(ctx);
79 ctx->rx_adu_err = modbus_serial_rx_adu(ctx);
80 }
81 break;
82 case MODBUS_MODE_RAW:
83 if (IS_ENABLED(CONFIG_MODBUS_RAW_ADU)) {
84 ctx->rx_adu_err = modbus_raw_rx_adu(ctx);
85 }
86 break;
87 default:
88 LOG_ERR("Unknown MODBUS mode");
89 return;
90 }
91
92 if (ctx->client == true) {
93 k_sem_give(&ctx->client_wait_sem);
94 } else if (IS_ENABLED(CONFIG_MODBUS_SERVER)) {
95 bool respond = modbus_server_handler(ctx);
96
97 if (respond) {
98 modbus_tx_adu(ctx);
99 } else {
100 LOG_DBG("Server has dropped frame");
101 }
102
103 switch (ctx->mode) {
104 case MODBUS_MODE_RTU:
105 case MODBUS_MODE_ASCII:
106 if (IS_ENABLED(CONFIG_MODBUS_SERIAL) &&
107 respond == false) {
108 modbus_serial_rx_enable(ctx);
109 }
110 break;
111 default:
112 break;
113 }
114 }
115 }
116
modbus_tx_adu(struct modbus_context * ctx)117 void modbus_tx_adu(struct modbus_context *ctx)
118 {
119 switch (ctx->mode) {
120 case MODBUS_MODE_RTU:
121 case MODBUS_MODE_ASCII:
122 if (IS_ENABLED(CONFIG_MODBUS_SERIAL) &&
123 modbus_serial_tx_adu(ctx)) {
124 LOG_ERR("Unsupported MODBUS serial mode");
125 }
126 break;
127 case MODBUS_MODE_RAW:
128 if (IS_ENABLED(CONFIG_MODBUS_RAW_ADU) &&
129 modbus_raw_tx_adu(ctx)) {
130 LOG_ERR("Unsupported MODBUS raw mode");
131 }
132 break;
133 default:
134 LOG_ERR("Unknown MODBUS mode");
135 }
136 }
137
modbus_tx_wait_rx_adu(struct modbus_context * ctx)138 int modbus_tx_wait_rx_adu(struct modbus_context *ctx)
139 {
140 modbus_tx_adu(ctx);
141
142 if (k_sem_take(&ctx->client_wait_sem, K_USEC(ctx->rxwait_to)) != 0) {
143 LOG_WRN("Client wait-for-RX timeout");
144 return -ETIMEDOUT;
145 }
146
147 return ctx->rx_adu_err;
148 }
149
modbus_get_context(const uint8_t iface)150 struct modbus_context *modbus_get_context(const uint8_t iface)
151 {
152 struct modbus_context *ctx;
153
154 if (iface >= ARRAY_SIZE(mb_ctx_tbl)) {
155 LOG_ERR("Interface %u not available", iface);
156 return NULL;
157 }
158
159 ctx = &mb_ctx_tbl[iface];
160
161 if (!atomic_test_bit(&ctx->state, MODBUS_STATE_CONFIGURED)) {
162 LOG_ERR("Interface not configured");
163 return NULL;
164 }
165
166 return ctx;
167 }
168
modbus_iface_get_by_ctx(const struct modbus_context * ctx)169 int modbus_iface_get_by_ctx(const struct modbus_context *ctx)
170 {
171 for (int i = 0; i < ARRAY_SIZE(mb_ctx_tbl); i++) {
172 if (&mb_ctx_tbl[i] == ctx) {
173 return i;
174 }
175 }
176
177 return -ENODEV;
178 }
179
modbus_iface_get_by_name(const char * iface_name)180 int modbus_iface_get_by_name(const char *iface_name)
181 {
182 for (int i = 0; i < ARRAY_SIZE(mb_ctx_tbl); i++) {
183 if (strcmp(iface_name, mb_ctx_tbl[i].iface_name) == 0) {
184 return i;
185 }
186 }
187
188 return -ENODEV;
189 }
190
modbus_init_iface(const uint8_t iface)191 static struct modbus_context *modbus_init_iface(const uint8_t iface)
192 {
193 struct modbus_context *ctx;
194
195 if (iface >= ARRAY_SIZE(mb_ctx_tbl)) {
196 LOG_ERR("Interface %u not available", iface);
197 return NULL;
198 }
199
200 ctx = &mb_ctx_tbl[iface];
201
202 if (atomic_test_and_set_bit(&ctx->state, MODBUS_STATE_CONFIGURED)) {
203 LOG_ERR("Interface already used");
204 return NULL;
205 }
206
207 k_mutex_init(&ctx->iface_lock);
208 k_sem_init(&ctx->client_wait_sem, 0, 1);
209 k_work_init(&ctx->server_work, modbus_rx_handler);
210
211 return ctx;
212 }
213
modbus_init_server(const int iface,struct modbus_iface_param param)214 int modbus_init_server(const int iface, struct modbus_iface_param param)
215 {
216 struct modbus_context *ctx = NULL;
217 int rc = 0;
218
219 if (!IS_ENABLED(CONFIG_MODBUS_SERVER)) {
220 LOG_ERR("Modbus server support is not enabled");
221 rc = -ENOTSUP;
222 goto init_server_error;
223 }
224
225 if (param.server.user_cb == NULL) {
226 LOG_ERR("User callbacks should be available");
227 rc = -EINVAL;
228 goto init_server_error;
229 }
230
231 ctx = modbus_init_iface(iface);
232 if (ctx == NULL) {
233 rc = -EINVAL;
234 goto init_server_error;
235 }
236
237 ctx->client = false;
238
239 switch (param.mode) {
240 case MODBUS_MODE_RTU:
241 case MODBUS_MODE_ASCII:
242 if (IS_ENABLED(CONFIG_MODBUS_SERIAL) &&
243 modbus_serial_init(ctx, param) != 0) {
244 LOG_ERR("Failed to init MODBUS over serial line");
245 rc = -EINVAL;
246 goto init_server_error;
247 }
248 break;
249 case MODBUS_MODE_RAW:
250 if (IS_ENABLED(CONFIG_MODBUS_RAW_ADU) &&
251 modbus_raw_init(ctx, param) != 0) {
252 LOG_ERR("Failed to init MODBUS raw ADU support");
253 rc = -EINVAL;
254 goto init_server_error;
255 }
256 break;
257 default:
258 LOG_ERR("Unknown MODBUS mode");
259 rc = -ENOTSUP;
260 goto init_server_error;
261 }
262
263 ctx->unit_id = param.server.unit_id;
264 ctx->mbs_user_cb = param.server.user_cb;
265 if (IS_ENABLED(CONFIG_MODBUS_FC08_DIAGNOSTIC)) {
266 modbus_reset_stats(ctx);
267 }
268
269 LOG_DBG("Modbus interface %s initialized", ctx->iface_name);
270
271 return 0;
272
273 init_server_error:
274 if (ctx != NULL) {
275 atomic_clear_bit(&ctx->state, MODBUS_STATE_CONFIGURED);
276 }
277
278 return rc;
279 }
280
modbus_init_client(const int iface,struct modbus_iface_param param)281 int modbus_init_client(const int iface, struct modbus_iface_param param)
282 {
283 struct modbus_context *ctx = NULL;
284 int rc = 0;
285
286 if (!IS_ENABLED(CONFIG_MODBUS_CLIENT)) {
287 LOG_ERR("Modbus client support is not enabled");
288 rc = -ENOTSUP;
289 goto init_client_error;
290 }
291
292 ctx = modbus_init_iface(iface);
293 if (ctx == NULL) {
294 rc = -EINVAL;
295 goto init_client_error;
296 }
297
298 ctx->client = true;
299
300 switch (param.mode) {
301 case MODBUS_MODE_RTU:
302 case MODBUS_MODE_ASCII:
303 if (IS_ENABLED(CONFIG_MODBUS_SERIAL) &&
304 modbus_serial_init(ctx, param) != 0) {
305 LOG_ERR("Failed to init MODBUS over serial line");
306 rc = -EINVAL;
307 goto init_client_error;
308 }
309 break;
310 case MODBUS_MODE_RAW:
311 if (IS_ENABLED(CONFIG_MODBUS_RAW_ADU) &&
312 modbus_raw_init(ctx, param) != 0) {
313 LOG_ERR("Failed to init MODBUS raw ADU support");
314 rc = -EINVAL;
315 goto init_client_error;
316 }
317 break;
318 default:
319 LOG_ERR("Unknown MODBUS mode");
320 rc = -ENOTSUP;
321 goto init_client_error;
322 }
323
324 ctx->unit_id = 0;
325 ctx->mbs_user_cb = NULL;
326 ctx->rxwait_to = param.rx_timeout;
327
328 return 0;
329
330 init_client_error:
331 if (ctx != NULL) {
332 atomic_clear_bit(&ctx->state, MODBUS_STATE_CONFIGURED);
333 }
334
335 return rc;
336 }
337
modbus_disable(const uint8_t iface)338 int modbus_disable(const uint8_t iface)
339 {
340 struct modbus_context *ctx;
341 struct k_work_sync work_sync;
342
343 ctx = modbus_get_context(iface);
344 if (ctx == NULL) {
345 LOG_ERR("Interface %u not initialized", iface);
346 return -EINVAL;
347 }
348
349 switch (ctx->mode) {
350 case MODBUS_MODE_RTU:
351 case MODBUS_MODE_ASCII:
352 if (IS_ENABLED(CONFIG_MODBUS_SERIAL)) {
353 modbus_serial_disable(ctx);
354 }
355 break;
356 case MODBUS_MODE_RAW:
357 break;
358 default:
359 LOG_ERR("Unknown MODBUS mode");
360 }
361
362 k_work_cancel_sync(&ctx->server_work, &work_sync);
363 ctx->rxwait_to = 0;
364 ctx->unit_id = 0;
365 ctx->mode = MODBUS_MODE_RTU;
366 ctx->mbs_user_cb = NULL;
367 atomic_clear_bit(&ctx->state, MODBUS_STATE_CONFIGURED);
368
369 LOG_INF("Modbus interface %u disabled", iface);
370
371 return 0;
372 }
373