Forking https://os.mbed.com/users/cam/code/Modbus/ to work for NUCLEO 64 boards

Fork of Cam's original FreeModbus port (https://os.mbed.com/users/cam/code/Modbus/)

Change: - Serial implementation to work for NUCLEO 64 boards and receive interrupts instead of timer. (see `portserial.cpp`)

Added: - Custom RTU mode. Allows for external implementation of packet receiving and sending. Sends and receives packets as whole frames (address + PDU) (i.e. this was added for a custom LoRa implementation). implement `xMBRTUCustGetPDU` and `xMBRTUCustSendResponse` (see `mbport.h`) and call `eMBRTUCustomInit( address )`. implementations need to be fully initialised as `eMBRTUCustomInit` only sets the address and nothing else.

Committer:
danielmckinnell
Date:
Mon Jan 06 01:17:26 2020 +0000
Revision:
3:4cda95d7b6c5
Child:
4:7621103c5a40
Added a full frame PDU RTU mode for custom implementations (i.e. LPWAN)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
danielmckinnell 3:4cda95d7b6c5 1 /*
danielmckinnell 3:4cda95d7b6c5 2 * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
danielmckinnell 3:4cda95d7b6c5 3 * Copyright (c) 2006 Christian Walter <wolti@sil.at>
danielmckinnell 3:4cda95d7b6c5 4 * All rights reserved.
danielmckinnell 3:4cda95d7b6c5 5 *
danielmckinnell 3:4cda95d7b6c5 6 * Redistribution and use in source and binary forms, with or without
danielmckinnell 3:4cda95d7b6c5 7 * modification, are permitted provided that the following conditions
danielmckinnell 3:4cda95d7b6c5 8 * are met:
danielmckinnell 3:4cda95d7b6c5 9 * 1. Redistributions of source code must retain the above copyright
danielmckinnell 3:4cda95d7b6c5 10 * notice, this list of conditions and the following disclaimer.
danielmckinnell 3:4cda95d7b6c5 11 * 2. Redistributions in binary form must reproduce the above copyright
danielmckinnell 3:4cda95d7b6c5 12 * notice, this list of conditions and the following disclaimer in the
danielmckinnell 3:4cda95d7b6c5 13 * documentation and/or other materials provided with the distribution.
danielmckinnell 3:4cda95d7b6c5 14 * 3. The name of the author may not be used to endorse or promote products
danielmckinnell 3:4cda95d7b6c5 15 * derived from this software without specific prior written permission.
danielmckinnell 3:4cda95d7b6c5 16 *
danielmckinnell 3:4cda95d7b6c5 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
danielmckinnell 3:4cda95d7b6c5 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
danielmckinnell 3:4cda95d7b6c5 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
danielmckinnell 3:4cda95d7b6c5 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
danielmckinnell 3:4cda95d7b6c5 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
danielmckinnell 3:4cda95d7b6c5 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
danielmckinnell 3:4cda95d7b6c5 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
danielmckinnell 3:4cda95d7b6c5 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
danielmckinnell 3:4cda95d7b6c5 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
danielmckinnell 3:4cda95d7b6c5 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
danielmckinnell 3:4cda95d7b6c5 27 *
danielmckinnell 3:4cda95d7b6c5 28 * File: $Id: mbtcp.c,v 1.3 2006/12/07 22:10:34 wolti Exp $
danielmckinnell 3:4cda95d7b6c5 29 */
danielmckinnell 3:4cda95d7b6c5 30
danielmckinnell 3:4cda95d7b6c5 31 /* ----------------------- System includes ----------------------------------*/
danielmckinnell 3:4cda95d7b6c5 32 #include "stdlib.h"
danielmckinnell 3:4cda95d7b6c5 33 #include "string.h"
danielmckinnell 3:4cda95d7b6c5 34
danielmckinnell 3:4cda95d7b6c5 35 /* ----------------------- Platform includes --------------------------------*/
danielmckinnell 3:4cda95d7b6c5 36 #include "port.h"
danielmckinnell 3:4cda95d7b6c5 37
danielmckinnell 3:4cda95d7b6c5 38 /* ----------------------- Modbus includes ----------------------------------*/
danielmckinnell 3:4cda95d7b6c5 39 #include "mb.h"
danielmckinnell 3:4cda95d7b6c5 40 #include "mbconfig.h"
danielmckinnell 3:4cda95d7b6c5 41 #include "mbrtucustom.h"
danielmckinnell 3:4cda95d7b6c5 42 #include "mbframe.h"
danielmckinnell 3:4cda95d7b6c5 43 #include "mbport.h"
danielmckinnell 3:4cda95d7b6c5 44
danielmckinnell 3:4cda95d7b6c5 45 #if MB_RTU_CUSTOM_ENABLED == 1
danielmckinnell 3:4cda95d7b6c5 46
danielmckinnell 3:4cda95d7b6c5 47 /* ----------------------- Defines ------------------------------------------*/
danielmckinnell 3:4cda95d7b6c5 48
danielmckinnell 3:4cda95d7b6c5 49 /* ----------------------- MBAP Header --------------------------------------*/
danielmckinnell 3:4cda95d7b6c5 50
danielmckinnell 3:4cda95d7b6c5 51 /* ----------------------- Start implementation -----------------------------*/
danielmckinnell 3:4cda95d7b6c5 52 eMBErrorCode
danielmckinnell 3:4cda95d7b6c5 53 eMBRTUCustInit( UCHAR ucMBAddress )
danielmckinnell 3:4cda95d7b6c5 54 {
danielmckinnell 3:4cda95d7b6c5 55 return MB_ENOERR;
danielmckinnell 3:4cda95d7b6c5 56 }
danielmckinnell 3:4cda95d7b6c5 57
danielmckinnell 3:4cda95d7b6c5 58 void
danielmckinnell 3:4cda95d7b6c5 59 eMBRTUCustStart( void )
danielmckinnell 3:4cda95d7b6c5 60 {
danielmckinnell 3:4cda95d7b6c5 61 }
danielmckinnell 3:4cda95d7b6c5 62
danielmckinnell 3:4cda95d7b6c5 63 void
danielmckinnell 3:4cda95d7b6c5 64 eMBRTUCustStop( void )
danielmckinnell 3:4cda95d7b6c5 65 {
danielmckinnell 3:4cda95d7b6c5 66 }
danielmckinnell 3:4cda95d7b6c5 67
danielmckinnell 3:4cda95d7b6c5 68 eMBErrorCode
danielmckinnell 3:4cda95d7b6c5 69 eMBRTUCustReceive( UCHAR * pucRcvAddress, UCHAR ** ppucFrame, USHORT * pusLength )
danielmckinnell 3:4cda95d7b6c5 70 {
danielmckinnell 3:4cda95d7b6c5 71 eMBErrorCode eStatus = MB_EIO;
danielmckinnell 3:4cda95d7b6c5 72 UCHAR *pucMBFrame;
danielmckinnell 3:4cda95d7b6c5 73 USHORT usLength;
danielmckinnell 3:4cda95d7b6c5 74
danielmckinnell 3:4cda95d7b6c5 75 ENTER_CRITICAL_SECTION( );
danielmckinnell 3:4cda95d7b6c5 76 if( xMBRTUCustGetPDU( &pucMBFrame, &usLength ) != FALSE )
danielmckinnell 3:4cda95d7b6c5 77 {
danielmckinnell 3:4cda95d7b6c5 78
danielmckinnell 3:4cda95d7b6c5 79 //TODO: check modbus CRC vs LoRa CRC (redundant if both)
danielmckinnell 3:4cda95d7b6c5 80
danielmckinnell 3:4cda95d7b6c5 81 // usMBCRC16( ( UCHAR * ) ucRTUBuf, usRcvBufferPos ) == 0;
danielmckinnell 3:4cda95d7b6c5 82 *pucRcvAddress = pucMBFrame[0]; //TODO: check this is address for modbus RTU
danielmckinnell 3:4cda95d7b6c5 83 *ppucFrame = &pucMBFrame[1];
danielmckinnell 3:4cda95d7b6c5 84 *pusLength = usLength - 1; // removed address
danielmckinnell 3:4cda95d7b6c5 85 eStatus = MB_ENOERR;
danielmckinnell 3:4cda95d7b6c5 86 }
danielmckinnell 3:4cda95d7b6c5 87 else
danielmckinnell 3:4cda95d7b6c5 88 {
danielmckinnell 3:4cda95d7b6c5 89 eStatus = MB_EIO;
danielmckinnell 3:4cda95d7b6c5 90 }
danielmckinnell 3:4cda95d7b6c5 91 EXIT_CRITICAL_SECTION( );
danielmckinnell 3:4cda95d7b6c5 92 return eStatus;
danielmckinnell 3:4cda95d7b6c5 93 }
danielmckinnell 3:4cda95d7b6c5 94
danielmckinnell 3:4cda95d7b6c5 95 eMBErrorCode
danielmckinnell 3:4cda95d7b6c5 96 eMBRTUCustSend( UCHAR ucSlaveAddress, const UCHAR * pucFrame, USHORT usLength )
danielmckinnell 3:4cda95d7b6c5 97 {
danielmckinnell 3:4cda95d7b6c5 98 eMBErrorCode eStatus = MB_ENOERR;
danielmckinnell 3:4cda95d7b6c5 99 UCHAR *pucMBFrame;
danielmckinnell 3:4cda95d7b6c5 100
danielmckinnell 3:4cda95d7b6c5 101 /* First byte before the Modbus-PDU is the slave address. */
danielmckinnell 3:4cda95d7b6c5 102 pucMBFrame = ( UCHAR * ) pucFrame - 1;
danielmckinnell 3:4cda95d7b6c5 103 *pucMBFrame = ucSlaveAddress;
danielmckinnell 3:4cda95d7b6c5 104 usLength++;
danielmckinnell 3:4cda95d7b6c5 105
danielmckinnell 3:4cda95d7b6c5 106 if( xMBRTUCustSendResponse( pucMBFrame, usLength ) == FALSE )
danielmckinnell 3:4cda95d7b6c5 107 {
danielmckinnell 3:4cda95d7b6c5 108 eStatus = MB_EIO;
danielmckinnell 3:4cda95d7b6c5 109 }
danielmckinnell 3:4cda95d7b6c5 110 return eStatus;
danielmckinnell 3:4cda95d7b6c5 111 }
danielmckinnell 3:4cda95d7b6c5 112
danielmckinnell 3:4cda95d7b6c5 113 #endif