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: mbutils.c,v 1.6 2007/02/18 23:49:07 wolti Exp $
stanley1228 0:aefcdfe9ca2f 29 */
stanley1228 0:aefcdfe9ca2f 30
stanley1228 0:aefcdfe9ca2f 31 /* ----------------------- System includes ----------------------------------*/
stanley1228 0:aefcdfe9ca2f 32 #include "stdlib.h"
stanley1228 0:aefcdfe9ca2f 33 #include "string.h"
stanley1228 0:aefcdfe9ca2f 34
stanley1228 0:aefcdfe9ca2f 35 /* ----------------------- Platform includes --------------------------------*/
stanley1228 0:aefcdfe9ca2f 36 #include "port.h"
stanley1228 0:aefcdfe9ca2f 37
stanley1228 0:aefcdfe9ca2f 38 /* ----------------------- Modbus includes ----------------------------------*/
stanley1228 0:aefcdfe9ca2f 39 #include "mb.h"
stanley1228 0:aefcdfe9ca2f 40 #include "mbproto.h"
stanley1228 0:aefcdfe9ca2f 41
stanley1228 0:aefcdfe9ca2f 42 /* ----------------------- Defines ------------------------------------------*/
stanley1228 0:aefcdfe9ca2f 43 #define BITS_UCHAR 8U
stanley1228 0:aefcdfe9ca2f 44
stanley1228 0:aefcdfe9ca2f 45 /* ----------------------- Start implementation -----------------------------*/
stanley1228 0:aefcdfe9ca2f 46 void
stanley1228 0:aefcdfe9ca2f 47 xMBUtilSetBits( UCHAR * ucByteBuf, USHORT usBitOffset, UCHAR ucNBits,
stanley1228 0:aefcdfe9ca2f 48 UCHAR ucValue )
stanley1228 0:aefcdfe9ca2f 49 {
stanley1228 0:aefcdfe9ca2f 50 USHORT usWordBuf;
stanley1228 0:aefcdfe9ca2f 51 USHORT usMask;
stanley1228 0:aefcdfe9ca2f 52 USHORT usByteOffset;
stanley1228 0:aefcdfe9ca2f 53 USHORT usNPreBits;
stanley1228 0:aefcdfe9ca2f 54 USHORT usValue = ucValue;
stanley1228 0:aefcdfe9ca2f 55
stanley1228 0:aefcdfe9ca2f 56 assert( ucNBits <= 8 );
stanley1228 0:aefcdfe9ca2f 57 assert( ( size_t )BITS_UCHAR == sizeof( UCHAR ) * 8 );
stanley1228 0:aefcdfe9ca2f 58
stanley1228 0:aefcdfe9ca2f 59 /* Calculate byte offset for first byte containing the bit values starting
stanley1228 0:aefcdfe9ca2f 60 * at usBitOffset. */
stanley1228 0:aefcdfe9ca2f 61 usByteOffset = ( USHORT )( ( usBitOffset ) / BITS_UCHAR );
stanley1228 0:aefcdfe9ca2f 62
stanley1228 0:aefcdfe9ca2f 63 /* How many bits precede our bits to set. */
stanley1228 0:aefcdfe9ca2f 64 usNPreBits = ( USHORT )( usBitOffset - usByteOffset * BITS_UCHAR );
stanley1228 0:aefcdfe9ca2f 65
stanley1228 0:aefcdfe9ca2f 66 /* Move bit field into position over bits to set */
stanley1228 0:aefcdfe9ca2f 67 usValue <<= usNPreBits;
stanley1228 0:aefcdfe9ca2f 68
stanley1228 0:aefcdfe9ca2f 69 /* Prepare a mask for setting the new bits. */
stanley1228 0:aefcdfe9ca2f 70 usMask = ( USHORT )( ( 1 << ( USHORT ) ucNBits ) - 1 );
stanley1228 0:aefcdfe9ca2f 71 usMask <<= usBitOffset - usByteOffset * BITS_UCHAR;
stanley1228 0:aefcdfe9ca2f 72
stanley1228 0:aefcdfe9ca2f 73 /* copy bits into temporary storage. */
stanley1228 0:aefcdfe9ca2f 74 usWordBuf = ucByteBuf[usByteOffset];
stanley1228 0:aefcdfe9ca2f 75 usWordBuf |= ucByteBuf[usByteOffset + 1] << BITS_UCHAR;
stanley1228 0:aefcdfe9ca2f 76
stanley1228 0:aefcdfe9ca2f 77 /* Zero out bit field bits and then or value bits into them. */
stanley1228 0:aefcdfe9ca2f 78 usWordBuf = ( USHORT )( ( usWordBuf & ( ~usMask ) ) | usValue );
stanley1228 0:aefcdfe9ca2f 79
stanley1228 0:aefcdfe9ca2f 80 /* move bits back into storage */
stanley1228 0:aefcdfe9ca2f 81 ucByteBuf[usByteOffset] = ( UCHAR )( usWordBuf & 0xFF );
stanley1228 0:aefcdfe9ca2f 82 ucByteBuf[usByteOffset + 1] = ( UCHAR )( usWordBuf >> BITS_UCHAR );
stanley1228 0:aefcdfe9ca2f 83 }
stanley1228 0:aefcdfe9ca2f 84
stanley1228 0:aefcdfe9ca2f 85 UCHAR
stanley1228 0:aefcdfe9ca2f 86 xMBUtilGetBits( UCHAR * ucByteBuf, USHORT usBitOffset, UCHAR ucNBits )
stanley1228 0:aefcdfe9ca2f 87 {
stanley1228 0:aefcdfe9ca2f 88 USHORT usWordBuf;
stanley1228 0:aefcdfe9ca2f 89 USHORT usMask;
stanley1228 0:aefcdfe9ca2f 90 USHORT usByteOffset;
stanley1228 0:aefcdfe9ca2f 91 USHORT usNPreBits;
stanley1228 0:aefcdfe9ca2f 92
stanley1228 0:aefcdfe9ca2f 93 /* Calculate byte offset for first byte containing the bit values starting
stanley1228 0:aefcdfe9ca2f 94 * at usBitOffset. */
stanley1228 0:aefcdfe9ca2f 95 usByteOffset = ( USHORT )( ( usBitOffset ) / BITS_UCHAR );
stanley1228 0:aefcdfe9ca2f 96
stanley1228 0:aefcdfe9ca2f 97 /* How many bits precede our bits to set. */
stanley1228 0:aefcdfe9ca2f 98 usNPreBits = ( USHORT )( usBitOffset - usByteOffset * BITS_UCHAR );
stanley1228 0:aefcdfe9ca2f 99
stanley1228 0:aefcdfe9ca2f 100 /* Prepare a mask for setting the new bits. */
stanley1228 0:aefcdfe9ca2f 101 usMask = ( USHORT )( ( 1 << ( USHORT ) ucNBits ) - 1 );
stanley1228 0:aefcdfe9ca2f 102
stanley1228 0:aefcdfe9ca2f 103 /* copy bits into temporary storage. */
stanley1228 0:aefcdfe9ca2f 104 usWordBuf = ucByteBuf[usByteOffset];
stanley1228 0:aefcdfe9ca2f 105 usWordBuf |= ucByteBuf[usByteOffset + 1] << BITS_UCHAR;
stanley1228 0:aefcdfe9ca2f 106
stanley1228 0:aefcdfe9ca2f 107 /* throw away unneeded bits. */
stanley1228 0:aefcdfe9ca2f 108 usWordBuf >>= usNPreBits;
stanley1228 0:aefcdfe9ca2f 109
stanley1228 0:aefcdfe9ca2f 110 /* mask away bits above the requested bitfield. */
stanley1228 0:aefcdfe9ca2f 111 usWordBuf &= usMask;
stanley1228 0:aefcdfe9ca2f 112
stanley1228 0:aefcdfe9ca2f 113 return ( UCHAR ) usWordBuf;
stanley1228 0:aefcdfe9ca2f 114 }
stanley1228 0:aefcdfe9ca2f 115
stanley1228 0:aefcdfe9ca2f 116 eMBException
stanley1228 0:aefcdfe9ca2f 117 prveMBError2Exception( eMBErrorCode eErrorCode )
stanley1228 0:aefcdfe9ca2f 118 {
stanley1228 0:aefcdfe9ca2f 119 eMBException eStatus;
stanley1228 0:aefcdfe9ca2f 120
stanley1228 0:aefcdfe9ca2f 121 switch ( eErrorCode )
stanley1228 0:aefcdfe9ca2f 122 {
stanley1228 0:aefcdfe9ca2f 123 case MB_ENOERR:
stanley1228 0:aefcdfe9ca2f 124 eStatus = MB_EX_NONE;
stanley1228 0:aefcdfe9ca2f 125 break;
stanley1228 0:aefcdfe9ca2f 126
stanley1228 0:aefcdfe9ca2f 127 case MB_ENOREG:
stanley1228 0:aefcdfe9ca2f 128 eStatus = MB_EX_ILLEGAL_DATA_ADDRESS;
stanley1228 0:aefcdfe9ca2f 129 break;
stanley1228 0:aefcdfe9ca2f 130
stanley1228 0:aefcdfe9ca2f 131 case MB_ETIMEDOUT:
stanley1228 0:aefcdfe9ca2f 132 eStatus = MB_EX_SLAVE_BUSY;
stanley1228 0:aefcdfe9ca2f 133 break;
stanley1228 0:aefcdfe9ca2f 134
stanley1228 0:aefcdfe9ca2f 135 default:
stanley1228 0:aefcdfe9ca2f 136 eStatus = MB_EX_SLAVE_DEVICE_FAILURE;
stanley1228 0:aefcdfe9ca2f 137 break;
stanley1228 0:aefcdfe9ca2f 138 }
stanley1228 0:aefcdfe9ca2f 139
stanley1228 0:aefcdfe9ca2f 140 return eStatus;
stanley1228 0:aefcdfe9ca2f 141 }