Skip to content

Commit 4e365f2

Browse files
committed
Add modbus
1 parent 532afd3 commit 4e365f2

File tree

26 files changed

+1100
-27
lines changed

26 files changed

+1100
-27
lines changed

App/App_main.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
#include "storageHandler.h"
4646
#include "wizchip_conf.h"
4747

48+
#include "mbrtu.h"
49+
4850
/* Private typedef -----------------------------------------------------------*/
4951

5052
/* Private define ------------------------------------------------------------*/
@@ -126,6 +128,11 @@ int main(void)
126128
{
127129
PRT_INFO(" > Serial to Ethernet Gateway Mode\r\n");
128130
}
131+
else
132+
{
133+
PRT_INFO(" > Modbus Mode\r\n");
134+
eMBRTUInit(dev_config->serial_option.baud_rate);
135+
}
129136

130137
if(flag_hw_trig_enable)
131138
{

App/CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@ target_link_libraries(${TARGET_NAME} PRIVATE
1414
hardware_structs
1515
hardware_spi
1616
ETHERNET_FILES
17-
IOLIBRARY_FILES
17+
APP_IOLIBRARY_FILES
1818
MQTT_FILES
1919
MBEDTLS_FILES
2020
DHCP_FILES
2121
DNS_FILES
2222
TIMER_FILES
2323
APP_S2E_FILES
24+
MODBUS_FILES
2425
)
2526
set_target_properties(${TARGET_NAME} PROPERTIES COMPILE_FLAGS "-Wall")
2627
pico_enable_stdio_usb(${TARGET_NAME} 0)
@@ -43,13 +44,14 @@ target_link_libraries(${TARGET_NAME_LINKER} PRIVATE
4344
hardware_structs
4445
hardware_spi
4546
ETHERNET_FILES
46-
IOLIBRARY_FILES
47+
APP_IOLIBRARY_FILES
4748
MQTT_FILES
4849
MBEDTLS_FILES
4950
DHCP_FILES
5051
DNS_FILES
5152
TIMER_FILES
5253
APP_S2E_FILES
54+
MODBUS_FILES
5355
)
5456
set_target_properties(${TARGET_NAME_LINKER} PROPERTIES COMPILE_FLAGS "-Wall")
5557
pico_set_linker_script(${TARGET_NAME_LINKER} ${CMAKE_CURRENT_SOURCE_DIR}/${TARGET_NAME_LINKER}.ld)

Boot/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ target_link_libraries(${TARGET_NAME} PRIVATE
1414
hardware_structs
1515
hardware_spi
1616
ETHERNET_FILES
17-
IOLIBRARY_FILES
17+
BOOT_IOLIBRARY_FILES
1818
DHCP_FILES
1919
DNS_FILES
2020
TIMER_FILES

WIZ5XXSR-RP_c_sdk_version.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ set(WIZ5XXSR-RP-C_SDK_VERSION_MAJOR 1)
33
# WIZ5XXSR-RP-C_BUILD_DEFINE: WIZ5XXSR-RP-C_SDK_VERSION_MINOR, SDK minor version number
44
set(WIZ5XXSR-RP-C_SDK_VERSION_MINOR 0)
55
# WIZ5XXSR-RP-C_BUILD_DEFINE: WIZ5XXSR-RP-C_SDK_VERSION_REVISION, SDK version revision
6-
set(WIZ5XXSR-RP-C_SDK_VERSION_REVISION 7)
6+
set(WIZ5XXSR-RP-C_SDK_VERSION_REVISION 8)
77
# WIZ5XXSR-RP-C_BUILD_DEFINE: WIZ5XXSR-RP-C_SDK_VERSION_STRING, SDK version
88
set(WIZ5XXSR-RP-C_SDK_VERSION_STRING "${WIZ5XXSR-RP-C_SDK_VERSION_MAJOR}.${WIZ5XXSR-RP-C_SDK_VERSION_MINOR}.${WIZ5XXSR-RP-C_SDK_VERSION_REVISION}")
99

port/app/CMakeLists.txt

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
# ioLibrary_Driver
2-
add_library(IOLIBRARY_FILES STATIC)
2+
add_library(APP_IOLIBRARY_FILES STATIC)
33

4-
target_sources(IOLIBRARY_FILES PUBLIC
4+
target_sources(APP_IOLIBRARY_FILES PUBLIC
55
${APP_PORT_DIR}/ioLibrary_Driver/src/w5x00_spi.c
66
${APP_PORT_DIR}/ioLibrary_Driver/src/w5x00_gpio_irq.c
77
)
88

9-
target_include_directories(IOLIBRARY_FILES PUBLIC
9+
target_include_directories(APP_IOLIBRARY_FILES PUBLIC
1010
${WIZNET_DIR}/Ethernet
1111
${APP_PORT_DIR}/ioLibrary_Driver/inc
1212
${APP_PORT_DIR}
1313
)
1414

15-
target_link_libraries(IOLIBRARY_FILES PRIVATE
15+
target_link_libraries(APP_IOLIBRARY_FILES PRIVATE
1616
cmsis_core
1717
pico_stdlib
1818
hardware_adc
@@ -67,9 +67,48 @@ target_link_libraries(MBEDTLS_FILES PRIVATE
6767
DNS_FILES
6868
DHCP_FILES
6969
ETHERNET_FILES
70-
IOLIBRARY_FILES
70+
APP_IOLIBRARY_FILES
7171
APP_S2E_FILES
7272
MQTT_FILES
73+
MODBUS_FILES
74+
mbedcrypto
75+
mbedx509
76+
mbedtls
77+
)
78+
79+
add_library(MODBUS_FILES STATIC)
80+
81+
target_sources(MODBUS_FILES PUBLIC
82+
${APP_PORT_DIR}/modbus/src/mb.c
83+
${APP_PORT_DIR}/modbus/src/mbascii.c
84+
${APP_PORT_DIR}/modbus/src/mbcrc.c
85+
${APP_PORT_DIR}/modbus/src/mbrtu.c
86+
${APP_PORT_DIR}/modbus/src/mbtcp.c
87+
${APP_PORT_DIR}/modbus/src/mbtimer.c
88+
${APP_PORT_DIR}/modbus/src/mbserial.c
89+
)
90+
91+
target_include_directories(MODBUS_FILES PUBLIC
92+
${APP_PORT_DIR}/modbus/inc
93+
${APP_PORT_DIR}
94+
)
95+
96+
target_link_libraries(MODBUS_FILES PRIVATE
97+
cmsis_core
98+
pico_stdlib
99+
hardware_adc
100+
hardware_clocks
101+
hardware_dma
102+
hardware_flash
103+
hardware_resets
104+
hardware_spi
105+
DNS_FILES
106+
DHCP_FILES
107+
ETHERNET_FILES
108+
APP_IOLIBRARY_FILES
109+
APP_S2E_FILES
110+
MBEDTLS_FILES
111+
MQTT_FILES
73112
mbedcrypto
74113
mbedx509
75114
mbedtls
@@ -117,9 +156,10 @@ target_link_libraries(APP_S2E_FILES PRIVATE
117156
DNS_FILES
118157
DHCP_FILES
119158
ETHERNET_FILES
120-
IOLIBRARY_FILES
159+
APP_IOLIBRARY_FILES
121160
MBEDTLS_FILES
122161
MQTT_FILES
162+
MODBUS_FILES
123163
mbedcrypto
124164
mbedx509
125165
mbedtls

port/app/configuration/inc/common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
/* Application Firmware Version */
1010
#define MAJOR_VER 1
1111
#define MINOR_VER 0
12-
#define MAINTENANCE_VER 7
12+
#define MAINTENANCE_VER 8
1313

1414
#define DEV_CONFIG_VER 103
1515

port/app/configuration/inc/segcp.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ typedef enum {SEGCP_MC, SEGCP_VR, SEGCP_MN, SEGCP_IM, SEGCP_OP, SEGCP_DD, SEGCP_
4444
// Command [K1] : Hidden command, This command erase the configutation data in flash / or EEPROM
4545
// S2E SSL from SEGCP_RC
4646
typedef enum {SEGCP_MC, SEGCP_VR, SEGCP_MN, SEGCP_IM, SEGCP_OP, SEGCP_CP, SEGCP_DG, SEGCP_KA, SEGCP_KI, SEGCP_KE,
47-
SEGCP_RI, SEGCP_LI, SEGCP_SM, SEGCP_GW, SEGCP_DS, SEGCP_DH, SEGCP_LP, SEGCP_RP, SEGCP_RH, SEGCP_BR,
48-
SEGCP_DB, SEGCP_PR, SEGCP_SB, SEGCP_FL, SEGCP_IT, SEGCP_PT, SEGCP_PS, SEGCP_PD, SEGCP_TE, SEGCP_SS,
49-
SEGCP_NP, SEGCP_SP, SEGCP_MA, SEGCP_PW, SEGCP_SV, SEGCP_EX, SEGCP_RT, SEGCP_UN, SEGCP_ST, SEGCP_FR,
50-
SEGCP_EC, SEGCP_GA, SEGCP_GB, SEGCP_GC, SEGCP_GD, SEGCP_CA, SEGCP_CB, SEGCP_CC, SEGCP_CD, SEGCP_SC,
51-
SEGCP_S0, SEGCP_S1, SEGCP_RX, SEGCP_UI, SEGCP_TR, SEGCP_QU, SEGCP_QP, SEGCP_QC, SEGCP_QK, SEGCP_PU,
52-
SEGCP_U0, SEGCP_U1, SEGCP_U2, SEGCP_QO, SEGCP_RC, SEGCP_CE, SEGCP_OC, SEGCP_LC, SEGCP_PK, SEGCP_UF,
53-
SEGCP_FW, SEGCP_SO, SEGCP_UNKNOWN=255
47+
SEGCP_RI, SEGCP_LI, SEGCP_SM, SEGCP_GW, SEGCP_DS, SEGCP_DH, SEGCP_LP, SEGCP_RP, SEGCP_RH, SEGCP_BR,
48+
SEGCP_DB, SEGCP_PR, SEGCP_SB, SEGCP_FL, SEGCP_PO, SEGCP_IT, SEGCP_PT, SEGCP_PS, SEGCP_PD, SEGCP_TE,
49+
SEGCP_SS, SEGCP_NP, SEGCP_SP, SEGCP_MA, SEGCP_PW, SEGCP_SV, SEGCP_EX, SEGCP_RT, SEGCP_UN, SEGCP_ST,
50+
SEGCP_FR, SEGCP_EC, SEGCP_GA, SEGCP_GB, SEGCP_GC, SEGCP_GD, SEGCP_CA, SEGCP_CB, SEGCP_CC, SEGCP_CD,
51+
SEGCP_SC, SEGCP_S0, SEGCP_S1, SEGCP_RX, SEGCP_UI, SEGCP_TR, SEGCP_QU, SEGCP_QP, SEGCP_QC, SEGCP_QK,
52+
SEGCP_PU, SEGCP_U0, SEGCP_U1, SEGCP_U2, SEGCP_QO, SEGCP_RC, SEGCP_CE, SEGCP_OC, SEGCP_LC, SEGCP_PK,
53+
SEGCP_UF, SEGCP_FW, SEGCP_SO, SEGCP_UNKNOWN=255
5454

5555
} teSEGCPCMDNUM;
5656

port/app/configuration/src/segcp.c

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@ uint8_t * tbSEGCPCMD[] = {"MC", "VR", "MN", "IM", "OP", "DD", "CP", "PO", "DG",
5757
#else
5858

5959
uint8_t * tbSEGCPCMD[] = {"MC", "VR", "MN", "IM", "OP", "CP", "DG", "KA", "KI", "KE",
60-
"RI", "LI", "SM", "GW", "DS", "DH", "LP", "RP", "RH", "BR",
61-
"DB", "PR", "SB", "FL", "IT", "PT", "PS", "PD", "TE", "SS",
62-
"NP", "SP", "MA", "PW", "SV", "EX", "RT", "UN", "ST", "FR",
63-
"EC", "GA", "GB", "GC", "GD", "CA", "CB", "CC", "CD", "SC",
64-
"S0", "S1", "RX", "UI", "TR", "QU", "QP", "QC", "QK", "PU",
65-
"U0", "U1", "U2", "QO", "RC", "CE", "OC", "LC", "PK", "UF",
66-
"FW", "SO", 0};
60+
"RI", "LI", "SM", "GW", "DS", "DH", "LP", "RP", "RH", "BR",
61+
"DB", "PR", "SB", "FL", "PO", "IT", "PT", "PS", "PD", "TE",
62+
"SS", "NP", "SP", "MA", "PW", "SV", "EX", "RT", "UN", "ST",
63+
"FR", "EC", "GA", "GB", "GC", "GD", "CA", "CB", "CC", "CD",
64+
"SC", "S0", "S1", "RX", "UI", "TR", "QU", "QP", "QC", "QK",
65+
"PU", "U0", "U1", "U2", "QO", "RC", "CE", "OC", "LC", "PK",
66+
"UF", "FW", "SO", 0};
6767

6868
#endif
6969
uint8_t * tbSEGCPERR[] = {"ERNULL", "ERNOTAVAIL", "ERNOPARAM", "ERIGNORED", "ERNOCOMMAND", "ERINVALIDPARAM", "ERNOPRIVILEGE"};
@@ -427,6 +427,8 @@ uint16_t proc_SEGCP(uint8_t* segcp_req, uint8_t* segcp_rep)
427427
break;
428428
case SEGCP_FL: sprintf(trep, "%d", dev_config->serial_option.flow_control);
429429
break;
430+
case SEGCP_PO: sprintf(trep, "%d", dev_config->serial_option.protocol);
431+
break;
430432
case SEGCP_IT: sprintf(trep, "%d", dev_config->tcp_option.inactivity);
431433
break;
432434
case SEGCP_PT: sprintf(trep, "%d", dev_config->serial_data_packing.packing_time);
@@ -790,6 +792,11 @@ uint16_t proc_SEGCP(uint8_t* segcp_req, uint8_t* segcp_rep)
790792
dev_config->serial_option.flow_control = tmp_byte;
791793
}
792794
break;
795+
case SEGCP_PO:
796+
tmp_int = atoi(param);
797+
if(param_len > 2 || tmp_int > modbus_ascii) ret |= SEGCP_RET_ERR_INVALIDPARAM;
798+
else dev_config->serial_option.protocol = tmp_int;
799+
break;
793800
case SEGCP_IT:
794801
tmp_long = atol(param);
795802
if(tmp_long > 0xFFFF) ret |= SEGCP_RET_ERR_INVALIDPARAM;

port/app/modbus/inc/mb.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#ifndef _MB_H_
2+
#define _MB_H_
3+
4+
void mbTCPtoRTU(uint8_t sock);
5+
void mbRTUtoTCP(uint8_t sock);
6+
void mbASCIItoTCP(uint8_t sock);
7+
void mbTCPtoASCII(uint8_t sock);
8+
#endif
9+

port/app/modbus/inc/mbascii.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include <stdbool.h>
2+
3+
#ifndef _MBASCII_H
4+
#define _MBASCII_H
5+
6+
#define MB_ASCII_START ':'
7+
#define MB_ASCII_DEFAULT_CR '\r' /*!< Default CR character for Modbus ASCII. */
8+
#define MB_ASCII_DEFAULT_LF '\n' /*!< Default LF character for Modbus ASCII. */
9+
10+
extern volatile uint8_t mb_state_ascii_finish;
11+
12+
bool MBtcp2asciiFrame(uint8_t sock);
13+
bool MBascii2tcpFrame(void);
14+
uint8_t prvucMBLRC( uint8_t * pucFrame, uint16_t usLen );
15+
uint8_t prvucMBCHAR2BIN( uint8_t ucCharacter );
16+
uint8_t prvucMBBIN2CHAR( uint8_t ucByte );
17+
void ASCII_Uart_RX(void);
18+
#endif
19+

0 commit comments

Comments
 (0)