modified for NuMaker_PFM series

Dependents:   NuMaker-mbed-modbus-sample NuMaker_NuWicam_Lite NuMaker-mbed-modbus-sample

Fork of Modbus by Wayne Lin

Committer:
wclin
Date:
Wed Jan 04 08:31:29 2017 +0000
Revision:
6:08849dd9654d
Parent:
5:d4c3b20819a7
Child:
7:abafdf714896
Support NuMaker-PFM-M453 board

Who changed what in which revision?

UserRevisionLine numberNew contents of line
giryan 0:274eb57e1df3 1 /*
giryan 0:274eb57e1df3 2 * FreeModbus Libary: BARE Port
giryan 0:274eb57e1df3 3 * Copyright (C) 2006 Christian Walter <wolti@sil.at>
giryan 0:274eb57e1df3 4 *
giryan 0:274eb57e1df3 5 * This library is free software; you can redistribute it and/or
giryan 0:274eb57e1df3 6 * modify it under the terms of the GNU Lesser General Public
giryan 0:274eb57e1df3 7 * License as published by the Free Software Foundation; either
giryan 0:274eb57e1df3 8 * version 2.1 of the License, or (at your option) any later version.
giryan 0:274eb57e1df3 9 *
giryan 0:274eb57e1df3 10 * This library is distributed in the hope that it will be useful,
giryan 0:274eb57e1df3 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
giryan 0:274eb57e1df3 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
giryan 0:274eb57e1df3 13 * Lesser General Public License for more details.
giryan 0:274eb57e1df3 14 *
giryan 0:274eb57e1df3 15 * You should have received a copy of the GNU Lesser General Public
giryan 0:274eb57e1df3 16 * License along with this library; if not, write to the Free Software
giryan 0:274eb57e1df3 17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
giryan 0:274eb57e1df3 18 *
giryan 0:274eb57e1df3 19 * File: $Id: portserial.c,v 1.1 2006/08/22 21:35:13 wolti Exp $
giryan 0:274eb57e1df3 20 */
giryan 0:274eb57e1df3 21
giryan 0:274eb57e1df3 22 /* ----------------------- System includes ----------------------------------*/
giryan 0:274eb57e1df3 23 #include "mbed.h" // Cam
giryan 0:274eb57e1df3 24
giryan 0:274eb57e1df3 25 /* ----------------------- Platform includes --------------------------------*/
giryan 0:274eb57e1df3 26 #include "port.h"
giryan 0:274eb57e1df3 27
giryan 0:274eb57e1df3 28 /* ----------------------- Modbus includes ----------------------------------*/
giryan 0:274eb57e1df3 29 #include "mb.h"
giryan 0:274eb57e1df3 30 #include "mbport.h"
giryan 0:274eb57e1df3 31
giryan 0:274eb57e1df3 32
giryan 0:274eb57e1df3 33 /* ----------------------- static functions ---------------------------------*/
giryan 0:274eb57e1df3 34 static void prvvUARTTxReadyISR( void );
giryan 0:274eb57e1df3 35 static void prvvUARTRxISR( void );
giryan 0:274eb57e1df3 36 static void prvvUARTISR( void );
giryan 0:274eb57e1df3 37
wclin 5:d4c3b20819a7 38 //#define DEF_RS485_PORT 1
giryan 0:274eb57e1df3 39 /* ----------------------- System Variables ---------------------------------*/
wclin 3:419ee4c5e10f 40
wclin 3:419ee4c5e10f 41 #if defined(DEF_RS485_PORT) // mbed serial port
wclin 6:08849dd9654d 42 #include "nvt_rs485.h"
wclin 6:08849dd9654d 43 // RS485 TX, RX, RTS pins
wclin 6:08849dd9654d 44 #if defined(TARGET_NUMAKER_PFM_NUC472) // for NUC472 board
wclin 6:08849dd9654d 45 NvtRS485 pc(PF_13, PF_14, PF_11);
wclin 6:08849dd9654d 46 #elif defined(TARGET_NUMAKER_PFM_M453) // for M453 board
wclin 6:08849dd9654d 47 NvtRS485 pc(PE_8, PE_9, PE_11);
wclin 6:08849dd9654d 48 #endif
wclin 3:419ee4c5e10f 49 #else
wclin 6:08849dd9654d 50 //UART TX, RX
wclin 6:08849dd9654d 51 #if defined(TARGET_NUMAKER_PFM_NUC472) // for NUC472 board
wclin 6:08849dd9654d 52 Serial pc(PG_2, PG_1);
wclin 6:08849dd9654d 53 #elif defined(TARGET_NUMAKER_PFM_M453) // for M453 board
wclin 6:08849dd9654d 54 Serial pc(PD_1, PD_6);
wclin 6:08849dd9654d 55 #endif
wclin 3:419ee4c5e10f 56 #endif
giryan 0:274eb57e1df3 57
wclin 2:6ee56c002f64 58 static volatile BOOL RxEnable, TxEnable; // Cam - keep a static copy of the RxEnable and TxEnable
giryan 0:274eb57e1df3 59 // status for the simulated ISR (ticker)
giryan 0:274eb57e1df3 60
giryan 0:274eb57e1df3 61
giryan 0:274eb57e1df3 62 /* ----------------------- Start implementation -----------------------------*/
giryan 0:274eb57e1df3 63 // Cam - This is called every 1mS to simulate Rx character received ISR and
giryan 0:274eb57e1df3 64 // Tx buffer empty ISR.
giryan 0:274eb57e1df3 65 static void
giryan 0:274eb57e1df3 66 prvvUARTISR( void )
giryan 0:274eb57e1df3 67 {
wclin 3:419ee4c5e10f 68 if ( TxEnable )
giryan 0:274eb57e1df3 69 if(pc.writeable())
giryan 0:274eb57e1df3 70 prvvUARTTxReadyISR();
wclin 3:419ee4c5e10f 71
wclin 3:419ee4c5e10f 72 if ( RxEnable )
giryan 0:274eb57e1df3 73 if(pc.readable())
wclin 3:419ee4c5e10f 74 prvvUARTRxISR();
giryan 0:274eb57e1df3 75 }
giryan 0:274eb57e1df3 76
giryan 0:274eb57e1df3 77 void
giryan 0:274eb57e1df3 78 vMBPortSerialEnable( BOOL xRxEnable, BOOL xTxEnable )
giryan 0:274eb57e1df3 79 {
giryan 0:274eb57e1df3 80 /* If xRXEnable enable serial receive interrupts. If xTxENable enable
giryan 0:274eb57e1df3 81 * transmitter empty interrupts.
giryan 0:274eb57e1df3 82 */
giryan 0:274eb57e1df3 83 RxEnable = xRxEnable;
giryan 0:274eb57e1df3 84 TxEnable = xTxEnable;
wclin 3:419ee4c5e10f 85
wclin 3:419ee4c5e10f 86 //printf("\r\nRx: %d, TX:%d\r\n", RxEnable, TxEnable);
giryan 0:274eb57e1df3 87 }
giryan 0:274eb57e1df3 88
wclin 1:cfde7320b0bf 89 /* ----------------------- Start implementation -----------------------------*/
giryan 0:274eb57e1df3 90 BOOL
giryan 0:274eb57e1df3 91 xMBPortSerialInit( UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits, eMBParity eParity )
giryan 0:274eb57e1df3 92 {
wclin 1:cfde7320b0bf 93 pc.baud(ulBaudRate);
wclin 3:419ee4c5e10f 94 #if defined(DEF_RS485_PORT) // mbed serial port
wclin 6:08849dd9654d 95 #if defined(TARGET_NUMAKER_PFM_NUC472) // for NUC472 board
wclin 3:419ee4c5e10f 96 pc.set_rs485_mode(PF_11);
wclin 6:08849dd9654d 97 #elif defined(TARGET_NUMAKER_PFM_M453) // for M453 board
wclin 6:08849dd9654d 98 pc.set_rs485_mode(PE_11);
wclin 6:08849dd9654d 99 #endif
wclin 3:419ee4c5e10f 100 #endif
wclin 1:cfde7320b0bf 101 return TRUE;
wclin 1:cfde7320b0bf 102 }
wclin 1:cfde7320b0bf 103
wclin 1:cfde7320b0bf 104 void xMBPortSerialPolling( void )
wclin 1:cfde7320b0bf 105 {
wclin 1:cfde7320b0bf 106 prvvUARTISR( );
wclin 1:cfde7320b0bf 107 }
wclin 1:cfde7320b0bf 108
giryan 0:274eb57e1df3 109 BOOL
giryan 0:274eb57e1df3 110 xMBPortSerialPutByte( CHAR ucByte )
giryan 0:274eb57e1df3 111 {
giryan 0:274eb57e1df3 112 /* Put a byte in the UARTs transmit buffer. This function is called
giryan 0:274eb57e1df3 113 * by the protocol stack if pxMBFrameCBTransmitterEmpty( ) has been
giryan 0:274eb57e1df3 114 * called. */
wclin 3:419ee4c5e10f 115 //printf("[%02x]", ucByte );
giryan 0:274eb57e1df3 116 pc.putc( ucByte);
giryan 0:274eb57e1df3 117 return TRUE;
giryan 0:274eb57e1df3 118 }
giryan 0:274eb57e1df3 119
giryan 0:274eb57e1df3 120 BOOL
giryan 0:274eb57e1df3 121 xMBPortSerialGetByte( CHAR * pucByte )
giryan 0:274eb57e1df3 122 {
giryan 0:274eb57e1df3 123 /* Return the byte in the UARTs receive buffer. This function is called
giryan 0:274eb57e1df3 124 * by the protocol stack after pxMBFrameCBByteReceived( ) has been called.
giryan 0:274eb57e1df3 125 */
wclin 3:419ee4c5e10f 126 *pucByte = pc.getc();
wclin 3:419ee4c5e10f 127 //printf("<%02x>", *pucByte );
giryan 0:274eb57e1df3 128 return TRUE;
giryan 0:274eb57e1df3 129 }
giryan 0:274eb57e1df3 130
giryan 0:274eb57e1df3 131 /* Create an interrupt handler for the transmit buffer empty interrupt
giryan 0:274eb57e1df3 132 * (or an equivalent) for your target processor. This function should then
giryan 0:274eb57e1df3 133 * call pxMBFrameCBTransmitterEmpty( ) which tells the protocol stack that
giryan 0:274eb57e1df3 134 * a new character can be sent. The protocol stack will then call
giryan 0:274eb57e1df3 135 * xMBPortSerialPutByte( ) to send the character.
giryan 0:274eb57e1df3 136 */
giryan 0:274eb57e1df3 137 static void prvvUARTTxReadyISR( void )
giryan 0:274eb57e1df3 138 {
giryan 0:274eb57e1df3 139 pxMBFrameCBTransmitterEmpty( );
giryan 0:274eb57e1df3 140 }
giryan 0:274eb57e1df3 141
giryan 0:274eb57e1df3 142 /* Create an interrupt handler for the receive interrupt for your target
giryan 0:274eb57e1df3 143 * processor. This function should then call pxMBFrameCBByteReceived( ). The
giryan 0:274eb57e1df3 144 * protocol stack will then call xMBPortSerialGetByte( ) to retrieve the
giryan 0:274eb57e1df3 145 * character.
giryan 0:274eb57e1df3 146 */
giryan 0:274eb57e1df3 147 static void prvvUARTRxISR( void )
giryan 0:274eb57e1df3 148 {
wclin 2:6ee56c002f64 149 vMBPortTimersDisable();
giryan 0:274eb57e1df3 150 pxMBFrameCBByteReceived( );
giryan 0:274eb57e1df3 151 }
giryan 0:274eb57e1df3 152
giryan 0:274eb57e1df3 153