diff options
Diffstat (limited to 'apps/modbus/rtu/mbrtu.c')
-rw-r--r-- | apps/modbus/rtu/mbrtu.c | 135 |
1 files changed, 69 insertions, 66 deletions
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; |