mbutils.h 4.2 KB

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