Miguel Alarcón Ortiz
/
IoTPFC
Proyecto publicado para compartir
Fork of Modbus by
portserial.cpp@1:ca1df3dffd0f, 2015-10-15 (annotated)
- Committer:
- alarcon
- Date:
- Thu Oct 15 18:04:00 2015 +0000
- Revision:
- 1:ca1df3dffd0f
- Parent:
- 0:0453a0a7e500
modificaci?n de la lib modbus para utilizar cualquier puerto cambiando los par?metros RX_MODBUS_PORT y TX_MODBUS_PORT en el archivo mb.h
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 ---------------------------------*/ |
alarcon | 1:ca1df3dffd0f | 39 | /////////////////////////////////////////////////////////////////////////////// |
alarcon | 1:ca1df3dffd0f | 40 | //Serial pc(USBTX, USBRX); // Cam - mbed USB serial port |
alarcon | 1:ca1df3dffd0f | 41 | /////////////////////////////////////////////////////////////////////////////// |
alarcon | 1:ca1df3dffd0f | 42 | //modication to select any port changing the define parameters TX_MODBUS_PORT and RX_MODBUS_PORT into the file "md.h" |
alarcon | 1:ca1df3dffd0f | 43 | Serial pc(TX_MODBUS_PORT,RX_MODBUS_PORT); |
cam | 0:0453a0a7e500 | 44 | Ticker simISR; // Cam - mbed ticker |
cam | 0:0453a0a7e500 | 45 | // we don't have the TX buff empty interrupt, so |
cam | 0:0453a0a7e500 | 46 | // we just interrupt every 1 mSec and read RX & TX |
cam | 0:0453a0a7e500 | 47 | // status to simulate the proper ISRs. |
cam | 0:0453a0a7e500 | 48 | |
cam | 0:0453a0a7e500 | 49 | static BOOL RxEnable, TxEnable; // Cam - keep a static copy of the RxEnable and TxEnable |
cam | 0:0453a0a7e500 | 50 | // status for the simulated ISR (ticker) |
cam | 0:0453a0a7e500 | 51 | |
cam | 0:0453a0a7e500 | 52 | |
cam | 0:0453a0a7e500 | 53 | /* ----------------------- Start implementation -----------------------------*/ |
cam | 0:0453a0a7e500 | 54 | // Cam - This is called every 1mS to simulate Rx character received ISR and |
cam | 0:0453a0a7e500 | 55 | // Tx buffer empty ISR. |
cam | 0:0453a0a7e500 | 56 | static void |
cam | 0:0453a0a7e500 | 57 | prvvUARTISR( void ) |
cam | 0:0453a0a7e500 | 58 | { |
cam | 0:0453a0a7e500 | 59 | if (TxEnable) |
cam | 0:0453a0a7e500 | 60 | if(pc.writeable()) |
cam | 0:0453a0a7e500 | 61 | prvvUARTTxReadyISR(); |
cam | 0:0453a0a7e500 | 62 | |
cam | 0:0453a0a7e500 | 63 | if (RxEnable) |
cam | 0:0453a0a7e500 | 64 | if(pc.readable()) |
cam | 0:0453a0a7e500 | 65 | prvvUARTRxISR(); |
cam | 0:0453a0a7e500 | 66 | } |
cam | 0:0453a0a7e500 | 67 | |
cam | 0:0453a0a7e500 | 68 | void |
cam | 0:0453a0a7e500 | 69 | vMBPortSerialEnable( BOOL xRxEnable, BOOL xTxEnable ) |
cam | 0:0453a0a7e500 | 70 | { |
cam | 0:0453a0a7e500 | 71 | /* If xRXEnable enable serial receive interrupts. If xTxENable enable |
cam | 0:0453a0a7e500 | 72 | * transmitter empty interrupts. |
cam | 0:0453a0a7e500 | 73 | */ |
cam | 0:0453a0a7e500 | 74 | RxEnable = xRxEnable; |
cam | 0:0453a0a7e500 | 75 | TxEnable = xTxEnable; |
cam | 0:0453a0a7e500 | 76 | } |
cam | 0:0453a0a7e500 | 77 | |
cam | 0:0453a0a7e500 | 78 | BOOL |
cam | 0:0453a0a7e500 | 79 | xMBPortSerialInit( UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits, eMBParity eParity ) |
cam | 0:0453a0a7e500 | 80 | { |
cam | 0:0453a0a7e500 | 81 | simISR.attach_us(&prvvUARTISR,1000); // Cam - attach prvvUARTISR to a 1mS ticker to simulate serial interrupt behaviour |
cam | 0:0453a0a7e500 | 82 | // 1mS is just short of a character time at 9600 bps, so quick enough to pick |
cam | 0:0453a0a7e500 | 83 | // up status on a character by character basis. |
cam | 0:0453a0a7e500 | 84 | return TRUE; |
cam | 0:0453a0a7e500 | 85 | } |
cam | 0:0453a0a7e500 | 86 | |
cam | 0:0453a0a7e500 | 87 | BOOL |
cam | 0:0453a0a7e500 | 88 | xMBPortSerialPutByte( CHAR ucByte ) |
cam | 0:0453a0a7e500 | 89 | { |
cam | 0:0453a0a7e500 | 90 | /* Put a byte in the UARTs transmit buffer. This function is called |
cam | 0:0453a0a7e500 | 91 | * by the protocol stack if pxMBFrameCBTransmitterEmpty( ) has been |
cam | 0:0453a0a7e500 | 92 | * called. */ |
cam | 0:0453a0a7e500 | 93 | pc.putc( ucByte); |
cam | 0:0453a0a7e500 | 94 | return TRUE; |
cam | 0:0453a0a7e500 | 95 | } |
cam | 0:0453a0a7e500 | 96 | |
cam | 0:0453a0a7e500 | 97 | BOOL |
cam | 0:0453a0a7e500 | 98 | xMBPortSerialGetByte( CHAR * pucByte ) |
cam | 0:0453a0a7e500 | 99 | { |
cam | 0:0453a0a7e500 | 100 | /* Return the byte in the UARTs receive buffer. This function is called |
cam | 0:0453a0a7e500 | 101 | * by the protocol stack after pxMBFrameCBByteReceived( ) has been called. |
cam | 0:0453a0a7e500 | 102 | */ |
cam | 0:0453a0a7e500 | 103 | * pucByte = pc.getc(); |
cam | 0:0453a0a7e500 | 104 | return TRUE; |
cam | 0:0453a0a7e500 | 105 | } |
cam | 0:0453a0a7e500 | 106 | |
cam | 0:0453a0a7e500 | 107 | /* Create an interrupt handler for the transmit buffer empty interrupt |
cam | 0:0453a0a7e500 | 108 | * (or an equivalent) for your target processor. This function should then |
cam | 0:0453a0a7e500 | 109 | * call pxMBFrameCBTransmitterEmpty( ) which tells the protocol stack that |
cam | 0:0453a0a7e500 | 110 | * a new character can be sent. The protocol stack will then call |
cam | 0:0453a0a7e500 | 111 | * xMBPortSerialPutByte( ) to send the character. |
cam | 0:0453a0a7e500 | 112 | */ |
cam | 0:0453a0a7e500 | 113 | static void prvvUARTTxReadyISR( void ) |
cam | 0:0453a0a7e500 | 114 | { |
cam | 0:0453a0a7e500 | 115 | pxMBFrameCBTransmitterEmpty( ); |
cam | 0:0453a0a7e500 | 116 | } |
cam | 0:0453a0a7e500 | 117 | |
cam | 0:0453a0a7e500 | 118 | /* Create an interrupt handler for the receive interrupt for your target |
cam | 0:0453a0a7e500 | 119 | * processor. This function should then call pxMBFrameCBByteReceived( ). The |
cam | 0:0453a0a7e500 | 120 | * protocol stack will then call xMBPortSerialGetByte( ) to retrieve the |
cam | 0:0453a0a7e500 | 121 | * character. |
cam | 0:0453a0a7e500 | 122 | */ |
cam | 0:0453a0a7e500 | 123 | static void prvvUARTRxISR( void ) |
cam | 0:0453a0a7e500 | 124 | { |
cam | 0:0453a0a7e500 | 125 | pxMBFrameCBByteReceived( ); |
cam | 0:0453a0a7e500 | 126 | } |
cam | 0:0453a0a7e500 | 127 | |
cam | 0:0453a0a7e500 | 128 |