Adicionar primeiro
This commit is contained in:
@@ -0,0 +1 @@
|
||||
351350613ceafba240b761b4ea991e0f231ac7a9f59a9ee901f751bddc0bb18f
|
||||
89
managed_components/espressif__cmake_utilities/CHANGELOG.md
Normal file
89
managed_components/espressif__cmake_utilities/CHANGELOG.md
Normal file
@@ -0,0 +1,89 @@
|
||||
## v0.5.3 - 2023-09-15
|
||||
|
||||
* fix `add_dependencies called with incorrect number of arguments` in `relinker.cmake`
|
||||
* `include(cmake_utilities)` is not suggested now, to avoid cmake_utilities dependency issue
|
||||
|
||||
## v0.5.2 - 2023-09-15
|
||||
|
||||
* Support work on older ESP-IDF, eg: 4.3.x
|
||||
|
||||
## v0.5.1 - 2023-08-22
|
||||
|
||||
* Add string 1-byte align support
|
||||
|
||||
## v0.5.0 - 2023-08-02
|
||||
|
||||
* Add GCC LTO support
|
||||
|
||||
## v0.4.8 - 2023-05-24
|
||||
|
||||
* Add unit test app
|
||||
|
||||
### Bugfix:
|
||||
|
||||
* fix customer target redefinition issue
|
||||
|
||||
## v0.4.7 - 2023-04-21
|
||||
|
||||
* gen_compressed_ota: support the addition of a v2 compressed OTA header to compressed firmware.
|
||||
|
||||
## v0.4.6 - 2023-04-20
|
||||
|
||||
* relinker: add IDF v4.3.x support
|
||||
|
||||
## v0.4.5 - 2023-04-17
|
||||
|
||||
* gen_compressed_ota: remove slash use in gen_custom_ota.py so that the script can be used in the Windows cmd terminal.
|
||||
|
||||
## v0.4.4 - 2023-04-07
|
||||
|
||||
* relinker: suppressing the creation of `__pycache__`
|
||||
* relinker: support same name objects in one library
|
||||
|
||||
## v0.4.3 - 2023-03-24
|
||||
|
||||
* relinker: support decoding to get IRAM excluded libraries
|
||||
|
||||
## v0.4.2 - 2023-03-20
|
||||
|
||||
### Bugfix:
|
||||
|
||||
* gen_compressed_ota: Fix the number of bytes reserved in the v3 compressed image header
|
||||
* gen_compressed_ota: Fix definition of MD5 length in compressed image header for different versions.
|
||||
|
||||
## v0.4.1 - 2023-03-15
|
||||
|
||||
* relinker: add option to use customized configuration files
|
||||
* relinker: add option to print error information instead of throwing exception when missing function
|
||||
* relinker: move functions of SPI flash from IRAM to flash only when enable CONFIG_SPI_FLASH_ROM_IMPL
|
||||
* relinker: move some functions of esp_timer from IRAM to flash only when disable CONFIG_ESP_TIMER_SUPPORTS_ISR_DISPATCH_METHOD
|
||||
|
||||
## v0.4.0 - 2023-03-13
|
||||
|
||||
### Feature:
|
||||
|
||||
* Add command `idf.py gen_single_bin` to generate merged bin file
|
||||
* Add command `idf.py flash_single_bin` to flash generated merged bin
|
||||
* Add config `Color in diagnostics` to control the GCC color output
|
||||
|
||||
## v0.3.0 - 2023-03-10
|
||||
|
||||
* Add gen_compressed_ota functionality, please refer to [gen_compressed_ota.md](https://github.com/espressif/esp-iot-solution/tree/master/tools/cmake_utilities/docs/gen_compressed_ota.md)
|
||||
|
||||
## v0.2.1 - 2023-03-09
|
||||
|
||||
### Bugfix:
|
||||
|
||||
* package manager: Fix the compile issue when the name of the component has `-`, just like esp-xxx
|
||||
|
||||
## v0.2.0 - 2023-02-23
|
||||
|
||||
* Add relinker functionality, please refer to [relinker.md](https://github.com/espressif/esp-iot-solution/tree/master/tools/cmake_utilities/docs/relinker.md)
|
||||
|
||||
## v0.1.0 - 2023-01-12
|
||||
|
||||
### Feature:
|
||||
|
||||
* Add function cu_pkg_get_version
|
||||
* Add macro cu_pkg_define_version
|
||||
* Add cmake script to CMAKE_MODULE_PATH
|
||||
@@ -0,0 +1 @@
|
||||
idf_component_register()
|
||||
65
managed_components/espressif__cmake_utilities/Kconfig
Normal file
65
managed_components/espressif__cmake_utilities/Kconfig
Normal file
@@ -0,0 +1,65 @@
|
||||
menu "CMake Utilities"
|
||||
|
||||
config CU_RELINKER_ENABLE
|
||||
bool "Enable relinker"
|
||||
default n
|
||||
help
|
||||
"Enable relinker to linker some IRAM functions to Flash"
|
||||
|
||||
if CU_RELINKER_ENABLE
|
||||
config CU_RELINKER_ENABLE_PRINT_ERROR_INFO_WHEN_MISSING_FUNCTION
|
||||
bool "Print error information when missing function"
|
||||
default y
|
||||
help
|
||||
"Enable this option to print error information instead of
|
||||
throwing exception when missing function"
|
||||
|
||||
config CU_RELINKER_ENABLE_CUSTOMIZED_CONFIGURATION_FILES
|
||||
bool "Enable customized relinker configuration files"
|
||||
default n
|
||||
help
|
||||
"Enable this option to use customized relinker configuration
|
||||
files instead of default ones"
|
||||
|
||||
if CU_RELINKER_ENABLE_CUSTOMIZED_CONFIGURATION_FILES
|
||||
config CU_RELINKER_CUSTOMIZED_CONFIGURATION_FILES_PATH
|
||||
string "Customized relinker configuration files path"
|
||||
default ""
|
||||
help
|
||||
"Customized relinker configuration files path. This path is
|
||||
evaluated relative to the project root directory."
|
||||
endif
|
||||
endif
|
||||
|
||||
choice CU_DIAGNOSTICS_COLOR
|
||||
prompt "Color in diagnostics"
|
||||
default CU_DIAGNOSTICS_COLOR_ALWAYS
|
||||
help
|
||||
Use color in diagnostics. "never", "always", or "auto". If "always", GCC will output
|
||||
with color defined in GCC_COLORS environment variable. If "never", only output plain
|
||||
text. If "auto", only output with color when the standard error is a terminal and when
|
||||
not executing in an emacs shell.
|
||||
|
||||
config CU_DIAGNOSTICS_COLOR_NEVER
|
||||
bool "never"
|
||||
config CU_DIAGNOSTICS_COLOR_ALWAYS
|
||||
bool "always"
|
||||
config CU_DIAGNOSTICS_COLOR_AUTO
|
||||
bool "auto"
|
||||
endchoice
|
||||
|
||||
config CU_GCC_LTO_ENABLE
|
||||
bool "Enable GCC link time optimization(LTO)"
|
||||
default n
|
||||
help
|
||||
"Enable this option, users can enable GCC link time optimization(LTO)
|
||||
feature for target components or dependencies.
|
||||
|
||||
config CU_GCC_STRING_1BYTE_ALIGN
|
||||
bool "GCC string 1-byte align"
|
||||
default n
|
||||
help
|
||||
"Enable this option, user can make string in designated components align
|
||||
by 1-byte instead 1-word(4-byte), this can reduce unnecessary filled data
|
||||
so that to reduce firmware size."
|
||||
endmenu
|
||||
31
managed_components/espressif__cmake_utilities/README.md
Normal file
31
managed_components/espressif__cmake_utilities/README.md
Normal file
@@ -0,0 +1,31 @@
|
||||
# Cmake utilities
|
||||
|
||||
[](https://components.espressif.com/components/espressif/cmake_utilities)
|
||||
|
||||
This component is aiming to provide some useful CMake utilities outside of ESP-IDF.
|
||||
|
||||
## Use
|
||||
|
||||
1. Add dependency of this component in your component or project's idf_component.yml.
|
||||
|
||||
```yml
|
||||
dependencies:
|
||||
espressif/cmake_utilities: "0.*"
|
||||
```
|
||||
|
||||
2. Include the CMake file you need in your component's CMakeLists.txt after `idf_component_register`, or in your project's CMakeLists.txt
|
||||
|
||||
```cmake
|
||||
// Note: should remove .cmake postfix when using include(), otherwise the requested file will not found
|
||||
// Note: should place this line after `idf_component_register` function
|
||||
// only include the one you needed.
|
||||
include(package_manager)
|
||||
```
|
||||
|
||||
3. Then you can use the corresponding CMake function which is provided by the CMake file.
|
||||
|
||||
## Supported features
|
||||
|
||||
1. [relinker](https://github.com/espressif/esp-iot-solution/blob/master/tools/cmake_utilities/docs/relinker.md)
|
||||
2. [gen_compressed_ota](https://github.com/espressif/esp-iot-solution/blob/master/tools/cmake_utilities/docs/gen_compressed_ota.md)
|
||||
3. [GCC Optimization](https://github.com/espressif/esp-iot-solution/blob/master/tools/cmake_utilities/docs/gcc.md)
|
||||
@@ -0,0 +1,7 @@
|
||||
# Include all cmake modules
|
||||
|
||||
include(gcc)
|
||||
include(gen_compressed_ota)
|
||||
include(gen_single_bin)
|
||||
include(package_manager)
|
||||
include(relinker)
|
||||
108
managed_components/espressif__cmake_utilities/docs/gcc.md
Normal file
108
managed_components/espressif__cmake_utilities/docs/gcc.md
Normal file
@@ -0,0 +1,108 @@
|
||||
# Link Time Optimization(LTO)
|
||||
|
||||
Link time optimization(LTO) improves the optimization effect of GCC, such as reducing binary size, increasing performance, and so on. For more details please refer to related [GCC documents](https://gcc.gnu.org/onlinedocs/gccint/LTO.html).
|
||||
|
||||
## Use
|
||||
|
||||
To use this feature, you need to include the required CMake file in your project's CMakeLists.txt after `project(XXXX)`.
|
||||
|
||||
```cmake
|
||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||
|
||||
project(XXXX)
|
||||
|
||||
include(gcc)
|
||||
```
|
||||
|
||||
The LTO feature is disabled by default. To use it, you should enable the option `CU_GCC_LTO_ENABLE` in menuconfig. Then specify target components or dependencies to be optimized by LTO after `include(gcc)` as follows:
|
||||
|
||||
```cmake
|
||||
include(gcc)
|
||||
|
||||
cu_gcc_lto_set(COMPONENTS component_a component_b
|
||||
DEPENDS dependence_a dependence_b)
|
||||
|
||||
cu_gcc_string_1byte_align(COMPONENTS component_c component_d
|
||||
DEPENDS dependence_c dependence_d)
|
||||
```
|
||||
|
||||
Based on your requirement, set compiling optimization level in the option `COMPILER_OPTIMIZATION`.
|
||||
|
||||
* Note
|
||||
|
||||
```
|
||||
1. Reducing firmware size may decrease performance
|
||||
2. Increasing performance may increase firmware size
|
||||
3. Enable LTO cause compiling time cost increases a lot
|
||||
4. Enable LTO may increase task stack cost
|
||||
5. Enable string 1-byte align may decrease string process speed
|
||||
```
|
||||
|
||||
## Limitation
|
||||
|
||||
At the linking stage, the LTO generates new function indexes instead of the file path as follows:
|
||||
|
||||
- LTO
|
||||
|
||||
```txt
|
||||
.text 0x00000000420016f4 0x6 /tmp/ccdjwYMH.ltrans51.ltrans.o
|
||||
0x00000000420016f4 app_main
|
||||
```
|
||||
|
||||
- Without LTO
|
||||
|
||||
```txt
|
||||
.text.app_main 0x00000000420016f4 0x6 esp-idf/main/libmain.a(app_main.c.obj)
|
||||
0x00000000420016f4 app_main
|
||||
```
|
||||
|
||||
So tools used to relink functions between flash and IRAM can't affect these optimized components and dependencies again. It is recommended that users had better optimize application components and dependencies than kernel and hardware driver ones.
|
||||
|
||||
## Example
|
||||
|
||||
The example applies LTO in `light` of `esp-matter` because its application code is much larger. Add LTO configuration into project script `CMakeLists.txt` as follows:
|
||||
|
||||
```cmake
|
||||
|
||||
project(light)
|
||||
|
||||
include(gcc)
|
||||
|
||||
# Add
|
||||
set(app_lto_components main chip esp_matter)
|
||||
# Add
|
||||
set(idf_lto_components lwip wpa_supplicant nvs_flash)
|
||||
# Add
|
||||
set(lto_depends mbedcrypto)
|
||||
|
||||
# Add
|
||||
cu_gcc_lto_set(COMPONENTS ${app_lto_components} ${idf_lto_components}
|
||||
DEPENDS ${lto_depends})
|
||||
```
|
||||
|
||||
Configure `ESP32-C2` as the target platform, enable `CU_GCC_LTO_ENABLE` and `CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_DISABLE`, set `COMPILER_OPTIMIZATION` to be `-Os`.
|
||||
Increase the `main` task stack size to `5120` by option `ESP_MAIN_TASK_STACK_SIZE`.
|
||||
Compile the project, and then you can see the firmware size decrease a lot:
|
||||
|
||||
Option | Firmware size | Stask cost
|
||||
|:-:|:-:|:-:|
|
||||
-Os | 1,113,376 | 2508
|
||||
-Os + LTO | 1,020,640 | 4204
|
||||
|
||||
Then add `cu_gcc_string_1byte_align` after `cu_gcc_lto_set`:
|
||||
|
||||
```cmake
|
||||
# Add
|
||||
cu_gcc_lto_set(COMPONENTS ${app_lto_components} ${idf_lto_components}
|
||||
DEPENDS ${lto_depends})
|
||||
|
||||
cu_gcc_string_1byte_align(COMPONENTS ${app_lto_components} ${idf_lto_components}
|
||||
DEPENDS ${lto_depends})
|
||||
```
|
||||
|
||||
Build the project and the firmware size is:
|
||||
|
||||
Option | Firmware size |
|
||||
|:-:|:-:|
|
||||
-Os + LTO | 1,020,640 |
|
||||
-Os + LTO + string 1-byte align | 1,018,340 |
|
||||
@@ -0,0 +1,41 @@
|
||||
# Gen Compressed OTA
|
||||
|
||||
When using the compressed OTA, we need to generate the compressed app firmware. This document mainly describes how to generate the compressed app firmware.
|
||||
|
||||
For more information about compressed OTA, refer to [bootloader_support_plus](https://github.com/espressif/esp-iot-solution/tree/master/components/bootloader_support_plus).
|
||||
|
||||
## Use
|
||||
In order to use this feature, you need to include the needed CMake file in your project's CMakeLists.txt after `project(XXXX)`.
|
||||
|
||||
```cmake
|
||||
project(XXXX)
|
||||
|
||||
include(gen_compressed_ota)
|
||||
```
|
||||
|
||||
Generate the compressed app firmware in an ESP-IDF "project" directory by running:
|
||||
|
||||
```plaintext
|
||||
idf.py gen_compressed_ota
|
||||
```
|
||||
|
||||
This command will compile your project first, then it will generate the compressed app firmware. For example, run the command under the project `simple_ota_examples` folder. If there are no errors, the `custom_ota_binaries` folder will be created and contains the following files:
|
||||
|
||||
```plaintext
|
||||
simple_ota.bin.xz
|
||||
simple_ota.bin.xz.packed
|
||||
```
|
||||
|
||||
The file named `simple_ota.bin.xz.packed` is the actual compressed app binary file to be transferred.
|
||||
|
||||
In addition, if [secure boot](https://docs.espressif.com/projects/esp-idf/en/latest/esp32c3/security/secure-boot-v2.html) is enabled, the command will generate the signed compressed app binary file:
|
||||
|
||||
```plaintext
|
||||
simple_ota.bin.xz.packed.signed
|
||||
```
|
||||
|
||||
you can also use the script [gen_custom_ota.py](https://github.com/espressif/esp-iot-solution/tree/master/tools/cmake_utilities/scripts/gen_custom_ota.py) to compress the specified app:
|
||||
|
||||
```plaintext
|
||||
python3 gen_custom_ota.py -i simple_ota.bin
|
||||
```
|
||||
@@ -0,0 +1,66 @@
|
||||
# Relinker
|
||||
|
||||
In ESP-IDF, some functions are put in SRAM when link stage, the reason is that some functions are critical, we need to put them in SRAM to speed up the program, or the functions will be executed when the cache is disabled. But actually, some functions can be put into Flash, here, we provide a script to let the user set the functions which are located in SRAM by default to put them into Flash, in order to save more SRAM which can be used as heap region later. This happens in the linker stage, so we call it as relinker.
|
||||
|
||||
## Use
|
||||
|
||||
In order to use this feature, you need to include the needed CMake file in your project's CMakeLists.txt after `project(XXXX)`.
|
||||
|
||||
```cmake
|
||||
project(XXXX)
|
||||
|
||||
include(relinker)
|
||||
```
|
||||
|
||||
The relinker feature is disabled by default, in order to use it, you need to enable the option `CU_RELINKER_ENABLE` in menuconfig.
|
||||
|
||||
Here are the default configuration files in the folder `cmake_utilities/scripts/relinker/examples/esp32c2`, it's just used as a reference. If you would like to use your own configuration files, please enable option `CU_RELINKER_ENABLE_CUSTOMIZED_CONFIGURATION_FILES` and set the path of your configuration files as following, this path is evaluated relative to the project root directory:
|
||||
|
||||
```
|
||||
[*] Enable customized relinker configuration files
|
||||
(path of your configuration files) Customized relinker configuration files path
|
||||
```
|
||||
|
||||
> Note: Currently only esp32c2 is supported.
|
||||
|
||||
## Configuration Files
|
||||
|
||||
You can refer to the files in the directory of `cmake_utilities/scripts/relinker/examples/esp32c2`:
|
||||
|
||||
- library.csv
|
||||
- object.csv
|
||||
- function.csv
|
||||
|
||||
For example, if you want to link function `__getreent` from SRAM to Flash, firstly you should add it to `function.csv` file as following:
|
||||
|
||||
```
|
||||
libfreertos.a,tasks.c.obj,__getreent,
|
||||
```
|
||||
|
||||
This means function `__getreent` is in object file `tasks.c.obj`, and object file `tasks.c.obj` is in library `libfreertos.a`.
|
||||
|
||||
If function `__getreent` depends on the option `FREERTOS_PLACE_FUNCTIONS_INTO_FLASH` in menuconfig, then it should be:
|
||||
|
||||
```
|
||||
libfreertos.a,tasks.c.obj,__getreent,CONFIG_FREERTOS_PLACE_FUNCTIONS_INTO_FLASH
|
||||
```
|
||||
|
||||
This means when only `FREERTOS_PLACE_FUNCTIONS_INTO_FLASH` is enabled in menuconfig, function `__getreent` will be linked from SRAM to Flash.
|
||||
|
||||
Next step you should add the path of the object file to `object.csv`:
|
||||
|
||||
```
|
||||
libfreertos.a,tasks.c.obj,esp-idf/freertos/CMakeFiles/__idf_freertos.dir/FreeRTOS-Kernel/tasks.c.obj
|
||||
```
|
||||
|
||||
This means the object file `tasks.c.obj` is in library `libfreertos.a` and its location is `esp-idf/freertos/CMakeFiles/__idf_freertos.dir/FreeRTOS-Kernel/tasks.c.obj` relative to directory of `build`.
|
||||
|
||||
Next step you should add path of library to `library.csv`:
|
||||
|
||||
```
|
||||
libfreertos.a,./esp-idf/freertos/libfreertos.a
|
||||
```
|
||||
|
||||
This means library `libfreertos.a`'s location is `./esp-idf/freertos/libfreertos.a` relative to `build`.
|
||||
|
||||
If above related data has exists in corresponding files, please don't add this repeatedly.
|
||||
84
managed_components/espressif__cmake_utilities/gcc.cmake
Normal file
84
managed_components/espressif__cmake_utilities/gcc.cmake
Normal file
@@ -0,0 +1,84 @@
|
||||
if(CONFIG_CU_GCC_LTO_ENABLE)
|
||||
# Enable cmake interprocedural optimization(IPO) support to check if GCC supports link time optimization(LTO)
|
||||
cmake_policy(SET CMP0069 NEW)
|
||||
include(CheckIPOSupported)
|
||||
|
||||
# Compare to "ar" and "ranlib", "gcc-ar" and "gcc-ranlib" integrate GCC LTO plugin
|
||||
set(CMAKE_AR ${_CMAKE_TOOLCHAIN_PREFIX}gcc-ar)
|
||||
set(CMAKE_RANLIB ${_CMAKE_TOOLCHAIN_PREFIX}gcc-ranlib)
|
||||
|
||||
macro(cu_gcc_lto_set)
|
||||
check_ipo_supported(RESULT result)
|
||||
if(result)
|
||||
message(STATUS "GCC link time optimization(LTO) is enable")
|
||||
|
||||
set(multi_value COMPONENTS DEPENDS)
|
||||
cmake_parse_arguments(LTO "" "" "${multi_value}" ${ARGN})
|
||||
|
||||
# Use full format LTO object file
|
||||
set(GCC_LTO_OBJECT_TYPE "-ffat-lto-objects")
|
||||
# Set compression level 9(min:0, max:9)
|
||||
set(GCC_LTO_COMPRESSION_LEVEL "-flto-compression-level=9")
|
||||
# Set partition level max to removed used symbol
|
||||
set(GCC_LTO_PARTITION_LEVEL "-flto-partition=max")
|
||||
|
||||
# Set mode "auto" to increase compiling speed
|
||||
set(GCC_LTO_COMPILE_OPTIONS "-flto=auto"
|
||||
${GCC_LTO_OBJECT_TYPE}
|
||||
${GCC_LTO_COMPRESSION_LEVEL})
|
||||
|
||||
# Enable GCC LTO and plugin when linking stage
|
||||
set(GCC_LTO_LINK_OPTIONS "-flto"
|
||||
"-fuse-linker-plugin"
|
||||
${GCC_LTO_OBJECT_TYPE}
|
||||
${GCC_LTO_PARTITION_LEVEL})
|
||||
|
||||
message(STATUS "GCC LTO for components: ${LTO_COMPONENTS}")
|
||||
foreach(c ${LTO_COMPONENTS})
|
||||
idf_component_get_property(t ${c} COMPONENT_LIB)
|
||||
target_compile_options(${t} PRIVATE ${GCC_LTO_COMPILE_OPTIONS})
|
||||
endforeach()
|
||||
|
||||
message(STATUS "GCC LTO for dependencies: ${LTO_DEPENDS}")
|
||||
foreach(d ${LTO_DEPENDS})
|
||||
target_compile_options(${d} PRIVATE ${GCC_LTO_COMPILE_OPTIONS})
|
||||
endforeach()
|
||||
|
||||
if("${IDF_VERSION_MAJOR}.${IDF_VERSION_MINOR}" VERSION_GREATER_EQUAL "4.4")
|
||||
target_link_libraries(${project_elf} PRIVATE ${GCC_LTO_LINK_OPTIONS})
|
||||
else()
|
||||
target_link_libraries(${project_elf} ${GCC_LTO_LINK_OPTIONS})
|
||||
endif()
|
||||
else()
|
||||
message(FATAL_ERROR "GCC link time optimization(LTO) is not supported")
|
||||
endif()
|
||||
endmacro()
|
||||
else()
|
||||
macro(cu_gcc_lto_set)
|
||||
message(STATUS "GCC link time optimization(LTO) is not enable")
|
||||
endmacro()
|
||||
endif()
|
||||
|
||||
if(CONFIG_CU_GCC_STRING_1BYTE_ALIGN)
|
||||
macro(cu_gcc_string_1byte_align)
|
||||
message(STATUS "GCC string 1-byte align is enable")
|
||||
|
||||
set(multi_value COMPONENTS DEPENDS)
|
||||
cmake_parse_arguments(STR_ALIGN "" "" "${multi_value}" ${ARGN})
|
||||
|
||||
message(STATUS "GCC string 1-byte align for components: ${STR_ALIGN_COMPONENTS}")
|
||||
foreach(c ${STR_ALIGN_COMPONENTS})
|
||||
idf_component_get_property(t ${c} COMPONENT_LIB)
|
||||
target_compile_options(${t} PRIVATE "-malign-data=natural")
|
||||
endforeach()
|
||||
|
||||
message(STATUS "GCC string 1-byte align for dependencies: ${STR_ALIGN_DEPENDS}")
|
||||
foreach(d ${STR_ALIGN_DEPENDS})
|
||||
target_compile_options(${d} PRIVATE "-malign-data=natural")
|
||||
endforeach()
|
||||
endmacro()
|
||||
else()
|
||||
macro(cu_gcc_string_1byte_align)
|
||||
message(STATUS "GCC string 1-byte align is not enable")
|
||||
endmacro()
|
||||
endif()
|
||||
@@ -0,0 +1,18 @@
|
||||
if (NOT TARGET gen_compressed_ota)
|
||||
add_custom_target(gen_compressed_ota)
|
||||
|
||||
if (CONFIG_SECURE_SIGNED_ON_UPDATE_NO_SECURE_BOOT OR CONFIG_SECURE_BOOT_V2_ENABLED)
|
||||
set(COMPRESSED_OTA_BIN_SIGN_PARA --sign_key ${PROJECT_DIR}/${CONFIG_SECURE_BOOT_SIGNING_KEY})
|
||||
else()
|
||||
set(COMPRESSED_OTA_BIN_SIGN_PARA )
|
||||
endif()
|
||||
|
||||
set(GEN_COMPRESSED_BIN_CMD ${CMAKE_CURRENT_LIST_DIR}/scripts/gen_custom_ota.py ${COMPRESSED_OTA_BIN_SIGN_PARA} --add_app_header)
|
||||
|
||||
add_custom_command(TARGET gen_compressed_ota
|
||||
POST_BUILD
|
||||
COMMAND ${PYTHON} ${GEN_COMPRESSED_BIN_CMD}
|
||||
COMMENT "The gen compresssed bin cmd is: ${GEN_COMPRESSED_BIN_CMD}"
|
||||
)
|
||||
add_dependencies(gen_compressed_ota gen_project_binary)
|
||||
endif()
|
||||
@@ -0,0 +1,27 @@
|
||||
# Extend command to idf.py
|
||||
|
||||
# Generate single bin with name ${CMAKE_PROJECT_NAME}_merged.bin
|
||||
if (NOT TARGET gen_single_bin)
|
||||
add_custom_target(
|
||||
gen_single_bin
|
||||
COMMAND ${CMAKE_COMMAND} -E echo "Merge bin files to ${CMAKE_PROJECT_NAME}_merged.bin"
|
||||
COMMAND ${ESPTOOLPY} --chip ${IDF_TARGET} merge_bin -o ${CMAKE_PROJECT_NAME}_merged.bin @flash_args
|
||||
COMMAND ${CMAKE_COMMAND} -E echo "Merge bin done"
|
||||
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
|
||||
DEPENDS gen_project_binary bootloader
|
||||
VERBATIM USES_TERMINAL
|
||||
)
|
||||
endif()
|
||||
|
||||
# Flash bin ${CMAKE_PROJECT_NAME}_merged.bin to target chip
|
||||
if (NOT TARGET flash_single_bin)
|
||||
add_custom_target(
|
||||
flash_single_bin
|
||||
COMMAND ${CMAKE_COMMAND} -E echo "Flash merged bin ${CMAKE_PROJECT_NAME}_merged.bin to address 0x0"
|
||||
COMMAND ${ESPTOOLPY} --chip ${IDF_TARGET} write_flash 0x0 ${CMAKE_PROJECT_NAME}_merged.bin
|
||||
COMMAND ${CMAKE_COMMAND} -E echo "Flash merged bin done"
|
||||
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
|
||||
DEPENDS gen_single_bin
|
||||
VERBATIM USES_TERMINAL
|
||||
)
|
||||
endif()
|
||||
@@ -0,0 +1,7 @@
|
||||
dependencies:
|
||||
idf:
|
||||
version: '>=4.1'
|
||||
description: A collection of useful cmake utilities
|
||||
issues: https://github.com/espressif/esp-iot-solution/issues
|
||||
url: https://github.com/espressif/esp-iot-solution/tree/master/tools/cmake_utilities
|
||||
version: 0.5.3
|
||||
202
managed_components/espressif__cmake_utilities/license.txt
Normal file
202
managed_components/espressif__cmake_utilities/license.txt
Normal file
@@ -0,0 +1,202 @@
|
||||
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
@@ -0,0 +1,45 @@
|
||||
# cu_pkg_get_version
|
||||
#
|
||||
# @brief Get the package's version information, the package is installed by component manager.
|
||||
#
|
||||
# @param[in] pkg_path the package's path, normally it's ${CMAKE_CURRENT_LIST_DIR}.
|
||||
#
|
||||
# @param[out] ver_major the major version of the package
|
||||
# @param[out] ver_minor the minor version of the package
|
||||
# @param[out] ver_patch the patch version of the package
|
||||
function(cu_pkg_get_version pkg_path ver_major ver_minor ver_patch)
|
||||
set(yml_file "${pkg_path}/idf_component.yml")
|
||||
if(EXISTS ${yml_file})
|
||||
file(READ ${yml_file} ver)
|
||||
string(REGEX MATCH "(^|\n)version: \"?([0-9]+).([0-9]+).([0-9]+)\"?" _ ${ver})
|
||||
set(${ver_major} ${CMAKE_MATCH_2} PARENT_SCOPE)
|
||||
set(${ver_minor} ${CMAKE_MATCH_3} PARENT_SCOPE)
|
||||
set(${ver_patch} ${CMAKE_MATCH_4} PARENT_SCOPE)
|
||||
else()
|
||||
message(WARNING " ${yml_file} not exist")
|
||||
endif()
|
||||
endfunction()
|
||||
|
||||
# cu_pkg_define_version
|
||||
#
|
||||
# @brief Add the package's version definitions using format ${NAME}_VER_MAJOR ${NAME}_VER_MINOR ${NAME}_VER_PATCH,
|
||||
# the ${NAME} will be inferred from package path, and namespace like `espressif__` will be removed if the package download from esp-registry
|
||||
# eg. espressif__usb_stream and usb_stream will generate same version definitions USB_STREAM_VER_MAJOR ...
|
||||
#
|
||||
# @param[in] pkg_path the package's path, normally it's ${CMAKE_CURRENT_LIST_DIR}.
|
||||
#
|
||||
macro(cu_pkg_define_version pkg_path)
|
||||
cu_pkg_get_version(${pkg_path} ver_major ver_minor ver_patch)
|
||||
get_filename_component(pkg_name ${pkg_path} NAME)
|
||||
string(FIND ${pkg_name} "__" pkg_name_pos)
|
||||
if(pkg_name_pos GREATER -1)
|
||||
math(EXPR pkg_name_pos "${pkg_name_pos} + 2")
|
||||
string(SUBSTRING ${pkg_name} ${pkg_name_pos} -1 pkg_name)
|
||||
endif()
|
||||
string(TOUPPER ${pkg_name} pkg_name)
|
||||
string(REPLACE "-" "_" pkg_name ${pkg_name})
|
||||
message(STATUS "${pkg_name}: ${ver_major}.${ver_minor}.${ver_patch}")
|
||||
list(LENGTH pkg_name_list len)
|
||||
target_compile_options(${COMPONENT_LIB} PUBLIC
|
||||
-D${pkg_name}_VER_MAJOR=${ver_major} -D${pkg_name}_VER_MINOR=${ver_minor} -D${pkg_name}_VER_PATCH=${ver_patch})
|
||||
endmacro()
|
||||
@@ -0,0 +1,9 @@
|
||||
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR} ${CMAKE_MODULE_PATH})
|
||||
|
||||
if(CONFIG_CU_DIAGNOSTICS_COLOR_ALWAYS)
|
||||
add_compile_options(-fdiagnostics-color=always)
|
||||
elseif(CONFIG_CU_DIAGNOSTICS_COLOR_AUTO)
|
||||
add_compile_options(-fdiagnostics-color=auto)
|
||||
elseif(CONFIG_CU_DIAGNOSTICS_COLOR_NEVER)
|
||||
add_compile_options(-fdiagnostics-color=never)
|
||||
endif()
|
||||
73
managed_components/espressif__cmake_utilities/relinker.cmake
Normal file
73
managed_components/espressif__cmake_utilities/relinker.cmake
Normal file
@@ -0,0 +1,73 @@
|
||||
# @brief Link designated functions from SRAM to Flash to save SRAM
|
||||
if(CONFIG_CU_RELINKER_ENABLE)
|
||||
# project_elf variable is only in project.cmake
|
||||
if(NOT TARGET customer_sections AND DEFINED project_elf)
|
||||
message(STATUS "Relinker is enabled.")
|
||||
if(CONFIG_IDF_TARGET_ESP32C2)
|
||||
set(target "esp32c2")
|
||||
else()
|
||||
message(FATAL_ERROR "Other targets are not supported.")
|
||||
endif()
|
||||
|
||||
if(CONFIG_CU_RELINKER_ENABLE_CUSTOMIZED_CONFIGURATION_FILES)
|
||||
idf_build_get_property(project_dir PROJECT_DIR)
|
||||
get_filename_component(cfg_file_path "${CONFIG_CU_RELINKER_CUSTOMIZED_CONFIGURATION_FILES_PATH}"
|
||||
ABSOLUTE BASE_DIR "${project_dir}")
|
||||
|
||||
if(NOT EXISTS "${cfg_file_path}")
|
||||
message(FATAL_ERROR "Relinker Configuration files path ${cfg_file_path} is not found.")
|
||||
endif()
|
||||
else()
|
||||
set(cfg_file_path ${PROJECT_DIR}/relinker/${target})
|
||||
if(NOT EXISTS ${cfg_file_path})
|
||||
set(cfg_file_path ${CMAKE_CURRENT_LIST_DIR}/scripts/relinker/examples/${target})
|
||||
endif()
|
||||
endif()
|
||||
|
||||
message(STATUS "Relinker configuration files: ${cfg_file_path}")
|
||||
|
||||
set(library_file "${cfg_file_path}/library.csv")
|
||||
set(object_file "${cfg_file_path}/object.csv")
|
||||
set(function_file "${cfg_file_path}/function.csv")
|
||||
set(relinker_script "${CMAKE_CURRENT_LIST_DIR}/scripts/relinker/relinker.py")
|
||||
|
||||
set(cmake_objdump "${CMAKE_OBJDUMP}")
|
||||
set(link_path "${CMAKE_BINARY_DIR}/esp-idf/esp_system/ld")
|
||||
set(link_src_file "${link_path}/sections.ld")
|
||||
set(link_dst_file "${link_path}/customer_sections.ld")
|
||||
|
||||
set(relinker_opts --input ${link_src_file}
|
||||
--output ${link_dst_file}
|
||||
--library ${library_file}
|
||||
--object ${object_file}
|
||||
--function ${function_file}
|
||||
--sdkconfig ${sdkconfig}
|
||||
--objdump ${cmake_objdump})
|
||||
|
||||
if(CONFIG_CU_RELINKER_ENABLE_PRINT_ERROR_INFO_WHEN_MISSING_FUNCTION)
|
||||
list(APPEND relinker_opts --missing_function_info True)
|
||||
endif()
|
||||
|
||||
idf_build_get_property(link_depends __LINK_DEPENDS)
|
||||
|
||||
add_custom_command(OUTPUT ${link_dst_file}
|
||||
COMMAND ${python} -B ${relinker_script}
|
||||
${relinker_opts}
|
||||
COMMAND ${CMAKE_COMMAND} -E copy
|
||||
${link_dst_file}
|
||||
${link_src_file}
|
||||
COMMAND ${CMAKE_COMMAND} -E echo
|
||||
/*relinker*/ >>
|
||||
${link_dst_file}
|
||||
DEPENDS "${link_depends}"
|
||||
"${library_file}"
|
||||
"${object_file}"
|
||||
"${function_file}"
|
||||
VERBATIM)
|
||||
|
||||
add_custom_target(customer_sections DEPENDS ${link_dst_file})
|
||||
add_dependencies(${project_elf} customer_sections)
|
||||
endif()
|
||||
else()
|
||||
message(STATUS "Relinker isn't enabled.")
|
||||
endif()
|
||||
@@ -0,0 +1,243 @@
|
||||
#!/usr/bin/env python
|
||||
#
|
||||
# SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
|
||||
#
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import sys
|
||||
import struct
|
||||
import argparse
|
||||
import binascii
|
||||
import hashlib
|
||||
import os
|
||||
import subprocess
|
||||
import shutil
|
||||
import json
|
||||
import lzma
|
||||
|
||||
# src_file = 'hello-world.bin'
|
||||
# compressed_file = 'hello-world.bin.xz'
|
||||
# packed_compressed_file = 'hello-world.bin.xz.packed'
|
||||
# siged_packed_compressed_file = 'hello-world.bin.xz.packed.signed'
|
||||
|
||||
binary_compress_type = {'none': 0, 'xz':1}
|
||||
header_version = {'v1': 1, 'v2': 2, 'v3': 3}
|
||||
|
||||
SCRIPT_VERSION = '1.0.0'
|
||||
ORIGIN_APP_IMAGE_HEADER_LEN = 288 # sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t) + sizeof(esp_app_desc_t). See esp_app_format.h
|
||||
# At present, we calculate the checksum of the first 4KB data of the old app.
|
||||
OLD_APP_CHECK_DATA_SIZE = 4096
|
||||
|
||||
# v1 compressed data header:
|
||||
# Note: Encryption_type field is deprecated, the field is reserved for compatibility.
|
||||
# |------|---------|---------|----------|------------|------------|-----------|----------|----------------|----------------|--------------|------------|
|
||||
# | | Magic | header | Compress | delta | Encryption | Reserved | Firmware | The length of | The MD5 of | The CRC32 for| compressed |
|
||||
# | | number | version | type | type | type | | version | compressed data| compressed data| the header | data |
|
||||
# |------|---------|---------|----------|------------|------------|-----------|----------|----------------|----------------|--------------|------------|
|
||||
# | Size | 4 bytes | 1 byte | 4 bits | 4 bits | 1 bytes | 1 bytes | 32 bytes | 4 bytes | 32 bytes | 4 bytes | |
|
||||
# |------|---------|---------|----------|------------|------------|-----------|----------|----------------|----------------|--------------|------------|
|
||||
# | Data | String | | | | | | String | little-endian | byte string | little-endian| |
|
||||
# | type | ended | | | | | | ended | integer | | integer | |
|
||||
# | |with ‘\0’| | | | | | with ‘\0’| | | | Binary data|
|
||||
# |------|---------|---------|----------|------------|------------|-----------|----------|----------------|----------------|--------------|------------|
|
||||
# | Data | “ESP” | 1 | 0/1 | 0/1 | | | | | | | |
|
||||
# |------|---------|---------|----------|------------|------------|-----------|----------|----------------|----------------|--------------|------------|
|
||||
|
||||
# v2 compressed data header
|
||||
# Note: Encryption_type field is deprecated, the field is reserved for compatibility.
|
||||
# |------|---------|---------|----------|------------|------------|-----------|----------|----------------|----------------|---------------|---------------|--------------|------------|
|
||||
# | | Magic | header | Compress | delta | Encryption | Reserved | Firmware | The length of | The MD5 of | base app check| The CRC32 for | The CRC32 for| compressed |
|
||||
# | | number | version | type | type | type | | version | compressed data| compressed data| data len | base app data | the header | data |
|
||||
# |------|---------|---------|----------|------------|------------|-----------|----------|----------------|----------------|---------------|---------------|--------------|------------|
|
||||
# | Size | 4 bytes | 1 byte | 4 bits | 4 bits | 1 bytes | 1 bytes | 32 bytes | 4 bytes | 32 bytes | 4 bytes | 4 bytes | 4 bytes | |
|
||||
# |------|---------|---------|----------|------------|------------|-----------|----------|----------------|----------------|---------------|---------------|--------------|------------|
|
||||
# | Data | String | | | | | | String | little-endian | byte string | little-endian | little-endian | little-endian| |
|
||||
# | type | ended | | | | | | ended | integer | | integer | integer | integer | |
|
||||
# | |with ‘\0’| | | | | | with ‘\0’| | | | | | Binary data|
|
||||
# |------|---------|---------|----------|------------|------------|-----------|----------|----------------|----------------|---------------|---------------|--------------|------------|
|
||||
# | Data | “ESP” | 1 | 0/1 | 0/1 | | | | | | | | | |
|
||||
# |------|---------|---------|----------|------------|------------|-----------|----------|----------------|----------------|---------------|---------------|--------------|------------|
|
||||
|
||||
# v3 compressed data header:
|
||||
# |------|---------|---------|----------|------------|-----------|----------------|----------------|--------------|------------|
|
||||
# | | Magic | header | Compress | Reserved | Reserved | The length of | The MD5 of | The CRC32 for| compressed |
|
||||
# | | number | version | type | | | compressed data| compressed data| the header | data |
|
||||
# |------|---------|---------|----------|------------|-----------|----------------|----------------|--------------|------------|
|
||||
# | Size | 4 bytes | 1 byte | 4 bits | 4 bits | 8 bytes | 4 bytes | 16 bytes | 4 bytes | |
|
||||
# |------|---------|---------|----------|------------|-----------|----------------|----------------|--------------|------------|
|
||||
# | Data | String | integer | | | | little-endian | byte string | little-endian| |
|
||||
# | type | ended | | | | | integer | | integer | |
|
||||
# | |with ‘\0’| | | | | | | | Binary data|
|
||||
# |------|---------|---------|----------|------------|-----------|----------------|----------------|--------------|------------|
|
||||
# | Data | “ESP” | 3 | 0/1 | | | | | | |
|
||||
# |------|---------|---------|----------|------------|-----------|----------------|----------------|--------------|------------|
|
||||
|
||||
def xz_compress(store_directory, in_file):
|
||||
compressed_file = ''.join([in_file,'.xz'])
|
||||
if(os.path.exists(compressed_file)):
|
||||
os.remove(compressed_file)
|
||||
|
||||
xz_compressor_filter = [
|
||||
{"id": lzma.FILTER_LZMA2, "preset": 6, "dict_size": 64*1024},
|
||||
]
|
||||
with open(in_file, 'rb') as src_f:
|
||||
data = src_f.read()
|
||||
with lzma.open(compressed_file, "wb", format=lzma.FORMAT_XZ, check=lzma.CHECK_CRC32, filters=xz_compressor_filter) as f:
|
||||
f.write(data)
|
||||
f.close()
|
||||
|
||||
if not os.path.exists(os.path.join(store_directory, os.path.split(compressed_file)[1])):
|
||||
shutil.copy(compressed_file, store_directory)
|
||||
print('copy xz file done')
|
||||
|
||||
def secure_boot_sign(sign_key, in_file, out_file):
|
||||
ret = subprocess.call('espsecure.py sign_data --version 2 --keyfile {} --output {} {}'.format(sign_key, out_file, in_file), shell = True)
|
||||
if ret:
|
||||
raise Exception('sign failed')
|
||||
|
||||
def get_app_name():
|
||||
with open('flasher_args.json') as f:
|
||||
try:
|
||||
flasher_args = json.load(f)
|
||||
return flasher_args['app']['file']
|
||||
except Exception as e:
|
||||
print(e)
|
||||
|
||||
return ''
|
||||
|
||||
def get_script_version():
|
||||
return SCRIPT_VERSION
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('-hv', '--header_ver', nargs='?', choices = ['v1', 'v2', 'v3'],
|
||||
default='v3', help='the version of the packed file header [default:v3]')
|
||||
parser.add_argument('-c', '--compress_type', nargs= '?', choices = ['none', 'xz'],
|
||||
default='xz', help='compressed type [default:xz]')
|
||||
parser.add_argument('-i', '--in_file', nargs = '?',
|
||||
default='', help='the new app firmware')
|
||||
parser.add_argument('--sign_key', nargs = '?',
|
||||
default='', help='the sign key used for secure boot')
|
||||
parser.add_argument('-fv', '--fw_ver', nargs='?',
|
||||
default='', help='the version of the compressed data(this field is deprecated in v3)')
|
||||
parser.add_argument('--add_app_header', action="store_true", help='add app header to use native esp_ota_* & esp_https_ota_* APIs')
|
||||
parser.add_argument('-v', '--version', action='version', version=get_script_version(), help='the version of the script')
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
compress_type = args.compress_type
|
||||
firmware_ver = args.fw_ver
|
||||
src_file = args.in_file
|
||||
sign_key = args.sign_key
|
||||
header_ver = args.header_ver
|
||||
add_app_header = args.add_app_header
|
||||
|
||||
if src_file == '':
|
||||
origin_app_name = get_app_name()
|
||||
if(origin_app_name == ''):
|
||||
print('get origin app name fail')
|
||||
return
|
||||
|
||||
if os.path.exists(origin_app_name):
|
||||
src_file = origin_app_name
|
||||
else:
|
||||
print('origin app.bin not found')
|
||||
return
|
||||
|
||||
print('src file is: {}'.format(src_file))
|
||||
|
||||
# rebuild the cpmpressed_app directroy
|
||||
cpmoressed_app_directory = 'custom_ota_binaries'
|
||||
if os.path.exists(cpmoressed_app_directory):
|
||||
shutil.rmtree(cpmoressed_app_directory)
|
||||
|
||||
os.mkdir(cpmoressed_app_directory)
|
||||
print('The compressed file will store in {}'.format(cpmoressed_app_directory))
|
||||
|
||||
#step1: compress
|
||||
if compress_type == 'xz':
|
||||
xz_compress(cpmoressed_app_directory, os.path.abspath(src_file))
|
||||
|
||||
origin_app_name = os.path.split(src_file)[1]
|
||||
|
||||
compressed_file_name = ''.join([origin_app_name, '.xz'])
|
||||
compressed_file = os.path.join(cpmoressed_app_directory, compressed_file_name)
|
||||
else:
|
||||
compressed_file = ''.join(src_file)
|
||||
|
||||
print('compressed file is: {}'.format(compressed_file))
|
||||
|
||||
#step2: packet the compressed image header
|
||||
with open(src_file, 'rb') as s_f:
|
||||
src_image_header = bytearray(s_f.read(ORIGIN_APP_IMAGE_HEADER_LEN))
|
||||
src_image_header[1] = 0x00
|
||||
|
||||
packed_file = ''.join([compressed_file,'.packed'])
|
||||
with open(compressed_file, 'rb') as src_f:
|
||||
data = src_f.read()
|
||||
f_len = src_f.tell()
|
||||
# magic number
|
||||
bin_data = struct.pack('4s', b'ESP')
|
||||
# header version
|
||||
bin_data += struct.pack('B', header_version[header_ver])
|
||||
# Compress type
|
||||
bin_data += struct.pack('B', binary_compress_type[compress_type])
|
||||
print('compressed type: {}'.format(binary_compress_type[compress_type]))
|
||||
# in header v1/v2, there is a field "Encryption type", this field has been deprecated in v3
|
||||
if (header_version[header_ver] < 3):
|
||||
bin_data += struct.pack('B', 0)
|
||||
# Reserved
|
||||
bin_data += struct.pack('?', 0)
|
||||
# Firmware version
|
||||
bin_data += struct.pack('32s', firmware_ver.encode())
|
||||
else:
|
||||
# Reserved
|
||||
bin_data += struct.pack('10s', b'0')
|
||||
# The length of the compressed data
|
||||
bin_data += struct.pack('<I', f_len)
|
||||
print('compressed data len: {}'.format(f_len))
|
||||
# The MD5 for the compressed data
|
||||
if (header_version[header_ver] < 3):
|
||||
bin_data += struct.pack('32s', hashlib.md5(data).digest())
|
||||
if (header_version[header_ver] == 2):
|
||||
# Todo, if it's diff OTA, write base app check data len
|
||||
bin_data += struct.pack('<I', 0)
|
||||
# Todo, if it's diff OTA, write base app crc32 checksum
|
||||
bin_data += struct.pack('<I', 0)
|
||||
else:
|
||||
bin_data += struct.pack('16s', hashlib.md5(data).digest())
|
||||
# The CRC32 for the header
|
||||
bin_data += struct.pack('<I', binascii.crc32(bin_data, 0x0))
|
||||
|
||||
# write compressed data
|
||||
bin_data += data
|
||||
with open(packed_file, 'wb') as dst_f:
|
||||
# write compressed image header and compressed dada
|
||||
dst_f.write(bin_data)
|
||||
|
||||
print('packed file is: {}'.format(packed_file))
|
||||
|
||||
#step3: if need sign, then sign the packed image
|
||||
if sign_key != '':
|
||||
signed_file = ''.join([packed_file,'.signed'])
|
||||
secure_boot_sign(sign_key, packed_file, signed_file)
|
||||
print('signed_file is: {}'.format(signed_file))
|
||||
else:
|
||||
signed_file = ''.join(packed_file)
|
||||
|
||||
if (header_version[header_ver] == 3) and add_app_header:
|
||||
with open(signed_file, 'rb+') as src_f:
|
||||
packed_data = src_f.read()
|
||||
src_f.seek(0)
|
||||
# write origin app image header
|
||||
src_f.write(src_image_header)
|
||||
# write compressed image header and compressed dada
|
||||
src_f.write(packed_data)
|
||||
print('app image header has been added')
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:
|
||||
main()
|
||||
except Exception as e:
|
||||
print(e)
|
||||
sys.exit(2)
|
||||
@@ -0,0 +1,211 @@
|
||||
#!/usr/bin/env python3
|
||||
#
|
||||
# SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
|
||||
import argparse
|
||||
import csv
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
import re
|
||||
from io import StringIO
|
||||
|
||||
OPT_MIN_LEN = 7
|
||||
|
||||
espidf_objdump = None
|
||||
espidf_missing_function_info = True
|
||||
|
||||
class sdkconfig_c:
|
||||
def __init__(self, path):
|
||||
lines = open(path).read().splitlines()
|
||||
config = dict()
|
||||
for l in lines:
|
||||
if len(l) > OPT_MIN_LEN and l[0] != '#':
|
||||
mo = re.match( r'(.*)=(.*)', l, re.M|re.I)
|
||||
if mo:
|
||||
config[mo.group(1)]=mo.group(2).replace('"', '')
|
||||
self.config = config
|
||||
|
||||
def index(self, i):
|
||||
return self.config[i]
|
||||
|
||||
def check(self, options):
|
||||
options = options.replace(' ', '')
|
||||
if '&&' in options:
|
||||
for i in options.split('&&'):
|
||||
if i[0] == '!':
|
||||
i = i[1:]
|
||||
if i in self.config:
|
||||
return False
|
||||
else:
|
||||
if i not in self.config:
|
||||
return False
|
||||
else:
|
||||
i = options
|
||||
if i[0] == '!':
|
||||
i = i[1:]
|
||||
if i in self.config:
|
||||
return False
|
||||
else:
|
||||
if i not in self.config:
|
||||
return False
|
||||
return True
|
||||
|
||||
class object_c:
|
||||
def read_dump_info(self, pathes):
|
||||
new_env = os.environ.copy()
|
||||
new_env['LC_ALL'] = 'C'
|
||||
dumps = list()
|
||||
print('pathes:', pathes)
|
||||
for path in pathes:
|
||||
try:
|
||||
dump = StringIO(subprocess.check_output([espidf_objdump, '-t', path], env=new_env).decode())
|
||||
dumps.append(dump.readlines())
|
||||
except subprocess.CalledProcessError as e:
|
||||
raise RuntimeError('cmd:%s result:%s'%(e.cmd, e.returncode))
|
||||
return dumps
|
||||
|
||||
def get_func_section(self, dumps, func):
|
||||
for dump in dumps:
|
||||
for l in dump:
|
||||
if ' %s'%(func) in l and '*UND*' not in l:
|
||||
m = re.match(r'(\S*)\s*([glw])\s*([F|O])\s*(\S*)\s*(\S*)\s*(\S*)\s*', l, re.M|re.I)
|
||||
if m and m[6] == func:
|
||||
return m[4].replace('.text.', '')
|
||||
if espidf_missing_function_info:
|
||||
print('%s failed to find section'%(func))
|
||||
return None
|
||||
else:
|
||||
raise RuntimeError('%s failed to find section'%(func))
|
||||
|
||||
def __init__(self, name, pathes, libray):
|
||||
self.name = name
|
||||
self.libray = libray
|
||||
self.funcs = dict()
|
||||
self.pathes = pathes
|
||||
self.dumps = self.read_dump_info(pathes)
|
||||
|
||||
def append(self, func):
|
||||
section = self.get_func_section(self.dumps, func)
|
||||
if section != None:
|
||||
self.funcs[func] = section
|
||||
|
||||
def functions(self):
|
||||
nlist = list()
|
||||
for i in self.funcs:
|
||||
nlist.append(i)
|
||||
return nlist
|
||||
|
||||
def sections(self):
|
||||
nlist = list()
|
||||
for i in self.funcs:
|
||||
nlist.append(self.funcs[i])
|
||||
return nlist
|
||||
|
||||
class library_c:
|
||||
def __init__(self, name, path):
|
||||
self.name = name
|
||||
self.path = path
|
||||
self.objs = dict()
|
||||
|
||||
def append(self, obj, path, func):
|
||||
if obj not in self.objs:
|
||||
self.objs[obj] = object_c(obj, path, self.name)
|
||||
self.objs[obj].append(func)
|
||||
|
||||
class libraries_c:
|
||||
def __init__(self):
|
||||
self.libs = dict()
|
||||
|
||||
def append(self, lib, lib_path, obj, obj_path, func):
|
||||
if lib not in self.libs:
|
||||
self.libs[lib] = library_c(lib, lib_path)
|
||||
self.libs[lib].append(obj, obj_path, func)
|
||||
|
||||
def dump(self):
|
||||
for libname in self.libs:
|
||||
lib = self.libs[libname]
|
||||
for objname in lib.objs:
|
||||
obj = lib.objs[objname]
|
||||
print('%s, %s, %s, %s'%(libname, objname, obj.path, obj.funcs))
|
||||
|
||||
class paths_c:
|
||||
def __init__(self):
|
||||
self.paths = dict()
|
||||
|
||||
def append(self, lib, obj, path):
|
||||
if '$IDF_PATH' in path:
|
||||
path = path.replace('$IDF_PATH', os.environ['IDF_PATH'])
|
||||
|
||||
if lib not in self.paths:
|
||||
self.paths[lib] = dict()
|
||||
if obj not in self.paths[lib]:
|
||||
self.paths[lib][obj] = list()
|
||||
self.paths[lib][obj].append(path)
|
||||
|
||||
def index(self, lib, obj):
|
||||
if lib not in self.paths:
|
||||
return None
|
||||
if '*' in self.paths[lib]:
|
||||
obj = '*'
|
||||
return self.paths[lib][obj]
|
||||
|
||||
def generator(library_file, object_file, function_file, sdkconfig_file, missing_function_info, objdump='riscv32-esp-elf-objdump'):
|
||||
global espidf_objdump, espidf_missing_function_info
|
||||
espidf_objdump = objdump
|
||||
espidf_missing_function_info = missing_function_info
|
||||
|
||||
sdkconfig = sdkconfig_c(sdkconfig_file)
|
||||
|
||||
lib_paths = paths_c()
|
||||
for p in csv.DictReader(open(library_file, 'r')):
|
||||
lib_paths.append(p['library'], '*', p['path'])
|
||||
|
||||
obj_paths = paths_c()
|
||||
for p in csv.DictReader(open(object_file, 'r')):
|
||||
obj_paths.append(p['library'], p['object'], p['path'])
|
||||
|
||||
libraries = libraries_c()
|
||||
for d in csv.DictReader(open(function_file, 'r')):
|
||||
if d['option'] and sdkconfig.check(d['option']) == False:
|
||||
print('skip %s(%s)'%(d['function'], d['option']))
|
||||
continue
|
||||
lib_path = lib_paths.index(d['library'], '*')
|
||||
obj_path = obj_paths.index(d['library'], d['object'])
|
||||
if not obj_path:
|
||||
obj_path = lib_path
|
||||
libraries.append(d['library'], lib_path[0], d['object'], obj_path, d['function'])
|
||||
return libraries
|
||||
|
||||
def main():
|
||||
argparser = argparse.ArgumentParser(description='Libraries management')
|
||||
|
||||
argparser.add_argument(
|
||||
'--library', '-l',
|
||||
help='Library description file',
|
||||
type=str)
|
||||
|
||||
argparser.add_argument(
|
||||
'--object', '-b',
|
||||
help='Object description file',
|
||||
type=str)
|
||||
|
||||
argparser.add_argument(
|
||||
'--function', '-f',
|
||||
help='Function description file',
|
||||
type=str)
|
||||
|
||||
argparser.add_argument(
|
||||
'--sdkconfig', '-s',
|
||||
help='sdkconfig file',
|
||||
type=str)
|
||||
|
||||
args = argparser.parse_args()
|
||||
|
||||
libraries = generator(args.library, args.object, args.function, args.sdkconfig)
|
||||
# libraries.dump()
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,365 @@
|
||||
library,object,function,option
|
||||
libble_app.a,ble_hw.c.o,r_ble_hw_resolv_list_get_cur_entry,
|
||||
libble_app.a,ble_ll_adv.c.o,r_ble_ll_adv_set_sched,
|
||||
libble_app.a,ble_ll_adv.c.o,r_ble_ll_adv_sync_pdu_make,
|
||||
libble_app.a,ble_ll_adv.c.o,r_ble_ll_adv_sync_calculate,
|
||||
libble_app.a,ble_ll_conn.c.o,r_ble_ll_conn_is_dev_connected,
|
||||
libble_app.a,ble_ll_ctrl.c.o,r_ble_ll_ctrl_tx_done,
|
||||
libble_app.a,ble_lll_adv.c.o,r_ble_lll_adv_aux_scannable_pdu_payload_len,
|
||||
libble_app.a,ble_lll_adv.c.o,r_ble_lll_adv_halt,
|
||||
libble_app.a,ble_lll_adv.c.o,r_ble_lll_adv_periodic_schedule_next,
|
||||
libble_app.a,ble_lll_conn.c.o,r_ble_lll_conn_cth_flow_free_credit,
|
||||
libble_app.a,ble_lll_conn.c.o,r_ble_lll_conn_update_encryption,
|
||||
libble_app.a,ble_lll_conn.c.o,r_ble_lll_conn_set_slave_flow_control,
|
||||
libble_app.a,ble_lll_conn.c.o,r_ble_lll_init_rx_pkt_isr,
|
||||
libble_app.a,ble_lll_conn.c.o,r_ble_lll_conn_get_rx_mbuf,
|
||||
libble_app.a,ble_lll_rfmgmt.c.o,r_ble_lll_rfmgmt_enable,
|
||||
libble_app.a,ble_lll_rfmgmt.c.o,r_ble_lll_rfmgmt_timer_reschedule,
|
||||
libble_app.a,ble_lll_rfmgmt.c.o,r_ble_lll_rfmgmt_timer_exp,
|
||||
libble_app.a,ble_lll_scan.c.o,r_ble_lll_scan_targeta_is_matched,
|
||||
libble_app.a,ble_lll_scan.c.o,r_ble_lll_scan_rx_isr_on_legacy,
|
||||
libble_app.a,ble_lll_scan.c.o,r_ble_lll_scan_rx_isr_on_aux,
|
||||
libble_app.a,ble_lll_scan.c.o,r_ble_lll_scan_process_rsp_in_isr,
|
||||
libble_app.a,ble_lll_scan.c.o,r_ble_lll_scan_rx_pkt_isr,
|
||||
libble_app.a,ble_lll_sched.c.o,r_ble_lll_sched_execute_check,
|
||||
libble_app.a,ble_lll_sync.c.o,r_ble_lll_sync_event_start_cb,
|
||||
libble_app.a,ble_phy.c.o,r_ble_phy_set_rxhdr,
|
||||
libble_app.a,ble_phy.c.o,r_ble_phy_update_conn_sequence,
|
||||
libble_app.a,ble_phy.c.o,r_ble_phy_set_sequence_mode,
|
||||
libble_app.a,ble_phy.c.o,r_ble_phy_txpower_round,
|
||||
libble_app.a,os_mempool.c.obj,r_os_memblock_put,
|
||||
libbootloader_support.a,bootloader_flash.c.obj,bootloader_read_flash_id,
|
||||
libbootloader_support.a,flash_encrypt.c.obj,esp_flash_encryption_enabled,
|
||||
libbt.a,bt_osi_mem.c.obj,bt_osi_mem_calloc,CONFIG_BT_ENABLED
|
||||
libbt.a,bt_osi_mem.c.obj,bt_osi_mem_malloc,CONFIG_BT_ENABLED
|
||||
libbt.a,bt_osi_mem.c.obj,bt_osi_mem_malloc_internal,CONFIG_BT_ENABLED
|
||||
libbt.a,bt_osi_mem.c.obj,bt_osi_mem_free,CONFIG_BT_ENABLED
|
||||
libbt.a,bt.c.obj,esp_reset_rpa_moudle,CONFIG_BT_ENABLED
|
||||
libbt.a,bt.c.obj,osi_assert_wrapper,CONFIG_BT_ENABLED
|
||||
libbt.a,bt.c.obj,osi_random_wrapper,CONFIG_BT_ENABLED
|
||||
libbt.a,nimble_port.c.obj,nimble_port_run,CONFIG_BT_NIMBLE_ENABLED
|
||||
libbt.a,nimble_port.c.obj,nimble_port_get_dflt_eventq,CONFIG_BT_NIMBLE_ENABLED
|
||||
libbt.a,npl_os_freertos.c.obj,os_callout_timer_cb,CONFIG_BT_ENABLED && !CONFIG_BT_NIMBLE_USE_ESP_TIMER
|
||||
libbt.a,npl_os_freertos.c.obj,npl_freertos_event_run,CONFIG_BT_ENABLED
|
||||
libbt.a,npl_os_freertos.c.obj,npl_freertos_callout_stop,CONFIG_BT_ENABLED
|
||||
libbt.a,npl_os_freertos.c.obj,npl_freertos_time_get,CONFIG_BT_ENABLED
|
||||
libbt.a,npl_os_freertos.c.obj,npl_freertos_callout_reset,CONFIG_BT_ENABLED
|
||||
libbt.a,npl_os_freertos.c.obj,npl_freertos_callout_is_active,CONFIG_BT_ENABLED
|
||||
libbt.a,npl_os_freertos.c.obj,npl_freertos_callout_get_ticks,CONFIG_BT_ENABLED
|
||||
libbt.a,npl_os_freertos.c.obj,npl_freertos_callout_remaining_ticks,CONFIG_BT_ENABLED
|
||||
libbt.a,npl_os_freertos.c.obj,npl_freertos_time_delay,CONFIG_BT_ENABLED
|
||||
libbt.a,npl_os_freertos.c.obj,npl_freertos_eventq_is_empty,CONFIG_BT_ENABLED
|
||||
libbt.a,npl_os_freertos.c.obj,npl_freertos_mutex_pend,CONFIG_BT_ENABLED
|
||||
libbt.a,npl_os_freertos.c.obj,npl_freertos_mutex_release,CONFIG_BT_ENABLED
|
||||
libbt.a,npl_os_freertos.c.obj,npl_freertos_sem_init,CONFIG_BT_ENABLED
|
||||
libbt.a,npl_os_freertos.c.obj,npl_freertos_sem_pend,CONFIG_BT_ENABLED
|
||||
libbt.a,npl_os_freertos.c.obj,npl_freertos_sem_release,CONFIG_BT_ENABLED
|
||||
libbt.a,npl_os_freertos.c.obj,npl_freertos_callout_init,CONFIG_BT_ENABLED
|
||||
libbt.a,npl_os_freertos.c.obj,npl_freertos_callout_deinit,CONFIG_BT_ENABLED
|
||||
libbt.a,npl_os_freertos.c.obj,npl_freertos_event_is_queued,CONFIG_BT_ENABLED
|
||||
libbt.a,npl_os_freertos.c.obj,npl_freertos_event_get_arg,CONFIG_BT_ENABLED
|
||||
libbt.a,npl_os_freertos.c.obj,npl_freertos_time_ms_to_ticks32,CONFIG_BT_ENABLED
|
||||
libbt.a,npl_os_freertos.c.obj,npl_freertos_time_ticks_to_ms32,CONFIG_BT_ENABLED
|
||||
libbt.a,npl_os_freertos.c.obj,npl_freertos_hw_is_in_critical,CONFIG_BT_ENABLED
|
||||
libbt.a,npl_os_freertos.c.obj,npl_freertos_get_time_forever,CONFIG_BT_ENABLED
|
||||
libbt.a,npl_os_freertos.c.obj,npl_freertos_os_started,CONFIG_BT_ENABLED
|
||||
libbt.a,npl_os_freertos.c.obj,npl_freertos_get_current_task_id,CONFIG_BT_ENABLED
|
||||
libbt.a,npl_os_freertos.c.obj,npl_freertos_event_reset,CONFIG_BT_ENABLED
|
||||
libbt.a,npl_os_freertos.c.obj,npl_freertos_callout_mem_reset,CONFIG_BT_ENABLED
|
||||
libbt.a,npl_os_freertos.c.obj,npl_freertos_event_init,CONFIG_BT_ENABLED
|
||||
libbt.a,npl_os_freertos.c.obj,npl_freertos_sem_get_count,CONFIG_BT_ENABLED
|
||||
libbt.a,npl_os_freertos.c.obj,npl_freertos_eventq_remove,CONFIG_BT_ENABLED
|
||||
libbt.a,npl_os_freertos.c.obj,npl_freertos_hw_exit_critical,CONFIG_BT_ENABLED
|
||||
libbt.a,npl_os_freertos.c.obj,npl_freertos_mutex_init,CONFIG_BT_ENABLED
|
||||
libbt.a,npl_os_freertos.c.obj,npl_freertos_hw_enter_critical,CONFIG_BT_ENABLED
|
||||
libbt.a,npl_os_freertos.c.obj,npl_freertos_eventq_put,CONFIG_BT_ENABLED
|
||||
libbt.a,npl_os_freertos.c.obj,npl_freertos_eventq_get,CONFIG_BT_ENABLED
|
||||
libbt.a,npl_os_freertos.c.obj,npl_freertos_sem_deinit,CONFIG_BT_ENABLED
|
||||
libbt.a,npl_os_freertos.c.obj,npl_freertos_mutex_deinit,CONFIG_BT_ENABLED
|
||||
libbt.a,npl_os_freertos.c.obj,npl_freertos_eventq_deinit,CONFIG_BT_ENABLED
|
||||
libbt.a,npl_os_freertos.c.obj,npl_freertos_eventq_init,CONFIG_BT_ENABLED
|
||||
libbt.a,npl_os_freertos.c.obj,npl_freertos_event_deinit,CONFIG_BT_ENABLED
|
||||
libbt.a,npl_os_freertos.c.obj,npl_freertos_time_ticks_to_ms,CONFIG_BT_ENABLED
|
||||
libbt.a,npl_os_freertos.c.obj,npl_freertos_time_ms_to_ticks,CONFIG_BT_ENABLED
|
||||
libbt.a,npl_os_freertos.c.obj,npl_freertos_callout_set_arg,CONFIG_BT_ENABLED
|
||||
libbt.a,npl_os_freertos.c.obj,npl_freertos_event_set_arg,CONFIG_BT_ENABLED
|
||||
libbt.a,npl_os_freertos.c.obj,npl_freertos_eventq_put,CONFIG_BT_ENABLED
|
||||
libdriver.a,gpio.c.obj,gpio_intr_service,
|
||||
libesp_app_format.a,esp_app_desc.c.obj,esp_app_get_elf_sha256,
|
||||
libesp_hw_support.a,cpu.c.obj,esp_cpu_wait_for_intr,
|
||||
libesp_hw_support.a,cpu.c.obj,esp_cpu_reset,
|
||||
libesp_hw_support.a,esp_clk.c.obj,esp_clk_cpu_freq,
|
||||
libesp_hw_support.a,esp_clk.c.obj,esp_clk_apb_freq,
|
||||
libesp_hw_support.a,esp_clk.c.obj,esp_clk_xtal_freq,
|
||||
libesp_hw_support.a,esp_memory_utils.c.obj,esp_ptr_byte_accessible,
|
||||
libesp_hw_support.a,hw_random.c.obj,esp_random,
|
||||
libesp_hw_support.a,intr_alloc.c.obj,shared_intr_isr,
|
||||
libesp_hw_support.a,intr_alloc.c.obj,esp_intr_noniram_disable,
|
||||
libesp_hw_support.a,intr_alloc.c.obj,esp_intr_noniram_enable,
|
||||
libesp_hw_support.a,intr_alloc.c.obj,esp_intr_enable,
|
||||
libesp_hw_support.a,intr_alloc.c.obj,esp_intr_disable,
|
||||
libesp_hw_support.a,periph_ctrl.c.obj,wifi_bt_common_module_enable,
|
||||
libesp_hw_support.a,periph_ctrl.c.obj,wifi_bt_common_module_disable,
|
||||
libesp_hw_support.a,regi2c_ctrl.c.obj,regi2c_ctrl_read_reg,
|
||||
libesp_hw_support.a,regi2c_ctrl.c.obj,regi2c_ctrl_read_reg_mask,
|
||||
libesp_hw_support.a,regi2c_ctrl.c.obj,regi2c_ctrl_write_reg,
|
||||
libesp_hw_support.a,regi2c_ctrl.c.obj,regi2c_ctrl_write_reg_mask,
|
||||
libesp_hw_support.a,regi2c_ctrl.c.obj,regi2c_enter_critical,
|
||||
libesp_hw_support.a,regi2c_ctrl.c.obj,regi2c_exit_critical,
|
||||
libesp_hw_support.a,regi2c_ctrl.c.obj,regi2c_analog_cali_reg_read,
|
||||
libesp_hw_support.a,regi2c_ctrl.c.obj,regi2c_analog_cali_reg_write,
|
||||
libesp_hw_support.a,rtc_clk.c.obj,rtc_clk_32k_enable_external,
|
||||
libesp_hw_support.a,rtc_clk.c.obj,rtc_clk_fast_src_set,
|
||||
libesp_hw_support.a,rtc_clk.c.obj,rtc_clk_slow_freq_get_hz,
|
||||
libesp_hw_support.a,rtc_clk.c.obj,rtc_clk_slow_src_get,
|
||||
libesp_hw_support.a,rtc_clk.c.obj,rtc_clk_slow_src_set,
|
||||
libesp_hw_support.a,rtc_clk.c.obj,rtc_dig_clk8m_disable,
|
||||
libesp_hw_support.a,rtc_clk.c.obj,rtc_clk_cpu_freq_set_config_fast,
|
||||
libesp_hw_support.a,rtc_clk.c.obj,rtc_clk_8m_enable,
|
||||
libesp_hw_support.a,rtc_clk.c.obj,rtc_clk_8md256_enabled,
|
||||
libesp_hw_support.a,rtc_clk.c.obj,rtc_clk_bbpll_configure,
|
||||
libesp_hw_support.a,rtc_clk.c.obj,rtc_clk_bbpll_enable,
|
||||
libesp_hw_support.a,rtc_clk.c.obj,rtc_clk_cpu_freq_get_config,
|
||||
libesp_hw_support.a,rtc_clk.c.obj,rtc_clk_cpu_freq_mhz_to_config,
|
||||
libesp_hw_support.a,rtc_clk.c.obj,rtc_clk_cpu_freq_set_config,
|
||||
libesp_hw_support.a,rtc_clk.c.obj,rtc_clk_cpu_freq_to_8m,
|
||||
libesp_hw_support.a,rtc_clk.c.obj,rtc_clk_cpu_freq_to_pll_mhz,
|
||||
libesp_hw_support.a,rtc_clk.c.obj,rtc_clk_cpu_freq_set_xtal,
|
||||
libesp_hw_support.a,rtc_clk.c.obj,rtc_clk_xtal_freq_get,
|
||||
libesp_hw_support.a,rtc_clk.c.obj,rtc_clk_cpu_freq_to_xtal,
|
||||
libesp_hw_support.a,rtc_clk.c.obj,rtc_clk_bbpll_disable,
|
||||
libesp_hw_support.a,rtc_clk.c.obj,rtc_clk_apb_freq_update,
|
||||
libesp_hw_support.a,rtc_clk.c.obj,clk_ll_rtc_slow_get_src,FALSE
|
||||
libesp_hw_support.a,rtc_init.c.obj,rtc_vddsdio_set_config,
|
||||
libesp_hw_support.a,rtc_module.c.obj,rtc_isr,
|
||||
libesp_hw_support.a,rtc_module.c.obj,rtc_isr_noniram_disable,
|
||||
libesp_hw_support.a,rtc_module.c.obj,rtc_isr_noniram_enable,
|
||||
libesp_hw_support.a,rtc_sleep.c.obj,rtc_sleep_pu,
|
||||
libesp_hw_support.a,rtc_sleep.c.obj,rtc_sleep_finish,
|
||||
libesp_hw_support.a,rtc_sleep.c.obj,rtc_sleep_get_default_config,
|
||||
libesp_hw_support.a,rtc_sleep.c.obj,rtc_sleep_init,
|
||||
libesp_hw_support.a,rtc_sleep.c.obj,rtc_sleep_low_init,
|
||||
libesp_hw_support.a,rtc_sleep.c.obj,rtc_sleep_start,
|
||||
libesp_hw_support.a,rtc_time.c.obj,rtc_clk_cal,
|
||||
libesp_hw_support.a,rtc_time.c.obj,rtc_clk_cal_internal,
|
||||
libesp_hw_support.a,rtc_time.c.obj,rtc_time_get,
|
||||
libesp_hw_support.a,rtc_time.c.obj,rtc_time_us_to_slowclk,
|
||||
libesp_hw_support.a,rtc_time.c.obj,rtc_time_slowclk_to_us,
|
||||
libesp_hw_support.a,sleep_modes.c.obj,periph_ll_periph_enabled,
|
||||
libesp_hw_support.a,sleep_modes.c.obj,flush_uarts,
|
||||
libesp_hw_support.a,sleep_modes.c.obj,suspend_uarts,
|
||||
libesp_hw_support.a,sleep_modes.c.obj,resume_uarts,
|
||||
libesp_hw_support.a,sleep_modes.c.obj,esp_sleep_start,
|
||||
libesp_hw_support.a,sleep_modes.c.obj,esp_deep_sleep_start,
|
||||
libesp_hw_support.a,sleep_modes.c.obj,esp_light_sleep_inner,
|
||||
libesp_phy.a,phy_init.c.obj,esp_phy_common_clock_enable,
|
||||
libesp_phy.a,phy_init.c.obj,esp_phy_common_clock_disable,
|
||||
libesp_phy.a,phy_init.c.obj,esp_wifi_bt_power_domain_on,
|
||||
libesp_phy.a,phy_override.c.obj,phy_i2c_enter_critical,
|
||||
libesp_phy.a,phy_override.c.obj,phy_i2c_exit_critical,
|
||||
libesp_pm.a,pm_locks.c.obj,esp_pm_lock_acquire,
|
||||
libesp_pm.a,pm_locks.c.obj,esp_pm_lock_release,
|
||||
libesp_pm.a,pm_impl.c.obj,get_lowest_allowed_mode,
|
||||
libesp_pm.a,pm_impl.c.obj,esp_pm_impl_switch_mode,
|
||||
libesp_pm.a,pm_impl.c.obj,on_freq_update,
|
||||
libesp_pm.a,pm_impl.c.obj,vApplicationSleep,CONFIG_FREERTOS_USE_TICKLESS_IDLE
|
||||
libesp_pm.a,pm_impl.c.obj,do_switch,
|
||||
libesp_ringbuf.a,ringbuf.c.obj,prvCheckItemAvail,
|
||||
libesp_ringbuf.a,ringbuf.c.obj,prvGetFreeSize,
|
||||
libesp_ringbuf.a,ringbuf.c.obj,prvReceiveGenericFromISR,
|
||||
libesp_ringbuf.a,ringbuf.c.obj,xRingbufferGetMaxItemSize,
|
||||
libesp_rom.a,esp_rom_systimer.c.obj,systimer_hal_init,
|
||||
libesp_rom.a,esp_rom_systimer.c.obj,systimer_hal_set_alarm_period,
|
||||
libesp_rom.a,esp_rom_systimer.c.obj,systimer_hal_set_alarm_target,
|
||||
libesp_rom.a,esp_rom_systimer.c.obj,systimer_hal_set_tick_rate_ops,
|
||||
libesp_rom.a,esp_rom_uart.c.obj,esp_rom_uart_set_clock_baudrate,
|
||||
libesp_system.a,brownout.c.obj,rtc_brownout_isr_handler,
|
||||
libesp_system.a,cache_err_int.c.obj,esp_cache_err_get_cpuid,
|
||||
libesp_system.a,cpu_start.c.obj,call_start_cpu0,
|
||||
libesp_system.a,crosscore_int.c.obj,esp_crosscore_int_send,
|
||||
libesp_system.a,crosscore_int.c.obj,esp_crosscore_int_send_yield,
|
||||
libesp_system.a,esp_system.c.obj,esp_restart,
|
||||
libesp_system.a,esp_system.c.obj,esp_system_abort,
|
||||
libesp_system.a,reset_reason.c.obj,esp_reset_reason_set_hint,
|
||||
libesp_system.a,reset_reason.c.obj,esp_reset_reason_get_hint,
|
||||
libesp_system.a,ubsan.c.obj,__ubsan_include,
|
||||
libesp_timer.a,esp_timer.c.obj,esp_timer_get_next_alarm_for_wake_up,
|
||||
libesp_timer.a,esp_timer.c.obj,timer_list_unlock,!CONFIG_ESP_TIMER_SUPPORTS_ISR_DISPATCH_METHOD
|
||||
libesp_timer.a,esp_timer.c.obj,timer_list_lock,!CONFIG_ESP_TIMER_SUPPORTS_ISR_DISPATCH_METHOD
|
||||
libesp_timer.a,esp_timer.c.obj,timer_armed,
|
||||
libesp_timer.a,esp_timer.c.obj,timer_remove,
|
||||
libesp_timer.a,esp_timer.c.obj,timer_insert,!CONFIG_ESP_TIMER_SUPPORTS_ISR_DISPATCH_METHOD
|
||||
libesp_timer.a,esp_timer.c.obj,esp_timer_start_once,
|
||||
libesp_timer.a,esp_timer.c.obj,esp_timer_start_periodic,
|
||||
libesp_timer.a,esp_timer.c.obj,esp_timer_stop,
|
||||
libesp_timer.a,esp_timer.c.obj,esp_timer_get_expiry_time,
|
||||
libesp_timer.a,esp_timer_impl_systimer.c.obj,esp_timer_impl_get_time,!CONFIG_ESP_TIMER_SUPPORTS_ISR_DISPATCH_METHOD
|
||||
libesp_timer.a,esp_timer_impl_systimer.c.obj,esp_timer_impl_set_alarm_id,!CONFIG_ESP_TIMER_SUPPORTS_ISR_DISPATCH_METHOD
|
||||
libesp_timer.a,esp_timer_impl_systimer.c.obj,esp_timer_impl_update_apb_freq,
|
||||
libesp_timer.a,esp_timer_impl_systimer.c.obj,esp_timer_impl_get_min_period_us,
|
||||
libesp_timer.a,ets_timer_legacy.c.obj,timer_initialized,
|
||||
libesp_timer.a,ets_timer_legacy.c.obj,ets_timer_arm_us,
|
||||
libesp_timer.a,ets_timer_legacy.c.obj,ets_timer_arm,
|
||||
libesp_timer.a,ets_timer_legacy.c.obj,ets_timer_disarm,
|
||||
libesp_timer.a,system_time.c.obj,esp_system_get_time,
|
||||
libesp_wifi.a,esp_adapter.c.obj,semphr_take_from_isr_wrapper,
|
||||
libesp_wifi.a,esp_adapter.c.obj,wifi_realloc,
|
||||
libesp_wifi.a,esp_adapter.c.obj,coex_event_duration_get_wrapper,
|
||||
libesp_wifi.a,esp_adapter.c.obj,coex_schm_interval_set_wrapper,
|
||||
libesp_wifi.a,esp_adapter.c.obj,esp_empty_wrapper,
|
||||
libesp_wifi.a,esp_adapter.c.obj,wifi_calloc,
|
||||
libesp_wifi.a,esp_adapter.c.obj,wifi_zalloc_wrapper,
|
||||
libesp_wifi.a,esp_adapter.c.obj,env_is_chip_wrapper,
|
||||
libesp_wifi.a,esp_adapter.c.obj,is_from_isr_wrapper,
|
||||
libesp_wifi.a,esp_adapter.c.obj,semphr_give_from_isr_wrapper,
|
||||
libesp_wifi.a,esp_adapter.c.obj,mutex_lock_wrapper,
|
||||
libesp_wifi.a,esp_adapter.c.obj,mutex_unlock_wrapper,
|
||||
libesp_wifi.a,esp_adapter.c.obj,task_ms_to_tick_wrapper,
|
||||
libesp_wifi.a,esp_adapter.c.obj,wifi_apb80m_request_wrapper,
|
||||
libesp_wifi.a,esp_adapter.c.obj,wifi_apb80m_release_wrapper,
|
||||
libesp_wifi.a,esp_adapter.c.obj,timer_arm_wrapper,
|
||||
libesp_wifi.a,esp_adapter.c.obj,wifi_malloc,
|
||||
libesp_wifi.a,esp_adapter.c.obj,timer_disarm_wrapper,
|
||||
libesp_wifi.a,esp_adapter.c.obj,timer_arm_us_wrapper,
|
||||
libesp_wifi.a,esp_adapter.c.obj,wifi_rtc_enable_iso_wrapper,
|
||||
libesp_wifi.a,esp_adapter.c.obj,wifi_rtc_disable_iso_wrapper,
|
||||
libesp_wifi.a,esp_adapter.c.obj,malloc_internal_wrapper,
|
||||
libesp_wifi.a,esp_adapter.c.obj,realloc_internal_wrapper,
|
||||
libesp_wifi.a,esp_adapter.c.obj,calloc_internal_wrapper,
|
||||
libesp_wifi.a,esp_adapter.c.obj,zalloc_internal_wrapper,
|
||||
libesp_wifi.a,esp_adapter.c.obj,coex_status_get_wrapper,
|
||||
libesp_wifi.a,esp_adapter.c.obj,coex_wifi_release_wrapper,
|
||||
libfreertos.a,list.c.obj,uxListRemove,FALSE
|
||||
libfreertos.a,list.c.obj,vListInitialise,CONFIG_FREERTOS_PLACE_FUNCTIONS_INTO_FLASH
|
||||
libfreertos.a,list.c.obj,vListInitialiseItem,CONFIG_FREERTOS_PLACE_FUNCTIONS_INTO_FLASH
|
||||
libfreertos.a,list.c.obj,vListInsert,CONFIG_FREERTOS_PLACE_FUNCTIONS_INTO_FLASH
|
||||
libfreertos.a,list.c.obj,vListInsertEnd,FALSE
|
||||
libfreertos.a,port.c.obj,vApplicationStackOverflowHook,FALSE
|
||||
libfreertos.a,port.c.obj,vPortYieldOtherCore,FALSE
|
||||
libfreertos.a,port.c.obj,vPortYield,
|
||||
libfreertos.a,port_common.c.obj,xPortcheckValidStackMem,
|
||||
libfreertos.a,port_common.c.obj,vApplicationGetTimerTaskMemory,
|
||||
libfreertos.a,port_common.c.obj,esp_startup_start_app_common,
|
||||
libfreertos.a,port_common.c.obj,xPortCheckValidTCBMem,
|
||||
libfreertos.a,port_common.c.obj,vApplicationGetIdleTaskMemory,
|
||||
libfreertos.a,port_systick.c.obj,vPortSetupTimer,
|
||||
libfreertos.a,port_systick.c.obj,xPortSysTickHandler,FALSE
|
||||
libfreertos.a,queue.c.obj,prvCopyDataFromQueue,
|
||||
libfreertos.a,queue.c.obj,prvGetDisinheritPriorityAfterTimeout,
|
||||
libfreertos.a,queue.c.obj,prvIsQueueEmpty,
|
||||
libfreertos.a,queue.c.obj,prvNotifyQueueSetContainer,
|
||||
libfreertos.a,queue.c.obj,xQueueReceive,
|
||||
libfreertos.a,queue.c.obj,prvUnlockQueue,
|
||||
libfreertos.a,queue.c.obj,xQueueSemaphoreTake,
|
||||
libfreertos.a,queue.c.obj,xQueueReceiveFromISR,
|
||||
libfreertos.a,queue.c.obj,uxQueueMessagesWaitingFromISR,
|
||||
libfreertos.a,queue.c.obj,xQueueIsQueueEmptyFromISR,
|
||||
libfreertos.a,queue.c.obj,xQueueGiveFromISR,
|
||||
libfreertos.a,tasks.c.obj,__getreent,CONFIG_FREERTOS_PLACE_FUNCTIONS_INTO_FLASH
|
||||
libfreertos.a,tasks.c.obj,pcTaskGetName,CONFIG_FREERTOS_PLACE_FUNCTIONS_INTO_FLASH
|
||||
libfreertos.a,tasks.c.obj,prvAddCurrentTaskToDelayedList,CONFIG_FREERTOS_PLACE_FUNCTIONS_INTO_FLASH
|
||||
libfreertos.a,tasks.c.obj,prvDeleteTLS,CONFIG_FREERTOS_PLACE_FUNCTIONS_INTO_FLASH
|
||||
libfreertos.a,tasks.c.obj,pvTaskIncrementMutexHeldCount,CONFIG_FREERTOS_PLACE_FUNCTIONS_INTO_FLASH
|
||||
libfreertos.a,tasks.c.obj,taskSelectHighestPriorityTaskSMP,FALSE
|
||||
libfreertos.a,tasks.c.obj,taskYIELD_OTHER_CORE,FALSE
|
||||
libfreertos.a,tasks.c.obj,vTaskGetSnapshot,CONFIG_FREERTOS_PLACE_SNAPSHOT_FUNS_INTO_FLASH
|
||||
libfreertos.a,tasks.c.obj,vTaskInternalSetTimeOutState,CONFIG_FREERTOS_PLACE_FUNCTIONS_INTO_FLASH
|
||||
libfreertos.a,tasks.c.obj,vTaskPlaceOnEventList,CONFIG_FREERTOS_PLACE_FUNCTIONS_INTO_FLASH
|
||||
libfreertos.a,tasks.c.obj,vTaskPlaceOnEventListRestricted,CONFIG_FREERTOS_PLACE_FUNCTIONS_INTO_FLASH
|
||||
libfreertos.a,tasks.c.obj,vTaskPlaceOnUnorderedEventList,CONFIG_FREERTOS_PLACE_FUNCTIONS_INTO_FLASH
|
||||
libfreertos.a,tasks.c.obj,vTaskPriorityDisinheritAfterTimeout,CONFIG_FREERTOS_PLACE_FUNCTIONS_INTO_FLASH
|
||||
libfreertos.a,tasks.c.obj,vTaskReleaseEventListLock,CONFIG_FREERTOS_PLACE_FUNCTIONS_INTO_FLASH
|
||||
libfreertos.a,tasks.c.obj,vTaskTakeEventListLock,CONFIG_FREERTOS_PLACE_FUNCTIONS_INTO_FLASH
|
||||
libfreertos.a,tasks.c.obj,xTaskCheckForTimeOut,CONFIG_FREERTOS_PLACE_FUNCTIONS_INTO_FLASH
|
||||
libfreertos.a,tasks.c.obj,xTaskGetCurrentTaskHandle,CONFIG_FREERTOS_PLACE_FUNCTIONS_INTO_FLASH
|
||||
libfreertos.a,tasks.c.obj,xTaskGetSchedulerState,CONFIG_FREERTOS_PLACE_FUNCTIONS_INTO_FLASH
|
||||
libfreertos.a,tasks.c.obj,xTaskGetTickCount,CONFIG_FREERTOS_PLACE_FUNCTIONS_INTO_FLASH
|
||||
libfreertos.a,tasks.c.obj,xTaskPriorityDisinherit,FALSE
|
||||
libfreertos.a,tasks.c.obj,xTaskPriorityInherit,CONFIG_FREERTOS_PLACE_FUNCTIONS_INTO_FLASH
|
||||
libfreertos.a,tasks.c.obj,prvGetExpectedIdleTime,FALSE
|
||||
libfreertos.a,tasks.c.obj,vTaskStepTick,FALSE
|
||||
libhal.a,brownout_hal.c.obj,brownout_hal_intr_clear,
|
||||
libhal.a,efuse_hal.c.obj,efuse_hal_chip_revision,
|
||||
libhal.a,efuse_hal.c.obj,efuse_hal_get_major_chip_version,
|
||||
libhal.a,efuse_hal.c.obj,efuse_hal_get_minor_chip_version,
|
||||
libheap.a,heap_caps.c.obj,dram_alloc_to_iram_addr,
|
||||
libheap.a,heap_caps.c.obj,heap_caps_free,
|
||||
libheap.a,heap_caps.c.obj,heap_caps_realloc_base,
|
||||
libheap.a,heap_caps.c.obj,heap_caps_realloc,
|
||||
libheap.a,heap_caps.c.obj,heap_caps_calloc_base,
|
||||
libheap.a,heap_caps.c.obj,heap_caps_calloc,
|
||||
libheap.a,heap_caps.c.obj,heap_caps_malloc_base,
|
||||
libheap.a,heap_caps.c.obj,heap_caps_malloc,
|
||||
libheap.a,heap_caps.c.obj,heap_caps_malloc_default,
|
||||
libheap.a,heap_caps.c.obj,heap_caps_realloc_default,
|
||||
libheap.a,heap_caps.c.obj,find_containing_heap,
|
||||
libheap.a,multi_heap.c.obj,_multi_heap_lock,
|
||||
libheap.a,multi_heap.c.obj,multi_heap_in_rom_init,
|
||||
liblog.a,log_freertos.c.obj,esp_log_timestamp,
|
||||
liblog.a,log_freertos.c.obj,esp_log_impl_lock,
|
||||
liblog.a,log_freertos.c.obj,esp_log_impl_unlock,
|
||||
liblog.a,log_freertos.c.obj,esp_log_impl_lock_timeout,
|
||||
liblog.a,log_freertos.c.obj,esp_log_early_timestamp,
|
||||
liblog.a,log.c.obj,esp_log_write,
|
||||
libmbedcrypto.a,esp_mem.c.obj,esp_mbedtls_mem_calloc,!CONFIG_MBEDTLS_CUSTOM_MEM_ALLOC
|
||||
libmbedcrypto.a,esp_mem.c.obj,esp_mbedtls_mem_free,!CONFIG_MBEDTLS_CUSTOM_MEM_ALLOC
|
||||
libnewlib.a,assert.c.obj,__assert_func,
|
||||
libnewlib.a,assert.c.obj,newlib_include_assert_impl,
|
||||
libnewlib.a,heap.c.obj,_calloc_r,
|
||||
libnewlib.a,heap.c.obj,_free_r,
|
||||
libnewlib.a,heap.c.obj,_malloc_r,
|
||||
libnewlib.a,heap.c.obj,_realloc_r,
|
||||
libnewlib.a,heap.c.obj,calloc,
|
||||
libnewlib.a,heap.c.obj,cfree,
|
||||
libnewlib.a,heap.c.obj,free,
|
||||
libnewlib.a,heap.c.obj,malloc,
|
||||
libnewlib.a,heap.c.obj,newlib_include_heap_impl,
|
||||
libnewlib.a,heap.c.obj,realloc,
|
||||
libnewlib.a,locks.c.obj,_lock_try_acquire_recursive,
|
||||
libnewlib.a,locks.c.obj,lock_release_generic,
|
||||
libnewlib.a,locks.c.obj,_lock_release,
|
||||
libnewlib.a,locks.c.obj,_lock_release_recursive,
|
||||
libnewlib.a,locks.c.obj,__retarget_lock_init,
|
||||
libnewlib.a,locks.c.obj,__retarget_lock_init_recursive,
|
||||
libnewlib.a,locks.c.obj,__retarget_lock_close,
|
||||
libnewlib.a,locks.c.obj,__retarget_lock_close_recursive,
|
||||
libnewlib.a,locks.c.obj,check_lock_nonzero,
|
||||
libnewlib.a,locks.c.obj,__retarget_lock_acquire,
|
||||
libnewlib.a,locks.c.obj,lock_init_generic,
|
||||
libnewlib.a,locks.c.obj,__retarget_lock_acquire_recursive,
|
||||
libnewlib.a,locks.c.obj,__retarget_lock_try_acquire,
|
||||
libnewlib.a,locks.c.obj,__retarget_lock_try_acquire_recursive,
|
||||
libnewlib.a,locks.c.obj,__retarget_lock_release,
|
||||
libnewlib.a,locks.c.obj,__retarget_lock_release_recursive,
|
||||
libnewlib.a,locks.c.obj,_lock_close,
|
||||
libnewlib.a,locks.c.obj,lock_acquire_generic,
|
||||
libnewlib.a,locks.c.obj,_lock_acquire,
|
||||
libnewlib.a,locks.c.obj,_lock_acquire_recursive,
|
||||
libnewlib.a,locks.c.obj,_lock_try_acquire,
|
||||
libnewlib.a,reent_init.c.obj,esp_reent_init,
|
||||
libnewlib.a,time.c.obj,_times_r,
|
||||
libnewlib.a,time.c.obj,_gettimeofday_r,
|
||||
libpp.a,pp_debug.o,wifi_gpio_debug,
|
||||
libpthread.a,pthread.c.obj,pthread_mutex_lock_internal,
|
||||
libpthread.a,pthread.c.obj,pthread_mutex_lock,
|
||||
libpthread.a,pthread.c.obj,pthread_mutex_unlock,
|
||||
libriscv.a,interrupt.c.obj,intr_handler_get,
|
||||
libriscv.a,interrupt.c.obj,intr_handler_set,
|
||||
libriscv.a,interrupt.c.obj,intr_matrix_route,
|
||||
libspi_flash.a,flash_brownout_hook.c.obj,spi_flash_needs_reset_check,FALSE
|
||||
libspi_flash.a,flash_brownout_hook.c.obj,spi_flash_set_erasing_flag,FALSE
|
||||
libspi_flash.a,flash_ops.c.obj,spi_flash_guard_set,CONFIG_SPI_FLASH_ROM_IMPL
|
||||
libspi_flash.a,flash_ops.c.obj,spi_flash_malloc_internal,CONFIG_SPI_FLASH_ROM_IMPL
|
||||
libspi_flash.a,flash_ops.c.obj,spi_flash_rom_impl_init,CONFIG_SPI_FLASH_ROM_IMPL
|
||||
libspi_flash.a,flash_ops.c.obj,esp_mspi_pin_init,CONFIG_SPI_FLASH_ROM_IMPL
|
||||
libspi_flash.a,flash_ops.c.obj,spi_flash_init_chip_state,CONFIG_SPI_FLASH_ROM_IMPL
|
||||
libspi_flash.a,spi_flash_os_func_app.c.obj,delay_us,CONFIG_SPI_FLASH_ROM_IMPL
|
||||
libspi_flash.a,spi_flash_os_func_app.c.obj,get_buffer_malloc,CONFIG_SPI_FLASH_ROM_IMPL
|
||||
libspi_flash.a,spi_flash_os_func_app.c.obj,release_buffer_malloc,CONFIG_SPI_FLASH_ROM_IMPL
|
||||
libspi_flash.a,spi_flash_os_func_app.c.obj,main_flash_region_protected,CONFIG_SPI_FLASH_ROM_IMPL
|
||||
libspi_flash.a,spi_flash_os_func_app.c.obj,main_flash_op_status,CONFIG_SPI_FLASH_ROM_IMPL
|
||||
libspi_flash.a,spi_flash_os_func_app.c.obj,spi1_flash_os_check_yield,CONFIG_SPI_FLASH_ROM_IMPL
|
||||
libspi_flash.a,spi_flash_os_func_app.c.obj,spi1_flash_os_yield,CONFIG_SPI_FLASH_ROM_IMPL
|
||||
libspi_flash.a,spi_flash_os_func_noos.c.obj,start,CONFIG_SPI_FLASH_ROM_IMPL
|
||||
libspi_flash.a,spi_flash_os_func_noos.c.obj,end,CONFIG_SPI_FLASH_ROM_IMPL
|
||||
libspi_flash.a,spi_flash_os_func_noos.c.obj,delay_us,CONFIG_SPI_FLASH_ROM_IMPL
|
||||
|
@@ -0,0 +1,24 @@
|
||||
library,path
|
||||
libble_app.a,$IDF_PATH/components/bt/controller/lib_esp32c2/esp32c2-bt-lib/libble_app.a
|
||||
libpp.a,$IDF_PATH/components/esp_wifi/lib/esp32c2/libpp.a
|
||||
libbootloader_support.a,./esp-idf/bootloader_support/libbootloader_support.a
|
||||
libbt.a,./esp-idf/bt/libbt.a
|
||||
libdriver.a,./esp-idf/driver/libdriver.a
|
||||
libesp_app_format.a,./esp-idf/esp_app_format/libesp_app_format.a
|
||||
libesp_hw_support.a,./esp-idf/esp_hw_support/libesp_hw_support.a
|
||||
libesp_phy.a,./esp-idf/esp_phy/libesp_phy.a
|
||||
libesp_pm.a,./esp-idf/esp_pm/libesp_pm.a
|
||||
libesp_ringbuf.a,./esp-idf/esp_ringbuf/libesp_ringbuf.a
|
||||
libesp_rom.a,./esp-idf/esp_rom/libesp_rom.a
|
||||
libesp_system.a,./esp-idf/esp_system/libesp_system.a
|
||||
libesp_timer.a,./esp-idf/esp_timer/libesp_timer.a
|
||||
libesp_wifi.a,./esp-idf/esp_wifi/libesp_wifi.a
|
||||
libfreertos.a,./esp-idf/freertos/libfreertos.a
|
||||
libhal.a,./esp-idf/hal/libhal.a
|
||||
libheap.a,./esp-idf/heap/libheap.a
|
||||
liblog.a,./esp-idf/log/liblog.a
|
||||
libmbedcrypto.a,./esp-idf/mbedtls/mbedtls/library/libmbedcrypto.a
|
||||
libnewlib.a,./esp-idf/newlib/libnewlib.a
|
||||
libpthread.a,./esp-idf/pthread/libpthread.a
|
||||
libriscv.a,./esp-idf/riscv/libriscv.a
|
||||
libspi_flash.a,./esp-idf/spi_flash/libspi_flash.a
|
||||
|
@@ -0,0 +1,66 @@
|
||||
library,object,path
|
||||
libbootloader_support.a,bootloader_flash.c.obj,esp-idf/bootloader_support/CMakeFiles/__idf_bootloader_support.dir/bootloader_flash/src/bootloader_flash.c.obj
|
||||
libbootloader_support.a,flash_encrypt.c.obj,esp-idf/bootloader_support/CMakeFiles/__idf_bootloader_support.dir/src/flash_encrypt.c.obj
|
||||
libbt.a,bt_osi_mem.c.obj,esp-idf/bt/CMakeFiles/__idf_bt.dir/porting/mem/bt_osi_mem.c.obj
|
||||
libbt.a,bt.c.obj,esp-idf/bt/CMakeFiles/__idf_bt.dir/controller/esp32c2/bt.c.obj
|
||||
libbt.a,npl_os_freertos.c.obj,esp-idf/bt/CMakeFiles/__idf_bt.dir/porting/npl/freertos/src/npl_os_freertos.c.obj
|
||||
libbt.a,nimble_port.c.obj,esp-idf/bt/CMakeFiles/__idf_bt.dir/host/nimble/nimble/porting/nimble/src/nimble_port.c.obj
|
||||
libdriver.a,gpio.c.obj,esp-idf/driver/CMakeFiles/__idf_driver.dir/gpio/gpio.c.obj
|
||||
libesp_app_format.a,esp_app_desc.c.obj,esp-idf/esp_app_format/CMakeFiles/__idf_esp_app_format.dir/esp_app_desc.c.obj
|
||||
libesp_hw_support.a,cpu.c.obj,esp-idf/esp_hw_support/CMakeFiles/__idf_esp_hw_support.dir/cpu.c.obj
|
||||
libesp_hw_support.a,esp_clk.c.obj,esp-idf/esp_hw_support/CMakeFiles/__idf_esp_hw_support.dir/esp_clk.c.obj
|
||||
libesp_hw_support.a,esp_memory_utils.c.obj,esp-idf/esp_hw_support/CMakeFiles/__idf_esp_hw_support.dir/esp_memory_utils.c.obj
|
||||
libesp_hw_support.a,hw_random.c.obj,esp-idf/esp_hw_support/CMakeFiles/__idf_esp_hw_support.dir/hw_random.c.obj
|
||||
libesp_hw_support.a,intr_alloc.c.obj,esp-idf/esp_hw_support/CMakeFiles/__idf_esp_hw_support.dir/intr_alloc.c.obj
|
||||
libesp_hw_support.a,periph_ctrl.c.obj,esp-idf/esp_hw_support/CMakeFiles/__idf_esp_hw_support.dir/periph_ctrl.c.obj
|
||||
libesp_hw_support.a,regi2c_ctrl.c.obj,esp-idf/esp_hw_support/CMakeFiles/__idf_esp_hw_support.dir/regi2c_ctrl.c.obj
|
||||
libesp_hw_support.a,rtc_clk.c.obj,esp-idf/esp_hw_support/CMakeFiles/__idf_esp_hw_support.dir/port/esp32c2/rtc_clk.c.obj
|
||||
libesp_hw_support.a,rtc_init.c.obj,esp-idf/esp_hw_support/CMakeFiles/__idf_esp_hw_support.dir/port/esp32c2/rtc_init.c.obj
|
||||
libesp_hw_support.a,rtc_module.c.obj,esp-idf/esp_hw_support/CMakeFiles/__idf_esp_hw_support.dir/rtc_module.c.obj
|
||||
libesp_hw_support.a,rtc_sleep.c.obj,esp-idf/esp_hw_support/CMakeFiles/__idf_esp_hw_support.dir/port/esp32c2/rtc_sleep.c.obj
|
||||
libesp_hw_support.a,rtc_time.c.obj,esp-idf/esp_hw_support/CMakeFiles/__idf_esp_hw_support.dir/port/esp32c2/rtc_time.c.obj
|
||||
libesp_hw_support.a,sleep_modes.c.obj,esp-idf/esp_hw_support/CMakeFiles/__idf_esp_hw_support.dir/sleep_modes.c.obj
|
||||
libesp_phy.a,phy_init.c.obj,esp-idf/esp_phy/CMakeFiles/__idf_esp_phy.dir/src/phy_init.c.obj
|
||||
libesp_phy.a,phy_override.c.obj,esp-idf/esp_phy/CMakeFiles/__idf_esp_phy.dir/src/phy_override.c.obj
|
||||
libesp_pm.a,pm_locks.c.obj,esp-idf/esp_pm/CMakeFiles/__idf_esp_pm.dir/pm_locks.c.obj
|
||||
libesp_pm.a,pm_impl.c.obj,esp-idf/esp_pm/CMakeFiles/__idf_esp_pm.dir/pm_impl.c.obj
|
||||
libesp_ringbuf.a,ringbuf.c.obj,esp-idf/esp_ringbuf/CMakeFiles/__idf_esp_ringbuf.dir/ringbuf.c.obj
|
||||
libesp_rom.a,esp_rom_systimer.c.obj,esp-idf/esp_rom/CMakeFiles/__idf_esp_rom.dir/patches/esp_rom_systimer.c.obj
|
||||
libesp_rom.a,esp_rom_uart.c.obj,esp-idf/esp_rom/CMakeFiles/__idf_esp_rom.dir/patches/esp_rom_uart.c.obj
|
||||
libesp_system.a,brownout.c.obj,esp-idf/esp_system/CMakeFiles/__idf_esp_system.dir/port/brownout.c.obj
|
||||
libesp_system.a,cache_err_int.c.obj,esp-idf/esp_system/CMakeFiles/__idf_esp_system.dir/port/soc/esp32c2/cache_err_int.c.obj
|
||||
libesp_system.a,cpu_start.c.obj,esp-idf/esp_system/CMakeFiles/__idf_esp_system.dir/port/cpu_start.c.obj
|
||||
libesp_system.a,crosscore_int.c.obj,esp-idf/esp_system/CMakeFiles/__idf_esp_system.dir/crosscore_int.c.obj
|
||||
libesp_system.a,esp_system.c.obj,esp-idf/esp_system/CMakeFiles/__idf_esp_system.dir/esp_system.c.obj
|
||||
libesp_system.a,reset_reason.c.obj,esp-idf/esp_system/CMakeFiles/__idf_esp_system.dir/port/soc/esp32c2/reset_reason.c.obj
|
||||
libesp_system.a,ubsan.c.obj,esp-idf/esp_system/CMakeFiles/__idf_esp_system.dir/ubsan.c.obj
|
||||
libesp_timer.a,esp_timer.c.obj,esp-idf/esp_timer/CMakeFiles/__idf_esp_timer.dir/src/esp_timer.c.obj
|
||||
libesp_timer.a,esp_timer_impl_systimer.c.obj,esp-idf/esp_timer/CMakeFiles/__idf_esp_timer.dir/src/esp_timer_impl_systimer.c.obj
|
||||
libesp_timer.a,ets_timer_legacy.c.obj,esp-idf/esp_timer/CMakeFiles/__idf_esp_timer.dir/src/ets_timer_legacy.c.obj
|
||||
libesp_timer.a,system_time.c.obj,esp-idf/esp_timer/CMakeFiles/__idf_esp_timer.dir/src/system_time.c.obj
|
||||
libesp_wifi.a,esp_adapter.c.obj,esp-idf/esp_wifi/CMakeFiles/__idf_esp_wifi.dir/esp32c2/esp_adapter.c.obj
|
||||
libfreertos.a,list.c.obj,esp-idf/freertos/CMakeFiles/__idf_freertos.dir/FreeRTOS-Kernel/list.c.obj
|
||||
libfreertos.a,port.c.obj,esp-idf/freertos/CMakeFiles/__idf_freertos.dir/FreeRTOS-Kernel/portable/riscv/port.c.obj
|
||||
libfreertos.a,port_common.c.obj,esp-idf/freertos/CMakeFiles/__idf_freertos.dir/FreeRTOS-Kernel/portable/port_common.c.obj
|
||||
libfreertos.a,port_systick.c.obj,esp-idf/freertos/CMakeFiles/__idf_freertos.dir/FreeRTOS-Kernel/portable/port_systick.c.obj
|
||||
libfreertos.a,queue.c.obj,esp-idf/freertos/CMakeFiles/__idf_freertos.dir/FreeRTOS-Kernel/queue.c.obj
|
||||
libfreertos.a,tasks.c.obj,esp-idf/freertos/CMakeFiles/__idf_freertos.dir/FreeRTOS-Kernel/tasks.c.obj
|
||||
libhal.a,brownout_hal.c.obj,esp-idf/hal/CMakeFiles/__idf_hal.dir/esp32c2/brownout_hal.c.obj
|
||||
libhal.a,efuse_hal.c.obj,esp-idf/hal/CMakeFiles/__idf_hal.dir/efuse_hal.c.obj
|
||||
libhal.a,efuse_hal.c.obj,esp-idf/hal/CMakeFiles/__idf_hal.dir/esp32c2/efuse_hal.c.obj
|
||||
libheap.a,heap_caps.c.obj,esp-idf/heap/CMakeFiles/__idf_heap.dir/heap_caps.c.obj
|
||||
libheap.a,multi_heap.c.obj,./esp-idf/heap/CMakeFiles/__idf_heap.dir/multi_heap.c.obj
|
||||
liblog.a,log_freertos.c.obj,esp-idf/log/CMakeFiles/__idf_log.dir/log_freertos.c.obj
|
||||
liblog.a,log.c.obj,esp-idf/log/CMakeFiles/__idf_log.dir/log.c.obj
|
||||
libmbedcrypto.a,esp_mem.c.obj,esp-idf/mbedtls/mbedtls/library/CMakeFiles/mbedcrypto.dir/$IDF_PATH/components/mbedtls/port/esp_mem.c.obj
|
||||
libnewlib.a,assert.c.obj,esp-idf/newlib/CMakeFiles/__idf_newlib.dir/assert.c.obj
|
||||
libnewlib.a,heap.c.obj,esp-idf/newlib/CMakeFiles/__idf_newlib.dir/heap.c.obj
|
||||
libnewlib.a,locks.c.obj,esp-idf/newlib/CMakeFiles/__idf_newlib.dir/locks.c.obj
|
||||
libnewlib.a,reent_init.c.obj,esp-idf/newlib/CMakeFiles/__idf_newlib.dir/reent_init.c.obj
|
||||
libnewlib.a,time.c.obj,esp-idf/newlib/CMakeFiles/__idf_newlib.dir/time.c.obj
|
||||
libpthread.a,pthread.c.obj,esp-idf/pthread/CMakeFiles/__idf_pthread.dir/pthread.c.obj
|
||||
libriscv.a,interrupt.c.obj,esp-idf/riscv/CMakeFiles/__idf_riscv.dir/interrupt.c.obj
|
||||
libspi_flash.a,flash_brownout_hook.c.obj,esp-idf/spi_flash/CMakeFiles/__idf_spi_flash.dir/flash_brownout_hook.c.obj
|
||||
libspi_flash.a,flash_ops.c.obj,esp-idf/spi_flash/CMakeFiles/__idf_spi_flash.dir/flash_ops.c.obj
|
||||
libspi_flash.a,spi_flash_os_func_app.c.obj,esp-idf/spi_flash/CMakeFiles/__idf_spi_flash.dir/spi_flash_os_func_app.c.obj
|
||||
libspi_flash.a,spi_flash_os_func_noos.c.obj,esp-idf/spi_flash/CMakeFiles/__idf_spi_flash.dir/spi_flash_os_func_noos.c.obj
|
||||
|
@@ -0,0 +1,311 @@
|
||||
#!/usr/bin/env python3
|
||||
#
|
||||
# SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
|
||||
import logging
|
||||
import argparse
|
||||
import csv
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
import re
|
||||
from io import StringIO
|
||||
import configuration
|
||||
|
||||
sys.path.append(os.environ['IDF_PATH'] + '/tools/ldgen')
|
||||
sys.path.append(os.environ['IDF_PATH'] + '/tools/ldgen/ldgen')
|
||||
from entity import EntityDB
|
||||
|
||||
espidf_objdump = None
|
||||
|
||||
def lib_secs(lib, file, lib_path):
|
||||
new_env = os.environ.copy()
|
||||
new_env['LC_ALL'] = 'C'
|
||||
dump = StringIO(subprocess.check_output([espidf_objdump, '-h', lib_path], env=new_env).decode())
|
||||
dump.name = lib
|
||||
|
||||
sections_infos = EntityDB()
|
||||
sections_infos.add_sections_info(dump)
|
||||
|
||||
secs = sections_infos.get_sections(lib, file.split('.')[0] + '.c')
|
||||
if len(secs) == 0:
|
||||
secs = sections_infos.get_sections(lib, file.split('.')[0])
|
||||
if len(secs) == 0:
|
||||
raise ValueError('Failed to get sections from lib %s'%(lib_path))
|
||||
|
||||
return secs
|
||||
|
||||
def filter_secs(secs_a, secs_b):
|
||||
new_secs = list()
|
||||
for s_a in secs_a:
|
||||
for s_b in secs_b:
|
||||
if s_b in s_a:
|
||||
new_secs.append(s_a)
|
||||
return new_secs
|
||||
|
||||
def strip_secs(secs_a, secs_b):
|
||||
secs = list(set(secs_a) - set(secs_b))
|
||||
secs.sort()
|
||||
return secs
|
||||
|
||||
def func2sect(func):
|
||||
if ' ' in func:
|
||||
func_l = func.split(' ')
|
||||
else:
|
||||
func_l = list()
|
||||
func_l.append(func)
|
||||
|
||||
secs = list()
|
||||
for l in func_l:
|
||||
if '.iram1.' not in l:
|
||||
secs.append('.literal.%s'%(l,))
|
||||
secs.append('.text.%s'%(l, ))
|
||||
else:
|
||||
secs.append(l)
|
||||
return secs
|
||||
|
||||
class filter_c:
|
||||
def __init__(self, file):
|
||||
lines = open(file).read().splitlines()
|
||||
self.libs_desc = ''
|
||||
self.libs = ''
|
||||
for l in lines:
|
||||
if ') .iram1 EXCLUDE_FILE(*' in l and ') .iram1.*)' in l:
|
||||
desc = '\(EXCLUDE_FILE\((.*)\) .iram1 '
|
||||
self.libs_desc = re.search(desc, l)[1]
|
||||
self.libs = self.libs_desc.replace('*', '')
|
||||
return
|
||||
|
||||
def match(self, lib):
|
||||
if lib in self.libs:
|
||||
print('Remove lib %s'%(lib))
|
||||
return True
|
||||
return False
|
||||
|
||||
def add(self):
|
||||
return self.libs_desc
|
||||
|
||||
class target_c:
|
||||
def __init__(self, lib, lib_path, file, fsecs):
|
||||
self.lib = lib
|
||||
self.file = file
|
||||
|
||||
self.lib_path = lib_path
|
||||
self.fsecs = func2sect(fsecs)
|
||||
self.desc = '*%s:%s.*'%(lib, file.split('.')[0])
|
||||
|
||||
secs = lib_secs(lib, file, lib_path)
|
||||
if '.iram1.' in self.fsecs[0]:
|
||||
self.secs = filter_secs(secs, ('.iram1.', ))
|
||||
else:
|
||||
self.secs = filter_secs(secs, ('.iram1.', '.text.', '.literal.'))
|
||||
self.isecs = strip_secs(self.secs, self.fsecs)
|
||||
|
||||
def __str__(self):
|
||||
s = 'lib=%s\nfile=%s\lib_path=%s\ndesc=%s\nsecs=%s\nfsecs=%s\nisecs=%s\n'%(\
|
||||
self.lib, self.file, self.lib_path, self.desc, self.secs, self.fsecs,\
|
||||
self.isecs)
|
||||
return s
|
||||
|
||||
class relink_c:
|
||||
def __init__(self, input, library_file, object_file, function_file, sdkconfig_file, missing_function_info):
|
||||
self.filter = filter_c(input)
|
||||
|
||||
libraries = configuration.generator(library_file, object_file, function_file, sdkconfig_file, missing_function_info, espidf_objdump)
|
||||
self.targets = list()
|
||||
for i in libraries.libs:
|
||||
lib = libraries.libs[i]
|
||||
|
||||
if self.filter.match(lib.name):
|
||||
continue
|
||||
|
||||
for j in lib.objs:
|
||||
obj = lib.objs[j]
|
||||
self.targets.append(target_c(lib.name, lib.path, obj.name,
|
||||
' '.join(obj.sections())))
|
||||
# for i in self.targets:
|
||||
# print(i)
|
||||
self.__transform__()
|
||||
|
||||
def __transform__(self):
|
||||
iram1_exclude = list()
|
||||
iram1_include = list()
|
||||
flash_include = list()
|
||||
|
||||
for t in self.targets:
|
||||
secs = filter_secs(t.fsecs, ('.iram1.', ))
|
||||
if len(secs) > 0:
|
||||
iram1_exclude.append(t.desc)
|
||||
|
||||
secs = filter_secs(t.isecs, ('.iram1.', ))
|
||||
if len(secs) > 0:
|
||||
iram1_include.append(' %s(%s)'%(t.desc, ' '.join(secs)))
|
||||
|
||||
secs = t.fsecs
|
||||
if len(secs) > 0:
|
||||
flash_include.append(' %s(%s)'%(t.desc, ' '.join(secs)))
|
||||
|
||||
self.iram1_exclude = ' *(EXCLUDE_FILE(%s %s) .iram1.*) *(EXCLUDE_FILE(%s %s) .iram1)' % \
|
||||
(self.filter.add(), ' '.join(iram1_exclude), \
|
||||
self.filter.add(), ' '.join(iram1_exclude))
|
||||
self.iram1_include = '\n'.join(iram1_include)
|
||||
self.flash_include = '\n'.join(flash_include)
|
||||
|
||||
logging.debug('IRAM1 Exclude: %s'%(self.iram1_exclude))
|
||||
logging.debug('IRAM1 Include: %s'%(self.iram1_include))
|
||||
logging.debug('Flash Include: %s'%(self.flash_include))
|
||||
|
||||
def __replace__(self, lines):
|
||||
def is_iram_desc(l):
|
||||
if '*(.iram1 .iram1.*)' in l or (') .iram1 EXCLUDE_FILE(*' in l and ') .iram1.*)' in l):
|
||||
return True
|
||||
return False
|
||||
|
||||
iram_start = False
|
||||
flash_done = False
|
||||
|
||||
for i in range(0, len(lines) - 1):
|
||||
l = lines[i]
|
||||
if '.iram0.text :' in l:
|
||||
logging.debug('start to process .iram0.text')
|
||||
iram_start = True
|
||||
elif '.dram0.data :' in l:
|
||||
logging.debug('end to process .iram0.text')
|
||||
iram_start = False
|
||||
elif is_iram_desc(l):
|
||||
if iram_start:
|
||||
lines[i] = '%s\n%s\n'%(self.iram1_exclude, self.iram1_include)
|
||||
elif '(.stub .gnu.warning' in l:
|
||||
if not flash_done:
|
||||
lines[i] = '%s\n\n%s'%(self.flash_include, l)
|
||||
elif self.flash_include in l:
|
||||
flash_done = True
|
||||
else:
|
||||
if iram_start:
|
||||
new_l = self._replace_func(l)
|
||||
if new_l:
|
||||
lines[i] = new_l
|
||||
|
||||
return lines
|
||||
|
||||
def _replace_func(self, l):
|
||||
for t in self.targets:
|
||||
if t.desc in l:
|
||||
S = '.literal .literal.* .text .text.*'
|
||||
if S in l:
|
||||
if len(t.isecs) > 0:
|
||||
return l.replace(S, ' '.join(t.isecs))
|
||||
else:
|
||||
return ' '
|
||||
|
||||
S = '%s(%s)'%(t.desc, ' '.join(t.fsecs))
|
||||
if S in l:
|
||||
return ' '
|
||||
|
||||
replaced = False
|
||||
for s in t.fsecs:
|
||||
s2 = s + ' '
|
||||
if s2 in l:
|
||||
l = l.replace(s2, '')
|
||||
replaced = True
|
||||
s2 = s + ')'
|
||||
if s2 in l:
|
||||
l = l.replace(s2, ')')
|
||||
replaced = True
|
||||
if '( )' in l or '()' in l:
|
||||
return ' '
|
||||
if replaced:
|
||||
return l
|
||||
else:
|
||||
index = '*%s:(EXCLUDE_FILE'%(t.lib)
|
||||
if index in l and t.file.split('.')[0] not in l:
|
||||
for m in self.targets:
|
||||
index = '*%s:(EXCLUDE_FILE'%(m.lib)
|
||||
if index in l and m.file.split('.')[0] not in l:
|
||||
l = l.replace('EXCLUDE_FILE(', 'EXCLUDE_FILE(%s '%(m.desc))
|
||||
if len(m.isecs) > 0:
|
||||
l += '\n %s(%s)'%(m.desc, ' '.join(m.isecs))
|
||||
return l
|
||||
|
||||
return False
|
||||
|
||||
def save(self, input, output):
|
||||
lines = open(input).read().splitlines()
|
||||
lines = self.__replace__(lines)
|
||||
open(output, 'w+').write('\n'.join(lines))
|
||||
|
||||
def main():
|
||||
argparser = argparse.ArgumentParser(description='Relinker script generator')
|
||||
|
||||
argparser.add_argument(
|
||||
'--input', '-i',
|
||||
help='Linker template file',
|
||||
type=str)
|
||||
|
||||
argparser.add_argument(
|
||||
'--output', '-o',
|
||||
help='Output linker script',
|
||||
type=str)
|
||||
|
||||
argparser.add_argument(
|
||||
'--library', '-l',
|
||||
help='Library description directory',
|
||||
type=str)
|
||||
|
||||
argparser.add_argument(
|
||||
'--object', '-b',
|
||||
help='Object description file',
|
||||
type=str)
|
||||
|
||||
argparser.add_argument(
|
||||
'--function', '-f',
|
||||
help='Function description file',
|
||||
type=str)
|
||||
|
||||
argparser.add_argument(
|
||||
'--sdkconfig', '-s',
|
||||
help='sdkconfig file',
|
||||
type=str)
|
||||
|
||||
argparser.add_argument(
|
||||
'--objdump', '-g',
|
||||
help='GCC objdump command',
|
||||
type=str)
|
||||
|
||||
argparser.add_argument(
|
||||
'--debug', '-d',
|
||||
help='Debug level(option is \'debug\')',
|
||||
default='no',
|
||||
type=str)
|
||||
|
||||
argparser.add_argument(
|
||||
'--missing_function_info',
|
||||
help='Print error information instead of throwing exception when missing function',
|
||||
default=False,
|
||||
type=bool)
|
||||
|
||||
args = argparser.parse_args()
|
||||
|
||||
if args.debug == 'debug':
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
|
||||
logging.debug('input: %s'%(args.input))
|
||||
logging.debug('output: %s'%(args.output))
|
||||
logging.debug('library: %s'%(args.library))
|
||||
logging.debug('object: %s'%(args.object))
|
||||
logging.debug('function: %s'%(args.function))
|
||||
logging.debug('sdkconfig:%s'%(args.sdkconfig))
|
||||
logging.debug('objdump: %s'%(args.objdump))
|
||||
logging.debug('debug: %s'%(args.debug))
|
||||
logging.debug('missing_function_info: %s'%(args.missing_function_info))
|
||||
|
||||
global espidf_objdump
|
||||
espidf_objdump = args.objdump
|
||||
|
||||
relink = relink_c(args.input, args.library, args.object, args.function, args.sdkconfig, args.missing_function_info)
|
||||
relink.save(args.input, args.output)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,8 @@
|
||||
# The following lines of boilerplate have to be in your project's CMakeLists
|
||||
# in this exact order for cmake to work correctly
|
||||
cmake_minimum_required(VERSION 3.5)
|
||||
|
||||
set(EXTRA_COMPONENT_DIRS "$ENV{IDF_PATH}/tools/unit-test-app/components"
|
||||
"../../cmake_utilities")
|
||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||
project(cmake_utilities_test_apps)
|
||||
@@ -0,0 +1,10 @@
|
||||
idf_component_register( SRC_DIRS "."
|
||||
INCLUDE_DIRS "."
|
||||
REQUIRES cmake_utilities)
|
||||
|
||||
include(gcc)
|
||||
include(gen_compressed_ota)
|
||||
include(gen_single_bin)
|
||||
include(package_manager)
|
||||
include(relinker)
|
||||
cu_pkg_define_version(${CMAKE_CURRENT_LIST_DIR})
|
||||
@@ -0,0 +1,6 @@
|
||||
version: "3.2.1"
|
||||
description: Test2 for cmake utilities
|
||||
url: https://github.com/espressif/esp-iot-solution/tree/master/tools/cmake_utilities
|
||||
issues: https://github.com/espressif/esp-iot-solution/issues
|
||||
dependencies:
|
||||
idf: ">=4.1"
|
||||
@@ -0,0 +1,16 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int test_component2_version_major()
|
||||
{
|
||||
return TEST_COMPONENT2_VER_MAJOR;
|
||||
}
|
||||
|
||||
int test_component2_version_minor()
|
||||
{
|
||||
return TEST_COMPONENT2_VER_MINOR;
|
||||
}
|
||||
|
||||
int test_component2_version_patch()
|
||||
{
|
||||
return TEST_COMPONENT2_VER_PATCH;
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
#include <stdio.h>
|
||||
#pragma once
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
int test_component2_version_major();
|
||||
int test_component2_version_minor();
|
||||
int test_component2_version_patch();
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,10 @@
|
||||
idf_component_register( SRC_DIRS "."
|
||||
INCLUDE_DIRS "."
|
||||
REQUIRES cmake_utilities)
|
||||
|
||||
include(gcc)
|
||||
include(gen_compressed_ota)
|
||||
include(gen_single_bin)
|
||||
include(package_manager)
|
||||
include(relinker)
|
||||
cu_pkg_define_version(${CMAKE_CURRENT_LIST_DIR})
|
||||
@@ -0,0 +1,6 @@
|
||||
version: "1.2.3"
|
||||
description: Test1 for cmake utilities
|
||||
url: https://github.com/espressif/esp-iot-solution/tree/master/tools/cmake_utilities
|
||||
issues: https://github.com/espressif/esp-iot-solution/issues
|
||||
dependencies:
|
||||
idf: ">=4.1"
|
||||
@@ -0,0 +1,16 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int test_component1_version_major()
|
||||
{
|
||||
return TEST_COMPONENT1_VER_MAJOR;
|
||||
}
|
||||
|
||||
int test_component1_version_minor()
|
||||
{
|
||||
return TEST_COMPONENT1_VER_MINOR;
|
||||
}
|
||||
|
||||
int test_component1_version_patch()
|
||||
{
|
||||
return TEST_COMPONENT1_VER_PATCH;
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
#include <stdio.h>
|
||||
#pragma once
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
int test_component1_version_major();
|
||||
int test_component1_version_minor();
|
||||
int test_component1_version_patch();
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,3 @@
|
||||
idf_component_register(SRC_DIRS "."
|
||||
INCLUDE_DIRS "."
|
||||
REQUIRES unity test_utils test_component1 TEST-component2)
|
||||
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <inttypes.h>
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "esp_system.h"
|
||||
#include "esp_log.h"
|
||||
#include "unity.h"
|
||||
#include "test_component1.h"
|
||||
#include "test_component2.h"
|
||||
|
||||
|
||||
/* USB PIN fixed in esp32-s2, can not use io matrix */
|
||||
#define TEST_MEMORY_LEAK_THRESHOLD (-400)
|
||||
|
||||
TEST_CASE("Test package manager version", "[cmake_utilities][package_manager]")
|
||||
{
|
||||
esp_log_level_set("*", ESP_LOG_INFO);
|
||||
TEST_ASSERT_EQUAL_INT(test_component1_version_major(), 1);
|
||||
TEST_ASSERT_EQUAL_INT(test_component1_version_minor(), 2);
|
||||
TEST_ASSERT_EQUAL_INT(test_component1_version_patch(), 3);
|
||||
TEST_ASSERT_EQUAL_INT(test_component2_version_major(), 3);
|
||||
TEST_ASSERT_EQUAL_INT(test_component2_version_minor(), 2);
|
||||
TEST_ASSERT_EQUAL_INT(test_component2_version_patch(), 1);
|
||||
}
|
||||
|
||||
static size_t before_free_8bit;
|
||||
static size_t before_free_32bit;
|
||||
|
||||
static void check_leak(size_t before_free, size_t after_free, const char *type)
|
||||
{
|
||||
ssize_t delta = after_free - before_free;
|
||||
printf("MALLOC_CAP_%s: Before %u bytes free, After %u bytes free (delta %d)\n", type, before_free, after_free, delta);
|
||||
TEST_ASSERT_MESSAGE(delta >= TEST_MEMORY_LEAK_THRESHOLD, "memory leak");
|
||||
}
|
||||
|
||||
void setUp(void)
|
||||
{
|
||||
before_free_8bit = heap_caps_get_free_size(MALLOC_CAP_8BIT);
|
||||
before_free_32bit = heap_caps_get_free_size(MALLOC_CAP_32BIT);
|
||||
}
|
||||
|
||||
void tearDown(void)
|
||||
{
|
||||
size_t after_free_8bit = heap_caps_get_free_size(MALLOC_CAP_8BIT);
|
||||
size_t after_free_32bit = heap_caps_get_free_size(MALLOC_CAP_32BIT);
|
||||
check_leak(before_free_8bit, after_free_8bit, "8BIT");
|
||||
check_leak(before_free_32bit, after_free_32bit, "32BIT");
|
||||
}
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
printf("Cmake Utilities TEST \n");
|
||||
unity_run_menu();
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
'''
|
||||
Steps to run these cases:
|
||||
- Build
|
||||
- . ${IDF_PATH}/export.sh
|
||||
- pip install idf_build_apps
|
||||
- python tools/build_apps.py tools/cmake_utilities/test_apps -t esp32s2
|
||||
- Test
|
||||
- pip install -r tools/requirements/requirement.pytest.txt
|
||||
- pytest tools/cmake_utilities/test_apps --target esp32s2
|
||||
'''
|
||||
|
||||
import pytest
|
||||
from pytest_embedded import Dut
|
||||
|
||||
@pytest.mark.target('esp32s3')
|
||||
@pytest.mark.env('generic')
|
||||
def test_cmake_utilities(dut: Dut)-> None:
|
||||
dut.expect_exact('Press ENTER to see the list of tests.')
|
||||
dut.write('*')
|
||||
dut.expect_unity_test_output(timeout = 1000)
|
||||
1
managed_components/espressif__esp-modbus/.component_hash
Normal file
1
managed_components/espressif__esp-modbus/.component_hash
Normal file
@@ -0,0 +1 @@
|
||||
2168e6b4cbda4d0281a2a2d1a40a3848e231473b2690d73217e3600fd2c98c12
|
||||
49
managed_components/espressif__esp-modbus/.gitignore
vendored
Normal file
49
managed_components/espressif__esp-modbus/.gitignore
vendored
Normal file
@@ -0,0 +1,49 @@
|
||||
.config
|
||||
*.o
|
||||
*.pyc
|
||||
|
||||
# gtags
|
||||
GTAGS
|
||||
GRTAGS
|
||||
GPATH
|
||||
|
||||
# emacs
|
||||
.dir-locals.el
|
||||
|
||||
# emacs temp file suffixes
|
||||
*~
|
||||
.#*
|
||||
\#*#
|
||||
|
||||
# eclipse setting
|
||||
.settings
|
||||
|
||||
# MacOS directory files
|
||||
.DS_Store
|
||||
|
||||
# Test files
|
||||
test/build
|
||||
test/sdkconfig
|
||||
test/sdkconfig.old
|
||||
|
||||
# Doc build artifacts
|
||||
docs/_build/
|
||||
docs/doxygen-warning-log.txt
|
||||
docs/sphinx-warning-log.txt
|
||||
docs/sphinx-warning-log-sanitized.txt
|
||||
docs/xml/
|
||||
docs/xml_in/
|
||||
docs/man/
|
||||
docs/doxygen_sqlite3.db
|
||||
|
||||
TEST_LOGS
|
||||
|
||||
|
||||
# gcov coverage reports
|
||||
*.gcda
|
||||
*.gcno
|
||||
coverage.info
|
||||
coverage_report/
|
||||
|
||||
# VS Code Settings
|
||||
.vscode/
|
||||
78
managed_components/espressif__esp-modbus/CMakeLists.txt
Normal file
78
managed_components/espressif__esp-modbus/CMakeLists.txt
Normal file
@@ -0,0 +1,78 @@
|
||||
# The following five lines of boilerplate have to be in your project's
|
||||
# CMakeLists in this exact order for cmake to work correctly
|
||||
set(srcs
|
||||
"common/esp_modbus_master.c"
|
||||
"common/esp_modbus_slave.c"
|
||||
"modbus/mb.c"
|
||||
"modbus/mb_m.c"
|
||||
"modbus/ascii/mbascii.c"
|
||||
"modbus/ascii/mbascii_m.c"
|
||||
"modbus/rtu/mbrtu_m.c"
|
||||
"modbus/rtu/mbrtu.c"
|
||||
"modbus/rtu/mbcrc.c"
|
||||
"modbus/tcp/mbtcp.c"
|
||||
"modbus/tcp/mbtcp_m.c"
|
||||
"port/port.c"
|
||||
"port/portevent.c"
|
||||
"port/portevent_m.c"
|
||||
"port/portother.c"
|
||||
"port/portother_m.c"
|
||||
"port/portserial.c"
|
||||
"port/portserial_m.c"
|
||||
"port/porttimer.c"
|
||||
"port/porttimer_m.c"
|
||||
"modbus/functions/mbfunccoils.c"
|
||||
"modbus/functions/mbfunccoils_m.c"
|
||||
"modbus/functions/mbfuncdiag.c"
|
||||
"modbus/functions/mbfuncdisc.c"
|
||||
"modbus/functions/mbfuncdisc_m.c"
|
||||
"modbus/functions/mbfuncholding.c"
|
||||
"modbus/functions/mbfuncholding_m.c"
|
||||
"modbus/functions/mbfuncinput.c"
|
||||
"modbus/functions/mbfuncinput_m.c"
|
||||
"modbus/functions/mbfuncother.c"
|
||||
"modbus/functions/mbutils.c"
|
||||
"serial_slave/modbus_controller/mbc_serial_slave.c"
|
||||
"serial_master/modbus_controller/mbc_serial_master.c"
|
||||
"tcp_slave/port/port_tcp_slave.c"
|
||||
"tcp_slave/modbus_controller/mbc_tcp_slave.c"
|
||||
"tcp_master/modbus_controller/mbc_tcp_master.c"
|
||||
"tcp_master/port/port_tcp_master.c"
|
||||
"common/esp_modbus_master_tcp.c"
|
||||
"common/esp_modbus_slave_tcp.c"
|
||||
"common/esp_modbus_master_serial.c"
|
||||
"common/esp_modbus_slave_serial.c")
|
||||
|
||||
set(include_dirs common/include)
|
||||
|
||||
set(priv_include_dirs common port modbus modbus/ascii modbus/functions
|
||||
modbus/rtu modbus/tcp modbus/include)
|
||||
|
||||
list(APPEND priv_include_dirs serial_slave/port serial_slave/modbus_controller
|
||||
serial_master/port serial_master/modbus_controller
|
||||
tcp_slave/port tcp_slave/modbus_controller
|
||||
tcp_master/port tcp_master/modbus_controller)
|
||||
|
||||
if(CONFIG_FMB_EXT_TYPE_SUPPORT)
|
||||
list(APPEND srcs "common/mb_endianness_utils.c")
|
||||
endif()
|
||||
|
||||
add_prefix(srcs "${CMAKE_CURRENT_LIST_DIR}/freemodbus/" ${srcs})
|
||||
add_prefix(include_dirs "${CMAKE_CURRENT_LIST_DIR}/freemodbus/" ${include_dirs})
|
||||
add_prefix(priv_include_dirs "${CMAKE_CURRENT_LIST_DIR}/freemodbus/" ${priv_include_dirs})
|
||||
|
||||
message(STATUS "DEBUG: Use esp-modbus component folder: ${CMAKE_CURRENT_LIST_DIR}.")
|
||||
|
||||
set(requires driver lwip)
|
||||
|
||||
# esp_timer component was introduced in v4.2
|
||||
if("${IDF_VERSION_MAJOR}.${IDF_VERSION_MINOR}" VERSION_GREATER "4.1")
|
||||
list(APPEND requires esp_timer)
|
||||
endif()
|
||||
|
||||
idf_component_register(SRCS "${srcs}"
|
||||
INCLUDE_DIRS "${include_dirs}"
|
||||
PRIV_INCLUDE_DIRS "${priv_include_dirs}"
|
||||
REQUIRES ${requires}
|
||||
PRIV_REQUIRES esp_netif)
|
||||
|
||||
233
managed_components/espressif__esp-modbus/Kconfig
Normal file
233
managed_components/espressif__esp-modbus/Kconfig
Normal file
@@ -0,0 +1,233 @@
|
||||
menu "Modbus configuration"
|
||||
|
||||
config FMB_COMM_MODE_TCP_EN
|
||||
bool "Enable Modbus stack support for TCP communication mode"
|
||||
default y
|
||||
help
|
||||
Enable Modbus TCP option for stack.
|
||||
|
||||
config FMB_TCP_PORT_DEFAULT
|
||||
int "Modbus TCP port number"
|
||||
range 0 65535
|
||||
default 502
|
||||
depends on FMB_COMM_MODE_TCP_EN
|
||||
help
|
||||
Modbus default port number used by Modbus TCP stack
|
||||
|
||||
config FMB_TCP_PORT_MAX_CONN
|
||||
int "Maximum allowed connections for TCP stack"
|
||||
range 1 8
|
||||
default 5
|
||||
depends on FMB_COMM_MODE_TCP_EN
|
||||
help
|
||||
Maximum allowed connections number for Modbus TCP stack.
|
||||
This is used by Modbus master and slave port layer to establish connections.
|
||||
This parameter may decrease performance of Modbus stack and can cause
|
||||
increasing of processing time (increase only if absolutely necessary).
|
||||
|
||||
config FMB_TCP_CONNECTION_TOUT_SEC
|
||||
int "Modbus TCP connection timeout"
|
||||
range 1 3600
|
||||
default 20
|
||||
depends on FMB_COMM_MODE_TCP_EN
|
||||
help
|
||||
Modbus TCP connection timeout in seconds.
|
||||
Once expired the current connection with the client will be closed
|
||||
and Modbus slave will be waiting for new connection to accept.
|
||||
|
||||
config FMB_TCP_UID_ENABLED
|
||||
bool "Modbus TCP enable UID (Unit Identifier) support"
|
||||
default n
|
||||
depends on FMB_COMM_MODE_TCP_EN
|
||||
help
|
||||
If this option is set the Modbus stack uses UID (Unit Identifier) field in MBAP frame.
|
||||
Else the UID is ignored by master and slave.
|
||||
|
||||
config FMB_COMM_MODE_RTU_EN
|
||||
bool "Enable Modbus stack support for RTU mode"
|
||||
default y
|
||||
help
|
||||
Enable RTU Modbus communication mode option for Modbus serial stack.
|
||||
|
||||
config FMB_COMM_MODE_ASCII_EN
|
||||
bool "Enable Modbus stack support for ASCII mode"
|
||||
default y
|
||||
help
|
||||
Enable ASCII Modbus communication mode option for Modbus serial stack.
|
||||
|
||||
config FMB_MASTER_TIMEOUT_MS_RESPOND
|
||||
int "Slave respond timeout (Milliseconds)"
|
||||
default 3000
|
||||
range 150 15000
|
||||
help
|
||||
If master sends a frame which is not broadcast, it has to wait sometime for slave response.
|
||||
if slave is not respond in this time, the master will process timeout error.
|
||||
|
||||
config FMB_MASTER_DELAY_MS_CONVERT
|
||||
int "Slave conversion delay (Milliseconds)"
|
||||
default 200
|
||||
range 150 2000
|
||||
help
|
||||
If master sends a broadcast frame, it has to wait conversion time to delay,
|
||||
then master can send next frame.
|
||||
|
||||
config FMB_QUEUE_LENGTH
|
||||
int "Modbus serial task queue length"
|
||||
range 0 200
|
||||
default 20
|
||||
help
|
||||
Modbus serial driver queue length. It is used by event queue task.
|
||||
See the serial driver API for more information.
|
||||
|
||||
config FMB_PORT_TASK_STACK_SIZE
|
||||
int "Modbus port task stack size"
|
||||
range 2048 8192
|
||||
default 4096
|
||||
help
|
||||
Modbus port task stack size for rx/tx event processing.
|
||||
It may be adjusted when debugging is enabled (for example).
|
||||
|
||||
config FMB_SERIAL_BUF_SIZE
|
||||
int "Modbus serial task RX/TX buffer size"
|
||||
range 0 2048
|
||||
default 256
|
||||
help
|
||||
Modbus serial task RX and TX buffer size for UART driver initialization.
|
||||
This buffer is used for modbus frame transfer. The Modbus protocol maximum
|
||||
frame size is 256 bytes. Bigger size can be used for non standard implementations.
|
||||
|
||||
config FMB_SERIAL_ASCII_BITS_PER_SYMB
|
||||
int "Number of data bits per ASCII character"
|
||||
default 8
|
||||
range 7 8
|
||||
depends on FMB_COMM_MODE_ASCII_EN
|
||||
help
|
||||
This option defines the number of data bits per ASCII character.
|
||||
|
||||
config FMB_ASCII_TIMEOUT_WAIT_BEFORE_SEND_MS
|
||||
int "Wait before send for ASCII communication mode (ms)"
|
||||
default 0
|
||||
range 0 1000
|
||||
depends on FMB_COMM_MODE_ASCII_EN
|
||||
help
|
||||
This option defines timeout before slave sends the response in ASCII communication mode.
|
||||
This allows to work with slow masters. Zero means delay before send is disabled.
|
||||
|
||||
config FMB_SERIAL_ASCII_TIMEOUT_RESPOND_MS
|
||||
int "Response timeout for ASCII communication mode (ms)"
|
||||
default 1000
|
||||
range 200 5000
|
||||
depends on FMB_COMM_MODE_ASCII_EN
|
||||
help
|
||||
This option defines response timeout of slave in milliseconds for ASCII communication mode.
|
||||
Thus the timeout will expire and allow the master program to handle the error.
|
||||
|
||||
config FMB_PORT_TASK_PRIO
|
||||
int "Modbus port task priority"
|
||||
range 3 23
|
||||
default 10
|
||||
help
|
||||
Modbus port data processing task priority.
|
||||
The priority of Modbus controller task is equal to (CONFIG_FMB_PORT_TASK_PRIO - 1).
|
||||
|
||||
choice FMB_PORT_TASK_AFFINITY
|
||||
prompt "Modbus task affinity"
|
||||
default FMB_PORT_TASK_AFFINITY_CPU0
|
||||
depends on !FREERTOS_UNICORE
|
||||
help
|
||||
Allows setting the core affinity of the Modbus controller task, i.e. whether the task is pinned to
|
||||
particular CPU, or allowed to run on any CPU.
|
||||
|
||||
config FMB_PORT_TASK_AFFINITY_NO_AFFINITY
|
||||
bool "No affinity"
|
||||
config FMB_PORT_TASK_AFFINITY_CPU0
|
||||
bool "CPU0"
|
||||
config FMB_PORT_TASK_AFFINITY_CPU1
|
||||
bool "CPU1"
|
||||
|
||||
endchoice
|
||||
|
||||
config FMB_PORT_TASK_AFFINITY
|
||||
hex
|
||||
default FREERTOS_NO_AFFINITY if FMB_PORT_TASK_AFFINITY_NO_AFFINITY || FREERTOS_UNICORE
|
||||
default 0x0 if FMB_PORT_TASK_AFFINITY_CPU0
|
||||
default 0x1 if FMB_PORT_TASK_AFFINITY_CPU1
|
||||
|
||||
config FMB_CONTROLLER_SLAVE_ID_SUPPORT
|
||||
bool "Modbus controller slave ID support"
|
||||
default y
|
||||
help
|
||||
Modbus slave ID support enable.
|
||||
When enabled the Modbus <Report Slave ID> command is supported by stack.
|
||||
|
||||
config FMB_CONTROLLER_SLAVE_ID
|
||||
hex "Modbus controller slave ID"
|
||||
range 0 4294967295
|
||||
default 0x00112233
|
||||
depends on FMB_CONTROLLER_SLAVE_ID_SUPPORT
|
||||
help
|
||||
Modbus slave ID value to identify modbus device
|
||||
in the network using <Report Slave ID> command.
|
||||
Most significant byte of ID is used as short device ID and
|
||||
other three bytes used as long ID.
|
||||
|
||||
config FMB_CONTROLLER_NOTIFY_TIMEOUT
|
||||
int "Modbus controller notification timeout (ms)"
|
||||
range 0 200
|
||||
default 20
|
||||
help
|
||||
Modbus controller notification timeout in milliseconds.
|
||||
This timeout is used to send notification about accessed parameters.
|
||||
|
||||
config FMB_CONTROLLER_NOTIFY_QUEUE_SIZE
|
||||
int "Modbus controller notification queue size"
|
||||
range 0 200
|
||||
default 20
|
||||
help
|
||||
Modbus controller notification queue size.
|
||||
The notification queue is used to get information about accessed parameters.
|
||||
|
||||
config FMB_CONTROLLER_STACK_SIZE
|
||||
int "Modbus controller stack size"
|
||||
range 0 8192
|
||||
default 4096
|
||||
help
|
||||
Modbus controller task stack size. The Stack size may be adjusted when
|
||||
debug mode is used which requires more stack size (for example).
|
||||
|
||||
config FMB_EVENT_QUEUE_TIMEOUT
|
||||
int "Modbus stack event queue timeout (ms)"
|
||||
range 0 500
|
||||
default 20
|
||||
help
|
||||
Modbus stack event queue timeout in milliseconds. This may help to optimize
|
||||
Modbus stack event processing time.
|
||||
|
||||
config FMB_TIMER_PORT_ENABLED
|
||||
bool "Modbus stack use timer for 3.5T symbol time measurement"
|
||||
default n
|
||||
help
|
||||
If this option is set the Modbus stack uses timer for T3.5 time measurement.
|
||||
Else the internal UART TOUT timeout is used for 3.5T symbol time measurement.
|
||||
|
||||
config FMB_TIMER_USE_ISR_DISPATCH_METHOD
|
||||
bool "Modbus timer uses ISR dispatch method"
|
||||
default n
|
||||
select ESP_TIMER_SUPPORTS_ISR_DISPATCH_METHOD
|
||||
select UART_ISR_IN_IRAM
|
||||
help
|
||||
If this option is set the Modbus stack uses ISR dispatch method
|
||||
to send timeout events from the callback function called from ISR.
|
||||
This option has dependency with the UART_ISR_IN_IRAM option which places UART interrupt
|
||||
handler into IRAM to prevent delays related to processing of UART events.
|
||||
|
||||
config FMB_EXT_TYPE_SUPPORT
|
||||
bool "Modbus uses extended types to support third party devices"
|
||||
default n
|
||||
help
|
||||
If this option is set the Modbus stack supports extended list of types
|
||||
in data dictionary and conversion API to work with the extended types
|
||||
otherwise the only legacy types are supported. The extended types include
|
||||
integer, float, double types with different endianness and size.
|
||||
|
||||
endmenu
|
||||
202
managed_components/espressif__esp-modbus/LICENSE
Normal file
202
managed_components/espressif__esp-modbus/LICENSE
Normal file
@@ -0,0 +1,202 @@
|
||||
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
65
managed_components/espressif__esp-modbus/README.md
Normal file
65
managed_components/espressif__esp-modbus/README.md
Normal file
@@ -0,0 +1,65 @@
|
||||
# ESP-Modbus Library
|
||||
|
||||
## Overview
|
||||
|
||||
An Espressif ESP-Modbus Library (esp-modbus) is a library to support Modbus communication in the networks based on RS485, WiFi, Ethernet interfaces. The Modbus is a data communications protocol originally published by Modicon (now Schneider Electric) in 1979 for use with its programmable logic controllers (PLCs).
|
||||
|
||||
* [ESP-Modbus component on GitHub](https://www.github.com/espressif/esp-modbus)
|
||||
|
||||
This library is to be used with Espressif’s IoT Development Framework, [ESP_IDF](https://github.com/espressif/esp-idf). The packages from this repository are uploaded to Espressif’s component repository.
|
||||
|
||||
* [esp-modbus component in component repository](https://components.espressif.com/component/espressif/esp-modbus)
|
||||
|
||||
You can add the component to your project via `idf.py add-dependency`. More information about idf-component-manager can be found in [Espressif API guide](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/tools/idf-component-manager.html) or [PyPi registry](https://pypi.org/project/idf-component-manager).
|
||||
|
||||
The ESP-Modbus library can be used with ESP-IDF v4.1 and later. ESP-IDF v4.x releases include an earlier version of ESP-Modbus library inside freemodbus component. To use ESP-Modbus with these releases, users need to exclude the built-in freemodbus component from the build process, and update application components to depend on esp-modbus component instead. To exclude freemodbus component from compilation, add the following line to the project CMakeLists.txt file:
|
||||
|
||||
```
|
||||
set(EXCLUDE_COMPONENTS freemodbus)
|
||||
```
|
||||
|
||||
ESP-IDF v5.x and later releases do not include freemodbus component, so no extra steps are necessary when adding esp-modbus component.
|
||||
|
||||
## Documentation
|
||||
|
||||
The documentation can be found on the link below:
|
||||
|
||||
* [ESP-Modbus documentation (English)](https://docs.espressif.com/projects/esp-modbus)
|
||||
|
||||
## Application Examples
|
||||
|
||||
The examples below demonstrate the ESP-Modbus library of serial, TCP ports for slave and master implementations accordingly.
|
||||
|
||||
- [Modbus serial slave example](https://github.com/espressif/esp-idf/tree/master/examples/protocols/modbus/serial/mb_slave)
|
||||
|
||||
- [Modbus serial master example](https://github.com/espressif/esp-idf/tree/master/examples/protocols/modbus/serial/mb_master)
|
||||
|
||||
- [Modbus TCP master example](https://github.com/espressif/esp-idf/tree/master/examples/protocols/modbus/tcp/mb_tcp_master)
|
||||
|
||||
- [Modbus TCP slave example](https://github.com/espressif/esp-idf/tree/master/examples/protocols/modbus/tcp/mb_tcp_slave)
|
||||
|
||||
Please refer to the specific example README.md for details.
|
||||
|
||||
## Protocol References
|
||||
|
||||
- [Modbus Organization with protocol specifications](https://modbus.org/specs.php)
|
||||
|
||||
## Contributing
|
||||
|
||||
We welcome contributions to this project in the form of bug reports, feature requests and pull requests.
|
||||
|
||||
Issue reports and feature requests can be submitted using Github Issues: https://github.com/espressif/esp-modbus/issues. Please check if the issue has already been reported before opening a new one.
|
||||
|
||||
Contributions in the form of pull requests should follow ESP-IDF project's [contribution guidelines](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/contribute/index.html). We kindly ask developers to start a discussion on an issue before proposing large changes to the project.
|
||||
|
||||
See the beta version of stack v2.0.0_beta introduced [here](https://github.com/espressif/esp-modbus/discussions/45)
|
||||
|
||||
## Licence
|
||||
|
||||
ESP-Modbus project is based on [FreeMODBUS library](https://github.com/cwalter-at/freemodbus), Copyright (c) 2006 Christian Walter and licensed under the BSD 3-clause license.
|
||||
|
||||
Modbus Master related code is Copyright (c) 2013 Armink and licensed under BSD 3-clause license.
|
||||
|
||||
All original code in this repository is Copyright (c) 2016-2022 Espressif Systems (Shanghai) Co. Ltd.
|
||||
|
||||
The project is distributed under Apache 2.0 license. See the accompanying [LICENSE file](https://github.com/espressif/esp-modbus/blob/master/LICENSE) for a copy.
|
||||
97
managed_components/espressif__esp-modbus/build_all.sh
Normal file
97
managed_components/espressif__esp-modbus/build_all.sh
Normal file
@@ -0,0 +1,97 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# Build the test app and all examples from the examples directory.
|
||||
# Expects TEST_TARGETS environment variables to be set.
|
||||
# Each variable is the list of IDF_TARGET values to build the examples and
|
||||
# the test app for, respectively.
|
||||
#
|
||||
# -----------------------------------------------------------------------------
|
||||
# Safety settings (see https://gist.github.com/ilg-ul/383869cbb01f61a51c4d).
|
||||
|
||||
if [[ -n "${DEBUG_SHELL}" ]]
|
||||
then
|
||||
set -x # Activate the expand mode if DEBUG is anything but empty.
|
||||
fi
|
||||
|
||||
if [[ -z "${EXAMPLE_TARGETS}" || -z "${TEST_TARGETS}" ]]
|
||||
then
|
||||
echo "EXAMPLE_TARGETS and TEST_TARGETS environment variables must be set before calling this script"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [[ -z "${SKIP_GNU_MAKE_BUILD}" ]]
|
||||
then
|
||||
echo "SKIP_GNU_MAKE_BUILD not set, will build with GNU Make based build system as well."
|
||||
export SKIP_GNU_MAKE_BUILD=0
|
||||
fi
|
||||
|
||||
set -o errexit # Exit if command failed.
|
||||
set -o pipefail # Exit if pipe failed.
|
||||
set -o nounset # Exit if variable not set.
|
||||
|
||||
|
||||
STARS='***************************************************'
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
|
||||
die() {
|
||||
echo "${1:-"Unknown Error"}" 1>&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
# build_for_targets <target list>
|
||||
# call this in the project directory
|
||||
function build_for_targets
|
||||
{
|
||||
target_list="$1"
|
||||
for IDF_TARGET in ${target_list}
|
||||
do
|
||||
export IDF_TARGET
|
||||
if [[ "${IDF_TARGET}" = "esp32" ]] && [[ "${SKIP_GNU_MAKE_BUILD}" = "0" ]]
|
||||
then
|
||||
echo "${STARS}"
|
||||
echo "Building in $PWD with Make"
|
||||
# -j option will be set via MAKEFLAGS in .gitlab-ci.yml
|
||||
# shellcheck disable=SC2015
|
||||
make defconfig && make || die "Make build in ${PWD} has failed"
|
||||
rm -rf build
|
||||
fi
|
||||
|
||||
echo "${STARS}"
|
||||
echo "Building in $PWD with CMake for ${IDF_TARGET}"
|
||||
preview_target=
|
||||
if [[ ${IDF_TARGET} == "esp32c6" ]]
|
||||
then
|
||||
preview_target="--preview"
|
||||
fi
|
||||
if [[ ${IDF_TARGET} != "esp32" ]]
|
||||
then
|
||||
# IDF 4.0 doesn't support idf.py set-target, and only supports esp32.
|
||||
idf.py ${preview_target} set-target "${IDF_TARGET}"
|
||||
fi
|
||||
idf.py build || die "CMake build in ${PWD} has failed for ${IDF_TARGET}"
|
||||
idf.py fullclean
|
||||
done
|
||||
}
|
||||
|
||||
function build_folders
|
||||
{
|
||||
pushd "$1"
|
||||
EXAMPLES=$(find . -maxdepth 1 -mindepth 1 -type d | cut -d '/' -f 2)
|
||||
for NAME in ${EXAMPLES}
|
||||
do
|
||||
cd "${NAME}"
|
||||
build_for_targets "$2"
|
||||
cd ..
|
||||
done
|
||||
popd
|
||||
}
|
||||
|
||||
echo "${STARS}"
|
||||
# Build the tests
|
||||
build_folders test/serial "${TEST_TARGETS}"
|
||||
echo "${STARS}"
|
||||
# Build the tests
|
||||
build_folders test/tcp "${TEST_TARGETS}"
|
||||
echo "${STARS}"
|
||||
|
||||
29
managed_components/espressif__esp-modbus/component.mk
Normal file
29
managed_components/espressif__esp-modbus/component.mk
Normal file
@@ -0,0 +1,29 @@
|
||||
INCLUDEDIRS := common/include
|
||||
PRIV_INCLUDEDIRS := common port modbus modbus/ascii modbus/functions
|
||||
PRIV_INCLUDEDIRS += modbus/rtu modbus/tcp modbus/include
|
||||
PRIV_INCLUDEDIRS += serial_slave/port serial_slave/modbus_controller
|
||||
PRIV_INCLUDEDIRS += serial_master/port serial_master/modbus_controller
|
||||
PRIV_INCLUDEDIRS += tcp_slave/port tcp_slave/modbus_controller
|
||||
PRIV_INCLUDEDIRS += tcp_master/port tcp_master/modbus_controller
|
||||
SRCDIRS := common
|
||||
SRCDIRS += modbus modbus/ascii modbus/functions modbus/rtu modbus/tcp
|
||||
SRCDIRS += serial_slave/port serial_slave/modbus_controller
|
||||
SRCDIRS += serial_master/port serial_master/modbus_controller
|
||||
SRCDIRS += tcp_slave/port tcp_slave/modbus_controller
|
||||
SRCDIRS += tcp_master/port tcp_master/modbus_controller
|
||||
SRCDIRS += port
|
||||
|
||||
COMPONENT_PRIV_INCLUDEDIRS = $(addprefix freemodbus/, \
|
||||
$(PRIV_INCLUDEDIRS) \
|
||||
)
|
||||
|
||||
COMPONENT_SRCDIRS = $(addprefix freemodbus/, \
|
||||
$(SRCDIRS) \
|
||||
)
|
||||
|
||||
COMPONENT_ADD_INCLUDEDIRS = $(addprefix freemodbus/, \
|
||||
$(INCLUDEDIRS) \
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2016-2021 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
// Stack callback functions prototypes
|
||||
|
||||
#ifndef _ESP_MODBUS_CALLBACKS_H_
|
||||
#define _ESP_MODBUS_CALLBACKS_H_
|
||||
|
||||
#include "mb.h"
|
||||
#include "mb_m.h"
|
||||
|
||||
typedef eMBErrorCode (*reg_input_cb)(UCHAR*, USHORT, USHORT);
|
||||
typedef eMBErrorCode (*reg_holding_cb)(UCHAR*, USHORT, USHORT, eMBRegisterMode);
|
||||
typedef eMBErrorCode (*reg_coils_cb)(UCHAR*, USHORT, USHORT, eMBRegisterMode);
|
||||
typedef eMBErrorCode (*reg_discrete_cb)(UCHAR*, USHORT, USHORT);
|
||||
|
||||
#endif /* _ESP_MODBUS_CALLBACKS_H_ */
|
||||
@@ -0,0 +1,532 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2016-2022 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include "esp_err.h" // for esp_err_t
|
||||
#include "mbc_master.h" // for master interface define
|
||||
#include "esp_modbus_master.h" // for public interface defines
|
||||
#include "esp_modbus_callbacks.h" // for callback functions
|
||||
#include "sdkconfig.h"
|
||||
|
||||
static const char TAG[] __attribute__((unused)) = "MB_CONTROLLER_MASTER";
|
||||
|
||||
// This file implements public API for Modbus master controller.
|
||||
// These functions are wrappers for interface functions of the controller
|
||||
static mb_master_interface_t* master_interface_ptr = NULL;
|
||||
|
||||
void mbc_master_init_iface(void* handler)
|
||||
{
|
||||
master_interface_ptr = (mb_master_interface_t*) handler;
|
||||
}
|
||||
|
||||
/**
|
||||
* Modbus controller destroy function
|
||||
*/
|
||||
esp_err_t mbc_master_destroy(void)
|
||||
{
|
||||
esp_err_t error = ESP_OK;
|
||||
MB_MASTER_CHECK((master_interface_ptr != NULL),
|
||||
ESP_ERR_INVALID_STATE,
|
||||
"Master interface is not correctly initialized.");
|
||||
MB_MASTER_CHECK((master_interface_ptr->destroy != NULL),
|
||||
ESP_ERR_INVALID_STATE,
|
||||
"Master interface is not correctly initialized.");
|
||||
error = master_interface_ptr->destroy();
|
||||
MB_MASTER_CHECK((error == ESP_OK),
|
||||
error,
|
||||
"Master destroy failure, error=(0x%x).",
|
||||
(int)error);
|
||||
return error;
|
||||
}
|
||||
|
||||
esp_err_t mbc_master_get_cid_info(uint16_t cid, const mb_parameter_descriptor_t** param_info)
|
||||
{
|
||||
esp_err_t error = ESP_OK;
|
||||
MB_MASTER_CHECK((master_interface_ptr != NULL),
|
||||
ESP_ERR_INVALID_STATE,
|
||||
"Master interface is not correctly initialized.");
|
||||
MB_MASTER_CHECK((master_interface_ptr->get_cid_info != NULL),
|
||||
ESP_ERR_INVALID_STATE,
|
||||
"Master interface is not correctly initialized.");
|
||||
error = master_interface_ptr->get_cid_info(cid, param_info);
|
||||
MB_MASTER_CHECK((error == ESP_OK),
|
||||
error,
|
||||
"Master get cid info failure, error=(0x%x).",
|
||||
(int)error);
|
||||
return error;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get parameter data for corresponding characteristic
|
||||
*/
|
||||
esp_err_t mbc_master_get_parameter(uint16_t cid, char* name, uint8_t* value, uint8_t* type)
|
||||
{
|
||||
esp_err_t error = ESP_OK;
|
||||
MB_MASTER_CHECK((master_interface_ptr != NULL),
|
||||
ESP_ERR_INVALID_STATE,
|
||||
"Master interface is not correctly initialized.");
|
||||
MB_MASTER_CHECK((master_interface_ptr->get_parameter != NULL),
|
||||
ESP_ERR_INVALID_STATE,
|
||||
"Master interface is not correctly initialized.");
|
||||
error = master_interface_ptr->get_parameter(cid, name, value, type);
|
||||
MB_MASTER_CHECK((error == ESP_OK),
|
||||
error,
|
||||
"Master get parameter failure, error=(0x%x) (%s).",
|
||||
(int)error, esp_err_to_name(error));
|
||||
return error;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send custom Modbus request defined as mb_param_request_t structure
|
||||
*/
|
||||
esp_err_t mbc_master_send_request(mb_param_request_t* request, void* data_ptr)
|
||||
{
|
||||
esp_err_t error = ESP_OK;
|
||||
MB_MASTER_CHECK((master_interface_ptr != NULL),
|
||||
ESP_ERR_INVALID_STATE,
|
||||
"Master interface is not correctly initialized.");
|
||||
MB_MASTER_CHECK((master_interface_ptr->send_request != NULL),
|
||||
ESP_ERR_INVALID_STATE,
|
||||
"Master interface is not correctly initialized.");
|
||||
error = master_interface_ptr->send_request(request, data_ptr);
|
||||
MB_MASTER_CHECK((error == ESP_OK),
|
||||
error,
|
||||
"Master send request failure error=(0x%x) (%s).",
|
||||
(int)error, esp_err_to_name(error));
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set Modbus parameter description table
|
||||
*/
|
||||
esp_err_t mbc_master_set_descriptor(const mb_parameter_descriptor_t* descriptor,
|
||||
const uint16_t num_elements)
|
||||
{
|
||||
esp_err_t error = ESP_OK;
|
||||
MB_MASTER_CHECK((master_interface_ptr != NULL),
|
||||
ESP_ERR_INVALID_STATE,
|
||||
"Master interface is not correctly initialized.");
|
||||
MB_MASTER_CHECK((master_interface_ptr->set_descriptor != NULL),
|
||||
ESP_ERR_INVALID_STATE,
|
||||
"Master interface is not correctly initialized.");
|
||||
error = master_interface_ptr->set_descriptor(descriptor, num_elements);
|
||||
MB_MASTER_CHECK((error == ESP_OK),
|
||||
error,
|
||||
"Master set descriptor failure, error=(0x%x) (%s).",
|
||||
(int)error, esp_err_to_name(error));
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set parameter value for characteristic selected by name and cid
|
||||
*/
|
||||
esp_err_t mbc_master_set_parameter(uint16_t cid, char* name, uint8_t* value, uint8_t* type)
|
||||
{
|
||||
esp_err_t error = ESP_OK;
|
||||
MB_MASTER_CHECK((master_interface_ptr != NULL),
|
||||
ESP_ERR_INVALID_STATE,
|
||||
"Master interface is not correctly initialized.");
|
||||
MB_MASTER_CHECK((master_interface_ptr->set_parameter != NULL),
|
||||
ESP_ERR_INVALID_STATE,
|
||||
"Master interface is not correctly initialized.");
|
||||
error = master_interface_ptr->set_parameter(cid, name, value, type);
|
||||
MB_MASTER_CHECK((error == ESP_OK),
|
||||
error,
|
||||
"Master set parameter failure, error=(0x%x) (%s).",
|
||||
(int)error, esp_err_to_name(error));
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setup Modbus controller parameters
|
||||
*/
|
||||
esp_err_t mbc_master_setup(void* comm_info)
|
||||
{
|
||||
esp_err_t error = ESP_OK;
|
||||
MB_MASTER_CHECK((master_interface_ptr != NULL),
|
||||
ESP_ERR_INVALID_STATE,
|
||||
"Master interface is not correctly initialized.");
|
||||
MB_MASTER_CHECK((master_interface_ptr->setup != NULL),
|
||||
ESP_ERR_INVALID_STATE,
|
||||
"Master interface is not correctly initialized.");
|
||||
error = master_interface_ptr->setup(comm_info);
|
||||
MB_MASTER_CHECK((error == ESP_OK),
|
||||
error,
|
||||
"Master setup failure, error=(0x%x) (%s).",
|
||||
(int)error, esp_err_to_name(error));
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* Modbus controller stack start function
|
||||
*/
|
||||
esp_err_t mbc_master_start(void)
|
||||
{
|
||||
esp_err_t error = ESP_OK;
|
||||
MB_MASTER_CHECK((master_interface_ptr != NULL),
|
||||
ESP_ERR_INVALID_STATE,
|
||||
"Master interface is not correctly initialized.");
|
||||
MB_MASTER_CHECK((master_interface_ptr->start != NULL),
|
||||
ESP_ERR_INVALID_STATE,
|
||||
"Master interface is not correctly initialized.");
|
||||
error = master_interface_ptr->start();
|
||||
MB_MASTER_CHECK((error == ESP_OK),
|
||||
error,
|
||||
"Master start failure, error=(0x%x) (%s).",
|
||||
(int)error, esp_err_to_name(error));
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
eMBErrorCode eMBMasterRegDiscreteCB(UCHAR * pucRegBuffer, USHORT usAddress,
|
||||
USHORT usNDiscrete)
|
||||
{
|
||||
eMBErrorCode error = MB_ENOERR;
|
||||
MB_MASTER_CHECK((master_interface_ptr != NULL),
|
||||
ESP_ERR_INVALID_STATE,
|
||||
"Master interface is not correctly initialized.");
|
||||
MB_MASTER_CHECK((master_interface_ptr->master_reg_cb_discrete != NULL),
|
||||
ESP_ERR_INVALID_STATE,
|
||||
"Master interface is not correctly initialized.");
|
||||
error = master_interface_ptr->master_reg_cb_discrete(pucRegBuffer, usAddress, usNDiscrete);
|
||||
return error;
|
||||
}
|
||||
|
||||
eMBErrorCode eMBMasterRegCoilsCB(UCHAR* pucRegBuffer, USHORT usAddress,
|
||||
USHORT usNCoils, eMBRegisterMode eMode)
|
||||
{
|
||||
eMBErrorCode error = MB_ENOERR;
|
||||
MB_MASTER_CHECK((master_interface_ptr != NULL),
|
||||
ESP_ERR_INVALID_STATE,
|
||||
"Master interface is not correctly initialized.");
|
||||
MB_MASTER_CHECK((master_interface_ptr->master_reg_cb_coils != NULL),
|
||||
ESP_ERR_INVALID_STATE,
|
||||
"Master interface is not correctly initialized.");
|
||||
error = master_interface_ptr->master_reg_cb_coils(pucRegBuffer, usAddress,
|
||||
usNCoils, eMode);
|
||||
return error;
|
||||
}
|
||||
|
||||
eMBErrorCode eMBMasterRegHoldingCB(UCHAR * pucRegBuffer, USHORT usAddress,
|
||||
USHORT usNRegs, eMBRegisterMode eMode)
|
||||
{
|
||||
eMBErrorCode error = MB_ENOERR;
|
||||
MB_MASTER_CHECK((master_interface_ptr != NULL),
|
||||
ESP_ERR_INVALID_STATE,
|
||||
"Master interface is not correctly initialized.");
|
||||
MB_MASTER_CHECK((master_interface_ptr->master_reg_cb_holding != NULL),
|
||||
ESP_ERR_INVALID_STATE,
|
||||
"Master interface is not correctly initialized.");
|
||||
error = master_interface_ptr->master_reg_cb_holding(pucRegBuffer, usAddress,
|
||||
usNRegs, eMode);
|
||||
return error;
|
||||
}
|
||||
|
||||
eMBErrorCode eMBMasterRegInputCB(UCHAR * pucRegBuffer, USHORT usAddress,
|
||||
USHORT usNRegs)
|
||||
{
|
||||
eMBErrorCode error = MB_ENOERR;
|
||||
MB_MASTER_CHECK((master_interface_ptr != NULL),
|
||||
ESP_ERR_INVALID_STATE,
|
||||
"Master interface is not correctly initialized.");
|
||||
MB_MASTER_CHECK((master_interface_ptr->master_reg_cb_input != NULL),
|
||||
ESP_ERR_INVALID_STATE,
|
||||
"Master interface is not correctly initialized.");
|
||||
error = master_interface_ptr->master_reg_cb_input(pucRegBuffer, usAddress, usNRegs);
|
||||
return error;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to get current transaction info
|
||||
*/
|
||||
esp_err_t mbc_master_get_transaction_info(mb_trans_info_t *ptinfo)
|
||||
{
|
||||
MB_MASTER_CHECK((ptinfo),
|
||||
ESP_ERR_INVALID_ARG,
|
||||
"Wrong argument.");
|
||||
MB_MASTER_CHECK(xMBMasterGetLastTransactionInfo(&ptinfo->trans_id, &ptinfo->dest_addr,
|
||||
&ptinfo->func_code, &ptinfo->exception,
|
||||
&ptinfo->err_type),
|
||||
ESP_ERR_INVALID_STATE,
|
||||
"Master can not get transaction info.");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
// Helper function to set parameter buffer according to its type
|
||||
esp_err_t mbc_master_set_param_data(void* dest, void* src, mb_descr_type_t param_type, size_t param_size)
|
||||
{
|
||||
esp_err_t err = ESP_OK;
|
||||
MB_RETURN_ON_FALSE((src), ESP_ERR_INVALID_STATE, TAG,"incorrect data pointer.");
|
||||
MB_RETURN_ON_FALSE((dest), ESP_ERR_INVALID_STATE, TAG,"incorrect data pointer.");
|
||||
void *pdest = dest;
|
||||
void *psrc = src;
|
||||
|
||||
// Transfer parameter data into value of characteristic
|
||||
switch(param_type)
|
||||
{
|
||||
case PARAM_TYPE_U8:
|
||||
for MB_EACH_ELEM(psrc, pdest, param_size, PARAM_SIZE_U8) {
|
||||
*((uint8_t*)pdest) = *((uint8_t*)psrc);
|
||||
}
|
||||
break;
|
||||
|
||||
case PARAM_TYPE_U16:
|
||||
for MB_EACH_ELEM(psrc, pdest, param_size, PARAM_SIZE_U16) {
|
||||
*((uint16_t*)pdest) = *((uint16_t*)psrc);
|
||||
}
|
||||
break;
|
||||
|
||||
case PARAM_TYPE_U32:
|
||||
for MB_EACH_ELEM(psrc, pdest, param_size, PARAM_SIZE_U32) {
|
||||
*((uint32_t*)pdest) = *((uint32_t*)psrc);
|
||||
}
|
||||
break;
|
||||
|
||||
case PARAM_TYPE_FLOAT:
|
||||
for MB_EACH_ELEM(psrc, pdest, param_size, PARAM_SIZE_FLOAT) {
|
||||
*((float*)pdest) = *(float*)psrc;
|
||||
}
|
||||
break;
|
||||
|
||||
case PARAM_TYPE_ASCII:
|
||||
case PARAM_TYPE_BIN:
|
||||
memcpy((void*)dest, (void*)src, (size_t)param_size);
|
||||
break;
|
||||
|
||||
#if CONFIG_FMB_EXT_TYPE_SUPPORT
|
||||
|
||||
case PARAM_TYPE_I8_A:
|
||||
for MB_EACH_ELEM(psrc, pdest, param_size, PARAM_SIZE_U8_REG) {
|
||||
mb_set_int8_a((val_16_arr *)pdest, (*(int8_t*)psrc));
|
||||
ESP_LOGV(TAG, "Convert uint8 B[%d] 0x%04" PRIx16 " = 0x%04" PRIx16, i, *(uint16_t *)psrc, *(uint16_t *)pdest);
|
||||
}
|
||||
break;
|
||||
|
||||
case PARAM_TYPE_I8_B:
|
||||
for MB_EACH_ELEM(psrc, pdest, param_size, PARAM_SIZE_U8_REG) {
|
||||
mb_set_int8_b((val_16_arr *)pdest, (int8_t)((*(uint16_t*)psrc) >> 8));
|
||||
ESP_LOGV(TAG, "Convert int8 A[%d] 0x%02" PRIx16 " = 0x%02" PRIx16, i, *(uint16_t *)psrc, *(uint16_t *)pdest);
|
||||
}
|
||||
break;
|
||||
|
||||
case PARAM_TYPE_U8_A:
|
||||
for MB_EACH_ELEM(psrc, pdest, param_size, PARAM_SIZE_U8_REG) {
|
||||
mb_set_uint8_a((val_16_arr *)pdest, (*(uint8_t*)psrc));
|
||||
ESP_LOGV(TAG, "Convert uint8 A[%d] 0x%02" PRIx16 " = %02" PRIx16, i, *(uint16_t *)psrc, *(uint16_t *)pdest);
|
||||
}
|
||||
break;
|
||||
|
||||
case PARAM_TYPE_U8_B:
|
||||
for MB_EACH_ELEM(psrc, pdest, param_size, PARAM_SIZE_U8_REG) {
|
||||
uint8_t data = (uint8_t)((*(uint16_t*)psrc) >> 8);
|
||||
mb_set_uint8_b((val_16_arr *)pdest, data);
|
||||
ESP_LOGV(TAG, "Convert uint8 B[%d] 0x%02" PRIx16 " = 0x%02" PRIx16, i, *(uint16_t *)psrc, *(uint16_t *)pdest);
|
||||
}
|
||||
break;
|
||||
|
||||
case PARAM_TYPE_I16_AB:
|
||||
for MB_EACH_ELEM(psrc, pdest, param_size, PARAM_SIZE_I16) {
|
||||
mb_set_int16_ab((val_16_arr *)pdest, *(int16_t*)psrc);
|
||||
ESP_LOGV(TAG, "Convert int16 AB[%d] 0x%04" PRIx16 " = 0x%04" PRIx16, i, *(uint16_t *)psrc, *(uint16_t *)pdest);
|
||||
}
|
||||
break;
|
||||
|
||||
case PARAM_TYPE_I16_BA:
|
||||
for MB_EACH_ELEM(psrc, pdest, param_size, PARAM_SIZE_I16) {
|
||||
mb_set_int16_ba((val_16_arr *)pdest, *(int16_t*)psrc);
|
||||
ESP_LOGV(TAG, "Convert int16 BA[%d] 0x%04" PRIx16 " = 0x%04" PRIx16, i, *(uint16_t *)psrc, *(uint16_t *)pdest);
|
||||
}
|
||||
break;
|
||||
|
||||
case PARAM_TYPE_U16_AB:
|
||||
for MB_EACH_ELEM(psrc, pdest, param_size, PARAM_SIZE_U16) {
|
||||
mb_set_uint16_ab((val_16_arr *)pdest, *(uint16_t*)psrc);
|
||||
ESP_LOGV(TAG, "Convert uint16 AB[%d] 0x%02" PRIx16 " = 0x%02" PRIx16, i, *(uint16_t *)psrc, *(uint16_t *)pdest);
|
||||
}
|
||||
break;
|
||||
|
||||
case PARAM_TYPE_U16_BA:
|
||||
for MB_EACH_ELEM(psrc, pdest, param_size, PARAM_SIZE_U16) {
|
||||
mb_set_uint16_ba((val_16_arr *)pdest, *(uint16_t*)psrc);
|
||||
ESP_LOGV(TAG, "Convert uint16 BA[%d] 0x%02" PRIx16 " = 0x%02" PRIx16, i, *(uint16_t *)psrc, *(uint16_t *)pdest);
|
||||
}
|
||||
break;
|
||||
|
||||
case PARAM_TYPE_I32_ABCD:
|
||||
for MB_EACH_ELEM(psrc, pdest, param_size, PARAM_SIZE_I32) {
|
||||
mb_set_int32_abcd((val_32_arr *)pdest, *(int32_t *)psrc);
|
||||
ESP_LOGV(TAG, "Convert int32 ABCD[%d] 0x%04" PRIx32 " = 0x%04" PRIx32, i, *(uint32_t *)psrc, *(uint32_t *)pdest);
|
||||
}
|
||||
break;
|
||||
|
||||
case PARAM_TYPE_U32_ABCD:
|
||||
for MB_EACH_ELEM(psrc, pdest, param_size, PARAM_SIZE_U32) {
|
||||
mb_set_uint32_abcd((val_32_arr *)pdest, *(uint32_t *)psrc);
|
||||
ESP_LOGV(TAG, "Convert uint32 ABCD[%d] 0x%04" PRIx32 " = 0x%04" PRIx32, i, *(uint32_t *)psrc, *(uint32_t *)pdest);
|
||||
}
|
||||
break;
|
||||
|
||||
case PARAM_TYPE_FLOAT_ABCD:
|
||||
for MB_EACH_ELEM(psrc, pdest, param_size, PARAM_SIZE_FLOAT) {
|
||||
mb_set_float_abcd((val_32_arr *)pdest, *(float *)psrc);
|
||||
ESP_LOGV(TAG, "Convert float ABCD[%d] 0x%04" PRIx32 " = 0x%04" PRIx32, i, *(uint32_t *)psrc, *(uint32_t *)pdest);
|
||||
}
|
||||
break;
|
||||
|
||||
case PARAM_TYPE_I32_CDAB:
|
||||
for MB_EACH_ELEM(psrc, pdest, param_size, PARAM_SIZE_I32) {
|
||||
mb_set_int32_cdab((val_32_arr *)pdest, *(int32_t *)psrc);
|
||||
ESP_LOGV(TAG, "Convert int32 CDAB[%d] 0x%04" PRIx32 " = 0x%04" PRIx32, i, *(uint32_t *)psrc, *(uint32_t *)pdest);
|
||||
}
|
||||
break;
|
||||
|
||||
case PARAM_TYPE_U32_CDAB:
|
||||
for MB_EACH_ELEM(psrc, pdest, param_size, PARAM_SIZE_U32) {
|
||||
mb_set_uint32_cdab((val_32_arr *)pdest, *(uint32_t *)psrc);
|
||||
ESP_LOGV(TAG, "Convert uint32 CDAB[%d] 0x%04" PRIx32 " = 0x%04" PRIx32, i, *(uint32_t *)psrc, *(uint32_t *)pdest);
|
||||
}
|
||||
break;
|
||||
|
||||
case PARAM_TYPE_FLOAT_CDAB:
|
||||
for MB_EACH_ELEM(psrc, pdest, param_size, PARAM_SIZE_FLOAT) {
|
||||
mb_set_float_cdab((val_32_arr *)pdest, *(float *)psrc);
|
||||
ESP_LOGV(TAG, "Convert float CDAB[%d] 0x%04" PRIx32 " = 0x%04" PRIx32, i, *(uint32_t *)psrc, *(uint32_t *)pdest);
|
||||
}
|
||||
break;
|
||||
|
||||
case PARAM_TYPE_I32_BADC:
|
||||
for MB_EACH_ELEM(psrc, pdest, param_size, PARAM_SIZE_I32) {
|
||||
mb_set_int32_badc((val_32_arr *)pdest, *(int32_t *)psrc);
|
||||
ESP_LOGV(TAG, "Convert int32 BADC[%d] 0x%04" PRIx32 " = 0x%04" PRIx32, i, *(uint32_t *)psrc, *(uint32_t *)pdest);
|
||||
}
|
||||
break;
|
||||
|
||||
case PARAM_TYPE_U32_BADC:
|
||||
for MB_EACH_ELEM(psrc, pdest, param_size, PARAM_SIZE_U32) {
|
||||
mb_set_uint32_badc((val_32_arr *)pdest, *(uint32_t *)psrc);
|
||||
ESP_LOGV(TAG, "Convert uint32 BADC[%d] 0x%04" PRIx32 " = 0x%04" PRIx32, i, *(uint32_t *)psrc, *(uint32_t *)pdest);
|
||||
}
|
||||
break;
|
||||
|
||||
case PARAM_TYPE_FLOAT_BADC:
|
||||
for MB_EACH_ELEM(psrc, pdest, param_size, PARAM_SIZE_FLOAT) {
|
||||
mb_set_float_badc((val_32_arr *)pdest, *(float *)psrc);
|
||||
ESP_LOGV(TAG, "Convert float BADC[%d] 0x%04" PRIx32 " = 0x%04" PRIx32, i, *(uint32_t *)psrc, *(uint32_t *)pdest);
|
||||
}
|
||||
break;
|
||||
|
||||
case PARAM_TYPE_I32_DCBA:
|
||||
for MB_EACH_ELEM(psrc, pdest, param_size, PARAM_SIZE_I32) {
|
||||
mb_set_int32_dcba((val_32_arr *)pdest, *(int32_t *)psrc);
|
||||
ESP_LOGV(TAG, "Convert int32 DCBA[%d] 0x%04" PRIx32 " = 0x%04" PRIx32, i, *(uint32_t *)psrc, *(uint32_t *)pdest);
|
||||
}
|
||||
break;
|
||||
|
||||
case PARAM_TYPE_U32_DCBA:
|
||||
for MB_EACH_ELEM(psrc, pdest, param_size, PARAM_SIZE_U32) {
|
||||
mb_set_uint32_dcba((val_32_arr *)pdest, *(uint32_t *)psrc);
|
||||
ESP_LOGV(TAG, "Convert uint32 DCBA[%d] 0x%04" PRIx32 " = 0x%04" PRIx32, i, *(uint32_t *)psrc, *(uint32_t *)pdest);
|
||||
}
|
||||
break;
|
||||
|
||||
case PARAM_TYPE_FLOAT_DCBA:
|
||||
for MB_EACH_ELEM(psrc, pdest, param_size, PARAM_SIZE_FLOAT) {
|
||||
mb_set_float_dcba((val_32_arr *)pdest, *(float *)psrc);
|
||||
ESP_LOGV(TAG, "Convert float DCBA[%d] 0x%04" PRIx32 " = 0x%04" PRIx32, i, *(uint32_t *)psrc, *(uint32_t *)pdest);
|
||||
}
|
||||
break;
|
||||
|
||||
case PARAM_TYPE_I64_ABCDEFGH:
|
||||
for MB_EACH_ELEM(psrc, pdest, param_size, PARAM_SIZE_I64) {
|
||||
mb_set_int64_abcdefgh((val_64_arr *)pdest, *(int64_t *)psrc);
|
||||
ESP_LOGV(TAG, "Convert int64 ABCDEFGH[%d] 0x%" PRIx64 " = 0x%" PRIx64, i, *(uint64_t *)psrc, *(uint64_t *)pdest);
|
||||
}
|
||||
break;
|
||||
|
||||
case PARAM_TYPE_U64_ABCDEFGH:
|
||||
for MB_EACH_ELEM(psrc, pdest, param_size, PARAM_SIZE_U64) {
|
||||
mb_set_uint64_abcdefgh((val_64_arr *)pdest, *(uint64_t *)psrc);
|
||||
ESP_LOGV(TAG, "Convert double ABCDEFGH[%d] 0x%" PRIx64 " = 0x%" PRIx64, i, *(uint64_t *)psrc, *(uint64_t *)pdest);
|
||||
}
|
||||
break;
|
||||
|
||||
case PARAM_TYPE_DOUBLE_ABCDEFGH:
|
||||
for MB_EACH_ELEM(psrc, pdest, param_size, PARAM_SIZE_DOUBLE) {
|
||||
mb_set_double_abcdefgh((val_64_arr *)pdest, *(double *)psrc);
|
||||
ESP_LOGV(TAG, "Convert double ABCDEFGH[%d] 0x%" PRIx64 " = 0x%" PRIx64, i, *(uint64_t *)psrc, *(uint64_t *)pdest);
|
||||
}
|
||||
break;
|
||||
|
||||
case PARAM_TYPE_I64_HGFEDCBA:
|
||||
for MB_EACH_ELEM(psrc, pdest, param_size, PARAM_SIZE_I64) {
|
||||
mb_set_int64_hgfedcba((val_64_arr *)pdest, *(int64_t *)psrc);
|
||||
ESP_LOGV(TAG, "Convert int64 HGFEDCBA[%d] 0x%" PRIx64 " = 0x%" PRIx64, i, *(uint64_t *)psrc, *(uint64_t *)pdest);
|
||||
}
|
||||
break;
|
||||
|
||||
case PARAM_TYPE_U64_HGFEDCBA:
|
||||
for MB_EACH_ELEM(psrc, pdest, param_size, PARAM_SIZE_U64) {
|
||||
mb_set_uint64_hgfedcba((val_64_arr *)pdest, *(uint64_t *)psrc);
|
||||
ESP_LOGV(TAG, "Convert double HGFEDCBA[%d] 0x%" PRIx64 " = 0x%" PRIx64, i, *(uint64_t *)psrc, *(uint64_t *)pdest);
|
||||
}
|
||||
break;
|
||||
|
||||
case PARAM_TYPE_DOUBLE_HGFEDCBA:
|
||||
for MB_EACH_ELEM(psrc, pdest, param_size, PARAM_SIZE_DOUBLE) {
|
||||
mb_set_double_hgfedcba((val_64_arr *)pdest, *(double *)psrc);
|
||||
ESP_LOGV(TAG, "Convert double HGFEDCBA[%d] 0x%" PRIx64 " = 0x%" PRIx64, i, *(uint64_t *)psrc, *(uint64_t *)pdest);
|
||||
}
|
||||
break;
|
||||
|
||||
case PARAM_TYPE_I64_GHEFCDAB:
|
||||
for MB_EACH_ELEM(psrc, pdest, param_size, PARAM_SIZE_I64) {
|
||||
mb_set_int64_ghefcdab((val_64_arr *)pdest, *(int64_t *)psrc);
|
||||
ESP_LOGV(TAG, "Convert int64 GHEFCDAB[%d] 0x%" PRIx64 " = 0x%" PRIx64, i, *(uint64_t *)psrc, *(uint64_t *)pdest);
|
||||
}
|
||||
break;
|
||||
|
||||
case PARAM_TYPE_U64_GHEFCDAB:
|
||||
for MB_EACH_ELEM(psrc, pdest, param_size, PARAM_SIZE_U64) {
|
||||
mb_set_uint64_ghefcdab((val_64_arr *)pdest, *(uint64_t *)psrc);
|
||||
ESP_LOGV(TAG, "Convert uint64 GHEFCDAB[%d] 0x%" PRIx64 " = 0x%" PRIx64, i, *(uint64_t *)psrc, *(uint64_t *)pdest);
|
||||
}
|
||||
break;
|
||||
|
||||
case PARAM_TYPE_DOUBLE_GHEFCDAB:
|
||||
for MB_EACH_ELEM(psrc, pdest, param_size, PARAM_SIZE_DOUBLE) {
|
||||
mb_set_double_ghefcdab((val_64_arr *)pdest, *(double *)psrc);
|
||||
ESP_LOGV(TAG, "Convert double GHEFCDAB[%d] 0x%" PRIx64 " = 0x%" PRIx64, i, *(uint64_t *)psrc, *(uint64_t *)pdest);
|
||||
}
|
||||
break;
|
||||
|
||||
case PARAM_TYPE_I64_BADCFEHG:
|
||||
for MB_EACH_ELEM(psrc, pdest, param_size, PARAM_SIZE_I64) {
|
||||
mb_set_int64_badcfehg((val_64_arr *)pdest, *(int64_t *)psrc);
|
||||
ESP_LOGV(TAG, "Convert int64 BADCFEHG[%d] 0x%" PRIx64 " = 0x%" PRIx64, i, *(uint64_t *)psrc, *(uint64_t *)pdest);
|
||||
}
|
||||
break;
|
||||
|
||||
case PARAM_TYPE_U64_BADCFEHG:
|
||||
for MB_EACH_ELEM(psrc, pdest, param_size, PARAM_SIZE_U64) {
|
||||
mb_set_uint64_badcfehg((val_64_arr *)pdest, *(uint64_t *)psrc);
|
||||
ESP_LOGV(TAG, "Convert uint64 BADCFEHG[%d] 0x%" PRIx64 " = 0x%" PRIx64, i, *(uint64_t *)psrc, *(uint64_t *)pdest);
|
||||
}
|
||||
break;
|
||||
|
||||
case PARAM_TYPE_DOUBLE_BADCFEHG:
|
||||
for MB_EACH_ELEM(psrc, pdest, param_size, PARAM_SIZE_DOUBLE) {
|
||||
mb_set_double_badcfehg((val_64_arr *)pdest, *(double *)psrc);
|
||||
ESP_LOGV(TAG, "Convert double BADCFEHG[%d] 0x%" PRIx64 " = 0x%" PRIx64, i, *(uint64_t *)psrc, *(uint64_t *)pdest);
|
||||
}
|
||||
break;
|
||||
|
||||
#endif
|
||||
default:
|
||||
ESP_LOGE(TAG, "%s: Incorrect param type (%u).",
|
||||
__FUNCTION__, (unsigned)param_type);
|
||||
err = ESP_ERR_NOT_SUPPORTED;
|
||||
break;
|
||||
}
|
||||
return err;
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2016-2021 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include "esp_err.h" // for esp_err_t
|
||||
#include "mbc_master.h" // for master interface define
|
||||
#include "esp_modbus_master.h" // for public slave defines
|
||||
#include "mbc_serial_master.h" // for public interface defines
|
||||
|
||||
/**
|
||||
* Initialization of Modbus master serial
|
||||
*/
|
||||
esp_err_t mbc_master_init(mb_port_type_t port_type, void** handler)
|
||||
{
|
||||
void* port_handler = NULL;
|
||||
esp_err_t error = ESP_ERR_NOT_SUPPORTED;
|
||||
switch(port_type)
|
||||
{
|
||||
case MB_PORT_SERIAL_MASTER:
|
||||
error = mbc_serial_master_create(&port_handler);
|
||||
break;
|
||||
default:
|
||||
return ESP_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
if ((port_handler != NULL) && (error == ESP_OK)) {
|
||||
mbc_master_init_iface(port_handler);
|
||||
*handler = port_handler;
|
||||
}
|
||||
return error;
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2016-2021 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include "esp_err.h" // for esp_err_t
|
||||
#include "esp_modbus_master.h" // for public interface defines
|
||||
#include "mbc_tcp_master.h" // for public interface defines
|
||||
|
||||
/**
|
||||
* Initialization of Modbus TCP Master controller interface
|
||||
*/
|
||||
esp_err_t mbc_master_init_tcp(void** handler)
|
||||
{
|
||||
void* port_handler = NULL;
|
||||
esp_err_t error = mbc_tcp_master_create(&port_handler);
|
||||
|
||||
if ((port_handler != NULL) && (error == ESP_OK)) {
|
||||
mbc_master_init_iface(port_handler);
|
||||
*handler = port_handler;
|
||||
}
|
||||
return error;
|
||||
}
|
||||
@@ -0,0 +1,530 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2016-2022 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include "esp_err.h" // for esp_err_t
|
||||
#include "esp_timer.h" // for esp_timer_get_time()
|
||||
#include "sdkconfig.h" // for KConfig defines
|
||||
|
||||
#include "mbc_slave.h" // for slave private type definitions
|
||||
#include "mbutils.h" // for stack bit setting utilities
|
||||
#include "esp_modbus_common.h" // for common defines
|
||||
#include "esp_modbus_slave.h" // for public slave defines
|
||||
#include "esp_modbus_callbacks.h" // for modbus callbacks function pointers declaration
|
||||
|
||||
#ifdef CONFIG_FMB_CONTROLLER_SLAVE_ID_SUPPORT
|
||||
|
||||
#define MB_ID_BYTE0(id) ((uint8_t)(id))
|
||||
#define MB_ID_BYTE1(id) ((uint8_t)(((uint16_t)(id) >> 8) & 0xFF))
|
||||
#define MB_ID_BYTE2(id) ((uint8_t)(((uint32_t)(id) >> 16) & 0xFF))
|
||||
#define MB_ID_BYTE3(id) ((uint8_t)(((uint32_t)(id) >> 24) & 0xFF))
|
||||
|
||||
#define MB_CONTROLLER_SLAVE_ID (CONFIG_FMB_CONTROLLER_SLAVE_ID)
|
||||
#define MB_SLAVE_ID_SHORT (MB_ID_BYTE3(MB_CONTROLLER_SLAVE_ID))
|
||||
|
||||
// Slave ID constant
|
||||
static uint8_t mb_slave_id[] = { MB_ID_BYTE0(MB_CONTROLLER_SLAVE_ID),
|
||||
MB_ID_BYTE1(MB_CONTROLLER_SLAVE_ID),
|
||||
MB_ID_BYTE2(MB_CONTROLLER_SLAVE_ID) };
|
||||
|
||||
#endif
|
||||
|
||||
#define REG_SIZE(type, nregs) ((type == MB_PARAM_INPUT) || (type == MB_PARAM_HOLDING)) ? (nregs >> 1) : (nregs << 3)
|
||||
|
||||
// Common interface pointer for slave port
|
||||
static mb_slave_interface_t* slave_interface_ptr = NULL;
|
||||
static const char TAG[] __attribute__((unused)) = "MB_CONTROLLER_SLAVE";
|
||||
|
||||
// Searches the register in the area specified by type, returns descriptor if found, else NULL
|
||||
static mb_descr_entry_t* mbc_slave_find_reg_descriptor(mb_param_type_t type, uint16_t addr, size_t regs)
|
||||
{
|
||||
mb_descr_entry_t* it;
|
||||
uint16_t reg_size = 0;
|
||||
|
||||
mb_slave_options_t* mbs_opts = &slave_interface_ptr->opts;
|
||||
|
||||
if (LIST_EMPTY(&mbs_opts->mbs_area_descriptors[type])) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// search for the register in each area
|
||||
for (it = LIST_FIRST(&mbs_opts->mbs_area_descriptors[type]); it != NULL; it = LIST_NEXT(it, entries)) {
|
||||
reg_size = REG_SIZE(type, it->size);
|
||||
if ((addr >= it->start_offset)
|
||||
&& (it->p_data)
|
||||
&& (regs >= 1)
|
||||
&& ((addr + regs) <= (it->start_offset + reg_size))
|
||||
&& (reg_size >= 1)) {
|
||||
return it;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void mbc_slave_free_descriptors(void) {
|
||||
|
||||
mb_descr_entry_t* it;
|
||||
mb_slave_options_t* mbs_opts = &slave_interface_ptr->opts;
|
||||
|
||||
for (int descr_type = 0; descr_type < MB_PARAM_COUNT; descr_type++) {
|
||||
while ((it = LIST_FIRST(&mbs_opts->mbs_area_descriptors[descr_type]))) {
|
||||
LIST_REMOVE(it, entries);
|
||||
free(it);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void mbc_slave_init_iface(void* handler)
|
||||
{
|
||||
slave_interface_ptr = (mb_slave_interface_t*) handler;
|
||||
mb_slave_options_t* mbs_opts = &slave_interface_ptr->opts;
|
||||
// Initialize list head for register areas
|
||||
LIST_INIT(&mbs_opts->mbs_area_descriptors[MB_PARAM_INPUT]);
|
||||
LIST_INIT(&mbs_opts->mbs_area_descriptors[MB_PARAM_HOLDING]);
|
||||
LIST_INIT(&mbs_opts->mbs_area_descriptors[MB_PARAM_COIL]);
|
||||
LIST_INIT(&mbs_opts->mbs_area_descriptors[MB_PARAM_DISCRETE]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Modbus controller destroy function
|
||||
*/
|
||||
esp_err_t mbc_slave_destroy(void)
|
||||
{
|
||||
esp_err_t error = ESP_OK;
|
||||
// Is initialization done?
|
||||
MB_SLAVE_CHECK((slave_interface_ptr != NULL),
|
||||
ESP_ERR_INVALID_STATE,
|
||||
"Slave interface is not correctly initialized.");
|
||||
// Check if interface has been initialized
|
||||
MB_SLAVE_CHECK((slave_interface_ptr->destroy != NULL),
|
||||
ESP_ERR_INVALID_STATE,
|
||||
"Slave interface is not correctly initialized.");
|
||||
// Call the slave port destroy function
|
||||
error = slave_interface_ptr->destroy();
|
||||
MB_SLAVE_CHECK((error == ESP_OK),
|
||||
ESP_ERR_INVALID_STATE,
|
||||
"Slave destroy failure error=(0x%x).",
|
||||
(int)error);
|
||||
// Destroy all opened descriptors
|
||||
mbc_slave_free_descriptors();
|
||||
free(slave_interface_ptr);
|
||||
slave_interface_ptr = NULL;
|
||||
return error;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setup Modbus controller parameters
|
||||
*/
|
||||
esp_err_t mbc_slave_setup(void* comm_info)
|
||||
{
|
||||
esp_err_t error = ESP_OK;
|
||||
MB_SLAVE_CHECK((slave_interface_ptr != NULL),
|
||||
ESP_ERR_INVALID_STATE,
|
||||
"Slave interface is not correctly initialized.");
|
||||
MB_SLAVE_CHECK((slave_interface_ptr->setup != NULL),
|
||||
ESP_ERR_INVALID_STATE,
|
||||
"Slave interface is not correctly initialized.");
|
||||
error = slave_interface_ptr->setup(comm_info);
|
||||
MB_SLAVE_CHECK((error == ESP_OK),
|
||||
ESP_ERR_INVALID_STATE,
|
||||
"Slave setup failure error=(0x%x).",
|
||||
(int)error);
|
||||
return error;
|
||||
}
|
||||
|
||||
/**
|
||||
* Start Modbus controller start function
|
||||
*/
|
||||
esp_err_t mbc_slave_start(void)
|
||||
{
|
||||
esp_err_t error = ESP_OK;
|
||||
MB_SLAVE_CHECK((slave_interface_ptr != NULL),
|
||||
ESP_ERR_INVALID_STATE,
|
||||
"Slave interface is not correctly initialized.");
|
||||
MB_SLAVE_CHECK((slave_interface_ptr->start != NULL),
|
||||
ESP_ERR_INVALID_STATE,
|
||||
"Slave interface is not correctly initialized.");
|
||||
#ifdef CONFIG_FMB_CONTROLLER_SLAVE_ID_SUPPORT
|
||||
// Set the slave ID if the KConfig option is selected
|
||||
eMBErrorCode status = eMBSetSlaveID(MB_SLAVE_ID_SHORT, TRUE, (UCHAR*)mb_slave_id, sizeof(mb_slave_id));
|
||||
MB_SLAVE_CHECK((status == MB_ENOERR), ESP_ERR_INVALID_STATE, "mb stack set slave ID failure.");
|
||||
#endif
|
||||
error = slave_interface_ptr->start();
|
||||
MB_SLAVE_CHECK((error == ESP_OK),
|
||||
ESP_ERR_INVALID_STATE,
|
||||
"Slave start failure error=(0x%x).",
|
||||
(int)error);
|
||||
return error;
|
||||
}
|
||||
|
||||
/**
|
||||
* Blocking function to get event on parameter group change for application task
|
||||
*/
|
||||
mb_event_group_t mbc_slave_check_event(mb_event_group_t group)
|
||||
{
|
||||
MB_SLAVE_CHECK((slave_interface_ptr != NULL),
|
||||
MB_EVENT_NO_EVENTS,
|
||||
"Slave interface is not correctly initialized.");
|
||||
MB_SLAVE_CHECK((slave_interface_ptr->check_event != NULL),
|
||||
MB_EVENT_NO_EVENTS,
|
||||
"Slave interface is not correctly initialized.");
|
||||
mb_event_group_t event = slave_interface_ptr->check_event(group);
|
||||
return event;
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to get notification about parameter change from application task
|
||||
*/
|
||||
esp_err_t mbc_slave_get_param_info(mb_param_info_t* reg_info, uint32_t timeout)
|
||||
{
|
||||
esp_err_t error = ESP_OK;
|
||||
MB_SLAVE_CHECK((slave_interface_ptr != NULL),
|
||||
ESP_ERR_INVALID_STATE,
|
||||
"Slave interface is not correctly initialized.");
|
||||
MB_SLAVE_CHECK((slave_interface_ptr->get_param_info != NULL),
|
||||
ESP_ERR_INVALID_STATE,
|
||||
"Slave interface is not correctly initialized.");
|
||||
error = slave_interface_ptr->get_param_info(reg_info, timeout);
|
||||
MB_SLAVE_CHECK((error == ESP_OK),
|
||||
ESP_ERR_INVALID_STATE,
|
||||
"Slave get parameter info failure error=(0x%x).",
|
||||
(int)error);
|
||||
return error;
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to set area descriptors for modbus parameters
|
||||
*/
|
||||
esp_err_t mbc_slave_set_descriptor(mb_register_area_descriptor_t descr_data)
|
||||
{
|
||||
esp_err_t error = ESP_OK;
|
||||
MB_SLAVE_CHECK((slave_interface_ptr != NULL),
|
||||
ESP_ERR_INVALID_STATE,
|
||||
"Slave interface is not correctly initialized.");
|
||||
|
||||
if (slave_interface_ptr->set_descriptor != NULL) {
|
||||
error = slave_interface_ptr->set_descriptor(descr_data);
|
||||
MB_SLAVE_CHECK((error == ESP_OK),
|
||||
ESP_ERR_INVALID_STATE,
|
||||
"Slave set descriptor failure error=(0x%x).",
|
||||
(int)error);
|
||||
} else {
|
||||
mb_slave_options_t* mbs_opts = &slave_interface_ptr->opts;
|
||||
// Check if the address is already in the descriptor list
|
||||
mb_descr_entry_t* it = mbc_slave_find_reg_descriptor(descr_data.type, descr_data.start_offset, 1);
|
||||
MB_SLAVE_CHECK((it == NULL), ESP_ERR_INVALID_ARG, "mb incorrect descriptor or already defined.");
|
||||
|
||||
mb_descr_entry_t* new_descr = (mb_descr_entry_t*) heap_caps_malloc(sizeof(mb_descr_entry_t),
|
||||
MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
|
||||
MB_SLAVE_CHECK((new_descr != NULL), ESP_ERR_NO_MEM, "mb can not allocate memory for descriptor.");
|
||||
new_descr->start_offset = descr_data.start_offset;
|
||||
new_descr->type = descr_data.type;
|
||||
new_descr->p_data = descr_data.address;
|
||||
new_descr->size = descr_data.size;
|
||||
LIST_INSERT_HEAD(&mbs_opts->mbs_area_descriptors[descr_data.type], new_descr, entries);
|
||||
error = ESP_OK;
|
||||
}
|
||||
return error;
|
||||
}
|
||||
|
||||
// The helper function to get time stamp in microseconds
|
||||
static uint64_t mbc_slave_get_time_stamp(void)
|
||||
{
|
||||
uint64_t time_stamp = esp_timer_get_time();
|
||||
return time_stamp;
|
||||
}
|
||||
|
||||
// Helper function to send parameter information to application task
|
||||
static esp_err_t mbc_slave_send_param_info(mb_event_group_t par_type, uint16_t mb_offset,
|
||||
uint8_t* par_address, uint16_t par_size)
|
||||
{
|
||||
MB_SLAVE_ASSERT(slave_interface_ptr != NULL);
|
||||
mb_slave_options_t* mbs_opts = &slave_interface_ptr->opts;
|
||||
esp_err_t error = ESP_FAIL;
|
||||
mb_param_info_t par_info;
|
||||
// Check if queue is not full the send parameter information
|
||||
par_info.type = par_type;
|
||||
par_info.size = par_size;
|
||||
par_info.address = par_address;
|
||||
par_info.time_stamp = mbc_slave_get_time_stamp();
|
||||
par_info.mb_offset = mb_offset;
|
||||
BaseType_t status = xQueueSend(mbs_opts->mbs_notification_queue_handle, &par_info, MB_PAR_INFO_TOUT);
|
||||
if (pdTRUE == status) {
|
||||
ESP_LOGD(TAG, "Queue send parameter info (type, address, size): %d, 0x%" PRIx32 ", %u",
|
||||
(int)par_type, (uint32_t)par_address, (unsigned)par_size);
|
||||
error = ESP_OK;
|
||||
} else if (errQUEUE_FULL == status) {
|
||||
ESP_LOGD(TAG, "Parameter queue is overflowed.");
|
||||
}
|
||||
return error;
|
||||
}
|
||||
|
||||
// Helper function to send notification
|
||||
static esp_err_t mbc_slave_send_param_access_notification(mb_event_group_t event)
|
||||
{
|
||||
MB_SLAVE_ASSERT(slave_interface_ptr != NULL);
|
||||
mb_slave_options_t* mbs_opts = &slave_interface_ptr->opts;
|
||||
esp_err_t err = ESP_FAIL;
|
||||
mb_event_group_t bits = (mb_event_group_t)xEventGroupSetBits(mbs_opts->mbs_event_group, (EventBits_t)event);
|
||||
if (bits & event) {
|
||||
ESP_LOGD(TAG, "The MB_REG_CHANGE_EVENT = 0x%.2x is set.", (int)event);
|
||||
err = ESP_OK;
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
/*
|
||||
* Below are the common slave read/write register callback functions
|
||||
* The concrete slave port can override them using interface function pointers
|
||||
*/
|
||||
|
||||
// Callback function for reading of MB Input Registers
|
||||
eMBErrorCode mbc_reg_input_slave_cb(UCHAR * reg_buffer, USHORT address, USHORT n_regs)
|
||||
{
|
||||
MB_SLAVE_CHECK((slave_interface_ptr != NULL),
|
||||
MB_EILLSTATE, "Slave stack uninitialized.");
|
||||
MB_SLAVE_CHECK((reg_buffer != NULL),
|
||||
MB_EINVAL, "Slave stack call failed.");
|
||||
eMBErrorCode status = MB_ENOERR;
|
||||
address--; // address of register is already +1
|
||||
mb_descr_entry_t* it = mbc_slave_find_reg_descriptor(MB_PARAM_INPUT, address, n_regs);
|
||||
if (it != NULL) {
|
||||
uint16_t input_reg_start = (uint16_t)it->start_offset; // Get Modbus start address
|
||||
uint8_t* input_buffer = (uint8_t*)it->p_data; // Get instance address
|
||||
uint16_t regs = n_regs;
|
||||
uint16_t reg_index;
|
||||
// If input or configuration parameters are incorrect then return an error to stack layer
|
||||
reg_index = (uint16_t)(address - input_reg_start);
|
||||
reg_index <<= 1; // register Address to byte address
|
||||
input_buffer += reg_index;
|
||||
uint8_t* buffer_start = input_buffer;
|
||||
while (regs > 0) {
|
||||
_XFER_2_RD(reg_buffer, input_buffer);
|
||||
reg_index += 2;
|
||||
regs -= 1;
|
||||
}
|
||||
// Send access notification
|
||||
(void)mbc_slave_send_param_access_notification(MB_EVENT_INPUT_REG_RD);
|
||||
// Send parameter info to application task
|
||||
(void)mbc_slave_send_param_info(MB_EVENT_INPUT_REG_RD, (uint16_t)address,
|
||||
(uint8_t*)buffer_start, (uint16_t)n_regs);
|
||||
} else {
|
||||
status = MB_ENOREG;
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
// Callback function for reading of MB Holding Registers
|
||||
// Executed by stack when request to read/write holding registers is received
|
||||
eMBErrorCode mbc_reg_holding_slave_cb(UCHAR * reg_buffer, USHORT address, USHORT n_regs, eMBRegisterMode mode)
|
||||
{
|
||||
MB_SLAVE_CHECK((slave_interface_ptr != NULL),
|
||||
MB_EILLSTATE, "Slave stack uninitialized.");
|
||||
MB_SLAVE_CHECK((reg_buffer != NULL),
|
||||
MB_EINVAL, "Slave stack call failed.");
|
||||
eMBErrorCode status = MB_ENOERR;
|
||||
uint16_t reg_index;
|
||||
address--; // address of register is already +1
|
||||
mb_descr_entry_t* it = mbc_slave_find_reg_descriptor(MB_PARAM_HOLDING, address, n_regs);
|
||||
if (it != NULL) {
|
||||
uint16_t reg_holding_start = (uint16_t)it->start_offset; // Get Modbus start address
|
||||
uint8_t* holding_buffer = (uint8_t*)it->p_data; // Get instance address
|
||||
uint16_t regs = n_regs;
|
||||
reg_index = (uint16_t) (address - reg_holding_start);
|
||||
reg_index <<= 1; // register Address to byte address
|
||||
holding_buffer += reg_index;
|
||||
uint8_t* buffer_start = holding_buffer;
|
||||
switch (mode) {
|
||||
case MB_REG_READ:
|
||||
while (regs > 0) {
|
||||
_XFER_2_RD(reg_buffer, holding_buffer);
|
||||
reg_index += 2;
|
||||
regs -= 1;
|
||||
};
|
||||
// Send access notification
|
||||
(void)mbc_slave_send_param_access_notification(MB_EVENT_HOLDING_REG_RD);
|
||||
// Send parameter info
|
||||
(void)mbc_slave_send_param_info(MB_EVENT_HOLDING_REG_RD, (uint16_t)address,
|
||||
(uint8_t*)buffer_start, (uint16_t)n_regs);
|
||||
break;
|
||||
case MB_REG_WRITE:
|
||||
while (regs > 0) {
|
||||
_XFER_2_WR(holding_buffer, reg_buffer);
|
||||
holding_buffer += 2;
|
||||
reg_index += 2;
|
||||
regs -= 1;
|
||||
};
|
||||
// Send access notification
|
||||
(void)mbc_slave_send_param_access_notification(MB_EVENT_HOLDING_REG_WR);
|
||||
// Send parameter info
|
||||
(void)mbc_slave_send_param_info(MB_EVENT_HOLDING_REG_WR, (uint16_t)address,
|
||||
(uint8_t*)buffer_start, (uint16_t)n_regs);
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
status = MB_ENOREG;
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
// Callback function for reading of MB Coils Registers
|
||||
eMBErrorCode mbc_reg_coils_slave_cb(UCHAR* reg_buffer, USHORT address, USHORT n_coils, eMBRegisterMode mode)
|
||||
{
|
||||
MB_SLAVE_CHECK((slave_interface_ptr != NULL),
|
||||
MB_EILLSTATE, "Slave stack uninitialized.");
|
||||
MB_SLAVE_CHECK((reg_buffer != NULL),
|
||||
MB_EINVAL, "Slave stack call failed.");
|
||||
eMBErrorCode status = MB_ENOERR;
|
||||
uint16_t reg_index;
|
||||
uint16_t coils = n_coils;
|
||||
address--; // The address is already +1
|
||||
mb_descr_entry_t* it = mbc_slave_find_reg_descriptor(MB_PARAM_COIL, address, n_coils);
|
||||
if (it != NULL) {
|
||||
uint16_t reg_coils_start = (uint16_t)it->start_offset; // MB offset of coils
|
||||
uint8_t* reg_coils_buf = (uint8_t*)it->p_data;
|
||||
reg_index = (uint16_t) (address - it->start_offset);
|
||||
CHAR* coils_data_buf = (CHAR*)(reg_coils_buf + (reg_index >> 3));
|
||||
switch (mode) {
|
||||
case MB_REG_READ:
|
||||
while (coils > 0) {
|
||||
uint8_t result = xMBUtilGetBits((uint8_t*)reg_coils_buf, reg_index, 1);
|
||||
xMBUtilSetBits(reg_buffer, reg_index - (address - reg_coils_start), 1, result);
|
||||
reg_index++;
|
||||
coils--;
|
||||
}
|
||||
// Send an event to notify application task about event
|
||||
(void)mbc_slave_send_param_access_notification(MB_EVENT_COILS_RD);
|
||||
(void)mbc_slave_send_param_info(MB_EVENT_COILS_RD, (uint16_t)address,
|
||||
(uint8_t*)(coils_data_buf), (uint16_t)n_coils);
|
||||
break;
|
||||
case MB_REG_WRITE:
|
||||
while (coils > 0) {
|
||||
uint8_t result = xMBUtilGetBits(reg_buffer,
|
||||
reg_index - (address - reg_coils_start), 1);
|
||||
xMBUtilSetBits((uint8_t*)reg_coils_buf, reg_index, 1, result);
|
||||
reg_index++;
|
||||
coils--;
|
||||
}
|
||||
// Send an event to notify application task about event
|
||||
(void)mbc_slave_send_param_access_notification(MB_EVENT_COILS_WR);
|
||||
(void)mbc_slave_send_param_info(MB_EVENT_COILS_WR, (uint16_t)address,
|
||||
(uint8_t*)coils_data_buf, (uint16_t)n_coils);
|
||||
break;
|
||||
} // switch ( eMode )
|
||||
} else {
|
||||
// If the configuration or input parameters are incorrect then return error to stack
|
||||
status = MB_ENOREG;
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
// Callback function for reading of MB Discrete Input Registers
|
||||
eMBErrorCode mbc_reg_discrete_slave_cb(UCHAR* reg_buffer, USHORT address, USHORT n_discrete)
|
||||
{
|
||||
MB_SLAVE_CHECK((slave_interface_ptr != NULL),
|
||||
MB_EILLSTATE, "Slave stack uninitialized.");
|
||||
MB_SLAVE_CHECK((reg_buffer != NULL),
|
||||
MB_EINVAL, "Slave stack call failed.");
|
||||
|
||||
eMBErrorCode status = MB_ENOERR;
|
||||
uint16_t reg_index;
|
||||
uint16_t reg_bit_index;
|
||||
uint16_t n_reg;
|
||||
uint8_t* discrete_input_buf;
|
||||
// It already plus one in modbus function method.
|
||||
address--;
|
||||
mb_descr_entry_t* it = mbc_slave_find_reg_descriptor(MB_PARAM_DISCRETE, address, n_discrete);
|
||||
if (it != NULL) {
|
||||
uint16_t reg_discrete_start = (uint16_t)it->start_offset; // MB offset of registers
|
||||
n_reg = (n_discrete >> 3) + 1;
|
||||
discrete_input_buf = (uint8_t*)it->p_data; // the storage address
|
||||
reg_index = (uint16_t) (address - reg_discrete_start) / 8; // Get register index in the buffer for bit number
|
||||
reg_bit_index = (uint16_t)(address - reg_discrete_start) % 8; // Get bit index
|
||||
uint8_t* temp_buf = &discrete_input_buf[reg_index];
|
||||
while (n_reg > 0) {
|
||||
*reg_buffer++ = xMBUtilGetBits(&discrete_input_buf[reg_index++], reg_bit_index, 8);
|
||||
n_reg--;
|
||||
}
|
||||
reg_buffer--;
|
||||
// Last discrete
|
||||
n_discrete = n_discrete % 8;
|
||||
// Filling zero to high bit
|
||||
*reg_buffer = *reg_buffer << (8 - n_discrete);
|
||||
*reg_buffer = *reg_buffer >> (8 - n_discrete);
|
||||
// Send an event to notify application task about event
|
||||
(void)mbc_slave_send_param_access_notification(MB_EVENT_DISCRETE_RD);
|
||||
(void)mbc_slave_send_param_info(MB_EVENT_DISCRETE_RD, (uint16_t)address,
|
||||
(uint8_t*)temp_buf, (uint16_t)n_discrete);
|
||||
} else {
|
||||
status = MB_ENOREG;
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
/**
|
||||
* Below are the stack callback functions to read/write registers
|
||||
*/
|
||||
eMBErrorCode eMBRegDiscreteCB(UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete)
|
||||
{
|
||||
eMBErrorCode error = MB_ENOERR;
|
||||
MB_SLAVE_CHECK((slave_interface_ptr != NULL),
|
||||
ESP_ERR_INVALID_STATE,
|
||||
"Slave interface is not correctly initialized.");
|
||||
// Check if the callback is overridden in concrete port
|
||||
if (slave_interface_ptr->slave_reg_cb_discrete) {
|
||||
error = slave_interface_ptr->slave_reg_cb_discrete(pucRegBuffer, usAddress, usNDiscrete);
|
||||
} else {
|
||||
error = mbc_reg_discrete_slave_cb(pucRegBuffer, usAddress, usNDiscrete);
|
||||
}
|
||||
|
||||
return error;
|
||||
}
|
||||
|
||||
eMBErrorCode eMBRegCoilsCB(UCHAR* pucRegBuffer, USHORT usAddress,
|
||||
USHORT usNCoils, eMBRegisterMode eMode)
|
||||
{
|
||||
eMBErrorCode error = MB_ENOERR;
|
||||
MB_SLAVE_CHECK((slave_interface_ptr != NULL),
|
||||
ESP_ERR_INVALID_STATE,
|
||||
"Slave interface is not correctly initialized.");
|
||||
|
||||
if (slave_interface_ptr->slave_reg_cb_coils) {
|
||||
error = slave_interface_ptr->slave_reg_cb_coils(pucRegBuffer, usAddress, usNCoils, eMode);
|
||||
} else {
|
||||
error = mbc_reg_coils_slave_cb(pucRegBuffer, usAddress, usNCoils, eMode);
|
||||
}
|
||||
return error;
|
||||
}
|
||||
|
||||
eMBErrorCode eMBRegHoldingCB(UCHAR * pucRegBuffer, USHORT usAddress,
|
||||
USHORT usNRegs, eMBRegisterMode eMode)
|
||||
{
|
||||
eMBErrorCode error = MB_ENOERR;
|
||||
MB_SLAVE_CHECK((slave_interface_ptr != NULL),
|
||||
ESP_ERR_INVALID_STATE,
|
||||
"Slave interface is not correctly initialized.");
|
||||
|
||||
if (slave_interface_ptr->slave_reg_cb_holding) {
|
||||
error = slave_interface_ptr->slave_reg_cb_holding(pucRegBuffer, usAddress, usNRegs, eMode);
|
||||
} else {
|
||||
error = mbc_reg_holding_slave_cb(pucRegBuffer, usAddress, usNRegs, eMode);
|
||||
}
|
||||
return error;
|
||||
}
|
||||
|
||||
eMBErrorCode eMBRegInputCB(UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs)
|
||||
{
|
||||
eMBErrorCode error = ESP_ERR_INVALID_STATE;
|
||||
MB_SLAVE_CHECK((slave_interface_ptr != NULL),
|
||||
ESP_ERR_INVALID_STATE,
|
||||
"Slave interface is not correctly initialized.");
|
||||
|
||||
if (slave_interface_ptr->slave_reg_cb_input) {
|
||||
error = slave_interface_ptr->slave_reg_cb_input(pucRegBuffer, usAddress, usNRegs);
|
||||
} else {
|
||||
error = mbc_reg_input_slave_cb(pucRegBuffer, usAddress, usNRegs);
|
||||
}
|
||||
return error;
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2016-2021 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include "esp_err.h" // for esp_err_t
|
||||
#include "sdkconfig.h" // for KConfig defines
|
||||
#include "mbc_slave.h" // for slave interface define
|
||||
#include "esp_modbus_slave.h" // for public slave defines
|
||||
#include "mbc_serial_slave.h" // for public interface defines
|
||||
|
||||
/**
|
||||
* Initialization of Modbus Serial slave controller
|
||||
*/
|
||||
esp_err_t mbc_slave_init(mb_port_type_t port_type, void** handler)
|
||||
{
|
||||
void* port_handler = NULL;
|
||||
esp_err_t error = ESP_ERR_NOT_SUPPORTED;
|
||||
switch(port_type)
|
||||
{
|
||||
case MB_PORT_SERIAL_SLAVE:
|
||||
// Call constructor function of actual port implementation
|
||||
error = mbc_serial_slave_create(&port_handler);
|
||||
break;
|
||||
default:
|
||||
return ESP_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
if ((port_handler != NULL) && (error == ESP_OK)) {
|
||||
mbc_slave_init_iface(port_handler);
|
||||
*handler = port_handler;
|
||||
}
|
||||
return error;
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2016-2021 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include "esp_err.h" // for esp_err_t
|
||||
#include "esp_modbus_slave.h" // for public slave defines
|
||||
#include "mbc_tcp_slave.h" // for public interface defines
|
||||
|
||||
/**
|
||||
* Initialization of Modbus TCP Slave controller
|
||||
*/
|
||||
esp_err_t mbc_slave_init_tcp(void** handler)
|
||||
{
|
||||
void* port_handler = NULL;
|
||||
esp_err_t error = mbc_tcp_slave_create(&port_handler);
|
||||
|
||||
if ((port_handler != NULL) && (error == ESP_OK)) {
|
||||
mbc_slave_init_iface(port_handler);
|
||||
*handler = port_handler;
|
||||
}
|
||||
return error;
|
||||
}
|
||||
@@ -0,0 +1,157 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2016-2022 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#ifndef _MB_IFACE_COMMON_H
|
||||
#define _MB_IFACE_COMMON_H
|
||||
|
||||
#include <inttypes.h> // needs to be included for default system types (such as PRIxx)
|
||||
#include "driver/uart.h" // for UART types
|
||||
#include "sdkconfig.h"
|
||||
|
||||
#if CONFIG_FMB_EXT_TYPE_SUPPORT
|
||||
#include "mb_endianness_utils.h"
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#if __has_include("esp_check.h")
|
||||
#include "esp_check.h"
|
||||
#include "esp_log.h"
|
||||
|
||||
#define MB_RETURN_ON_FALSE(a, err_code, tag, format, ...) ESP_RETURN_ON_FALSE(a, err_code, tag, format __VA_OPT__(,) __VA_ARGS__)
|
||||
|
||||
#else
|
||||
|
||||
// if cannot include esp_check then use custom check macro
|
||||
|
||||
#define MB_RETURN_ON_FALSE(a, err_code, tag, format, ...) do { \
|
||||
if (!(a)) { \
|
||||
ESP_LOGE(tag, "%s(%" PRIu32 "): " format, __FUNCTION__, __LINE__ __VA_OPT__(,) __VA_ARGS__); \
|
||||
return err_code; \
|
||||
} \
|
||||
} while(0)
|
||||
|
||||
#endif
|
||||
|
||||
#define MB_CONTROLLER_STACK_SIZE (CONFIG_FMB_CONTROLLER_STACK_SIZE) // Stack size for Modbus controller
|
||||
#define MB_CONTROLLER_PRIORITY (CONFIG_FMB_PORT_TASK_PRIO - 1) // priority of MB controller task
|
||||
|
||||
// Default port defines
|
||||
#define MB_DEVICE_ADDRESS (1) // Default slave device address in Modbus
|
||||
#define MB_DEVICE_SPEED (115200) // Default Modbus speed for now hard defined
|
||||
#define MB_UART_PORT (UART_NUM_MAX - 1) // Default UART port number
|
||||
#define MB_PAR_INFO_TOUT (10) // Timeout for get parameter info
|
||||
#define MB_PARITY_NONE (UART_PARITY_DISABLE)
|
||||
|
||||
// The Macros below handle the endianness while transfer N byte data into buffer (convert from network byte order)
|
||||
#define _XFER_2_RD(dst, src) { \
|
||||
*(uint8_t *)(dst)++ = *(uint8_t *)(src + 1); \
|
||||
*(uint8_t *)(dst)++ = *(uint8_t *)(src + 0); \
|
||||
(src) += 2; \
|
||||
}
|
||||
|
||||
#define _XFER_2_WR(dst, src) { \
|
||||
*(uint8_t *)(dst + 1) = *(uint8_t *)(src)++; \
|
||||
*(uint8_t *)(dst + 0) = *(uint8_t *)(src)++; \
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Types of actual Modbus implementation
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
MB_PORT_SERIAL_MASTER = 0x00, /*!< Modbus port type serial master. */
|
||||
MB_PORT_SERIAL_SLAVE, /*!< Modbus port type serial slave. */
|
||||
MB_PORT_TCP_MASTER, /*!< Modbus port type TCP master. */
|
||||
MB_PORT_TCP_SLAVE, /*!< Modbus port type TCP slave. */
|
||||
MB_PORT_COUNT, /*!< Modbus port count. */
|
||||
MB_PORT_INACTIVE = 0xFF
|
||||
} mb_port_type_t;
|
||||
|
||||
/**
|
||||
* @brief Event group for parameters notification
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
MB_EVENT_NO_EVENTS = 0x00,
|
||||
MB_EVENT_HOLDING_REG_WR = BIT0, /*!< Modbus Event Write Holding registers. */
|
||||
MB_EVENT_HOLDING_REG_RD = BIT1, /*!< Modbus Event Read Holding registers. */
|
||||
MB_EVENT_INPUT_REG_RD = BIT3, /*!< Modbus Event Read Input registers. */
|
||||
MB_EVENT_COILS_WR = BIT4, /*!< Modbus Event Write Coils. */
|
||||
MB_EVENT_COILS_RD = BIT5, /*!< Modbus Event Read Coils. */
|
||||
MB_EVENT_DISCRETE_RD = BIT6, /*!< Modbus Event Read Discrete bits. */
|
||||
MB_EVENT_STACK_STARTED = BIT7 /*!< Modbus Event Stack started */
|
||||
} mb_event_group_t;
|
||||
|
||||
/**
|
||||
* @brief Type of Modbus parameter
|
||||
*/
|
||||
typedef enum {
|
||||
MB_PARAM_HOLDING = 0x00, /*!< Modbus Holding register. */
|
||||
MB_PARAM_INPUT, /*!< Modbus Input register. */
|
||||
MB_PARAM_COIL, /*!< Modbus Coils. */
|
||||
MB_PARAM_DISCRETE, /*!< Modbus Discrete bits. */
|
||||
MB_PARAM_COUNT,
|
||||
MB_PARAM_UNKNOWN = 0xFF
|
||||
} mb_param_type_t;
|
||||
|
||||
/*!
|
||||
* \brief Modbus serial transmission modes (RTU/ASCII).
|
||||
*/
|
||||
typedef enum {
|
||||
MB_MODE_RTU, /*!< RTU transmission mode. */
|
||||
MB_MODE_ASCII, /*!< ASCII transmission mode. */
|
||||
MB_MODE_TCP, /*!< TCP communication mode. */
|
||||
MB_MODE_UDP /*!< UDP communication mode. */
|
||||
} mb_mode_type_t;
|
||||
|
||||
/*!
|
||||
* \brief Modbus TCP type of address.
|
||||
*/
|
||||
typedef enum {
|
||||
MB_IPV4 = 0, /*!< TCP IPV4 addressing */
|
||||
MB_IPV6 = 1 /*!< TCP IPV6 addressing */
|
||||
} mb_tcp_addr_type_t;
|
||||
|
||||
/**
|
||||
* @brief Device communication structure to setup Modbus controller
|
||||
*/
|
||||
typedef union {
|
||||
// Serial communication structure
|
||||
struct {
|
||||
mb_mode_type_t mode; /*!< Modbus communication mode */
|
||||
uint8_t slave_addr; /*!< Modbus slave address field (dummy for master) */
|
||||
uart_port_t port; /*!< Modbus communication port (UART) number */
|
||||
uint32_t baudrate; /*!< Modbus baudrate */
|
||||
uart_parity_t parity; /*!< Modbus UART parity settings */
|
||||
uint16_t dummy_port; /*!< Dummy field, unused */
|
||||
};
|
||||
// TCP/UDP communication structure
|
||||
struct {
|
||||
mb_mode_type_t ip_mode; /*!< Modbus communication mode */
|
||||
uint8_t slave_uid; /*!< Modbus slave address field for UID */
|
||||
uint16_t ip_port; /*!< Modbus port */
|
||||
mb_tcp_addr_type_t ip_addr_type; /*!< Modbus address type */
|
||||
void* ip_addr; /*!< Modbus address table for connection */
|
||||
void* ip_netif_ptr; /*!< Modbus network interface */
|
||||
};
|
||||
} mb_communication_info_t;
|
||||
|
||||
/**
|
||||
* common interface method types
|
||||
*/
|
||||
typedef esp_err_t (*iface_init)(void**); /*!< Interface method init */
|
||||
typedef esp_err_t (*iface_destroy)(void); /*!< Interface method destroy */
|
||||
typedef esp_err_t (*iface_setup)(void*); /*!< Interface method setup */
|
||||
typedef esp_err_t (*iface_start)(void); /*!< Interface method start */
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // _MB_IFACE_COMMON_H
|
||||
@@ -0,0 +1,351 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2016-2022 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#ifndef _ESP_MB_MASTER_INTERFACE_H
|
||||
#define _ESP_MB_MASTER_INTERFACE_H
|
||||
|
||||
#include <stdint.h> // for standard int types definition
|
||||
#include <stddef.h> // for NULL and std defines
|
||||
#include "soc/soc.h" // for BITN definitions
|
||||
#include "esp_modbus_common.h" // for common types
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define MB_MASTER_CHECK(a, err_code, format, ...) MB_RETURN_ON_FALSE(a, err_code, TAG, format __VA_OPT__(,) __VA_ARGS__)
|
||||
|
||||
#define MB_MASTER_ASSERT(con) do { \
|
||||
if (!(con)) { ESP_LOGE(TAG, "assert errno:%u, errno_str: !(%s)", (unsigned)errno, strerror(errno)); assert(0 && #con); } \
|
||||
} while (0)
|
||||
|
||||
/*!
|
||||
* \brief The macro to access arrays of elements for type conversion.
|
||||
*/
|
||||
#define MB_EACH_ELEM(psrc, pdest, arr_size, elem_size) \
|
||||
(int i = 0; (i < (arr_size / elem_size)); i++, pdest += elem_size, psrc += elem_size)
|
||||
|
||||
/*!
|
||||
* \brief Modbus descriptor table parameter type defines.
|
||||
*/
|
||||
typedef enum {
|
||||
PARAM_TYPE_U8 = 0x00, /*!< Unsigned 8 */
|
||||
PARAM_TYPE_U16 = 0x01, /*!< Unsigned 16 */
|
||||
PARAM_TYPE_U32 = 0x02, /*!< Unsigned 32 */
|
||||
PARAM_TYPE_FLOAT = 0x03, /*!< Float type */
|
||||
PARAM_TYPE_ASCII = 0x04, /*!< ASCII type */
|
||||
PARAM_TYPE_BIN = 0x07, /*!< BIN type */
|
||||
PARAM_TYPE_I8_A = 0x0A, /*!< I8 signed integer in high byte of register */
|
||||
PARAM_TYPE_I8_B = 0x0B, /*!< I8 signed integer in low byte of register */
|
||||
PARAM_TYPE_U8_A = 0x0C, /*!< U8 unsigned integer written to hi byte of register */
|
||||
PARAM_TYPE_U8_B = 0x0D, /*!< U8 unsigned integer written to low byte of register */
|
||||
PARAM_TYPE_I16_AB = 0x0E, /*!< I16 signed integer, big endian */
|
||||
PARAM_TYPE_I16_BA = 0x0F, /*!< I16 signed integer, little endian */
|
||||
PARAM_TYPE_U16_AB = 0x10, /*!< U16 unsigned integer, big endian*/
|
||||
PARAM_TYPE_U16_BA = 0x11, /*!< U16 unsigned integer, little endian */
|
||||
PARAM_TYPE_I32_ABCD = 0x12, /*!< I32 ABCD signed integer, big endian */
|
||||
PARAM_TYPE_I32_CDAB = 0x13, /*!< I32 CDAB signed integer, big endian, reversed register order */
|
||||
PARAM_TYPE_I32_BADC = 0x14, /*!< I32 BADC signed integer, little endian, reversed register order */
|
||||
PARAM_TYPE_I32_DCBA = 0x15, /*!< I32 DCBA signed integer, little endian */
|
||||
PARAM_TYPE_U32_ABCD = 0x16, /*!< U32 ABCD unsigned integer, big endian */
|
||||
PARAM_TYPE_U32_CDAB = 0x17, /*!< U32 CDAB unsigned integer, big endian, reversed register order */
|
||||
PARAM_TYPE_U32_BADC = 0x18, /*!< U32 BADC unsigned integer, little endian, reversed register order */
|
||||
PARAM_TYPE_U32_DCBA = 0x19, /*!< U32 DCBA unsigned integer, little endian */
|
||||
PARAM_TYPE_FLOAT_ABCD = 0x1A, /*!< Float ABCD floating point, big endian */
|
||||
PARAM_TYPE_FLOAT_CDAB = 0x1B, /*!< Float CDAB floating point big endian, reversed register order */
|
||||
PARAM_TYPE_FLOAT_BADC = 0x1C, /*!< Float BADC floating point, little endian, reversed register order */
|
||||
PARAM_TYPE_FLOAT_DCBA = 0x1D, /*!< Float DCBA floating point, little endian */
|
||||
PARAM_TYPE_I64_ABCDEFGH = 0x1E, /*!< I64, ABCDEFGH signed integer, big endian */
|
||||
PARAM_TYPE_I64_HGFEDCBA = 0x1F, /*!< I64, HGFEDCBA signed integer, little endian */
|
||||
PARAM_TYPE_I64_GHEFCDAB = 0x20, /*!< I64, GHEFCDAB signed integer, big endian, reversed register order */
|
||||
PARAM_TYPE_I64_BADCFEHG = 0x21, /*!< I64, BADCFEHG signed integer, little endian, reversed register order */
|
||||
PARAM_TYPE_U64_ABCDEFGH = 0x22, /*!< U64, ABCDEFGH unsigned integer, big endian */
|
||||
PARAM_TYPE_U64_HGFEDCBA = 0x23, /*!< U64, HGFEDCBA unsigned integer, little endian */
|
||||
PARAM_TYPE_U64_GHEFCDAB = 0x24, /*!< U64, GHEFCDAB unsigned integer, big endian, reversed register order */
|
||||
PARAM_TYPE_U64_BADCFEHG = 0x25, /*!< U64, BADCFEHG unsigned integer, little endian, reversed register order */
|
||||
PARAM_TYPE_DOUBLE_ABCDEFGH = 0x26, /*!< Double ABCDEFGH floating point, big endian*/
|
||||
PARAM_TYPE_DOUBLE_HGFEDCBA = 0x27, /*!< Double HGFEDCBA floating point, little endian*/
|
||||
PARAM_TYPE_DOUBLE_GHEFCDAB = 0x28, /*!< Double GHEFCDAB floating point, big endian, reversed register order */
|
||||
PARAM_TYPE_DOUBLE_BADCFEHG = 0x29 /*!< Double BADCFEHG floating point, little endian, reversed register order */
|
||||
} mb_descr_type_t;
|
||||
|
||||
/*!
|
||||
* \brief Modbus descriptor table parameter size in bytes.
|
||||
*/
|
||||
typedef enum {
|
||||
PARAM_SIZE_U8 = 0x01, /*!< Unsigned 8 */
|
||||
PARAM_SIZE_U8_REG = 0x02, /*!< Unsigned 8, register value */
|
||||
PARAM_SIZE_I8_REG = 0x02, /*!< Signed 8, register value */
|
||||
PARAM_SIZE_I16 = 0x02, /*!< Unsigned 16 */
|
||||
PARAM_SIZE_U16 = 0x02, /*!< Unsigned 16 */
|
||||
PARAM_SIZE_I32 = 0x04, /*!< Signed 32 */
|
||||
PARAM_SIZE_U32 = 0x04, /*!< Unsigned 32 */
|
||||
PARAM_SIZE_FLOAT = 0x04, /*!< Float 32 size */
|
||||
PARAM_SIZE_ASCII = 0x08, /*!< ASCII size default*/
|
||||
PARAM_SIZE_ASCII24 = 0x18, /*!< ASCII24 size */
|
||||
PARAM_SIZE_I64 = 0x08, /*!< Signed integer 64 size */
|
||||
PARAM_SIZE_U64 = 0x08, /*!< Unsigned integer 64 size */
|
||||
PARAM_SIZE_DOUBLE = 0x08, /*!< Double 64 size */
|
||||
PARAM_MAX_SIZE
|
||||
} mb_descr_size_t;
|
||||
|
||||
/*!
|
||||
* \brief Modbus parameter options for description table
|
||||
*/
|
||||
typedef union {
|
||||
struct {
|
||||
int opt1; /*!< Parameter option1 */
|
||||
int opt2; /*!< Parameter option2 */
|
||||
int opt3; /*!< Parameter option3 */
|
||||
};
|
||||
struct {
|
||||
int min; /*!< Parameter minimum value */
|
||||
int max; /*!< Parameter maximum value */
|
||||
int step; /*!< Step of parameter change tracking */
|
||||
};
|
||||
} mb_parameter_opt_t;
|
||||
|
||||
/**
|
||||
* @brief Permissions for the characteristics
|
||||
*/
|
||||
typedef enum {
|
||||
PAR_PERMS_READ = 1 << BIT0, /**< the characteristic of the device are readable */
|
||||
PAR_PERMS_WRITE = 1 << BIT1, /**< the characteristic of the device are writable*/
|
||||
PAR_PERMS_TRIGGER = 1 << BIT2, /**< the characteristic of the device are triggerable */
|
||||
PAR_PERMS_READ_WRITE = PAR_PERMS_READ | PAR_PERMS_WRITE, /**< the characteristic of the device are readable & writable */
|
||||
PAR_PERMS_READ_TRIGGER = PAR_PERMS_READ | PAR_PERMS_TRIGGER, /**< the characteristic of the device are readable & triggerable */
|
||||
PAR_PERMS_WRITE_TRIGGER = PAR_PERMS_WRITE | PAR_PERMS_TRIGGER, /**< the characteristic of the device are writable & triggerable */
|
||||
PAR_PERMS_READ_WRITE_TRIGGER = PAR_PERMS_READ_WRITE | PAR_PERMS_TRIGGER, /**< the characteristic of the device are readable & writable & triggerable */
|
||||
} mb_param_perms_t;
|
||||
|
||||
/**
|
||||
* @brief Characteristics descriptor type is used to describe characteristic and
|
||||
* link it with Modbus parameters that reflect its data.
|
||||
*/
|
||||
typedef struct {
|
||||
uint16_t cid; /*!< Characteristic cid */
|
||||
const char* param_key; /*!< The key (name) of the parameter */
|
||||
const char* param_units; /*!< The physical units of the parameter */
|
||||
uint8_t mb_slave_addr; /*!< Slave address of device in the Modbus segment */
|
||||
mb_param_type_t mb_param_type; /*!< Type of modbus parameter */
|
||||
uint16_t mb_reg_start; /*!< This is the Modbus register address. This is the 0 based value. */
|
||||
uint16_t mb_size; /*!< Size of mb parameter in registers */
|
||||
uint16_t param_offset; /*!< Parameter name (OFFSET in the parameter structure) */
|
||||
mb_descr_type_t param_type; /*!< Float, U8, U16, U32, ASCII, etc. */
|
||||
mb_descr_size_t param_size; /*!< Number of bytes in the parameter. */
|
||||
mb_parameter_opt_t param_opts; /*!< Parameter options used to check limits and etc. */
|
||||
mb_param_perms_t access; /*!< Access permissions based on mode */
|
||||
} mb_parameter_descriptor_t;
|
||||
|
||||
/**
|
||||
* @brief Modbus register request type structure
|
||||
*/
|
||||
typedef struct {
|
||||
uint8_t slave_addr; /*!< Modbus slave address */
|
||||
uint8_t command; /*!< Modbus command to send */
|
||||
uint16_t reg_start; /*!< Modbus start register */
|
||||
uint16_t reg_size; /*!< Modbus number of registers */
|
||||
} mb_param_request_t;
|
||||
|
||||
/**
|
||||
* @brief Modbus transacion info structure
|
||||
*/
|
||||
typedef struct {
|
||||
uint64_t trans_id; /*!< Modbus unique transaction identificator */
|
||||
uint16_t err_type; /*!< Modbus last transaction error type */
|
||||
uint8_t dest_addr; /*!< Modbus destination short address (or UID) */
|
||||
uint8_t func_code; /*!< Modbus last transaction function code */
|
||||
uint8_t exception; /*!< Modbus last transaction exception code returned by slave */
|
||||
} mb_trans_info_t;
|
||||
|
||||
/**
|
||||
* @brief Initialize Modbus controller and stack for TCP port
|
||||
*
|
||||
* @param[out] handler handler(pointer) to master data structure
|
||||
* @return
|
||||
* - ESP_OK Success
|
||||
* - ESP_ERR_NO_MEM Parameter error
|
||||
* - ESP_ERR_NOT_SUPPORTED Port type not supported
|
||||
* - ESP_ERR_INVALID_STATE Initialization failure
|
||||
*/
|
||||
esp_err_t mbc_master_init_tcp(void** handler);
|
||||
|
||||
/**
|
||||
* @brief Initialize Modbus Master controller and stack for Serial port
|
||||
*
|
||||
* @param[out] handler handler(pointer) to master data structure
|
||||
* @param[in] port_type type of stack
|
||||
* @return
|
||||
* - ESP_OK Success
|
||||
* - ESP_ERR_NO_MEM Parameter error
|
||||
* - ESP_ERR_NOT_SUPPORTED Port type not supported
|
||||
* - ESP_ERR_INVALID_STATE Initialization failure
|
||||
*/
|
||||
esp_err_t mbc_master_init(mb_port_type_t port_type, void** handler);
|
||||
|
||||
/**
|
||||
* @brief Initialize Modbus Master controller interface handle
|
||||
*
|
||||
* @param[in] handler - pointer to master data structure
|
||||
*/
|
||||
void mbc_master_init_iface(void* handler);
|
||||
|
||||
/**
|
||||
* @brief Destroy Modbus controller and stack
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK Success
|
||||
* - ESP_ERR_INVALID_STATE Parameter error
|
||||
*/
|
||||
esp_err_t mbc_master_destroy(void);
|
||||
|
||||
/**
|
||||
* @brief Start Modbus communication stack
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK Success
|
||||
* - ESP_ERR_INVALID_ARG Modbus stack start error
|
||||
*/
|
||||
esp_err_t mbc_master_start(void);
|
||||
|
||||
/**
|
||||
* @brief Set Modbus communication parameters for the controller
|
||||
*
|
||||
* @param comm_info Communication parameters structure.
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK Success
|
||||
* - ESP_ERR_INVALID_ARG Incorrect parameter data
|
||||
*/
|
||||
esp_err_t mbc_master_setup(void* comm_info);
|
||||
|
||||
/***************************** Specific interface functions ********************************************
|
||||
* Interface functions below provide basic methods to read/write access to slave devices in Modbus
|
||||
* segment as well as API to read specific supported characteristics linked to Modbus parameters
|
||||
* of devices in Modbus network.
|
||||
*******************************************************************************************************/
|
||||
|
||||
/**
|
||||
* @brief Assign parameter description table for Modbus controller interface.
|
||||
*
|
||||
* @param[in] descriptor pointer to parameter description table
|
||||
* @param num_elements number of elements in the table
|
||||
*
|
||||
* @return
|
||||
* - esp_err_t ESP_OK - set descriptor successfully
|
||||
* - esp_err_t ESP_ERR_INVALID_ARG - invalid argument in function call
|
||||
*/
|
||||
esp_err_t mbc_master_set_descriptor(const mb_parameter_descriptor_t* descriptor, const uint16_t num_elements);
|
||||
|
||||
/**
|
||||
* @brief Send data request as defined in parameter request, waits response
|
||||
* from slave and returns status of command execution. This function provides standard way
|
||||
* for read/write access to Modbus devices in the network.
|
||||
*
|
||||
* @param[in] request pointer to request structure of type mb_param_request_t
|
||||
* @param[in] data_ptr pointer to data buffer to send or received data (dependent of command field in request)
|
||||
*
|
||||
* @return
|
||||
* - esp_err_t ESP_OK - request was successful
|
||||
* - esp_err_t ESP_ERR_INVALID_ARG - invalid argument of function
|
||||
* - esp_err_t ESP_ERR_INVALID_RESPONSE - an invalid response from slave
|
||||
* - esp_err_t ESP_ERR_TIMEOUT - operation timeout or no response from slave
|
||||
* - esp_err_t ESP_ERR_NOT_SUPPORTED - the request command is not supported by slave
|
||||
* - esp_err_t ESP_FAIL - slave returned an exception or other failure
|
||||
*/
|
||||
esp_err_t mbc_master_send_request(mb_param_request_t* request, void* data_ptr);
|
||||
|
||||
/**
|
||||
* @brief Get information about supported characteristic defined as cid. Uses parameter description table to get
|
||||
* this information. The function will check if characteristic defined as a cid parameter is supported
|
||||
* and returns its description in param_info. Returns ESP_ERR_NOT_FOUND if characteristic is not supported.
|
||||
*
|
||||
* @param[in] cid characteristic id
|
||||
* @param param_info pointer to pointer of characteristic data.
|
||||
*
|
||||
* @return
|
||||
* - esp_err_t ESP_OK - request was successful and buffer contains the supported characteristic name
|
||||
* - esp_err_t ESP_ERR_INVALID_ARG - invalid argument of function
|
||||
* - esp_err_t ESP_ERR_NOT_FOUND - the characteristic (cid) not found
|
||||
* - esp_err_t ESP_FAIL - unknown error during lookup table processing
|
||||
*/
|
||||
esp_err_t mbc_master_get_cid_info(uint16_t cid, const mb_parameter_descriptor_t** param_info);
|
||||
|
||||
/**
|
||||
* @brief Read parameter from modbus slave device whose name is defined by name and has cid.
|
||||
* The additional data for request is taken from parameter description (lookup) table.
|
||||
*
|
||||
* @param[in] cid id of the characteristic for parameter
|
||||
* @param[in] name pointer into string name (key) of parameter (null terminated)
|
||||
* @param[out] value pointer to data buffer of parameter
|
||||
* @param[out] type parameter type associated with the name returned from parameter description table.
|
||||
*
|
||||
* @return
|
||||
* - esp_err_t ESP_OK - request was successful and value buffer contains
|
||||
* representation of actual parameter data from slave
|
||||
* - esp_err_t ESP_ERR_INVALID_ARG - invalid argument of function or parameter descriptor
|
||||
* - esp_err_t ESP_ERR_INVALID_RESPONSE - an invalid response from slave
|
||||
* - esp_err_t ESP_ERR_INVALID_STATE - invalid state during data processing or allocation failure
|
||||
* - esp_err_t ESP_ERR_TIMEOUT - operation timed out and no response from slave
|
||||
* - esp_err_t ESP_ERR_NOT_SUPPORTED - the request command is not supported by slave
|
||||
* - esp_err_t ESP_ERR_NOT_FOUND - the parameter is not found in the parameter description table
|
||||
* - esp_err_t ESP_FAIL - slave returned an exception or other failure
|
||||
*/
|
||||
esp_err_t mbc_master_get_parameter(uint16_t cid, char* name, uint8_t* value, uint8_t *type);
|
||||
|
||||
/**
|
||||
* @brief Set characteristic's value defined as a name and cid parameter.
|
||||
* The additional data for cid parameter request is taken from master parameter lookup table.
|
||||
*
|
||||
* @param[in] cid id of the characteristic for parameter
|
||||
* @param[in] name pointer into string name (key) of parameter (null terminated)
|
||||
* @param[out] value pointer to data buffer of parameter (actual representation of json value field in binary form)
|
||||
* @param[out] type pointer to parameter type associated with the name returned from parameter lookup table.
|
||||
*
|
||||
* @return
|
||||
* - esp_err_t ESP_OK - request was successful and value was saved in the slave device registers
|
||||
* - esp_err_t ESP_ERR_INVALID_ARG - invalid argument of function or parameter descriptor
|
||||
* - esp_err_t ESP_ERR_INVALID_RESPONSE - an invalid response from slave during processing of parameter
|
||||
* - esp_err_t ESP_ERR_INVALID_STATE - invalid state during data processing or allocation failure
|
||||
* - esp_err_t ESP_ERR_TIMEOUT - operation timed out and no response from slave
|
||||
* - esp_err_t ESP_ERR_NOT_SUPPORTED - the request command is not supported by slave
|
||||
* - esp_err_t ESP_FAIL - slave returned an exception or other failure
|
||||
*/
|
||||
esp_err_t mbc_master_set_parameter(uint16_t cid, char* name, uint8_t* value, uint8_t *type);
|
||||
|
||||
/**
|
||||
* @brief The helper function to set data of parameters according to its type
|
||||
*
|
||||
* @param[in] dest the destination address of the parameter
|
||||
* @param[in] src the source address of the parameter
|
||||
* @param[out] param_type type of parameter from data dictionary
|
||||
* @param[out] param_size the storage size of the characteristic (in bytes).
|
||||
* Describes the size of data to keep into data instance during mapping.
|
||||
*
|
||||
* @return
|
||||
* - esp_err_t ESP_OK - request was successful and value was saved in the slave device registers
|
||||
* - esp_err_t ESP_ERR_INVALID_ARG - invalid argument of function or parameter descriptor
|
||||
* - esp_err_t ESP_ERR_NOT_SUPPORTED - the request command is not supported by slave
|
||||
*/
|
||||
esp_err_t mbc_master_set_param_data(void* dest, void* src, mb_descr_type_t param_type, size_t param_size);
|
||||
|
||||
/**
|
||||
* @brief The helper function to expose transaction info from modbus layer
|
||||
*
|
||||
* @param[in] ptinfo the pointer to transaction info structure
|
||||
*
|
||||
* @return
|
||||
* - esp_err_t ESP_OK - the transaction info is saved in the appropriate parameter structure
|
||||
* - esp_err_t ESP_ERR_INVALID_ARG - invalid argument of function or parameter descriptor
|
||||
* - esp_err_t ESP_ERR_INVALID_STATE - invalid state during data processing or allocation failure
|
||||
*/
|
||||
esp_err_t mbc_master_get_transaction_info(mb_trans_info_t *ptinfo);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // _ESP_MB_MASTER_INTERFACE_H
|
||||
@@ -0,0 +1,148 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2016-2022 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#ifndef _ESP_MB_SLAVE_INTERFACE_H
|
||||
#define _ESP_MB_SLAVE_INTERFACE_H
|
||||
|
||||
// Public interface header for slave
|
||||
#include <stdint.h> // for standard int types definition
|
||||
#include <stddef.h> // for NULL and std defines
|
||||
#include "soc/soc.h" // for BITN definitions
|
||||
#include "freertos/FreeRTOS.h" // for task creation and queues access
|
||||
#include "freertos/event_groups.h" // for event groups
|
||||
#include "esp_modbus_common.h" // for common types
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define MB_SLAVE_CHECK(a, err_code, format, ...) MB_RETURN_ON_FALSE(a, err_code, TAG, format __VA_OPT__(,) __VA_ARGS__)
|
||||
|
||||
#define MB_SLAVE_ASSERT(con) do { \
|
||||
if (!(con)) { ESP_LOGE(TAG, "assert errno:%u, errno_str: !(%s)", (unsigned)errno, strerror(errno)); assert(0 && #con); } \
|
||||
} while (0)
|
||||
|
||||
/**
|
||||
* @brief Parameter access event information type
|
||||
*/
|
||||
typedef struct {
|
||||
uint32_t time_stamp; /*!< Timestamp of Modbus Event (uS)*/
|
||||
uint16_t mb_offset; /*!< Modbus register offset */
|
||||
mb_event_group_t type; /*!< Modbus event type */
|
||||
uint8_t* address; /*!< Modbus data storage address */
|
||||
size_t size; /*!< Modbus event register size (number of registers)*/
|
||||
} mb_param_info_t;
|
||||
|
||||
/**
|
||||
* @brief Parameter storage area descriptor
|
||||
*/
|
||||
typedef struct {
|
||||
uint16_t start_offset; /*!< Modbus start address for area descriptor */
|
||||
mb_param_type_t type; /*!< Type of storage area descriptor */
|
||||
void* address; /*!< Instance address for storage area descriptor */
|
||||
size_t size; /*!< Instance size for area descriptor (bytes) */
|
||||
} mb_register_area_descriptor_t;
|
||||
|
||||
/**
|
||||
* @brief Initialize Modbus Slave controller and stack for TCP port
|
||||
*
|
||||
* @param[out] handler handler(pointer) to master data structure
|
||||
* @return
|
||||
* - ESP_OK Success
|
||||
* - ESP_ERR_NO_MEM Parameter error
|
||||
* - ESP_ERR_NOT_SUPPORTED Port type not supported
|
||||
* - ESP_ERR_INVALID_STATE Initialization failure
|
||||
*/
|
||||
esp_err_t mbc_slave_init_tcp(void** handler);
|
||||
|
||||
/**
|
||||
* @brief Initialize Modbus Slave controller and stack for Serial port
|
||||
*
|
||||
* @param[out] handler handler(pointer) to master data structure
|
||||
* @param[in] port_type the type of port
|
||||
* @return
|
||||
* - ESP_OK Success
|
||||
* - ESP_ERR_NO_MEM Parameter error
|
||||
* - ESP_ERR_NOT_SUPPORTED Port type not supported
|
||||
* - ESP_ERR_INVALID_STATE Initialization failure
|
||||
*/
|
||||
esp_err_t mbc_slave_init(mb_port_type_t port_type, void** handler);
|
||||
|
||||
/**
|
||||
* @brief Initialize Modbus Slave controller interface handle
|
||||
*
|
||||
* @param[in] handler - pointer to slave interface data structure
|
||||
*/
|
||||
void mbc_slave_init_iface(void* handler);
|
||||
|
||||
/**
|
||||
* @brief Destroy Modbus controller and stack
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK Success
|
||||
* - ESP_ERR_INVALID_STATE Parameter error
|
||||
*/
|
||||
esp_err_t mbc_slave_destroy(void);
|
||||
|
||||
/**
|
||||
* @brief Start Modbus communication stack
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK Success
|
||||
* - ESP_ERR_INVALID_ARG Modbus stack start error
|
||||
*/
|
||||
esp_err_t mbc_slave_start(void);
|
||||
|
||||
/**
|
||||
* @brief Set Modbus communication parameters for the controller
|
||||
*
|
||||
* @param comm_info Communication parameters structure.
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK Success
|
||||
* - ESP_ERR_INVALID_ARG Incorrect parameter data
|
||||
*/
|
||||
esp_err_t mbc_slave_setup(void* comm_info);
|
||||
|
||||
/**
|
||||
* @brief Wait for specific event on parameter change.
|
||||
*
|
||||
* @param group Group event bit mask to wait for change
|
||||
*
|
||||
* @return
|
||||
* - mb_event_group_t event bits triggered
|
||||
*/
|
||||
mb_event_group_t mbc_slave_check_event(mb_event_group_t group);
|
||||
|
||||
/**
|
||||
* @brief Get parameter information
|
||||
*
|
||||
* @param[out] reg_info parameter info structure
|
||||
* @param timeout Timeout in milliseconds to read information from
|
||||
* parameter queue
|
||||
* @return
|
||||
* - ESP_OK Success
|
||||
* - ESP_ERR_TIMEOUT Can not get data from parameter queue
|
||||
* or queue overflow
|
||||
*/
|
||||
esp_err_t mbc_slave_get_param_info(mb_param_info_t* reg_info, uint32_t timeout);
|
||||
|
||||
/**
|
||||
* @brief Set Modbus area descriptor
|
||||
*
|
||||
* @param descr_data Modbus registers area descriptor structure
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK: The appropriate descriptor is set
|
||||
* - ESP_ERR_INVALID_ARG: The argument is incorrect
|
||||
*/
|
||||
esp_err_t mbc_slave_set_descriptor(mb_register_area_descriptor_t descr_data);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,556 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2016-2024 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
/**
|
||||
* @brief Defines the constant values based on native compiler byte ordering.
|
||||
*/
|
||||
#define MB_BO16_0 0
|
||||
#define MB_BO16_1 1
|
||||
|
||||
#define MB_BO32_0 0
|
||||
#define MB_BO32_1 1
|
||||
#define MB_BO32_2 2
|
||||
#define MB_BO32_3 3
|
||||
|
||||
#define MB_BO64_0 0
|
||||
#define MB_BO64_1 1
|
||||
#define MB_BO64_2 2
|
||||
#define MB_BO64_3 3
|
||||
#define MB_BO64_4 4
|
||||
#define MB_BO64_5 5
|
||||
#define MB_BO64_6 6
|
||||
#define MB_BO64_7 7
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief The sized array types used for mapping of extended values
|
||||
*/
|
||||
typedef uint8_t val_16_arr[2];
|
||||
typedef uint8_t val_32_arr[4];
|
||||
typedef uint8_t val_64_arr[8];
|
||||
|
||||
/**
|
||||
* @brief Get int8_t (low byte) value represenatation from register
|
||||
*
|
||||
* @return
|
||||
* - int8_t value of converted from register value
|
||||
*/
|
||||
int8_t mb_get_int8_a(val_16_arr *pi16);
|
||||
|
||||
/**
|
||||
* @brief Set i8 value to the register value pointed by pi16
|
||||
*
|
||||
* @return
|
||||
* - uint16_t value which represents the actual hex value of the register
|
||||
*/
|
||||
uint16_t mb_set_int8_a(val_16_arr *pi16, int8_t i8);
|
||||
|
||||
/**
|
||||
* @brief Get int8_t (high byte) value from the register value pointed by pi16
|
||||
*
|
||||
* @return
|
||||
* - uint16_t value which represents the actual hex value of the register
|
||||
*/
|
||||
int8_t mb_get_int8_b(val_16_arr *pi16);
|
||||
|
||||
/**
|
||||
* @brief Set i8 (high byte) value from the register value pointed by pi16
|
||||
*
|
||||
* @return
|
||||
* - uint16_t value which represents the actual hex value of the register
|
||||
*/
|
||||
uint16_t mb_set_int8_b(val_16_arr *pi16, int8_t i8);
|
||||
|
||||
/**
|
||||
* @brief Get uint8_t (low byte) value represenatation from register poined by pu16
|
||||
*
|
||||
* @return
|
||||
* - uint8_t the value of converted from register value
|
||||
*/
|
||||
uint8_t mb_get_uint8_a(val_16_arr *pu16);
|
||||
|
||||
/**
|
||||
* @brief Set u8 (low byte) value into the register value pointed by pu16
|
||||
*
|
||||
* @return
|
||||
* - uint16_t the value which represents the actual hex value of the register
|
||||
*/
|
||||
uint16_t mb_set_uint8_a(val_16_arr *pu16, uint8_t u8);
|
||||
|
||||
/**
|
||||
* @brief Get uint8_t (high byte) value from the register value pointed by pu16
|
||||
*
|
||||
* @return
|
||||
* - uint16_t the value which represents the actual hex value of the register
|
||||
*/
|
||||
uint8_t mb_get_uint8_b(val_16_arr *pu16);
|
||||
|
||||
/**
|
||||
* @brief Set u8 (high byte) value into the register value pointed by pu16
|
||||
*
|
||||
* @return
|
||||
* - uint16_t the value which represents the actual hex value of the register
|
||||
*/
|
||||
uint16_t mb_set_uint8_b(val_16_arr *pu16, uint8_t u8);
|
||||
|
||||
/**
|
||||
* @brief Get int16_t value from the register value pointed by pu16 with ab endianness
|
||||
*
|
||||
* @return
|
||||
* - int16_t the value which represents the converted value from register
|
||||
*/
|
||||
int16_t mb_get_int16_ab(val_16_arr *pi16);
|
||||
|
||||
/**
|
||||
* @brief Set i16 value to the register pointed by pi16 with ab endianness
|
||||
*
|
||||
* @return
|
||||
* - int16_t the value which represents the converted value from register
|
||||
*/
|
||||
uint16_t mb_set_int16_ab(val_16_arr *pi16, int16_t i16);
|
||||
|
||||
/**
|
||||
* @brief Get uint16_t value from the register value pointed by pu16 with ab endianness
|
||||
*
|
||||
* @return
|
||||
* - uint16_t value which represents the converted register value
|
||||
*/
|
||||
uint16_t mb_get_uint16_ab(val_16_arr *pu16);
|
||||
|
||||
/**
|
||||
* @brief Set u16 value to the register pointed by pu16 with ab endianness
|
||||
*
|
||||
* @return
|
||||
* - uint16_t value which represents the converted value from register
|
||||
*/
|
||||
uint16_t mb_set_uint16_ab(val_16_arr *pu16, uint16_t u16);
|
||||
|
||||
/**
|
||||
* @brief Get int16_t value from the register value pointed by pu16 with ba endianness
|
||||
*
|
||||
* @return
|
||||
* - int16_t value which represents the converted register value
|
||||
*/
|
||||
int16_t mb_get_int16_ba(val_16_arr *pi16);
|
||||
|
||||
/**
|
||||
* @brief Set i16 value to the register pointed by pi16 with ba endianness
|
||||
*
|
||||
* @return
|
||||
* - uint16_t value which represents the converted value from register
|
||||
*/
|
||||
uint16_t mb_set_int16_ba(val_16_arr *pi16, int16_t i16);
|
||||
|
||||
/**
|
||||
* @brief Get uint16_t value from the register value pointed by pu16 with ba endianness
|
||||
*
|
||||
* @return
|
||||
* - uint16_t value which represents the converted register value
|
||||
*/
|
||||
uint16_t mb_get_uint16_ba(val_16_arr *pu16);
|
||||
|
||||
/**
|
||||
* @brief Set u16 value to the register pointed by pu16 with ba endianness
|
||||
*
|
||||
* @return
|
||||
* - uint16_t value which represents the converted value from register
|
||||
*/
|
||||
uint16_t mb_set_uint16_ba(val_16_arr *pu16, uint16_t u16);
|
||||
|
||||
/**
|
||||
* @brief Get int32_t value from the register value pointed by pi32 with abcd endianness
|
||||
*
|
||||
* @return
|
||||
* - int32_t value which represents the converted register value
|
||||
*/
|
||||
int32_t mb_get_int32_abcd(val_32_arr *pi32);
|
||||
|
||||
/**
|
||||
* @brief Set i32 value to the register pointed by pi32 with abcd endianness
|
||||
*
|
||||
* @return
|
||||
* - uint32_t value which represents the converted value from register
|
||||
*/
|
||||
uint32_t mb_set_int32_abcd(val_32_arr *pi32, int32_t i32);
|
||||
|
||||
/**
|
||||
* @brief Get uint32_t value from the register value pointed by pu32 with abcd endianness
|
||||
*
|
||||
* @return
|
||||
* - uint32_t value which represents the converted register value
|
||||
*/
|
||||
uint32_t mb_get_uint32_abcd(val_32_arr *pu32);
|
||||
|
||||
/**
|
||||
* @brief Set u32 value to the register pointed by pu32 with abcd endianness
|
||||
*
|
||||
* @return
|
||||
* - uint32_t value which represents the converted value from register
|
||||
*/
|
||||
uint32_t mb_set_uint32_abcd(val_32_arr *pu32, uint32_t u32);
|
||||
|
||||
/**
|
||||
* @brief Get int32_t value from the register value pointed by pi32 with badc endianness
|
||||
*
|
||||
* @return
|
||||
* - int32_t value which represents the converted register value
|
||||
*/
|
||||
int32_t mb_get_int32_badc(val_32_arr *pi32);
|
||||
|
||||
/**
|
||||
* @brief Set i32 value to the register pointed by pi32 with badc endianness
|
||||
*
|
||||
* @return
|
||||
* - uint32_t value which represents the converted value from register
|
||||
*/
|
||||
uint32_t mb_set_int32_badc(val_32_arr *pi32, int32_t i32);
|
||||
|
||||
/**
|
||||
* @brief Get uint32_t value from the register value pointed by pu32 with badc endianness
|
||||
*
|
||||
* @return
|
||||
* - unt32_t value which represents the converted register value
|
||||
*/
|
||||
uint32_t mb_get_uint32_badc(val_32_arr *pu32);
|
||||
|
||||
/**
|
||||
* @brief Set u32 value to the register pointed by pu32 with badc endianness
|
||||
*
|
||||
* @return
|
||||
* - uint32_t value which represents the converted value from register
|
||||
*/
|
||||
uint32_t mb_set_uint32_badc(val_32_arr *pu32, uint32_t u32);
|
||||
|
||||
/**
|
||||
* @brief Get int32_t value from the register value pointed by pi32 with cdab endianness
|
||||
*
|
||||
* @return
|
||||
* - int32_t value which represents the converted register value
|
||||
*/
|
||||
int32_t mb_get_int32_cdab(val_32_arr *pi32);
|
||||
|
||||
/**
|
||||
* @brief Set i32 value to the register pointed by pi32 with cdab endianness
|
||||
*
|
||||
* @return
|
||||
* - uint32_t value which represents the converted value from register
|
||||
*/
|
||||
uint32_t mb_set_int32_cdab(val_32_arr *pi32, int32_t i32);
|
||||
|
||||
/**
|
||||
* @brief Get uint32_t value from the register value pointed by pu32 with cdab endianness
|
||||
*
|
||||
* @return
|
||||
* - int32_t value which represents the converted register value
|
||||
*/
|
||||
uint32_t mb_get_uint32_cdab(val_32_arr *pu32);
|
||||
|
||||
/**
|
||||
* @brief Set u32 value to the register pointed by pu32 with cdab endianness
|
||||
*
|
||||
* @return
|
||||
* - uint32_t value which represents the converted value from register
|
||||
*/
|
||||
uint32_t mb_set_uint32_cdab(val_32_arr *pu32, uint32_t u32);
|
||||
|
||||
/**
|
||||
* @brief Get int32_t value from the register value pointed by pi32 with dcba endianness
|
||||
*
|
||||
* @return
|
||||
* - int32_t value which represents the converted register value
|
||||
*/
|
||||
int32_t mb_get_int32_dcba(val_32_arr *pi32);
|
||||
|
||||
/**
|
||||
* @brief Set i32 value to the register pointed by pi32 with dcba endianness
|
||||
*
|
||||
* @return
|
||||
* - uint32_t value which represents the converted value from register
|
||||
*/
|
||||
uint32_t mb_set_int32_dcba(val_32_arr *pi32, int32_t i32);
|
||||
|
||||
/**
|
||||
* @brief Get uint32_t value from the register value pointed by pu32 with dcba endianness
|
||||
*
|
||||
* @return
|
||||
* - uint32_t value which represents the converted register value
|
||||
*/
|
||||
uint32_t mb_get_uint32_dcba(val_32_arr *pu32);
|
||||
|
||||
/**
|
||||
* @brief Set u32 value to the register pointed by pu32 with dcba endianness
|
||||
*
|
||||
* @return
|
||||
* - uint32_t value which represents the converted value from register
|
||||
*/
|
||||
uint32_t mb_set_uint32_dcba(val_32_arr *pu32, uint32_t u32);
|
||||
|
||||
/**
|
||||
* @brief Get float value from the register pointed by pf with abcd endianness
|
||||
*
|
||||
* @return
|
||||
* - float value which represents the converted register value
|
||||
*/
|
||||
float mb_get_float_abcd(val_32_arr *pf);
|
||||
|
||||
/**
|
||||
* @brief Set f value to the register pointed by pf with abcd endianness
|
||||
*
|
||||
* @return
|
||||
* - uint32_t value which represents the converted value from register
|
||||
*/
|
||||
uint32_t mb_set_float_abcd(val_32_arr *pf, float f);
|
||||
|
||||
/**
|
||||
* @brief Get float value from the register pointed by pf with badc endianness
|
||||
*
|
||||
* @return
|
||||
* - float value which represents the converted register value
|
||||
*/
|
||||
float mb_get_float_badc(val_32_arr *pf);
|
||||
|
||||
/**
|
||||
* @brief Set f value to the register pointed by pf with badc endianness
|
||||
*
|
||||
* @return
|
||||
* - uint32_t value which represents the converted value from register
|
||||
*/
|
||||
uint32_t mb_set_float_badc(val_32_arr *pf, float f);
|
||||
|
||||
/**
|
||||
* @brief Get float value from the register pointed by pf with cdab endianness
|
||||
*
|
||||
* @return
|
||||
* - float value which represents the converted register value
|
||||
*/
|
||||
float mb_get_float_cdab(val_32_arr *pf);
|
||||
|
||||
/**
|
||||
* @brief Set f value to the register pointed by pf with cdab endianness
|
||||
*
|
||||
* @return
|
||||
* - uint32_t value which represents the converted value from register
|
||||
*/
|
||||
uint32_t mb_set_float_cdab(val_32_arr *pf, float f);
|
||||
|
||||
/**
|
||||
* @brief Get float value from the register pointed by pf with dcba endianness
|
||||
*
|
||||
* @return
|
||||
* - float value which represents the converted register value
|
||||
*/
|
||||
float mb_get_float_dcba(val_32_arr *pf);
|
||||
|
||||
/**
|
||||
* @brief Set f value to the register pointed by pf with dcba endianness
|
||||
*
|
||||
* @return
|
||||
* - uint32_t value which represents the converted value from register
|
||||
*/
|
||||
uint32_t mb_set_float_dcba(val_32_arr *pf, float f);
|
||||
|
||||
/**
|
||||
* @brief Get double value from the register pointed by pd with abcdefgh endianness
|
||||
*
|
||||
* @return
|
||||
* - double value which represents the converted register value
|
||||
*/
|
||||
double mb_get_double_abcdefgh(val_64_arr *pd);
|
||||
|
||||
/**
|
||||
* @brief Set d value to the register pointed by pd with abcdefgh endianness
|
||||
*
|
||||
* @return
|
||||
* - uint64_t value which represents the converted value from register
|
||||
*/
|
||||
uint64_t mb_set_double_abcdefgh(val_64_arr *pd, double d);
|
||||
|
||||
/**
|
||||
* @brief Get double value from the register pointed by pd with hgfedcba endianness
|
||||
*
|
||||
* @return
|
||||
* - double value which represents the converted register value
|
||||
*/
|
||||
double mb_get_double_hgfedcba(val_64_arr *pd);
|
||||
|
||||
/**
|
||||
* @brief Set d value to the register pointed by pd with hgfedcba endianness
|
||||
*
|
||||
* @return
|
||||
* - uint64_t value which represents the converted value from register
|
||||
*/
|
||||
uint64_t mb_set_double_hgfedcba(val_64_arr *pd, double d);
|
||||
|
||||
/**
|
||||
* @brief Get double value from the register pointed by pd with ghefcdab endianness
|
||||
*
|
||||
* @return
|
||||
* - double value which represents the converted register value
|
||||
*/
|
||||
double mb_get_double_ghefcdab(val_64_arr *pd);
|
||||
|
||||
/**
|
||||
* @brief Set d value to the register pointed by pd with ghefcdab endianness
|
||||
*
|
||||
* @return
|
||||
* - uint64_t value which represents the converted value from register
|
||||
*/
|
||||
uint64_t mb_set_double_ghefcdab(val_64_arr *pd, double d);
|
||||
|
||||
/**
|
||||
* @brief Get double value from the register pointed by pd with badcfehg endianness
|
||||
*
|
||||
* @return
|
||||
* - double value which represents the converted register value
|
||||
*/
|
||||
double mb_get_double_badcfehg(val_64_arr *pd);
|
||||
|
||||
/**
|
||||
* @brief Set d value to the register pointed by pd with badcfehg endianness
|
||||
*
|
||||
* @return
|
||||
* - uint64_t value which represents the converted value from register
|
||||
*/
|
||||
uint64_t mb_set_double_badcfehg(val_64_arr *pd, double d);
|
||||
|
||||
/**
|
||||
* @brief Get int64_t value from the register pointed by pi64 with abcdefgh endianness
|
||||
*
|
||||
* @return
|
||||
* - int64_t value which represents the converted register value
|
||||
*/
|
||||
int64_t mb_get_int64_abcdefgh(val_64_arr *pi64);
|
||||
|
||||
/**
|
||||
* @brief Set i value to the register pointed by pi with abcdefgh endianness
|
||||
*
|
||||
* @return
|
||||
* - uint64_t value which represents the converted value from register
|
||||
*/
|
||||
uint64_t mb_set_int64_abcdefgh(val_64_arr *pi, int64_t i);
|
||||
|
||||
/**
|
||||
* @brief Get int64_t value from the register pointed by pi64 with ghefcdab endianness
|
||||
*
|
||||
* @return
|
||||
* - int64_t value which represents the converted register value
|
||||
*/
|
||||
int64_t mb_get_int64_ghefcdab(val_64_arr *pi64);
|
||||
|
||||
/**
|
||||
* @brief Set i value to the register pointed by pi with ghefcdab endianness
|
||||
*
|
||||
* @return
|
||||
* - uint64_t value which represents the converted value from register
|
||||
*/
|
||||
uint64_t mb_set_int64_ghefcdab(val_64_arr *pi, int64_t i);
|
||||
|
||||
/**
|
||||
* @brief Get int64_t value from the register pointed by pi64 with hgfedcba endianness
|
||||
*
|
||||
* @return
|
||||
* - int64_t value which represents the converted register value
|
||||
*/
|
||||
int64_t mb_get_int64_hgfedcba(val_64_arr *pi64);
|
||||
|
||||
/**
|
||||
* @brief Set i value to the register pointed by pi with hgfedcba endianness
|
||||
*
|
||||
* @return
|
||||
* - uint64_t value which represents the converted value from register
|
||||
*/
|
||||
uint64_t mb_set_int64_hgfedcba(val_64_arr *pi, int64_t i);
|
||||
|
||||
/**
|
||||
* @brief Get int64_t value from the register pointed by pi64 with badcfehg endianness
|
||||
*
|
||||
* @return
|
||||
* - int64_t value which represents the converted register value
|
||||
*/
|
||||
int64_t mb_get_int64_badcfehg(val_64_arr *pi64);
|
||||
|
||||
/**
|
||||
* @brief Set i value to the register pointed by pi with badcfehg endianness
|
||||
*
|
||||
* @return
|
||||
* - uint64_t value which represents the converted value from register
|
||||
*/
|
||||
uint64_t mb_set_int64_badcfehg(val_64_arr *pi, int64_t i);
|
||||
|
||||
/**
|
||||
* @brief Get uint64_t value from the register pointed by pui with abcdefgh endianness
|
||||
*
|
||||
* @return
|
||||
* - uint64_t value which represents the converted register value
|
||||
*/
|
||||
uint64_t mb_get_uint64_abcdefgh(val_64_arr *pui);
|
||||
|
||||
/**
|
||||
* @brief Set ui value to the register pointed by pi with abcdefgh endianness
|
||||
*
|
||||
* @return
|
||||
* - uint64_t value which represents the converted value from register
|
||||
*/
|
||||
uint64_t mb_set_uint64_abcdefgh(val_64_arr *pui, uint64_t ui);
|
||||
|
||||
/**
|
||||
* @brief Get uint64_t value from the register pointed by pui with hgfedcba endianness
|
||||
*
|
||||
* @return
|
||||
* - uint64_t value which represents the converted register value
|
||||
*/
|
||||
uint64_t mb_get_uint64_hgfedcba(val_64_arr *pui);
|
||||
|
||||
/**
|
||||
* @brief Set ui value to the register pointed by pui with hgfedcba endianness
|
||||
*
|
||||
* @return
|
||||
* - uint64_t value which represents the converted value from register
|
||||
*/
|
||||
uint64_t mb_set_uint64_hgfedcba(val_64_arr *pui, uint64_t ui);
|
||||
|
||||
/**
|
||||
* @brief Get uint64_t value from the register pointed by pui with ghefcdab endianness
|
||||
*
|
||||
* @return
|
||||
* - uint64_t value which represents the converted register value
|
||||
*/
|
||||
uint64_t mb_get_uint64_ghefcdab(val_64_arr *pui);
|
||||
|
||||
/**
|
||||
* @brief Set ui value to the register pointed by pui with ghefcdab endianness
|
||||
*
|
||||
* @return
|
||||
* - uint64_t value which represents the converted value from register
|
||||
*/
|
||||
uint64_t mb_set_uint64_ghefcdab(val_64_arr *pui, uint64_t ui);
|
||||
|
||||
/**
|
||||
* @brief Get uint64_t value from the register pointed by pui with badcfehg endianness
|
||||
*
|
||||
* @return
|
||||
* - uint64_t value which represents the converted register value
|
||||
*/
|
||||
uint64_t mb_get_uint64_badcfehg(val_64_arr *pui);
|
||||
|
||||
/**
|
||||
* @brief Set ui value to the register pointed by pui with badcfehg endianness
|
||||
*
|
||||
* @return
|
||||
* - uint64_t value which represents the converted value from register
|
||||
*/
|
||||
uint64_t mb_set_uint64_badcfehg(val_64_arr *pui, uint64_t ui);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2016-2021 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
// mbcontroller.h
|
||||
// mbcontroller - common Modbus controller header file
|
||||
|
||||
#ifndef _MODBUS_CONTROLLER_COMMON
|
||||
#define _MODBUS_CONTROLLER_COMMON
|
||||
|
||||
#include <stdint.h> // for standard int types definition
|
||||
#include <stddef.h> // for NULL and std defines
|
||||
#include "string.h" // for strerror()
|
||||
#include "errno.h" // for errno
|
||||
#include "esp_err.h" // for error handling
|
||||
#include "driver/uart.h" // for uart port number defines
|
||||
#include "sdkconfig.h" // for KConfig options
|
||||
|
||||
#include "esp_modbus_master.h"
|
||||
#include "esp_modbus_slave.h"
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,683 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2016-2024 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "mb_endianness_utils.h"
|
||||
|
||||
#define INLINE inline __attribute__((always_inline))
|
||||
|
||||
static INLINE int16_t mb_get_int16_generic(int n0, int n1, val_16_arr *psrc)
|
||||
{
|
||||
val_16_arr *pv = psrc;
|
||||
union {
|
||||
val_16_arr arr;
|
||||
int16_t value;
|
||||
} bov;
|
||||
bov.arr[n0] = (*pv)[MB_BO16_0];
|
||||
bov.arr[n1] = (*pv)[MB_BO16_1];
|
||||
return (bov.value);
|
||||
}
|
||||
|
||||
static INLINE uint16_t mb_get_uint16_generic(int n0, int n1, val_16_arr *psrc)
|
||||
{
|
||||
val_16_arr *pv = psrc;
|
||||
union {
|
||||
val_16_arr arr;
|
||||
uint16_t value;
|
||||
} bov;
|
||||
bov.arr[n0] = (*pv)[MB_BO16_0];
|
||||
bov.arr[n1] = (*pv)[MB_BO16_1];
|
||||
return (bov.value);
|
||||
}
|
||||
|
||||
static INLINE uint16_t mb_set_uint16_generic(int n0, int n1, val_16_arr *pdest, uint16_t val)
|
||||
{
|
||||
val_16_arr *pv = pdest;
|
||||
union {
|
||||
val_16_arr arr;
|
||||
uint16_t value;
|
||||
} bov;
|
||||
bov.value = val;
|
||||
(*pv)[MB_BO16_0] = bov.arr[n0];
|
||||
(*pv)[MB_BO16_1] = bov.arr[n1];
|
||||
return (*((uint16_t *)pv));
|
||||
}
|
||||
|
||||
static INLINE int16_t mb_set_int16_generic(int n0, int n1, val_16_arr *pdest, int16_t val)
|
||||
{
|
||||
val_16_arr *pv = pdest;
|
||||
union {
|
||||
val_16_arr arr;
|
||||
int16_t value;
|
||||
} bov;
|
||||
bov.value = val;
|
||||
(*pv)[MB_BO16_0] = bov.arr[n0];
|
||||
(*pv)[MB_BO16_1] = bov.arr[n1];
|
||||
return (*((uint16_t *)pv));
|
||||
}
|
||||
|
||||
static INLINE uint32_t mb_get_uint32_generic(int n0, int n1, int n2, int n3, val_32_arr *psrc)
|
||||
{
|
||||
val_32_arr *pv = psrc;
|
||||
union {
|
||||
val_32_arr arr;
|
||||
uint32_t value;
|
||||
} bov;
|
||||
bov.arr[n0] = (*pv)[MB_BO32_0];
|
||||
bov.arr[n1] = (*pv)[MB_BO32_1];
|
||||
bov.arr[n2] = (*pv)[MB_BO32_2];
|
||||
bov.arr[n3] = (*pv)[MB_BO32_3];
|
||||
return (bov.value);
|
||||
}
|
||||
|
||||
static INLINE int32_t mb_get_int32_generic(int n0, int n1, int n2, int n3, val_32_arr *psrc)
|
||||
{
|
||||
val_32_arr *pv = psrc;
|
||||
union {
|
||||
val_32_arr arr;
|
||||
int32_t value;
|
||||
} bov;
|
||||
bov.arr[n0] = (*pv)[MB_BO32_0];
|
||||
bov.arr[n1] = (*pv)[MB_BO32_1];
|
||||
bov.arr[n2] = (*pv)[MB_BO32_2];
|
||||
bov.arr[n3] = (*pv)[MB_BO32_3];
|
||||
return (bov.value);
|
||||
}
|
||||
|
||||
static INLINE float mb_get_float_generic(int n0, int n1, int n2, int n3, val_32_arr *psrc)
|
||||
{
|
||||
val_32_arr *pv = psrc;
|
||||
union {
|
||||
val_32_arr arr;
|
||||
float value;
|
||||
} bov;
|
||||
bov.arr[n0] = (*pv)[MB_BO32_0];
|
||||
bov.arr[n1] = (*pv)[MB_BO32_1];
|
||||
bov.arr[n2] = (*pv)[MB_BO32_2];
|
||||
bov.arr[n3] = (*pv)[MB_BO32_3];
|
||||
return (bov.value);
|
||||
}
|
||||
|
||||
static INLINE uint32_t mb_set_int32_generic(int n0, int n1, int n2, int n3, val_32_arr *pdest, int32_t val)
|
||||
{
|
||||
val_32_arr *pv = pdest;
|
||||
union {
|
||||
val_32_arr arr;
|
||||
int32_t value;
|
||||
} bov;
|
||||
bov.value = val;
|
||||
(*pv)[MB_BO32_0] = bov.arr[n0];
|
||||
(*pv)[MB_BO32_1] = bov.arr[n1];
|
||||
(*pv)[MB_BO32_2] = bov.arr[n2];
|
||||
(*pv)[MB_BO32_3] = bov.arr[n3];
|
||||
return (*((uint32_t *)pv));
|
||||
}
|
||||
|
||||
static INLINE uint32_t mb_set_uint32_generic(int n0, int n1, int n2, int n3, val_32_arr *pdest, uint32_t val)
|
||||
{
|
||||
val_32_arr *pv = pdest;
|
||||
union {
|
||||
val_32_arr arr;
|
||||
uint32_t value;
|
||||
} bov;
|
||||
bov.value = val;
|
||||
(*pv)[MB_BO32_0] = bov.arr[n0];
|
||||
(*pv)[MB_BO32_1] = bov.arr[n1];
|
||||
(*pv)[MB_BO32_2] = bov.arr[n2];
|
||||
(*pv)[MB_BO32_3] = bov.arr[n3];
|
||||
return (*((uint32_t *)pv));
|
||||
}
|
||||
|
||||
static INLINE uint32_t mb_set_float_generic(int n0, int n1, int n2, int n3, val_32_arr *pdest, float val)
|
||||
{
|
||||
val_32_arr *pv = pdest;
|
||||
union {
|
||||
val_32_arr arr;
|
||||
float value;
|
||||
} bov;
|
||||
bov.value = val;
|
||||
(*pv)[MB_BO32_0] = bov.arr[n0];
|
||||
(*pv)[MB_BO32_1] = bov.arr[n1];
|
||||
(*pv)[MB_BO32_2] = bov.arr[n2];
|
||||
(*pv)[MB_BO32_3] = bov.arr[n3];
|
||||
return (*((uint32_t *)pv));
|
||||
}
|
||||
|
||||
static INLINE int64_t mb_get_int64_generic(int n0, int n1, int n2, int n3, int n4, int n5, int n6, int n7, val_64_arr *psrc)
|
||||
{
|
||||
val_64_arr *pv64 = psrc;
|
||||
union {
|
||||
val_64_arr arr;
|
||||
int64_t value;
|
||||
} bo64;
|
||||
bo64.arr[n0] = (*pv64)[MB_BO64_0];
|
||||
bo64.arr[n1] = (*pv64)[MB_BO64_1];
|
||||
bo64.arr[n2] = (*pv64)[MB_BO64_2];
|
||||
bo64.arr[n3] = (*pv64)[MB_BO64_3];
|
||||
bo64.arr[n4] = (*pv64)[MB_BO64_4];
|
||||
bo64.arr[n5] = (*pv64)[MB_BO64_5];
|
||||
bo64.arr[n6] = (*pv64)[MB_BO64_6];
|
||||
bo64.arr[n7] = (*pv64)[MB_BO64_7];
|
||||
return (bo64.value);
|
||||
}
|
||||
|
||||
static INLINE uint64_t mb_get_uint64_generic(int n0, int n1, int n2, int n3, int n4, int n5, int n6, int n7, val_64_arr *psrc)
|
||||
{
|
||||
val_64_arr *pv64 = psrc;
|
||||
union {
|
||||
val_64_arr arr;
|
||||
uint64_t value;
|
||||
} bo64;
|
||||
bo64.arr[n0] = (*pv64)[MB_BO64_0];
|
||||
bo64.arr[n1] = (*pv64)[MB_BO64_1];
|
||||
bo64.arr[n2] = (*pv64)[MB_BO64_2];
|
||||
bo64.arr[n3] = (*pv64)[MB_BO64_3];
|
||||
bo64.arr[n4] = (*pv64)[MB_BO64_4];
|
||||
bo64.arr[n5] = (*pv64)[MB_BO64_5];
|
||||
bo64.arr[n6] = (*pv64)[MB_BO64_6];
|
||||
bo64.arr[n7] = (*pv64)[MB_BO64_7];
|
||||
return (bo64.value);
|
||||
}
|
||||
|
||||
static INLINE double mb_get_double_generic(int n0, int n1, int n2, int n3, int n4, int n5, int n6, int n7, val_64_arr *psrc)
|
||||
{
|
||||
val_64_arr *pv64 = psrc;
|
||||
union {
|
||||
val_64_arr arr;
|
||||
double value;
|
||||
} bo64;
|
||||
bo64.arr[n0] = (*pv64)[MB_BO64_0];
|
||||
bo64.arr[n1] = (*pv64)[MB_BO64_1];
|
||||
bo64.arr[n2] = (*pv64)[MB_BO64_2];
|
||||
bo64.arr[n3] = (*pv64)[MB_BO64_3];
|
||||
bo64.arr[n4] = (*pv64)[MB_BO64_4];
|
||||
bo64.arr[n5] = (*pv64)[MB_BO64_5];
|
||||
bo64.arr[n6] = (*pv64)[MB_BO64_6];
|
||||
bo64.arr[n7] = (*pv64)[MB_BO64_7];
|
||||
return (bo64.value);
|
||||
}
|
||||
|
||||
static INLINE uint64_t mb_set_int64_generic(int n0, int n1, int n2, int n3, int n4, int n5, int n6, int n7, val_64_arr *pdest, int64_t val)
|
||||
{
|
||||
val_64_arr *pv = pdest;
|
||||
union {
|
||||
val_64_arr arr;
|
||||
int64_t value;
|
||||
} bo64;
|
||||
bo64.value = val;
|
||||
(*pv)[MB_BO64_0] = bo64.arr[n0];
|
||||
(*pv)[MB_BO64_1] = bo64.arr[n1];
|
||||
(*pv)[MB_BO64_2] = bo64.arr[n2];
|
||||
(*pv)[MB_BO64_3] = bo64.arr[n3];
|
||||
(*pv)[MB_BO64_4] = bo64.arr[n4];
|
||||
(*pv)[MB_BO64_5] = bo64.arr[n5];
|
||||
(*pv)[MB_BO64_6] = bo64.arr[n6];
|
||||
(*pv)[MB_BO64_7] = bo64.arr[n7];
|
||||
return (*((uint64_t *)pv));
|
||||
}
|
||||
|
||||
static INLINE uint64_t mb_set_uint64_generic(int n0, int n1, int n2, int n3, int n4, int n5, int n6, int n7, val_64_arr *pdest, uint64_t val)
|
||||
{
|
||||
val_64_arr *pv = pdest;
|
||||
union {
|
||||
val_64_arr arr;
|
||||
uint64_t value;
|
||||
} bo64;
|
||||
bo64.value = val;
|
||||
(*pv)[MB_BO64_0] = bo64.arr[n0];
|
||||
(*pv)[MB_BO64_1] = bo64.arr[n1];
|
||||
(*pv)[MB_BO64_2] = bo64.arr[n2];
|
||||
(*pv)[MB_BO64_3] = bo64.arr[n3];
|
||||
(*pv)[MB_BO64_4] = bo64.arr[n4];
|
||||
(*pv)[MB_BO64_5] = bo64.arr[n5];
|
||||
(*pv)[MB_BO64_6] = bo64.arr[n6];
|
||||
(*pv)[MB_BO64_7] = bo64.arr[n7];
|
||||
return (*((uint64_t *)pv));
|
||||
}
|
||||
|
||||
static INLINE uint64_t mb_set_double_generic(int n0, int n1, int n2, int n3, int n4, int n5, int n6, int n7, val_64_arr *pdest, double val)
|
||||
{
|
||||
val_64_arr *pv = pdest;
|
||||
union {
|
||||
val_64_arr arr;
|
||||
double value;
|
||||
} bo64;
|
||||
bo64.value = val;
|
||||
(*pv)[MB_BO64_0] = bo64.arr[n0];
|
||||
(*pv)[MB_BO64_1] = bo64.arr[n1];
|
||||
(*pv)[MB_BO64_2] = bo64.arr[n2];
|
||||
(*pv)[MB_BO64_3] = bo64.arr[n3];
|
||||
(*pv)[MB_BO64_4] = bo64.arr[n4];
|
||||
(*pv)[MB_BO64_5] = bo64.arr[n5];
|
||||
(*pv)[MB_BO64_6] = bo64.arr[n6];
|
||||
(*pv)[MB_BO64_7] = bo64.arr[n7];
|
||||
return (*((uint64_t *)pv));
|
||||
}
|
||||
|
||||
int8_t mb_get_int8_a(pi16)
|
||||
val_16_arr *pi16;
|
||||
{
|
||||
return((int8_t)(*pi16)[MB_BO16_0]);
|
||||
}
|
||||
|
||||
uint16_t mb_set_int8_a(pi16, i8)
|
||||
val_16_arr *pi16;
|
||||
int8_t i8;
|
||||
{
|
||||
(*pi16)[MB_BO16_0] = (uint8_t)i8;
|
||||
(*pi16)[MB_BO16_1] = 0;
|
||||
return (*((uint16_t *)pi16));
|
||||
}
|
||||
|
||||
int8_t mb_get_int8_b(pi16)
|
||||
val_16_arr *pi16;
|
||||
{
|
||||
return((int8_t)(*pi16)[MB_BO16_1]);
|
||||
}
|
||||
|
||||
uint16_t mb_set_int8_b(pi16, i8)
|
||||
val_16_arr *pi16;
|
||||
int8_t i8;
|
||||
{
|
||||
(*pi16)[MB_BO16_0] = 0;
|
||||
(*pi16)[MB_BO16_1] = (int8_t)i8;
|
||||
return (*((uint16_t *)pi16));
|
||||
}
|
||||
|
||||
uint8_t mb_get_uint8_a(pu16)
|
||||
val_16_arr *pu16;
|
||||
{
|
||||
return((uint8_t)(*pu16)[MB_BO16_0]);
|
||||
}
|
||||
|
||||
uint16_t mb_set_uint8_a(pu16, u8)
|
||||
val_16_arr *pu16;
|
||||
uint8_t u8;
|
||||
{
|
||||
(*pu16)[MB_BO16_0] = (uint8_t)u8;
|
||||
(*pu16)[MB_BO16_1] = 0;
|
||||
return (*((uint16_t *)pu16));
|
||||
}
|
||||
|
||||
uint8_t mb_get_uint8_b(pu16)
|
||||
val_16_arr *pu16;
|
||||
{
|
||||
return((uint8_t)(*pu16)[MB_BO16_1]);
|
||||
}
|
||||
|
||||
uint16_t mb_set_uint8_b(pu16, u8)
|
||||
val_16_arr *pu16;
|
||||
uint8_t u8;
|
||||
{
|
||||
(*pu16)[MB_BO16_0] = 0;
|
||||
(*pu16)[MB_BO16_1] = (uint8_t)u8;
|
||||
return (*((uint16_t *)pu16));
|
||||
}
|
||||
|
||||
int16_t mb_get_int16_ab(pi16)
|
||||
val_16_arr *pi16;
|
||||
{
|
||||
return mb_get_int16_generic(0, 1, pi16);
|
||||
}
|
||||
|
||||
uint16_t mb_set_int16_ab(pi16, i16)
|
||||
val_16_arr *pi16;
|
||||
int16_t i16;
|
||||
{
|
||||
return mb_set_int16_generic(0, 1, pi16, i16);
|
||||
}
|
||||
|
||||
uint16_t mb_get_uint16_ab(pu16)
|
||||
val_16_arr *pu16;
|
||||
{
|
||||
return mb_get_uint16_generic(0, 1, pu16);
|
||||
}
|
||||
|
||||
uint16_t mb_set_uint16_ab(pu16, u16)
|
||||
val_16_arr *pu16;
|
||||
uint16_t u16;
|
||||
{
|
||||
return mb_set_uint16_generic(0, 1, pu16, u16);
|
||||
}
|
||||
|
||||
int16_t mb_get_int16_ba(pi16)
|
||||
val_16_arr *pi16;
|
||||
{
|
||||
return mb_get_int16_generic(1, 0, pi16);
|
||||
}
|
||||
|
||||
uint16_t mb_set_int16_ba(pi16, i16)
|
||||
val_16_arr *pi16;
|
||||
int16_t i16;
|
||||
{
|
||||
return mb_set_int16_generic(1, 0, pi16, i16);
|
||||
}
|
||||
|
||||
uint16_t mb_get_uint16_ba(pu16)
|
||||
val_16_arr *pu16;
|
||||
{
|
||||
return mb_get_int16_generic(1, 0, pu16);
|
||||
}
|
||||
|
||||
uint16_t mb_set_uint16_ba(pu16, u16)
|
||||
val_16_arr *pu16;
|
||||
uint16_t u16;
|
||||
{
|
||||
return mb_set_int16_generic(1, 0, pu16, u16);
|
||||
}
|
||||
|
||||
int32_t mb_get_int32_abcd(pi32)
|
||||
val_32_arr *pi32;
|
||||
{
|
||||
return mb_get_int32_generic(0, 1, 2, 3, pi32);
|
||||
}
|
||||
|
||||
uint32_t mb_set_int32_abcd(pi32, i32)
|
||||
val_32_arr *pi32;
|
||||
int32_t i32;
|
||||
{
|
||||
return mb_set_int32_generic(0, 1, 2, 3, pi32, i32);
|
||||
}
|
||||
|
||||
uint32_t mb_get_uint32_abcd(pu32)
|
||||
val_32_arr *pu32;
|
||||
{
|
||||
return mb_get_uint32_generic(0, 1, 2, 3, pu32);
|
||||
}
|
||||
|
||||
uint32_t mb_set_uint32_abcd(pu32, u32)
|
||||
val_32_arr *pu32;
|
||||
uint32_t u32;
|
||||
{
|
||||
return mb_set_uint32_generic(0, 1, 2, 3, pu32, u32);
|
||||
}
|
||||
|
||||
int32_t mb_get_int32_badc(pi32)
|
||||
val_32_arr *pi32;
|
||||
{
|
||||
return mb_get_int32_generic(1, 0, 3, 2, pi32);
|
||||
}
|
||||
|
||||
uint32_t mb_set_int32_badc(pi32, i32)
|
||||
val_32_arr *pi32;
|
||||
int32_t i32;
|
||||
{
|
||||
return mb_set_int32_generic(1, 0, 3, 2, pi32, i32);
|
||||
}
|
||||
|
||||
uint32_t mb_get_uint32_badc(pu32)
|
||||
val_32_arr *pu32;
|
||||
{
|
||||
return mb_get_uint32_generic(1, 0, 3, 2, pu32);
|
||||
}
|
||||
|
||||
uint32_t mb_set_uint32_badc(pu32, u32)
|
||||
val_32_arr *pu32;
|
||||
uint32_t u32;
|
||||
{
|
||||
return mb_set_uint32_generic(1, 0, 3, 2, pu32, u32);
|
||||
}
|
||||
|
||||
int32_t mb_get_int32_cdab(pi32)
|
||||
val_32_arr *pi32;
|
||||
{
|
||||
return mb_get_int32_generic(2, 3, 0, 1, pi32);
|
||||
}
|
||||
|
||||
uint32_t mb_set_int32_cdab(pi32, i32)
|
||||
val_32_arr *pi32;
|
||||
int32_t i32;
|
||||
{
|
||||
return mb_set_int32_generic(2, 3, 0, 1, pi32, i32);
|
||||
}
|
||||
|
||||
uint32_t mb_get_uint32_cdab(pu32)
|
||||
val_32_arr *pu32;
|
||||
{
|
||||
return mb_get_uint32_generic(2, 3, 0, 1, pu32);
|
||||
}
|
||||
|
||||
uint32_t mb_set_uint32_cdab(pu32, u32)
|
||||
val_32_arr *pu32;
|
||||
uint32_t u32;
|
||||
{
|
||||
return mb_set_uint32_generic(2, 3, 0, 1, pu32, u32);
|
||||
}
|
||||
|
||||
int32_t mb_get_int32_dcba(pi32)
|
||||
val_32_arr *pi32;
|
||||
{
|
||||
return mb_get_int32_generic(3, 2, 1, 0, pi32);
|
||||
}
|
||||
|
||||
uint32_t mb_set_int32_dcba(pi32, i32)
|
||||
val_32_arr *pi32;
|
||||
int32_t i32;
|
||||
{
|
||||
return mb_set_int32_generic(3, 2, 1, 0, pi32, i32);
|
||||
}
|
||||
|
||||
uint32_t mb_get_uint32_dcba(pu32)
|
||||
val_32_arr *pu32;
|
||||
{
|
||||
return mb_get_uint32_generic(3, 2, 1, 0, pu32);
|
||||
}
|
||||
|
||||
uint32_t mb_set_uint32_dcba(pu32, u32)
|
||||
val_32_arr *pu32;
|
||||
uint32_t u32;
|
||||
{
|
||||
return mb_set_uint32_generic(3, 2, 1, 0, pu32, u32);
|
||||
}
|
||||
|
||||
float mb_get_float_abcd(pf)
|
||||
val_32_arr *pf;
|
||||
{
|
||||
return mb_get_float_generic(0, 1, 2, 3, pf);
|
||||
}
|
||||
|
||||
uint32_t mb_set_float_abcd(pf, f)
|
||||
val_32_arr *pf;
|
||||
float f;
|
||||
{
|
||||
return mb_set_float_generic(0, 1, 2, 3, pf, f);
|
||||
}
|
||||
|
||||
float mb_get_float_badc(pf)
|
||||
val_32_arr *pf;
|
||||
{
|
||||
return mb_get_float_generic(1, 0, 3, 2, pf);
|
||||
}
|
||||
|
||||
uint32_t mb_set_float_badc(pf, f)
|
||||
val_32_arr *pf;
|
||||
float f;
|
||||
{
|
||||
return mb_set_float_generic(1, 0, 3, 2, pf, f);
|
||||
}
|
||||
|
||||
float mb_get_float_cdab(pf)
|
||||
val_32_arr *pf;
|
||||
{
|
||||
return mb_get_float_generic(2, 3, 0, 1, pf);
|
||||
}
|
||||
|
||||
uint32_t mb_set_float_cdab(pf, f)
|
||||
val_32_arr *pf;
|
||||
float f;
|
||||
{
|
||||
return mb_set_float_generic(2, 3, 0, 1, pf, f);
|
||||
}
|
||||
|
||||
float mb_get_float_dcba(pf)
|
||||
val_32_arr *pf;
|
||||
{
|
||||
return mb_get_float_generic(3, 2, 1, 0, pf);
|
||||
}
|
||||
|
||||
uint32_t mb_set_float_dcba(pf, f)
|
||||
val_32_arr *pf;
|
||||
float f;
|
||||
{
|
||||
return mb_set_float_generic(3, 2, 1, 0, pf, f);
|
||||
}
|
||||
|
||||
double mb_get_double_abcdefgh(pd)
|
||||
val_64_arr *pd;
|
||||
{
|
||||
return mb_get_double_generic(0, 1, 2, 3, 4, 5, 6, 7, pd);
|
||||
}
|
||||
|
||||
uint64_t mb_set_double_abcdefgh(pd, d)
|
||||
val_64_arr *pd;
|
||||
double d;
|
||||
{
|
||||
return mb_set_double_generic(0, 1, 2, 3, 4, 5, 6, 7, pd, d);
|
||||
}
|
||||
|
||||
double mb_get_double_hgfedcba(pd)
|
||||
val_64_arr *pd;
|
||||
{
|
||||
return mb_get_double_generic(7, 6, 5, 4, 3, 2, 1, 0, pd);
|
||||
}
|
||||
|
||||
uint64_t mb_set_double_hgfedcba(pd, d)
|
||||
val_64_arr *pd;
|
||||
double d;
|
||||
{
|
||||
return mb_set_double_generic(7, 6, 5, 4, 3, 2, 1, 0, pd, d);
|
||||
}
|
||||
|
||||
double mb_get_double_ghefcdab(pd)
|
||||
val_64_arr *pd;
|
||||
{
|
||||
return mb_get_double_generic(6, 7, 4, 5, 2, 3, 0, 1, pd);
|
||||
}
|
||||
|
||||
uint64_t mb_set_double_ghefcdab(pd, d)
|
||||
val_64_arr *pd;
|
||||
double d;
|
||||
{
|
||||
return mb_set_double_generic(6, 7, 4, 5, 2, 3, 0, 1, pd, d);
|
||||
}
|
||||
|
||||
double mb_get_double_badcfehg(pd)
|
||||
val_64_arr *pd;
|
||||
{
|
||||
return mb_get_double_generic(1, 0, 3, 2, 5, 4, 7, 6, pd);
|
||||
}
|
||||
|
||||
uint64_t mb_set_double_badcfehg(pd, d)
|
||||
val_64_arr *pd;
|
||||
double d;
|
||||
{
|
||||
return mb_set_double_generic(1, 0, 3, 2, 5, 4, 7, 6, pd, d);
|
||||
}
|
||||
|
||||
int64_t mb_get_int64_abcdefgh(pi64)
|
||||
val_64_arr *pi64;
|
||||
{
|
||||
return mb_get_int64_generic(0, 1, 2, 3, 4, 5, 6, 7, pi64);
|
||||
}
|
||||
|
||||
uint64_t mb_set_int64_abcdefgh(pi, i)
|
||||
val_64_arr *pi;
|
||||
int64_t i;
|
||||
{
|
||||
return mb_set_int64_generic(0, 1, 2, 3, 4, 5, 6, 7, pi, i);
|
||||
}
|
||||
|
||||
int64_t mb_get_int64_hgfedcba(pi64)
|
||||
val_64_arr *pi64;
|
||||
{
|
||||
return mb_get_int64_generic(7, 6, 5, 4, 3, 2, 1, 0, pi64);
|
||||
}
|
||||
|
||||
uint64_t mb_set_int64_hgfedcba(pi, i)
|
||||
val_64_arr *pi;
|
||||
int64_t i;
|
||||
{
|
||||
return mb_set_int64_generic(7, 6, 5, 4, 3, 2, 1, 0, pi, i);
|
||||
}
|
||||
|
||||
int64_t mb_get_int64_ghefcdab(pi64)
|
||||
val_64_arr *pi64;
|
||||
{
|
||||
return mb_get_int64_generic(6, 7, 4, 5, 2, 3, 0, 1, pi64);
|
||||
}
|
||||
|
||||
uint64_t mb_set_int64_ghefcdab(pi, i)
|
||||
val_64_arr *pi;
|
||||
int64_t i;
|
||||
{
|
||||
return mb_set_int64_generic(6, 7, 4, 5, 2, 3, 0, 1, pi, i);
|
||||
}
|
||||
|
||||
int64_t mb_get_int64_badcfehg(pi64)
|
||||
val_64_arr *pi64;
|
||||
{
|
||||
return mb_get_int64_generic(1, 0, 3, 2, 5, 4, 7, 6, pi64);
|
||||
}
|
||||
|
||||
uint64_t mb_set_int64_badcfehg(pi, i)
|
||||
val_64_arr *pi;
|
||||
int64_t i;
|
||||
{
|
||||
return mb_set_int64_generic(1, 0, 3, 2, 5, 4, 7, 6, pi, i);
|
||||
}
|
||||
|
||||
uint64_t mb_get_uint64_abcdefgh(pui)
|
||||
val_64_arr *pui;
|
||||
{
|
||||
return mb_get_uint64_generic(0, 1, 2, 3, 4, 5, 6, 7, pui);
|
||||
}
|
||||
|
||||
uint64_t mb_set_uint64_abcdefgh(pui, ui)
|
||||
val_64_arr *pui;
|
||||
uint64_t ui;
|
||||
{
|
||||
return mb_set_uint64_generic(0, 1, 2, 3, 4, 5, 6, 7, pui, ui);
|
||||
}
|
||||
|
||||
uint64_t mb_get_uint64_hgfedcba(pui)
|
||||
val_64_arr *pui;
|
||||
{
|
||||
return mb_get_uint64_generic(7, 6, 5, 4, 3, 2, 1, 0, pui);
|
||||
}
|
||||
|
||||
uint64_t mb_set_uint64_hgfedcba(pui, ui)
|
||||
val_64_arr *pui;
|
||||
uint64_t ui;
|
||||
{
|
||||
return mb_set_uint64_generic(7, 6, 5, 4, 3, 2, 1, 0, pui, ui);
|
||||
}
|
||||
|
||||
uint64_t mb_get_uint64_ghefcdab(pui)
|
||||
val_64_arr *pui;
|
||||
{
|
||||
return mb_get_uint64_generic(6, 7, 4, 5, 2, 3, 0, 1, pui);
|
||||
}
|
||||
|
||||
uint64_t mb_set_uint64_ghefcdab(pui, ui)
|
||||
val_64_arr *pui;
|
||||
uint64_t ui;
|
||||
{
|
||||
return mb_set_uint64_generic(6, 7, 4, 5, 2, 3, 0, 1, pui, ui);
|
||||
}
|
||||
|
||||
uint64_t mb_get_uint64_badcfehg(pui)
|
||||
val_64_arr *pui;
|
||||
{
|
||||
return mb_get_int64_generic(1, 0, 3, 2, 5, 4, 7, 6, pui);
|
||||
}
|
||||
|
||||
uint64_t mb_set_uint64_badcfehg(pui, ui)
|
||||
val_64_arr *pui;
|
||||
uint64_t ui;
|
||||
{
|
||||
return mb_set_uint64_generic(1, 0, 3, 2, 5, 4, 7, 6, pui, ui);
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2016-2022 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#ifndef _MB_CONTROLLER_MASTER_H
|
||||
#define _MB_CONTROLLER_MASTER_H
|
||||
|
||||
#include <sys/queue.h> // for list
|
||||
#include "freertos/FreeRTOS.h" // for task creation and queue access
|
||||
#include "freertos/task.h" // for task api access
|
||||
#include "freertos/event_groups.h" // for event groups
|
||||
#include "driver/uart.h" // for UART types
|
||||
#include "errno.h" // for errno
|
||||
#include "esp_log.h" // for log write
|
||||
#include "string.h" // for strerror()
|
||||
#include "esp_modbus_common.h" // for common types
|
||||
#include "esp_modbus_master.h" // for public master types
|
||||
#include "esp_modbus_callbacks.h"
|
||||
#include "mb_m.h" // this is required to expose current transaction info
|
||||
|
||||
/* ----------------------- Defines ------------------------------------------*/
|
||||
|
||||
/**
|
||||
* @brief Request mode for parameter to use in data dictionary
|
||||
*/
|
||||
typedef enum {
|
||||
MB_PARAM_READ, /*!< Read parameter values. */
|
||||
MB_PARAM_WRITE /*!< Write parameter values. */
|
||||
} mb_param_mode_t;
|
||||
|
||||
/**
|
||||
* @brief Device communication parameters for master
|
||||
*/
|
||||
typedef struct {
|
||||
mb_mode_type_t mode; /*!< Modbus communication mode */
|
||||
uint8_t dummy; /*!< Dummy field */
|
||||
uart_port_t port; /*!< Modbus communication port (UART) number */
|
||||
uint32_t baudrate; /*!< Modbus baudrate */
|
||||
uart_parity_t parity; /*!< Modbus UART parity settings */
|
||||
} mb_master_comm_info_t;
|
||||
|
||||
#if MB_MASTER_TCP_ENABLED
|
||||
/**
|
||||
* @brief Modbus slave addr list item for the master
|
||||
*/
|
||||
typedef struct mb_slave_addr_entry_s{
|
||||
uint16_t index; /*!< Index of the slave address */
|
||||
const char* ip_address; /*!< IP address string of the slave */
|
||||
uint8_t slave_addr; /*!< Short slave address */
|
||||
void* p_data; /*!< pointer to data structure */
|
||||
LIST_ENTRY(mb_slave_addr_entry_s) entries; /*!< The slave address entry */
|
||||
} mb_slave_addr_entry_t;
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Modbus controller handler structure
|
||||
*/
|
||||
typedef struct {
|
||||
mb_port_type_t port_type; /*!< Modbus port type */
|
||||
mb_communication_info_t mbm_comm; /*!< Modbus communication info */
|
||||
uint8_t* mbm_reg_buffer_ptr; /*!< Modbus data buffer pointer */
|
||||
uint16_t mbm_reg_buffer_size; /*!< Modbus data buffer size */
|
||||
TaskHandle_t mbm_task_handle; /*!< Modbus task handle */
|
||||
EventGroupHandle_t mbm_event_group; /*!< Modbus controller event group */
|
||||
const mb_parameter_descriptor_t* mbm_param_descriptor_table; /*!< Modbus controller parameter description table */
|
||||
size_t mbm_param_descriptor_size; /*!< Modbus controller parameter description table size*/
|
||||
#if MB_MASTER_TCP_ENABLED
|
||||
LIST_HEAD(mbm_slave_addr_info_, mb_slave_addr_entry_s) mbm_slave_list; /*!< Slave address information list */
|
||||
uint16_t mbm_slave_list_count;
|
||||
#endif
|
||||
} mb_master_options_t;
|
||||
|
||||
typedef esp_err_t (*iface_get_cid_info)(uint16_t, const mb_parameter_descriptor_t**); /*!< Interface get_cid_info method */
|
||||
typedef esp_err_t (*iface_get_parameter)(uint16_t, char*, uint8_t*, uint8_t*); /*!< Interface get_parameter method */
|
||||
typedef esp_err_t (*iface_send_request)(mb_param_request_t*, void*); /*!< Interface send_request method */
|
||||
typedef esp_err_t (*iface_set_descriptor)(const mb_parameter_descriptor_t*, const uint16_t); /*!< Interface set_descriptor method */
|
||||
typedef esp_err_t (*iface_set_parameter)(uint16_t, char*, uint8_t*, uint8_t*); /*!< Interface set_parameter method */
|
||||
|
||||
/**
|
||||
* @brief Modbus controller interface structure
|
||||
*/
|
||||
typedef struct {
|
||||
// Master object interface options
|
||||
mb_master_options_t opts;
|
||||
|
||||
// Public interface methods
|
||||
iface_init init; /*!< Interface method init */
|
||||
iface_destroy destroy; /*!< Interface method destroy */
|
||||
iface_setup setup; /*!< Interface method setup */
|
||||
iface_start start; /*!< Interface method start */
|
||||
iface_get_cid_info get_cid_info; /*!< Interface get_cid_info method */
|
||||
iface_get_parameter get_parameter; /*!< Interface get_parameter method */
|
||||
iface_send_request send_request; /*!< Interface send_request method */
|
||||
iface_set_descriptor set_descriptor; /*!< Interface set_descriptor method */
|
||||
iface_set_parameter set_parameter; /*!< Interface set_parameter method */
|
||||
// Modbus register calback function pointers
|
||||
reg_discrete_cb master_reg_cb_discrete; /*!< Stack callback discrete rw method */
|
||||
reg_input_cb master_reg_cb_input; /*!< Stack callback input rw method */
|
||||
reg_holding_cb master_reg_cb_holding; /*!< Stack callback holding rw method */
|
||||
reg_coils_cb master_reg_cb_coils; /*!< Stack callback coils rw method */
|
||||
} mb_master_interface_t;
|
||||
|
||||
#endif //_MB_CONTROLLER_MASTER_H
|
||||
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2016-2022 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#ifndef _MB_CONTROLLER_SLAVE_H
|
||||
#define _MB_CONTROLLER_SLAVE_H
|
||||
|
||||
#include "driver/uart.h" // for uart defines
|
||||
#include "errno.h" // for errno
|
||||
#include "sys/queue.h" // for list
|
||||
#include "esp_log.h" // for log write
|
||||
#include "string.h" // for strerror()
|
||||
|
||||
#include "esp_modbus_slave.h" // for public type defines
|
||||
#include "esp_modbus_callbacks.h" // for callback functions
|
||||
|
||||
/* ----------------------- Defines ------------------------------------------*/
|
||||
#define MB_INST_MIN_SIZE (2) // The minimal size of Modbus registers area in bytes
|
||||
#define MB_INST_MAX_SIZE (65535 * 2) // The maximum size of Modbus area in bytes
|
||||
|
||||
#define MB_CONTROLLER_NOTIFY_QUEUE_SIZE (CONFIG_FMB_CONTROLLER_NOTIFY_QUEUE_SIZE) // Number of messages in parameter notification queue
|
||||
#define MB_CONTROLLER_NOTIFY_TIMEOUT (pdMS_TO_TICKS(CONFIG_FMB_CONTROLLER_NOTIFY_TIMEOUT)) // notification timeout
|
||||
|
||||
/**
|
||||
* @brief Device communication parameters for master
|
||||
*/
|
||||
typedef struct {
|
||||
mb_mode_type_t mode; /*!< Modbus communication mode */
|
||||
uint8_t slave_addr; /*!< Slave address field */
|
||||
uart_port_t port; /*!< Modbus communication port (UART) number */
|
||||
uint32_t baudrate; /*!< Modbus baudrate */
|
||||
uart_parity_t parity; /*!< Modbus UART parity settings */
|
||||
} mb_slave_comm_info_t;
|
||||
|
||||
/**
|
||||
* @brief Modbus area descriptor list item
|
||||
*/
|
||||
typedef struct mb_descr_entry_s{
|
||||
uint16_t start_offset; /*!< Modbus start address for area descriptor */
|
||||
mb_param_type_t type; /*!< Type of storage area descriptor */
|
||||
void* p_data; /*!< Instance address for storage area descriptor */
|
||||
size_t size; /*!< Instance size for area descriptor (bytes) */
|
||||
LIST_ENTRY(mb_descr_entry_s) entries; /*!< The Modbus area descriptor entry */
|
||||
} mb_descr_entry_t;
|
||||
|
||||
/**
|
||||
* @brief Modbus controller handler structure
|
||||
*/
|
||||
typedef struct {
|
||||
mb_port_type_t port_type; /*!< port type */
|
||||
mb_communication_info_t mbs_comm; /*!< communication info */
|
||||
TaskHandle_t mbs_task_handle; /*!< task handle */
|
||||
EventGroupHandle_t mbs_event_group; /*!< controller event group */
|
||||
QueueHandle_t mbs_notification_queue_handle; /*!< controller notification queue */
|
||||
LIST_HEAD(mbs_area_descriptors_, mb_descr_entry_s) mbs_area_descriptors[MB_PARAM_COUNT]; /*!< register area descriptors */
|
||||
} mb_slave_options_t;
|
||||
|
||||
typedef mb_event_group_t (*iface_check_event)(mb_event_group_t); /*!< Interface method check_event */
|
||||
typedef esp_err_t (*iface_get_param_info)(mb_param_info_t*, uint32_t); /*!< Interface method get_param_info */
|
||||
typedef esp_err_t (*iface_set_descriptor)(mb_register_area_descriptor_t); /*!< Interface method set_descriptor */
|
||||
|
||||
/**
|
||||
* @brief Request mode for parameter to use in data dictionary
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
mb_slave_options_t opts; /*!< Modbus slave options */
|
||||
|
||||
// Functional pointers to internal static functions of the implementation (public interface methods)
|
||||
iface_init init; /*!< Interface method init */
|
||||
iface_destroy destroy; /*!< Interface method destroy */
|
||||
iface_setup setup; /*!< Interface method setup */
|
||||
iface_start start; /*!< Interface method start */
|
||||
iface_check_event check_event; /*!< Interface method check_event */
|
||||
iface_get_param_info get_param_info; /*!< Interface method get_param_info */
|
||||
iface_set_descriptor set_descriptor; /*!< Interface method set_descriptor */
|
||||
|
||||
// Modbus register calback function pointers
|
||||
reg_discrete_cb slave_reg_cb_discrete; /*!< Stack callback discrete rw method */
|
||||
reg_input_cb slave_reg_cb_input; /*!< Stack callback input rw method */
|
||||
reg_holding_cb slave_reg_cb_holding; /*!< Stack callback holding rw method */
|
||||
reg_coils_cb slave_reg_cb_coils; /*!< Stack callback coils rw method */
|
||||
} mb_slave_interface_t;
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,495 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2006 Christian Walter
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*
|
||||
* SPDX-FileContributor: 2016-2021 Espressif Systems (Shanghai) CO LTD
|
||||
*/
|
||||
/*
|
||||
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
|
||||
* Copyright (c) 2006 Christian Walter <wolti@sil.at>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* File: $Id: mbascii.c,v 1.17 2010/06/06 13:47:07 wolti Exp $
|
||||
*/
|
||||
|
||||
/* ----------------------- System includes ----------------------------------*/
|
||||
#include "stdlib.h"
|
||||
#include "string.h"
|
||||
|
||||
/* ----------------------- Platform includes --------------------------------*/
|
||||
#include "port.h"
|
||||
|
||||
/* ----------------------- Modbus includes ----------------------------------*/
|
||||
#include "mb.h"
|
||||
#include "mbconfig.h"
|
||||
#include "mbascii.h"
|
||||
#include "mbframe.h"
|
||||
|
||||
#include "mbcrc.h"
|
||||
#include "mbport.h"
|
||||
|
||||
#if MB_SLAVE_ASCII_ENABLED > 0
|
||||
|
||||
/* ----------------------- Type definitions ---------------------------------*/
|
||||
typedef enum
|
||||
{
|
||||
STATE_RX_IDLE, /*!< Receiver is in idle state. */
|
||||
STATE_RX_RCV, /*!< Frame is beeing received. */
|
||||
STATE_RX_WAIT_EOF /*!< Wait for End of Frame. */
|
||||
} eMBRcvState;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
STATE_TX_IDLE, /*!< Transmitter is in idle state. */
|
||||
STATE_TX_START, /*!< Starting transmission (':' sent). */
|
||||
STATE_TX_DATA, /*!< Sending of data (Address, Data, LRC). */
|
||||
STATE_TX_END, /*!< End of transmission. */
|
||||
STATE_TX_NOTIFY /*!< Notify sender that the frame has been sent. */
|
||||
} eMBSndState;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
BYTE_HIGH_NIBBLE, /*!< Character for high nibble of byte. */
|
||||
BYTE_LOW_NIBBLE /*!< Character for low nibble of byte. */
|
||||
} eMBBytePos;
|
||||
|
||||
/* ----------------------- Shared variables ---------------------------------*/
|
||||
/* We reuse the Modbus RTU buffer because only one driver is active */
|
||||
extern volatile UCHAR ucMbSlaveBuf[];
|
||||
|
||||
/* ----------------------- Static functions ---------------------------------*/
|
||||
static UCHAR prvucMBCHAR2BIN( UCHAR ucCharacter );
|
||||
|
||||
static UCHAR prvucMBBIN2CHAR( UCHAR ucByte );
|
||||
|
||||
static UCHAR prvucMBLRC( UCHAR * pucFrame, USHORT usLen );
|
||||
|
||||
/* ----------------------- Static variables ---------------------------------*/
|
||||
static volatile eMBSndState eSndState;
|
||||
static volatile eMBRcvState eRcvState;
|
||||
|
||||
static volatile UCHAR *ucASCIIBuf = ucMbSlaveBuf;
|
||||
|
||||
static volatile USHORT usRcvBufferPos;
|
||||
static volatile eMBBytePos eBytePos;
|
||||
|
||||
static volatile UCHAR *pucSndBufferCur;
|
||||
static volatile USHORT usSndBufferCount;
|
||||
|
||||
static volatile UCHAR ucLRC;
|
||||
static volatile UCHAR ucMBLFCharacter;
|
||||
|
||||
/* ----------------------- Start implementation -----------------------------*/
|
||||
eMBErrorCode
|
||||
eMBASCIIInit( UCHAR ucSlaveAddress, UCHAR ucPort, ULONG ulBaudRate, eMBParity eParity )
|
||||
{
|
||||
eMBErrorCode eStatus = MB_ENOERR;
|
||||
( void )ucSlaveAddress;
|
||||
|
||||
ENTER_CRITICAL_SECTION( );
|
||||
ucMBLFCharacter = MB_ASCII_DEFAULT_LF;
|
||||
|
||||
if( xMBPortSerialInit( ucPort, ulBaudRate, MB_ASCII_BITS_PER_SYMB, eParity ) != TRUE )
|
||||
{
|
||||
eStatus = MB_EPORTERR;
|
||||
}
|
||||
else if( xMBPortTimersInit( MB_ASCII_TIMEOUT_MS * 20UL ) != TRUE )
|
||||
{
|
||||
eStatus = MB_EPORTERR;
|
||||
}
|
||||
|
||||
EXIT_CRITICAL_SECTION( );
|
||||
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
void
|
||||
eMBASCIIStart( void )
|
||||
{
|
||||
ENTER_CRITICAL_SECTION( );
|
||||
vMBPortSerialEnable( TRUE, FALSE );
|
||||
eRcvState = STATE_RX_IDLE;
|
||||
EXIT_CRITICAL_SECTION( );
|
||||
|
||||
/* No special startup required for ASCII. */
|
||||
( void )xMBPortEventPost( EV_READY );
|
||||
}
|
||||
|
||||
void
|
||||
eMBASCIIStop( void )
|
||||
{
|
||||
ENTER_CRITICAL_SECTION( );
|
||||
vMBPortSerialEnable( FALSE, FALSE );
|
||||
vMBPortTimersDisable( );
|
||||
EXIT_CRITICAL_SECTION( );
|
||||
}
|
||||
|
||||
eMBErrorCode
|
||||
eMBASCIIReceive( UCHAR * pucRcvAddress, UCHAR ** pucFrame, USHORT * pusLength )
|
||||
{
|
||||
eMBErrorCode eStatus = MB_ENOERR;
|
||||
UCHAR *pucMBASCIIFrame = ( UCHAR* ) ucASCIIBuf;
|
||||
USHORT usFrameLength = usRcvBufferPos;
|
||||
|
||||
if( xMBPortSerialGetRequest( &pucMBASCIIFrame, &usFrameLength ) == FALSE )
|
||||
{
|
||||
return MB_EIO;
|
||||
}
|
||||
|
||||
ENTER_CRITICAL_SECTION( );
|
||||
assert( usFrameLength < MB_SER_PDU_SIZE_MAX );
|
||||
|
||||
/* Length and CRC check */
|
||||
if( ( usFrameLength >= MB_ASCII_SER_PDU_SIZE_MIN )
|
||||
&& ( prvucMBLRC( ( UCHAR * ) pucMBASCIIFrame, usFrameLength ) == 0 ) )
|
||||
{
|
||||
/* Save the address field. All frames are passed to the upper layed
|
||||
* and the decision if a frame is used is done there.
|
||||
*/
|
||||
*pucRcvAddress = pucMBASCIIFrame[MB_SER_PDU_ADDR_OFF];
|
||||
|
||||
/* Total length of Modbus-PDU is Modbus-Serial-Line-PDU minus
|
||||
* size of address field and CRC checksum.
|
||||
*/
|
||||
*pusLength = ( USHORT )( usFrameLength - MB_SER_PDU_PDU_OFF - MB_SER_PDU_SIZE_LRC );
|
||||
|
||||
/* Return the start of the Modbus PDU to the caller. */
|
||||
*pucFrame = ( UCHAR * ) & pucMBASCIIFrame[MB_SER_PDU_PDU_OFF];
|
||||
}
|
||||
else
|
||||
{
|
||||
eStatus = MB_EIO;
|
||||
}
|
||||
EXIT_CRITICAL_SECTION( );
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
eMBErrorCode
|
||||
eMBASCIISend( UCHAR ucSlaveAddress, const UCHAR * pucFrame, USHORT usLength )
|
||||
{
|
||||
eMBErrorCode eStatus = MB_ENOERR;
|
||||
UCHAR usLRC;
|
||||
|
||||
|
||||
/* Check if the receiver is still in idle state. If not we where too
|
||||
* slow with processing the received frame and the master sent another
|
||||
* frame on the network. We have to abort sending the frame.
|
||||
*/
|
||||
if( eRcvState == STATE_RX_IDLE )
|
||||
{
|
||||
ENTER_CRITICAL_SECTION( );
|
||||
/* First byte before the Modbus-PDU is the slave address. */
|
||||
pucSndBufferCur = ( UCHAR * ) pucFrame - 1;
|
||||
usSndBufferCount = 1;
|
||||
|
||||
/* Now copy the Modbus-PDU into the Modbus-Serial-Line-PDU. */
|
||||
pucSndBufferCur[MB_SER_PDU_ADDR_OFF] = ucSlaveAddress;
|
||||
usSndBufferCount += usLength;
|
||||
|
||||
/* Calculate LRC checksum for Modbus-Serial-Line-PDU. */
|
||||
usLRC = prvucMBLRC( ( UCHAR * ) pucSndBufferCur, usSndBufferCount );
|
||||
ucASCIIBuf[usSndBufferCount++] = usLRC;
|
||||
|
||||
/* Activate the transmitter. */
|
||||
eSndState = STATE_TX_START;
|
||||
EXIT_CRITICAL_SECTION( );
|
||||
|
||||
if ( xMBPortSerialSendResponse( ( UCHAR * ) pucSndBufferCur, usSndBufferCount ) == FALSE )
|
||||
{
|
||||
eStatus = MB_EIO;
|
||||
}
|
||||
|
||||
vMBPortSerialEnable( FALSE, TRUE );
|
||||
}
|
||||
else
|
||||
{
|
||||
eStatus = MB_EIO;
|
||||
}
|
||||
EXIT_CRITICAL_SECTION( );
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
BOOL
|
||||
xMBASCIIReceiveFSM( void )
|
||||
{
|
||||
BOOL xNeedPoll = FALSE;
|
||||
UCHAR ucByte;
|
||||
UCHAR ucResult;
|
||||
|
||||
assert( eSndState == STATE_TX_IDLE );
|
||||
|
||||
xNeedPoll = xMBPortSerialGetByte( ( CHAR * ) & ucByte );
|
||||
switch ( eRcvState )
|
||||
{
|
||||
/* A new character is received. If the character is a ':' the input
|
||||
* buffer is cleared. A CR-character signals the end of the data
|
||||
* block. Other characters are part of the data block and their
|
||||
* ASCII value is converted back to a binary representation.
|
||||
*/
|
||||
case STATE_RX_RCV:
|
||||
/* Enable timer for character timeout. */
|
||||
vMBPortTimersEnable( );
|
||||
if( ucByte == ':' )
|
||||
{
|
||||
/* Empty receive buffer. */
|
||||
eBytePos = BYTE_HIGH_NIBBLE;
|
||||
usRcvBufferPos = 0;
|
||||
}
|
||||
else if( ucByte == MB_ASCII_DEFAULT_CR )
|
||||
{
|
||||
eRcvState = STATE_RX_WAIT_EOF;
|
||||
}
|
||||
else
|
||||
{
|
||||
ucResult = prvucMBCHAR2BIN( ucByte );
|
||||
switch ( eBytePos )
|
||||
{
|
||||
/* High nibble of the byte comes first. We check for
|
||||
* a buffer overflow here. */
|
||||
case BYTE_HIGH_NIBBLE:
|
||||
if( usRcvBufferPos < MB_SER_PDU_SIZE_MAX )
|
||||
{
|
||||
ucASCIIBuf[usRcvBufferPos] = ( UCHAR )( ucResult << 4 );
|
||||
eBytePos = BYTE_LOW_NIBBLE;
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* not handled in Modbus specification but seems
|
||||
* a resonable implementation. */
|
||||
eRcvState = STATE_RX_IDLE;
|
||||
/* Disable previously activated timer because of error state. */
|
||||
vMBPortTimersDisable( );
|
||||
}
|
||||
break;
|
||||
|
||||
case BYTE_LOW_NIBBLE:
|
||||
ucASCIIBuf[usRcvBufferPos] |= ucResult;
|
||||
usRcvBufferPos++;
|
||||
eBytePos = BYTE_HIGH_NIBBLE;
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case STATE_RX_WAIT_EOF:
|
||||
if( ucByte == ucMBLFCharacter )
|
||||
{
|
||||
/* Disable character timeout timer because all characters are
|
||||
* received. */
|
||||
vMBPortTimersDisable( );
|
||||
/* Receiver is again in idle state. */
|
||||
eRcvState = STATE_RX_IDLE;
|
||||
|
||||
/* Notify the caller of eMBASCIIReceive that a new frame
|
||||
* was received. */
|
||||
(void)xMBPortEventPost( EV_FRAME_RECEIVED );
|
||||
}
|
||||
else if( ucByte == ':' )
|
||||
{
|
||||
/* Empty receive buffer and back to receive state. */
|
||||
eBytePos = BYTE_HIGH_NIBBLE;
|
||||
usRcvBufferPos = 0;
|
||||
eRcvState = STATE_RX_RCV;
|
||||
|
||||
/* Enable timer for character timeout. */
|
||||
vMBPortTimersEnable( );
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Frame is not okay. Delete entire frame. */
|
||||
eRcvState = STATE_RX_IDLE;
|
||||
}
|
||||
break;
|
||||
|
||||
case STATE_RX_IDLE:
|
||||
if( ucByte == ':' )
|
||||
{
|
||||
/* Enable timer for character timeout. */
|
||||
vMBPortTimersEnable( );
|
||||
/* Reset the input buffers to store the frame. */
|
||||
usRcvBufferPos = 0;
|
||||
eBytePos = BYTE_HIGH_NIBBLE;
|
||||
eRcvState = STATE_RX_RCV;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return xNeedPoll;
|
||||
}
|
||||
|
||||
BOOL
|
||||
xMBASCIITransmitFSM( void )
|
||||
{
|
||||
BOOL xNeedPoll = TRUE;
|
||||
UCHAR ucByte;
|
||||
|
||||
assert( eRcvState == STATE_RX_IDLE );
|
||||
switch ( eSndState )
|
||||
{
|
||||
/* Start of transmission. The start of a frame is defined by sending
|
||||
* the character ':'. */
|
||||
case STATE_TX_START:
|
||||
ucByte = ':';
|
||||
xMBPortSerialPutByte( ( CHAR )ucByte );
|
||||
eSndState = STATE_TX_DATA;
|
||||
eBytePos = BYTE_HIGH_NIBBLE;
|
||||
break;
|
||||
|
||||
/* Send the data block. Each data byte is encoded as a character hex
|
||||
* stream with the high nibble sent first and the low nibble sent
|
||||
* last. If all data bytes are exhausted we send a '\r' character
|
||||
* to end the transmission. */
|
||||
case STATE_TX_DATA:
|
||||
if( usSndBufferCount > 0 )
|
||||
{
|
||||
switch ( eBytePos )
|
||||
{
|
||||
case BYTE_HIGH_NIBBLE:
|
||||
ucByte = prvucMBBIN2CHAR( ( UCHAR )( *pucSndBufferCur >> 4 ) );
|
||||
xMBPortSerialPutByte( ( CHAR ) ucByte );
|
||||
eBytePos = BYTE_LOW_NIBBLE;
|
||||
break;
|
||||
|
||||
case BYTE_LOW_NIBBLE:
|
||||
ucByte = prvucMBBIN2CHAR( ( UCHAR )( *pucSndBufferCur & 0x0F ) );
|
||||
xMBPortSerialPutByte( ( CHAR )ucByte );
|
||||
pucSndBufferCur++;
|
||||
eBytePos = BYTE_HIGH_NIBBLE;
|
||||
usSndBufferCount--;
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
xMBPortSerialPutByte( MB_ASCII_DEFAULT_CR );
|
||||
eSndState = STATE_TX_END;
|
||||
}
|
||||
break;
|
||||
|
||||
/* Finish the frame by sending a LF character. */
|
||||
case STATE_TX_END:
|
||||
xMBPortSerialPutByte( ( CHAR )ucMBLFCharacter );
|
||||
/* We need another state to make sure that the CR character has
|
||||
* been sent. */
|
||||
eSndState = STATE_TX_NOTIFY;
|
||||
break;
|
||||
|
||||
/* Notify the task which called eMBASCIISend that the frame has
|
||||
* been sent. */
|
||||
case STATE_TX_NOTIFY:
|
||||
eSndState = STATE_TX_IDLE;
|
||||
xMBPortEventPost( EV_FRAME_TRANSMIT );
|
||||
xNeedPoll = FALSE;
|
||||
break;
|
||||
|
||||
/* We should not get a transmitter event if the transmitter is in
|
||||
* idle state. */
|
||||
case STATE_TX_IDLE:
|
||||
break;
|
||||
}
|
||||
|
||||
return xNeedPoll;
|
||||
}
|
||||
|
||||
BOOL MB_PORT_ISR_ATTR
|
||||
xMBASCIITimerT1SExpired( void )
|
||||
{
|
||||
switch ( eRcvState )
|
||||
{
|
||||
/* If we have a timeout we go back to the idle state and wait for
|
||||
* the next frame.
|
||||
*/
|
||||
case STATE_RX_RCV:
|
||||
case STATE_RX_WAIT_EOF:
|
||||
eRcvState = STATE_RX_IDLE;
|
||||
break;
|
||||
|
||||
default:
|
||||
assert( ( eRcvState == STATE_RX_RCV ) || ( eRcvState == STATE_RX_WAIT_EOF )
|
||||
|| (eRcvState == STATE_RX_IDLE ));
|
||||
break;
|
||||
}
|
||||
vMBPortTimersDisable( );
|
||||
|
||||
/* no context switch required. */
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
|
||||
static UCHAR
|
||||
prvucMBCHAR2BIN( UCHAR ucCharacter )
|
||||
{
|
||||
if( ( ucCharacter >= '0' ) && ( ucCharacter <= '9' ) )
|
||||
{
|
||||
return ( UCHAR )( ucCharacter - '0' );
|
||||
}
|
||||
else if( ( ucCharacter >= 'A' ) && ( ucCharacter <= 'F' ) )
|
||||
{
|
||||
return ( UCHAR )( ucCharacter - 'A' + 0x0A );
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0xFF;
|
||||
}
|
||||
}
|
||||
|
||||
static UCHAR
|
||||
prvucMBBIN2CHAR( UCHAR ucByte )
|
||||
{
|
||||
if( ucByte <= 0x09 )
|
||||
{
|
||||
return ( UCHAR )( '0' + ucByte );
|
||||
}
|
||||
else if( ( ucByte >= 0x0A ) && ( ucByte <= 0x0F ) )
|
||||
{
|
||||
return ( UCHAR )( ucByte - 0x0A + 'A' );
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Programming error. */
|
||||
assert( 0 );
|
||||
}
|
||||
return '0';
|
||||
}
|
||||
|
||||
|
||||
static UCHAR
|
||||
prvucMBLRC( UCHAR * pucFrame, USHORT usLen )
|
||||
{
|
||||
UCHAR ucLRC = 0; /* LRC char initialized */
|
||||
|
||||
while( usLen-- )
|
||||
{
|
||||
ucLRC += *pucFrame++; /* Add buffer byte without carry */
|
||||
}
|
||||
|
||||
/* Return twos complement */
|
||||
ucLRC = ( UCHAR ) ( -( ( CHAR ) ucLRC ) );
|
||||
return ucLRC;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,85 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2006 Christian Walter
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*
|
||||
* SPDX-FileContributor: 2016-2021 Espressif Systems (Shanghai) CO LTD
|
||||
*/
|
||||
/*
|
||||
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
|
||||
* Copyright (c) 2006 Christian Walter <wolti@sil.at>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* File: $Id: mbascii.h,v 1.8 2006/12/07 22:10:34 wolti Exp $
|
||||
*/
|
||||
|
||||
#ifndef _MB_ASCII_H
|
||||
#define _MB_ASCII_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
PR_BEGIN_EXTERN_C
|
||||
#endif
|
||||
|
||||
/* ----------------------- Defines ------------------------------------------*/
|
||||
#define MB_ASCII_DEFAULT_CR '\r' /*!< Default CR character for Modbus ASCII. */
|
||||
#define MB_ASCII_DEFAULT_LF '\n' /*!< Default LF character for Modbus ASCII. */
|
||||
#define MB_ASCII_SER_PDU_SIZE_MIN 3 /*!< Minimum size of a Modbus ASCII frame. */
|
||||
|
||||
/* ----------------------- Function declaration -----------------------------*/
|
||||
|
||||
#if MB_SLAVE_ASCII_ENABLED > 0
|
||||
eMBErrorCode eMBASCIIInit( UCHAR slaveAddress, UCHAR ucPort,
|
||||
ULONG ulBaudRate, eMBParity eParity );
|
||||
void eMBASCIIStart( void );
|
||||
void eMBASCIIStop( void );
|
||||
|
||||
eMBErrorCode eMBASCIIReceive( UCHAR * pucRcvAddress, UCHAR ** pucFrame,
|
||||
USHORT * pusLength );
|
||||
eMBErrorCode eMBASCIISend( UCHAR slaveAddress, const UCHAR * pucFrame,
|
||||
USHORT usLength );
|
||||
BOOL xMBASCIIReceiveFSM( void );
|
||||
BOOL xMBASCIITransmitFSM( void );
|
||||
BOOL xMBASCIITimerT1SExpired( void );
|
||||
#endif
|
||||
|
||||
#if MB_MASTER_ASCII_ENABLED > 0
|
||||
eMBErrorCode eMBMasterASCIIInit( UCHAR ucPort,
|
||||
ULONG ulBaudRate, eMBParity eParity );
|
||||
void eMBMasterASCIIStart( void );
|
||||
void eMBMasterASCIIStop( void );
|
||||
|
||||
eMBErrorCode eMBMasterASCIIReceive( UCHAR * pucRcvAddress, UCHAR ** pucFrame,
|
||||
USHORT * pusLength );
|
||||
eMBErrorCode eMBMasterASCIISend( UCHAR slaveAddress, const UCHAR * pucFrame,
|
||||
USHORT usLength );
|
||||
BOOL xMBMasterASCIIReceiveFSM( void );
|
||||
BOOL xMBMasterASCIITransmitFSM( void );
|
||||
BOOL xMBMasterASCIITimerT1SExpired( void );
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
PR_END_EXTERN_C
|
||||
#endif
|
||||
#endif
|
||||
@@ -0,0 +1,587 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2016-2021 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
/*
|
||||
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
|
||||
* Copyright (c) 2006 Christian Walter <wolti@sil.at>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* File: $Id: mbascii.c,v 1.17 2010/06/06 13:47:07 wolti Exp $
|
||||
*/
|
||||
|
||||
/* ----------------------- System includes ----------------------------------*/
|
||||
#include "stdlib.h"
|
||||
#include "string.h"
|
||||
|
||||
/* ----------------------- Platform includes --------------------------------*/
|
||||
#include "port.h"
|
||||
|
||||
/* ----------------------- Modbus includes ----------------------------------*/
|
||||
#include "mb_m.h"
|
||||
#include "mbconfig.h"
|
||||
#include "mbascii.h"
|
||||
#include "mbframe.h"
|
||||
|
||||
#include "mbcrc.h"
|
||||
#include "mbport.h"
|
||||
|
||||
#if MB_MASTER_ASCII_ENABLED > 0
|
||||
|
||||
/* ----------------------- Defines ------------------------------------------*/
|
||||
|
||||
/* ----------------------- Type definitions ---------------------------------*/
|
||||
typedef enum
|
||||
{
|
||||
STATE_M_RX_INIT, /*!< Receiver is in initial state. */
|
||||
STATE_M_RX_IDLE, /*!< Receiver is in idle state. */
|
||||
STATE_M_RX_RCV, /*!< Frame is beeing received. */
|
||||
STATE_M_RX_WAIT_EOF, /*!< Wait for End of Frame. */
|
||||
STATE_M_RX_ERROR, /*!< If the frame is invalid. */
|
||||
} eMBMasterAsciiRcvState;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
STATE_M_TX_IDLE, /*!< Transmitter is in idle state. */
|
||||
STATE_M_TX_START, /*!< Starting transmission (':' sent). */
|
||||
STATE_M_TX_DATA, /*!< Sending of data (Address, Data, LRC). */
|
||||
STATE_M_TX_END, /*!< End of transmission. */
|
||||
STATE_M_TX_NOTIFY, /*!< Notify sender that the frame has been sent. */
|
||||
STATE_M_TX_XFWR, /*!< Transmitter is in transfer finish and wait receive state. */
|
||||
} eMBMasterAsciiSndState;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
BYTE_HIGH_NIBBLE, /*!< Character for high nibble of byte. */
|
||||
BYTE_LOW_NIBBLE /*!< Character for low nibble of byte. */
|
||||
} eMBBytePos;
|
||||
|
||||
/* ----------------------- Shared values -----------------------------------*/
|
||||
/* These Modbus values are shared in ASCII mode*/
|
||||
extern volatile UCHAR ucMasterRcvBuf[];
|
||||
extern volatile UCHAR ucMasterSndBuf[];
|
||||
|
||||
/* ----------------------- Static functions ---------------------------------*/
|
||||
static UCHAR prvucMBCHAR2BIN( UCHAR ucCharacter );
|
||||
|
||||
static UCHAR prvucMBBIN2CHAR( UCHAR ucByte );
|
||||
|
||||
static UCHAR prvucMBLRC( UCHAR * pucFrame, USHORT usLen );
|
||||
|
||||
/* ----------------------- Static variables ---------------------------------*/
|
||||
static volatile eMBMasterAsciiSndState eSndState;
|
||||
static volatile eMBMasterAsciiRcvState eRcvState;
|
||||
|
||||
static volatile UCHAR *ucMasterASCIIRcvBuf = ucMasterRcvBuf;
|
||||
static volatile UCHAR *ucMasterASCIISndBuf = ucMasterSndBuf;
|
||||
|
||||
static volatile USHORT usMasterRcvBufferPos;
|
||||
static volatile eMBBytePos eBytePos;
|
||||
|
||||
static volatile UCHAR *pucMasterSndBufferCur;
|
||||
static volatile USHORT usMasterSndBufferCount;
|
||||
|
||||
static volatile UCHAR ucLRC;
|
||||
static volatile UCHAR ucMBLFCharacter;
|
||||
|
||||
/* ----------------------- Start implementation -----------------------------*/
|
||||
eMBErrorCode
|
||||
eMBMasterASCIIInit( UCHAR ucPort, ULONG ulBaudRate, eMBParity eParity )
|
||||
{
|
||||
eMBErrorCode eStatus = MB_ENOERR;
|
||||
|
||||
ENTER_CRITICAL_SECTION( );
|
||||
ucMBLFCharacter = MB_ASCII_DEFAULT_LF;
|
||||
|
||||
if( xMBMasterPortSerialInit( ucPort, ulBaudRate, MB_ASCII_BITS_PER_SYMB, eParity ) != TRUE )
|
||||
{
|
||||
eStatus = MB_EPORTERR;
|
||||
}
|
||||
else if( xMBMasterPortTimersInit( MB_ASCII_TIMEOUT_MS * MB_TIMER_TICS_PER_MS ) != TRUE )
|
||||
{
|
||||
eStatus = MB_EPORTERR;
|
||||
}
|
||||
|
||||
EXIT_CRITICAL_SECTION( );
|
||||
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
void
|
||||
eMBMasterASCIIStart( void )
|
||||
{
|
||||
ENTER_CRITICAL_SECTION( );
|
||||
eRcvState = STATE_M_RX_IDLE;
|
||||
vMBMasterPortSerialEnable( TRUE, FALSE );
|
||||
xMBMasterPortEventPost(EV_MASTER_READY);
|
||||
EXIT_CRITICAL_SECTION( );
|
||||
}
|
||||
|
||||
void
|
||||
eMBMasterASCIIStop( void )
|
||||
{
|
||||
ENTER_CRITICAL_SECTION( );
|
||||
vMBMasterPortSerialEnable( FALSE, FALSE );
|
||||
vMBMasterPortTimersDisable( );
|
||||
EXIT_CRITICAL_SECTION( );
|
||||
}
|
||||
|
||||
eMBErrorCode
|
||||
eMBMasterASCIIReceive( UCHAR * pucRcvAddress, UCHAR ** pucFrame, USHORT * pusLength )
|
||||
{
|
||||
eMBErrorCode eStatus = MB_ENOERR;
|
||||
UCHAR *pucMBASCIIFrame = ( UCHAR* ) ucMasterASCIIRcvBuf;
|
||||
USHORT usFrameLength = usMasterRcvBufferPos;
|
||||
|
||||
if( xMBMasterPortSerialGetResponse( &pucMBASCIIFrame, &usFrameLength ) == FALSE )
|
||||
{
|
||||
return MB_EIO;
|
||||
}
|
||||
ENTER_CRITICAL_SECTION( );
|
||||
assert( usFrameLength < MB_SER_PDU_SIZE_MAX );
|
||||
|
||||
assert( pucMBASCIIFrame );
|
||||
/* Length and CRC check */
|
||||
if( ( usFrameLength >= MB_ASCII_SER_PDU_SIZE_MIN )
|
||||
&& ( prvucMBLRC( ( UCHAR * ) pucMBASCIIFrame, usFrameLength ) == 0 ) )
|
||||
{
|
||||
/* Save the address field. All frames are passed to the upper layed
|
||||
* and the decision if a frame is used is done there.
|
||||
*/
|
||||
*pucRcvAddress = pucMBASCIIFrame[MB_SER_PDU_ADDR_OFF];
|
||||
|
||||
/* Total length of Modbus-PDU is Modbus-Serial-Line-PDU minus
|
||||
* size of address field and CRC checksum.
|
||||
*/
|
||||
*pusLength = ( USHORT )( usFrameLength - MB_SER_PDU_PDU_OFF - MB_SER_PDU_SIZE_LRC );
|
||||
|
||||
/* Return the start of the Modbus PDU to the caller. */
|
||||
*pucFrame = ( UCHAR * ) & pucMBASCIIFrame[MB_SER_PDU_PDU_OFF];
|
||||
} else {
|
||||
eStatus = MB_EIO;
|
||||
}
|
||||
EXIT_CRITICAL_SECTION( );
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
eMBErrorCode
|
||||
eMBMasterASCIISend( UCHAR ucSlaveAddress, const UCHAR * pucFrame, USHORT usLength )
|
||||
{
|
||||
eMBErrorCode eStatus = MB_ENOERR;
|
||||
UCHAR usLRC;
|
||||
|
||||
if ( ucSlaveAddress > MB_MASTER_TOTAL_SLAVE_NUM ) return MB_EINVAL;
|
||||
|
||||
/* Check if the receiver is still in idle state. If not we where too
|
||||
* slow with processing the received frame and the master sent another
|
||||
* frame on the network. We have to abort sending the frame.
|
||||
*/
|
||||
if(eRcvState == STATE_M_RX_IDLE)
|
||||
{
|
||||
ENTER_CRITICAL_SECTION( );
|
||||
/* First byte before the Modbus-PDU is the slave address. */
|
||||
pucMasterSndBufferCur = ( UCHAR * ) pucFrame - 1;
|
||||
usMasterSndBufferCount = 1;
|
||||
|
||||
/* Now copy the Modbus-PDU into the Modbus-Serial-Line-PDU. */
|
||||
pucMasterSndBufferCur[MB_SER_PDU_ADDR_OFF] = ucSlaveAddress;
|
||||
usMasterSndBufferCount += usLength;
|
||||
|
||||
/* Calculate LRC checksum for Modbus-Serial-Line-PDU. */
|
||||
usLRC = prvucMBLRC( ( UCHAR * ) pucMasterSndBufferCur, usMasterSndBufferCount );
|
||||
pucMasterSndBufferCur[usMasterSndBufferCount++] = usLRC;
|
||||
|
||||
/* Activate the transmitter. */
|
||||
eSndState = STATE_M_TX_START;
|
||||
EXIT_CRITICAL_SECTION( );
|
||||
|
||||
if ( xMBMasterPortSerialSendRequest( ( UCHAR * ) pucMasterSndBufferCur, usMasterSndBufferCount ) == FALSE )
|
||||
{
|
||||
eStatus = MB_EIO;
|
||||
}
|
||||
vMBMasterPortSerialEnable( FALSE, TRUE );
|
||||
}
|
||||
else
|
||||
{
|
||||
eStatus = MB_EIO;
|
||||
}
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
BOOL
|
||||
xMBMasterASCIIReceiveFSM( void )
|
||||
{
|
||||
BOOL xNeedPoll = FALSE;
|
||||
UCHAR ucByte;
|
||||
UCHAR ucResult;
|
||||
|
||||
assert(( eSndState == STATE_M_TX_IDLE ) || ( eSndState == STATE_M_TX_XFWR ));
|
||||
|
||||
/* Always read the character. */
|
||||
xNeedPoll = xMBMasterPortSerialGetByte( ( CHAR * ) & ucByte );
|
||||
|
||||
switch ( eRcvState )
|
||||
{
|
||||
/* If we have received a character in the init state we have to
|
||||
* wait until the frame is finished.
|
||||
*/
|
||||
case STATE_M_RX_INIT:
|
||||
vMBMasterPortTimersT35Enable( );
|
||||
break;
|
||||
|
||||
/* In the error state we wait until all characters in the
|
||||
* damaged frame are transmitted.
|
||||
*/
|
||||
case STATE_M_RX_ERROR:
|
||||
vMBMasterPortTimersRespondTimeoutEnable( );
|
||||
break;
|
||||
|
||||
/* In the idle state we wait for a new character. If a character
|
||||
* is received the t1.5 and t3.5 timers are started and the
|
||||
* receiver is in the state STATE_RX_RECEIVE and disable early
|
||||
* the timer of respond timeout .
|
||||
*/
|
||||
case STATE_M_RX_IDLE:
|
||||
/* Waiting for the start of frame character during respond timeout */
|
||||
vMBMasterPortTimersRespondTimeoutEnable( );
|
||||
if( ucByte == ':' )
|
||||
{
|
||||
/* Reset the input buffers to store the frame in receive state. */
|
||||
usMasterRcvBufferPos = 0;
|
||||
eBytePos = BYTE_HIGH_NIBBLE;
|
||||
eRcvState = STATE_M_RX_RCV;
|
||||
eSndState = STATE_M_TX_IDLE;
|
||||
}
|
||||
break;
|
||||
|
||||
/* A new character is received. If the character is a ':' the input
|
||||
* buffer is cleared. A CR-character signals the end of the data
|
||||
* block. Other characters are part of the data block and their
|
||||
* ASCII value is converted back to a binary representation.
|
||||
*/
|
||||
case STATE_M_RX_RCV:
|
||||
/* Enable timer timeout. */
|
||||
vMBMasterPortTimersT35Enable( );
|
||||
if( ucByte == ':' )
|
||||
{
|
||||
/* Empty receive buffer. */
|
||||
eBytePos = BYTE_HIGH_NIBBLE;
|
||||
usMasterRcvBufferPos = 0;
|
||||
}
|
||||
else if( ucByte == MB_ASCII_DEFAULT_CR )
|
||||
{
|
||||
eRcvState = STATE_M_RX_WAIT_EOF;
|
||||
}
|
||||
else
|
||||
{
|
||||
ucResult = prvucMBCHAR2BIN( ucByte );
|
||||
switch ( eBytePos )
|
||||
{
|
||||
/* High nibble of the byte comes first. We check for
|
||||
* a buffer overflow here. */
|
||||
case BYTE_HIGH_NIBBLE:
|
||||
if( usMasterRcvBufferPos < MB_SER_PDU_SIZE_MAX )
|
||||
{
|
||||
ucMasterASCIIRcvBuf[usMasterRcvBufferPos] = ( UCHAR )( ucResult << 4 );
|
||||
eBytePos = BYTE_LOW_NIBBLE;
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* not handled in Modbus specification but seems
|
||||
* a resonable implementation. */
|
||||
eRcvState = STATE_M_RX_ERROR;
|
||||
/* Disable previously activated timer because of error state. */
|
||||
vMBPortTimersDisable( );
|
||||
}
|
||||
break;
|
||||
|
||||
case BYTE_LOW_NIBBLE:
|
||||
ucMasterASCIIRcvBuf[usMasterRcvBufferPos] |= ucResult;
|
||||
usMasterRcvBufferPos++;
|
||||
eBytePos = BYTE_HIGH_NIBBLE;
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case STATE_M_RX_WAIT_EOF:
|
||||
if( ucByte == ucMBLFCharacter )
|
||||
{
|
||||
/* Disable character timeout timer because all characters are
|
||||
* received. */
|
||||
vMBMasterPortTimersDisable( );
|
||||
/* Receiver is again in idle state. */
|
||||
eRcvState = STATE_M_RX_IDLE;
|
||||
|
||||
/* Notify the caller of eMBMasterASCIIReceive that a new frame
|
||||
* was received. */
|
||||
(void)xMBMasterPortEventPost( EV_MASTER_FRAME_RECEIVED );
|
||||
xNeedPoll = FALSE;
|
||||
}
|
||||
else if( ucByte == ':' )
|
||||
{
|
||||
/* Start of frame character received but last message is not completed.
|
||||
* Empty receive buffer and back to receive state. */
|
||||
eBytePos = BYTE_HIGH_NIBBLE;
|
||||
usMasterRcvBufferPos = 0;
|
||||
eRcvState = STATE_M_RX_IDLE;
|
||||
|
||||
/* Enable timer for respond timeout and wait for next frame. */
|
||||
vMBMasterPortTimersRespondTimeoutEnable( );
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Frame is not okay. Delete entire frame. */
|
||||
eRcvState = STATE_M_RX_IDLE;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return xNeedPoll;
|
||||
}
|
||||
|
||||
BOOL
|
||||
xMBMasterASCIITransmitFSM( void )
|
||||
{
|
||||
BOOL xNeedPoll = TRUE;
|
||||
UCHAR ucByte;
|
||||
BOOL xFrameIsBroadcast = FALSE;
|
||||
|
||||
assert( eRcvState == STATE_M_RX_IDLE );
|
||||
|
||||
switch ( eSndState )
|
||||
{
|
||||
/* We should not get a transmitter event if the transmitter is in
|
||||
* idle state. */
|
||||
case STATE_M_TX_XFWR:
|
||||
break;
|
||||
|
||||
/* We should not get a transmitter event if the transmitter is in
|
||||
* idle state. */
|
||||
case STATE_M_TX_IDLE:
|
||||
break;
|
||||
|
||||
/* Start of transmission. The start of a frame is defined by sending
|
||||
* the character ':'. */
|
||||
case STATE_M_TX_START:
|
||||
ucByte = ':';
|
||||
xMBMasterPortSerialPutByte( ( CHAR )ucByte );
|
||||
eSndState = STATE_M_TX_DATA;
|
||||
eBytePos = BYTE_HIGH_NIBBLE;
|
||||
break;
|
||||
|
||||
/* Send the data block. Each data byte is encoded as a character hex
|
||||
* stream with the high nibble sent first and the low nibble sent
|
||||
* last. If all data bytes are exhausted we send a '\r' character
|
||||
* to end the transmission. */
|
||||
case STATE_M_TX_DATA:
|
||||
if( usMasterSndBufferCount > 0 )
|
||||
{
|
||||
switch ( eBytePos )
|
||||
{
|
||||
case BYTE_HIGH_NIBBLE:
|
||||
ucByte = prvucMBBIN2CHAR( ( UCHAR )( *pucMasterSndBufferCur >> 4 ) );
|
||||
xMBMasterPortSerialPutByte( ( CHAR ) ucByte );
|
||||
eBytePos = BYTE_LOW_NIBBLE;
|
||||
break;
|
||||
|
||||
case BYTE_LOW_NIBBLE:
|
||||
ucByte = prvucMBBIN2CHAR( ( UCHAR )( *pucMasterSndBufferCur & 0x0F ) );
|
||||
xMBMasterPortSerialPutByte( ( CHAR )ucByte );
|
||||
pucMasterSndBufferCur++;
|
||||
eBytePos = BYTE_HIGH_NIBBLE;
|
||||
usMasterSndBufferCount--;
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
xMBMasterPortSerialPutByte( MB_ASCII_DEFAULT_CR );
|
||||
eSndState = STATE_M_TX_END;
|
||||
}
|
||||
break;
|
||||
|
||||
/* Finish the frame by sending a LF character. */
|
||||
case STATE_M_TX_END:
|
||||
xMBMasterPortSerialPutByte( ( CHAR )ucMBLFCharacter );
|
||||
/* We need another state to make sure that the CR character has
|
||||
* been sent. */
|
||||
eSndState = STATE_M_TX_NOTIFY;
|
||||
break;
|
||||
|
||||
/* Notify the task which called eMBMasterASCIISend that the frame has
|
||||
* been sent. */
|
||||
case STATE_M_TX_NOTIFY:
|
||||
xFrameIsBroadcast = ( ucMasterASCIISndBuf[MB_SEND_BUF_PDU_OFF - MB_SER_PDU_PDU_OFF]
|
||||
== MB_ADDRESS_BROADCAST ) ? TRUE : FALSE;
|
||||
vMBMasterRequestSetType( xFrameIsBroadcast );
|
||||
eSndState = STATE_M_TX_XFWR;
|
||||
/* If the frame is broadcast ,master will enable timer of convert delay,
|
||||
* else master will enable timer of respond timeout. */
|
||||
if ( xFrameIsBroadcast == TRUE )
|
||||
{
|
||||
vMBMasterPortTimersConvertDelayEnable( );
|
||||
}
|
||||
else
|
||||
{
|
||||
vMBMasterPortTimersRespondTimeoutEnable( );
|
||||
}
|
||||
xNeedPoll = FALSE;
|
||||
break;
|
||||
}
|
||||
|
||||
return xNeedPoll;
|
||||
}
|
||||
|
||||
BOOL MB_PORT_ISR_ATTR
|
||||
xMBMasterASCIITimerT1SExpired( void )
|
||||
{
|
||||
BOOL xNeedPoll = FALSE;
|
||||
|
||||
switch ( eRcvState )
|
||||
{
|
||||
/* Timer t35 expired. Startup phase is finished. */
|
||||
case STATE_M_RX_INIT:
|
||||
xNeedPoll = xMBMasterPortEventPost(EV_MASTER_READY);
|
||||
break;
|
||||
|
||||
/* Start of message is not received during respond timeout.
|
||||
* Process error. */
|
||||
case STATE_M_RX_IDLE:
|
||||
eRcvState = STATE_M_RX_ERROR;
|
||||
break;
|
||||
|
||||
/* A recieve timeout expired and no any new character received.
|
||||
* Wait for respond time and go to error state to inform listener about error */
|
||||
case STATE_M_RX_RCV:
|
||||
eRcvState = STATE_M_RX_ERROR;
|
||||
break;
|
||||
|
||||
/* An error occured while receiving the frame. */
|
||||
case STATE_M_RX_ERROR:
|
||||
vMBMasterSetErrorType(EV_ERROR_RECEIVE_DATA);
|
||||
xNeedPoll = xMBMasterPortEventPost( EV_MASTER_ERROR_PROCESS );
|
||||
break;
|
||||
|
||||
/* If we have a timeout we go back to the idle state and wait for
|
||||
* the next frame.
|
||||
*/
|
||||
case STATE_M_RX_WAIT_EOF:
|
||||
eRcvState = STATE_M_RX_IDLE;
|
||||
break;
|
||||
|
||||
default:
|
||||
assert( 0 );
|
||||
break;
|
||||
}
|
||||
eRcvState = STATE_M_RX_IDLE;
|
||||
|
||||
switch (eSndState)
|
||||
{
|
||||
/* A frame was send finish and convert delay or respond timeout expired.
|
||||
* If the frame is broadcast,The master will idle,and if the frame is not
|
||||
* broadcast.*/
|
||||
case STATE_M_TX_XFWR:
|
||||
if ( xMBMasterRequestIsBroadcast( ) == FALSE ) {
|
||||
vMBMasterSetErrorType(EV_ERROR_RESPOND_TIMEOUT);
|
||||
xNeedPoll = xMBMasterPortEventPost(EV_MASTER_ERROR_PROCESS);
|
||||
}
|
||||
break;
|
||||
|
||||
/* Function called in an illegal state. */
|
||||
default:
|
||||
assert( ( eSndState == STATE_M_TX_START ) || ( eSndState == STATE_M_TX_IDLE )
|
||||
|| ( eSndState == STATE_M_TX_DATA ) || ( eSndState == STATE_M_TX_END )
|
||||
|| ( eSndState == STATE_M_TX_NOTIFY ) );
|
||||
break;
|
||||
}
|
||||
eSndState = STATE_M_TX_IDLE;
|
||||
|
||||
vMBMasterPortTimersDisable( );
|
||||
/* If timer mode is convert delay, the master event then turns EV_MASTER_EXECUTE status. */
|
||||
if (xMBMasterGetCurTimerMode() == MB_TMODE_CONVERT_DELAY) {
|
||||
xNeedPoll = xMBMasterPortEventPost( EV_MASTER_EXECUTE );
|
||||
}
|
||||
|
||||
vMBMasterPortTimersDisable( );
|
||||
|
||||
/* no context switch required. */
|
||||
return xNeedPoll;
|
||||
}
|
||||
|
||||
static UCHAR
|
||||
prvucMBCHAR2BIN( UCHAR ucCharacter )
|
||||
{
|
||||
if( ( ucCharacter >= '0' ) && ( ucCharacter <= '9' ) )
|
||||
{
|
||||
return ( UCHAR )( ucCharacter - '0' );
|
||||
}
|
||||
else if( ( ucCharacter >= 'A' ) && ( ucCharacter <= 'F' ) )
|
||||
{
|
||||
return ( UCHAR )( ucCharacter - 'A' + 0x0A );
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0xFF;
|
||||
}
|
||||
}
|
||||
|
||||
static UCHAR
|
||||
prvucMBBIN2CHAR( UCHAR ucByte )
|
||||
{
|
||||
if( ucByte <= 0x09 )
|
||||
{
|
||||
return ( UCHAR )( '0' + ucByte );
|
||||
}
|
||||
else if( ( ucByte >= 0x0A ) && ( ucByte <= 0x0F ) )
|
||||
{
|
||||
return ( UCHAR )( ucByte - 0x0A + 'A' );
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Programming error. */
|
||||
assert( 0 );
|
||||
}
|
||||
return '0';
|
||||
}
|
||||
|
||||
static UCHAR
|
||||
prvucMBLRC( UCHAR * pucFrame, USHORT usLen )
|
||||
{
|
||||
UCHAR ucLRC = 0; /* LRC char initialized */
|
||||
|
||||
while( usLen-- )
|
||||
{
|
||||
ucLRC += *pucFrame++; /* Add buffer byte without carry */
|
||||
}
|
||||
|
||||
/* Return twos complement */
|
||||
ucLRC = ( UCHAR ) ( -( ( CHAR ) ucLRC ) );
|
||||
return ucLRC;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,280 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2006 Christian Walter
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*
|
||||
* SPDX-FileContributor: 2016-2021 Espressif Systems (Shanghai) CO LTD
|
||||
*/
|
||||
/*
|
||||
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
|
||||
* Copyright (c) 2006 Christian Walter <wolti@sil.at>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* File: $Id: mbfunccoils.c,v 1.8 2007/02/18 23:47:16 wolti Exp $
|
||||
*/
|
||||
|
||||
/* ----------------------- System includes ----------------------------------*/
|
||||
#include "stdlib.h"
|
||||
#include "string.h"
|
||||
|
||||
/* ----------------------- Platform includes --------------------------------*/
|
||||
#include "port.h"
|
||||
|
||||
/* ----------------------- Modbus includes ----------------------------------*/
|
||||
#include "mb.h"
|
||||
#include "mbframe.h"
|
||||
#include "mbproto.h"
|
||||
#include "mbconfig.h"
|
||||
|
||||
/* ----------------------- Defines ------------------------------------------*/
|
||||
#define MB_PDU_FUNC_READ_ADDR_OFF ( MB_PDU_DATA_OFF )
|
||||
#define MB_PDU_FUNC_READ_COILCNT_OFF ( MB_PDU_DATA_OFF + 2 )
|
||||
#define MB_PDU_FUNC_READ_SIZE ( 4 )
|
||||
#define MB_PDU_FUNC_READ_COILCNT_MAX ( 0x07D0 )
|
||||
|
||||
#define MB_PDU_FUNC_WRITE_ADDR_OFF ( MB_PDU_DATA_OFF )
|
||||
#define MB_PDU_FUNC_WRITE_VALUE_OFF ( MB_PDU_DATA_OFF + 2 )
|
||||
#define MB_PDU_FUNC_WRITE_SIZE ( 4 )
|
||||
|
||||
#define MB_PDU_FUNC_WRITE_MUL_ADDR_OFF ( MB_PDU_DATA_OFF )
|
||||
#define MB_PDU_FUNC_WRITE_MUL_COILCNT_OFF ( MB_PDU_DATA_OFF + 2 )
|
||||
#define MB_PDU_FUNC_WRITE_MUL_BYTECNT_OFF ( MB_PDU_DATA_OFF + 4 )
|
||||
#define MB_PDU_FUNC_WRITE_MUL_VALUES_OFF ( MB_PDU_DATA_OFF + 5 )
|
||||
#define MB_PDU_FUNC_WRITE_MUL_SIZE_MIN ( 5 )
|
||||
#define MB_PDU_FUNC_WRITE_MUL_COILCNT_MAX ( 0x07B0 )
|
||||
|
||||
/* ----------------------- Static functions ---------------------------------*/
|
||||
eMBException prveMBError2Exception( eMBErrorCode eErrorCode );
|
||||
|
||||
/* ----------------------- Start implementation -----------------------------*/
|
||||
#if MB_SLAVE_RTU_ENABLED || MB_SLAVE_ASCII_ENABLED || MB_TCP_ENABLED
|
||||
|
||||
#if MB_FUNC_READ_COILS_ENABLED
|
||||
|
||||
eMBException
|
||||
eMBFuncReadCoils( UCHAR * pucFrame, USHORT * usLen )
|
||||
{
|
||||
USHORT usRegAddress;
|
||||
USHORT usCoilCount;
|
||||
UCHAR ucNBytes;
|
||||
UCHAR *pucFrameCur;
|
||||
|
||||
eMBException eStatus = MB_EX_NONE;
|
||||
eMBErrorCode eRegStatus;
|
||||
|
||||
if( *usLen == ( MB_PDU_FUNC_READ_SIZE + MB_PDU_SIZE_MIN ) )
|
||||
{
|
||||
usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF] << 8 );
|
||||
usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF + 1] );
|
||||
usRegAddress++;
|
||||
|
||||
usCoilCount = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_COILCNT_OFF] << 8 );
|
||||
usCoilCount |= ( USHORT )( pucFrame[MB_PDU_FUNC_READ_COILCNT_OFF + 1] );
|
||||
|
||||
/* Check if the number of registers to read is valid. If not
|
||||
* return Modbus illegal data value exception.
|
||||
*/
|
||||
if( ( usCoilCount >= 1 ) &&
|
||||
( usCoilCount < MB_PDU_FUNC_READ_COILCNT_MAX ) )
|
||||
{
|
||||
/* Set the current PDU data pointer to the beginning. */
|
||||
pucFrameCur = &pucFrame[MB_PDU_FUNC_OFF];
|
||||
*usLen = MB_PDU_FUNC_OFF;
|
||||
|
||||
/* First byte contains the function code. */
|
||||
*pucFrameCur++ = MB_FUNC_READ_COILS;
|
||||
*usLen += 1;
|
||||
|
||||
/* Test if the quantity of coils is a multiple of 8. If not last
|
||||
* byte is only partially field with unused coils set to zero. */
|
||||
if( ( usCoilCount & 0x0007 ) != 0 )
|
||||
{
|
||||
ucNBytes = ( UCHAR )( usCoilCount / 8 + 1 );
|
||||
}
|
||||
else
|
||||
{
|
||||
ucNBytes = ( UCHAR )( usCoilCount / 8 );
|
||||
}
|
||||
*pucFrameCur++ = ucNBytes;
|
||||
*usLen += 1;
|
||||
|
||||
eRegStatus =
|
||||
eMBRegCoilsCB( pucFrameCur, usRegAddress, usCoilCount,
|
||||
MB_REG_READ );
|
||||
|
||||
/* If an error occured convert it into a Modbus exception. */
|
||||
if( eRegStatus != MB_ENOERR )
|
||||
{
|
||||
eStatus = prveMBError2Exception( eRegStatus );
|
||||
}
|
||||
else
|
||||
{
|
||||
/* The response contains the function code, the starting address
|
||||
* and the quantity of registers. We reuse the old values in the
|
||||
* buffer because they are still valid. */
|
||||
*usLen += ucNBytes;;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Can't be a valid read coil register request because the length
|
||||
* is incorrect. */
|
||||
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
|
||||
}
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
#if MB_FUNC_WRITE_COIL_ENABLED > 0
|
||||
eMBException
|
||||
eMBFuncWriteCoil( UCHAR * pucFrame, USHORT * usLen )
|
||||
{
|
||||
USHORT usRegAddress;
|
||||
UCHAR ucBuf[2];
|
||||
|
||||
eMBException eStatus = MB_EX_NONE;
|
||||
eMBErrorCode eRegStatus;
|
||||
|
||||
if( *usLen == ( MB_PDU_FUNC_WRITE_SIZE + MB_PDU_SIZE_MIN ) )
|
||||
{
|
||||
usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_ADDR_OFF] << 8 );
|
||||
usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_ADDR_OFF + 1] );
|
||||
usRegAddress++;
|
||||
|
||||
if( ( pucFrame[MB_PDU_FUNC_WRITE_VALUE_OFF + 1] == 0x00 ) &&
|
||||
( ( pucFrame[MB_PDU_FUNC_WRITE_VALUE_OFF] == 0xFF ) ||
|
||||
( pucFrame[MB_PDU_FUNC_WRITE_VALUE_OFF] == 0x00 ) ) )
|
||||
{
|
||||
ucBuf[1] = 0;
|
||||
if( pucFrame[MB_PDU_FUNC_WRITE_VALUE_OFF] == 0xFF )
|
||||
{
|
||||
ucBuf[0] = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
ucBuf[0] = 0;
|
||||
}
|
||||
eRegStatus =
|
||||
eMBRegCoilsCB( &ucBuf[0], usRegAddress, 1, MB_REG_WRITE );
|
||||
|
||||
/* If an error occured convert it into a Modbus exception. */
|
||||
if( eRegStatus != MB_ENOERR )
|
||||
{
|
||||
eStatus = prveMBError2Exception( eRegStatus );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Can't be a valid write coil register request because the length
|
||||
* is incorrect. */
|
||||
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
|
||||
}
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#if MB_FUNC_WRITE_MULTIPLE_COILS_ENABLED > 0
|
||||
eMBException
|
||||
eMBFuncWriteMultipleCoils( UCHAR * pucFrame, USHORT * usLen )
|
||||
{
|
||||
USHORT usRegAddress;
|
||||
USHORT usCoilCnt;
|
||||
UCHAR ucByteCount;
|
||||
UCHAR ucByteCountVerify;
|
||||
|
||||
eMBException eStatus = MB_EX_NONE;
|
||||
eMBErrorCode eRegStatus;
|
||||
|
||||
if( *usLen > ( MB_PDU_FUNC_WRITE_SIZE + MB_PDU_SIZE_MIN ) )
|
||||
{
|
||||
usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_ADDR_OFF] << 8 );
|
||||
usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_ADDR_OFF + 1] );
|
||||
usRegAddress++;
|
||||
|
||||
usCoilCnt = ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_COILCNT_OFF] << 8 );
|
||||
usCoilCnt |= ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_COILCNT_OFF + 1] );
|
||||
|
||||
ucByteCount = pucFrame[MB_PDU_FUNC_WRITE_MUL_BYTECNT_OFF];
|
||||
|
||||
/* Compute the number of expected bytes in the request. */
|
||||
if( ( usCoilCnt & 0x0007 ) != 0 )
|
||||
{
|
||||
ucByteCountVerify = ( UCHAR )( usCoilCnt / 8 + 1 );
|
||||
}
|
||||
else
|
||||
{
|
||||
ucByteCountVerify = ( UCHAR )( usCoilCnt / 8 );
|
||||
}
|
||||
|
||||
if( ( usCoilCnt >= 1 ) &&
|
||||
( usCoilCnt <= MB_PDU_FUNC_WRITE_MUL_COILCNT_MAX ) &&
|
||||
( ucByteCountVerify == ucByteCount ) )
|
||||
{
|
||||
eRegStatus =
|
||||
eMBRegCoilsCB( &pucFrame[MB_PDU_FUNC_WRITE_MUL_VALUES_OFF],
|
||||
usRegAddress, usCoilCnt, MB_REG_WRITE );
|
||||
|
||||
/* If an error occured convert it into a Modbus exception. */
|
||||
if( eRegStatus != MB_ENOERR )
|
||||
{
|
||||
eStatus = prveMBError2Exception( eRegStatus );
|
||||
}
|
||||
else
|
||||
{
|
||||
/* The response contains the function code, the starting address
|
||||
* and the quantity of registers. We reuse the old values in the
|
||||
* buffer because they are still valid. */
|
||||
*usLen = MB_PDU_FUNC_WRITE_MUL_BYTECNT_OFF;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Can't be a valid write coil register request because the length
|
||||
* is incorrect. */
|
||||
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
|
||||
}
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,398 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2013 Armink
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*
|
||||
* SPDX-FileContributor: 2016-2021 Espressif Systems (Shanghai) CO LTD
|
||||
*/
|
||||
/*
|
||||
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
|
||||
* Copyright (C) 2013 Armink <armink.ztl@gmail.com>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* File: $Id: mbfunccoils_m.c,v 1.60 2013/10/12 15:10:12 Armink Add Master Functions
|
||||
*/
|
||||
|
||||
/* ----------------------- System includes ----------------------------------*/
|
||||
#include "stdlib.h"
|
||||
#include "string.h"
|
||||
|
||||
/* ----------------------- Platform includes --------------------------------*/
|
||||
#include "port.h"
|
||||
|
||||
/* ----------------------- Modbus includes ----------------------------------*/
|
||||
#include "mb_m.h"
|
||||
#include "mbframe.h"
|
||||
#include "mbproto.h"
|
||||
#include "mbconfig.h"
|
||||
#include "mbutils.h"
|
||||
|
||||
/* ----------------------- Defines ------------------------------------------*/
|
||||
#define MB_PDU_REQ_READ_ADDR_OFF ( MB_PDU_DATA_OFF + 0 )
|
||||
#define MB_PDU_REQ_READ_COILCNT_OFF ( MB_PDU_DATA_OFF + 2 )
|
||||
#define MB_PDU_REQ_READ_SIZE ( 4 )
|
||||
#define MB_PDU_FUNC_READ_COILCNT_OFF ( MB_PDU_DATA_OFF + 0 )
|
||||
#define MB_PDU_FUNC_READ_VALUES_OFF ( MB_PDU_DATA_OFF + 1 )
|
||||
#define MB_PDU_FUNC_READ_SIZE_MIN ( 1 )
|
||||
|
||||
#define MB_PDU_REQ_WRITE_ADDR_OFF ( MB_PDU_DATA_OFF )
|
||||
#define MB_PDU_REQ_WRITE_VALUE_OFF ( MB_PDU_DATA_OFF + 2 )
|
||||
#define MB_PDU_REQ_WRITE_SIZE ( 4 )
|
||||
#define MB_PDU_FUNC_WRITE_ADDR_OFF ( MB_PDU_DATA_OFF )
|
||||
#define MB_PDU_FUNC_WRITE_VALUE_OFF ( MB_PDU_DATA_OFF + 2 )
|
||||
#define MB_PDU_FUNC_WRITE_SIZE ( 4 )
|
||||
|
||||
#define MB_PDU_REQ_WRITE_MUL_ADDR_OFF ( MB_PDU_DATA_OFF )
|
||||
#define MB_PDU_REQ_WRITE_MUL_COILCNT_OFF ( MB_PDU_DATA_OFF + 2 )
|
||||
#define MB_PDU_REQ_WRITE_MUL_BYTECNT_OFF ( MB_PDU_DATA_OFF + 4 )
|
||||
#define MB_PDU_REQ_WRITE_MUL_VALUES_OFF ( MB_PDU_DATA_OFF + 5 )
|
||||
#define MB_PDU_REQ_WRITE_MUL_SIZE_MIN ( 5 )
|
||||
#define MB_PDU_REQ_WRITE_MUL_COILCNT_MAX ( 0x07B0 )
|
||||
#define MB_PDU_FUNC_WRITE_MUL_ADDR_OFF ( MB_PDU_DATA_OFF )
|
||||
#define MB_PDU_FUNC_WRITE_MUL_COILCNT_OFF ( MB_PDU_DATA_OFF + 2 )
|
||||
#define MB_PDU_FUNC_WRITE_MUL_SIZE ( 5 )
|
||||
|
||||
/* ----------------------- Static functions ---------------------------------*/
|
||||
eMBException prveMBError2Exception( eMBErrorCode eErrorCode );
|
||||
|
||||
/* ----------------------- Start implementation -----------------------------*/
|
||||
#if MB_MASTER_RTU_ENABLED || MB_MASTER_ASCII_ENABLED || MB_MASTER_TCP_ENABLED
|
||||
|
||||
#if MB_FUNC_READ_COILS_ENABLED
|
||||
|
||||
/**
|
||||
* This function will request read coil.
|
||||
*
|
||||
* @param ucSndAddr salve address
|
||||
* @param usCoilAddr coil start address
|
||||
* @param usNCoils coil total number
|
||||
* @param lTimeOut timeout (-1 will waiting forever)
|
||||
*
|
||||
* @return error code
|
||||
*/
|
||||
eMBMasterReqErrCode
|
||||
eMBMasterReqReadCoils( UCHAR ucSndAddr, USHORT usCoilAddr, USHORT usNCoils, LONG lTimeOut )
|
||||
{
|
||||
UCHAR *ucMBFrame;
|
||||
eMBMasterReqErrCode eErrStatus = MB_MRE_NO_ERR;
|
||||
|
||||
if ( ucSndAddr > MB_MASTER_TOTAL_SLAVE_NUM ) eErrStatus = MB_MRE_ILL_ARG;
|
||||
else if ( xMBMasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY;
|
||||
else
|
||||
{
|
||||
vMBMasterGetPDUSndBuf(&ucMBFrame);
|
||||
vMBMasterSetDestAddress(ucSndAddr);
|
||||
ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_READ_COILS;
|
||||
ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF] = usCoilAddr >> 8;
|
||||
ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF + 1] = usCoilAddr;
|
||||
ucMBFrame[MB_PDU_REQ_READ_COILCNT_OFF ] = usNCoils >> 8;
|
||||
ucMBFrame[MB_PDU_REQ_READ_COILCNT_OFF + 1] = usNCoils;
|
||||
vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_READ_SIZE );
|
||||
( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_TRANSMIT | EV_MASTER_TRANS_START );
|
||||
eErrStatus = eMBMasterWaitRequestFinish( );
|
||||
|
||||
}
|
||||
return eErrStatus;
|
||||
}
|
||||
|
||||
eMBException
|
||||
eMBMasterFuncReadCoils( UCHAR * pucFrame, USHORT * usLen )
|
||||
{
|
||||
UCHAR *ucMBFrame;
|
||||
USHORT usRegAddress;
|
||||
USHORT usCoilCount;
|
||||
UCHAR ucByteCount;
|
||||
|
||||
eMBException eStatus = MB_EX_NONE;
|
||||
eMBErrorCode eRegStatus;
|
||||
|
||||
/* If this request is broadcast, and it's read mode. This request don't need execute. */
|
||||
if ( xMBMasterRequestIsBroadcast() )
|
||||
{
|
||||
eStatus = MB_EX_NONE;
|
||||
}
|
||||
else if ( *usLen >= MB_PDU_SIZE_MIN + MB_PDU_FUNC_READ_SIZE_MIN )
|
||||
{
|
||||
vMBMasterGetPDUSndBuf(&ucMBFrame);
|
||||
usRegAddress = ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF] << 8 );
|
||||
usRegAddress |= ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF + 1] );
|
||||
usRegAddress++;
|
||||
|
||||
usCoilCount = ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_COILCNT_OFF] << 8 );
|
||||
usCoilCount |= ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_COILCNT_OFF + 1] );
|
||||
|
||||
/* Test if the quantity of coils is a multiple of 8. If not last
|
||||
* byte is only partially field with unused coils set to zero. */
|
||||
if( ( usCoilCount & 0x0007 ) != 0 )
|
||||
{
|
||||
ucByteCount = ( UCHAR )( usCoilCount / 8 + 1 );
|
||||
}
|
||||
else
|
||||
{
|
||||
ucByteCount = ( UCHAR )( usCoilCount / 8 );
|
||||
}
|
||||
|
||||
/* Check if the number of registers to read is valid. If not
|
||||
* return Modbus illegal data value exception.
|
||||
*/
|
||||
if( ( usCoilCount >= 1 ) &&
|
||||
( ucByteCount == pucFrame[MB_PDU_FUNC_READ_COILCNT_OFF] ) )
|
||||
{
|
||||
/* Make callback to fill the buffer. */
|
||||
eRegStatus = eMBMasterRegCoilsCB( &pucFrame[MB_PDU_FUNC_READ_VALUES_OFF], usRegAddress, usCoilCount, MB_REG_READ );
|
||||
|
||||
/* If an error occurred convert it into a Modbus exception. */
|
||||
if( eRegStatus != MB_ENOERR )
|
||||
{
|
||||
eStatus = prveMBError2Exception( eRegStatus );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Can't be a valid read coil register request because the length
|
||||
* is incorrect. */
|
||||
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
|
||||
}
|
||||
return eStatus;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if MB_FUNC_WRITE_COIL_ENABLED > 0
|
||||
|
||||
/**
|
||||
* This function will request write one coil.
|
||||
*
|
||||
* @param ucSndAddr salve address
|
||||
* @param usCoilAddr coil start address
|
||||
* @param usCoilData data to be written
|
||||
* @param lTimeOut timeout (-1 will waiting forever)
|
||||
*
|
||||
* @return error code
|
||||
*
|
||||
* @see eMBMasterReqWriteMultipleCoils
|
||||
*/
|
||||
eMBMasterReqErrCode
|
||||
eMBMasterReqWriteCoil( UCHAR ucSndAddr, USHORT usCoilAddr, USHORT usCoilData, LONG lTimeOut )
|
||||
{
|
||||
UCHAR *ucMBFrame;
|
||||
eMBMasterReqErrCode eErrStatus = MB_MRE_NO_ERR;
|
||||
|
||||
if ( ucSndAddr > MB_MASTER_TOTAL_SLAVE_NUM ) eErrStatus = MB_MRE_ILL_ARG;
|
||||
else if ( ( usCoilData != 0xFF00 ) && ( usCoilData != 0x0000 ) ) eErrStatus = MB_MRE_ILL_ARG;
|
||||
else if ( xMBMasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY;
|
||||
else
|
||||
{
|
||||
vMBMasterGetPDUSndBuf(&ucMBFrame);
|
||||
vMBMasterSetDestAddress(ucSndAddr);
|
||||
ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_WRITE_SINGLE_COIL;
|
||||
ucMBFrame[MB_PDU_REQ_WRITE_ADDR_OFF] = usCoilAddr >> 8;
|
||||
ucMBFrame[MB_PDU_REQ_WRITE_ADDR_OFF + 1] = usCoilAddr;
|
||||
ucMBFrame[MB_PDU_REQ_WRITE_VALUE_OFF ] = usCoilData >> 8;
|
||||
ucMBFrame[MB_PDU_REQ_WRITE_VALUE_OFF + 1] = usCoilData;
|
||||
vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_WRITE_SIZE );
|
||||
( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_TRANSMIT | EV_MASTER_TRANS_START );
|
||||
eErrStatus = eMBMasterWaitRequestFinish( );
|
||||
}
|
||||
return eErrStatus;
|
||||
}
|
||||
|
||||
eMBException
|
||||
eMBMasterFuncWriteCoil( UCHAR * pucFrame, USHORT * usLen )
|
||||
{
|
||||
USHORT usRegAddress;
|
||||
UCHAR ucBuf[2];
|
||||
|
||||
eMBException eStatus = MB_EX_NONE;
|
||||
eMBErrorCode eRegStatus;
|
||||
|
||||
if( *usLen == ( MB_PDU_FUNC_WRITE_SIZE + MB_PDU_SIZE_MIN ) )
|
||||
{
|
||||
usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_ADDR_OFF] << 8 );
|
||||
usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_ADDR_OFF + 1] );
|
||||
usRegAddress++;
|
||||
|
||||
if( ( pucFrame[MB_PDU_FUNC_WRITE_VALUE_OFF + 1] == 0x00 ) &&
|
||||
( ( pucFrame[MB_PDU_FUNC_WRITE_VALUE_OFF] == 0xFF ) ||
|
||||
( pucFrame[MB_PDU_FUNC_WRITE_VALUE_OFF] == 0x00 ) ) )
|
||||
{
|
||||
ucBuf[1] = 0;
|
||||
if( pucFrame[MB_PDU_FUNC_WRITE_VALUE_OFF] == 0xFF )
|
||||
{
|
||||
ucBuf[0] = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
ucBuf[0] = 0;
|
||||
}
|
||||
eRegStatus =
|
||||
eMBMasterRegCoilsCB( &ucBuf[0], usRegAddress, 1, MB_REG_WRITE );
|
||||
|
||||
/* If an error occured convert it into a Modbus exception. */
|
||||
if( eRegStatus != MB_ENOERR )
|
||||
{
|
||||
eStatus = prveMBError2Exception( eRegStatus );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Can't be a valid write coil register request because the length
|
||||
* is incorrect. */
|
||||
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
|
||||
}
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
#endif // #if MB_FUNC_WRITE_COIL_ENABLED > 0
|
||||
|
||||
#if MB_FUNC_WRITE_MULTIPLE_COILS_ENABLED > 0
|
||||
|
||||
/**
|
||||
* This function will request write multiple coils.
|
||||
*
|
||||
* @param ucSndAddr salve address
|
||||
* @param usCoilAddr coil start address
|
||||
* @param usNCoils coil total number
|
||||
* @param usCoilData data to be written
|
||||
* @param lTimeOut timeout (-1 will waiting forever)
|
||||
*
|
||||
* @return error code
|
||||
*
|
||||
* @see eMBMasterReqWriteCoil
|
||||
*/
|
||||
eMBMasterReqErrCode
|
||||
eMBMasterReqWriteMultipleCoils( UCHAR ucSndAddr,
|
||||
USHORT usCoilAddr, USHORT usNCoils, UCHAR * pucDataBuffer, LONG lTimeOut)
|
||||
{
|
||||
UCHAR *ucMBFrame;
|
||||
USHORT usRegIndex = 0;
|
||||
UCHAR ucByteCount;
|
||||
eMBMasterReqErrCode eErrStatus = MB_MRE_NO_ERR;
|
||||
|
||||
if ( ucSndAddr > MB_MASTER_TOTAL_SLAVE_NUM ) eErrStatus = MB_MRE_ILL_ARG;
|
||||
else if ( usNCoils > MB_PDU_REQ_WRITE_MUL_COILCNT_MAX ) eErrStatus = MB_MRE_ILL_ARG;
|
||||
else if ( xMBMasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY;
|
||||
else
|
||||
{
|
||||
vMBMasterGetPDUSndBuf(&ucMBFrame);
|
||||
vMBMasterSetDestAddress(ucSndAddr);
|
||||
ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_WRITE_MULTIPLE_COILS;
|
||||
ucMBFrame[MB_PDU_REQ_WRITE_MUL_ADDR_OFF] = usCoilAddr >> 8;
|
||||
ucMBFrame[MB_PDU_REQ_WRITE_MUL_ADDR_OFF + 1] = usCoilAddr;
|
||||
ucMBFrame[MB_PDU_REQ_WRITE_MUL_COILCNT_OFF] = usNCoils >> 8;
|
||||
ucMBFrame[MB_PDU_REQ_WRITE_MUL_COILCNT_OFF + 1] = usNCoils ;
|
||||
if( ( usNCoils & 0x0007 ) != 0 )
|
||||
{
|
||||
ucByteCount = ( UCHAR )( usNCoils / 8 + 1 );
|
||||
}
|
||||
else
|
||||
{
|
||||
ucByteCount = ( UCHAR )( usNCoils / 8 );
|
||||
}
|
||||
ucMBFrame[MB_PDU_REQ_WRITE_MUL_BYTECNT_OFF] = ucByteCount;
|
||||
ucMBFrame += MB_PDU_REQ_WRITE_MUL_VALUES_OFF;
|
||||
while( ucByteCount > usRegIndex)
|
||||
{
|
||||
*ucMBFrame++ = pucDataBuffer[usRegIndex++];
|
||||
}
|
||||
vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_WRITE_MUL_SIZE_MIN + ucByteCount );
|
||||
( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_TRANSMIT | EV_MASTER_TRANS_START );
|
||||
eErrStatus = eMBMasterWaitRequestFinish( );
|
||||
}
|
||||
return eErrStatus;
|
||||
}
|
||||
|
||||
eMBException
|
||||
eMBMasterFuncWriteMultipleCoils( UCHAR * pucFrame, USHORT * usLen )
|
||||
{
|
||||
USHORT usRegAddress;
|
||||
USHORT usCoilCnt;
|
||||
UCHAR ucByteCount;
|
||||
UCHAR ucByteCountVerify;
|
||||
UCHAR *ucMBFrame;
|
||||
|
||||
eMBException eStatus = MB_EX_NONE;
|
||||
eMBErrorCode eRegStatus;
|
||||
|
||||
/* If this request is broadcast, the *usLen is not need check. */
|
||||
if( ( *usLen == MB_PDU_FUNC_WRITE_MUL_SIZE ) || xMBMasterRequestIsBroadcast() )
|
||||
{
|
||||
vMBMasterGetPDUSndBuf(&ucMBFrame);
|
||||
usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_ADDR_OFF] << 8 );
|
||||
usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_ADDR_OFF + 1] );
|
||||
usRegAddress++;
|
||||
|
||||
usCoilCnt = ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_COILCNT_OFF] << 8 );
|
||||
usCoilCnt |= ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_COILCNT_OFF + 1] );
|
||||
|
||||
ucByteCount = ucMBFrame[MB_PDU_REQ_WRITE_MUL_BYTECNT_OFF];
|
||||
|
||||
/* Compute the number of expected bytes in the request. */
|
||||
if( ( usCoilCnt & 0x0007 ) != 0 )
|
||||
{
|
||||
ucByteCountVerify = ( UCHAR )( usCoilCnt / 8 + 1 );
|
||||
}
|
||||
else
|
||||
{
|
||||
ucByteCountVerify = ( UCHAR )( usCoilCnt / 8 );
|
||||
}
|
||||
|
||||
if( ( usCoilCnt >= 1 ) && ( ucByteCountVerify == ucByteCount ) )
|
||||
{
|
||||
eRegStatus =
|
||||
eMBMasterRegCoilsCB( &ucMBFrame[MB_PDU_REQ_WRITE_MUL_VALUES_OFF],
|
||||
usRegAddress, usCoilCnt, MB_REG_WRITE );
|
||||
|
||||
/* If an error occured convert it into a Modbus exception. */
|
||||
if( eRegStatus != MB_ENOERR )
|
||||
{
|
||||
eStatus = prveMBError2Exception( eRegStatus );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Can't be a valid write coil register request because the length
|
||||
* is incorrect. */
|
||||
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
|
||||
}
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
#endif // #if MB_FUNC_WRITE_MULTIPLE_COILS_ENABLED > 0
|
||||
#endif // #if MB_MASTER_RTU_ENABLED > 0 || MB_MASTER_ASCII_ENABLED > 0
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2006 Christian Walter
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*
|
||||
* SPDX-FileContributor: 2016-2021 Espressif Systems (Shanghai) CO LTD
|
||||
*/
|
||||
/*
|
||||
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
|
||||
* Copyright (c) 2006 Christian Walter <wolti@sil.at>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* File: $Id: mbfuncdiag.c,v 1.3 2006/12/07 22:10:34 wolti Exp $
|
||||
*/
|
||||
@@ -0,0 +1,143 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2006 Christian Walter
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*
|
||||
* SPDX-FileContributor: 2016-2021 Espressif Systems (Shanghai) CO LTD
|
||||
*/
|
||||
/*
|
||||
* FreeRTOS Modbus Libary: A Modbus serial implementation for FreeRTOS
|
||||
* Copyright (C) 2006 Christian Walter <wolti@sil.at>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* IF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* File: $Id: mbfuncdisc.c,v 1.10 2007/09/12 10:15:56 wolti Exp $
|
||||
*/
|
||||
|
||||
/* ----------------------- System includes ----------------------------------*/
|
||||
#include "stdlib.h"
|
||||
#include "string.h"
|
||||
|
||||
/* ----------------------- Platform includes --------------------------------*/
|
||||
#include "port.h"
|
||||
|
||||
/* ----------------------- Modbus includes ----------------------------------*/
|
||||
#include "mb.h"
|
||||
#include "mbframe.h"
|
||||
#include "mbproto.h"
|
||||
#include "mbconfig.h"
|
||||
|
||||
/* ----------------------- Defines ------------------------------------------*/
|
||||
#define MB_PDU_FUNC_READ_ADDR_OFF ( MB_PDU_DATA_OFF )
|
||||
#define MB_PDU_FUNC_READ_DISCCNT_OFF ( MB_PDU_DATA_OFF + 2 )
|
||||
#define MB_PDU_FUNC_READ_SIZE ( 4 )
|
||||
#define MB_PDU_FUNC_READ_DISCCNT_MAX ( 0x07D0 )
|
||||
|
||||
/* ----------------------- Static functions ---------------------------------*/
|
||||
eMBException prveMBError2Exception( eMBErrorCode eErrorCode );
|
||||
|
||||
/* ----------------------- Start implementation -----------------------------*/
|
||||
#if MB_SLAVE_RTU_ENABLED || MB_SLAVE_ASCII_ENABLED || MB_TCP_ENABLED
|
||||
|
||||
#if MB_FUNC_READ_COILS_ENABLED
|
||||
|
||||
eMBException
|
||||
eMBFuncReadDiscreteInputs( UCHAR * pucFrame, USHORT * usLen )
|
||||
{
|
||||
USHORT usRegAddress;
|
||||
USHORT usDiscreteCnt;
|
||||
UCHAR ucNBytes;
|
||||
UCHAR *pucFrameCur;
|
||||
|
||||
eMBException eStatus = MB_EX_NONE;
|
||||
eMBErrorCode eRegStatus;
|
||||
|
||||
if( *usLen == ( MB_PDU_FUNC_READ_SIZE + MB_PDU_SIZE_MIN ) )
|
||||
{
|
||||
usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF] << 8 );
|
||||
usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF + 1] );
|
||||
usRegAddress++;
|
||||
|
||||
usDiscreteCnt = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_DISCCNT_OFF] << 8 );
|
||||
usDiscreteCnt |= ( USHORT )( pucFrame[MB_PDU_FUNC_READ_DISCCNT_OFF + 1] );
|
||||
|
||||
/* Check if the number of registers to read is valid. If not
|
||||
* return Modbus illegal data value exception.
|
||||
*/
|
||||
if( ( usDiscreteCnt >= 1 ) &&
|
||||
( usDiscreteCnt < MB_PDU_FUNC_READ_DISCCNT_MAX ) )
|
||||
{
|
||||
/* Set the current PDU data pointer to the beginning. */
|
||||
pucFrameCur = &pucFrame[MB_PDU_FUNC_OFF];
|
||||
*usLen = MB_PDU_FUNC_OFF;
|
||||
|
||||
/* First byte contains the function code. */
|
||||
*pucFrameCur++ = MB_FUNC_READ_DISCRETE_INPUTS;
|
||||
*usLen += 1;
|
||||
|
||||
/* Test if the quantity of coils is a multiple of 8. If not last
|
||||
* byte is only partially field with unused coils set to zero. */
|
||||
if( ( usDiscreteCnt & 0x0007 ) != 0 )
|
||||
{
|
||||
ucNBytes = ( UCHAR ) ( usDiscreteCnt / 8 + 1 );
|
||||
}
|
||||
else
|
||||
{
|
||||
ucNBytes = ( UCHAR ) ( usDiscreteCnt / 8 );
|
||||
}
|
||||
*pucFrameCur++ = ucNBytes;
|
||||
*usLen += 1;
|
||||
|
||||
eRegStatus =
|
||||
eMBRegDiscreteCB( pucFrameCur, usRegAddress, usDiscreteCnt );
|
||||
|
||||
/* If an error occured convert it into a Modbus exception. */
|
||||
if( eRegStatus != MB_ENOERR )
|
||||
{
|
||||
eStatus = prveMBError2Exception( eRegStatus );
|
||||
}
|
||||
else
|
||||
{
|
||||
/* The response contains the function code, the starting address
|
||||
* and the quantity of registers. We reuse the old values in the
|
||||
* buffer because they are still valid. */
|
||||
*usLen += ucNBytes;;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Can't be a valid read coil register request because the length
|
||||
* is incorrect. */
|
||||
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
|
||||
}
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,167 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2013 Armink
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*
|
||||
* SPDX-FileContributor: 2016-2021 Espressif Systems (Shanghai) CO LTD
|
||||
*/
|
||||
/*
|
||||
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
|
||||
* Copyright (C) 2013 Armink <armink.ztl@gmail.com>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* File: $Id: mbfuncdisc_m.c,v 1.60 2013/10/15 8:48:20 Armink Add Master Functions Exp $
|
||||
*/
|
||||
|
||||
/* ----------------------- System includes ----------------------------------*/
|
||||
#include "stdlib.h"
|
||||
#include "string.h"
|
||||
|
||||
/* ----------------------- Platform includes --------------------------------*/
|
||||
#include "port.h"
|
||||
|
||||
/* ----------------------- Modbus includes ----------------------------------*/
|
||||
#include "mb_m.h"
|
||||
#include "mbframe.h"
|
||||
#include "mbproto.h"
|
||||
#include "mbconfig.h"
|
||||
|
||||
/* ----------------------- Defines ------------------------------------------*/
|
||||
#define MB_PDU_REQ_READ_ADDR_OFF ( MB_PDU_DATA_OFF + 0 )
|
||||
#define MB_PDU_REQ_READ_DISCCNT_OFF ( MB_PDU_DATA_OFF + 2 )
|
||||
#define MB_PDU_REQ_READ_SIZE ( 4 )
|
||||
#define MB_PDU_FUNC_READ_DISCCNT_OFF ( MB_PDU_DATA_OFF + 0 )
|
||||
#define MB_PDU_FUNC_READ_VALUES_OFF ( MB_PDU_DATA_OFF + 1 )
|
||||
#define MB_PDU_FUNC_READ_SIZE_MIN ( 1 )
|
||||
|
||||
/* ----------------------- Static functions ---------------------------------*/
|
||||
eMBException prveMBError2Exception( eMBErrorCode eErrorCode );
|
||||
|
||||
/* ----------------------- Start implementation -----------------------------*/
|
||||
#if MB_MASTER_RTU_ENABLED || MB_MASTER_ASCII_ENABLED || MB_MASTER_TCP_ENABLED
|
||||
|
||||
#if MB_FUNC_READ_DISCRETE_INPUTS_ENABLED
|
||||
|
||||
/**
|
||||
* This function will request read discrete inputs.
|
||||
*
|
||||
* @param ucSndAddr salve address
|
||||
* @param usDiscreteAddr discrete start address
|
||||
* @param usNDiscreteIn discrete total number
|
||||
* @param lTimeOut timeout (-1 will waiting forever)
|
||||
*
|
||||
* @return error code
|
||||
*/
|
||||
eMBMasterReqErrCode
|
||||
eMBMasterReqReadDiscreteInputs( UCHAR ucSndAddr, USHORT usDiscreteAddr, USHORT usNDiscreteIn, LONG lTimeOut )
|
||||
{
|
||||
UCHAR *ucMBFrame;
|
||||
eMBMasterReqErrCode eErrStatus = MB_MRE_NO_ERR;
|
||||
|
||||
if ( ucSndAddr > MB_MASTER_TOTAL_SLAVE_NUM ) eErrStatus = MB_MRE_ILL_ARG;
|
||||
else if ( xMBMasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY;
|
||||
else
|
||||
{
|
||||
vMBMasterGetPDUSndBuf(&ucMBFrame);
|
||||
vMBMasterSetDestAddress(ucSndAddr);
|
||||
ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_READ_DISCRETE_INPUTS;
|
||||
ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF] = usDiscreteAddr >> 8;
|
||||
ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF + 1] = usDiscreteAddr;
|
||||
ucMBFrame[MB_PDU_REQ_READ_DISCCNT_OFF ] = usNDiscreteIn >> 8;
|
||||
ucMBFrame[MB_PDU_REQ_READ_DISCCNT_OFF + 1] = usNDiscreteIn;
|
||||
vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_READ_SIZE );
|
||||
( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_TRANSMIT | EV_MASTER_TRANS_START );
|
||||
eErrStatus = eMBMasterWaitRequestFinish( );
|
||||
}
|
||||
return eErrStatus;
|
||||
}
|
||||
|
||||
eMBException
|
||||
eMBMasterFuncReadDiscreteInputs( UCHAR * pucFrame, USHORT * usLen )
|
||||
{
|
||||
USHORT usRegAddress;
|
||||
USHORT usDiscreteCnt;
|
||||
UCHAR ucNBytes;
|
||||
UCHAR *ucMBFrame;
|
||||
|
||||
eMBException eStatus = MB_EX_NONE;
|
||||
eMBErrorCode eRegStatus;
|
||||
|
||||
/* If this request is broadcast, and it's read mode. This request don't need execute. */
|
||||
if ( xMBMasterRequestIsBroadcast() )
|
||||
{
|
||||
eStatus = MB_EX_NONE;
|
||||
}
|
||||
else if( *usLen >= MB_PDU_SIZE_MIN + MB_PDU_FUNC_READ_SIZE_MIN )
|
||||
{
|
||||
vMBMasterGetPDUSndBuf(&ucMBFrame);
|
||||
usRegAddress = ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF] << 8 );
|
||||
usRegAddress |= ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF + 1] );
|
||||
usRegAddress++;
|
||||
|
||||
usDiscreteCnt = ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_DISCCNT_OFF] << 8 );
|
||||
usDiscreteCnt |= ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_DISCCNT_OFF + 1] );
|
||||
|
||||
/* Test if the quantity of coils is a multiple of 8. If not last
|
||||
* byte is only partially field with unused coils set to zero. */
|
||||
if( ( usDiscreteCnt & 0x0007 ) != 0 )
|
||||
{
|
||||
ucNBytes = ( UCHAR )( usDiscreteCnt / 8 + 1 );
|
||||
}
|
||||
else
|
||||
{
|
||||
ucNBytes = ( UCHAR )( usDiscreteCnt / 8 );
|
||||
}
|
||||
|
||||
/* Check if the number of registers to read is valid. If not
|
||||
* return Modbus illegal data value exception.
|
||||
*/
|
||||
if ((usDiscreteCnt >= 1) && ucNBytes == pucFrame[MB_PDU_FUNC_READ_DISCCNT_OFF])
|
||||
{
|
||||
/* Make callback to fill the buffer. */
|
||||
eRegStatus = eMBMasterRegDiscreteCB( &pucFrame[MB_PDU_FUNC_READ_VALUES_OFF], usRegAddress, usDiscreteCnt );
|
||||
|
||||
/* If an error occured convert it into a Modbus exception. */
|
||||
if( eRegStatus != MB_ENOERR )
|
||||
{
|
||||
eStatus = prveMBError2Exception( eRegStatus );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Can't be a valid read coil register request because the length
|
||||
* is incorrect. */
|
||||
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
|
||||
}
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
#endif
|
||||
#endif // #if MB_SERIAL_MASTER_RTU_ENABLED || MB_SERIAL_MASTER_ASCII_ENABLED || MB_MASTER_TCP_ENABLED
|
||||
@@ -0,0 +1,318 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2006 Christian Walter
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*
|
||||
* SPDX-FileContributor: 2016-2021 Espressif Systems (Shanghai) CO LTD
|
||||
*/
|
||||
/*
|
||||
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
|
||||
* Copyright (c) 2006 Christian Walter <wolti@sil.at>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* File: $Id: mbfuncholding.c,v 1.12 2007/02/18 23:48:22 wolti Exp $
|
||||
*/
|
||||
|
||||
/* ----------------------- System includes ----------------------------------*/
|
||||
#include "stdlib.h"
|
||||
#include "string.h"
|
||||
|
||||
/* ----------------------- Platform includes --------------------------------*/
|
||||
#include "port.h"
|
||||
|
||||
/* ----------------------- Modbus includes ----------------------------------*/
|
||||
#include "mb.h"
|
||||
#include "mbframe.h"
|
||||
#include "mbproto.h"
|
||||
#include "mbconfig.h"
|
||||
|
||||
/* ----------------------- Defines ------------------------------------------*/
|
||||
#define MB_PDU_FUNC_READ_ADDR_OFF ( MB_PDU_DATA_OFF + 0)
|
||||
#define MB_PDU_FUNC_READ_REGCNT_OFF ( MB_PDU_DATA_OFF + 2 )
|
||||
#define MB_PDU_FUNC_READ_SIZE ( 4 )
|
||||
#define MB_PDU_FUNC_READ_REGCNT_MAX ( 0x007D )
|
||||
|
||||
#define MB_PDU_FUNC_WRITE_ADDR_OFF ( MB_PDU_DATA_OFF + 0)
|
||||
#define MB_PDU_FUNC_WRITE_VALUE_OFF ( MB_PDU_DATA_OFF + 2 )
|
||||
#define MB_PDU_FUNC_WRITE_SIZE ( 4 )
|
||||
|
||||
#define MB_PDU_FUNC_WRITE_MUL_ADDR_OFF ( MB_PDU_DATA_OFF + 0 )
|
||||
#define MB_PDU_FUNC_WRITE_MUL_REGCNT_OFF ( MB_PDU_DATA_OFF + 2 )
|
||||
#define MB_PDU_FUNC_WRITE_MUL_BYTECNT_OFF ( MB_PDU_DATA_OFF + 4 )
|
||||
#define MB_PDU_FUNC_WRITE_MUL_VALUES_OFF ( MB_PDU_DATA_OFF + 5 )
|
||||
#define MB_PDU_FUNC_WRITE_MUL_SIZE_MIN ( 5 )
|
||||
#define MB_PDU_FUNC_WRITE_MUL_REGCNT_MAX ( 0x0078 )
|
||||
|
||||
#define MB_PDU_FUNC_READWRITE_READ_ADDR_OFF ( MB_PDU_DATA_OFF + 0 )
|
||||
#define MB_PDU_FUNC_READWRITE_READ_REGCNT_OFF ( MB_PDU_DATA_OFF + 2 )
|
||||
#define MB_PDU_FUNC_READWRITE_WRITE_ADDR_OFF ( MB_PDU_DATA_OFF + 4 )
|
||||
#define MB_PDU_FUNC_READWRITE_WRITE_REGCNT_OFF ( MB_PDU_DATA_OFF + 6 )
|
||||
#define MB_PDU_FUNC_READWRITE_BYTECNT_OFF ( MB_PDU_DATA_OFF + 8 )
|
||||
#define MB_PDU_FUNC_READWRITE_WRITE_VALUES_OFF ( MB_PDU_DATA_OFF + 9 )
|
||||
#define MB_PDU_FUNC_READWRITE_SIZE_MIN ( 9 )
|
||||
|
||||
/* ----------------------- Static functions ---------------------------------*/
|
||||
eMBException prveMBError2Exception( eMBErrorCode eErrorCode );
|
||||
|
||||
/* ----------------------- Start implementation -----------------------------*/
|
||||
#if MB_SLAVE_RTU_ENABLED || MB_SLAVE_ASCII_ENABLED || MB_TCP_ENABLED
|
||||
|
||||
#if MB_FUNC_WRITE_HOLDING_ENABLED
|
||||
|
||||
eMBException
|
||||
eMBFuncWriteHoldingRegister( UCHAR * pucFrame, USHORT * usLen )
|
||||
{
|
||||
USHORT usRegAddress;
|
||||
eMBException eStatus = MB_EX_NONE;
|
||||
eMBErrorCode eRegStatus;
|
||||
|
||||
if( *usLen == ( MB_PDU_FUNC_WRITE_SIZE + MB_PDU_SIZE_MIN ) )
|
||||
{
|
||||
usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_ADDR_OFF] << 8 );
|
||||
usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_ADDR_OFF + 1] );
|
||||
usRegAddress++;
|
||||
|
||||
/* Make callback to update the value. */
|
||||
eRegStatus = eMBRegHoldingCB( &pucFrame[MB_PDU_FUNC_WRITE_VALUE_OFF],
|
||||
usRegAddress, 1, MB_REG_WRITE );
|
||||
|
||||
/* If an error occured convert it into a Modbus exception. */
|
||||
if( eRegStatus != MB_ENOERR )
|
||||
{
|
||||
eStatus = prveMBError2Exception( eRegStatus );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Can't be a valid request because the length is incorrect. */
|
||||
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
|
||||
}
|
||||
return eStatus;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if MB_FUNC_WRITE_MULTIPLE_HOLDING_ENABLED > 0
|
||||
eMBException
|
||||
eMBFuncWriteMultipleHoldingRegister( UCHAR * pucFrame, USHORT * usLen )
|
||||
{
|
||||
USHORT usRegAddress;
|
||||
USHORT usRegCount;
|
||||
UCHAR ucRegByteCount;
|
||||
|
||||
eMBException eStatus = MB_EX_NONE;
|
||||
eMBErrorCode eRegStatus;
|
||||
|
||||
if( *usLen >= ( MB_PDU_FUNC_WRITE_MUL_SIZE_MIN + MB_PDU_SIZE_MIN ) )
|
||||
{
|
||||
usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_ADDR_OFF] << 8 );
|
||||
usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_ADDR_OFF + 1] );
|
||||
usRegAddress++;
|
||||
|
||||
usRegCount = ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_REGCNT_OFF] << 8 );
|
||||
usRegCount |= ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_REGCNT_OFF + 1] );
|
||||
|
||||
ucRegByteCount = pucFrame[MB_PDU_FUNC_WRITE_MUL_BYTECNT_OFF];
|
||||
|
||||
if( ( usRegCount >= 1 ) &&
|
||||
( usRegCount <= MB_PDU_FUNC_WRITE_MUL_REGCNT_MAX ) &&
|
||||
( ucRegByteCount == ( UCHAR ) ( 2 * usRegCount ) ) )
|
||||
{
|
||||
/* Make callback to update the register values. */
|
||||
eRegStatus =
|
||||
eMBRegHoldingCB( &pucFrame[MB_PDU_FUNC_WRITE_MUL_VALUES_OFF],
|
||||
usRegAddress, usRegCount, MB_REG_WRITE );
|
||||
|
||||
/* If an error occured convert it into a Modbus exception. */
|
||||
if( eRegStatus != MB_ENOERR )
|
||||
{
|
||||
eStatus = prveMBError2Exception( eRegStatus );
|
||||
}
|
||||
else
|
||||
{
|
||||
/* The response contains the function code, the starting
|
||||
* address and the quantity of registers. We reuse the
|
||||
* old values in the buffer because they are still valid.
|
||||
*/
|
||||
*usLen = MB_PDU_FUNC_WRITE_MUL_BYTECNT_OFF;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Can't be a valid request because the length is incorrect. */
|
||||
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
|
||||
}
|
||||
return eStatus;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if MB_FUNC_READ_HOLDING_ENABLED > 0
|
||||
|
||||
eMBException
|
||||
eMBFuncReadHoldingRegister( UCHAR * pucFrame, USHORT * usLen )
|
||||
{
|
||||
USHORT usRegAddress;
|
||||
USHORT usRegCount;
|
||||
UCHAR *pucFrameCur;
|
||||
|
||||
eMBException eStatus = MB_EX_NONE;
|
||||
eMBErrorCode eRegStatus;
|
||||
|
||||
if( *usLen == ( MB_PDU_FUNC_READ_SIZE + MB_PDU_SIZE_MIN ) )
|
||||
{
|
||||
usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF] << 8 );
|
||||
usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF + 1] );
|
||||
usRegAddress++;
|
||||
|
||||
usRegCount = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_REGCNT_OFF] << 8 );
|
||||
usRegCount |= ( USHORT )( pucFrame[MB_PDU_FUNC_READ_REGCNT_OFF + 1] );
|
||||
|
||||
/* Check if the number of registers to read is valid. If not
|
||||
* return Modbus illegal data value exception.
|
||||
*/
|
||||
if( ( usRegCount >= 1 ) && ( usRegCount <= MB_PDU_FUNC_READ_REGCNT_MAX ) )
|
||||
{
|
||||
/* Set the current PDU data pointer to the beginning. */
|
||||
pucFrameCur = &pucFrame[MB_PDU_FUNC_OFF];
|
||||
*usLen = MB_PDU_FUNC_OFF;
|
||||
|
||||
/* First byte contains the function code. */
|
||||
*pucFrameCur++ = MB_FUNC_READ_HOLDING_REGISTER;
|
||||
*usLen += 1;
|
||||
|
||||
/* Second byte in the response contain the number of bytes. */
|
||||
*pucFrameCur++ = ( UCHAR ) ( usRegCount * 2 );
|
||||
*usLen += 1;
|
||||
|
||||
/* Make callback to fill the buffer. */
|
||||
eRegStatus = eMBRegHoldingCB( pucFrameCur, usRegAddress, usRegCount, MB_REG_READ );
|
||||
/* If an error occured convert it into a Modbus exception. */
|
||||
if( eRegStatus != MB_ENOERR )
|
||||
{
|
||||
eStatus = prveMBError2Exception( eRegStatus );
|
||||
}
|
||||
else
|
||||
{
|
||||
*usLen += usRegCount * 2;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Can't be a valid request because the length is incorrect. */
|
||||
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
|
||||
}
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#if MB_FUNC_READWRITE_HOLDING_ENABLED > 0
|
||||
|
||||
eMBException
|
||||
eMBFuncReadWriteMultipleHoldingRegister( UCHAR * pucFrame, USHORT * usLen )
|
||||
{
|
||||
USHORT usRegReadAddress;
|
||||
USHORT usRegReadCount;
|
||||
USHORT usRegWriteAddress;
|
||||
USHORT usRegWriteCount;
|
||||
UCHAR ucRegWriteByteCount;
|
||||
UCHAR *pucFrameCur;
|
||||
|
||||
eMBException eStatus = MB_EX_NONE;
|
||||
eMBErrorCode eRegStatus;
|
||||
|
||||
if( *usLen >= ( MB_PDU_FUNC_READWRITE_SIZE_MIN + MB_PDU_SIZE_MIN ) )
|
||||
{
|
||||
usRegReadAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_READ_ADDR_OFF] << 8U );
|
||||
usRegReadAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_READ_ADDR_OFF + 1] );
|
||||
usRegReadAddress++;
|
||||
|
||||
usRegReadCount = ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_READ_REGCNT_OFF] << 8U );
|
||||
usRegReadCount |= ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_READ_REGCNT_OFF + 1] );
|
||||
|
||||
usRegWriteAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_WRITE_ADDR_OFF] << 8U );
|
||||
usRegWriteAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_WRITE_ADDR_OFF + 1] );
|
||||
usRegWriteAddress++;
|
||||
|
||||
usRegWriteCount = ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_WRITE_REGCNT_OFF] << 8U );
|
||||
usRegWriteCount |= ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_WRITE_REGCNT_OFF + 1] );
|
||||
|
||||
ucRegWriteByteCount = pucFrame[MB_PDU_FUNC_READWRITE_BYTECNT_OFF];
|
||||
|
||||
if( ( usRegReadCount >= 1 ) && ( usRegReadCount <= 0x7D ) &&
|
||||
( usRegWriteCount >= 1 ) && ( usRegWriteCount <= 0x79 ) &&
|
||||
( ( 2 * usRegWriteCount ) == ucRegWriteByteCount ) )
|
||||
{
|
||||
/* Make callback to update the register values. */
|
||||
eRegStatus = eMBRegHoldingCB( &pucFrame[MB_PDU_FUNC_READWRITE_WRITE_VALUES_OFF],
|
||||
usRegWriteAddress, usRegWriteCount, MB_REG_WRITE );
|
||||
|
||||
if( eRegStatus == MB_ENOERR )
|
||||
{
|
||||
/* Set the current PDU data pointer to the beginning. */
|
||||
pucFrameCur = &pucFrame[MB_PDU_FUNC_OFF];
|
||||
*usLen = MB_PDU_FUNC_OFF;
|
||||
|
||||
/* First byte contains the function code. */
|
||||
*pucFrameCur++ = MB_FUNC_READWRITE_MULTIPLE_REGISTERS;
|
||||
*usLen += 1;
|
||||
|
||||
/* Second byte in the response contain the number of bytes. */
|
||||
*pucFrameCur++ = ( UCHAR ) ( usRegReadCount * 2 );
|
||||
*usLen += 1;
|
||||
|
||||
/* Make the read callback. */
|
||||
eRegStatus =
|
||||
eMBRegHoldingCB( pucFrameCur, usRegReadAddress, usRegReadCount, MB_REG_READ );
|
||||
if( eRegStatus == MB_ENOERR )
|
||||
{
|
||||
*usLen += 2 * usRegReadCount;
|
||||
}
|
||||
}
|
||||
if( eRegStatus != MB_ENOERR )
|
||||
{
|
||||
eStatus = prveMBError2Exception( eRegStatus );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
|
||||
}
|
||||
}
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,461 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2013 Armink
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*
|
||||
* SPDX-FileContributor: 2016-2021 Espressif Systems (Shanghai) CO LTD
|
||||
*/
|
||||
/*
|
||||
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
|
||||
* Copyright (C) 2013 Armink <armink.ztl@gmail.com>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* File: $Id: mbfuncholding_m.c,v 1.60 2013/09/02 14:13:40 Armink Add Master Functions Exp $
|
||||
*/
|
||||
|
||||
/* ----------------------- System includes ----------------------------------*/
|
||||
#include "stdlib.h"
|
||||
#include "string.h"
|
||||
|
||||
/* ----------------------- Platform includes --------------------------------*/
|
||||
#include "port.h"
|
||||
|
||||
/* ----------------------- Modbus includes ----------------------------------*/
|
||||
//#include "mb.h"
|
||||
#include "mb_m.h"
|
||||
#include "mbframe.h"
|
||||
#include "mbproto.h"
|
||||
#include "mbconfig.h"
|
||||
|
||||
/* ----------------------- Defines ------------------------------------------*/
|
||||
#define MB_PDU_REQ_READ_ADDR_OFF ( MB_PDU_DATA_OFF + 0 )
|
||||
#define MB_PDU_REQ_READ_REGCNT_OFF ( MB_PDU_DATA_OFF + 2 )
|
||||
#define MB_PDU_REQ_READ_SIZE ( 4 )
|
||||
#define MB_PDU_FUNC_READ_REGCNT_MAX ( 0x007D )
|
||||
#define MB_PDU_FUNC_READ_BYTECNT_OFF ( MB_PDU_DATA_OFF + 0 )
|
||||
#define MB_PDU_FUNC_READ_VALUES_OFF ( MB_PDU_DATA_OFF + 1 )
|
||||
#define MB_PDU_FUNC_READ_SIZE_MIN ( 1 )
|
||||
|
||||
#define MB_PDU_REQ_WRITE_ADDR_OFF ( MB_PDU_DATA_OFF + 0)
|
||||
#define MB_PDU_REQ_WRITE_VALUE_OFF ( MB_PDU_DATA_OFF + 2 )
|
||||
#define MB_PDU_REQ_WRITE_SIZE ( 4 )
|
||||
#define MB_PDU_FUNC_WRITE_ADDR_OFF ( MB_PDU_DATA_OFF + 0)
|
||||
#define MB_PDU_FUNC_WRITE_VALUE_OFF ( MB_PDU_DATA_OFF + 2 )
|
||||
#define MB_PDU_FUNC_WRITE_SIZE ( 4 )
|
||||
|
||||
#define MB_PDU_REQ_WRITE_MUL_ADDR_OFF ( MB_PDU_DATA_OFF + 0 )
|
||||
#define MB_PDU_REQ_WRITE_MUL_REGCNT_OFF ( MB_PDU_DATA_OFF + 2 )
|
||||
#define MB_PDU_REQ_WRITE_MUL_BYTECNT_OFF ( MB_PDU_DATA_OFF + 4 )
|
||||
#define MB_PDU_REQ_WRITE_MUL_VALUES_OFF ( MB_PDU_DATA_OFF + 5 )
|
||||
#define MB_PDU_REQ_WRITE_MUL_SIZE_MIN ( 5 )
|
||||
#define MB_PDU_REQ_WRITE_MUL_REGCNT_MAX ( 0x0078 )
|
||||
#define MB_PDU_FUNC_WRITE_MUL_ADDR_OFF ( MB_PDU_DATA_OFF + 0 )
|
||||
#define MB_PDU_FUNC_WRITE_MUL_REGCNT_OFF ( MB_PDU_DATA_OFF + 2 )
|
||||
#define MB_PDU_FUNC_WRITE_MUL_SIZE ( 4 )
|
||||
|
||||
#define MB_PDU_REQ_READWRITE_READ_ADDR_OFF ( MB_PDU_DATA_OFF + 0 )
|
||||
#define MB_PDU_REQ_READWRITE_READ_REGCNT_OFF ( MB_PDU_DATA_OFF + 2 )
|
||||
#define MB_PDU_REQ_READWRITE_WRITE_ADDR_OFF ( MB_PDU_DATA_OFF + 4 )
|
||||
#define MB_PDU_REQ_READWRITE_WRITE_REGCNT_OFF ( MB_PDU_DATA_OFF + 6 )
|
||||
#define MB_PDU_REQ_READWRITE_WRITE_BYTECNT_OFF ( MB_PDU_DATA_OFF + 8 )
|
||||
#define MB_PDU_REQ_READWRITE_WRITE_VALUES_OFF ( MB_PDU_DATA_OFF + 9 )
|
||||
#define MB_PDU_REQ_READWRITE_SIZE_MIN ( 9 )
|
||||
#define MB_PDU_FUNC_READWRITE_READ_BYTECNT_OFF ( MB_PDU_DATA_OFF + 0 )
|
||||
#define MB_PDU_FUNC_READWRITE_READ_VALUES_OFF ( MB_PDU_DATA_OFF + 1 )
|
||||
#define MB_PDU_FUNC_READWRITE_SIZE_MIN ( 1 )
|
||||
|
||||
/* ----------------------- Static functions ---------------------------------*/
|
||||
eMBException prveMBError2Exception( eMBErrorCode eErrorCode );
|
||||
|
||||
/* ----------------------- Start implementation -----------------------------*/
|
||||
#if MB_MASTER_RTU_ENABLED || MB_MASTER_ASCII_ENABLED || MB_MASTER_TCP_ENABLED
|
||||
|
||||
#if MB_FUNC_WRITE_HOLDING_ENABLED
|
||||
|
||||
/**
|
||||
* This function will request write holding register.
|
||||
*
|
||||
* @param ucSndAddr salve address
|
||||
* @param usRegAddr register start address
|
||||
* @param usRegData register data to be written
|
||||
* @param lTimeOut timeout (-1 will waiting forever)
|
||||
*
|
||||
* @return error code
|
||||
*/
|
||||
eMBMasterReqErrCode
|
||||
eMBMasterReqWriteHoldingRegister( UCHAR ucSndAddr, USHORT usRegAddr, USHORT usRegData, LONG lTimeOut )
|
||||
{
|
||||
UCHAR *ucMBFrame;
|
||||
eMBMasterReqErrCode eErrStatus = MB_MRE_NO_ERR;
|
||||
|
||||
if ( ucSndAddr > MB_MASTER_TOTAL_SLAVE_NUM ) eErrStatus = MB_MRE_ILL_ARG;
|
||||
else if ( xMBMasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY;
|
||||
else
|
||||
{
|
||||
vMBMasterGetPDUSndBuf(&ucMBFrame);
|
||||
vMBMasterSetDestAddress(ucSndAddr);
|
||||
ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_WRITE_REGISTER;
|
||||
ucMBFrame[MB_PDU_REQ_WRITE_ADDR_OFF] = usRegAddr >> 8;
|
||||
ucMBFrame[MB_PDU_REQ_WRITE_ADDR_OFF + 1] = usRegAddr;
|
||||
ucMBFrame[MB_PDU_REQ_WRITE_VALUE_OFF] = usRegData >> 8;
|
||||
ucMBFrame[MB_PDU_REQ_WRITE_VALUE_OFF + 1] = usRegData ;
|
||||
vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_WRITE_SIZE );
|
||||
( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_TRANSMIT | EV_MASTER_TRANS_START );
|
||||
eErrStatus = eMBMasterWaitRequestFinish( );
|
||||
}
|
||||
return eErrStatus;
|
||||
}
|
||||
|
||||
eMBException
|
||||
eMBMasterFuncWriteHoldingRegister( UCHAR * pucFrame, USHORT * usLen )
|
||||
{
|
||||
USHORT usRegAddress;
|
||||
eMBException eStatus = MB_EX_NONE;
|
||||
eMBErrorCode eRegStatus;
|
||||
|
||||
if( *usLen == ( MB_PDU_SIZE_MIN + MB_PDU_FUNC_WRITE_SIZE ) )
|
||||
{
|
||||
usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_ADDR_OFF] << 8 );
|
||||
usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_ADDR_OFF + 1] );
|
||||
usRegAddress++;
|
||||
|
||||
/* Make callback to update the value. */
|
||||
eRegStatus = eMBMasterRegHoldingCB( &pucFrame[MB_PDU_FUNC_WRITE_VALUE_OFF],
|
||||
usRegAddress, 1, MB_REG_WRITE );
|
||||
|
||||
/* If an error occured convert it into a Modbus exception. */
|
||||
if( eRegStatus != MB_ENOERR )
|
||||
{
|
||||
eStatus = prveMBError2Exception( eRegStatus );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Can't be a valid request because the length is incorrect. */
|
||||
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
|
||||
}
|
||||
return eStatus;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if MB_FUNC_WRITE_MULTIPLE_HOLDING_ENABLED > 0
|
||||
|
||||
/**
|
||||
* This function will request write multiple holding register.
|
||||
*
|
||||
* @param ucSndAddr salve address
|
||||
* @param usRegAddr register start address
|
||||
* @param usNRegs register total number
|
||||
* @param pusDataBuffer data to be written
|
||||
* @param lTimeOut timeout (-1 will waiting forever)
|
||||
*
|
||||
* @return error code
|
||||
*/
|
||||
eMBMasterReqErrCode
|
||||
eMBMasterReqWriteMultipleHoldingRegister( UCHAR ucSndAddr,
|
||||
USHORT usRegAddr, USHORT usNRegs, USHORT * pusDataBuffer, LONG lTimeOut )
|
||||
{
|
||||
UCHAR *ucMBFrame;
|
||||
USHORT usRegIndex = 0;
|
||||
eMBMasterReqErrCode eErrStatus = MB_MRE_NO_ERR;
|
||||
|
||||
if ( ucSndAddr > MB_MASTER_TOTAL_SLAVE_NUM ) eErrStatus = MB_MRE_ILL_ARG;
|
||||
else if ( xMBMasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY;
|
||||
else
|
||||
{
|
||||
vMBMasterGetPDUSndBuf(&ucMBFrame);
|
||||
vMBMasterSetDestAddress(ucSndAddr);
|
||||
ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_WRITE_MULTIPLE_REGISTERS;
|
||||
ucMBFrame[MB_PDU_REQ_WRITE_MUL_ADDR_OFF] = usRegAddr >> 8;
|
||||
ucMBFrame[MB_PDU_REQ_WRITE_MUL_ADDR_OFF + 1] = usRegAddr;
|
||||
ucMBFrame[MB_PDU_REQ_WRITE_MUL_REGCNT_OFF] = usNRegs >> 8;
|
||||
ucMBFrame[MB_PDU_REQ_WRITE_MUL_REGCNT_OFF + 1] = usNRegs ;
|
||||
ucMBFrame[MB_PDU_REQ_WRITE_MUL_BYTECNT_OFF] = usNRegs * 2;
|
||||
ucMBFrame += MB_PDU_REQ_WRITE_MUL_VALUES_OFF;
|
||||
while( usNRegs > usRegIndex)
|
||||
{
|
||||
*ucMBFrame++ = pusDataBuffer[usRegIndex] >> 8;
|
||||
*ucMBFrame++ = pusDataBuffer[usRegIndex++] ;
|
||||
}
|
||||
vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_WRITE_MUL_SIZE_MIN + 2*usNRegs );
|
||||
( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_TRANSMIT | EV_MASTER_TRANS_START );
|
||||
eErrStatus = eMBMasterWaitRequestFinish( );
|
||||
}
|
||||
return eErrStatus;
|
||||
}
|
||||
|
||||
eMBException
|
||||
eMBMasterFuncWriteMultipleHoldingRegister( UCHAR * pucFrame, USHORT * usLen )
|
||||
{
|
||||
UCHAR *ucMBFrame;
|
||||
USHORT usRegAddress;
|
||||
USHORT usRegCount;
|
||||
UCHAR ucRegByteCount;
|
||||
|
||||
eMBException eStatus = MB_EX_NONE;
|
||||
eMBErrorCode eRegStatus;
|
||||
|
||||
/* If this request is broadcast, the *usLen is not need check. */
|
||||
if( ( *usLen == MB_PDU_SIZE_MIN + MB_PDU_FUNC_WRITE_MUL_SIZE ) || xMBMasterRequestIsBroadcast() )
|
||||
{
|
||||
vMBMasterGetPDUSndBuf(&ucMBFrame);
|
||||
usRegAddress = ( USHORT )( ucMBFrame[MB_PDU_REQ_WRITE_MUL_ADDR_OFF] << 8 );
|
||||
usRegAddress |= ( USHORT )( ucMBFrame[MB_PDU_REQ_WRITE_MUL_ADDR_OFF + 1] );
|
||||
usRegAddress++;
|
||||
|
||||
usRegCount = ( USHORT )( ucMBFrame[MB_PDU_REQ_WRITE_MUL_REGCNT_OFF] << 8 );
|
||||
usRegCount |= ( USHORT )( ucMBFrame[MB_PDU_REQ_WRITE_MUL_REGCNT_OFF + 1] );
|
||||
|
||||
ucRegByteCount = ucMBFrame[MB_PDU_REQ_WRITE_MUL_BYTECNT_OFF];
|
||||
|
||||
if( ucRegByteCount == 2 * usRegCount )
|
||||
{
|
||||
/* Make callback to update the register values. */
|
||||
eRegStatus = eMBMasterRegHoldingCB( &ucMBFrame[MB_PDU_REQ_WRITE_MUL_VALUES_OFF],
|
||||
usRegAddress, usRegCount, MB_REG_WRITE );
|
||||
|
||||
/* If an error occured convert it into a Modbus exception. */
|
||||
if( eRegStatus != MB_ENOERR )
|
||||
{
|
||||
eStatus = prveMBError2Exception( eRegStatus );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Can't be a valid request because the length is incorrect. */
|
||||
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
|
||||
}
|
||||
return eStatus;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if MB_FUNC_READ_HOLDING_ENABLED > 0
|
||||
|
||||
/**
|
||||
* This function will request read holding register.
|
||||
*
|
||||
* @param ucSndAddr salve address
|
||||
* @param usRegAddr register start address
|
||||
* @param usNRegs register total number
|
||||
* @param lTimeOut timeout (-1 will waiting forever)
|
||||
*
|
||||
* @return error code
|
||||
*/
|
||||
eMBMasterReqErrCode
|
||||
eMBMasterReqReadHoldingRegister( UCHAR ucSndAddr, USHORT usRegAddr, USHORT usNRegs, LONG lTimeOut )
|
||||
{
|
||||
UCHAR *ucMBFrame;
|
||||
eMBMasterReqErrCode eErrStatus = MB_MRE_NO_ERR;
|
||||
|
||||
if ( ucSndAddr > MB_MASTER_TOTAL_SLAVE_NUM ) eErrStatus = MB_MRE_ILL_ARG;
|
||||
else if ( xMBMasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY;
|
||||
else
|
||||
{
|
||||
vMBMasterGetPDUSndBuf(&ucMBFrame);
|
||||
vMBMasterSetDestAddress(ucSndAddr);
|
||||
ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_READ_HOLDING_REGISTER;
|
||||
ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF] = usRegAddr >> 8;
|
||||
ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF + 1] = usRegAddr;
|
||||
ucMBFrame[MB_PDU_REQ_READ_REGCNT_OFF] = usNRegs >> 8;
|
||||
ucMBFrame[MB_PDU_REQ_READ_REGCNT_OFF + 1] = usNRegs;
|
||||
vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_READ_SIZE );
|
||||
( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_TRANSMIT | EV_MASTER_TRANS_START );
|
||||
eErrStatus = eMBMasterWaitRequestFinish( );
|
||||
}
|
||||
return eErrStatus;
|
||||
}
|
||||
|
||||
eMBException
|
||||
eMBMasterFuncReadHoldingRegister( UCHAR * pucFrame, USHORT * usLen )
|
||||
{
|
||||
UCHAR *ucMBFrame;
|
||||
USHORT usRegAddress;
|
||||
USHORT usRegCount;
|
||||
|
||||
eMBException eStatus = MB_EX_NONE;
|
||||
eMBErrorCode eRegStatus;
|
||||
|
||||
/* If this request is broadcast, and it's read mode. This request don't need execute. */
|
||||
if ( xMBMasterRequestIsBroadcast() )
|
||||
{
|
||||
eStatus = MB_EX_NONE;
|
||||
}
|
||||
else if( *usLen >= MB_PDU_SIZE_MIN + MB_PDU_FUNC_READ_SIZE_MIN )
|
||||
{
|
||||
vMBMasterGetPDUSndBuf(&ucMBFrame);
|
||||
usRegAddress = ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF] << 8 );
|
||||
usRegAddress |= ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF + 1] );
|
||||
usRegAddress++;
|
||||
|
||||
usRegCount = ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_REGCNT_OFF] << 8 );
|
||||
usRegCount |= ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_REGCNT_OFF + 1] );
|
||||
|
||||
/* Check if the number of registers to read is valid. If not
|
||||
* return Modbus illegal data value exception.
|
||||
*/
|
||||
if( ( usRegCount >= 1 ) && ( 2 * usRegCount == pucFrame[MB_PDU_FUNC_READ_BYTECNT_OFF] ) )
|
||||
{
|
||||
/* Make callback to fill the buffer. */
|
||||
eRegStatus = eMBMasterRegHoldingCB( &pucFrame[MB_PDU_FUNC_READ_VALUES_OFF], usRegAddress, usRegCount, MB_REG_READ );
|
||||
/* If an error occured convert it into a Modbus exception. */
|
||||
if( eRegStatus != MB_ENOERR )
|
||||
{
|
||||
eStatus = prveMBError2Exception( eRegStatus );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Can't be a valid request because the length is incorrect. */
|
||||
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
|
||||
}
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#if MB_FUNC_READWRITE_HOLDING_ENABLED > 0
|
||||
|
||||
/**
|
||||
* This function will request read and write holding register.
|
||||
*
|
||||
* @param ucSndAddr salve address
|
||||
* @param usReadRegAddr read register start address
|
||||
* @param usNReadRegs read register total number
|
||||
* @param pusDataBuffer data to be written
|
||||
* @param usWriteRegAddr write register start address
|
||||
* @param usNWriteRegs write register total number
|
||||
* @param lTimeOut timeout (-1 will waiting forever)
|
||||
*
|
||||
* @return error code
|
||||
*/
|
||||
eMBMasterReqErrCode
|
||||
eMBMasterReqReadWriteMultipleHoldingRegister( UCHAR ucSndAddr,
|
||||
USHORT usReadRegAddr, USHORT usNReadRegs, USHORT * pusDataBuffer,
|
||||
USHORT usWriteRegAddr, USHORT usNWriteRegs, LONG lTimeOut )
|
||||
{
|
||||
UCHAR *ucMBFrame;
|
||||
USHORT usRegIndex = 0;
|
||||
eMBMasterReqErrCode eErrStatus = MB_MRE_NO_ERR;
|
||||
|
||||
if ( ucSndAddr > MB_MASTER_TOTAL_SLAVE_NUM ) eErrStatus = MB_MRE_ILL_ARG;
|
||||
else if ( xMBMasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY;
|
||||
else
|
||||
{
|
||||
vMBMasterGetPDUSndBuf(&ucMBFrame);
|
||||
vMBMasterSetDestAddress(ucSndAddr);
|
||||
ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_READWRITE_MULTIPLE_REGISTERS;
|
||||
ucMBFrame[MB_PDU_REQ_READWRITE_READ_ADDR_OFF] = usReadRegAddr >> 8;
|
||||
ucMBFrame[MB_PDU_REQ_READWRITE_READ_ADDR_OFF + 1] = usReadRegAddr;
|
||||
ucMBFrame[MB_PDU_REQ_READWRITE_READ_REGCNT_OFF] = usNReadRegs >> 8;
|
||||
ucMBFrame[MB_PDU_REQ_READWRITE_READ_REGCNT_OFF + 1] = usNReadRegs ;
|
||||
ucMBFrame[MB_PDU_REQ_READWRITE_WRITE_ADDR_OFF] = usWriteRegAddr >> 8;
|
||||
ucMBFrame[MB_PDU_REQ_READWRITE_WRITE_ADDR_OFF + 1] = usWriteRegAddr;
|
||||
ucMBFrame[MB_PDU_REQ_READWRITE_WRITE_REGCNT_OFF] = usNWriteRegs >> 8;
|
||||
ucMBFrame[MB_PDU_REQ_READWRITE_WRITE_REGCNT_OFF + 1] = usNWriteRegs ;
|
||||
ucMBFrame[MB_PDU_REQ_READWRITE_WRITE_BYTECNT_OFF] = usNWriteRegs * 2;
|
||||
ucMBFrame += MB_PDU_REQ_READWRITE_WRITE_VALUES_OFF;
|
||||
while( usNWriteRegs > usRegIndex)
|
||||
{
|
||||
*ucMBFrame++ = pusDataBuffer[usRegIndex] >> 8;
|
||||
*ucMBFrame++ = pusDataBuffer[usRegIndex++] ;
|
||||
}
|
||||
vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_READWRITE_SIZE_MIN + 2*usNWriteRegs );
|
||||
( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_TRANSMIT | EV_MASTER_TRANS_START );
|
||||
eErrStatus = eMBMasterWaitRequestFinish( );
|
||||
}
|
||||
return eErrStatus;
|
||||
}
|
||||
|
||||
eMBException
|
||||
eMBMasterFuncReadWriteMultipleHoldingRegister( UCHAR * pucFrame, USHORT * usLen )
|
||||
{
|
||||
USHORT usRegReadAddress;
|
||||
USHORT usRegReadCount;
|
||||
USHORT usRegWriteAddress;
|
||||
USHORT usRegWriteCount;
|
||||
UCHAR *ucMBFrame;
|
||||
|
||||
eMBException eStatus = MB_EX_NONE;
|
||||
eMBErrorCode eRegStatus;
|
||||
|
||||
/* If this request is broadcast, and it's read mode. This request don't need execute. */
|
||||
if ( xMBMasterRequestIsBroadcast() )
|
||||
{
|
||||
eStatus = MB_EX_NONE;
|
||||
}
|
||||
else if( *usLen >= MB_PDU_SIZE_MIN + MB_PDU_FUNC_READWRITE_SIZE_MIN )
|
||||
{
|
||||
vMBMasterGetPDUSndBuf(&ucMBFrame);
|
||||
usRegReadAddress = ( USHORT )( ucMBFrame[MB_PDU_REQ_READWRITE_READ_ADDR_OFF] << 8U );
|
||||
usRegReadAddress |= ( USHORT )( ucMBFrame[MB_PDU_REQ_READWRITE_READ_ADDR_OFF + 1] );
|
||||
usRegReadAddress++;
|
||||
|
||||
usRegReadCount = ( USHORT )( ucMBFrame[MB_PDU_REQ_READWRITE_READ_REGCNT_OFF] << 8U );
|
||||
usRegReadCount |= ( USHORT )( ucMBFrame[MB_PDU_REQ_READWRITE_READ_REGCNT_OFF + 1] );
|
||||
|
||||
usRegWriteAddress = ( USHORT )( ucMBFrame[MB_PDU_REQ_READWRITE_WRITE_ADDR_OFF] << 8U );
|
||||
usRegWriteAddress |= ( USHORT )( ucMBFrame[MB_PDU_REQ_READWRITE_WRITE_ADDR_OFF + 1] );
|
||||
usRegWriteAddress++;
|
||||
|
||||
usRegWriteCount = ( USHORT )( ucMBFrame[MB_PDU_REQ_READWRITE_WRITE_REGCNT_OFF] << 8U );
|
||||
usRegWriteCount |= ( USHORT )( ucMBFrame[MB_PDU_REQ_READWRITE_WRITE_REGCNT_OFF + 1] );
|
||||
|
||||
if( ( 2 * usRegReadCount ) == pucFrame[MB_PDU_FUNC_READWRITE_READ_BYTECNT_OFF] )
|
||||
{
|
||||
/* Make callback to update the register values. */
|
||||
eRegStatus = eMBMasterRegHoldingCB( &ucMBFrame[MB_PDU_REQ_READWRITE_WRITE_VALUES_OFF],
|
||||
usRegWriteAddress, usRegWriteCount, MB_REG_WRITE );
|
||||
|
||||
if( eRegStatus == MB_ENOERR )
|
||||
{
|
||||
/* Make the read callback. */
|
||||
eRegStatus = eMBMasterRegHoldingCB(&pucFrame[MB_PDU_FUNC_READWRITE_READ_VALUES_OFF],
|
||||
usRegReadAddress, usRegReadCount, MB_REG_READ);
|
||||
}
|
||||
if( eRegStatus != MB_ENOERR )
|
||||
{
|
||||
eStatus = prveMBError2Exception( eRegStatus );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
|
||||
}
|
||||
}
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
#endif
|
||||
#endif // #if MB_MASTER_RTU_ENABLED || MB_MASTER_ASCII_ENABLED || MB_MASTER_TCP_ENABLED
|
||||
@@ -0,0 +1,133 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2006 Christian Walter
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*
|
||||
* SPDX-FileContributor: 2016-2021 Espressif Systems (Shanghai) CO LTD
|
||||
*/
|
||||
/*
|
||||
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
|
||||
* Copyright (c) 2006 Christian Walter <wolti@sil.at>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* File: $Id: mbfuncinput.c,v 1.10 2007/09/12 10:15:56 wolti Exp $
|
||||
*/
|
||||
|
||||
/* ----------------------- System includes ----------------------------------*/
|
||||
#include "stdlib.h"
|
||||
#include "string.h"
|
||||
|
||||
/* ----------------------- Platform includes --------------------------------*/
|
||||
#include "port.h"
|
||||
|
||||
/* ----------------------- Modbus includes ----------------------------------*/
|
||||
#include "mb.h"
|
||||
#include "mbframe.h"
|
||||
#include "mbproto.h"
|
||||
#include "mbconfig.h"
|
||||
|
||||
/* ----------------------- Defines ------------------------------------------*/
|
||||
#define MB_PDU_FUNC_READ_ADDR_OFF ( MB_PDU_DATA_OFF )
|
||||
#define MB_PDU_FUNC_READ_REGCNT_OFF ( MB_PDU_DATA_OFF + 2 )
|
||||
#define MB_PDU_FUNC_READ_SIZE ( 4 )
|
||||
#define MB_PDU_FUNC_READ_REGCNT_MAX ( 0x007D )
|
||||
|
||||
#define MB_PDU_FUNC_READ_RSP_BYTECNT_OFF ( MB_PDU_DATA_OFF )
|
||||
|
||||
/* ----------------------- Static functions ---------------------------------*/
|
||||
eMBException prveMBError2Exception( eMBErrorCode eErrorCode );
|
||||
|
||||
/* ----------------------- Start implementation -----------------------------*/
|
||||
#if MB_SLAVE_RTU_ENABLED || MB_SLAVE_ASCII_ENABLED || MB_TCP_ENABLED
|
||||
|
||||
#if MB_FUNC_READ_INPUT_ENABLED
|
||||
|
||||
eMBException
|
||||
eMBFuncReadInputRegister( UCHAR * pucFrame, USHORT * usLen )
|
||||
{
|
||||
USHORT usRegAddress;
|
||||
USHORT usRegCount;
|
||||
UCHAR *pucFrameCur;
|
||||
|
||||
eMBException eStatus = MB_EX_NONE;
|
||||
eMBErrorCode eRegStatus;
|
||||
|
||||
if( *usLen == ( MB_PDU_FUNC_READ_SIZE + MB_PDU_SIZE_MIN ) )
|
||||
{
|
||||
usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF] << 8 );
|
||||
usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF + 1] );
|
||||
usRegAddress++;
|
||||
|
||||
usRegCount = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_REGCNT_OFF] << 8 );
|
||||
usRegCount |= ( USHORT )( pucFrame[MB_PDU_FUNC_READ_REGCNT_OFF + 1] );
|
||||
|
||||
/* Check if the number of registers to read is valid. If not
|
||||
* return Modbus illegal data value exception.
|
||||
*/
|
||||
if( ( usRegCount >= 1 )
|
||||
&& ( usRegCount <= MB_PDU_FUNC_READ_REGCNT_MAX ) )
|
||||
{
|
||||
/* Set the current PDU data pointer to the beginning. */
|
||||
pucFrameCur = &pucFrame[MB_PDU_FUNC_OFF];
|
||||
*usLen = MB_PDU_FUNC_OFF;
|
||||
|
||||
/* First byte contains the function code. */
|
||||
*pucFrameCur++ = MB_FUNC_READ_INPUT_REGISTER;
|
||||
*usLen += 1;
|
||||
|
||||
/* Second byte in the response contain the number of bytes. */
|
||||
*pucFrameCur++ = ( UCHAR )( usRegCount * 2 );
|
||||
*usLen += 1;
|
||||
|
||||
eRegStatus =
|
||||
eMBRegInputCB( pucFrameCur, usRegAddress, usRegCount );
|
||||
|
||||
/* If an error occured convert it into a Modbus exception. */
|
||||
if( eRegStatus != MB_ENOERR )
|
||||
{
|
||||
eStatus = prveMBError2Exception( eRegStatus );
|
||||
}
|
||||
else
|
||||
{
|
||||
*usLen += usRegCount * 2;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Can't be a valid read input register request because the length
|
||||
* is incorrect. */
|
||||
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
|
||||
}
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,154 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2013 Armink
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*
|
||||
* SPDX-FileContributor: 2016-2021 Espressif Systems (Shanghai) CO LTD
|
||||
*/
|
||||
/*
|
||||
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
|
||||
* Copyright (C) 2013 Armink <armink.ztl@gmail.com>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* File: $Id: mbfuncinput_m.c,v 1.60 2013/10/12 14:23:40 Armink Add Master Functions Exp $
|
||||
*/
|
||||
|
||||
/* ----------------------- System includes ----------------------------------*/
|
||||
#include "stdlib.h"
|
||||
#include "string.h"
|
||||
|
||||
/* ----------------------- Platform includes --------------------------------*/
|
||||
#include "port.h"
|
||||
|
||||
/* ----------------------- Modbus includes ----------------------------------*/
|
||||
#include "mb_m.h"
|
||||
#include "mbframe.h"
|
||||
#include "mbproto.h"
|
||||
#include "mbconfig.h"
|
||||
|
||||
/* ----------------------- Defines ------------------------------------------*/
|
||||
#define MB_PDU_REQ_READ_ADDR_OFF ( MB_PDU_DATA_OFF + 0 )
|
||||
#define MB_PDU_REQ_READ_REGCNT_OFF ( MB_PDU_DATA_OFF + 2 )
|
||||
#define MB_PDU_REQ_READ_SIZE ( 4 )
|
||||
#define MB_PDU_FUNC_READ_BYTECNT_OFF ( MB_PDU_DATA_OFF + 0 )
|
||||
#define MB_PDU_FUNC_READ_VALUES_OFF ( MB_PDU_DATA_OFF + 1 )
|
||||
#define MB_PDU_FUNC_READ_SIZE_MIN ( 1 )
|
||||
|
||||
#define MB_PDU_FUNC_READ_RSP_BYTECNT_OFF ( MB_PDU_DATA_OFF )
|
||||
|
||||
/* ----------------------- Static functions ---------------------------------*/
|
||||
eMBException prveMBError2Exception( eMBErrorCode eErrorCode );
|
||||
|
||||
/* ----------------------- Start implementation -----------------------------*/
|
||||
#if MB_MASTER_RTU_ENABLED || MB_MASTER_ASCII_ENABLED || MB_MASTER_TCP_ENABLED
|
||||
#if MB_FUNC_READ_INPUT_ENABLED
|
||||
|
||||
/**
|
||||
* This function will request read input register.
|
||||
*
|
||||
* @param ucSndAddr salve address
|
||||
* @param usRegAddr register start address
|
||||
* @param usNRegs register total number
|
||||
* @param lTimeOut timeout (-1 will waiting forever)
|
||||
*
|
||||
* @return error code
|
||||
*/
|
||||
eMBMasterReqErrCode
|
||||
eMBMasterReqReadInputRegister( UCHAR ucSndAddr, USHORT usRegAddr, USHORT usNRegs, LONG lTimeOut )
|
||||
{
|
||||
UCHAR *ucMBFrame;
|
||||
eMBMasterReqErrCode eErrStatus = MB_MRE_NO_ERR;
|
||||
|
||||
if ( ucSndAddr > MB_MASTER_TOTAL_SLAVE_NUM ) eErrStatus = MB_MRE_ILL_ARG;
|
||||
else if ( xMBMasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY;
|
||||
else
|
||||
{
|
||||
vMBMasterGetPDUSndBuf(&ucMBFrame);
|
||||
vMBMasterSetDestAddress(ucSndAddr);
|
||||
ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_READ_INPUT_REGISTER;
|
||||
ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF] = usRegAddr >> 8;
|
||||
ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF + 1] = usRegAddr;
|
||||
ucMBFrame[MB_PDU_REQ_READ_REGCNT_OFF] = usNRegs >> 8;
|
||||
ucMBFrame[MB_PDU_REQ_READ_REGCNT_OFF + 1] = usNRegs;
|
||||
vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_READ_SIZE );
|
||||
( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_TRANSMIT | EV_MASTER_TRANS_START );
|
||||
eErrStatus = eMBMasterWaitRequestFinish( );
|
||||
}
|
||||
return eErrStatus;
|
||||
}
|
||||
|
||||
eMBException
|
||||
eMBMasterFuncReadInputRegister( UCHAR * pucFrame, USHORT * usLen )
|
||||
{
|
||||
UCHAR *ucMBFrame;
|
||||
USHORT usRegAddress;
|
||||
USHORT usRegCount;
|
||||
|
||||
eMBException eStatus = MB_EX_NONE;
|
||||
eMBErrorCode eRegStatus;
|
||||
|
||||
/* If this request is broadcast, and it's read mode. This request don't need execute. */
|
||||
if ( xMBMasterRequestIsBroadcast() )
|
||||
{
|
||||
eStatus = MB_EX_NONE;
|
||||
}
|
||||
else if( *usLen >= MB_PDU_SIZE_MIN + MB_PDU_FUNC_READ_SIZE_MIN )
|
||||
{
|
||||
vMBMasterGetPDUSndBuf(&ucMBFrame);
|
||||
usRegAddress = ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF] << 8 );
|
||||
usRegAddress |= ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF + 1] );
|
||||
usRegAddress++;
|
||||
|
||||
usRegCount = ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_REGCNT_OFF] << 8 );
|
||||
usRegCount |= ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_REGCNT_OFF + 1] );
|
||||
|
||||
/* Check if the number of registers to read is valid. If not
|
||||
* return Modbus illegal data value exception.
|
||||
*/
|
||||
if( ( usRegCount >= 1 ) && ( 2 * usRegCount == pucFrame[MB_PDU_FUNC_READ_BYTECNT_OFF] ) )
|
||||
{
|
||||
/* Make callback to fill the buffer. */
|
||||
eRegStatus = eMBMasterRegInputCB( &pucFrame[MB_PDU_FUNC_READ_VALUES_OFF], usRegAddress, usRegCount );
|
||||
/* If an error occured convert it into a Modbus exception. */
|
||||
if( eRegStatus != MB_ENOERR )
|
||||
{
|
||||
eStatus = prveMBError2Exception( eRegStatus );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Can't be a valid request because the length is incorrect. */
|
||||
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
|
||||
}
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
#endif
|
||||
#endif // #if MB_MASTER_RTU_ENABLED || MB_MASTER_ASCII_ENABLED || MB_MASTER_TCP_ENABLED
|
||||
@@ -0,0 +1,99 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2006 Christian Walter
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*
|
||||
* SPDX-FileContributor: 2016-2021 Espressif Systems (Shanghai) CO LTD
|
||||
*/
|
||||
/*
|
||||
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
|
||||
* Copyright (c) 2006 Christian Walter <wolti@sil.at>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* File: $Id: mbfuncother.c,v 1.8 2006/12/07 22:10:34 wolti Exp $
|
||||
*/
|
||||
|
||||
/* ----------------------- System includes ----------------------------------*/
|
||||
#include "stdlib.h"
|
||||
#include "string.h"
|
||||
|
||||
/* ----------------------- Platform includes --------------------------------*/
|
||||
#include "port.h"
|
||||
|
||||
/* ----------------------- Modbus includes ----------------------------------*/
|
||||
#include "mb.h"
|
||||
#include "mbframe.h"
|
||||
#include "mbproto.h"
|
||||
#include "mbconfig.h"
|
||||
|
||||
#if MB_SLAVE_RTU_ENABLED || MB_SLAVE_ASCII_ENABLED || MB_TCP_ENABLED
|
||||
|
||||
#if MB_FUNC_OTHER_REP_SLAVEID_ENABLED
|
||||
|
||||
/* ----------------------- Static variables ---------------------------------*/
|
||||
static UCHAR ucMBSlaveID[MB_FUNC_OTHER_REP_SLAVEID_BUF];
|
||||
static USHORT usMBSlaveIDLen;
|
||||
|
||||
/* ----------------------- Start implementation -----------------------------*/
|
||||
|
||||
eMBErrorCode
|
||||
eMBSetSlaveID( UCHAR ucSlaveID, BOOL xIsRunning,
|
||||
UCHAR const *pucAdditional, USHORT usAdditionalLen )
|
||||
{
|
||||
eMBErrorCode eStatus = MB_ENOERR;
|
||||
|
||||
/* the first byte and second byte in the buffer is reserved for
|
||||
* the parameter ucSlaveID and the running flag. The rest of
|
||||
* the buffer is available for additional data. */
|
||||
if( usAdditionalLen + 2 < MB_FUNC_OTHER_REP_SLAVEID_BUF )
|
||||
{
|
||||
usMBSlaveIDLen = 0;
|
||||
ucMBSlaveID[usMBSlaveIDLen++] = ucSlaveID;
|
||||
ucMBSlaveID[usMBSlaveIDLen++] = ( UCHAR )( xIsRunning ? 0xFF : 0x00 );
|
||||
if( usAdditionalLen > 0 )
|
||||
{
|
||||
memcpy( &ucMBSlaveID[usMBSlaveIDLen], pucAdditional,
|
||||
( size_t )usAdditionalLen );
|
||||
usMBSlaveIDLen += usAdditionalLen;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
eStatus = MB_ENORES;
|
||||
}
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
eMBException
|
||||
eMBFuncReportSlaveID( UCHAR * pucFrame, USHORT * usLen )
|
||||
{
|
||||
memcpy( &pucFrame[MB_PDU_DATA_OFF], &ucMBSlaveID[0], ( size_t )usMBSlaveIDLen );
|
||||
*usLen = ( USHORT )( MB_PDU_DATA_OFF + usMBSlaveIDLen );
|
||||
return MB_EX_NONE;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,148 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2006 Christian Walter
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*
|
||||
* SPDX-FileContributor: 2016-2021 Espressif Systems (Shanghai) CO LTD
|
||||
*/
|
||||
/*
|
||||
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
|
||||
* Copyright (c) 2006 Christian Walter <wolti@sil.at>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* File: $Id: mbutils.c,v 1.6 2007/02/18 23:49:07 wolti Exp $
|
||||
*/
|
||||
|
||||
/* ----------------------- System includes ----------------------------------*/
|
||||
#include "stdlib.h"
|
||||
#include "string.h"
|
||||
|
||||
/* ----------------------- Platform includes --------------------------------*/
|
||||
#include "port.h"
|
||||
|
||||
/* ----------------------- Modbus includes ----------------------------------*/
|
||||
#include "mb.h"
|
||||
#include "mbproto.h"
|
||||
|
||||
/* ----------------------- Defines ------------------------------------------*/
|
||||
#define BITS_UCHAR 8U
|
||||
|
||||
/* ----------------------- Start implementation -----------------------------*/
|
||||
void
|
||||
xMBUtilSetBits( UCHAR * ucByteBuf, USHORT usBitOffset, UCHAR ucNBits,
|
||||
UCHAR ucValue )
|
||||
{
|
||||
USHORT usWordBuf;
|
||||
USHORT usMask;
|
||||
USHORT usByteOffset;
|
||||
USHORT usNPreBits;
|
||||
USHORT usValue = ucValue;
|
||||
|
||||
assert( ucNBits <= 8 );
|
||||
assert( ( size_t )BITS_UCHAR == sizeof( UCHAR ) * 8 );
|
||||
|
||||
/* Calculate byte offset for first byte containing the bit values starting
|
||||
* at usBitOffset. */
|
||||
usByteOffset = ( USHORT )( ( usBitOffset ) / BITS_UCHAR );
|
||||
|
||||
/* How many bits precede our bits to set. */
|
||||
usNPreBits = ( USHORT )( usBitOffset - usByteOffset * BITS_UCHAR );
|
||||
|
||||
/* Move bit field into position over bits to set */
|
||||
usValue <<= usNPreBits;
|
||||
|
||||
/* Prepare a mask for setting the new bits. */
|
||||
usMask = ( USHORT )( ( 1 << ( USHORT ) ucNBits ) - 1 );
|
||||
usMask <<= usBitOffset - usByteOffset * BITS_UCHAR;
|
||||
|
||||
/* copy bits into temporary storage. */
|
||||
usWordBuf = ucByteBuf[usByteOffset];
|
||||
usWordBuf |= ucByteBuf[usByteOffset + 1] << BITS_UCHAR;
|
||||
|
||||
/* Zero out bit field bits and then or value bits into them. */
|
||||
usWordBuf = ( USHORT )( ( usWordBuf & ( ~usMask ) ) | usValue );
|
||||
|
||||
/* move bits back into storage */
|
||||
ucByteBuf[usByteOffset] = ( UCHAR )( usWordBuf & 0xFF );
|
||||
ucByteBuf[usByteOffset + 1] = ( UCHAR )( usWordBuf >> BITS_UCHAR );
|
||||
}
|
||||
|
||||
UCHAR
|
||||
xMBUtilGetBits( UCHAR * ucByteBuf, USHORT usBitOffset, UCHAR ucNBits )
|
||||
{
|
||||
USHORT usWordBuf;
|
||||
USHORT usMask;
|
||||
USHORT usByteOffset;
|
||||
USHORT usNPreBits;
|
||||
|
||||
/* Calculate byte offset for first byte containing the bit values starting
|
||||
* at usBitOffset. */
|
||||
usByteOffset = ( USHORT )( ( usBitOffset ) / BITS_UCHAR );
|
||||
|
||||
/* How many bits precede our bits to set. */
|
||||
usNPreBits = ( USHORT )( usBitOffset - usByteOffset * BITS_UCHAR );
|
||||
|
||||
/* Prepare a mask for setting the new bits. */
|
||||
usMask = ( USHORT )( ( 1 << ( USHORT ) ucNBits ) - 1 );
|
||||
|
||||
/* copy bits into temporary storage. */
|
||||
usWordBuf = ucByteBuf[usByteOffset];
|
||||
usWordBuf |= ucByteBuf[usByteOffset + 1] << BITS_UCHAR;
|
||||
|
||||
/* throw away unneeded bits. */
|
||||
usWordBuf >>= usNPreBits;
|
||||
|
||||
/* mask away bits above the requested bitfield. */
|
||||
usWordBuf &= usMask;
|
||||
|
||||
return ( UCHAR ) usWordBuf;
|
||||
}
|
||||
|
||||
eMBException
|
||||
prveMBError2Exception( eMBErrorCode eErrorCode )
|
||||
{
|
||||
eMBException eStatus;
|
||||
|
||||
switch ( eErrorCode )
|
||||
{
|
||||
case MB_ENOERR:
|
||||
eStatus = MB_EX_NONE;
|
||||
break;
|
||||
|
||||
case MB_ENOREG:
|
||||
eStatus = MB_EX_ILLEGAL_DATA_ADDRESS;
|
||||
break;
|
||||
|
||||
case MB_ETIMEDOUT:
|
||||
eStatus = MB_EX_SLAVE_BUSY;
|
||||
break;
|
||||
|
||||
default:
|
||||
eStatus = MB_EX_SLAVE_DEVICE_FAILURE;
|
||||
break;
|
||||
}
|
||||
|
||||
return eStatus;
|
||||
}
|
||||
@@ -0,0 +1,425 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2006 Christian Walter
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*
|
||||
* SPDX-FileContributor: 2016-2021 Espressif Systems (Shanghai) CO LTD
|
||||
*/
|
||||
/*
|
||||
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
|
||||
* Copyright (c) 2006 Christian Walter <wolti@sil.at>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* File: $Id: mb.h,v 1.17 2006/12/07 22:10:34 wolti Exp $
|
||||
*/
|
||||
|
||||
#ifndef _MB_H
|
||||
#define _MB_H
|
||||
|
||||
#include "port.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
PR_BEGIN_EXTERN_C
|
||||
#endif
|
||||
|
||||
#include "mbport.h"
|
||||
#include "mbproto.h"
|
||||
|
||||
/*! \defgroup modbus Modbus
|
||||
* \code #include "mb.h" \endcode
|
||||
*
|
||||
* This module defines the interface for the application. It contains
|
||||
* the basic functions and types required to use the Modbus protocol stack.
|
||||
* A typical application will want to call eMBInit() first. If the device
|
||||
* is ready to answer network requests it must then call eMBEnable() to activate
|
||||
* the protocol stack. In the main loop the function eMBPoll() must be called
|
||||
* periodically. The time interval between pooling depends on the configured
|
||||
* Modbus timeout. If an RTOS is available a separate task should be created
|
||||
* and the task should always call the function eMBPoll().
|
||||
*
|
||||
* \code
|
||||
* // Initialize protocol stack in RTU mode for a slave with address 10 = 0x0A
|
||||
* eMBInit( MB_RTU, 0x0A, 38400, MB_PAR_EVEN );
|
||||
* // Enable the Modbus Protocol Stack.
|
||||
* eMBEnable( );
|
||||
* for( ;; )
|
||||
* {
|
||||
* // Call the main polling loop of the Modbus protocol stack.
|
||||
* eMBPoll( );
|
||||
* ...
|
||||
* }
|
||||
* \endcode
|
||||
*/
|
||||
|
||||
/* ----------------------- Defines ------------------------------------------*/
|
||||
|
||||
/*! \ingroup modbus
|
||||
* \brief Use the default Modbus TCP port (502)
|
||||
*/
|
||||
#define MB_TCP_PORT_USE_DEFAULT 0
|
||||
|
||||
#define MB_FUNC_CODE_MAX 127
|
||||
|
||||
/* ----------------------- Type definitions ---------------------------------*/
|
||||
/*! \ingroup modbus
|
||||
* \brief Modbus serial transmission modes (RTU/ASCII).
|
||||
*
|
||||
* Modbus serial supports two transmission modes. Either ASCII or RTU. RTU
|
||||
* is faster but has more hardware requirements and requires a network with
|
||||
* a low jitter. ASCII is slower and more reliable on slower links (E.g. modems)
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
MB_RTU, /*!< RTU transmission mode. */
|
||||
MB_ASCII, /*!< ASCII transmission mode. */
|
||||
MB_TCP /*!< TCP mode. */
|
||||
} eMBMode;
|
||||
|
||||
/*! \ingroup modbus
|
||||
* \brief If register should be written or read.
|
||||
*
|
||||
* This value is passed to the callback functions which support either
|
||||
* reading or writing register values. Writing means that the application
|
||||
* registers should be updated and reading means that the modbus protocol
|
||||
* stack needs to know the current register values.
|
||||
*
|
||||
* \see eMBRegHoldingCB( ), eMBRegCoilsCB( ), eMBRegDiscreteCB( ) and
|
||||
* eMBRegInputCB( ).
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
MB_REG_READ, /*!< Read register values and pass to protocol stack. */
|
||||
MB_REG_WRITE /*!< Update register values. */
|
||||
} eMBRegisterMode;
|
||||
|
||||
/*! \ingroup modbus
|
||||
* \brief Errorcodes used by all function in the protocol stack.
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
MB_ENOERR, /*!< no error. */
|
||||
MB_ENOREG, /*!< illegal register address. */
|
||||
MB_EINVAL, /*!< illegal argument. */
|
||||
MB_EPORTERR, /*!< porting layer error. */
|
||||
MB_ENORES, /*!< insufficient resources. */
|
||||
MB_EIO, /*!< I/O error. */
|
||||
MB_EILLSTATE, /*!< protocol stack in illegal state. */
|
||||
MB_ETIMEDOUT /*!< timeout error occurred. */
|
||||
} eMBErrorCode;
|
||||
|
||||
/* ----------------------- Function prototypes ------------------------------*/
|
||||
/*! \ingroup modbus
|
||||
* \brief Initialize the Modbus protocol stack.
|
||||
*
|
||||
* This functions initializes the ASCII or RTU module and calls the
|
||||
* init functions of the porting layer to prepare the hardware. Please
|
||||
* note that the receiver is still disabled and no Modbus frames are
|
||||
* processed until eMBEnable( ) has been called.
|
||||
*
|
||||
* \param eMode If ASCII or RTU mode should be used.
|
||||
* \param ucSlaveAddress The slave address. Only frames sent to this
|
||||
* address or to the broadcast address are processed.
|
||||
* \param ucPort The port to use. E.g. 1 for COM1 on windows. This value
|
||||
* is platform dependent and some ports simply choose to ignore it.
|
||||
* \param ulBaudRate The baudrate. E.g. 19200. Supported baudrates depend
|
||||
* on the porting layer.
|
||||
* \param eParity Parity used for serial transmission.
|
||||
*
|
||||
* \return If no error occurs the function returns eMBErrorCode::MB_ENOERR.
|
||||
* The protocol is then in the disabled state and ready for activation
|
||||
* by calling eMBEnable( ). Otherwise one of the following error codes
|
||||
* is returned:
|
||||
* - eMBErrorCode::MB_EINVAL If the slave address was not valid. Valid
|
||||
* slave addresses are in the range 1 - 247.
|
||||
* - eMBErrorCode::MB_EPORTERR IF the porting layer returned an error.
|
||||
*/
|
||||
eMBErrorCode eMBInit( eMBMode eMode, UCHAR ucSlaveAddress,
|
||||
UCHAR ucPort, ULONG ulBaudRate, eMBParity eParity );
|
||||
|
||||
/*! \ingroup modbus
|
||||
* \brief Initialize the Modbus protocol stack for Modbus TCP.
|
||||
*
|
||||
* This function initializes the Modbus TCP Module. Please note that
|
||||
* frame processing is still disabled until eMBEnable( ) is called.
|
||||
*
|
||||
* \param usTCPPort The TCP port to listen on.
|
||||
* \param ucSlaveUid The UID field for slave to listen on.
|
||||
* \return If the protocol stack has been initialized correctly the function
|
||||
* returns eMBErrorCode::MB_ENOERR. Otherwise one of the following error
|
||||
* codes is returned:
|
||||
* - eMBErrorCode::MB_EINVAL If the slave address was not valid. Valid
|
||||
* slave addresses are in the range 1 - 247.
|
||||
* - eMBErrorCode::MB_EPORTERR IF the porting layer returned an error.
|
||||
*/
|
||||
eMBErrorCode eMBTCPInit( UCHAR ucSlaveUid, USHORT usTCPPort );
|
||||
|
||||
/*! \ingroup modbus
|
||||
* \brief Release resources used by the protocol stack.
|
||||
*
|
||||
* This function disables the Modbus protocol stack and release all
|
||||
* hardware resources. It must only be called when the protocol stack
|
||||
* is disabled.
|
||||
*
|
||||
* \note Note all ports implement this function. A port which wants to
|
||||
* get an callback must define the macro MB_PORT_HAS_CLOSE to 1.
|
||||
*
|
||||
* \return If the resources where released it return eMBErrorCode::MB_ENOERR.
|
||||
* If the protocol stack is not in the disabled state it returns
|
||||
* eMBErrorCode::MB_EILLSTATE.
|
||||
*/
|
||||
eMBErrorCode eMBClose( void );
|
||||
|
||||
/*! \ingroup modbus
|
||||
* \brief Enable the Modbus protocol stack.
|
||||
*
|
||||
* This function enables processing of Modbus frames. Enabling the protocol
|
||||
* stack is only possible if it is in the disabled state.
|
||||
*
|
||||
* \return If the protocol stack is now in the state enabled it returns
|
||||
* eMBErrorCode::MB_ENOERR. If it was not in the disabled state it
|
||||
* return eMBErrorCode::MB_EILLSTATE.
|
||||
*/
|
||||
eMBErrorCode eMBEnable( void );
|
||||
|
||||
/*! \ingroup modbus
|
||||
* \brief Disable the Modbus protocol stack.
|
||||
*
|
||||
* This function disables processing of Modbus frames.
|
||||
*
|
||||
* \return If the protocol stack has been disabled it returns
|
||||
* eMBErrorCode::MB_ENOERR. If it was not in the enabled state it returns
|
||||
* eMBErrorCode::MB_EILLSTATE.
|
||||
*/
|
||||
eMBErrorCode eMBDisable( void );
|
||||
|
||||
/*! \ingroup modbus
|
||||
* \brief The main pooling loop of the Modbus protocol stack.
|
||||
*
|
||||
* This function must be called periodically. The timer interval required
|
||||
* is given by the application dependent Modbus slave timeout. Internally the
|
||||
* function calls xMBPortEventGet() and waits for an event from the receiver or
|
||||
* transmitter state machines.
|
||||
*
|
||||
* \return If the protocol stack is not in the enabled state the function
|
||||
* returns eMBErrorCode::MB_EILLSTATE. Otherwise it returns
|
||||
* eMBErrorCode::MB_ENOERR.
|
||||
*/
|
||||
eMBErrorCode eMBPoll( void );
|
||||
|
||||
/*! \ingroup modbus
|
||||
* \brief Configure the slave id of the device.
|
||||
*
|
||||
* This function should be called when the Modbus function <em>Report Slave ID</em>
|
||||
* is enabled ( By defining MB_FUNC_OTHER_REP_SLAVEID_ENABLED in mbconfig.h ).
|
||||
*
|
||||
* \param ucSlaveID Values is returned in the <em>Slave ID</em> byte of the
|
||||
* <em>Report Slave ID</em> response.
|
||||
* \param xIsRunning If TRUE the <em>Run Indicator Status</em> byte is set to 0xFF.
|
||||
* otherwise the <em>Run Indicator Status</em> is 0x00.
|
||||
* \param pucAdditional Values which should be returned in the <em>Additional</em>
|
||||
* bytes of the <em> Report Slave ID</em> response.
|
||||
* \param usAdditionalLen Length of the buffer <code>pucAdditonal</code>.
|
||||
*
|
||||
* \return If the static buffer defined by MB_FUNC_OTHER_REP_SLAVEID_BUF in
|
||||
* mbconfig.h is to small it returns eMBErrorCode::MB_ENORES. Otherwise
|
||||
* it returns eMBErrorCode::MB_ENOERR.
|
||||
*/
|
||||
eMBErrorCode eMBSetSlaveID( UCHAR ucSlaveID, BOOL xIsRunning,
|
||||
UCHAR const *pucAdditional,
|
||||
USHORT usAdditionalLen );
|
||||
|
||||
/*! \ingroup modbus
|
||||
* \brief Registers a callback handler for a given function code.
|
||||
*
|
||||
* This function registers a new callback handler for a given function code.
|
||||
* The callback handler supplied is responsible for interpreting the Modbus PDU and
|
||||
* the creation of an appropriate response. In case of an error it should return
|
||||
* one of the possible Modbus exceptions which results in a Modbus exception frame
|
||||
* sent by the protocol stack.
|
||||
*
|
||||
* \param ucFunctionCode The Modbus function code for which this handler should
|
||||
* be registers. Valid function codes are in the range 1 to 127.
|
||||
* \param pxHandler The function handler which should be called in case
|
||||
* such a frame is received. If \c NULL a previously registered function handler
|
||||
* for this function code is removed.
|
||||
*
|
||||
* \return eMBErrorCode::MB_ENOERR if the handler has been installed. If no
|
||||
* more resources are available it returns eMBErrorCode::MB_ENORES. In this
|
||||
* case the values in mbconfig.h should be adjusted. If the argument was not
|
||||
* valid it returns eMBErrorCode::MB_EINVAL.
|
||||
*/
|
||||
eMBErrorCode eMBRegisterCB( UCHAR ucFunctionCode,
|
||||
pxMBFunctionHandler pxHandler );
|
||||
|
||||
/* ----------------------- Callback -----------------------------------------*/
|
||||
|
||||
/*! \defgroup modbus_registers Modbus Registers
|
||||
* \code #include "mb.h" \endcode
|
||||
* The protocol stack does not internally allocate any memory for the
|
||||
* registers. This makes the protocol stack very small and also usable on
|
||||
* low end targets. In addition the values don't have to be in the memory
|
||||
* and could for example be stored in a flash.<br>
|
||||
* Whenever the protocol stack requires a value it calls one of the callback
|
||||
* function with the register address and the number of registers to read
|
||||
* as an argument. The application should then read the actual register values
|
||||
* (for example the ADC voltage) and should store the result in the supplied
|
||||
* buffer.<br>
|
||||
* If the protocol stack wants to update a register value because a write
|
||||
* register function was received a buffer with the new register values is
|
||||
* passed to the callback function. The function should then use these values
|
||||
* to update the application register values.
|
||||
*/
|
||||
|
||||
/*! \ingroup modbus_registers
|
||||
* \brief Callback function used if the value of a <em>Input Register</em>
|
||||
* is required by the protocol stack. The starting register address is given
|
||||
* by \c usAddress and the last register is given by <tt>usAddress +
|
||||
* usNRegs - 1</tt>.
|
||||
*
|
||||
* \param pucRegBuffer A buffer where the callback function should write
|
||||
* the current value of the modbus registers to.
|
||||
* \param usAddress The starting address of the register. Input registers
|
||||
* are in the range 1 - 65535.
|
||||
* \param usNRegs Number of registers the callback function must supply.
|
||||
*
|
||||
* \return The function must return one of the following error codes:
|
||||
* - eMBErrorCode::MB_ENOERR If no error occurred. In this case a normal
|
||||
* Modbus response is sent.
|
||||
* - eMBErrorCode::MB_ENOREG If the application can not supply values
|
||||
* for registers within this range. In this case a
|
||||
* <b>ILLEGAL DATA ADDRESS</b> exception frame is sent as a response.
|
||||
* - eMBErrorCode::MB_ETIMEDOUT If the requested register block is
|
||||
* currently not available and the application dependent response
|
||||
* timeout would be violated. In this case a <b>SLAVE DEVICE BUSY</b>
|
||||
* exception is sent as a response.
|
||||
* - eMBErrorCode::MB_EIO If an unrecoverable error occurred. In this case
|
||||
* a <b>SLAVE DEVICE FAILURE</b> exception is sent as a response.
|
||||
*/
|
||||
eMBErrorCode eMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress,
|
||||
USHORT usNRegs );
|
||||
|
||||
/*! \ingroup modbus_registers
|
||||
* \brief Callback function used if a <em>Holding Register</em> value is
|
||||
* read or written by the protocol stack. The starting register address
|
||||
* is given by \c usAddress and the last register is given by
|
||||
* <tt>usAddress + usNRegs - 1</tt>.
|
||||
*
|
||||
* \param pucRegBuffer If the application registers values should be updated the
|
||||
* buffer points to the new registers values. If the protocol stack needs
|
||||
* to now the current values the callback function should write them into
|
||||
* this buffer.
|
||||
* \param usAddress The starting address of the register.
|
||||
* \param usNRegs Number of registers to read or write.
|
||||
* \param eMode If eMBRegisterMode::MB_REG_WRITE the application register
|
||||
* values should be updated from the values in the buffer. For example
|
||||
* this would be the case when the Modbus master has issued an
|
||||
* <b>WRITE SINGLE REGISTER</b> command.
|
||||
* If the value eMBRegisterMode::MB_REG_READ the application should copy
|
||||
* the current values into the buffer \c pucRegBuffer.
|
||||
*
|
||||
* \return The function must return one of the following error codes:
|
||||
* - eMBErrorCode::MB_ENOERR If no error occurred. In this case a normal
|
||||
* Modbus response is sent.
|
||||
* - eMBErrorCode::MB_ENOREG If the application can not supply values
|
||||
* for registers within this range. In this case a
|
||||
* <b>ILLEGAL DATA ADDRESS</b> exception frame is sent as a response.
|
||||
* - eMBErrorCode::MB_ETIMEDOUT If the requested register block is
|
||||
* currently not available and the application dependent response
|
||||
* timeout would be violated. In this case a <b>SLAVE DEVICE BUSY</b>
|
||||
* exception is sent as a response.
|
||||
* - eMBErrorCode::MB_EIO If an unrecoverable error occurred. In this case
|
||||
* a <b>SLAVE DEVICE FAILURE</b> exception is sent as a response.
|
||||
*/
|
||||
eMBErrorCode eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress,
|
||||
USHORT usNRegs, eMBRegisterMode eMode );
|
||||
|
||||
/*! \ingroup modbus_registers
|
||||
* \brief Callback function used if a <em>Coil Register</em> value is
|
||||
* read or written by the protocol stack. If you are going to use
|
||||
* this function you might use the functions xMBUtilSetBits( ) and
|
||||
* xMBUtilGetBits( ) for working with bitfields.
|
||||
*
|
||||
* \param pucRegBuffer The bits are packed in bytes where the first coil
|
||||
* starting at address \c usAddress is stored in the LSB of the
|
||||
* first byte in the buffer <code>pucRegBuffer</code>.
|
||||
* If the buffer should be written by the callback function unused
|
||||
* coil values (I.e. if not a multiple of eight coils is used) should be set
|
||||
* to zero.
|
||||
* \param usAddress The first coil number.
|
||||
* \param usNCoils Number of coil values requested.
|
||||
* \param eMode If eMBRegisterMode::MB_REG_WRITE the application values should
|
||||
* be updated from the values supplied in the buffer \c pucRegBuffer.
|
||||
* If eMBRegisterMode::MB_REG_READ the application should store the current
|
||||
* values in the buffer \c pucRegBuffer.
|
||||
*
|
||||
* \return The function must return one of the following error codes:
|
||||
* - eMBErrorCode::MB_ENOERR If no error occurred. In this case a normal
|
||||
* Modbus response is sent.
|
||||
* - eMBErrorCode::MB_ENOREG If the application does not map an coils
|
||||
* within the requested address range. In this case a
|
||||
* <b>ILLEGAL DATA ADDRESS</b> is sent as a response.
|
||||
* - eMBErrorCode::MB_ETIMEDOUT If the requested register block is
|
||||
* currently not available and the application dependent response
|
||||
* timeout would be violated. In this case a <b>SLAVE DEVICE BUSY</b>
|
||||
* exception is sent as a response.
|
||||
* - eMBErrorCode::MB_EIO If an unrecoverable error occurred. In this case
|
||||
* a <b>SLAVE DEVICE FAILURE</b> exception is sent as a response.
|
||||
*/
|
||||
eMBErrorCode eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress,
|
||||
USHORT usNCoils, eMBRegisterMode eMode );
|
||||
|
||||
/*! \ingroup modbus_registers
|
||||
* \brief Callback function used if a <em>Input Discrete Register</em> value is
|
||||
* read by the protocol stack.
|
||||
*
|
||||
* If you are going to use his function you might use the functions
|
||||
* xMBUtilSetBits( ) and xMBUtilGetBits( ) for working with bitfields.
|
||||
*
|
||||
* \param pucRegBuffer The buffer should be updated with the current
|
||||
* coil values. The first discrete input starting at \c usAddress must be
|
||||
* stored at the LSB of the first byte in the buffer. If the requested number
|
||||
* is not a multiple of eight the remaining bits should be set to zero.
|
||||
* \param usAddress The starting address of the first discrete input.
|
||||
* \param usNDiscrete Number of discrete input values.
|
||||
* \return The function must return one of the following error codes:
|
||||
* - eMBErrorCode::MB_ENOERR If no error occurred. In this case a normal
|
||||
* Modbus response is sent.
|
||||
* - eMBErrorCode::MB_ENOREG If no such discrete inputs exists.
|
||||
* In this case a <b>ILLEGAL DATA ADDRESS</b> exception frame is sent
|
||||
* as a response.
|
||||
* - eMBErrorCode::MB_ETIMEDOUT If the requested register block is
|
||||
* currently not available and the application dependent response
|
||||
* timeout would be violated. In this case a <b>SLAVE DEVICE BUSY</b>
|
||||
* exception is sent as a response.
|
||||
* - eMBErrorCode::MB_EIO If an unrecoverable error occurred. In this case
|
||||
* a <b>SLAVE DEVICE FAILURE</b> exception is sent as a response.
|
||||
*/
|
||||
eMBErrorCode eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress,
|
||||
USHORT usNDiscrete );
|
||||
|
||||
#ifdef __cplusplus
|
||||
PR_END_EXTERN_C
|
||||
#endif
|
||||
#endif
|
||||
@@ -0,0 +1,438 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2013 Armink
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*
|
||||
* SPDX-FileContributor: 2016-2021 Espressif Systems (Shanghai) CO LTD
|
||||
*/
|
||||
/*
|
||||
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
|
||||
* Copyright (C) 2013 Armink <armink.ztl@gmail.com>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* File: $Id: mb_m.h,v 1.60 2013/09/03 10:20:05 Armink Add Master Functions $
|
||||
*/
|
||||
|
||||
#ifndef _MB_M_H
|
||||
#define _MB_M_H
|
||||
|
||||
#include "mbconfig.h"
|
||||
#include "port.h"
|
||||
#include "mb.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
PR_BEGIN_EXTERN_C
|
||||
#endif
|
||||
|
||||
#include "mbport.h"
|
||||
#include "mbproto.h"
|
||||
/*! \defgroup modbus Modbus
|
||||
* \code #include "mb.h" \endcode
|
||||
*
|
||||
* This module defines the interface for the application. It contains
|
||||
* the basic functions and types required to use the Modbus Master protocol stack.
|
||||
* A typical application will want to call eMBMasterInit() first. If the device
|
||||
* is ready to answer network requests it must then call eMBEnable() to activate
|
||||
* the protocol stack. In the main loop the function eMBMasterPoll() must be called
|
||||
* periodically. The time interval between pooling depends on the configured
|
||||
* Modbus timeout. If an RTOS is available a separate task should be created
|
||||
* and the task should always call the function eMBMasterPoll().
|
||||
*
|
||||
* \code
|
||||
* // Initialize protocol stack in RTU mode for a Master
|
||||
* eMBMasterInit( MB_RTU, 38400, MB_PAR_EVEN );
|
||||
* // Enable the Modbus Protocol Stack.
|
||||
* eMBMasterEnable( );
|
||||
* for( ;; )
|
||||
* {
|
||||
* // Call the main polling loop of the Modbus Master protocol stack.
|
||||
* eMBMasterPoll( );
|
||||
* ...
|
||||
* }
|
||||
* \endcode
|
||||
*/
|
||||
|
||||
/* ----------------------- Defines ------------------------------------------*/
|
||||
|
||||
/*! \ingroup modbus
|
||||
* \brief Use the default Modbus Master TCP port (502)
|
||||
*/
|
||||
#define MB_MASTER_TCP_PORT_USE_DEFAULT 0
|
||||
|
||||
/*! \ingroup modbus
|
||||
* \brief Errorcodes used by all function in the Master request.
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
MB_MRE_NO_ERR, /*!< no error. */
|
||||
MB_MRE_NO_REG, /*!< illegal register address. */
|
||||
MB_MRE_ILL_ARG, /*!< illegal argument. */
|
||||
MB_MRE_REV_DATA, /*!< receive data error. */
|
||||
MB_MRE_TIMEDOUT, /*!< timeout error occurred. */
|
||||
MB_MRE_MASTER_BUSY, /*!< master is busy now. */
|
||||
MB_MRE_EXE_FUN /*!< execute function error. */
|
||||
} eMBMasterReqErrCode;
|
||||
|
||||
|
||||
/*! \ingroup modbus
|
||||
* \brief Transaction information structure.
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
uint64_t xTransId;
|
||||
UCHAR ucDestAddr;
|
||||
UCHAR ucFuncCode;
|
||||
eMBException eException;
|
||||
UCHAR ucFrameError;
|
||||
} TransactionInfo_t;
|
||||
|
||||
/*! \ingroup modbus
|
||||
* \brief TimerMode is Master 3 kind of Timer modes.
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
MB_TMODE_T35, /*!< Master receive frame T3.5 timeout. */
|
||||
MB_TMODE_RESPOND_TIMEOUT, /*!< Master wait respond for slave. */
|
||||
MB_TMODE_CONVERT_DELAY /*!< Master sent broadcast ,then delay sometime.*/
|
||||
} eMBMasterTimerMode;
|
||||
|
||||
extern _lock_t xMBMLock; // Modbus lock object
|
||||
|
||||
#define MB_ATOMIC_SECTION CRITICAL_SECTION(xMBMLock)
|
||||
|
||||
/* ----------------------- Function prototypes ------------------------------*/
|
||||
/*! \ingroup modbus
|
||||
* \brief Initialize the Modbus Master protocol stack.
|
||||
*
|
||||
* This functions initializes the ASCII or RTU module and calls the
|
||||
* init functions of the porting layer to prepare the hardware. Please
|
||||
* note that the receiver is still disabled and no Modbus frames are
|
||||
* processed until eMBMasterEnable( ) has been called.
|
||||
*
|
||||
* \param eMode If ASCII or RTU mode should be used.
|
||||
* \param ucPort The port to use. E.g. 1 for COM1 on windows. This value
|
||||
* is platform dependent and some ports simply choose to ignore it.
|
||||
* \param ulBaudRate The baudrate. E.g. 19200. Supported baudrates depend
|
||||
* on the porting layer.
|
||||
* \param eParity Parity used for serial transmission.
|
||||
*
|
||||
* \return If no error occurs the function returns eMBErrorCode::MB_ENOERR.
|
||||
* The protocol is then in the disabled state and ready for activation
|
||||
* by calling eMBMasterEnable( ). Otherwise one of the following error codes
|
||||
* is returned:
|
||||
* - eMBErrorCode::MB_EPORTERR IF the porting layer returned an error.
|
||||
*/
|
||||
eMBErrorCode eMBMasterSerialInit( eMBMode eMode, UCHAR ucPort,
|
||||
ULONG ulBaudRate, eMBParity eParity );
|
||||
|
||||
/*! \ingroup modbus
|
||||
* \brief Initialize the Modbus Master protocol stack for Modbus TCP.
|
||||
*
|
||||
* This function initializes the Modbus TCP Module. Please note that
|
||||
* frame processing is still disabled until eMBEnable( ) is called.
|
||||
*
|
||||
* \param usTCPPort The TCP port to listen on.
|
||||
* \return If the protocol stack has been initialized correctly the function
|
||||
* returns eMBErrorCode::MB_ENOERR. Otherwise one of the following error
|
||||
* codes is returned:
|
||||
* - eMBErrorCode::MB_EINVAL If the slave address was not valid. Valid
|
||||
* slave addresses are in the range 1 - 247.
|
||||
* - eMBErrorCode::MB_EPORTERR IF the porting layer returned an error.
|
||||
*/
|
||||
eMBErrorCode eMBMasterTCPInit( USHORT usTCPPort );
|
||||
|
||||
/*! \ingroup modbus
|
||||
* \brief Release resources used by the protocol stack.
|
||||
*
|
||||
* This function disables the Modbus Master protocol stack and release all
|
||||
* hardware resources. It must only be called when the protocol stack
|
||||
* is disabled.
|
||||
*
|
||||
* \note Note all ports implement this function. A port which wants to
|
||||
* get an callback must define the macro MB_PORT_HAS_CLOSE to 1.
|
||||
*
|
||||
* \return If the resources where released it return eMBErrorCode::MB_ENOERR.
|
||||
* If the protocol stack is not in the disabled state it returns
|
||||
* eMBErrorCode::MB_EILLSTATE.
|
||||
*/
|
||||
eMBErrorCode eMBMasterClose( void );
|
||||
|
||||
/*! \ingroup modbus
|
||||
* \brief Enable the Modbus Master protocol stack.
|
||||
*
|
||||
* This function enables processing of Modbus Master frames. Enabling the protocol
|
||||
* stack is only possible if it is in the disabled state.
|
||||
*
|
||||
* \return If the protocol stack is now in the state enabled it returns
|
||||
* eMBErrorCode::MB_ENOERR. If it was not in the disabled state it
|
||||
* return eMBErrorCode::MB_EILLSTATE.
|
||||
*/
|
||||
eMBErrorCode eMBMasterEnable( void );
|
||||
|
||||
/*! \ingroup modbus
|
||||
* \brief Disable the Modbus Master protocol stack.
|
||||
*
|
||||
* This function disables processing of Modbus frames.
|
||||
*
|
||||
* \return If the protocol stack has been disabled it returns
|
||||
* eMBErrorCode::MB_ENOERR. If it was not in the enabled state it returns
|
||||
* eMBErrorCode::MB_EILLSTATE.
|
||||
*/
|
||||
eMBErrorCode eMBMasterDisable( void );
|
||||
|
||||
/*! \ingroup modbus
|
||||
* \brief The main pooling loop of the Modbus Master protocol stack.
|
||||
*
|
||||
* This function must be called periodically. The timer interval required
|
||||
* is given by the application dependent Modbus slave timeout. Internally the
|
||||
* function calls xMBMasterPortEventGet() and waits for an event from the receiver or
|
||||
* transmitter state machines.
|
||||
*
|
||||
* \return If the protocol stack is not in the enabled state the function
|
||||
* returns eMBErrorCode::MB_EILLSTATE. Otherwise it returns
|
||||
* eMBErrorCode::MB_ENOERR.
|
||||
*/
|
||||
eMBErrorCode eMBMasterPoll( void );
|
||||
|
||||
/*! \ingroup modbus
|
||||
* \brief Registers a callback handler for a given function code.
|
||||
*
|
||||
* This function registers a new callback handler for a given function code.
|
||||
* The callback handler supplied is responsible for interpreting the Modbus PDU and
|
||||
* the creation of an appropriate response. In case of an error it should return
|
||||
* one of the possible Modbus exceptions which results in a Modbus exception frame
|
||||
* sent by the protocol stack.
|
||||
*
|
||||
* \param ucFunctionCode The Modbus function code for which this handler should
|
||||
* be registers. Valid function codes are in the range 1 to 127.
|
||||
* \param pxHandler The function handler which should be called in case
|
||||
* such a frame is received. If \c NULL a previously registered function handler
|
||||
* for this function code is removed.
|
||||
*
|
||||
* \return eMBErrorCode::MB_ENOERR if the handler has been installed. If no
|
||||
* more resources are available it returns eMBErrorCode::MB_ENORES. In this
|
||||
* case the values in mbconfig.h should be adjusted. If the argument was not
|
||||
* valid it returns eMBErrorCode::MB_EINVAL.
|
||||
*/
|
||||
eMBErrorCode eMBMasterRegisterCB( UCHAR ucFunctionCode,
|
||||
pxMBFunctionHandler pxHandler );
|
||||
|
||||
/* ----------------------- Callback -----------------------------------------*/
|
||||
|
||||
/*! \defgroup modbus_master registers Modbus Registers
|
||||
* \code #include "mb_m.h" \endcode
|
||||
* The protocol stack does not internally allocate any memory for the
|
||||
* registers. This makes the protocol stack very small and also usable on
|
||||
* low end targets. In addition the values don't have to be in the memory
|
||||
* and could for example be stored in a flash.<br>
|
||||
* Whenever the protocol stack requires a value it calls one of the callback
|
||||
* function with the register address and the number of registers to read
|
||||
* as an argument. The application should then read the actual register values
|
||||
* (for example the ADC voltage) and should store the result in the supplied
|
||||
* buffer.<br>
|
||||
* If the protocol stack wants to update a register value because a write
|
||||
* register function was received a buffer with the new register values is
|
||||
* passed to the callback function. The function should then use these values
|
||||
* to update the application register values.
|
||||
*/
|
||||
|
||||
/*! \ingroup modbus_registers
|
||||
* \brief Callback function used if the value of a <em>Input Register</em>
|
||||
* is required by the protocol stack. The starting register address is given
|
||||
* by \c usAddress and the last register is given by <tt>usAddress +
|
||||
* usNRegs - 1</tt>.
|
||||
*
|
||||
* \param pucRegBuffer A buffer where the callback function should write
|
||||
* the current value of the modbus registers to.
|
||||
* \param usAddress The starting address of the register. Input registers
|
||||
* are in the range 1 - 65535.
|
||||
* \param usNRegs Number of registers the callback function must supply.
|
||||
*
|
||||
* \return The function must return one of the following error codes:
|
||||
* - eMBErrorCode::MB_ENOERR If no error occurred. In this case a normal
|
||||
* Modbus response is sent.
|
||||
* - eMBErrorCode::MB_ENOREG If the application does not map an coils
|
||||
* within the requested address range. In this case a
|
||||
* <b>ILLEGAL DATA ADDRESS</b> is sent as a response.
|
||||
*/
|
||||
eMBErrorCode eMBMasterRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress,
|
||||
USHORT usNRegs );
|
||||
|
||||
/*! \ingroup modbus_registers
|
||||
* \brief Callback function used if a <em>Holding Register</em> value is
|
||||
* read or written by the protocol stack. The starting register address
|
||||
* is given by \c usAddress and the last register is given by
|
||||
* <tt>usAddress + usNRegs - 1</tt>.
|
||||
*
|
||||
* \param pucRegBuffer If the application registers values should be updated the
|
||||
* buffer points to the new registers values. If the protocol stack needs
|
||||
* to now the current values the callback function should write them into
|
||||
* this buffer.
|
||||
* \param usAddress The starting address of the register.
|
||||
* \param usNRegs Number of registers to read or write.
|
||||
* \param eMode If eMBRegisterMode::MB_REG_WRITE the application register
|
||||
* values should be updated from the values in the buffer. For example
|
||||
* this would be the case when the Modbus master has issued an
|
||||
* <b>WRITE SINGLE REGISTER</b> command.
|
||||
* If the value eMBRegisterMode::MB_REG_READ the application should copy
|
||||
* the current values into the buffer \c pucRegBuffer.
|
||||
*
|
||||
* \return The function must return one of the following error codes:
|
||||
* - eMBErrorCode::MB_ENOERR If no error occurred. In this case a normal
|
||||
* Modbus response is sent.
|
||||
* - eMBErrorCode::MB_ENOREG If the application does not map an coils
|
||||
* within the requested address range. In this case a
|
||||
* <b>ILLEGAL DATA ADDRESS</b> is sent as a response.
|
||||
*/
|
||||
eMBErrorCode eMBMasterRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress,
|
||||
USHORT usNRegs, eMBRegisterMode eMode );
|
||||
|
||||
/*! \ingroup modbus_registers
|
||||
* \brief Callback function used if a <em>Coil Register</em> value is
|
||||
* read or written by the protocol stack. If you are going to use
|
||||
* this function you might use the functions xMBUtilSetBits( ) and
|
||||
* xMBUtilGetBits( ) for working with bitfields.
|
||||
*
|
||||
* \param pucRegBuffer The bits are packed in bytes where the first coil
|
||||
* starting at address \c usAddress is stored in the LSB of the
|
||||
* first byte in the buffer <code>pucRegBuffer</code>.
|
||||
* If the buffer should be written by the callback function unused
|
||||
* coil values (I.e. if not a multiple of eight coils is used) should be set
|
||||
* to zero.
|
||||
* \param usAddress The first coil number.
|
||||
* \param usNCoils Number of coil values requested.
|
||||
* \param eMode If eMBRegisterMode::MB_REG_WRITE the application values should
|
||||
* be updated from the values supplied in the buffer \c pucRegBuffer.
|
||||
* If eMBRegisterMode::MB_REG_READ the application should store the current
|
||||
* values in the buffer \c pucRegBuffer.
|
||||
*
|
||||
* \return The function must return one of the following error codes:
|
||||
* - eMBErrorCode::MB_ENOERR If no error occurred. In this case a normal
|
||||
* Modbus response is sent.
|
||||
* - eMBErrorCode::MB_ENOREG If the application does not map an coils
|
||||
* within the requested address range. In this case a
|
||||
* <b>ILLEGAL DATA ADDRESS</b> is sent as a response.
|
||||
*/
|
||||
eMBErrorCode eMBMasterRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress,
|
||||
USHORT usNCoils, eMBRegisterMode eMode );
|
||||
|
||||
/*! \ingroup modbus_registers
|
||||
* \brief Callback function used if a <em>Input Discrete Register</em> value is
|
||||
* read by the protocol stack.
|
||||
*
|
||||
* If you are going to use his function you might use the functions
|
||||
* xMBUtilSetBits( ) and xMBUtilGetBits( ) for working with bitfields.
|
||||
*
|
||||
* \param pucRegBuffer The buffer should be updated with the current
|
||||
* coil values. The first discrete input starting at \c usAddress must be
|
||||
* stored at the LSB of the first byte in the buffer. If the requested number
|
||||
* is not a multiple of eight the remaining bits should be set to zero.
|
||||
* \param usAddress The starting address of the first discrete input.
|
||||
* \param usNDiscrete Number of discrete input values.
|
||||
* \return The function must return one of the following error codes:
|
||||
* - eMBErrorCode::MB_ENOERR If no error occurred. In this case a normal
|
||||
* Modbus response is sent.
|
||||
* - eMBErrorCode::MB_ENOREG If the application does not map an coils
|
||||
* within the requested address range. In this case a
|
||||
* <b>ILLEGAL DATA ADDRESS</b> is sent as a response.
|
||||
*/
|
||||
eMBErrorCode eMBMasterRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress,
|
||||
USHORT usNDiscrete );
|
||||
|
||||
/*! \ingroup modbus
|
||||
*\brief These Modbus functions are called for user when Modbus run in Master Mode.
|
||||
*/
|
||||
eMBMasterReqErrCode
|
||||
eMBMasterReqReadInputRegister( UCHAR ucSndAddr, USHORT usRegAddr, USHORT usNRegs, LONG lTimeOut );
|
||||
eMBMasterReqErrCode
|
||||
eMBMasterReqWriteHoldingRegister( UCHAR ucSndAddr, USHORT usRegAddr, USHORT usRegData, LONG lTimeOut );
|
||||
eMBMasterReqErrCode
|
||||
eMBMasterReqWriteMultipleHoldingRegister( UCHAR ucSndAddr, USHORT usRegAddr,
|
||||
USHORT usNRegs, USHORT * pusDataBuffer, LONG lTimeOut );
|
||||
eMBMasterReqErrCode
|
||||
eMBMasterReqReadHoldingRegister( UCHAR ucSndAddr, USHORT usRegAddr, USHORT usNRegs, LONG lTimeOut );
|
||||
eMBMasterReqErrCode
|
||||
eMBMasterReqReadWriteMultipleHoldingRegister( UCHAR ucSndAddr,
|
||||
USHORT usReadRegAddr, USHORT usNReadRegs, USHORT * pusDataBuffer,
|
||||
USHORT usWriteRegAddr, USHORT usNWriteRegs, LONG lTimeOut );
|
||||
eMBMasterReqErrCode
|
||||
eMBMasterReqReadCoils( UCHAR ucSndAddr, USHORT usCoilAddr, USHORT usNCoils, LONG lTimeOut );
|
||||
eMBMasterReqErrCode
|
||||
eMBMasterReqWriteCoil( UCHAR ucSndAddr, USHORT usCoilAddr, USHORT usCoilData, LONG lTimeOut );
|
||||
eMBMasterReqErrCode
|
||||
eMBMasterReqWriteMultipleCoils( UCHAR ucSndAddr,
|
||||
USHORT usCoilAddr, USHORT usNCoils, UCHAR * pucDataBuffer, LONG lTimeOut );
|
||||
eMBMasterReqErrCode
|
||||
eMBMasterReqReadDiscreteInputs( UCHAR ucSndAddr, USHORT usDiscreteAddr, USHORT usNDiscreteIn, LONG lTimeOut );
|
||||
|
||||
eMBException
|
||||
eMBMasterFuncReportSlaveID( UCHAR * pucFrame, USHORT * usLen );
|
||||
eMBException
|
||||
eMBMasterFuncReadInputRegister( UCHAR * pucFrame, USHORT * usLen );
|
||||
eMBException
|
||||
eMBMasterFuncReadHoldingRegister( UCHAR * pucFrame, USHORT * usLen );
|
||||
eMBException
|
||||
eMBMasterFuncWriteHoldingRegister( UCHAR * pucFrame, USHORT * usLen );
|
||||
eMBException
|
||||
eMBMasterFuncWriteMultipleHoldingRegister( UCHAR * pucFrame, USHORT * usLen );
|
||||
eMBException
|
||||
eMBMasterFuncReadCoils( UCHAR * pucFrame, USHORT * usLen );
|
||||
eMBException
|
||||
eMBMasterFuncWriteCoil( UCHAR * pucFrame, USHORT * usLen );
|
||||
eMBException
|
||||
eMBMasterFuncWriteMultipleCoils( UCHAR * pucFrame, USHORT * usLen );
|
||||
eMBException
|
||||
eMBMasterFuncReadDiscreteInputs( UCHAR * pucFrame, USHORT * usLen );
|
||||
eMBException
|
||||
eMBMasterFuncReadWriteMultipleHoldingRegister( UCHAR * pucFrame, USHORT * usLen );
|
||||
|
||||
/* \ingroup modbus
|
||||
* \brief These functions are interface for Modbus Master
|
||||
*/
|
||||
void vMBMasterGetPDUSndBuf( UCHAR ** pucFrame );
|
||||
UCHAR ucMBMasterGetDestAddress( void );
|
||||
void vMBMasterSetDestAddress( UCHAR Address );
|
||||
BOOL xMBMasterGetCBRunInMasterMode( void );
|
||||
void vMBMasterSetCBRunInMasterMode( BOOL IsMasterMode );
|
||||
USHORT usMBMasterGetPDUSndLength( void );
|
||||
void vMBMasterSetPDUSndLength( USHORT SendPDULength );
|
||||
void vMBMasterSetCurTimerMode( eMBMasterTimerMode eMBTimerMode );
|
||||
void vMBMasterRequestSetType( BOOL xIsBroadcast );
|
||||
eMBMasterTimerMode xMBMasterGetCurTimerMode( void );
|
||||
BOOL xMBMasterRequestIsBroadcast( void );
|
||||
eMBMasterErrorEventType eMBMasterGetErrorType( void );
|
||||
void vMBMasterSetErrorType( eMBMasterErrorEventType errorType );
|
||||
eMBMasterReqErrCode eMBMasterWaitRequestFinish( void );
|
||||
eMBMode ucMBMasterGetCommMode( void );
|
||||
BOOL xMBMasterGetLastTransactionInfo( uint64_t *pxTransId, UCHAR *pucDestAddress,
|
||||
UCHAR *pucFunctionCode, UCHAR *pucException,
|
||||
USHORT *pusErrorType );
|
||||
|
||||
/* ----------------------- Callback -----------------------------------------*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
PR_END_EXTERN_C
|
||||
#endif
|
||||
#endif
|
||||
@@ -0,0 +1,200 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2006 Christian Walter
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*
|
||||
* SPDX-FileContributor: 2016-2021 Espressif Systems (Shanghai) CO LTD
|
||||
*/
|
||||
/*
|
||||
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
|
||||
* Copyright (c) 2006 Christian Walter <wolti@sil.at>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* File: $Id: mbconfig.h,v 1.15 2010/06/06 13:54:40 wolti Exp $
|
||||
* $Id: mbconfig.h,v 1.60 2013/08/13 21:19:55 Armink Add Master Functions $
|
||||
*/
|
||||
|
||||
#ifndef _MB_CONFIG_H
|
||||
#define _MB_CONFIG_H
|
||||
|
||||
#include <inttypes.h> // needs to be included for default system types (such as PRIxx)
|
||||
#include "sdkconfig.h" // for KConfig options
|
||||
|
||||
#if __has_include("esp_idf_version.h")
|
||||
#include "esp_idf_version.h"
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
PR_BEGIN_EXTERN_C
|
||||
#endif
|
||||
/* ----------------------- Defines ------------------------------------------*/
|
||||
/*! \defgroup modbus_cfg Modbus Configuration
|
||||
*
|
||||
* Most modules in the protocol stack are completly optional and can be
|
||||
* excluded. This is specially important if target resources are very small
|
||||
* and program memory space should be saved.<br>
|
||||
*
|
||||
* All of these settings are available in the file <code>mbconfig.h</code>
|
||||
*/
|
||||
/*! \addtogroup modbus_cfg
|
||||
* @{
|
||||
*/
|
||||
/*! \brief If Modbus Master ASCII support is enabled. */
|
||||
#define MB_MASTER_ASCII_ENABLED ( CONFIG_FMB_COMM_MODE_ASCII_EN )
|
||||
/*! \brief If Modbus Master RTU support is enabled. */
|
||||
#define MB_MASTER_RTU_ENABLED ( CONFIG_FMB_COMM_MODE_RTU_EN )
|
||||
/*! \brief If Modbus Master TCP support is enabled. */
|
||||
#define MB_MASTER_TCP_ENABLED ( CONFIG_FMB_COMM_MODE_TCP_EN )
|
||||
/*! \brief If Modbus Slave ASCII support is enabled. */
|
||||
#define MB_SLAVE_ASCII_ENABLED ( CONFIG_FMB_COMM_MODE_ASCII_EN )
|
||||
/*! \brief If Modbus Slave RTU support is enabled. */
|
||||
#define MB_SLAVE_RTU_ENABLED ( CONFIG_FMB_COMM_MODE_RTU_EN )
|
||||
/*! \brief If Modbus Slave TCP support is enabled. */
|
||||
#define MB_TCP_ENABLED ( CONFIG_FMB_COMM_MODE_TCP_EN )
|
||||
|
||||
#if !CONFIG_FMB_COMM_MODE_ASCII_EN && !CONFIG_FMB_COMM_MODE_RTU_EN && !MB_MASTER_TCP_ENABLED && !MB_TCP_ENABLED
|
||||
#error "None of Modbus communication mode is enabled. Please enable one of (ASCII, RTU, TCP) mode in Kconfig."
|
||||
#endif
|
||||
|
||||
#ifdef ESP_IDF_VERSION
|
||||
|
||||
#if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 4, 0))
|
||||
// Features supported from 4.4
|
||||
#define MB_TIMER_SUPPORTS_ISR_DISPATCH_METHOD 1
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
/*! \brief The option is required for support of RTU over TCP.
|
||||
*/
|
||||
#define MB_TCP_UID_ENABLED ( CONFIG_FMB_TCP_UID_ENABLED )
|
||||
|
||||
/*! \brief This option defines the number of data bits per ASCII character.
|
||||
*
|
||||
* A parity bit is added before the stop bit which keeps the actual byte size at 10 bits.
|
||||
*/
|
||||
#ifdef CONFIG_FMB_SERIAL_ASCII_BITS_PER_SYMB
|
||||
#define MB_ASCII_BITS_PER_SYMB ( CONFIG_FMB_SERIAL_ASCII_BITS_PER_SYMB )
|
||||
#endif
|
||||
|
||||
#define MB_EVENT_QUEUE_SIZE ( CONFIG_FMB_CONTROLLER_NOTIFY_QUEUE_SIZE )
|
||||
#define MB_EVENT_QUEUE_TIMEOUT ( pdMS_TO_TICKS( CONFIG_FMB_EVENT_QUEUE_TIMEOUT ) )
|
||||
|
||||
/*! \brief The character timeout value for Modbus ASCII.
|
||||
*
|
||||
* The character timeout value is not fixed for Modbus ASCII and is therefore
|
||||
* a configuration option. It should be set to the maximum expected delay
|
||||
* time of the network.
|
||||
*/
|
||||
#ifdef CONFIG_FMB_SERIAL_ASCII_TIMEOUT_RESPOND_MS
|
||||
#define MB_ASCII_TIMEOUT_MS ( CONFIG_FMB_SERIAL_ASCII_TIMEOUT_RESPOND_MS )
|
||||
#endif
|
||||
|
||||
/*! \brief Timeout to wait in ASCII prior to enabling transmitter.
|
||||
*
|
||||
* If defined the function calls vMBPortSerialDelay with the argument
|
||||
* MB_ASCII_TIMEOUT_WAIT_BEFORE_SEND_MS to allow for a delay before
|
||||
* the serial transmitter is enabled. This is required because some
|
||||
* targets are so fast that there is no time between receiving and
|
||||
* transmitting the frame. If the master is to slow with enabling its
|
||||
* receiver then he will not receive the response correctly.
|
||||
*/
|
||||
#ifndef CONFIG_FMB_ASCII_TIMEOUT_WAIT_BEFORE_SEND_MS
|
||||
#define MB_ASCII_TIMEOUT_WAIT_BEFORE_SEND_MS ( 0 )
|
||||
#else
|
||||
#define MB_ASCII_TIMEOUT_WAIT_BEFORE_SEND_MS ( CONFIG_FMB_ASCII_TIMEOUT_WAIT_BEFORE_SEND_MS )
|
||||
#endif
|
||||
|
||||
/*! \brief Maximum number of Modbus functions codes the protocol stack
|
||||
* should support.
|
||||
*
|
||||
* The maximum number of supported Modbus functions must be greater than
|
||||
* the sum of all enabled functions in this file and custom function
|
||||
* handlers. If set to small adding more functions will fail.
|
||||
*/
|
||||
#define MB_FUNC_HANDLERS_MAX ( 16 )
|
||||
|
||||
/*! \brief Number of bytes which should be allocated for the <em>Report Slave ID
|
||||
* </em>command.
|
||||
*
|
||||
* This number limits the maximum size of the additional segment in the
|
||||
* report slave id function. See eMBSetSlaveID( ) for more information on
|
||||
* how to set this value. It is only used if MB_FUNC_OTHER_REP_SLAVEID_ENABLED
|
||||
* is set to <code>1</code>.
|
||||
*/
|
||||
#define MB_FUNC_OTHER_REP_SLAVEID_BUF ( 32 )
|
||||
|
||||
/*! \brief If the <em>Report Slave ID</em> function should be enabled. */
|
||||
#define MB_FUNC_OTHER_REP_SLAVEID_ENABLED ( CONFIG_FMB_CONTROLLER_SLAVE_ID_SUPPORT )
|
||||
|
||||
/*! \brief If the <em>Read Input Registers</em> function should be enabled. */
|
||||
#define MB_FUNC_READ_INPUT_ENABLED ( 1 )
|
||||
|
||||
/*! \brief If the <em>Read Holding Registers</em> function should be enabled. */
|
||||
#define MB_FUNC_READ_HOLDING_ENABLED ( 1 )
|
||||
|
||||
/*! \brief If the <em>Write Single Register</em> function should be enabled. */
|
||||
#define MB_FUNC_WRITE_HOLDING_ENABLED ( 1 )
|
||||
|
||||
/*! \brief If the <em>Write Multiple registers</em> function should be enabled. */
|
||||
#define MB_FUNC_WRITE_MULTIPLE_HOLDING_ENABLED ( 1 )
|
||||
|
||||
/*! \brief If the <em>Read Coils</em> function should be enabled. */
|
||||
#define MB_FUNC_READ_COILS_ENABLED ( 1 )
|
||||
|
||||
/*! \brief If the <em>Write Coils</em> function should be enabled. */
|
||||
#define MB_FUNC_WRITE_COIL_ENABLED ( 1 )
|
||||
|
||||
/*! \brief If the <em>Write Multiple Coils</em> function should be enabled. */
|
||||
#define MB_FUNC_WRITE_MULTIPLE_COILS_ENABLED ( 1 )
|
||||
|
||||
/*! \brief If the <em>Read Discrete Inputs</em> function should be enabled. */
|
||||
#define MB_FUNC_READ_DISCRETE_INPUTS_ENABLED ( 1 )
|
||||
|
||||
/*! \brief If the <em>Read/Write Multiple Registers</em> function should be enabled. */
|
||||
#define MB_FUNC_READWRITE_HOLDING_ENABLED ( 1 )
|
||||
|
||||
/*! \brief Check the option to place timer handler into IRAM */
|
||||
#define MB_PORT_TIMER_ISR_IN_IRAM ( CONFIG_FMB_TIMER_USE_ISR_DISPATCH_METHOD )
|
||||
|
||||
/*! @} */
|
||||
#ifdef __cplusplus
|
||||
PR_END_EXTERN_C
|
||||
#endif
|
||||
|
||||
#if MB_MASTER_RTU_ENABLED || MB_MASTER_ASCII_ENABLED || MB_MASTER_TCP_ENABLED
|
||||
/*! \brief If master send a broadcast frame, the master will wait time of convert to delay,
|
||||
* then master can send other frame */
|
||||
#define MB_MASTER_DELAY_MS_CONVERT ( CONFIG_FMB_MASTER_DELAY_MS_CONVERT )
|
||||
/*! \brief If master send a frame which is not broadcast,the master will wait sometime for slave.
|
||||
* And if slave is not respond in this time,the master will process this timeout error.
|
||||
* Then master can send other frame */
|
||||
#define MB_MASTER_TIMEOUT_MS_RESPOND ( CONFIG_FMB_MASTER_TIMEOUT_MS_RESPOND )
|
||||
/*! \brief The total slaves in Modbus Master system.
|
||||
* \note : The slave ID must be continuous from 1.*/
|
||||
#define MB_MASTER_TOTAL_SLAVE_NUM ( 247 )
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,116 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2006 Christian Walter
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*
|
||||
* SPDX-FileContributor: 2016-2021 Espressif Systems (Shanghai) CO LTD
|
||||
*/
|
||||
/*
|
||||
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
|
||||
* Copyright (c) 2006 Christian Walter <wolti@sil.at>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* File: $Id: mbframe.h,v 1.9 2006/12/07 22:10:34 wolti Exp $
|
||||
*/
|
||||
|
||||
#ifndef _MB_FRAME_H
|
||||
#define _MB_FRAME_H
|
||||
|
||||
#include "port.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
PR_BEGIN_EXTERN_C
|
||||
#endif
|
||||
|
||||
/*!
|
||||
* Constants which defines the format of a modbus frame. The example is
|
||||
* shown for a Modbus RTU/ASCII frame. Note that the Modbus PDU is not
|
||||
* dependent on the underlying transport.
|
||||
*
|
||||
* <code>
|
||||
* <------------------------ MODBUS SERIAL LINE PDU (1) ------------------->
|
||||
* <----------- MODBUS PDU (1') ---------------->
|
||||
* +-----------+---------------+----------------------------+-------------+
|
||||
* | Address | Function Code | Data | CRC/LRC |
|
||||
* +-----------+---------------+----------------------------+-------------+
|
||||
* | | | |
|
||||
* (2) (3/2') (3') (4)
|
||||
*
|
||||
* (1) ... MB_SER_PDU_SIZE_MAX = 256
|
||||
* (2) ... MB_SER_PDU_ADDR_OFF = 0
|
||||
* (3) ... MB_SER_PDU_PDU_OFF = 1
|
||||
* (4) ... MB_SER_PDU_SIZE_CRC = 2
|
||||
*
|
||||
* (1') ... MB_PDU_SIZE_MAX = 253
|
||||
* (2') ... MB_PDU_FUNC_OFF = 0
|
||||
* (3') ... MB_PDU_DATA_OFF = 1
|
||||
* </code>
|
||||
*/
|
||||
|
||||
/* ----------------------- Defines ------------------------------------------*/
|
||||
#define MB_PDU_SIZE_MAX 253 /*!< Maximum size of a PDU. */
|
||||
#define MB_PDU_SIZE_MIN 1 /*!< Function Code */
|
||||
#define MB_PDU_FUNC_OFF 0 /*!< Offset of function code in PDU. */
|
||||
#define MB_PDU_DATA_OFF 1 /*!< Offset for response data in PDU. */
|
||||
|
||||
#define MB_SER_PDU_SIZE_MAX MB_SERIAL_BUF_SIZE /*!< Maximum size of a Modbus frame. */
|
||||
#define MB_SER_PDU_SIZE_LRC 1 /*!< Size of LRC field in PDU. */
|
||||
#define MB_SER_PDU_ADDR_OFF 0 /*!< Offset of slave address in Ser-PDU. */
|
||||
#define MB_SER_PDU_PDU_OFF 1 /*!< Offset of Modbus-PDU in Ser-PDU. */
|
||||
#define MB_SER_PDU_SIZE_CRC 2 /*!< Size of CRC field in PDU. */
|
||||
|
||||
#define MB_TCP_TID 0
|
||||
#define MB_TCP_PID 2
|
||||
#define MB_TCP_LEN 4
|
||||
#define MB_TCP_UID 6
|
||||
#define MB_TCP_FUNC 7
|
||||
|
||||
#if MB_MASTER_TCP_ENABLED
|
||||
#define MB_SEND_BUF_PDU_OFF MB_TCP_FUNC
|
||||
#else
|
||||
#define MB_SEND_BUF_PDU_OFF MB_SER_PDU_PDU_OFF
|
||||
#endif
|
||||
|
||||
#define MB_TCP_PSEUDO_ADDRESS 255
|
||||
|
||||
/* ----------------------- Prototypes 0-------------------------------------*/
|
||||
typedef void ( *pvMBFrameStart ) ( void );
|
||||
|
||||
typedef void ( *pvMBFrameStop ) ( void );
|
||||
|
||||
typedef eMBErrorCode( *peMBFrameReceive ) ( UCHAR * pucRcvAddress,
|
||||
UCHAR ** pucFrame,
|
||||
USHORT * pusLength );
|
||||
|
||||
typedef eMBErrorCode( *peMBFrameSend ) ( UCHAR slaveAddress,
|
||||
const UCHAR * pucFrame,
|
||||
USHORT usLength );
|
||||
|
||||
typedef void( *pvMBFrameClose ) ( void );
|
||||
|
||||
#ifdef __cplusplus
|
||||
PR_END_EXTERN_C
|
||||
#endif
|
||||
#endif
|
||||
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2006 Christian Walter
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*
|
||||
* SPDX-FileContributor: 2016-2021 Espressif Systems (Shanghai) CO LTD
|
||||
*/
|
||||
/*
|
||||
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
|
||||
* Copyright (c) 2006 Christian Walter <wolti@sil.at>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* File: $Id: mbfunc.h,v 1.12 2006/12/07 22:10:34 wolti Exp $
|
||||
*/
|
||||
|
||||
#ifndef _MB_FUNC_H
|
||||
#define _MB_FUNC_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
PR_BEGIN_EXTERN_C
|
||||
#endif
|
||||
#if MB_FUNC_OTHER_REP_SLAVEID_BUF > 0
|
||||
eMBException eMBFuncReportSlaveID( UCHAR * pucFrame, USHORT * usLen );
|
||||
#endif
|
||||
|
||||
#if MB_FUNC_READ_INPUT_ENABLED > 0
|
||||
eMBException eMBFuncReadInputRegister( UCHAR * pucFrame, USHORT * usLen );
|
||||
#endif
|
||||
|
||||
#if MB_FUNC_READ_HOLDING_ENABLED > 0
|
||||
eMBException eMBFuncReadHoldingRegister( UCHAR * pucFrame, USHORT * usLen );
|
||||
#endif
|
||||
|
||||
#if MB_FUNC_WRITE_HOLDING_ENABLED > 0
|
||||
eMBException eMBFuncWriteHoldingRegister( UCHAR * pucFrame, USHORT * usLen );
|
||||
#endif
|
||||
|
||||
#if MB_FUNC_WRITE_MULTIPLE_HOLDING_ENABLED > 0
|
||||
eMBException eMBFuncWriteMultipleHoldingRegister( UCHAR * pucFrame, USHORT * usLen );
|
||||
#endif
|
||||
|
||||
#if MB_FUNC_READ_COILS_ENABLED > 0
|
||||
eMBException eMBFuncReadCoils( UCHAR * pucFrame, USHORT * usLen );
|
||||
#endif
|
||||
|
||||
#if MB_FUNC_WRITE_COIL_ENABLED > 0
|
||||
eMBException eMBFuncWriteCoil( UCHAR * pucFrame, USHORT * usLen );
|
||||
#endif
|
||||
|
||||
#if MB_FUNC_WRITE_MULTIPLE_COILS_ENABLED > 0
|
||||
eMBException eMBFuncWriteMultipleCoils( UCHAR * pucFrame, USHORT * usLen );
|
||||
#endif
|
||||
|
||||
#if MB_FUNC_READ_DISCRETE_INPUTS_ENABLED > 0
|
||||
eMBException eMBFuncReadDiscreteInputs( UCHAR * pucFrame, USHORT * usLen );
|
||||
#endif
|
||||
|
||||
#if MB_FUNC_READWRITE_HOLDING_ENABLED > 0
|
||||
eMBException eMBFuncReadWriteMultipleHoldingRegister( UCHAR * pucFrame, USHORT * usLen );
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
PR_END_EXTERN_C
|
||||
#endif
|
||||
#endif
|
||||
@@ -0,0 +1,284 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2006 Christian Walter
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*
|
||||
* SPDX-FileContributor: 2016-2022 Espressif Systems (Shanghai) CO LTD
|
||||
*/
|
||||
/*
|
||||
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
|
||||
* Copyright (c) 2006 Christian Walter <wolti@sil.at>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* File: $Id: mbport.h,v 1.19 2010/06/06 13:54:40 wolti Exp $
|
||||
*/
|
||||
|
||||
#ifndef _MB_PORT_H
|
||||
#define _MB_PORT_H
|
||||
|
||||
#include "mbconfig.h" // for options
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
PR_BEGIN_EXTERN_C
|
||||
#endif
|
||||
|
||||
#if CONFIG_UART_ISR_IN_IRAM
|
||||
#define MB_PORT_SERIAL_ISR_FLAG ESP_INTR_FLAG_IRAM
|
||||
#else
|
||||
#define MB_PORT_SERIAL_ISR_FLAG ESP_INTR_FLAG_LOWMED
|
||||
#endif
|
||||
|
||||
#if CONFIG_FMB_TIMER_USE_ISR_DISPATCH_METHOD && MB_TIMER_SUPPORTS_ISR_DISPATCH_METHOD
|
||||
#define MB_PORT_ISR_ATTR IRAM_ATTR
|
||||
#else
|
||||
#define MB_PORT_ISR_ATTR
|
||||
#endif
|
||||
|
||||
/* ----------------------- Type definitions ---------------------------------*/
|
||||
|
||||
typedef enum
|
||||
{
|
||||
EV_READY = 0x01, /*!< Startup finished. */
|
||||
EV_FRAME_RECEIVED = 0x02, /*!< Frame received. */
|
||||
EV_EXECUTE = 0x04, /*!< Execute function. */
|
||||
EV_FRAME_SENT = 0x08, /*!< Frame sent. */
|
||||
EV_FRAME_TRANSMIT = 0x10 /*!< Frame transmit. */
|
||||
} eMBEventType;
|
||||
|
||||
#if MB_MASTER_RTU_ENABLED || MB_MASTER_ASCII_ENABLED || MB_MASTER_TCP_ENABLED
|
||||
typedef enum {
|
||||
EV_MASTER_NO_EVENT = 0x0000,
|
||||
EV_MASTER_TRANS_START = 0x0001, /*!< Transaction start flag */
|
||||
EV_MASTER_READY = 0x0002, /*!< Startup finished. */
|
||||
EV_MASTER_FRAME_RECEIVED = 0x0004, /*!< Frame received. */
|
||||
EV_MASTER_EXECUTE = 0x0008, /*!< Execute function. */
|
||||
EV_MASTER_FRAME_SENT = 0x0010, /*!< Frame sent. */
|
||||
EV_MASTER_FRAME_TRANSMIT = 0x0020, /*!< Frame transmission. */
|
||||
EV_MASTER_ERROR_PROCESS = 0x0040, /*!< Frame error process. */
|
||||
EV_MASTER_PROCESS_SUCCESS = 0x0080, /*!< Request process success. */
|
||||
EV_MASTER_ERROR_RESPOND_TIMEOUT = 0x0100, /*!< Request respond timeout. */
|
||||
EV_MASTER_ERROR_RECEIVE_DATA = 0x0200, /*!< Request receive data error. */
|
||||
EV_MASTER_ERROR_EXECUTE_FUNCTION = 0x0400 /*!< Request execute function error. */
|
||||
} eMBMasterEventEnum;
|
||||
|
||||
typedef enum {
|
||||
EV_ERROR_INIT, /*!< No error, initial state. */
|
||||
EV_ERROR_RESPOND_TIMEOUT, /*!< Slave respond timeout. */
|
||||
EV_ERROR_RECEIVE_DATA, /*!< Receive frame data error. */
|
||||
EV_ERROR_EXECUTE_FUNCTION, /*!< Execute function error. */
|
||||
EV_ERROR_OK /*!< No error, processing completed. */
|
||||
} eMBMasterErrorEventType;
|
||||
|
||||
typedef struct _MbEventType {
|
||||
eMBMasterEventEnum eEvent; /*!< event itself. */
|
||||
uint64_t xTransactionId; /*!< ID of the transaction */
|
||||
uint64_t xPostTimestamp; /*!< timestamp of event posted */
|
||||
uint64_t xGetTimestamp; /*!< timestamp of event get */
|
||||
} xMBMasterEventType;
|
||||
|
||||
#endif
|
||||
|
||||
/*! \ingroup modbus
|
||||
* \brief Parity used for characters in serial mode.
|
||||
*
|
||||
* The parity which should be applied to the characters sent over the serial
|
||||
* link. Please note that this values are actually passed to the porting
|
||||
* layer and therefore not all parity modes might be available.
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
MB_PAR_NONE, /*!< No parity. */
|
||||
MB_PAR_ODD, /*!< Odd parity. */
|
||||
MB_PAR_EVEN /*!< Even parity. */
|
||||
} eMBParity;
|
||||
|
||||
/* ----------------------- Supporting functions -----------------------------*/
|
||||
BOOL xMBPortEventInit( void );
|
||||
|
||||
BOOL xMBPortEventPost( eMBEventType eEvent );
|
||||
|
||||
BOOL xMBPortEventGet( /*@out@ */ eMBEventType * eEvent );
|
||||
|
||||
#if MB_MASTER_RTU_ENABLED || MB_MASTER_ASCII_ENABLED || MB_MASTER_TCP_ENABLED
|
||||
BOOL xMBMasterPortEventInit( void );
|
||||
|
||||
BOOL xMBMasterPortEventPost( eMBMasterEventEnum eEvent );
|
||||
|
||||
BOOL xMBMasterPortEventGet( /*@out@ */ xMBMasterEventType * eEvent );
|
||||
|
||||
eMBMasterEventEnum
|
||||
xMBMasterPortFsmWaitConfirmation( eMBMasterEventEnum eEventMask, ULONG ulTimeout);
|
||||
|
||||
void vMBMasterOsResInit( void );
|
||||
|
||||
BOOL xMBMasterRunResTake( LONG time );
|
||||
|
||||
void vMBMasterRunResRelease( void );
|
||||
|
||||
uint64_t xMBMasterPortGetTransactionId( void );
|
||||
|
||||
#endif // MB_MASTER_RTU_ENABLED || MB_MASTER_ASCII_ENABLED || MB_MASTER_TCP_ENABLED
|
||||
/* ----------------------- Serial port functions ----------------------------*/
|
||||
|
||||
BOOL xMBPortSerialInit( UCHAR ucPort, ULONG ulBaudRate,
|
||||
UCHAR ucDataBits, eMBParity eParity );
|
||||
|
||||
void vMBPortClose( void );
|
||||
|
||||
void xMBPortSerialClose( void );
|
||||
|
||||
void vMBPortSerialEnable( BOOL xRxEnable, BOOL xTxEnable );
|
||||
|
||||
BOOL xMBPortSerialGetByte( CHAR * pucByte );
|
||||
|
||||
BOOL xMBPortSerialPutByte( CHAR ucByte );
|
||||
|
||||
BOOL xMBPortSerialGetRequest( UCHAR **ppucMBSerialFrame, USHORT * pusSerialLength ) __attribute__ ((weak));
|
||||
|
||||
BOOL xMBPortSerialSendResponse( UCHAR *pucMBSerialFrame, USHORT usSerialLength ) __attribute__ ((weak));
|
||||
|
||||
#if MB_MASTER_RTU_ENABLED || MB_MASTER_ASCII_ENABLED
|
||||
BOOL xMBMasterPortSerialInit( UCHAR ucPort, ULONG ulBaudRate,
|
||||
UCHAR ucDataBits, eMBParity eParity );
|
||||
|
||||
void vMBMasterPortClose( void );
|
||||
|
||||
void xMBMasterPortSerialClose( void );
|
||||
|
||||
void vMBMasterPortSerialEnable( BOOL xRxEnable, BOOL xTxEnable );
|
||||
|
||||
BOOL xMBMasterPortSerialGetByte( CHAR * pucByte );
|
||||
|
||||
BOOL xMBMasterPortSerialPutByte( CHAR ucByte );
|
||||
|
||||
BOOL xMBMasterPortSerialGetResponse( UCHAR **ppucMBSerialFrame, USHORT * usSerialLength );
|
||||
|
||||
BOOL xMBMasterPortSerialSendRequest( UCHAR *pucMBSerialFrame, USHORT usSerialLength );
|
||||
|
||||
void vMBMasterRxFlush( void );
|
||||
|
||||
#endif
|
||||
|
||||
/* ----------------------- Timers functions ---------------------------------*/
|
||||
BOOL xMBPortTimersInit( USHORT usTimeOut50us );
|
||||
|
||||
void xMBPortTimersClose( void );
|
||||
|
||||
void vMBPortTimersEnable( void );
|
||||
|
||||
void vMBPortTimersDisable( void );
|
||||
|
||||
void vMBPortTimersDelay( USHORT usTimeOutMS );
|
||||
|
||||
#if MB_MASTER_RTU_ENABLED || MB_MASTER_ASCII_ENABLED || MB_MASTER_TCP_ENABLED
|
||||
BOOL xMBMasterPortTimersInit( USHORT usTimeOut50us );
|
||||
|
||||
void xMBMasterPortTimersClose( void );
|
||||
|
||||
void vMBMasterPortTimersT35Enable( void );
|
||||
|
||||
void vMBMasterPortTimersConvertDelayEnable( void );
|
||||
|
||||
void vMBMasterPortTimersRespondTimeoutEnable( void );
|
||||
|
||||
void vMBMasterPortTimersDisable( void );
|
||||
|
||||
|
||||
/* ----------------- Callback for the master error process ------------------*/
|
||||
void vMBMasterErrorCBRespondTimeout( uint64_t xTransId, UCHAR ucDestAddress,
|
||||
const UCHAR* pucSendData, USHORT ucSendLength );
|
||||
|
||||
void vMBMasterErrorCBReceiveData( uint64_t xTransId, UCHAR ucDestAddress,
|
||||
const UCHAR* pucRecvData, USHORT ucRecvLength,
|
||||
const UCHAR* pucSendData, USHORT ucSendLength );
|
||||
|
||||
void vMBMasterErrorCBExecuteFunction( uint64_t xTransId, UCHAR ucDestAddress,
|
||||
const UCHAR* pucRecvData, USHORT ucRecvLength,
|
||||
const UCHAR* pucSendData, USHORT ucSendLength );
|
||||
|
||||
void vMBMasterCBRequestSuccess( uint64_t xTransId, UCHAR ucDestAddress,
|
||||
const UCHAR* pucRecvData, USHORT ucRecvLength,
|
||||
const UCHAR* pucSendData, USHORT ucSendLength );
|
||||
#endif
|
||||
/* ----------------------- Callback for the protocol stack ------------------*/
|
||||
/*!
|
||||
* \brief Callback function for the porting layer when a new byte is
|
||||
* available.
|
||||
*
|
||||
* Depending upon the mode this callback function is used by the RTU or
|
||||
* ASCII transmission layers. In any case a call to xMBPortSerialGetByte()
|
||||
* must immediately return a new character.
|
||||
*
|
||||
* \return <code>TRUE</code> if a event was posted to the queue because
|
||||
* a new byte was received. The port implementation should wake up the
|
||||
* tasks which are currently blocked on the eventqueue.
|
||||
*/
|
||||
extern BOOL( *pxMBFrameCBByteReceived ) ( void );
|
||||
|
||||
extern BOOL( *pxMBFrameCBTransmitterEmpty ) ( void );
|
||||
|
||||
extern BOOL( *pxMBPortCBTimerExpired ) ( void );
|
||||
|
||||
#if MB_MASTER_RTU_ENABLED || MB_MASTER_ASCII_ENABLED || MB_MASTER_TCP_ENABLED
|
||||
extern BOOL( *pxMBMasterFrameCBByteReceived ) ( void );
|
||||
|
||||
extern BOOL( *pxMBMasterFrameCBTransmitterEmpty ) ( void );
|
||||
|
||||
extern BOOL( *pxMBMasterPortCBTimerExpired ) ( void );
|
||||
#endif
|
||||
/* ----------------------- TCP port functions -------------------------------*/
|
||||
#if MB_TCP_ENABLED
|
||||
BOOL xMBTCPPortInit( USHORT usTCPPort );
|
||||
|
||||
void vMBTCPPortClose( void );
|
||||
|
||||
void vMBTCPPortEnable( void );
|
||||
|
||||
void vMBTCPPortDisable( void );
|
||||
|
||||
BOOL xMBTCPPortGetRequest( UCHAR **ppucMBTCPFrame, USHORT * usTCPLength );
|
||||
|
||||
BOOL xMBTCPPortSendResponse( UCHAR *pucMBTCPFrame, USHORT usTCPLength );
|
||||
|
||||
#endif
|
||||
|
||||
#if MB_MASTER_TCP_ENABLED
|
||||
BOOL xMBMasterTCPPortInit( USHORT usTCPPort );
|
||||
|
||||
void vMBMasterTCPPortClose( void );
|
||||
|
||||
void vMBMasterTCPPortEnable( void );
|
||||
|
||||
void vMBMasterTCPPortDisable( void );
|
||||
|
||||
BOOL xMBMasterTCPPortGetRequest( UCHAR **ppucMBTCPFrame, USHORT * usTCPLength );
|
||||
|
||||
BOOL xMBMasterTCPPortSendResponse( UCHAR *pucMBTCPFrame, USHORT usTCPLength );
|
||||
#endif
|
||||
#ifdef __cplusplus
|
||||
PR_END_EXTERN_C
|
||||
#endif
|
||||
#endif
|
||||
@@ -0,0 +1,90 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2006 Christian Walter
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*
|
||||
* SPDX-FileContributor: 2016-2021 Espressif Systems (Shanghai) CO LTD
|
||||
*/
|
||||
/*
|
||||
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
|
||||
* Copyright (c) 2006 Christian Walter <wolti@sil.at>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* File: $Id: mbproto.h,v 1.14 2006/12/07 22:10:34 wolti Exp $
|
||||
*/
|
||||
|
||||
#ifndef _MB_PROTO_H
|
||||
#define _MB_PROTO_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
PR_BEGIN_EXTERN_C
|
||||
#endif
|
||||
/* ----------------------- Defines ------------------------------------------*/
|
||||
#define MB_ADDRESS_BROADCAST ( 0 ) /*! Modbus broadcast address. */
|
||||
#define MB_ADDRESS_MIN ( 1 ) /*! Smallest possible slave address. */
|
||||
#define MB_ADDRESS_MAX ( 247 ) /*! Biggest possible slave address. */
|
||||
#define MB_FUNC_NONE ( 0 )
|
||||
#define MB_FUNC_READ_COILS ( 1 )
|
||||
#define MB_FUNC_READ_DISCRETE_INPUTS ( 2 )
|
||||
#define MB_FUNC_WRITE_SINGLE_COIL ( 5 )
|
||||
#define MB_FUNC_WRITE_MULTIPLE_COILS ( 15 )
|
||||
#define MB_FUNC_READ_HOLDING_REGISTER ( 3 )
|
||||
#define MB_FUNC_READ_INPUT_REGISTER ( 4 )
|
||||
#define MB_FUNC_WRITE_REGISTER ( 6 )
|
||||
#define MB_FUNC_WRITE_MULTIPLE_REGISTERS ( 16 )
|
||||
#define MB_FUNC_READWRITE_MULTIPLE_REGISTERS ( 23 )
|
||||
#define MB_FUNC_DIAG_READ_EXCEPTION ( 7 )
|
||||
#define MB_FUNC_DIAG_DIAGNOSTIC ( 8 )
|
||||
#define MB_FUNC_DIAG_GET_COM_EVENT_CNT ( 11 )
|
||||
#define MB_FUNC_DIAG_GET_COM_EVENT_LOG ( 12 )
|
||||
#define MB_FUNC_OTHER_REPORT_SLAVEID ( 17 )
|
||||
#define MB_FUNC_ERROR ( 128u )
|
||||
/* ----------------------- Type definitions ---------------------------------*/
|
||||
typedef enum
|
||||
{
|
||||
MB_EX_NONE = 0x00,
|
||||
MB_EX_ILLEGAL_FUNCTION = 0x01,
|
||||
MB_EX_ILLEGAL_DATA_ADDRESS = 0x02,
|
||||
MB_EX_ILLEGAL_DATA_VALUE = 0x03,
|
||||
MB_EX_SLAVE_DEVICE_FAILURE = 0x04,
|
||||
MB_EX_ACKNOWLEDGE = 0x05,
|
||||
MB_EX_SLAVE_BUSY = 0x06,
|
||||
MB_EX_MEMORY_PARITY_ERROR = 0x08,
|
||||
MB_EX_GATEWAY_PATH_FAILED = 0x0A,
|
||||
MB_EX_GATEWAY_TGT_FAILED = 0x0B
|
||||
} eMBException;
|
||||
|
||||
typedef eMBException( *pxMBFunctionHandler ) ( UCHAR * pucFrame, USHORT * pusLength );
|
||||
|
||||
typedef struct
|
||||
{
|
||||
UCHAR ucFunctionCode;
|
||||
pxMBFunctionHandler pxHandler;
|
||||
} xMBFunctionHandler;
|
||||
|
||||
#ifdef __cplusplus
|
||||
PR_END_EXTERN_C
|
||||
#endif
|
||||
#endif
|
||||
@@ -0,0 +1,115 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2006 Christian Walter
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*
|
||||
* SPDX-FileContributor: 2016-2021 Espressif Systems (Shanghai) CO LTD
|
||||
*/
|
||||
/*
|
||||
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
|
||||
* Copyright (c) 2006 Christian Walter <wolti@sil.at>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* File: $Id: mbutils.h,v 1.5 2006/12/07 22:10:34 wolti Exp $
|
||||
*/
|
||||
|
||||
#ifndef _MB_UTILS_H
|
||||
#define _MB_UTILS_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
PR_BEGIN_EXTERN_C
|
||||
#endif
|
||||
/*! \defgroup modbus_utils Utilities
|
||||
*
|
||||
* This module contains some utility functions which can be used by
|
||||
* the application. It includes some special functions for working with
|
||||
* bitfields backed by a character array buffer.
|
||||
*
|
||||
*/
|
||||
/*! \addtogroup modbus_utils
|
||||
* @{
|
||||
*/
|
||||
/*! \brief Function to set bits in a byte buffer.
|
||||
*
|
||||
* This function allows the efficient use of an array to implement bitfields.
|
||||
* The array used for storing the bits must always be a multiple of two
|
||||
* bytes. Up to eight bits can be set or cleared in one operation.
|
||||
*
|
||||
* \param ucByteBuf A buffer where the bit values are stored. Must be a
|
||||
* multiple of 2 bytes. No length checking is performed and if
|
||||
* usBitOffset / 8 is greater than the size of the buffer memory contents
|
||||
* is overwritten.
|
||||
* \param usBitOffset The starting address of the bits to set. The first
|
||||
* bit has the offset 0.
|
||||
* \param ucNBits Number of bits to modify. The value must always be smaller
|
||||
* than 8.
|
||||
* \param ucValues Thew new values for the bits. The value for the first bit
|
||||
* starting at <code>usBitOffset</code> is the LSB of the value
|
||||
* <code>ucValues</code>
|
||||
*
|
||||
* \code
|
||||
* ucBits[2] = {0, 0};
|
||||
*
|
||||
* // Set bit 4 to 1 (read: set 1 bit starting at bit offset 4 to value 1)
|
||||
* xMBUtilSetBits( ucBits, 4, 1, 1 );
|
||||
*
|
||||
* // Set bit 7 to 1 and bit 8 to 0.
|
||||
* xMBUtilSetBits( ucBits, 7, 2, 0x01 );
|
||||
*
|
||||
* // Set bits 8 - 11 to 0x05 and bits 12 - 15 to 0x0A;
|
||||
* xMBUtilSetBits( ucBits, 8, 8, 0x5A);
|
||||
* \endcode
|
||||
*/
|
||||
void xMBUtilSetBits( UCHAR * ucByteBuf, USHORT usBitOffset,
|
||||
UCHAR ucNBits, UCHAR ucValues );
|
||||
|
||||
/*! \brief Function to read bits in a byte buffer.
|
||||
*
|
||||
* This function is used to extract up bit values from an array. Up to eight
|
||||
* bit values can be extracted in one step.
|
||||
*
|
||||
* \param ucByteBuf A buffer where the bit values are stored.
|
||||
* \param usBitOffset The starting address of the bits to set. The first
|
||||
* bit has the offset 0.
|
||||
* \param ucNBits Number of bits to modify. The value must always be smaller
|
||||
* than 8.
|
||||
*
|
||||
* \code
|
||||
* UCHAR ucBits[2] = {0, 0};
|
||||
* UCHAR ucResult;
|
||||
*
|
||||
* // Extract the bits 3 - 10.
|
||||
* ucResult = xMBUtilGetBits( ucBits, 3, 8 );
|
||||
* \endcode
|
||||
*/
|
||||
UCHAR xMBUtilGetBits( UCHAR * ucByteBuf, USHORT usBitOffset,
|
||||
UCHAR ucNBits );
|
||||
|
||||
/*! @} */
|
||||
|
||||
#ifdef __cplusplus
|
||||
PR_END_EXTERN_C
|
||||
#endif
|
||||
#endif
|
||||
441
managed_components/espressif__esp-modbus/freemodbus/modbus/mb.c
Normal file
441
managed_components/espressif__esp-modbus/freemodbus/modbus/mb.c
Normal file
@@ -0,0 +1,441 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2006 Christian Walter
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*
|
||||
* SPDX-FileContributor: 2016-2021 Espressif Systems (Shanghai) CO LTD
|
||||
*/
|
||||
/*
|
||||
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
|
||||
* Copyright (c) 2006 Christian Walter <wolti@sil.at>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* File: $Id: mb.c,v 1.28 2010/06/06 13:54:40 wolti Exp $
|
||||
*/
|
||||
|
||||
/* ----------------------- System includes ----------------------------------*/
|
||||
#include "stdlib.h"
|
||||
#include "string.h"
|
||||
|
||||
/* ----------------------- Platform includes --------------------------------*/
|
||||
#include "port.h"
|
||||
|
||||
/* ----------------------- Modbus includes ----------------------------------*/
|
||||
#include "mb.h"
|
||||
#include "mbconfig.h"
|
||||
#include "mbframe.h"
|
||||
#include "mbproto.h"
|
||||
#include "mbfunc.h"
|
||||
#include "mbport.h"
|
||||
|
||||
#if MB_SLAVE_RTU_ENABLED
|
||||
#include "mbrtu.h"
|
||||
#endif
|
||||
#if MB_SLAVE_ASCII_ENABLED
|
||||
#include "mbascii.h"
|
||||
#endif
|
||||
#if MB_TCP_ENABLED
|
||||
#include "mbtcp.h"
|
||||
#endif
|
||||
|
||||
#ifndef MB_PORT_HAS_CLOSE
|
||||
#define MB_PORT_HAS_CLOSE 1
|
||||
#endif
|
||||
|
||||
/* ----------------------- Static variables ---------------------------------*/
|
||||
|
||||
static UCHAR ucMBAddress;
|
||||
static eMBMode eMBCurrentMode;
|
||||
|
||||
volatile UCHAR ucMbSlaveBuf[MB_SERIAL_BUF_SIZE];
|
||||
|
||||
static enum
|
||||
{
|
||||
STATE_ENABLED,
|
||||
STATE_DISABLED,
|
||||
STATE_NOT_INITIALIZED
|
||||
} eMBState = STATE_NOT_INITIALIZED;
|
||||
|
||||
/* Functions pointer which are initialized in eMBInit( ). Depending on the
|
||||
* mode (RTU or ASCII) the are set to the correct implementations.
|
||||
*/
|
||||
static peMBFrameSend peMBFrameSendCur;
|
||||
static pvMBFrameStart pvMBFrameStartCur;
|
||||
static pvMBFrameStop pvMBFrameStopCur;
|
||||
static peMBFrameReceive peMBFrameReceiveCur;
|
||||
static pvMBFrameClose pvMBFrameCloseCur;
|
||||
|
||||
/* Callback functions required by the porting layer. They are called when
|
||||
* an external event has happend which includes a timeout or the reception
|
||||
* or transmission of a character.
|
||||
*/
|
||||
BOOL( *pxMBFrameCBByteReceived ) ( void );
|
||||
BOOL( *pxMBFrameCBTransmitterEmpty ) ( void );
|
||||
BOOL( *pxMBPortCBTimerExpired ) ( void );
|
||||
BOOL( *pxMBFrameCBReceiveFSMCur ) ( void );
|
||||
BOOL( *pxMBFrameCBTransmitFSMCur ) ( void );
|
||||
|
||||
/* An array of Modbus functions handlers which associates Modbus function
|
||||
* codes with implementing functions.
|
||||
*/
|
||||
static xMBFunctionHandler xFuncHandlers[MB_FUNC_HANDLERS_MAX] = {
|
||||
#if MB_FUNC_OTHER_REP_SLAVEID_ENABLED > 0
|
||||
{MB_FUNC_OTHER_REPORT_SLAVEID, eMBFuncReportSlaveID},
|
||||
#endif
|
||||
#if MB_FUNC_READ_INPUT_ENABLED > 0
|
||||
{MB_FUNC_READ_INPUT_REGISTER, eMBFuncReadInputRegister},
|
||||
#endif
|
||||
#if MB_FUNC_READ_HOLDING_ENABLED > 0
|
||||
{MB_FUNC_READ_HOLDING_REGISTER, eMBFuncReadHoldingRegister},
|
||||
#endif
|
||||
#if MB_FUNC_WRITE_MULTIPLE_HOLDING_ENABLED > 0
|
||||
{MB_FUNC_WRITE_MULTIPLE_REGISTERS, eMBFuncWriteMultipleHoldingRegister},
|
||||
#endif
|
||||
#if MB_FUNC_WRITE_HOLDING_ENABLED > 0
|
||||
{MB_FUNC_WRITE_REGISTER, eMBFuncWriteHoldingRegister},
|
||||
#endif
|
||||
#if MB_FUNC_READWRITE_HOLDING_ENABLED > 0
|
||||
{MB_FUNC_READWRITE_MULTIPLE_REGISTERS, eMBFuncReadWriteMultipleHoldingRegister},
|
||||
#endif
|
||||
#if MB_FUNC_READ_COILS_ENABLED > 0
|
||||
{MB_FUNC_READ_COILS, eMBFuncReadCoils},
|
||||
#endif
|
||||
#if MB_FUNC_WRITE_COIL_ENABLED > 0
|
||||
{MB_FUNC_WRITE_SINGLE_COIL, eMBFuncWriteCoil},
|
||||
#endif
|
||||
#if MB_FUNC_WRITE_MULTIPLE_COILS_ENABLED > 0
|
||||
{MB_FUNC_WRITE_MULTIPLE_COILS, eMBFuncWriteMultipleCoils},
|
||||
#endif
|
||||
#if MB_FUNC_READ_DISCRETE_INPUTS_ENABLED > 0
|
||||
{MB_FUNC_READ_DISCRETE_INPUTS, eMBFuncReadDiscreteInputs},
|
||||
#endif
|
||||
};
|
||||
|
||||
/* ----------------------- Start implementation -----------------------------*/
|
||||
eMBErrorCode
|
||||
eMBInit( eMBMode eMode, UCHAR ucSlaveAddress, UCHAR ucPort, ULONG ulBaudRate, eMBParity eParity )
|
||||
{
|
||||
eMBErrorCode eStatus = MB_ENOERR;
|
||||
|
||||
/* check preconditions */
|
||||
if( ( ucSlaveAddress == MB_ADDRESS_BROADCAST ) ||
|
||||
( ucSlaveAddress < MB_ADDRESS_MIN ) || ( ucSlaveAddress > MB_ADDRESS_MAX ) )
|
||||
{
|
||||
eStatus = MB_EINVAL;
|
||||
}
|
||||
else
|
||||
{
|
||||
ucMBAddress = ucSlaveAddress;
|
||||
|
||||
switch ( eMode )
|
||||
{
|
||||
#if MB_SLAVE_RTU_ENABLED > 0
|
||||
case MB_RTU:
|
||||
pvMBFrameStartCur = eMBRTUStart;
|
||||
pvMBFrameStopCur = eMBRTUStop;
|
||||
peMBFrameSendCur = eMBRTUSend;
|
||||
peMBFrameReceiveCur = eMBRTUReceive;
|
||||
pvMBFrameCloseCur = MB_PORT_HAS_CLOSE ? vMBPortClose : NULL;
|
||||
pxMBFrameCBByteReceived = xMBRTUReceiveFSM;
|
||||
pxMBFrameCBTransmitterEmpty = xMBRTUTransmitFSM;
|
||||
pxMBPortCBTimerExpired = xMBRTUTimerT35Expired;
|
||||
|
||||
eStatus = eMBRTUInit( ucMBAddress, ucPort, ulBaudRate, eParity );
|
||||
break;
|
||||
#endif
|
||||
#if MB_SLAVE_ASCII_ENABLED > 0
|
||||
case MB_ASCII:
|
||||
pvMBFrameStartCur = eMBASCIIStart;
|
||||
pvMBFrameStopCur = eMBASCIIStop;
|
||||
peMBFrameSendCur = eMBASCIISend;
|
||||
peMBFrameReceiveCur = eMBASCIIReceive;
|
||||
pvMBFrameCloseCur = MB_PORT_HAS_CLOSE ? vMBPortClose : NULL;
|
||||
pxMBFrameCBByteReceived = xMBASCIIReceiveFSM;
|
||||
pxMBFrameCBTransmitterEmpty = xMBASCIITransmitFSM;
|
||||
pxMBPortCBTimerExpired = xMBASCIITimerT1SExpired;
|
||||
|
||||
eStatus = eMBASCIIInit( ucMBAddress, ucPort, ulBaudRate, eParity );
|
||||
break;
|
||||
#endif
|
||||
default:
|
||||
eStatus = MB_EINVAL;
|
||||
}
|
||||
|
||||
if( eStatus == MB_ENOERR )
|
||||
{
|
||||
if( !xMBPortEventInit( ) )
|
||||
{
|
||||
/* port dependent event module initalization failed. */
|
||||
eStatus = MB_EPORTERR;
|
||||
}
|
||||
else
|
||||
{
|
||||
eMBCurrentMode = eMode;
|
||||
eMBState = STATE_DISABLED;
|
||||
}
|
||||
}
|
||||
}
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
#if MB_TCP_ENABLED > 0
|
||||
eMBErrorCode
|
||||
eMBTCPInit( UCHAR ucSlaveUid, USHORT ucTCPPort )
|
||||
{
|
||||
eMBErrorCode eStatus = MB_ENOERR;
|
||||
|
||||
/* Check preconditions */
|
||||
if( ucSlaveUid > MB_ADDRESS_MAX )
|
||||
{
|
||||
eStatus = MB_EINVAL;
|
||||
}
|
||||
else if( ( eStatus = eMBTCPDoInit( ucTCPPort ) ) != MB_ENOERR )
|
||||
{
|
||||
eMBState = STATE_DISABLED;
|
||||
}
|
||||
else if( !xMBPortEventInit( ) )
|
||||
{
|
||||
/* Port dependent event module initalization failed. */
|
||||
eStatus = MB_EPORTERR;
|
||||
}
|
||||
else
|
||||
{
|
||||
pvMBFrameStartCur = eMBTCPStart;
|
||||
pvMBFrameStopCur = eMBTCPStop;
|
||||
peMBFrameReceiveCur = eMBTCPReceive;
|
||||
peMBFrameSendCur = eMBTCPSend;
|
||||
pvMBFrameCloseCur = MB_PORT_HAS_CLOSE ? vMBTCPPortClose : NULL;
|
||||
ucMBAddress = ucSlaveUid;
|
||||
eMBCurrentMode = MB_TCP;
|
||||
eMBState = STATE_DISABLED;
|
||||
}
|
||||
return eStatus;
|
||||
}
|
||||
#endif
|
||||
|
||||
eMBErrorCode
|
||||
eMBRegisterCB( UCHAR ucFunctionCode, pxMBFunctionHandler pxHandler )
|
||||
{
|
||||
int i;
|
||||
eMBErrorCode eStatus;
|
||||
|
||||
if( ( 0 < ucFunctionCode ) && ( ucFunctionCode <= MB_FUNC_CODE_MAX ) )
|
||||
{
|
||||
ENTER_CRITICAL_SECTION( );
|
||||
if( pxHandler != NULL )
|
||||
{
|
||||
for( i = 0; i < MB_FUNC_HANDLERS_MAX; i++ )
|
||||
{
|
||||
if( ( xFuncHandlers[i].pxHandler == NULL ) ||
|
||||
( xFuncHandlers[i].pxHandler == pxHandler ) )
|
||||
{
|
||||
xFuncHandlers[i].ucFunctionCode = ucFunctionCode;
|
||||
xFuncHandlers[i].pxHandler = pxHandler;
|
||||
break;
|
||||
}
|
||||
}
|
||||
eStatus = ( i != MB_FUNC_HANDLERS_MAX ) ? MB_ENOERR : MB_ENORES;
|
||||
}
|
||||
else
|
||||
{
|
||||
for( i = 0; i < MB_FUNC_HANDLERS_MAX; i++ )
|
||||
{
|
||||
if( xFuncHandlers[i].ucFunctionCode == ucFunctionCode )
|
||||
{
|
||||
xFuncHandlers[i].ucFunctionCode = 0;
|
||||
xFuncHandlers[i].pxHandler = NULL;
|
||||
break;
|
||||
}
|
||||
}
|
||||
/* Remove can't fail. */
|
||||
eStatus = MB_ENOERR;
|
||||
}
|
||||
EXIT_CRITICAL_SECTION( );
|
||||
}
|
||||
else
|
||||
{
|
||||
eStatus = MB_EINVAL;
|
||||
}
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
|
||||
eMBErrorCode
|
||||
eMBClose( void )
|
||||
{
|
||||
eMBErrorCode eStatus = MB_ENOERR;
|
||||
|
||||
if( eMBState == STATE_DISABLED )
|
||||
{
|
||||
if( pvMBFrameCloseCur != NULL )
|
||||
{
|
||||
pvMBFrameCloseCur( );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
eStatus = MB_EILLSTATE;
|
||||
}
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
eMBErrorCode
|
||||
eMBEnable( void )
|
||||
{
|
||||
eMBErrorCode eStatus = MB_ENOERR;
|
||||
|
||||
if( eMBState == STATE_DISABLED )
|
||||
{
|
||||
/* Activate the protocol stack. */
|
||||
pvMBFrameStartCur( );
|
||||
eMBState = STATE_ENABLED;
|
||||
}
|
||||
else
|
||||
{
|
||||
eStatus = MB_EILLSTATE;
|
||||
}
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
eMBErrorCode
|
||||
eMBDisable( void )
|
||||
{
|
||||
eMBErrorCode eStatus;
|
||||
|
||||
if( eMBState == STATE_ENABLED )
|
||||
{
|
||||
pvMBFrameStopCur( );
|
||||
eMBState = STATE_DISABLED;
|
||||
eStatus = MB_ENOERR;
|
||||
}
|
||||
else if( eMBState == STATE_DISABLED )
|
||||
{
|
||||
eStatus = MB_ENOERR;
|
||||
}
|
||||
else
|
||||
{
|
||||
eStatus = MB_EILLSTATE;
|
||||
}
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
eMBErrorCode
|
||||
eMBPoll( void )
|
||||
{
|
||||
static UCHAR *ucMBFrame = NULL;
|
||||
static UCHAR ucRcvAddress;
|
||||
static UCHAR ucFunctionCode;
|
||||
static USHORT usLength;
|
||||
static eMBException eException;
|
||||
|
||||
int i;
|
||||
eMBErrorCode eStatus = MB_ENOERR;
|
||||
eMBEventType eEvent;
|
||||
|
||||
/* Check if the protocol stack is ready. */
|
||||
if( eMBState != STATE_ENABLED )
|
||||
{
|
||||
return MB_EILLSTATE;
|
||||
}
|
||||
|
||||
/* Check if there is a event available. If not return control to caller.
|
||||
* Otherwise we will handle the event. */
|
||||
if( xMBPortEventGet( &eEvent ) == TRUE )
|
||||
{
|
||||
switch ( eEvent )
|
||||
{
|
||||
case EV_READY:
|
||||
ESP_LOGD(MB_PORT_TAG, "%s:EV_READY", __func__);
|
||||
break;
|
||||
|
||||
case EV_FRAME_RECEIVED:
|
||||
ESP_LOGD(MB_PORT_TAG, "EV_FRAME_RECEIVED");
|
||||
eStatus = peMBFrameReceiveCur( &ucRcvAddress, &ucMBFrame, &usLength );
|
||||
if( eStatus == MB_ENOERR )
|
||||
{
|
||||
/* Check if the frame is for us. If not ignore the frame. */
|
||||
if( ( ucRcvAddress == ucMBAddress ) || ( ucRcvAddress == MB_ADDRESS_BROADCAST )
|
||||
|| ( ucRcvAddress == MB_TCP_PSEUDO_ADDRESS ) )
|
||||
{
|
||||
( void )xMBPortEventPost( EV_EXECUTE );
|
||||
ESP_LOG_BUFFER_HEX_LEVEL(MB_PORT_TAG, &ucMBFrame[MB_PDU_FUNC_OFF], usLength, ESP_LOG_DEBUG);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case EV_EXECUTE:
|
||||
if ( !ucMBFrame ) {
|
||||
return MB_EILLSTATE;
|
||||
}
|
||||
ESP_LOGD(MB_PORT_TAG, "%s:EV_EXECUTE", __func__);
|
||||
ucFunctionCode = ucMBFrame[MB_PDU_FUNC_OFF];
|
||||
eException = MB_EX_ILLEGAL_FUNCTION;
|
||||
for( i = 0; i < MB_FUNC_HANDLERS_MAX; i++ )
|
||||
{
|
||||
/* No more function handlers registered. Abort. */
|
||||
if( xFuncHandlers[i].ucFunctionCode == 0 )
|
||||
{
|
||||
break;
|
||||
}
|
||||
if( xFuncHandlers[i].ucFunctionCode == ucFunctionCode )
|
||||
{
|
||||
eException = xFuncHandlers[i].pxHandler( ucMBFrame, &usLength );
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* If the request was not sent to the broadcast address we
|
||||
* return a reply. In case of TCP the slave answers to broadcast address. */
|
||||
if( ( ucRcvAddress != MB_ADDRESS_BROADCAST ) || ( eMBCurrentMode == MB_TCP ) )
|
||||
{
|
||||
if( eException != MB_EX_NONE )
|
||||
{
|
||||
/* An exception occurred. Build an error frame. */
|
||||
usLength = 0;
|
||||
ucMBFrame[usLength++] = ( UCHAR )( ucFunctionCode | MB_FUNC_ERROR );
|
||||
ucMBFrame[usLength++] = eException;
|
||||
}
|
||||
if( ( eMBCurrentMode == MB_ASCII ) && MB_ASCII_TIMEOUT_WAIT_BEFORE_SEND_MS )
|
||||
{
|
||||
vMBPortTimersDelay( MB_ASCII_TIMEOUT_WAIT_BEFORE_SEND_MS );
|
||||
}
|
||||
eStatus = peMBFrameSendCur( ucMBAddress, ucMBFrame, usLength );
|
||||
}
|
||||
break;
|
||||
|
||||
case EV_FRAME_TRANSMIT:
|
||||
ESP_LOGD(MB_PORT_TAG, "%s:EV_FRAME_TRANSMIT", __func__);
|
||||
break;
|
||||
|
||||
case EV_FRAME_SENT:
|
||||
ESP_LOGD(MB_PORT_TAG, "%s:EV_FRAME_SENT", __func__);
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
return eStatus;
|
||||
}
|
||||
@@ -0,0 +1,641 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2013 Armink
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*
|
||||
* SPDX-FileContributor: 2016-2021 Espressif Systems (Shanghai) CO LTD
|
||||
*/
|
||||
/*
|
||||
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
|
||||
* Copyright (C) 2013 Armink <armink.ztl@gmail.com>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* File: $Id: mbrtu_m.c,v 1.60 2013/08/20 11:18:10 Armink Add Master Functions $
|
||||
*/
|
||||
|
||||
/* ----------------------- System includes ----------------------------------*/
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdatomic.h>
|
||||
|
||||
/* ----------------------- Platform includes --------------------------------*/
|
||||
#include "port.h"
|
||||
|
||||
/* ----------------------- Modbus includes ----------------------------------*/
|
||||
#include "mb_m.h"
|
||||
#include "mbconfig.h"
|
||||
#include "mbframe.h"
|
||||
#include "mbproto.h"
|
||||
#include "mbfunc.h"
|
||||
|
||||
#include "mbport.h"
|
||||
#if MB_MASTER_RTU_ENABLED
|
||||
#include "mbrtu.h"
|
||||
#endif
|
||||
#if MB_MASTER_ASCII_ENABLED
|
||||
#include "mbascii.h"
|
||||
#endif
|
||||
#if MB_MASTER_TCP_ENABLED
|
||||
#include "mbtcp.h"
|
||||
#include "mbtcp_m.h"
|
||||
#endif
|
||||
|
||||
#if MB_MASTER_RTU_ENABLED || MB_MASTER_ASCII_ENABLED || MB_MASTER_TCP_ENABLED
|
||||
|
||||
#ifndef MB_PORT_HAS_CLOSE
|
||||
#define MB_PORT_HAS_CLOSE 1
|
||||
#endif
|
||||
|
||||
/*------------------------ Shared variables ---------------------------------*/
|
||||
|
||||
_lock_t xMBMLock; // base modbus object lock
|
||||
volatile UCHAR ucMasterSndBuf[MB_SERIAL_BUF_SIZE];
|
||||
volatile UCHAR ucMasterRcvBuf[MB_SERIAL_BUF_SIZE];
|
||||
|
||||
static _Atomic USHORT usMasterSendPDULength = 0;
|
||||
static _Atomic eMBMasterErrorEventType eMBMasterCurErrorType = EV_ERROR_INIT;
|
||||
static _Atomic BOOL xMBRunInMasterMode = FALSE;
|
||||
static _Atomic UCHAR ucMBMasterDestAddress = 0;
|
||||
static _Atomic BOOL xFrameIsBroadcast = FALSE;
|
||||
|
||||
static _Atomic eMBMasterTimerMode eMasterCurTimerMode;
|
||||
|
||||
/* ----------------------- Static variables ---------------------------------*/
|
||||
static uint64_t xCurTransactionId = 0;
|
||||
static UCHAR *pucMBSendFrame = NULL;
|
||||
static UCHAR *pucMBRecvFrame = NULL;
|
||||
static UCHAR ucRecvAddress = 0;
|
||||
static eMBMode eMBMasterCurrentMode;
|
||||
|
||||
/* The transaction information structure which keep last processing state */
|
||||
static TransactionInfo_t xTransactionInfo = {0};
|
||||
|
||||
static enum
|
||||
{
|
||||
STATE_ENABLED,
|
||||
STATE_DISABLED,
|
||||
STATE_NOT_INITIALIZED
|
||||
} eMBState = STATE_NOT_INITIALIZED;
|
||||
|
||||
/* Functions pointer which are initialized in eMBInit( ). Depending on the
|
||||
* mode (RTU or ASCII) the are set to the correct implementations.
|
||||
* Using for Modbus Master,Add by Armink 20130813
|
||||
*/
|
||||
static peMBFrameSend peMBMasterFrameSendCur;
|
||||
static pvMBFrameStart pvMBMasterFrameStartCur;
|
||||
static pvMBFrameStop pvMBMasterFrameStopCur;
|
||||
static peMBFrameReceive peMBMasterFrameReceiveCur;
|
||||
static pvMBFrameClose pvMBMasterFrameCloseCur;
|
||||
|
||||
/* Callback functions required by the porting layer. They are called when
|
||||
* an external event has happend which includes a timeout or the reception
|
||||
* or transmission of a character.
|
||||
* Using for Modbus Master,Add by Armink 20130813
|
||||
*/
|
||||
BOOL( *pxMBMasterFrameCBByteReceived ) ( void );
|
||||
|
||||
BOOL( *pxMBMasterFrameCBTransmitterEmpty ) ( void );
|
||||
|
||||
BOOL( *pxMBMasterPortCBTimerExpired ) ( void );
|
||||
|
||||
BOOL( *pxMBMasterFrameCBReceiveFSMCur ) ( void );
|
||||
|
||||
BOOL( *pxMBMasterFrameCBTransmitFSMCur ) ( void );
|
||||
|
||||
/* An array of Modbus functions handlers which associates Modbus function
|
||||
* codes with implementing functions.
|
||||
*/
|
||||
static xMBFunctionHandler xMasterFuncHandlers[MB_FUNC_HANDLERS_MAX] = {
|
||||
#if MB_FUNC_OTHER_REP_SLAVEID_ENABLED > 0
|
||||
{MB_FUNC_OTHER_REPORT_SLAVEID, eMBFuncReportSlaveID},
|
||||
#endif
|
||||
#if MB_FUNC_READ_INPUT_ENABLED > 0
|
||||
{MB_FUNC_READ_INPUT_REGISTER, eMBMasterFuncReadInputRegister},
|
||||
#endif
|
||||
#if MB_FUNC_READ_HOLDING_ENABLED > 0
|
||||
{MB_FUNC_READ_HOLDING_REGISTER, eMBMasterFuncReadHoldingRegister},
|
||||
#endif
|
||||
#if MB_FUNC_WRITE_MULTIPLE_HOLDING_ENABLED > 0
|
||||
{MB_FUNC_WRITE_MULTIPLE_REGISTERS, eMBMasterFuncWriteMultipleHoldingRegister},
|
||||
#endif
|
||||
#if MB_FUNC_WRITE_HOLDING_ENABLED > 0
|
||||
{MB_FUNC_WRITE_REGISTER, eMBMasterFuncWriteHoldingRegister},
|
||||
#endif
|
||||
#if MB_FUNC_READWRITE_HOLDING_ENABLED > 0
|
||||
{MB_FUNC_READWRITE_MULTIPLE_REGISTERS, eMBMasterFuncReadWriteMultipleHoldingRegister},
|
||||
#endif
|
||||
#if MB_FUNC_READ_COILS_ENABLED > 0
|
||||
{MB_FUNC_READ_COILS, eMBMasterFuncReadCoils},
|
||||
#endif
|
||||
#if MB_FUNC_WRITE_COIL_ENABLED > 0
|
||||
{MB_FUNC_WRITE_SINGLE_COIL, eMBMasterFuncWriteCoil},
|
||||
#endif
|
||||
#if MB_FUNC_WRITE_MULTIPLE_COILS_ENABLED > 0
|
||||
{MB_FUNC_WRITE_MULTIPLE_COILS, eMBMasterFuncWriteMultipleCoils},
|
||||
#endif
|
||||
#if MB_FUNC_READ_DISCRETE_INPUTS_ENABLED > 0
|
||||
{MB_FUNC_READ_DISCRETE_INPUTS, eMBMasterFuncReadDiscreteInputs},
|
||||
#endif
|
||||
};
|
||||
|
||||
/* ----------------------- Start implementation -----------------------------*/
|
||||
#if MB_MASTER_TCP_ENABLED
|
||||
eMBErrorCode
|
||||
eMBMasterTCPInit( USHORT ucTCPPort )
|
||||
{
|
||||
eMBErrorCode eStatus = MB_ENOERR;
|
||||
|
||||
if( ( eStatus = eMBMasterTCPDoInit( ucTCPPort ) ) != MB_ENOERR ) {
|
||||
eMBState = STATE_DISABLED;
|
||||
}
|
||||
else if( !xMBMasterPortEventInit( ) ) {
|
||||
/* Port dependent event module initialization failed. */
|
||||
eStatus = MB_EPORTERR;
|
||||
} else {
|
||||
pvMBMasterFrameStartCur = eMBMasterTCPStart;
|
||||
pvMBMasterFrameStopCur = eMBMasterTCPStop;
|
||||
peMBMasterFrameReceiveCur = eMBMasterTCPReceive;
|
||||
peMBMasterFrameSendCur = eMBMasterTCPSend;
|
||||
pxMBMasterPortCBTimerExpired = xMBMasterTCPTimerExpired;
|
||||
pvMBMasterFrameCloseCur = MB_PORT_HAS_CLOSE ? vMBMasterTCPPortClose : NULL;
|
||||
eMBMasterCurrentMode = MB_TCP;
|
||||
eMBState = STATE_DISABLED;
|
||||
ucRecvAddress = MB_TCP_PSEUDO_ADDRESS;
|
||||
xCurTransactionId = 0;
|
||||
xTransactionInfo.xTransId = 0;
|
||||
xTransactionInfo.ucDestAddr = 0;
|
||||
xTransactionInfo.ucFuncCode = 0;
|
||||
xTransactionInfo.eException = MB_EX_NONE;
|
||||
xTransactionInfo.ucFrameError = 0;
|
||||
|
||||
/* initialize the state values. */
|
||||
atomic_init(&usMasterSendPDULength, 0);
|
||||
atomic_init(&eMBMasterCurErrorType, EV_ERROR_INIT);
|
||||
atomic_init(&xMBRunInMasterMode, FALSE);
|
||||
atomic_init(&ucMBMasterDestAddress, MB_TCP_PSEUDO_ADDRESS);
|
||||
|
||||
// initialize the OS resource for modbus master.
|
||||
vMBMasterOsResInit();
|
||||
if (xMBMasterPortTimersInit(MB_MASTER_TIMEOUT_MS_RESPOND * MB_TIMER_TICS_PER_MS) != TRUE)
|
||||
{
|
||||
eStatus = MB_EPORTERR;
|
||||
}
|
||||
|
||||
}
|
||||
return eStatus;
|
||||
}
|
||||
#endif
|
||||
|
||||
eMBErrorCode
|
||||
eMBMasterSerialInit( eMBMode eMode, UCHAR ucPort, ULONG ulBaudRate, eMBParity eParity )
|
||||
{
|
||||
eMBErrorCode eStatus = MB_ENOERR;
|
||||
|
||||
switch (eMode)
|
||||
{
|
||||
#if MB_MASTER_RTU_ENABLED > 0
|
||||
case MB_RTU:
|
||||
pvMBMasterFrameStartCur = eMBMasterRTUStart;
|
||||
pvMBMasterFrameStopCur = eMBMasterRTUStop;
|
||||
peMBMasterFrameSendCur = eMBMasterRTUSend;
|
||||
peMBMasterFrameReceiveCur = eMBMasterRTUReceive;
|
||||
pvMBMasterFrameCloseCur = MB_PORT_HAS_CLOSE ? vMBMasterPortClose : NULL;
|
||||
pxMBMasterFrameCBByteReceived = xMBMasterRTUReceiveFSM;
|
||||
pxMBMasterFrameCBTransmitterEmpty = xMBMasterRTUTransmitFSM;
|
||||
pxMBMasterPortCBTimerExpired = xMBMasterRTUTimerExpired;
|
||||
eMBMasterCurrentMode = eMode;
|
||||
|
||||
eStatus = eMBMasterRTUInit(ucPort, ulBaudRate, eParity);
|
||||
break;
|
||||
#endif
|
||||
#if MB_MASTER_ASCII_ENABLED > 0
|
||||
case MB_ASCII:
|
||||
pvMBMasterFrameStartCur = eMBMasterASCIIStart;
|
||||
pvMBMasterFrameStopCur = eMBMasterASCIIStop;
|
||||
peMBMasterFrameSendCur = eMBMasterASCIISend;
|
||||
peMBMasterFrameReceiveCur = eMBMasterASCIIReceive;
|
||||
pvMBMasterFrameCloseCur = MB_PORT_HAS_CLOSE ? vMBMasterPortClose : NULL;
|
||||
pxMBMasterFrameCBByteReceived = xMBMasterASCIIReceiveFSM;
|
||||
pxMBMasterFrameCBTransmitterEmpty = xMBMasterASCIITransmitFSM;
|
||||
pxMBMasterPortCBTimerExpired = xMBMasterASCIITimerT1SExpired;
|
||||
eMBMasterCurrentMode = eMode;
|
||||
|
||||
eStatus = eMBMasterASCIIInit(ucPort, ulBaudRate, eParity );
|
||||
break;
|
||||
#endif
|
||||
default:
|
||||
eStatus = MB_EINVAL;
|
||||
break;
|
||||
}
|
||||
|
||||
if (eStatus == MB_ENOERR)
|
||||
{
|
||||
if (!xMBMasterPortEventInit())
|
||||
{
|
||||
/* port dependent event module initalization failed. */
|
||||
eStatus = MB_EPORTERR;
|
||||
}
|
||||
else
|
||||
{
|
||||
eMBState = STATE_DISABLED;
|
||||
ucRecvAddress = 0;
|
||||
xCurTransactionId = 0;
|
||||
xTransactionInfo.xTransId = 0;
|
||||
xTransactionInfo.ucDestAddr = 0;
|
||||
xTransactionInfo.ucFuncCode = 0;
|
||||
xTransactionInfo.eException = MB_EX_NONE;
|
||||
xTransactionInfo.ucFrameError = 0;
|
||||
|
||||
/* initialize the state values. */
|
||||
atomic_init(&usMasterSendPDULength, 0);
|
||||
atomic_init(&eMBMasterCurErrorType, EV_ERROR_INIT);
|
||||
atomic_init(&xMBRunInMasterMode, FALSE);
|
||||
atomic_init(&ucMBMasterDestAddress, 0);
|
||||
}
|
||||
/* initialize the OS resource for modbus master. */
|
||||
vMBMasterOsResInit();
|
||||
}
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
eMBErrorCode
|
||||
eMBMasterClose( void )
|
||||
{
|
||||
eMBErrorCode eStatus = MB_ENOERR;
|
||||
|
||||
if( eMBState == STATE_DISABLED )
|
||||
{
|
||||
if( pvMBMasterFrameCloseCur != NULL )
|
||||
{
|
||||
pvMBMasterFrameCloseCur( );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
eStatus = MB_EILLSTATE;
|
||||
}
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
eMBErrorCode
|
||||
eMBMasterEnable( void )
|
||||
{
|
||||
eMBErrorCode eStatus = MB_ENOERR;
|
||||
|
||||
if( eMBState == STATE_DISABLED )
|
||||
{
|
||||
/* Activate the protocol stack. */
|
||||
pvMBMasterFrameStartCur( );
|
||||
/* Release the resource, because it created in busy state */
|
||||
//vMBMasterRunResRelease( );
|
||||
eMBState = STATE_ENABLED;
|
||||
}
|
||||
else
|
||||
{
|
||||
eStatus = MB_EILLSTATE;
|
||||
}
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
eMBErrorCode
|
||||
eMBMasterDisable( void )
|
||||
{
|
||||
eMBErrorCode eStatus;
|
||||
|
||||
if( eMBState == STATE_ENABLED )
|
||||
{
|
||||
pvMBMasterFrameStopCur( );
|
||||
eMBState = STATE_DISABLED;
|
||||
eStatus = MB_ENOERR;
|
||||
}
|
||||
else if( eMBState == STATE_DISABLED )
|
||||
{
|
||||
eStatus = MB_ENOERR;
|
||||
}
|
||||
else
|
||||
{
|
||||
eStatus = MB_EILLSTATE;
|
||||
}
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
eMBErrorCode
|
||||
eMBMasterPoll( void )
|
||||
{
|
||||
int i;
|
||||
int j;
|
||||
eMBErrorCode eStatus = MB_ENOERR;
|
||||
xMBMasterEventType xEvent;
|
||||
eMBMasterErrorEventType errorType = EV_ERROR_INIT;
|
||||
static eMBException eException = MB_EX_NONE;
|
||||
static UCHAR ucFunctionCode = 0;
|
||||
static USHORT usRecvLength = 0;
|
||||
|
||||
/* Check if the protocol stack is ready. */
|
||||
if( eMBState != STATE_ENABLED ) {
|
||||
return MB_EILLSTATE;
|
||||
}
|
||||
|
||||
/* Check if there is a event available. If not return control to caller.
|
||||
* Otherwise we will handle the event. */
|
||||
if ( xMBMasterPortEventGet( &xEvent ) == TRUE ) {
|
||||
switch( xEvent.eEvent ) {
|
||||
// In some cases it is possible that more than one event set
|
||||
// together (even from one subset mask) than process them consistently
|
||||
case EV_MASTER_READY:
|
||||
ESP_LOGD(MB_PORT_TAG, "%" PRIu64 ":EV_MASTER_READY", xEvent.xTransactionId);
|
||||
vMBMasterSetErrorType( EV_ERROR_INIT );
|
||||
vMBMasterRunResRelease( );
|
||||
break;
|
||||
case EV_MASTER_FRAME_TRANSMIT:
|
||||
ESP_LOGD(MB_PORT_TAG, "%" PRIu64 ":EV_MASTER_FRAME_TRANSMIT", xEvent.xTransactionId);
|
||||
/* Master is busy now. */
|
||||
vMBMasterGetPDUSndBuf( &pucMBSendFrame );
|
||||
ESP_LOG_BUFFER_HEX_LEVEL("POLL transmit buffer", (void*)pucMBSendFrame, usMBMasterGetPDUSndLength(), ESP_LOG_DEBUG);
|
||||
eStatus = peMBMasterFrameSendCur( ucMBMasterGetDestAddress(), pucMBSendFrame, usMBMasterGetPDUSndLength() );
|
||||
if (eStatus != MB_ENOERR) {
|
||||
vMBMasterSetErrorType(EV_ERROR_RECEIVE_DATA);
|
||||
( void ) xMBMasterPortEventPost( EV_MASTER_ERROR_PROCESS );
|
||||
ESP_LOGE( MB_PORT_TAG, "%" PRIu64 ":Frame send error = %d", xEvent.xTransactionId, (unsigned)eStatus );
|
||||
}
|
||||
xCurTransactionId = xEvent.xTransactionId;
|
||||
break;
|
||||
case EV_MASTER_FRAME_SENT:
|
||||
if (xCurTransactionId == xEvent.xTransactionId) {
|
||||
ESP_LOGD( MB_PORT_TAG, "%" PRIu64 ":EV_MASTER_FRAME_SENT", xEvent.xTransactionId );
|
||||
ESP_LOG_BUFFER_HEX_LEVEL("POLL sent buffer", (void*)pucMBSendFrame, usMBMasterGetPDUSndLength(), ESP_LOG_DEBUG);
|
||||
}
|
||||
break;
|
||||
case EV_MASTER_FRAME_RECEIVED:
|
||||
ESP_LOGD( MB_PORT_TAG, "%" PRIu64 ":EV_MASTER_FRAME_RECEIVED", xEvent.xTransactionId );
|
||||
eStatus = peMBMasterFrameReceiveCur( &ucRecvAddress, &pucMBRecvFrame, &usRecvLength);
|
||||
if (xCurTransactionId == xEvent.xTransactionId) {
|
||||
MB_PORT_CHECK(pucMBSendFrame, MB_EILLSTATE, "Send buffer initialization fail.");
|
||||
// Check if the frame is for us. If not ,send an error process event.
|
||||
if ( ( eStatus == MB_ENOERR ) && ( ( ucRecvAddress == ucMBMasterGetDestAddress() )
|
||||
|| ( ucRecvAddress == MB_TCP_PSEUDO_ADDRESS) ) ) {
|
||||
if ( ( pucMBRecvFrame[MB_PDU_FUNC_OFF] & ~MB_FUNC_ERROR ) == ( pucMBSendFrame[MB_PDU_FUNC_OFF] ) ) {
|
||||
ESP_LOGD(MB_PORT_TAG, "%" PRIu64 ": Packet data received successfully (%u).", xEvent.xTransactionId, (unsigned)eStatus);
|
||||
ESP_LOG_BUFFER_HEX_LEVEL("POLL receive buffer", (void*)pucMBRecvFrame, (uint16_t)usRecvLength, ESP_LOG_DEBUG);
|
||||
( void ) xMBMasterPortEventPost( EV_MASTER_EXECUTE );
|
||||
} else {
|
||||
ESP_LOGE( MB_PORT_TAG, "Drop incorrect frame, receive_func(%u) != send_func(%u)",
|
||||
pucMBRecvFrame[MB_PDU_FUNC_OFF], pucMBSendFrame[MB_PDU_FUNC_OFF]);
|
||||
vMBMasterSetErrorType(EV_ERROR_RECEIVE_DATA);
|
||||
( void ) xMBMasterPortEventPost( EV_MASTER_ERROR_PROCESS );
|
||||
}
|
||||
} else {
|
||||
vMBMasterSetErrorType(EV_ERROR_RECEIVE_DATA);
|
||||
( void ) xMBMasterPortEventPost( EV_MASTER_ERROR_PROCESS );
|
||||
ESP_LOGD( MB_PORT_TAG, "%" PRIu64 ": Packet data receive failed (addr=%u)(%u).",
|
||||
xEvent.xTransactionId, (unsigned)ucRecvAddress, (unsigned)eStatus);
|
||||
}
|
||||
} else {
|
||||
// Ignore the `EV_MASTER_FRAME_RECEIVED` event because the respond timeout occurred
|
||||
// and this is likely respond to previous transaction
|
||||
ESP_LOGE( MB_PORT_TAG, "Drop data received outside of transaction (%" PRIu64 ")", xEvent.xTransactionId );
|
||||
}
|
||||
break;
|
||||
case EV_MASTER_EXECUTE:
|
||||
if (xCurTransactionId == xEvent.xTransactionId) {
|
||||
if ( xMBMasterRequestIsBroadcast()
|
||||
&& (( ucMBMasterGetCommMode() == MB_RTU ) || ( ucMBMasterGetCommMode() == MB_ASCII ) ) ) {
|
||||
pucMBRecvFrame = pucMBSendFrame;
|
||||
}
|
||||
MB_PORT_CHECK(pucMBRecvFrame, MB_EILLSTATE, "receive buffer initialization fail.");
|
||||
ESP_LOGD(MB_PORT_TAG, "%" PRIu64 ":EV_MASTER_EXECUTE", xEvent.xTransactionId);
|
||||
ucFunctionCode = pucMBRecvFrame[MB_PDU_FUNC_OFF];
|
||||
eException = MB_EX_ILLEGAL_FUNCTION;
|
||||
/* If receive frame has exception. The receive function code highest bit is 1.*/
|
||||
if (ucFunctionCode & MB_FUNC_ERROR) {
|
||||
eException = (eMBException)pucMBRecvFrame[MB_PDU_DATA_OFF];
|
||||
} else {
|
||||
for ( i = 0; i < MB_FUNC_HANDLERS_MAX; i++ )
|
||||
{
|
||||
/* No more function handlers registered. Abort. */
|
||||
if (xMasterFuncHandlers[i].ucFunctionCode == 0) {
|
||||
break;
|
||||
}
|
||||
if (xMasterFuncHandlers[i].ucFunctionCode == ucFunctionCode) {
|
||||
vMBMasterSetCBRunInMasterMode(TRUE);
|
||||
/* If master request is broadcast,
|
||||
* the master need execute function for all slave.
|
||||
*/
|
||||
if ( xMBMasterRequestIsBroadcast() ) {
|
||||
USHORT usLength = usMBMasterGetPDUSndLength();
|
||||
for(j = 1; j <= MB_MASTER_TOTAL_SLAVE_NUM; j++)
|
||||
{
|
||||
vMBMasterSetDestAddress(j);
|
||||
eException = xMasterFuncHandlers[i].pxHandler(pucMBRecvFrame, &usLength);
|
||||
}
|
||||
} else {
|
||||
eException = xMasterFuncHandlers[i].pxHandler(pucMBRecvFrame, &usRecvLength);
|
||||
}
|
||||
vMBMasterSetCBRunInMasterMode( FALSE );
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
/* If master has exception, will send error process event. Otherwise the master is idle.*/
|
||||
if ( eException != MB_EX_NONE ) {
|
||||
vMBMasterSetErrorType( EV_ERROR_EXECUTE_FUNCTION );
|
||||
( void ) xMBMasterPortEventPost( EV_MASTER_ERROR_PROCESS );
|
||||
} else {
|
||||
if ( eMBMasterGetErrorType( ) == EV_ERROR_INIT ) {
|
||||
vMBMasterSetErrorType(EV_ERROR_OK);
|
||||
ESP_LOGD( MB_PORT_TAG, "%" PRIu64 ":set event EV_ERROR_OK", xEvent.xTransactionId );
|
||||
( void ) xMBMasterPortEventPost( EV_MASTER_ERROR_PROCESS );
|
||||
}
|
||||
}
|
||||
} else {
|
||||
ESP_LOGD( MB_PORT_TAG, "%" PRIu64 ":EV_MASTER_EXECUTE is expired", xEvent.xTransactionId );
|
||||
}
|
||||
break;
|
||||
case EV_MASTER_ERROR_PROCESS:
|
||||
if (xCurTransactionId == xEvent.xTransactionId) {
|
||||
ESP_LOGD( MB_PORT_TAG, "%" PRIu64 ":EV_MASTER_ERROR_PROCESS", xEvent.xTransactionId);
|
||||
/* Execute specified error process callback function. */
|
||||
errorType = eMBMasterGetErrorType( );
|
||||
vMBMasterGetPDUSndBuf( &pucMBSendFrame );
|
||||
switch ( errorType )
|
||||
{
|
||||
case EV_ERROR_RESPOND_TIMEOUT:
|
||||
vMBMasterErrorCBRespondTimeout( xEvent.xTransactionId,
|
||||
ucMBMasterGetDestAddress( ),
|
||||
pucMBSendFrame, usMBMasterGetPDUSndLength( ) );
|
||||
break;
|
||||
case EV_ERROR_RECEIVE_DATA:
|
||||
vMBMasterErrorCBReceiveData( xEvent.xTransactionId,
|
||||
ucMBMasterGetDestAddress( ),
|
||||
pucMBRecvFrame, usRecvLength,
|
||||
pucMBSendFrame, usMBMasterGetPDUSndLength( ) );
|
||||
break;
|
||||
case EV_ERROR_EXECUTE_FUNCTION:
|
||||
vMBMasterErrorCBExecuteFunction( xEvent.xTransactionId,
|
||||
ucMBMasterGetDestAddress( ),
|
||||
pucMBRecvFrame, usRecvLength,
|
||||
pucMBSendFrame, usMBMasterGetPDUSndLength( ) );
|
||||
break;
|
||||
case EV_ERROR_OK:
|
||||
vMBMasterCBRequestSuccess( xEvent.xTransactionId,
|
||||
ucMBMasterGetDestAddress( ),
|
||||
pucMBRecvFrame, usRecvLength,
|
||||
pucMBSendFrame, usMBMasterGetPDUSndLength( ) );
|
||||
break;
|
||||
default:
|
||||
ESP_LOGE( MB_PORT_TAG, "%" PRIu64 ":incorrect error type = %d.", xEvent.xTransactionId, (int)errorType);
|
||||
break;
|
||||
}
|
||||
}
|
||||
vMBMasterPortTimersDisable( );
|
||||
uint64_t xProcTime = xCurTransactionId ? ( xEvent.xPostTimestamp - xCurTransactionId ) : 0;
|
||||
ESP_LOGD( MB_PORT_TAG, "Transaction (%" PRIu64 "), processing time(us) = %" PRId64, xCurTransactionId, xProcTime );
|
||||
MB_ATOMIC_SECTION {
|
||||
xTransactionInfo.xTransId = xCurTransactionId;
|
||||
xTransactionInfo.ucDestAddr = ucMBMasterGetDestAddress();
|
||||
xTransactionInfo.ucFuncCode = ucFunctionCode;
|
||||
xTransactionInfo.eException = eException;
|
||||
xTransactionInfo.ucFrameError = errorType;
|
||||
}
|
||||
xCurTransactionId = 0;
|
||||
vMBMasterSetErrorType( EV_ERROR_INIT );
|
||||
vMBMasterRunResRelease( );
|
||||
break;
|
||||
default:
|
||||
ESP_LOGE( MB_PORT_TAG, "%" PRIu64 ":Unexpected event triggered 0x%02x.", xEvent.xTransactionId, (int)xEvent.eEvent );
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
// Something went wrong and task unblocked but there are no any correct events set
|
||||
ESP_LOGE( MB_PORT_TAG, "%" PRIu64 ": Unexpected event triggered 0x%02x.", xEvent.xTransactionId, (int)xEvent.eEvent );
|
||||
eStatus = MB_EILLSTATE;
|
||||
}
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
// Get whether the Modbus Master is run in master mode.
|
||||
BOOL xMBMasterGetCBRunInMasterMode( void )
|
||||
{
|
||||
return atomic_load(&xMBRunInMasterMode);
|
||||
}
|
||||
|
||||
// Set whether the Modbus Master is run in master mode.
|
||||
void vMBMasterSetCBRunInMasterMode( BOOL IsMasterMode )
|
||||
{
|
||||
atomic_store(&xMBRunInMasterMode, IsMasterMode);
|
||||
}
|
||||
|
||||
// Get Modbus Master send destination address.
|
||||
UCHAR ucMBMasterGetDestAddress( void )
|
||||
{
|
||||
return atomic_load(&ucMBMasterDestAddress);
|
||||
}
|
||||
|
||||
// Set Modbus Master send destination address.
|
||||
void vMBMasterSetDestAddress( UCHAR Address )
|
||||
{
|
||||
atomic_store(&ucMBMasterDestAddress, Address);
|
||||
}
|
||||
|
||||
// Get Modbus Master current error event type.
|
||||
eMBMasterErrorEventType inline eMBMasterGetErrorType( void )
|
||||
{
|
||||
return atomic_load(&eMBMasterCurErrorType);
|
||||
}
|
||||
|
||||
// Set Modbus Master current error event type.
|
||||
void IRAM_ATTR vMBMasterSetErrorType( eMBMasterErrorEventType errorType )
|
||||
{
|
||||
atomic_store(&eMBMasterCurErrorType, errorType);
|
||||
}
|
||||
|
||||
/* Get Modbus Master send PDU's buffer address pointer.*/
|
||||
void vMBMasterGetPDUSndBuf( UCHAR ** pucFrame )
|
||||
{
|
||||
*pucFrame = ( UCHAR * ) &ucMasterSndBuf[MB_SEND_BUF_PDU_OFF];
|
||||
}
|
||||
|
||||
/* Set Modbus Master send PDU's buffer length.*/
|
||||
void vMBMasterSetPDUSndLength( USHORT SendPDULength )
|
||||
{
|
||||
atomic_store(&usMasterSendPDULength, SendPDULength);
|
||||
}
|
||||
|
||||
/* Get Modbus Master send PDU's buffer length.*/
|
||||
USHORT usMBMasterGetPDUSndLength( void )
|
||||
{
|
||||
return atomic_load(&usMasterSendPDULength);
|
||||
}
|
||||
|
||||
/* Set Modbus Master current timer mode.*/
|
||||
void vMBMasterSetCurTimerMode( eMBMasterTimerMode eMBTimerMode )
|
||||
{
|
||||
atomic_store(&eMasterCurTimerMode, eMBTimerMode);
|
||||
}
|
||||
|
||||
/* Get Modbus Master current timer mode.*/
|
||||
eMBMasterTimerMode MB_PORT_ISR_ATTR xMBMasterGetCurTimerMode( void )
|
||||
{
|
||||
return atomic_load(&eMasterCurTimerMode);
|
||||
}
|
||||
|
||||
/* The master request is broadcast? */
|
||||
BOOL MB_PORT_ISR_ATTR xMBMasterRequestIsBroadcast( void )
|
||||
{
|
||||
return atomic_load(&xFrameIsBroadcast);
|
||||
}
|
||||
|
||||
/* The master request is broadcast? */
|
||||
void vMBMasterRequestSetType( BOOL xIsBroadcast )
|
||||
{
|
||||
atomic_store(&xFrameIsBroadcast, xIsBroadcast);
|
||||
}
|
||||
|
||||
// Get Modbus Master communication mode.
|
||||
eMBMode ucMBMasterGetCommMode(void)
|
||||
{
|
||||
return eMBMasterCurrentMode;
|
||||
}
|
||||
|
||||
/* Get current transaction information */
|
||||
BOOL xMBMasterGetLastTransactionInfo( uint64_t *pxTransId, UCHAR *pucDestAddress,
|
||||
UCHAR *pucFunctionCode, UCHAR *pucException,
|
||||
USHORT *pusErrorType )
|
||||
{
|
||||
BOOL xState = (eMBState == STATE_ENABLED);
|
||||
if (xState && pxTransId && pucDestAddress && pucFunctionCode
|
||||
&& pucException && pusErrorType) {
|
||||
MB_ATOMIC_SECTION {
|
||||
*pxTransId = xTransactionInfo.xTransId;
|
||||
*pucDestAddress = xTransactionInfo.ucDestAddr;
|
||||
*pucFunctionCode = xTransactionInfo.ucFuncCode;
|
||||
*pucException = xTransactionInfo.eException;
|
||||
*pusErrorType = xTransactionInfo.ucFrameError;
|
||||
}
|
||||
}
|
||||
return xState;
|
||||
}
|
||||
|
||||
#endif // MB_MASTER_RTU_ENABLED || MB_MASTER_ASCII_ENABLED || MB_MASTER_TCP_ENABLED
|
||||
@@ -0,0 +1,110 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2006 Christian Walter
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*
|
||||
* SPDX-FileContributor: 2016-2021 Espressif Systems (Shanghai) CO LTD
|
||||
*/
|
||||
/*
|
||||
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
|
||||
* Copyright (c) 2006 Christian Walter <wolti@sil.at>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* File: $Id: mbcrc.c,v 1.7 2007/02/18 23:50:27 wolti Exp $
|
||||
*/
|
||||
|
||||
/* ----------------------- Platform includes --------------------------------*/
|
||||
#include "port.h"
|
||||
#include "mbconfig.h"
|
||||
|
||||
#if (MB_MASTER_RTU_ENABLED || MB_SLAVE_RTU_ENABLED || CONFIG_MB_UTEST)
|
||||
|
||||
static const UCHAR aucCRCHi[] = {
|
||||
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
|
||||
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
|
||||
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
|
||||
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
|
||||
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
|
||||
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
|
||||
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
|
||||
0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
|
||||
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
|
||||
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
|
||||
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
|
||||
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
|
||||
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
|
||||
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
|
||||
0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
|
||||
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
|
||||
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
|
||||
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
|
||||
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
|
||||
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
|
||||
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
|
||||
0x00, 0xC1, 0x81, 0x40
|
||||
};
|
||||
|
||||
static const UCHAR aucCRCLo[] = {
|
||||
0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0xC7,
|
||||
0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E,
|
||||
0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09, 0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9,
|
||||
0x1B, 0xDB, 0xDA, 0x1A, 0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC,
|
||||
0x14, 0xD4, 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3,
|
||||
0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3, 0xF2, 0x32,
|
||||
0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4, 0x3C, 0xFC, 0xFD, 0x3D,
|
||||
0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A, 0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38,
|
||||
0x28, 0xE8, 0xE9, 0x29, 0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2E, 0x2F, 0xEF,
|
||||
0x2D, 0xED, 0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26,
|
||||
0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60, 0x61, 0xA1,
|
||||
0x63, 0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67, 0xA5, 0x65, 0x64, 0xA4,
|
||||
0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F, 0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB,
|
||||
0x69, 0xA9, 0xA8, 0x68, 0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA,
|
||||
0xBE, 0x7E, 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5,
|
||||
0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, 0x70, 0xB0,
|
||||
0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92, 0x96, 0x56, 0x57, 0x97,
|
||||
0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C, 0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E,
|
||||
0x5A, 0x9A, 0x9B, 0x5B, 0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89,
|
||||
0x4B, 0x8B, 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,
|
||||
0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83,
|
||||
0x41, 0x81, 0x80, 0x40
|
||||
};
|
||||
|
||||
USHORT
|
||||
usMBCRC16( UCHAR * pucFrame, USHORT usLen )
|
||||
{
|
||||
UCHAR ucCRCHi = 0xFF;
|
||||
UCHAR ucCRCLo = 0xFF;
|
||||
int iIndex;
|
||||
|
||||
while( usLen-- )
|
||||
{
|
||||
iIndex = ucCRCLo ^ *( pucFrame++ );
|
||||
ucCRCLo = ( UCHAR )( ucCRCHi ^ aucCRCHi[iIndex] );
|
||||
ucCRCHi = aucCRCLo[iIndex];
|
||||
}
|
||||
return ( USHORT )( ucCRCHi << 8 | ucCRCLo );
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2006 Christian Walter
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*
|
||||
* SPDX-FileContributor: 2016-2021 Espressif Systems (Shanghai) CO LTD
|
||||
*/
|
||||
/*
|
||||
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
|
||||
* Copyright (c) 2006 Christian Walter <wolti@sil.at>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* File: $Id: mbcrc.h,v 1.5 2006/12/07 22:10:34 wolti Exp $
|
||||
*/
|
||||
|
||||
#ifndef _MB_CRC_H
|
||||
#define _MB_CRC_H
|
||||
|
||||
USHORT usMBCRC16( UCHAR * pucFrame, USHORT usLen );
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,372 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2006 Christian Walter
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*
|
||||
* SPDX-FileContributor: 2016-2021 Espressif Systems (Shanghai) CO LTD
|
||||
*/
|
||||
/*
|
||||
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
|
||||
* Copyright (c) 2006 Christian Walter <wolti@sil.at>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* File: $Id: mbrtu.c,v 1.18 2007/09/12 10:15:56 wolti Exp $
|
||||
*/
|
||||
|
||||
/* ----------------------- System includes ----------------------------------*/
|
||||
#include "stdlib.h"
|
||||
#include "string.h"
|
||||
|
||||
/* ----------------------- Platform includes --------------------------------*/
|
||||
#include "port.h"
|
||||
|
||||
/* ----------------------- Modbus includes ----------------------------------*/
|
||||
#include "mb.h"
|
||||
#include "mbrtu.h"
|
||||
#include "mbframe.h"
|
||||
|
||||
#include "mbcrc.h"
|
||||
#include "mbport.h"
|
||||
|
||||
#if MB_SLAVE_RTU_ENABLED > 0
|
||||
|
||||
/* ----------------------- Type definitions ---------------------------------*/
|
||||
typedef enum
|
||||
{
|
||||
STATE_RX_INIT, /*!< Receiver is in initial state. */
|
||||
STATE_RX_IDLE, /*!< Receiver is in idle state. */
|
||||
STATE_RX_RCV, /*!< Frame is beeing received. */
|
||||
STATE_RX_ERROR /*!< If the frame is invalid. */
|
||||
} eMBRcvState;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
STATE_TX_IDLE, /*!< Transmitter is in idle state. */
|
||||
STATE_TX_XMIT /*!< Transmitter is in transfer state. */
|
||||
} eMBSndState;
|
||||
|
||||
/* ----------------------- Shared variables ---------------------------------*/
|
||||
extern volatile UCHAR ucMbSlaveBuf[];
|
||||
|
||||
/* ----------------------- Static variables ---------------------------------*/
|
||||
static volatile eMBSndState eSndState;
|
||||
static volatile eMBRcvState eRcvState;
|
||||
|
||||
static volatile UCHAR *pucSndBufferCur;
|
||||
static volatile USHORT usSndBufferCount;
|
||||
|
||||
static volatile USHORT usRcvBufferPos;
|
||||
static volatile UCHAR *ucRTUBuf = ucMbSlaveBuf;
|
||||
|
||||
/* ----------------------- Start implementation -----------------------------*/
|
||||
eMBErrorCode
|
||||
eMBRTUInit( UCHAR ucSlaveAddress, UCHAR ucPort, ULONG ulBaudRate, eMBParity eParity )
|
||||
{
|
||||
eMBErrorCode eStatus = MB_ENOERR;
|
||||
ULONG usTimerT35_50us;
|
||||
|
||||
( void )ucSlaveAddress;
|
||||
ENTER_CRITICAL_SECTION( );
|
||||
|
||||
/* Modbus RTU uses 8 Databits. */
|
||||
if( xMBPortSerialInit( ucPort, ulBaudRate, 8, eParity ) != TRUE )
|
||||
{
|
||||
eStatus = MB_EPORTERR;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* If baudrate > 19200 then we should use the fixed timer values
|
||||
* t35 = 1750us. Otherwise t35 must be 3.5 times the character time.
|
||||
*/
|
||||
if( ulBaudRate > 19200 )
|
||||
{
|
||||
usTimerT35_50us = 35; /* 1800us. */
|
||||
}
|
||||
else
|
||||
{
|
||||
/* The timer reload value for a character is given by:
|
||||
*
|
||||
* ChTimeValue = Ticks_per_1s / ( Baudrate / 11 )
|
||||
* = 11 * Ticks_per_1s / Baudrate
|
||||
* = 220000 / Baudrate
|
||||
* The reload for t3.5 is 1.5 times this value and similary
|
||||
* for t3.5.
|
||||
*/
|
||||
usTimerT35_50us = ( 7UL * 220000UL ) / ( 2UL * ulBaudRate );
|
||||
}
|
||||
if( xMBPortTimersInit( ( USHORT ) usTimerT35_50us ) != TRUE )
|
||||
{
|
||||
eStatus = MB_EPORTERR;
|
||||
}
|
||||
}
|
||||
EXIT_CRITICAL_SECTION( );
|
||||
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
void
|
||||
eMBRTUStart( void )
|
||||
{
|
||||
ENTER_CRITICAL_SECTION( );
|
||||
/* Initially the receiver is in the state STATE_RX_INIT. we start
|
||||
* the timer and if no character is received within t3.5 we change
|
||||
* to STATE_RX_IDLE. This makes sure that we delay startup of the
|
||||
* modbus protocol stack until the bus is free.
|
||||
*/
|
||||
eRcvState = STATE_RX_INIT;
|
||||
vMBPortSerialEnable( TRUE, FALSE );
|
||||
#if CONFIG_FMB_TIMER_PORT_ENABLED
|
||||
vMBPortTimersEnable( );
|
||||
#else
|
||||
pxMBPortCBTimerExpired();
|
||||
#endif
|
||||
EXIT_CRITICAL_SECTION( );
|
||||
}
|
||||
|
||||
void
|
||||
eMBRTUStop( void )
|
||||
{
|
||||
ENTER_CRITICAL_SECTION( );
|
||||
vMBPortSerialEnable( FALSE, FALSE );
|
||||
vMBPortTimersDisable( );
|
||||
EXIT_CRITICAL_SECTION( );
|
||||
}
|
||||
|
||||
eMBErrorCode
|
||||
eMBRTUReceive( UCHAR * pucRcvAddress, UCHAR ** pucFrame, USHORT * pusLength )
|
||||
{
|
||||
eMBErrorCode eStatus = MB_ENOERR;
|
||||
UCHAR *pucMBRTUFrame = ( UCHAR* ) ucRTUBuf;
|
||||
USHORT usFrameLength = usRcvBufferPos;
|
||||
|
||||
if( xMBPortSerialGetRequest( &pucMBRTUFrame, &usFrameLength ) == FALSE )
|
||||
{
|
||||
return MB_EIO;
|
||||
}
|
||||
|
||||
ENTER_CRITICAL_SECTION( );
|
||||
assert( usFrameLength < MB_SER_PDU_SIZE_MAX );
|
||||
|
||||
/* Length and CRC check */
|
||||
if( ( usFrameLength >= MB_SER_PDU_SIZE_MIN )
|
||||
&& ( usMBCRC16( ( UCHAR * ) pucMBRTUFrame, usFrameLength ) == 0 ) )
|
||||
{
|
||||
/* Save the address field. All frames are passed to the upper layed
|
||||
* and the decision if a frame is used is done there.
|
||||
*/
|
||||
*pucRcvAddress = pucMBRTUFrame[MB_SER_PDU_ADDR_OFF];
|
||||
|
||||
/* Total length of Modbus-PDU is Modbus-Serial-Line-PDU minus
|
||||
* size of address field and CRC checksum.
|
||||
*/
|
||||
*pusLength = ( USHORT )( usFrameLength - MB_SER_PDU_PDU_OFF - MB_SER_PDU_SIZE_CRC );
|
||||
|
||||
/* Return the start of the Modbus PDU to the caller. */
|
||||
*pucFrame = ( UCHAR * ) & pucMBRTUFrame[MB_SER_PDU_PDU_OFF];
|
||||
}
|
||||
else
|
||||
{
|
||||
eStatus = MB_EIO;
|
||||
}
|
||||
|
||||
EXIT_CRITICAL_SECTION( );
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
eMBErrorCode
|
||||
eMBRTUSend( UCHAR ucSlaveAddress, const UCHAR * pucFrame, USHORT usLength )
|
||||
{
|
||||
eMBErrorCode eStatus = MB_ENOERR;
|
||||
USHORT usCRC16;
|
||||
|
||||
/* Check if the receiver is still in idle state. If not we where to
|
||||
* slow with processing the received frame and the master sent another
|
||||
* frame on the network. We have to abort sending the frame.
|
||||
*/
|
||||
if( eRcvState == STATE_RX_IDLE )
|
||||
{
|
||||
ENTER_CRITICAL_SECTION( );
|
||||
/* First byte before the Modbus-PDU is the slave address. */
|
||||
pucSndBufferCur = ( UCHAR * ) pucFrame - 1;
|
||||
usSndBufferCount = 1;
|
||||
|
||||
/* Now copy the Modbus-PDU into the Modbus-Serial-Line-PDU. */
|
||||
pucSndBufferCur[MB_SER_PDU_ADDR_OFF] = ucSlaveAddress;
|
||||
usSndBufferCount += usLength;
|
||||
|
||||
/* Calculate CRC16 checksum for Modbus-Serial-Line-PDU. */
|
||||
usCRC16 = usMBCRC16( ( UCHAR * ) pucSndBufferCur, usSndBufferCount );
|
||||
ucRTUBuf[usSndBufferCount++] = ( UCHAR )( usCRC16 & 0xFF );
|
||||
ucRTUBuf[usSndBufferCount++] = ( UCHAR )( usCRC16 >> 8 );
|
||||
|
||||
/* Activate the transmitter. */
|
||||
eSndState = STATE_TX_XMIT;
|
||||
EXIT_CRITICAL_SECTION( );
|
||||
|
||||
if( xMBPortSerialSendResponse( ( UCHAR * ) pucSndBufferCur, usSndBufferCount ) == FALSE )
|
||||
{
|
||||
eStatus = MB_EIO;
|
||||
}
|
||||
|
||||
vMBPortSerialEnable( FALSE, TRUE );
|
||||
}
|
||||
else
|
||||
{
|
||||
eStatus = MB_EIO;
|
||||
}
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
BOOL
|
||||
xMBRTUReceiveFSM( void )
|
||||
{
|
||||
BOOL xStatus = FALSE;
|
||||
UCHAR ucByte;
|
||||
|
||||
assert( eSndState == STATE_TX_IDLE );
|
||||
|
||||
/* Always read the character. */
|
||||
xStatus = xMBPortSerialGetByte( ( CHAR * ) & ucByte );
|
||||
|
||||
switch ( eRcvState )
|
||||
{
|
||||
/* If we have received a character in the init state we have to
|
||||
* wait until the frame is finished.
|
||||
*/
|
||||
case STATE_RX_INIT:
|
||||
vMBPortTimersEnable( );
|
||||
break;
|
||||
|
||||
/* In the error state we wait until all characters in the
|
||||
* damaged frame are transmitted.
|
||||
*/
|
||||
case STATE_RX_ERROR:
|
||||
vMBPortTimersEnable( );
|
||||
break;
|
||||
|
||||
/* In the idle state we wait for a new character. If a character
|
||||
* is received the t1.5 and t3.5 timers are started and the
|
||||
* receiver is in the state STATE_RX_RCV.
|
||||
*/
|
||||
case STATE_RX_IDLE:
|
||||
usRcvBufferPos = 0;
|
||||
ucRTUBuf[usRcvBufferPos++] = ucByte;
|
||||
eRcvState = STATE_RX_RCV;
|
||||
|
||||
/* Enable t3.5 timers. */
|
||||
vMBPortTimersEnable( );
|
||||
break;
|
||||
|
||||
/* We are currently receiving a frame. Reset the timer after
|
||||
* every character received. If more than the maximum possible
|
||||
* number of bytes in a modbus frame is received the frame is
|
||||
* ignored.
|
||||
*/
|
||||
case STATE_RX_RCV:
|
||||
if( usRcvBufferPos < MB_SER_PDU_SIZE_MAX )
|
||||
{
|
||||
if( xStatus ) {
|
||||
ucRTUBuf[usRcvBufferPos++] = ucByte;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
eRcvState = STATE_RX_ERROR;
|
||||
}
|
||||
vMBPortTimersEnable( );
|
||||
break;
|
||||
}
|
||||
|
||||
return xStatus;
|
||||
}
|
||||
|
||||
BOOL
|
||||
xMBRTUTransmitFSM( void )
|
||||
{
|
||||
BOOL xNeedPoll = TRUE;
|
||||
|
||||
assert( eRcvState == STATE_RX_IDLE );
|
||||
|
||||
switch ( eSndState )
|
||||
{
|
||||
/* We should not get a transmitter event if the transmitter is in
|
||||
* idle state. */
|
||||
case STATE_TX_IDLE:
|
||||
break;
|
||||
|
||||
case STATE_TX_XMIT:
|
||||
/* check if we are finished. */
|
||||
if( usSndBufferCount != 0 )
|
||||
{
|
||||
xMBPortSerialPutByte( ( CHAR )*pucSndBufferCur );
|
||||
pucSndBufferCur++; /* next byte in sendbuffer. */
|
||||
usSndBufferCount--;
|
||||
}
|
||||
else
|
||||
{
|
||||
xMBPortEventPost( EV_FRAME_TRANSMIT );
|
||||
xNeedPoll = FALSE;
|
||||
eSndState = STATE_TX_IDLE;
|
||||
vMBPortTimersEnable( );
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return xNeedPoll;
|
||||
}
|
||||
|
||||
BOOL MB_PORT_ISR_ATTR
|
||||
xMBRTUTimerT35Expired( void )
|
||||
{
|
||||
BOOL xNeedPoll = FALSE;
|
||||
|
||||
switch ( eRcvState )
|
||||
{
|
||||
/* Timer t35 expired. Startup phase is finished. */
|
||||
case STATE_RX_INIT:
|
||||
xNeedPoll = xMBPortEventPost( EV_READY );
|
||||
break;
|
||||
|
||||
/* A frame was received and t35 expired. Notify the listener that
|
||||
* a new frame was received. */
|
||||
case STATE_RX_RCV:
|
||||
xNeedPoll = xMBPortEventPost( EV_FRAME_RECEIVED );
|
||||
break;
|
||||
|
||||
/* An error occured while receiving the frame. */
|
||||
case STATE_RX_ERROR:
|
||||
break;
|
||||
|
||||
/* Function called in an illegal state. */
|
||||
default:
|
||||
assert( ( eRcvState == STATE_RX_IDLE ) || ( eRcvState == STATE_RX_ERROR ) );
|
||||
}
|
||||
|
||||
vMBPortTimersDisable( );
|
||||
eRcvState = STATE_RX_IDLE;
|
||||
|
||||
return xNeedPoll;
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2006 Christian Walter
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*
|
||||
* SPDX-FileContributor: 2016-2021 Espressif Systems (Shanghai) CO LTD
|
||||
*/
|
||||
/*
|
||||
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
|
||||
* Copyright (c) 2006 Christian Walter <wolti@sil.at>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* File: $Id: mbrtu.h,v 1.9 2006/12/07 22:10:34 wolti Exp $
|
||||
*/
|
||||
#include "mbconfig.h"
|
||||
|
||||
#ifndef _MB_RTU_H
|
||||
#define _MB_RTU_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
PR_BEGIN_EXTERN_C
|
||||
#endif
|
||||
|
||||
/* ----------------------- Defines ------------------------------------------*/
|
||||
#define MB_SER_PDU_SIZE_MIN 4 /*!< Minimum size of a Modbus RTU frame. */
|
||||
|
||||
#if MB_SLAVE_RTU_ENABLED
|
||||
eMBErrorCode eMBRTUInit( UCHAR slaveAddress, UCHAR ucPort, ULONG ulBaudRate,
|
||||
eMBParity eParity );
|
||||
void eMBRTUStart( void );
|
||||
void eMBRTUStop( void );
|
||||
eMBErrorCode eMBRTUReceive( UCHAR * pucRcvAddress, UCHAR ** pucFrame, USHORT * pusLength );
|
||||
eMBErrorCode eMBRTUSend( UCHAR slaveAddress, const UCHAR * pucFrame, USHORT usLength );
|
||||
BOOL xMBRTUReceiveFSM( void );
|
||||
BOOL xMBRTUTransmitFSM( void );
|
||||
BOOL xMBRTUTimerT15Expired( void );
|
||||
BOOL xMBRTUTimerT35Expired( void );
|
||||
#endif
|
||||
|
||||
#if MB_MASTER_RTU_ENABLED
|
||||
eMBErrorCode eMBMasterRTUInit( UCHAR ucPort, ULONG ulBaudRate,eMBParity eParity );
|
||||
void eMBMasterRTUStart( void );
|
||||
void eMBMasterRTUStop( void );
|
||||
eMBErrorCode eMBMasterRTUReceive( UCHAR * pucRcvAddress, UCHAR ** pucFrame, USHORT * pusLength );
|
||||
eMBErrorCode eMBMasterRTUSend( UCHAR slaveAddress, const UCHAR * pucFrame, USHORT usLength );
|
||||
BOOL xMBMasterRTUReceiveFSM( void );
|
||||
BOOL xMBMasterRTUTransmitFSM( void );
|
||||
BOOL xMBMasterRTUTimerExpired( void );
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
PR_END_EXTERN_C
|
||||
#endif
|
||||
#endif
|
||||
@@ -0,0 +1,439 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2013 Armink
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*
|
||||
* SPDX-FileContributor: 2016-2021 Espressif Systems (Shanghai) CO LTD
|
||||
*/
|
||||
/*
|
||||
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
|
||||
* Copyright (c) 2013 China Beijing Armink <armink.ztl@gmail.com>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* File: $Id: mbrtu_m.c,v 1.60 2013/08/17 11:42:56 Armink Add Master Functions $
|
||||
*/
|
||||
|
||||
/* ----------------------- System includes ----------------------------------*/
|
||||
#include "stdlib.h"
|
||||
#include "string.h"
|
||||
#include "stdio.h"
|
||||
|
||||
/* ----------------------- Platform includes --------------------------------*/
|
||||
#include "port.h"
|
||||
|
||||
/* ----------------------- Modbus includes ----------------------------------*/
|
||||
|
||||
#include "mb_m.h"
|
||||
#include "mbrtu.h"
|
||||
#include "mbframe.h"
|
||||
|
||||
#include "mbcrc.h"
|
||||
#include "mbport.h"
|
||||
|
||||
/* ----------------------- Defines ------------------------------------------*/
|
||||
#define MB_RTU_SER_PDU_SIZE_MIN 4 /*!< Minimum size of a Modbus RTU frame. */
|
||||
|
||||
/* ----------------------- Type definitions ---------------------------------*/
|
||||
typedef enum
|
||||
{
|
||||
STATE_M_RX_INIT, /*!< Receiver is in initial state. */
|
||||
STATE_M_RX_IDLE, /*!< Receiver is in idle state. */
|
||||
STATE_M_RX_RCV, /*!< Frame is beeing received. */
|
||||
STATE_M_RX_ERROR, /*!< If the frame is invalid. */
|
||||
} eMBMasterRcvState;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
STATE_M_TX_IDLE, /*!< Transmitter is in idle state. */
|
||||
STATE_M_TX_XMIT, /*!< Transmitter is in transfer state. */
|
||||
STATE_M_TX_XFWR, /*!< Transmitter is in transfer finish and wait receive state. */
|
||||
} eMBMasterSndState;
|
||||
|
||||
#if MB_MASTER_RTU_ENABLED > 0
|
||||
/*------------------------ Shared variables ---------------------------------*/
|
||||
extern volatile UCHAR ucMasterRcvBuf[];
|
||||
extern volatile UCHAR ucMasterSndBuf[];
|
||||
|
||||
/* ----------------------- Static variables ---------------------------------*/
|
||||
static volatile eMBMasterSndState eSndState;
|
||||
static volatile eMBMasterRcvState eRcvState;
|
||||
|
||||
static volatile UCHAR *pucMasterSndBufferCur;
|
||||
static volatile USHORT usMasterSndBufferCount;
|
||||
static volatile USHORT usMasterRcvBufferPos;
|
||||
|
||||
static volatile UCHAR *ucMasterRTURcvBuf = ucMasterRcvBuf;
|
||||
static volatile UCHAR *ucMasterRTUSndBuf = ucMasterSndBuf;
|
||||
|
||||
/* ----------------------- Start implementation -----------------------------*/
|
||||
eMBErrorCode
|
||||
eMBMasterRTUInit(UCHAR ucPort, ULONG ulBaudRate, eMBParity eParity )
|
||||
{
|
||||
eMBErrorCode eStatus = MB_ENOERR;
|
||||
ULONG usTimerT35_50us;
|
||||
|
||||
ENTER_CRITICAL_SECTION( );
|
||||
|
||||
/* Modbus RTU uses 8 Databits. */
|
||||
if( xMBMasterPortSerialInit( ucPort, ulBaudRate, 8, eParity ) != TRUE )
|
||||
{
|
||||
eStatus = MB_EPORTERR;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* If baudrate > 19200 then we should use the fixed timer values
|
||||
* t35 = 1750us. Otherwise t35 must be 3.5 times the character time.
|
||||
*/
|
||||
if( ulBaudRate > 19200 )
|
||||
{
|
||||
usTimerT35_50us = 35; /* 1800us. */
|
||||
}
|
||||
else
|
||||
{
|
||||
/* The timer reload value for a character is given by:
|
||||
*
|
||||
* ChTimeValue = Ticks_per_1s / ( Baudrate / 11 )
|
||||
* = 11 * Ticks_per_1s / Baudrate
|
||||
* = 220000 / Baudrate
|
||||
* The reload for t3.5 is 1.5 times this value and similary
|
||||
* for t3.5.
|
||||
*/
|
||||
usTimerT35_50us = ( 7UL * 220000UL ) / ( 2UL * ulBaudRate );
|
||||
}
|
||||
if( xMBMasterPortTimersInit( ( USHORT ) usTimerT35_50us ) != TRUE )
|
||||
{
|
||||
eStatus = MB_EPORTERR;
|
||||
}
|
||||
}
|
||||
EXIT_CRITICAL_SECTION( );
|
||||
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
void
|
||||
eMBMasterRTUStart( void )
|
||||
{
|
||||
ENTER_CRITICAL_SECTION( );
|
||||
/* Initially the receiver is in the state STATE_M_RX_INIT. we start
|
||||
* the timer and if no character is received within t3.5 we change
|
||||
* to STATE_M_RX_IDLE. This makes sure that we delay startup of the
|
||||
* modbus protocol stack until the bus is free.
|
||||
*/
|
||||
eRcvState = STATE_M_RX_INIT;
|
||||
vMBMasterPortSerialEnable( TRUE, FALSE );
|
||||
vMBMasterPortTimersT35Enable( );
|
||||
|
||||
EXIT_CRITICAL_SECTION( );
|
||||
}
|
||||
|
||||
void
|
||||
eMBMasterRTUStop( void )
|
||||
{
|
||||
ENTER_CRITICAL_SECTION( );
|
||||
vMBMasterPortSerialEnable( FALSE, FALSE );
|
||||
vMBMasterPortTimersDisable( );
|
||||
EXIT_CRITICAL_SECTION( );
|
||||
}
|
||||
|
||||
eMBErrorCode
|
||||
eMBMasterRTUReceive( UCHAR * pucRcvAddress, UCHAR ** pucFrame, USHORT * pusLength )
|
||||
{
|
||||
eMBErrorCode eStatus = MB_ENOERR;
|
||||
UCHAR *pucMBRTUFrame = ( UCHAR* ) ucMasterRTURcvBuf;
|
||||
USHORT usFrameLength = usMasterRcvBufferPos;
|
||||
|
||||
if( xMBMasterPortSerialGetResponse( &pucMBRTUFrame, &usFrameLength ) == FALSE )
|
||||
{
|
||||
return MB_EIO;
|
||||
}
|
||||
|
||||
ENTER_CRITICAL_SECTION( );
|
||||
assert( usFrameLength < MB_SER_PDU_SIZE_MAX );
|
||||
assert( pucMBRTUFrame );
|
||||
|
||||
/* Length and CRC check */
|
||||
if( ( usFrameLength >= MB_RTU_SER_PDU_SIZE_MIN )
|
||||
&& ( usMBCRC16( ( UCHAR * ) pucMBRTUFrame, usFrameLength ) == 0 ) )
|
||||
{
|
||||
/* Save the address field. All frames are passed to the upper layer
|
||||
* and the decision if a frame is used is done there.
|
||||
*/
|
||||
*pucRcvAddress = pucMBRTUFrame[MB_SER_PDU_ADDR_OFF];
|
||||
|
||||
/* Total length of Modbus-PDU is Modbus-Serial-Line-PDU minus
|
||||
* size of address field and CRC checksum.
|
||||
*/
|
||||
*pusLength = ( USHORT )( usFrameLength - MB_SER_PDU_PDU_OFF - MB_SER_PDU_SIZE_CRC );
|
||||
|
||||
/* Return the start of the Modbus PDU to the caller. */
|
||||
*pucFrame = ( UCHAR * ) & pucMBRTUFrame[MB_SER_PDU_PDU_OFF];
|
||||
}
|
||||
else
|
||||
{
|
||||
eStatus = MB_EIO;
|
||||
}
|
||||
|
||||
EXIT_CRITICAL_SECTION( );
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
eMBErrorCode
|
||||
eMBMasterRTUSend( UCHAR ucSlaveAddress, const UCHAR * pucFrame, USHORT usLength )
|
||||
{
|
||||
eMBErrorCode eStatus = MB_ENOERR;
|
||||
USHORT usCRC16;
|
||||
|
||||
if ( ucSlaveAddress > MB_MASTER_TOTAL_SLAVE_NUM ) return MB_EINVAL;
|
||||
|
||||
/* Check if the receiver is still in idle state. If not we where to
|
||||
* slow with processing the received frame and the master sent another
|
||||
* frame on the network. We have to abort sending the frame.
|
||||
*/
|
||||
if( eRcvState == STATE_M_RX_IDLE )
|
||||
{
|
||||
ENTER_CRITICAL_SECTION( );
|
||||
/* First byte before the Modbus-PDU is the slave address. */
|
||||
pucMasterSndBufferCur = ( UCHAR * ) pucFrame - 1;
|
||||
usMasterSndBufferCount = 1;
|
||||
|
||||
/* Now copy the Modbus-PDU into the Modbus-Serial-Line-PDU. */
|
||||
pucMasterSndBufferCur[MB_SER_PDU_ADDR_OFF] = ucSlaveAddress;
|
||||
usMasterSndBufferCount += usLength;
|
||||
|
||||
/* Calculate CRC16 checksum for Modbus-Serial-Line-PDU. */
|
||||
usCRC16 = usMBCRC16( ( UCHAR * ) pucMasterSndBufferCur, usMasterSndBufferCount );
|
||||
pucMasterSndBufferCur[usMasterSndBufferCount++] = ( UCHAR )( usCRC16 & 0xFF );
|
||||
pucMasterSndBufferCur[usMasterSndBufferCount++] = ( UCHAR )( usCRC16 >> 8 );
|
||||
EXIT_CRITICAL_SECTION( );
|
||||
|
||||
/* Activate the transmitter. */
|
||||
eSndState = STATE_M_TX_XMIT;
|
||||
|
||||
if ( xMBMasterPortSerialSendRequest( ( UCHAR * ) pucMasterSndBufferCur, usMasterSndBufferCount ) == FALSE )
|
||||
{
|
||||
eStatus = MB_EIO;
|
||||
}
|
||||
|
||||
// The place to enable RS485 driver
|
||||
vMBMasterPortSerialEnable( FALSE, TRUE );
|
||||
}
|
||||
else
|
||||
{
|
||||
eStatus = MB_EIO;
|
||||
}
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
BOOL
|
||||
xMBMasterRTUReceiveFSM( void )
|
||||
{
|
||||
BOOL xStatus = FALSE;
|
||||
UCHAR ucByte;
|
||||
|
||||
if ( ( eSndState != STATE_M_TX_IDLE ) && ( eSndState != STATE_M_TX_XFWR ) ) {
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/* Always read the character. */
|
||||
xStatus = xMBMasterPortSerialGetByte( ( CHAR * ) & ucByte );
|
||||
|
||||
switch ( eRcvState )
|
||||
{
|
||||
/* If we have received a character in the init state we have to
|
||||
* wait until the frame is finished.
|
||||
*/
|
||||
case STATE_M_RX_INIT:
|
||||
vMBMasterPortTimersT35Enable( );
|
||||
ESP_LOGD("DBG", "Start initialization phase.");
|
||||
break;
|
||||
|
||||
/* In the error state we wait until all characters in the
|
||||
* damaged frame are transmitted.
|
||||
*/
|
||||
case STATE_M_RX_ERROR:
|
||||
vMBMasterPortTimersT35Enable( );
|
||||
break;
|
||||
|
||||
/* In the idle state we wait for a new character. If a character
|
||||
* is received the t1.5 and t3.5 timers are started and the
|
||||
* receiver is in the state STATE_M_RX_RCV and disable early
|
||||
* the timer of respond timeout .
|
||||
*/
|
||||
case STATE_M_RX_IDLE:
|
||||
/* In time of respond timeout,the receiver receive a frame.
|
||||
* Disable timer of respond timeout and change the transmiter state to idle.
|
||||
*/
|
||||
vMBMasterPortTimersDisable( );
|
||||
|
||||
usMasterRcvBufferPos = 0;
|
||||
if( xStatus && ucByte ) {
|
||||
ucMasterRTURcvBuf[usMasterRcvBufferPos++] = ucByte;
|
||||
eRcvState = STATE_M_RX_RCV;
|
||||
eSndState = STATE_M_TX_IDLE;
|
||||
}
|
||||
|
||||
/* Enable t3.5 timers. */
|
||||
#if CONFIG_FMB_TIMER_PORT_ENABLED
|
||||
vMBMasterPortTimersT35Enable( );
|
||||
#endif
|
||||
break;
|
||||
|
||||
/* We are currently receiving a frame. Reset the timer after
|
||||
* every character received. If more than the maximum possible
|
||||
* number of bytes in a modbus frame is received the frame is
|
||||
* ignored.
|
||||
*/
|
||||
case STATE_M_RX_RCV:
|
||||
if( usMasterRcvBufferPos < MB_SER_PDU_SIZE_MAX )
|
||||
{
|
||||
if ( xStatus ) {
|
||||
ucMasterRTURcvBuf[usMasterRcvBufferPos++] = ucByte;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
eRcvState = STATE_M_RX_ERROR;
|
||||
}
|
||||
#if CONFIG_FMB_TIMER_PORT_ENABLED
|
||||
vMBMasterPortTimersT35Enable( );
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
return xStatus;
|
||||
}
|
||||
|
||||
BOOL
|
||||
xMBMasterRTUTransmitFSM( void )
|
||||
{
|
||||
BOOL xNeedPoll = TRUE;
|
||||
BOOL xFrameIsBroadcast = FALSE;
|
||||
|
||||
if ( eRcvState != STATE_M_RX_IDLE ) {
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
switch ( eSndState )
|
||||
{
|
||||
/* We should not get a transmitter event if the transmitter is in
|
||||
* idle state. */
|
||||
case STATE_M_TX_XFWR:
|
||||
xNeedPoll = FALSE;
|
||||
break;
|
||||
|
||||
case STATE_M_TX_IDLE:
|
||||
break;
|
||||
|
||||
case STATE_M_TX_XMIT:
|
||||
/* check if we are finished. */
|
||||
if( usMasterSndBufferCount != 0 )
|
||||
{
|
||||
xMBMasterPortSerialPutByte( ( CHAR )*pucMasterSndBufferCur );
|
||||
pucMasterSndBufferCur++; /* next byte in sendbuffer. */
|
||||
usMasterSndBufferCount--;
|
||||
}
|
||||
else
|
||||
{
|
||||
xFrameIsBroadcast = ( ucMasterRTUSndBuf[MB_SEND_BUF_PDU_OFF - MB_SER_PDU_PDU_OFF]
|
||||
== MB_ADDRESS_BROADCAST ) ? TRUE : FALSE;
|
||||
vMBMasterRequestSetType( xFrameIsBroadcast );
|
||||
eSndState = STATE_M_TX_XFWR;
|
||||
/* If the frame is broadcast ,master will enable timer of convert delay,
|
||||
* else master will enable timer of respond timeout. */
|
||||
if ( xFrameIsBroadcast == TRUE )
|
||||
{
|
||||
vMBMasterPortTimersConvertDelayEnable( );
|
||||
}
|
||||
else
|
||||
{
|
||||
vMBMasterPortTimersRespondTimeoutEnable( );
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return xNeedPoll;
|
||||
}
|
||||
|
||||
BOOL MB_PORT_ISR_ATTR
|
||||
xMBMasterRTUTimerExpired(void)
|
||||
{
|
||||
BOOL xNeedPoll = FALSE;
|
||||
|
||||
switch (eRcvState)
|
||||
{
|
||||
/* Timer t35 expired. Startup phase is finished. */
|
||||
case STATE_M_RX_INIT:
|
||||
xNeedPoll = xMBMasterPortEventPost(EV_MASTER_READY);
|
||||
ESP_EARLY_LOGD("DBG", "RTU timer, init FSM state.");
|
||||
break;
|
||||
|
||||
/* A frame was received and t35 expired. Notify the listener that
|
||||
* a new frame was received. */
|
||||
case STATE_M_RX_RCV:
|
||||
xNeedPoll = xMBMasterPortEventPost(EV_MASTER_FRAME_RECEIVED);
|
||||
break;
|
||||
|
||||
/* An error occured while receiving the frame. */
|
||||
case STATE_M_RX_ERROR:
|
||||
vMBMasterSetErrorType(EV_ERROR_RECEIVE_DATA);
|
||||
xNeedPoll = xMBMasterPortEventPost(EV_MASTER_ERROR_PROCESS);
|
||||
break;
|
||||
|
||||
/* Function called in an illegal state. */
|
||||
default:
|
||||
assert(eRcvState == STATE_M_RX_IDLE);
|
||||
break;
|
||||
}
|
||||
eRcvState = STATE_M_RX_IDLE;
|
||||
|
||||
switch (eSndState)
|
||||
{
|
||||
/* A frame was send finish and convert delay or respond timeout expired.
|
||||
* If the frame is broadcast,The master will idle,and if the frame is not
|
||||
* broadcast. Notify the listener process error.*/
|
||||
case STATE_M_TX_XFWR:
|
||||
if ( xMBMasterRequestIsBroadcast( ) == FALSE ) {
|
||||
vMBMasterSetErrorType(EV_ERROR_RESPOND_TIMEOUT);
|
||||
xNeedPoll = xMBMasterPortEventPost(EV_MASTER_ERROR_PROCESS);
|
||||
}
|
||||
break;
|
||||
/* Function called in an illegal state. */
|
||||
default:
|
||||
assert( ( eSndState == STATE_M_TX_XMIT ) || ( eSndState == STATE_M_TX_IDLE ));
|
||||
break;
|
||||
}
|
||||
eSndState = STATE_M_TX_IDLE;
|
||||
|
||||
vMBMasterPortTimersDisable( );
|
||||
/* If timer mode is convert delay, the master event then turns EV_MASTER_EXECUTE status. */
|
||||
if (xMBMasterGetCurTimerMode() == MB_TMODE_CONVERT_DELAY) {
|
||||
xNeedPoll = xMBMasterPortEventPost(EV_MASTER_EXECUTE);
|
||||
}
|
||||
|
||||
return xNeedPoll;
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,167 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2006 Christian Walter
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*
|
||||
* SPDX-FileContributor: 2016-2022 Espressif Systems (Shanghai) CO LTD
|
||||
*/
|
||||
/*
|
||||
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
|
||||
* Copyright (c) 2006 Christian Walter <wolti@sil.at>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* File: $Id: mbtcp.c,v 1.3 2006/12/07 22:10:34 wolti Exp $
|
||||
*/
|
||||
|
||||
/* ----------------------- System includes ----------------------------------*/
|
||||
#include "stdlib.h"
|
||||
#include "string.h"
|
||||
|
||||
/* ----------------------- Platform includes --------------------------------*/
|
||||
#include "port.h"
|
||||
|
||||
/* ----------------------- Modbus includes ----------------------------------*/
|
||||
#include "mb.h"
|
||||
#include "mbconfig.h"
|
||||
#include "mbtcp.h"
|
||||
#include "mbframe.h"
|
||||
#include "mbport.h"
|
||||
|
||||
#if MB_TCP_ENABLED
|
||||
|
||||
/* ----------------------- Defines ------------------------------------------*/
|
||||
|
||||
/* ----------------------- MBAP Header --------------------------------------*/
|
||||
/*
|
||||
*
|
||||
* <------------------------ MODBUS TCP/IP ADU(1) ------------------------->
|
||||
* <----------- MODBUS PDU (1') ---------------->
|
||||
* +-----------+---------------+------------------------------------------+
|
||||
* | TID | PID | Length | UID |Code | Data |
|
||||
* +-----------+---------------+------------------------------------------+
|
||||
* | | | | |
|
||||
* (2) (3) (4) (5) (6)
|
||||
*
|
||||
* (2) ... MB_TCP_TID = 0 (Transaction Identifier - 2 Byte)
|
||||
* (3) ... MB_TCP_PID = 2 (Protocol Identifier - 2 Byte)
|
||||
* (4) ... MB_TCP_LEN = 4 (Number of bytes - 2 Byte)
|
||||
* (5) ... MB_TCP_UID = 6 (Unit Identifier - 1 Byte)
|
||||
* (6) ... MB_TCP_FUNC = 7 (Modbus Function Code)
|
||||
*
|
||||
* (1) ... Modbus TCP/IP Application Data Unit
|
||||
* (1') ... Modbus Protocol Data Unit
|
||||
*/
|
||||
|
||||
#define MB_TCP_PROTOCOL_ID 0 /* 0 = Modbus Protocol */
|
||||
|
||||
|
||||
/* ----------------------- Start implementation -----------------------------*/
|
||||
eMBErrorCode
|
||||
eMBTCPDoInit( USHORT ucTCPPort )
|
||||
{
|
||||
eMBErrorCode eStatus = MB_ENOERR;
|
||||
|
||||
if( xMBTCPPortInit( ucTCPPort ) == FALSE )
|
||||
{
|
||||
eStatus = MB_EPORTERR;
|
||||
}
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
void
|
||||
eMBTCPStart( void )
|
||||
{
|
||||
ESP_LOGD(MB_PORT_TAG, "TCP Slave port enable.");
|
||||
vMBTCPPortEnable( );
|
||||
}
|
||||
|
||||
void
|
||||
eMBTCPStop( void )
|
||||
{
|
||||
/* Make sure that no more clients are connected. */
|
||||
ESP_LOGD(MB_PORT_TAG, "TCP Slave port disable.");
|
||||
vMBTCPPortDisable( );
|
||||
}
|
||||
|
||||
eMBErrorCode
|
||||
eMBTCPReceive( UCHAR * pucRcvAddress, UCHAR ** ppucFrame, USHORT * pusLength )
|
||||
{
|
||||
eMBErrorCode eStatus = MB_EIO;
|
||||
UCHAR *pucMBTCPFrame;
|
||||
USHORT usLength;
|
||||
USHORT usPID;
|
||||
|
||||
if( xMBTCPPortGetRequest( &pucMBTCPFrame, &usLength ) != FALSE )
|
||||
{
|
||||
usPID = pucMBTCPFrame[MB_TCP_PID] << 8U;
|
||||
usPID |= pucMBTCPFrame[MB_TCP_PID + 1];
|
||||
|
||||
if( usPID == MB_TCP_PROTOCOL_ID )
|
||||
{
|
||||
*ppucFrame = &pucMBTCPFrame[MB_TCP_FUNC];
|
||||
*pusLength = usLength - MB_TCP_FUNC;
|
||||
eStatus = MB_ENOERR;
|
||||
|
||||
/* The regular Modbus TCP does not use any addresses. Fake the MBAP UID in this case.
|
||||
* The MBAP UID field support is used for RTU over TCP option if enabled.
|
||||
*/
|
||||
#if MB_TCP_UID_ENABLED
|
||||
*pucRcvAddress = pucMBTCPFrame[MB_TCP_UID];
|
||||
#else
|
||||
*pucRcvAddress = MB_TCP_PSEUDO_ADDRESS;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
eStatus = MB_EIO;
|
||||
}
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
eMBErrorCode
|
||||
eMBTCPSend( UCHAR _unused, const UCHAR * pucFrame, USHORT usLength )
|
||||
{
|
||||
eMBErrorCode eStatus = MB_ENOERR;
|
||||
UCHAR *pucMBTCPFrame = ( UCHAR * ) pucFrame - MB_TCP_FUNC;
|
||||
USHORT usTCPLength = usLength + MB_TCP_FUNC;
|
||||
|
||||
/* The MBAP header is already initialized because the caller calls this
|
||||
* function with the buffer returned by the previous call. Therefore we
|
||||
* only have to update the length in the header. Note that the length
|
||||
* header includes the size of the Modbus PDU and the UID Byte. Therefore
|
||||
* the length is usLength plus one.
|
||||
*/
|
||||
pucMBTCPFrame[MB_TCP_LEN] = ( usLength + 1 ) >> 8U;
|
||||
pucMBTCPFrame[MB_TCP_LEN + 1] = ( usLength + 1 ) & 0xFF;
|
||||
|
||||
if( xMBTCPPortSendResponse( pucMBTCPFrame, usTCPLength ) == FALSE )
|
||||
{
|
||||
eStatus = MB_EIO;
|
||||
}
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2006 Christian Walter
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*
|
||||
* SPDX-FileContributor: 2016-2021 Espressif Systems (Shanghai) CO LTD
|
||||
*/
|
||||
/*
|
||||
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
|
||||
* Copyright (c) 2006 Christian Walter <wolti@sil.at>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* File: $Id: mbtcp.h,v 1.2 2006/12/07 22:10:34 wolti Exp $
|
||||
*/
|
||||
|
||||
#ifndef _MB_TCP_H
|
||||
#define _MB_TCP_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
PR_BEGIN_EXTERN_C
|
||||
#endif
|
||||
|
||||
/* ----------------------- Defines ------------------------------------------*/
|
||||
|
||||
#if MB_TCP_ENABLED
|
||||
|
||||
/* ----------------------- Function prototypes ------------------------------*/
|
||||
eMBErrorCode eMBTCPDoInit( USHORT ucTCPPort );
|
||||
void eMBTCPStart( void );
|
||||
void eMBTCPStop( void );
|
||||
eMBErrorCode eMBTCPReceive( UCHAR * pucRcvAddress, UCHAR ** pucFrame,
|
||||
USHORT * pusLength );
|
||||
eMBErrorCode eMBTCPSend( UCHAR _unused, const UCHAR * pucFrame,
|
||||
USHORT usLength );
|
||||
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
PR_END_EXTERN_C
|
||||
#endif
|
||||
#endif
|
||||
@@ -0,0 +1,172 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2006 Christian Walter
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*
|
||||
* SPDX-FileContributor: 2016-2022 Espressif Systems (Shanghai) CO LTD
|
||||
*/
|
||||
/*
|
||||
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
|
||||
* Copyright (c) 2006 Christian Walter <wolti@sil.at>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* File: $Id: mbtcp.c,v 1.3 2006/12/07 22:10:34 wolti Exp $
|
||||
*/
|
||||
|
||||
/* ----------------------- System includes ----------------------------------*/
|
||||
#include "stdlib.h"
|
||||
#include "string.h"
|
||||
|
||||
/* ----------------------- Platform includes --------------------------------*/
|
||||
#include "port.h"
|
||||
|
||||
/* ----------------------- Modbus includes ----------------------------------*/
|
||||
#include "mb.h"
|
||||
#include "mbconfig.h"
|
||||
#include "mbtcp_m.h"
|
||||
#include "mbframe.h"
|
||||
#include "mbport.h"
|
||||
|
||||
#if MB_MASTER_TCP_ENABLED
|
||||
|
||||
/* ----------------------- Defines ------------------------------------------*/
|
||||
|
||||
/* ----------------------- MBAP Header --------------------------------------*/
|
||||
/*
|
||||
*
|
||||
* <------------------------ MODBUS TCP/IP ADU(1) ------------------------->
|
||||
* <----------- MODBUS PDU (1') ---------------->
|
||||
* +-----------+---------------+------------------------------------------+
|
||||
* | TID | PID | Length | UID |Code | Data |
|
||||
* +-----------+---------------+------------------------------------------+
|
||||
* | | | | |
|
||||
* (2) (3) (4) (5) (6)
|
||||
*
|
||||
* (2) ... MB_TCP_TID = 0 (Transaction Identifier - 2 Byte)
|
||||
* (3) ... MB_TCP_PID = 2 (Protocol Identifier - 2 Byte)
|
||||
* (4) ... MB_TCP_LEN = 4 (Number of bytes - 2 Byte)
|
||||
* (5) ... MB_TCP_UID = 6 (Unit Identifier - 1 Byte)
|
||||
* (6) ... MB_TCP_FUNC = 7 (Modbus Function Code)
|
||||
*
|
||||
* (1) ... Modbus TCP/IP Application Data Unit
|
||||
* (1') ... Modbus Protocol Data Unit
|
||||
*/
|
||||
|
||||
#define MB_TCP_PROTOCOL_ID 0 /* 0 = Modbus Protocol */
|
||||
|
||||
|
||||
/* ----------------------- Start implementation -----------------------------*/
|
||||
eMBErrorCode
|
||||
eMBMasterTCPDoInit( USHORT ucTCPPort )
|
||||
{
|
||||
eMBErrorCode eStatus = MB_ENOERR;
|
||||
|
||||
if( xMBMasterTCPPortInit( ucTCPPort ) == FALSE )
|
||||
{
|
||||
eStatus = MB_EPORTERR;
|
||||
}
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
void
|
||||
eMBMasterTCPStart( void )
|
||||
{
|
||||
ESP_LOGD(MB_PORT_TAG, "TCP Master port enable.");
|
||||
vMBMasterTCPPortEnable( );
|
||||
}
|
||||
|
||||
void
|
||||
eMBMasterTCPStop( void )
|
||||
{
|
||||
ESP_LOGD(MB_PORT_TAG, "TCP Master port disable.");
|
||||
vMBMasterTCPPortDisable( );
|
||||
}
|
||||
|
||||
eMBErrorCode
|
||||
eMBMasterTCPReceive( UCHAR * pucRcvAddress, UCHAR ** ppucFrame, USHORT * pusLength )
|
||||
{
|
||||
eMBErrorCode eStatus = MB_EIO;
|
||||
UCHAR *pucMBTCPFrame;
|
||||
USHORT usLength;
|
||||
USHORT usPID;
|
||||
|
||||
if( xMBMasterTCPPortGetRequest( &pucMBTCPFrame, &usLength ) != FALSE )
|
||||
{
|
||||
usPID = pucMBTCPFrame[MB_TCP_PID] << 8U;
|
||||
usPID |= pucMBTCPFrame[MB_TCP_PID + 1];
|
||||
|
||||
if( usPID == MB_TCP_PROTOCOL_ID )
|
||||
{
|
||||
*ppucFrame = &pucMBTCPFrame[MB_TCP_FUNC];
|
||||
*pusLength = usLength - MB_TCP_FUNC;
|
||||
eStatus = MB_ENOERR;
|
||||
|
||||
/* Get MBAP UID field if its support is enabled.
|
||||
* Otherwise just ignore this field.
|
||||
*/
|
||||
#if MB_TCP_UID_ENABLED
|
||||
*pucRcvAddress = pucMBTCPFrame[MB_TCP_UID];
|
||||
#else
|
||||
*pucRcvAddress = MB_TCP_PSEUDO_ADDRESS;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
eStatus = MB_EIO;
|
||||
}
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
eMBErrorCode
|
||||
eMBMasterTCPSend( UCHAR ucAddress, const UCHAR * pucFrame, USHORT usLength )
|
||||
{
|
||||
eMBErrorCode eStatus = MB_ENOERR;
|
||||
UCHAR *pucMBTCPFrame = ( UCHAR * ) pucFrame - MB_TCP_FUNC;
|
||||
USHORT usTCPLength = usLength + MB_TCP_FUNC;
|
||||
|
||||
/* Note that the length in the MBAP header includes the size of the Modbus PDU
|
||||
* and the UID Byte. Therefore the length is usLength plus one.
|
||||
*/
|
||||
pucMBTCPFrame[MB_TCP_LEN] = ( usLength + 1 ) >> 8U;
|
||||
pucMBTCPFrame[MB_TCP_LEN + 1] = ( usLength + 1 ) & 0xFF;
|
||||
|
||||
/* Set UID field in the MBAP if it is supported.
|
||||
* If the RTU over TCP is not supported, the UID = 0 or 0xFF.
|
||||
*/
|
||||
#if MB_TCP_UID_ENABLED
|
||||
pucMBTCPFrame[MB_TCP_UID] = ucAddress;
|
||||
#else
|
||||
pucMBTCPFrame[MB_TCP_UID] = 0x00;
|
||||
#endif
|
||||
|
||||
if( xMBMasterTCPPortSendResponse( pucMBTCPFrame, usTCPLength ) == FALSE )
|
||||
{
|
||||
eStatus = MB_EIO;
|
||||
}
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2016-2021 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
/*
|
||||
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
|
||||
* Copyright (c) 2006 Christian Walter <wolti@sil.at>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* File: $Id: mbtcp.h,v 1.2 2006/12/07 22:10:34 wolti Exp $
|
||||
*/
|
||||
|
||||
#ifndef _MB_TCP_M_H
|
||||
#define _MB_TCP_M_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
PR_BEGIN_EXTERN_C
|
||||
#endif
|
||||
|
||||
/* ----------------------- Defines ------------------------------------------*/
|
||||
|
||||
#if MB_MASTER_TCP_ENABLED
|
||||
|
||||
/* ----------------------- Function prototypes ------------------------------*/
|
||||
eMBErrorCode eMBMasterTCPDoInit( USHORT ucTCPPort );
|
||||
void eMBMasterTCPStart( void );
|
||||
void eMBMasterTCPStop( void );
|
||||
eMBErrorCode eMBMasterTCPReceive( UCHAR * pucRcvAddress, UCHAR ** pucFrame,
|
||||
USHORT * pusLength );
|
||||
eMBErrorCode eMBMasterTCPSend( UCHAR ucAddress, const UCHAR * pucFrame,
|
||||
USHORT usLength );
|
||||
BOOL xMBMasterTCPTimerExpired(void);
|
||||
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
PR_END_EXTERN_C
|
||||
#endif
|
||||
#endif
|
||||
213
managed_components/espressif__esp-modbus/freemodbus/port/port.c
Normal file
213
managed_components/espressif__esp-modbus/freemodbus/port/port.c
Normal file
@@ -0,0 +1,213 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2013 Armink
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*
|
||||
* SPDX-FileContributor: 2016-2021 Espressif Systems (Shanghai) CO LTD
|
||||
*/
|
||||
/*
|
||||
* FreeModbus Libary: ESP32 Port
|
||||
* Copyright (C) 2013 Armink <armink.ztl@gmail.com>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* IF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* File: $Id: port.c,v 1.60 2015/02/01 9:18:05 Armink $
|
||||
*/
|
||||
|
||||
/* ----------------------- System includes --------------------------------*/
|
||||
|
||||
/* ----------------------- Modbus includes ----------------------------------*/
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/queue.h"
|
||||
#include "port.h"
|
||||
|
||||
/* ----------------------- Variables ----------------------------------------*/
|
||||
static _lock_t s_port_lock;
|
||||
static UCHAR ucPortMode = 0;
|
||||
|
||||
/* ----------------------- Start implementation -----------------------------*/
|
||||
INLINE int lock_obj(_lock_t *plock)
|
||||
{
|
||||
_lock_acquire(plock);
|
||||
return 1;
|
||||
}
|
||||
|
||||
INLINE void unlock_obj(_lock_t *plock)
|
||||
{
|
||||
_lock_release(plock);
|
||||
}
|
||||
|
||||
INLINE void
|
||||
vMBPortEnterCritical(void)
|
||||
{
|
||||
_lock_acquire(&s_port_lock);
|
||||
}
|
||||
|
||||
INLINE void
|
||||
vMBPortExitCritical(void)
|
||||
{
|
||||
_lock_release(&s_port_lock);
|
||||
}
|
||||
|
||||
UCHAR
|
||||
ucMBPortGetMode( void )
|
||||
{
|
||||
return ucPortMode;
|
||||
}
|
||||
|
||||
void
|
||||
vMBPortSetMode( UCHAR ucMode )
|
||||
{
|
||||
ENTER_CRITICAL_SECTION();
|
||||
ucPortMode = ucMode;
|
||||
EXIT_CRITICAL_SECTION();
|
||||
}
|
||||
|
||||
#if MB_MASTER_RTU_ENABLED || MB_MASTER_ASCII_ENABLED || MB_SLAVE_RTU_ENABLED || MB_SLAVE_ASCII_ENABLED
|
||||
|
||||
BOOL xMBPortSerialWaitEvent(QueueHandle_t xMbUartQueue, uart_event_t* pxEvent, ULONG xTimeout)
|
||||
{
|
||||
BOOL xResult = (BaseType_t)xQueueReceive(xMbUartQueue, (void*)pxEvent, (TickType_t) xTimeout);
|
||||
ESP_LOGD(MB_PORT_TAG, "%s, UART event: %u ", __func__, (unsigned)pxEvent->type);
|
||||
return xResult;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#if MB_MASTER_RTU_ENABLED || MB_MASTER_ASCII_ENABLED
|
||||
|
||||
/*
|
||||
* The function is called from ASCII/RTU module to get processed data buffer. Sets the
|
||||
* received buffer and its length using parameters.
|
||||
*/
|
||||
__attribute__ ((weak))
|
||||
BOOL xMBMasterPortSerialGetResponse( UCHAR **ppucMBSerialFrame, USHORT * usSerialLength )
|
||||
{
|
||||
ESP_LOGD(MB_PORT_TAG, " %s default", __func__);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/*
|
||||
* The function is called from ASCII/RTU module to set processed data buffer
|
||||
* to be sent in transmitter state machine.
|
||||
*/
|
||||
__attribute__ ((weak))
|
||||
BOOL xMBMasterPortSerialSendRequest( UCHAR *pucMBSerialFrame, USHORT usSerialLength )
|
||||
{
|
||||
ESP_LOGD(MB_PORT_TAG, "%s default", __func__);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#if MB_SLAVE_RTU_ENABLED || MB_SLAVE_ASCII_ENABLED
|
||||
|
||||
__attribute__ ((weak))
|
||||
BOOL xMBPortSerialGetRequest( UCHAR **ppucMBSerialFrame, USHORT * usSerialLength )
|
||||
{
|
||||
ESP_LOGD(MB_PORT_TAG, "%s default", __func__);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
__attribute__ ((weak))
|
||||
BOOL xMBPortSerialSendResponse( UCHAR *pucMBSerialFrame, USHORT usSerialLength )
|
||||
{
|
||||
ESP_LOGD(MB_PORT_TAG, "%s default", __func__);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#if MB_TCP_DEBUG
|
||||
|
||||
// This function is kept to realize legacy freemodbus frame logging functionality
|
||||
void
|
||||
prvvMBTCPLogFrame( const CHAR * pucMsg, UCHAR * pucFrame, USHORT usFrameLen )
|
||||
{
|
||||
int i;
|
||||
int res = 0;
|
||||
int iBufPos = 0;
|
||||
size_t iBufLeft = MB_TCP_FRAME_LOG_BUFSIZE;
|
||||
static CHAR arcBuffer[MB_TCP_FRAME_LOG_BUFSIZE];
|
||||
|
||||
assert( pucFrame != NULL );
|
||||
|
||||
for ( i = 0; i < usFrameLen; i++ ) {
|
||||
// Print some additional frame information.
|
||||
switch ( i )
|
||||
{
|
||||
case 0:
|
||||
// TID = Transaction Identifier.
|
||||
res = snprintf( &arcBuffer[iBufPos], iBufLeft, "| TID = " );
|
||||
break;
|
||||
case 2:
|
||||
// PID = Protocol Identifier.
|
||||
res = snprintf( &arcBuffer[iBufPos], iBufLeft, " | PID = " );
|
||||
break;
|
||||
case 4:
|
||||
// Length
|
||||
res = snprintf( &arcBuffer[iBufPos], iBufLeft, " | LEN = " );
|
||||
break;
|
||||
case 6:
|
||||
// UID = Unit Identifier.
|
||||
res = snprintf( &arcBuffer[iBufPos], iBufLeft, " | UID = " );
|
||||
break;
|
||||
case 7:
|
||||
// MB Function Code.
|
||||
res = snprintf( &arcBuffer[iBufPos], iBufLeft, " | FUNC = " );
|
||||
break;
|
||||
case 8:
|
||||
// MB PDU rest.
|
||||
res = snprintf( &arcBuffer[iBufPos], iBufLeft, " | DATA = " );
|
||||
break;
|
||||
default:
|
||||
res = 0;
|
||||
break;
|
||||
}
|
||||
if( res == -1 ) {
|
||||
break;
|
||||
}
|
||||
else {
|
||||
iBufPos += res;
|
||||
iBufLeft -= res;
|
||||
}
|
||||
|
||||
// Print the data.
|
||||
res = snprintf( &arcBuffer[iBufPos], iBufLeft, "%02X", pucFrame[i] );
|
||||
if( res == -1 ) {
|
||||
break;
|
||||
} else {
|
||||
iBufPos += res;
|
||||
iBufLeft -= res;
|
||||
}
|
||||
}
|
||||
|
||||
if( res != -1 ) {
|
||||
// Append an end of frame string.
|
||||
res = snprintf( &arcBuffer[iBufPos], iBufLeft, " |" );
|
||||
if( res != -1 ) {
|
||||
ESP_LOGD(pucMsg, "%s", arcBuffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
254
managed_components/espressif__esp-modbus/freemodbus/port/port.h
Normal file
254
managed_components/espressif__esp-modbus/freemodbus/port/port.h
Normal file
@@ -0,0 +1,254 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2010 Christian Walter
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*
|
||||
* SPDX-FileContributor: 2016-2021 Espressif Systems (Shanghai) CO LTD
|
||||
*/
|
||||
/*
|
||||
* FreeModbus Libary: ESP32 Port
|
||||
* Copyright (C) 2010 Christian Walter <cwalter@embedded-solutions.at>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* IF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* File: $Id: port.h,v 1.1 2010/06/06 13:07:20 wolti Exp $
|
||||
*/
|
||||
|
||||
#ifndef PORT_COMMON_H_
|
||||
#define PORT_COMMON_H_
|
||||
|
||||
#include "sys/lock.h"
|
||||
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/queue.h" // for queue
|
||||
|
||||
#include "esp_log.h" // for ESP_LOGE macro
|
||||
#include "esp_timer.h"
|
||||
#include "driver/uart.h" // for uart_event_t
|
||||
|
||||
#if __has_include("driver/gptimer.h")
|
||||
#include "driver/gptimer.h"
|
||||
#else
|
||||
#include "driver/timer.h"
|
||||
#endif
|
||||
|
||||
#include "mbconfig.h"
|
||||
|
||||
#define INLINE inline __attribute__((always_inline))
|
||||
#define PR_BEGIN_EXTERN_C extern "C" {
|
||||
#define PR_END_EXTERN_C }
|
||||
|
||||
#define MB_PORT_TAG "MB_PORT_COMMON"
|
||||
|
||||
#define MB_BAUD_RATE_DEFAULT (115200)
|
||||
#define MB_QUEUE_LENGTH (CONFIG_FMB_QUEUE_LENGTH)
|
||||
|
||||
#define MB_SERIAL_TASK_PRIO (CONFIG_FMB_PORT_TASK_PRIO)
|
||||
#define MB_SERIAL_TASK_STACK_SIZE (CONFIG_FMB_PORT_TASK_STACK_SIZE)
|
||||
#define MB_SERIAL_TOUT (3) // 3.5*8 = 28 ticks, TOUT=3 -> ~24..33 ticks
|
||||
|
||||
// Set buffer size for transmission
|
||||
#define MB_SERIAL_BUF_SIZE (CONFIG_FMB_SERIAL_BUF_SIZE)
|
||||
|
||||
// common definitions for serial port implementations
|
||||
#define MB_SERIAL_TX_TOUT_MS (2200) // maximum time for transmission of longest allowed frame buffer
|
||||
#define MB_SERIAL_TX_TOUT_TICKS (pdMS_TO_TICKS(MB_SERIAL_TX_TOUT_MS)) // timeout for transmission
|
||||
#define MB_SERIAL_RX_TOUT_MS (1)
|
||||
#define MB_SERIAL_RX_TOUT_TICKS (pdMS_TO_TICKS(MB_SERIAL_RX_TOUT_MS)) // timeout for receive
|
||||
|
||||
#define MB_SERIAL_RESP_LEN_MIN (4)
|
||||
|
||||
// Common definitions for TCP port
|
||||
#define MB_TCP_BUF_SIZE (256 + 7) // Must hold a complete Modbus TCP frame.
|
||||
#define MB_TCP_DEFAULT_PORT (CONFIG_FMB_TCP_PORT_DEFAULT)
|
||||
#define MB_TCP_STACK_SIZE (CONFIG_FMB_PORT_TASK_STACK_SIZE)
|
||||
#define MB_TCP_TASK_PRIO (CONFIG_FMB_PORT_TASK_PRIO)
|
||||
|
||||
// The task affinity for Modbus stack tasks
|
||||
#define MB_PORT_TASK_AFFINITY (CONFIG_FMB_PORT_TASK_AFFINITY)
|
||||
|
||||
#define MB_TCP_READ_TIMEOUT_MS (100) // read timeout in mS
|
||||
#define MB_TCP_READ_TIMEOUT (pdMS_TO_TICKS(MB_TCP_READ_TIMEOUT_MS))
|
||||
#define MB_TCP_SEND_TIMEOUT_MS (500) // send event timeout in mS
|
||||
#define MB_TCP_SEND_TIMEOUT (pdMS_TO_TICKS(MB_TCP_SEND_TIMEOUT_MS))
|
||||
#define MB_TCP_PORT_MAX_CONN (CONFIG_FMB_TCP_PORT_MAX_CONN)
|
||||
|
||||
// Set the API unlock time to maximum response time
|
||||
// The actual release time will be dependent on the timer time
|
||||
#define MB_MAX_RESPONSE_TIME_MS (5000)
|
||||
|
||||
#define MB_TCP_FRAME_LOG_BUFSIZE (256)
|
||||
|
||||
#define MB_PORT_HAS_CLOSE (1) // Define to explicitly close port on destroy
|
||||
|
||||
// Define number of timer reloads per 1 mS
|
||||
#define MB_TIMER_TICS_PER_MS (20UL)
|
||||
#define MB_TIMER_TICK_TIME_US (1000 / MB_TIMER_TICS_PER_MS) // 50uS = one discreet for timer
|
||||
|
||||
#define MB_TCP_DEBUG (LOG_LOCAL_LEVEL >= ESP_LOG_DEBUG) // Enable legacy debug output in TCP module.
|
||||
|
||||
#define MB_ATTR_WEAK __attribute__ ((weak))
|
||||
|
||||
#define MB_TCP_GET_FIELD(buffer, field) ((USHORT)((buffer[field] << 8U) | buffer[field + 1]))
|
||||
|
||||
#define MB_PORT_CHECK(a, ret_val, str, ...) \
|
||||
if (!(a)) { \
|
||||
ESP_LOGE(MB_PORT_TAG, "%s(%u): " str, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
|
||||
return ret_val; \
|
||||
}
|
||||
|
||||
// Macro to check if stack shutdown event is active
|
||||
#define TCP_PORT_CHECK_SHDN(sema_ptr, callback_func) do { \
|
||||
if (sema_ptr) { \
|
||||
ESP_LOGD(TAG, "Shutdown stack from %s(%d)", __func__, __LINE__); \
|
||||
callback_func(); \
|
||||
} \
|
||||
} while(0)
|
||||
|
||||
int lock_obj(_lock_t *plock);
|
||||
void unlock_obj(_lock_t *plock);
|
||||
|
||||
#define CRITICAL_SECTION_INIT(lock) \
|
||||
do \
|
||||
{ \
|
||||
_lock_init((_lock_t *)&lock); \
|
||||
} while (0)
|
||||
|
||||
#define CRITICAL_SECTION_CLOSE(lock) \
|
||||
do \
|
||||
{ \
|
||||
_lock_close((_lock_t *)&lock); \
|
||||
} while (0)
|
||||
|
||||
#define CRITICAL_SECTION_LOCK(lock) \
|
||||
do \
|
||||
{ \
|
||||
lock_obj((_lock_t *)&lock); \
|
||||
} while (0)
|
||||
|
||||
#define CRITICAL_SECTION_UNLOCK(lock) \
|
||||
do \
|
||||
{ \
|
||||
unlock_obj((_lock_t *)&lock); \
|
||||
} while (0)
|
||||
|
||||
#define CRITICAL_SECTION(lock) for (int st = lock_obj((_lock_t *)&lock); (st > 0); unlock_obj((_lock_t *)&lock), st = -1)
|
||||
|
||||
#ifdef __cplusplus
|
||||
PR_BEGIN_EXTERN_C
|
||||
#endif /* __cplusplus */
|
||||
|
||||
#ifndef TRUE
|
||||
#define TRUE 1
|
||||
#endif
|
||||
|
||||
#ifndef FALSE
|
||||
#define FALSE 0
|
||||
#endif
|
||||
|
||||
typedef char BOOL;
|
||||
|
||||
typedef unsigned char UCHAR;
|
||||
typedef char CHAR;
|
||||
|
||||
typedef unsigned short USHORT;
|
||||
typedef short SHORT;
|
||||
|
||||
typedef unsigned long ULONG;
|
||||
typedef long LONG;
|
||||
|
||||
#if MB_TCP_DEBUG
|
||||
typedef enum
|
||||
{
|
||||
MB_LOG_DEBUG,
|
||||
MB_LOG_INFO,
|
||||
MB_LOG_WARN,
|
||||
MB_LOG_ERROR
|
||||
} eMBPortLogLevel;
|
||||
#endif
|
||||
|
||||
typedef enum
|
||||
{
|
||||
MB_PROTO_TCP,
|
||||
MB_PROTO_UDP,
|
||||
} eMBPortProto;
|
||||
|
||||
typedef enum {
|
||||
MB_PORT_IPV4 = 0, /*!< TCP IPV4 addressing */
|
||||
MB_PORT_IPV6 = 1 /*!< TCP IPV6 addressing */
|
||||
} eMBPortIpVer;
|
||||
|
||||
typedef struct {
|
||||
esp_timer_handle_t xTimerIntHandle;
|
||||
USHORT usT35Ticks;
|
||||
BOOL xTimerState;
|
||||
} xTimerContext_t;
|
||||
|
||||
void vMBPortEnterCritical(void);
|
||||
void vMBPortExitCritical(void);
|
||||
|
||||
#define ENTER_CRITICAL_SECTION( ) { ESP_EARLY_LOGD(MB_PORT_TAG,"%s: Port enter critical.", __func__); \
|
||||
vMBPortEnterCritical(); }
|
||||
|
||||
#define EXIT_CRITICAL_SECTION( ) { vMBPortExitCritical(); \
|
||||
ESP_EARLY_LOGD(MB_PORT_TAG,"%s: Port exit critical", __func__); }
|
||||
|
||||
#define MB_PORT_CHECK_EVENT( event, mask ) ( event & mask )
|
||||
#define MB_PORT_CLEAR_EVENT( event, mask ) do { event &= ~mask; } while(0)
|
||||
|
||||
#define MB_PORT_PARITY_GET(parity) ((parity != UART_PARITY_DISABLE) ? \
|
||||
((parity == UART_PARITY_ODD) ? MB_PAR_ODD : MB_PAR_EVEN) : MB_PAR_NONE)
|
||||
|
||||
// Legacy Modbus logging function
|
||||
#if MB_TCP_DEBUG
|
||||
void vMBPortLog( eMBPortLogLevel eLevel, const CHAR * szModule,
|
||||
const CHAR * szFmt, ... );
|
||||
void prvvMBTCPLogFrame( const CHAR * pucMsg, UCHAR * pucFrame, USHORT usFrameLen );
|
||||
#endif
|
||||
|
||||
void vMBPortSetMode( UCHAR ucMode );
|
||||
UCHAR ucMBPortGetMode( void );
|
||||
|
||||
BOOL xMBPortSerialWaitEvent(QueueHandle_t xMbUartQueue, uart_event_t* pxEvent, ULONG xTimeout);
|
||||
|
||||
/**
|
||||
* This is modbus master user error handling funcion.
|
||||
* If it is defined in the user application, then helps to handle the errors
|
||||
* and received/sent buffers to transfer as well as handle the slave exception codes.
|
||||
*
|
||||
* @param xTransId - the identification of the trasaction
|
||||
* @param ucDestAddress destination salve address
|
||||
* @param usError - the error code, see the enumeration eMBMasterErrorEventType
|
||||
* @param pucRecvData current receive data pointer
|
||||
* @param ucRecvLength current length of receive buffer
|
||||
* @param pucSendData Send buffer data
|
||||
* @param ucSendLength Send buffer length
|
||||
*/
|
||||
void vMBMasterErrorCBUserHandler( uint64_t xTransId, USHORT usError, UCHAR ucDestAddress, const UCHAR* pucRecvData, USHORT ucRecvLength,
|
||||
const UCHAR* pucSendData, USHORT ucSendLength ) MB_ATTR_WEAK;
|
||||
|
||||
#ifdef __cplusplus
|
||||
PR_END_EXTERN_C
|
||||
#endif /* __cplusplus */
|
||||
|
||||
#endif /* PORT_COMMON_H_ */
|
||||
@@ -0,0 +1,121 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2010 Christian Walter
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*
|
||||
* SPDX-FileContributor: 2016-2022 Espressif Systems (Shanghai) CO LTD
|
||||
*/
|
||||
/*
|
||||
* FreeModbus Libary: ESP32 Port Demo Application
|
||||
* Copyright (C) 2010 Christian Walter <cwalter@embedded-solutions.at>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
*
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* IF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* File: $Id: portevent.c,v 1.1 2010/06/06 13:07:20 wolti Exp $
|
||||
*/
|
||||
|
||||
/* ----------------------- System includes ----------------------------------*/
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/task.h>
|
||||
#include <freertos/queue.h>
|
||||
|
||||
/* ----------------------- Modbus includes ----------------------------------*/
|
||||
#include "mb.h"
|
||||
#include "mbport.h"
|
||||
#include "port.h"
|
||||
#include "mbconfig.h"
|
||||
#include "port_serial_slave.h"
|
||||
/* ----------------------- Variables ----------------------------------------*/
|
||||
static QueueHandle_t xQueueHdl;
|
||||
|
||||
/* ----------------------- Start implementation -----------------------------*/
|
||||
BOOL
|
||||
xMBPortEventInit( void )
|
||||
{
|
||||
BOOL bStatus = FALSE;
|
||||
if((xQueueHdl = xQueueCreate(MB_EVENT_QUEUE_SIZE, sizeof(eMBEventType))) != NULL)
|
||||
{
|
||||
vQueueAddToRegistry(xQueueHdl, "MbPortEventQueue");
|
||||
bStatus = TRUE;
|
||||
}
|
||||
return bStatus;
|
||||
}
|
||||
|
||||
void
|
||||
vMBPortEventClose( void )
|
||||
{
|
||||
if(xQueueHdl != NULL)
|
||||
{
|
||||
vQueueDelete(xQueueHdl);
|
||||
xQueueHdl = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
BOOL MB_PORT_ISR_ATTR
|
||||
xMBPortEventPost( eMBEventType eEvent )
|
||||
{
|
||||
BaseType_t xStatus, xHigherPriorityTaskWoken = pdFALSE;
|
||||
assert(xQueueHdl != NULL);
|
||||
|
||||
if( (BOOL)xPortInIsrContext() == TRUE )
|
||||
{
|
||||
xStatus = xQueueSendFromISR(xQueueHdl, (const void*)&eEvent, &xHigherPriorityTaskWoken);
|
||||
if ( xHigherPriorityTaskWoken )
|
||||
{
|
||||
portYIELD_FROM_ISR();
|
||||
}
|
||||
if (xStatus != pdTRUE) {
|
||||
ESP_EARLY_LOGV(MB_PORT_TAG, "%s: Post message failure = %u.", __func__, (unsigned)xStatus);
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
xStatus = xQueueSend(xQueueHdl, (const void*)&eEvent, MB_EVENT_QUEUE_TIMEOUT);
|
||||
MB_PORT_CHECK((xStatus == pdTRUE), FALSE, "%s: Post message failure.", __func__);
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
BOOL
|
||||
xMBPortEventGet(eMBEventType * peEvent)
|
||||
{
|
||||
assert(xQueueHdl != NULL);
|
||||
BOOL xEventHappened = FALSE;
|
||||
|
||||
if (xQueueReceive(xQueueHdl, peEvent, portMAX_DELAY) == pdTRUE) {
|
||||
xEventHappened = TRUE;
|
||||
}
|
||||
return xEventHappened;
|
||||
}
|
||||
|
||||
QueueHandle_t
|
||||
xMBPortEventGetHandle(void)
|
||||
{
|
||||
if(xQueueHdl != NULL)
|
||||
{
|
||||
return xQueueHdl;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
@@ -0,0 +1,358 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2013 Armink
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*
|
||||
* SPDX-FileContributor: 2016-2021 Espressif Systems (Shanghai) CO LTD
|
||||
*/
|
||||
/*
|
||||
* FreeModbus Libary: ESP32 Port
|
||||
* Copyright (C) 2013 Armink <armink.ztl@gmail.com>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* IF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* File: $Id: portevent.c v 1.60 2013/08/13 15:07:05 Armink add Master Functions$
|
||||
*/
|
||||
|
||||
/* ----------------------- Modbus includes ----------------------------------*/
|
||||
#include <stdatomic.h>
|
||||
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/event_groups.h"
|
||||
#include "freertos/semphr.h"
|
||||
|
||||
#include "mb_m.h"
|
||||
#include "mbport.h"
|
||||
#include "mbconfig.h"
|
||||
|
||||
#include "port.h"
|
||||
#include "mbport.h"
|
||||
#include "freertos/semphr.h"
|
||||
|
||||
#if MB_MASTER_RTU_ENABLED || MB_MASTER_ASCII_ENABLED || MB_MASTER_TCP_ENABLED
|
||||
/* ----------------------- Defines ------------------------------------------*/
|
||||
|
||||
// Event bit mask for eMBMasterWaitRequestFinish()
|
||||
#define MB_EVENT_REQ_MASK (EventBits_t)( EV_MASTER_PROCESS_SUCCESS | \
|
||||
EV_MASTER_ERROR_RESPOND_TIMEOUT | \
|
||||
EV_MASTER_ERROR_RECEIVE_DATA | \
|
||||
EV_MASTER_ERROR_EXECUTE_FUNCTION )
|
||||
|
||||
/* ----------------------- Variables ----------------------------------------*/
|
||||
static SemaphoreHandle_t xResourceMasterHdl;
|
||||
static EventGroupHandle_t xEventGroupMasterHdl;
|
||||
static EventGroupHandle_t xEventGroupMasterConfirmHdl;
|
||||
static QueueHandle_t xQueueMasterHdl;
|
||||
|
||||
static _Atomic uint64_t xTransactionID = 0;
|
||||
|
||||
/* ----------------------- Start implementation -----------------------------*/
|
||||
|
||||
BOOL
|
||||
xMBMasterPortEventInit( void )
|
||||
{
|
||||
xEventGroupMasterHdl = xEventGroupCreate();
|
||||
xEventGroupMasterConfirmHdl = xEventGroupCreate();
|
||||
MB_PORT_CHECK((xEventGroupMasterHdl != NULL) && (xEventGroupMasterConfirmHdl != NULL),
|
||||
FALSE, "mb stack event group creation error.");
|
||||
xQueueMasterHdl = xQueueCreate(MB_EVENT_QUEUE_SIZE, sizeof(xMBMasterEventType));
|
||||
MB_PORT_CHECK(xQueueMasterHdl, FALSE, "mb stack event group creation error.");
|
||||
vQueueAddToRegistry(xQueueMasterHdl, "MbMasterPortEventQueue");
|
||||
atomic_init(&xTransactionID, 0);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
BOOL MB_PORT_ISR_ATTR
|
||||
xMBMasterPortEventPost( eMBMasterEventEnum eEvent)
|
||||
{
|
||||
BaseType_t xStatus, xHigherPriorityTaskWoken = pdFALSE;
|
||||
assert(xQueueMasterHdl != NULL);
|
||||
xMBMasterEventType xEvent;
|
||||
xEvent.xPostTimestamp = esp_timer_get_time();
|
||||
|
||||
if (eEvent & EV_MASTER_TRANS_START) {
|
||||
atomic_store(&(xTransactionID), xEvent.xPostTimestamp);
|
||||
}
|
||||
xEvent.eEvent = (eEvent & ~EV_MASTER_TRANS_START);
|
||||
|
||||
if( (BOOL)xPortInIsrContext() == TRUE ) {
|
||||
xStatus = xQueueSendFromISR(xQueueMasterHdl, (const void*)&xEvent, &xHigherPriorityTaskWoken);
|
||||
if ( xHigherPriorityTaskWoken ) {
|
||||
portYIELD_FROM_ISR();
|
||||
}
|
||||
if (xStatus != pdTRUE) {
|
||||
ESP_EARLY_LOGV(MB_PORT_TAG, "%s: Post message failure = %d.", __func__, xStatus);
|
||||
return FALSE;
|
||||
}
|
||||
} else {
|
||||
xStatus = xQueueSend(xQueueMasterHdl, (const void*)&xEvent, MB_EVENT_QUEUE_TIMEOUT);
|
||||
MB_PORT_CHECK((xStatus == pdTRUE), FALSE, "%s: Post message failure.", __func__);
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
BOOL
|
||||
xMBMasterPortEventGet(xMBMasterEventType *peEvent)
|
||||
{
|
||||
assert(xQueueMasterHdl != NULL);
|
||||
BOOL xEventHappened = FALSE;
|
||||
|
||||
if (xQueueReceive(xQueueMasterHdl, peEvent, portMAX_DELAY) == pdTRUE) {
|
||||
peEvent->xTransactionId = atomic_load(&xTransactionID);
|
||||
// Set event bits in confirmation group (for synchronization with port task)
|
||||
xEventGroupSetBits(xEventGroupMasterConfirmHdl, peEvent->eEvent);
|
||||
peEvent->xGetTimestamp = esp_timer_get_time();
|
||||
xEventHappened = TRUE;
|
||||
}
|
||||
return xEventHappened;
|
||||
}
|
||||
|
||||
eMBMasterEventEnum
|
||||
xMBMasterPortFsmWaitConfirmation( eMBMasterEventEnum eEventMask, ULONG ulTimeout)
|
||||
{
|
||||
EventBits_t uxBits;
|
||||
uxBits = xEventGroupWaitBits( xEventGroupMasterConfirmHdl, // The event group being tested.
|
||||
eEventMask, // The bits within the event group to wait for.
|
||||
pdFALSE, // Keep masked bits.
|
||||
pdFALSE, // Don't wait for both bits, either bit will do.
|
||||
ulTimeout); // Wait timeout for either bit to be set.
|
||||
if (ulTimeout && uxBits) {
|
||||
// Clear confirmation events that where set in the mask
|
||||
xEventGroupClearBits( xEventGroupMasterConfirmHdl, (uxBits & eEventMask) );
|
||||
}
|
||||
return (eMBMasterEventEnum)(uxBits & eEventMask);
|
||||
}
|
||||
|
||||
uint64_t xMBMasterPortGetTransactionId( )
|
||||
{
|
||||
return atomic_load(&xTransactionID);
|
||||
}
|
||||
|
||||
// This function is initialize the OS resource for modbus master.
|
||||
void vMBMasterOsResInit( void )
|
||||
{
|
||||
xResourceMasterHdl = xSemaphoreCreateBinary();
|
||||
MB_PORT_CHECK((xResourceMasterHdl != NULL), ; , "%s: Resource create error.", __func__);
|
||||
}
|
||||
|
||||
/**
|
||||
* This function is take Mobus Master running resource.
|
||||
* Note:The resource is define by Operating System.
|
||||
*
|
||||
* @param lTimeOut the waiting time.
|
||||
*
|
||||
* @return resource take result
|
||||
*/
|
||||
BOOL xMBMasterRunResTake( LONG lTimeOut )
|
||||
{
|
||||
BaseType_t xStatus = pdTRUE;
|
||||
xStatus = xSemaphoreTake( xResourceMasterHdl, lTimeOut );
|
||||
MB_PORT_CHECK((xStatus == pdTRUE), FALSE , "%s: Resource take failure.", __func__);
|
||||
ESP_LOGD(MB_PORT_TAG,"%s:Take MB resource (%lu ticks).", __func__, lTimeOut);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function is release Modbus Master running resource.
|
||||
* Note:The resource is define by Operating System. If you not use OS this function can be empty.
|
||||
*/
|
||||
void vMBMasterRunResRelease( void )
|
||||
{
|
||||
BaseType_t xStatus = pdFALSE;
|
||||
xStatus = xSemaphoreGive( xResourceMasterHdl );
|
||||
if (xStatus != pdTRUE) {
|
||||
ESP_LOGD(MB_PORT_TAG,"%s: Release resource fail.", __func__);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This is modbus master respond timeout error process callback function.
|
||||
* @note There functions will block modbus master poll while execute OS waiting.
|
||||
*
|
||||
* @param xTransId - the identification of the trasaction
|
||||
* @param ucDestAddress destination salve address
|
||||
* @param pucRecvData current receive data pointer
|
||||
* @param ucRecvLength current length of receive buffer
|
||||
* @param pucSendData Send buffer data
|
||||
* @param ucSendLength Send buffer length
|
||||
*
|
||||
*/
|
||||
void vMBMasterErrorCBRespondTimeout(uint64_t xTransId, UCHAR ucDestAddress, const UCHAR* pucSendData, USHORT ucSendLength)
|
||||
{
|
||||
(void)xEventGroupSetBits( xEventGroupMasterHdl, EV_MASTER_ERROR_RESPOND_TIMEOUT );
|
||||
ESP_LOGD(MB_PORT_TAG,"%s:Callback respond timeout.", __func__);
|
||||
if (vMBMasterErrorCBUserHandler) {
|
||||
vMBMasterErrorCBUserHandler( xTransId,
|
||||
(USHORT)EV_ERROR_RESPOND_TIMEOUT, ucDestAddress,
|
||||
NULL, 0,
|
||||
pucSendData, ucSendLength );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This is modbus master receive data error process callback function.
|
||||
* @note There functions will block modbus master poll while execute OS waiting.
|
||||
*
|
||||
* @param xTransId - the identification of the trasaction
|
||||
* @param ucDestAddress destination salve address
|
||||
* @param pucRecvData current receive data pointer
|
||||
* @param ucRecvLength current length of receive buffer
|
||||
* @param pucSendData Send buffer data
|
||||
* @param ucSendLength Send buffer length
|
||||
*/
|
||||
void vMBMasterErrorCBReceiveData(uint64_t xTransId, UCHAR ucDestAddress,
|
||||
const UCHAR* pucRecvData, USHORT ucRecvLength,
|
||||
const UCHAR* pucSendData, USHORT ucSendLength)
|
||||
{
|
||||
(void)xEventGroupSetBits( xEventGroupMasterHdl, EV_MASTER_ERROR_RECEIVE_DATA );
|
||||
ESP_LOGD(MB_PORT_TAG,"%s:Callback receive data failure.", __func__);
|
||||
if (vMBMasterErrorCBUserHandler) {
|
||||
vMBMasterErrorCBUserHandler( xTransId,
|
||||
(USHORT)EV_ERROR_RECEIVE_DATA, ucDestAddress,
|
||||
pucRecvData, ucRecvLength,
|
||||
pucSendData, ucSendLength );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This is modbus master execute function error process callback function.
|
||||
* @note There functions will block modbus master poll while execute OS waiting.
|
||||
* So,for real-time of system.Do not execute too much waiting process.
|
||||
*
|
||||
* @param xTransId - the identification of the trasaction
|
||||
* @param ucDestAddress destination salve address
|
||||
* @param pucRecvData current receive data pointer
|
||||
* @param ucRecvLength current length of receive buffer
|
||||
* @param pucSendData Send buffer data
|
||||
* @param ucSendLength Send buffer length
|
||||
*
|
||||
*/
|
||||
void vMBMasterErrorCBExecuteFunction(uint64_t xTransId, UCHAR ucDestAddress,
|
||||
const UCHAR* pucRecvData, USHORT ucRecvLength,
|
||||
const UCHAR* pucSendData, USHORT ucSendLength)
|
||||
{
|
||||
xEventGroupSetBits( xEventGroupMasterHdl, EV_MASTER_ERROR_EXECUTE_FUNCTION );
|
||||
ESP_LOGD(MB_PORT_TAG,"%s:Callback execute data handler failure.", __func__);
|
||||
if (vMBMasterErrorCBUserHandler) {
|
||||
vMBMasterErrorCBUserHandler( xTransId,
|
||||
(USHORT)EV_ERROR_EXECUTE_FUNCTION, ucDestAddress,
|
||||
pucRecvData, ucRecvLength,
|
||||
pucSendData, ucSendLength );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This is modbus master request process success callback function.
|
||||
* @note There functions will block modbus master poll while execute OS waiting.
|
||||
* So,for real-time of system. Do not execute too much waiting process.
|
||||
*
|
||||
* @param xTransId - the identification of the trasaction
|
||||
* @param ucDestAddress destination salve address
|
||||
* @param pucRecvData current receive data pointer
|
||||
* @param ucRecvLength current length of receive buffer
|
||||
* @param pucSendData Send buffer data
|
||||
* @param ucSendLength Send buffer length
|
||||
*/
|
||||
void vMBMasterCBRequestSuccess(uint64_t xTransId, UCHAR ucDestAddress,
|
||||
const UCHAR* pucRecvData, USHORT ucRecvLength,
|
||||
const UCHAR* pucSendData, USHORT ucSendLength)
|
||||
{
|
||||
(void)xEventGroupSetBits( xEventGroupMasterHdl, EV_MASTER_PROCESS_SUCCESS );
|
||||
ESP_LOGD(MB_PORT_TAG,"%s: Callback request success.", __func__);
|
||||
if (vMBMasterErrorCBUserHandler) {
|
||||
vMBMasterErrorCBUserHandler( xTransId,
|
||||
(USHORT)EV_ERROR_OK, ucDestAddress,
|
||||
pucRecvData, ucRecvLength,
|
||||
pucSendData, ucSendLength );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This function is wait for modbus master request finish and return result.
|
||||
* Waiting result include request process success, request respond timeout,
|
||||
* receive data error and execute function error.You can use the above callback function.
|
||||
* @note If you are use OS, you can use OS's event mechanism. Otherwise you have to run
|
||||
* much user custom delay for waiting.
|
||||
*
|
||||
* @return request error code
|
||||
*/
|
||||
eMBMasterReqErrCode eMBMasterWaitRequestFinish( void ) {
|
||||
eMBMasterReqErrCode eErrStatus = MB_MRE_NO_ERR;
|
||||
eMBMasterEventEnum xRecvedEvent;
|
||||
|
||||
EventBits_t uxBits = xEventGroupWaitBits( xEventGroupMasterHdl, // The event group being tested.
|
||||
MB_EVENT_REQ_MASK, // The bits within the event group to wait for.
|
||||
pdTRUE, // Masked bits should be cleared before returning.
|
||||
pdFALSE, // Don't wait for both bits, either bit will do.
|
||||
portMAX_DELAY ); // Wait forever for either bit to be set.
|
||||
xRecvedEvent = (eMBMasterEventEnum)(uxBits);
|
||||
if (xRecvedEvent) {
|
||||
ESP_LOGD(MB_PORT_TAG,"%s: returned event = 0x%x", __func__, (int)xRecvedEvent);
|
||||
if (!(xRecvedEvent & MB_EVENT_REQ_MASK)) {
|
||||
// if we wait for certain event bits but get from poll subset
|
||||
ESP_LOGE(MB_PORT_TAG,"%s: incorrect event set = 0x%x", __func__, (int)xRecvedEvent);
|
||||
}
|
||||
xEventGroupSetBits( xEventGroupMasterConfirmHdl, (xRecvedEvent & MB_EVENT_REQ_MASK) );
|
||||
if (MB_PORT_CHECK_EVENT(xRecvedEvent, EV_MASTER_PROCESS_SUCCESS)) {
|
||||
eErrStatus = MB_MRE_NO_ERR;
|
||||
} else if (MB_PORT_CHECK_EVENT(xRecvedEvent, EV_MASTER_ERROR_RESPOND_TIMEOUT)) {
|
||||
eErrStatus = MB_MRE_TIMEDOUT;
|
||||
} else if (MB_PORT_CHECK_EVENT(xRecvedEvent, EV_MASTER_ERROR_RECEIVE_DATA)) {
|
||||
eErrStatus = MB_MRE_REV_DATA;
|
||||
} else if (MB_PORT_CHECK_EVENT(xRecvedEvent, EV_MASTER_ERROR_EXECUTE_FUNCTION)) {
|
||||
eErrStatus = MB_MRE_EXE_FUN;
|
||||
}
|
||||
} else {
|
||||
ESP_LOGE(MB_PORT_TAG,"%s: Incorrect event or timeout xRecvedEvent = 0x%x", __func__, (int)uxBits);
|
||||
// https://github.com/espressif/esp-idf/issues/5275
|
||||
// if a no event is received, that means vMBMasterPortEventClose()
|
||||
// has been closed, so event group has been deleted by FreeRTOS, which
|
||||
// triggers the send of 0 value to the event group to unlock this task
|
||||
// waiting on it. For this patch, handles it as a time out without assert.
|
||||
eErrStatus = MB_MRE_TIMEDOUT;
|
||||
}
|
||||
return eErrStatus;
|
||||
}
|
||||
|
||||
void vMBMasterPortEventClose(void)
|
||||
{
|
||||
if (xEventGroupMasterHdl) {
|
||||
vEventGroupDelete(xEventGroupMasterHdl);
|
||||
xEventGroupMasterHdl = NULL;
|
||||
}
|
||||
if (xQueueMasterHdl) {
|
||||
vQueueDelete(xQueueMasterHdl);
|
||||
xQueueMasterHdl = NULL;
|
||||
}
|
||||
if (xEventGroupMasterConfirmHdl) {
|
||||
vEventGroupDelete(xEventGroupMasterConfirmHdl);
|
||||
xEventGroupMasterConfirmHdl = NULL;
|
||||
}
|
||||
if (xResourceMasterHdl) {
|
||||
vSemaphoreDelete(xResourceMasterHdl);
|
||||
xResourceMasterHdl = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2010 Christian Walter
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*
|
||||
* SPDX-FileContributor: 2016-2021 Espressif Systems (Shanghai) CO LTD
|
||||
*/
|
||||
/*
|
||||
* FreeModbus Libary: ESP32 Demo Application
|
||||
* Copyright (C) 2010 Christian Walter <cwalter@embedded-solutions.at>
|
||||
*
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* IF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* File: $Id: portother.c,v 1.1 2010/06/06 13:07:20 wolti Exp $
|
||||
*/
|
||||
|
||||
/* ----------------------- System includes ----------------------------------*/
|
||||
#include <stdlib.h>
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/task.h>
|
||||
|
||||
/* ----------------------- Modbus includes ----------------------------------*/
|
||||
#include "mb.h"
|
||||
#include "mbport.h"
|
||||
|
||||
/* ----------------------- Modbus includes ----------------------------------*/
|
||||
|
||||
/* ----------------------- Variables ----------------------------------------*/
|
||||
|
||||
/* ----------------------- Start implementation -----------------------------*/
|
||||
|
||||
BOOL
|
||||
bMBPortIsWithinException( void )
|
||||
{
|
||||
BOOL bIsWithinException = xPortInIsrContext();
|
||||
return bIsWithinException;
|
||||
}
|
||||
|
||||
void
|
||||
vMBPortClose( void )
|
||||
{
|
||||
extern void vMBPortSerialClose( void );
|
||||
extern void vMBPortTimerClose( void );
|
||||
extern void vMBPortEventClose( void );
|
||||
vMBPortSerialClose( );
|
||||
vMBPortTimerClose( );
|
||||
vMBPortEventClose( );
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2006 Christian Walter
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*
|
||||
* SPDX-FileContributor: 2016-2021 Espressif Systems (Shanghai) CO LTD
|
||||
*/
|
||||
/*
|
||||
* FreeModbus Libary: ESP32 Demo Application
|
||||
* Copyright (c) 2006 Christian Walter <wolti@sil.at>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* File: $Id: portother.c,v 1.1 2010/06/06 13:07:20 wolti Exp $
|
||||
*/
|
||||
|
||||
/* ----------------------- System includes ----------------------------------*/
|
||||
#include <stdlib.h>
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/task.h>
|
||||
#include <freertos/semphr.h>
|
||||
|
||||
/* ----------------------- Modbus includes ----------------------------------*/
|
||||
#include "mb_m.h"
|
||||
#include "mbport.h"
|
||||
|
||||
/* ----------------------- Modbus includes ----------------------------------*/
|
||||
|
||||
/* ----------------------- Variables ----------------------------------------*/
|
||||
|
||||
/* ----------------------- Start implementation -----------------------------*/
|
||||
|
||||
void
|
||||
vMBMasterPortClose( void )
|
||||
{
|
||||
extern void vMBMasterPortSerialClose( void );
|
||||
extern void vMBMasterPortTimerClose( void );
|
||||
extern void vMBMasterPortEventClose( void );
|
||||
vMBMasterPortSerialClose( );
|
||||
vMBMasterPortTimerClose( );
|
||||
vMBMasterPortEventClose( );
|
||||
}
|
||||
@@ -0,0 +1,288 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2010 Christian Walter
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*
|
||||
* SPDX-FileContributor: 2016-2021 Espressif Systems (Shanghai) CO LTD
|
||||
*/
|
||||
/*
|
||||
* FreeModbus Libary: ESP32 Port
|
||||
* Copyright (C) 2010 Christian Walter <cwalter@embedded-solutions.at>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* IF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* File: $Id: portother.c,v 1.1 2010/06/06 13:07:20 wolti Exp $
|
||||
*/
|
||||
|
||||
#include "driver/uart.h"
|
||||
#include "port.h"
|
||||
#include "driver/uart.h"
|
||||
#include "freertos/queue.h" // for queue support
|
||||
#include "soc/uart_periph.h"
|
||||
#include "driver/gpio.h"
|
||||
#include "esp_log.h" // for esp_log
|
||||
#include "esp_err.h" // for ESP_ERROR_CHECK macro
|
||||
|
||||
/* ----------------------- Modbus includes ----------------------------------*/
|
||||
#include "mb.h"
|
||||
#include "mbport.h"
|
||||
#include "sdkconfig.h" // for KConfig options
|
||||
#include "port_serial_slave.h"
|
||||
|
||||
// Note: This code uses mixed coding standard from legacy IDF code and used freemodbus stack
|
||||
|
||||
// A queue to handle UART event.
|
||||
static QueueHandle_t xMbUartQueue;
|
||||
static TaskHandle_t xMbTaskHandle;
|
||||
static const CHAR *TAG = "MB_SERIAL";
|
||||
|
||||
// The UART hardware port number
|
||||
static UCHAR ucUartNumber = UART_NUM_MAX - 1;
|
||||
|
||||
static BOOL bRxStateEnabled = FALSE; // Receiver enabled flag
|
||||
static BOOL bTxStateEnabled = FALSE; // Transmitter enabled flag
|
||||
|
||||
void vMBPortSerialEnable(BOOL bRxEnable, BOOL bTxEnable)
|
||||
{
|
||||
// This function can be called from xMBRTUTransmitFSM() of different task
|
||||
if (bTxEnable) {
|
||||
bTxStateEnabled = TRUE;
|
||||
} else {
|
||||
bTxStateEnabled = FALSE;
|
||||
}
|
||||
if (bRxEnable) {
|
||||
//uart_enable_rx_intr(ucUartNumber);
|
||||
bRxStateEnabled = TRUE;
|
||||
vTaskResume(xMbTaskHandle); // Resume receiver task
|
||||
} else {
|
||||
vTaskSuspend(xMbTaskHandle); // Block receiver task
|
||||
bRxStateEnabled = FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
static USHORT usMBPortSerialRxPoll(size_t xEventSize)
|
||||
{
|
||||
BOOL xReadStatus = TRUE;
|
||||
USHORT usCnt = 0;
|
||||
|
||||
if (bRxStateEnabled) {
|
||||
// Get received packet into Rx buffer
|
||||
while(xReadStatus && (usCnt++ <= xEventSize)) {
|
||||
// Call the Modbus stack callback function and let it fill the buffers.
|
||||
xReadStatus = pxMBFrameCBByteReceived(); // callback to execute receive FSM
|
||||
}
|
||||
uart_flush_input(ucUartNumber);
|
||||
// Send event EV_FRAME_RECEIVED to allow stack process packet
|
||||
#if !CONFIG_FMB_TIMER_PORT_ENABLED
|
||||
pxMBPortCBTimerExpired();
|
||||
#endif
|
||||
ESP_LOGD(TAG, "RX: %u bytes\n", (unsigned)usCnt);
|
||||
}
|
||||
return usCnt;
|
||||
}
|
||||
|
||||
BOOL xMBPortSerialTxPoll(void)
|
||||
{
|
||||
USHORT usCount = 0;
|
||||
BOOL bNeedPoll = TRUE;
|
||||
|
||||
if( bTxStateEnabled ) {
|
||||
// Continue while all response bytes put in buffer or out of buffer
|
||||
while((bNeedPoll) && (usCount++ < MB_SERIAL_BUF_SIZE)) {
|
||||
// Calls the modbus stack callback function to let it fill the UART transmit buffer.
|
||||
bNeedPoll = pxMBFrameCBTransmitterEmpty( ); // callback to transmit FSM
|
||||
}
|
||||
ESP_LOGD(TAG, "MB_TX_buffer send: (%u) bytes\n", (unsigned)usCount);
|
||||
// Waits while UART sending the packet
|
||||
esp_err_t xTxStatus = uart_wait_tx_done(ucUartNumber, MB_SERIAL_TX_TOUT_TICKS);
|
||||
vMBPortSerialEnable(TRUE, FALSE);
|
||||
MB_PORT_CHECK((xTxStatus == ESP_OK), FALSE, "mb serial sent buffer failure.");
|
||||
return TRUE;
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
static void vUartTask(void *pvParameters)
|
||||
{
|
||||
uart_event_t xEvent;
|
||||
USHORT usResult = 0;
|
||||
for(;;) {
|
||||
if (xMBPortSerialWaitEvent(xMbUartQueue, (void*)&xEvent, portMAX_DELAY)) {
|
||||
ESP_LOGD(TAG, "MB_uart[%u] event:", (unsigned)ucUartNumber);
|
||||
switch(xEvent.type) {
|
||||
//Event of UART receving data
|
||||
case UART_DATA:
|
||||
ESP_LOGD(TAG,"Data event, length: %u", (unsigned)xEvent.size);
|
||||
// This flag set in the event means that no more
|
||||
// data received during configured timeout and UART TOUT feature is triggered
|
||||
if (xEvent.timeout_flag) {
|
||||
// Get buffered data length
|
||||
ESP_ERROR_CHECK(uart_get_buffered_data_len(ucUartNumber, &xEvent.size));
|
||||
// Read received data and send it to modbus stack
|
||||
usResult = usMBPortSerialRxPoll(xEvent.size);
|
||||
ESP_LOGD(TAG,"Timeout occured, processed: %u bytes", (unsigned)usResult);
|
||||
}
|
||||
break;
|
||||
//Event of HW FIFO overflow detected
|
||||
case UART_FIFO_OVF:
|
||||
ESP_LOGD(TAG, "hw fifo overflow");
|
||||
xQueueReset(xMbUartQueue);
|
||||
break;
|
||||
//Event of UART ring buffer full
|
||||
case UART_BUFFER_FULL:
|
||||
ESP_LOGD(TAG, "ring buffer full");
|
||||
xQueueReset(xMbUartQueue);
|
||||
uart_flush_input(ucUartNumber);
|
||||
break;
|
||||
//Event of UART RX break detected
|
||||
case UART_BREAK:
|
||||
ESP_LOGD(TAG, "uart rx break");
|
||||
break;
|
||||
//Event of UART parity check error
|
||||
case UART_PARITY_ERR:
|
||||
ESP_LOGD(TAG, "uart parity error");
|
||||
break;
|
||||
//Event of UART frame error
|
||||
case UART_FRAME_ERR:
|
||||
ESP_LOGD(TAG, "uart frame error");
|
||||
break;
|
||||
default:
|
||||
ESP_LOGD(TAG, "uart event type: %u", (unsigned)xEvent.type);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
BOOL xMBPortSerialInit(UCHAR ucPORT, ULONG ulBaudRate,
|
||||
UCHAR ucDataBits, eMBParity eParity)
|
||||
{
|
||||
esp_err_t xErr = ESP_OK;
|
||||
// Set communication port number
|
||||
ucUartNumber = ucPORT;
|
||||
// Configure serial communication parameters
|
||||
UCHAR ucParity = UART_PARITY_DISABLE;
|
||||
UCHAR ucData = UART_DATA_8_BITS;
|
||||
switch(eParity){
|
||||
case MB_PAR_NONE:
|
||||
ucParity = UART_PARITY_DISABLE;
|
||||
break;
|
||||
case MB_PAR_ODD:
|
||||
ucParity = UART_PARITY_ODD;
|
||||
break;
|
||||
case MB_PAR_EVEN:
|
||||
ucParity = UART_PARITY_EVEN;
|
||||
break;
|
||||
default:
|
||||
ESP_LOGE(TAG, "Incorrect parity option: %u", (unsigned)eParity);
|
||||
return FALSE;
|
||||
}
|
||||
switch(ucDataBits){
|
||||
case 5:
|
||||
ucData = UART_DATA_5_BITS;
|
||||
break;
|
||||
case 6:
|
||||
ucData = UART_DATA_6_BITS;
|
||||
break;
|
||||
case 7:
|
||||
ucData = UART_DATA_7_BITS;
|
||||
break;
|
||||
case 8:
|
||||
ucData = UART_DATA_8_BITS;
|
||||
break;
|
||||
default:
|
||||
ucData = UART_DATA_8_BITS;
|
||||
break;
|
||||
}
|
||||
uart_config_t xUartConfig = {
|
||||
.baud_rate = ulBaudRate,
|
||||
.data_bits = ucData,
|
||||
.parity = ucParity,
|
||||
.stop_bits = UART_STOP_BITS_1,
|
||||
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
|
||||
.rx_flow_ctrl_thresh = 2,
|
||||
#if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0))
|
||||
.source_clk = UART_SCLK_DEFAULT,
|
||||
#else
|
||||
.source_clk = UART_SCLK_APB,
|
||||
#endif
|
||||
};
|
||||
// Set UART config
|
||||
xErr = uart_param_config(ucUartNumber, &xUartConfig);
|
||||
MB_PORT_CHECK((xErr == ESP_OK),
|
||||
FALSE, "mb config failure, uart_param_config() returned (0x%x).", (int)xErr);
|
||||
// Install UART driver, and get the queue.
|
||||
xErr = uart_driver_install(ucUartNumber, MB_SERIAL_BUF_SIZE, MB_SERIAL_BUF_SIZE,
|
||||
MB_QUEUE_LENGTH, &xMbUartQueue, MB_PORT_SERIAL_ISR_FLAG);
|
||||
MB_PORT_CHECK((xErr == ESP_OK), FALSE,
|
||||
"mb serial driver failure, uart_driver_install() returned (0x%x).", (int)xErr);
|
||||
#if !CONFIG_FMB_TIMER_PORT_ENABLED
|
||||
// Set timeout for TOUT interrupt (T3.5 modbus time)
|
||||
xErr = uart_set_rx_timeout(ucUartNumber, MB_SERIAL_TOUT);
|
||||
MB_PORT_CHECK((xErr == ESP_OK), FALSE,
|
||||
"mb serial set rx timeout failure, uart_set_rx_timeout() returned (0x%x).", (int)xErr);
|
||||
#endif
|
||||
|
||||
// Set always timeout flag to trigger timeout interrupt even after rx fifo full
|
||||
uart_set_always_rx_timeout(ucUartNumber, true);
|
||||
|
||||
// Create a task to handle UART events
|
||||
BaseType_t xStatus = xTaskCreatePinnedToCore(vUartTask, "uart_queue_task",
|
||||
MB_SERIAL_TASK_STACK_SIZE,
|
||||
NULL, MB_SERIAL_TASK_PRIO,
|
||||
&xMbTaskHandle, MB_PORT_TASK_AFFINITY);
|
||||
if (xStatus != pdPASS) {
|
||||
vTaskDelete(xMbTaskHandle);
|
||||
// Force exit from function with failure
|
||||
MB_PORT_CHECK(FALSE, FALSE,
|
||||
"mb stack serial task creation error. xTaskCreate() returned (0x%x).",
|
||||
(int)xStatus);
|
||||
} else {
|
||||
vTaskSuspend(xMbTaskHandle); // Suspend serial task while stack is not started
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
void vMBPortSerialClose(void)
|
||||
{
|
||||
(void)vTaskSuspend(xMbTaskHandle);
|
||||
(void)vTaskDelete(xMbTaskHandle);
|
||||
ESP_ERROR_CHECK(uart_driver_delete(ucUartNumber));
|
||||
}
|
||||
|
||||
BOOL xMBPortSerialPutByte(CHAR ucByte)
|
||||
{
|
||||
// Send one byte to UART transmission buffer
|
||||
// This function is called by Modbus stack
|
||||
UCHAR ucLength = uart_write_bytes(ucUartNumber, &ucByte, 1);
|
||||
return (ucLength == 1);
|
||||
}
|
||||
|
||||
// Get one byte from intermediate RX buffer
|
||||
BOOL xMBPortSerialGetByte(CHAR* pucByte)
|
||||
{
|
||||
assert(pucByte != NULL);
|
||||
USHORT usLength = uart_read_bytes(ucUartNumber, (uint8_t*)pucByte, 1, MB_SERIAL_RX_TOUT_TICKS);
|
||||
return (usLength == 1);
|
||||
}
|
||||
@@ -0,0 +1,371 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2013 Armink
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*
|
||||
* SPDX-FileContributor: 2016-2021 Espressif Systems (Shanghai) CO LTD
|
||||
*/
|
||||
/*
|
||||
* FreeModbus Libary: ESP32 Port
|
||||
* Copyright (C) 2013 Armink <armink.ztl@gmail.com>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* IF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* File: $Id: portserial.c,v 1.60 2013/08/13 15:07:05 Armink add Master Functions $
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include "driver/uart.h"
|
||||
#include "soc/dport_access.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/queue.h"
|
||||
#include "esp_log.h"
|
||||
#include "sdkconfig.h"
|
||||
|
||||
/* ----------------------- Modbus includes ----------------------------------*/
|
||||
#include "port.h"
|
||||
#include "mbport.h"
|
||||
#include "mb_m.h"
|
||||
#include "mbrtu.h"
|
||||
#include "mbconfig.h"
|
||||
#include "port_serial_master.h"
|
||||
|
||||
/* ----------------------- Defines ------------------------------------------*/
|
||||
#define MB_SERIAL_RX_SEMA_TOUT_MS (1000)
|
||||
#define MB_SERIAL_RX_SEMA_TOUT (pdMS_TO_TICKS(MB_SERIAL_RX_SEMA_TOUT_MS))
|
||||
#define MB_SERIAL_RX_FLUSH_RETRY (2)
|
||||
|
||||
/* ----------------------- Static variables ---------------------------------*/
|
||||
static const CHAR *TAG = "MB_MASTER_SERIAL";
|
||||
|
||||
// A queue to handle UART event.
|
||||
static QueueHandle_t xMbUartQueue;
|
||||
static TaskHandle_t xMbTaskHandle;
|
||||
|
||||
// The UART hardware port number
|
||||
static UCHAR ucUartNumber = UART_NUM_MAX - 1;
|
||||
|
||||
static BOOL bRxStateEnabled = FALSE; // Receiver enabled flag
|
||||
static BOOL bTxStateEnabled = FALSE; // Transmitter enabled flag
|
||||
|
||||
static SemaphoreHandle_t xMasterSemaRxHandle; // Rx blocking semaphore handle
|
||||
|
||||
static BOOL xMBMasterPortRxSemaInit( void )
|
||||
{
|
||||
xMasterSemaRxHandle = xSemaphoreCreateBinary();
|
||||
MB_PORT_CHECK((xMasterSemaRxHandle != NULL), FALSE , "%s: RX semaphore create failure.", __func__);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static void vMBMasterPortRxSemaClose( void )
|
||||
{
|
||||
if (xMasterSemaRxHandle) {
|
||||
vSemaphoreDelete(xMasterSemaRxHandle);
|
||||
xMasterSemaRxHandle = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
static BOOL xMBMasterPortRxSemaTake( LONG lTimeOut )
|
||||
{
|
||||
BaseType_t xStatus = pdTRUE;
|
||||
xStatus = xSemaphoreTake(xMasterSemaRxHandle, lTimeOut );
|
||||
MB_PORT_CHECK((xStatus == pdTRUE), FALSE , "%s: RX semaphore take failure.", __func__);
|
||||
ESP_LOGV(MB_PORT_TAG,"%s:Take RX semaphore (%" PRIu64 " ticks).", __func__, (uint64_t)lTimeOut);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static void vMBMasterRxSemaRelease( void )
|
||||
{
|
||||
BaseType_t xStatus = pdFALSE;
|
||||
xStatus = xSemaphoreGive(xMasterSemaRxHandle);
|
||||
if (xStatus != pdTRUE) {
|
||||
ESP_LOGD(MB_PORT_TAG,"%s:RX semaphore is free.", __func__);
|
||||
}
|
||||
}
|
||||
|
||||
static BOOL vMBMasterRxSemaIsBusy( void )
|
||||
{
|
||||
BaseType_t xStatus = pdFALSE;
|
||||
xStatus = (uxSemaphoreGetCount(xMasterSemaRxHandle) == 0) ? TRUE : FALSE;
|
||||
return xStatus;
|
||||
}
|
||||
|
||||
void vMBMasterRxFlush( void )
|
||||
{
|
||||
size_t xSize = 1;
|
||||
esp_err_t xErr = ESP_OK;
|
||||
for (int xCount = 0; (xCount < MB_SERIAL_RX_FLUSH_RETRY) && xSize; xCount++) {
|
||||
xErr = uart_get_buffered_data_len(ucUartNumber, &xSize);
|
||||
MB_PORT_CHECK((xErr == ESP_OK), ; , "mb flush serial fail, error = 0x%x.", (int)xErr);
|
||||
BaseType_t xStatus = xQueueReset(xMbUartQueue);
|
||||
if (xStatus) {
|
||||
xErr = uart_flush_input(ucUartNumber);
|
||||
MB_PORT_CHECK((xErr == ESP_OK), ; , "mb flush serial fail, error = 0x%x.", (int)xErr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void vMBMasterPortSerialEnable(BOOL bRxEnable, BOOL bTxEnable)
|
||||
{
|
||||
// This function can be called from xMBRTUTransmitFSM() of different task
|
||||
if (bTxEnable) {
|
||||
vMBMasterRxFlush();
|
||||
bTxStateEnabled = TRUE;
|
||||
} else {
|
||||
bTxStateEnabled = FALSE;
|
||||
}
|
||||
if (bRxEnable) {
|
||||
bRxStateEnabled = TRUE;
|
||||
vMBMasterRxSemaRelease();
|
||||
vTaskResume(xMbTaskHandle); // Resume receiver task
|
||||
} else {
|
||||
vTaskSuspend(xMbTaskHandle); // Block receiver task
|
||||
bRxStateEnabled = FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
static USHORT usMBMasterPortSerialRxPoll(size_t xEventSize)
|
||||
{
|
||||
BOOL xStatus = TRUE;
|
||||
USHORT usCnt = 0;
|
||||
|
||||
xStatus = xMBMasterPortRxSemaTake(MB_SERIAL_RX_SEMA_TOUT);
|
||||
if (xStatus) {
|
||||
while(xStatus && (usCnt++ <= xEventSize)) {
|
||||
// Call the Modbus stack callback function and let it fill the stack buffers.
|
||||
xStatus = pxMBMasterFrameCBByteReceived(); // callback to receive FSM
|
||||
}
|
||||
// The buffer is transferred into Modbus stack and is not needed here any more
|
||||
uart_flush_input(ucUartNumber);
|
||||
ESP_LOGD(TAG, "Received data: %u(bytes in buffer)", (unsigned)usCnt);
|
||||
#if !CONFIG_FMB_TIMER_PORT_ENABLED
|
||||
vMBMasterSetCurTimerMode(MB_TMODE_T35);
|
||||
xStatus = pxMBMasterPortCBTimerExpired();
|
||||
if (!xStatus) {
|
||||
xMBMasterPortEventPost(EV_MASTER_FRAME_RECEIVED);
|
||||
ESP_LOGD(TAG, "Send additional RX ready event.");
|
||||
}
|
||||
#endif
|
||||
} else {
|
||||
ESP_LOGE(TAG, "%s: bRxState disabled but junk data (%u bytes) received. ",
|
||||
__func__, (unsigned)xEventSize);
|
||||
}
|
||||
return usCnt;
|
||||
}
|
||||
|
||||
BOOL xMBMasterPortSerialTxPoll(void)
|
||||
{
|
||||
USHORT usCount = 0;
|
||||
BOOL bNeedPoll = TRUE;
|
||||
|
||||
if( bTxStateEnabled ) {
|
||||
// Continue while all response bytes put in buffer or out of buffer
|
||||
while(bNeedPoll && (usCount++ < MB_SERIAL_BUF_SIZE)) {
|
||||
// Calls the modbus stack callback function to let it fill the UART transmit buffer.
|
||||
bNeedPoll = pxMBMasterFrameCBTransmitterEmpty( ); // callback to transmit FSM
|
||||
}
|
||||
ESP_LOGD(TAG, "MB_TX_buffer sent: (%u) bytes.", (unsigned)(usCount - 1));
|
||||
// Waits while UART sending the packet
|
||||
esp_err_t xTxStatus = uart_wait_tx_done(ucUartNumber, MB_SERIAL_TX_TOUT_TICKS);
|
||||
vMBMasterPortSerialEnable(TRUE, FALSE);
|
||||
MB_PORT_CHECK((xTxStatus == ESP_OK), FALSE, "mb serial sent buffer failure.");
|
||||
return TRUE;
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
// UART receive event task
|
||||
static void vUartTask(void* pvParameters)
|
||||
{
|
||||
uart_event_t xEvent;
|
||||
USHORT usResult = 0;
|
||||
for(;;) {
|
||||
if (xMBPortSerialWaitEvent(xMbUartQueue, (void*)&xEvent, portMAX_DELAY)) {
|
||||
ESP_LOGD(TAG, "MB_uart[%u] event:", (unsigned)ucUartNumber);
|
||||
switch(xEvent.type) {
|
||||
//Event of UART receiving data
|
||||
case UART_DATA:
|
||||
ESP_LOGD(TAG,"Data event, len: %u.", (unsigned)xEvent.size);
|
||||
// This flag set in the event means that no more
|
||||
// data received during configured timeout and UART TOUT feature is triggered
|
||||
if (xEvent.timeout_flag) {
|
||||
// Response is received but previous packet processing is pending
|
||||
// Do not wait completion of processing and just discard received data as incorrect
|
||||
if (vMBMasterRxSemaIsBusy()) {
|
||||
vMBMasterRxFlush();
|
||||
break;
|
||||
}
|
||||
// Get buffered data length
|
||||
ESP_ERROR_CHECK(uart_get_buffered_data_len(ucUartNumber, &xEvent.size));
|
||||
// Read received data and send it to modbus stack
|
||||
usResult = usMBMasterPortSerialRxPoll(xEvent.size);
|
||||
ESP_LOGD(TAG,"Timeout occured, processed: %u bytes", (unsigned)usResult);
|
||||
}
|
||||
break;
|
||||
//Event of HW FIFO overflow detected
|
||||
case UART_FIFO_OVF:
|
||||
ESP_LOGD(TAG, "hw fifo overflow.");
|
||||
xQueueReset(xMbUartQueue);
|
||||
break;
|
||||
//Event of UART ring buffer full
|
||||
case UART_BUFFER_FULL:
|
||||
ESP_LOGD(TAG, "ring buffer full.");
|
||||
xQueueReset(xMbUartQueue);
|
||||
uart_flush_input(ucUartNumber);
|
||||
break;
|
||||
//Event of UART RX break detected
|
||||
case UART_BREAK:
|
||||
ESP_LOGD(TAG, "uart rx break.");
|
||||
break;
|
||||
//Event of UART parity check error
|
||||
case UART_PARITY_ERR:
|
||||
ESP_LOGD(TAG, "uart parity error.");
|
||||
xQueueReset(xMbUartQueue);
|
||||
uart_flush_input(ucUartNumber);
|
||||
break;
|
||||
//Event of UART frame error
|
||||
case UART_FRAME_ERR:
|
||||
ESP_LOGD(TAG, "uart frame error.");
|
||||
xQueueReset(xMbUartQueue);
|
||||
uart_flush_input(ucUartNumber);
|
||||
break;
|
||||
default:
|
||||
ESP_LOGD(TAG, "uart event type: %u.", (unsigned)xEvent.type);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
/* ----------------------- Start implementation -----------------------------*/
|
||||
BOOL xMBMasterPortSerialInit( UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits, eMBParity eParity )
|
||||
{
|
||||
esp_err_t xErr = ESP_OK;
|
||||
// Set communication port number
|
||||
ucUartNumber = ucPORT;
|
||||
// Configure serial communication parameters
|
||||
UCHAR ucParity = UART_PARITY_DISABLE;
|
||||
UCHAR ucData = UART_DATA_8_BITS;
|
||||
switch(eParity){
|
||||
case MB_PAR_NONE:
|
||||
ucParity = UART_PARITY_DISABLE;
|
||||
break;
|
||||
case MB_PAR_ODD:
|
||||
ucParity = UART_PARITY_ODD;
|
||||
break;
|
||||
case MB_PAR_EVEN:
|
||||
ucParity = UART_PARITY_EVEN;
|
||||
break;
|
||||
default:
|
||||
ESP_LOGE(TAG, "Incorrect parity option: %u", (unsigned)eParity);
|
||||
return FALSE;
|
||||
}
|
||||
switch(ucDataBits){
|
||||
case 5:
|
||||
ucData = UART_DATA_5_BITS;
|
||||
break;
|
||||
case 6:
|
||||
ucData = UART_DATA_6_BITS;
|
||||
break;
|
||||
case 7:
|
||||
ucData = UART_DATA_7_BITS;
|
||||
break;
|
||||
case 8:
|
||||
ucData = UART_DATA_8_BITS;
|
||||
break;
|
||||
default:
|
||||
ucData = UART_DATA_8_BITS;
|
||||
break;
|
||||
}
|
||||
uart_config_t xUartConfig = {
|
||||
.baud_rate = ulBaudRate,
|
||||
.data_bits = ucData,
|
||||
.parity = ucParity,
|
||||
.stop_bits = UART_STOP_BITS_1,
|
||||
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
|
||||
.rx_flow_ctrl_thresh = 2,
|
||||
#if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0))
|
||||
.source_clk = UART_SCLK_DEFAULT,
|
||||
#else
|
||||
.source_clk = UART_SCLK_APB,
|
||||
#endif
|
||||
};
|
||||
// Set UART config
|
||||
xErr = uart_param_config(ucUartNumber, &xUartConfig);
|
||||
MB_PORT_CHECK((xErr == ESP_OK),
|
||||
FALSE, "mb config failure, uart_param_config() returned (0x%x).", (int)xErr);
|
||||
// Install UART driver, and get the queue.
|
||||
xErr = uart_driver_install(ucUartNumber, MB_SERIAL_BUF_SIZE, MB_SERIAL_BUF_SIZE,
|
||||
MB_QUEUE_LENGTH, &xMbUartQueue, MB_PORT_SERIAL_ISR_FLAG);
|
||||
MB_PORT_CHECK((xErr == ESP_OK), FALSE,
|
||||
"mb serial driver failure, uart_driver_install() returned (0x%x).", (int)xErr);
|
||||
// Set timeout for TOUT interrupt (T3.5 modbus time)
|
||||
xErr = uart_set_rx_timeout(ucUartNumber, MB_SERIAL_TOUT);
|
||||
MB_PORT_CHECK((xErr == ESP_OK), FALSE,
|
||||
"mb serial set rx timeout failure, uart_set_rx_timeout() returned (0x%x).", (int)xErr);
|
||||
|
||||
// Set always timeout flag to trigger timeout interrupt even after rx fifo full
|
||||
uart_set_always_rx_timeout(ucUartNumber, true);
|
||||
MB_PORT_CHECK((xMBMasterPortRxSemaInit()), FALSE,
|
||||
"mb serial RX semaphore create fail.");
|
||||
// Create a task to handle UART events
|
||||
BaseType_t xStatus = xTaskCreatePinnedToCore(vUartTask, "uart_queue_task",
|
||||
MB_SERIAL_TASK_STACK_SIZE,
|
||||
NULL, MB_SERIAL_TASK_PRIO,
|
||||
&xMbTaskHandle, MB_PORT_TASK_AFFINITY);
|
||||
if (xStatus != pdPASS) {
|
||||
vTaskDelete(xMbTaskHandle);
|
||||
// Force exit from function with failure
|
||||
MB_PORT_CHECK(FALSE, FALSE,
|
||||
"mb stack serial task creation error. xTaskCreate() returned (0x%x).", (int)xStatus);
|
||||
} else {
|
||||
vTaskSuspend(xMbTaskHandle); // Suspend serial task while stack is not started
|
||||
}
|
||||
ESP_LOGD(MB_PORT_TAG,"%s Init serial.", __func__);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
void vMBMasterPortSerialClose(void)
|
||||
{
|
||||
vMBMasterPortRxSemaClose();
|
||||
(void)vTaskDelete(xMbTaskHandle);
|
||||
ESP_ERROR_CHECK(uart_driver_delete(ucUartNumber));
|
||||
}
|
||||
|
||||
BOOL xMBMasterPortSerialPutByte(CHAR ucByte)
|
||||
{
|
||||
// Send one byte to UART transmission buffer
|
||||
// This function is called by Modbus stack
|
||||
UCHAR ucLength = uart_write_bytes(ucUartNumber, &ucByte, 1);
|
||||
return (ucLength == 1);
|
||||
}
|
||||
|
||||
// Get one byte from intermediate RX buffer
|
||||
BOOL xMBMasterPortSerialGetByte(CHAR* pucByte)
|
||||
{
|
||||
assert(pucByte != NULL);
|
||||
USHORT usLength = uart_read_bytes(ucUartNumber, (uint8_t*)pucByte, 1, MB_SERIAL_RX_TOUT_TICKS);
|
||||
return (usLength == 1);
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2010 Christian Walter
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*
|
||||
* SPDX-FileContributor: 2016-2021 Espressif Systems (Shanghai) CO LTD
|
||||
*/
|
||||
/*
|
||||
* FreeModbus Libary: ESP32 Port Demo Application
|
||||
* Copyright (C) 2010 Christian Walter <cwalter@embedded-solutions.at>
|
||||
*
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* IF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* File: $Id: portother.c,v 1.1 2010/06/06 13:07:20 wolti Exp $
|
||||
*/
|
||||
/* ----------------------- Platform includes --------------------------------*/
|
||||
#include "port.h"
|
||||
|
||||
/* ----------------------- Modbus includes ----------------------------------*/
|
||||
#include "mb.h"
|
||||
#include "mbport.h"
|
||||
#include "sdkconfig.h"
|
||||
|
||||
#if CONFIG_FMB_TIMER_PORT_ENABLED
|
||||
|
||||
static const char *TAG = "MBS_TIMER";
|
||||
|
||||
static xTimerContext_t* pxTimerContext = NULL;
|
||||
|
||||
/* ----------------------- Start implementation -----------------------------*/
|
||||
static void IRAM_ATTR vTimerAlarmCBHandler(void *param)
|
||||
{
|
||||
pxMBPortCBTimerExpired(); // Timer expired callback function
|
||||
pxTimerContext->xTimerState = TRUE;
|
||||
ESP_EARLY_LOGD(TAG, "Slave timeout triggered.");
|
||||
}
|
||||
#endif
|
||||
|
||||
BOOL xMBPortTimersInit(USHORT usTimeOut50us)
|
||||
{
|
||||
#if CONFIG_FMB_TIMER_PORT_ENABLED
|
||||
MB_PORT_CHECK((usTimeOut50us > 0), FALSE,
|
||||
"Modbus timeout discreet is incorrect.");
|
||||
MB_PORT_CHECK(!pxTimerContext, FALSE,
|
||||
"Modbus timer is already created.");
|
||||
pxTimerContext = calloc(1, sizeof(xTimerContext_t));
|
||||
if (!pxTimerContext) {
|
||||
return FALSE;
|
||||
}
|
||||
pxTimerContext->xTimerIntHandle = NULL;
|
||||
// Save timer reload value for Modbus T35 period
|
||||
pxTimerContext->usT35Ticks = usTimeOut50us;
|
||||
esp_timer_create_args_t xTimerConf = {
|
||||
.callback = vTimerAlarmCBHandler,
|
||||
.arg = NULL,
|
||||
#if (MB_TIMER_SUPPORTS_ISR_DISPATCH_METHOD && CONFIG_FMB_TIMER_USE_ISR_DISPATCH_METHOD)
|
||||
.dispatch_method = ESP_TIMER_ISR,
|
||||
#else
|
||||
.dispatch_method = ESP_TIMER_TASK,
|
||||
#endif
|
||||
.name = "MBS_T35timer"
|
||||
};
|
||||
// Create Modbus timer
|
||||
esp_err_t xErr = esp_timer_create(&xTimerConf, &(pxTimerContext->xTimerIntHandle));
|
||||
if (xErr) {
|
||||
return FALSE;
|
||||
}
|
||||
#endif
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
void vMBPortTimersEnable(void)
|
||||
{
|
||||
#if CONFIG_FMB_TIMER_PORT_ENABLED
|
||||
MB_PORT_CHECK((pxTimerContext && pxTimerContext->xTimerIntHandle), ; ,
|
||||
"timer is not initialized.");
|
||||
uint64_t xToutUs = (pxTimerContext->usT35Ticks * MB_TIMER_TICK_TIME_US);
|
||||
esp_timer_stop(pxTimerContext->xTimerIntHandle);
|
||||
esp_timer_start_once(pxTimerContext->xTimerIntHandle, xToutUs);
|
||||
pxTimerContext->xTimerState = FALSE;
|
||||
#endif
|
||||
}
|
||||
|
||||
void MB_PORT_ISR_ATTR
|
||||
vMBPortTimersDisable(void)
|
||||
{
|
||||
#if CONFIG_FMB_TIMER_PORT_ENABLED
|
||||
// Disable timer alarm
|
||||
esp_timer_stop(pxTimerContext->xTimerIntHandle);
|
||||
#endif
|
||||
}
|
||||
|
||||
void vMBPortTimerClose(void)
|
||||
{
|
||||
#if CONFIG_FMB_TIMER_PORT_ENABLED
|
||||
// Delete active timer
|
||||
if (pxTimerContext) {
|
||||
if (pxTimerContext->xTimerIntHandle) {
|
||||
esp_timer_stop(pxTimerContext->xTimerIntHandle);
|
||||
esp_timer_delete(pxTimerContext->xTimerIntHandle);
|
||||
}
|
||||
free(pxTimerContext);
|
||||
pxTimerContext = NULL;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void vMBPortTimersDelay(USHORT usTimeOutMS)
|
||||
{
|
||||
vTaskDelay(usTimeOutMS / portTICK_PERIOD_MS);
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user