|
|
@@ -1,98 +1,159 @@
|
|
|
-// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
|
|
|
-//
|
|
|
-// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
-// you may not use this file except in compliance with the License.
|
|
|
-// You may obtain a copy of the License at
|
|
|
-//
|
|
|
-// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
-//
|
|
|
-// Unless required by applicable law or agreed to in writing, software
|
|
|
-// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
-// See the License for the specific language governing permissions and
|
|
|
-// limitations under the License.
|
|
|
-
|
|
|
-// mbcontroller.c
|
|
|
-// Implementation of the modbus controller
|
|
|
-// The modbus controller is responsible for processing of modbus packet and transfer data
|
|
|
-// into parameter instance.
|
|
|
+/* Copyright 2018 Espressif Systems (Shanghai) PTE LTD
|
|
|
+ *
|
|
|
+ * Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
+ * you may not use this file except in compliance with the License.
|
|
|
+ * You may obtain a copy of the License at
|
|
|
+ *
|
|
|
+ * http://www.apache.org/licenses/LICENSE-2.0
|
|
|
+ *
|
|
|
+ * Unless required by applicable law or agreed to in writing, software
|
|
|
+ * distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
+ * See the License for the specific language governing permissions and
|
|
|
+ * limitations under the License.
|
|
|
+ */
|
|
|
+
|
|
|
+// mbc_serial_slave.c
|
|
|
+// Implementation of the Modbus controller serial slave
|
|
|
|
|
|
#include <sys/time.h> // for calculation of time stamp in milliseconds
|
|
|
#include "esp_log.h" // for log_write
|
|
|
-#include "freertos/FreeRTOS.h" // for task creation and queue access
|
|
|
-#include "freertos/task.h" // for task api access
|
|
|
-#include "freertos/event_groups.h" // for event groups
|
|
|
-#include "freertos/queue.h" // for queue api access
|
|
|
#include "mb.h" // for mb types definition
|
|
|
#include "mbutils.h" // for mbutils functions definition for stack callback
|
|
|
#include "sdkconfig.h" // for KConfig values
|
|
|
-#include "mbcontroller.h"
|
|
|
+#include "esp_modbus_common.h" // for common defines
|
|
|
+#include "esp_modbus_slave.h" // for public slave interface types
|
|
|
+#include "mbc_slave.h" // for private slave interface types
|
|
|
+#include "mbc_serial_slave.h" // for serial slave implementation definitions
|
|
|
+#include "port_serial_slave.h"
|
|
|
|
|
|
-static const char* TAG = "MB_CONTROLLER";
|
|
|
+// Shared pointer to interface structure
|
|
|
+static mb_slave_interface_t* mbs_interface_ptr = NULL; // &default_interface_inst;
|
|
|
|
|
|
-#define MB_CHECK(a, ret_val, str, ...) \
|
|
|
- if (!(a)) { \
|
|
|
- ESP_LOGE(TAG, "%s(%u): " str, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
|
|
|
- return (ret_val); \
|
|
|
+// Modbus task function
|
|
|
+static void modbus_slave_task(void *pvParameters)
|
|
|
+{
|
|
|
+ // Modbus interface must be initialized before start
|
|
|
+ MB_SLAVE_ASSERT(mbs_interface_ptr != NULL);
|
|
|
+ mb_slave_options_t* mbs_opts = &mbs_interface_ptr->opts;
|
|
|
+
|
|
|
+ MB_SLAVE_ASSERT(mbs_opts != NULL);
|
|
|
+ // Main Modbus stack processing cycle
|
|
|
+ for (;;) {
|
|
|
+ BaseType_t status = xEventGroupWaitBits(mbs_opts->mbs_event_group,
|
|
|
+ (BaseType_t)(MB_EVENT_STACK_STARTED),
|
|
|
+ pdFALSE, // do not clear bits
|
|
|
+ pdFALSE,
|
|
|
+ portMAX_DELAY);
|
|
|
+ // Check if stack started then poll for data
|
|
|
+ if (status & MB_EVENT_STACK_STARTED) {
|
|
|
+ (void)eMBPoll(); // allow stack to process data
|
|
|
+ (void)xMBPortSerialTxPoll(); // Send response buffer if ready
|
|
|
+ }
|
|
|
}
|
|
|
-
|
|
|
-// The Macros below handle the endianness while transfer N byte data into buffer
|
|
|
-#define _XFER_4_RD(dst, src) { \
|
|
|
- *(uint8_t *)(dst)++ = *(uint8_t*)(src + 1); \
|
|
|
- *(uint8_t *)(dst)++ = *(uint8_t*)(src + 0); \
|
|
|
- *(uint8_t *)(dst)++ = *(uint8_t*)(src + 3); \
|
|
|
- *(uint8_t *)(dst)++ = *(uint8_t*)(src + 2); \
|
|
|
- (src) += 4; \
|
|
|
-}
|
|
|
-
|
|
|
-#define _XFER_2_RD(dst, src) { \
|
|
|
- *(uint8_t *)(dst)++ = *(uint8_t *)(src + 1); \
|
|
|
- *(uint8_t *)(dst)++ = *(uint8_t *)(src + 0); \
|
|
|
- (src) += 2; \
|
|
|
-}
|
|
|
-
|
|
|
-#define _XFER_4_WR(dst, src) { \
|
|
|
- *(uint8_t *)(dst + 1) = *(uint8_t *)(src)++; \
|
|
|
- *(uint8_t *)(dst + 0) = *(uint8_t *)(src)++; \
|
|
|
- *(uint8_t *)(dst + 3) = *(uint8_t *)(src)++; \
|
|
|
- *(uint8_t *)(dst + 2) = *(uint8_t *)(src)++ ; \
|
|
|
}
|
|
|
|
|
|
-#define _XFER_2_WR(dst, src) { \
|
|
|
- *(uint8_t *)(dst + 1) = *(uint8_t *)(src)++; \
|
|
|
- *(uint8_t *)(dst + 0) = *(uint8_t *)(src)++; \
|
|
|
+// Setup Modbus controller parameters
|
|
|
+static esp_err_t mbc_serial_slave_setup(void* comm_info)
|
|
|
+{
|
|
|
+ MB_SLAVE_CHECK((mbs_interface_ptr != NULL),
|
|
|
+ ESP_ERR_INVALID_STATE,
|
|
|
+ "Slave interface is not correctly initialized.");
|
|
|
+ MB_SLAVE_CHECK((comm_info != NULL), ESP_ERR_INVALID_ARG,
|
|
|
+ "mb wrong communication settings.");
|
|
|
+ mb_slave_options_t* mbs_opts = &mbs_interface_ptr->opts;
|
|
|
+ mb_slave_comm_info_t* comm_settings = (mb_slave_comm_info_t*)comm_info;
|
|
|
+ MB_SLAVE_CHECK(((comm_settings->mode == MB_MODE_RTU) || (comm_settings->mode == MB_MODE_ASCII)),
|
|
|
+ ESP_ERR_INVALID_ARG, "mb incorrect mode = (0x%x).",
|
|
|
+ (uint32_t)comm_settings->mode);
|
|
|
+ MB_SLAVE_CHECK((comm_settings->slave_addr <= MB_ADDRESS_MAX),
|
|
|
+ ESP_ERR_INVALID_ARG, "mb wrong slave address = (0x%x).",
|
|
|
+ (uint32_t)comm_settings->slave_addr);
|
|
|
+ MB_SLAVE_CHECK((comm_settings->port <= UART_NUM_2), ESP_ERR_INVALID_ARG,
|
|
|
+ "mb wrong port to set = (0x%x).", (uint32_t)comm_settings->port);
|
|
|
+ MB_SLAVE_CHECK((comm_settings->parity <= UART_PARITY_EVEN), ESP_ERR_INVALID_ARG,
|
|
|
+ "mb wrong parity option = (0x%x).", (uint32_t)comm_settings->parity);
|
|
|
+
|
|
|
+ // Set communication options of the controller
|
|
|
+ mbs_opts->mbs_comm = *(mb_communication_info_t*)comm_settings;
|
|
|
+ return ESP_OK;
|
|
|
}
|
|
|
|
|
|
+// Start Modbus controller start function
|
|
|
+static esp_err_t mbc_serial_slave_start(void)
|
|
|
+{
|
|
|
+ MB_SLAVE_CHECK((mbs_interface_ptr != NULL),
|
|
|
+ ESP_ERR_INVALID_STATE,
|
|
|
+ "Slave interface is not correctly initialized.");
|
|
|
+ mb_slave_options_t* mbs_opts = &mbs_interface_ptr->opts;
|
|
|
+ eMBErrorCode status = MB_EIO;
|
|
|
+ // Initialize Modbus stack using mbcontroller parameters
|
|
|
+ status = eMBInit((eMBMode)mbs_opts->mbs_comm.mode,
|
|
|
+ (UCHAR)mbs_opts->mbs_comm.slave_addr,
|
|
|
+ (UCHAR)mbs_opts->mbs_comm.port,
|
|
|
+ (ULONG)mbs_opts->mbs_comm.baudrate,
|
|
|
+ (eMBParity)mbs_opts->mbs_comm.parity);
|
|
|
+ MB_SLAVE_CHECK((status == MB_ENOERR), ESP_ERR_INVALID_STATE,
|
|
|
+ "mb stack initialization failure, eMBInit() returns (0x%x).", status);
|
|
|
#ifdef CONFIG_MB_CONTROLLER_SLAVE_ID_SUPPORT
|
|
|
-
|
|
|
-#define MB_ID_BYTE0(id) ((uint8_t)(id))
|
|
|
-#define MB_ID_BYTE1(id) ((uint8_t)(((uint16_t)(id) >> 8) & 0xFF))
|
|
|
-#define MB_ID_BYTE2(id) ((uint8_t)(((uint32_t)(id) >> 16) & 0xFF))
|
|
|
-#define MB_ID_BYTE3(id) ((uint8_t)(((uint32_t)(id) >> 24) & 0xFF))
|
|
|
-
|
|
|
-#define MB_CONTROLLER_SLAVE_ID (CONFIG_MB_CONTROLLER_SLAVE_ID)
|
|
|
-#define MB_SLAVE_ID_SHORT (MB_ID_BYTE3(MB_CONTROLLER_SLAVE_ID))
|
|
|
-
|
|
|
-// Slave ID constant
|
|
|
-static uint8_t mb_slave_id[] = { MB_ID_BYTE0(MB_CONTROLLER_SLAVE_ID),
|
|
|
- MB_ID_BYTE1(MB_CONTROLLER_SLAVE_ID),
|
|
|
- MB_ID_BYTE2(MB_CONTROLLER_SLAVE_ID) };
|
|
|
-
|
|
|
+ status = eMBSetSlaveID(MB_SLAVE_ID_SHORT, TRUE, (UCHAR*)mb_slave_id, sizeof(mb_slave_id));
|
|
|
+ MB_SLAVE_CHECK((status == MB_ENOERR), ESP_ERR_INVALID_STATE, "mb stack set slave ID failure.");
|
|
|
#endif
|
|
|
+ status = eMBEnable();
|
|
|
+ MB_SLAVE_CHECK((status == MB_ENOERR), ESP_ERR_INVALID_STATE,
|
|
|
+ "mb stack set slave ID failure, eMBEnable() returned (0x%x).", (uint32_t)status);
|
|
|
+ // Set the mbcontroller start flag
|
|
|
+ EventBits_t flag = xEventGroupSetBits(mbs_opts->mbs_event_group,
|
|
|
+ (EventBits_t)MB_EVENT_STACK_STARTED);
|
|
|
+ MB_SLAVE_CHECK((flag & MB_EVENT_STACK_STARTED),
|
|
|
+ ESP_ERR_INVALID_STATE, "mb stack start event set error.");
|
|
|
+ return ESP_OK;
|
|
|
+}
|
|
|
|
|
|
-// Event group parameters
|
|
|
-static TaskHandle_t mb_controller_task_handle = NULL;
|
|
|
-static EventGroupHandle_t mb_controller_event_group = NULL;
|
|
|
-static QueueHandle_t mb_controller_notification_queue_handle = NULL;
|
|
|
+// Modbus controller destroy function
|
|
|
+static esp_err_t mbc_serial_slave_destroy(void)
|
|
|
+{
|
|
|
+ MB_SLAVE_CHECK((mbs_interface_ptr != NULL),
|
|
|
+ ESP_ERR_INVALID_STATE,
|
|
|
+ "Slave interface is not correctly initialized.");
|
|
|
+ mb_slave_options_t* mbs_opts = &mbs_interface_ptr->opts;
|
|
|
+ eMBErrorCode mb_error = MB_ENOERR;
|
|
|
+ // Stop polling by clearing correspondent bit in the event group
|
|
|
+ EventBits_t flag = xEventGroupClearBits(mbs_opts->mbs_event_group,
|
|
|
+ (EventBits_t)MB_EVENT_STACK_STARTED);
|
|
|
+ MB_SLAVE_CHECK((flag & MB_EVENT_STACK_STARTED),
|
|
|
+ ESP_ERR_INVALID_STATE, "mb stack stop event failure.");
|
|
|
+ // Disable and then destroy the Modbus stack
|
|
|
+ mb_error = eMBDisable();
|
|
|
+ MB_SLAVE_CHECK((mb_error == MB_ENOERR), ESP_ERR_INVALID_STATE, "mb stack disable failure.");
|
|
|
+ (void)vTaskDelete(mbs_opts->mbs_task_handle);
|
|
|
+ (void)vQueueDelete(mbs_opts->mbs_notification_queue_handle);
|
|
|
+ (void)vEventGroupDelete(mbs_opts->mbs_event_group);
|
|
|
+ mb_error = eMBClose();
|
|
|
+ MB_SLAVE_CHECK((mb_error == MB_ENOERR), ESP_ERR_INVALID_STATE,
|
|
|
+ "mb stack close failure returned (0x%x).", (uint32_t)mb_error);
|
|
|
+ free(mbs_interface_ptr);
|
|
|
|
|
|
-static uint8_t mb_type = 0;
|
|
|
-static uint8_t mb_address = 0;
|
|
|
-static uint8_t mb_port = 0;
|
|
|
-static uint32_t mb_speed = 0;
|
|
|
-static uint16_t mb_parity = 0;
|
|
|
+ return ESP_OK;
|
|
|
+}
|
|
|
|
|
|
-// This is array of Modbus address area descriptors
|
|
|
-static mb_register_area_descriptor_t mb_area_descriptors[MB_PARAM_COUNT] = { 0 };
|
|
|
+esp_err_t mbc_serial_slave_set_descriptor(const mb_register_area_descriptor_t descr_info)
|
|
|
+{
|
|
|
+ MB_SLAVE_CHECK((mbs_interface_ptr != NULL),
|
|
|
+ ESP_ERR_INVALID_STATE,
|
|
|
+ "Slave interface is not correctly initialized.");
|
|
|
+ mb_slave_options_t* mbs_opts = &mbs_interface_ptr->opts;
|
|
|
+ MB_SLAVE_CHECK(((descr_info.type < MB_PARAM_COUNT) && (descr_info.type >= MB_PARAM_HOLDING)),
|
|
|
+ ESP_ERR_INVALID_ARG, "mb incorrect modbus instance type = (0x%x).",
|
|
|
+ (uint32_t)descr_info.type);
|
|
|
+ MB_SLAVE_CHECK((descr_info.address != NULL),
|
|
|
+ ESP_ERR_INVALID_ARG, "mb instance pointer is NULL.");
|
|
|
+ MB_SLAVE_CHECK((descr_info.size >= MB_INST_MIN_SIZE) && (descr_info.size < (MB_INST_MAX_SIZE)),
|
|
|
+ ESP_ERR_INVALID_ARG, "mb instance size is incorrect = (0x%x).",
|
|
|
+ (uint32_t)descr_info.size);
|
|
|
+ mbs_opts->mbs_area_descriptors[descr_info.type] = descr_info;
|
|
|
+ return ESP_OK;
|
|
|
+}
|
|
|
|
|
|
// The helper function to get time stamp in microseconds
|
|
|
static uint64_t get_time_stamp()
|
|
|
@@ -105,6 +166,10 @@ static uint64_t get_time_stamp()
|
|
|
static esp_err_t send_param_info(mb_event_group_t par_type, uint16_t mb_offset,
|
|
|
uint8_t* par_address, uint16_t par_size)
|
|
|
{
|
|
|
+ MB_SLAVE_CHECK((mbs_interface_ptr != NULL),
|
|
|
+ ESP_ERR_INVALID_STATE,
|
|
|
+ "Slave interface is not correctly initialized.");
|
|
|
+ mb_slave_options_t* mbs_opts = &mbs_interface_ptr->opts;
|
|
|
esp_err_t error = ESP_FAIL;
|
|
|
mb_param_info_t par_info;
|
|
|
// Check if queue is not full the send parameter information
|
|
|
@@ -113,116 +178,60 @@ static esp_err_t send_param_info(mb_event_group_t par_type, uint16_t mb_offset,
|
|
|
par_info.address = par_address;
|
|
|
par_info.time_stamp = get_time_stamp();
|
|
|
par_info.mb_offset = mb_offset;
|
|
|
- BaseType_t status = xQueueSend(mb_controller_notification_queue_handle, &par_info, MB_PAR_INFO_TOUT);
|
|
|
+ BaseType_t status = xQueueSend(mbs_opts->mbs_notification_queue_handle,
|
|
|
+ &par_info, MB_PAR_INFO_TOUT);
|
|
|
if (pdTRUE == status) {
|
|
|
- ESP_LOGD(TAG, "Queue send parameter info (type, address, size): %d, 0x%.4x, %d",
|
|
|
+ ESP_LOGD(MB_SLAVE_TAG, "Queue send parameter info (type, address, size): %d, 0x%.4x, %d",
|
|
|
par_type, (uint32_t)par_address, par_size);
|
|
|
error = ESP_OK;
|
|
|
} else if (errQUEUE_FULL == status) {
|
|
|
- ESP_LOGD(TAG, "Parameter queue is overflowed.");
|
|
|
+ ESP_LOGD(MB_SLAVE_TAG, "Parameter queue is overflowed.");
|
|
|
}
|
|
|
return error;
|
|
|
}
|
|
|
|
|
|
+// Helper function to send notification
|
|
|
static esp_err_t send_param_access_notification(mb_event_group_t event)
|
|
|
{
|
|
|
+ MB_SLAVE_CHECK((mbs_interface_ptr != NULL),
|
|
|
+ ESP_ERR_INVALID_STATE,
|
|
|
+ "Slave interface is not correctly initialized.");
|
|
|
+ mb_slave_options_t* mbs_opts = &mbs_interface_ptr->opts;
|
|
|
esp_err_t err = ESP_FAIL;
|
|
|
- mb_event_group_t bits = (mb_event_group_t)xEventGroupSetBits(mb_controller_event_group, (EventBits_t)event);
|
|
|
+ mb_event_group_t bits = (mb_event_group_t)xEventGroupSetBits(mbs_opts->mbs_event_group,
|
|
|
+ (EventBits_t)event);
|
|
|
if (bits & event) {
|
|
|
- ESP_LOGD(TAG, "The MB_REG_CHANGE_EVENT = 0x%.2x is set.", (uint8_t)event);
|
|
|
+ ESP_LOGD(MB_SLAVE_TAG, "The MB_REG_CHANGE_EVENT = 0x%.2x is set.", (uint8_t)event);
|
|
|
err = ESP_OK;
|
|
|
}
|
|
|
return err;
|
|
|
}
|
|
|
|
|
|
-// Modbus task function
|
|
|
-static void modbus_task(void *pvParameters) {
|
|
|
- // Main Modbus stack processing cycle
|
|
|
- for (;;) {
|
|
|
- BaseType_t status = xEventGroupWaitBits(mb_controller_event_group,
|
|
|
- (BaseType_t)(MB_EVENT_STACK_STARTED),
|
|
|
- pdFALSE, // do not clear bits
|
|
|
- pdFALSE,
|
|
|
- portMAX_DELAY);
|
|
|
- // Check if stack started then poll for data
|
|
|
- if (status & MB_EVENT_STACK_STARTED) {
|
|
|
- (void)eMBPoll(); // allow stack to process data
|
|
|
- (void)xMBPortSerialTxPoll(); // Send response buffer if ready
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
// Blocking function to get event on parameter group change for application task
|
|
|
-mb_event_group_t mbcontroller_check_event(mb_event_group_t group)
|
|
|
+static mb_event_group_t mbc_serial_slave_check_event(mb_event_group_t group)
|
|
|
{
|
|
|
- assert(mb_controller_event_group != NULL);
|
|
|
- BaseType_t status = xEventGroupWaitBits(mb_controller_event_group, (BaseType_t)group,
|
|
|
+ MB_SLAVE_CHECK((mbs_interface_ptr != NULL),
|
|
|
+ ESP_ERR_INVALID_STATE,
|
|
|
+ "Slave interface is not correctly initialized.");
|
|
|
+ mb_slave_options_t* mbs_opts = &mbs_interface_ptr->opts;
|
|
|
+ MB_SLAVE_ASSERT(mbs_opts->mbs_event_group != NULL);
|
|
|
+ BaseType_t status = xEventGroupWaitBits(mbs_opts->mbs_event_group, (BaseType_t)group,
|
|
|
pdTRUE , pdFALSE, portMAX_DELAY);
|
|
|
return (mb_event_group_t)status;
|
|
|
}
|
|
|
|
|
|
-esp_err_t mbcontroller_set_descriptor(const mb_register_area_descriptor_t descr_info)
|
|
|
-{
|
|
|
- MB_CHECK(((descr_info.type < MB_PARAM_COUNT) && (descr_info.type >= MB_PARAM_HOLDING)),
|
|
|
- ESP_ERR_INVALID_ARG, "mb incorrect modbus instance type = (0x%x).",
|
|
|
- (uint32_t)descr_info.type);
|
|
|
- MB_CHECK((descr_info.address != NULL),
|
|
|
- ESP_ERR_INVALID_ARG, "mb instance pointer is NULL.");
|
|
|
- MB_CHECK((descr_info.size >= MB_INST_MIN_SIZE) && (descr_info.size < (MB_INST_MAX_SIZE)),
|
|
|
- ESP_ERR_INVALID_ARG, "mb instance size is incorrect = (0x%x).",
|
|
|
- (uint32_t)descr_info.size);
|
|
|
- mb_area_descriptors[descr_info.type].type = descr_info.type;
|
|
|
- mb_area_descriptors[descr_info.type].start_offset = descr_info.start_offset;
|
|
|
- mb_area_descriptors[descr_info.type].address = (uint8_t*)descr_info.address;
|
|
|
- mb_area_descriptors[descr_info.type].size = descr_info.size;
|
|
|
- return ESP_OK;
|
|
|
-}
|
|
|
-
|
|
|
-// Initialization of Modbus controller
|
|
|
-esp_err_t mbcontroller_init(void) {
|
|
|
- mb_type = MB_MODE_RTU;
|
|
|
- mb_address = MB_DEVICE_ADDRESS;
|
|
|
- mb_port = MB_UART_PORT;
|
|
|
- mb_speed = MB_DEVICE_SPEED;
|
|
|
- mb_parity = MB_PARITY_NONE;
|
|
|
-
|
|
|
- // Initialization of active context of the modbus controller
|
|
|
- BaseType_t status = 0;
|
|
|
- // Parameter change notification queue
|
|
|
- mb_controller_event_group = xEventGroupCreate();
|
|
|
- MB_CHECK((mb_controller_event_group != NULL),
|
|
|
- ESP_ERR_NO_MEM, "mb event group error.");
|
|
|
- // Parameter change notification queue
|
|
|
- mb_controller_notification_queue_handle = xQueueCreate(
|
|
|
- MB_CONTROLLER_NOTIFY_QUEUE_SIZE,
|
|
|
- sizeof(mb_param_info_t));
|
|
|
- MB_CHECK((mb_controller_notification_queue_handle != NULL),
|
|
|
- ESP_ERR_NO_MEM, "mb notify queue creation error.");
|
|
|
- // Create modbus controller task
|
|
|
- status = xTaskCreate((void*)&modbus_task,
|
|
|
- "modbus_task",
|
|
|
- MB_CONTROLLER_STACK_SIZE,
|
|
|
- NULL,
|
|
|
- MB_CONTROLLER_PRIORITY,
|
|
|
- &mb_controller_task_handle);
|
|
|
- if (status != pdPASS) {
|
|
|
- vTaskDelete(mb_controller_task_handle);
|
|
|
- MB_CHECK((status == pdPASS), ESP_ERR_NO_MEM,
|
|
|
- "mb controller task creation error, xTaskCreate() returns (0x%x).",
|
|
|
- (uint32_t)status);
|
|
|
- }
|
|
|
- assert(mb_controller_task_handle != NULL); // The task is created but handle is incorrect
|
|
|
- return ESP_OK;
|
|
|
-}
|
|
|
-
|
|
|
// Function to get notification about parameter change from application task
|
|
|
-esp_err_t mbcontroller_get_param_info(mb_param_info_t* reg_info, uint32_t timeout)
|
|
|
+static esp_err_t mbc_serial_slave_get_param_info(mb_param_info_t* reg_info, uint32_t timeout)
|
|
|
{
|
|
|
+ MB_SLAVE_CHECK((mbs_interface_ptr != NULL),
|
|
|
+ ESP_ERR_INVALID_STATE,
|
|
|
+ "Slave interface is not correctly initialized.");
|
|
|
+ mb_slave_options_t* mbs_opts = &mbs_interface_ptr->opts;
|
|
|
esp_err_t err = ESP_ERR_TIMEOUT;
|
|
|
- MB_CHECK((mb_controller_notification_queue_handle != NULL),
|
|
|
+ MB_SLAVE_CHECK((mbs_opts->mbs_notification_queue_handle != NULL),
|
|
|
ESP_ERR_INVALID_ARG, "mb queue handle is invalid.");
|
|
|
- MB_CHECK((reg_info != NULL), ESP_ERR_INVALID_ARG, "mb register information is invalid.");
|
|
|
- BaseType_t status = xQueueReceive(mb_controller_notification_queue_handle,
|
|
|
+ MB_SLAVE_CHECK((reg_info != NULL), ESP_ERR_INVALID_ARG, "mb register information is invalid.");
|
|
|
+ BaseType_t status = xQueueReceive(mbs_opts->mbs_notification_queue_handle,
|
|
|
reg_info, pdMS_TO_TICKS(timeout));
|
|
|
if (status == pdTRUE) {
|
|
|
err = ESP_OK;
|
|
|
@@ -230,72 +239,6 @@ esp_err_t mbcontroller_get_param_info(mb_param_info_t* reg_info, uint32_t timeou
|
|
|
return err;
|
|
|
}
|
|
|
|
|
|
-// Start Modbus controller start function
|
|
|
-esp_err_t mbcontroller_start(void)
|
|
|
-{
|
|
|
- eMBErrorCode status = MB_EIO;
|
|
|
- // Initialize Modbus stack using mbcontroller parameters
|
|
|
- status = eMBInit((eMBMode)mb_type, (UCHAR)mb_address, (UCHAR)mb_port,
|
|
|
- (ULONG)mb_speed, (eMBParity)mb_parity);
|
|
|
- MB_CHECK((status == MB_ENOERR), ESP_ERR_INVALID_STATE,
|
|
|
- "mb stack initialization failure, eMBInit() returns (0x%x).", status);
|
|
|
-#ifdef CONFIG_MB_CONTROLLER_SLAVE_ID_SUPPORT
|
|
|
- status = eMBSetSlaveID(MB_SLAVE_ID_SHORT, TRUE, (UCHAR*)mb_slave_id, sizeof(mb_slave_id));
|
|
|
- MB_CHECK((status == MB_ENOERR), ESP_ERR_INVALID_STATE, "mb stack set slave ID failure.");
|
|
|
-#endif
|
|
|
- status = eMBEnable();
|
|
|
- MB_CHECK((status == MB_ENOERR), ESP_ERR_INVALID_STATE,
|
|
|
- "mb stack set slave ID failure, eMBEnable() returned (0x%x).", (uint32_t)status);
|
|
|
- // Set the mbcontroller start flag
|
|
|
- EventBits_t flag = xEventGroupSetBits(mb_controller_event_group,
|
|
|
- (EventBits_t)MB_EVENT_STACK_STARTED);
|
|
|
- MB_CHECK((flag & MB_EVENT_STACK_STARTED),
|
|
|
- ESP_ERR_INVALID_STATE, "mb stack start event set error.");
|
|
|
- return ESP_OK;
|
|
|
-}
|
|
|
-
|
|
|
-// Modbus controller destroy function
|
|
|
-esp_err_t mbcontroller_destroy(void)
|
|
|
-{
|
|
|
- eMBErrorCode mb_error = MB_ENOERR;
|
|
|
- // Stop polling by clearing correspondent bit in the event group
|
|
|
- EventBits_t flag = xEventGroupClearBits(mb_controller_event_group,
|
|
|
- (EventBits_t)MB_EVENT_STACK_STARTED);
|
|
|
- MB_CHECK((flag & MB_EVENT_STACK_STARTED),
|
|
|
- ESP_ERR_INVALID_STATE, "mb stack stop event failure.");
|
|
|
- // Desable and then destroy the Modbus stack
|
|
|
- mb_error = eMBDisable();
|
|
|
- MB_CHECK((mb_error == MB_ENOERR), ESP_ERR_INVALID_STATE, "mb stack disable failure.");
|
|
|
- (void)vTaskDelete(mb_controller_task_handle);
|
|
|
- (void)vQueueDelete(mb_controller_notification_queue_handle);
|
|
|
- (void)vEventGroupDelete(mb_controller_event_group);
|
|
|
- mb_error = eMBClose();
|
|
|
- MB_CHECK((mb_error == MB_ENOERR), ESP_ERR_INVALID_STATE,
|
|
|
- "mb stack close failure returned (0x%x).", (uint32_t)mb_error);
|
|
|
- return ESP_OK;
|
|
|
-}
|
|
|
-
|
|
|
-// Setup modbus controller parameters
|
|
|
-esp_err_t mbcontroller_setup(const mb_communication_info_t comm_info)
|
|
|
-{
|
|
|
- MB_CHECK(((comm_info.mode == MB_MODE_RTU) || (comm_info.mode == MB_MODE_ASCII)),
|
|
|
- ESP_ERR_INVALID_ARG, "mb incorrect mode = (0x%x).",
|
|
|
- (uint32_t)comm_info.mode);
|
|
|
- MB_CHECK((comm_info.slave_addr <= MB_ADDRESS_MAX),
|
|
|
- ESP_ERR_INVALID_ARG, "mb wrong slave address = (0x%x).",
|
|
|
- (uint32_t)comm_info.slave_addr);
|
|
|
- MB_CHECK((comm_info.port <= UART_NUM_2), ESP_ERR_INVALID_ARG,
|
|
|
- "mb wrong port to set = (0x%x).", (uint32_t)comm_info.port);
|
|
|
- MB_CHECK((comm_info.parity <= UART_PARITY_EVEN), ESP_ERR_INVALID_ARG,
|
|
|
- "mb wrong parity option = (0x%x).", (uint32_t)comm_info.parity);
|
|
|
- mb_type = (uint8_t)comm_info.mode;
|
|
|
- mb_address = (uint8_t)comm_info.slave_addr;
|
|
|
- mb_port = (uint8_t)comm_info.port;
|
|
|
- mb_speed = (uint32_t)comm_info.baudrate;
|
|
|
- mb_parity = (uint8_t)comm_info.parity;
|
|
|
- return ESP_OK;
|
|
|
-}
|
|
|
-
|
|
|
/* ----------------------- Callback functions for Modbus stack ---------------------------------*/
|
|
|
// These are executed by modbus stack to read appropriate type of registers.
|
|
|
|
|
|
@@ -303,13 +246,17 @@ esp_err_t mbcontroller_setup(const mb_communication_info_t comm_info)
|
|
|
#pragma GCC diagnostic ignored "-Wtype-limits"
|
|
|
|
|
|
// Callback function for reading of MB Input Registers
|
|
|
-eMBErrorCode eMBRegInputCB(UCHAR * pucRegBuffer, USHORT usAddress,
|
|
|
+eMBErrorCode eMBRegInputCBSerialSlave(UCHAR * pucRegBuffer, USHORT usAddress,
|
|
|
USHORT usNRegs)
|
|
|
{
|
|
|
- assert(pucRegBuffer != NULL);
|
|
|
- USHORT usRegInputNregs = (USHORT)(mb_area_descriptors[MB_PARAM_INPUT].size >> 1); // Number of input registers
|
|
|
- USHORT usInputRegStart = (USHORT)mb_area_descriptors[MB_PARAM_INPUT].start_offset; // Get Modbus start address
|
|
|
- UCHAR* pucInputBuffer = (UCHAR*)mb_area_descriptors[MB_PARAM_INPUT].address; // Get instance address
|
|
|
+ MB_SLAVE_CHECK((mbs_interface_ptr != NULL),
|
|
|
+ MB_EILLSTATE, "Slave stack uninitialized.");
|
|
|
+ MB_SLAVE_CHECK((pucRegBuffer != NULL),
|
|
|
+ MB_EINVAL, "Slave stack call failed.");
|
|
|
+ mb_slave_options_t* mbs_opts = &mbs_interface_ptr->opts;
|
|
|
+ USHORT usRegInputNregs = (USHORT)(mbs_opts->mbs_area_descriptors[MB_PARAM_INPUT].size >> 1); // Number of input registers
|
|
|
+ USHORT usInputRegStart = (USHORT)mbs_opts->mbs_area_descriptors[MB_PARAM_INPUT].start_offset; // Get Modbus start address
|
|
|
+ UCHAR* pucInputBuffer = (UCHAR*)mbs_opts->mbs_area_descriptors[MB_PARAM_INPUT].address; // Get instance address
|
|
|
USHORT usRegs = usNRegs;
|
|
|
eMBErrorCode eStatus = MB_ENOERR;
|
|
|
USHORT iRegIndex;
|
|
|
@@ -341,13 +288,17 @@ eMBErrorCode eMBRegInputCB(UCHAR * pucRegBuffer, USHORT usAddress,
|
|
|
|
|
|
// Callback function for reading of MB Holding Registers
|
|
|
// Executed by stack when request to read/write holding registers is received
|
|
|
-eMBErrorCode eMBRegHoldingCB(UCHAR * pucRegBuffer, USHORT usAddress,
|
|
|
+eMBErrorCode eMBRegHoldingCBSerialSlave(UCHAR * pucRegBuffer, USHORT usAddress,
|
|
|
USHORT usNRegs, eMBRegisterMode eMode)
|
|
|
{
|
|
|
- assert(pucRegBuffer != NULL);
|
|
|
- USHORT usRegHoldingNregs = (USHORT)(mb_area_descriptors[MB_PARAM_HOLDING].size >> 1);
|
|
|
- USHORT usRegHoldingStart = (USHORT)mb_area_descriptors[MB_PARAM_HOLDING].start_offset;
|
|
|
- UCHAR* pucHoldingBuffer = (UCHAR*)mb_area_descriptors[MB_PARAM_HOLDING].address;
|
|
|
+ MB_SLAVE_CHECK((mbs_interface_ptr != NULL),
|
|
|
+ MB_EILLSTATE, "Slave stack uninitialized.");
|
|
|
+ MB_SLAVE_CHECK((pucRegBuffer != NULL),
|
|
|
+ MB_EINVAL, "Slave stack call failed.");
|
|
|
+ mb_slave_options_t* mbs_opts = &mbs_interface_ptr->opts;
|
|
|
+ USHORT usRegHoldingNregs = (USHORT)(mbs_opts->mbs_area_descriptors[MB_PARAM_HOLDING].size >> 1);
|
|
|
+ USHORT usRegHoldingStart = (USHORT)mbs_opts->mbs_area_descriptors[MB_PARAM_HOLDING].start_offset;
|
|
|
+ UCHAR* pucHoldingBuffer = (UCHAR*)mbs_opts->mbs_area_descriptors[MB_PARAM_HOLDING].address;
|
|
|
eMBErrorCode eStatus = MB_ENOERR;
|
|
|
USHORT iRegIndex;
|
|
|
USHORT usRegs = usNRegs;
|
|
|
@@ -395,13 +346,17 @@ eMBErrorCode eMBRegHoldingCB(UCHAR * pucRegBuffer, USHORT usAddress,
|
|
|
}
|
|
|
|
|
|
// Callback function for reading of MB Coils Registers
|
|
|
-eMBErrorCode eMBRegCoilsCB(UCHAR* pucRegBuffer, USHORT usAddress,
|
|
|
+eMBErrorCode eMBRegCoilsCBSerialSlave(UCHAR* pucRegBuffer, USHORT usAddress,
|
|
|
USHORT usNCoils, eMBRegisterMode eMode)
|
|
|
{
|
|
|
- assert(NULL != pucRegBuffer);
|
|
|
- USHORT usRegCoilNregs = (USHORT)(mb_area_descriptors[MB_PARAM_COIL].size >> 1); // number of registers in storage area
|
|
|
- USHORT usRegCoilsStart = (USHORT)mb_area_descriptors[MB_PARAM_COIL].start_offset; // MB offset of coils registers
|
|
|
- UCHAR* pucRegCoilsBuf = (UCHAR*)mb_area_descriptors[MB_PARAM_COIL].address;
|
|
|
+ MB_SLAVE_CHECK((mbs_interface_ptr != NULL),
|
|
|
+ MB_EILLSTATE, "Slave stack uninitialized.");
|
|
|
+ MB_SLAVE_CHECK((pucRegBuffer != NULL),
|
|
|
+ MB_EINVAL, "Slave stack call failed.");
|
|
|
+ mb_slave_options_t* mbs_opts = &mbs_interface_ptr->opts;
|
|
|
+ USHORT usRegCoilNregs = (USHORT)(mbs_opts->mbs_area_descriptors[MB_PARAM_COIL].size >> 1); // number of registers in storage area
|
|
|
+ USHORT usRegCoilsStart = (USHORT)mbs_opts->mbs_area_descriptors[MB_PARAM_COIL].start_offset; // MB offset of coils registers
|
|
|
+ UCHAR* pucRegCoilsBuf = (UCHAR*)mbs_opts->mbs_area_descriptors[MB_PARAM_COIL].address;
|
|
|
eMBErrorCode eStatus = MB_ENOERR;
|
|
|
USHORT iRegIndex;
|
|
|
USHORT usCoils = usNCoils;
|
|
|
@@ -448,13 +403,17 @@ eMBErrorCode eMBRegCoilsCB(UCHAR* pucRegBuffer, USHORT usAddress,
|
|
|
}
|
|
|
|
|
|
// Callback function for reading of MB Discrete Input Registers
|
|
|
-eMBErrorCode eMBRegDiscreteCB(UCHAR * pucRegBuffer, USHORT usAddress,
|
|
|
+eMBErrorCode eMBRegDiscreteCBSerialSlave(UCHAR* pucRegBuffer, USHORT usAddress,
|
|
|
USHORT usNDiscrete)
|
|
|
{
|
|
|
- assert(pucRegBuffer != NULL);
|
|
|
- USHORT usRegDiscreteNregs = (USHORT)(mb_area_descriptors[MB_PARAM_DISCRETE].size >> 1); // number of registers in storage area
|
|
|
- USHORT usRegDiscreteStart = (USHORT)mb_area_descriptors[MB_PARAM_DISCRETE].start_offset; // MB offset of registers
|
|
|
- UCHAR* pucRegDiscreteBuf = (UCHAR*)mb_area_descriptors[MB_PARAM_DISCRETE].address; // the storage address
|
|
|
+ MB_SLAVE_CHECK((mbs_interface_ptr != NULL),
|
|
|
+ MB_EILLSTATE, "Slave stack uninitialized.");
|
|
|
+ MB_SLAVE_CHECK((pucRegBuffer != NULL),
|
|
|
+ MB_EINVAL, "Slave stack call failed.");
|
|
|
+ mb_slave_options_t* mbs_opts = &mbs_interface_ptr->opts;
|
|
|
+ USHORT usRegDiscreteNregs = (USHORT)(mbs_opts->mbs_area_descriptors[MB_PARAM_DISCRETE].size >> 1); // number of registers in storage area
|
|
|
+ USHORT usRegDiscreteStart = (USHORT)mbs_opts->mbs_area_descriptors[MB_PARAM_DISCRETE].start_offset; // MB offset of registers
|
|
|
+ UCHAR* pucRegDiscreteBuf = (UCHAR*)mbs_opts->mbs_area_descriptors[MB_PARAM_DISCRETE].address; // the storage address
|
|
|
eMBErrorCode eStatus = MB_ENOERR;
|
|
|
USHORT iRegIndex, iRegBitIndex, iNReg;
|
|
|
UCHAR* pucDiscreteInputBuf;
|
|
|
@@ -490,3 +449,73 @@ eMBErrorCode eMBRegDiscreteCB(UCHAR * pucRegBuffer, USHORT usAddress,
|
|
|
return eStatus;
|
|
|
}
|
|
|
#pragma GCC diagnostic pop // require GCC
|
|
|
+
|
|
|
+// Initialization of Modbus controller
|
|
|
+esp_err_t mbc_serial_slave_create(mb_port_type_t port_type, void** handler)
|
|
|
+{
|
|
|
+ MB_SLAVE_CHECK((port_type == MB_PORT_SERIAL_SLAVE),
|
|
|
+ ESP_ERR_NOT_SUPPORTED,
|
|
|
+ "mb port not supported = %u.", (uint32_t)port_type);
|
|
|
+ // Allocate space for options
|
|
|
+ if (mbs_interface_ptr == NULL) {
|
|
|
+ mbs_interface_ptr = malloc(sizeof(mb_slave_interface_t));
|
|
|
+ }
|
|
|
+ MB_SLAVE_ASSERT(mbs_interface_ptr != NULL);
|
|
|
+ vMBPortSetMode((UCHAR)port_type);
|
|
|
+ mb_slave_options_t* mbs_opts = &mbs_interface_ptr->opts;
|
|
|
+ mbs_opts->port_type = MB_PORT_SERIAL_SLAVE; // set interface port type
|
|
|
+
|
|
|
+ // Set default values of communication options
|
|
|
+ mbs_opts->mbs_comm.mode = MB_MODE_RTU;
|
|
|
+ mbs_opts->mbs_comm.slave_addr = MB_DEVICE_ADDRESS;
|
|
|
+ mbs_opts->mbs_comm.port = MB_UART_PORT;
|
|
|
+ mbs_opts->mbs_comm.baudrate = MB_DEVICE_SPEED;
|
|
|
+ mbs_opts->mbs_comm.parity = MB_PARITY_NONE;
|
|
|
+
|
|
|
+ // Initialization of active context of the Modbus controller
|
|
|
+ BaseType_t status = 0;
|
|
|
+ // Parameter change notification queue
|
|
|
+ mbs_opts->mbs_event_group = xEventGroupCreate();
|
|
|
+ MB_SLAVE_CHECK((mbs_opts->mbs_event_group != NULL),
|
|
|
+ ESP_ERR_NO_MEM, "mb event group error.");
|
|
|
+ // Parameter change notification queue
|
|
|
+ mbs_opts->mbs_notification_queue_handle = xQueueCreate(
|
|
|
+ MB_CONTROLLER_NOTIFY_QUEUE_SIZE,
|
|
|
+ sizeof(mb_param_info_t));
|
|
|
+ MB_SLAVE_CHECK((mbs_opts->mbs_notification_queue_handle != NULL),
|
|
|
+ ESP_ERR_NO_MEM, "mb notify queue creation error.");
|
|
|
+ // Create Modbus controller task
|
|
|
+ status = xTaskCreate((void*)&modbus_slave_task,
|
|
|
+ "modbus_slave_task",
|
|
|
+ MB_CONTROLLER_STACK_SIZE,
|
|
|
+ NULL,
|
|
|
+ MB_CONTROLLER_PRIORITY,
|
|
|
+ &mbs_opts->mbs_task_handle);
|
|
|
+ if (status != pdPASS) {
|
|
|
+ vTaskDelete(mbs_opts->mbs_task_handle);
|
|
|
+ MB_SLAVE_CHECK((status == pdPASS), ESP_ERR_NO_MEM,
|
|
|
+ "mb controller task creation error, xTaskCreate() returns (0x%x).",
|
|
|
+ (uint32_t)status);
|
|
|
+ }
|
|
|
+ MB_SLAVE_ASSERT(mbs_opts->mbs_task_handle != NULL); // The task is created but handle is incorrect
|
|
|
+
|
|
|
+ // Initialize interface function pointers
|
|
|
+ mbs_interface_ptr->check_event = mbc_serial_slave_check_event;
|
|
|
+ mbs_interface_ptr->destroy = mbc_serial_slave_destroy;
|
|
|
+ mbs_interface_ptr->get_param_info = mbc_serial_slave_get_param_info;
|
|
|
+ mbs_interface_ptr->init = mbc_serial_slave_create;
|
|
|
+ mbs_interface_ptr->set_descriptor = mbc_serial_slave_set_descriptor;
|
|
|
+ mbs_interface_ptr->setup = mbc_serial_slave_setup;
|
|
|
+ mbs_interface_ptr->start = mbc_serial_slave_start;
|
|
|
+
|
|
|
+ // Initialize stack callback function pointers
|
|
|
+ mbs_interface_ptr->slave_reg_cb_discrete = eMBRegDiscreteCBSerialSlave;
|
|
|
+ mbs_interface_ptr->slave_reg_cb_input = eMBRegInputCBSerialSlave;
|
|
|
+ mbs_interface_ptr->slave_reg_cb_holding = eMBRegHoldingCBSerialSlave;
|
|
|
+ mbs_interface_ptr->slave_reg_cb_coils = eMBRegCoilsCBSerialSlave;
|
|
|
+
|
|
|
+ *handler = (void*)mbs_interface_ptr;
|
|
|
+
|
|
|
+ return ESP_OK;
|
|
|
+}
|
|
|
+
|