1 /* Copyright 2018 Espressif Systems (Shanghai) PTE LTD
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "esp_err.h" // for esp_err_t
17 #include "sdkconfig.h" // for KConfig defines
18
19 #include "mbc_slave.h" // for slave private type definitions
20 #include "mbutils.h" // for stack bit setting utilities
21 #include "esp_modbus_common.h" // for common defines
22 #include "esp_modbus_slave.h" // for public slave defines
23 #include "esp_modbus_callbacks.h" // for modbus callbacks function pointers declaration
24
25 #ifdef CONFIG_FMB_CONTROLLER_SLAVE_ID_SUPPORT
26
27 #define MB_ID_BYTE0(id) ((uint8_t)(id))
28 #define MB_ID_BYTE1(id) ((uint8_t)(((uint16_t)(id) >> 8) & 0xFF))
29 #define MB_ID_BYTE2(id) ((uint8_t)(((uint32_t)(id) >> 16) & 0xFF))
30 #define MB_ID_BYTE3(id) ((uint8_t)(((uint32_t)(id) >> 24) & 0xFF))
31
32 #define MB_CONTROLLER_SLAVE_ID (CONFIG_FMB_CONTROLLER_SLAVE_ID)
33 #define MB_SLAVE_ID_SHORT (MB_ID_BYTE3(MB_CONTROLLER_SLAVE_ID))
34
35 // Slave ID constant
36 static uint8_t mb_slave_id[] = { MB_ID_BYTE0(MB_CONTROLLER_SLAVE_ID),
37 MB_ID_BYTE1(MB_CONTROLLER_SLAVE_ID),
38 MB_ID_BYTE2(MB_CONTROLLER_SLAVE_ID) };
39
40 #endif
41
42 #define REG_SIZE(type, nregs) ((type == MB_PARAM_INPUT) || (type == MB_PARAM_HOLDING)) ? (nregs >> 1) : (nregs << 3)
43
44 // Common interface pointer for slave port
45 static mb_slave_interface_t* slave_interface_ptr = NULL;
46 static const char TAG[] __attribute__((unused)) = "MB_CONTROLLER_SLAVE";
47
48 // Searches the register in the area specified by type, returns descriptor if found, else NULL
mbc_slave_find_reg_descriptor(mb_param_type_t type,uint16_t addr,size_t regs)49 static mb_descr_entry_t* mbc_slave_find_reg_descriptor(mb_param_type_t type, uint16_t addr, size_t regs)
50 {
51 mb_descr_entry_t* it;
52 uint16_t reg_size = 0;
53
54 mb_slave_options_t* mbs_opts = &slave_interface_ptr->opts;
55
56 if (LIST_EMPTY(&mbs_opts->mbs_area_descriptors[type])) {
57 return NULL;
58 }
59
60 // search for the register in each area
61 for (it = LIST_FIRST(&mbs_opts->mbs_area_descriptors[type]); it != NULL; it = LIST_NEXT(it, entries)) {
62 reg_size = REG_SIZE(type, it->size);
63 if ((addr >= it->start_offset)
64 && (it->p_data)
65 && (regs >= 1)
66 && ((addr + regs) <= (it->start_offset + reg_size))
67 && (reg_size >= 1)) {
68 return it;
69 }
70 }
71 return NULL;
72 }
73
mbc_slave_free_descriptors(void)74 static void mbc_slave_free_descriptors(void) {
75
76 mb_descr_entry_t* it;
77 mb_slave_options_t* mbs_opts = &slave_interface_ptr->opts;
78
79 for (int descr_type = 0; descr_type < MB_PARAM_COUNT; descr_type++) {
80 for (it = LIST_FIRST(&mbs_opts->mbs_area_descriptors[descr_type]); it != NULL; it = LIST_NEXT(it, entries)) {
81 LIST_REMOVE(it, entries);
82 free(it);
83 }
84 }
85 }
86
mbc_slave_init_iface(void * handler)87 void mbc_slave_init_iface(void* handler)
88 {
89 slave_interface_ptr = (mb_slave_interface_t*) handler;
90 mb_slave_options_t* mbs_opts = &slave_interface_ptr->opts;
91 // Initialize list head for register areas
92 LIST_INIT(&mbs_opts->mbs_area_descriptors[MB_PARAM_INPUT]);
93 LIST_INIT(&mbs_opts->mbs_area_descriptors[MB_PARAM_HOLDING]);
94 LIST_INIT(&mbs_opts->mbs_area_descriptors[MB_PARAM_COIL]);
95 LIST_INIT(&mbs_opts->mbs_area_descriptors[MB_PARAM_DISCRETE]);
96 }
97
98 /**
99 * Modbus controller destroy function
100 */
mbc_slave_destroy(void)101 esp_err_t mbc_slave_destroy(void)
102 {
103 esp_err_t error = ESP_OK;
104 // Is initialization done?
105 MB_SLAVE_CHECK((slave_interface_ptr != NULL),
106 ESP_ERR_INVALID_STATE,
107 "Slave interface is not correctly initialized.");
108 // Check if interface has been initialized
109 MB_SLAVE_CHECK((slave_interface_ptr->destroy != NULL),
110 ESP_ERR_INVALID_STATE,
111 "Slave interface is not correctly initialized.");
112 // Call the slave port destroy function
113 error = slave_interface_ptr->destroy();
114 MB_SLAVE_CHECK((error == ESP_OK),
115 ESP_ERR_INVALID_STATE,
116 "Slave destroy failure error=(0x%x).",
117 error);
118 // Destroy all opened descriptors
119 mbc_slave_free_descriptors();
120 free(slave_interface_ptr);
121 slave_interface_ptr = NULL;
122 return error;
123 }
124
125 /**
126 * Setup Modbus controller parameters
127 */
mbc_slave_setup(void * comm_info)128 esp_err_t mbc_slave_setup(void* comm_info)
129 {
130 esp_err_t error = ESP_OK;
131 MB_SLAVE_CHECK((slave_interface_ptr != NULL),
132 ESP_ERR_INVALID_STATE,
133 "Slave interface is not correctly initialized.");
134 MB_SLAVE_CHECK((slave_interface_ptr->setup != NULL),
135 ESP_ERR_INVALID_STATE,
136 "Slave interface is not correctly initialized.");
137 error = slave_interface_ptr->setup(comm_info);
138 MB_SLAVE_CHECK((error == ESP_OK),
139 ESP_ERR_INVALID_STATE,
140 "Slave setup failure error=(0x%x).",
141 error);
142 return error;
143 }
144
145 /**
146 * Start Modbus controller start function
147 */
mbc_slave_start(void)148 esp_err_t mbc_slave_start(void)
149 {
150 esp_err_t error = ESP_OK;
151 MB_SLAVE_CHECK((slave_interface_ptr != NULL),
152 ESP_ERR_INVALID_STATE,
153 "Slave interface is not correctly initialized.");
154 MB_SLAVE_CHECK((slave_interface_ptr->start != NULL),
155 ESP_ERR_INVALID_STATE,
156 "Slave interface is not correctly initialized.");
157 #ifdef CONFIG_FMB_CONTROLLER_SLAVE_ID_SUPPORT
158 // Set the slave ID if the KConfig option is selected
159 eMBErrorCode status = eMBSetSlaveID(MB_SLAVE_ID_SHORT, TRUE, (UCHAR*)mb_slave_id, sizeof(mb_slave_id));
160 MB_SLAVE_CHECK((status == MB_ENOERR), ESP_ERR_INVALID_STATE, "mb stack set slave ID failure.");
161 #endif
162 error = slave_interface_ptr->start();
163 MB_SLAVE_CHECK((error == ESP_OK),
164 ESP_ERR_INVALID_STATE,
165 "Slave start failure error=(0x%x).",
166 error);
167 return error;
168 }
169
170 /**
171 * Blocking function to get event on parameter group change for application task
172 */
mbc_slave_check_event(mb_event_group_t group)173 mb_event_group_t mbc_slave_check_event(mb_event_group_t group)
174 {
175 MB_SLAVE_CHECK((slave_interface_ptr != NULL),
176 MB_EVENT_NO_EVENTS,
177 "Slave interface is not correctly initialized.");
178 MB_SLAVE_CHECK((slave_interface_ptr->check_event != NULL),
179 MB_EVENT_NO_EVENTS,
180 "Slave interface is not correctly initialized.");
181 mb_event_group_t event = slave_interface_ptr->check_event(group);
182 return event;
183 }
184
185 /**
186 * Function to get notification about parameter change from application task
187 */
mbc_slave_get_param_info(mb_param_info_t * reg_info,uint32_t timeout)188 esp_err_t mbc_slave_get_param_info(mb_param_info_t* reg_info, uint32_t timeout)
189 {
190 esp_err_t error = ESP_OK;
191 MB_SLAVE_CHECK((slave_interface_ptr != NULL),
192 ESP_ERR_INVALID_STATE,
193 "Slave interface is not correctly initialized.");
194 MB_SLAVE_CHECK((slave_interface_ptr->get_param_info != NULL),
195 ESP_ERR_INVALID_STATE,
196 "Slave interface is not correctly initialized.");
197 error = slave_interface_ptr->get_param_info(reg_info, timeout);
198 MB_SLAVE_CHECK((error == ESP_OK),
199 ESP_ERR_INVALID_STATE,
200 "Slave get parameter info failure error=(0x%x).",
201 error);
202 return error;
203 }
204
205 /**
206 * Function to set area descriptors for modbus parameters
207 */
mbc_slave_set_descriptor(mb_register_area_descriptor_t descr_data)208 esp_err_t mbc_slave_set_descriptor(mb_register_area_descriptor_t descr_data)
209 {
210 esp_err_t error = ESP_OK;
211 MB_SLAVE_CHECK((slave_interface_ptr != NULL),
212 ESP_ERR_INVALID_STATE,
213 "Slave interface is not correctly initialized.");
214
215 if (slave_interface_ptr->set_descriptor != NULL) {
216 error = slave_interface_ptr->set_descriptor(descr_data);
217 MB_SLAVE_CHECK((error == ESP_OK),
218 ESP_ERR_INVALID_STATE,
219 "Slave set descriptor failure error=(0x%x).",
220 (uint16_t)error);
221 } else {
222 mb_slave_options_t* mbs_opts = &slave_interface_ptr->opts;
223 // Check if the address is already in the descriptor list
224 mb_descr_entry_t* it = mbc_slave_find_reg_descriptor(descr_data.type, descr_data.start_offset, 1);
225 MB_SLAVE_CHECK((it == NULL), ESP_ERR_INVALID_ARG, "mb incorrect descriptor or already defined.");
226
227 mb_descr_entry_t* new_descr = (mb_descr_entry_t*) heap_caps_malloc(sizeof(mb_descr_entry_t),
228 MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
229 MB_SLAVE_CHECK((new_descr != NULL), ESP_ERR_NO_MEM, "mb can not allocate memory for descriptor.");
230 new_descr->start_offset = descr_data.start_offset;
231 new_descr->type = descr_data.type;
232 new_descr->p_data = descr_data.address;
233 new_descr->size = descr_data.size;
234 LIST_INSERT_HEAD(&mbs_opts->mbs_area_descriptors[descr_data.type], new_descr, entries);
235 error = ESP_OK;
236 }
237 return error;
238 }
239
240 // The helper function to get time stamp in microseconds
mbc_slave_get_time_stamp(void)241 static uint64_t mbc_slave_get_time_stamp(void)
242 {
243 uint64_t time_stamp = esp_timer_get_time();
244 return time_stamp;
245 }
246
247 // Helper function to send parameter information to application task
mbc_slave_send_param_info(mb_event_group_t par_type,uint16_t mb_offset,uint8_t * par_address,uint16_t par_size)248 static esp_err_t mbc_slave_send_param_info(mb_event_group_t par_type, uint16_t mb_offset,
249 uint8_t* par_address, uint16_t par_size)
250 {
251 MB_SLAVE_ASSERT(slave_interface_ptr != NULL);
252 mb_slave_options_t* mbs_opts = &slave_interface_ptr->opts;
253 esp_err_t error = ESP_FAIL;
254 mb_param_info_t par_info;
255 // Check if queue is not full the send parameter information
256 par_info.type = par_type;
257 par_info.size = par_size;
258 par_info.address = par_address;
259 par_info.time_stamp = mbc_slave_get_time_stamp();
260 par_info.mb_offset = mb_offset;
261 BaseType_t status = xQueueSend(mbs_opts->mbs_notification_queue_handle, &par_info, MB_PAR_INFO_TOUT);
262 if (pdTRUE == status) {
263 ESP_LOGD(TAG, "Queue send parameter info (type, address, size): %d, 0x%.4x, %d",
264 par_type, (uint32_t)par_address, par_size);
265 error = ESP_OK;
266 } else if (errQUEUE_FULL == status) {
267 ESP_LOGD(TAG, "Parameter queue is overflowed.");
268 }
269 return error;
270 }
271
272 // Helper function to send notification
mbc_slave_send_param_access_notification(mb_event_group_t event)273 static esp_err_t mbc_slave_send_param_access_notification(mb_event_group_t event)
274 {
275 MB_SLAVE_ASSERT(slave_interface_ptr != NULL);
276 mb_slave_options_t* mbs_opts = &slave_interface_ptr->opts;
277 esp_err_t err = ESP_FAIL;
278 mb_event_group_t bits = (mb_event_group_t)xEventGroupSetBits(mbs_opts->mbs_event_group, (EventBits_t)event);
279 if (bits & event) {
280 ESP_LOGD(TAG, "The MB_REG_CHANGE_EVENT = 0x%.2x is set.", (uint8_t)event);
281 err = ESP_OK;
282 }
283 return err;
284 }
285
286 /*
287 * Below are the common slave read/write register callback functions
288 * The concrete slave port can override them using interface function pointers
289 */
290
291 // Callback function for reading of MB Input Registers
mbc_reg_input_slave_cb(UCHAR * reg_buffer,USHORT address,USHORT n_regs)292 eMBErrorCode mbc_reg_input_slave_cb(UCHAR * reg_buffer, USHORT address, USHORT n_regs)
293 {
294 MB_SLAVE_CHECK((slave_interface_ptr != NULL),
295 MB_EILLSTATE, "Slave stack uninitialized.");
296 MB_SLAVE_CHECK((reg_buffer != NULL),
297 MB_EINVAL, "Slave stack call failed.");
298 eMBErrorCode status = MB_ENOERR;
299 address--; // address of register is already +1
300 mb_descr_entry_t* it = mbc_slave_find_reg_descriptor(MB_PARAM_INPUT, address, n_regs);
301 if (it != NULL) {
302 uint16_t input_reg_start = (uint16_t)it->start_offset; // Get Modbus start address
303 uint8_t* input_buffer = (uint8_t*)it->p_data; // Get instance address
304 uint16_t regs = n_regs;
305 uint16_t reg_index;
306 // If input or configuration parameters are incorrect then return an error to stack layer
307 reg_index = (uint16_t)(address - input_reg_start);
308 reg_index <<= 1; // register Address to byte address
309 input_buffer += reg_index;
310 uint8_t* buffer_start = input_buffer;
311 while (regs > 0) {
312 _XFER_2_RD(reg_buffer, input_buffer);
313 reg_index += 2;
314 regs -= 1;
315 }
316 // Send access notification
317 (void)mbc_slave_send_param_access_notification(MB_EVENT_INPUT_REG_RD);
318 // Send parameter info to application task
319 (void)mbc_slave_send_param_info(MB_EVENT_INPUT_REG_RD, (uint16_t)address,
320 (uint8_t*)buffer_start, (uint16_t)n_regs);
321 } else {
322 status = MB_ENOREG;
323 }
324 return status;
325 }
326
327 // Callback function for reading of MB Holding Registers
328 // Executed by stack when request to read/write holding registers is received
mbc_reg_holding_slave_cb(UCHAR * reg_buffer,USHORT address,USHORT n_regs,eMBRegisterMode mode)329 eMBErrorCode mbc_reg_holding_slave_cb(UCHAR * reg_buffer, USHORT address, USHORT n_regs, eMBRegisterMode mode)
330 {
331 MB_SLAVE_CHECK((slave_interface_ptr != NULL),
332 MB_EILLSTATE, "Slave stack uninitialized.");
333 MB_SLAVE_CHECK((reg_buffer != NULL),
334 MB_EINVAL, "Slave stack call failed.");
335 eMBErrorCode status = MB_ENOERR;
336 uint16_t reg_index;
337 address--; // address of register is already +1
338 mb_descr_entry_t* it = mbc_slave_find_reg_descriptor(MB_PARAM_HOLDING, address, n_regs);
339 if (it != NULL) {
340 uint16_t reg_holding_start = (uint16_t)it->start_offset; // Get Modbus start address
341 uint8_t* holding_buffer = (uint8_t*)it->p_data; // Get instance address
342 uint16_t regs = n_regs;
343 reg_index = (uint16_t) (address - reg_holding_start);
344 reg_index <<= 1; // register Address to byte address
345 holding_buffer += reg_index;
346 uint8_t* buffer_start = holding_buffer;
347 switch (mode) {
348 case MB_REG_READ:
349 while (regs > 0) {
350 _XFER_2_RD(reg_buffer, holding_buffer);
351 reg_index += 2;
352 regs -= 1;
353 };
354 // Send access notification
355 (void)mbc_slave_send_param_access_notification(MB_EVENT_HOLDING_REG_RD);
356 // Send parameter info
357 (void)mbc_slave_send_param_info(MB_EVENT_HOLDING_REG_RD, (uint16_t)address,
358 (uint8_t*)buffer_start, (uint16_t)n_regs);
359 break;
360 case MB_REG_WRITE:
361 while (regs > 0) {
362 _XFER_2_WR(holding_buffer, reg_buffer);
363 holding_buffer += 2;
364 reg_index += 2;
365 regs -= 1;
366 };
367 // Send access notification
368 (void)mbc_slave_send_param_access_notification(MB_EVENT_HOLDING_REG_WR);
369 // Send parameter info
370 (void)mbc_slave_send_param_info(MB_EVENT_HOLDING_REG_WR, (uint16_t)address,
371 (uint8_t*)buffer_start, (uint16_t)n_regs);
372 break;
373 }
374 } else {
375 status = MB_ENOREG;
376 }
377 return status;
378 }
379
380 // Callback function for reading of MB Coils Registers
mbc_reg_coils_slave_cb(UCHAR * reg_buffer,USHORT address,USHORT n_coils,eMBRegisterMode mode)381 eMBErrorCode mbc_reg_coils_slave_cb(UCHAR* reg_buffer, USHORT address, USHORT n_coils, eMBRegisterMode mode)
382 {
383 MB_SLAVE_CHECK((slave_interface_ptr != NULL),
384 MB_EILLSTATE, "Slave stack uninitialized.");
385 MB_SLAVE_CHECK((reg_buffer != NULL),
386 MB_EINVAL, "Slave stack call failed.");
387 eMBErrorCode status = MB_ENOERR;
388 uint16_t reg_index;
389 uint16_t coils = n_coils;
390 address--; // The address is already +1
391 mb_descr_entry_t* it = mbc_slave_find_reg_descriptor(MB_PARAM_COIL, address, n_coils);
392 if (it != NULL) {
393 uint16_t reg_coils_start = (uint16_t)it->start_offset; // MB offset of coils
394 uint8_t* reg_coils_buf = (uint8_t*)it->p_data;
395 reg_index = (uint16_t) (address - it->start_offset);
396 CHAR* coils_data_buf = (CHAR*)(reg_coils_buf + (reg_index >> 3));
397 switch (mode) {
398 case MB_REG_READ:
399 while (coils > 0) {
400 uint8_t result = xMBUtilGetBits((uint8_t*)reg_coils_buf, reg_index, 1);
401 xMBUtilSetBits(reg_buffer, reg_index - (address - reg_coils_start), 1, result);
402 reg_index++;
403 coils--;
404 }
405 // Send an event to notify application task about event
406 (void)mbc_slave_send_param_access_notification(MB_EVENT_COILS_RD);
407 (void)mbc_slave_send_param_info(MB_EVENT_COILS_RD, (uint16_t)address,
408 (uint8_t*)(coils_data_buf), (uint16_t)n_coils);
409 break;
410 case MB_REG_WRITE:
411 while (coils > 0) {
412 uint8_t result = xMBUtilGetBits(reg_buffer,
413 reg_index - (address - reg_coils_start), 1);
414 xMBUtilSetBits((uint8_t*)reg_coils_buf, reg_index, 1, result);
415 reg_index++;
416 coils--;
417 }
418 // Send an event to notify application task about event
419 (void)mbc_slave_send_param_access_notification(MB_EVENT_COILS_WR);
420 (void)mbc_slave_send_param_info(MB_EVENT_COILS_WR, (uint16_t)address,
421 (uint8_t*)coils_data_buf, (uint16_t)n_coils);
422 break;
423 } // switch ( eMode )
424 } else {
425 // If the configuration or input parameters are incorrect then return error to stack
426 status = MB_ENOREG;
427 }
428 return status;
429 }
430
431 // Callback function for reading of MB Discrete Input Registers
mbc_reg_discrete_slave_cb(UCHAR * reg_buffer,USHORT address,USHORT n_discrete)432 eMBErrorCode mbc_reg_discrete_slave_cb(UCHAR* reg_buffer, USHORT address, USHORT n_discrete)
433 {
434 MB_SLAVE_CHECK((slave_interface_ptr != NULL),
435 MB_EILLSTATE, "Slave stack uninitialized.");
436 MB_SLAVE_CHECK((reg_buffer != NULL),
437 MB_EINVAL, "Slave stack call failed.");
438
439 eMBErrorCode status = MB_ENOERR;
440 uint16_t reg_index;
441 uint16_t reg_bit_index;
442 uint16_t n_reg;
443 uint8_t* discrete_input_buf;
444 // It already plus one in modbus function method.
445 address--;
446 mb_descr_entry_t* it = mbc_slave_find_reg_descriptor(MB_PARAM_DISCRETE, address, n_discrete);
447 if (it != NULL) {
448 uint16_t reg_discrete_start = (uint16_t)it->start_offset; // MB offset of registers
449 n_reg = (n_discrete >> 3) + 1;
450 discrete_input_buf = (uint8_t*)it->p_data; // the storage address
451 reg_index = (uint16_t) (address - reg_discrete_start) / 8; // Get register index in the buffer for bit number
452 reg_bit_index = (uint16_t)(address - reg_discrete_start) % 8; // Get bit index
453 uint8_t* temp_buf = &discrete_input_buf[reg_index];
454 while (n_reg > 0) {
455 *reg_buffer++ = xMBUtilGetBits(&discrete_input_buf[reg_index++], reg_bit_index, 8);
456 n_reg--;
457 }
458 reg_buffer--;
459 // Last discrete
460 n_discrete = n_discrete % 8;
461 // Filling zero to high bit
462 *reg_buffer = *reg_buffer << (8 - n_discrete);
463 *reg_buffer = *reg_buffer >> (8 - n_discrete);
464 // Send an event to notify application task about event
465 (void)mbc_slave_send_param_access_notification(MB_EVENT_DISCRETE_RD);
466 (void)mbc_slave_send_param_info(MB_EVENT_DISCRETE_RD, (uint16_t)address,
467 (uint8_t*)temp_buf, (uint16_t)n_discrete);
468 } else {
469 status = MB_ENOREG;
470 }
471 return status;
472 }
473
474 /**
475 * Below are the stack callback functions to read/write registers
476 */
eMBRegDiscreteCB(UCHAR * pucRegBuffer,USHORT usAddress,USHORT usNDiscrete)477 eMBErrorCode eMBRegDiscreteCB(UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete)
478 {
479 eMBErrorCode error = MB_ENOERR;
480 MB_SLAVE_CHECK((slave_interface_ptr != NULL),
481 ESP_ERR_INVALID_STATE,
482 "Slave interface is not correctly initialized.");
483 // Check if the callback is overridden in concrete port
484 if (slave_interface_ptr->slave_reg_cb_discrete) {
485 error = slave_interface_ptr->slave_reg_cb_discrete(pucRegBuffer, usAddress, usNDiscrete);
486 } else {
487 error = mbc_reg_discrete_slave_cb(pucRegBuffer, usAddress, usNDiscrete);
488 }
489
490 return error;
491 }
492
eMBRegCoilsCB(UCHAR * pucRegBuffer,USHORT usAddress,USHORT usNCoils,eMBRegisterMode eMode)493 eMBErrorCode eMBRegCoilsCB(UCHAR* pucRegBuffer, USHORT usAddress,
494 USHORT usNCoils, eMBRegisterMode eMode)
495 {
496 eMBErrorCode error = MB_ENOERR;
497 MB_SLAVE_CHECK((slave_interface_ptr != NULL),
498 ESP_ERR_INVALID_STATE,
499 "Slave interface is not correctly initialized.");
500
501 if (slave_interface_ptr->slave_reg_cb_coils) {
502 error = slave_interface_ptr->slave_reg_cb_coils(pucRegBuffer, usAddress, usNCoils, eMode);
503 } else {
504 error = mbc_reg_coils_slave_cb(pucRegBuffer, usAddress, usNCoils, eMode);
505 }
506 return error;
507 }
508
eMBRegHoldingCB(UCHAR * pucRegBuffer,USHORT usAddress,USHORT usNRegs,eMBRegisterMode eMode)509 eMBErrorCode eMBRegHoldingCB(UCHAR * pucRegBuffer, USHORT usAddress,
510 USHORT usNRegs, eMBRegisterMode eMode)
511 {
512 eMBErrorCode error = MB_ENOERR;
513 MB_SLAVE_CHECK((slave_interface_ptr != NULL),
514 ESP_ERR_INVALID_STATE,
515 "Slave interface is not correctly initialized.");
516
517 if (slave_interface_ptr->slave_reg_cb_holding) {
518 error = slave_interface_ptr->slave_reg_cb_holding(pucRegBuffer, usAddress, usNRegs, eMode);
519 } else {
520 error = mbc_reg_holding_slave_cb(pucRegBuffer, usAddress, usNRegs, eMode);
521 }
522 return error;
523 }
524
eMBRegInputCB(UCHAR * pucRegBuffer,USHORT usAddress,USHORT usNRegs)525 eMBErrorCode eMBRegInputCB(UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs)
526 {
527 eMBErrorCode error = ESP_ERR_INVALID_STATE;
528 MB_SLAVE_CHECK((slave_interface_ptr != NULL),
529 ESP_ERR_INVALID_STATE,
530 "Slave interface is not correctly initialized.");
531
532 if (slave_interface_ptr->slave_reg_cb_input) {
533 error = slave_interface_ptr->slave_reg_cb_input(pucRegBuffer, usAddress, usNRegs);
534 } else {
535 error = mbc_reg_input_slave_cb(pucRegBuffer, usAddress, usNRegs);
536 }
537 return error;
538 }
539