Forking https://os.mbed.com/users/cam/code/Modbus/ to work for NUCLEO 64 boards
Fork of Cam's original FreeModbus port (https://os.mbed.com/users/cam/code/Modbus/)
Change: - Serial implementation to work for NUCLEO 64 boards and receive interrupts instead of timer. (see `portserial.cpp`)
Added: - Custom RTU mode. Allows for external implementation of packet receiving and sending. Sends and receives packets as whole frames (address + PDU) (i.e. this was added for a custom LoRa implementation). implement `xMBRTUCustGetPDU` and `xMBRTUCustSendResponse` (see `mbport.h`) and call `eMBRTUCustomInit( address )`. implementations need to be fully initialised as `eMBRTUCustomInit` only sets the address and nothing else.
portserial.cpp@4:7621103c5a40, 2020-08-04 (annotated)
- Committer:
- danielmckinnell
- Date:
- Tue Aug 04 04:42:52 2020 +0000
- Revision:
- 4:7621103c5a40
- Parent:
- 1:e2f569d323c9
RTU Serial updates
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cam | 0:0453a0a7e500 | 1 | /* |
cam | 0:0453a0a7e500 | 2 | * FreeModbus Libary: BARE Port |
cam | 0:0453a0a7e500 | 3 | * Copyright (C) 2006 Christian Walter <wolti@sil.at> |
cam | 0:0453a0a7e500 | 4 | * |
cam | 0:0453a0a7e500 | 5 | * This library is free software; you can redistribute it and/or |
cam | 0:0453a0a7e500 | 6 | * modify it under the terms of the GNU Lesser General Public |
cam | 0:0453a0a7e500 | 7 | * License as published by the Free Software Foundation; either |
cam | 0:0453a0a7e500 | 8 | * version 2.1 of the License, or (at your option) any later version. |
cam | 0:0453a0a7e500 | 9 | * |
cam | 0:0453a0a7e500 | 10 | * This library is distributed in the hope that it will be useful, |
cam | 0:0453a0a7e500 | 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
cam | 0:0453a0a7e500 | 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
cam | 0:0453a0a7e500 | 13 | * Lesser General Public License for more details. |
cam | 0:0453a0a7e500 | 14 | * |
cam | 0:0453a0a7e500 | 15 | * You should have received a copy of the GNU Lesser General Public |
cam | 0:0453a0a7e500 | 16 | * License along with this library; if not, write to the Free Software |
cam | 0:0453a0a7e500 | 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
cam | 0:0453a0a7e500 | 18 | * |
cam | 0:0453a0a7e500 | 19 | * File: $Id: portserial.c,v 1.1 2006/08/22 21:35:13 wolti Exp $ |
cam | 0:0453a0a7e500 | 20 | */ |
cam | 0:0453a0a7e500 | 21 | |
cam | 0:0453a0a7e500 | 22 | /* ----------------------- System includes ----------------------------------*/ |
cam | 0:0453a0a7e500 | 23 | #include "mbed.h" // Cam |
danielmckinnell | 1:e2f569d323c9 | 24 | #include "RawSerial.h" |
cam | 0:0453a0a7e500 | 25 | /* ----------------------- Platform includes --------------------------------*/ |
cam | 0:0453a0a7e500 | 26 | #include "port.h" |
cam | 0:0453a0a7e500 | 27 | |
cam | 0:0453a0a7e500 | 28 | /* ----------------------- Modbus includes ----------------------------------*/ |
cam | 0:0453a0a7e500 | 29 | #include "mb.h" |
cam | 0:0453a0a7e500 | 30 | #include "mbport.h" |
cam | 0:0453a0a7e500 | 31 | |
danielmckinnell | 1:e2f569d323c9 | 32 | // Dan - Allow serial pins to be configurable in json config |
danielmckinnell | 1:e2f569d323c9 | 33 | #ifndef MBED_CONF_APP_MODBUS_SERIAL_TX |
danielmckinnell | 1:e2f569d323c9 | 34 | #define MBED_CONF_APP_MODBUS_SERIAL_TX USBTX |
danielmckinnell | 1:e2f569d323c9 | 35 | #endif |
danielmckinnell | 1:e2f569d323c9 | 36 | |
danielmckinnell | 1:e2f569d323c9 | 37 | #ifndef MBED_CONF_APP_MODBUS_SERIAL_RX |
danielmckinnell | 1:e2f569d323c9 | 38 | #define MBED_CONF_APP_MODBUS_SERIAL_RX USBRX |
danielmckinnell | 1:e2f569d323c9 | 39 | #endif |
cam | 0:0453a0a7e500 | 40 | |
cam | 0:0453a0a7e500 | 41 | /* ----------------------- static functions ---------------------------------*/ |
danielmckinnell | 4:7621103c5a40 | 42 | |
danielmckinnell | 4:7621103c5a40 | 43 | static void prvvUARTTxISR( void ); |
cam | 0:0453a0a7e500 | 44 | static void prvvUARTRxISR( void ); |
cam | 0:0453a0a7e500 | 45 | |
cam | 0:0453a0a7e500 | 46 | /* ----------------------- System Variables ---------------------------------*/ |
cam | 0:0453a0a7e500 | 47 | |
danielmckinnell | 4:7621103c5a40 | 48 | static RawSerial* modbus_serial; |
cam | 0:0453a0a7e500 | 49 | |
cam | 0:0453a0a7e500 | 50 | /* ----------------------- Start implementation -----------------------------*/ |
danielmckinnell | 4:7621103c5a40 | 51 | |
danielmckinnell | 4:7621103c5a40 | 52 | void xMBSetSerial(RawSerial* s){ |
danielmckinnell | 4:7621103c5a40 | 53 | modbus_serial = s; |
cam | 0:0453a0a7e500 | 54 | } |
cam | 0:0453a0a7e500 | 55 | |
cam | 0:0453a0a7e500 | 56 | void |
cam | 0:0453a0a7e500 | 57 | vMBPortSerialEnable( BOOL xRxEnable, BOOL xTxEnable ) |
cam | 0:0453a0a7e500 | 58 | { |
danielmckinnell | 4:7621103c5a40 | 59 | if(xRxEnable){ |
danielmckinnell | 4:7621103c5a40 | 60 | modbus_serial->attach(&prvvUARTRxISR, RawSerial::RxIrq); // Dan |
danielmckinnell | 4:7621103c5a40 | 61 | }else{ |
danielmckinnell | 4:7621103c5a40 | 62 | modbus_serial->attach(nullptr, RawSerial::RxIrq); |
danielmckinnell | 4:7621103c5a40 | 63 | } |
danielmckinnell | 4:7621103c5a40 | 64 | |
danielmckinnell | 4:7621103c5a40 | 65 | // if(xTxEnable){ |
danielmckinnell | 4:7621103c5a40 | 66 | // modbus_serial->attach(&prvvUARTTxISR, RawSerial::TxIrq); // Dan |
danielmckinnell | 4:7621103c5a40 | 67 | // }else{ |
danielmckinnell | 4:7621103c5a40 | 68 | // modbus_serial->attach(nullptr, RawSerial::TxIrq); |
danielmckinnell | 4:7621103c5a40 | 69 | // } |
cam | 0:0453a0a7e500 | 70 | } |
cam | 0:0453a0a7e500 | 71 | |
danielmckinnell | 4:7621103c5a40 | 72 | |
danielmckinnell | 4:7621103c5a40 | 73 | |
cam | 0:0453a0a7e500 | 74 | BOOL |
cam | 0:0453a0a7e500 | 75 | xMBPortSerialInit( UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits, eMBParity eParity ) |
cam | 0:0453a0a7e500 | 76 | { |
danielmckinnell | 4:7621103c5a40 | 77 | SerialBase::Parity p = eParity == MB_PAR_NONE ? SerialBase::None : |
danielmckinnell | 4:7621103c5a40 | 78 | eParity == MB_PAR_EVEN ? SerialBase::Even : SerialBase::Odd; |
danielmckinnell | 4:7621103c5a40 | 79 | |
danielmckinnell | 4:7621103c5a40 | 80 | modbus_serial->baud(ulBaudRate); |
danielmckinnell | 4:7621103c5a40 | 81 | modbus_serial->format(ucDataBits, p); |
danielmckinnell | 4:7621103c5a40 | 82 | |
cam | 0:0453a0a7e500 | 83 | return TRUE; |
cam | 0:0453a0a7e500 | 84 | } |
cam | 0:0453a0a7e500 | 85 | |
cam | 0:0453a0a7e500 | 86 | BOOL |
cam | 0:0453a0a7e500 | 87 | xMBPortSerialPutByte( CHAR ucByte ) |
cam | 0:0453a0a7e500 | 88 | { |
cam | 0:0453a0a7e500 | 89 | /* Put a byte in the UARTs transmit buffer. This function is called |
cam | 0:0453a0a7e500 | 90 | * by the protocol stack if pxMBFrameCBTransmitterEmpty( ) has been |
cam | 0:0453a0a7e500 | 91 | * called. */ |
danielmckinnell | 4:7621103c5a40 | 92 | modbus_serial->putc( ucByte); |
cam | 0:0453a0a7e500 | 93 | return TRUE; |
cam | 0:0453a0a7e500 | 94 | } |
cam | 0:0453a0a7e500 | 95 | |
cam | 0:0453a0a7e500 | 96 | BOOL |
cam | 0:0453a0a7e500 | 97 | xMBPortSerialGetByte( CHAR * pucByte ) |
cam | 0:0453a0a7e500 | 98 | { |
cam | 0:0453a0a7e500 | 99 | /* Return the byte in the UARTs receive buffer. This function is called |
cam | 0:0453a0a7e500 | 100 | * by the protocol stack after pxMBFrameCBByteReceived( ) has been called. |
cam | 0:0453a0a7e500 | 101 | */ |
danielmckinnell | 4:7621103c5a40 | 102 | *pucByte = modbus_serial->getc(); |
cam | 0:0453a0a7e500 | 103 | return TRUE; |
cam | 0:0453a0a7e500 | 104 | } |
cam | 0:0453a0a7e500 | 105 | |
danielmckinnell | 4:7621103c5a40 | 106 | |
danielmckinnell | 4:7621103c5a40 | 107 | static void prvvUARTTxISR( void ) |
cam | 0:0453a0a7e500 | 108 | { |
cam | 0:0453a0a7e500 | 109 | pxMBFrameCBTransmitterEmpty( ); |
cam | 0:0453a0a7e500 | 110 | } |
cam | 0:0453a0a7e500 | 111 | |
cam | 0:0453a0a7e500 | 112 | /* Create an interrupt handler for the receive interrupt for your target |
cam | 0:0453a0a7e500 | 113 | * processor. This function should then call pxMBFrameCBByteReceived( ). The |
cam | 0:0453a0a7e500 | 114 | * protocol stack will then call xMBPortSerialGetByte( ) to retrieve the |
cam | 0:0453a0a7e500 | 115 | * character. |
cam | 0:0453a0a7e500 | 116 | */ |
cam | 0:0453a0a7e500 | 117 | static void prvvUARTRxISR( void ) |
cam | 0:0453a0a7e500 | 118 | { |
cam | 0:0453a0a7e500 | 119 | pxMBFrameCBByteReceived( ); |
danielmckinnell | 4:7621103c5a40 | 120 | } |