2015-04-07 23:57:52 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* apps/modbus/ascii/mbascii.c
|
|
|
|
*
|
2012-07-21 15:35:35 +02:00
|
|
|
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
|
|
|
|
* 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-07 23:57:52 +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"
|
|
|
|
|
2012-07-21 18:18:16 +02:00
|
|
|
#include <apps/modbus/mb.h>
|
|
|
|
#include <apps/modbus/mbframe.h>
|
|
|
|
#include <apps/modbus/mbport.h>
|
2012-07-21 15:35:35 +02:00
|
|
|
|
2012-07-21 18:18:16 +02:00
|
|
|
#include "mbascii.h"
|
2012-07-21 15:35:35 +02:00
|
|
|
#include "mbcrc.h"
|
|
|
|
|
2012-07-21 16:56:21 +02:00
|
|
|
#ifdef CONFIG_MB_ASCII_ENABLED
|
2012-07-21 15:35:35 +02:00
|
|
|
|
2015-04-07 23:57:52 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#define MB_ASCII_DEFAULT_CR '\r' /* Default CR character for Modbus ASCII. */
|
|
|
|
#define MB_ASCII_DEFAULT_LF '\n' /* Default LF character for Modbus ASCII. */
|
|
|
|
#define MB_SER_PDU_SIZE_MIN 3 /* Minimum size of a Modbus ASCII frame. */
|
|
|
|
#define MB_SER_PDU_SIZE_MAX 256 /* Maximum size of a Modbus ASCII frame. */
|
|
|
|
#define MB_SER_PDU_SIZE_LRC 1 /* Size of LRC 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-07 23:57:52 +02:00
|
|
|
STATE_RX_IDLE, /* Receiver is in idle state. */
|
|
|
|
STATE_RX_RCV, /* Frame is beeing received. */
|
|
|
|
STATE_RX_WAIT_EOF /* Wait for End of Frame. */
|
2012-07-21 15:35:35 +02:00
|
|
|
} eMBRcvState;
|
|
|
|
|
|
|
|
typedef enum
|
|
|
|
{
|
2015-04-07 23:57:52 +02:00
|
|
|
STATE_TX_IDLE, /* Transmitter is in idle state. */
|
|
|
|
STATE_TX_START, /* Starting transmission (':' sent). */
|
|
|
|
STATE_TX_DATA, /* Sending of data (Address, Data, LRC). */
|
|
|
|
STATE_TX_END, /* End of transmission. */
|
|
|
|
STATE_TX_NOTIFY /* Notify sender that the frame has been sent. */
|
2012-07-21 15:35:35 +02:00
|
|
|
} eMBSndState;
|
|
|
|
|
|
|
|
typedef enum
|
|
|
|
{
|
2015-04-07 23:57:52 +02:00
|
|
|
BYTE_HIGH_NIBBLE, /* Character for high nibble of byte. */
|
|
|
|
BYTE_LOW_NIBBLE /* Character for low nibble of byte. */
|
2012-07-21 15:35:35 +02:00
|
|
|
} eMBBytePos;
|
|
|
|
|
2015-04-07 23:57:52 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
2012-07-21 15:35:35 +02:00
|
|
|
|
2015-04-07 23:57:52 +02:00
|
|
|
static uint8_t prvucMBint8_t2BIN(uint8_t ucCharacter);
|
|
|
|
static uint8_t prvucMBBIN2int8_t(uint8_t ucByte);
|
|
|
|
static uint8_t prvucMBLRC(uint8_t *pucFrame, uint16_t usLen);
|
2012-07-21 15:35:35 +02:00
|
|
|
|
2015-04-07 23:57:52 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
2012-07-21 15:35:35 +02:00
|
|
|
|
|
|
|
static volatile eMBSndState eSndState;
|
|
|
|
static volatile eMBRcvState eRcvState;
|
|
|
|
|
|
|
|
/* We reuse the Modbus RTU buffer because only one buffer is needed and the
|
2015-04-07 23:57:52 +02:00
|
|
|
* RTU buffer is bigger.
|
|
|
|
*/
|
|
|
|
|
2012-07-21 23:23:18 +02:00
|
|
|
extern volatile uint8_t ucRTUBuf[];
|
|
|
|
static volatile uint8_t *ucASCIIBuf = ucRTUBuf;
|
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
|
|
|
static volatile eMBBytePos eBytePos;
|
|
|
|
|
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 uint8_t ucLRC;
|
|
|
|
static volatile uint8_t ucMBLFCharacter;
|
2012-07-21 15:35:35 +02:00
|
|
|
|
2015-04-07 23:57:52 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2015-04-07 23:57:52 +02:00
|
|
|
static uint8_t prvucMBint8_t2BIN(uint8_t ucCharacter)
|
|
|
|
{
|
|
|
|
if ((ucCharacter >= '0') && (ucCharacter <= '9'))
|
|
|
|
{
|
|
|
|
return (uint8_t)(ucCharacter - '0');
|
|
|
|
}
|
|
|
|
else if ((ucCharacter >= 'A') && (ucCharacter <= 'F'))
|
|
|
|
{
|
|
|
|
return (uint8_t)(ucCharacter - 'A' + 0x0A);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 0xFF;
|
|
|
|
}
|
|
|
|
}
|
2012-07-21 15:35:35 +02:00
|
|
|
|
2015-04-07 23:57:52 +02:00
|
|
|
static uint8_t prvucMBBIN2int8_t(uint8_t ucByte)
|
|
|
|
{
|
|
|
|
if (ucByte <= 0x09)
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-07 23:57:52 +02:00
|
|
|
return (uint8_t)('0' + ucByte);
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|
2015-04-07 23:57:52 +02:00
|
|
|
else if ((ucByte >= 0x0A) && (ucByte <= 0x0F))
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-07 23:57:52 +02:00
|
|
|
return (uint8_t)(ucByte - 0x0A + 'A');
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|
2015-04-07 23:57:52 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Programming error. */
|
2012-07-21 15:35:35 +02:00
|
|
|
|
2015-04-07 23:57:52 +02:00
|
|
|
ASSERT(0);
|
|
|
|
}
|
2012-07-21 15:35:35 +02:00
|
|
|
|
2015-04-07 23:57:52 +02:00
|
|
|
return '0';
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|
|
|
|
|
2015-04-07 23:57:52 +02:00
|
|
|
static uint8_t prvucMBLRC(uint8_t * pucFrame, uint16_t usLen)
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-07 23:57:52 +02:00
|
|
|
uint8_t ucLocalLRC = 0; /* LRC char initialized */
|
2012-07-21 15:35:35 +02:00
|
|
|
|
2015-04-07 23:57:52 +02:00
|
|
|
while (usLen--)
|
|
|
|
{
|
|
|
|
ucLocalLRC += *pucFrame++; /* Add buffer byte without carry */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return twos complement */
|
|
|
|
|
|
|
|
ucLocalLRC = (uint8_t) (-((int8_t) ucLocalLRC));
|
|
|
|
return ucLocalLRC;
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|
|
|
|
|
2015-04-07 23:57:52 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
eMBErrorCode eMBASCIIInit(uint8_t ucSlaveAddress, uint8_t ucPort,
|
|
|
|
speed_t ulBaudRate, eMBParity eParity)
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-07 23:57:52 +02:00
|
|
|
eMBErrorCode eStatus = MB_ENOERR;
|
|
|
|
(void)ucSlaveAddress;
|
|
|
|
|
|
|
|
ENTER_CRITICAL_SECTION();
|
|
|
|
ucMBLFCharacter = MB_ASCII_DEFAULT_LF;
|
|
|
|
|
|
|
|
if (xMBPortSerialInit(ucPort, ulBaudRate, 7, eParity) != true)
|
|
|
|
{
|
|
|
|
eStatus = MB_EPORTERR;
|
|
|
|
}
|
|
|
|
else if (xMBPortTimersInit(CONFIG_MB_ASCII_TIMEOUT_SEC * 20000UL) != true)
|
|
|
|
{
|
|
|
|
eStatus = MB_EPORTERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
EXIT_CRITICAL_SECTION();
|
|
|
|
return eStatus;
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|
|
|
|
|
2015-04-07 23:57:52 +02:00
|
|
|
void eMBASCIIStart(void)
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-07 23:57:52 +02:00
|
|
|
ENTER_CRITICAL_SECTION();
|
|
|
|
vMBPortSerialEnable(true, false);
|
|
|
|
eRcvState = STATE_RX_IDLE;
|
|
|
|
EXIT_CRITICAL_SECTION();
|
2012-07-21 15:35:35 +02:00
|
|
|
|
2015-04-07 23:57:52 +02:00
|
|
|
/* No special startup required for ASCII. */
|
2012-07-21 15:35:35 +02:00
|
|
|
|
2015-04-07 23:57:52 +02:00
|
|
|
(void)xMBPortEventPost(EV_READY);
|
|
|
|
}
|
|
|
|
|
|
|
|
void eMBASCIIStop(void)
|
|
|
|
{
|
|
|
|
ENTER_CRITICAL_SECTION();
|
|
|
|
vMBPortSerialEnable(false, false);
|
|
|
|
vMBPortTimersDisable();
|
|
|
|
EXIT_CRITICAL_SECTION();
|
|
|
|
}
|
|
|
|
|
|
|
|
eMBErrorCode eMBASCIIReceive(uint8_t *pucRcvAddress, uint8_t **pucFrame,
|
|
|
|
uint16_t *pusLength)
|
|
|
|
{
|
|
|
|
eMBErrorCode eStatus = MB_ENOERR;
|
|
|
|
|
|
|
|
ENTER_CRITICAL_SECTION();
|
|
|
|
ASSERT(usRcvBufferPos < MB_SER_PDU_SIZE_MAX);
|
|
|
|
|
|
|
|
/* Length and CRC check */
|
|
|
|
|
|
|
|
if ((usRcvBufferPos >= MB_SER_PDU_SIZE_MIN) &&
|
|
|
|
(prvucMBLRC((uint8_t *) ucASCIIBuf, usRcvBufferPos) == 0))
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-07 23:57:52 +02:00
|
|
|
/* Save the address field. All frames are passed to the upper layed
|
|
|
|
* and the decision if a frame is used is done there.
|
|
|
|
*/
|
|
|
|
|
|
|
|
*pucRcvAddress = ucASCIIBuf[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_LRC);
|
|
|
|
|
|
|
|
/* Return the start of the Modbus PDU to the caller. */
|
|
|
|
|
|
|
|
*pucFrame = (uint8_t *) & ucASCIIBuf[MB_SER_PDU_PDU_OFF];
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|
2015-04-07 23:57:52 +02:00
|
|
|
else
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-07 23:57:52 +02:00
|
|
|
eStatus = MB_EIO;
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|
2015-04-07 23:57:52 +02:00
|
|
|
|
|
|
|
EXIT_CRITICAL_SECTION();
|
|
|
|
return eStatus;
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|
|
|
|
|
2015-04-07 23:57:52 +02:00
|
|
|
eMBErrorCode eMBASCIISend(uint8_t ucSlaveAddress, const uint8_t *pucFrame,
|
|
|
|
uint16_t usLength)
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-07 23:57:52 +02:00
|
|
|
eMBErrorCode eStatus = MB_ENOERR;
|
|
|
|
uint8_t usLRC;
|
2012-07-21 15:35:35 +02:00
|
|
|
|
2015-04-07 23:57:52 +02:00
|
|
|
ENTER_CRITICAL_SECTION();
|
|
|
|
|
|
|
|
/* Check if the receiver is still in idle state. If not we where too
|
|
|
|
* 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)
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-07 23:57:52 +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;
|
2012-07-21 15:35:35 +02:00
|
|
|
|
2015-04-07 23:57:52 +02:00
|
|
|
/* Calculate LRC checksum for Modbus-Serial-Line-PDU. */
|
2012-07-21 15:35:35 +02:00
|
|
|
|
2015-04-07 23:57:52 +02:00
|
|
|
usLRC = prvucMBLRC((uint8_t *) pucSndBufferCur, usSndBufferCount);
|
|
|
|
ucASCIIBuf[usSndBufferCount++] = usLRC;
|
2012-07-21 15:35:35 +02:00
|
|
|
|
2015-04-07 23:57:52 +02:00
|
|
|
/* Activate the transmitter. */
|
|
|
|
|
|
|
|
eSndState = STATE_TX_START;
|
|
|
|
vMBPortSerialEnable(false, true);
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|
2015-04-07 23:57:52 +02:00
|
|
|
else
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-07 23:57:52 +02:00
|
|
|
eStatus = MB_EIO;
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|
2015-04-07 23:57:52 +02:00
|
|
|
|
|
|
|
EXIT_CRITICAL_SECTION();
|
|
|
|
return eStatus;
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|
|
|
|
|
2015-04-07 23:57:52 +02:00
|
|
|
bool xMBASCIIReceiveFSM(void)
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-07 23:57:52 +02:00
|
|
|
bool xNeedPoll = false;
|
|
|
|
uint8_t ucByte;
|
|
|
|
uint8_t ucResult;
|
2012-07-21 15:35:35 +02:00
|
|
|
|
2015-04-07 23:57:52 +02:00
|
|
|
ASSERT(eSndState == STATE_TX_IDLE);
|
2012-07-21 15:35:35 +02:00
|
|
|
|
2015-04-07 23:57:52 +02:00
|
|
|
(void)xMBPortSerialGetByte((int8_t *) & ucByte);
|
|
|
|
switch (eRcvState)
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-07 23:57:52 +02:00
|
|
|
/* A new character is received. If the character is a ':' the input
|
|
|
|
* buffer is cleared. A CR-character signals the end of the data
|
|
|
|
* block. Other characters are part of the data block and their
|
|
|
|
* ASCII value is converted back to a binary representation.
|
|
|
|
*/
|
|
|
|
|
2012-07-21 15:35:35 +02:00
|
|
|
case STATE_RX_RCV:
|
2015-04-07 23:57:52 +02:00
|
|
|
/* Enable timer for character timeout. */
|
|
|
|
|
|
|
|
vMBPortTimersEnable();
|
|
|
|
if (ucByte == ':')
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-07 23:57:52 +02:00
|
|
|
/* Empty receive buffer. */
|
|
|
|
|
|
|
|
eBytePos = BYTE_HIGH_NIBBLE;
|
|
|
|
usRcvBufferPos = 0;
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|
2015-04-07 23:57:52 +02:00
|
|
|
else if (ucByte == MB_ASCII_DEFAULT_CR)
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-07 23:57:52 +02:00
|
|
|
eRcvState = STATE_RX_WAIT_EOF;
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|
2015-04-07 23:57:52 +02:00
|
|
|
else
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-07 23:57:52 +02:00
|
|
|
ucResult = prvucMBint8_t2BIN(ucByte);
|
|
|
|
switch (eBytePos)
|
|
|
|
{
|
|
|
|
/* High nibble of the byte comes first. We check for
|
|
|
|
* a buffer overflow here.
|
|
|
|
*/
|
|
|
|
|
|
|
|
case BYTE_HIGH_NIBBLE:
|
|
|
|
if (usRcvBufferPos < MB_SER_PDU_SIZE_MAX)
|
|
|
|
{
|
|
|
|
ucASCIIBuf[usRcvBufferPos] = (uint8_t)(ucResult << 4);
|
|
|
|
eBytePos = BYTE_LOW_NIBBLE;
|
2012-07-21 15:35:35 +02:00
|
|
|
break;
|
2015-04-07 23:57:52 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* not handled in Modbus specification but seems
|
|
|
|
* a resonable implementation.
|
|
|
|
*/
|
2012-07-21 15:35:35 +02:00
|
|
|
|
2015-04-07 23:57:52 +02:00
|
|
|
eRcvState = STATE_RX_IDLE;
|
|
|
|
|
|
|
|
/* Disable previously activated timer because of error state. */
|
|
|
|
|
|
|
|
vMBPortTimersDisable();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BYTE_LOW_NIBBLE:
|
|
|
|
ucASCIIBuf[usRcvBufferPos] |= ucResult;
|
|
|
|
usRcvBufferPos++;
|
|
|
|
eBytePos = BYTE_HIGH_NIBBLE;
|
|
|
|
break;
|
|
|
|
}
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case STATE_RX_WAIT_EOF:
|
2015-04-07 23:57:52 +02:00
|
|
|
if (ucByte == ucMBLFCharacter)
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-07 23:57:52 +02:00
|
|
|
/* Disable character timeout timer because all characters are
|
|
|
|
* received.
|
|
|
|
*/
|
|
|
|
|
|
|
|
vMBPortTimersDisable();
|
|
|
|
|
|
|
|
/* Receiver is again in idle state. */
|
|
|
|
|
|
|
|
eRcvState = STATE_RX_IDLE;
|
|
|
|
|
|
|
|
/* Notify the caller of eMBASCIIReceive that a new frame
|
|
|
|
* was received.
|
|
|
|
*/
|
|
|
|
|
|
|
|
xNeedPoll = xMBPortEventPost(EV_FRAME_RECEIVED);
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|
2015-04-07 23:57:52 +02:00
|
|
|
else if (ucByte == ':')
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-07 23:57:52 +02:00
|
|
|
/* Empty receive buffer and back to receive state. */
|
|
|
|
|
|
|
|
eBytePos = BYTE_HIGH_NIBBLE;
|
|
|
|
usRcvBufferPos = 0;
|
|
|
|
eRcvState = STATE_RX_RCV;
|
2012-07-21 15:35:35 +02:00
|
|
|
|
2015-04-07 23:57:52 +02:00
|
|
|
/* Enable timer for character timeout. */
|
|
|
|
|
|
|
|
vMBPortTimersEnable();
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|
2015-04-07 23:57:52 +02:00
|
|
|
else
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-07 23:57:52 +02:00
|
|
|
/* Frame is not okay. Delete entire frame. */
|
|
|
|
|
|
|
|
eRcvState = STATE_RX_IDLE;
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case STATE_RX_IDLE:
|
2015-04-07 23:57:52 +02:00
|
|
|
if (ucByte == ':')
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-07 23:57:52 +02:00
|
|
|
/* Enable timer for character timeout. */
|
|
|
|
|
|
|
|
vMBPortTimersEnable();
|
|
|
|
|
|
|
|
/* Reset the input buffers to store the frame. */
|
|
|
|
|
|
|
|
usRcvBufferPos = 0;;
|
|
|
|
eBytePos = BYTE_HIGH_NIBBLE;
|
|
|
|
eRcvState = STATE_RX_RCV;
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-04-07 23:57:52 +02:00
|
|
|
return xNeedPoll;
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|
|
|
|
|
2015-04-07 23:57:52 +02:00
|
|
|
bool xMBASCIITransmitFSM(void)
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-07 23:57:52 +02:00
|
|
|
bool xNeedPoll = false;
|
|
|
|
uint8_t ucByte;
|
|
|
|
|
|
|
|
ASSERT(eRcvState == STATE_RX_IDLE);
|
|
|
|
switch (eSndState)
|
|
|
|
{
|
|
|
|
/* Start of transmission. The start of a frame is defined by sending
|
|
|
|
* the character ':'.
|
|
|
|
*/
|
|
|
|
|
|
|
|
case STATE_TX_START:
|
|
|
|
ucByte = ':';
|
|
|
|
xMBPortSerialPutByte((int8_t)ucByte);
|
|
|
|
eSndState = STATE_TX_DATA;
|
|
|
|
eBytePos = BYTE_HIGH_NIBBLE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Send the data block. Each data byte is encoded as a character hex
|
|
|
|
* stream with the high nibble sent first and the low nibble sent
|
|
|
|
* last. If all data bytes are exhausted we send a '\r' character
|
|
|
|
* to end the transmission.
|
|
|
|
*/
|
|
|
|
|
|
|
|
case STATE_TX_DATA:
|
|
|
|
if (usSndBufferCount > 0)
|
|
|
|
{
|
|
|
|
switch (eBytePos)
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-07 23:57:52 +02:00
|
|
|
case BYTE_HIGH_NIBBLE:
|
|
|
|
ucByte = prvucMBBIN2int8_t((uint8_t)(*pucSndBufferCur >> 4));
|
|
|
|
xMBPortSerialPutByte((int8_t) ucByte);
|
|
|
|
eBytePos = BYTE_LOW_NIBBLE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BYTE_LOW_NIBBLE:
|
|
|
|
ucByte = prvucMBBIN2int8_t((uint8_t)(*pucSndBufferCur & 0x0F));
|
|
|
|
xMBPortSerialPutByte((int8_t)ucByte);
|
|
|
|
pucSndBufferCur++;
|
|
|
|
eBytePos = BYTE_HIGH_NIBBLE;
|
|
|
|
usSndBufferCount--;
|
|
|
|
break;
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|
2015-04-07 23:57:52 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
xMBPortSerialPutByte(MB_ASCII_DEFAULT_CR);
|
|
|
|
eSndState = STATE_TX_END;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Finish the frame by sending a LF character. */
|
2012-07-21 15:35:35 +02:00
|
|
|
|
|
|
|
case STATE_TX_END:
|
2015-04-07 23:57:52 +02:00
|
|
|
xMBPortSerialPutByte((int8_t)ucMBLFCharacter);
|
2012-07-21 15:35:35 +02:00
|
|
|
|
2015-04-07 23:57:52 +02:00
|
|
|
/* We need another state to make sure that the CR character has
|
|
|
|
* been sent.
|
|
|
|
*/
|
2012-07-21 15:35:35 +02:00
|
|
|
|
2015-04-07 23:57:52 +02:00
|
|
|
eSndState = STATE_TX_NOTIFY;
|
|
|
|
break;
|
2012-07-21 15:35:35 +02:00
|
|
|
|
2015-04-07 23:57:52 +02:00
|
|
|
/* Notify the task which called eMBASCIISend that the frame has
|
|
|
|
* been sent.
|
|
|
|
*/
|
2012-07-21 15:35:35 +02:00
|
|
|
|
2015-04-07 23:57:52 +02:00
|
|
|
case STATE_TX_NOTIFY:
|
|
|
|
eSndState = STATE_TX_IDLE;
|
|
|
|
xNeedPoll = xMBPortEventPost(EV_FRAME_SENT);
|
2012-07-21 15:35:35 +02:00
|
|
|
|
2015-04-07 23:57:52 +02:00
|
|
|
/* Disable transmitter. This prevents another transmit buffer
|
|
|
|
* empty interrupt.
|
|
|
|
*/
|
2012-07-21 15:35:35 +02:00
|
|
|
|
2015-04-07 23:57:52 +02:00
|
|
|
vMBPortSerialEnable(true, false);
|
|
|
|
eSndState = STATE_TX_IDLE;
|
|
|
|
break;
|
2012-07-21 15:35:35 +02:00
|
|
|
|
2015-04-07 23:57:52 +02:00
|
|
|
/* We should not get a transmitter event if the transmitter is in
|
|
|
|
* idle state.
|
|
|
|
*/
|
2012-07-21 15:35:35 +02:00
|
|
|
|
2015-04-07 23:57:52 +02:00
|
|
|
case STATE_TX_IDLE:
|
|
|
|
/* enable receiver/disable transmitter. */
|
2012-07-21 15:35:35 +02:00
|
|
|
|
2015-04-07 23:57:52 +02:00
|
|
|
vMBPortSerialEnable(true, false);
|
|
|
|
break;
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|
2015-04-07 23:57:52 +02:00
|
|
|
|
|
|
|
return xNeedPoll;
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|
|
|
|
|
2015-04-07 23:57:52 +02:00
|
|
|
bool xMBASCIITimerT1SExpired(void)
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-07 23:57:52 +02:00
|
|
|
switch (eRcvState)
|
|
|
|
{
|
|
|
|
/* If we have a timeout we go back to the idle state and wait for
|
|
|
|
* the next frame.
|
|
|
|
*/
|
|
|
|
case STATE_RX_RCV:
|
|
|
|
case STATE_RX_WAIT_EOF:
|
|
|
|
eRcvState = STATE_RX_IDLE;
|
|
|
|
break;
|
2012-07-21 15:35:35 +02:00
|
|
|
|
2015-04-07 23:57:52 +02:00
|
|
|
default:
|
|
|
|
ASSERT((eRcvState == STATE_RX_RCV) || (eRcvState == STATE_RX_WAIT_EOF));
|
|
|
|
break;
|
|
|
|
}
|
2012-07-21 15:35:35 +02:00
|
|
|
|
2015-04-07 23:57:52 +02:00
|
|
|
vMBPortTimersDisable();
|
2012-07-21 15:35:35 +02:00
|
|
|
|
2015-04-07 23:57:52 +02:00
|
|
|
/* no context switch required. */
|
2012-07-21 15:35:35 +02:00
|
|
|
|
2015-04-07 23:57:52 +02:00
|
|
|
return false;
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|