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