Port of the FreeModbus Libary for mbed, copied from https://developer.mbed.org/users/cam/code/Modbus/ and upgraded to mbed 5

Dependents:   NUCLEO-F401-printf

Committer:
Rajit Singh
Date:
Wed Aug 16 17:31:26 2017 +0100
Revision:
1:3e360cf155b6
Parent:
0:9db3bed8fffd
Remove main.cpp

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Rajit Singh 0:9db3bed8fffd 1 /*
Rajit Singh 0:9db3bed8fffd 2 * FreeModbus Libary: BARE Port
Rajit Singh 0:9db3bed8fffd 3 * Copyright (C) 2006 Christian Walter <wolti@sil.at>
Rajit Singh 0:9db3bed8fffd 4 *
Rajit Singh 0:9db3bed8fffd 5 * This library is free software; you can redistribute it and/or
Rajit Singh 0:9db3bed8fffd 6 * modify it under the terms of the GNU Lesser General Public
Rajit Singh 0:9db3bed8fffd 7 * License as published by the Free Software Foundation; either
Rajit Singh 0:9db3bed8fffd 8 * version 2.1 of the License, or (at your option) any later version.
Rajit Singh 0:9db3bed8fffd 9 *
Rajit Singh 0:9db3bed8fffd 10 * This library is distributed in the hope that it will be useful,
Rajit Singh 0:9db3bed8fffd 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Rajit Singh 0:9db3bed8fffd 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Rajit Singh 0:9db3bed8fffd 13 * Lesser General Public License for more details.
Rajit Singh 0:9db3bed8fffd 14 *
Rajit Singh 0:9db3bed8fffd 15 * You should have received a copy of the GNU Lesser General Public
Rajit Singh 0:9db3bed8fffd 16 * License along with this library; if not, write to the Free Software
Rajit Singh 0:9db3bed8fffd 17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Rajit Singh 0:9db3bed8fffd 18 *
Rajit Singh 0:9db3bed8fffd 19 * File: $Id: portserial.c,v 1.1 2006/08/22 21:35:13 wolti Exp $
Rajit Singh 0:9db3bed8fffd 20 */
Rajit Singh 0:9db3bed8fffd 21
Rajit Singh 0:9db3bed8fffd 22 /* ----------------------- System includes ----------------------------------*/
Rajit Singh 0:9db3bed8fffd 23 #include "mbed.h" // Cam
Rajit Singh 0:9db3bed8fffd 24
Rajit Singh 0:9db3bed8fffd 25 /* ----------------------- Platform includes --------------------------------*/
Rajit Singh 0:9db3bed8fffd 26 #include "port.h"
Rajit Singh 0:9db3bed8fffd 27
Rajit Singh 0:9db3bed8fffd 28 /* ----------------------- Modbus includes ----------------------------------*/
Rajit Singh 0:9db3bed8fffd 29 #include "mb.h"
Rajit Singh 0:9db3bed8fffd 30 #include "mbport.h"
Rajit Singh 0:9db3bed8fffd 31
Rajit Singh 0:9db3bed8fffd 32
Rajit Singh 0:9db3bed8fffd 33 /* ----------------------- static functions ---------------------------------*/
Rajit Singh 0:9db3bed8fffd 34 static void prvvUARTTxReadyISR( void );
Rajit Singh 0:9db3bed8fffd 35 static void prvvUARTRxISR( void );
Rajit Singh 0:9db3bed8fffd 36 static void prvvUARTISR( void );
Rajit Singh 0:9db3bed8fffd 37
Rajit Singh 0:9db3bed8fffd 38 /* ----------------------- System Variables ---------------------------------*/
Rajit Singh 0:9db3bed8fffd 39 Serial pc(USBTX, USBRX); // Cam - mbed USB serial port
Rajit Singh 0:9db3bed8fffd 40
Rajit Singh 0:9db3bed8fffd 41 Ticker simISR; // Cam - mbed ticker
Rajit Singh 0:9db3bed8fffd 42 // we don't have the TX buff empty interrupt, so
Rajit Singh 0:9db3bed8fffd 43 // we just interrupt every 1 mSec and read RX & TX
Rajit Singh 0:9db3bed8fffd 44 // status to simulate the proper ISRs.
Rajit Singh 0:9db3bed8fffd 45
Rajit Singh 0:9db3bed8fffd 46 static BOOL RxEnable, TxEnable; // Cam - keep a static copy of the RxEnable and TxEnable
Rajit Singh 0:9db3bed8fffd 47 // status for the simulated ISR (ticker)
Rajit Singh 0:9db3bed8fffd 48
Rajit Singh 0:9db3bed8fffd 49
Rajit Singh 0:9db3bed8fffd 50 /* ----------------------- Start implementation -----------------------------*/
Rajit Singh 0:9db3bed8fffd 51 // Cam - This is called every 1mS to simulate Rx character received ISR and
Rajit Singh 0:9db3bed8fffd 52 // Tx buffer empty ISR.
Rajit Singh 0:9db3bed8fffd 53 static void
Rajit Singh 0:9db3bed8fffd 54 prvvUARTISR( void )
Rajit Singh 0:9db3bed8fffd 55 {
Rajit Singh 0:9db3bed8fffd 56 if (TxEnable)
Rajit Singh 0:9db3bed8fffd 57 if(pc.writeable())
Rajit Singh 0:9db3bed8fffd 58 prvvUARTTxReadyISR();
Rajit Singh 0:9db3bed8fffd 59
Rajit Singh 0:9db3bed8fffd 60 if (RxEnable)
Rajit Singh 0:9db3bed8fffd 61 if(pc.readable())
Rajit Singh 0:9db3bed8fffd 62 prvvUARTRxISR();
Rajit Singh 0:9db3bed8fffd 63 }
Rajit Singh 0:9db3bed8fffd 64
Rajit Singh 0:9db3bed8fffd 65 void
Rajit Singh 0:9db3bed8fffd 66 vMBPortSerialEnable( BOOL xRxEnable, BOOL xTxEnable )
Rajit Singh 0:9db3bed8fffd 67 {
Rajit Singh 0:9db3bed8fffd 68 /* If xRXEnable enable serial receive interrupts. If xTxENable enable
Rajit Singh 0:9db3bed8fffd 69 * transmitter empty interrupts.
Rajit Singh 0:9db3bed8fffd 70 */
Rajit Singh 0:9db3bed8fffd 71 RxEnable = xRxEnable;
Rajit Singh 0:9db3bed8fffd 72 TxEnable = xTxEnable;
Rajit Singh 0:9db3bed8fffd 73 }
Rajit Singh 0:9db3bed8fffd 74
Rajit Singh 0:9db3bed8fffd 75 BOOL
Rajit Singh 0:9db3bed8fffd 76 xMBPortSerialInit( UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits, eMBParity eParity )
Rajit Singh 0:9db3bed8fffd 77 {
Rajit Singh 0:9db3bed8fffd 78 simISR.attach_us(&prvvUARTISR,1000); // Cam - attach prvvUARTISR to a 1mS ticker to simulate serial interrupt behaviour
Rajit Singh 0:9db3bed8fffd 79 // 1mS is just short of a character time at 9600 bps, so quick enough to pick
Rajit Singh 0:9db3bed8fffd 80 // up status on a character by character basis.
Rajit Singh 0:9db3bed8fffd 81 return TRUE;
Rajit Singh 0:9db3bed8fffd 82 }
Rajit Singh 0:9db3bed8fffd 83
Rajit Singh 0:9db3bed8fffd 84 BOOL
Rajit Singh 0:9db3bed8fffd 85 xMBPortSerialPutByte( CHAR ucByte )
Rajit Singh 0:9db3bed8fffd 86 {
Rajit Singh 0:9db3bed8fffd 87 /* Put a byte in the UARTs transmit buffer. This function is called
Rajit Singh 0:9db3bed8fffd 88 * by the protocol stack if pxMBFrameCBTransmitterEmpty( ) has been
Rajit Singh 0:9db3bed8fffd 89 * called. */
Rajit Singh 0:9db3bed8fffd 90 pc.putc( ucByte);
Rajit Singh 0:9db3bed8fffd 91 return TRUE;
Rajit Singh 0:9db3bed8fffd 92 }
Rajit Singh 0:9db3bed8fffd 93
Rajit Singh 0:9db3bed8fffd 94 BOOL
Rajit Singh 0:9db3bed8fffd 95 xMBPortSerialGetByte( CHAR * pucByte )
Rajit Singh 0:9db3bed8fffd 96 {
Rajit Singh 0:9db3bed8fffd 97 /* Return the byte in the UARTs receive buffer. This function is called
Rajit Singh 0:9db3bed8fffd 98 * by the protocol stack after pxMBFrameCBByteReceived( ) has been called.
Rajit Singh 0:9db3bed8fffd 99 */
Rajit Singh 0:9db3bed8fffd 100 * pucByte = pc.getc();
Rajit Singh 0:9db3bed8fffd 101 return TRUE;
Rajit Singh 0:9db3bed8fffd 102 }
Rajit Singh 0:9db3bed8fffd 103
Rajit Singh 0:9db3bed8fffd 104 /* Create an interrupt handler for the transmit buffer empty interrupt
Rajit Singh 0:9db3bed8fffd 105 * (or an equivalent) for your target processor. This function should then
Rajit Singh 0:9db3bed8fffd 106 * call pxMBFrameCBTransmitterEmpty( ) which tells the protocol stack that
Rajit Singh 0:9db3bed8fffd 107 * a new character can be sent. The protocol stack will then call
Rajit Singh 0:9db3bed8fffd 108 * xMBPortSerialPutByte( ) to send the character.
Rajit Singh 0:9db3bed8fffd 109 */
Rajit Singh 0:9db3bed8fffd 110 static void prvvUARTTxReadyISR( void )
Rajit Singh 0:9db3bed8fffd 111 {
Rajit Singh 0:9db3bed8fffd 112 pxMBFrameCBTransmitterEmpty( );
Rajit Singh 0:9db3bed8fffd 113 }
Rajit Singh 0:9db3bed8fffd 114
Rajit Singh 0:9db3bed8fffd 115 /* Create an interrupt handler for the receive interrupt for your target
Rajit Singh 0:9db3bed8fffd 116 * processor. This function should then call pxMBFrameCBByteReceived( ). The
Rajit Singh 0:9db3bed8fffd 117 * protocol stack will then call xMBPortSerialGetByte( ) to retrieve the
Rajit Singh 0:9db3bed8fffd 118 * character.
Rajit Singh 0:9db3bed8fffd 119 */
Rajit Singh 0:9db3bed8fffd 120 static void prvvUARTRxISR( void )
Rajit Singh 0:9db3bed8fffd 121 {
Rajit Singh 0:9db3bed8fffd 122 pxMBFrameCBByteReceived( );
Rajit Singh 0:9db3bed8fffd 123 }
Rajit Singh 0:9db3bed8fffd 124
Rajit Singh 0:9db3bed8fffd 125