From 3dd64da78b5ab4785933dc256b6f9c2846599483 Mon Sep 17 00:00:00 2001 From: tangweikang Date: Wed, 15 May 2019 12:03:03 +0800 Subject: [PATCH 1/2] =?UTF-8?q?[components][freemodbus]=20update=20functio?= =?UTF-8?q?n=20:=20xMBPortSerialInit(...)=20and=20xMBMasterPortSerialInit(?= =?UTF-8?q?...)=20|=20=E6=9B=B4=E6=96=B0=20xMBPortSerialInit()=20=E4=B8=8E?= =?UTF-8?q?=20xMBMasterPortSerialInit()=20=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/net/freemodbus/port/portserial.c | 37 +++++++++------- components/net/freemodbus/port/portserial_m.c | 43 ++++++++++++------- 2 files changed, 49 insertions(+), 31 deletions(-) diff --git a/components/net/freemodbus/port/portserial.c b/components/net/freemodbus/port/portserial.c index 7c546a1691..775ce6db28 100644 --- a/components/net/freemodbus/port/portserial.c +++ b/components/net/freemodbus/port/portserial.c @@ -52,6 +52,7 @@ static void serial_soft_trans_irq(void* parameter); BOOL xMBPortSerialInit(UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits, eMBParity eParity) { + rt_device_t dev = RT_NULL; /** * set 485 mode receive and transmit control IO * @note MODBUS_SLAVE_RT_CONTROL_PIN_INDEX need be defined by user @@ -60,22 +61,28 @@ BOOL xMBPortSerialInit(UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits, rt_pin_mode(MODBUS_SLAVE_RT_CONTROL_PIN_INDEX, PIN_MODE_OUTPUT); #endif /* set serial name */ - if (ucPORT == 1) { -#if defined(RT_USING_UART1) || defined(RT_USING_REMAP_UART1) - extern struct rt_serial_device serial1; - serial = &serial1; -#endif - } else if (ucPORT == 2) { -#if defined(RT_USING_UART2) - extern struct rt_serial_device serial2; - serial = &serial2; -#endif - } else if (ucPORT == 3) { -#if defined(RT_USING_UART3) - extern struct rt_serial_device serial3; - serial = &serial3; -#endif + if (ucPORT == 1) + { + dev = rt_device_find("uart1"); } + else if (ucPORT == 2) + { + dev = rt_device_find("uart2"); + } + else if (ucPORT == 3) + { + dev = rt_device_find("uart3"); + } + + if(dev == RT_NULL) + { + return FALSE; + } + else + { + serial = (struct rt_serial_device*)dev; + } + /* set serial configure parameter */ serial->config.baud_rate = ulBaudRate; serial->config.stop_bits = STOP_BITS_1; diff --git a/components/net/freemodbus/port/portserial_m.c b/components/net/freemodbus/port/portserial_m.c index f68794a617..cbe26bd0ae 100644 --- a/components/net/freemodbus/port/portserial_m.c +++ b/components/net/freemodbus/port/portserial_m.c @@ -53,6 +53,7 @@ static void serial_soft_trans_irq(void* parameter); BOOL xMBMasterPortSerialInit(UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits, eMBParity eParity) { + rt_device_t dev = RT_NULL; /** * set 485 mode receive and transmit control IO * @note MODBUS_MASTER_RT_CONTROL_PIN_INDEX need be defined by user @@ -60,23 +61,27 @@ BOOL xMBMasterPortSerialInit(UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits, #if defined(RT_MODBUS_MASTER_USE_CONTROL_PIN) rt_pin_mode(MODBUS_MASTER_RT_CONTROL_PIN_INDEX, PIN_MODE_OUTPUT); #endif - /* set serial name */ - if (ucPORT == 1) { -#if defined(RT_USING_UART1) || defined(RT_USING_REMAP_UART1) - extern struct rt_serial_device serial1; - serial = &serial1; -#endif - } else if (ucPORT == 2) { -#if defined(RT_USING_UART2) - extern struct rt_serial_device serial2; - serial = &serial2; -#endif - } else if (ucPORT == 3) { -#if defined(RT_USING_UART3) - extern struct rt_serial_device serial3; - serial = &serial3; -#endif + if (ucPORT == 1) + { + dev = rt_device_find("uart1"); + } + else if (ucPORT == 2) + { + dev = rt_device_find("uart2"); + } + else if (ucPORT == 3) + { + dev = rt_device_find("uart3"); + } + + if(dev == RT_NULL) + { + return FALSE; + } + else + { + serial = (struct rt_serial_device*)dev; } /* set serial configure parameter */ serial->config.baud_rate = ulBaudRate; @@ -211,6 +216,12 @@ static void serial_soft_trans_irq(void* parameter) { RT_WAITING_FOREVER, &recved_event); /* execute modbus callback */ prvvUARTTxReadyISR(); + /* waiting for serial transmit start */ + // if (rt_event_recv(&event_serial, EVENT_SERIAL_TRANS_START, RT_EVENT_FLAG_OR,RT_WAITING_FOREVER, &recved_event) == RT_EOK ) + // { + // /* execute modbus callback */ + // prvvUARTTxReadyISR(); + // } } } From 4f44fa8f60b0aab862d0b8dd46eaa568ac366f22 Mon Sep 17 00:00:00 2001 From: tangweikang Date: Wed, 15 May 2019 15:07:33 +0800 Subject: [PATCH 2/2] =?UTF-8?q?[components][freemodbus]=20update=20functio?= =?UTF-8?q?n=20:=20xMBPortSerialInit(...)=20and=20xMBMasterPortSerialInit(?= =?UTF-8?q?...)=20|=20=E6=9B=B4=E6=96=B0=20xMBPortSerialInit()=20=E4=B8=8E?= =?UTF-8?q?=20xMBMasterPortSerialInit()=20=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/net/freemodbus/port/portserial.c | 16 ++++--------- components/net/freemodbus/port/portserial_m.c | 24 +++++-------------- 2 files changed, 10 insertions(+), 30 deletions(-) diff --git a/components/net/freemodbus/port/portserial.c b/components/net/freemodbus/port/portserial.c index 775ce6db28..cbd846d7ff 100644 --- a/components/net/freemodbus/port/portserial.c +++ b/components/net/freemodbus/port/portserial.c @@ -53,6 +53,7 @@ BOOL xMBPortSerialInit(UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits, eMBParity eParity) { rt_device_t dev = RT_NULL; + char uart_name[20]; /** * set 485 mode receive and transmit control IO * @note MODBUS_SLAVE_RT_CONTROL_PIN_INDEX need be defined by user @@ -61,21 +62,12 @@ BOOL xMBPortSerialInit(UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits, rt_pin_mode(MODBUS_SLAVE_RT_CONTROL_PIN_INDEX, PIN_MODE_OUTPUT); #endif /* set serial name */ - if (ucPORT == 1) - { - dev = rt_device_find("uart1"); - } - else if (ucPORT == 2) - { - dev = rt_device_find("uart2"); - } - else if (ucPORT == 3) - { - dev = rt_device_find("uart3"); - } + rt_snprintf(uart_name,sizeof(uart_name), "uart%d", ucPORT); + dev = rt_device_find(uart_name); if(dev == RT_NULL) { + /* can not find uart */ return FALSE; } else diff --git a/components/net/freemodbus/port/portserial_m.c b/components/net/freemodbus/port/portserial_m.c index cbe26bd0ae..47a70dd905 100644 --- a/components/net/freemodbus/port/portserial_m.c +++ b/components/net/freemodbus/port/portserial_m.c @@ -54,6 +54,8 @@ BOOL xMBMasterPortSerialInit(UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits, eMBParity eParity) { rt_device_t dev = RT_NULL; + char uart_name[20]; + /** * set 485 mode receive and transmit control IO * @note MODBUS_MASTER_RT_CONTROL_PIN_INDEX need be defined by user @@ -62,27 +64,19 @@ BOOL xMBMasterPortSerialInit(UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits, rt_pin_mode(MODBUS_MASTER_RT_CONTROL_PIN_INDEX, PIN_MODE_OUTPUT); #endif /* set serial name */ - if (ucPORT == 1) - { - dev = rt_device_find("uart1"); - } - else if (ucPORT == 2) - { - dev = rt_device_find("uart2"); - } - else if (ucPORT == 3) - { - dev = rt_device_find("uart3"); - } + rt_snprintf(uart_name,sizeof(uart_name), "uart%d", ucPORT); + dev = rt_device_find(uart_name); if(dev == RT_NULL) { + /* can not find uart */ return FALSE; } else { serial = (struct rt_serial_device*)dev; } + /* set serial configure parameter */ serial->config.baud_rate = ulBaudRate; serial->config.stop_bits = STOP_BITS_1; @@ -216,12 +210,6 @@ static void serial_soft_trans_irq(void* parameter) { RT_WAITING_FOREVER, &recved_event); /* execute modbus callback */ prvvUARTTxReadyISR(); - /* waiting for serial transmit start */ - // if (rt_event_recv(&event_serial, EVENT_SERIAL_TRANS_START, RT_EVENT_FLAG_OR,RT_WAITING_FOREVER, &recved_event) == RT_EOK ) - // { - // /* execute modbus callback */ - // prvvUARTTxReadyISR(); - // } } }