Merge branch 'bugfix/freemodbus_fix_clang_warnings' into 'master'

freemodbus: fix clang warnings of freemodbus stack

See merge request espressif/esp-idf!5193
This commit is contained in:
Angus Gratton 2019-11-28 07:06:19 +08:00
commit ecaf816c0b
8 changed files with 72 additions and 304 deletions

View file

@ -76,6 +76,8 @@ PR_BEGIN_EXTERN_C
/* ----------------------- Type definitions ---------------------------------*/ /* ----------------------- Type definitions ---------------------------------*/
#ifndef _MB_M_H #ifndef _MB_M_H
#define MB_FUNC_CODE_MAX 127
/*! \ingroup modbus /*! \ingroup modbus
* \brief Modbus serial transmission modes (RTU/ASCII). * \brief Modbus serial transmission modes (RTU/ASCII).
* *

View file

@ -53,7 +53,7 @@ PR_BEGIN_EXTERN_C
#define MB_FUNC_DIAG_GET_COM_EVENT_CNT ( 11 ) #define MB_FUNC_DIAG_GET_COM_EVENT_CNT ( 11 )
#define MB_FUNC_DIAG_GET_COM_EVENT_LOG ( 12 ) #define MB_FUNC_DIAG_GET_COM_EVENT_LOG ( 12 )
#define MB_FUNC_OTHER_REPORT_SLAVEID ( 17 ) #define MB_FUNC_OTHER_REPORT_SLAVEID ( 17 )
#define MB_FUNC_ERROR ( 128 ) #define MB_FUNC_ERROR ( 128u )
/* ----------------------- Type definitions ---------------------------------*/ /* ----------------------- Type definitions ---------------------------------*/
typedef enum typedef enum
{ {

View file

@ -229,7 +229,7 @@ eMBRegisterCB( UCHAR ucFunctionCode, pxMBFunctionHandler pxHandler )
int i; int i;
eMBErrorCode eStatus; eMBErrorCode eStatus;
if( ( 0 < ucFunctionCode ) && ( ucFunctionCode <= 127 ) ) if( ( 0 < ucFunctionCode ) && ( ucFunctionCode <= MB_FUNC_CODE_MAX ) )
{ {
ENTER_CRITICAL_SECTION( ); ENTER_CRITICAL_SECTION( );
if( pxHandler != NULL ) if( pxHandler != NULL )
@ -332,7 +332,7 @@ eMBDisable( void )
eMBErrorCode eMBErrorCode
eMBPoll( void ) eMBPoll( void )
{ {
static UCHAR *ucMBFrame; static UCHAR *ucMBFrame = NULL;
static UCHAR ucRcvAddress; static UCHAR ucRcvAddress;
static UCHAR ucFunctionCode; static UCHAR ucFunctionCode;
static USHORT usLength; static USHORT usLength;
@ -373,8 +373,10 @@ eMBPoll( void )
break; break;
case EV_EXECUTE: case EV_EXECUTE:
ESP_LOGD(MB_PORT_TAG, "%s:EV_EXECUTE", __func__); if ( !ucMBFrame ) {
ucFunctionCode = ucMBFrame[MB_PDU_FUNC_OFF]; return MB_EILLSTATE;
}
ESP_LOGD(MB_PORT_TAG, "%s:EV_EXECUTE", __func__); ucFunctionCode = ucMBFrame[MB_PDU_FUNC_OFF];
eException = MB_EX_ILLEGAL_FUNCTION; eException = MB_EX_ILLEGAL_FUNCTION;
for( i = 0; i < MB_FUNC_HANDLERS_MAX; i++ ) for( i = 0; i < MB_FUNC_HANDLERS_MAX; i++ )
{ {
@ -416,7 +418,10 @@ eMBPoll( void )
case EV_FRAME_SENT: case EV_FRAME_SENT:
ESP_LOGD(MB_PORT_TAG, "%s:EV_FRAME_SENT", __func__); ESP_LOGD(MB_PORT_TAG, "%s:EV_FRAME_SENT", __func__);
break; break;
default:
break;
} }
} }
return MB_ENOERR; return eStatus;
} }

View file

@ -44,17 +44,17 @@
#include "mbfunc.h" #include "mbfunc.h"
#include "mbport.h" #include "mbport.h"
#if MB_MASTER_RTU_ENABLED == 1 #if MB_MASTER_RTU_ENABLED
#include "mbrtu.h" #include "mbrtu.h"
#endif #endif
#if MB_MASTER_ASCII_ENABLED == 1 #if MB_MASTER_ASCII_ENABLED
#include "mbascii.h" #include "mbascii.h"
#endif #endif
#if MB_MASTER_TCP_ENABLED == 1 #if MB_MASTER_TCP_ENABLED
#include "mbtcp.h" #include "mbtcp.h"
#endif #endif
#if MB_MASTER_RTU_ENABLED > 0 || MB_MASTER_ASCII_ENABLED > 0 #if MB_MASTER_RTU_ENABLED || MB_MASTER_ASCII_ENABLED
#ifndef MB_PORT_HAS_CLOSE #ifndef MB_PORT_HAS_CLOSE
#define MB_PORT_HAS_CLOSE 1 #define MB_PORT_HAS_CLOSE 1
@ -264,25 +264,28 @@ eMBMasterDisable( void )
eMBErrorCode eMBErrorCode
eMBMasterPoll( void ) eMBMasterPoll( void )
{ {
static UCHAR *ucMBFrame; static UCHAR *ucMBFrame = NULL;
static UCHAR ucRcvAddress; static UCHAR ucRcvAddress;
static UCHAR ucFunctionCode; static UCHAR ucFunctionCode;
static USHORT usLength; static USHORT usLength;
static eMBException eException; static eMBException eException;
int i , j; int i;
int j;
eMBErrorCode eStatus = MB_ENOERR; eMBErrorCode eStatus = MB_ENOERR;
eMBMasterEventType eEvent; eMBMasterEventType eEvent;
eMBMasterErrorEventType errorType; eMBMasterErrorEventType errorType;
/* Check if the protocol stack is ready. */ /* Check if the protocol stack is ready. */
if( eMBState != STATE_ENABLED ) { if( eMBState != STATE_ENABLED )
{
return MB_EILLSTATE; return MB_EILLSTATE;
} }
/* Check if there is a event available. If not return control to caller. /* Check if there is a event available. If not return control to caller.
* Otherwise we will handle the event. */ * Otherwise we will handle the event. */
if( xMBMasterPortEventGet( &eEvent ) == TRUE ) { if( xMBMasterPortEventGet( &eEvent ) == TRUE )
{
switch ( eEvent ) switch ( eEvent )
{ {
case EV_MASTER_NO_EVENT: case EV_MASTER_NO_EVENT:
@ -308,40 +311,57 @@ eMBMasterPoll( void )
case EV_MASTER_FRAME_RECEIVED: case EV_MASTER_FRAME_RECEIVED:
eStatus = peMBMasterFrameReceiveCur( &ucRcvAddress, &ucMBFrame, &usLength ); eStatus = peMBMasterFrameReceiveCur( &ucRcvAddress, &ucMBFrame, &usLength );
// Check if the frame is for us. If not ,send an error process event. // Check if the frame is for us. If not ,send an error process event.
if ( ( eStatus == MB_ENOERR ) && ( ucRcvAddress == ucMBMasterGetDestAddress() ) ) { if ( ( eStatus == MB_ENOERR ) && ( ucRcvAddress == ucMBMasterGetDestAddress() ) )
{
ESP_LOGD(MB_PORT_TAG, "%s: Packet data received successfully (%u).", __func__, eStatus); ESP_LOGD(MB_PORT_TAG, "%s: Packet data received successfully (%u).", __func__, eStatus);
( void ) xMBMasterPortEventPost( EV_MASTER_EXECUTE ); ( void ) xMBMasterPortEventPost( EV_MASTER_EXECUTE );
} else { }
else
{
vMBMasterSetErrorType(EV_ERROR_RECEIVE_DATA); vMBMasterSetErrorType(EV_ERROR_RECEIVE_DATA);
ESP_LOGD(MB_PORT_TAG, "%s: Packet data receive failed (addr=%u)(%u).", __func__, ucRcvAddress, eStatus); ESP_LOGD(MB_PORT_TAG, "%s: Packet data receive failed (addr=%u)(%u).", __func__, ucRcvAddress, eStatus);
( void ) xMBMasterPortEventPost( EV_MASTER_ERROR_PROCESS ); ( void ) xMBMasterPortEventPost( EV_MASTER_ERROR_PROCESS );
} }
break; break;
case EV_MASTER_EXECUTE: case EV_MASTER_EXECUTE:
if ( !ucMBFrame )
{
return MB_EILLSTATE;
}
ESP_LOGD(MB_PORT_TAG, "%s:EV_MASTER_EXECUTE", __func__); ESP_LOGD(MB_PORT_TAG, "%s:EV_MASTER_EXECUTE", __func__);
ucFunctionCode = ucMBFrame[MB_PDU_FUNC_OFF]; ucFunctionCode = ucMBFrame[MB_PDU_FUNC_OFF];
eException = MB_EX_ILLEGAL_FUNCTION; eException = MB_EX_ILLEGAL_FUNCTION;
/* If receive frame has exception. The receive function code highest bit is 1.*/ /* If receive frame has exception. The receive function code highest bit is 1.*/
if(ucFunctionCode >> 7) { if (ucFunctionCode & MB_FUNC_ERROR)
{
eException = (eMBException)ucMBFrame[MB_PDU_DATA_OFF]; eException = (eMBException)ucMBFrame[MB_PDU_DATA_OFF];
} else { }
for (i = 0; i < MB_FUNC_HANDLERS_MAX; i++) { else
{
for (i = 0; i < MB_FUNC_HANDLERS_MAX; i++)
{
/* No more function handlers registered. Abort. */ /* No more function handlers registered. Abort. */
if (xMasterFuncHandlers[i].ucFunctionCode == 0) { if (xMasterFuncHandlers[i].ucFunctionCode == 0)
{
break; break;
} }
else if (xMasterFuncHandlers[i].ucFunctionCode == ucFunctionCode) { if (xMasterFuncHandlers[i].ucFunctionCode == ucFunctionCode)
{
vMBMasterSetCBRunInMasterMode(TRUE); vMBMasterSetCBRunInMasterMode(TRUE);
/* If master request is broadcast, /* If master request is broadcast,
* the master need execute function for all slave. * the master need execute function for all slave.
*/ */
if ( xMBMasterRequestIsBroadcast() ) { if ( xMBMasterRequestIsBroadcast() )
{
usLength = usMBMasterGetPDUSndLength(); usLength = usMBMasterGetPDUSndLength();
for(j = 1; j <= MB_MASTER_TOTAL_SLAVE_NUM; j++){ for(j = 1; j <= MB_MASTER_TOTAL_SLAVE_NUM; j++)
{
vMBMasterSetDestAddress(j); vMBMasterSetDestAddress(j);
eException = xMasterFuncHandlers[i].pxHandler(ucMBFrame, &usLength); eException = xMasterFuncHandlers[i].pxHandler(ucMBFrame, &usLength);
} }
} else { }
else
{
eException = xMasterFuncHandlers[i].pxHandler(ucMBFrame, &usLength); eException = xMasterFuncHandlers[i].pxHandler(ucMBFrame, &usLength);
} }
vMBMasterSetCBRunInMasterMode(FALSE); vMBMasterSetCBRunInMasterMode(FALSE);
@ -350,10 +370,13 @@ eMBMasterPoll( void )
} }
} }
/* If master has exception ,Master will send error process.Otherwise the Master is idle.*/ /* If master has exception ,Master will send error process.Otherwise the Master is idle.*/
if (eException != MB_EX_NONE) { if (eException != MB_EX_NONE)
{
vMBMasterSetErrorType(EV_ERROR_EXECUTE_FUNCTION); vMBMasterSetErrorType(EV_ERROR_EXECUTE_FUNCTION);
( void ) xMBMasterPortEventPost( EV_MASTER_ERROR_PROCESS ); ( void ) xMBMasterPortEventPost( EV_MASTER_ERROR_PROCESS );
} else { }
else
{
vMBMasterCBRequestSuccess( ); vMBMasterCBRequestSuccess( );
vMBMasterRunResRelease( ); vMBMasterRunResRelease( );
} }
@ -363,10 +386,10 @@ eMBMasterPoll( void )
/* Master is busy now. */ /* Master is busy now. */
vMBMasterGetPDUSndBuf( &ucMBFrame ); vMBMasterGetPDUSndBuf( &ucMBFrame );
eStatus = peMBMasterFrameSendCur( ucMBMasterGetDestAddress(), ucMBFrame, usMBMasterGetPDUSndLength() ); eStatus = peMBMasterFrameSendCur( ucMBMasterGetDestAddress(), ucMBFrame, usMBMasterGetPDUSndLength() );
if (eStatus != MB_ENOERR) { if (eStatus != MB_ENOERR)
{
ESP_LOGD(MB_PORT_TAG, "%s:Frame send error. %d", __func__, eStatus); ESP_LOGD(MB_PORT_TAG, "%s:Frame send error. %d", __func__, eStatus);
} }
break; break;
case EV_MASTER_FRAME_SENT: case EV_MASTER_FRAME_SENT:
ESP_LOGD(MB_PORT_TAG, "%s:EV_MASTER_FRAME_SENT", __func__); ESP_LOGD(MB_PORT_TAG, "%s:EV_MASTER_FRAME_SENT", __func__);
@ -396,6 +419,9 @@ eMBMasterPoll( void )
} }
vMBMasterRunResRelease(); vMBMasterRunResRelease();
break; break;
default:
ESP_LOGD(MB_PORT_TAG, "%s: incorrect event triggered = %d.", __func__, eEvent);
break;
} }
} else { } else {
// xMBMasterPortEventGet has unbloked the task but the event bits are not set // xMBMasterPortEventGet has unbloked the task but the event bits are not set
@ -465,13 +491,13 @@ void vMBMasterSetCurTimerMode( eMBMasterTimerMode eMBTimerMode )
} }
/* Get Modbus Master current timer mode.*/ /* Get Modbus Master current timer mode.*/
eMBMasterTimerMode xMBMasterGetCurTimerMode( void ) eMBMasterTimerMode MB_PORT_ISR_ATTR xMBMasterGetCurTimerMode( void )
{ {
return eMasterCurTimerMode; return eMasterCurTimerMode;
} }
/* The master request is broadcast? */ /* The master request is broadcast? */
BOOL xMBMasterRequestIsBroadcast( void ){ BOOL MB_PORT_ISR_ATTR xMBMasterRequestIsBroadcast( void ){
return xFrameIsBroadcast; return xFrameIsBroadcast;
} }
@ -480,5 +506,4 @@ void vMBMasterRequestSetType( BOOL xIsBroadcast ){
xFrameIsBroadcast = xIsBroadcast; xFrameIsBroadcast = xIsBroadcast;
} }
#endif // MB_MASTER_RTU_ENABLED || MB_MASTER_ASCII_ENABLED
#endif // MB_MASTER_RTU_ENABLED > 0 || MB_MASTER_ASCII_ENABLED > 0

View file

@ -362,7 +362,7 @@ BOOL MB_PORT_ISR_ATTR xMBMasterRTUTimerExpired(void)
/* An error occured while receiving the frame. */ /* An error occured while receiving the frame. */
case STATE_M_RX_ERROR: case STATE_M_RX_ERROR:
vMBMasterSetErrorType(EV_ERROR_RECEIVE_DATA); vMBMasterSetErrorType(EV_ERROR_RECEIVE_DATA);
xNeedPoll = xMBMasterPortEventPost( EV_MASTER_ERROR_PROCESS ); xNeedPoll = xMBMasterPortEventPost(EV_MASTER_ERROR_PROCESS);
break; break;
/* Function called in an illegal state. */ /* Function called in an illegal state. */
@ -393,7 +393,7 @@ BOOL MB_PORT_ISR_ATTR xMBMasterRTUTimerExpired(void)
vMBMasterPortTimersDisable( ); vMBMasterPortTimersDisable( );
/* If timer mode is convert delay, the master event then turns EV_MASTER_EXECUTE status. */ /* If timer mode is convert delay, the master event then turns EV_MASTER_EXECUTE status. */
if (xMBMasterGetCurTimerMode() == MB_TMODE_CONVERT_DELAY) { if (xMBMasterGetCurTimerMode() == MB_TMODE_CONVERT_DELAY) {
xNeedPoll = xMBMasterPortEventPost( EV_MASTER_EXECUTE ); xNeedPoll = xMBMasterPortEventPost(EV_MASTER_EXECUTE);
} }
return xNeedPoll; return xNeedPoll;

View file

@ -119,18 +119,18 @@ xMBMasterPortEventGet( eMBMasterEventType * eEvent)
EventBits_t uxBits; EventBits_t uxBits;
BOOL xEventHappened = FALSE; BOOL xEventHappened = FALSE;
uxBits = xEventGroupWaitBits( uxBits = xEventGroupWaitBits(
xEventGroupMasterHdl, // The event group being tested. xEventGroupMasterHdl, // The event group being tested.
MB_EVENT_POLL_MASK, // The bits within the event group to wait for. MB_EVENT_POLL_MASK, // The bits within the event group to wait for.
pdTRUE, // Masked bits should be cleared before returning. pdTRUE, // Masked bits should be cleared before returning.
pdFALSE, // Don't wait for both bits, either bit will do. pdFALSE, // Don't wait for both bits, either bit will do.
portMAX_DELAY); // Wait forever for either bit to be set. portMAX_DELAY); // Wait forever for either bit to be set.
// Check if poll event is correct // Check if poll event is correct
if (uxBits & MB_EVENT_POLL_MASK) { if (uxBits & MB_EVENT_POLL_MASK) {
*eEvent = (eMBMasterEventType)(uxBits); *eEvent = (eMBMasterEventType)(uxBits);
xEventHappened = TRUE; xEventHappened = TRUE;
} else { } else {
ESP_LOGE(MB_PORT_TAG,"%s: Incorrect event triggered.", __func__); ESP_LOGE(MB_PORT_TAG,"%s: Incorrect event triggered = %d.", __func__, uxBits);
xEventHappened = FALSE; xEventHappened = FALSE;
} }
return xEventHappened; return xEventHappened;

View file

@ -1,254 +0,0 @@
/*
* ESPRESSIF MIT License
*
* Copyright (c) 2018 <ESPRESSIF SYSTEMS (SHANGHAI) PTE LTD>
*
* Permission is hereby granted for use on all ESPRESSIF SYSTEMS products, in which case,
* it is free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
// This module allows to hide all Modbus dependencies and work with characteristics
// instead of Modbus parameters
#include "stdlib.h"
#include "string.h" // for memset, memcpy functions
#include "esp_err.h"
#include "esp_log.h"
#include "sense_modbus.h" // for Modbus defines
// This module provide an easy way to work with characteristics instead of
// Modbus parameters
static const char* TAG = "SENSE_MB";
#define SENSE_MB_CHECK(a, ret_val, str, ...) \
if (!(a)) { \
ESP_LOGE(TAG, "%s(%u): " str, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
return (ret_val); \
}
// Define port options for the master application
#define MB_BAUDRATE 115200
#define MB_PORTNUM 2
#define MB_PARITY UART_PARITY_DISABLE
// Keep the pointer to active characteristic table and its size
static characteristic_descriptor_t** active_cid_table = NULL;
static uint32_t active_cid_table_size = 0;
// The function to get pointer to parameter storage (instance) according to parameter description table
static void* sense_modbus_get_param_data(const mb_parameter_descriptor_t* param_descriptor)
{
assert(param_descriptor != NULL);
void* instance_ptr = NULL;
if (param_descriptor->param_offset != 0) {
switch(param_descriptor->mb_param_type)
{
case MB_PARAM_HOLDING:
instance_ptr = (void*)((uint32_t)&holding_reg_params + param_descriptor->param_offset - 1);
break;
case MB_PARAM_INPUT:
instance_ptr = (void*)((uint32_t)&input_reg_params + param_descriptor->param_offset - 1);
break;
case MB_PARAM_COIL:
instance_ptr = (void*)((uint32_t)&coil_reg_params + param_descriptor->param_offset - 1);
break;
case MB_PARAM_DISCRETE:
instance_ptr = (void*)((uint32_t)&discrete_reg_params + param_descriptor->param_offset - 1);
break;
default:
instance_ptr = NULL;
break;
}
} else {
instance_ptr = malloc((size_t)(param_descriptor->param_size));;
}
return instance_ptr;
}
// The helper function to get time stamp in microseconds
static uint64_t sense_modbus_get_time_stamp_us(void)
{
uint64_t time_stamp = esp_timer_get_time();
return time_stamp;
}
// Initialization of Modbus stack
esp_err_t sense_modbus_init(void)
{
mb_communication_info_t comm = {
.port = MB_PORTNUM,
.mode = MB_MODE_RTU,
.baudrate = MB_BAUDRATE,
.parity = MB_PARITY
};
void* master_handler = NULL;
esp_err_t err = mbc_master_init(MB_PORT_SERIAL_MASTER, &master_handler);
SENSE_MB_CHECK((master_handler != NULL), ESP_ERR_INVALID_STATE,
"mb controller initialization fail.");
SENSE_MB_CHECK((err == ESP_OK), ESP_ERR_INVALID_STATE,
"mb controller initialization fail, returns(0x%x).",
(uint32_t)err);
err = mbc_master_setup((void*)&comm);
SENSE_MB_CHECK((err == ESP_OK), ESP_ERR_INVALID_STATE,
"mb controller setup fail, returns(0x%x).",
(uint32_t)err);
err = mbc_master_start();
SENSE_MB_CHECK((err == ESP_OK), ESP_ERR_INVALID_STATE,
"mb controller start fail, returns(0x%x).",
(uint32_t)err);
// Set UART pin numbers
err = uart_set_pin(MB_PORTNUM, CONFIG_MB_UART_TXD, CONFIG_MB_UART_RXD,
CONFIG_MB_UART_RTS, UART_PIN_NO_CHANGE);
SENSE_MB_CHECK((err == ESP_OK), ESP_ERR_INVALID_STATE,
"mb serial set pin failure, uart_set_pin() returned (0x%x).", (uint32_t)err);
// Set driver mode to Half Duplex
err = uart_set_mode(MB_PORTNUM, UART_MODE_RS485_HALF_DUPLEX);
SENSE_MB_CHECK((err == ESP_OK), ESP_ERR_INVALID_STATE,
"mb serial set mode failure, uart_set_mode() returned (0x%x).", (uint32_t)err);
vTaskDelay(5);
err = mbc_master_set_descriptor(&device_parameters[0], num_device_parameters);
SENSE_MB_CHECK((err == ESP_OK), ESP_ERR_INVALID_STATE,
"mb controller set descriptor fail, returns(0x%x).",
(uint32_t)err);
return err;
}
// Check Modbus parameters description table and create characteristic description table according to it
esp_err_t sense_modbus_get_characteristics(characteristic_descriptor_t** cid_table, uint16_t* table_size)
{
SENSE_MB_CHECK((table_size != NULL), ESP_ERR_INVALID_ARG, "incorrect cid table size.");
SENSE_MB_CHECK((*table_size >= 1), ESP_ERR_NOT_FOUND, "incorrect cid of characteristic.");
SENSE_MB_CHECK((cid_table != NULL), ESP_ERR_INVALID_ARG, "incorrect cid table pointer.");
esp_err_t error_code = ESP_OK;
const mb_parameter_descriptor_t* param_descriptor = NULL;
characteristic_descriptor_t* cid_instance_ptr = NULL;
uint16_t cid_cnt = 0;
for (cid_cnt = 0; (error_code != ESP_ERR_NOT_FOUND) && (cid_cnt < *table_size); cid_cnt++) {
// Get data from parameters description table
// and use this information to fill the characteristics description table
// and having all required fields in just one table
error_code = mbc_master_get_cid_info(cid_cnt, &param_descriptor);
if ((error_code != ESP_ERR_NOT_FOUND) && (param_descriptor != NULL)) {
void* temp_data_ptr = sense_modbus_get_param_data(param_descriptor);
SENSE_MB_CHECK((temp_data_ptr != NULL),
ESP_ERR_INVALID_STATE, "incorrect instance pointer for cid.");
// Allocate space for characteristic description
cid_instance_ptr = (characteristic_descriptor_t*)malloc(sizeof(characteristic_descriptor_t));
// If not enough memory, try to repair from this error
SENSE_MB_CHECK((cid_instance_ptr != NULL),
ESP_ERR_NO_MEM, "incorrect memory allocation for the cid.");
cid_table[cid_cnt] = cid_instance_ptr;
// Fill the description according to parameter description table
cid_instance_ptr->instance_ptr = temp_data_ptr;
cid_instance_ptr->param_opts = param_descriptor->param_opts;
cid_instance_ptr->cid = param_descriptor->cid;
cid_instance_ptr->access = param_descriptor->access;
cid_instance_ptr->param_key = param_descriptor->param_key;
cid_instance_ptr->param_units = param_descriptor->param_units;
cid_instance_ptr->status = ESP_FAIL;
cid_instance_ptr->instance_size = (size_t)param_descriptor->param_size;
cid_instance_ptr->instance_type = param_descriptor->param_type;
cid_instance_ptr->change_flag = 0; // clear flag
cid_instance_ptr->timestamp = 0; // Keeps timestamp of updated value
assert(cid_instance_ptr->param_key != NULL);
} else {
break;
}
}
ESP_LOGI(TAG, "%s: Found (%u) characteristics in table.", __FUNCTION__, cid_cnt);
// Keep the table pointer and size of table
active_cid_table = cid_table;
active_cid_table_size = cid_cnt;
*table_size = cid_cnt;
return ESP_OK;
}
// Read characteristic value from Modbus parameter according to description table
esp_err_t sense_modbus_read_value(uint16_t cid, void *value)
{
assert(active_cid_table != NULL);
SENSE_MB_CHECK((value != NULL), ESP_ERR_INVALID_ARG, "incorrect value pointer.");
SENSE_MB_CHECK((cid < active_cid_table_size), ESP_ERR_INVALID_ARG, "incorrect cid to read.");
characteristic_descriptor_t* cid_info = active_cid_table[cid];
SENSE_MB_CHECK((cid_info->instance_ptr != NULL), ESP_ERR_INVALID_STATE, "incorrect cid table instance_pointer.");
SENSE_MB_CHECK((cid_info->param_key != NULL), ESP_ERR_INVALID_STATE, "incorrect cid table or not initialized.");
SENSE_MB_CHECK((cid_info->cid == cid), ESP_ERR_INVALID_STATE, "incorrect cid table or not initialized.");
// Set the data pointer for get request to value instance
uint8_t* value_ptr = (uint8_t*)cid_info->instance_ptr;
uint8_t type = 0;
memset((void*)value_ptr, 0, cid_info->instance_size); // Clear value instance first
// Send Modbus request to read cid correspond registers
esp_err_t error = mbc_master_get_parameter(cid, (char*)cid_info->param_key, value_ptr, &type);
SENSE_MB_CHECK((type <= PARAM_TYPE_ASCII), ESP_ERR_NOT_SUPPORTED, "returned data type is not supported (%u)", type);
cid_info->status = error; // Keep last read status of the cid in the information table
if (error == ESP_OK) {
// Copy the value to param
memcpy((void*)value, (void*)value_ptr, cid_info->instance_size);
cid_info->timestamp = sense_modbus_get_time_stamp_us(); // Set timestamp of last access to cid
}
return error;
}
// Write characteristic value into associated Modbus parameter
esp_err_t sense_modbus_send_value(uint16_t cid, void* value)
{
assert(active_cid_table != NULL);
SENSE_MB_CHECK((cid < active_cid_table_size), ESP_ERR_INVALID_ARG, "incorrect cid to set.");
characteristic_descriptor_t* cid_info = active_cid_table[cid];
SENSE_MB_CHECK((cid_info->instance_ptr != NULL), ESP_ERR_INVALID_STATE, "incorrect cid table instance_ptr.");
SENSE_MB_CHECK((cid_info->param_key != NULL), ESP_ERR_INVALID_STATE, "incorrect cid table param_key or not initialized.");
SENSE_MB_CHECK((cid_info->cid == cid), ESP_ERR_INVALID_STATE, "incorrect cid in table or not initialized.");
uint8_t* value_ptr = (uint8_t*)cid_info->instance_ptr;
// Set the instance value of the characteristic to the value
memcpy((void*)value_ptr, (void*)value, cid_info->instance_size);
uint8_t type = 0;
// Set modbus parameter according to value of characteristic
esp_err_t error = mbc_master_set_parameter(cid, (char*)cid_info->param_key, value_ptr, &type);
SENSE_MB_CHECK((cid_info->instance_type == type), ESP_ERR_NOT_FOUND, "incorrect type of parameter (%u)", type);
SENSE_MB_CHECK((type <= PARAM_TYPE_ASCII), ESP_ERR_NOT_SUPPORTED, "returned data type is not supported (%u)", type);
cid_info->change_flag = 0x55; // Set value changed flag to inform higher level
cid_info->status = error; // Keep last read/write status of cid in the information table
cid_info->timestamp = sense_modbus_get_time_stamp_us(); // Set time stamp of last access to cid
return error;
}
// Get cid data from characteristic description table
esp_err_t sense_modbus_get_cid_data(uint16_t cid, characteristic_descriptor_t* cid_data)
{
assert(active_cid_table != NULL);
SENSE_MB_CHECK((cid_data != NULL), ESP_ERR_INVALID_ARG, "incorrect data pointer.");
SENSE_MB_CHECK((cid < active_cid_table_size), ESP_ERR_INVALID_ARG, "cid is not found in the table.");
characteristic_descriptor_t* cid_info = active_cid_table[cid];
SENSE_MB_CHECK((cid_info != NULL), ESP_ERR_INVALID_STATE, "incorrect cid table or not initialized.");
SENSE_MB_CHECK((cid_info->instance_ptr != NULL), ESP_ERR_INVALID_STATE, "incorrect cid table instance_ptr.");
SENSE_MB_CHECK((cid_info->param_key != NULL), ESP_ERR_INVALID_STATE, "incorrect cid table param_key.");
SENSE_MB_CHECK((cid_info->cid == cid), ESP_ERR_INVALID_STATE, "incorrect cid in the table or not initialized.");
*cid_data = *cid_info; // Set cid data
cid_info->change_flag = 0x00; // Reset flag once we get changed value
return ESP_OK;
}
// Todo: add modbus code here

View file

@ -1,10 +0,0 @@
#
# Modbus configuration
#
CONFIG_MB_UART_RXD=22
CONFIG_MB_UART_TXD=23
CONFIG_MB_UART_RTS=18
CONFIG_FMB_TIMER_PORT_ENABLED=y
CONFIG_FMB_TIMER_GROUP=0
CONFIG_FMB_TIMER_INDEX=0
CONFIG_FMB_TIMER_ISR_IN_IRAM=y