2018-10-19 09:51:27 -04:00
|
|
|
/* 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
|
2018-06-28 11:45:41 -04:00
|
|
|
|
|
|
|
#include <sys/time.h> // for calculation of time stamp in milliseconds
|
|
|
|
#include "esp_log.h" // for log_write
|
|
|
|
#include "mb.h" // for mb types definition
|
|
|
|
#include "mbutils.h" // for mbutils functions definition for stack callback
|
|
|
|
#include "sdkconfig.h" // for KConfig values
|
2018-10-19 09:51:27 -04:00
|
|
|
#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"
|
2018-06-28 11:45:41 -04:00
|
|
|
|
2018-10-19 09:51:27 -04:00
|
|
|
// Shared pointer to interface structure
|
2020-07-21 12:34:04 -04:00
|
|
|
static mb_slave_interface_t* mbs_interface_ptr = NULL;
|
2018-06-28 11:45:41 -04:00
|
|
|
|
2018-10-19 09:51:27 -04:00
|
|
|
// Modbus task function
|
|
|
|
static void modbus_slave_task(void *pvParameters)
|
|
|
|
{
|
2020-11-10 02:40:01 -05:00
|
|
|
// Modbus interface must be initialized before start
|
2018-10-19 09:51:27 -04:00
|
|
|
MB_SLAVE_ASSERT(mbs_interface_ptr != NULL);
|
|
|
|
mb_slave_options_t* mbs_opts = &mbs_interface_ptr->opts;
|
2020-07-21 12:34:04 -04:00
|
|
|
|
2018-10-19 09:51:27 -04:00
|
|
|
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
|
2019-11-08 03:55:42 -05:00
|
|
|
// Send response buffer
|
2019-11-26 00:16:25 -05:00
|
|
|
BOOL xSentState = xMBPortSerialTxPoll();
|
2019-11-08 03:55:42 -05:00
|
|
|
if (xSentState) {
|
|
|
|
(void)xMBPortEventPost( EV_FRAME_SENT );
|
|
|
|
}
|
2018-10-19 09:51:27 -04:00
|
|
|
}
|
2018-06-28 11:45:41 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-19 09:51:27 -04:00
|
|
|
// 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);
|
2019-06-13 23:01:30 -04:00
|
|
|
MB_SLAVE_CHECK((comm_settings->port < UART_NUM_MAX), ESP_ERR_INVALID_ARG,
|
2018-10-19 09:51:27 -04:00
|
|
|
"mb wrong port to set = (0x%x).", (uint32_t)comm_settings->port);
|
2021-01-11 04:55:36 -05:00
|
|
|
MB_SLAVE_CHECK((comm_settings->parity <= UART_PARITY_ODD), ESP_ERR_INVALID_ARG,
|
2018-10-19 09:51:27 -04:00
|
|
|
"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;
|
2018-06-28 11:45:41 -04:00
|
|
|
}
|
|
|
|
|
2018-10-19 09:51:27 -04:00
|
|
|
// 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;
|
2021-01-11 04:55:36 -05:00
|
|
|
const mb_communication_info_t* comm_info = (mb_communication_info_t*)&mbs_opts->mbs_comm;
|
|
|
|
|
2018-10-19 09:51:27 -04:00
|
|
|
// Initialize Modbus stack using mbcontroller parameters
|
2021-01-11 04:55:36 -05:00
|
|
|
status = eMBInit((eMBMode)comm_info->mode,
|
|
|
|
(UCHAR)comm_info->slave_addr,
|
|
|
|
(UCHAR)comm_info->port,
|
|
|
|
(ULONG)comm_info->baudrate,
|
|
|
|
MB_PORT_PARITY_GET(comm_info->parity));
|
|
|
|
|
2018-10-19 09:51:27 -04:00
|
|
|
MB_SLAVE_CHECK((status == MB_ENOERR), ESP_ERR_INVALID_STATE,
|
|
|
|
"mb stack initialization failure, eMBInit() returns (0x%x).", status);
|
|
|
|
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;
|
|
|
|
}
|
2018-06-28 11:45:41 -04:00
|
|
|
|
|
|
|
// Blocking function to get event on parameter group change for application task
|
2018-10-19 09:51:27 -04:00
|
|
|
static mb_event_group_t mbc_serial_slave_check_event(mb_event_group_t group)
|
2018-06-28 11:45:41 -04:00
|
|
|
{
|
2018-10-19 09:51:27 -04:00
|
|
|
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,
|
2018-06-28 11:45:41 -04:00
|
|
|
pdTRUE , pdFALSE, portMAX_DELAY);
|
|
|
|
return (mb_event_group_t)status;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Function to get notification about parameter change from application task
|
2018-10-19 09:51:27 -04:00
|
|
|
static esp_err_t mbc_serial_slave_get_param_info(mb_param_info_t* reg_info, uint32_t timeout)
|
2018-06-28 11:45:41 -04:00
|
|
|
{
|
2018-10-19 09:51:27 -04:00
|
|
|
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;
|
2018-06-28 11:45:41 -04:00
|
|
|
esp_err_t err = ESP_ERR_TIMEOUT;
|
2018-10-19 09:51:27 -04:00
|
|
|
MB_SLAVE_CHECK((mbs_opts->mbs_notification_queue_handle != NULL),
|
2018-06-28 11:45:41 -04:00
|
|
|
ESP_ERR_INVALID_ARG, "mb queue handle is invalid.");
|
2018-10-19 09:51:27 -04:00
|
|
|
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,
|
2018-06-28 11:45:41 -04:00
|
|
|
reg_info, pdMS_TO_TICKS(timeout));
|
|
|
|
if (status == pdTRUE) {
|
|
|
|
err = ESP_OK;
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------- Callback functions for Modbus stack ---------------------------------*/
|
|
|
|
// These are executed by modbus stack to read appropriate type of registers.
|
|
|
|
|
|
|
|
// This is required to suppress warning when register start address is zero
|
|
|
|
#pragma GCC diagnostic ignored "-Wtype-limits"
|
|
|
|
|
2021-01-27 23:29:32 -05:00
|
|
|
// Modbus controller destroy function
|
|
|
|
static esp_err_t mbc_serial_slave_destroy(void)
|
2018-06-28 11:45:41 -04:00
|
|
|
{
|
2020-11-10 02:40:01 -05:00
|
|
|
MB_SLAVE_CHECK((mbs_interface_ptr != NULL),
|
2021-01-27 23:29:32 -05:00
|
|
|
ESP_ERR_INVALID_STATE,
|
|
|
|
"Slave interface is not correctly initialized.");
|
2018-10-19 09:51:27 -04:00
|
|
|
mb_slave_options_t* mbs_opts = &mbs_interface_ptr->opts;
|
2021-01-27 23:29:32 -05:00
|
|
|
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);
|
2021-10-27 23:08:12 -04:00
|
|
|
mbs_interface_ptr = NULL;
|
2021-01-27 23:29:32 -05:00
|
|
|
vMBPortSetMode((UCHAR)MB_PORT_INACTIVE);
|
|
|
|
return ESP_OK;
|
2018-06-28 11:45:41 -04:00
|
|
|
}
|
2018-10-19 09:51:27 -04:00
|
|
|
|
|
|
|
// Initialization of Modbus controller
|
2020-07-21 12:34:04 -04:00
|
|
|
esp_err_t mbc_serial_slave_create(void** handler)
|
2018-10-19 09:51:27 -04:00
|
|
|
{
|
|
|
|
// 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);
|
2020-07-21 12:34:04 -04:00
|
|
|
|
|
|
|
vMBPortSetMode((UCHAR)MB_PORT_SERIAL_SLAVE);
|
|
|
|
|
2018-10-19 09:51:27 -04:00
|
|
|
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
|
2021-07-28 01:45:13 -04:00
|
|
|
status = xTaskCreatePinnedToCore((void*)&modbus_slave_task,
|
2018-10-19 09:51:27 -04:00
|
|
|
"modbus_slave_task",
|
|
|
|
MB_CONTROLLER_STACK_SIZE,
|
|
|
|
NULL,
|
|
|
|
MB_CONTROLLER_PRIORITY,
|
2021-07-28 01:45:13 -04:00
|
|
|
&mbs_opts->mbs_task_handle,
|
|
|
|
MB_PORT_TASK_AFFINITY);
|
2018-10-19 09:51:27 -04:00
|
|
|
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;
|
2021-01-27 23:29:32 -05:00
|
|
|
mbs_interface_ptr->set_descriptor = NULL; // Use common set descriptor function
|
2018-10-19 09:51:27 -04:00
|
|
|
mbs_interface_ptr->setup = mbc_serial_slave_setup;
|
|
|
|
mbs_interface_ptr->start = mbc_serial_slave_start;
|
|
|
|
|
|
|
|
// Initialize stack callback function pointers
|
2021-01-27 23:29:32 -05:00
|
|
|
mbs_interface_ptr->slave_reg_cb_discrete = NULL; // implemented in common layer
|
|
|
|
mbs_interface_ptr->slave_reg_cb_input = NULL;
|
|
|
|
mbs_interface_ptr->slave_reg_cb_holding = NULL;
|
|
|
|
mbs_interface_ptr->slave_reg_cb_coils = NULL;
|
2018-10-19 09:51:27 -04:00
|
|
|
|
|
|
|
*handler = (void*)mbs_interface_ptr;
|
|
|
|
|
|
|
|
return ESP_OK;
|
|
|
|
}
|