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 #ifndef _ESP_MB_SLAVE_INTERFACE_H 17 #define _ESP_MB_SLAVE_INTERFACE_H 18 19 // Public interface header for slave 20 #include <stdint.h> // for standard int types definition 21 #include <stddef.h> // for NULL and std defines 22 #include "soc/soc.h" // for BITN definitions 23 #include "freertos/FreeRTOS.h" // for task creation and queues access 24 #include "freertos/event_groups.h" // for event groups 25 #include "esp_modbus_common.h" // for common types 26 27 #ifdef __cplusplus 28 extern "C" { 29 #endif 30 31 #define MB_SLAVE_CHECK(a, err_code, format, ...) MB_RETURN_ON_FALSE(a, err_code, TAG, format __VA_OPT__(,) __VA_ARGS__) 32 33 #define MB_SLAVE_ASSERT(con) do { \ 34 if (!(con)) { ESP_LOGE(TAG, "assert errno:%d, errno_str: !(%s)", errno, strerror(errno)); assert(0 && #con); } \ 35 } while (0) 36 37 /** 38 * @brief Parameter access event information type 39 */ 40 typedef struct { 41 uint32_t time_stamp; /*!< Timestamp of Modbus Event (uS)*/ 42 uint16_t mb_offset; /*!< Modbus register offset */ 43 mb_event_group_t type; /*!< Modbus event type */ 44 uint8_t* address; /*!< Modbus data storage address */ 45 size_t size; /*!< Modbus event register size (number of registers)*/ 46 } mb_param_info_t; 47 48 /** 49 * @brief Parameter storage area descriptor 50 */ 51 typedef struct { 52 uint16_t start_offset; /*!< Modbus start address for area descriptor */ 53 mb_param_type_t type; /*!< Type of storage area descriptor */ 54 void* address; /*!< Instance address for storage area descriptor */ 55 size_t size; /*!< Instance size for area descriptor (bytes) */ 56 } mb_register_area_descriptor_t; 57 58 /** 59 * @brief Initialize Modbus Slave controller and stack for TCP port 60 * 61 * @param[out] handler handler(pointer) to master data structure 62 * @return 63 * - ESP_OK Success 64 * - ESP_ERR_NO_MEM Parameter error 65 * - ESP_ERR_NOT_SUPPORTED Port type not supported 66 * - ESP_ERR_INVALID_STATE Initialization failure 67 */ 68 esp_err_t mbc_slave_init_tcp(void** handler); 69 70 /** 71 * @brief Initialize Modbus Slave controller and stack for Serial port 72 * 73 * @param[out] handler handler(pointer) to master data structure 74 * @param[in] port_type the type of port 75 * @return 76 * - ESP_OK Success 77 * - ESP_ERR_NO_MEM Parameter error 78 * - ESP_ERR_NOT_SUPPORTED Port type not supported 79 * - ESP_ERR_INVALID_STATE Initialization failure 80 */ 81 esp_err_t mbc_slave_init(mb_port_type_t port_type, void** handler); 82 83 /** 84 * @brief Initialize Modbus Slave controller interface handle 85 * 86 * @param[in] handler - pointer to slave interface data structure 87 */ 88 void mbc_slave_init_iface(void* handler); 89 90 /** 91 * @brief Destroy Modbus controller and stack 92 * 93 * @return 94 * - ESP_OK Success 95 * - ESP_ERR_INVALID_STATE Parameter error 96 */ 97 esp_err_t mbc_slave_destroy(void); 98 99 /** 100 * @brief Start Modbus communication stack 101 * 102 * @return 103 * - ESP_OK Success 104 * - ESP_ERR_INVALID_ARG Modbus stack start error 105 */ 106 esp_err_t mbc_slave_start(void); 107 108 /** 109 * @brief Set Modbus communication parameters for the controller 110 * 111 * @param comm_info Communication parameters structure. 112 * 113 * @return 114 * - ESP_OK Success 115 * - ESP_ERR_INVALID_ARG Incorrect parameter data 116 */ 117 esp_err_t mbc_slave_setup(void* comm_info); 118 119 /** 120 * @brief Wait for specific event on parameter change. 121 * 122 * @param group Group event bit mask to wait for change 123 * 124 * @return 125 * - mb_event_group_t event bits triggered 126 */ 127 mb_event_group_t mbc_slave_check_event(mb_event_group_t group); 128 129 /** 130 * @brief Get parameter information 131 * 132 * @param[out] reg_info parameter info structure 133 * @param timeout Timeout in milliseconds to read information from 134 * parameter queue 135 * @return 136 * - ESP_OK Success 137 * - ESP_ERR_TIMEOUT Can not get data from parameter queue 138 * or queue overflow 139 */ 140 esp_err_t mbc_slave_get_param_info(mb_param_info_t* reg_info, uint32_t timeout); 141 142 /** 143 * @brief Set Modbus area descriptor 144 * 145 * @param descr_data Modbus registers area descriptor structure 146 * 147 * @return 148 * - ESP_OK: The appropriate descriptor is set 149 * - ESP_ERR_INVALID_ARG: The argument is incorrect 150 */ 151 esp_err_t mbc_slave_set_descriptor(mb_register_area_descriptor_t descr_data); 152 153 #ifdef __cplusplus 154 } 155 #endif 156 157 #endif 158