diff options
author | Gregory Nutt <gnutt@nuttx.org> | 2014-11-25 15:14:28 -0600 |
---|---|---|
committer | Gregory Nutt <gnutt@nuttx.org> | 2014-11-25 15:14:28 -0600 |
commit | 6b0412f5128e2fdf94852de586b3f90cbf476240 (patch) | |
tree | 91ab0edfb4492a25abf3c7e177e102201a77b23f /apps/modbus | |
parent | b845f19a3bb200e49a7a314787b3290e3155ce01 (diff) | |
download | nuttx-6b0412f5128e2fdf94852de586b3f90cbf476240.tar.gz nuttx-6b0412f5128e2fdf94852de586b3f90cbf476240.tar.bz2 nuttx-6b0412f5128e2fdf94852de586b3f90cbf476240.zip |
Fixes for more cppcheck complaints. Mostly cosmetic
Diffstat (limited to 'apps/modbus')
-rw-r--r-- | apps/modbus/functions/mbfuncholding.c | 159 | ||||
-rw-r--r-- | apps/modbus/mb.c | 115 | ||||
-rw-r--r-- | apps/modbus/rtu/mbrtu.c | 135 |
3 files changed, 208 insertions, 201 deletions
diff --git a/apps/modbus/functions/mbfuncholding.c b/apps/modbus/functions/mbfuncholding.c index 7e0dee86b..28693d0f5 100644 --- a/apps/modbus/functions/mbfuncholding.c +++ b/apps/modbus/functions/mbfuncholding.c @@ -42,58 +42,58 @@ #include <apps/modbus/mbproto.h> /* ----------------------- Defines ------------------------------------------*/ -#define MB_PDU_FUNC_READ_ADDR_OFF ( MB_PDU_DATA_OFF + 0) -#define MB_PDU_FUNC_READ_REGCNT_OFF ( MB_PDU_DATA_OFF + 2 ) -#define MB_PDU_FUNC_READ_SIZE ( 4 ) -#define MB_PDU_FUNC_READ_REGCNT_MAX ( 0x007D ) - -#define MB_PDU_FUNC_WRITE_ADDR_OFF ( MB_PDU_DATA_OFF + 0) -#define MB_PDU_FUNC_WRITE_VALUE_OFF ( MB_PDU_DATA_OFF + 2 ) -#define MB_PDU_FUNC_WRITE_SIZE ( 4 ) - -#define MB_PDU_FUNC_WRITE_MUL_ADDR_OFF ( MB_PDU_DATA_OFF + 0 ) -#define MB_PDU_FUNC_WRITE_MUL_REGCNT_OFF ( MB_PDU_DATA_OFF + 2 ) -#define MB_PDU_FUNC_WRITE_MUL_BYTECNT_OFF ( MB_PDU_DATA_OFF + 4 ) -#define MB_PDU_FUNC_WRITE_MUL_VALUES_OFF ( MB_PDU_DATA_OFF + 5 ) -#define MB_PDU_FUNC_WRITE_MUL_SIZE_MIN ( 5 ) -#define MB_PDU_FUNC_WRITE_MUL_REGCNT_MAX ( 0x0078 ) - -#define MB_PDU_FUNC_READWRITE_READ_ADDR_OFF ( MB_PDU_DATA_OFF + 0 ) -#define MB_PDU_FUNC_READWRITE_READ_REGCNT_OFF ( MB_PDU_DATA_OFF + 2 ) -#define MB_PDU_FUNC_READWRITE_WRITE_ADDR_OFF ( MB_PDU_DATA_OFF + 4 ) -#define MB_PDU_FUNC_READWRITE_WRITE_REGCNT_OFF ( MB_PDU_DATA_OFF + 6 ) -#define MB_PDU_FUNC_READWRITE_BYTECNT_OFF ( MB_PDU_DATA_OFF + 8 ) -#define MB_PDU_FUNC_READWRITE_WRITE_VALUES_OFF ( MB_PDU_DATA_OFF + 9 ) -#define MB_PDU_FUNC_READWRITE_SIZE_MIN ( 9 ) +#define MB_PDU_FUNC_READ_ADDR_OFF (MB_PDU_DATA_OFF + 0) +#define MB_PDU_FUNC_READ_REGCNT_OFF (MB_PDU_DATA_OFF + 2) +#define MB_PDU_FUNC_READ_SIZE (4) +#define MB_PDU_FUNC_READ_REGCNT_MAX (0x007D) + +#define MB_PDU_FUNC_WRITE_ADDR_OFF (MB_PDU_DATA_OFF + 0) +#define MB_PDU_FUNC_WRITE_VALUE_OFF (MB_PDU_DATA_OFF + 2) +#define MB_PDU_FUNC_WRITE_SIZE (4) + +#define MB_PDU_FUNC_WRITE_MUL_ADDR_OFF (MB_PDU_DATA_OFF + 0) +#define MB_PDU_FUNC_WRITE_MUL_REGCNT_OFF (MB_PDU_DATA_OFF + 2) +#define MB_PDU_FUNC_WRITE_MUL_BYTECNT_OFF (MB_PDU_DATA_OFF + 4) +#define MB_PDU_FUNC_WRITE_MUL_VALUES_OFF (MB_PDU_DATA_OFF + 5) +#define MB_PDU_FUNC_WRITE_MUL_SIZE_MIN (5) +#define MB_PDU_FUNC_WRITE_MUL_REGCNT_MAX (0x0078) + +#define MB_PDU_FUNC_READWRITE_READ_ADDR_OFF (MB_PDU_DATA_OFF + 0) +#define MB_PDU_FUNC_READWRITE_READ_REGCNT_OFF (MB_PDU_DATA_OFF + 2) +#define MB_PDU_FUNC_READWRITE_WRITE_ADDR_OFF (MB_PDU_DATA_OFF + 4) +#define MB_PDU_FUNC_READWRITE_WRITE_REGCNT_OFF (MB_PDU_DATA_OFF + 6) +#define MB_PDU_FUNC_READWRITE_BYTECNT_OFF (MB_PDU_DATA_OFF + 8) +#define MB_PDU_FUNC_READWRITE_WRITE_VALUES_OFF (MB_PDU_DATA_OFF + 9) +#define MB_PDU_FUNC_READWRITE_SIZE_MIN (9) /* ----------------------- Static functions ---------------------------------*/ -eMBException prveMBError2Exception( eMBErrorCode eErrorCode ); +eMBException prveMBError2Exception(eMBErrorCode eErrorCode); /* ----------------------- Start implementation -----------------------------*/ #ifdef CONFIG_MB_FUNC_WRITE_HOLDING_ENABLED eMBException -eMBFuncWriteHoldingRegister( uint8_t * pucFrame, uint16_t * usLen ) +eMBFuncWriteHoldingRegister(uint8_t * pucFrame, uint16_t * usLen) { uint16_t usRegAddress; eMBException eStatus = MB_EX_NONE; eMBErrorCode eRegStatus; - if( *usLen == ( MB_PDU_FUNC_WRITE_SIZE + MB_PDU_SIZE_MIN ) ) + if (*usLen == (MB_PDU_FUNC_WRITE_SIZE + MB_PDU_SIZE_MIN)) { - usRegAddress = ( uint16_t )( pucFrame[MB_PDU_FUNC_WRITE_ADDR_OFF] << 8 ); - usRegAddress |= ( uint16_t )( pucFrame[MB_PDU_FUNC_WRITE_ADDR_OFF + 1] ); + usRegAddress = (uint16_t)(pucFrame[MB_PDU_FUNC_WRITE_ADDR_OFF] << 8); + usRegAddress |= (uint16_t)(pucFrame[MB_PDU_FUNC_WRITE_ADDR_OFF + 1]); usRegAddress++; /* Make callback to update the value. */ - eRegStatus = eMBRegHoldingCB( &pucFrame[MB_PDU_FUNC_WRITE_VALUE_OFF], - usRegAddress, 1, MB_REG_WRITE ); + eRegStatus = eMBRegHoldingCB(&pucFrame[MB_PDU_FUNC_WRITE_VALUE_OFF], + usRegAddress, 1, MB_REG_WRITE); /* If an error occured convert it into a Modbus exception. */ - if( eRegStatus != MB_ENOERR ) + if (eRegStatus != MB_ENOERR) { - eStatus = prveMBError2Exception( eRegStatus ); + eStatus = prveMBError2Exception(eRegStatus); } } else @@ -107,7 +107,7 @@ eMBFuncWriteHoldingRegister( uint8_t * pucFrame, uint16_t * usLen ) #ifdef CONFIG_MB_FUNC_WRITE_MULTIPLE_HOLDING_ENABLED eMBException -eMBFuncWriteMultipleHoldingRegister( uint8_t * pucFrame, uint16_t * usLen ) +eMBFuncWriteMultipleHoldingRegister(uint8_t * pucFrame, uint16_t * usLen) { uint16_t usRegAddress; uint16_t usRegCount; @@ -116,30 +116,31 @@ eMBFuncWriteMultipleHoldingRegister( uint8_t * pucFrame, uint16_t * usLen ) eMBException eStatus = MB_EX_NONE; eMBErrorCode eRegStatus; - if( *usLen >= ( MB_PDU_FUNC_WRITE_MUL_SIZE_MIN + MB_PDU_SIZE_MIN ) ) + if (*usLen >= (MB_PDU_FUNC_WRITE_MUL_SIZE_MIN + MB_PDU_SIZE_MIN)) { - usRegAddress = ( uint16_t )( pucFrame[MB_PDU_FUNC_WRITE_MUL_ADDR_OFF] << 8 ); - usRegAddress |= ( uint16_t )( pucFrame[MB_PDU_FUNC_WRITE_MUL_ADDR_OFF + 1] ); + usRegAddress = (uint16_t)(pucFrame[MB_PDU_FUNC_WRITE_MUL_ADDR_OFF] << 8); + usRegAddress |= (uint16_t)(pucFrame[MB_PDU_FUNC_WRITE_MUL_ADDR_OFF + 1]); usRegAddress++; - usRegCount = ( uint16_t )( pucFrame[MB_PDU_FUNC_WRITE_MUL_REGCNT_OFF] << 8 ); - usRegCount |= ( uint16_t )( pucFrame[MB_PDU_FUNC_WRITE_MUL_REGCNT_OFF + 1] ); + usRegCount = (uint16_t)(pucFrame[MB_PDU_FUNC_WRITE_MUL_REGCNT_OFF] << 8); + usRegCount |= (uint16_t)(pucFrame[MB_PDU_FUNC_WRITE_MUL_REGCNT_OFF + 1]); ucRegByteCount = pucFrame[MB_PDU_FUNC_WRITE_MUL_BYTECNT_OFF]; - if( ( usRegCount >= 1 ) && - ( usRegCount <= MB_PDU_FUNC_WRITE_MUL_REGCNT_MAX ) && - ( ucRegByteCount == ( uint8_t ) ( 2 * usRegCount ) ) ) + if ((usRegCount >= 1) && + (usRegCount <= MB_PDU_FUNC_WRITE_MUL_REGCNT_MAX) && + (ucRegByteCount == (uint8_t) (2 * usRegCount))) { /* Make callback to update the register values. */ eRegStatus = - eMBRegHoldingCB( &pucFrame[MB_PDU_FUNC_WRITE_MUL_VALUES_OFF], - usRegAddress, usRegCount, MB_REG_WRITE ); + eMBRegHoldingCB(&pucFrame[MB_PDU_FUNC_WRITE_MUL_VALUES_OFF], + usRegAddress, usRegCount, MB_REG_WRITE); - /* If an error occured convert it into a Modbus exception. */ - if( eRegStatus != MB_ENOERR ) + /* If an error occurred convert it into a Modbus exception. */ + + if (eRegStatus != MB_ENOERR) { - eStatus = prveMBError2Exception( eRegStatus ); + eStatus = prveMBError2Exception(eRegStatus); } else { @@ -147,6 +148,7 @@ eMBFuncWriteMultipleHoldingRegister( uint8_t * pucFrame, uint16_t * usLen ) * address and the quantity of registers. We reuse the * old values in the buffer because they are still valid. */ + *usLen = MB_PDU_FUNC_WRITE_MUL_BYTECNT_OFF; } } @@ -167,7 +169,7 @@ eMBFuncWriteMultipleHoldingRegister( uint8_t * pucFrame, uint16_t * usLen ) #ifdef CONFIG_MB_FUNC_READ_HOLDING_ENABLED eMBException -eMBFuncReadHoldingRegister( uint8_t * pucFrame, uint16_t * usLen ) +eMBFuncReadHoldingRegister(uint8_t * pucFrame, uint16_t * usLen) { uint16_t usRegAddress; uint16_t usRegCount; @@ -176,19 +178,20 @@ eMBFuncReadHoldingRegister( uint8_t * pucFrame, uint16_t * usLen ) eMBException eStatus = MB_EX_NONE; eMBErrorCode eRegStatus; - if( *usLen == ( MB_PDU_FUNC_READ_SIZE + MB_PDU_SIZE_MIN ) ) + if (*usLen == (MB_PDU_FUNC_READ_SIZE + MB_PDU_SIZE_MIN)) { - usRegAddress = ( uint16_t )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF] << 8 ); - usRegAddress |= ( uint16_t )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF + 1] ); + usRegAddress = (uint16_t)(pucFrame[MB_PDU_FUNC_READ_ADDR_OFF] << 8); + usRegAddress |= (uint16_t)(pucFrame[MB_PDU_FUNC_READ_ADDR_OFF + 1]); usRegAddress++; - usRegCount = ( uint16_t )( pucFrame[MB_PDU_FUNC_READ_REGCNT_OFF] << 8 ); - usRegCount = ( uint16_t )( pucFrame[MB_PDU_FUNC_READ_REGCNT_OFF + 1] ); + usRegCount = (uint16_t)(pucFrame[MB_PDU_FUNC_READ_REGCNT_OFF] << 8); + usRegCount |= (uint16_t)(pucFrame[MB_PDU_FUNC_READ_REGCNT_OFF + 1]); /* Check if the number of registers to read is valid. If not * return Modbus illegal data value exception. */ - if( ( usRegCount >= 1 ) && ( usRegCount <= MB_PDU_FUNC_READ_REGCNT_MAX ) ) + + if ((usRegCount >= 1) && (usRegCount <= MB_PDU_FUNC_READ_REGCNT_MAX)) { /* Set the current PDU data pointer to the beginning. */ pucFrameCur = &pucFrame[MB_PDU_FUNC_OFF]; @@ -199,15 +202,15 @@ eMBFuncReadHoldingRegister( uint8_t * pucFrame, uint16_t * usLen ) *usLen += 1; /* Second byte in the response contain the number of bytes. */ - *pucFrameCur++ = ( uint8_t ) ( usRegCount * 2 ); + *pucFrameCur++ = (uint8_t) (usRegCount * 2); *usLen += 1; /* Make callback to fill the buffer. */ - eRegStatus = eMBRegHoldingCB( pucFrameCur, usRegAddress, usRegCount, MB_REG_READ ); + eRegStatus = eMBRegHoldingCB(pucFrameCur, usRegAddress, usRegCount, MB_REG_READ); /* If an error occured convert it into a Modbus exception. */ - if( eRegStatus != MB_ENOERR ) + if (eRegStatus != MB_ENOERR) { - eStatus = prveMBError2Exception( eRegStatus ); + eStatus = prveMBError2Exception(eRegStatus); } else { @@ -232,7 +235,7 @@ eMBFuncReadHoldingRegister( uint8_t * pucFrame, uint16_t * usLen ) #ifdef CONFIG_MB_FUNC_READWRITE_HOLDING_ENABLED eMBException -eMBFuncReadWriteMultipleHoldingRegister( uint8_t * pucFrame, uint16_t * usLen ) +eMBFuncReadWriteMultipleHoldingRegister(uint8_t * pucFrame, uint16_t * usLen) { uint16_t usRegReadAddress; uint16_t usRegReadCount; @@ -244,33 +247,33 @@ eMBFuncReadWriteMultipleHoldingRegister( uint8_t * pucFrame, uint16_t * usLen ) eMBException eStatus = MB_EX_NONE; eMBErrorCode eRegStatus; - if( *usLen >= ( MB_PDU_FUNC_READWRITE_SIZE_MIN + MB_PDU_SIZE_MIN ) ) + if (*usLen >= (MB_PDU_FUNC_READWRITE_SIZE_MIN + MB_PDU_SIZE_MIN)) { - usRegReadAddress = ( uint16_t )( pucFrame[MB_PDU_FUNC_READWRITE_READ_ADDR_OFF] << 8U ); - usRegReadAddress |= ( uint16_t )( pucFrame[MB_PDU_FUNC_READWRITE_READ_ADDR_OFF + 1] ); + usRegReadAddress = (uint16_t)(pucFrame[MB_PDU_FUNC_READWRITE_READ_ADDR_OFF] << 8U); + usRegReadAddress |= (uint16_t)(pucFrame[MB_PDU_FUNC_READWRITE_READ_ADDR_OFF + 1]); usRegReadAddress++; - usRegReadCount = ( uint16_t )( pucFrame[MB_PDU_FUNC_READWRITE_READ_REGCNT_OFF] << 8U ); - usRegReadCount |= ( uint16_t )( pucFrame[MB_PDU_FUNC_READWRITE_READ_REGCNT_OFF + 1] ); + usRegReadCount = (uint16_t)(pucFrame[MB_PDU_FUNC_READWRITE_READ_REGCNT_OFF] << 8U); + usRegReadCount |= (uint16_t)(pucFrame[MB_PDU_FUNC_READWRITE_READ_REGCNT_OFF + 1]); - usRegWriteAddress = ( uint16_t )( pucFrame[MB_PDU_FUNC_READWRITE_WRITE_ADDR_OFF] << 8U ); - usRegWriteAddress |= ( uint16_t )( pucFrame[MB_PDU_FUNC_READWRITE_WRITE_ADDR_OFF + 1] ); + usRegWriteAddress = (uint16_t)(pucFrame[MB_PDU_FUNC_READWRITE_WRITE_ADDR_OFF] << 8U); + usRegWriteAddress |= (uint16_t)(pucFrame[MB_PDU_FUNC_READWRITE_WRITE_ADDR_OFF + 1]); usRegWriteAddress++; - usRegWriteCount = ( uint16_t )( pucFrame[MB_PDU_FUNC_READWRITE_WRITE_REGCNT_OFF] << 8U ); - usRegWriteCount |= ( uint16_t )( pucFrame[MB_PDU_FUNC_READWRITE_WRITE_REGCNT_OFF + 1] ); + usRegWriteCount = (uint16_t)(pucFrame[MB_PDU_FUNC_READWRITE_WRITE_REGCNT_OFF] << 8U); + usRegWriteCount |= (uint16_t)(pucFrame[MB_PDU_FUNC_READWRITE_WRITE_REGCNT_OFF + 1]); ucRegWriteByteCount = pucFrame[MB_PDU_FUNC_READWRITE_BYTECNT_OFF]; - if( ( usRegReadCount >= 1 ) && ( usRegReadCount <= 0x7D ) && - ( usRegWriteCount >= 1 ) && ( usRegWriteCount <= 0x79 ) && - ( ( 2 * usRegWriteCount ) == ucRegWriteByteCount ) ) + if ((usRegReadCount >= 1) && (usRegReadCount <= 0x7D) && + (usRegWriteCount >= 1) && (usRegWriteCount <= 0x79) && + ((2 * usRegWriteCount) == ucRegWriteByteCount)) { /* Make callback to update the register values. */ - eRegStatus = eMBRegHoldingCB( &pucFrame[MB_PDU_FUNC_READWRITE_WRITE_VALUES_OFF], - usRegWriteAddress, usRegWriteCount, MB_REG_WRITE ); + eRegStatus = eMBRegHoldingCB(&pucFrame[MB_PDU_FUNC_READWRITE_WRITE_VALUES_OFF], + usRegWriteAddress, usRegWriteCount, MB_REG_WRITE); - if( eRegStatus == MB_ENOERR ) + if (eRegStatus == MB_ENOERR) { /* Set the current PDU data pointer to the beginning. */ pucFrameCur = &pucFrame[MB_PDU_FUNC_OFF]; @@ -281,20 +284,20 @@ eMBFuncReadWriteMultipleHoldingRegister( uint8_t * pucFrame, uint16_t * usLen ) *usLen += 1; /* Second byte in the response contain the number of bytes. */ - *pucFrameCur++ = ( uint8_t ) ( usRegReadCount * 2 ); + *pucFrameCur++ = (uint8_t) (usRegReadCount * 2); *usLen += 1; /* Make the read callback. */ eRegStatus = - eMBRegHoldingCB( pucFrameCur, usRegReadAddress, usRegReadCount, MB_REG_READ ); - if( eRegStatus == MB_ENOERR ) + eMBRegHoldingCB(pucFrameCur, usRegReadAddress, usRegReadCount, MB_REG_READ); + if (eRegStatus == MB_ENOERR) { *usLen += 2 * usRegReadCount; } } - if( eRegStatus != MB_ENOERR ) + if (eRegStatus != MB_ENOERR) { - eStatus = prveMBError2Exception( eRegStatus ); + eStatus = prveMBError2Exception(eRegStatus); } } else diff --git a/apps/modbus/mb.c b/apps/modbus/mb.c index a86ce343f..9cb0543d8 100644 --- a/apps/modbus/mb.c +++ b/apps/modbus/mb.c @@ -72,7 +72,7 @@ static enum STATE_NOT_INITIALIZED } eMBState = STATE_NOT_INITIALIZED; -/* Functions pointer which are initialized in eMBInit( ). Depending on the +/* Functions pointer which are initialized in eMBInit(). Depending on the * mode (RTU or ASCII) the are set to the correct implementations. */ static peMBFrameSend peMBFrameSendCur; @@ -85,12 +85,12 @@ static pvMBFrameClose pvMBFrameCloseCur; * an external event has happend which includes a timeout or the reception * or transmission of a character. */ -bool( *pxMBFrameCBByteReceived ) ( void ); -bool( *pxMBFrameCBTransmitterEmpty ) ( void ); -bool( *pxMBPortCBTimerExpired ) ( void ); +bool(*pxMBFrameCBByteReceived)(void); +bool(*pxMBFrameCBTransmitterEmpty)(void); +bool(*pxMBPortCBTimerExpired)(void); -bool( *pxMBFrameCBReceiveFSMCur ) ( void ); -bool( *pxMBFrameCBTransmitFSMCur ) ( void ); +bool(*pxMBFrameCBReceiveFSMCur)(void); +bool(*pxMBFrameCBTransmitFSMCur)(void); /* An array of Modbus functions handlers which associates Modbus function * codes with implementing functions. @@ -130,13 +130,14 @@ static xMBFunctionHandler xFuncHandlers[CONFIG_MB_FUNC_HANDLERS_MAX] = { /* ----------------------- Start implementation -----------------------------*/ eMBErrorCode -eMBInit( eMBMode eMode, uint8_t ucSlaveAddress, uint8_t ucPort, speed_t ulBaudRate, eMBParity eParity ) +eMBInit(eMBMode eMode, uint8_t ucSlaveAddress, uint8_t ucPort, speed_t ulBaudRate, eMBParity eParity) { eMBErrorCode eStatus = MB_ENOERR; /* check preconditions */ - if( ( ucSlaveAddress == MB_ADDRESS_BROADCAST ) || - ( ucSlaveAddress < MB_ADDRESS_MIN ) || ( ucSlaveAddress > MB_ADDRESS_MAX ) ) + + if ((ucSlaveAddress == MB_ADDRESS_BROADCAST) || + (ucSlaveAddress < MB_ADDRESS_MIN) || (ucSlaveAddress > MB_ADDRESS_MAX)) { eStatus = MB_EINVAL; } @@ -144,7 +145,7 @@ eMBInit( eMBMode eMode, uint8_t ucSlaveAddress, uint8_t ucPort, speed_t ulBaudRa { ucMBAddress = ucSlaveAddress; - switch ( eMode ) + switch (eMode) { #ifdef CONFIG_MB_RTU_ENABLED case MB_RTU: @@ -157,7 +158,7 @@ eMBInit( eMBMode eMode, uint8_t ucSlaveAddress, uint8_t ucPort, speed_t ulBaudRa pxMBFrameCBTransmitterEmpty = xMBRTUTransmitFSM; pxMBPortCBTimerExpired = xMBRTUTimerT35Expired; - eStatus = eMBRTUInit( ucMBAddress, ucPort, ulBaudRate, eParity ); + eStatus = eMBRTUInit(ucMBAddress, ucPort, ulBaudRate, eParity); break; #endif #ifdef CONFIG_MB_ASCII_ENABLED @@ -171,16 +172,16 @@ eMBInit( eMBMode eMode, uint8_t ucSlaveAddress, uint8_t ucPort, speed_t ulBaudRa pxMBFrameCBTransmitterEmpty = xMBASCIITransmitFSM; pxMBPortCBTimerExpired = xMBASCIITimerT1SExpired; - eStatus = eMBASCIIInit( ucMBAddress, ucPort, ulBaudRate, eParity ); + eStatus = eMBASCIIInit(ucMBAddress, ucPort, ulBaudRate, eParity); break; #endif default: eStatus = MB_EINVAL; } - if( eStatus == MB_ENOERR ) + if (eStatus == MB_ENOERR) { - if( !xMBPortEventInit( ) ) + if (!xMBPortEventInit()) { /* port dependent event module initalization failed. */ eStatus = MB_EPORTERR; @@ -197,15 +198,15 @@ eMBInit( eMBMode eMode, uint8_t ucSlaveAddress, uint8_t ucPort, speed_t ulBaudRa #ifdef CONFIG_MB_TCP_ENABLED eMBErrorCode -eMBTCPInit( uint16_t ucTCPPort ) +eMBTCPInit(uint16_t ucTCPPort) { eMBErrorCode eStatus = MB_ENOERR; - if( ( eStatus = eMBTCPDoInit( ucTCPPort ) ) != MB_ENOERR ) + if ((eStatus = eMBTCPDoInit(ucTCPPort)) != MB_ENOERR) { eMBState = STATE_DISABLED; } - else if( !xMBPortEventInit( ) ) + else if (!xMBPortEventInit()) { /* Port dependent event module initalization failed. */ eStatus = MB_EPORTERR; @@ -226,33 +227,33 @@ eMBTCPInit( uint16_t ucTCPPort ) #endif eMBErrorCode -eMBRegisterCB( uint8_t ucFunctionCode, pxMBFunctionHandler pxHandler ) +eMBRegisterCB(uint8_t ucFunctionCode, pxMBFunctionHandler pxHandler) { int i; eMBErrorCode eStatus; - if( ( 0 < ucFunctionCode ) && ( ucFunctionCode <= 127 ) ) + if ((0 < ucFunctionCode) && (ucFunctionCode <= 127)) { - ENTER_CRITICAL_SECTION( ); - if( pxHandler != NULL ) + ENTER_CRITICAL_SECTION(); + if (pxHandler != NULL) { - for( i = 0; i < CONFIG_MB_FUNC_HANDLERS_MAX; i++ ) + for(i = 0; i < CONFIG_MB_FUNC_HANDLERS_MAX; i++) { - if( ( xFuncHandlers[i].pxHandler == NULL ) || - ( xFuncHandlers[i].pxHandler == pxHandler ) ) + if ((xFuncHandlers[i].pxHandler == NULL) || + (xFuncHandlers[i].pxHandler == pxHandler)) { xFuncHandlers[i].ucFunctionCode = ucFunctionCode; xFuncHandlers[i].pxHandler = pxHandler; break; } } - eStatus = ( i != CONFIG_MB_FUNC_HANDLERS_MAX ) ? MB_ENOERR : MB_ENORES; + eStatus = (i != CONFIG_MB_FUNC_HANDLERS_MAX) ? MB_ENOERR : MB_ENORES; } else { - for( i = 0; i < CONFIG_MB_FUNC_HANDLERS_MAX; i++ ) + for(i = 0; i < CONFIG_MB_FUNC_HANDLERS_MAX; i++) { - if( xFuncHandlers[i].ucFunctionCode == ucFunctionCode ) + if (xFuncHandlers[i].ucFunctionCode == ucFunctionCode) { xFuncHandlers[i].ucFunctionCode = 0; xFuncHandlers[i].pxHandler = NULL; @@ -262,7 +263,7 @@ eMBRegisterCB( uint8_t ucFunctionCode, pxMBFunctionHandler pxHandler ) /* Remove can't fail. */ eStatus = MB_ENOERR; } - EXIT_CRITICAL_SECTION( ); + EXIT_CRITICAL_SECTION(); } else { @@ -273,15 +274,15 @@ eMBRegisterCB( uint8_t ucFunctionCode, pxMBFunctionHandler pxHandler ) eMBErrorCode -eMBClose( void ) +eMBClose(void) { eMBErrorCode eStatus = MB_ENOERR; - if( eMBState == STATE_DISABLED ) + if (eMBState == STATE_DISABLED) { - if( pvMBFrameCloseCur != NULL ) + if (pvMBFrameCloseCur != NULL) { - pvMBFrameCloseCur( ); + pvMBFrameCloseCur(); } } else @@ -292,14 +293,14 @@ eMBClose( void ) } eMBErrorCode -eMBEnable( void ) +eMBEnable(void) { eMBErrorCode eStatus = MB_ENOERR; - if( eMBState == STATE_DISABLED ) + if (eMBState == STATE_DISABLED) { /* Activate the protocol stack. */ - pvMBFrameStartCur( ); + pvMBFrameStartCur(); eMBState = STATE_ENABLED; } else @@ -310,17 +311,17 @@ eMBEnable( void ) } eMBErrorCode -eMBDisable( void ) +eMBDisable(void) { eMBErrorCode eStatus; - if( eMBState == STATE_ENABLED ) + if (eMBState == STATE_ENABLED) { - pvMBFrameStopCur( ); + pvMBFrameStopCur(); eMBState = STATE_DISABLED; eStatus = MB_ENOERR; } - else if( eMBState == STATE_DISABLED ) + else if (eMBState == STATE_DISABLED) { eStatus = MB_ENOERR; } @@ -332,7 +333,7 @@ eMBDisable( void ) } eMBErrorCode -eMBPoll( void ) +eMBPoll(void) { static uint8_t *ucMBFrame; static uint8_t ucRcvAddress; @@ -345,28 +346,28 @@ eMBPoll( void ) eMBEventType eEvent; /* Check if the protocol stack is ready. */ - if( eMBState != STATE_ENABLED ) + if (eMBState != STATE_ENABLED) { return MB_EILLSTATE; } /* Check if there is a event available. If not return control to caller. * Otherwise we will handle the event. */ - if( xMBPortEventGet( &eEvent ) == true ) + if (xMBPortEventGet(&eEvent) == true) { - switch ( eEvent ) + switch (eEvent) { case EV_READY: break; case EV_FRAME_RECEIVED: - eStatus = peMBFrameReceiveCur( &ucRcvAddress, &ucMBFrame, &usLength ); - if( eStatus == MB_ENOERR ) + eStatus = peMBFrameReceiveCur(&ucRcvAddress, &ucMBFrame, &usLength); + if (eStatus == MB_ENOERR) { /* Check if the frame is for us. If not ignore the frame. */ - if( ( ucRcvAddress == ucMBAddress ) || ( ucRcvAddress == MB_ADDRESS_BROADCAST ) ) + if ((ucRcvAddress == ucMBAddress) || (ucRcvAddress == MB_ADDRESS_BROADCAST)) { - ( void )xMBPortEventPost( EV_EXECUTE ); + (void)xMBPortEventPost(EV_EXECUTE); } } break; @@ -374,38 +375,38 @@ eMBPoll( void ) case EV_EXECUTE: ucFunctionCode = ucMBFrame[MB_PDU_FUNC_OFF]; eException = MB_EX_ILLEGAL_FUNCTION; - for( i = 0; i < CONFIG_MB_FUNC_HANDLERS_MAX; i++ ) + for(i = 0; i < CONFIG_MB_FUNC_HANDLERS_MAX; i++) { /* No more function handlers registered. Abort. */ - if( xFuncHandlers[i].ucFunctionCode == 0 ) + if (xFuncHandlers[i].ucFunctionCode == 0) { break; } - else if( xFuncHandlers[i].ucFunctionCode == ucFunctionCode ) + else if (xFuncHandlers[i].ucFunctionCode == ucFunctionCode) { - eException = xFuncHandlers[i].pxHandler( ucMBFrame, &usLength ); + eException = xFuncHandlers[i].pxHandler(ucMBFrame, &usLength); break; } } /* If the request was not sent to the broadcast address we * return a reply. */ - if( ucRcvAddress != MB_ADDRESS_BROADCAST ) + if (ucRcvAddress != MB_ADDRESS_BROADCAST) { - if( eException != MB_EX_NONE ) + if (eException != MB_EX_NONE) { /* An exception occured. Build an error frame. */ usLength = 0; - ucMBFrame[usLength++] = ( uint8_t )( ucFunctionCode | MB_FUNC_ERROR ); + ucMBFrame[usLength++] = (uint8_t)(ucFunctionCode | MB_FUNC_ERROR); ucMBFrame[usLength++] = eException; } #ifdef CONFIG_MB_ASCII_ENABLED - if( ( eMBCurrentMode == MB_ASCII ) && CONFIG_MB_ASCII_TIMEOUT_WAIT_BEFORE_SEND_MS ) + if ((eMBCurrentMode == MB_ASCII) && CONFIG_MB_ASCII_TIMEOUT_WAIT_BEFORE_SEND_MS) { - vMBPortTimersDelay( CONFIG_MB_ASCII_TIMEOUT_WAIT_BEFORE_SEND_MS ); + vMBPortTimersDelay(CONFIG_MB_ASCII_TIMEOUT_WAIT_BEFORE_SEND_MS); } #endif - eStatus = peMBFrameSendCur( ucMBAddress, ucMBFrame, usLength ); + (void)peMBFrameSendCur(ucMBAddress, ucMBFrame, usLength); } break; diff --git a/apps/modbus/rtu/mbrtu.c b/apps/modbus/rtu/mbrtu.c index ee4f95fca..5bbf8c23d 100644 --- a/apps/modbus/rtu/mbrtu.c +++ b/apps/modbus/rtu/mbrtu.c @@ -80,16 +80,17 @@ static volatile uint16_t usRcvBufferPos; /* ----------------------- Start implementation -----------------------------*/ eMBErrorCode -eMBRTUInit( uint8_t ucSlaveAddress, uint8_t ucPort, speed_t ulBaudRate, eMBParity eParity ) +eMBRTUInit(uint8_t ucSlaveAddress, uint8_t ucPort, speed_t ulBaudRate, eMBParity eParity) { eMBErrorCode eStatus = MB_ENOERR; uint32_t usTimerT35_50us; - ( void )ucSlaveAddress; - ENTER_CRITICAL_SECTION( ); + (void)ucSlaveAddress; + ENTER_CRITICAL_SECTION(); /* Modbus RTU uses 8 Databits. */ - if( xMBPortSerialInit( ucPort, ulBaudRate, 8, eParity ) != true ) + + if (xMBPortSerialInit(ucPort, ulBaudRate, 8, eParity) != true) { eStatus = MB_EPORTERR; } @@ -98,7 +99,7 @@ eMBRTUInit( uint8_t ucSlaveAddress, uint8_t ucPort, speed_t ulBaudRate, eMBParit /* If baudrate > 19200 then we should use the fixed timer values * t35 = 1750us. Otherwise t35 must be 3.5 times the character time. */ - if( ulBaudRate > 19200 ) + if (ulBaudRate > 19200) { usTimerT35_50us = 35; /* 1800us. */ } @@ -106,101 +107,103 @@ eMBRTUInit( uint8_t ucSlaveAddress, uint8_t ucPort, speed_t ulBaudRate, eMBParit { /* The timer reload value for a character is given by: * - * ChTimeValue = Ticks_per_1s / ( Baudrate / 11 ) + * ChTimeValue = Ticks_per_1s / (Baudrate / 11) * = 11 * Ticks_per_1s / Baudrate * = 220000 / Baudrate * The reload for t3.5 is 1.5 times this value and similary * for t3.5. */ - usTimerT35_50us = ( 7UL * 220000UL ) / ( 2UL * ulBaudRate ); + usTimerT35_50us = (7UL * 220000UL) / (2UL * ulBaudRate); } - if( xMBPortTimersInit( ( uint16_t ) usTimerT35_50us ) != true ) + if (xMBPortTimersInit((uint16_t) usTimerT35_50us) != true) { eStatus = MB_EPORTERR; } } - EXIT_CRITICAL_SECTION( ); + EXIT_CRITICAL_SECTION(); return eStatus; } void -eMBRTUStart( void ) +eMBRTUStart(void) { - ENTER_CRITICAL_SECTION( ); + 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 * modbus protocol stack until the bus is free. */ eRcvState = STATE_RX_INIT; - vMBPortSerialEnable( true, false ); - vMBPortTimersEnable( ); + vMBPortSerialEnable(true, false); + vMBPortTimersEnable(); - EXIT_CRITICAL_SECTION( ); + EXIT_CRITICAL_SECTION(); } void -eMBRTUStop( void ) +eMBRTUStop(void) { - ENTER_CRITICAL_SECTION( ); - vMBPortSerialEnable( false, false ); - vMBPortTimersDisable( ); - EXIT_CRITICAL_SECTION( ); + ENTER_CRITICAL_SECTION(); + vMBPortSerialEnable(false, false); + vMBPortTimersDisable(); + EXIT_CRITICAL_SECTION(); } eMBErrorCode -eMBRTUReceive( uint8_t * pucRcvAddress, uint8_t ** pucFrame, uint16_t * pusLength ) +eMBRTUReceive(uint8_t * pucRcvAddress, uint8_t ** pucFrame, uint16_t * pusLength) { - bool xFrameReceived = false; - eMBErrorCode eStatus = MB_ENOERR; + eMBErrorCode eStatus = MB_ENOERR; - ENTER_CRITICAL_SECTION( ); - ASSERT( usRcvBufferPos < MB_SER_PDU_SIZE_MAX ); + ENTER_CRITICAL_SECTION(); + ASSERT(usRcvBufferPos < MB_SER_PDU_SIZE_MAX); /* Length and CRC check */ - if( ( usRcvBufferPos >= MB_SER_PDU_SIZE_MIN ) - && ( usMBCRC16( ( uint8_t * ) ucRTUBuf, usRcvBufferPos ) == 0 ) ) + + if ((usRcvBufferPos >= MB_SER_PDU_SIZE_MIN) + && (usMBCRC16((uint8_t *) ucRTUBuf, usRcvBufferPos) == 0)) { /* Save the address field. All frames are passed to the upper layed * and the decision if a frame is used is done there. */ - *pucRcvAddress = ucRTUBuf[MB_SER_PDU_ADDR_OFF]; + + *pucRcvAddress = ucRTUBuf[MB_SER_PDU_ADDR_OFF]; /* Total length of Modbus-PDU is Modbus-Serial-Line-PDU minus * size of address field and CRC checksum. */ - *pusLength = ( uint16_t )( usRcvBufferPos - MB_SER_PDU_PDU_OFF - MB_SER_PDU_SIZE_CRC ); + + *pusLength = (uint16_t)(usRcvBufferPos - MB_SER_PDU_PDU_OFF - MB_SER_PDU_SIZE_CRC); /* Return the start of the Modbus PDU to the caller. */ - *pucFrame = ( uint8_t * ) & ucRTUBuf[MB_SER_PDU_PDU_OFF]; - xFrameReceived = true; + + *pucFrame = (uint8_t *) & ucRTUBuf[MB_SER_PDU_PDU_OFF]; } else { eStatus = MB_EIO; } - EXIT_CRITICAL_SECTION( ); + EXIT_CRITICAL_SECTION(); return eStatus; } eMBErrorCode -eMBRTUSend( uint8_t ucSlaveAddress, const uint8_t * pucFrame, uint16_t usLength ) +eMBRTUSend(uint8_t ucSlaveAddress, const uint8_t * pucFrame, uint16_t usLength) { - eMBErrorCode eStatus = MB_ENOERR; - uint16_t usCRC16; + eMBErrorCode eStatus = MB_ENOERR; + uint16_t usCRC16; - ENTER_CRITICAL_SECTION( ); + ENTER_CRITICAL_SECTION(); /* Check if the receiver is still in idle state. If not we where to * slow with processing the received frame and the master sent another * frame on the network. We have to abort sending the frame. */ - if( eRcvState == STATE_RX_IDLE ) + if (eRcvState == STATE_RX_IDLE) { /* First byte before the Modbus-PDU is the slave address. */ - pucSndBufferCur = ( uint8_t * ) pucFrame - 1; + pucSndBufferCur = (uint8_t *) pucFrame - 1; usSndBufferCount = 1; /* Now copy the Modbus-PDU into the Modbus-Serial-Line-PDU. */ @@ -208,47 +211,47 @@ eMBRTUSend( uint8_t ucSlaveAddress, const uint8_t * pucFrame, uint16_t usLength usSndBufferCount += usLength; /* Calculate CRC16 checksum for Modbus-Serial-Line-PDU. */ - usCRC16 = usMBCRC16( ( uint8_t * ) pucSndBufferCur, usSndBufferCount ); - ucRTUBuf[usSndBufferCount++] = ( uint8_t )( usCRC16 & 0xFF ); - ucRTUBuf[usSndBufferCount++] = ( uint8_t )( usCRC16 >> 8 ); + usCRC16 = usMBCRC16((uint8_t *) pucSndBufferCur, usSndBufferCount); + ucRTUBuf[usSndBufferCount++] = (uint8_t)(usCRC16 & 0xFF); + ucRTUBuf[usSndBufferCount++] = (uint8_t)(usCRC16 >> 8); /* Activate the transmitter. */ eSndState = STATE_TX_XMIT; - vMBPortSerialEnable( false, true ); + vMBPortSerialEnable(false, true); } else { eStatus = MB_EIO; } - EXIT_CRITICAL_SECTION( ); + EXIT_CRITICAL_SECTION(); return eStatus; } bool -xMBRTUReceiveFSM( void ) +xMBRTUReceiveFSM(void) { bool xTaskNeedSwitch = false; uint8_t ucByte; - ASSERT( eSndState == STATE_TX_IDLE ); + ASSERT(eSndState == STATE_TX_IDLE); /* Always read the character. */ - ( void )xMBPortSerialGetByte( ( int8_t * ) & ucByte ); + (void)xMBPortSerialGetByte((int8_t *) & ucByte); - switch ( eRcvState ) + switch (eRcvState) { /* If we have received a character in the init state we have to * wait until the frame is finished. */ case STATE_RX_INIT: - vMBPortTimersEnable( ); + vMBPortTimersEnable(); break; /* In the error state we wait until all characters in the * damaged frame are transmitted. */ case STATE_RX_ERROR: - vMBPortTimersEnable( ); + vMBPortTimersEnable(); break; /* In the idle state we wait for a new character. If a character @@ -261,7 +264,7 @@ xMBRTUReceiveFSM( void ) eRcvState = STATE_RX_RCV; /* Enable t3.5 timers. */ - vMBPortTimersEnable( ); + vMBPortTimersEnable(); break; /* We are currently receiving a frame. Reset the timer after @@ -270,7 +273,7 @@ xMBRTUReceiveFSM( void ) * ignored. */ case STATE_RX_RCV: - if( usRcvBufferPos < MB_SER_PDU_SIZE_MAX ) + if (usRcvBufferPos < MB_SER_PDU_SIZE_MAX) { ucRTUBuf[usRcvBufferPos++] = ucByte; } @@ -278,42 +281,42 @@ xMBRTUReceiveFSM( void ) { eRcvState = STATE_RX_ERROR; } - vMBPortTimersEnable( ); + vMBPortTimersEnable(); break; } return xTaskNeedSwitch; } bool -xMBRTUTransmitFSM( void ) +xMBRTUTransmitFSM(void) { bool xNeedPoll = false; - ASSERT( eRcvState == STATE_RX_IDLE ); + ASSERT(eRcvState == STATE_RX_IDLE); - switch ( eSndState ) + switch (eSndState) { /* We should not get a transmitter event if the transmitter is in * idle state. */ case STATE_TX_IDLE: /* enable receiver/disable transmitter. */ - vMBPortSerialEnable( true, false ); + vMBPortSerialEnable(true, false); break; case STATE_TX_XMIT: /* check if we are finished. */ - if( usSndBufferCount != 0 ) + if (usSndBufferCount != 0) { - xMBPortSerialPutByte( ( int8_t )*pucSndBufferCur ); + xMBPortSerialPutByte((int8_t)*pucSndBufferCur); pucSndBufferCur++; /* next byte in sendbuffer. */ usSndBufferCount--; } else { - xNeedPoll = xMBPortEventPost( EV_FRAME_SENT ); + xNeedPoll = xMBPortEventPost(EV_FRAME_SENT); /* Disable transmitter. This prevents another transmit buffer * empty interrupt. */ - vMBPortSerialEnable( true, false ); + vMBPortSerialEnable(true, false); eSndState = STATE_TX_IDLE; } break; @@ -323,21 +326,21 @@ xMBRTUTransmitFSM( void ) } bool -xMBRTUTimerT35Expired( void ) +xMBRTUTimerT35Expired(void) { bool xNeedPoll = false; - switch ( eRcvState ) + switch (eRcvState) { /* Timer t35 expired. Startup phase is finished. */ case STATE_RX_INIT: - xNeedPoll = xMBPortEventPost( EV_READY ); + xNeedPoll = xMBPortEventPost(EV_READY); break; /* A frame was received and t35 expired. Notify the listener that * a new frame was received. */ case STATE_RX_RCV: - xNeedPoll = xMBPortEventPost( EV_FRAME_RECEIVED ); + xNeedPoll = xMBPortEventPost(EV_FRAME_RECEIVED); break; /* An error occured while receiving the frame. */ @@ -346,11 +349,11 @@ xMBRTUTimerT35Expired( void ) /* Function called in an illegal state. */ default: - ASSERT( ( eRcvState == STATE_RX_INIT ) || - ( eRcvState == STATE_RX_RCV ) || ( eRcvState == STATE_RX_ERROR ) ); + ASSERT((eRcvState == STATE_RX_INIT) || + (eRcvState == STATE_RX_RCV) || (eRcvState == STATE_RX_ERROR)); } - vMBPortTimersDisable( ); + vMBPortTimersDisable(); eRcvState = STATE_RX_IDLE; return xNeedPoll; |