eMBErrorCode eStatus = MB_ENOERR;
( void )ucSlaveAddress;
+ ENTER_CRITICAL_SECTION( );
ucMBLFCharacter = MB_ASCII_DEFAULT_LF;
if( xMBPortSerialInit( ucPort, ulBaudRate, 7, eParity ) != TRUE )
eStatus = MB_EPORTERR;
}
+ EXIT_CRITICAL_SECTION( );
return eStatus;
}
void
eMBASCIIStart( void )
{
+ ENTER_CRITICAL_SECTION( );
vMBPortSerialEnable( TRUE, FALSE );
eRcvState = STATE_RX_IDLE;
+ EXIT_CRITICAL_SECTION( );
/* No special startup required for ASCII. */
( void )xMBPortEventPost( EV_READY );
void
eMBASCIIStop( void )
{
+ ENTER_CRITICAL_SECTION( );
vMBPortSerialEnable( FALSE, FALSE );
vMBPortTimersDisable( );
+ EXIT_CRITICAL_SECTION( );
}
eMBErrorCode
ULONG usTimerT35_50us;
( void )ucSlaveAddress;
+ ENTER_CRITICAL_SECTION( );
/* Modbus RTU uses 8 Databits. */
if( xMBPortSerialInit( ucPort, ulBaudRate, 8, eParity ) != TRUE )
eStatus = MB_EPORTERR;
}
}
+ EXIT_CRITICAL_SECTION( );
return eStatus;
}
void
eMBRTUStart( void )
{
+ ENTER_CRITICAL_SECTION( );
/* Initially the receiver is in the state STATE_RX_INIT. we start
* the timer and if no character is received within t3.5 we change
* to STATE_RX_IDLE. This makes sure that we delay startup of the
vMBPortSerialEnable( TRUE, FALSE );
vMBPortTimersEnable( );
+ EXIT_CRITICAL_SECTION( );
}
void
eMBRTUStop( void )
{
+ ENTER_CRITICAL_SECTION( );
vMBPortSerialEnable( FALSE, FALSE );
vMBPortTimersDisable( );
+ EXIT_CRITICAL_SECTION( );
}
// The lines below are required to suppress GCC warnings about unused but set variable 'xFrameReceived'
#define MB_ENTER_CRITICAL(mux) portENTER_CRITICAL(mux)
#define MB_EXIT_CRITICAL(mux) portEXIT_CRITICAL(mux)
-#define ENTER_CRITICAL_SECTION( ) ( vMBPortEnterCritical() )
-#define EXIT_CRITICAL_SECTION( ) ( vMBPortExitCritical() )
+#define ENTER_CRITICAL_SECTION( ) { ESP_LOGD(MB_PORT_TAG,"%s: Port enter critical.", __func__); \
+ vMBPortEnterCritical(); }
+
+#define EXIT_CRITICAL_SECTION( ) { vMBPortExitCritical(); \
+ ESP_LOGD(MB_PORT_TAG,"%s: Port exit critical", __func__); }
typedef char BOOL;
#include <stdlib.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
-#include <freertos/semphr.h>
/* ----------------------- Modbus includes ----------------------------------*/
#include "mb.h"
#include "mbport.h"
+#include "sys/lock.h"
/* ----------------------- Modbus includes ----------------------------------*/
/* ----------------------- Variables ----------------------------------------*/
-static portMUX_TYPE mb_mutex = portMUX_INITIALIZER_UNLOCKED;
+static _lock_t s_port_lock;
/* ----------------------- Start implementation -----------------------------*/
return bIsWithinException;
}
-void
+inline void
vMBPortEnterCritical( void )
{
- portENTER_CRITICAL(&mb_mutex);
+ _lock_acquire(&s_port_lock);
}
-void
+inline void
vMBPortExitCritical( void )
{
- portEXIT_CRITICAL(&mb_mutex);
+ _lock_release(&s_port_lock);
}
void
void vMBPortSerialEnable(BOOL bRxEnable, BOOL bTxEnable)
{
// This function can be called from xMBRTUTransmitFSM() of different task
- ENTER_CRITICAL_SECTION();
if (bRxEnable) {
//uart_enable_rx_intr(ucUartNumber);
bRxStateEnabled = TRUE;
} else {
bTxStateEnabled = FALSE;
}
- EXIT_CRITICAL_SECTION();
}
static void vMBPortSerialRxPoll(size_t xEventSize)