mirror of
https://github.com/PX4/PX4-Autopilot.git
synced 2026-05-27 10:17:45 +08:00
Use NuttX types in FreeModBus port; Add FreeModBus demo at apps/examples/modbus; Add new termios APIs
git-svn-id: https://nuttx.svn.sourceforge.net/svnroot/nuttx/trunk@4964 7fd9a85b-ad96-42d3-883c-3090e2eb8679
This commit is contained in:
+6
-2
@@ -251,5 +251,9 @@
|
|||||||
6.21 2012-xx-xx Gregory Nutt <gnutt@nuttx.org>
|
6.21 2012-xx-xx Gregory Nutt <gnutt@nuttx.org>
|
||||||
|
|
||||||
* apps/include/: Stylistic clean-up of all header files.
|
* apps/include/: Stylistic clean-up of all header files.
|
||||||
* apps/modbus and apps/include/modbus: The beginnings of a port of
|
* apps/modbus and apps/include/modbus: A port of freemodbus-v1.5.0
|
||||||
freemodbus-v1.5.0.
|
has been added to the NuttX apps/ source tree.
|
||||||
|
* apps/examples/modbus: A port of the freemodbus-v1.5.0 "demo"
|
||||||
|
program that will be used to verify the FreeModBus port
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -63,6 +63,10 @@ menu "File system mount example"
|
|||||||
source "$APPSDIR/examples/mount/Kconfig"
|
source "$APPSDIR/examples/mount/Kconfig"
|
||||||
endmenu
|
endmenu
|
||||||
|
|
||||||
|
menu "FreeModBus example"
|
||||||
|
source "$APPSDIR/examples/modbus/Kconfig"
|
||||||
|
endmenu
|
||||||
|
|
||||||
menu "Network test example"
|
menu "Network test example"
|
||||||
source "$APPSDIR/examples/nettest/Kconfig"
|
source "$APPSDIR/examples/nettest/Kconfig"
|
||||||
endmenu
|
endmenu
|
||||||
|
|||||||
@@ -38,10 +38,10 @@
|
|||||||
# Sub-directories
|
# Sub-directories
|
||||||
|
|
||||||
SUBDIRS = adc buttons can cdcacm composite dhcpd ftpc ftpd hello helloxx \
|
SUBDIRS = adc buttons can cdcacm composite dhcpd ftpc ftpd hello helloxx \
|
||||||
hidkbd igmp lcdrw mm mount nettest nsh null nx nxconsole nxffs nxflat \
|
hidkbd igmp lcdrw mm modbus mount nettest nsh null nx nxconsole nxffs \
|
||||||
nxhello nximage nxlines nxtext ostest pashello pipe poll pwm qencoder \
|
nxflat nxhello nximage nxlines nxtext ostest pashello pipe poll pwm \
|
||||||
rgmp romfs serloop telnetd thttpd tiff touchscreen udp uip usbserial \
|
qencoder rgmp romfs serloop telnetd thttpd tiff touchscreen udp uip \
|
||||||
sendmail usbstorage usbterm watchdog wget wlan
|
usbserial sendmail usbstorage usbterm watchdog wget wlan
|
||||||
|
|
||||||
# Sub-directories that might need context setup. Directories may need
|
# Sub-directories that might need context setup. Directories may need
|
||||||
# context setup for a variety of reasons, but the most common is because
|
# context setup for a variety of reasons, but the most common is because
|
||||||
@@ -56,7 +56,7 @@ SUBDIRS = adc buttons can cdcacm composite dhcpd ftpc ftpd hello helloxx \
|
|||||||
CNTXTDIRS = pwm
|
CNTXTDIRS = pwm
|
||||||
|
|
||||||
ifeq ($(CONFIG_NSH_BUILTIN_APPS),y)
|
ifeq ($(CONFIG_NSH_BUILTIN_APPS),y)
|
||||||
CNTXTDIRS += adc can cdcacm composite ftpd dhcpd nettest qencoder telnetd watchdog
|
CNTXTDIRS += adc can cdcacm composite ftpd dhcpd modbus nettest qencoder telnetd watchdog
|
||||||
endif
|
endif
|
||||||
|
|
||||||
ifeq ($(CONFIG_EXAMPLES_HELLO_BUILTIN),y)
|
ifeq ($(CONFIG_EXAMPLES_HELLO_BUILTIN),y)
|
||||||
|
|||||||
@@ -483,6 +483,21 @@ examples/mm
|
|||||||
advantage that it runs in the actual NuttX tasking environment (the
|
advantage that it runs in the actual NuttX tasking environment (the
|
||||||
mm/mm_test.c only runs in a PC simulation environment).
|
mm/mm_test.c only runs in a PC simulation environment).
|
||||||
|
|
||||||
|
examples/modbus
|
||||||
|
^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
This is a port of the FreeModbus Linux demo. It derives from the
|
||||||
|
demos/LINUX directory of the FreeModBus version 1.5.0 (June 6, 2010)
|
||||||
|
that can be downloaded in its entirety from http://developer.berlios.de/project/showfiles.php?group_id=6120.
|
||||||
|
|
||||||
|
CONFIG_EXAMPLES_MODBUS_REG_INPUT_START, Default 1000
|
||||||
|
CONFIG_EXAMPLES_MODBUS_REG_INPUT_NREGS, Default 4
|
||||||
|
CONFIG_EXAMPLES_MODBUS_REG_HOLDING_START, Default 2000
|
||||||
|
CONFIG_EXAMPLES_MODBUS_REG_HOLDING_NREGS, Default 130
|
||||||
|
|
||||||
|
The FreeModBus library resides at apps/modbus. See apps/modbus/README.txt
|
||||||
|
for additional configuration information.
|
||||||
|
|
||||||
examples/mount
|
examples/mount
|
||||||
^^^^^^^^^^^^^^
|
^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
|||||||
Executable → Regular
@@ -0,0 +1,13 @@
|
|||||||
|
#
|
||||||
|
# For a description of the syntax of this configuration file,
|
||||||
|
# see misc/tools/kconfig-language.txt.
|
||||||
|
#
|
||||||
|
|
||||||
|
config EXAMPLES_MODBUS
|
||||||
|
bool "FreeModBus example"
|
||||||
|
default n
|
||||||
|
---help---
|
||||||
|
Enable the FreeModBus example
|
||||||
|
|
||||||
|
if EXAMPLES_MODBUS
|
||||||
|
endif
|
||||||
@@ -0,0 +1,105 @@
|
|||||||
|
############################################################################
|
||||||
|
# apps/examples/modbus/Makefile
|
||||||
|
#
|
||||||
|
# Copyright (C) 2012 Gregory Nutt. All rights reserved.
|
||||||
|
# Author: Gregory Nutt <gnutt@nuttx.org>
|
||||||
|
#
|
||||||
|
# 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. Neither the name NuttX nor the names of its contributors may be
|
||||||
|
# used to endorse or promote products derived from this software
|
||||||
|
# without specific prior written permission.
|
||||||
|
#
|
||||||
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||||
|
# "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
|
||||||
|
# COPYRIGHT OWNER OR CONTRIBUTORS 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.
|
||||||
|
#
|
||||||
|
############################################################################
|
||||||
|
|
||||||
|
-include $(TOPDIR)/.config
|
||||||
|
-include $(TOPDIR)/Make.defs
|
||||||
|
include $(APPDIR)/Make.defs
|
||||||
|
|
||||||
|
# FreeModBus demo built-in application info
|
||||||
|
|
||||||
|
APPNAME = modbus
|
||||||
|
PRIORITY = SCHED_PRIORITY_DEFAULT
|
||||||
|
STACKSIZE = 2048
|
||||||
|
|
||||||
|
# FreeModBus demo
|
||||||
|
|
||||||
|
ASRCS =
|
||||||
|
CSRCS = modbus_main.c
|
||||||
|
|
||||||
|
AOBJS = $(ASRCS:.S=$(OBJEXT))
|
||||||
|
COBJS = $(CSRCS:.c=$(OBJEXT))
|
||||||
|
|
||||||
|
SRCS = $(ASRCS) $(CSRCS)
|
||||||
|
OBJS = $(AOBJS) $(COBJS)
|
||||||
|
|
||||||
|
ifeq ($(WINTOOL),y)
|
||||||
|
BIN = "${shell cygpath -w $(APPDIR)/libapps$(LIBEXT)}"
|
||||||
|
else
|
||||||
|
BIN = "$(APPDIR)/libapps$(LIBEXT)"
|
||||||
|
endif
|
||||||
|
|
||||||
|
ROOTDEPPATH = --dep-path .
|
||||||
|
|
||||||
|
# Common build
|
||||||
|
|
||||||
|
VPATH =
|
||||||
|
|
||||||
|
all: .built
|
||||||
|
.PHONY: clean depend distclean
|
||||||
|
|
||||||
|
$(AOBJS): %$(OBJEXT): %.S
|
||||||
|
$(call ASSEMBLE, $<, $@)
|
||||||
|
|
||||||
|
$(COBJS): %$(OBJEXT): %.c
|
||||||
|
$(call COMPILE, $<, $@)
|
||||||
|
|
||||||
|
.built: $(OBJS)
|
||||||
|
@( for obj in $(OBJS) ; do \
|
||||||
|
$(call ARCHIVE, $(BIN), $${obj}); \
|
||||||
|
done ; )
|
||||||
|
@touch .built
|
||||||
|
|
||||||
|
.context:
|
||||||
|
ifeq ($(CONFIG_NSH_BUILTIN_APPS),y)
|
||||||
|
$(call REGISTER,$(APPNAME),$(PRIORITY),$(STACKSIZE),$(APPNAME)_main)
|
||||||
|
@touch $@
|
||||||
|
endif
|
||||||
|
|
||||||
|
context: .context
|
||||||
|
|
||||||
|
.depend: Makefile $(SRCS)
|
||||||
|
@$(MKDEP) $(ROOTDEPPATH) $(CC) -- $(CFLAGS) -- $(SRCS) >Make.dep
|
||||||
|
@touch $@
|
||||||
|
|
||||||
|
depend: .depend
|
||||||
|
|
||||||
|
clean:
|
||||||
|
@rm -f *.o *~ .*.swp .built
|
||||||
|
$(call CLEAN)
|
||||||
|
|
||||||
|
distclean: clean
|
||||||
|
@rm -f Make.dep .depend
|
||||||
|
|
||||||
|
-include Make.dep
|
||||||
File diff suppressed because it is too large
Load Diff
+18
-18
@@ -31,7 +31,8 @@
|
|||||||
#ifndef _MB_H
|
#ifndef _MB_H
|
||||||
#define _MB_H
|
#define _MB_H
|
||||||
|
|
||||||
#include "port.h"
|
#include <stdint.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
PR_BEGIN_EXTERN_C
|
PR_BEGIN_EXTERN_C
|
||||||
@@ -121,7 +122,6 @@ typedef enum
|
|||||||
MB_ETIMEDOUT /*!< timeout error occurred. */
|
MB_ETIMEDOUT /*!< timeout error occurred. */
|
||||||
} eMBErrorCode;
|
} eMBErrorCode;
|
||||||
|
|
||||||
|
|
||||||
/* ----------------------- Function prototypes ------------------------------*/
|
/* ----------------------- Function prototypes ------------------------------*/
|
||||||
/*! \ingroup modbus
|
/*! \ingroup modbus
|
||||||
* \brief Initialize the Modbus protocol stack.
|
* \brief Initialize the Modbus protocol stack.
|
||||||
@@ -148,8 +148,8 @@ typedef enum
|
|||||||
* slave addresses are in the range 1 - 247.
|
* slave addresses are in the range 1 - 247.
|
||||||
* - eMBErrorCode::MB_EPORTERR IF the porting layer returned an error.
|
* - eMBErrorCode::MB_EPORTERR IF the porting layer returned an error.
|
||||||
*/
|
*/
|
||||||
eMBErrorCode eMBInit( eMBMode eMode, UCHAR ucSlaveAddress,
|
eMBErrorCode eMBInit( eMBMode eMode, uint8_t ucSlaveAddress,
|
||||||
UCHAR ucPort, ULONG ulBaudRate, eMBParity eParity );
|
uint8_t ucPort, uint32_t ulBaudRate, eMBParity eParity );
|
||||||
|
|
||||||
/*! \ingroup modbus
|
/*! \ingroup modbus
|
||||||
* \brief Initialize the Modbus protocol stack for Modbus TCP.
|
* \brief Initialize the Modbus protocol stack for Modbus TCP.
|
||||||
@@ -165,7 +165,7 @@ eMBErrorCode eMBInit( eMBMode eMode, UCHAR ucSlaveAddress,
|
|||||||
* slave addresses are in the range 1 - 247.
|
* slave addresses are in the range 1 - 247.
|
||||||
* - eMBErrorCode::MB_EPORTERR IF the porting layer returned an error.
|
* - eMBErrorCode::MB_EPORTERR IF the porting layer returned an error.
|
||||||
*/
|
*/
|
||||||
eMBErrorCode eMBTCPInit( USHORT usTCPPort );
|
eMBErrorCode eMBTCPInit( uint16_t usTCPPort );
|
||||||
|
|
||||||
/*! \ingroup modbus
|
/*! \ingroup modbus
|
||||||
* \brief Release resources used by the protocol stack.
|
* \brief Release resources used by the protocol stack.
|
||||||
@@ -228,7 +228,7 @@ eMBErrorCode eMBPoll( void );
|
|||||||
*
|
*
|
||||||
* \param ucSlaveID Values is returned in the <em>Slave ID</em> byte of the
|
* \param ucSlaveID Values is returned in the <em>Slave ID</em> byte of the
|
||||||
* <em>Report Slave ID</em> response.
|
* <em>Report Slave ID</em> response.
|
||||||
* \param xIsRunning If TRUE the <em>Run Indicator Status</em> byte is set to 0xFF.
|
* \param xIsRunning If true the <em>Run Indicator Status</em> byte is set to 0xFF.
|
||||||
* otherwise the <em>Run Indicator Status</em> is 0x00.
|
* otherwise the <em>Run Indicator Status</em> is 0x00.
|
||||||
* \param pucAdditional Values which should be returned in the <em>Additional</em>
|
* \param pucAdditional Values which should be returned in the <em>Additional</em>
|
||||||
* bytes of the <em> Report Slave ID</em> response.
|
* bytes of the <em> Report Slave ID</em> response.
|
||||||
@@ -238,9 +238,9 @@ eMBErrorCode eMBPoll( void );
|
|||||||
* is too small it returns eMBErrorCode::MB_ENORES. Otherwise
|
* is too small it returns eMBErrorCode::MB_ENORES. Otherwise
|
||||||
* it returns eMBErrorCode::MB_ENOERR.
|
* it returns eMBErrorCode::MB_ENOERR.
|
||||||
*/
|
*/
|
||||||
eMBErrorCode eMBSetSlaveID( UCHAR ucSlaveID, BOOL xIsRunning,
|
eMBErrorCode eMBSetSlaveID( uint8_t ucSlaveID, bool xIsRunning,
|
||||||
UCHAR const *pucAdditional,
|
uint8_t const *pucAdditional,
|
||||||
USHORT usAdditionalLen );
|
uint16_t usAdditionalLen );
|
||||||
|
|
||||||
/*! \ingroup modbus
|
/*! \ingroup modbus
|
||||||
* \brief Registers a callback handler for a given function code.
|
* \brief Registers a callback handler for a given function code.
|
||||||
@@ -262,7 +262,7 @@ eMBErrorCode eMBSetSlaveID( UCHAR ucSlaveID, BOOL xIsRunning,
|
|||||||
* case the values in config.h should be adjusted. If the argument was not
|
* case the values in config.h should be adjusted. If the argument was not
|
||||||
* valid it returns eMBErrorCode::MB_EINVAL.
|
* valid it returns eMBErrorCode::MB_EINVAL.
|
||||||
*/
|
*/
|
||||||
eMBErrorCode eMBRegisterCB( UCHAR ucFunctionCode,
|
eMBErrorCode eMBRegisterCB( uint8_t ucFunctionCode,
|
||||||
pxMBFunctionHandler pxHandler );
|
pxMBFunctionHandler pxHandler );
|
||||||
|
|
||||||
/* ----------------------- Callback -----------------------------------------*/
|
/* ----------------------- Callback -----------------------------------------*/
|
||||||
@@ -309,8 +309,8 @@ eMBErrorCode eMBRegisterCB( UCHAR ucFunctionCode,
|
|||||||
* - eMBErrorCode::MB_EIO If an unrecoverable error occurred. In this case
|
* - eMBErrorCode::MB_EIO If an unrecoverable error occurred. In this case
|
||||||
* a <b>SLAVE DEVICE FAILURE</b> exception is sent as a response.
|
* a <b>SLAVE DEVICE FAILURE</b> exception is sent as a response.
|
||||||
*/
|
*/
|
||||||
eMBErrorCode eMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress,
|
eMBErrorCode eMBRegInputCB( uint8_t * pucRegBuffer, uint16_t usAddress,
|
||||||
USHORT usNRegs );
|
uint16_t usNRegs );
|
||||||
|
|
||||||
/*! \ingroup modbus_registers
|
/*! \ingroup modbus_registers
|
||||||
* \brief Callback function used if a <em>Holding Register</em> value is
|
* \brief Callback function used if a <em>Holding Register</em> value is
|
||||||
@@ -344,8 +344,8 @@ eMBErrorCode eMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress,
|
|||||||
* - eMBErrorCode::MB_EIO If an unrecoverable error occurred. In this case
|
* - eMBErrorCode::MB_EIO If an unrecoverable error occurred. In this case
|
||||||
* a <b>SLAVE DEVICE FAILURE</b> exception is sent as a response.
|
* a <b>SLAVE DEVICE FAILURE</b> exception is sent as a response.
|
||||||
*/
|
*/
|
||||||
eMBErrorCode eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress,
|
eMBErrorCode eMBRegHoldingCB( uint8_t * pucRegBuffer, uint16_t usAddress,
|
||||||
USHORT usNRegs, eMBRegisterMode eMode );
|
uint16_t usNRegs, eMBRegisterMode eMode );
|
||||||
|
|
||||||
/*! \ingroup modbus_registers
|
/*! \ingroup modbus_registers
|
||||||
* \brief Callback function used if a <em>Coil Register</em> value is
|
* \brief Callback function used if a <em>Coil Register</em> value is
|
||||||
@@ -379,8 +379,8 @@ eMBErrorCode eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress,
|
|||||||
* - eMBErrorCode::MB_EIO If an unrecoverable error occurred. In this case
|
* - eMBErrorCode::MB_EIO If an unrecoverable error occurred. In this case
|
||||||
* a <b>SLAVE DEVICE FAILURE</b> exception is sent as a response.
|
* a <b>SLAVE DEVICE FAILURE</b> exception is sent as a response.
|
||||||
*/
|
*/
|
||||||
eMBErrorCode eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress,
|
eMBErrorCode eMBRegCoilsCB( uint8_t * pucRegBuffer, uint16_t usAddress,
|
||||||
USHORT usNCoils, eMBRegisterMode eMode );
|
uint16_t usNCoils, eMBRegisterMode eMode );
|
||||||
|
|
||||||
/*! \ingroup modbus_registers
|
/*! \ingroup modbus_registers
|
||||||
* \brief Callback function used if a <em>Input Discrete Register</em> value is
|
* \brief Callback function used if a <em>Input Discrete Register</em> value is
|
||||||
@@ -408,8 +408,8 @@ eMBErrorCode eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress,
|
|||||||
* - eMBErrorCode::MB_EIO If an unrecoverable error occurred. In this case
|
* - eMBErrorCode::MB_EIO If an unrecoverable error occurred. In this case
|
||||||
* a <b>SLAVE DEVICE FAILURE</b> exception is sent as a response.
|
* a <b>SLAVE DEVICE FAILURE</b> exception is sent as a response.
|
||||||
*/
|
*/
|
||||||
eMBErrorCode eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress,
|
eMBErrorCode eMBRegDiscreteCB( uint8_t * pucRegBuffer, uint16_t usAddress,
|
||||||
USHORT usNDiscrete );
|
uint16_t usNDiscrete );
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
PR_END_EXTERN_C
|
PR_END_EXTERN_C
|
||||||
|
|||||||
@@ -71,13 +71,13 @@ typedef void ( *pvMBFrameStart ) ( void );
|
|||||||
|
|
||||||
typedef void ( *pvMBFrameStop ) ( void );
|
typedef void ( *pvMBFrameStop ) ( void );
|
||||||
|
|
||||||
typedef eMBErrorCode( *peMBFrameReceive ) ( UCHAR * pucRcvAddress,
|
typedef eMBErrorCode( *peMBFrameReceive ) ( uint8_t * pucRcvAddress,
|
||||||
UCHAR ** pucFrame,
|
uint8_t ** pucFrame,
|
||||||
USHORT * pusLength );
|
uint16_t * pusLength );
|
||||||
|
|
||||||
typedef eMBErrorCode( *peMBFrameSend ) ( UCHAR slaveAddress,
|
typedef eMBErrorCode( *peMBFrameSend ) ( uint8_t slaveAddress,
|
||||||
const UCHAR * pucFrame,
|
const uint8_t * pucFrame,
|
||||||
USHORT usLength );
|
uint16_t usLength );
|
||||||
|
|
||||||
typedef void( *pvMBFrameClose ) ( void );
|
typedef void( *pvMBFrameClose ) ( void );
|
||||||
|
|
||||||
|
|||||||
@@ -35,43 +35,43 @@
|
|||||||
PR_BEGIN_EXTERN_C
|
PR_BEGIN_EXTERN_C
|
||||||
#endif
|
#endif
|
||||||
#ifdef CONFIG_MB_FUNC_OTHER_REP_SLAVEID_BUF
|
#ifdef CONFIG_MB_FUNC_OTHER_REP_SLAVEID_BUF
|
||||||
eMBException eMBFuncReportSlaveID( UCHAR * pucFrame, USHORT * usLen );
|
eMBException eMBFuncReportSlaveID( uint8_t * pucFrame, uint16_t * usLen );
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef CONFIG_MB_FUNC_READ_INPUT_ENABLED
|
#ifdef CONFIG_MB_FUNC_READ_INPUT_ENABLED
|
||||||
eMBException eMBFuncReadInputRegister( UCHAR * pucFrame, USHORT * usLen );
|
eMBException eMBFuncReadInputRegister( uint8_t * pucFrame, uint16_t * usLen );
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef CONFIG_MB_FUNC_READ_HOLDING_ENABLED
|
#ifdef CONFIG_MB_FUNC_READ_HOLDING_ENABLED
|
||||||
eMBException eMBFuncReadHoldingRegister( UCHAR * pucFrame, USHORT * usLen );
|
eMBException eMBFuncReadHoldingRegister( uint8_t * pucFrame, uint16_t * usLen );
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef CONFIG_MB_FUNC_WRITE_HOLDING_ENABLED
|
#ifdef CONFIG_MB_FUNC_WRITE_HOLDING_ENABLED
|
||||||
eMBException eMBFuncWriteHoldingRegister( UCHAR * pucFrame, USHORT * usLen );
|
eMBException eMBFuncWriteHoldingRegister( uint8_t * pucFrame, uint16_t * usLen );
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef CONFIG_MB_FUNC_WRITE_MULTIPLE_HOLDING_ENABLED
|
#ifdef CONFIG_MB_FUNC_WRITE_MULTIPLE_HOLDING_ENABLED
|
||||||
eMBException eMBFuncWriteMultipleHoldingRegister( UCHAR * pucFrame, USHORT * usLen );
|
eMBException eMBFuncWriteMultipleHoldingRegister( uint8_t * pucFrame, uint16_t * usLen );
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef CONFIG_MB_FUNC_READ_COILS_ENABLED
|
#ifdef CONFIG_MB_FUNC_READ_COILS_ENABLED
|
||||||
eMBException eMBFuncReadCoils( UCHAR * pucFrame, USHORT * usLen );
|
eMBException eMBFuncReadCoils( uint8_t * pucFrame, uint16_t * usLen );
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef CONFIG_MB_FUNC_WRITE_COIL_ENABLED
|
#ifdef CONFIG_MB_FUNC_WRITE_COIL_ENABLED
|
||||||
eMBException eMBFuncWriteCoil( UCHAR * pucFrame, USHORT * usLen );
|
eMBException eMBFuncWriteCoil( uint8_t * pucFrame, uint16_t * usLen );
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef CONFIG_MB_FUNC_WRITE_MULTIPLE_COILS_ENABLED
|
#ifdef CONFIG_MB_FUNC_WRITE_MULTIPLE_COILS_ENABLED
|
||||||
eMBException eMBFuncWriteMultipleCoils( UCHAR * pucFrame, USHORT * usLen );
|
eMBException eMBFuncWriteMultipleCoils( uint8_t * pucFrame, uint16_t * usLen );
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef CONFIG_MB_FUNC_READ_DISCRETE_INPUTS_ENABLED
|
#ifdef CONFIG_MB_FUNC_READ_DISCRETE_INPUTS_ENABLED
|
||||||
eMBException eMBFuncReadDiscreteInputs( UCHAR * pucFrame, USHORT * usLen );
|
eMBException eMBFuncReadDiscreteInputs( uint8_t * pucFrame, uint16_t * usLen );
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef CONFIG_MB_FUNC_READWRITE_HOLDING_ENABLED
|
#ifdef CONFIG_MB_FUNC_READWRITE_HOLDING_ENABLED
|
||||||
eMBException eMBFuncReadWriteMultipleHoldingRegister( UCHAR * pucFrame, USHORT * usLen );
|
eMBException eMBFuncReadWriteMultipleHoldingRegister( uint8_t * pucFrame, uint16_t * usLen );
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
|||||||
@@ -31,6 +31,9 @@
|
|||||||
#ifndef _MB_PORT_H
|
#ifndef _MB_PORT_H
|
||||||
#define _MB_PORT_H
|
#define _MB_PORT_H
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
PR_BEGIN_EXTERN_C
|
PR_BEGIN_EXTERN_C
|
||||||
#endif
|
#endif
|
||||||
@@ -60,29 +63,29 @@ typedef enum
|
|||||||
} eMBParity;
|
} eMBParity;
|
||||||
|
|
||||||
/* ----------------------- Supporting functions -----------------------------*/
|
/* ----------------------- Supporting functions -----------------------------*/
|
||||||
BOOL xMBPortEventInit( void );
|
bool xMBPortEventInit( void );
|
||||||
|
|
||||||
BOOL xMBPortEventPost( eMBEventType eEvent );
|
bool xMBPortEventPost( eMBEventType eEvent );
|
||||||
|
|
||||||
BOOL xMBPortEventGet( /*@out@ */ eMBEventType * eEvent );
|
bool xMBPortEventGet( /*@out@ */ eMBEventType * eEvent );
|
||||||
|
|
||||||
/* ----------------------- Serial port functions ----------------------------*/
|
/* ----------------------- Serial port functions ----------------------------*/
|
||||||
|
|
||||||
BOOL xMBPortSerialInit( UCHAR ucPort, ULONG ulBaudRate,
|
bool xMBPortSerialInit( uint8_t ucPort, uint32_t ulBaudRate,
|
||||||
UCHAR ucDataBits, eMBParity eParity );
|
uint8_t ucDataBits, eMBParity eParity );
|
||||||
|
|
||||||
void vMBPortClose( void );
|
void vMBPortClose( void );
|
||||||
|
|
||||||
void xMBPortSerialClose( void );
|
void xMBPortSerialClose( void );
|
||||||
|
|
||||||
void vMBPortSerialEnable( BOOL xRxEnable, BOOL xTxEnable );
|
void vMBPortSerialEnable( bool xRxEnable, bool xTxEnable );
|
||||||
|
|
||||||
BOOL xMBPortSerialGetByte( CHAR * pucByte );
|
bool xMBPortSerialGetByte( int8_t * pucByte );
|
||||||
|
|
||||||
BOOL xMBPortSerialPutByte( CHAR ucByte );
|
bool xMBPortSerialPutByte( int8_t ucByte );
|
||||||
|
|
||||||
/* ----------------------- Timers functions ---------------------------------*/
|
/* ----------------------- Timers functions ---------------------------------*/
|
||||||
BOOL xMBPortTimersInit( USHORT usTimeOut50us );
|
bool xMBPortTimersInit( uint16_t usTimeOut50us );
|
||||||
|
|
||||||
void xMBPortTimersClose( void );
|
void xMBPortTimersClose( void );
|
||||||
|
|
||||||
@@ -90,7 +93,7 @@ void vMBPortTimersEnable( void );
|
|||||||
|
|
||||||
void vMBPortTimersDisable( void );
|
void vMBPortTimersDisable( void );
|
||||||
|
|
||||||
void vMBPortTimersDelay( USHORT usTimeOutMS );
|
void vMBPortTimersDelay( uint16_t usTimeOutMS );
|
||||||
|
|
||||||
/* ----------------------- Callback for the protocol stack ------------------*/
|
/* ----------------------- Callback for the protocol stack ------------------*/
|
||||||
|
|
||||||
@@ -102,26 +105,26 @@ void vMBPortTimersDelay( USHORT usTimeOutMS );
|
|||||||
* ASCII transmission layers. In any case a call to xMBPortSerialGetByte()
|
* ASCII transmission layers. In any case a call to xMBPortSerialGetByte()
|
||||||
* must immediately return a new character.
|
* must immediately return a new character.
|
||||||
*
|
*
|
||||||
* \return <code>TRUE</code> if a event was posted to the queue because
|
* \return <code>true</code> if a event was posted to the queue because
|
||||||
* a new byte was received. The port implementation should wake up the
|
* a new byte was received. The port implementation should wake up the
|
||||||
* tasks which are currently blocked on the eventqueue.
|
* tasks which are currently blocked on the eventqueue.
|
||||||
*/
|
*/
|
||||||
extern BOOL( *pxMBFrameCBByteReceived ) ( void );
|
extern bool( *pxMBFrameCBByteReceived ) ( void );
|
||||||
|
|
||||||
extern BOOL( *pxMBFrameCBTransmitterEmpty ) ( void );
|
extern bool( *pxMBFrameCBTransmitterEmpty ) ( void );
|
||||||
|
|
||||||
extern BOOL( *pxMBPortCBTimerExpired ) ( void );
|
extern bool( *pxMBPortCBTimerExpired ) ( void );
|
||||||
|
|
||||||
/* ----------------------- TCP port functions -------------------------------*/
|
/* ----------------------- TCP port functions -------------------------------*/
|
||||||
BOOL xMBTCPPortInit( USHORT usTCPPort );
|
bool xMBTCPPortInit( uint16_t usTCPPort );
|
||||||
|
|
||||||
void vMBTCPPortClose( void );
|
void vMBTCPPortClose( void );
|
||||||
|
|
||||||
void vMBTCPPortDisable( void );
|
void vMBTCPPortDisable( void );
|
||||||
|
|
||||||
BOOL xMBTCPPortGetRequest( UCHAR **ppucMBTCPFrame, USHORT * usTCPLength );
|
bool xMBTCPPortGetRequest( uint8_t **ppucMBTCPFrame, uint16_t * usTCPLength );
|
||||||
|
|
||||||
BOOL xMBTCPPortSendResponse( const UCHAR *pucMBTCPFrame, USHORT usTCPLength );
|
bool xMBTCPPortSendResponse( const uint8_t *pucMBTCPFrame, uint16_t usTCPLength );
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
PR_END_EXTERN_C
|
PR_END_EXTERN_C
|
||||||
|
|||||||
@@ -69,11 +69,11 @@ PR_BEGIN_EXTERN_C
|
|||||||
MB_EX_GATEWAY_TGT_FAILED = 0x0B
|
MB_EX_GATEWAY_TGT_FAILED = 0x0B
|
||||||
} eMBException;
|
} eMBException;
|
||||||
|
|
||||||
typedef eMBException( *pxMBFunctionHandler ) ( UCHAR * pucFrame, USHORT * pusLength );
|
typedef eMBException( *pxMBFunctionHandler ) ( uint8_t * pucFrame, uint16_t * pusLength );
|
||||||
|
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
UCHAR ucFunctionCode;
|
uint8_t ucFunctionCode;
|
||||||
pxMBFunctionHandler pxHandler;
|
pxMBFunctionHandler pxHandler;
|
||||||
} xMBFunctionHandler;
|
} xMBFunctionHandler;
|
||||||
|
|
||||||
|
|||||||
@@ -75,8 +75,8 @@ PR_BEGIN_EXTERN_C
|
|||||||
* xMBUtilSetBits( ucBits, 8, 8, 0x5A);
|
* xMBUtilSetBits( ucBits, 8, 8, 0x5A);
|
||||||
* \endcode
|
* \endcode
|
||||||
*/
|
*/
|
||||||
void xMBUtilSetBits( UCHAR * ucByteBuf, USHORT usBitOffset,
|
void xMBUtilSetBits( uint8_t * ucByteBuf, uint16_t usBitOffset,
|
||||||
UCHAR ucNBits, UCHAR ucValues );
|
uint8_t ucNBits, uint8_t ucValues );
|
||||||
|
|
||||||
/*! \brief Function to read bits in a byte buffer.
|
/*! \brief Function to read bits in a byte buffer.
|
||||||
*
|
*
|
||||||
@@ -90,15 +90,15 @@ void xMBUtilSetBits( UCHAR * ucByteBuf, USHORT usBitOffset,
|
|||||||
* than 8.
|
* than 8.
|
||||||
*
|
*
|
||||||
* \code
|
* \code
|
||||||
* UCHAR ucBits[2] = {0, 0};
|
* uint8_t ucBits[2] = {0, 0};
|
||||||
* UCHAR ucResult;
|
* uint8_t ucResult;
|
||||||
*
|
*
|
||||||
* // Extract the bits 3 - 10.
|
* // Extract the bits 3 - 10.
|
||||||
* ucResult = xMBUtilGetBits( ucBits, 3, 8 );
|
* ucResult = xMBUtilGetBits( ucBits, 3, 8 );
|
||||||
* \endcode
|
* \endcode
|
||||||
*/
|
*/
|
||||||
UCHAR xMBUtilGetBits( UCHAR * ucByteBuf, USHORT usBitOffset,
|
uint8_t xMBUtilGetBits( uint8_t * ucByteBuf, uint16_t usBitOffset,
|
||||||
UCHAR ucNBits );
|
uint8_t ucNBits );
|
||||||
|
|
||||||
/*! @} */
|
/*! @} */
|
||||||
|
|
||||||
|
|||||||
+58
-58
@@ -80,11 +80,11 @@ typedef enum
|
|||||||
} eMBBytePos;
|
} eMBBytePos;
|
||||||
|
|
||||||
/* ----------------------- Static functions ---------------------------------*/
|
/* ----------------------- Static functions ---------------------------------*/
|
||||||
static UCHAR prvucMBCHAR2BIN( UCHAR ucCharacter );
|
static uint8_t prvucMBint8_t2BIN( uint8_t ucCharacter );
|
||||||
|
|
||||||
static UCHAR prvucMBBIN2CHAR( UCHAR ucByte );
|
static uint8_t prvucMBBIN2int8_t( uint8_t ucByte );
|
||||||
|
|
||||||
static UCHAR prvucMBLRC( UCHAR * pucFrame, USHORT usLen );
|
static uint8_t prvucMBLRC( uint8_t * pucFrame, uint16_t usLen );
|
||||||
|
|
||||||
/* ----------------------- Static variables ---------------------------------*/
|
/* ----------------------- Static variables ---------------------------------*/
|
||||||
static volatile eMBSndState eSndState;
|
static volatile eMBSndState eSndState;
|
||||||
@@ -92,21 +92,21 @@ static volatile eMBRcvState eRcvState;
|
|||||||
|
|
||||||
/* We reuse the Modbus RTU buffer because only one buffer is needed and the
|
/* We reuse the Modbus RTU buffer because only one buffer is needed and the
|
||||||
* RTU buffer is bigger. */
|
* RTU buffer is bigger. */
|
||||||
extern volatile UCHAR ucRTUBuf[];
|
extern volatile uint8_t ucRTUBuf[];
|
||||||
static volatile UCHAR *ucASCIIBuf = ucRTUBuf;
|
static volatile uint8_t *ucASCIIBuf = ucRTUBuf;
|
||||||
|
|
||||||
static volatile USHORT usRcvBufferPos;
|
static volatile uint16_t usRcvBufferPos;
|
||||||
static volatile eMBBytePos eBytePos;
|
static volatile eMBBytePos eBytePos;
|
||||||
|
|
||||||
static volatile UCHAR *pucSndBufferCur;
|
static volatile uint8_t *pucSndBufferCur;
|
||||||
static volatile USHORT usSndBufferCount;
|
static volatile uint16_t usSndBufferCount;
|
||||||
|
|
||||||
static volatile UCHAR ucLRC;
|
static volatile uint8_t ucLRC;
|
||||||
static volatile UCHAR ucMBLFCharacter;
|
static volatile uint8_t ucMBLFCharacter;
|
||||||
|
|
||||||
/* ----------------------- Start implementation -----------------------------*/
|
/* ----------------------- Start implementation -----------------------------*/
|
||||||
eMBErrorCode
|
eMBErrorCode
|
||||||
eMBASCIIInit( UCHAR ucSlaveAddress, UCHAR ucPort, ULONG ulBaudRate, eMBParity eParity )
|
eMBASCIIInit( uint8_t ucSlaveAddress, uint8_t ucPort, uint32_t ulBaudRate, eMBParity eParity )
|
||||||
{
|
{
|
||||||
eMBErrorCode eStatus = MB_ENOERR;
|
eMBErrorCode eStatus = MB_ENOERR;
|
||||||
( void )ucSlaveAddress;
|
( void )ucSlaveAddress;
|
||||||
@@ -114,11 +114,11 @@ eMBASCIIInit( UCHAR ucSlaveAddress, UCHAR ucPort, ULONG ulBaudRate, eMBParity eP
|
|||||||
ENTER_CRITICAL_SECTION( );
|
ENTER_CRITICAL_SECTION( );
|
||||||
ucMBLFCharacter = MB_ASCII_DEFAULT_LF;
|
ucMBLFCharacter = MB_ASCII_DEFAULT_LF;
|
||||||
|
|
||||||
if( xMBPortSerialInit( ucPort, ulBaudRate, 7, eParity ) != TRUE )
|
if( xMBPortSerialInit( ucPort, ulBaudRate, 7, eParity ) != true )
|
||||||
{
|
{
|
||||||
eStatus = MB_EPORTERR;
|
eStatus = MB_EPORTERR;
|
||||||
}
|
}
|
||||||
else if( xMBPortTimersInit( CONFIG_MB_ASCII_TIMEOUT_SEC * 20000UL ) != TRUE )
|
else if( xMBPortTimersInit( CONFIG_MB_ASCII_TIMEOUT_SEC * 20000UL ) != true )
|
||||||
{
|
{
|
||||||
eStatus = MB_EPORTERR;
|
eStatus = MB_EPORTERR;
|
||||||
}
|
}
|
||||||
@@ -132,7 +132,7 @@ void
|
|||||||
eMBASCIIStart( void )
|
eMBASCIIStart( void )
|
||||||
{
|
{
|
||||||
ENTER_CRITICAL_SECTION( );
|
ENTER_CRITICAL_SECTION( );
|
||||||
vMBPortSerialEnable( TRUE, FALSE );
|
vMBPortSerialEnable( true, false );
|
||||||
eRcvState = STATE_RX_IDLE;
|
eRcvState = STATE_RX_IDLE;
|
||||||
EXIT_CRITICAL_SECTION( );
|
EXIT_CRITICAL_SECTION( );
|
||||||
|
|
||||||
@@ -144,13 +144,13 @@ void
|
|||||||
eMBASCIIStop( void )
|
eMBASCIIStop( void )
|
||||||
{
|
{
|
||||||
ENTER_CRITICAL_SECTION( );
|
ENTER_CRITICAL_SECTION( );
|
||||||
vMBPortSerialEnable( FALSE, FALSE );
|
vMBPortSerialEnable( false, false );
|
||||||
vMBPortTimersDisable( );
|
vMBPortTimersDisable( );
|
||||||
EXIT_CRITICAL_SECTION( );
|
EXIT_CRITICAL_SECTION( );
|
||||||
}
|
}
|
||||||
|
|
||||||
eMBErrorCode
|
eMBErrorCode
|
||||||
eMBASCIIReceive( UCHAR * pucRcvAddress, UCHAR ** pucFrame, USHORT * pusLength )
|
eMBASCIIReceive( uint8_t * pucRcvAddress, uint8_t ** pucFrame, uint16_t * pusLength )
|
||||||
{
|
{
|
||||||
eMBErrorCode eStatus = MB_ENOERR;
|
eMBErrorCode eStatus = MB_ENOERR;
|
||||||
|
|
||||||
@@ -159,7 +159,7 @@ eMBASCIIReceive( UCHAR * pucRcvAddress, UCHAR ** pucFrame, USHORT * pusLength )
|
|||||||
|
|
||||||
/* Length and CRC check */
|
/* Length and CRC check */
|
||||||
if( ( usRcvBufferPos >= MB_SER_PDU_SIZE_MIN )
|
if( ( usRcvBufferPos >= MB_SER_PDU_SIZE_MIN )
|
||||||
&& ( prvucMBLRC( ( UCHAR * ) ucASCIIBuf, usRcvBufferPos ) == 0 ) )
|
&& ( prvucMBLRC( ( uint8_t * ) ucASCIIBuf, usRcvBufferPos ) == 0 ) )
|
||||||
{
|
{
|
||||||
/* Save the address field. All frames are passed to the upper layed
|
/* Save the address field. All frames are passed to the upper layed
|
||||||
* and the decision if a frame is used is done there.
|
* and the decision if a frame is used is done there.
|
||||||
@@ -169,10 +169,10 @@ eMBASCIIReceive( UCHAR * pucRcvAddress, UCHAR ** pucFrame, USHORT * pusLength )
|
|||||||
/* Total length of Modbus-PDU is Modbus-Serial-Line-PDU minus
|
/* Total length of Modbus-PDU is Modbus-Serial-Line-PDU minus
|
||||||
* size of address field and CRC checksum.
|
* size of address field and CRC checksum.
|
||||||
*/
|
*/
|
||||||
*pusLength = ( USHORT )( usRcvBufferPos - MB_SER_PDU_PDU_OFF - MB_SER_PDU_SIZE_LRC );
|
*pusLength = ( uint16_t )( usRcvBufferPos - MB_SER_PDU_PDU_OFF - MB_SER_PDU_SIZE_LRC );
|
||||||
|
|
||||||
/* Return the start of the Modbus PDU to the caller. */
|
/* Return the start of the Modbus PDU to the caller. */
|
||||||
*pucFrame = ( UCHAR * ) & ucASCIIBuf[MB_SER_PDU_PDU_OFF];
|
*pucFrame = ( uint8_t * ) & ucASCIIBuf[MB_SER_PDU_PDU_OFF];
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -183,10 +183,10 @@ eMBASCIIReceive( UCHAR * pucRcvAddress, UCHAR ** pucFrame, USHORT * pusLength )
|
|||||||
}
|
}
|
||||||
|
|
||||||
eMBErrorCode
|
eMBErrorCode
|
||||||
eMBASCIISend( UCHAR ucSlaveAddress, const UCHAR * pucFrame, USHORT usLength )
|
eMBASCIISend( uint8_t ucSlaveAddress, const uint8_t * pucFrame, uint16_t usLength )
|
||||||
{
|
{
|
||||||
eMBErrorCode eStatus = MB_ENOERR;
|
eMBErrorCode eStatus = MB_ENOERR;
|
||||||
UCHAR usLRC;
|
uint8_t usLRC;
|
||||||
|
|
||||||
ENTER_CRITICAL_SECTION( );
|
ENTER_CRITICAL_SECTION( );
|
||||||
/* Check if the receiver is still in idle state. If not we where too
|
/* Check if the receiver is still in idle state. If not we where too
|
||||||
@@ -196,7 +196,7 @@ eMBASCIISend( UCHAR ucSlaveAddress, const UCHAR * pucFrame, USHORT usLength )
|
|||||||
if( eRcvState == STATE_RX_IDLE )
|
if( eRcvState == STATE_RX_IDLE )
|
||||||
{
|
{
|
||||||
/* First byte before the Modbus-PDU is the slave address. */
|
/* First byte before the Modbus-PDU is the slave address. */
|
||||||
pucSndBufferCur = ( UCHAR * ) pucFrame - 1;
|
pucSndBufferCur = ( uint8_t * ) pucFrame - 1;
|
||||||
usSndBufferCount = 1;
|
usSndBufferCount = 1;
|
||||||
|
|
||||||
/* Now copy the Modbus-PDU into the Modbus-Serial-Line-PDU. */
|
/* Now copy the Modbus-PDU into the Modbus-Serial-Line-PDU. */
|
||||||
@@ -204,12 +204,12 @@ eMBASCIISend( UCHAR ucSlaveAddress, const UCHAR * pucFrame, USHORT usLength )
|
|||||||
usSndBufferCount += usLength;
|
usSndBufferCount += usLength;
|
||||||
|
|
||||||
/* Calculate LRC checksum for Modbus-Serial-Line-PDU. */
|
/* Calculate LRC checksum for Modbus-Serial-Line-PDU. */
|
||||||
usLRC = prvucMBLRC( ( UCHAR * ) pucSndBufferCur, usSndBufferCount );
|
usLRC = prvucMBLRC( ( uint8_t * ) pucSndBufferCur, usSndBufferCount );
|
||||||
ucASCIIBuf[usSndBufferCount++] = usLRC;
|
ucASCIIBuf[usSndBufferCount++] = usLRC;
|
||||||
|
|
||||||
/* Activate the transmitter. */
|
/* Activate the transmitter. */
|
||||||
eSndState = STATE_TX_START;
|
eSndState = STATE_TX_START;
|
||||||
vMBPortSerialEnable( FALSE, TRUE );
|
vMBPortSerialEnable( false, true );
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -219,16 +219,16 @@ eMBASCIISend( UCHAR ucSlaveAddress, const UCHAR * pucFrame, USHORT usLength )
|
|||||||
return eStatus;
|
return eStatus;
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOL
|
bool
|
||||||
xMBASCIIReceiveFSM( void )
|
xMBASCIIReceiveFSM( void )
|
||||||
{
|
{
|
||||||
BOOL xNeedPoll = FALSE;
|
bool xNeedPoll = false;
|
||||||
UCHAR ucByte;
|
uint8_t ucByte;
|
||||||
UCHAR ucResult;
|
uint8_t ucResult;
|
||||||
|
|
||||||
ASSERT( eSndState == STATE_TX_IDLE );
|
ASSERT( eSndState == STATE_TX_IDLE );
|
||||||
|
|
||||||
( void )xMBPortSerialGetByte( ( CHAR * ) & ucByte );
|
( void )xMBPortSerialGetByte( ( int8_t * ) & ucByte );
|
||||||
switch ( eRcvState )
|
switch ( eRcvState )
|
||||||
{
|
{
|
||||||
/* A new character is received. If the character is a ':' the input
|
/* A new character is received. If the character is a ':' the input
|
||||||
@@ -251,7 +251,7 @@ xMBASCIIReceiveFSM( void )
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
ucResult = prvucMBCHAR2BIN( ucByte );
|
ucResult = prvucMBint8_t2BIN( ucByte );
|
||||||
switch ( eBytePos )
|
switch ( eBytePos )
|
||||||
{
|
{
|
||||||
/* High nibble of the byte comes first. We check for
|
/* High nibble of the byte comes first. We check for
|
||||||
@@ -259,7 +259,7 @@ xMBASCIIReceiveFSM( void )
|
|||||||
case BYTE_HIGH_NIBBLE:
|
case BYTE_HIGH_NIBBLE:
|
||||||
if( usRcvBufferPos < MB_SER_PDU_SIZE_MAX )
|
if( usRcvBufferPos < MB_SER_PDU_SIZE_MAX )
|
||||||
{
|
{
|
||||||
ucASCIIBuf[usRcvBufferPos] = ( UCHAR )( ucResult << 4 );
|
ucASCIIBuf[usRcvBufferPos] = ( uint8_t )( ucResult << 4 );
|
||||||
eBytePos = BYTE_LOW_NIBBLE;
|
eBytePos = BYTE_LOW_NIBBLE;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -328,11 +328,11 @@ xMBASCIIReceiveFSM( void )
|
|||||||
return xNeedPoll;
|
return xNeedPoll;
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOL
|
bool
|
||||||
xMBASCIITransmitFSM( void )
|
xMBASCIITransmitFSM( void )
|
||||||
{
|
{
|
||||||
BOOL xNeedPoll = FALSE;
|
bool xNeedPoll = false;
|
||||||
UCHAR ucByte;
|
uint8_t ucByte;
|
||||||
|
|
||||||
ASSERT( eRcvState == STATE_RX_IDLE );
|
ASSERT( eRcvState == STATE_RX_IDLE );
|
||||||
switch ( eSndState )
|
switch ( eSndState )
|
||||||
@@ -341,7 +341,7 @@ xMBASCIITransmitFSM( void )
|
|||||||
* the character ':'. */
|
* the character ':'. */
|
||||||
case STATE_TX_START:
|
case STATE_TX_START:
|
||||||
ucByte = ':';
|
ucByte = ':';
|
||||||
xMBPortSerialPutByte( ( CHAR )ucByte );
|
xMBPortSerialPutByte( ( int8_t )ucByte );
|
||||||
eSndState = STATE_TX_DATA;
|
eSndState = STATE_TX_DATA;
|
||||||
eBytePos = BYTE_HIGH_NIBBLE;
|
eBytePos = BYTE_HIGH_NIBBLE;
|
||||||
break;
|
break;
|
||||||
@@ -356,14 +356,14 @@ xMBASCIITransmitFSM( void )
|
|||||||
switch ( eBytePos )
|
switch ( eBytePos )
|
||||||
{
|
{
|
||||||
case BYTE_HIGH_NIBBLE:
|
case BYTE_HIGH_NIBBLE:
|
||||||
ucByte = prvucMBBIN2CHAR( ( UCHAR )( *pucSndBufferCur >> 4 ) );
|
ucByte = prvucMBBIN2int8_t( ( uint8_t )( *pucSndBufferCur >> 4 ) );
|
||||||
xMBPortSerialPutByte( ( CHAR ) ucByte );
|
xMBPortSerialPutByte( ( int8_t ) ucByte );
|
||||||
eBytePos = BYTE_LOW_NIBBLE;
|
eBytePos = BYTE_LOW_NIBBLE;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case BYTE_LOW_NIBBLE:
|
case BYTE_LOW_NIBBLE:
|
||||||
ucByte = prvucMBBIN2CHAR( ( UCHAR )( *pucSndBufferCur & 0x0F ) );
|
ucByte = prvucMBBIN2int8_t( ( uint8_t )( *pucSndBufferCur & 0x0F ) );
|
||||||
xMBPortSerialPutByte( ( CHAR )ucByte );
|
xMBPortSerialPutByte( ( int8_t )ucByte );
|
||||||
pucSndBufferCur++;
|
pucSndBufferCur++;
|
||||||
eBytePos = BYTE_HIGH_NIBBLE;
|
eBytePos = BYTE_HIGH_NIBBLE;
|
||||||
usSndBufferCount--;
|
usSndBufferCount--;
|
||||||
@@ -379,7 +379,7 @@ xMBASCIITransmitFSM( void )
|
|||||||
|
|
||||||
/* Finish the frame by sending a LF character. */
|
/* Finish the frame by sending a LF character. */
|
||||||
case STATE_TX_END:
|
case STATE_TX_END:
|
||||||
xMBPortSerialPutByte( ( CHAR )ucMBLFCharacter );
|
xMBPortSerialPutByte( ( int8_t )ucMBLFCharacter );
|
||||||
/* We need another state to make sure that the CR character has
|
/* We need another state to make sure that the CR character has
|
||||||
* been sent. */
|
* been sent. */
|
||||||
eSndState = STATE_TX_NOTIFY;
|
eSndState = STATE_TX_NOTIFY;
|
||||||
@@ -393,7 +393,7 @@ xMBASCIITransmitFSM( void )
|
|||||||
|
|
||||||
/* Disable transmitter. This prevents another transmit buffer
|
/* Disable transmitter. This prevents another transmit buffer
|
||||||
* empty interrupt. */
|
* empty interrupt. */
|
||||||
vMBPortSerialEnable( TRUE, FALSE );
|
vMBPortSerialEnable( true, false );
|
||||||
eSndState = STATE_TX_IDLE;
|
eSndState = STATE_TX_IDLE;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@@ -401,14 +401,14 @@ xMBASCIITransmitFSM( void )
|
|||||||
* idle state. */
|
* idle state. */
|
||||||
case STATE_TX_IDLE:
|
case STATE_TX_IDLE:
|
||||||
/* enable receiver/disable transmitter. */
|
/* enable receiver/disable transmitter. */
|
||||||
vMBPortSerialEnable( TRUE, FALSE );
|
vMBPortSerialEnable( true, false );
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return xNeedPoll;
|
return xNeedPoll;
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOL
|
bool
|
||||||
xMBASCIITimerT1SExpired( void )
|
xMBASCIITimerT1SExpired( void )
|
||||||
{
|
{
|
||||||
switch ( eRcvState )
|
switch ( eRcvState )
|
||||||
@@ -428,20 +428,20 @@ xMBASCIITimerT1SExpired( void )
|
|||||||
vMBPortTimersDisable( );
|
vMBPortTimersDisable( );
|
||||||
|
|
||||||
/* no context switch required. */
|
/* no context switch required. */
|
||||||
return FALSE;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static UCHAR
|
static uint8_t
|
||||||
prvucMBCHAR2BIN( UCHAR ucCharacter )
|
prvucMBint8_t2BIN( uint8_t ucCharacter )
|
||||||
{
|
{
|
||||||
if( ( ucCharacter >= '0' ) && ( ucCharacter <= '9' ) )
|
if( ( ucCharacter >= '0' ) && ( ucCharacter <= '9' ) )
|
||||||
{
|
{
|
||||||
return ( UCHAR )( ucCharacter - '0' );
|
return ( uint8_t )( ucCharacter - '0' );
|
||||||
}
|
}
|
||||||
else if( ( ucCharacter >= 'A' ) && ( ucCharacter <= 'F' ) )
|
else if( ( ucCharacter >= 'A' ) && ( ucCharacter <= 'F' ) )
|
||||||
{
|
{
|
||||||
return ( UCHAR )( ucCharacter - 'A' + 0x0A );
|
return ( uint8_t )( ucCharacter - 'A' + 0x0A );
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -449,16 +449,16 @@ prvucMBCHAR2BIN( UCHAR ucCharacter )
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static UCHAR
|
static uint8_t
|
||||||
prvucMBBIN2CHAR( UCHAR ucByte )
|
prvucMBBIN2int8_t( uint8_t ucByte )
|
||||||
{
|
{
|
||||||
if( ucByte <= 0x09 )
|
if( ucByte <= 0x09 )
|
||||||
{
|
{
|
||||||
return ( UCHAR )( '0' + ucByte );
|
return ( uint8_t )( '0' + ucByte );
|
||||||
}
|
}
|
||||||
else if( ( ucByte >= 0x0A ) && ( ucByte <= 0x0F ) )
|
else if( ( ucByte >= 0x0A ) && ( ucByte <= 0x0F ) )
|
||||||
{
|
{
|
||||||
return ( UCHAR )( ucByte - 0x0A + 'A' );
|
return ( uint8_t )( ucByte - 0x0A + 'A' );
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -469,19 +469,19 @@ prvucMBBIN2CHAR( UCHAR ucByte )
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static UCHAR
|
static uint8_t
|
||||||
prvucMBLRC( UCHAR * pucFrame, USHORT usLen )
|
prvucMBLRC( uint8_t * pucFrame, uint16_t usLen )
|
||||||
{
|
{
|
||||||
UCHAR ucLRC = 0; /* LRC char initialized */
|
uint8_t ucLocalLRC = 0; /* LRC char initialized */
|
||||||
|
|
||||||
while( usLen-- )
|
while( usLen-- )
|
||||||
{
|
{
|
||||||
ucLRC += *pucFrame++; /* Add buffer byte without carry */
|
ucLocalLRC += *pucFrame++; /* Add buffer byte without carry */
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Return twos complement */
|
/* Return twos complement */
|
||||||
ucLRC = ( UCHAR ) ( -( ( CHAR ) ucLRC ) );
|
ucLocalLRC = ( uint8_t ) ( -( ( int8_t ) ucLocalLRC ) );
|
||||||
return ucLRC;
|
return ucLocalLRC;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -36,18 +36,18 @@ PR_BEGIN_EXTERN_C
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef CONFIG_MB_ASCII_ENABLED
|
#ifdef CONFIG_MB_ASCII_ENABLED
|
||||||
eMBErrorCode eMBASCIIInit( UCHAR slaveAddress, UCHAR ucPort,
|
eMBErrorCode eMBASCIIInit( uint8_t slaveAddress, uint8_t ucPort,
|
||||||
ULONG ulBaudRate, eMBParity eParity );
|
uint32_t ulBaudRate, eMBParity eParity );
|
||||||
void eMBASCIIStart( void );
|
void eMBASCIIStart( void );
|
||||||
void eMBASCIIStop( void );
|
void eMBASCIIStop( void );
|
||||||
|
|
||||||
eMBErrorCode eMBASCIIReceive( UCHAR * pucRcvAddress, UCHAR ** pucFrame,
|
eMBErrorCode eMBASCIIReceive( uint8_t * pucRcvAddress, uint8_t ** pucFrame,
|
||||||
USHORT * pusLength );
|
uint16_t * pusLength );
|
||||||
eMBErrorCode eMBASCIISend( UCHAR slaveAddress, const UCHAR * pucFrame,
|
eMBErrorCode eMBASCIISend( uint8_t slaveAddress, const uint8_t * pucFrame,
|
||||||
USHORT usLength );
|
uint16_t usLength );
|
||||||
BOOL xMBASCIIReceiveFSM( void );
|
bool xMBASCIIReceiveFSM( void );
|
||||||
BOOL xMBASCIITransmitFSM( void );
|
bool xMBASCIITransmitFSM( void );
|
||||||
BOOL xMBASCIITimerT1SExpired( void );
|
bool xMBASCIITimerT1SExpired( void );
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
|||||||
@@ -66,24 +66,24 @@ eMBException prveMBError2Exception( eMBErrorCode eErrorCode );
|
|||||||
#ifdef CONFIG_MB_FUNC_READ_COILS_ENABLED
|
#ifdef CONFIG_MB_FUNC_READ_COILS_ENABLED
|
||||||
|
|
||||||
eMBException
|
eMBException
|
||||||
eMBFuncReadCoils( UCHAR * pucFrame, USHORT * usLen )
|
eMBFuncReadCoils( uint8_t * pucFrame, uint16_t * usLen )
|
||||||
{
|
{
|
||||||
USHORT usRegAddress;
|
uint16_t usRegAddress;
|
||||||
USHORT usCoilCount;
|
uint16_t usCoilCount;
|
||||||
UCHAR ucNBytes;
|
uint8_t ucNBytes;
|
||||||
UCHAR *pucFrameCur;
|
uint8_t *pucFrameCur;
|
||||||
|
|
||||||
eMBException eStatus = MB_EX_NONE;
|
eMBException eStatus = MB_EX_NONE;
|
||||||
eMBErrorCode eRegStatus;
|
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 = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF] << 8 );
|
usRegAddress = ( uint16_t )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF] << 8 );
|
||||||
usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF + 1] );
|
usRegAddress |= ( uint16_t )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF + 1] );
|
||||||
usRegAddress++;
|
usRegAddress++;
|
||||||
|
|
||||||
usCoilCount = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_COILCNT_OFF] << 8 );
|
usCoilCount = ( uint16_t )( pucFrame[MB_PDU_FUNC_READ_COILCNT_OFF] << 8 );
|
||||||
usCoilCount |= ( USHORT )( pucFrame[MB_PDU_FUNC_READ_COILCNT_OFF + 1] );
|
usCoilCount |= ( uint16_t )( pucFrame[MB_PDU_FUNC_READ_COILCNT_OFF + 1] );
|
||||||
|
|
||||||
/* Check if the number of registers to read is valid. If not
|
/* Check if the number of registers to read is valid. If not
|
||||||
* return Modbus illegal data value exception.
|
* return Modbus illegal data value exception.
|
||||||
@@ -103,11 +103,11 @@ eMBFuncReadCoils( UCHAR * pucFrame, USHORT * usLen )
|
|||||||
* byte is only partially field with unused coils set to zero. */
|
* byte is only partially field with unused coils set to zero. */
|
||||||
if( ( usCoilCount & 0x0007 ) != 0 )
|
if( ( usCoilCount & 0x0007 ) != 0 )
|
||||||
{
|
{
|
||||||
ucNBytes = ( UCHAR )( usCoilCount / 8 + 1 );
|
ucNBytes = ( uint8_t )( usCoilCount / 8 + 1 );
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
ucNBytes = ( UCHAR )( usCoilCount / 8 );
|
ucNBytes = ( uint8_t )( usCoilCount / 8 );
|
||||||
}
|
}
|
||||||
*pucFrameCur++ = ucNBytes;
|
*pucFrameCur++ = ucNBytes;
|
||||||
*usLen += 1;
|
*usLen += 1;
|
||||||
@@ -145,18 +145,18 @@ eMBFuncReadCoils( UCHAR * pucFrame, USHORT * usLen )
|
|||||||
|
|
||||||
#ifdef CONFIG_MB_FUNC_WRITE_COIL_ENABLED
|
#ifdef CONFIG_MB_FUNC_WRITE_COIL_ENABLED
|
||||||
eMBException
|
eMBException
|
||||||
eMBFuncWriteCoil( UCHAR * pucFrame, USHORT * usLen )
|
eMBFuncWriteCoil( uint8_t * pucFrame, uint16_t * usLen )
|
||||||
{
|
{
|
||||||
USHORT usRegAddress;
|
uint16_t usRegAddress;
|
||||||
UCHAR ucBuf[2];
|
uint8_t ucBuf[2];
|
||||||
|
|
||||||
eMBException eStatus = MB_EX_NONE;
|
eMBException eStatus = MB_EX_NONE;
|
||||||
eMBErrorCode eRegStatus;
|
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 = ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_ADDR_OFF] << 8 );
|
usRegAddress = ( uint16_t )( pucFrame[MB_PDU_FUNC_WRITE_ADDR_OFF] << 8 );
|
||||||
usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_ADDR_OFF + 1] );
|
usRegAddress |= ( uint16_t )( pucFrame[MB_PDU_FUNC_WRITE_ADDR_OFF + 1] );
|
||||||
usRegAddress++;
|
usRegAddress++;
|
||||||
|
|
||||||
if( ( pucFrame[MB_PDU_FUNC_WRITE_VALUE_OFF + 1] == 0x00 ) &&
|
if( ( pucFrame[MB_PDU_FUNC_WRITE_VALUE_OFF + 1] == 0x00 ) &&
|
||||||
@@ -199,35 +199,35 @@ eMBFuncWriteCoil( UCHAR * pucFrame, USHORT * usLen )
|
|||||||
|
|
||||||
#ifdef CONFIG_MB_FUNC_WRITE_MULTIPLE_COILS_ENABLED
|
#ifdef CONFIG_MB_FUNC_WRITE_MULTIPLE_COILS_ENABLED
|
||||||
eMBException
|
eMBException
|
||||||
eMBFuncWriteMultipleCoils( UCHAR * pucFrame, USHORT * usLen )
|
eMBFuncWriteMultipleCoils( uint8_t * pucFrame, uint16_t * usLen )
|
||||||
{
|
{
|
||||||
USHORT usRegAddress;
|
uint16_t usRegAddress;
|
||||||
USHORT usCoilCnt;
|
uint16_t usCoilCnt;
|
||||||
UCHAR ucByteCount;
|
uint8_t ucByteCount;
|
||||||
UCHAR ucByteCountVerify;
|
uint8_t ucByteCountVerify;
|
||||||
|
|
||||||
eMBException eStatus = MB_EX_NONE;
|
eMBException eStatus = MB_EX_NONE;
|
||||||
eMBErrorCode eRegStatus;
|
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 = ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_ADDR_OFF] << 8 );
|
usRegAddress = ( uint16_t )( pucFrame[MB_PDU_FUNC_WRITE_MUL_ADDR_OFF] << 8 );
|
||||||
usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_ADDR_OFF + 1] );
|
usRegAddress |= ( uint16_t )( pucFrame[MB_PDU_FUNC_WRITE_MUL_ADDR_OFF + 1] );
|
||||||
usRegAddress++;
|
usRegAddress++;
|
||||||
|
|
||||||
usCoilCnt = ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_COILCNT_OFF] << 8 );
|
usCoilCnt = ( uint16_t )( pucFrame[MB_PDU_FUNC_WRITE_MUL_COILCNT_OFF] << 8 );
|
||||||
usCoilCnt |= ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_COILCNT_OFF + 1] );
|
usCoilCnt |= ( uint16_t )( pucFrame[MB_PDU_FUNC_WRITE_MUL_COILCNT_OFF + 1] );
|
||||||
|
|
||||||
ucByteCount = pucFrame[MB_PDU_FUNC_WRITE_MUL_BYTECNT_OFF];
|
ucByteCount = pucFrame[MB_PDU_FUNC_WRITE_MUL_BYTECNT_OFF];
|
||||||
|
|
||||||
/* Compute the number of expected bytes in the request. */
|
/* Compute the number of expected bytes in the request. */
|
||||||
if( ( usCoilCnt & 0x0007 ) != 0 )
|
if( ( usCoilCnt & 0x0007 ) != 0 )
|
||||||
{
|
{
|
||||||
ucByteCountVerify = ( UCHAR )( usCoilCnt / 8 + 1 );
|
ucByteCountVerify = ( uint8_t )( usCoilCnt / 8 + 1 );
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
ucByteCountVerify = ( UCHAR )( usCoilCnt / 8 );
|
ucByteCountVerify = ( uint8_t )( usCoilCnt / 8 );
|
||||||
}
|
}
|
||||||
|
|
||||||
if( ( usCoilCnt >= 1 ) &&
|
if( ( usCoilCnt >= 1 ) &&
|
||||||
|
|||||||
@@ -44,24 +44,24 @@ eMBException prveMBError2Exception( eMBErrorCode eErrorCode );
|
|||||||
#ifdef CONFIG_MB_FUNC_READ_COILS_ENABLED
|
#ifdef CONFIG_MB_FUNC_READ_COILS_ENABLED
|
||||||
|
|
||||||
eMBException
|
eMBException
|
||||||
eMBFuncReadDiscreteInputs( UCHAR * pucFrame, USHORT * usLen )
|
eMBFuncReadDiscreteInputs( uint8_t * pucFrame, uint16_t * usLen )
|
||||||
{
|
{
|
||||||
USHORT usRegAddress;
|
uint16_t usRegAddress;
|
||||||
USHORT usDiscreteCnt;
|
uint16_t usDiscreteCnt;
|
||||||
UCHAR ucNBytes;
|
uint8_t ucNBytes;
|
||||||
UCHAR *pucFrameCur;
|
uint8_t *pucFrameCur;
|
||||||
|
|
||||||
eMBException eStatus = MB_EX_NONE;
|
eMBException eStatus = MB_EX_NONE;
|
||||||
eMBErrorCode eRegStatus;
|
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 = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF] << 8 );
|
usRegAddress = ( uint16_t )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF] << 8 );
|
||||||
usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF + 1] );
|
usRegAddress |= ( uint16_t )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF + 1] );
|
||||||
usRegAddress++;
|
usRegAddress++;
|
||||||
|
|
||||||
usDiscreteCnt = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_DISCCNT_OFF] << 8 );
|
usDiscreteCnt = ( uint16_t )( pucFrame[MB_PDU_FUNC_READ_DISCCNT_OFF] << 8 );
|
||||||
usDiscreteCnt |= ( USHORT )( pucFrame[MB_PDU_FUNC_READ_DISCCNT_OFF + 1] );
|
usDiscreteCnt |= ( uint16_t )( pucFrame[MB_PDU_FUNC_READ_DISCCNT_OFF + 1] );
|
||||||
|
|
||||||
/* Check if the number of registers to read is valid. If not
|
/* Check if the number of registers to read is valid. If not
|
||||||
* return Modbus illegal data value exception.
|
* return Modbus illegal data value exception.
|
||||||
@@ -81,11 +81,11 @@ eMBFuncReadDiscreteInputs( UCHAR * pucFrame, USHORT * usLen )
|
|||||||
* byte is only partially field with unused coils set to zero. */
|
* byte is only partially field with unused coils set to zero. */
|
||||||
if( ( usDiscreteCnt & 0x0007 ) != 0 )
|
if( ( usDiscreteCnt & 0x0007 ) != 0 )
|
||||||
{
|
{
|
||||||
ucNBytes = ( UCHAR ) ( usDiscreteCnt / 8 + 1 );
|
ucNBytes = ( uint8_t ) ( usDiscreteCnt / 8 + 1 );
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
ucNBytes = ( UCHAR ) ( usDiscreteCnt / 8 );
|
ucNBytes = ( uint8_t ) ( usDiscreteCnt / 8 );
|
||||||
}
|
}
|
||||||
*pucFrameCur++ = ucNBytes;
|
*pucFrameCur++ = ucNBytes;
|
||||||
*usLen += 1;
|
*usLen += 1;
|
||||||
|
|||||||
@@ -74,16 +74,16 @@ eMBException prveMBError2Exception( eMBErrorCode eErrorCode );
|
|||||||
#ifdef CONFIG_MB_FUNC_WRITE_HOLDING_ENABLED
|
#ifdef CONFIG_MB_FUNC_WRITE_HOLDING_ENABLED
|
||||||
|
|
||||||
eMBException
|
eMBException
|
||||||
eMBFuncWriteHoldingRegister( UCHAR * pucFrame, USHORT * usLen )
|
eMBFuncWriteHoldingRegister( uint8_t * pucFrame, uint16_t * usLen )
|
||||||
{
|
{
|
||||||
USHORT usRegAddress;
|
uint16_t usRegAddress;
|
||||||
eMBException eStatus = MB_EX_NONE;
|
eMBException eStatus = MB_EX_NONE;
|
||||||
eMBErrorCode eRegStatus;
|
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 = ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_ADDR_OFF] << 8 );
|
usRegAddress = ( uint16_t )( pucFrame[MB_PDU_FUNC_WRITE_ADDR_OFF] << 8 );
|
||||||
usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_ADDR_OFF + 1] );
|
usRegAddress |= ( uint16_t )( pucFrame[MB_PDU_FUNC_WRITE_ADDR_OFF + 1] );
|
||||||
usRegAddress++;
|
usRegAddress++;
|
||||||
|
|
||||||
/* Make callback to update the value. */
|
/* Make callback to update the value. */
|
||||||
@@ -107,29 +107,29 @@ eMBFuncWriteHoldingRegister( UCHAR * pucFrame, USHORT * usLen )
|
|||||||
|
|
||||||
#ifdef CONFIG_MB_FUNC_WRITE_MULTIPLE_HOLDING_ENABLED
|
#ifdef CONFIG_MB_FUNC_WRITE_MULTIPLE_HOLDING_ENABLED
|
||||||
eMBException
|
eMBException
|
||||||
eMBFuncWriteMultipleHoldingRegister( UCHAR * pucFrame, USHORT * usLen )
|
eMBFuncWriteMultipleHoldingRegister( uint8_t * pucFrame, uint16_t * usLen )
|
||||||
{
|
{
|
||||||
USHORT usRegAddress;
|
uint16_t usRegAddress;
|
||||||
USHORT usRegCount;
|
uint16_t usRegCount;
|
||||||
UCHAR ucRegByteCount;
|
uint8_t ucRegByteCount;
|
||||||
|
|
||||||
eMBException eStatus = MB_EX_NONE;
|
eMBException eStatus = MB_EX_NONE;
|
||||||
eMBErrorCode eRegStatus;
|
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 = ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_ADDR_OFF] << 8 );
|
usRegAddress = ( uint16_t )( pucFrame[MB_PDU_FUNC_WRITE_MUL_ADDR_OFF] << 8 );
|
||||||
usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_ADDR_OFF + 1] );
|
usRegAddress |= ( uint16_t )( pucFrame[MB_PDU_FUNC_WRITE_MUL_ADDR_OFF + 1] );
|
||||||
usRegAddress++;
|
usRegAddress++;
|
||||||
|
|
||||||
usRegCount = ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_REGCNT_OFF] << 8 );
|
usRegCount = ( uint16_t )( pucFrame[MB_PDU_FUNC_WRITE_MUL_REGCNT_OFF] << 8 );
|
||||||
usRegCount |= ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_REGCNT_OFF + 1] );
|
usRegCount |= ( uint16_t )( pucFrame[MB_PDU_FUNC_WRITE_MUL_REGCNT_OFF + 1] );
|
||||||
|
|
||||||
ucRegByteCount = pucFrame[MB_PDU_FUNC_WRITE_MUL_BYTECNT_OFF];
|
ucRegByteCount = pucFrame[MB_PDU_FUNC_WRITE_MUL_BYTECNT_OFF];
|
||||||
|
|
||||||
if( ( usRegCount >= 1 ) &&
|
if( ( usRegCount >= 1 ) &&
|
||||||
( usRegCount <= MB_PDU_FUNC_WRITE_MUL_REGCNT_MAX ) &&
|
( usRegCount <= MB_PDU_FUNC_WRITE_MUL_REGCNT_MAX ) &&
|
||||||
( ucRegByteCount == ( UCHAR ) ( 2 * usRegCount ) ) )
|
( ucRegByteCount == ( uint8_t ) ( 2 * usRegCount ) ) )
|
||||||
{
|
{
|
||||||
/* Make callback to update the register values. */
|
/* Make callback to update the register values. */
|
||||||
eRegStatus =
|
eRegStatus =
|
||||||
@@ -167,23 +167,23 @@ eMBFuncWriteMultipleHoldingRegister( UCHAR * pucFrame, USHORT * usLen )
|
|||||||
#ifdef CONFIG_MB_FUNC_READ_HOLDING_ENABLED
|
#ifdef CONFIG_MB_FUNC_READ_HOLDING_ENABLED
|
||||||
|
|
||||||
eMBException
|
eMBException
|
||||||
eMBFuncReadHoldingRegister( UCHAR * pucFrame, USHORT * usLen )
|
eMBFuncReadHoldingRegister( uint8_t * pucFrame, uint16_t * usLen )
|
||||||
{
|
{
|
||||||
USHORT usRegAddress;
|
uint16_t usRegAddress;
|
||||||
USHORT usRegCount;
|
uint16_t usRegCount;
|
||||||
UCHAR *pucFrameCur;
|
uint8_t *pucFrameCur;
|
||||||
|
|
||||||
eMBException eStatus = MB_EX_NONE;
|
eMBException eStatus = MB_EX_NONE;
|
||||||
eMBErrorCode eRegStatus;
|
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 = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF] << 8 );
|
usRegAddress = ( uint16_t )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF] << 8 );
|
||||||
usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF + 1] );
|
usRegAddress |= ( uint16_t )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF + 1] );
|
||||||
usRegAddress++;
|
usRegAddress++;
|
||||||
|
|
||||||
usRegCount = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_REGCNT_OFF] << 8 );
|
usRegCount = ( uint16_t )( pucFrame[MB_PDU_FUNC_READ_REGCNT_OFF] << 8 );
|
||||||
usRegCount = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_REGCNT_OFF + 1] );
|
usRegCount = ( uint16_t )( pucFrame[MB_PDU_FUNC_READ_REGCNT_OFF + 1] );
|
||||||
|
|
||||||
/* Check if the number of registers to read is valid. If not
|
/* Check if the number of registers to read is valid. If not
|
||||||
* return Modbus illegal data value exception.
|
* return Modbus illegal data value exception.
|
||||||
@@ -199,7 +199,7 @@ eMBFuncReadHoldingRegister( UCHAR * pucFrame, USHORT * usLen )
|
|||||||
*usLen += 1;
|
*usLen += 1;
|
||||||
|
|
||||||
/* Second byte in the response contain the number of bytes. */
|
/* Second byte in the response contain the number of bytes. */
|
||||||
*pucFrameCur++ = ( UCHAR ) ( usRegCount * 2 );
|
*pucFrameCur++ = ( uint8_t ) ( usRegCount * 2 );
|
||||||
*usLen += 1;
|
*usLen += 1;
|
||||||
|
|
||||||
/* Make callback to fill the buffer. */
|
/* Make callback to fill the buffer. */
|
||||||
@@ -232,33 +232,33 @@ eMBFuncReadHoldingRegister( UCHAR * pucFrame, USHORT * usLen )
|
|||||||
#ifdef CONFIG_MB_FUNC_READWRITE_HOLDING_ENABLED
|
#ifdef CONFIG_MB_FUNC_READWRITE_HOLDING_ENABLED
|
||||||
|
|
||||||
eMBException
|
eMBException
|
||||||
eMBFuncReadWriteMultipleHoldingRegister( UCHAR * pucFrame, USHORT * usLen )
|
eMBFuncReadWriteMultipleHoldingRegister( uint8_t * pucFrame, uint16_t * usLen )
|
||||||
{
|
{
|
||||||
USHORT usRegReadAddress;
|
uint16_t usRegReadAddress;
|
||||||
USHORT usRegReadCount;
|
uint16_t usRegReadCount;
|
||||||
USHORT usRegWriteAddress;
|
uint16_t usRegWriteAddress;
|
||||||
USHORT usRegWriteCount;
|
uint16_t usRegWriteCount;
|
||||||
UCHAR ucRegWriteByteCount;
|
uint8_t ucRegWriteByteCount;
|
||||||
UCHAR *pucFrameCur;
|
uint8_t *pucFrameCur;
|
||||||
|
|
||||||
eMBException eStatus = MB_EX_NONE;
|
eMBException eStatus = MB_EX_NONE;
|
||||||
eMBErrorCode eRegStatus;
|
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 = ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_READ_ADDR_OFF] << 8U );
|
usRegReadAddress = ( uint16_t )( pucFrame[MB_PDU_FUNC_READWRITE_READ_ADDR_OFF] << 8U );
|
||||||
usRegReadAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_READ_ADDR_OFF + 1] );
|
usRegReadAddress |= ( uint16_t )( pucFrame[MB_PDU_FUNC_READWRITE_READ_ADDR_OFF + 1] );
|
||||||
usRegReadAddress++;
|
usRegReadAddress++;
|
||||||
|
|
||||||
usRegReadCount = ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_READ_REGCNT_OFF] << 8U );
|
usRegReadCount = ( uint16_t )( pucFrame[MB_PDU_FUNC_READWRITE_READ_REGCNT_OFF] << 8U );
|
||||||
usRegReadCount |= ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_READ_REGCNT_OFF + 1] );
|
usRegReadCount |= ( uint16_t )( pucFrame[MB_PDU_FUNC_READWRITE_READ_REGCNT_OFF + 1] );
|
||||||
|
|
||||||
usRegWriteAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_WRITE_ADDR_OFF] << 8U );
|
usRegWriteAddress = ( uint16_t )( pucFrame[MB_PDU_FUNC_READWRITE_WRITE_ADDR_OFF] << 8U );
|
||||||
usRegWriteAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_WRITE_ADDR_OFF + 1] );
|
usRegWriteAddress |= ( uint16_t )( pucFrame[MB_PDU_FUNC_READWRITE_WRITE_ADDR_OFF + 1] );
|
||||||
usRegWriteAddress++;
|
usRegWriteAddress++;
|
||||||
|
|
||||||
usRegWriteCount = ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_WRITE_REGCNT_OFF] << 8U );
|
usRegWriteCount = ( uint16_t )( pucFrame[MB_PDU_FUNC_READWRITE_WRITE_REGCNT_OFF] << 8U );
|
||||||
usRegWriteCount |= ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_WRITE_REGCNT_OFF + 1] );
|
usRegWriteCount |= ( uint16_t )( pucFrame[MB_PDU_FUNC_READWRITE_WRITE_REGCNT_OFF + 1] );
|
||||||
|
|
||||||
ucRegWriteByteCount = pucFrame[MB_PDU_FUNC_READWRITE_BYTECNT_OFF];
|
ucRegWriteByteCount = pucFrame[MB_PDU_FUNC_READWRITE_BYTECNT_OFF];
|
||||||
|
|
||||||
@@ -281,7 +281,7 @@ eMBFuncReadWriteMultipleHoldingRegister( UCHAR * pucFrame, USHORT * usLen )
|
|||||||
*usLen += 1;
|
*usLen += 1;
|
||||||
|
|
||||||
/* Second byte in the response contain the number of bytes. */
|
/* Second byte in the response contain the number of bytes. */
|
||||||
*pucFrameCur++ = ( UCHAR ) ( usRegReadCount * 2 );
|
*pucFrameCur++ = ( uint8_t ) ( usRegReadCount * 2 );
|
||||||
*usLen += 1;
|
*usLen += 1;
|
||||||
|
|
||||||
/* Make the read callback. */
|
/* Make the read callback. */
|
||||||
|
|||||||
@@ -56,23 +56,23 @@ eMBException prveMBError2Exception( eMBErrorCode eErrorCode );
|
|||||||
|
|
||||||
#ifdef CONFIG_MB_FUNC_READ_INPUT_ENABLED
|
#ifdef CONFIG_MB_FUNC_READ_INPUT_ENABLED
|
||||||
eMBException
|
eMBException
|
||||||
eMBFuncReadInputRegister( UCHAR * pucFrame, USHORT * usLen )
|
eMBFuncReadInputRegister( uint8_t * pucFrame, uint16_t * usLen )
|
||||||
{
|
{
|
||||||
USHORT usRegAddress;
|
uint16_t usRegAddress;
|
||||||
USHORT usRegCount;
|
uint16_t usRegCount;
|
||||||
UCHAR *pucFrameCur;
|
uint8_t *pucFrameCur;
|
||||||
|
|
||||||
eMBException eStatus = MB_EX_NONE;
|
eMBException eStatus = MB_EX_NONE;
|
||||||
eMBErrorCode eRegStatus;
|
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 = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF] << 8 );
|
usRegAddress = ( uint16_t )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF] << 8 );
|
||||||
usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF + 1] );
|
usRegAddress |= ( uint16_t )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF + 1] );
|
||||||
usRegAddress++;
|
usRegAddress++;
|
||||||
|
|
||||||
usRegCount = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_REGCNT_OFF] << 8 );
|
usRegCount = ( uint16_t )( pucFrame[MB_PDU_FUNC_READ_REGCNT_OFF] << 8 );
|
||||||
usRegCount |= ( USHORT )( pucFrame[MB_PDU_FUNC_READ_REGCNT_OFF + 1] );
|
usRegCount |= ( uint16_t )( pucFrame[MB_PDU_FUNC_READ_REGCNT_OFF + 1] );
|
||||||
|
|
||||||
/* Check if the number of registers to read is valid. If not
|
/* Check if the number of registers to read is valid. If not
|
||||||
* return Modbus illegal data value exception.
|
* return Modbus illegal data value exception.
|
||||||
@@ -89,7 +89,7 @@ eMBFuncReadInputRegister( UCHAR * pucFrame, USHORT * usLen )
|
|||||||
*usLen += 1;
|
*usLen += 1;
|
||||||
|
|
||||||
/* Second byte in the response contain the number of bytes. */
|
/* Second byte in the response contain the number of bytes. */
|
||||||
*pucFrameCur++ = ( UCHAR )( usRegCount * 2 );
|
*pucFrameCur++ = ( uint8_t )( usRegCount * 2 );
|
||||||
*usLen += 1;
|
*usLen += 1;
|
||||||
|
|
||||||
eRegStatus =
|
eRegStatus =
|
||||||
|
|||||||
@@ -44,14 +44,14 @@
|
|||||||
#ifdef CONFIG_MB_FUNC_OTHER_REP_SLAVEID_ENABLED
|
#ifdef CONFIG_MB_FUNC_OTHER_REP_SLAVEID_ENABLED
|
||||||
|
|
||||||
/* ----------------------- Static variables ---------------------------------*/
|
/* ----------------------- Static variables ---------------------------------*/
|
||||||
static UCHAR ucMBSlaveID[CONFIG_MB_FUNC_OTHER_REP_SLAVEID_BUF];
|
static uint8_t ucMBSlaveID[CONFIG_MB_FUNC_OTHER_REP_SLAVEID_BUF];
|
||||||
static USHORT usMBSlaveIDLen;
|
static uint16_t usMBSlaveIDLen;
|
||||||
|
|
||||||
/* ----------------------- Start implementation -----------------------------*/
|
/* ----------------------- Start implementation -----------------------------*/
|
||||||
|
|
||||||
eMBErrorCode
|
eMBErrorCode
|
||||||
eMBSetSlaveID( UCHAR ucSlaveID, BOOL xIsRunning,
|
eMBSetSlaveID( uint8_t ucSlaveID, bool xIsRunning,
|
||||||
UCHAR const *pucAdditional, USHORT usAdditionalLen )
|
uint8_t const *pucAdditional, uint16_t usAdditionalLen )
|
||||||
{
|
{
|
||||||
eMBErrorCode eStatus = MB_ENOERR;
|
eMBErrorCode eStatus = MB_ENOERR;
|
||||||
|
|
||||||
@@ -62,7 +62,7 @@ eMBSetSlaveID( UCHAR ucSlaveID, BOOL xIsRunning,
|
|||||||
{
|
{
|
||||||
usMBSlaveIDLen = 0;
|
usMBSlaveIDLen = 0;
|
||||||
ucMBSlaveID[usMBSlaveIDLen++] = ucSlaveID;
|
ucMBSlaveID[usMBSlaveIDLen++] = ucSlaveID;
|
||||||
ucMBSlaveID[usMBSlaveIDLen++] = ( UCHAR )( xIsRunning ? 0xFF : 0x00 );
|
ucMBSlaveID[usMBSlaveIDLen++] = ( uint8_t )( xIsRunning ? 0xFF : 0x00 );
|
||||||
if( usAdditionalLen > 0 )
|
if( usAdditionalLen > 0 )
|
||||||
{
|
{
|
||||||
memcpy( &ucMBSlaveID[usMBSlaveIDLen], pucAdditional,
|
memcpy( &ucMBSlaveID[usMBSlaveIDLen], pucAdditional,
|
||||||
@@ -78,10 +78,10 @@ eMBSetSlaveID( UCHAR ucSlaveID, BOOL xIsRunning,
|
|||||||
}
|
}
|
||||||
|
|
||||||
eMBException
|
eMBException
|
||||||
eMBFuncReportSlaveID( UCHAR * pucFrame, USHORT * usLen )
|
eMBFuncReportSlaveID( uint8_t * pucFrame, uint16_t * usLen )
|
||||||
{
|
{
|
||||||
memcpy( &pucFrame[MB_PDU_DATA_OFF], &ucMBSlaveID[0], ( size_t )usMBSlaveIDLen );
|
memcpy( &pucFrame[MB_PDU_DATA_OFF], &ucMBSlaveID[0], ( size_t )usMBSlaveIDLen );
|
||||||
*usLen = ( USHORT )( MB_PDU_DATA_OFF + usMBSlaveIDLen );
|
*usLen = ( uint16_t )( MB_PDU_DATA_OFF + usMBSlaveIDLen );
|
||||||
return MB_EX_NONE;
|
return MB_EX_NONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -42,69 +42,69 @@
|
|||||||
#include <apps/modbus/mbproto.h>
|
#include <apps/modbus/mbproto.h>
|
||||||
|
|
||||||
/* ----------------------- Defines ------------------------------------------*/
|
/* ----------------------- Defines ------------------------------------------*/
|
||||||
#define BITS_UCHAR 8U
|
#define BITS_uint8_t 8U
|
||||||
|
|
||||||
/* ----------------------- Start implementation -----------------------------*/
|
/* ----------------------- Start implementation -----------------------------*/
|
||||||
void
|
void
|
||||||
xMBUtilSetBits( UCHAR * ucByteBuf, USHORT usBitOffset, UCHAR ucNBits,
|
xMBUtilSetBits( uint8_t * ucByteBuf, uint16_t usBitOffset, uint8_t ucNBits,
|
||||||
UCHAR ucValue )
|
uint8_t ucValue )
|
||||||
{
|
{
|
||||||
USHORT usWordBuf;
|
uint16_t usWordBuf;
|
||||||
USHORT usMask;
|
uint16_t usMask;
|
||||||
USHORT usByteOffset;
|
uint16_t usByteOffset;
|
||||||
USHORT usNPreBits;
|
uint16_t usNPreBits;
|
||||||
USHORT usValue = ucValue;
|
uint16_t usValue = ucValue;
|
||||||
|
|
||||||
ASSERT( ucNBits <= 8 );
|
ASSERT( ucNBits <= 8 );
|
||||||
ASSERT( ( size_t )BITS_UCHAR == sizeof( UCHAR ) * 8 );
|
ASSERT( ( size_t )BITS_uint8_t == sizeof( uint8_t ) * 8 );
|
||||||
|
|
||||||
/* Calculate byte offset for first byte containing the bit values starting
|
/* Calculate byte offset for first byte containing the bit values starting
|
||||||
* at usBitOffset. */
|
* at usBitOffset. */
|
||||||
usByteOffset = ( USHORT )( ( usBitOffset ) / BITS_UCHAR );
|
usByteOffset = ( uint16_t )( ( usBitOffset ) / BITS_uint8_t );
|
||||||
|
|
||||||
/* How many bits precede our bits to set. */
|
/* How many bits precede our bits to set. */
|
||||||
usNPreBits = ( USHORT )( usBitOffset - usByteOffset * BITS_UCHAR );
|
usNPreBits = ( uint16_t )( usBitOffset - usByteOffset * BITS_uint8_t );
|
||||||
|
|
||||||
/* Move bit field into position over bits to set */
|
/* Move bit field into position over bits to set */
|
||||||
usValue <<= usNPreBits;
|
usValue <<= usNPreBits;
|
||||||
|
|
||||||
/* Prepare a mask for setting the new bits. */
|
/* Prepare a mask for setting the new bits. */
|
||||||
usMask = ( USHORT )( ( 1 << ( USHORT ) ucNBits ) - 1 );
|
usMask = ( uint16_t )( ( 1 << ( uint16_t ) ucNBits ) - 1 );
|
||||||
usMask <<= usBitOffset - usByteOffset * BITS_UCHAR;
|
usMask <<= usBitOffset - usByteOffset * BITS_uint8_t;
|
||||||
|
|
||||||
/* copy bits into temporary storage. */
|
/* copy bits into temporary storage. */
|
||||||
usWordBuf = ucByteBuf[usByteOffset];
|
usWordBuf = ucByteBuf[usByteOffset];
|
||||||
usWordBuf |= ucByteBuf[usByteOffset + 1] << BITS_UCHAR;
|
usWordBuf |= ucByteBuf[usByteOffset + 1] << BITS_uint8_t;
|
||||||
|
|
||||||
/* Zero out bit field bits and then or value bits into them. */
|
/* Zero out bit field bits and then or value bits into them. */
|
||||||
usWordBuf = ( USHORT )( ( usWordBuf & ( ~usMask ) ) | usValue );
|
usWordBuf = ( uint16_t )( ( usWordBuf & ( ~usMask ) ) | usValue );
|
||||||
|
|
||||||
/* move bits back into storage */
|
/* move bits back into storage */
|
||||||
ucByteBuf[usByteOffset] = ( UCHAR )( usWordBuf & 0xFF );
|
ucByteBuf[usByteOffset] = ( uint8_t )( usWordBuf & 0xFF );
|
||||||
ucByteBuf[usByteOffset + 1] = ( UCHAR )( usWordBuf >> BITS_UCHAR );
|
ucByteBuf[usByteOffset + 1] = ( uint8_t )( usWordBuf >> BITS_uint8_t );
|
||||||
}
|
}
|
||||||
|
|
||||||
UCHAR
|
uint8_t
|
||||||
xMBUtilGetBits( UCHAR * ucByteBuf, USHORT usBitOffset, UCHAR ucNBits )
|
xMBUtilGetBits( uint8_t * ucByteBuf, uint16_t usBitOffset, uint8_t ucNBits )
|
||||||
{
|
{
|
||||||
USHORT usWordBuf;
|
uint16_t usWordBuf;
|
||||||
USHORT usMask;
|
uint16_t usMask;
|
||||||
USHORT usByteOffset;
|
uint16_t usByteOffset;
|
||||||
USHORT usNPreBits;
|
uint16_t usNPreBits;
|
||||||
|
|
||||||
/* Calculate byte offset for first byte containing the bit values starting
|
/* Calculate byte offset for first byte containing the bit values starting
|
||||||
* at usBitOffset. */
|
* at usBitOffset. */
|
||||||
usByteOffset = ( USHORT )( ( usBitOffset ) / BITS_UCHAR );
|
usByteOffset = ( uint16_t )( ( usBitOffset ) / BITS_uint8_t );
|
||||||
|
|
||||||
/* How many bits precede our bits to set. */
|
/* How many bits precede our bits to set. */
|
||||||
usNPreBits = ( USHORT )( usBitOffset - usByteOffset * BITS_UCHAR );
|
usNPreBits = ( uint16_t )( usBitOffset - usByteOffset * BITS_uint8_t );
|
||||||
|
|
||||||
/* Prepare a mask for setting the new bits. */
|
/* Prepare a mask for setting the new bits. */
|
||||||
usMask = ( USHORT )( ( 1 << ( USHORT ) ucNBits ) - 1 );
|
usMask = ( uint16_t )( ( 1 << ( uint16_t ) ucNBits ) - 1 );
|
||||||
|
|
||||||
/* copy bits into temporary storage. */
|
/* copy bits into temporary storage. */
|
||||||
usWordBuf = ucByteBuf[usByteOffset];
|
usWordBuf = ucByteBuf[usByteOffset];
|
||||||
usWordBuf |= ucByteBuf[usByteOffset + 1] << BITS_UCHAR;
|
usWordBuf |= ucByteBuf[usByteOffset + 1] << BITS_uint8_t;
|
||||||
|
|
||||||
/* throw away unneeded bits. */
|
/* throw away unneeded bits. */
|
||||||
usWordBuf >>= usNPreBits;
|
usWordBuf >>= usNPreBits;
|
||||||
@@ -112,7 +112,7 @@ xMBUtilGetBits( UCHAR * ucByteBuf, USHORT usBitOffset, UCHAR ucNBits )
|
|||||||
/* mask away bits above the requested bitfield. */
|
/* mask away bits above the requested bitfield. */
|
||||||
usWordBuf &= usMask;
|
usWordBuf &= usMask;
|
||||||
|
|
||||||
return ( UCHAR ) usWordBuf;
|
return ( uint8_t ) usWordBuf;
|
||||||
}
|
}
|
||||||
|
|
||||||
eMBException
|
eMBException
|
||||||
|
|||||||
+15
-15
@@ -62,7 +62,7 @@
|
|||||||
|
|
||||||
/* ----------------------- Static variables ---------------------------------*/
|
/* ----------------------- Static variables ---------------------------------*/
|
||||||
|
|
||||||
static UCHAR ucMBAddress;
|
static uint8_t ucMBAddress;
|
||||||
static eMBMode eMBCurrentMode;
|
static eMBMode eMBCurrentMode;
|
||||||
|
|
||||||
static enum
|
static enum
|
||||||
@@ -85,12 +85,12 @@ static pvMBFrameClose pvMBFrameCloseCur;
|
|||||||
* an external event has happend which includes a timeout or the reception
|
* an external event has happend which includes a timeout or the reception
|
||||||
* or transmission of a character.
|
* or transmission of a character.
|
||||||
*/
|
*/
|
||||||
BOOL( *pxMBFrameCBByteReceived ) ( void );
|
bool( *pxMBFrameCBByteReceived ) ( void );
|
||||||
BOOL( *pxMBFrameCBTransmitterEmpty ) ( void );
|
bool( *pxMBFrameCBTransmitterEmpty ) ( void );
|
||||||
BOOL( *pxMBPortCBTimerExpired ) ( void );
|
bool( *pxMBPortCBTimerExpired ) ( void );
|
||||||
|
|
||||||
BOOL( *pxMBFrameCBReceiveFSMCur ) ( void );
|
bool( *pxMBFrameCBReceiveFSMCur ) ( void );
|
||||||
BOOL( *pxMBFrameCBTransmitFSMCur ) ( void );
|
bool( *pxMBFrameCBTransmitFSMCur ) ( void );
|
||||||
|
|
||||||
/* An array of Modbus functions handlers which associates Modbus function
|
/* An array of Modbus functions handlers which associates Modbus function
|
||||||
* codes with implementing functions.
|
* codes with implementing functions.
|
||||||
@@ -130,7 +130,7 @@ static xMBFunctionHandler xFuncHandlers[CONFIG_MB_FUNC_HANDLERS_MAX] = {
|
|||||||
|
|
||||||
/* ----------------------- Start implementation -----------------------------*/
|
/* ----------------------- Start implementation -----------------------------*/
|
||||||
eMBErrorCode
|
eMBErrorCode
|
||||||
eMBInit( eMBMode eMode, UCHAR ucSlaveAddress, UCHAR ucPort, ULONG ulBaudRate, eMBParity eParity )
|
eMBInit( eMBMode eMode, uint8_t ucSlaveAddress, uint8_t ucPort, uint32_t ulBaudRate, eMBParity eParity )
|
||||||
{
|
{
|
||||||
eMBErrorCode eStatus = MB_ENOERR;
|
eMBErrorCode eStatus = MB_ENOERR;
|
||||||
|
|
||||||
@@ -197,7 +197,7 @@ eMBInit( eMBMode eMode, UCHAR ucSlaveAddress, UCHAR ucPort, ULONG ulBaudRate, eM
|
|||||||
|
|
||||||
#ifdef CONFIG_MB_TCP_ENABLED
|
#ifdef CONFIG_MB_TCP_ENABLED
|
||||||
eMBErrorCode
|
eMBErrorCode
|
||||||
eMBTCPInit( USHORT ucTCPPort )
|
eMBTCPInit( uint16_t ucTCPPort )
|
||||||
{
|
{
|
||||||
eMBErrorCode eStatus = MB_ENOERR;
|
eMBErrorCode eStatus = MB_ENOERR;
|
||||||
|
|
||||||
@@ -226,7 +226,7 @@ eMBTCPInit( USHORT ucTCPPort )
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
eMBErrorCode
|
eMBErrorCode
|
||||||
eMBRegisterCB( UCHAR ucFunctionCode, pxMBFunctionHandler pxHandler )
|
eMBRegisterCB( uint8_t ucFunctionCode, pxMBFunctionHandler pxHandler )
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
eMBErrorCode eStatus;
|
eMBErrorCode eStatus;
|
||||||
@@ -334,10 +334,10 @@ eMBDisable( void )
|
|||||||
eMBErrorCode
|
eMBErrorCode
|
||||||
eMBPoll( void )
|
eMBPoll( void )
|
||||||
{
|
{
|
||||||
static UCHAR *ucMBFrame;
|
static uint8_t *ucMBFrame;
|
||||||
static UCHAR ucRcvAddress;
|
static uint8_t ucRcvAddress;
|
||||||
static UCHAR ucFunctionCode;
|
static uint8_t ucFunctionCode;
|
||||||
static USHORT usLength;
|
static uint16_t usLength;
|
||||||
static eMBException eException;
|
static eMBException eException;
|
||||||
|
|
||||||
int i;
|
int i;
|
||||||
@@ -352,7 +352,7 @@ eMBPoll( void )
|
|||||||
|
|
||||||
/* Check if there is a event available. If not return control to caller.
|
/* Check if there is a event available. If not return control to caller.
|
||||||
* Otherwise we will handle the event. */
|
* Otherwise we will handle the event. */
|
||||||
if( xMBPortEventGet( &eEvent ) == TRUE )
|
if( xMBPortEventGet( &eEvent ) == true )
|
||||||
{
|
{
|
||||||
switch ( eEvent )
|
switch ( eEvent )
|
||||||
{
|
{
|
||||||
@@ -396,7 +396,7 @@ eMBPoll( void )
|
|||||||
{
|
{
|
||||||
/* An exception occured. Build an error frame. */
|
/* An exception occured. Build an error frame. */
|
||||||
usLength = 0;
|
usLength = 0;
|
||||||
ucMBFrame[usLength++] = ( UCHAR )( ucFunctionCode | MB_FUNC_ERROR );
|
ucMBFrame[usLength++] = ( uint8_t )( ucFunctionCode | MB_FUNC_ERROR );
|
||||||
ucMBFrame[usLength++] = eException;
|
ucMBFrame[usLength++] = eException;
|
||||||
}
|
}
|
||||||
if( ( eMBCurrentMode == MB_ASCII ) && CONFIG_MB_ASCII_TIMEOUT_WAIT_BEFORE_SEND_MS )
|
if( ( eMBCurrentMode == MB_ASCII ) && CONFIG_MB_ASCII_TIMEOUT_WAIT_BEFORE_SEND_MS )
|
||||||
|
|||||||
@@ -43,12 +43,12 @@ PR_BEGIN_EXTERN_C
|
|||||||
|
|
||||||
#define MB_PORT_HAS_CLOSE 1
|
#define MB_PORT_HAS_CLOSE 1
|
||||||
|
|
||||||
#ifndef TRUE
|
#ifndef true
|
||||||
# define TRUE true
|
# define true true
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef FALSE
|
#ifndef false
|
||||||
# define FALSE false
|
# define false false
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* ----------------------- Type definitions ---------------------------------*/
|
/* ----------------------- Type definitions ---------------------------------*/
|
||||||
@@ -61,23 +61,15 @@ typedef enum
|
|||||||
MB_LOG_DEBUG = 3
|
MB_LOG_DEBUG = 3
|
||||||
} eMBPortLogLevel;
|
} eMBPortLogLevel;
|
||||||
|
|
||||||
typedef bool BOOL;
|
|
||||||
typedef uint8_t UCHAR;
|
|
||||||
typedef int8_t CHAR;
|
|
||||||
typedef uint16_t USHORT;
|
|
||||||
typedef int16_t SHORT;
|
|
||||||
typedef uint32_t ULONG;
|
|
||||||
typedef int32_t LONG;
|
|
||||||
|
|
||||||
/* ----------------------- Function prototypes ------------------------------*/
|
/* ----------------------- Function prototypes ------------------------------*/
|
||||||
|
|
||||||
void vMBPortEnterCritical(void);
|
void vMBPortEnterCritical(void);
|
||||||
void vMBPortExitCritical(void);
|
void vMBPortExitCritical(void);
|
||||||
void vMBPortLog(eMBPortLogLevel eLevel, const CHAR * szModule,
|
void vMBPortLog(eMBPortLogLevel eLevel, const char * szModule,
|
||||||
const CHAR * szFmt, ...);
|
const char * szFmt, ...);
|
||||||
void vMBPortTimerPoll(void);
|
void vMBPortTimerPoll(void);
|
||||||
BOOL xMBPortSerialPoll(void);
|
bool xMBPortSerialPoll(void);
|
||||||
BOOL xMBPortSerialSetTimeout(ULONG dwTimeoutMs);
|
bool xMBPortSerialSetTimeout(uint32_t dwTimeoutMs);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
PR_END_EXTERN_C
|
PR_END_EXTERN_C
|
||||||
|
|||||||
@@ -25,36 +25,38 @@
|
|||||||
#include <apps/modbus/mb.h>
|
#include <apps/modbus/mb.h>
|
||||||
#include <apps/modbus/mbport.h>
|
#include <apps/modbus/mbport.h>
|
||||||
|
|
||||||
|
#include "port.h"
|
||||||
|
|
||||||
/* ----------------------- Variables ----------------------------------------*/
|
/* ----------------------- Variables ----------------------------------------*/
|
||||||
static eMBEventType eQueuedEvent;
|
static eMBEventType eQueuedEvent;
|
||||||
static BOOL xEventInQueue;
|
static bool xEventInQueue;
|
||||||
|
|
||||||
/* ----------------------- Start implementation -----------------------------*/
|
/* ----------------------- Start implementation -----------------------------*/
|
||||||
BOOL
|
bool
|
||||||
xMBPortEventInit( void )
|
xMBPortEventInit( void )
|
||||||
{
|
{
|
||||||
xEventInQueue = FALSE;
|
xEventInQueue = false;
|
||||||
return TRUE;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOL
|
bool
|
||||||
xMBPortEventPost( eMBEventType eEvent )
|
xMBPortEventPost( eMBEventType eEvent )
|
||||||
{
|
{
|
||||||
xEventInQueue = TRUE;
|
xEventInQueue = true;
|
||||||
eQueuedEvent = eEvent;
|
eQueuedEvent = eEvent;
|
||||||
return TRUE;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOL
|
bool
|
||||||
xMBPortEventGet( eMBEventType * eEvent )
|
xMBPortEventGet( eMBEventType * eEvent )
|
||||||
{
|
{
|
||||||
BOOL xEventHappened = FALSE;
|
bool xEventHappened = false;
|
||||||
|
|
||||||
if( xEventInQueue )
|
if( xEventInQueue )
|
||||||
{
|
{
|
||||||
*eEvent = eQueuedEvent;
|
*eEvent = eQueuedEvent;
|
||||||
xEventInQueue = FALSE;
|
xEventInQueue = false;
|
||||||
xEventHappened = TRUE;
|
xEventHappened = true;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -58,9 +58,9 @@ vMBPortLogFile( FILE * fNewLogFile )
|
|||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
vMBPortLog( eMBPortLogLevel eLevel, const CHAR * szModule, const CHAR * szFmt, ... )
|
vMBPortLog( eMBPortLogLevel eLevel, const char * szModule, const char * szFmt, ... )
|
||||||
{
|
{
|
||||||
CHAR szBuf[512];
|
char szBuf[512];
|
||||||
int i;
|
int i;
|
||||||
va_list args;
|
va_list args;
|
||||||
FILE *fOutput = fLogFile == NULL ? stderr : fLogFile;
|
FILE *fOutput = fLogFile == NULL ? stderr : fLogFile;
|
||||||
|
|||||||
@@ -50,23 +50,23 @@
|
|||||||
|
|
||||||
/* ----------------------- Static variables ---------------------------------*/
|
/* ----------------------- Static variables ---------------------------------*/
|
||||||
static int iSerialFd = -1;
|
static int iSerialFd = -1;
|
||||||
static BOOL bRxEnabled;
|
static bool bRxEnabled;
|
||||||
static BOOL bTxEnabled;
|
static bool bTxEnabled;
|
||||||
|
|
||||||
static ULONG ulTimeoutMs;
|
static uint32_t ulTimeoutMs;
|
||||||
static UCHAR ucBuffer[BUF_SIZE];
|
static uint8_t ucBuffer[BUF_SIZE];
|
||||||
static int uiRxBufferPos;
|
static int uiRxBufferPos;
|
||||||
static int uiTxBufferPos;
|
static int uiTxBufferPos;
|
||||||
|
|
||||||
static struct termios xOldTIO;
|
static struct termios xOldTIO;
|
||||||
|
|
||||||
/* ----------------------- Function prototypes ------------------------------*/
|
/* ----------------------- Function prototypes ------------------------------*/
|
||||||
static BOOL prvbMBPortSerialRead( UCHAR * pucBuffer, USHORT usNBytes, USHORT * usNBytesRead );
|
static bool prvbMBPortSerialRead( uint8_t * pucBuffer, uint16_t usNBytes, uint16_t * usNBytesRead );
|
||||||
static BOOL prvbMBPortSerialWrite( UCHAR * pucBuffer, USHORT usNBytes );
|
static bool prvbMBPortSerialWrite( uint8_t * pucBuffer, uint16_t usNBytes );
|
||||||
|
|
||||||
/* ----------------------- Begin implementation -----------------------------*/
|
/* ----------------------- Begin implementation -----------------------------*/
|
||||||
void
|
void
|
||||||
vMBPortSerialEnable( BOOL bEnableRx, BOOL bEnableTx )
|
vMBPortSerialEnable( bool bEnableRx, bool bEnableTx )
|
||||||
{
|
{
|
||||||
/* it is not allowed that both receiver and transmitter are enabled. */
|
/* it is not allowed that both receiver and transmitter are enabled. */
|
||||||
ASSERT( !bEnableRx || !bEnableTx );
|
ASSERT( !bEnableRx || !bEnableTx );
|
||||||
@@ -75,28 +75,28 @@ vMBPortSerialEnable( BOOL bEnableRx, BOOL bEnableTx )
|
|||||||
{
|
{
|
||||||
( void )tcflush( iSerialFd, TCIFLUSH );
|
( void )tcflush( iSerialFd, TCIFLUSH );
|
||||||
uiRxBufferPos = 0;
|
uiRxBufferPos = 0;
|
||||||
bRxEnabled = TRUE;
|
bRxEnabled = true;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
bRxEnabled = FALSE;
|
bRxEnabled = false;
|
||||||
}
|
}
|
||||||
if( bEnableTx )
|
if( bEnableTx )
|
||||||
{
|
{
|
||||||
bTxEnabled = TRUE;
|
bTxEnabled = true;
|
||||||
uiTxBufferPos = 0;
|
uiTxBufferPos = 0;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
bTxEnabled = FALSE;
|
bTxEnabled = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOL
|
bool
|
||||||
xMBPortSerialInit( UCHAR ucPort, ULONG ulBaudRate, UCHAR ucDataBits, eMBParity eParity )
|
xMBPortSerialInit( uint8_t ucPort, uint32_t ulBaudRate, uint8_t ucDataBits, eMBParity eParity )
|
||||||
{
|
{
|
||||||
CHAR szDevice[16];
|
char szDevice[16];
|
||||||
BOOL bStatus = TRUE;
|
bool bStatus = true;
|
||||||
|
|
||||||
struct termios xNewTIO;
|
struct termios xNewTIO;
|
||||||
speed_t xNewSpeed;
|
speed_t xNewSpeed;
|
||||||
@@ -130,7 +130,7 @@ xMBPortSerialInit( UCHAR ucPort, ULONG ulBaudRate, UCHAR ucDataBits, eMBParity e
|
|||||||
xNewTIO.c_cflag |= PARENB | PARODD;
|
xNewTIO.c_cflag |= PARENB | PARODD;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
bStatus = FALSE;
|
bStatus = false;
|
||||||
}
|
}
|
||||||
switch ( ucDataBits )
|
switch ( ucDataBits )
|
||||||
{
|
{
|
||||||
@@ -141,7 +141,7 @@ xMBPortSerialInit( UCHAR ucPort, ULONG ulBaudRate, UCHAR ucDataBits, eMBParity e
|
|||||||
xNewTIO.c_cflag |= CS7;
|
xNewTIO.c_cflag |= CS7;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
bStatus = FALSE;
|
bStatus = false;
|
||||||
}
|
}
|
||||||
switch ( ulBaudRate )
|
switch ( ulBaudRate )
|
||||||
{
|
{
|
||||||
@@ -161,7 +161,7 @@ xMBPortSerialInit( UCHAR ucPort, ULONG ulBaudRate, UCHAR ucDataBits, eMBParity e
|
|||||||
xNewSpeed = B115200;
|
xNewSpeed = B115200;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
bStatus = FALSE;
|
bStatus = false;
|
||||||
}
|
}
|
||||||
if( bStatus )
|
if( bStatus )
|
||||||
{
|
{
|
||||||
@@ -182,16 +182,16 @@ xMBPortSerialInit( UCHAR ucPort, ULONG ulBaudRate, UCHAR ucDataBits, eMBParity e
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
vMBPortSerialEnable( FALSE, FALSE );
|
vMBPortSerialEnable( false, false );
|
||||||
bStatus = TRUE;
|
bStatus = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return bStatus;
|
return bStatus;
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOL
|
bool
|
||||||
xMBPortSerialSetTimeout( ULONG ulNewTimeoutMs )
|
xMBPortSerialSetTimeout( uint32_t ulNewTimeoutMs )
|
||||||
{
|
{
|
||||||
if( ulNewTimeoutMs > 0 )
|
if( ulNewTimeoutMs > 0 )
|
||||||
{
|
{
|
||||||
@@ -201,7 +201,7 @@ xMBPortSerialSetTimeout( ULONG ulNewTimeoutMs )
|
|||||||
{
|
{
|
||||||
ulTimeoutMs = 1;
|
ulTimeoutMs = 1;
|
||||||
}
|
}
|
||||||
return TRUE;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
@@ -215,10 +215,10 @@ vMBPortClose( void )
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOL
|
bool
|
||||||
prvbMBPortSerialRead( UCHAR * pucBuffer, USHORT usNBytes, USHORT * usNBytesRead )
|
prvbMBPortSerialRead( uint8_t * pucBuffer, uint16_t usNBytes, uint16_t * usNBytesRead )
|
||||||
{
|
{
|
||||||
BOOL bResult = TRUE;
|
bool bResult = true;
|
||||||
ssize_t res;
|
ssize_t res;
|
||||||
fd_set rfds;
|
fd_set rfds;
|
||||||
struct timeval tv;
|
struct timeval tv;
|
||||||
@@ -236,18 +236,18 @@ prvbMBPortSerialRead( UCHAR * pucBuffer, USHORT usNBytes, USHORT * usNBytesRead
|
|||||||
{
|
{
|
||||||
if( errno != EINTR )
|
if( errno != EINTR )
|
||||||
{
|
{
|
||||||
bResult = FALSE;
|
bResult = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if( FD_ISSET( iSerialFd, &rfds ) )
|
else if( FD_ISSET( iSerialFd, &rfds ) )
|
||||||
{
|
{
|
||||||
if( ( res = read( iSerialFd, pucBuffer, usNBytes ) ) == -1 )
|
if( ( res = read( iSerialFd, pucBuffer, usNBytes ) ) == -1 )
|
||||||
{
|
{
|
||||||
bResult = FALSE;
|
bResult = false;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
*usNBytesRead = ( USHORT ) res;
|
*usNBytesRead = ( uint16_t ) res;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -257,12 +257,12 @@ prvbMBPortSerialRead( UCHAR * pucBuffer, USHORT usNBytes, USHORT * usNBytesRead
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
while( bResult == TRUE );
|
while( bResult == true );
|
||||||
return bResult;
|
return bResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOL
|
bool
|
||||||
prvbMBPortSerialWrite( UCHAR * pucBuffer, USHORT usNBytes )
|
prvbMBPortSerialWrite( uint8_t * pucBuffer, uint16_t usNBytes )
|
||||||
{
|
{
|
||||||
ssize_t res;
|
ssize_t res;
|
||||||
size_t left = ( size_t ) usNBytes;
|
size_t left = ( size_t ) usNBytes;
|
||||||
@@ -282,14 +282,14 @@ prvbMBPortSerialWrite( UCHAR * pucBuffer, USHORT usNBytes )
|
|||||||
done += res;
|
done += res;
|
||||||
left -= res;
|
left -= res;
|
||||||
}
|
}
|
||||||
return left == 0 ? TRUE : FALSE;
|
return left == 0 ? true : false;
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOL
|
bool
|
||||||
xMBPortSerialPoll( )
|
xMBPortSerialPoll( )
|
||||||
{
|
{
|
||||||
BOOL bStatus = TRUE;
|
bool bStatus = true;
|
||||||
USHORT usBytesRead;
|
uint16_t usBytesRead;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
while( bRxEnabled )
|
while( bRxEnabled )
|
||||||
@@ -315,7 +315,7 @@ xMBPortSerialPoll( )
|
|||||||
{
|
{
|
||||||
vMBPortLog( MB_LOG_ERROR, "SER-POLL", "read failed on serial device: %s\n",
|
vMBPortLog( MB_LOG_ERROR, "SER-POLL", "read failed on serial device: %s\n",
|
||||||
strerror( errno ) );
|
strerror( errno ) );
|
||||||
bStatus = FALSE;
|
bStatus = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if( bTxEnabled )
|
if( bTxEnabled )
|
||||||
@@ -329,27 +329,27 @@ xMBPortSerialPoll( )
|
|||||||
{
|
{
|
||||||
vMBPortLog( MB_LOG_ERROR, "SER-POLL", "write failed on serial device: %s\n",
|
vMBPortLog( MB_LOG_ERROR, "SER-POLL", "write failed on serial device: %s\n",
|
||||||
strerror( errno ) );
|
strerror( errno ) );
|
||||||
bStatus = FALSE;
|
bStatus = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return bStatus;
|
return bStatus;
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOL
|
bool
|
||||||
xMBPortSerialPutByte( CHAR ucByte )
|
xMBPortSerialPutByte( int8_t ucByte )
|
||||||
{
|
{
|
||||||
ASSERT( uiTxBufferPos < BUF_SIZE );
|
ASSERT( uiTxBufferPos < BUF_SIZE );
|
||||||
ucBuffer[uiTxBufferPos] = ucByte;
|
ucBuffer[uiTxBufferPos] = ucByte;
|
||||||
uiTxBufferPos++;
|
uiTxBufferPos++;
|
||||||
return TRUE;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOL
|
bool
|
||||||
xMBPortSerialGetByte( CHAR * pucByte )
|
xMBPortSerialGetByte( int8_t * pucByte )
|
||||||
{
|
{
|
||||||
ASSERT( uiRxBufferPos < BUF_SIZE );
|
ASSERT( uiRxBufferPos < BUF_SIZE );
|
||||||
*pucByte = ucBuffer[uiRxBufferPos];
|
*pucByte = ucBuffer[uiRxBufferPos];
|
||||||
uiRxBufferPos++;
|
uiRxBufferPos++;
|
||||||
return TRUE;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,14 +37,14 @@
|
|||||||
/* ----------------------- Defines ------------------------------------------*/
|
/* ----------------------- Defines ------------------------------------------*/
|
||||||
|
|
||||||
/* ----------------------- Static variables ---------------------------------*/
|
/* ----------------------- Static variables ---------------------------------*/
|
||||||
ULONG ulTimeOut;
|
uint32_t ulTimeOut;
|
||||||
BOOL bTimeoutEnable;
|
bool bTimeoutEnable;
|
||||||
|
|
||||||
static struct timeval xTimeLast;
|
static struct timeval xTimeLast;
|
||||||
|
|
||||||
/* ----------------------- Start implementation -----------------------------*/
|
/* ----------------------- Start implementation -----------------------------*/
|
||||||
BOOL
|
bool
|
||||||
xMBPortTimersInit( USHORT usTim1Timerout50us )
|
xMBPortTimersInit( uint16_t usTim1Timerout50us )
|
||||||
{
|
{
|
||||||
ulTimeOut = usTim1Timerout50us / 20U;
|
ulTimeOut = usTim1Timerout50us / 20U;
|
||||||
if( ulTimeOut == 0 )
|
if( ulTimeOut == 0 )
|
||||||
@@ -62,7 +62,7 @@ xMBPortTimersClose( )
|
|||||||
void
|
void
|
||||||
vMBPortTimerPoll( )
|
vMBPortTimerPoll( )
|
||||||
{
|
{
|
||||||
ULONG ulDeltaMS;
|
uint32_t ulDeltaMS;
|
||||||
struct timeval xTimeCur;
|
struct timeval xTimeCur;
|
||||||
|
|
||||||
/* Timers are called from the serial layer because we have no high
|
/* Timers are called from the serial layer because we have no high
|
||||||
@@ -79,7 +79,7 @@ vMBPortTimerPoll( )
|
|||||||
( xTimeCur.tv_usec - xTimeLast.tv_usec ) * 1000L;
|
( xTimeCur.tv_usec - xTimeLast.tv_usec ) * 1000L;
|
||||||
if( ulDeltaMS > ulTimeOut )
|
if( ulDeltaMS > ulTimeOut )
|
||||||
{
|
{
|
||||||
bTimeoutEnable = FALSE;
|
bTimeoutEnable = false;
|
||||||
( void )pxMBPortCBTimerExpired( );
|
( void )pxMBPortCBTimerExpired( );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -92,11 +92,11 @@ vMBPortTimersEnable( )
|
|||||||
int res = gettimeofday( &xTimeLast, NULL );
|
int res = gettimeofday( &xTimeLast, NULL );
|
||||||
|
|
||||||
ASSERT( res == 0 );
|
ASSERT( res == 0 );
|
||||||
bTimeoutEnable = TRUE;
|
bTimeoutEnable = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
vMBPortTimersDisable( )
|
vMBPortTimersDisable( )
|
||||||
{
|
{
|
||||||
bTimeoutEnable = FALSE;
|
bTimeoutEnable = false;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,7 +31,7 @@
|
|||||||
/* ----------------------- Platform includes --------------------------------*/
|
/* ----------------------- Platform includes --------------------------------*/
|
||||||
#include "port.h"
|
#include "port.h"
|
||||||
|
|
||||||
static const UCHAR aucCRCHi[] = {
|
static const uint8_t aucCRCHi[] = {
|
||||||
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
|
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
|
||||||
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
|
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
|
||||||
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
|
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
|
||||||
@@ -56,7 +56,7 @@ static const UCHAR aucCRCHi[] = {
|
|||||||
0x00, 0xC1, 0x81, 0x40
|
0x00, 0xC1, 0x81, 0x40
|
||||||
};
|
};
|
||||||
|
|
||||||
static const UCHAR aucCRCLo[] = {
|
static const uint8_t aucCRCLo[] = {
|
||||||
0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0xC7,
|
0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0xC7,
|
||||||
0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E,
|
0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E,
|
||||||
0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09, 0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9,
|
0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09, 0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9,
|
||||||
@@ -81,18 +81,18 @@ static const UCHAR aucCRCLo[] = {
|
|||||||
0x41, 0x81, 0x80, 0x40
|
0x41, 0x81, 0x80, 0x40
|
||||||
};
|
};
|
||||||
|
|
||||||
USHORT
|
uint16_t
|
||||||
usMBCRC16( UCHAR * pucFrame, USHORT usLen )
|
usMBCRC16( uint8_t * pucFrame, uint16_t usLen )
|
||||||
{
|
{
|
||||||
UCHAR ucCRCHi = 0xFF;
|
uint8_t ucCRCHi = 0xFF;
|
||||||
UCHAR ucCRCLo = 0xFF;
|
uint8_t ucCRCLo = 0xFF;
|
||||||
int iIndex;
|
int iIndex;
|
||||||
|
|
||||||
while( usLen-- )
|
while( usLen-- )
|
||||||
{
|
{
|
||||||
iIndex = ucCRCLo ^ *( pucFrame++ );
|
iIndex = ucCRCLo ^ *( pucFrame++ );
|
||||||
ucCRCLo = ( UCHAR )( ucCRCHi ^ aucCRCHi[iIndex] );
|
ucCRCLo = ( uint8_t )( ucCRCHi ^ aucCRCHi[iIndex] );
|
||||||
ucCRCHi = aucCRCLo[iIndex];
|
ucCRCHi = aucCRCLo[iIndex];
|
||||||
}
|
}
|
||||||
return ( USHORT )( ucCRCHi << 8 | ucCRCLo );
|
return ( uint16_t )( ucCRCHi << 8 | ucCRCLo );
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,6 +31,6 @@
|
|||||||
#ifndef _MB_CRC_H
|
#ifndef _MB_CRC_H
|
||||||
#define _MB_CRC_H
|
#define _MB_CRC_H
|
||||||
|
|
||||||
USHORT usMBCRC16( UCHAR * pucFrame, USHORT usLen );
|
uint16_t usMBCRC16( uint8_t * pucFrame, uint16_t usLen );
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
+34
-34
@@ -71,25 +71,25 @@ typedef enum
|
|||||||
static volatile eMBSndState eSndState;
|
static volatile eMBSndState eSndState;
|
||||||
static volatile eMBRcvState eRcvState;
|
static volatile eMBRcvState eRcvState;
|
||||||
|
|
||||||
volatile UCHAR ucRTUBuf[MB_SER_PDU_SIZE_MAX];
|
volatile uint8_t ucRTUBuf[MB_SER_PDU_SIZE_MAX];
|
||||||
|
|
||||||
static volatile UCHAR *pucSndBufferCur;
|
static volatile uint8_t *pucSndBufferCur;
|
||||||
static volatile USHORT usSndBufferCount;
|
static volatile uint16_t usSndBufferCount;
|
||||||
|
|
||||||
static volatile USHORT usRcvBufferPos;
|
static volatile uint16_t usRcvBufferPos;
|
||||||
|
|
||||||
/* ----------------------- Start implementation -----------------------------*/
|
/* ----------------------- Start implementation -----------------------------*/
|
||||||
eMBErrorCode
|
eMBErrorCode
|
||||||
eMBRTUInit( UCHAR ucSlaveAddress, UCHAR ucPort, ULONG ulBaudRate, eMBParity eParity )
|
eMBRTUInit( uint8_t ucSlaveAddress, uint8_t ucPort, uint32_t ulBaudRate, eMBParity eParity )
|
||||||
{
|
{
|
||||||
eMBErrorCode eStatus = MB_ENOERR;
|
eMBErrorCode eStatus = MB_ENOERR;
|
||||||
ULONG usTimerT35_50us;
|
uint32_t usTimerT35_50us;
|
||||||
|
|
||||||
( void )ucSlaveAddress;
|
( void )ucSlaveAddress;
|
||||||
ENTER_CRITICAL_SECTION( );
|
ENTER_CRITICAL_SECTION( );
|
||||||
|
|
||||||
/* Modbus RTU uses 8 Databits. */
|
/* Modbus RTU uses 8 Databits. */
|
||||||
if( xMBPortSerialInit( ucPort, ulBaudRate, 8, eParity ) != TRUE )
|
if( xMBPortSerialInit( ucPort, ulBaudRate, 8, eParity ) != true )
|
||||||
{
|
{
|
||||||
eStatus = MB_EPORTERR;
|
eStatus = MB_EPORTERR;
|
||||||
}
|
}
|
||||||
@@ -114,7 +114,7 @@ eMBRTUInit( UCHAR ucSlaveAddress, UCHAR ucPort, ULONG ulBaudRate, eMBParity ePar
|
|||||||
*/
|
*/
|
||||||
usTimerT35_50us = ( 7UL * 220000UL ) / ( 2UL * ulBaudRate );
|
usTimerT35_50us = ( 7UL * 220000UL ) / ( 2UL * ulBaudRate );
|
||||||
}
|
}
|
||||||
if( xMBPortTimersInit( ( USHORT ) usTimerT35_50us ) != TRUE )
|
if( xMBPortTimersInit( ( uint16_t ) usTimerT35_50us ) != true )
|
||||||
{
|
{
|
||||||
eStatus = MB_EPORTERR;
|
eStatus = MB_EPORTERR;
|
||||||
}
|
}
|
||||||
@@ -134,7 +134,7 @@ eMBRTUStart( void )
|
|||||||
* modbus protocol stack until the bus is free.
|
* modbus protocol stack until the bus is free.
|
||||||
*/
|
*/
|
||||||
eRcvState = STATE_RX_INIT;
|
eRcvState = STATE_RX_INIT;
|
||||||
vMBPortSerialEnable( TRUE, FALSE );
|
vMBPortSerialEnable( true, false );
|
||||||
vMBPortTimersEnable( );
|
vMBPortTimersEnable( );
|
||||||
|
|
||||||
EXIT_CRITICAL_SECTION( );
|
EXIT_CRITICAL_SECTION( );
|
||||||
@@ -144,15 +144,15 @@ void
|
|||||||
eMBRTUStop( void )
|
eMBRTUStop( void )
|
||||||
{
|
{
|
||||||
ENTER_CRITICAL_SECTION( );
|
ENTER_CRITICAL_SECTION( );
|
||||||
vMBPortSerialEnable( FALSE, FALSE );
|
vMBPortSerialEnable( false, false );
|
||||||
vMBPortTimersDisable( );
|
vMBPortTimersDisable( );
|
||||||
EXIT_CRITICAL_SECTION( );
|
EXIT_CRITICAL_SECTION( );
|
||||||
}
|
}
|
||||||
|
|
||||||
eMBErrorCode
|
eMBErrorCode
|
||||||
eMBRTUReceive( UCHAR * pucRcvAddress, UCHAR ** pucFrame, USHORT * pusLength )
|
eMBRTUReceive( uint8_t * pucRcvAddress, uint8_t ** pucFrame, uint16_t * pusLength )
|
||||||
{
|
{
|
||||||
BOOL xFrameReceived = FALSE;
|
bool xFrameReceived = false;
|
||||||
eMBErrorCode eStatus = MB_ENOERR;
|
eMBErrorCode eStatus = MB_ENOERR;
|
||||||
|
|
||||||
ENTER_CRITICAL_SECTION( );
|
ENTER_CRITICAL_SECTION( );
|
||||||
@@ -160,7 +160,7 @@ eMBRTUReceive( UCHAR * pucRcvAddress, UCHAR ** pucFrame, USHORT * pusLength )
|
|||||||
|
|
||||||
/* Length and CRC check */
|
/* Length and CRC check */
|
||||||
if( ( usRcvBufferPos >= MB_SER_PDU_SIZE_MIN )
|
if( ( usRcvBufferPos >= MB_SER_PDU_SIZE_MIN )
|
||||||
&& ( usMBCRC16( ( UCHAR * ) ucRTUBuf, usRcvBufferPos ) == 0 ) )
|
&& ( usMBCRC16( ( uint8_t * ) ucRTUBuf, usRcvBufferPos ) == 0 ) )
|
||||||
{
|
{
|
||||||
/* Save the address field. All frames are passed to the upper layed
|
/* Save the address field. All frames are passed to the upper layed
|
||||||
* and the decision if a frame is used is done there.
|
* and the decision if a frame is used is done there.
|
||||||
@@ -170,11 +170,11 @@ eMBRTUReceive( UCHAR * pucRcvAddress, UCHAR ** pucFrame, USHORT * pusLength )
|
|||||||
/* Total length of Modbus-PDU is Modbus-Serial-Line-PDU minus
|
/* Total length of Modbus-PDU is Modbus-Serial-Line-PDU minus
|
||||||
* size of address field and CRC checksum.
|
* size of address field and CRC checksum.
|
||||||
*/
|
*/
|
||||||
*pusLength = ( USHORT )( 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. */
|
/* Return the start of the Modbus PDU to the caller. */
|
||||||
*pucFrame = ( UCHAR * ) & ucRTUBuf[MB_SER_PDU_PDU_OFF];
|
*pucFrame = ( uint8_t * ) & ucRTUBuf[MB_SER_PDU_PDU_OFF];
|
||||||
xFrameReceived = TRUE;
|
xFrameReceived = true;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -186,10 +186,10 @@ eMBRTUReceive( UCHAR * pucRcvAddress, UCHAR ** pucFrame, USHORT * pusLength )
|
|||||||
}
|
}
|
||||||
|
|
||||||
eMBErrorCode
|
eMBErrorCode
|
||||||
eMBRTUSend( UCHAR ucSlaveAddress, const UCHAR * pucFrame, USHORT usLength )
|
eMBRTUSend( uint8_t ucSlaveAddress, const uint8_t * pucFrame, uint16_t usLength )
|
||||||
{
|
{
|
||||||
eMBErrorCode eStatus = MB_ENOERR;
|
eMBErrorCode eStatus = MB_ENOERR;
|
||||||
USHORT usCRC16;
|
uint16_t usCRC16;
|
||||||
|
|
||||||
ENTER_CRITICAL_SECTION( );
|
ENTER_CRITICAL_SECTION( );
|
||||||
|
|
||||||
@@ -200,7 +200,7 @@ eMBRTUSend( UCHAR ucSlaveAddress, const UCHAR * pucFrame, USHORT usLength )
|
|||||||
if( eRcvState == STATE_RX_IDLE )
|
if( eRcvState == STATE_RX_IDLE )
|
||||||
{
|
{
|
||||||
/* First byte before the Modbus-PDU is the slave address. */
|
/* First byte before the Modbus-PDU is the slave address. */
|
||||||
pucSndBufferCur = ( UCHAR * ) pucFrame - 1;
|
pucSndBufferCur = ( uint8_t * ) pucFrame - 1;
|
||||||
usSndBufferCount = 1;
|
usSndBufferCount = 1;
|
||||||
|
|
||||||
/* Now copy the Modbus-PDU into the Modbus-Serial-Line-PDU. */
|
/* Now copy the Modbus-PDU into the Modbus-Serial-Line-PDU. */
|
||||||
@@ -208,13 +208,13 @@ eMBRTUSend( UCHAR ucSlaveAddress, const UCHAR * pucFrame, USHORT usLength )
|
|||||||
usSndBufferCount += usLength;
|
usSndBufferCount += usLength;
|
||||||
|
|
||||||
/* Calculate CRC16 checksum for Modbus-Serial-Line-PDU. */
|
/* Calculate CRC16 checksum for Modbus-Serial-Line-PDU. */
|
||||||
usCRC16 = usMBCRC16( ( UCHAR * ) pucSndBufferCur, usSndBufferCount );
|
usCRC16 = usMBCRC16( ( uint8_t * ) pucSndBufferCur, usSndBufferCount );
|
||||||
ucRTUBuf[usSndBufferCount++] = ( UCHAR )( usCRC16 & 0xFF );
|
ucRTUBuf[usSndBufferCount++] = ( uint8_t )( usCRC16 & 0xFF );
|
||||||
ucRTUBuf[usSndBufferCount++] = ( UCHAR )( usCRC16 >> 8 );
|
ucRTUBuf[usSndBufferCount++] = ( uint8_t )( usCRC16 >> 8 );
|
||||||
|
|
||||||
/* Activate the transmitter. */
|
/* Activate the transmitter. */
|
||||||
eSndState = STATE_TX_XMIT;
|
eSndState = STATE_TX_XMIT;
|
||||||
vMBPortSerialEnable( FALSE, TRUE );
|
vMBPortSerialEnable( false, true );
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -224,16 +224,16 @@ eMBRTUSend( UCHAR ucSlaveAddress, const UCHAR * pucFrame, USHORT usLength )
|
|||||||
return eStatus;
|
return eStatus;
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOL
|
bool
|
||||||
xMBRTUReceiveFSM( void )
|
xMBRTUReceiveFSM( void )
|
||||||
{
|
{
|
||||||
BOOL xTaskNeedSwitch = FALSE;
|
bool xTaskNeedSwitch = false;
|
||||||
UCHAR ucByte;
|
uint8_t ucByte;
|
||||||
|
|
||||||
ASSERT( eSndState == STATE_TX_IDLE );
|
ASSERT( eSndState == STATE_TX_IDLE );
|
||||||
|
|
||||||
/* Always read the character. */
|
/* Always read the character. */
|
||||||
( void )xMBPortSerialGetByte( ( CHAR * ) & ucByte );
|
( void )xMBPortSerialGetByte( ( int8_t * ) & ucByte );
|
||||||
|
|
||||||
switch ( eRcvState )
|
switch ( eRcvState )
|
||||||
{
|
{
|
||||||
@@ -284,10 +284,10 @@ xMBRTUReceiveFSM( void )
|
|||||||
return xTaskNeedSwitch;
|
return xTaskNeedSwitch;
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOL
|
bool
|
||||||
xMBRTUTransmitFSM( void )
|
xMBRTUTransmitFSM( void )
|
||||||
{
|
{
|
||||||
BOOL xNeedPoll = FALSE;
|
bool xNeedPoll = false;
|
||||||
|
|
||||||
ASSERT( eRcvState == STATE_RX_IDLE );
|
ASSERT( eRcvState == STATE_RX_IDLE );
|
||||||
|
|
||||||
@@ -297,14 +297,14 @@ xMBRTUTransmitFSM( void )
|
|||||||
* idle state. */
|
* idle state. */
|
||||||
case STATE_TX_IDLE:
|
case STATE_TX_IDLE:
|
||||||
/* enable receiver/disable transmitter. */
|
/* enable receiver/disable transmitter. */
|
||||||
vMBPortSerialEnable( TRUE, FALSE );
|
vMBPortSerialEnable( true, false );
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case STATE_TX_XMIT:
|
case STATE_TX_XMIT:
|
||||||
/* check if we are finished. */
|
/* check if we are finished. */
|
||||||
if( usSndBufferCount != 0 )
|
if( usSndBufferCount != 0 )
|
||||||
{
|
{
|
||||||
xMBPortSerialPutByte( ( CHAR )*pucSndBufferCur );
|
xMBPortSerialPutByte( ( int8_t )*pucSndBufferCur );
|
||||||
pucSndBufferCur++; /* next byte in sendbuffer. */
|
pucSndBufferCur++; /* next byte in sendbuffer. */
|
||||||
usSndBufferCount--;
|
usSndBufferCount--;
|
||||||
}
|
}
|
||||||
@@ -313,7 +313,7 @@ xMBRTUTransmitFSM( void )
|
|||||||
xNeedPoll = xMBPortEventPost( EV_FRAME_SENT );
|
xNeedPoll = xMBPortEventPost( EV_FRAME_SENT );
|
||||||
/* Disable transmitter. This prevents another transmit buffer
|
/* Disable transmitter. This prevents another transmit buffer
|
||||||
* empty interrupt. */
|
* empty interrupt. */
|
||||||
vMBPortSerialEnable( TRUE, FALSE );
|
vMBPortSerialEnable( true, false );
|
||||||
eSndState = STATE_TX_IDLE;
|
eSndState = STATE_TX_IDLE;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@@ -322,10 +322,10 @@ xMBRTUTransmitFSM( void )
|
|||||||
return xNeedPoll;
|
return xNeedPoll;
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOL
|
bool
|
||||||
xMBRTUTimerT35Expired( void )
|
xMBRTUTimerT35Expired( void )
|
||||||
{
|
{
|
||||||
BOOL xNeedPoll = FALSE;
|
bool xNeedPoll = false;
|
||||||
|
|
||||||
switch ( eRcvState )
|
switch ( eRcvState )
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -34,16 +34,16 @@
|
|||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
PR_BEGIN_EXTERN_C
|
PR_BEGIN_EXTERN_C
|
||||||
#endif
|
#endif
|
||||||
eMBErrorCode eMBRTUInit( UCHAR slaveAddress, UCHAR ucPort, ULONG ulBaudRate,
|
eMBErrorCode eMBRTUInit( uint8_t slaveAddress, uint8_t ucPort, uint32_t ulBaudRate,
|
||||||
eMBParity eParity );
|
eMBParity eParity );
|
||||||
void eMBRTUStart( void );
|
void eMBRTUStart( void );
|
||||||
void eMBRTUStop( void );
|
void eMBRTUStop( void );
|
||||||
eMBErrorCode eMBRTUReceive( UCHAR * pucRcvAddress, UCHAR ** pucFrame, USHORT * pusLength );
|
eMBErrorCode eMBRTUReceive( uint8_t * pucRcvAddress, uint8_t ** pucFrame, uint16_t * pusLength );
|
||||||
eMBErrorCode eMBRTUSend( UCHAR slaveAddress, const UCHAR * pucFrame, USHORT usLength );
|
eMBErrorCode eMBRTUSend( uint8_t slaveAddress, const uint8_t * pucFrame, uint16_t usLength );
|
||||||
BOOL xMBRTUReceiveFSM( void );
|
bool xMBRTUReceiveFSM( void );
|
||||||
BOOL xMBRTUTransmitFSM( void );
|
bool xMBRTUTransmitFSM( void );
|
||||||
BOOL xMBRTUTimerT15Expired( void );
|
bool xMBRTUTimerT15Expired( void );
|
||||||
BOOL xMBRTUTimerT35Expired( void );
|
bool xMBRTUTimerT35Expired( void );
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
PR_END_EXTERN_C
|
PR_END_EXTERN_C
|
||||||
|
|||||||
+11
-11
@@ -79,11 +79,11 @@
|
|||||||
|
|
||||||
/* ----------------------- Start implementation -----------------------------*/
|
/* ----------------------- Start implementation -----------------------------*/
|
||||||
eMBErrorCode
|
eMBErrorCode
|
||||||
eMBTCPDoInit( USHORT ucTCPPort )
|
eMBTCPDoInit( uint16_t ucTCPPort )
|
||||||
{
|
{
|
||||||
eMBErrorCode eStatus = MB_ENOERR;
|
eMBErrorCode eStatus = MB_ENOERR;
|
||||||
|
|
||||||
if( xMBTCPPortInit( ucTCPPort ) == FALSE )
|
if( xMBTCPPortInit( ucTCPPort ) == false )
|
||||||
{
|
{
|
||||||
eStatus = MB_EPORTERR;
|
eStatus = MB_EPORTERR;
|
||||||
}
|
}
|
||||||
@@ -103,14 +103,14 @@ eMBTCPStop( void )
|
|||||||
}
|
}
|
||||||
|
|
||||||
eMBErrorCode
|
eMBErrorCode
|
||||||
eMBTCPReceive( UCHAR * pucRcvAddress, UCHAR ** ppucFrame, USHORT * pusLength )
|
eMBTCPReceive( uint8_t * pucRcvAddress, uint8_t ** ppucFrame, uint16_t * pusLength )
|
||||||
{
|
{
|
||||||
eMBErrorCode eStatus = MB_EIO;
|
eMBErrorCode eStatus = MB_EIO;
|
||||||
UCHAR *pucMBTCPFrame;
|
uint8_t *pucMBTCPFrame;
|
||||||
USHORT usLength;
|
uint16_t usLength;
|
||||||
USHORT usPID;
|
uint16_t usPID;
|
||||||
|
|
||||||
if( xMBTCPPortGetRequest( &pucMBTCPFrame, &usLength ) != FALSE )
|
if( xMBTCPPortGetRequest( &pucMBTCPFrame, &usLength ) != false )
|
||||||
{
|
{
|
||||||
usPID = pucMBTCPFrame[MB_TCP_PID] << 8U;
|
usPID = pucMBTCPFrame[MB_TCP_PID] << 8U;
|
||||||
usPID |= pucMBTCPFrame[MB_TCP_PID + 1];
|
usPID |= pucMBTCPFrame[MB_TCP_PID + 1];
|
||||||
@@ -135,11 +135,11 @@ eMBTCPReceive( UCHAR * pucRcvAddress, UCHAR ** ppucFrame, USHORT * pusLength )
|
|||||||
}
|
}
|
||||||
|
|
||||||
eMBErrorCode
|
eMBErrorCode
|
||||||
eMBTCPSend( UCHAR _unused, const UCHAR * pucFrame, USHORT usLength )
|
eMBTCPSend( uint8_t _unused, const uint8_t * pucFrame, uint16_t usLength )
|
||||||
{
|
{
|
||||||
eMBErrorCode eStatus = MB_ENOERR;
|
eMBErrorCode eStatus = MB_ENOERR;
|
||||||
UCHAR *pucMBTCPFrame = ( UCHAR * ) pucFrame - MB_TCP_FUNC;
|
uint8_t *pucMBTCPFrame = ( uint8_t * ) pucFrame - MB_TCP_FUNC;
|
||||||
USHORT usTCPLength = usLength + MB_TCP_FUNC;
|
uint16_t usTCPLength = usLength + MB_TCP_FUNC;
|
||||||
|
|
||||||
/* The MBAP header is already initialized because the caller calls this
|
/* The MBAP header is already initialized because the caller calls this
|
||||||
* function with the buffer returned by the previous call. Therefore we
|
* function with the buffer returned by the previous call. Therefore we
|
||||||
@@ -149,7 +149,7 @@ eMBTCPSend( UCHAR _unused, const UCHAR * pucFrame, USHORT usLength )
|
|||||||
*/
|
*/
|
||||||
pucMBTCPFrame[MB_TCP_LEN] = ( usLength + 1 ) >> 8U;
|
pucMBTCPFrame[MB_TCP_LEN] = ( usLength + 1 ) >> 8U;
|
||||||
pucMBTCPFrame[MB_TCP_LEN + 1] = ( usLength + 1 ) & 0xFF;
|
pucMBTCPFrame[MB_TCP_LEN + 1] = ( usLength + 1 ) & 0xFF;
|
||||||
if( xMBTCPPortSendResponse( pucMBTCPFrame, usTCPLength ) == FALSE )
|
if( xMBTCPPortSendResponse( pucMBTCPFrame, usTCPLength ) == false )
|
||||||
{
|
{
|
||||||
eStatus = MB_EIO;
|
eStatus = MB_EIO;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,13 +39,13 @@ PR_BEGIN_EXTERN_C
|
|||||||
#define MB_TCP_PSEUDO_ADDRESS 255
|
#define MB_TCP_PSEUDO_ADDRESS 255
|
||||||
|
|
||||||
/* ----------------------- Function prototypes ------------------------------*/
|
/* ----------------------- Function prototypes ------------------------------*/
|
||||||
eMBErrorCode eMBTCPDoInit( USHORT ucTCPPort );
|
eMBErrorCode eMBTCPDoInit( uint16_t ucTCPPort );
|
||||||
void eMBTCPStart( void );
|
void eMBTCPStart( void );
|
||||||
void eMBTCPStop( void );
|
void eMBTCPStop( void );
|
||||||
eMBErrorCode eMBTCPReceive( UCHAR * pucRcvAddress, UCHAR ** pucFrame,
|
eMBErrorCode eMBTCPReceive( uint8_t * pucRcvAddress, uint8_t ** pucFrame,
|
||||||
USHORT * pusLength );
|
uint16_t * pusLength );
|
||||||
eMBErrorCode eMBTCPSend( UCHAR _unused, const UCHAR * pucFrame,
|
eMBErrorCode eMBTCPSend( uint8_t _unused, const uint8_t * pucFrame,
|
||||||
USHORT usLength );
|
uint16_t usLength );
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
PR_END_EXTERN_C
|
PR_END_EXTERN_C
|
||||||
|
|||||||
+2
-1
@@ -3033,5 +3033,6 @@
|
|||||||
and some pin configuration. This would have caused problems for STM32 F107xx.
|
and some pin configuration. This would have caused problems for STM32 F107xx.
|
||||||
Typos noted by Mike Smith.
|
Typos noted by Mike Smith.
|
||||||
* arch/arm/src/lpc43xx/lpc43_serial.c: Add support for certain RS-485 features
|
* arch/arm/src/lpc43xx/lpc43_serial.c: Add support for certain RS-485 features
|
||||||
in the serial driver (still a work in progress on initial check-in).
|
* lib/termios/lib_cfsetispeed.c, lib_cfsetospeed.c, lib_tcflush.c: Add
|
||||||
|
simple implementations of cfsetispeed(), cfsetospeed(), and tcflush().
|
||||||
|
|
||||||
|
|||||||
@@ -514,6 +514,13 @@
|
|||||||
<li><a href="NuttxPortingGuide.html#pwrmgmt">Power management</a> sub-system.</li>
|
<li><a href="NuttxPortingGuide.html#pwrmgmt">Power management</a> sub-system.</li>
|
||||||
</p>
|
</p>
|
||||||
</tr>
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td><br></td>
|
||||||
|
<td>
|
||||||
|
<p>
|
||||||
|
<li>ModBus support provided by built-in <a href="http://freemodbus.berlios.de/">FreeModBus</a> version 1.5.0.</li>
|
||||||
|
</p>
|
||||||
|
</tr>
|
||||||
|
|
||||||
<tr>
|
<tr>
|
||||||
<td valign="top" width="22"><img height="20" width="20" src="favicon.ico"></td>
|
<td valign="top" width="22"><img height="20" width="20" src="favicon.ico"></td>
|
||||||
|
|||||||
@@ -58,6 +58,12 @@ CONFIGURED_APPS += netutils/telnetd
|
|||||||
endif
|
endif
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
# FreeModBus library
|
||||||
|
|
||||||
|
ifeq ($(CONFIG_MODBUS),y)
|
||||||
|
CONFIGURED_APPS += modbus
|
||||||
|
endif
|
||||||
|
|
||||||
# Applications configured as an NX built-in commands
|
# Applications configured as an NX built-in commands
|
||||||
|
|
||||||
ifeq ($(CONFIG_ADC),y)
|
ifeq ($(CONFIG_ADC),y)
|
||||||
@@ -80,6 +86,10 @@ ifeq ($(CONFIG_WATCHDOG),y)
|
|||||||
CONFIGURED_APPS += examples/watchdog
|
CONFIGURED_APPS += examples/watchdog
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
ifeq ($(CONFIG_MODBUS),y)
|
||||||
|
CONFIGURED_APPS += examples/modbus
|
||||||
|
endif
|
||||||
|
|
||||||
# Uncomment examples/ftpc to include the FTP client example
|
# Uncomment examples/ftpc to include the FTP client example
|
||||||
# Uncomment examples/ftpd to include the FTP daemon example
|
# Uncomment examples/ftpd to include the FTP daemon example
|
||||||
|
|
||||||
|
|||||||
@@ -492,7 +492,7 @@ CONFIG_DISABLE_SIGNALS=n
|
|||||||
CONFIG_DISABLE_MQUEUE=n
|
CONFIG_DISABLE_MQUEUE=n
|
||||||
CONFIG_DISABLE_MOUNTPOINT=n
|
CONFIG_DISABLE_MOUNTPOINT=n
|
||||||
CONFIG_DISABLE_ENVIRON=n
|
CONFIG_DISABLE_ENVIRON=n
|
||||||
CONFIG_DISABLE_POLL=y
|
CONFIG_DISABLE_POLL=n
|
||||||
|
|
||||||
#
|
#
|
||||||
# Misc libc settings
|
# Misc libc settings
|
||||||
|
|||||||
@@ -110,6 +110,10 @@
|
|||||||
#define HUPCL (1 << 6) /* Hang up on last close */
|
#define HUPCL (1 << 6) /* Hang up on last close */
|
||||||
#define CLOCAL (1 << 7) /* Ignore modem status lines */
|
#define CLOCAL (1 << 7) /* Ignore modem status lines */
|
||||||
|
|
||||||
|
#define CBAUD (0x1f << 8) /* Baud mask (not POSIX) */
|
||||||
|
#define CBAUDEX (0x10 << 8) /* Extra speed mask (not POSIX) */
|
||||||
|
#define _CBAUD_SHIFT 8 /* So that we all agree where the baud code is stored */
|
||||||
|
|
||||||
/* Local Modes (c_lflag in the termios structure) */
|
/* Local Modes (c_lflag in the termios structure) */
|
||||||
|
|
||||||
#define ECHO (1 << 0) /* Enable echo */
|
#define ECHO (1 << 0) /* Enable echo */
|
||||||
|
|||||||
@@ -36,5 +36,7 @@
|
|||||||
TERMIOS_SRCS =
|
TERMIOS_SRCS =
|
||||||
|
|
||||||
ifneq ($(CONFIG_NFILE_DESCRIPTORS),0)
|
ifneq ($(CONFIG_NFILE_DESCRIPTORS),0)
|
||||||
TERMIOS_SRCS += lib_tcgetattr.c lib_tcsetattr.c
|
TERMIOS_SRCS += lib_cfgetispeed.c lib_cfgetospeed.c lib_cfsetispeed.c
|
||||||
|
TERMIOS_SRCS += lib_cfsetospeed.c lib_tcflush.c lib_tcgetattr.c
|
||||||
|
TERMIOS_SRCS += lib_tcsetattr.c
|
||||||
endif
|
endif
|
||||||
|
|||||||
@@ -0,0 +1,93 @@
|
|||||||
|
/****************************************************************************
|
||||||
|
* lib/termios/lib_cfsetispeed.c
|
||||||
|
*
|
||||||
|
* Copyright (C) 2012 Gregory Nutt. All rights reserved.
|
||||||
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||||
|
*
|
||||||
|
* 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. Neither the name NuttX nor the names of its contributors may be
|
||||||
|
* used to endorse or promote products derived from this software
|
||||||
|
* without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||||
|
* "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
|
||||||
|
* COPYRIGHT OWNER OR CONTRIBUTORS 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.
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Included Files
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <termios.h>
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Pre-processor Definitions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Private Variables
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Public Variables
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Private Functions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Public Functions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: cfsetispeed
|
||||||
|
*
|
||||||
|
* Descripton:
|
||||||
|
* The cfsetispeed() function sets the input baud rate stored in the
|
||||||
|
* structure pointed to by termios_p to speed.
|
||||||
|
*
|
||||||
|
* There is no effect on the baud rates set in the hardware until a
|
||||||
|
* subsequent successful call to tcsetattr() on the same termios structure.
|
||||||
|
*
|
||||||
|
* NOTE: NuttX does not no control input/output baud rates independently
|
||||||
|
* This function is *identical* to cfsetospeed
|
||||||
|
*
|
||||||
|
* Input Parameters:
|
||||||
|
* termiosp - The termiosp argument is a pointer to a termios structure.
|
||||||
|
* speed - The new input speed
|
||||||
|
*
|
||||||
|
* Returned Value:
|
||||||
|
* Baud is not checked... OK is always returned.
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
int cfsetispeed(struct termios *termios_p, speed_t speed)
|
||||||
|
{
|
||||||
|
DEBUGASSERT(termios_p);
|
||||||
|
|
||||||
|
termios_p->c_cflag &= ~CBAUD;
|
||||||
|
termios_p->c_cflag |= ((tcflag_t)speed << _CBAUD_SHIFT);
|
||||||
|
return OK;
|
||||||
|
}
|
||||||
@@ -0,0 +1,93 @@
|
|||||||
|
/****************************************************************************
|
||||||
|
* lib/termios/lib_cfsetospeed.c
|
||||||
|
*
|
||||||
|
* Copyright (C) 2012 Gregory Nutt. All rights reserved.
|
||||||
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||||
|
*
|
||||||
|
* 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. Neither the name NuttX nor the names of its contributors may be
|
||||||
|
* used to endorse or promote products derived from this software
|
||||||
|
* without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||||
|
* "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
|
||||||
|
* COPYRIGHT OWNER OR CONTRIBUTORS 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.
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Included Files
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <termios.h>
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Pre-processor Definitions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Private Variables
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Public Variables
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Private Functions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Public Functions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: cfsetospeed
|
||||||
|
*
|
||||||
|
* Descripton:
|
||||||
|
* The cfsetospeed() function sets the output baud rate stored in the
|
||||||
|
* structure pointed to by termios_p to speed.
|
||||||
|
*
|
||||||
|
* There is no effect on the baud rates set in the hardware until a
|
||||||
|
* subsequent successful call to tcsetattr() on the same termios structure.
|
||||||
|
*
|
||||||
|
* NOTE: NuttX does not no control input/output baud rates independently
|
||||||
|
* This function is *identical* to cfsetispeed
|
||||||
|
*
|
||||||
|
* Input Parameters:
|
||||||
|
* termiosp - The termiosp argument is a pointer to a termios structure.
|
||||||
|
* speed - The new output speed
|
||||||
|
*
|
||||||
|
* Returned Value:
|
||||||
|
* Baud is not checked... OK is always returned.
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
int cfsetospeed(struct termios *termios_p, speed_t speed)
|
||||||
|
{
|
||||||
|
DEBUGASSERT(termios_p);
|
||||||
|
|
||||||
|
termios_p->c_cflag &= ~CBAUD;
|
||||||
|
termios_p->c_cflag |= ((tcflag_t)speed << _CBAUD_SHIFT);
|
||||||
|
return OK;
|
||||||
|
}
|
||||||
@@ -0,0 +1,90 @@
|
|||||||
|
/****************************************************************************
|
||||||
|
* lib/termios/lib_tcflush.c
|
||||||
|
*
|
||||||
|
* Copyright (C) 2012 Gregory Nutt. All rights reserved.
|
||||||
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||||
|
*
|
||||||
|
* 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. Neither the name NuttX nor the names of its contributors may be
|
||||||
|
* used to endorse or promote products derived from this software
|
||||||
|
* without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||||
|
* "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
|
||||||
|
* COPYRIGHT OWNER OR CONTRIBUTORS 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.
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Included Files
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include <nuttx/config.h>
|
||||||
|
|
||||||
|
#include <sys/ioctl.h>
|
||||||
|
|
||||||
|
#include <termios.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
|
#include <nuttx/serial/tioctl.h>
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Pre-processor Definitions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Private Variables
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Public Variables
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Private Functions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Public Functions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: tcflush
|
||||||
|
*
|
||||||
|
* Descripton:
|
||||||
|
* Function for flushing a terminal/serial device
|
||||||
|
*
|
||||||
|
* Input Parameters:
|
||||||
|
* fd - The 'fd' argument is an open file descriptor associated with a terminal.
|
||||||
|
* cmd - The TCFLSH ioctl argument.
|
||||||
|
*
|
||||||
|
* Returned Value:
|
||||||
|
* Upon successful completion, 0 is returned. Otherwise, -1 is returned and
|
||||||
|
* errno is set to indicate the error.
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
int tcflush(int fd, int cmd)
|
||||||
|
{
|
||||||
|
/* Not yet implemented */
|
||||||
|
|
||||||
|
return ioctl(fd, TCFLSH, (unsigned long)cmd);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user