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 #ifndef _MB_CONTROLLER_SLAVE_H 16 #define _MB_CONTROLLER_SLAVE_H 17 18 #include "driver/uart.h" // for uart defines 19 #include "errno.h" // for errno 20 #include "sys/queue.h" // for list 21 #include "esp_log.h" // for log write 22 #include "string.h" // for strerror() 23 24 #include "esp_modbus_slave.h" // for public type defines 25 #include "esp_modbus_callbacks.h" // for callback functions 26 27 /* ----------------------- Defines ------------------------------------------*/ 28 #define MB_INST_MIN_SIZE (2) // The minimal size of Modbus registers area in bytes 29 #define MB_INST_MAX_SIZE (65535 * 2) // The maximum size of Modbus area in bytes 30 31 #define MB_CONTROLLER_NOTIFY_QUEUE_SIZE (CONFIG_FMB_CONTROLLER_NOTIFY_QUEUE_SIZE) // Number of messages in parameter notification queue 32 #define MB_CONTROLLER_NOTIFY_TIMEOUT (pdMS_TO_TICKS(CONFIG_FMB_CONTROLLER_NOTIFY_TIMEOUT)) // notification timeout 33 34 /** 35 * @brief Device communication parameters for master 36 */ 37 typedef struct { 38 mb_mode_type_t mode; /*!< Modbus communication mode */ 39 uint8_t slave_addr; /*!< Slave address field */ 40 uart_port_t port; /*!< Modbus communication port (UART) number */ 41 uint32_t baudrate; /*!< Modbus baudrate */ 42 uart_parity_t parity; /*!< Modbus UART parity settings */ 43 } mb_slave_comm_info_t; 44 45 /** 46 * @brief Modbus area descriptor list item 47 */ 48 typedef struct mb_descr_entry_s{ 49 uint16_t start_offset; /*!< Modbus start address for area descriptor */ 50 mb_param_type_t type; /*!< Type of storage area descriptor */ 51 void* p_data; /*!< Instance address for storage area descriptor */ 52 size_t size; /*!< Instance size for area descriptor (bytes) */ 53 LIST_ENTRY(mb_descr_entry_s) entries; /*!< The Modbus area descriptor entry */ 54 } mb_descr_entry_t; 55 56 /** 57 * @brief Modbus controller handler structure 58 */ 59 typedef struct { 60 mb_port_type_t port_type; /*!< port type */ 61 mb_communication_info_t mbs_comm; /*!< communication info */ 62 TaskHandle_t mbs_task_handle; /*!< task handle */ 63 EventGroupHandle_t mbs_event_group; /*!< controller event group */ 64 QueueHandle_t mbs_notification_queue_handle; /*!< controller notification queue */ 65 LIST_HEAD(mbs_area_descriptors_, mb_descr_entry_s) mbs_area_descriptors[MB_PARAM_COUNT]; /*!< register area descriptors */ 66 } mb_slave_options_t; 67 68 typedef mb_event_group_t (*iface_check_event)(mb_event_group_t); /*!< Interface method check_event */ 69 typedef esp_err_t (*iface_get_param_info)(mb_param_info_t*, uint32_t); /*!< Interface method get_param_info */ 70 typedef esp_err_t (*iface_set_descriptor)(mb_register_area_descriptor_t); /*!< Interface method set_descriptor */ 71 72 /** 73 * @brief Request mode for parameter to use in data dictionary 74 */ 75 typedef struct 76 { 77 mb_slave_options_t opts; /*!< Modbus slave options */ 78 79 // Functional pointers to internal static functions of the implementation (public interface methods) 80 iface_init init; /*!< Interface method init */ 81 iface_destroy destroy; /*!< Interface method destroy */ 82 iface_setup setup; /*!< Interface method setup */ 83 iface_start start; /*!< Interface method start */ 84 iface_check_event check_event; /*!< Interface method check_event */ 85 iface_get_param_info get_param_info; /*!< Interface method get_param_info */ 86 iface_set_descriptor set_descriptor; /*!< Interface method set_descriptor */ 87 88 // Modbus register calback function pointers 89 reg_discrete_cb slave_reg_cb_discrete; /*!< Stack callback discrete rw method */ 90 reg_input_cb slave_reg_cb_input; /*!< Stack callback input rw method */ 91 reg_holding_cb slave_reg_cb_holding; /*!< Stack callback holding rw method */ 92 reg_coils_cb slave_reg_cb_coils; /*!< Stack callback coils rw method */ 93 } mb_slave_interface_t; 94 95 #endif 96