]> granicus.if.org Git - esp-idf/commitdiff
examples: freemodbus port serial slave move uart mode settings into example
authoraleks <aleks@espressif.com>
Mon, 3 Dec 2018 13:16:36 +0000 (14:16 +0100)
committeraleks <aleks@espressif.com>
Fri, 21 Dec 2018 08:54:26 +0000 (09:54 +0100)
Update freemodbus component file esp-idf/components/freemodbus/port/portserial.c to remove UART mode settings;
Move UART mode settings into esp-idf/examples/protocols/modbus_slave/main/freemodbus.c
Move UART pin settings from Component config into example Kconfig.projbuild file
Move setup of UART port pins from freemodbus component into example file

TW#27721
Closes https://github.com/espressif/esp-idf/issues/2784#issuecomment-443600157

components/freemodbus/Kconfig
components/freemodbus/port/portserial.c
examples/protocols/modbus_slave/main/Kconfig.projbuild [new file with mode: 0644]
examples/protocols/modbus_slave/main/freemodbus.c

index fd4314beca7ab76ec3e81ffe44ef6a40cd16bfce..b6d3d29fb4b514ef902d3bb8a0ebb73c295a2fa9 100644 (file)
@@ -1,29 +1,5 @@
 menu "Modbus configuration"
 
-config MB_UART_RXD
-    int "UART RXD pin number"
-    range 0 34
-    default 22
-    help
-        GPIO number for UART RX pin. See UART documentation for more information
-        about available pin numbers for UART.
-        
-config MB_UART_TXD
-    int "UART TXD pin number"
-    range 0 34
-    default 23
-    help
-        GPIO number for UART TX pin. See UART documentation for more information
-        about available pin numbers for UART.
-
-config MB_UART_RTS
-    int "UART RTS pin number"
-    range 0 34
-    default 18
-    help
-        GPIO number for UART RTS pin. This pin is connected to 
-        ~RE/DE pin of RS485 transceiver to switch direction.
-
 config MB_QUEUE_LENGTH
     int "Modbus serial task queue length"
     range 0 200
index e9103d0b75befded641bc238235f8655ed4a0d77..3543135ee3deaf1009f810c43e3e2af15f2cca8b 100644 (file)
@@ -231,19 +231,11 @@ BOOL xMBPortSerialInit(UCHAR ucPORT, ULONG ulBaudRate,
     xErr = uart_param_config(ucUartNumber, &xUartConfig);
     MB_PORT_CHECK((xErr == ESP_OK),
             FALSE, "mb config failure, uart_param_config() returned (0x%x).", (uint32_t)xErr);
-    // Set UART pin numbers
-    xErr = uart_set_pin(ucUartNumber, MB_UART_TXD, MB_UART_RXD, MB_UART_RTS, UART_PIN_NO_CHANGE);
-    MB_PORT_CHECK((xErr == ESP_OK), FALSE,
-            "mb set pin failure, uart_set_pin() returned (0x%x).", (uint32_t)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, ESP_INTR_FLAG_LOWMED);
     MB_PORT_CHECK((xErr == ESP_OK), FALSE,
             "mb serial driver failure, uart_driver_install() returned (0x%x).", (uint32_t)xErr);
-    // Set driver mode to Half Duplex
-    xErr = uart_set_mode(ucUartNumber, UART_MODE_RS485_HALF_DUPLEX);
-    MB_PORT_CHECK((xErr == ESP_OK), FALSE,
-            "mb serial set mode failure, uart_set_mode() returned (0x%x).", (uint32_t)xErr);
 #ifndef MB_TIMER_PORT_ENABLED
     // Set timeout for TOUT interrupt (T3.5 modbus time)
     xErr = uart_set_rx_timeout(ucUartNumber, MB_SERIAL_TOUT);
diff --git a/examples/protocols/modbus_slave/main/Kconfig.projbuild b/examples/protocols/modbus_slave/main/Kconfig.projbuild
new file mode 100644 (file)
index 0000000..c4e1217
--- /dev/null
@@ -0,0 +1,27 @@
+menu "Modbus Slave Example Configuration"
+
+config MB_UART_RXD
+    int "UART RXD pin number"
+    range 0 34
+    default 22
+    help
+        GPIO number for UART RX pin. See UART documentation for more information
+        about available pin numbers for UART.
+        
+config MB_UART_TXD
+    int "UART TXD pin number"
+    range 0 34
+    default 23
+    help
+        GPIO number for UART TX pin. See UART documentation for more information
+        about available pin numbers for UART.
+
+config MB_UART_RTS
+    int "UART RTS pin number"
+    range 0 34
+    default 18
+    help
+        GPIO number for UART RTS pin. This pin is connected to 
+        ~RE/DE pin of RS485 transceiver to switch direction.
+
+endmenu
index 19e18f350c1dd7000855743eb5dd08509326ef27..ff52efcb46d391727d7525f379572428935403c4 100644 (file)
@@ -75,7 +75,7 @@ void app_main()
     comm_info.baudrate = MB_DEV_SPEED;
     comm_info.parity = MB_PARITY_NONE;
     ESP_ERROR_CHECK(mbcontroller_setup(comm_info));
-
+    
     // The code below initializes Modbus register area descriptors
     // for Modbus Holding Registers, Input Registers, Coils and Discrete Inputs
     // Initialization should be done for each supported Modbus register area according to register map.
@@ -113,6 +113,14 @@ void app_main()
 
     // Starts of modbus controller and stack
     ESP_ERROR_CHECK(mbcontroller_start());
+    
+    // Set UART driver mode to Half Duplex
+    ESP_ERROR_CHECK(uart_set_mode(MB_PORT_NUM, UART_MODE_RS485_HALF_DUPLEX));  
+
+    // Set UART pin numbers
+    ESP_ERROR_CHECK(uart_set_pin(MB_PORT_NUM, CONFIG_MB_UART_TXD, 
+                                    CONFIG_MB_UART_RXD, CONFIG_MB_UART_RTS, 
+                                    UART_PIN_NO_CHANGE));
 
     // The cycle below will be terminated when parameter holdingRegParams.dataChan0
     // incremented each access cycle reaches the CHAN_DATA_MAX_VAL value.