1 /* 2 * SPDX-FileCopyrightText: 2010 Christian Walter 3 * 4 * SPDX-License-Identifier: BSD-3-Clause 5 * 6 * SPDX-FileContributor: 2016-2021 Espressif Systems (Shanghai) CO LTD 7 */ 8 /* 9 * FreeModbus Libary: ESP32 Port 10 * Copyright (C) 2010 Christian Walter <cwalter@embedded-solutions.at> 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. The name of the author may not be used to endorse or promote products 21 * derived from this software without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 25 * IF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 * 34 * File: $Id: port.h,v 1.1 2010/06/06 13:07:20 wolti Exp $ 35 */ 36 37 #ifndef PORT_COMMON_H_ 38 #define PORT_COMMON_H_ 39 40 #include "freertos/FreeRTOS.h" 41 #include "esp_log.h" // for ESP_LOGE macro 42 #include "mbconfig.h" 43 44 #define INLINE inline 45 #define PR_BEGIN_EXTERN_C extern "C" { 46 #define PR_END_EXTERN_C } 47 48 #define MB_PORT_TAG "MB_PORT_COMMON" 49 50 #define MB_BAUD_RATE_DEFAULT (115200) 51 #define MB_QUEUE_LENGTH (CONFIG_FMB_QUEUE_LENGTH) 52 53 #define MB_SERIAL_TASK_PRIO (CONFIG_FMB_PORT_TASK_PRIO) 54 #define MB_SERIAL_TASK_STACK_SIZE (CONFIG_FMB_PORT_TASK_STACK_SIZE) 55 #define MB_SERIAL_TOUT (3) // 3.5*8 = 28 ticks, TOUT=3 -> ~24..33 ticks 56 57 // Set buffer size for transmission 58 #define MB_SERIAL_BUF_SIZE (CONFIG_FMB_SERIAL_BUF_SIZE) 59 60 // common definitions for serial port implementations 61 #define MB_SERIAL_TX_TOUT_MS (2200) // maximum time for transmission of longest allowed frame buffer 62 #define MB_SERIAL_TX_TOUT_TICKS (pdMS_TO_TICKS(MB_SERIAL_TX_TOUT_MS)) // timeout for transmission 63 #define MB_SERIAL_RX_TOUT_MS (1) 64 #define MB_SERIAL_RX_TOUT_TICKS (pdMS_TO_TICKS(MB_SERIAL_RX_TOUT_MS)) // timeout for receive 65 66 #define MB_SERIAL_RESP_LEN_MIN (4) 67 68 // Common definitions for TCP port 69 #define MB_TCP_BUF_SIZE (256 + 7) // Must hold a complete Modbus TCP frame. 70 #define MB_TCP_DEFAULT_PORT (CONFIG_FMB_TCP_PORT_DEFAULT) 71 #define MB_TCP_STACK_SIZE (CONFIG_FMB_PORT_TASK_STACK_SIZE) 72 #define MB_TCP_TASK_PRIO (CONFIG_FMB_PORT_TASK_PRIO) 73 74 // The task affinity for Modbus stack tasks 75 #define MB_PORT_TASK_AFFINITY (CONFIG_FMB_PORT_TASK_AFFINITY) 76 77 #define MB_TCP_READ_TIMEOUT_MS (100) // read timeout in mS 78 #define MB_TCP_READ_TIMEOUT (pdMS_TO_TICKS(MB_TCP_READ_TIMEOUT_MS)) 79 #define MB_TCP_SEND_TIMEOUT_MS (500) // send event timeout in mS 80 #define MB_TCP_SEND_TIMEOUT (pdMS_TO_TICKS(MB_TCP_SEND_TIMEOUT_MS)) 81 #define MB_TCP_PORT_MAX_CONN (CONFIG_FMB_TCP_PORT_MAX_CONN) 82 83 #define MB_TCP_FRAME_LOG_BUFSIZE (256) 84 85 #define MB_PORT_HAS_CLOSE (1) // Define to explicitly close port on destroy 86 87 // Define number of timer reloads per 1 mS 88 #define MB_TIMER_TICS_PER_MS (20UL) 89 90 #define MB_TCP_DEBUG (LOG_LOCAL_LEVEL >= ESP_LOG_DEBUG) // Enable legacy debug output in TCP module. 91 92 #define MB_TCP_GET_FIELD(buffer, field) ((USHORT)((buffer[field] << 8U) | buffer[field + 1])) 93 94 #define MB_PORT_CHECK(a, ret_val, str, ...) \ 95 if (!(a)) { \ 96 ESP_LOGE(MB_PORT_TAG, "%s(%u): " str, __FUNCTION__, __LINE__, ##__VA_ARGS__); \ 97 return ret_val; \ 98 } 99 100 #ifdef __cplusplus 101 PR_BEGIN_EXTERN_C 102 #endif /* __cplusplus */ 103 104 #ifndef TRUE 105 #define TRUE 1 106 #endif 107 108 #ifndef FALSE 109 #define FALSE 0 110 #endif 111 112 typedef char BOOL; 113 114 typedef unsigned char UCHAR; 115 typedef char CHAR; 116 117 typedef unsigned short USHORT; 118 typedef short SHORT; 119 120 typedef unsigned long ULONG; 121 typedef long LONG; 122 123 #if MB_TCP_DEBUG 124 typedef enum 125 { 126 MB_LOG_DEBUG, 127 MB_LOG_INFO, 128 MB_LOG_WARN, 129 MB_LOG_ERROR 130 } eMBPortLogLevel; 131 #endif 132 133 typedef enum 134 { 135 MB_PROTO_TCP, 136 MB_PROTO_UDP, 137 } eMBPortProto; 138 139 typedef enum { 140 MB_PORT_IPV4 = 0, /*!< TCP IPV4 addressing */ 141 MB_PORT_IPV6 = 1 /*!< TCP IPV6 addressing */ 142 } eMBPortIpVer; 143 144 void vMBPortEnterCritical(void); 145 void vMBPortExitCritical(void); 146 147 #define ENTER_CRITICAL_SECTION( ) { ESP_EARLY_LOGD(MB_PORT_TAG,"%s: Port enter critical.", __func__); \ 148 vMBPortEnterCritical(); } 149 150 #define EXIT_CRITICAL_SECTION( ) { vMBPortExitCritical(); \ 151 ESP_EARLY_LOGD(MB_PORT_TAG,"%s: Port exit critical", __func__); } 152 153 #define MB_PORT_CHECK_EVENT( event, mask ) ( event & mask ) 154 #define MB_PORT_CLEAR_EVENT( event, mask ) do { event &= ~mask; } while(0) 155 156 #define MB_PORT_PARITY_GET(parity) ((parity != UART_PARITY_DISABLE) ? \ 157 ((parity == UART_PARITY_ODD) ? MB_PAR_ODD : MB_PAR_EVEN) : MB_PAR_NONE) 158 159 // Legacy Modbus logging function 160 #if MB_TCP_DEBUG 161 void vMBPortLog( eMBPortLogLevel eLevel, const CHAR * szModule, 162 const CHAR * szFmt, ... ); 163 void prvvMBTCPLogFrame( const CHAR * pucMsg, UCHAR * pucFrame, USHORT usFrameLen ); 164 #endif 165 166 void vMBPortSetMode( UCHAR ucMode ); 167 UCHAR ucMBPortGetMode( void ); 168 169 #ifdef __cplusplus 170 PR_END_EXTERN_C 171 #endif /* __cplusplus */ 172 173 #endif /* PORT_COMMON_H_ */ 174