Protocole MB

Dependents:   TP3_RBT_2GII_Ex1

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers portserial.cpp Source File

portserial.cpp

00001 /*
00002  * FreeModbus Libary: BARE Port
00003  * Copyright (C) 2006 Christian Walter <wolti@sil.at>
00004  *
00005  * This library is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU Lesser General Public
00007  * License as published by the Free Software Foundation; either
00008  * version 2.1 of the License, or (at your option) any later version.
00009  *
00010  * This library is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Lesser General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Lesser General Public
00016  * License along with this library; if not, write to the Free Software
00017  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018  *
00019  * File: $Id: portserial.c,v 1.1 2006/08/22 21:35:13 wolti Exp $
00020  */
00021 
00022 /* ----------------------- System includes ----------------------------------*/
00023 #include "mbed.h"                   // Cam
00024 
00025 /* ----------------------- Platform includes --------------------------------*/
00026 #include "port.h"
00027 
00028 /* ----------------------- Modbus includes ----------------------------------*/
00029 #include "mb.h"
00030 #include "mbport.h"
00031 
00032 
00033 /* ----------------------- static functions ---------------------------------*/
00034 static void prvvUARTTxReadyISR( void );
00035 static void prvvUARTRxISR( void );
00036 
00037 
00038 /* ----------------------- System Variables ---------------------------------*/
00039 Serial ser3(PB_6, PB_7); //serial3
00040 extern Serial pc;
00041 static uint8_t grx_buf=0;
00042 
00043 /* ----------------------- Start implementation -----------------------------*/
00044 void
00045 vMBPortSerialEnable( BOOL xRxEnable, BOOL xTxEnable )
00046 {
00047     /* If xRXEnable enable serial receive interrupts. If xTxENable enable
00048      * transmitter empty interrupts.
00049      */
00050    
00051 
00052     //stanley
00053     ENTER_CRITICAL_SECTION( );
00054     
00055 
00056     ser3.attach(NULL); //close all
00057     
00058     if(xRxEnable)
00059         ser3.attach(&prvvUARTRxISR,Serial::RxIrq);
00060     
00061     if(xTxEnable)
00062     {
00063         ser3.attach(&prvvUARTTxReadyISR,Serial::TxIrq);
00064         while(!ser3.writeable());
00065         prvvUARTTxReadyISR();
00066     }       
00067     EXIT_CRITICAL_SECTION( );
00068 
00069     //if(xTxEnable)
00070         //���Φb�o��]�wser3.write(tx_buf,TX_BUFF_LENGTH,serialTxCBEvent,SERIAL_EVENT_TX_COMPLETE);
00071 
00072     //else
00073     //{
00074     //  ser3.attach(NULL,Serial::RxIrq);  //stanley  the test result find that this may disable RxIrq and TxIrq so have to enable TxIrq
00075     //  ser3.attach(&prvvUARTTxReadyISR,Serial::TxIrq);  
00076     //}
00077 }
00078 
00079 BOOL
00080 xMBPortSerialInit( UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits, eMBParity eParity )
00081 {
00082    
00083     ser3.baud(ulBaudRate); //stanley
00084     
00085 
00086     return TRUE;
00087 }
00088 
00089 BOOL
00090 xMBPortSerialPutByte( CHAR ucByte )
00091 {
00092     /* Put a byte in the UARTs transmit buffer. This function is called
00093      * by the protocol stack if pxMBFrameCBTransmitterEmpty( ) has been
00094      * called. */
00095 
00096     //stanley
00097     ser3.putc( ucByte);
00098 
00099     return TRUE;
00100 }
00101 
00102 BOOL
00103 xMBPortSerialGetByte( CHAR * pucByte )
00104 {
00105     /* Return the byte in the UARTs receive buffer. This function is called
00106      * by the protocol stack after pxMBFrameCBByteReceived( ) has been called.
00107      */
00108     
00109     *pucByte = grx_buf;//stanley
00110 
00111     
00112 
00113     return TRUE;
00114 }
00115 
00116 /* Create an interrupt handler for the transmit buffer empty interrupt
00117  * (or an equivalent) for your target processor. This function should then
00118  * call pxMBFrameCBTransmitterEmpty( ) which tells the protocol stack that
00119  * a new character can be sent. The protocol stack will then call 
00120  * xMBPortSerialPutByte( ) to send the character.
00121  */
00122 static void prvvUARTTxReadyISR( void )
00123 {
00124     pxMBFrameCBTransmitterEmpty();
00125 }
00126 
00127 /* Create an interrupt handler for the receive interrupt for your target
00128  * processor. This function should then call pxMBFrameCBByteReceived( ). The
00129  * protocol stack will then call xMBPortSerialGetByte( ) to retrieve the
00130  * character.
00131  */
00132 static void prvvUARTRxISR( void )
00133 {
00134     if(ser3.readable()) //stanlely RX ISR must contain getc
00135         grx_buf=ser3.getc();
00136 
00137     pxMBFrameCBByteReceived();
00138 }
00139 
00140