Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: NuMaker-mbed-modbus-sample NuMaker_NuWicam_Lite NuMaker-mbed-modbus-sample
Fork of Modbus by
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 #if MBED_MAJOR_VERSION >= 6 00032 #include "MyUnbufferedSerial.h" 00033 #endif 00034 00035 /* ----------------------- static functions ---------------------------------*/ 00036 static void prvvUARTTxReadyISR( void ); 00037 static void prvvUARTRxISR( void ); 00038 static void prvvUARTISR( void ); 00039 00040 /* ----------------------- System Variables ---------------------------------*/ 00041 #if defined(MBED_CONF_APP_DEF_RS485_PORT) && (MBED_CONF_APP_DEF_RS485_PORT)// mbed serial port 00042 #include "nvt_rs485.h" 00043 // RS485 TX, RX, RTS pins 00044 #if defined(TARGET_NUMAKER_PFM_NUC472) // for NUC472 board 00045 NvtRS485 pc(PF_13, PF_14, PF_11); 00046 #elif defined(TARGET_NUMAKER_PFM_M453) // for M453 board 00047 NvtRS485 pc(PE_8, PE_9, PE_11); 00048 #elif defined(TARGET_NUMAKER_PFM_M487) || defined(TARGET_NUMAKER_IOT_M487) // for M487 board 00049 NvtRS485 pc(PB_3, PB_2, PB_8); 00050 #warning "Notice: It has no RS485 port on NUMAKER-PFM-M487 board." 00051 #warning "But, you can connect with a RS485 daughter board to (D1, D0, A2) PB_3(TX), PB_2(RX) and PB_8(RTS) pin." 00052 #elif defined(TARGET_NUMAKER_IOT_M467) // for M467 board 00053 NvtRS485 pc(PB_3, PB_2, PB_8); 00054 #warning "Notice: It has no RS485 port on NUMAKER-IOT-M467 board." 00055 #warning "But, you can connect with a RS485 daughter board to (D1, D0, A2) PB_3(TX), PB_2(RX) and PB_8(RTS) pin." 00056 #else 00057 #error "The demo code can't be executed on this board." 00058 #endif 00059 #else 00060 //UART TX, RX 00061 #if defined(TARGET_NUMAKER_PFM_NUC472) // for NUC472 board 00062 Serial pc(PG_2, PG_1); 00063 #elif defined(TARGET_NUMAKER_PFM_M453) // for M453 board 00064 Serial pc(PD_1, PD_6); 00065 #elif defined(TARGET_NUMAKER_PFM_M487) // for M487 board 00066 Serial pc(PC_12, PC_11); 00067 #elif defined(TARGET_NUMAKER_IOT_M467) // for M467 board 00068 Serial pc(PC_12, PC_11); 00069 #else 00070 #error "The demo code can't be executed on this board." 00071 #endif 00072 #endif 00073 00074 static volatile BOOL RxEnable, TxEnable; // Cam - keep a static copy of the RxEnable and TxEnable 00075 // status for the simulated ISR (ticker) 00076 00077 00078 /* ----------------------- Start implementation -----------------------------*/ 00079 // Cam - This is called every 1mS to simulate Rx character received ISR and 00080 // Tx buffer empty ISR. 00081 static void 00082 prvvUARTISR( void ) 00083 { 00084 if ( TxEnable ) 00085 if(pc.writeable()) 00086 prvvUARTTxReadyISR(); 00087 00088 if ( RxEnable ) 00089 if(pc.readable()) 00090 prvvUARTRxISR(); 00091 } 00092 00093 void 00094 vMBPortSerialEnable( BOOL xRxEnable, BOOL xTxEnable ) 00095 { 00096 /* If xRXEnable enable serial receive interrupts. If xTxENable enable 00097 * transmitter empty interrupts. 00098 */ 00099 RxEnable = xRxEnable; 00100 TxEnable = xTxEnable; 00101 00102 //printf("\r\nRx: %d, TX:%d\r\n", RxEnable, TxEnable); 00103 } 00104 00105 /* ----------------------- Start implementation -----------------------------*/ 00106 BOOL 00107 xMBPortSerialInit( UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits, eMBParity eParity ) 00108 { 00109 pc.baud(ulBaudRate); 00110 #if defined(MBED_CONF_APP_DEF_RS485_PORT) && (MBED_CONF_APP_DEF_RS485_PORT) // mbed serial port 00111 #if defined(TARGET_NUMAKER_PFM_NUC472) // for NUC472 board 00112 pc.set_rs485_mode(PF_11); 00113 #elif defined(TARGET_NUMAKER_PFM_M453) // for M453 board 00114 pc.set_rs485_mode(PE_11); 00115 #elif defined(TARGET_NUMAKER_PFM_M487) // for M487 board 00116 pc.set_rs485_mode(PB_8); 00117 #elif defined(TARGET_NUMAKER_IOT_M467) // for M467 board 00118 pc.set_rs485_mode(PB_8); 00119 #endif 00120 #endif 00121 //#if 0 //MBED_MAJOR_VERSION >= 6 00122 SerialBase::Parity parity; 00123 if (eParity == MB_PAR_NONE ) 00124 parity = SerialBase::None; 00125 else if ( eParity == MB_PAR_ODD ) 00126 parity = SerialBase::Odd; 00127 else if ( eParity == MB_PAR_EVEN ) 00128 parity = SerialBase::Even; 00129 pc.format( 00130 /* bits */ 8, 00131 /* parity */ parity, 00132 /* stop bit */ 1 00133 ); 00134 //#endif 00135 return TRUE; 00136 } 00137 00138 void xMBPortSerialPolling( void ) 00139 { 00140 prvvUARTISR( ); 00141 } 00142 00143 BOOL 00144 xMBPortSerialPutByte( CHAR ucByte ) 00145 { 00146 /* Put a byte in the UARTs transmit buffer. This function is called 00147 * by the protocol stack if pxMBFrameCBTransmitterEmpty( ) has been 00148 * called. */ 00149 //printf("[%02x]", ucByte ); 00150 #if MBED_MAJOR_VERSION >= 6 00151 pc.write(&ucByte, 1); 00152 #else 00153 pc.putc( ucByte); 00154 #endif 00155 return TRUE; 00156 } 00157 00158 BOOL 00159 xMBPortSerialGetByte( CHAR * pucByte ) 00160 { 00161 /* Return the byte in the UARTs receive buffer. This function is called 00162 * by the protocol stack after pxMBFrameCBByteReceived( ) has been called. 00163 */ 00164 #if MBED_MAJOR_VERSION >= 6 00165 pc.read(pucByte,1); 00166 #else 00167 *pucByte = pc.getc(); 00168 #endif 00169 //printf("<%02x>", *pucByte ); 00170 return TRUE; 00171 } 00172 00173 /* Create an interrupt handler for the transmit buffer empty interrupt 00174 * (or an equivalent) for your target processor. This function should then 00175 * call pxMBFrameCBTransmitterEmpty( ) which tells the protocol stack that 00176 * a new character can be sent. The protocol stack will then call 00177 * xMBPortSerialPutByte( ) to send the character. 00178 */ 00179 static void prvvUARTTxReadyISR( void ) 00180 { 00181 pxMBFrameCBTransmitterEmpty( ); 00182 } 00183 00184 /* Create an interrupt handler for the receive interrupt for your target 00185 * processor. This function should then call pxMBFrameCBByteReceived( ). The 00186 * protocol stack will then call xMBPortSerialGetByte( ) to retrieve the 00187 * character. 00188 */ 00189 static void prvvUARTRxISR( void ) 00190 { 00191 vMBPortTimersDisable(); 00192 pxMBFrameCBByteReceived( ); 00193 } 00194 00195
Generated on Mon May 13 2024 08:55:19 by
1.7.2
