modified for nuc472

Dependents:   modbus-over-rs485-sample NTOUEE-mbed-modbus-RTU NuMaker_NuWicam_Lite

Fork of Modbus by Matthew Waddilove

Committer:
wclin
Date:
Tue Aug 09 08:21:33 2016 +0000
Revision:
1:cfde7320b0bf
Parent:
0:274eb57e1df3
Child:
2:6ee56c002f64
For Nu472 platform

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
giryan 0:274eb57e1df3 38 /* ----------------------- System Variables ---------------------------------*/
wclin 1:cfde7320b0bf 39 //Serial pc(USBTX, USBRX); // Cam - mbed USB serial port
wclin 1:cfde7320b0bf 40 Serial pc(PG_2, PG_1); // Cam - mbed USB serial port
giryan 0:274eb57e1df3 41
giryan 0:274eb57e1df3 42 Ticker simISR; // Cam - mbed ticker
giryan 0:274eb57e1df3 43 // we don't have the TX buff empty interrupt, so
giryan 0:274eb57e1df3 44 // we just interrupt every 1 mSec and read RX & TX
giryan 0:274eb57e1df3 45 // status to simulate the proper ISRs.
giryan 0:274eb57e1df3 46
giryan 0:274eb57e1df3 47 static BOOL RxEnable, TxEnable; // Cam - keep a static copy of the RxEnable and TxEnable
giryan 0:274eb57e1df3 48 // status for the simulated ISR (ticker)
giryan 0:274eb57e1df3 49
giryan 0:274eb57e1df3 50
giryan 0:274eb57e1df3 51 /* ----------------------- Start implementation -----------------------------*/
giryan 0:274eb57e1df3 52 // Cam - This is called every 1mS to simulate Rx character received ISR and
giryan 0:274eb57e1df3 53 // Tx buffer empty ISR.
giryan 0:274eb57e1df3 54 static void
giryan 0:274eb57e1df3 55 prvvUARTISR( void )
giryan 0:274eb57e1df3 56 {
giryan 0:274eb57e1df3 57 if (TxEnable)
giryan 0:274eb57e1df3 58 if(pc.writeable())
wclin 1:cfde7320b0bf 59 {
giryan 0:274eb57e1df3 60 prvvUARTTxReadyISR();
wclin 1:cfde7320b0bf 61 }
giryan 0:274eb57e1df3 62
giryan 0:274eb57e1df3 63 if (RxEnable)
giryan 0:274eb57e1df3 64 if(pc.readable())
wclin 1:cfde7320b0bf 65 {
giryan 0:274eb57e1df3 66 prvvUARTRxISR();
wclin 1:cfde7320b0bf 67 }
giryan 0:274eb57e1df3 68 }
giryan 0:274eb57e1df3 69
giryan 0:274eb57e1df3 70 void
giryan 0:274eb57e1df3 71 vMBPortSerialEnable( BOOL xRxEnable, BOOL xTxEnable )
giryan 0:274eb57e1df3 72 {
giryan 0:274eb57e1df3 73 /* If xRXEnable enable serial receive interrupts. If xTxENable enable
giryan 0:274eb57e1df3 74 * transmitter empty interrupts.
giryan 0:274eb57e1df3 75 */
giryan 0:274eb57e1df3 76 RxEnable = xRxEnable;
giryan 0:274eb57e1df3 77 TxEnable = xTxEnable;
giryan 0:274eb57e1df3 78 }
giryan 0:274eb57e1df3 79
wclin 1:cfde7320b0bf 80
wclin 1:cfde7320b0bf 81 #if 1
wclin 1:cfde7320b0bf 82 /* ----------------------- Start implementation -----------------------------*/
giryan 0:274eb57e1df3 83 BOOL
giryan 0:274eb57e1df3 84 xMBPortSerialInit( UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits, eMBParity eParity )
giryan 0:274eb57e1df3 85 {
wclin 1:cfde7320b0bf 86 pc.baud(ulBaudRate);
wclin 1:cfde7320b0bf 87 return TRUE;
wclin 1:cfde7320b0bf 88 }
wclin 1:cfde7320b0bf 89
wclin 1:cfde7320b0bf 90 void xMBPortSerialPolling( void )
wclin 1:cfde7320b0bf 91 {
wclin 1:cfde7320b0bf 92 prvvUARTISR( );
wclin 1:cfde7320b0bf 93 }
wclin 1:cfde7320b0bf 94
wclin 1:cfde7320b0bf 95 #else
wclin 1:cfde7320b0bf 96 BOOL
wclin 1:cfde7320b0bf 97 xMBPortSerialInit( UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits, eMBParity eParity )
wclin 1:cfde7320b0bf 98 {
wclin 1:cfde7320b0bf 99 pc.baud(ulBaudRate);
wclin 1:cfde7320b0bf 100 simISR.attach_us(&prvvUARTISR, 500); // Cam - attach prvvUARTISR to a 1mS ticker to simulate serial interrupt behaviour
giryan 0:274eb57e1df3 101 // 1mS is just short of a character time at 9600 bps, so quick enough to pick
giryan 0:274eb57e1df3 102 // up status on a character by character basis.
giryan 0:274eb57e1df3 103 return TRUE;
giryan 0:274eb57e1df3 104 }
wclin 1:cfde7320b0bf 105 #endif
giryan 0:274eb57e1df3 106
giryan 0:274eb57e1df3 107 BOOL
giryan 0:274eb57e1df3 108 xMBPortSerialPutByte( CHAR ucByte )
giryan 0:274eb57e1df3 109 {
giryan 0:274eb57e1df3 110 /* Put a byte in the UARTs transmit buffer. This function is called
giryan 0:274eb57e1df3 111 * by the protocol stack if pxMBFrameCBTransmitterEmpty( ) has been
giryan 0:274eb57e1df3 112 * called. */
giryan 0:274eb57e1df3 113 pc.putc( ucByte);
giryan 0:274eb57e1df3 114 return TRUE;
giryan 0:274eb57e1df3 115 }
giryan 0:274eb57e1df3 116
giryan 0:274eb57e1df3 117 BOOL
giryan 0:274eb57e1df3 118 xMBPortSerialGetByte( CHAR * pucByte )
giryan 0:274eb57e1df3 119 {
giryan 0:274eb57e1df3 120 /* Return the byte in the UARTs receive buffer. This function is called
giryan 0:274eb57e1df3 121 * by the protocol stack after pxMBFrameCBByteReceived( ) has been called.
giryan 0:274eb57e1df3 122 */
wclin 1:cfde7320b0bf 123 *pucByte = pc.getc();
giryan 0:274eb57e1df3 124 return TRUE;
giryan 0:274eb57e1df3 125 }
giryan 0:274eb57e1df3 126
giryan 0:274eb57e1df3 127 /* Create an interrupt handler for the transmit buffer empty interrupt
giryan 0:274eb57e1df3 128 * (or an equivalent) for your target processor. This function should then
giryan 0:274eb57e1df3 129 * call pxMBFrameCBTransmitterEmpty( ) which tells the protocol stack that
giryan 0:274eb57e1df3 130 * a new character can be sent. The protocol stack will then call
giryan 0:274eb57e1df3 131 * xMBPortSerialPutByte( ) to send the character.
giryan 0:274eb57e1df3 132 */
giryan 0:274eb57e1df3 133 static void prvvUARTTxReadyISR( void )
giryan 0:274eb57e1df3 134 {
giryan 0:274eb57e1df3 135 pxMBFrameCBTransmitterEmpty( );
giryan 0:274eb57e1df3 136 }
giryan 0:274eb57e1df3 137
giryan 0:274eb57e1df3 138 /* Create an interrupt handler for the receive interrupt for your target
giryan 0:274eb57e1df3 139 * processor. This function should then call pxMBFrameCBByteReceived( ). The
giryan 0:274eb57e1df3 140 * protocol stack will then call xMBPortSerialGetByte( ) to retrieve the
giryan 0:274eb57e1df3 141 * character.
giryan 0:274eb57e1df3 142 */
giryan 0:274eb57e1df3 143 static void prvvUARTRxISR( void )
giryan 0:274eb57e1df3 144 {
giryan 0:274eb57e1df3 145 pxMBFrameCBByteReceived( );
giryan 0:274eb57e1df3 146 }
giryan 0:274eb57e1df3 147
giryan 0:274eb57e1df3 148