sense_modbus.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * ESPRESSIF MIT License
  3. *
  4. * Copyright (c) 2018 <ESPRESSIF SYSTEMS (SHANGHAI) PTE LTD>
  5. *
  6. * Permission is hereby granted for use on all ESPRESSIF SYSTEMS products, in which case,
  7. * it is free of charge, to any person obtaining a copy of this software and associated
  8. * documentation files (the "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the Software is furnished
  11. * to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in all copies or
  14. * substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  18. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  19. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  20. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. *
  23. */
  24. // This module allows to hide all Modbus dependencies and work with characteristics
  25. // instead of Modbus parameters
  26. #include "stdlib.h"
  27. #include "string.h" // for memset, memcpy functions
  28. #include "esp_err.h"
  29. #include "esp_log.h"
  30. #include "sense_modbus.h" // for Modbus defines
  31. // This module provide an easy way to work with characteristics instead of
  32. // Modbus parameters
  33. static const char* TAG = "SENSE_MB";
  34. #define SENSE_MB_CHECK(a, ret_val, str, ...) \
  35. if (!(a)) { \
  36. ESP_LOGE(TAG, "%s(%u): " str, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
  37. return (ret_val); \
  38. }
  39. // Define port options for the master application
  40. #define MB_BAUDRATE 115200
  41. #define MB_PORTNUM 2
  42. #define MB_PARITY UART_PARITY_DISABLE
  43. // Keep the pointer to active characteristic table and its size
  44. static characteristic_descriptor_t** active_cid_table = NULL;
  45. static uint32_t active_cid_table_size = 0;
  46. // The function to get pointer to parameter storage (instance) according to parameter description table
  47. static void* sense_modbus_get_param_data(const mb_parameter_descriptor_t* param_descriptor)
  48. {
  49. assert(param_descriptor != NULL);
  50. void* instance_ptr = NULL;
  51. if (param_descriptor->param_offset != 0) {
  52. switch(param_descriptor->mb_param_type)
  53. {
  54. case MB_PARAM_HOLDING:
  55. instance_ptr = (void*)(&holding_reg_params + param_descriptor->param_offset - 1);
  56. break;
  57. case MB_PARAM_INPUT:
  58. instance_ptr = (void*)(&input_reg_params + param_descriptor->param_offset - 1);
  59. break;
  60. case MB_PARAM_COIL:
  61. instance_ptr = (void*)(&coil_reg_params + param_descriptor->param_offset - 1);
  62. break;
  63. case MB_PARAM_DISCRETE:
  64. instance_ptr = (void*)(&discrete_reg_params + param_descriptor->param_offset - 1);
  65. break;
  66. default:
  67. instance_ptr = NULL;
  68. break;
  69. }
  70. } else {
  71. instance_ptr = malloc((size_t)(param_descriptor->param_size));;
  72. }
  73. return instance_ptr;
  74. }
  75. // The helper function to get time stamp in microseconds
  76. static uint64_t sense_modbus_get_time_stamp_us()
  77. {
  78. uint64_t time_stamp = esp_timer_get_time();
  79. return time_stamp;
  80. }
  81. // Initialization of Modbus stack
  82. esp_err_t sense_modbus_init()
  83. {
  84. mb_communication_info_t comm = {
  85. .port = MB_PORTNUM,
  86. .mode = MB_MODE_RTU,
  87. .baudrate = MB_BAUDRATE,
  88. .parity = MB_PARITY
  89. };
  90. void* master_handler = NULL;
  91. esp_err_t err = mbc_master_init(MB_PORT_SERIAL_MASTER, &master_handler);
  92. SENSE_MB_CHECK((master_handler != NULL), ESP_ERR_INVALID_STATE,
  93. "mb controller initialization fail.");
  94. SENSE_MB_CHECK((err == ESP_OK), ESP_ERR_INVALID_STATE,
  95. "mb controller initialization fail, returns(0x%x).",
  96. (uint32_t)err);
  97. err = mbc_master_setup((void*)&comm);
  98. SENSE_MB_CHECK((err == ESP_OK), ESP_ERR_INVALID_STATE,
  99. "mb controller setup fail, returns(0x%x).",
  100. (uint32_t)err);
  101. err = mbc_master_start();
  102. SENSE_MB_CHECK((err == ESP_OK), ESP_ERR_INVALID_STATE,
  103. "mb controller start fail, returns(0x%x).",
  104. (uint32_t)err);
  105. // Set UART pin numbers
  106. err = uart_set_pin(MB_PORTNUM, CONFIG_MB_UART_TXD, CONFIG_MB_UART_RXD,
  107. CONFIG_MB_UART_RTS, UART_PIN_NO_CHANGE);
  108. SENSE_MB_CHECK((err == ESP_OK), ESP_ERR_INVALID_STATE,
  109. "mb serial set pin failure, uart_set_pin() returned (0x%x).", (uint32_t)err);
  110. // Set driver mode to Half Duplex
  111. err = uart_set_mode(MB_PORTNUM, UART_MODE_RS485_HALF_DUPLEX);
  112. SENSE_MB_CHECK((err == ESP_OK), ESP_ERR_INVALID_STATE,
  113. "mb serial set mode failure, uart_set_mode() returned (0x%x).", (uint32_t)err);
  114. vTaskDelay(5);
  115. err = mbc_master_set_descriptor(&device_parameters[0], num_device_parameters);
  116. SENSE_MB_CHECK((err == ESP_OK), ESP_ERR_INVALID_STATE,
  117. "mb controller set descriptor fail, returns(0x%x).",
  118. (uint32_t)err);
  119. return err;
  120. }
  121. // Check Modbus parameters description table and create characteristic description table according to it
  122. esp_err_t sense_modbus_get_characteristics(characteristic_descriptor_t** cid_table, uint16_t* table_size)
  123. {
  124. SENSE_MB_CHECK((table_size != NULL), ESP_ERR_INVALID_ARG, "incorrect cid table size.");
  125. SENSE_MB_CHECK((*table_size >= 1), ESP_ERR_NOT_FOUND, "incorrect cid of characteristic.");
  126. SENSE_MB_CHECK((cid_table != NULL), ESP_ERR_INVALID_ARG, "incorrect cid table pointer.");
  127. esp_err_t error_code = ESP_OK;
  128. const mb_parameter_descriptor_t* param_descriptor = NULL;
  129. characteristic_descriptor_t* cid_instance_ptr = NULL;
  130. uint16_t cid_cnt = 0;
  131. for (cid_cnt = 0; (error_code != ESP_ERR_NOT_FOUND) && (cid_cnt < *table_size); cid_cnt++) {
  132. // Get data from parameters description table
  133. // and use this information to fill the characteristics description table
  134. // and having all required fields in just one table
  135. error_code = mbc_master_get_cid_info(cid_cnt, &param_descriptor);
  136. if ((error_code != ESP_ERR_NOT_FOUND) && (param_descriptor != NULL)) {
  137. void* temp_data_ptr = sense_modbus_get_param_data(param_descriptor);
  138. SENSE_MB_CHECK((temp_data_ptr != NULL),
  139. ESP_ERR_INVALID_STATE, "incorrect instance pointer for cid.");
  140. // Allocate space for characteristic description
  141. cid_instance_ptr = (characteristic_descriptor_t*)malloc(sizeof(characteristic_descriptor_t));
  142. // If not enough memory, try to repair from this error
  143. SENSE_MB_CHECK((cid_instance_ptr != NULL),
  144. ESP_ERR_NO_MEM, "incorrect memory allocation for the cid.");
  145. cid_table[cid_cnt] = cid_instance_ptr;
  146. // Fill the description according to parameter description table
  147. cid_instance_ptr->instance_ptr = temp_data_ptr;
  148. cid_instance_ptr->param_opts = param_descriptor->param_opts;
  149. cid_instance_ptr->cid = param_descriptor->cid;
  150. cid_instance_ptr->access = param_descriptor->access;
  151. cid_instance_ptr->param_key = param_descriptor->param_key;
  152. cid_instance_ptr->param_units = param_descriptor->param_units;
  153. cid_instance_ptr->status = ESP_FAIL;
  154. cid_instance_ptr->instance_size = (size_t)param_descriptor->param_size;
  155. cid_instance_ptr->instance_type = param_descriptor->param_type;
  156. cid_instance_ptr->change_flag = 0; // clear flag
  157. cid_instance_ptr->timestamp = 0; // Keeps timestamp of updated value
  158. assert(cid_instance_ptr->param_key != NULL);
  159. } else {
  160. break;
  161. }
  162. }
  163. ESP_LOGI(TAG, "%s: Found (%u) characteristics in table.", __FUNCTION__, cid_cnt);
  164. // Keep the table pointer and size of table
  165. active_cid_table = cid_table;
  166. active_cid_table_size = cid_cnt;
  167. *table_size = cid_cnt;
  168. return ESP_OK;
  169. }
  170. // Read characteristic value from Modbus parameter according to description table
  171. esp_err_t sense_modbus_read_value(uint16_t cid, void *value)
  172. {
  173. assert(active_cid_table != NULL);
  174. SENSE_MB_CHECK((value != NULL), ESP_ERR_INVALID_ARG, "incorrect value pointer.");
  175. SENSE_MB_CHECK((cid < active_cid_table_size), ESP_ERR_INVALID_ARG, "incorrect cid to read.");
  176. characteristic_descriptor_t* cid_info = active_cid_table[cid];
  177. SENSE_MB_CHECK((cid_info->instance_ptr != NULL), ESP_ERR_INVALID_STATE, "incorrect cid table instance_pointer.");
  178. SENSE_MB_CHECK((cid_info->param_key != NULL), ESP_ERR_INVALID_STATE, "incorrect cid table or not initialized.");
  179. SENSE_MB_CHECK((cid_info->cid == cid), ESP_ERR_INVALID_STATE, "incorrect cid table or not initialized.");
  180. // Set the data pointer for get request to value instance
  181. uint8_t* value_ptr = (uint8_t*)cid_info->instance_ptr;
  182. uint8_t type = 0;
  183. memset((void*)value_ptr, 0, cid_info->instance_size); // Clear value instance first
  184. // Send Modbus request to read cid correspond registers
  185. esp_err_t error = mbc_master_get_parameter(cid, (char*)cid_info->param_key, value_ptr, &type);
  186. SENSE_MB_CHECK((type <= PARAM_TYPE_ASCII), ESP_ERR_NOT_SUPPORTED, "returned data type is not supported (%u)", type);
  187. cid_info->status = error; // Keep last read status of the cid in the information table
  188. if (error == ESP_OK) {
  189. // Copy the value to param
  190. memcpy((void*)value, (void*)value_ptr, cid_info->instance_size);
  191. cid_info->timestamp = sense_modbus_get_time_stamp_us(); // Set timestamp of last access to cid
  192. }
  193. return error;
  194. }
  195. // Write characteristic value into associated Modbus parameter
  196. esp_err_t sense_modbus_send_value(uint16_t cid, void* value)
  197. {
  198. assert(active_cid_table != NULL);
  199. SENSE_MB_CHECK((cid < active_cid_table_size), ESP_ERR_INVALID_ARG, "incorrect cid to set.");
  200. characteristic_descriptor_t* cid_info = active_cid_table[cid];
  201. SENSE_MB_CHECK((cid_info->instance_ptr != NULL), ESP_ERR_INVALID_STATE, "incorrect cid table instance_ptr.");
  202. SENSE_MB_CHECK((cid_info->param_key != NULL), ESP_ERR_INVALID_STATE, "incorrect cid table param_key or not initialized.");
  203. SENSE_MB_CHECK((cid_info->cid == cid), ESP_ERR_INVALID_STATE, "incorrect cid in table or not initialized.");
  204. uint8_t* value_ptr = (uint8_t*)cid_info->instance_ptr;
  205. // Set the instance value of the characteristic to the value
  206. memcpy((void*)value_ptr, (void*)value, cid_info->instance_size);
  207. uint8_t type = 0;
  208. // Set modbus parameter according to value of characteristic
  209. esp_err_t error = mbc_master_set_parameter(cid, (char*)cid_info->param_key, value_ptr, &type);
  210. SENSE_MB_CHECK((cid_info->instance_type == type), ESP_ERR_NOT_FOUND, "incorrect type of parameter (%u)", type);
  211. SENSE_MB_CHECK((type <= PARAM_TYPE_ASCII), ESP_ERR_NOT_SUPPORTED, "returned data type is not supported (%u)", type);
  212. cid_info->change_flag = 0x55; // Set value changed flag to inform higher level
  213. cid_info->status = error; // Keep last read/write status of cid in the information table
  214. cid_info->timestamp = sense_modbus_get_time_stamp_us(); // Set time stamp of last access to cid
  215. return error;
  216. }
  217. // Get cid data from characteristic description table
  218. esp_err_t sense_modbus_get_cid_data(uint16_t cid, characteristic_descriptor_t* cid_data)
  219. {
  220. assert(active_cid_table != NULL);
  221. SENSE_MB_CHECK((cid_data != NULL), ESP_ERR_INVALID_ARG, "incorrect data pointer.");
  222. SENSE_MB_CHECK((cid < active_cid_table_size), ESP_ERR_INVALID_ARG, "cid is not found in the table.");
  223. characteristic_descriptor_t* cid_info = active_cid_table[cid];
  224. SENSE_MB_CHECK((cid_info != NULL), ESP_ERR_INVALID_STATE, "incorrect cid table or not initialized.");
  225. SENSE_MB_CHECK((cid_info->instance_ptr != NULL), ESP_ERR_INVALID_STATE, "incorrect cid table instance_ptr.");
  226. SENSE_MB_CHECK((cid_info->param_key != NULL), ESP_ERR_INVALID_STATE, "incorrect cid table param_key.");
  227. SENSE_MB_CHECK((cid_info->cid == cid), ESP_ERR_INVALID_STATE, "incorrect cid in the table or not initialized.");
  228. *cid_data = *cid_info; // Set cid data
  229. cid_info->change_flag = 0x00; // Reset flag once we get changed value
  230. return ESP_OK;
  231. }
  232. // Todo: add modbus code here