fork frdm kl25z slave
portserial.cpp@8:aa64e731174a, 2022-04-01 (annotated)
- Committer:
- jlsalvat
- Date:
- Fri Apr 01 17:35:58 2022 +0000
- Revision:
- 8:aa64e731174a
- Parent:
- 7:be466464a18c
change for frdm-kl25Z
Who changed what in which revision?
User | Revision | Line number | New 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 | |
jlsalvat | 8:aa64e731174a | 32 | #define INPUT 0 |
jlsalvat | 8:aa64e731174a | 33 | #define OUTPUT 1 |
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 | |
wclin | 5:d4c3b20819a7 | 40 | //#define DEF_RS485_PORT 1 |
giryan | 0:274eb57e1df3 | 41 | /* ----------------------- System Variables ---------------------------------*/ |
wclin | 3:419ee4c5e10f | 42 | |
wclin | 7:be466464a18c | 43 | #if defined(MBED_CONF_APP_DEF_RS485_PORT) && (MBED_CONF_APP_DEF_RS485_PORT)// mbed serial port |
wclin | 6:08849dd9654d | 44 | #include "nvt_rs485.h" |
wclin | 6:08849dd9654d | 45 | // RS485 TX, RX, RTS pins |
wclin | 7:be466464a18c | 46 | #if defined(TARGET_NUMAKER_PFM_NUC472) // for NUC472 board |
wclin | 6:08849dd9654d | 47 | NvtRS485 pc(PF_13, PF_14, PF_11); |
wclin | 6:08849dd9654d | 48 | #elif defined(TARGET_NUMAKER_PFM_M453) // for M453 board |
wclin | 6:08849dd9654d | 49 | NvtRS485 pc(PE_8, PE_9, PE_11); |
wclin | 7:be466464a18c | 50 | #else |
wclin | 7:be466464a18c | 51 | #error "The demo code can't be executed on this board." |
wclin | 6:08849dd9654d | 52 | #endif |
wclin | 3:419ee4c5e10f | 53 | #else |
wclin | 6:08849dd9654d | 54 | //UART TX, RX |
wclin | 7:be466464a18c | 55 | #if defined(TARGET_NUMAKER_PFM_NUC472) // for NUC472 board |
wclin | 6:08849dd9654d | 56 | Serial pc(PG_2, PG_1); |
wclin | 6:08849dd9654d | 57 | #elif defined(TARGET_NUMAKER_PFM_M453) // for M453 board |
wclin | 6:08849dd9654d | 58 | Serial pc(PD_1, PD_6); |
wclin | 7:be466464a18c | 59 | #elif defined(TARGET_NUMAKER_PFM_M487) // for M478 board |
wclin | 7:be466464a18c | 60 | Serial pc(PC_12, PC_11); |
wclin | 7:be466464a18c | 61 | #else |
jlsalvat | 8:aa64e731174a | 62 | static UnbufferedSerial modbus_serial(D14, D15); |
jlsalvat | 8:aa64e731174a | 63 | DigitalOut modbus_dir(PTD6); |
jlsalvat | 8:aa64e731174a | 64 | |
wclin | 6:08849dd9654d | 65 | #endif |
wclin | 3:419ee4c5e10f | 66 | #endif |
giryan | 0:274eb57e1df3 | 67 | |
jlsalvat | 8:aa64e731174a | 68 | int us_time_to_send_bit; |
wclin | 2:6ee56c002f64 | 69 | static volatile BOOL RxEnable, TxEnable; // Cam - keep a static copy of the RxEnable and TxEnable |
giryan | 0:274eb57e1df3 | 70 | // status for the simulated ISR (ticker) |
giryan | 0:274eb57e1df3 | 71 | |
giryan | 0:274eb57e1df3 | 72 | |
giryan | 0:274eb57e1df3 | 73 | /* ----------------------- Start implementation -----------------------------*/ |
giryan | 0:274eb57e1df3 | 74 | // Cam - This is called every 1mS to simulate Rx character received ISR and |
giryan | 0:274eb57e1df3 | 75 | // Tx buffer empty ISR. |
giryan | 0:274eb57e1df3 | 76 | static void |
giryan | 0:274eb57e1df3 | 77 | prvvUARTISR( void ) |
giryan | 0:274eb57e1df3 | 78 | { |
wclin | 3:419ee4c5e10f | 79 | if ( TxEnable ) |
jlsalvat | 8:aa64e731174a | 80 | if(modbus_serial.writeable()) |
giryan | 0:274eb57e1df3 | 81 | prvvUARTTxReadyISR(); |
wclin | 3:419ee4c5e10f | 82 | |
wclin | 3:419ee4c5e10f | 83 | if ( RxEnable ) |
jlsalvat | 8:aa64e731174a | 84 | if(modbus_serial.readable()) |
wclin | 3:419ee4c5e10f | 85 | prvvUARTRxISR(); |
giryan | 0:274eb57e1df3 | 86 | } |
giryan | 0:274eb57e1df3 | 87 | |
giryan | 0:274eb57e1df3 | 88 | void |
giryan | 0:274eb57e1df3 | 89 | vMBPortSerialEnable( BOOL xRxEnable, BOOL xTxEnable ) |
giryan | 0:274eb57e1df3 | 90 | { |
giryan | 0:274eb57e1df3 | 91 | /* If xRXEnable enable serial receive interrupts. If xTxENable enable |
giryan | 0:274eb57e1df3 | 92 | * transmitter empty interrupts. |
giryan | 0:274eb57e1df3 | 93 | */ |
giryan | 0:274eb57e1df3 | 94 | RxEnable = xRxEnable; |
giryan | 0:274eb57e1df3 | 95 | TxEnable = xTxEnable; |
wclin | 3:419ee4c5e10f | 96 | |
wclin | 3:419ee4c5e10f | 97 | //printf("\r\nRx: %d, TX:%d\r\n", RxEnable, TxEnable); |
giryan | 0:274eb57e1df3 | 98 | } |
giryan | 0:274eb57e1df3 | 99 | |
wclin | 1:cfde7320b0bf | 100 | /* ----------------------- Start implementation -----------------------------*/ |
giryan | 0:274eb57e1df3 | 101 | BOOL |
giryan | 0:274eb57e1df3 | 102 | xMBPortSerialInit( UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits, eMBParity eParity ) |
giryan | 0:274eb57e1df3 | 103 | { |
jlsalvat | 8:aa64e731174a | 104 | modbus_dir=INPUT; |
jlsalvat | 8:aa64e731174a | 105 | modbus_serial.baud(ulBaudRate); |
jlsalvat | 8:aa64e731174a | 106 | modbus_serial.format(8,(SerialBase::Parity) eParity,1); |
jlsalvat | 8:aa64e731174a | 107 | us_time_to_send_bit=1000000/(ulBaudRate); |
wclin | 3:419ee4c5e10f | 108 | #if defined(DEF_RS485_PORT) // mbed serial port |
wclin | 6:08849dd9654d | 109 | #if defined(TARGET_NUMAKER_PFM_NUC472) // for NUC472 board |
wclin | 3:419ee4c5e10f | 110 | pc.set_rs485_mode(PF_11); |
wclin | 6:08849dd9654d | 111 | #elif defined(TARGET_NUMAKER_PFM_M453) // for M453 board |
wclin | 6:08849dd9654d | 112 | pc.set_rs485_mode(PE_11); |
wclin | 6:08849dd9654d | 113 | #endif |
wclin | 3:419ee4c5e10f | 114 | #endif |
wclin | 1:cfde7320b0bf | 115 | return TRUE; |
wclin | 1:cfde7320b0bf | 116 | } |
wclin | 1:cfde7320b0bf | 117 | |
wclin | 1:cfde7320b0bf | 118 | void xMBPortSerialPolling( void ) |
wclin | 1:cfde7320b0bf | 119 | { |
wclin | 1:cfde7320b0bf | 120 | prvvUARTISR( ); |
wclin | 1:cfde7320b0bf | 121 | } |
wclin | 1:cfde7320b0bf | 122 | |
giryan | 0:274eb57e1df3 | 123 | BOOL |
giryan | 0:274eb57e1df3 | 124 | xMBPortSerialPutByte( CHAR ucByte ) |
giryan | 0:274eb57e1df3 | 125 | { |
giryan | 0:274eb57e1df3 | 126 | /* Put a byte in the UARTs transmit buffer. This function is called |
giryan | 0:274eb57e1df3 | 127 | * by the protocol stack if pxMBFrameCBTransmitterEmpty( ) has been |
giryan | 0:274eb57e1df3 | 128 | * called. */ |
jlsalvat | 8:aa64e731174a | 129 | printf("[%02x]", ucByte ); |
jlsalvat | 8:aa64e731174a | 130 | modbus_dir=OUTPUT; |
jlsalvat | 8:aa64e731174a | 131 | modbus_serial.write( &ucByte,1); |
jlsalvat | 8:aa64e731174a | 132 | wait_us(us_time_to_send_bit*11);//maximum 11 bits to send |
jlsalvat | 8:aa64e731174a | 133 | modbus_dir=INPUT; |
giryan | 0:274eb57e1df3 | 134 | return TRUE; |
giryan | 0:274eb57e1df3 | 135 | } |
giryan | 0:274eb57e1df3 | 136 | |
giryan | 0:274eb57e1df3 | 137 | BOOL |
giryan | 0:274eb57e1df3 | 138 | xMBPortSerialGetByte( CHAR * pucByte ) |
giryan | 0:274eb57e1df3 | 139 | { |
giryan | 0:274eb57e1df3 | 140 | /* Return the byte in the UARTs receive buffer. This function is called |
giryan | 0:274eb57e1df3 | 141 | * by the protocol stack after pxMBFrameCBByteReceived( ) has been called. |
giryan | 0:274eb57e1df3 | 142 | */ |
jlsalvat | 8:aa64e731174a | 143 | modbus_dir=INPUT; |
jlsalvat | 8:aa64e731174a | 144 | modbus_serial.read(pucByte,1); |
jlsalvat | 8:aa64e731174a | 145 | printf("<%02x>", *pucByte ); |
giryan | 0:274eb57e1df3 | 146 | return TRUE; |
giryan | 0:274eb57e1df3 | 147 | } |
giryan | 0:274eb57e1df3 | 148 | |
giryan | 0:274eb57e1df3 | 149 | /* Create an interrupt handler for the transmit buffer empty interrupt |
giryan | 0:274eb57e1df3 | 150 | * (or an equivalent) for your target processor. This function should then |
giryan | 0:274eb57e1df3 | 151 | * call pxMBFrameCBTransmitterEmpty( ) which tells the protocol stack that |
giryan | 0:274eb57e1df3 | 152 | * a new character can be sent. The protocol stack will then call |
giryan | 0:274eb57e1df3 | 153 | * xMBPortSerialPutByte( ) to send the character. |
giryan | 0:274eb57e1df3 | 154 | */ |
giryan | 0:274eb57e1df3 | 155 | static void prvvUARTTxReadyISR( void ) |
giryan | 0:274eb57e1df3 | 156 | { |
giryan | 0:274eb57e1df3 | 157 | pxMBFrameCBTransmitterEmpty( ); |
giryan | 0:274eb57e1df3 | 158 | } |
giryan | 0:274eb57e1df3 | 159 | |
giryan | 0:274eb57e1df3 | 160 | /* Create an interrupt handler for the receive interrupt for your target |
giryan | 0:274eb57e1df3 | 161 | * processor. This function should then call pxMBFrameCBByteReceived( ). The |
giryan | 0:274eb57e1df3 | 162 | * protocol stack will then call xMBPortSerialGetByte( ) to retrieve the |
giryan | 0:274eb57e1df3 | 163 | * character. |
giryan | 0:274eb57e1df3 | 164 | */ |
giryan | 0:274eb57e1df3 | 165 | static void prvvUARTRxISR( void ) |
giryan | 0:274eb57e1df3 | 166 | { |
wclin | 2:6ee56c002f64 | 167 | vMBPortTimersDisable(); |
giryan | 0:274eb57e1df3 | 168 | pxMBFrameCBByteReceived( ); |
giryan | 0:274eb57e1df3 | 169 | } |
giryan | 0:274eb57e1df3 | 170 | |
giryan | 0:274eb57e1df3 | 171 |