2015-04-08 00:12:49 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* apps/modbus/rtu/mbrtu.c
|
|
|
|
*
|
2015-04-08 00:48:09 +02:00
|
|
|
* FreeModbus Library: A portable Modbus implementation for Modbus ASCII/RTU.
|
2012-07-21 15:35:35 +02:00
|
|
|
* Copyright (c) 2006 Christian Walter <wolti@sil.at>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
2015-04-08 00:12:49 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
2012-07-21 15:35:35 +02:00
|
|
|
|
2012-07-21 16:56:21 +02:00
|
|
|
#include <nuttx/config.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2012-07-21 18:18:16 +02:00
|
|
|
#include <assert.h>
|
2012-07-21 15:35:35 +02:00
|
|
|
|
|
|
|
#include "port.h"
|
|
|
|
|
2016-11-03 14:54:32 +01:00
|
|
|
#include "modbus/mb.h"
|
2016-07-11 18:11:18 +02:00
|
|
|
#include "modbus/mbframe.h"
|
|
|
|
#include "modbus/mbport.h"
|
2012-07-21 15:35:35 +02:00
|
|
|
|
2012-07-21 18:18:16 +02:00
|
|
|
#include "mbrtu.h"
|
2012-07-21 15:35:35 +02:00
|
|
|
#include "mbcrc.h"
|
|
|
|
|
2015-04-08 00:12:49 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#define MB_SER_PDU_SIZE_MIN 4 /* Minimum size of a Modbus RTU frame. */
|
|
|
|
#define MB_SER_PDU_SIZE_MAX 256 /* Maximum size of a Modbus RTU frame. */
|
|
|
|
#define MB_SER_PDU_SIZE_CRC 2 /* Size of CRC field in PDU. */
|
|
|
|
#define MB_SER_PDU_ADDR_OFF 0 /* Offset of slave address in Ser-PDU. */
|
|
|
|
#define MB_SER_PDU_PDU_OFF 1 /* Offset of Modbus-PDU in Ser-PDU. */
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Type Definitions
|
|
|
|
****************************************************************************/
|
2012-07-21 15:35:35 +02:00
|
|
|
|
|
|
|
typedef enum
|
|
|
|
{
|
2015-04-08 00:12:49 +02:00
|
|
|
STATE_RX_INIT, /* Receiver is in initial state. */
|
|
|
|
STATE_RX_IDLE, /* Receiver is in idle state. */
|
|
|
|
STATE_RX_RCV, /* Frame is being received. */
|
|
|
|
STATE_RX_ERROR /* If the frame is invalid. */
|
2012-07-21 15:35:35 +02:00
|
|
|
} eMBRcvState;
|
|
|
|
|
|
|
|
typedef enum
|
|
|
|
{
|
2015-04-08 00:12:49 +02:00
|
|
|
STATE_TX_IDLE, /* Transmitter is in idle state. */
|
|
|
|
STATE_TX_XMIT /* Transmitter is in transfer state. */
|
2012-07-21 15:35:35 +02:00
|
|
|
} eMBSndState;
|
|
|
|
|
2015-04-08 00:12:49 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-07-21 15:35:35 +02:00
|
|
|
static volatile eMBSndState eSndState;
|
|
|
|
static volatile eMBRcvState eRcvState;
|
|
|
|
|
2012-07-21 23:23:18 +02:00
|
|
|
volatile uint8_t ucRTUBuf[MB_SER_PDU_SIZE_MAX];
|
2012-07-21 15:35:35 +02:00
|
|
|
|
2012-07-21 23:23:18 +02:00
|
|
|
static volatile uint8_t *pucSndBufferCur;
|
|
|
|
static volatile uint16_t usSndBufferCount;
|
2012-07-21 15:35:35 +02:00
|
|
|
|
2012-07-21 23:23:18 +02:00
|
|
|
static volatile uint16_t usRcvBufferPos;
|
2012-07-21 15:35:35 +02:00
|
|
|
|
2015-04-08 00:12:49 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
eMBErrorCode eMBRTUInit(uint8_t ucSlaveAddress, uint8_t ucPort,
|
|
|
|
speed_t ulBaudRate, eMBParity eParity)
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-08 00:12:49 +02:00
|
|
|
eMBErrorCode eStatus = MB_ENOERR;
|
|
|
|
uint32_t usTimerT35_50us;
|
2012-07-21 15:35:35 +02:00
|
|
|
|
2015-04-08 00:12:49 +02:00
|
|
|
ENTER_CRITICAL_SECTION();
|
2012-07-21 15:35:35 +02:00
|
|
|
|
2015-04-08 00:12:49 +02:00
|
|
|
/* Modbus RTU uses 8 Databits. */
|
2014-11-25 22:14:28 +01:00
|
|
|
|
2015-04-08 00:12:49 +02:00
|
|
|
if (xMBPortSerialInit(ucPort, ulBaudRate, 8, eParity) != true)
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-08 00:12:49 +02:00
|
|
|
eStatus = MB_EPORTERR;
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|
2015-04-08 00:12:49 +02:00
|
|
|
else
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-08 00:12:49 +02:00
|
|
|
/* 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)
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-08 00:12:49 +02:00
|
|
|
usTimerT35_50us = 35; /* 1800us. */
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|
2015-04-08 00:12:49 +02:00
|
|
|
else
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-08 00:12:49 +02:00
|
|
|
/* The timer reload value for a character is given by:
|
|
|
|
*
|
|
|
|
* ChTimeValue = Ticks_per_1s / (Baudrate / 11)
|
|
|
|
* = 11 * Ticks_per_1s / Baudrate
|
|
|
|
* = 220000 / Baudrate
|
2020-02-23 05:51:44 +01:00
|
|
|
* The reload for t3.5 is 1.5 times this value and similarly
|
2015-04-08 00:12:49 +02:00
|
|
|
* for t3.5.
|
|
|
|
*/
|
|
|
|
|
|
|
|
usTimerT35_50us = (7UL * 220000UL) / (2UL * ulBaudRate);
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|
2015-04-08 00:12:49 +02:00
|
|
|
|
|
|
|
if (xMBPortTimersInit((uint16_t) usTimerT35_50us) != true)
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-08 00:12:49 +02:00
|
|
|
eStatus = MB_EPORTERR;
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-08 00:12:49 +02:00
|
|
|
EXIT_CRITICAL_SECTION();
|
|
|
|
return eStatus;
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|
|
|
|
|
2015-04-08 00:12:49 +02:00
|
|
|
void eMBRTUStart(void)
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-08 00:12:49 +02:00
|
|
|
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();
|
|
|
|
|
|
|
|
EXIT_CRITICAL_SECTION();
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|
|
|
|
|
2015-04-08 00:12:49 +02:00
|
|
|
void eMBRTUStop(void)
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-08 00:12:49 +02:00
|
|
|
ENTER_CRITICAL_SECTION();
|
|
|
|
vMBPortSerialEnable(false, false);
|
|
|
|
vMBPortTimersDisable();
|
|
|
|
EXIT_CRITICAL_SECTION();
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|
|
|
|
|
2015-04-08 00:12:49 +02:00
|
|
|
eMBErrorCode eMBRTUReceive(uint8_t *pucRcvAddress, uint8_t **pucFrame,
|
|
|
|
uint16_t *pusLength)
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-08 00:12:49 +02:00
|
|
|
eMBErrorCode eStatus = MB_ENOERR;
|
2012-07-21 15:35:35 +02:00
|
|
|
|
2015-04-08 00:12:49 +02:00
|
|
|
ENTER_CRITICAL_SECTION();
|
2018-08-24 14:19:35 +02:00
|
|
|
DEBUGASSERT(usRcvBufferPos < MB_SER_PDU_SIZE_MAX);
|
2012-07-21 15:35:35 +02:00
|
|
|
|
2015-04-08 00:12:49 +02:00
|
|
|
/* Length and CRC check */
|
2014-11-25 22:14:28 +01:00
|
|
|
|
2015-04-08 00:12:49 +02:00
|
|
|
if ((usRcvBufferPos >= MB_SER_PDU_SIZE_MIN) &&
|
|
|
|
(usMBCRC16((uint8_t *) ucRTUBuf, usRcvBufferPos) == 0))
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2020-02-22 19:52:12 +01:00
|
|
|
/* Save the address field. All frames are passed to the upper laid
|
2015-04-08 00:12:49 +02:00
|
|
|
* and the decision if a frame is used is done there.
|
|
|
|
*/
|
2014-11-25 22:14:28 +01:00
|
|
|
|
2015-04-08 00:12:49 +02:00
|
|
|
*pucRcvAddress = ucRTUBuf[MB_SER_PDU_ADDR_OFF];
|
2012-07-21 15:35:35 +02:00
|
|
|
|
2015-04-08 00:12:49 +02:00
|
|
|
/* Total length of Modbus-PDU is Modbus-Serial-Line-PDU minus
|
|
|
|
* size of address field and CRC checksum.
|
|
|
|
*/
|
2014-11-25 22:14:28 +01:00
|
|
|
|
2015-04-08 00:12:49 +02:00
|
|
|
*pusLength = (uint16_t)(usRcvBufferPos - MB_SER_PDU_PDU_OFF - MB_SER_PDU_SIZE_CRC);
|
2012-07-21 15:35:35 +02:00
|
|
|
|
2015-04-08 00:12:49 +02:00
|
|
|
/* Return the start of the Modbus PDU to the caller. */
|
2014-11-25 22:14:28 +01:00
|
|
|
|
2015-04-08 00:12:49 +02:00
|
|
|
*pucFrame = (uint8_t *) & ucRTUBuf[MB_SER_PDU_PDU_OFF];
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|
2015-04-08 00:12:49 +02:00
|
|
|
else
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-08 00:12:49 +02:00
|
|
|
eStatus = MB_EIO;
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|
|
|
|
|
2015-04-08 00:12:49 +02:00
|
|
|
EXIT_CRITICAL_SECTION();
|
|
|
|
return eStatus;
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|
|
|
|
|
2015-04-08 00:12:49 +02:00
|
|
|
eMBErrorCode eMBRTUSend(uint8_t ucSlaveAddress, const uint8_t *pucFrame, uint16_t usLength)
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-08 00:12:49 +02:00
|
|
|
eMBErrorCode eStatus = MB_ENOERR;
|
|
|
|
uint16_t usCRC16;
|
|
|
|
|
|
|
|
ENTER_CRITICAL_SECTION();
|
2012-07-21 15:35:35 +02:00
|
|
|
|
2015-04-08 00:12:49 +02:00
|
|
|
/* 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.
|
|
|
|
*/
|
2012-07-21 15:35:35 +02:00
|
|
|
|
2015-04-08 00:12:49 +02:00
|
|
|
if (eRcvState == STATE_RX_IDLE)
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-08 00:12:49 +02:00
|
|
|
/* First byte before the Modbus-PDU is the slave address. */
|
|
|
|
|
|
|
|
pucSndBufferCur = (uint8_t *) pucFrame - 1;
|
|
|
|
usSndBufferCount = 1;
|
|
|
|
|
|
|
|
/* Now copy the Modbus-PDU into the Modbus-Serial-Line-PDU. */
|
|
|
|
|
|
|
|
pucSndBufferCur[MB_SER_PDU_ADDR_OFF] = ucSlaveAddress;
|
|
|
|
usSndBufferCount += usLength;
|
|
|
|
|
|
|
|
/* Calculate CRC16 checksum for Modbus-Serial-Line-PDU. */
|
2015-10-03 19:03:42 +02:00
|
|
|
|
2015-04-08 00:12:49 +02:00
|
|
|
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);
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|
2015-04-08 00:12:49 +02:00
|
|
|
else
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-08 00:12:49 +02:00
|
|
|
eStatus = MB_EIO;
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|
2015-04-08 00:12:49 +02:00
|
|
|
|
|
|
|
EXIT_CRITICAL_SECTION();
|
|
|
|
return eStatus;
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|
|
|
|
|
2015-04-08 00:12:49 +02:00
|
|
|
bool xMBRTUReceiveFSM(void)
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-08 00:12:49 +02:00
|
|
|
bool xTaskNeedSwitch = false;
|
|
|
|
uint8_t ucByte;
|
2012-07-21 15:35:35 +02:00
|
|
|
|
2018-08-24 14:19:35 +02:00
|
|
|
DEBUGASSERT(eSndState == STATE_TX_IDLE);
|
2012-07-21 15:35:35 +02:00
|
|
|
|
2015-04-08 00:12:49 +02:00
|
|
|
/* Always read the character. */
|
2012-07-21 15:35:35 +02:00
|
|
|
|
2020-01-02 13:09:50 +01:00
|
|
|
xMBPortSerialGetByte((int8_t *) & ucByte);
|
2015-04-08 00:12:49 +02:00
|
|
|
|
|
|
|
switch (eRcvState)
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-08 00:12:49 +02:00
|
|
|
/* If we have received a character in the init state we have to
|
|
|
|
* wait until the frame is finished.
|
|
|
|
*/
|
|
|
|
|
|
|
|
case STATE_RX_INIT:
|
2014-11-25 22:14:28 +01:00
|
|
|
vMBPortTimersEnable();
|
2012-07-21 15:35:35 +02:00
|
|
|
break;
|
|
|
|
|
2015-04-08 00:12:49 +02:00
|
|
|
/* In the error state we wait until all characters in the
|
|
|
|
* damaged frame are transmitted.
|
|
|
|
*/
|
|
|
|
|
|
|
|
case STATE_RX_ERROR:
|
2014-11-25 22:14:28 +01:00
|
|
|
vMBPortTimersEnable();
|
2012-07-21 15:35:35 +02:00
|
|
|
break;
|
|
|
|
|
2015-04-08 00:12:49 +02:00
|
|
|
/* In the idle state we wait for a new character. If a character
|
|
|
|
* is received the t1.5 and t3.5 timers are started and the
|
|
|
|
* receiver is in the state STATE_RX_RECEIVCE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
case STATE_RX_IDLE:
|
2012-07-21 15:35:35 +02:00
|
|
|
usRcvBufferPos = 0;
|
|
|
|
ucRTUBuf[usRcvBufferPos++] = ucByte;
|
|
|
|
eRcvState = STATE_RX_RCV;
|
|
|
|
|
|
|
|
/* Enable t3.5 timers. */
|
2015-04-08 00:12:49 +02:00
|
|
|
|
2014-11-25 22:14:28 +01:00
|
|
|
vMBPortTimersEnable();
|
2012-07-21 15:35:35 +02:00
|
|
|
break;
|
|
|
|
|
2015-04-08 00:12:49 +02:00
|
|
|
/* We are currently receiving a frame. Reset the timer after
|
|
|
|
* every character received. If more than the maximum possible
|
|
|
|
* number of bytes in a modbus frame is received the frame is
|
|
|
|
* ignored.
|
|
|
|
*/
|
|
|
|
|
|
|
|
case STATE_RX_RCV:
|
2014-11-25 22:14:28 +01:00
|
|
|
if (usRcvBufferPos < MB_SER_PDU_SIZE_MAX)
|
2015-04-08 00:12:49 +02:00
|
|
|
{
|
2012-07-21 15:35:35 +02:00
|
|
|
ucRTUBuf[usRcvBufferPos++] = ucByte;
|
2015-04-08 00:12:49 +02:00
|
|
|
}
|
2012-07-21 15:35:35 +02:00
|
|
|
else
|
2015-04-08 00:12:49 +02:00
|
|
|
{
|
2012-07-21 15:35:35 +02:00
|
|
|
eRcvState = STATE_RX_ERROR;
|
2015-04-08 00:12:49 +02:00
|
|
|
}
|
|
|
|
|
2014-11-25 22:14:28 +01:00
|
|
|
vMBPortTimersEnable();
|
2012-07-21 15:35:35 +02:00
|
|
|
break;
|
|
|
|
}
|
2015-04-08 00:12:49 +02:00
|
|
|
|
|
|
|
return xTaskNeedSwitch;
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|
|
|
|
|
2015-04-08 00:12:49 +02:00
|
|
|
bool xMBRTUTransmitFSM(void)
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-08 00:12:49 +02:00
|
|
|
bool xNeedPoll = false;
|
2012-07-21 15:35:35 +02:00
|
|
|
|
2018-08-24 14:19:35 +02:00
|
|
|
DEBUGASSERT(eRcvState == STATE_RX_IDLE);
|
2012-07-21 15:35:35 +02:00
|
|
|
|
2015-04-08 00:12:49 +02:00
|
|
|
switch (eSndState)
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-08 00:12:49 +02:00
|
|
|
/* We should not get a transmitter event if the transmitter is in
|
|
|
|
* idle state.
|
|
|
|
*/
|
|
|
|
|
2012-07-21 15:35:35 +02:00
|
|
|
case STATE_TX_IDLE:
|
2015-04-08 00:12:49 +02:00
|
|
|
/* enable receiver/disable transmitter. */
|
|
|
|
|
|
|
|
vMBPortSerialEnable(true, false);
|
|
|
|
break;
|
2012-07-21 15:35:35 +02:00
|
|
|
|
|
|
|
case STATE_TX_XMIT:
|
2015-04-08 00:12:49 +02:00
|
|
|
/* check if we are finished. */
|
|
|
|
|
|
|
|
if (usSndBufferCount != 0)
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-08 00:12:49 +02:00
|
|
|
xMBPortSerialPutByte((int8_t)*pucSndBufferCur);
|
|
|
|
pucSndBufferCur++; /* next byte in sendbuffer. */
|
|
|
|
usSndBufferCount--;
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|
2015-04-08 00:12:49 +02:00
|
|
|
else
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-08 00:12:49 +02:00
|
|
|
xNeedPoll = xMBPortEventPost(EV_FRAME_SENT);
|
|
|
|
|
|
|
|
/* Disable transmitter. This prevents another transmit buffer
|
|
|
|
* empty interrupt.
|
|
|
|
*/
|
|
|
|
|
|
|
|
vMBPortSerialEnable(true, false);
|
|
|
|
eSndState = STATE_TX_IDLE;
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|
2015-04-08 00:12:49 +02:00
|
|
|
break;
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|
|
|
|
|
2015-04-08 00:12:49 +02:00
|
|
|
return xNeedPoll;
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|
|
|
|
|
2015-04-08 00:12:49 +02:00
|
|
|
bool xMBRTUTimerT35Expired(void)
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-08 00:12:49 +02:00
|
|
|
bool xNeedPoll = false;
|
2012-07-21 15:35:35 +02:00
|
|
|
|
2015-04-08 00:12:49 +02:00
|
|
|
switch (eRcvState)
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-08 00:12:49 +02:00
|
|
|
/* Timer t35 expired. Start-up phase is finished. */
|
|
|
|
|
|
|
|
case STATE_RX_INIT:
|
2014-11-25 22:14:28 +01:00
|
|
|
xNeedPoll = xMBPortEventPost(EV_READY);
|
2012-07-21 15:35:35 +02:00
|
|
|
break;
|
|
|
|
|
2015-04-08 00:12:49 +02:00
|
|
|
/* A frame was received and t35 expired. Notify the listener that
|
|
|
|
* a new frame was received.
|
|
|
|
*/
|
|
|
|
|
|
|
|
case STATE_RX_RCV:
|
2014-11-25 22:14:28 +01:00
|
|
|
xNeedPoll = xMBPortEventPost(EV_FRAME_RECEIVED);
|
2012-07-21 15:35:35 +02:00
|
|
|
break;
|
|
|
|
|
2015-04-08 00:12:49 +02:00
|
|
|
/* An error occurred while receiving the frame. */
|
|
|
|
|
|
|
|
case STATE_RX_ERROR:
|
2012-07-21 15:35:35 +02:00
|
|
|
break;
|
|
|
|
|
2015-04-08 00:12:49 +02:00
|
|
|
/* Function called in an illegal state. */
|
|
|
|
|
|
|
|
default:
|
2018-08-24 14:19:35 +02:00
|
|
|
DEBUGASSERT((eRcvState == STATE_RX_INIT) ||
|
2015-04-08 00:12:49 +02:00
|
|
|
(eRcvState == STATE_RX_RCV) ||
|
|
|
|
(eRcvState == STATE_RX_ERROR));
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|
|
|
|
|
2015-04-08 00:12:49 +02:00
|
|
|
vMBPortTimersDisable();
|
|
|
|
eRcvState = STATE_RX_IDLE;
|
2012-07-21 15:35:35 +02:00
|
|
|
|
2015-04-08 00:12:49 +02:00
|
|
|
return xNeedPoll;
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|