1 /* 2 * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU. 3 * Copyright (c) 2006 Christian Walter <wolti@sil.at> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. The name of the author may not be used to endorse or promote products 15 * derived from this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * 28 * File: $Id: mbutils.h,v 1.5 2006/12/07 22:10:34 wolti Exp $ 29 */ 30 31 #ifndef _MB_UTILS_H 32 #define _MB_UTILS_H 33 34 #ifdef __cplusplus 35 PR_BEGIN_EXTERN_C 36 #endif 37 /*! \defgroup modbus_utils Utilities 38 * 39 * This module contains some utility functions which can be used by 40 * the application. It includes some special functions for working with 41 * bitfields backed by a character array buffer. 42 * 43 */ 44 /*! \addtogroup modbus_utils 45 * @{ 46 */ 47 /*! \brief Function to set bits in a byte buffer. 48 * 49 * This function allows the efficient use of an array to implement bitfields. 50 * The array used for storing the bits must always be a multiple of two 51 * bytes. Up to eight bits can be set or cleared in one operation. 52 * 53 * \param ucByteBuf A buffer where the bit values are stored. Must be a 54 * multiple of 2 bytes. No length checking is performed and if 55 * usBitOffset / 8 is greater than the size of the buffer memory contents 56 * is overwritten. 57 * \param usBitOffset The starting address of the bits to set. The first 58 * bit has the offset 0. 59 * \param ucNBits Number of bits to modify. The value must always be smaller 60 * than 8. 61 * \param ucValues Thew new values for the bits. The value for the first bit 62 * starting at <code>usBitOffset</code> is the LSB of the value 63 * <code>ucValues</code> 64 * 65 * \code 66 * ucBits[2] = {0, 0}; 67 * 68 * // Set bit 4 to 1 (read: set 1 bit starting at bit offset 4 to value 1) 69 * xMBUtilSetBits( ucBits, 4, 1, 1 ); 70 * 71 * // Set bit 7 to 1 and bit 8 to 0. 72 * xMBUtilSetBits( ucBits, 7, 2, 0x01 ); 73 * 74 * // Set bits 8 - 11 to 0x05 and bits 12 - 15 to 0x0A; 75 * xMBUtilSetBits( ucBits, 8, 8, 0x5A); 76 * \endcode 77 */ 78 void xMBUtilSetBits( UCHAR * ucByteBuf, USHORT usBitOffset, 79 UCHAR ucNBits, UCHAR ucValues ); 80 81 /*! \brief Function to read bits in a byte buffer. 82 * 83 * This function is used to extract up bit values from an array. Up to eight 84 * bit values can be extracted in one step. 85 * 86 * \param ucByteBuf A buffer where the bit values are stored. 87 * \param usBitOffset The starting address of the bits to set. The first 88 * bit has the offset 0. 89 * \param ucNBits Number of bits to modify. The value must always be smaller 90 * than 8. 91 * 92 * \code 93 * UCHAR ucBits[2] = {0, 0}; 94 * UCHAR ucResult; 95 * 96 * // Extract the bits 3 - 10. 97 * ucResult = xMBUtilGetBits( ucBits, 3, 8 ); 98 * \endcode 99 */ 100 UCHAR xMBUtilGetBits( UCHAR * ucByteBuf, USHORT usBitOffset, 101 UCHAR ucNBits ); 102 103 /*! @} */ 104 105 #ifdef __cplusplus 106 PR_END_EXTERN_C 107 #endif 108 #endif 109