diff --git a/FreeModbus/modbus/functions/mbfunccoils_m.c b/FreeModbus/modbus/functions/mbfunccoils_m.c index 4d7dd38..9a69322 100644 --- a/FreeModbus/modbus/functions/mbfunccoils_m.c +++ b/FreeModbus/modbus/functions/mbfunccoils_m.c @@ -80,7 +80,7 @@ eMBMasterReqReadCoils( UCHAR ucSndAddr, USHORT usCoilAddr, USHORT usNCoils ,LONG UCHAR *ucMBFrame; eMBMasterReqErrCode eErrStatus = MB_MRE_NO_ERR; - if ( xMBasterRunMutexLock( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY; + if ( xMBasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY; else if ( ucSndAddr > MB_MASTER_TOTAL_SLAVE_NUM ) eErrStatus = MB_MRE_ILL_ARG; else { @@ -167,7 +167,7 @@ eMBMasterReqWriteCoil( UCHAR ucSndAddr, USHORT usCoilAddr, USHORT usCoilData, LO UCHAR *ucMBFrame; eMBMasterReqErrCode eErrStatus = MB_MRE_NO_ERR; - if ( xMBasterRunMutexLock( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY; + if ( xMBasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY; else if ( usCoilAddr > MB_MASTER_TOTAL_SLAVE_NUM ) eErrStatus = MB_MRE_ILL_ARG; else if ( ( usCoilData != 0xFF00 ) && ( usCoilData != 0x0000 ) ) eErrStatus = MB_MRE_ILL_ARG; else @@ -249,7 +249,7 @@ eMBMasterReqWriteMultipleCoils( UCHAR ucSndAddr, UCHAR ucByteCount; eMBMasterReqErrCode eErrStatus = MB_MRE_NO_ERR; - if ( xMBasterRunMutexLock( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY; + if ( xMBasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY; else if ( ucSndAddr > MB_MASTER_TOTAL_SLAVE_NUM ) eErrStatus = MB_MRE_ILL_ARG; else if ( usNCoils > MB_PDU_REQ_WRITE_MUL_COILCNT_MAX ) eErrStatus = MB_MRE_ILL_ARG; else diff --git a/FreeModbus/modbus/functions/mbfuncdisc_m.c b/FreeModbus/modbus/functions/mbfuncdisc_m.c index 1614858..0aca128 100644 --- a/FreeModbus/modbus/functions/mbfuncdisc_m.c +++ b/FreeModbus/modbus/functions/mbfuncdisc_m.c @@ -65,7 +65,7 @@ eMBMasterReqReadDiscreteInputs( UCHAR ucSndAddr, USHORT usDiscreteAddr, USHORT u UCHAR *ucMBFrame; eMBMasterReqErrCode eErrStatus = MB_MRE_NO_ERR; - if ( xMBasterRunMutexLock( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY; + if ( xMBasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY; else if ( ucSndAddr > MB_MASTER_TOTAL_SLAVE_NUM ) eErrStatus = MB_MRE_ILL_ARG; else { diff --git a/FreeModbus/modbus/functions/mbfuncholding_m.c b/FreeModbus/modbus/functions/mbfuncholding_m.c index cce53c0..25778a0 100644 --- a/FreeModbus/modbus/functions/mbfuncholding_m.c +++ b/FreeModbus/modbus/functions/mbfuncholding_m.c @@ -92,7 +92,7 @@ eMBMasterReqWriteHoldingRegister( UCHAR ucSndAddr, USHORT usRegAddr, USHORT usRe UCHAR *ucMBFrame; eMBMasterReqErrCode eErrStatus = MB_MRE_NO_ERR; - if ( xMBasterRunMutexLock( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY; + if ( xMBasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY; else if ( ucSndAddr > MB_MASTER_TOTAL_SLAVE_NUM ) eErrStatus = MB_MRE_ILL_ARG; else { @@ -151,7 +151,7 @@ eMBMasterReqWriteMultipleHoldingRegister( UCHAR ucSndAddr, USHORT usRegIndex = 0; eMBMasterReqErrCode eErrStatus = MB_MRE_NO_ERR; - if ( xMBasterRunMutexLock( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY; + if ( xMBasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY; else if ( ucSndAddr > MB_MASTER_TOTAL_SLAVE_NUM ) eErrStatus = MB_MRE_ILL_ARG; else { @@ -233,7 +233,7 @@ eMBMasterReqReadHoldingRegister( UCHAR ucSndAddr, USHORT usRegAddr, USHORT usNRe UCHAR *ucMBFrame; eMBMasterReqErrCode eErrStatus = MB_MRE_NO_ERR; - if ( xMBasterRunMutexLock( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY; + if ( xMBasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY; else if ( ucSndAddr > MB_MASTER_TOTAL_SLAVE_NUM ) eErrStatus = MB_MRE_ILL_ARG; else { @@ -309,7 +309,7 @@ eMBMasterReqReadWriteMultipleHoldingRegister( UCHAR ucSndAddr, USHORT usRegIndex = 0; eMBMasterReqErrCode eErrStatus = MB_MRE_NO_ERR; - if ( xMBasterRunMutexLock( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY; + if ( xMBasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY; else if ( ucSndAddr > MB_MASTER_TOTAL_SLAVE_NUM ) eErrStatus = MB_MRE_ILL_ARG; else { diff --git a/FreeModbus/modbus/functions/mbfuncinput_m.c b/FreeModbus/modbus/functions/mbfuncinput_m.c index 9574bd1..542275c 100644 --- a/FreeModbus/modbus/functions/mbfuncinput_m.c +++ b/FreeModbus/modbus/functions/mbfuncinput_m.c @@ -65,7 +65,7 @@ eMBMasterReqReadInputRegister( UCHAR ucSndAddr, USHORT usRegAddr, USHORT usNRegs UCHAR *ucMBFrame; eMBMasterReqErrCode eErrStatus = MB_MRE_NO_ERR; - if ( xMBasterRunMutexLock( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY; + if ( xMBasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY; else if ( ucSndAddr > MB_MASTER_TOTAL_SLAVE_NUM ) eErrStatus = MB_MRE_ILL_ARG; else { diff --git a/FreeModbus/modbus/include/mbport.h b/FreeModbus/modbus/include/mbport.h index f065c88..35c841d 100644 --- a/FreeModbus/modbus/include/mbport.h +++ b/FreeModbus/modbus/include/mbport.h @@ -92,11 +92,11 @@ BOOL xMBMasterPortEventPost( eMBMasterEventType eEvent ); BOOL xMBMasterPortEventGet( /*@out@ */ eMBMasterEventType * eEvent ); -void vMBasterRunMutexInit( void ); +void vMBasterRunResInit( void ); -BOOL xMBasterRunMutexLock( int32_t time ); +BOOL xMBasterRunResTake( int32_t time ); -void vMBasterRunMutexUnlock( void ); +void vMBasterRunResRelease( void ); /* ----------------------- Serial port functions ----------------------------*/ diff --git a/FreeModbus/modbus/mb_m.c b/FreeModbus/modbus/mb_m.c index 281cbf5..2971f39 100644 --- a/FreeModbus/modbus/mb_m.c +++ b/FreeModbus/modbus/mb_m.c @@ -184,8 +184,8 @@ eMBMasterInit( eMBMode eMode, UCHAR ucPort, ULONG ulBaudRate, eMBParity eParity { eMBState = STATE_DISABLED; } - /* initialize the Mobus Master mutex */ - vMBasterRunMutexInit(); + /* initialize the Mobus Master running resource. */ + vMBasterRunResInit(); } return eStatus; } @@ -319,7 +319,7 @@ eMBMasterPoll( void ) } /* If master has exception ,Master will send error process.Otherwise the Master is idle.*/ if (eException != MB_EX_NONE) ( void ) xMBMasterPortEventPost( EV_MASTER_ERROR_PROCESS ); - else vMBasterRunMutexUnlock(); + else vMBasterRunResRelease(); break; case EV_MASTER_FRAME_SENT: @@ -329,7 +329,7 @@ eMBMasterPoll( void ) break; case EV_MASTER_ERROR_PROCESS: - vMBasterRunMutexUnlock(); + vMBasterRunResRelease(); break; } } diff --git a/FreeModbus/modbus/rtu/mbrtu_m.c b/FreeModbus/modbus/rtu/mbrtu_m.c index b0ae52b..13e682d 100644 --- a/FreeModbus/modbus/rtu/mbrtu_m.c +++ b/FreeModbus/modbus/rtu/mbrtu_m.c @@ -393,7 +393,7 @@ xMBMasterRTUTimerExpired(void) vMBMasterPortTimersDisable( ); /* If timer mode is convert delay ,then Master is idel now. */ - if (eMasterCurTimerMode == MB_TMODE_CONVERT_DELAY) vMBasterRunMutexUnlock(); + if (eMasterCurTimerMode == MB_TMODE_CONVERT_DELAY) vMBasterRunResRelease(); return xNeedPoll; } diff --git a/FreeModbus/port/portevent_m.c b/FreeModbus/port/portevent_m.c index 07caa85..70a851d 100644 --- a/FreeModbus/port/portevent_m.c +++ b/FreeModbus/port/portevent_m.c @@ -26,9 +26,9 @@ #if MB_MASTER_RTU_ENABLED > 0 || MB_MASTER_ASCII_ENABLED > 0 /* ----------------------- Variables ----------------------------------------*/ -static eMBMasterEventType eMasterQueuedEvent; -static BOOL xMasterEventInQueue; -static struct rt_mutex xMasterRunMutex; +static eMBMasterEventType eMasterQueuedEvent; +static BOOL xMasterEventInQueue; +static struct rt_semaphore xMasterRunRes; /* ----------------------- Start implementation -----------------------------*/ BOOL @@ -61,37 +61,39 @@ xMBMasterPortEventGet( eMBMasterEventType * eEvent ) } /** - * This function is initialize the Mobus Master mutex . - * Note:The mutex is define by Operating System.If you not use Opearting System this function can be empty. + * This function is initialize the Mobus Master running resource . + * Note:The resource is define by Operating System.If you not use Opearting System this function can be empty. * */ -void vMBasterRunMutexInit( void ) +void vMBasterRunResInit( void ) { - rt_mutex_init(&xMasterRunMutex, "master run", RT_IPC_FLAG_PRIO); + rt_sem_init(&xMasterRunRes, "master res", 0x01 , RT_IPC_FLAG_PRIO); } /** - * This function is lock Mobus Master mutex. - * Note:The mutex is define by Operating System.If you not use Opearting System this function can be just return TRUE. + * This function is take Mobus Master running resource. + * Note:The resource is define by Operating System.If you not use Opearting System this function can be just return TRUE. * * @param lTimeOut the waiting time. * - * @return mutex lock result + * @return resource taked result */ -BOOL xMBasterRunMutexLock( LONG lTimeOut ) +BOOL xMBasterRunResTake( LONG lTimeOut ) { /*If waiting time is -1 .It will wait forever */ - return rt_mutex_take(&xMasterRunMutex, lTimeOut) ? FALSE : TRUE ; + return rt_sem_take(&xMasterRunRes, lTimeOut) ? FALSE : TRUE ; } /** - * This function is unlock Mobus Master mutex. - * Note:The mutex is define by Operating System.If you not use Opearting System this function can be empty. + * This function is release Mobus Master running resource. + * Note:The resource is define by Operating System.If you not use Opearting System this function can be empty. * */ -void vMBasterRunMutexUnlock( void ) +void vMBasterRunResRelease( void ) { - rt_mutex_release(&xMasterRunMutex); + /* Clear up resource when need release resource. */ + rt_sem_control(&xMasterRunRes, RT_IPC_CMD_RESET, 0); + rt_sem_release(&xMasterRunRes); } #endif