mirror of
https://github.com/espressif/esp-modbus.git
synced 2025-08-03 20:34:26 +02:00
update as per review notes
This commit is contained in:
@@ -433,7 +433,9 @@ The function writes characteristic's value defined as a name and cid parameter i
|
||||
ESP_LOGE(TAG, "Set data fail, err = 0x%x (%s).", (int)err, (char*)esp_err_to_name(err));
|
||||
}
|
||||
|
||||
The above functions propagate the status of the transaction as a return error code. This return code does not clarify some information such as slave exception code returned in the response. In this case the below function can be useful.
|
||||
# Extra error information
|
||||
|
||||
In case the does not clarify some information, such as slave exception code returned in the response, the functions below can be useful.
|
||||
|
||||
:cpp:func:`mbc_master_get_transaction_info`
|
||||
|
||||
@@ -507,7 +509,7 @@ Below is the way to expose the transaction info and request/response buffers def
|
||||
|
||||
The user handler function can be useful to check the Modbus frame buffers and expose some information right before returning from the call :cpp:func:`mbc_master_set_parameter` or :cpp:func:`mbc_master_get_parameter` functions.
|
||||
|
||||
.. note:: The above handler function may prevent the Modbus FSM to work properly! The body of the handler needs to be as short as possible and contain just simple functionality that will not block processing for relatively long time. This is user software responcibility to not break the Modbus functionality using the function.
|
||||
.. warning:: The above handler function may prevent the Modbus FSM to work properly! The body of the handler needs to be as short as possible and contain just simple functionality that will not block processing for relatively long time. This is user software responcibility to not break the Modbus functionality using the function.
|
||||
|
||||
.. _modbus_api_master_destroy:
|
||||
|
||||
|
@@ -180,7 +180,7 @@ esp_err_t mbc_master_start(void)
|
||||
}
|
||||
|
||||
eMBErrorCode eMBMasterRegDiscreteCB(UCHAR * pucRegBuffer, USHORT usAddress,
|
||||
USHORT usNDiscrete)
|
||||
USHORT usNDiscrete)
|
||||
{
|
||||
eMBErrorCode error = MB_ENOERR;
|
||||
MB_MASTER_CHECK((master_interface_ptr != NULL),
|
||||
@@ -194,7 +194,7 @@ eMBErrorCode eMBMasterRegDiscreteCB(UCHAR * pucRegBuffer, USHORT usAddress,
|
||||
}
|
||||
|
||||
eMBErrorCode eMBMasterRegCoilsCB(UCHAR* pucRegBuffer, USHORT usAddress,
|
||||
USHORT usNCoils, eMBRegisterMode eMode)
|
||||
USHORT usNCoils, eMBRegisterMode eMode)
|
||||
{
|
||||
eMBErrorCode error = MB_ENOERR;
|
||||
MB_MASTER_CHECK((master_interface_ptr != NULL),
|
||||
@@ -209,7 +209,7 @@ eMBErrorCode eMBMasterRegCoilsCB(UCHAR* pucRegBuffer, USHORT usAddress,
|
||||
}
|
||||
|
||||
eMBErrorCode eMBMasterRegHoldingCB(UCHAR * pucRegBuffer, USHORT usAddress,
|
||||
USHORT usNRegs, eMBRegisterMode eMode)
|
||||
USHORT usNRegs, eMBRegisterMode eMode)
|
||||
{
|
||||
eMBErrorCode error = MB_ENOERR;
|
||||
MB_MASTER_CHECK((master_interface_ptr != NULL),
|
||||
@@ -224,7 +224,7 @@ eMBErrorCode eMBMasterRegHoldingCB(UCHAR * pucRegBuffer, USHORT usAddress,
|
||||
}
|
||||
|
||||
eMBErrorCode eMBMasterRegInputCB(UCHAR * pucRegBuffer, USHORT usAddress,
|
||||
USHORT usNRegs)
|
||||
USHORT usNRegs)
|
||||
{
|
||||
eMBErrorCode error = MB_ENOERR;
|
||||
MB_MASTER_CHECK((master_interface_ptr != NULL),
|
||||
@@ -242,16 +242,14 @@ eMBErrorCode eMBMasterRegInputCB(UCHAR * pucRegBuffer, USHORT usAddress,
|
||||
*/
|
||||
esp_err_t mbc_master_get_transaction_info(mb_trans_info_t *ptinfo)
|
||||
{
|
||||
mb_trans_info_t tinfo = {0};
|
||||
MB_MASTER_CHECK((ptinfo),
|
||||
ESP_ERR_INVALID_ARG,
|
||||
"Wrong argument.");
|
||||
MB_MASTER_CHECK(xMBMasterGetLastTransactionInfo(&tinfo.trans_id, &tinfo.dest_addr,
|
||||
&tinfo.func_code, &tinfo.exception,
|
||||
&tinfo.err_type),
|
||||
MB_MASTER_CHECK(xMBMasterGetLastTransactionInfo(&ptinfo->trans_id, &ptinfo->dest_addr,
|
||||
&ptinfo->func_code, &ptinfo->exception,
|
||||
&ptinfo->err_type),
|
||||
ESP_ERR_INVALID_STATE,
|
||||
"Master can not get transaction info.");
|
||||
*ptinfo = tinfo;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
|
@@ -155,10 +155,10 @@ typedef struct {
|
||||
*/
|
||||
typedef struct {
|
||||
uint64_t trans_id; /*!< Modbus unique transaction identificator */
|
||||
uint16_t err_type; /*!< Modbus last transaction error type */
|
||||
uint8_t dest_addr; /*!< Modbus destination short address (or UID) */
|
||||
uint8_t func_code; /*!< Modbus last transaction function code */
|
||||
uint8_t exception; /*!< Modbus last transaction exception code returned by slave */
|
||||
uint16_t err_type; /*!< Modbus last transaction error type */
|
||||
} mb_trans_info_t;
|
||||
|
||||
/**
|
||||
|
@@ -105,6 +105,12 @@ typedef enum
|
||||
MB_TMODE_CONVERT_DELAY /*!< Master sent broadcast ,then delay sometime.*/
|
||||
} eMBMasterTimerMode;
|
||||
|
||||
extern _lock_t xMBMLock; // Modbus lock object
|
||||
|
||||
#define MB_ATOMIC_SECTION() CRITICAL_SECTION(xMBMLock)
|
||||
#define MB_ATOMIC_STORE(PTR, DES) CRITICAL_STORE(xMBMLock, PTR, DES)
|
||||
#define MB_ATOMIC_LOAD(PTR) CRITICAL_LOAD(xMBMLock, PTR)
|
||||
|
||||
/* ----------------------- Function prototypes ------------------------------*/
|
||||
/*! \ingroup modbus
|
||||
* \brief Initialize the Modbus Master protocol stack.
|
||||
|
@@ -40,6 +40,7 @@
|
||||
|
||||
#include "mbconfig.h" // for options
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
PR_BEGIN_EXTERN_C
|
||||
#endif
|
||||
@@ -92,10 +93,10 @@ typedef enum {
|
||||
} eMBMasterErrorEventType;
|
||||
|
||||
typedef struct _MbEventType {
|
||||
eMBMasterEventEnum eEvent; /*!< event itself. */
|
||||
uint64_t xTransactionId; /*!< ID of the transaction */
|
||||
uint64_t xPostTimestamp; /*!< timestamp of event posted */
|
||||
uint64_t xGetTimestamp; /*!< timestamp of event get */
|
||||
eMBMasterEventEnum eEvent; /*!< event itself. */
|
||||
uint64_t xTransactionId; /*!< ID of the transaction */
|
||||
uint64_t xPostTimestamp; /*!< timestamp of event posted */
|
||||
uint64_t xGetTimestamp; /*!< timestamp of event get */
|
||||
} xMBMasterEventType;
|
||||
|
||||
#endif
|
||||
|
@@ -36,7 +36,6 @@
|
||||
*/
|
||||
|
||||
/* ----------------------- System includes ----------------------------------*/
|
||||
#include <stdatomic.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
@@ -44,7 +43,6 @@
|
||||
#include "port.h"
|
||||
|
||||
/* ----------------------- Modbus includes ----------------------------------*/
|
||||
|
||||
#include "mb_m.h"
|
||||
#include "mbconfig.h"
|
||||
#include "mbframe.h"
|
||||
@@ -71,20 +69,23 @@
|
||||
|
||||
/* ----------------------- Static variables ---------------------------------*/
|
||||
|
||||
static UCHAR ucMBMasterDestAddress = 0;
|
||||
static BOOL xMBRunInMasterMode = FALSE;
|
||||
static volatile eMBMasterErrorEventType eMBMasterCurErrorType = EV_ERROR_INIT;
|
||||
static volatile USHORT usMasterSendPDULength;
|
||||
static volatile eMBMode eMBMasterCurrentMode;
|
||||
static uint64_t xCurTransactionId = 0;
|
||||
|
||||
static UCHAR *pucMBSendFrame = NULL;
|
||||
static UCHAR *pucMBRecvFrame = NULL;
|
||||
static UCHAR ucRecvAddress = 0;
|
||||
static UCHAR ucLastFunctionCode = 0;
|
||||
static UCHAR usLastFrameError = 0;
|
||||
_lock_t xMBMLock; // base modbus object lock
|
||||
|
||||
static UCHAR *pucMBSendFrame = NULL;
|
||||
static UCHAR *pucMBRecvFrame = NULL;
|
||||
static UCHAR ucRecvAddress = 0;
|
||||
|
||||
static BOOL xMBRunInMasterMode =FALSE;
|
||||
static UCHAR ucMBMasterDestAddress = 0;
|
||||
static UCHAR ucLastFunctionCode = 0;
|
||||
static UCHAR usLastFrameError = 0;
|
||||
static eMBException eLastException = MB_EX_NONE;
|
||||
static uint64_t xLastTransactionId = 0;
|
||||
static uint64_t xCurTransactionId = 0;
|
||||
|
||||
/*------------------------ Shared variables ---------------------------------*/
|
||||
|
||||
@@ -192,7 +193,7 @@ eMBMasterTCPInit( USHORT ucTCPPort )
|
||||
eStatus = MB_EPORTERR;
|
||||
}
|
||||
/* initialize the state values. */
|
||||
ucRecvAddress = 0;
|
||||
ucRecvAddress = MB_TCP_PSEUDO_ADDRESS;
|
||||
ucLastFunctionCode = 0;
|
||||
usLastFrameError = 0;
|
||||
eLastException = MB_EX_NONE;
|
||||
@@ -256,7 +257,7 @@ eMBMasterSerialInit( eMBMode eMode, UCHAR ucPort, ULONG ulBaudRate, eMBParity eP
|
||||
{
|
||||
eMBState = STATE_DISABLED;
|
||||
/* initialize the state values. */
|
||||
ucRecvAddress = 0;
|
||||
ucRecvAddress = MB_TCP_PSEUDO_ADDRESS;
|
||||
ucLastFunctionCode = 0;
|
||||
usLastFrameError = 0;
|
||||
eLastException = MB_EX_NONE;
|
||||
@@ -370,7 +371,7 @@ eMBMasterPoll( void )
|
||||
ESP_LOGE( MB_PORT_TAG, "%" PRIu64 ":Frame send error = %d", xEvent.xTransactionId, (unsigned)eStatus );
|
||||
}
|
||||
xCurTransactionId = xEvent.xTransactionId;
|
||||
atomic_store(&(xLastTransactionId), xCurTransactionId);
|
||||
MB_ATOMIC_STORE(&(xLastTransactionId), xCurTransactionId);
|
||||
break;
|
||||
case EV_MASTER_FRAME_SENT:
|
||||
if (xCurTransactionId == xEvent.xTransactionId) {
|
||||
@@ -417,7 +418,7 @@ eMBMasterPoll( void )
|
||||
MB_PORT_CHECK(pucMBRecvFrame, MB_EILLSTATE, "receive buffer initialization fail.");
|
||||
ESP_LOGD(MB_PORT_TAG, "%" PRIu64 ":EV_MASTER_EXECUTE", xEvent.xTransactionId);
|
||||
ucFunctionCode = pucMBRecvFrame[MB_PDU_FUNC_OFF];
|
||||
atomic_store(&(ucLastFunctionCode), ucFunctionCode);
|
||||
MB_ATOMIC_STORE(&(ucLastFunctionCode), ucFunctionCode);
|
||||
eException = MB_EX_ILLEGAL_FUNCTION;
|
||||
/* If receive frame has exception. The receive function code highest bit is 1.*/
|
||||
if (ucFunctionCode & MB_FUNC_ERROR) {
|
||||
@@ -449,7 +450,7 @@ eMBMasterPoll( void )
|
||||
}
|
||||
}
|
||||
}
|
||||
atomic_store(&(eLastException), eException);
|
||||
MB_ATOMIC_STORE(&(eLastException), eException);
|
||||
/* If master has exception, will send error process event. Otherwise the master is idle.*/
|
||||
if ( eException != MB_EX_NONE ) {
|
||||
vMBMasterSetErrorType( EV_ERROR_EXECUTE_FUNCTION );
|
||||
@@ -477,28 +478,28 @@ eMBMasterPoll( void )
|
||||
vMBMasterErrorCBRespondTimeout( xEvent.xTransactionId,
|
||||
ucMBMasterGetDestAddress( ),
|
||||
pucMBSendFrame, usMBMasterGetPDUSndLength( ) );
|
||||
atomic_store(&(usLastFrameError), ( UCHAR )errorType);
|
||||
MB_ATOMIC_STORE(&(usLastFrameError), errorType);
|
||||
break;
|
||||
case EV_ERROR_RECEIVE_DATA:
|
||||
vMBMasterErrorCBReceiveData( xEvent.xTransactionId,
|
||||
ucMBMasterGetDestAddress( ),
|
||||
pucMBRecvFrame, usRecvLength,
|
||||
pucMBSendFrame, usMBMasterGetPDUSndLength( ) );
|
||||
atomic_store(&(usLastFrameError), ( UCHAR )errorType);
|
||||
MB_ATOMIC_STORE(&(usLastFrameError), errorType);
|
||||
break;
|
||||
case EV_ERROR_EXECUTE_FUNCTION:
|
||||
vMBMasterErrorCBExecuteFunction( xEvent.xTransactionId,
|
||||
ucMBMasterGetDestAddress( ),
|
||||
pucMBRecvFrame, usRecvLength,
|
||||
pucMBSendFrame, usMBMasterGetPDUSndLength( ) );
|
||||
atomic_store(&(usLastFrameError), ( UCHAR )errorType);
|
||||
MB_ATOMIC_STORE(&(usLastFrameError), errorType);
|
||||
break;
|
||||
case EV_ERROR_OK:
|
||||
vMBMasterCBRequestSuccess( xEvent.xTransactionId,
|
||||
ucMBMasterGetDestAddress( ),
|
||||
pucMBRecvFrame, usRecvLength,
|
||||
pucMBSendFrame, usMBMasterGetPDUSndLength( ) );
|
||||
atomic_store(&(usLastFrameError), ( UCHAR )errorType);
|
||||
MB_ATOMIC_STORE(&(usLastFrameError), errorType);
|
||||
break;
|
||||
default:
|
||||
ESP_LOGE( MB_PORT_TAG, "%" PRIu64 ":incorrect error type = %d.", xEvent.xTransactionId, (int)errorType);
|
||||
@@ -527,37 +528,37 @@ eMBMasterPoll( void )
|
||||
// Get whether the Modbus Master is run in master mode.
|
||||
BOOL xMBMasterGetCBRunInMasterMode( void )
|
||||
{
|
||||
return atomic_load(&xMBRunInMasterMode);
|
||||
return MB_ATOMIC_LOAD( &xMBRunInMasterMode);
|
||||
}
|
||||
|
||||
// Set whether the Modbus Master is run in master mode.
|
||||
void vMBMasterSetCBRunInMasterMode( BOOL IsMasterMode )
|
||||
{
|
||||
atomic_store(&(xMBRunInMasterMode), IsMasterMode);
|
||||
MB_ATOMIC_STORE(&(xMBRunInMasterMode), IsMasterMode);
|
||||
}
|
||||
|
||||
// Get Modbus Master send destination address.
|
||||
UCHAR ucMBMasterGetDestAddress( void )
|
||||
{
|
||||
return atomic_load(&ucMBMasterDestAddress);
|
||||
return MB_ATOMIC_LOAD( &ucMBMasterDestAddress);
|
||||
}
|
||||
|
||||
// Set Modbus Master send destination address.
|
||||
void vMBMasterSetDestAddress( UCHAR Address )
|
||||
{
|
||||
atomic_store(&(ucMBMasterDestAddress), Address);
|
||||
MB_ATOMIC_STORE(&(ucMBMasterDestAddress), Address);
|
||||
}
|
||||
|
||||
// Get Modbus Master current error event type.
|
||||
eMBMasterErrorEventType inline eMBMasterGetErrorType( void )
|
||||
{
|
||||
return atomic_load(&eMBMasterCurErrorType);
|
||||
return MB_ATOMIC_LOAD(&eMBMasterCurErrorType);
|
||||
}
|
||||
|
||||
// Set Modbus Master current error event type.
|
||||
void IRAM_ATTR vMBMasterSetErrorType( eMBMasterErrorEventType errorType )
|
||||
{
|
||||
atomic_store(&(eMBMasterCurErrorType), errorType);
|
||||
MB_ATOMIC_STORE(&(eMBMasterCurErrorType), errorType);
|
||||
}
|
||||
|
||||
/* Get Modbus Master send PDU's buffer address pointer.*/
|
||||
@@ -569,37 +570,37 @@ void vMBMasterGetPDUSndBuf( UCHAR ** pucFrame )
|
||||
/* Set Modbus Master send PDU's buffer length.*/
|
||||
void vMBMasterSetPDUSndLength( USHORT SendPDULength )
|
||||
{
|
||||
atomic_store(&(usMasterSendPDULength), SendPDULength);
|
||||
MB_ATOMIC_STORE(&(usMasterSendPDULength), SendPDULength);
|
||||
}
|
||||
|
||||
/* Get Modbus Master send PDU's buffer length.*/
|
||||
USHORT usMBMasterGetPDUSndLength( void )
|
||||
{
|
||||
return atomic_load(&usMasterSendPDULength);
|
||||
return MB_ATOMIC_LOAD(&usMasterSendPDULength);
|
||||
}
|
||||
|
||||
/* Set Modbus Master current timer mode.*/
|
||||
void vMBMasterSetCurTimerMode( eMBMasterTimerMode eMBTimerMode )
|
||||
{
|
||||
atomic_store(&(eMasterCurTimerMode), eMBTimerMode);
|
||||
MB_ATOMIC_STORE(&(eMasterCurTimerMode), eMBTimerMode);
|
||||
}
|
||||
|
||||
/* Get Modbus Master current timer mode.*/
|
||||
eMBMasterTimerMode MB_PORT_ISR_ATTR xMBMasterGetCurTimerMode( void )
|
||||
{
|
||||
return atomic_load(&eMasterCurTimerMode);
|
||||
return MB_ATOMIC_LOAD(&eMasterCurTimerMode);
|
||||
}
|
||||
|
||||
/* The master request is broadcast? */
|
||||
BOOL MB_PORT_ISR_ATTR xMBMasterRequestIsBroadcast( void )
|
||||
{
|
||||
return xFrameIsBroadcast;
|
||||
return MB_ATOMIC_LOAD( &xFrameIsBroadcast);
|
||||
}
|
||||
|
||||
/* The master request is broadcast? */
|
||||
void vMBMasterRequestSetType( BOOL xIsBroadcast )
|
||||
{
|
||||
atomic_store(&(xFrameIsBroadcast), xIsBroadcast);
|
||||
MB_ATOMIC_STORE(&(xFrameIsBroadcast), xIsBroadcast);
|
||||
}
|
||||
|
||||
// Get Modbus Master communication mode.
|
||||
@@ -616,11 +617,13 @@ BOOL xMBMasterGetLastTransactionInfo( uint64_t *pxTransId, UCHAR *pucDestAddress
|
||||
BOOL xState = (eMBState == STATE_ENABLED);
|
||||
if (xState && pxTransId && pucDestAddress && pucFunctionCode
|
||||
&& pucException && pusErrorType) {
|
||||
*pxTransId = atomic_load(&xLastTransactionId);
|
||||
*pucDestAddress = ucMBMasterGetDestAddress();
|
||||
*pucFunctionCode = atomic_load(&ucLastFunctionCode);
|
||||
*pucException = (UCHAR) atomic_load(&eLastException);
|
||||
*pusErrorType = atomic_load(&usLastFrameError);
|
||||
MB_ATOMIC_SECTION() {
|
||||
*pxTransId = xLastTransactionId;
|
||||
*pucDestAddress = ucMBMasterDestAddress;
|
||||
*pucFunctionCode = ucLastFunctionCode;
|
||||
*pucException = eLastException;
|
||||
*pusErrorType = usLastFrameError;
|
||||
}
|
||||
}
|
||||
return xState;
|
||||
}
|
||||
|
@@ -39,7 +39,6 @@
|
||||
/* ----------------------- Modbus includes ----------------------------------*/
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/queue.h"
|
||||
#include "sys/lock.h"
|
||||
#include "port.h"
|
||||
|
||||
/* ----------------------- Variables ----------------------------------------*/
|
||||
@@ -47,13 +46,24 @@ static _lock_t s_port_lock;
|
||||
static UCHAR ucPortMode = 0;
|
||||
|
||||
/* ----------------------- Start implementation -----------------------------*/
|
||||
inline void
|
||||
INLINE int lock_obj(_lock_t *plock)
|
||||
{
|
||||
_lock_acquire(plock);
|
||||
return 1;
|
||||
}
|
||||
|
||||
INLINE void unlock_obj(_lock_t *plock)
|
||||
{
|
||||
_lock_release(plock);
|
||||
}
|
||||
|
||||
INLINE void
|
||||
vMBPortEnterCritical(void)
|
||||
{
|
||||
_lock_acquire(&s_port_lock);
|
||||
}
|
||||
|
||||
inline void
|
||||
INLINE void
|
||||
vMBPortExitCritical(void)
|
||||
{
|
||||
_lock_release(&s_port_lock);
|
||||
|
@@ -37,6 +37,8 @@
|
||||
#ifndef PORT_COMMON_H_
|
||||
#define PORT_COMMON_H_
|
||||
|
||||
#include "sys/lock.h"
|
||||
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/queue.h" // for queue
|
||||
|
||||
@@ -52,7 +54,7 @@
|
||||
|
||||
#include "mbconfig.h"
|
||||
|
||||
#define INLINE inline
|
||||
#define INLINE inline __attribute__((always_inline))
|
||||
#define PR_BEGIN_EXTERN_C extern "C" {
|
||||
#define PR_END_EXTERN_C }
|
||||
|
||||
@@ -123,6 +125,57 @@
|
||||
} \
|
||||
} while(0)
|
||||
|
||||
int lock_obj(_lock_t *plock);
|
||||
void unlock_obj(_lock_t *plock);
|
||||
|
||||
#define CRITICAL_SECTION_INIT(lock) \
|
||||
do \
|
||||
{ \
|
||||
_lock_init((_lock_t *)&lock); \
|
||||
} while (0)
|
||||
|
||||
#define CRITICAL_SECTION_CLOSE(lock) \
|
||||
do \
|
||||
{ \
|
||||
_lock_close((_lock_t *)&lock); \
|
||||
} while (0)
|
||||
|
||||
#define CRITICAL_SECTION_LOCK(lock) \
|
||||
do \
|
||||
{ \
|
||||
lock_obj((_lock_t *)&lock); \
|
||||
} while (0)
|
||||
|
||||
#define CRITICAL_SECTION_UNLOCK(lock) \
|
||||
do \
|
||||
{ \
|
||||
unlock_obj((_lock_t *)&lock); \
|
||||
} while (0)
|
||||
|
||||
#define CRITICAL_SECTION(lock) for (int st = lock_obj((_lock_t *)&lock); (st > 0); unlock_obj((_lock_t *)&lock), st = -1)
|
||||
|
||||
#define CRITICAL_STORE(LOCK, PTR, DES) \
|
||||
__extension__ \
|
||||
({ \
|
||||
__auto_type __atomic_ptr = (PTR); \
|
||||
__typeof__ ((void)0, *__atomic_ptr) __atomic_tmp = (DES); \
|
||||
lock_obj((_lock_t *)&LOCK); \
|
||||
*__atomic_ptr = __atomic_tmp; \
|
||||
unlock_obj((_lock_t *)&LOCK); \
|
||||
(__atomic_tmp); \
|
||||
})
|
||||
|
||||
#define CRITICAL_LOAD(LOCK, PTR) \
|
||||
__extension__ \
|
||||
({ \
|
||||
__auto_type __atomic_ptr = (PTR); \
|
||||
__typeof__ ((void)0, *__atomic_ptr) __atomic_tmp; \
|
||||
lock_obj((_lock_t *)&LOCK); \
|
||||
__atomic_tmp = (*__atomic_ptr); \
|
||||
unlock_obj((_lock_t *)&LOCK); \
|
||||
(__atomic_tmp); \
|
||||
})
|
||||
|
||||
#ifdef __cplusplus
|
||||
PR_BEGIN_EXTERN_C
|
||||
#endif /* __cplusplus */
|
||||
@@ -213,9 +266,8 @@ BOOL xMBPortSerialWaitEvent(QueueHandle_t xMbUartQueue, uart_event_t* pxEvent, U
|
||||
* @param pucSendData Send buffer data
|
||||
* @param ucSendLength Send buffer length
|
||||
*/
|
||||
MB_ATTR_WEAK
|
||||
void vMBMasterErrorCBUserHandler( uint64_t xTransId, USHORT usError, UCHAR ucDestAddress, const UCHAR* pucRecvData, USHORT ucRecvLength,
|
||||
const UCHAR* pucSendData, USHORT ucSendLength );
|
||||
const UCHAR* pucSendData, USHORT ucSendLength ) MB_ATTR_WEAK;
|
||||
|
||||
#ifdef __cplusplus
|
||||
PR_END_EXTERN_C
|
||||
|
@@ -36,8 +36,6 @@
|
||||
|
||||
/* ----------------------- Modbus includes ----------------------------------*/
|
||||
|
||||
#include <stdatomic.h>
|
||||
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/event_groups.h"
|
||||
@@ -93,7 +91,7 @@ xMBMasterPortEventPost( eMBMasterEventEnum eEvent)
|
||||
xEvent.xPostTimestamp = esp_timer_get_time();
|
||||
|
||||
if (eEvent & EV_MASTER_TRANS_START) {
|
||||
atomic_store(&(xTransactionID), xEvent.xPostTimestamp);
|
||||
MB_ATOMIC_STORE(&(xTransactionID), xEvent.xPostTimestamp);
|
||||
}
|
||||
xEvent.eEvent = (eEvent & ~EV_MASTER_TRANS_START);
|
||||
|
||||
@@ -120,7 +118,7 @@ xMBMasterPortEventGet(xMBMasterEventType *peEvent)
|
||||
BOOL xEventHappened = FALSE;
|
||||
|
||||
if (xQueueReceive(xQueueMasterHdl, peEvent, portMAX_DELAY) == pdTRUE) {
|
||||
peEvent->xTransactionId = atomic_load(&xTransactionID);
|
||||
peEvent->xTransactionId = MB_ATOMIC_LOAD(&xTransactionID);
|
||||
// Set event bits in confirmation group (for synchronization with port task)
|
||||
xEventGroupSetBits(xEventGroupMasterConfirmHdl, peEvent->eEvent);
|
||||
peEvent->xGetTimestamp = esp_timer_get_time();
|
||||
@@ -147,7 +145,7 @@ xMBMasterPortFsmWaitConfirmation( eMBMasterEventEnum eEventMask, ULONG ulTimeout
|
||||
|
||||
uint64_t xMBMasterPortGetTransactionId( )
|
||||
{
|
||||
return atomic_load(&xTransactionID);
|
||||
return MB_ATOMIC_LOAD(&xTransactionID);
|
||||
}
|
||||
|
||||
// This function is initialize the OS resource for modbus master.
|
||||
@@ -204,8 +202,9 @@ void vMBMasterErrorCBRespondTimeout(uint64_t xTransId, UCHAR ucDestAddress, cons
|
||||
(void)xEventGroupSetBits( xEventGroupMasterHdl, EV_MASTER_ERROR_RESPOND_TIMEOUT );
|
||||
ESP_LOGD(MB_PORT_TAG,"%s:Callback respond timeout.", __func__);
|
||||
if (vMBMasterErrorCBUserHandler) {
|
||||
vMBMasterErrorCBUserHandler( xTransId, (USHORT)EV_ERROR_RESPOND_TIMEOUT,
|
||||
ucDestAddress, NULL, 0,
|
||||
vMBMasterErrorCBUserHandler( xTransId,
|
||||
(USHORT)EV_ERROR_RESPOND_TIMEOUT, ucDestAddress,
|
||||
NULL, 0,
|
||||
pucSendData, ucSendLength );
|
||||
}
|
||||
}
|
||||
@@ -228,9 +227,10 @@ void vMBMasterErrorCBReceiveData(uint64_t xTransId, UCHAR ucDestAddress,
|
||||
(void)xEventGroupSetBits( xEventGroupMasterHdl, EV_MASTER_ERROR_RECEIVE_DATA );
|
||||
ESP_LOGD(MB_PORT_TAG,"%s:Callback receive data failure.", __func__);
|
||||
if (vMBMasterErrorCBUserHandler) {
|
||||
vMBMasterErrorCBUserHandler( xTransId, (USHORT)EV_ERROR_RECEIVE_DATA,
|
||||
ucDestAddress, pucRecvData, ucRecvLength,
|
||||
pucSendData, ucSendLength );
|
||||
vMBMasterErrorCBUserHandler( xTransId,
|
||||
(USHORT)EV_ERROR_RECEIVE_DATA, ucDestAddress,
|
||||
pucRecvData, ucRecvLength,
|
||||
pucSendData, ucSendLength );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -254,8 +254,9 @@ void vMBMasterErrorCBExecuteFunction(uint64_t xTransId, UCHAR ucDestAddress,
|
||||
xEventGroupSetBits( xEventGroupMasterHdl, EV_MASTER_ERROR_EXECUTE_FUNCTION );
|
||||
ESP_LOGD(MB_PORT_TAG,"%s:Callback execute data handler failure.", __func__);
|
||||
if (vMBMasterErrorCBUserHandler) {
|
||||
vMBMasterErrorCBUserHandler( xTransId, (USHORT)EV_ERROR_EXECUTE_FUNCTION,
|
||||
ucDestAddress, pucRecvData, ucRecvLength,
|
||||
vMBMasterErrorCBUserHandler( xTransId,
|
||||
(USHORT)EV_ERROR_EXECUTE_FUNCTION, ucDestAddress,
|
||||
pucRecvData, ucRecvLength,
|
||||
pucSendData, ucSendLength );
|
||||
}
|
||||
}
|
||||
@@ -279,8 +280,9 @@ void vMBMasterCBRequestSuccess(uint64_t xTransId, UCHAR ucDestAddress,
|
||||
(void)xEventGroupSetBits( xEventGroupMasterHdl, EV_MASTER_PROCESS_SUCCESS );
|
||||
ESP_LOGD(MB_PORT_TAG,"%s: Callback request success.", __func__);
|
||||
if (vMBMasterErrorCBUserHandler) {
|
||||
vMBMasterErrorCBUserHandler( xTransId, (USHORT)EV_ERROR_OK,
|
||||
ucDestAddress, pucRecvData, ucRecvLength,
|
||||
vMBMasterErrorCBUserHandler( xTransId,
|
||||
(USHORT)EV_ERROR_OK, ucDestAddress,
|
||||
pucRecvData, ucRecvLength,
|
||||
pucSendData, ucSendLength );
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user