Modbus RTU/ASCII/TCP with lwip TCP working partial, but with errors (retransmitions)

Dependencies:   EthernetNetIf mbed

Committer:
tmav123
Date:
Mon Dec 05 22:49:02 2011 +0000
Revision:
0:f54e9507171b

        

Who changed what in which revision?

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