1.original from Cam Marshall 2.use for F446RE Test 3.use the interrupt method of uart 4.not change to RTOS yet

Dependents:   RoboticArm Modbus_Gripper_Test

Committer:
stanley1228
Date:
Fri Mar 31 01:47:35 2017 +0000
Revision:
0:aefcdfe9ca2f
1.change the file in "port" folder to F446RE use

Who changed what in which revision?

UserRevisionLine numberNew contents of line
stanley1228 0:aefcdfe9ca2f 1 /*
stanley1228 0:aefcdfe9ca2f 2 * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
stanley1228 0:aefcdfe9ca2f 3 * Copyright (c) 2006 Christian Walter <wolti@sil.at>
stanley1228 0:aefcdfe9ca2f 4 * All rights reserved.
stanley1228 0:aefcdfe9ca2f 5 *
stanley1228 0:aefcdfe9ca2f 6 * Redistribution and use in source and binary forms, with or without
stanley1228 0:aefcdfe9ca2f 7 * modification, are permitted provided that the following conditions
stanley1228 0:aefcdfe9ca2f 8 * are met:
stanley1228 0:aefcdfe9ca2f 9 * 1. Redistributions of source code must retain the above copyright
stanley1228 0:aefcdfe9ca2f 10 * notice, this list of conditions and the following disclaimer.
stanley1228 0:aefcdfe9ca2f 11 * 2. Redistributions in binary form must reproduce the above copyright
stanley1228 0:aefcdfe9ca2f 12 * notice, this list of conditions and the following disclaimer in the
stanley1228 0:aefcdfe9ca2f 13 * documentation and/or other materials provided with the distribution.
stanley1228 0:aefcdfe9ca2f 14 * 3. The name of the author may not be used to endorse or promote products
stanley1228 0:aefcdfe9ca2f 15 * derived from this software without specific prior written permission.
stanley1228 0:aefcdfe9ca2f 16 *
stanley1228 0:aefcdfe9ca2f 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
stanley1228 0:aefcdfe9ca2f 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
stanley1228 0:aefcdfe9ca2f 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
stanley1228 0:aefcdfe9ca2f 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
stanley1228 0:aefcdfe9ca2f 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
stanley1228 0:aefcdfe9ca2f 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
stanley1228 0:aefcdfe9ca2f 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
stanley1228 0:aefcdfe9ca2f 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
stanley1228 0:aefcdfe9ca2f 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
stanley1228 0:aefcdfe9ca2f 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
stanley1228 0:aefcdfe9ca2f 27 *
stanley1228 0:aefcdfe9ca2f 28 * File: $Id: mb.h,v 1.17 2006/12/07 22:10:34 wolti Exp $
stanley1228 0:aefcdfe9ca2f 29 */
stanley1228 0:aefcdfe9ca2f 30
stanley1228 0:aefcdfe9ca2f 31 #ifndef _MB_H
stanley1228 0:aefcdfe9ca2f 32 #define _MB_H
stanley1228 0:aefcdfe9ca2f 33
stanley1228 0:aefcdfe9ca2f 34 #include "port.h"
stanley1228 0:aefcdfe9ca2f 35
stanley1228 0:aefcdfe9ca2f 36 #ifdef __cplusplus
stanley1228 0:aefcdfe9ca2f 37 PR_BEGIN_EXTERN_C
stanley1228 0:aefcdfe9ca2f 38 #endif
stanley1228 0:aefcdfe9ca2f 39
stanley1228 0:aefcdfe9ca2f 40 #include "mbport.h"
stanley1228 0:aefcdfe9ca2f 41 #include "mbproto.h"
stanley1228 0:aefcdfe9ca2f 42
stanley1228 0:aefcdfe9ca2f 43 /*! \defgroup modbus Modbus
stanley1228 0:aefcdfe9ca2f 44 * \code #include "mb.h" \endcode
stanley1228 0:aefcdfe9ca2f 45 *
stanley1228 0:aefcdfe9ca2f 46 * This module defines the interface for the application. It contains
stanley1228 0:aefcdfe9ca2f 47 * the basic functions and types required to use the Modbus protocol stack.
stanley1228 0:aefcdfe9ca2f 48 * A typical application will want to call eMBInit() first. If the device
stanley1228 0:aefcdfe9ca2f 49 * is ready to answer network requests it must then call eMBEnable() to activate
stanley1228 0:aefcdfe9ca2f 50 * the protocol stack. In the main loop the function eMBPoll() must be called
stanley1228 0:aefcdfe9ca2f 51 * periodically. The time interval between pooling depends on the configured
stanley1228 0:aefcdfe9ca2f 52 * Modbus timeout. If an RTOS is available a separate task should be created
stanley1228 0:aefcdfe9ca2f 53 * and the task should always call the function eMBPoll().
stanley1228 0:aefcdfe9ca2f 54 *
stanley1228 0:aefcdfe9ca2f 55 * \code
stanley1228 0:aefcdfe9ca2f 56 * // Initialize protocol stack in RTU mode for a slave with address 10 = 0x0A
stanley1228 0:aefcdfe9ca2f 57 * eMBInit( MB_RTU, 0x0A, 38400, MB_PAR_EVEN );
stanley1228 0:aefcdfe9ca2f 58 * // Enable the Modbus Protocol Stack.
stanley1228 0:aefcdfe9ca2f 59 * eMBEnable( );
stanley1228 0:aefcdfe9ca2f 60 * for( ;; )
stanley1228 0:aefcdfe9ca2f 61 * {
stanley1228 0:aefcdfe9ca2f 62 * // Call the main polling loop of the Modbus protocol stack.
stanley1228 0:aefcdfe9ca2f 63 * eMBPoll( );
stanley1228 0:aefcdfe9ca2f 64 * ...
stanley1228 0:aefcdfe9ca2f 65 * }
stanley1228 0:aefcdfe9ca2f 66 * \endcode
stanley1228 0:aefcdfe9ca2f 67 */
stanley1228 0:aefcdfe9ca2f 68
stanley1228 0:aefcdfe9ca2f 69 /* ----------------------- Defines ------------------------------------------*/
stanley1228 0:aefcdfe9ca2f 70
stanley1228 0:aefcdfe9ca2f 71 /*! \ingroup modbus
stanley1228 0:aefcdfe9ca2f 72 * \brief Use the default Modbus TCP port (502)
stanley1228 0:aefcdfe9ca2f 73 */
stanley1228 0:aefcdfe9ca2f 74 #define MB_TCP_PORT_USE_DEFAULT 0
stanley1228 0:aefcdfe9ca2f 75
stanley1228 0:aefcdfe9ca2f 76 /* ----------------------- Type definitions ---------------------------------*/
stanley1228 0:aefcdfe9ca2f 77
stanley1228 0:aefcdfe9ca2f 78 /*! \ingroup modbus
stanley1228 0:aefcdfe9ca2f 79 * \brief Modbus serial transmission modes (RTU/ASCII).
stanley1228 0:aefcdfe9ca2f 80 *
stanley1228 0:aefcdfe9ca2f 81 * Modbus serial supports two transmission modes. Either ASCII or RTU. RTU
stanley1228 0:aefcdfe9ca2f 82 * is faster but has more hardware requirements and requires a network with
stanley1228 0:aefcdfe9ca2f 83 * a low jitter. ASCII is slower and more reliable on slower links (E.g. modems)
stanley1228 0:aefcdfe9ca2f 84 */
stanley1228 0:aefcdfe9ca2f 85 typedef enum
stanley1228 0:aefcdfe9ca2f 86 {
stanley1228 0:aefcdfe9ca2f 87 MB_RTU, /*!< RTU transmission mode. */
stanley1228 0:aefcdfe9ca2f 88 MB_ASCII, /*!< ASCII transmission mode. */
stanley1228 0:aefcdfe9ca2f 89 MB_TCP /*!< TCP mode. */
stanley1228 0:aefcdfe9ca2f 90 } eMBMode;
stanley1228 0:aefcdfe9ca2f 91
stanley1228 0:aefcdfe9ca2f 92 /*! \ingroup modbus
stanley1228 0:aefcdfe9ca2f 93 * \brief If register should be written or read.
stanley1228 0:aefcdfe9ca2f 94 *
stanley1228 0:aefcdfe9ca2f 95 * This value is passed to the callback functions which support either
stanley1228 0:aefcdfe9ca2f 96 * reading or writing register values. Writing means that the application
stanley1228 0:aefcdfe9ca2f 97 * registers should be updated and reading means that the modbus protocol
stanley1228 0:aefcdfe9ca2f 98 * stack needs to know the current register values.
stanley1228 0:aefcdfe9ca2f 99 *
stanley1228 0:aefcdfe9ca2f 100 * \see eMBRegHoldingCB( ), eMBRegCoilsCB( ), eMBRegDiscreteCB( ) and
stanley1228 0:aefcdfe9ca2f 101 * eMBRegInputCB( ).
stanley1228 0:aefcdfe9ca2f 102 */
stanley1228 0:aefcdfe9ca2f 103 typedef enum
stanley1228 0:aefcdfe9ca2f 104 {
stanley1228 0:aefcdfe9ca2f 105 MB_REG_READ, /*!< Read register values and pass to protocol stack. */
stanley1228 0:aefcdfe9ca2f 106 MB_REG_WRITE /*!< Update register values. */
stanley1228 0:aefcdfe9ca2f 107 } eMBRegisterMode;
stanley1228 0:aefcdfe9ca2f 108
stanley1228 0:aefcdfe9ca2f 109 /*! \ingroup modbus
stanley1228 0:aefcdfe9ca2f 110 * \brief Errorcodes used by all function in the protocol stack.
stanley1228 0:aefcdfe9ca2f 111 */
stanley1228 0:aefcdfe9ca2f 112 typedef enum
stanley1228 0:aefcdfe9ca2f 113 {
stanley1228 0:aefcdfe9ca2f 114 MB_ENOERR, /*!< no error. */
stanley1228 0:aefcdfe9ca2f 115 MB_ENOREG, /*!< illegal register address. */
stanley1228 0:aefcdfe9ca2f 116 MB_EINVAL, /*!< illegal argument. */
stanley1228 0:aefcdfe9ca2f 117 MB_EPORTERR, /*!< porting layer error. */
stanley1228 0:aefcdfe9ca2f 118 MB_ENORES, /*!< insufficient resources. */
stanley1228 0:aefcdfe9ca2f 119 MB_EIO, /*!< I/O error. */
stanley1228 0:aefcdfe9ca2f 120 MB_EILLSTATE, /*!< protocol stack in illegal state. */
stanley1228 0:aefcdfe9ca2f 121 MB_ETIMEDOUT /*!< timeout error occurred. */
stanley1228 0:aefcdfe9ca2f 122 } eMBErrorCode;
stanley1228 0:aefcdfe9ca2f 123
stanley1228 0:aefcdfe9ca2f 124
stanley1228 0:aefcdfe9ca2f 125 /* ----------------------- Function prototypes ------------------------------*/
stanley1228 0:aefcdfe9ca2f 126 /*! \ingroup modbus
stanley1228 0:aefcdfe9ca2f 127 * \brief Initialize the Modbus protocol stack.
stanley1228 0:aefcdfe9ca2f 128 *
stanley1228 0:aefcdfe9ca2f 129 * This functions initializes the ASCII or RTU module and calls the
stanley1228 0:aefcdfe9ca2f 130 * init functions of the porting layer to prepare the hardware. Please
stanley1228 0:aefcdfe9ca2f 131 * note that the receiver is still disabled and no Modbus frames are
stanley1228 0:aefcdfe9ca2f 132 * processed until eMBEnable( ) has been called.
stanley1228 0:aefcdfe9ca2f 133 *
stanley1228 0:aefcdfe9ca2f 134 * \param eMode If ASCII or RTU mode should be used.
stanley1228 0:aefcdfe9ca2f 135 * \param ucSlaveAddress The slave address. Only frames sent to this
stanley1228 0:aefcdfe9ca2f 136 * address or to the broadcast address are processed.
stanley1228 0:aefcdfe9ca2f 137 * \param ucPort The port to use. E.g. 1 for COM1 on windows. This value
stanley1228 0:aefcdfe9ca2f 138 * is platform dependent and some ports simply choose to ignore it.
stanley1228 0:aefcdfe9ca2f 139 * \param ulBaudRate The baudrate. E.g. 19200. Supported baudrates depend
stanley1228 0:aefcdfe9ca2f 140 * on the porting layer.
stanley1228 0:aefcdfe9ca2f 141 * \param eParity Parity used for serial transmission.
stanley1228 0:aefcdfe9ca2f 142 *
stanley1228 0:aefcdfe9ca2f 143 * \return If no error occurs the function returns eMBErrorCode::MB_ENOERR.
stanley1228 0:aefcdfe9ca2f 144 * The protocol is then in the disabled state and ready for activation
stanley1228 0:aefcdfe9ca2f 145 * by calling eMBEnable( ). Otherwise one of the following error codes
stanley1228 0:aefcdfe9ca2f 146 * is returned:
stanley1228 0:aefcdfe9ca2f 147 * - eMBErrorCode::MB_EINVAL If the slave address was not valid. Valid
stanley1228 0:aefcdfe9ca2f 148 * slave addresses are in the range 1 - 247.
stanley1228 0:aefcdfe9ca2f 149 * - eMBErrorCode::MB_EPORTERR IF the porting layer returned an error.
stanley1228 0:aefcdfe9ca2f 150 */
stanley1228 0:aefcdfe9ca2f 151 eMBErrorCode eMBInit( eMBMode eMode, UCHAR ucSlaveAddress,
stanley1228 0:aefcdfe9ca2f 152 UCHAR ucPort, ULONG ulBaudRate, eMBParity eParity );
stanley1228 0:aefcdfe9ca2f 153
stanley1228 0:aefcdfe9ca2f 154 /*! \ingroup modbus
stanley1228 0:aefcdfe9ca2f 155 * \brief Initialize the Modbus protocol stack for Modbus TCP.
stanley1228 0:aefcdfe9ca2f 156 *
stanley1228 0:aefcdfe9ca2f 157 * This function initializes the Modbus TCP Module. Please note that
stanley1228 0:aefcdfe9ca2f 158 * frame processing is still disabled until eMBEnable( ) is called.
stanley1228 0:aefcdfe9ca2f 159 *
stanley1228 0:aefcdfe9ca2f 160 * \param usTCPPort The TCP port to listen on.
stanley1228 0:aefcdfe9ca2f 161 * \return If the protocol stack has been initialized correctly the function
stanley1228 0:aefcdfe9ca2f 162 * returns eMBErrorCode::MB_ENOERR. Otherwise one of the following error
stanley1228 0:aefcdfe9ca2f 163 * codes is returned:
stanley1228 0:aefcdfe9ca2f 164 * - eMBErrorCode::MB_EINVAL If the slave address was not valid. Valid
stanley1228 0:aefcdfe9ca2f 165 * slave addresses are in the range 1 - 247.
stanley1228 0:aefcdfe9ca2f 166 * - eMBErrorCode::MB_EPORTERR IF the porting layer returned an error.
stanley1228 0:aefcdfe9ca2f 167 */
stanley1228 0:aefcdfe9ca2f 168 eMBErrorCode eMBTCPInit( USHORT usTCPPort );
stanley1228 0:aefcdfe9ca2f 169
stanley1228 0:aefcdfe9ca2f 170 /*! \ingroup modbus
stanley1228 0:aefcdfe9ca2f 171 * \brief Release resources used by the protocol stack.
stanley1228 0:aefcdfe9ca2f 172 *
stanley1228 0:aefcdfe9ca2f 173 * This function disables the Modbus protocol stack and release all
stanley1228 0:aefcdfe9ca2f 174 * hardware resources. It must only be called when the protocol stack
stanley1228 0:aefcdfe9ca2f 175 * is disabled.
stanley1228 0:aefcdfe9ca2f 176 *
stanley1228 0:aefcdfe9ca2f 177 * \note Note all ports implement this function. A port which wants to
stanley1228 0:aefcdfe9ca2f 178 * get an callback must define the macro MB_PORT_HAS_CLOSE to 1.
stanley1228 0:aefcdfe9ca2f 179 *
stanley1228 0:aefcdfe9ca2f 180 * \return If the resources where released it return eMBErrorCode::MB_ENOERR.
stanley1228 0:aefcdfe9ca2f 181 * If the protocol stack is not in the disabled state it returns
stanley1228 0:aefcdfe9ca2f 182 * eMBErrorCode::MB_EILLSTATE.
stanley1228 0:aefcdfe9ca2f 183 */
stanley1228 0:aefcdfe9ca2f 184 eMBErrorCode eMBClose( void );
stanley1228 0:aefcdfe9ca2f 185
stanley1228 0:aefcdfe9ca2f 186 /*! \ingroup modbus
stanley1228 0:aefcdfe9ca2f 187 * \brief Enable the Modbus protocol stack.
stanley1228 0:aefcdfe9ca2f 188 *
stanley1228 0:aefcdfe9ca2f 189 * This function enables processing of Modbus frames. Enabling the protocol
stanley1228 0:aefcdfe9ca2f 190 * stack is only possible if it is in the disabled state.
stanley1228 0:aefcdfe9ca2f 191 *
stanley1228 0:aefcdfe9ca2f 192 * \return If the protocol stack is now in the state enabled it returns
stanley1228 0:aefcdfe9ca2f 193 * eMBErrorCode::MB_ENOERR. If it was not in the disabled state it
stanley1228 0:aefcdfe9ca2f 194 * return eMBErrorCode::MB_EILLSTATE.
stanley1228 0:aefcdfe9ca2f 195 */
stanley1228 0:aefcdfe9ca2f 196 eMBErrorCode eMBEnable( void );
stanley1228 0:aefcdfe9ca2f 197
stanley1228 0:aefcdfe9ca2f 198 /*! \ingroup modbus
stanley1228 0:aefcdfe9ca2f 199 * \brief Disable the Modbus protocol stack.
stanley1228 0:aefcdfe9ca2f 200 *
stanley1228 0:aefcdfe9ca2f 201 * This function disables processing of Modbus frames.
stanley1228 0:aefcdfe9ca2f 202 *
stanley1228 0:aefcdfe9ca2f 203 * \return If the protocol stack has been disabled it returns
stanley1228 0:aefcdfe9ca2f 204 * eMBErrorCode::MB_ENOERR. If it was not in the enabled state it returns
stanley1228 0:aefcdfe9ca2f 205 * eMBErrorCode::MB_EILLSTATE.
stanley1228 0:aefcdfe9ca2f 206 */
stanley1228 0:aefcdfe9ca2f 207 eMBErrorCode eMBDisable( void );
stanley1228 0:aefcdfe9ca2f 208
stanley1228 0:aefcdfe9ca2f 209 /*! \ingroup modbus
stanley1228 0:aefcdfe9ca2f 210 * \brief The main pooling loop of the Modbus protocol stack.
stanley1228 0:aefcdfe9ca2f 211 *
stanley1228 0:aefcdfe9ca2f 212 * This function must be called periodically. The timer interval required
stanley1228 0:aefcdfe9ca2f 213 * is given by the application dependent Modbus slave timeout. Internally the
stanley1228 0:aefcdfe9ca2f 214 * function calls xMBPortEventGet() and waits for an event from the receiver or
stanley1228 0:aefcdfe9ca2f 215 * transmitter state machines.
stanley1228 0:aefcdfe9ca2f 216 *
stanley1228 0:aefcdfe9ca2f 217 * \return If the protocol stack is not in the enabled state the function
stanley1228 0:aefcdfe9ca2f 218 * returns eMBErrorCode::MB_EILLSTATE. Otherwise it returns
stanley1228 0:aefcdfe9ca2f 219 * eMBErrorCode::MB_ENOERR.
stanley1228 0:aefcdfe9ca2f 220 */
stanley1228 0:aefcdfe9ca2f 221 eMBErrorCode eMBPoll( void );
stanley1228 0:aefcdfe9ca2f 222
stanley1228 0:aefcdfe9ca2f 223 /*! \ingroup modbus
stanley1228 0:aefcdfe9ca2f 224 * \brief Configure the slave id of the device.
stanley1228 0:aefcdfe9ca2f 225 *
stanley1228 0:aefcdfe9ca2f 226 * This function should be called when the Modbus function <em>Report Slave ID</em>
stanley1228 0:aefcdfe9ca2f 227 * is enabled ( By defining MB_FUNC_OTHER_REP_SLAVEID_ENABLED in mbconfig.h ).
stanley1228 0:aefcdfe9ca2f 228 *
stanley1228 0:aefcdfe9ca2f 229 * \param ucSlaveID Values is returned in the <em>Slave ID</em> byte of the
stanley1228 0:aefcdfe9ca2f 230 * <em>Report Slave ID</em> response.
stanley1228 0:aefcdfe9ca2f 231 * \param xIsRunning If TRUE the <em>Run Indicator Status</em> byte is set to 0xFF.
stanley1228 0:aefcdfe9ca2f 232 * otherwise the <em>Run Indicator Status</em> is 0x00.
stanley1228 0:aefcdfe9ca2f 233 * \param pucAdditional Values which should be returned in the <em>Additional</em>
stanley1228 0:aefcdfe9ca2f 234 * bytes of the <em> Report Slave ID</em> response.
stanley1228 0:aefcdfe9ca2f 235 * \param usAdditionalLen Length of the buffer <code>pucAdditonal</code>.
stanley1228 0:aefcdfe9ca2f 236 *
stanley1228 0:aefcdfe9ca2f 237 * \return If the static buffer defined by MB_FUNC_OTHER_REP_SLAVEID_BUF in
stanley1228 0:aefcdfe9ca2f 238 * mbconfig.h is to small it returns eMBErrorCode::MB_ENORES. Otherwise
stanley1228 0:aefcdfe9ca2f 239 * it returns eMBErrorCode::MB_ENOERR.
stanley1228 0:aefcdfe9ca2f 240 */
stanley1228 0:aefcdfe9ca2f 241 eMBErrorCode eMBSetSlaveID( UCHAR ucSlaveID, BOOL xIsRunning,
stanley1228 0:aefcdfe9ca2f 242 UCHAR const *pucAdditional,
stanley1228 0:aefcdfe9ca2f 243 USHORT usAdditionalLen );
stanley1228 0:aefcdfe9ca2f 244
stanley1228 0:aefcdfe9ca2f 245 /*! \ingroup modbus
stanley1228 0:aefcdfe9ca2f 246 * \brief Registers a callback handler for a given function code.
stanley1228 0:aefcdfe9ca2f 247 *
stanley1228 0:aefcdfe9ca2f 248 * This function registers a new callback handler for a given function code.
stanley1228 0:aefcdfe9ca2f 249 * The callback handler supplied is responsible for interpreting the Modbus PDU and
stanley1228 0:aefcdfe9ca2f 250 * the creation of an appropriate response. In case of an error it should return
stanley1228 0:aefcdfe9ca2f 251 * one of the possible Modbus exceptions which results in a Modbus exception frame
stanley1228 0:aefcdfe9ca2f 252 * sent by the protocol stack.
stanley1228 0:aefcdfe9ca2f 253 *
stanley1228 0:aefcdfe9ca2f 254 * \param ucFunctionCode The Modbus function code for which this handler should
stanley1228 0:aefcdfe9ca2f 255 * be registers. Valid function codes are in the range 1 to 127.
stanley1228 0:aefcdfe9ca2f 256 * \param pxHandler The function handler which should be called in case
stanley1228 0:aefcdfe9ca2f 257 * such a frame is received. If \c NULL a previously registered function handler
stanley1228 0:aefcdfe9ca2f 258 * for this function code is removed.
stanley1228 0:aefcdfe9ca2f 259 *
stanley1228 0:aefcdfe9ca2f 260 * \return eMBErrorCode::MB_ENOERR if the handler has been installed. If no
stanley1228 0:aefcdfe9ca2f 261 * more resources are available it returns eMBErrorCode::MB_ENORES. In this
stanley1228 0:aefcdfe9ca2f 262 * case the values in mbconfig.h should be adjusted. If the argument was not
stanley1228 0:aefcdfe9ca2f 263 * valid it returns eMBErrorCode::MB_EINVAL.
stanley1228 0:aefcdfe9ca2f 264 */
stanley1228 0:aefcdfe9ca2f 265 eMBErrorCode eMBRegisterCB( UCHAR ucFunctionCode,
stanley1228 0:aefcdfe9ca2f 266 pxMBFunctionHandler pxHandler );
stanley1228 0:aefcdfe9ca2f 267
stanley1228 0:aefcdfe9ca2f 268 /* ----------------------- Callback -----------------------------------------*/
stanley1228 0:aefcdfe9ca2f 269
stanley1228 0:aefcdfe9ca2f 270 /*! \defgroup modbus_registers Modbus Registers
stanley1228 0:aefcdfe9ca2f 271 * \code #include "mb.h" \endcode
stanley1228 0:aefcdfe9ca2f 272 * The protocol stack does not internally allocate any memory for the
stanley1228 0:aefcdfe9ca2f 273 * registers. This makes the protocol stack very small and also usable on
stanley1228 0:aefcdfe9ca2f 274 * low end targets. In addition the values don't have to be in the memory
stanley1228 0:aefcdfe9ca2f 275 * and could for example be stored in a flash.<br>
stanley1228 0:aefcdfe9ca2f 276 * Whenever the protocol stack requires a value it calls one of the callback
stanley1228 0:aefcdfe9ca2f 277 * function with the register address and the number of registers to read
stanley1228 0:aefcdfe9ca2f 278 * as an argument. The application should then read the actual register values
stanley1228 0:aefcdfe9ca2f 279 * (for example the ADC voltage) and should store the result in the supplied
stanley1228 0:aefcdfe9ca2f 280 * buffer.<br>
stanley1228 0:aefcdfe9ca2f 281 * If the protocol stack wants to update a register value because a write
stanley1228 0:aefcdfe9ca2f 282 * register function was received a buffer with the new register values is
stanley1228 0:aefcdfe9ca2f 283 * passed to the callback function. The function should then use these values
stanley1228 0:aefcdfe9ca2f 284 * to update the application register values.
stanley1228 0:aefcdfe9ca2f 285 */
stanley1228 0:aefcdfe9ca2f 286
stanley1228 0:aefcdfe9ca2f 287 /*! \ingroup modbus_registers
stanley1228 0:aefcdfe9ca2f 288 * \brief Callback function used if the value of a <em>Input Register</em>
stanley1228 0:aefcdfe9ca2f 289 * is required by the protocol stack. The starting register address is given
stanley1228 0:aefcdfe9ca2f 290 * by \c usAddress and the last register is given by <tt>usAddress +
stanley1228 0:aefcdfe9ca2f 291 * usNRegs - 1</tt>.
stanley1228 0:aefcdfe9ca2f 292 *
stanley1228 0:aefcdfe9ca2f 293 * \param pucRegBuffer A buffer where the callback function should write
stanley1228 0:aefcdfe9ca2f 294 * the current value of the modbus registers to.
stanley1228 0:aefcdfe9ca2f 295 * \param usAddress The starting address of the register. Input registers
stanley1228 0:aefcdfe9ca2f 296 * are in the range 1 - 65535.
stanley1228 0:aefcdfe9ca2f 297 * \param usNRegs Number of registers the callback function must supply.
stanley1228 0:aefcdfe9ca2f 298 *
stanley1228 0:aefcdfe9ca2f 299 * \return The function must return one of the following error codes:
stanley1228 0:aefcdfe9ca2f 300 * - eMBErrorCode::MB_ENOERR If no error occurred. In this case a normal
stanley1228 0:aefcdfe9ca2f 301 * Modbus response is sent.
stanley1228 0:aefcdfe9ca2f 302 * - eMBErrorCode::MB_ENOREG If the application can not supply values
stanley1228 0:aefcdfe9ca2f 303 * for registers within this range. In this case a
stanley1228 0:aefcdfe9ca2f 304 * <b>ILLEGAL DATA ADDRESS</b> exception frame is sent as a response.
stanley1228 0:aefcdfe9ca2f 305 * - eMBErrorCode::MB_ETIMEDOUT If the requested register block is
stanley1228 0:aefcdfe9ca2f 306 * currently not available and the application dependent response
stanley1228 0:aefcdfe9ca2f 307 * timeout would be violated. In this case a <b>SLAVE DEVICE BUSY</b>
stanley1228 0:aefcdfe9ca2f 308 * exception is sent as a response.
stanley1228 0:aefcdfe9ca2f 309 * - eMBErrorCode::MB_EIO If an unrecoverable error occurred. In this case
stanley1228 0:aefcdfe9ca2f 310 * a <b>SLAVE DEVICE FAILURE</b> exception is sent as a response.
stanley1228 0:aefcdfe9ca2f 311 */
stanley1228 0:aefcdfe9ca2f 312 eMBErrorCode eMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress,
stanley1228 0:aefcdfe9ca2f 313 USHORT usNRegs );
stanley1228 0:aefcdfe9ca2f 314
stanley1228 0:aefcdfe9ca2f 315 /*! \ingroup modbus_registers
stanley1228 0:aefcdfe9ca2f 316 * \brief Callback function used if a <em>Holding Register</em> value is
stanley1228 0:aefcdfe9ca2f 317 * read or written by the protocol stack. The starting register address
stanley1228 0:aefcdfe9ca2f 318 * is given by \c usAddress and the last register is given by
stanley1228 0:aefcdfe9ca2f 319 * <tt>usAddress + usNRegs - 1</tt>.
stanley1228 0:aefcdfe9ca2f 320 *
stanley1228 0:aefcdfe9ca2f 321 * \param pucRegBuffer If the application registers values should be updated the
stanley1228 0:aefcdfe9ca2f 322 * buffer points to the new registers values. If the protocol stack needs
stanley1228 0:aefcdfe9ca2f 323 * to now the current values the callback function should write them into
stanley1228 0:aefcdfe9ca2f 324 * this buffer.
stanley1228 0:aefcdfe9ca2f 325 * \param usAddress The starting address of the register.
stanley1228 0:aefcdfe9ca2f 326 * \param usNRegs Number of registers to read or write.
stanley1228 0:aefcdfe9ca2f 327 * \param eMode If eMBRegisterMode::MB_REG_WRITE the application register
stanley1228 0:aefcdfe9ca2f 328 * values should be updated from the values in the buffer. For example
stanley1228 0:aefcdfe9ca2f 329 * this would be the case when the Modbus master has issued an
stanley1228 0:aefcdfe9ca2f 330 * <b>WRITE SINGLE REGISTER</b> command.
stanley1228 0:aefcdfe9ca2f 331 * If the value eMBRegisterMode::MB_REG_READ the application should copy
stanley1228 0:aefcdfe9ca2f 332 * the current values into the buffer \c pucRegBuffer.
stanley1228 0:aefcdfe9ca2f 333 *
stanley1228 0:aefcdfe9ca2f 334 * \return The function must return one of the following error codes:
stanley1228 0:aefcdfe9ca2f 335 * - eMBErrorCode::MB_ENOERR If no error occurred. In this case a normal
stanley1228 0:aefcdfe9ca2f 336 * Modbus response is sent.
stanley1228 0:aefcdfe9ca2f 337 * - eMBErrorCode::MB_ENOREG If the application can not supply values
stanley1228 0:aefcdfe9ca2f 338 * for registers within this range. In this case a
stanley1228 0:aefcdfe9ca2f 339 * <b>ILLEGAL DATA ADDRESS</b> exception frame is sent as a response.
stanley1228 0:aefcdfe9ca2f 340 * - eMBErrorCode::MB_ETIMEDOUT If the requested register block is
stanley1228 0:aefcdfe9ca2f 341 * currently not available and the application dependent response
stanley1228 0:aefcdfe9ca2f 342 * timeout would be violated. In this case a <b>SLAVE DEVICE BUSY</b>
stanley1228 0:aefcdfe9ca2f 343 * exception is sent as a response.
stanley1228 0:aefcdfe9ca2f 344 * - eMBErrorCode::MB_EIO If an unrecoverable error occurred. In this case
stanley1228 0:aefcdfe9ca2f 345 * a <b>SLAVE DEVICE FAILURE</b> exception is sent as a response.
stanley1228 0:aefcdfe9ca2f 346 */
stanley1228 0:aefcdfe9ca2f 347 eMBErrorCode eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress,
stanley1228 0:aefcdfe9ca2f 348 USHORT usNRegs, eMBRegisterMode eMode );
stanley1228 0:aefcdfe9ca2f 349
stanley1228 0:aefcdfe9ca2f 350 /*! \ingroup modbus_registers
stanley1228 0:aefcdfe9ca2f 351 * \brief Callback function used if a <em>Coil Register</em> value is
stanley1228 0:aefcdfe9ca2f 352 * read or written by the protocol stack. If you are going to use
stanley1228 0:aefcdfe9ca2f 353 * this function you might use the functions xMBUtilSetBits( ) and
stanley1228 0:aefcdfe9ca2f 354 * xMBUtilGetBits( ) for working with bitfields.
stanley1228 0:aefcdfe9ca2f 355 *
stanley1228 0:aefcdfe9ca2f 356 * \param pucRegBuffer The bits are packed in bytes where the first coil
stanley1228 0:aefcdfe9ca2f 357 * starting at address \c usAddress is stored in the LSB of the
stanley1228 0:aefcdfe9ca2f 358 * first byte in the buffer <code>pucRegBuffer</code>.
stanley1228 0:aefcdfe9ca2f 359 * If the buffer should be written by the callback function unused
stanley1228 0:aefcdfe9ca2f 360 * coil values (I.e. if not a multiple of eight coils is used) should be set
stanley1228 0:aefcdfe9ca2f 361 * to zero.
stanley1228 0:aefcdfe9ca2f 362 * \param usAddress The first coil number.
stanley1228 0:aefcdfe9ca2f 363 * \param usNCoils Number of coil values requested.
stanley1228 0:aefcdfe9ca2f 364 * \param eMode If eMBRegisterMode::MB_REG_WRITE the application values should
stanley1228 0:aefcdfe9ca2f 365 * be updated from the values supplied in the buffer \c pucRegBuffer.
stanley1228 0:aefcdfe9ca2f 366 * If eMBRegisterMode::MB_REG_READ the application should store the current
stanley1228 0:aefcdfe9ca2f 367 * values in the buffer \c pucRegBuffer.
stanley1228 0:aefcdfe9ca2f 368 *
stanley1228 0:aefcdfe9ca2f 369 * \return The function must return one of the following error codes:
stanley1228 0:aefcdfe9ca2f 370 * - eMBErrorCode::MB_ENOERR If no error occurred. In this case a normal
stanley1228 0:aefcdfe9ca2f 371 * Modbus response is sent.
stanley1228 0:aefcdfe9ca2f 372 * - eMBErrorCode::MB_ENOREG If the application does not map an coils
stanley1228 0:aefcdfe9ca2f 373 * within the requested address range. In this case a
stanley1228 0:aefcdfe9ca2f 374 * <b>ILLEGAL DATA ADDRESS</b> is sent as a response.
stanley1228 0:aefcdfe9ca2f 375 * - eMBErrorCode::MB_ETIMEDOUT If the requested register block is
stanley1228 0:aefcdfe9ca2f 376 * currently not available and the application dependent response
stanley1228 0:aefcdfe9ca2f 377 * timeout would be violated. In this case a <b>SLAVE DEVICE BUSY</b>
stanley1228 0:aefcdfe9ca2f 378 * exception is sent as a response.
stanley1228 0:aefcdfe9ca2f 379 * - eMBErrorCode::MB_EIO If an unrecoverable error occurred. In this case
stanley1228 0:aefcdfe9ca2f 380 * a <b>SLAVE DEVICE FAILURE</b> exception is sent as a response.
stanley1228 0:aefcdfe9ca2f 381 */
stanley1228 0:aefcdfe9ca2f 382 eMBErrorCode eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress,
stanley1228 0:aefcdfe9ca2f 383 USHORT usNCoils, eMBRegisterMode eMode );
stanley1228 0:aefcdfe9ca2f 384
stanley1228 0:aefcdfe9ca2f 385 /*! \ingroup modbus_registers
stanley1228 0:aefcdfe9ca2f 386 * \brief Callback function used if a <em>Input Discrete Register</em> value is
stanley1228 0:aefcdfe9ca2f 387 * read by the protocol stack.
stanley1228 0:aefcdfe9ca2f 388 *
stanley1228 0:aefcdfe9ca2f 389 * If you are going to use his function you might use the functions
stanley1228 0:aefcdfe9ca2f 390 * xMBUtilSetBits( ) and xMBUtilGetBits( ) for working with bitfields.
stanley1228 0:aefcdfe9ca2f 391 *
stanley1228 0:aefcdfe9ca2f 392 * \param pucRegBuffer The buffer should be updated with the current
stanley1228 0:aefcdfe9ca2f 393 * coil values. The first discrete input starting at \c usAddress must be
stanley1228 0:aefcdfe9ca2f 394 * stored at the LSB of the first byte in the buffer. If the requested number
stanley1228 0:aefcdfe9ca2f 395 * is not a multiple of eight the remaining bits should be set to zero.
stanley1228 0:aefcdfe9ca2f 396 * \param usAddress The starting address of the first discrete input.
stanley1228 0:aefcdfe9ca2f 397 * \param usNDiscrete Number of discrete input values.
stanley1228 0:aefcdfe9ca2f 398 * \return The function must return one of the following error codes:
stanley1228 0:aefcdfe9ca2f 399 * - eMBErrorCode::MB_ENOERR If no error occurred. In this case a normal
stanley1228 0:aefcdfe9ca2f 400 * Modbus response is sent.
stanley1228 0:aefcdfe9ca2f 401 * - eMBErrorCode::MB_ENOREG If no such discrete inputs exists.
stanley1228 0:aefcdfe9ca2f 402 * In this case a <b>ILLEGAL DATA ADDRESS</b> exception frame is sent
stanley1228 0:aefcdfe9ca2f 403 * as a response.
stanley1228 0:aefcdfe9ca2f 404 * - eMBErrorCode::MB_ETIMEDOUT If the requested register block is
stanley1228 0:aefcdfe9ca2f 405 * currently not available and the application dependent response
stanley1228 0:aefcdfe9ca2f 406 * timeout would be violated. In this case a <b>SLAVE DEVICE BUSY</b>
stanley1228 0:aefcdfe9ca2f 407 * exception is sent as a response.
stanley1228 0:aefcdfe9ca2f 408 * - eMBErrorCode::MB_EIO If an unrecoverable error occurred. In this case
stanley1228 0:aefcdfe9ca2f 409 * a <b>SLAVE DEVICE FAILURE</b> exception is sent as a response.
stanley1228 0:aefcdfe9ca2f 410 */
stanley1228 0:aefcdfe9ca2f 411 eMBErrorCode eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress,
stanley1228 0:aefcdfe9ca2f 412 USHORT usNDiscrete );
stanley1228 0:aefcdfe9ca2f 413
stanley1228 0:aefcdfe9ca2f 414 #ifdef __cplusplus
stanley1228 0:aefcdfe9ca2f 415 PR_END_EXTERN_C
stanley1228 0:aefcdfe9ca2f 416 #endif
stanley1228 0:aefcdfe9ca2f 417 #endif