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