modified for nuc472

Dependents:   modbus-over-rs485-sample NTOUEE-mbed-modbus-RTU NuMaker_NuWicam_Lite

Fork of Modbus by Matthew Waddilove

Committer:
wclin
Date:
Fri Oct 06 02:54:27 2017 +0000
Revision:
7:be466464a18c
Parent:
0:274eb57e1df3
Support M487 board.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
giryan 0:274eb57e1df3 1 /*
giryan 0:274eb57e1df3 2 * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
giryan 0:274eb57e1df3 3 * Copyright (c) 2006 Christian Walter <wolti@sil.at>
giryan 0:274eb57e1df3 4 * All rights reserved.
giryan 0:274eb57e1df3 5 *
giryan 0:274eb57e1df3 6 * Redistribution and use in source and binary forms, with or without
giryan 0:274eb57e1df3 7 * modification, are permitted provided that the following conditions
giryan 0:274eb57e1df3 8 * are met:
giryan 0:274eb57e1df3 9 * 1. Redistributions of source code must retain the above copyright
giryan 0:274eb57e1df3 10 * notice, this list of conditions and the following disclaimer.
giryan 0:274eb57e1df3 11 * 2. Redistributions in binary form must reproduce the above copyright
giryan 0:274eb57e1df3 12 * notice, this list of conditions and the following disclaimer in the
giryan 0:274eb57e1df3 13 * documentation and/or other materials provided with the distribution.
giryan 0:274eb57e1df3 14 * 3. The name of the author may not be used to endorse or promote products
giryan 0:274eb57e1df3 15 * derived from this software without specific prior written permission.
giryan 0:274eb57e1df3 16 *
giryan 0:274eb57e1df3 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
giryan 0:274eb57e1df3 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
giryan 0:274eb57e1df3 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
giryan 0:274eb57e1df3 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
giryan 0:274eb57e1df3 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
giryan 0:274eb57e1df3 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
giryan 0:274eb57e1df3 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
giryan 0:274eb57e1df3 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
giryan 0:274eb57e1df3 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
giryan 0:274eb57e1df3 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
giryan 0:274eb57e1df3 27 *
giryan 0:274eb57e1df3 28 * File: $Id: mbutils.c,v 1.6 2007/02/18 23:49:07 wolti Exp $
giryan 0:274eb57e1df3 29 */
giryan 0:274eb57e1df3 30
giryan 0:274eb57e1df3 31 /* ----------------------- System includes ----------------------------------*/
giryan 0:274eb57e1df3 32 #include "stdlib.h"
giryan 0:274eb57e1df3 33 #include "string.h"
giryan 0:274eb57e1df3 34
giryan 0:274eb57e1df3 35 /* ----------------------- Platform includes --------------------------------*/
giryan 0:274eb57e1df3 36 #include "port.h"
giryan 0:274eb57e1df3 37
giryan 0:274eb57e1df3 38 /* ----------------------- Modbus includes ----------------------------------*/
giryan 0:274eb57e1df3 39 #include "mb.h"
giryan 0:274eb57e1df3 40 #include "mbproto.h"
giryan 0:274eb57e1df3 41
giryan 0:274eb57e1df3 42 /* ----------------------- Defines ------------------------------------------*/
giryan 0:274eb57e1df3 43 #define BITS_UCHAR 8U
giryan 0:274eb57e1df3 44
giryan 0:274eb57e1df3 45 /* ----------------------- Start implementation -----------------------------*/
giryan 0:274eb57e1df3 46 void
giryan 0:274eb57e1df3 47 xMBUtilSetBits( UCHAR * ucByteBuf, USHORT usBitOffset, UCHAR ucNBits,
giryan 0:274eb57e1df3 48 UCHAR ucValue )
giryan 0:274eb57e1df3 49 {
giryan 0:274eb57e1df3 50 USHORT usWordBuf;
giryan 0:274eb57e1df3 51 USHORT usMask;
giryan 0:274eb57e1df3 52 USHORT usByteOffset;
giryan 0:274eb57e1df3 53 USHORT usNPreBits;
giryan 0:274eb57e1df3 54 USHORT usValue = ucValue;
giryan 0:274eb57e1df3 55
giryan 0:274eb57e1df3 56 assert( ucNBits <= 8 );
giryan 0:274eb57e1df3 57 assert( ( size_t )BITS_UCHAR == sizeof( UCHAR ) * 8 );
giryan 0:274eb57e1df3 58
giryan 0:274eb57e1df3 59 /* Calculate byte offset for first byte containing the bit values starting
giryan 0:274eb57e1df3 60 * at usBitOffset. */
giryan 0:274eb57e1df3 61 usByteOffset = ( USHORT )( ( usBitOffset ) / BITS_UCHAR );
giryan 0:274eb57e1df3 62
giryan 0:274eb57e1df3 63 /* How many bits precede our bits to set. */
giryan 0:274eb57e1df3 64 usNPreBits = ( USHORT )( usBitOffset - usByteOffset * BITS_UCHAR );
giryan 0:274eb57e1df3 65
giryan 0:274eb57e1df3 66 /* Move bit field into position over bits to set */
giryan 0:274eb57e1df3 67 usValue <<= usNPreBits;
giryan 0:274eb57e1df3 68
giryan 0:274eb57e1df3 69 /* Prepare a mask for setting the new bits. */
giryan 0:274eb57e1df3 70 usMask = ( USHORT )( ( 1 << ( USHORT ) ucNBits ) - 1 );
giryan 0:274eb57e1df3 71 usMask <<= usBitOffset - usByteOffset * BITS_UCHAR;
giryan 0:274eb57e1df3 72
giryan 0:274eb57e1df3 73 /* copy bits into temporary storage. */
giryan 0:274eb57e1df3 74 usWordBuf = ucByteBuf[usByteOffset];
giryan 0:274eb57e1df3 75 usWordBuf |= ucByteBuf[usByteOffset + 1] << BITS_UCHAR;
giryan 0:274eb57e1df3 76
giryan 0:274eb57e1df3 77 /* Zero out bit field bits and then or value bits into them. */
giryan 0:274eb57e1df3 78 usWordBuf = ( USHORT )( ( usWordBuf & ( ~usMask ) ) | usValue );
giryan 0:274eb57e1df3 79
giryan 0:274eb57e1df3 80 /* move bits back into storage */
giryan 0:274eb57e1df3 81 ucByteBuf[usByteOffset] = ( UCHAR )( usWordBuf & 0xFF );
giryan 0:274eb57e1df3 82 ucByteBuf[usByteOffset + 1] = ( UCHAR )( usWordBuf >> BITS_UCHAR );
giryan 0:274eb57e1df3 83 }
giryan 0:274eb57e1df3 84
giryan 0:274eb57e1df3 85 UCHAR
giryan 0:274eb57e1df3 86 xMBUtilGetBits( UCHAR * ucByteBuf, USHORT usBitOffset, UCHAR ucNBits )
giryan 0:274eb57e1df3 87 {
giryan 0:274eb57e1df3 88 USHORT usWordBuf;
giryan 0:274eb57e1df3 89 USHORT usMask;
giryan 0:274eb57e1df3 90 USHORT usByteOffset;
giryan 0:274eb57e1df3 91 USHORT usNPreBits;
giryan 0:274eb57e1df3 92
giryan 0:274eb57e1df3 93 /* Calculate byte offset for first byte containing the bit values starting
giryan 0:274eb57e1df3 94 * at usBitOffset. */
giryan 0:274eb57e1df3 95 usByteOffset = ( USHORT )( ( usBitOffset ) / BITS_UCHAR );
giryan 0:274eb57e1df3 96
giryan 0:274eb57e1df3 97 /* How many bits precede our bits to set. */
giryan 0:274eb57e1df3 98 usNPreBits = ( USHORT )( usBitOffset - usByteOffset * BITS_UCHAR );
giryan 0:274eb57e1df3 99
giryan 0:274eb57e1df3 100 /* Prepare a mask for setting the new bits. */
giryan 0:274eb57e1df3 101 usMask = ( USHORT )( ( 1 << ( USHORT ) ucNBits ) - 1 );
giryan 0:274eb57e1df3 102
giryan 0:274eb57e1df3 103 /* copy bits into temporary storage. */
giryan 0:274eb57e1df3 104 usWordBuf = ucByteBuf[usByteOffset];
giryan 0:274eb57e1df3 105 usWordBuf |= ucByteBuf[usByteOffset + 1] << BITS_UCHAR;
giryan 0:274eb57e1df3 106
giryan 0:274eb57e1df3 107 /* throw away unneeded bits. */
giryan 0:274eb57e1df3 108 usWordBuf >>= usNPreBits;
giryan 0:274eb57e1df3 109
giryan 0:274eb57e1df3 110 /* mask away bits above the requested bitfield. */
giryan 0:274eb57e1df3 111 usWordBuf &= usMask;
giryan 0:274eb57e1df3 112
giryan 0:274eb57e1df3 113 return ( UCHAR ) usWordBuf;
giryan 0:274eb57e1df3 114 }
giryan 0:274eb57e1df3 115
giryan 0:274eb57e1df3 116 eMBException
giryan 0:274eb57e1df3 117 prveMBError2Exception( eMBErrorCode eErrorCode )
giryan 0:274eb57e1df3 118 {
giryan 0:274eb57e1df3 119 eMBException eStatus;
giryan 0:274eb57e1df3 120
giryan 0:274eb57e1df3 121 switch ( eErrorCode )
giryan 0:274eb57e1df3 122 {
giryan 0:274eb57e1df3 123 case MB_ENOERR:
giryan 0:274eb57e1df3 124 eStatus = MB_EX_NONE;
giryan 0:274eb57e1df3 125 break;
giryan 0:274eb57e1df3 126
giryan 0:274eb57e1df3 127 case MB_ENOREG:
giryan 0:274eb57e1df3 128 eStatus = MB_EX_ILLEGAL_DATA_ADDRESS;
giryan 0:274eb57e1df3 129 break;
giryan 0:274eb57e1df3 130
giryan 0:274eb57e1df3 131 case MB_ETIMEDOUT:
giryan 0:274eb57e1df3 132 eStatus = MB_EX_SLAVE_BUSY;
giryan 0:274eb57e1df3 133 break;
giryan 0:274eb57e1df3 134
giryan 0:274eb57e1df3 135 default:
giryan 0:274eb57e1df3 136 eStatus = MB_EX_SLAVE_DEVICE_FAILURE;
giryan 0:274eb57e1df3 137 break;
giryan 0:274eb57e1df3 138 }
giryan 0:274eb57e1df3 139
giryan 0:274eb57e1df3 140 return eStatus;
giryan 0:274eb57e1df3 141 }