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 * FreeRTOS Modbus Libary: A Modbus serial implementation for FreeRTOS
stanley1228 0:aefcdfe9ca2f 3 * Copyright (C) 2006 Christian Walter <wolti@sil.at>
stanley1228 0:aefcdfe9ca2f 4 *
stanley1228 0:aefcdfe9ca2f 5 * This library is free software; you can redistribute it and/or
stanley1228 0:aefcdfe9ca2f 6 * modify it under the terms of the GNU Lesser General Public
stanley1228 0:aefcdfe9ca2f 7 * License as published by the Free Software Foundation; either
stanley1228 0:aefcdfe9ca2f 8 * version 2.1 of the License, or (at your option) any later version.
stanley1228 0:aefcdfe9ca2f 9 *
stanley1228 0:aefcdfe9ca2f 10 * This library is distributed in the hope that it will be useful,
stanley1228 0:aefcdfe9ca2f 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
stanley1228 0:aefcdfe9ca2f 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
stanley1228 0:aefcdfe9ca2f 13 * Lesser General Public License for more details.
stanley1228 0:aefcdfe9ca2f 14 *
stanley1228 0:aefcdfe9ca2f 15 * You should have received a copy of the GNU Lesser General Public
stanley1228 0:aefcdfe9ca2f 16 * License along with this library; if not, write to the Free Software
stanley1228 0:aefcdfe9ca2f 17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
stanley1228 0:aefcdfe9ca2f 18 */
stanley1228 0:aefcdfe9ca2f 19
stanley1228 0:aefcdfe9ca2f 20
stanley1228 0:aefcdfe9ca2f 21
stanley1228 0:aefcdfe9ca2f 22 /* ----------------------- System includes ----------------------------------*/
stanley1228 0:aefcdfe9ca2f 23 #include "stdlib.h"
stanley1228 0:aefcdfe9ca2f 24 #include "string.h"
stanley1228 0:aefcdfe9ca2f 25
stanley1228 0:aefcdfe9ca2f 26 /* ----------------------- Platform includes --------------------------------*/
stanley1228 0:aefcdfe9ca2f 27 #include "port.h"
stanley1228 0:aefcdfe9ca2f 28
stanley1228 0:aefcdfe9ca2f 29 /* ----------------------- Modbus includes ----------------------------------*/
stanley1228 0:aefcdfe9ca2f 30 #include "mb.h"
stanley1228 0:aefcdfe9ca2f 31 #include "mbframe.h"
stanley1228 0:aefcdfe9ca2f 32 #include "mbproto.h"
stanley1228 0:aefcdfe9ca2f 33 #include "mbconfig.h"
stanley1228 0:aefcdfe9ca2f 34
stanley1228 0:aefcdfe9ca2f 35 /* ----------------------- Defines ------------------------------------------*/
stanley1228 0:aefcdfe9ca2f 36 #define MB_PDU_FUNC_READ_ADDR_OFF ( MB_PDU_DATA_OFF )
stanley1228 0:aefcdfe9ca2f 37 #define MB_PDU_FUNC_READ_DISCCNT_OFF ( MB_PDU_DATA_OFF + 2 )
stanley1228 0:aefcdfe9ca2f 38 #define MB_PDU_FUNC_READ_SIZE ( 4 )
stanley1228 0:aefcdfe9ca2f 39 #define MB_PDU_FUNC_READ_DISCCNT_MAX ( 0x07D0 )
stanley1228 0:aefcdfe9ca2f 40
stanley1228 0:aefcdfe9ca2f 41 /* ----------------------- Static functions ---------------------------------*/
stanley1228 0:aefcdfe9ca2f 42 eMBException prveMBError2Exception( eMBErrorCode eErrorCode );
stanley1228 0:aefcdfe9ca2f 43
stanley1228 0:aefcdfe9ca2f 44 /* ----------------------- Start implementation -----------------------------*/
stanley1228 0:aefcdfe9ca2f 45
stanley1228 0:aefcdfe9ca2f 46 #if MB_FUNC_READ_COILS_ENABLED > 0
stanley1228 0:aefcdfe9ca2f 47
stanley1228 0:aefcdfe9ca2f 48 eMBException
stanley1228 0:aefcdfe9ca2f 49 eMBFuncReadDiscreteInputs( UCHAR * pucFrame, USHORT * usLen )
stanley1228 0:aefcdfe9ca2f 50 {
stanley1228 0:aefcdfe9ca2f 51 USHORT usRegAddress;
stanley1228 0:aefcdfe9ca2f 52 USHORT usDiscreteCnt;
stanley1228 0:aefcdfe9ca2f 53 UCHAR ucNBytes;
stanley1228 0:aefcdfe9ca2f 54 UCHAR *pucFrameCur;
stanley1228 0:aefcdfe9ca2f 55
stanley1228 0:aefcdfe9ca2f 56 eMBException eStatus = MB_EX_NONE;
stanley1228 0:aefcdfe9ca2f 57 eMBErrorCode eRegStatus;
stanley1228 0:aefcdfe9ca2f 58
stanley1228 0:aefcdfe9ca2f 59 if( *usLen == ( MB_PDU_FUNC_READ_SIZE + MB_PDU_SIZE_MIN ) )
stanley1228 0:aefcdfe9ca2f 60 {
stanley1228 0:aefcdfe9ca2f 61 usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF] << 8 );
stanley1228 0:aefcdfe9ca2f 62 usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF + 1] );
stanley1228 0:aefcdfe9ca2f 63 usRegAddress++;
stanley1228 0:aefcdfe9ca2f 64
stanley1228 0:aefcdfe9ca2f 65 usDiscreteCnt = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_DISCCNT_OFF] << 8 );
stanley1228 0:aefcdfe9ca2f 66 usDiscreteCnt |= ( USHORT )( pucFrame[MB_PDU_FUNC_READ_DISCCNT_OFF + 1] );
stanley1228 0:aefcdfe9ca2f 67
stanley1228 0:aefcdfe9ca2f 68 /* Check if the number of registers to read is valid. If not
stanley1228 0:aefcdfe9ca2f 69 * return Modbus illegal data value exception.
stanley1228 0:aefcdfe9ca2f 70 */
stanley1228 0:aefcdfe9ca2f 71 if( ( usDiscreteCnt >= 1 ) &&
stanley1228 0:aefcdfe9ca2f 72 ( usDiscreteCnt < MB_PDU_FUNC_READ_DISCCNT_MAX ) )
stanley1228 0:aefcdfe9ca2f 73 {
stanley1228 0:aefcdfe9ca2f 74 /* Set the current PDU data pointer to the beginning. */
stanley1228 0:aefcdfe9ca2f 75 pucFrameCur = &pucFrame[MB_PDU_FUNC_OFF];
stanley1228 0:aefcdfe9ca2f 76 *usLen = MB_PDU_FUNC_OFF;
stanley1228 0:aefcdfe9ca2f 77
stanley1228 0:aefcdfe9ca2f 78 /* First byte contains the function code. */
stanley1228 0:aefcdfe9ca2f 79 *pucFrameCur++ = MB_FUNC_READ_DISCRETE_INPUTS;
stanley1228 0:aefcdfe9ca2f 80 *usLen += 1;
stanley1228 0:aefcdfe9ca2f 81
stanley1228 0:aefcdfe9ca2f 82 /* Test if the quantity of coils is a multiple of 8. If not last
stanley1228 0:aefcdfe9ca2f 83 * byte is only partially field with unused coils set to zero. */
stanley1228 0:aefcdfe9ca2f 84 if( ( usDiscreteCnt & 0x0007 ) != 0 )
stanley1228 0:aefcdfe9ca2f 85 {
stanley1228 0:aefcdfe9ca2f 86 ucNBytes = ( UCHAR ) ( usDiscreteCnt / 8 + 1 );
stanley1228 0:aefcdfe9ca2f 87 }
stanley1228 0:aefcdfe9ca2f 88 else
stanley1228 0:aefcdfe9ca2f 89 {
stanley1228 0:aefcdfe9ca2f 90 ucNBytes = ( UCHAR ) ( usDiscreteCnt / 8 );
stanley1228 0:aefcdfe9ca2f 91 }
stanley1228 0:aefcdfe9ca2f 92 *pucFrameCur++ = ucNBytes;
stanley1228 0:aefcdfe9ca2f 93 *usLen += 1;
stanley1228 0:aefcdfe9ca2f 94
stanley1228 0:aefcdfe9ca2f 95 eRegStatus =
stanley1228 0:aefcdfe9ca2f 96 eMBRegDiscreteCB( pucFrameCur, usRegAddress, usDiscreteCnt );
stanley1228 0:aefcdfe9ca2f 97
stanley1228 0:aefcdfe9ca2f 98 /* If an error occured convert it into a Modbus exception. */
stanley1228 0:aefcdfe9ca2f 99 if( eRegStatus != MB_ENOERR )
stanley1228 0:aefcdfe9ca2f 100 {
stanley1228 0:aefcdfe9ca2f 101 eStatus = prveMBError2Exception( eRegStatus );
stanley1228 0:aefcdfe9ca2f 102 }
stanley1228 0:aefcdfe9ca2f 103 else
stanley1228 0:aefcdfe9ca2f 104 {
stanley1228 0:aefcdfe9ca2f 105 /* The response contains the function code, the starting address
stanley1228 0:aefcdfe9ca2f 106 * and the quantity of registers. We reuse the old values in the
stanley1228 0:aefcdfe9ca2f 107 * buffer because they are still valid. */
stanley1228 0:aefcdfe9ca2f 108 *usLen += ucNBytes;;
stanley1228 0:aefcdfe9ca2f 109 }
stanley1228 0:aefcdfe9ca2f 110 }
stanley1228 0:aefcdfe9ca2f 111 else
stanley1228 0:aefcdfe9ca2f 112 {
stanley1228 0:aefcdfe9ca2f 113 eStatus = MB_EX_ILLEGAL_DATA_VALUE;
stanley1228 0:aefcdfe9ca2f 114 }
stanley1228 0:aefcdfe9ca2f 115 }
stanley1228 0:aefcdfe9ca2f 116 else
stanley1228 0:aefcdfe9ca2f 117 {
stanley1228 0:aefcdfe9ca2f 118 /* Can't be a valid read coil register request because the length
stanley1228 0:aefcdfe9ca2f 119 * is incorrect. */
stanley1228 0:aefcdfe9ca2f 120 eStatus = MB_EX_ILLEGAL_DATA_VALUE;
stanley1228 0:aefcdfe9ca2f 121 }
stanley1228 0:aefcdfe9ca2f 122 return eStatus;
stanley1228 0:aefcdfe9ca2f 123 }
stanley1228 0:aefcdfe9ca2f 124
stanley1228 0:aefcdfe9ca2f 125 #endif