1 /* 2 * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU. 3 * Copyright (c) 2006 Christian Walter <wolti@sil.at> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. The name of the author may not be used to endorse or promote products 15 * derived from this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * 28 * File: $Id: mbport.h,v 1.19 2010/06/06 13:54:40 wolti Exp $ 29 */ 30 31 #ifndef _MB_PORT_H 32 #define _MB_PORT_H 33 34 #include "mbconfig.h" // for options 35 36 #ifdef __cplusplus 37 PR_BEGIN_EXTERN_C 38 #endif 39 40 #if CONFIG_UART_ISR_IN_IRAM 41 #define MB_PORT_SERIAL_ISR_FLAG ESP_INTR_FLAG_IRAM 42 #else 43 #define MB_PORT_SERIAL_ISR_FLAG ESP_INTR_FLAG_LOWMED 44 #endif 45 46 #if MB_PORT_TIMER_ISR_IN_IRAM 47 #define MB_PORT_ISR_ATTR IRAM_ATTR 48 #define MB_PORT_TIMER_ISR_FLAG ESP_INTR_FLAG_IRAM 49 #else 50 #define MB_PORT_ISR_ATTR 51 #define MB_PORT_TIMER_ISR_FLAG ESP_INTR_FLAG_LOWMED 52 #endif 53 54 /* ----------------------- Type definitions ---------------------------------*/ 55 56 typedef enum 57 { 58 EV_READY = 0x01, /*!< Startup finished. */ 59 EV_FRAME_RECEIVED = 0x02, /*!< Frame received. */ 60 EV_EXECUTE = 0x04, /*!< Execute function. */ 61 EV_FRAME_SENT = 0x08, /*!< Frame sent. */ 62 EV_FRAME_TRANSMIT = 0x10 /*!< Frame transmit. */ 63 } eMBEventType; 64 65 #if MB_MASTER_RTU_ENABLED || MB_MASTER_ASCII_ENABLED || MB_MASTER_TCP_ENABLED 66 typedef enum { 67 EV_MASTER_NO_EVENT = 0x0000, 68 EV_MASTER_READY = 0x0001, /*!< Startup finished. */ 69 EV_MASTER_FRAME_RECEIVED = 0x0002, /*!< Frame received. */ 70 EV_MASTER_EXECUTE = 0x0004, /*!< Execute function. */ 71 EV_MASTER_FRAME_SENT = 0x0008, /*!< Frame sent. */ 72 EV_MASTER_FRAME_TRANSMIT = 0x0010, /*!< Frame transmission. */ 73 EV_MASTER_ERROR_PROCESS = 0x0020, /*!< Frame error process. */ 74 EV_MASTER_PROCESS_SUCCESS = 0x0040, /*!< Request process success. */ 75 EV_MASTER_ERROR_RESPOND_TIMEOUT = 0x0080, /*!< Request respond timeout. */ 76 EV_MASTER_ERROR_RECEIVE_DATA = 0x0100, /*!< Request receive data error. */ 77 EV_MASTER_ERROR_EXECUTE_FUNCTION = 0x0200 /*!< Request execute function error. */ 78 } eMBMasterEventType; 79 80 typedef enum { 81 EV_ERROR_INIT, /*!< No error, initial state. */ 82 EV_ERROR_RESPOND_TIMEOUT, /*!< Slave respond timeout. */ 83 EV_ERROR_RECEIVE_DATA, /*!< Receive frame data error. */ 84 EV_ERROR_EXECUTE_FUNCTION, /*!< Execute function error. */ 85 EV_ERROR_OK /*!< No error, processing completed. */ 86 } eMBMasterErrorEventType; 87 #endif 88 89 /*! \ingroup modbus 90 * \brief Parity used for characters in serial mode. 91 * 92 * The parity which should be applied to the characters sent over the serial 93 * link. Please note that this values are actually passed to the porting 94 * layer and therefore not all parity modes might be available. 95 */ 96 typedef enum 97 { 98 MB_PAR_NONE, /*!< No parity. */ 99 MB_PAR_ODD, /*!< Odd parity. */ 100 MB_PAR_EVEN /*!< Even parity. */ 101 } eMBParity; 102 103 /* ----------------------- Supporting functions -----------------------------*/ 104 BOOL xMBPortEventInit( void ); 105 106 BOOL xMBPortEventPost( eMBEventType eEvent ); 107 108 BOOL xMBPortEventGet( /*@out@ */ eMBEventType * eEvent ); 109 110 #if MB_MASTER_RTU_ENABLED || MB_MASTER_ASCII_ENABLED || MB_MASTER_TCP_ENABLED 111 BOOL xMBMasterPortEventInit( void ); 112 113 BOOL xMBMasterPortEventPost( eMBMasterEventType eEvent ); 114 115 BOOL xMBMasterPortEventGet( /*@out@ */ eMBMasterEventType * eEvent ); 116 117 eMBMasterEventType 118 xMBMasterPortFsmWaitConfirmation( eMBMasterEventType eEventMask, ULONG ulTimeout); 119 120 void vMBMasterOsResInit( void ); 121 122 BOOL xMBMasterRunResTake( LONG time ); 123 124 void vMBMasterRunResRelease( void ); 125 #endif // MB_MASTER_RTU_ENABLED || MB_MASTER_ASCII_ENABLED || MB_MASTER_TCP_ENABLED 126 /* ----------------------- Serial port functions ----------------------------*/ 127 128 BOOL xMBPortSerialInit( UCHAR ucPort, ULONG ulBaudRate, 129 UCHAR ucDataBits, eMBParity eParity ); 130 131 void vMBPortClose( void ); 132 133 void xMBPortSerialClose( void ); 134 135 void vMBPortSerialEnable( BOOL xRxEnable, BOOL xTxEnable ); 136 137 BOOL xMBPortSerialGetByte( CHAR * pucByte ); 138 139 BOOL xMBPortSerialPutByte( CHAR ucByte ); 140 141 #if MB_MASTER_RTU_ENABLED || MB_MASTER_ASCII_ENABLED 142 BOOL xMBMasterPortSerialInit( UCHAR ucPort, ULONG ulBaudRate, 143 UCHAR ucDataBits, eMBParity eParity ); 144 145 void vMBMasterPortClose( void ); 146 147 void xMBMasterPortSerialClose( void ); 148 149 void vMBMasterPortSerialEnable( BOOL xRxEnable, BOOL xTxEnable ); 150 151 BOOL xMBMasterPortSerialGetByte( CHAR * pucByte ); 152 153 BOOL xMBMasterPortSerialPutByte( CHAR ucByte ); 154 #endif 155 156 /* ----------------------- Timers functions ---------------------------------*/ 157 BOOL xMBPortTimersInit( USHORT usTimeOut50us ); 158 159 void xMBPortTimersClose( void ); 160 161 void vMBPortTimersEnable( void ); 162 163 void vMBPortTimersDisable( void ); 164 165 void vMBPortTimersDelay( USHORT usTimeOutMS ); 166 167 #if MB_MASTER_RTU_ENABLED || MB_MASTER_ASCII_ENABLED || MB_MASTER_TCP_ENABLED 168 BOOL xMBMasterPortTimersInit( USHORT usTimeOut50us ); 169 170 void xMBMasterPortTimersClose( void ); 171 172 void vMBMasterPortTimersT35Enable( void ); 173 174 void vMBMasterPortTimersConvertDelayEnable( void ); 175 176 void vMBMasterPortTimersRespondTimeoutEnable( void ); 177 178 void vMBMasterPortTimersDisable( void ); 179 180 181 /* ----------------- Callback for the master error process ------------------*/ 182 void vMBMasterErrorCBRespondTimeout( UCHAR ucDestAddress, const UCHAR* pucPDUData, 183 USHORT ucPDULength ); 184 185 void vMBMasterErrorCBReceiveData( UCHAR ucDestAddress, const UCHAR* pucPDUData, 186 USHORT ucPDULength ); 187 188 void vMBMasterErrorCBExecuteFunction( UCHAR ucDestAddress, const UCHAR* pucPDUData, 189 USHORT ucPDULength ); 190 191 void vMBMasterCBRequestSuccess( void ); 192 #endif 193 /* ----------------------- Callback for the protocol stack ------------------*/ 194 /*! 195 * \brief Callback function for the porting layer when a new byte is 196 * available. 197 * 198 * Depending upon the mode this callback function is used by the RTU or 199 * ASCII transmission layers. In any case a call to xMBPortSerialGetByte() 200 * must immediately return a new character. 201 * 202 * \return <code>TRUE</code> if a event was posted to the queue because 203 * a new byte was received. The port implementation should wake up the 204 * tasks which are currently blocked on the eventqueue. 205 */ 206 extern BOOL( *pxMBFrameCBByteReceived ) ( void ); 207 208 extern BOOL( *pxMBFrameCBTransmitterEmpty ) ( void ); 209 210 extern BOOL( *pxMBPortCBTimerExpired ) ( void ); 211 212 #if MB_MASTER_RTU_ENABLED || MB_MASTER_ASCII_ENABLED || MB_MASTER_TCP_ENABLED 213 extern BOOL( *pxMBMasterFrameCBByteReceived ) ( void ); 214 215 extern BOOL( *pxMBMasterFrameCBTransmitterEmpty ) ( void ); 216 217 extern BOOL( *pxMBMasterPortCBTimerExpired ) ( void ); 218 #endif 219 /* ----------------------- TCP port functions -------------------------------*/ 220 #if MB_TCP_ENABLED 221 BOOL xMBTCPPortInit( USHORT usTCPPort ); 222 223 void vMBTCPPortClose( void ); 224 225 void vMBTCPPortEnable( void ); 226 227 void vMBTCPPortDisable( void ); 228 229 BOOL xMBTCPPortGetRequest( UCHAR **ppucMBTCPFrame, USHORT * usTCPLength ); 230 231 BOOL xMBTCPPortSendResponse( UCHAR *pucMBTCPFrame, USHORT usTCPLength ); 232 233 #endif 234 235 #if MB_MASTER_TCP_ENABLED 236 BOOL xMBMasterTCPPortInit( USHORT usTCPPort ); 237 238 void vMBMasterTCPPortClose( void ); 239 240 void vMBMasterTCPPortEnable( void ); 241 242 void vMBMasterTCPPortDisable( void ); 243 244 BOOL xMBMasterTCPPortGetRequest( UCHAR **ppucMBTCPFrame, USHORT * usTCPLength ); 245 246 BOOL xMBMasterTCPPortSendResponse( UCHAR *pucMBTCPFrame, USHORT usTCPLength ); 247 #endif 248 #ifdef __cplusplus 249 PR_END_EXTERN_C 250 #endif 251 #endif 252