hsu han-lin
/
Modbus_Test
from Cam Marshall original modbus
Fork of Modbus by
Modbus/portserial.cpp@5:c712a10c9414, 2017-03-14 (annotated)
- Committer:
- stanley1228
- Date:
- Tue Mar 14 22:23:17 2017 +0800
- Revision:
- 5:c712a10c9414
- Parent:
- 4:5000041d2dc2
1.change baud rate to 115200
2.add define enter critical section (__disable_Irq)
3.add define exit critical section (__enable_Irq)
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 ); |
stanley1228 | 5:c712a10c9414 | 36 | |
cam | 0:0453a0a7e500 | 37 | |
cam | 0:0453a0a7e500 | 38 | /* ----------------------- System Variables ---------------------------------*/ |
stanley1228 | 4:5000041d2dc2 | 39 | Serial ser3(PB_10, PC_5); //serial3 |
stanley1228 | 2:5f360876388e | 40 | extern Serial pc; |
stanley1228 | 4:5000041d2dc2 | 41 | static uint8_t grx_buf=0; |
stanley1228 | 4:5000041d2dc2 | 42 | |
cam | 0:0453a0a7e500 | 43 | /* ----------------------- Start implementation -----------------------------*/ |
cam | 0:0453a0a7e500 | 44 | void |
cam | 0:0453a0a7e500 | 45 | vMBPortSerialEnable( BOOL xRxEnable, BOOL xTxEnable ) |
cam | 0:0453a0a7e500 | 46 | { |
cam | 0:0453a0a7e500 | 47 | /* If xRXEnable enable serial receive interrupts. If xTxENable enable |
cam | 0:0453a0a7e500 | 48 | * transmitter empty interrupts. |
cam | 0:0453a0a7e500 | 49 | */ |
stanley1228 | 5:c712a10c9414 | 50 | |
stanley1228 | 2:5f360876388e | 51 | |
stanley1228 | 2:5f360876388e | 52 | //stanley |
stanley1228 | 5:c712a10c9414 | 53 | ENTER_CRITICAL_SECTION( ); |
stanley1228 | 5:c712a10c9414 | 54 | |
stanley1228 | 4:5000041d2dc2 | 55 | |
stanley1228 | 4:5000041d2dc2 | 56 | ser3.attach(NULL); //close all |
stanley1228 | 4:5000041d2dc2 | 57 | |
stanley1228 | 2:5f360876388e | 58 | if(xRxEnable) |
stanley1228 | 2:5f360876388e | 59 | ser3.attach(&prvvUARTRxISR,Serial::RxIrq); |
stanley1228 | 2:5f360876388e | 60 | |
stanley1228 | 4:5000041d2dc2 | 61 | if(xTxEnable) |
stanley1228 | 4:5000041d2dc2 | 62 | { |
stanley1228 | 2:5f360876388e | 63 | ser3.attach(&prvvUARTTxReadyISR,Serial::TxIrq); |
stanley1228 | 4:5000041d2dc2 | 64 | while(!ser3.writeable()); |
stanley1228 | 4:5000041d2dc2 | 65 | prvvUARTTxReadyISR(); |
stanley1228 | 4:5000041d2dc2 | 66 | } |
stanley1228 | 5:c712a10c9414 | 67 | EXIT_CRITICAL_SECTION( ); |
stanley1228 | 4:5000041d2dc2 | 68 | |
stanley1228 | 2:5f360876388e | 69 | //if(xTxEnable) |
stanley1228 | 2:5f360876388e | 70 | //¤£¥Î¦b³oÃä³]©wser3.write(tx_buf,TX_BUFF_LENGTH,serialTxCBEvent,SERIAL_EVENT_TX_COMPLETE); |
stanley1228 | 4:5000041d2dc2 | 71 | |
stanley1228 | 4:5000041d2dc2 | 72 | //else |
stanley1228 | 4:5000041d2dc2 | 73 | //{ |
stanley1228 | 4:5000041d2dc2 | 74 | // ser3.attach(NULL,Serial::RxIrq); //stanley the test result find that this may disable RxIrq and TxIrq so have to enable TxIrq |
stanley1228 | 4:5000041d2dc2 | 75 | // ser3.attach(&prvvUARTTxReadyISR,Serial::TxIrq); |
stanley1228 | 4:5000041d2dc2 | 76 | //} |
cam | 0:0453a0a7e500 | 77 | } |
cam | 0:0453a0a7e500 | 78 | |
cam | 0:0453a0a7e500 | 79 | BOOL |
cam | 0:0453a0a7e500 | 80 | xMBPortSerialInit( UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits, eMBParity eParity ) |
cam | 0:0453a0a7e500 | 81 | { |
stanley1228 | 2:5f360876388e | 82 | |
stanley1228 | 5:c712a10c9414 | 83 | ser3.baud(ulBaudRate); //stanley |
stanley1228 | 5:c712a10c9414 | 84 | |
stanley1228 | 2:5f360876388e | 85 | |
stanley1228 | 2:5f360876388e | 86 | return TRUE; |
cam | 0:0453a0a7e500 | 87 | } |
cam | 0:0453a0a7e500 | 88 | |
cam | 0:0453a0a7e500 | 89 | BOOL |
cam | 0:0453a0a7e500 | 90 | xMBPortSerialPutByte( CHAR ucByte ) |
cam | 0:0453a0a7e500 | 91 | { |
cam | 0:0453a0a7e500 | 92 | /* Put a byte in the UARTs transmit buffer. This function is called |
cam | 0:0453a0a7e500 | 93 | * by the protocol stack if pxMBFrameCBTransmitterEmpty( ) has been |
cam | 0:0453a0a7e500 | 94 | * called. */ |
stanley1228 | 4:5000041d2dc2 | 95 | |
stanley1228 | 2:5f360876388e | 96 | //stanley |
stanley1228 | 2:5f360876388e | 97 | ser3.putc( ucByte); |
stanley1228 | 2:5f360876388e | 98 | |
cam | 0:0453a0a7e500 | 99 | return TRUE; |
cam | 0:0453a0a7e500 | 100 | } |
cam | 0:0453a0a7e500 | 101 | |
cam | 0:0453a0a7e500 | 102 | BOOL |
cam | 0:0453a0a7e500 | 103 | xMBPortSerialGetByte( CHAR * pucByte ) |
cam | 0:0453a0a7e500 | 104 | { |
cam | 0:0453a0a7e500 | 105 | /* Return the byte in the UARTs receive buffer. This function is called |
cam | 0:0453a0a7e500 | 106 | * by the protocol stack after pxMBFrameCBByteReceived( ) has been called. |
cam | 0:0453a0a7e500 | 107 | */ |
stanley1228 | 4:5000041d2dc2 | 108 | |
stanley1228 | 4:5000041d2dc2 | 109 | *pucByte = grx_buf;//stanley |
stanley1228 | 2:5f360876388e | 110 | |
stanley1228 | 2:5f360876388e | 111 | |
stanley1228 | 2:5f360876388e | 112 | |
cam | 0:0453a0a7e500 | 113 | return TRUE; |
cam | 0:0453a0a7e500 | 114 | } |
cam | 0:0453a0a7e500 | 115 | |
cam | 0:0453a0a7e500 | 116 | /* Create an interrupt handler for the transmit buffer empty interrupt |
cam | 0:0453a0a7e500 | 117 | * (or an equivalent) for your target processor. This function should then |
cam | 0:0453a0a7e500 | 118 | * call pxMBFrameCBTransmitterEmpty( ) which tells the protocol stack that |
cam | 0:0453a0a7e500 | 119 | * a new character can be sent. The protocol stack will then call |
cam | 0:0453a0a7e500 | 120 | * xMBPortSerialPutByte( ) to send the character. |
cam | 0:0453a0a7e500 | 121 | */ |
cam | 0:0453a0a7e500 | 122 | static void prvvUARTTxReadyISR( void ) |
cam | 0:0453a0a7e500 | 123 | { |
stanley1228 | 2:5f360876388e | 124 | pxMBFrameCBTransmitterEmpty(); |
cam | 0:0453a0a7e500 | 125 | } |
cam | 0:0453a0a7e500 | 126 | |
cam | 0:0453a0a7e500 | 127 | /* Create an interrupt handler for the receive interrupt for your target |
cam | 0:0453a0a7e500 | 128 | * processor. This function should then call pxMBFrameCBByteReceived( ). The |
cam | 0:0453a0a7e500 | 129 | * protocol stack will then call xMBPortSerialGetByte( ) to retrieve the |
cam | 0:0453a0a7e500 | 130 | * character. |
cam | 0:0453a0a7e500 | 131 | */ |
cam | 0:0453a0a7e500 | 132 | static void prvvUARTRxISR( void ) |
cam | 0:0453a0a7e500 | 133 | { |
stanley1228 | 4:5000041d2dc2 | 134 | if(ser3.readable()) //stanlely RX ISR must contain getc |
stanley1228 | 4:5000041d2dc2 | 135 | grx_buf=ser3.getc(); |
stanley1228 | 4:5000041d2dc2 | 136 | |
stanley1228 | 2:5f360876388e | 137 | pxMBFrameCBByteReceived(); |
cam | 0:0453a0a7e500 | 138 | } |
cam | 0:0453a0a7e500 | 139 | |
cam | 0:0453a0a7e500 | 140 |