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