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@1:e2f569d323c9, 2019-12-03 (annotated)
- Committer:
- danielmckinnell
- Date:
- Tue Dec 03 04:20:22 2019 +0000
- Revision:
- 1:e2f569d323c9
- Parent:
- 0:0453a0a7e500
- Child:
- 4:7621103c5a40
forking https://os.mbed.com/users/cam/code/Modbus/ to work for NUCLEO 64 boards
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 ---------------------------------*/ |
cam | 0:0453a0a7e500 | 42 | static void prvvUARTTxReadyISR( void ); |
cam | 0:0453a0a7e500 | 43 | static void prvvUARTRxISR( void ); |
cam | 0:0453a0a7e500 | 44 | static void prvvUARTISR( void ); |
cam | 0:0453a0a7e500 | 45 | |
cam | 0:0453a0a7e500 | 46 | /* ----------------------- System Variables ---------------------------------*/ |
danielmckinnell | 1:e2f569d323c9 | 47 | RawSerial modbus_serial(MBED_CONF_APP_MODBUS_SERIAL_TX, MBED_CONF_APP_MODBUS_SERIAL_RX); // Dan |
cam | 0:0453a0a7e500 | 48 | |
cam | 0:0453a0a7e500 | 49 | Ticker simISR; // Cam - mbed ticker |
cam | 0:0453a0a7e500 | 50 | // we don't have the TX buff empty interrupt, so |
cam | 0:0453a0a7e500 | 51 | // we just interrupt every 1 mSec and read RX & TX |
cam | 0:0453a0a7e500 | 52 | // status to simulate the proper ISRs. |
cam | 0:0453a0a7e500 | 53 | |
cam | 0:0453a0a7e500 | 54 | static BOOL RxEnable, TxEnable; // Cam - keep a static copy of the RxEnable and TxEnable |
cam | 0:0453a0a7e500 | 55 | // status for the simulated ISR (ticker) |
cam | 0:0453a0a7e500 | 56 | |
cam | 0:0453a0a7e500 | 57 | |
cam | 0:0453a0a7e500 | 58 | /* ----------------------- Start implementation -----------------------------*/ |
cam | 0:0453a0a7e500 | 59 | // Cam - This is called every 1mS to simulate Rx character received ISR and |
cam | 0:0453a0a7e500 | 60 | // Tx buffer empty ISR. |
danielmckinnell | 1:e2f569d323c9 | 61 | // Dan - Edited to only simulate Tx buffer empty ISR. |
cam | 0:0453a0a7e500 | 62 | static void |
cam | 0:0453a0a7e500 | 63 | prvvUARTISR( void ) |
cam | 0:0453a0a7e500 | 64 | { |
cam | 0:0453a0a7e500 | 65 | if (TxEnable) |
danielmckinnell | 1:e2f569d323c9 | 66 | if(modbus_serial.writeable()) |
danielmckinnell | 1:e2f569d323c9 | 67 | prvvUARTTxReadyISR(); |
cam | 0:0453a0a7e500 | 68 | } |
cam | 0:0453a0a7e500 | 69 | |
cam | 0:0453a0a7e500 | 70 | void |
cam | 0:0453a0a7e500 | 71 | vMBPortSerialEnable( BOOL xRxEnable, BOOL xTxEnable ) |
cam | 0:0453a0a7e500 | 72 | { |
cam | 0:0453a0a7e500 | 73 | /* If xRXEnable enable serial receive interrupts. If xTxENable enable |
cam | 0:0453a0a7e500 | 74 | * transmitter empty interrupts. |
cam | 0:0453a0a7e500 | 75 | */ |
cam | 0:0453a0a7e500 | 76 | RxEnable = xRxEnable; |
cam | 0:0453a0a7e500 | 77 | TxEnable = xTxEnable; |
cam | 0:0453a0a7e500 | 78 | } |
cam | 0:0453a0a7e500 | 79 | |
cam | 0:0453a0a7e500 | 80 | BOOL |
cam | 0:0453a0a7e500 | 81 | xMBPortSerialInit( UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits, eMBParity eParity ) |
cam | 0:0453a0a7e500 | 82 | { |
danielmckinnell | 1:e2f569d323c9 | 83 | modbus_serial.attach(&prvvUARTRxISR, RawSerial::RxIrq); // Dan |
cam | 0:0453a0a7e500 | 84 | simISR.attach_us(&prvvUARTISR,1000); // Cam - attach prvvUARTISR to a 1mS ticker to simulate serial interrupt behaviour |
cam | 0:0453a0a7e500 | 85 | // 1mS is just short of a character time at 9600 bps, so quick enough to pick |
cam | 0:0453a0a7e500 | 86 | // up status on a character by character basis. |
cam | 0:0453a0a7e500 | 87 | return TRUE; |
cam | 0:0453a0a7e500 | 88 | } |
cam | 0:0453a0a7e500 | 89 | |
cam | 0:0453a0a7e500 | 90 | BOOL |
cam | 0:0453a0a7e500 | 91 | xMBPortSerialPutByte( CHAR ucByte ) |
cam | 0:0453a0a7e500 | 92 | { |
cam | 0:0453a0a7e500 | 93 | /* Put a byte in the UARTs transmit buffer. This function is called |
cam | 0:0453a0a7e500 | 94 | * by the protocol stack if pxMBFrameCBTransmitterEmpty( ) has been |
cam | 0:0453a0a7e500 | 95 | * called. */ |
danielmckinnell | 1:e2f569d323c9 | 96 | modbus_serial.putc( ucByte); |
cam | 0:0453a0a7e500 | 97 | return TRUE; |
cam | 0:0453a0a7e500 | 98 | } |
cam | 0:0453a0a7e500 | 99 | |
cam | 0:0453a0a7e500 | 100 | BOOL |
cam | 0:0453a0a7e500 | 101 | xMBPortSerialGetByte( CHAR * pucByte ) |
cam | 0:0453a0a7e500 | 102 | { |
cam | 0:0453a0a7e500 | 103 | /* Return the byte in the UARTs receive buffer. This function is called |
cam | 0:0453a0a7e500 | 104 | * by the protocol stack after pxMBFrameCBByteReceived( ) has been called. |
cam | 0:0453a0a7e500 | 105 | */ |
danielmckinnell | 1:e2f569d323c9 | 106 | *pucByte = modbus_serial.getc(); |
cam | 0:0453a0a7e500 | 107 | return TRUE; |
cam | 0:0453a0a7e500 | 108 | } |
cam | 0:0453a0a7e500 | 109 | |
cam | 0:0453a0a7e500 | 110 | /* Create an interrupt handler for the transmit buffer empty interrupt |
cam | 0:0453a0a7e500 | 111 | * (or an equivalent) for your target processor. This function should then |
cam | 0:0453a0a7e500 | 112 | * call pxMBFrameCBTransmitterEmpty( ) which tells the protocol stack that |
cam | 0:0453a0a7e500 | 113 | * a new character can be sent. The protocol stack will then call |
cam | 0:0453a0a7e500 | 114 | * xMBPortSerialPutByte( ) to send the character. |
cam | 0:0453a0a7e500 | 115 | */ |
cam | 0:0453a0a7e500 | 116 | static void prvvUARTTxReadyISR( void ) |
cam | 0:0453a0a7e500 | 117 | { |
cam | 0:0453a0a7e500 | 118 | pxMBFrameCBTransmitterEmpty( ); |
cam | 0:0453a0a7e500 | 119 | } |
cam | 0:0453a0a7e500 | 120 | |
cam | 0:0453a0a7e500 | 121 | /* Create an interrupt handler for the receive interrupt for your target |
cam | 0:0453a0a7e500 | 122 | * processor. This function should then call pxMBFrameCBByteReceived( ). The |
cam | 0:0453a0a7e500 | 123 | * protocol stack will then call xMBPortSerialGetByte( ) to retrieve the |
cam | 0:0453a0a7e500 | 124 | * character. |
cam | 0:0453a0a7e500 | 125 | */ |
cam | 0:0453a0a7e500 | 126 | static void prvvUARTRxISR( void ) |
cam | 0:0453a0a7e500 | 127 | { |
cam | 0:0453a0a7e500 | 128 | pxMBFrameCBByteReceived( ); |
cam | 0:0453a0a7e500 | 129 | } |
cam | 0:0453a0a7e500 | 130 | |
cam | 0:0453a0a7e500 | 131 |