modified for NuMaker_PFM series

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

Fork of Modbus by Wayne Lin

Committer:
CYLiang@OANBE04066.nuvoton.com
Date:
Tue Mar 14 17:28:55 2023 +0800
Revision:
11:513c55a1b277
Parent:
10:288aeae1458f
Child:
12:59942e75caa6
Add RS485 declaration for M467 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"
cyliang 10:288aeae1458f 31 #if MBED_MAJOR_VERSION >= 6
cyliang 10:288aeae1458f 32 #include "MyUnbufferedSerial.h"
cyliang 10:288aeae1458f 33 #endif
giryan 0:274eb57e1df3 34
giryan 0:274eb57e1df3 35 /* ----------------------- static functions ---------------------------------*/
giryan 0:274eb57e1df3 36 static void prvvUARTTxReadyISR( void );
giryan 0:274eb57e1df3 37 static void prvvUARTRxISR( void );
giryan 0:274eb57e1df3 38 static void prvvUARTISR( void );
giryan 0:274eb57e1df3 39
giryan 0:274eb57e1df3 40 /* ----------------------- System Variables ---------------------------------*/
cyliang 7:abafdf714896 41 #if defined(MBED_CONF_APP_DEF_RS485_PORT) && (MBED_CONF_APP_DEF_RS485_PORT)// mbed serial port
wclin 6:08849dd9654d 42 #include "nvt_rs485.h"
wclin 6:08849dd9654d 43 // RS485 TX, RX, RTS pins
wclin 8:456375d72644 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 9:0c172c06e04d 48 #elif defined(TARGET_NUMAKER_PFM_M487) // for M487 board
CYLiang@OANBE04066.nuvoton.com 11:513c55a1b277 49 NvtRS485 pc(PB_3, PB_2, PA_0);
CYLiang@OANBE04066.nuvoton.com 11:513c55a1b277 50 #warning "Notice: It has no RS485 port on NUMAKER-PFM-M487 board."
CYLiang@OANBE04066.nuvoton.com 11:513c55a1b277 51 #warning "But, you can connect with a RS485 daughter board to (D1, D0, D11) PB_3(TX), PB_2(RX) and PA_0(RTS) pin."
CYLiang@OANBE04066.nuvoton.com 11:513c55a1b277 52 #elif defined(TARGET_NUMAKER_IOT_M467) // for M467 board
CYLiang@OANBE04066.nuvoton.com 11:513c55a1b277 53 NvtRS485 pc(PB_3, PB_2, PA_0);
CYLiang@OANBE04066.nuvoton.com 11:513c55a1b277 54 #warning "Notice: It has no RS485 port on NUMAKER-IOT-M467 board."
CYLiang@OANBE04066.nuvoton.com 11:513c55a1b277 55 #warning "But, you can connect with a RS485 daughter board to (D1, D0, D11) PB_3(TX), PB_2(RX) and PA_0(RTS) pin."
wclin 8:456375d72644 56 #else
wclin 8:456375d72644 57 #error "The demo code can't be executed on this board."
wclin 6:08849dd9654d 58 #endif
wclin 3:419ee4c5e10f 59 #else
wclin 6:08849dd9654d 60 //UART TX, RX
wclin 6:08849dd9654d 61 #if defined(TARGET_NUMAKER_PFM_NUC472) // for NUC472 board
wclin 6:08849dd9654d 62 Serial pc(PG_2, PG_1);
wclin 6:08849dd9654d 63 #elif defined(TARGET_NUMAKER_PFM_M453) // for M453 board
wclin 6:08849dd9654d 64 Serial pc(PD_1, PD_6);
CYLiang@OANBE04066.nuvoton.com 11:513c55a1b277 65 #elif defined(TARGET_NUMAKER_PFM_M487) // for M487 board
CYLiang@OANBE04066.nuvoton.com 11:513c55a1b277 66 Serial pc(PC_12, PC_11);
CYLiang@OANBE04066.nuvoton.com 11:513c55a1b277 67 #elif defined(TARGET_NUMAKER_IOT_M467) // for M467 board
wclin 8:456375d72644 68 Serial pc(PC_12, PC_11);
wclin 8:456375d72644 69 #else
wclin 8:456375d72644 70 #error "The demo code can't be executed on this board."
wclin 6:08849dd9654d 71 #endif
wclin 3:419ee4c5e10f 72 #endif
giryan 0:274eb57e1df3 73
wclin 2:6ee56c002f64 74 static volatile BOOL RxEnable, TxEnable; // Cam - keep a static copy of the RxEnable and TxEnable
giryan 0:274eb57e1df3 75 // status for the simulated ISR (ticker)
giryan 0:274eb57e1df3 76
giryan 0:274eb57e1df3 77
giryan 0:274eb57e1df3 78 /* ----------------------- Start implementation -----------------------------*/
giryan 0:274eb57e1df3 79 // Cam - This is called every 1mS to simulate Rx character received ISR and
giryan 0:274eb57e1df3 80 // Tx buffer empty ISR.
giryan 0:274eb57e1df3 81 static void
giryan 0:274eb57e1df3 82 prvvUARTISR( void )
giryan 0:274eb57e1df3 83 {
wclin 3:419ee4c5e10f 84 if ( TxEnable )
giryan 0:274eb57e1df3 85 if(pc.writeable())
giryan 0:274eb57e1df3 86 prvvUARTTxReadyISR();
wclin 3:419ee4c5e10f 87
wclin 3:419ee4c5e10f 88 if ( RxEnable )
giryan 0:274eb57e1df3 89 if(pc.readable())
wclin 3:419ee4c5e10f 90 prvvUARTRxISR();
giryan 0:274eb57e1df3 91 }
giryan 0:274eb57e1df3 92
giryan 0:274eb57e1df3 93 void
giryan 0:274eb57e1df3 94 vMBPortSerialEnable( BOOL xRxEnable, BOOL xTxEnable )
giryan 0:274eb57e1df3 95 {
giryan 0:274eb57e1df3 96 /* If xRXEnable enable serial receive interrupts. If xTxENable enable
giryan 0:274eb57e1df3 97 * transmitter empty interrupts.
giryan 0:274eb57e1df3 98 */
giryan 0:274eb57e1df3 99 RxEnable = xRxEnable;
giryan 0:274eb57e1df3 100 TxEnable = xTxEnable;
wclin 3:419ee4c5e10f 101
wclin 3:419ee4c5e10f 102 //printf("\r\nRx: %d, TX:%d\r\n", RxEnable, TxEnable);
giryan 0:274eb57e1df3 103 }
giryan 0:274eb57e1df3 104
wclin 1:cfde7320b0bf 105 /* ----------------------- Start implementation -----------------------------*/
giryan 0:274eb57e1df3 106 BOOL
giryan 0:274eb57e1df3 107 xMBPortSerialInit( UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits, eMBParity eParity )
giryan 0:274eb57e1df3 108 {
wclin 1:cfde7320b0bf 109 pc.baud(ulBaudRate);
cyliang 7:abafdf714896 110 #if defined(MBED_CONF_APP_DEF_RS485_PORT) && (MBED_CONF_APP_DEF_RS485_PORT) // mbed serial port
wclin 6:08849dd9654d 111 #if defined(TARGET_NUMAKER_PFM_NUC472) // for NUC472 board
wclin 3:419ee4c5e10f 112 pc.set_rs485_mode(PF_11);
wclin 6:08849dd9654d 113 #elif defined(TARGET_NUMAKER_PFM_M453) // for M453 board
wclin 6:08849dd9654d 114 pc.set_rs485_mode(PE_11);
wclin 9:0c172c06e04d 115 #elif defined(TARGET_NUMAKER_PFM_M487) // for M487 board
wclin 9:0c172c06e04d 116 pc.set_rs485_mode(PA_0);
CYLiang@OANBE04066.nuvoton.com 11:513c55a1b277 117 #elif defined(TARGET_NUMAKER_IOT_M467) // for M467 board
CYLiang@OANBE04066.nuvoton.com 11:513c55a1b277 118 pc.set_rs485_mode(PA_0);
wclin 9:0c172c06e04d 119 #endif
wclin 3:419ee4c5e10f 120 #endif
cyliang 10:288aeae1458f 121 #if 0 //MBED_MAJOR_VERSION >= 6
cyliang 10:288aeae1458f 122 SerialBase::Parity parity;
cyliang 10:288aeae1458f 123 if (eParity == MB_PAR_NONE)
cyliang 10:288aeae1458f 124 parity = SerialBase::None;
cyliang 10:288aeae1458f 125 else if ( eParity == MB_PAR_ODD )
cyliang 10:288aeae1458f 126 parity = SerialBase::Odd;
cyliang 10:288aeae1458f 127 else if ( eParity == MB_PAR_EVEN)
cyliang 10:288aeae1458f 128 parity = SerialBase::Even;
cyliang 10:288aeae1458f 129 pc.format(
cyliang 10:288aeae1458f 130 /* bits */ 8,
cyliang 10:288aeae1458f 131 /* parity */ parity,
cyliang 10:288aeae1458f 132 /* stop bit */ 1
cyliang 10:288aeae1458f 133 );
cyliang 10:288aeae1458f 134 #endif
wclin 1:cfde7320b0bf 135 return TRUE;
wclin 1:cfde7320b0bf 136 }
wclin 1:cfde7320b0bf 137
wclin 1:cfde7320b0bf 138 void xMBPortSerialPolling( void )
wclin 1:cfde7320b0bf 139 {
wclin 1:cfde7320b0bf 140 prvvUARTISR( );
wclin 1:cfde7320b0bf 141 }
wclin 1:cfde7320b0bf 142
giryan 0:274eb57e1df3 143 BOOL
giryan 0:274eb57e1df3 144 xMBPortSerialPutByte( CHAR ucByte )
giryan 0:274eb57e1df3 145 {
giryan 0:274eb57e1df3 146 /* Put a byte in the UARTs transmit buffer. This function is called
giryan 0:274eb57e1df3 147 * by the protocol stack if pxMBFrameCBTransmitterEmpty( ) has been
giryan 0:274eb57e1df3 148 * called. */
wclin 3:419ee4c5e10f 149 //printf("[%02x]", ucByte );
cyliang 10:288aeae1458f 150 #if MBED_MAJOR_VERSION >= 6
cyliang 10:288aeae1458f 151 pc.write(&ucByte, 1);
cyliang 10:288aeae1458f 152 #else
giryan 0:274eb57e1df3 153 pc.putc( ucByte);
cyliang 10:288aeae1458f 154 #endif
giryan 0:274eb57e1df3 155 return TRUE;
giryan 0:274eb57e1df3 156 }
giryan 0:274eb57e1df3 157
giryan 0:274eb57e1df3 158 BOOL
giryan 0:274eb57e1df3 159 xMBPortSerialGetByte( CHAR * pucByte )
giryan 0:274eb57e1df3 160 {
giryan 0:274eb57e1df3 161 /* Return the byte in the UARTs receive buffer. This function is called
giryan 0:274eb57e1df3 162 * by the protocol stack after pxMBFrameCBByteReceived( ) has been called.
giryan 0:274eb57e1df3 163 */
cyliang 10:288aeae1458f 164 #if MBED_MAJOR_VERSION >= 6
cyliang 10:288aeae1458f 165 pc.read(pucByte,1);
cyliang 10:288aeae1458f 166 #else
wclin 3:419ee4c5e10f 167 *pucByte = pc.getc();
cyliang 10:288aeae1458f 168 #endif
wclin 3:419ee4c5e10f 169 //printf("<%02x>", *pucByte );
giryan 0:274eb57e1df3 170 return TRUE;
giryan 0:274eb57e1df3 171 }
giryan 0:274eb57e1df3 172
giryan 0:274eb57e1df3 173 /* Create an interrupt handler for the transmit buffer empty interrupt
giryan 0:274eb57e1df3 174 * (or an equivalent) for your target processor. This function should then
giryan 0:274eb57e1df3 175 * call pxMBFrameCBTransmitterEmpty( ) which tells the protocol stack that
giryan 0:274eb57e1df3 176 * a new character can be sent. The protocol stack will then call
giryan 0:274eb57e1df3 177 * xMBPortSerialPutByte( ) to send the character.
giryan 0:274eb57e1df3 178 */
giryan 0:274eb57e1df3 179 static void prvvUARTTxReadyISR( void )
giryan 0:274eb57e1df3 180 {
giryan 0:274eb57e1df3 181 pxMBFrameCBTransmitterEmpty( );
giryan 0:274eb57e1df3 182 }
giryan 0:274eb57e1df3 183
giryan 0:274eb57e1df3 184 /* Create an interrupt handler for the receive interrupt for your target
giryan 0:274eb57e1df3 185 * processor. This function should then call pxMBFrameCBByteReceived( ). The
giryan 0:274eb57e1df3 186 * protocol stack will then call xMBPortSerialGetByte( ) to retrieve the
giryan 0:274eb57e1df3 187 * character.
giryan 0:274eb57e1df3 188 */
giryan 0:274eb57e1df3 189 static void prvvUARTRxISR( void )
giryan 0:274eb57e1df3 190 {
wclin 2:6ee56c002f64 191 vMBPortTimersDisable();
giryan 0:274eb57e1df3 192 pxMBFrameCBByteReceived( );
giryan 0:274eb57e1df3 193 }
giryan 0:274eb57e1df3 194
giryan 0:274eb57e1df3 195