From 5185501d7296cbfbcb018b5fcc60f50ca9f2e66c Mon Sep 17 00:00:00 2001 From: wlyu Date: Tue, 15 Feb 2022 17:40:40 +0800 Subject: [PATCH] support PLC bus and clean codes --- .../socket_demo/lwip_tcp_socket_demo.c | 17 +- .../socket_demo/lwip_udp_socket_demo.c | 17 +- .../control_app/plc_demo/plc_demo.c | 29 +-- .../Framework/control/plc/shared/Makefile | 2 +- .../Framework/control/plc/shared/plc.c | 185 --------------- .../Framework/control/plc/shared/plc.h | 140 ------------ .../Framework/control/plc/shared/plc_bus.c | 2 + .../Framework/control/plc/shared/plc_dev.c | 183 ++++++--------- .../Framework/control/plc/shared/plc_dev.h | 212 +++++++++--------- .../Framework/control/plc/shared/plc_drv.c | 2 + .../ethernet/cmd_lwip/lwip_config_demo.c | 21 +- .../ethernet/cmd_lwip/lwip_dhcp_demo.c | 16 +- .../ethernet/cmd_lwip/lwip_ping_demo.c | 20 +- .../ethernet/cmd_lwip/lwip_tcp_demo.c | 16 +- .../ethernet/cmd_lwip/lwip_udp_demo.c | 17 +- .../XiUOS/tool/shell/letter-shell/cmd.c | 1 + 16 files changed, 200 insertions(+), 680 deletions(-) delete mode 100755 APP_Framework/Framework/control/plc/shared/plc.c delete mode 100755 APP_Framework/Framework/control/plc/shared/plc.h diff --git a/APP_Framework/Applications/connection_app/socket_demo/lwip_tcp_socket_demo.c b/APP_Framework/Applications/connection_app/socket_demo/lwip_tcp_socket_demo.c index 758e13609..790ef53a2 100755 --- a/APP_Framework/Applications/connection_app/socket_demo/lwip_tcp_socket_demo.c +++ b/APP_Framework/Applications/connection_app/socket_demo/lwip_tcp_socket_demo.c @@ -17,6 +17,7 @@ * @author AIIT XUOS Lab * @date 2021-05-29 */ + #include #include #include "board.h" @@ -24,25 +25,11 @@ #include #include "lwip/sys.h" -/******************************************************************************* - * Definitions - ******************************************************************************/ - #define TCP_DEMO_BUF_SIZE 65535 -/******************************************************************************* - * Prototypes - ******************************************************************************/ - -/******************************************************************************* - * Variables - ******************************************************************************/ - char tcp_socket_ip[] = {192, 168, 250, 252}; -/******************************************************************************* - * Code - ******************************************************************************/ +/******************************************************************************/ static void TCPSocketRecvTask(void *arg) { diff --git a/APP_Framework/Applications/connection_app/socket_demo/lwip_udp_socket_demo.c b/APP_Framework/Applications/connection_app/socket_demo/lwip_udp_socket_demo.c index eb7b6b138..45e9f9c82 100755 --- a/APP_Framework/Applications/connection_app/socket_demo/lwip_udp_socket_demo.c +++ b/APP_Framework/Applications/connection_app/socket_demo/lwip_udp_socket_demo.c @@ -26,27 +26,14 @@ #include #include "lwip/sys.h" -/******************************************************************************* - * Definitions - ******************************************************************************/ - #define UDP_BUF_SIZE 65536 -/******************************************************************************* - * Prototypes - ******************************************************************************/ - -/******************************************************************************* - * Variables - ******************************************************************************/ - extern char udp_target[]; static struct udp_pcb *udpecho_raw_pcb; char udp_socket_ip[] = {192, 168, 250, 252}; -/******************************************************************************* - * Code - ******************************************************************************/ +/******************************************************************************/ + static void UdpSocketRecvTask(void *arg) { lw_print("UdpSocketRecvTask start.\n"); diff --git a/APP_Framework/Applications/control_app/plc_demo/plc_demo.c b/APP_Framework/Applications/control_app/plc_demo/plc_demo.c index 6b8621d77..5ebf240e1 100755 --- a/APP_Framework/Applications/control_app/plc_demo/plc_demo.c +++ b/APP_Framework/Applications/control_app/plc_demo/plc_demo.c @@ -19,43 +19,27 @@ */ #include "open62541.h" -#include "plc.h" #include "ua_api.h" #include "sys_arch.h" #include "plc_bus.h" #include "plc_dev.h" -/******************************************************************************* - * Definitions - ******************************************************************************/ +#define PLC_BUS_NAME "PLC" +#define PLC_DRV_NAME "OPCUA" +#define PLC_DEV_NAME "PLC_1" -#define PLC_BUS_NAME "plc bus" -#define PLC_DRV_NAME "plc driver" #define PLC_BUF_LEN 1000 #define PLC_STACK_SIZE 4096 #define PLC_TASK_PRIO 15 -#define plc_print KPrintf - -/******************************************************************************* - * Prototypes - ******************************************************************************/ - -/******************************************************************************* - * Variables - ******************************************************************************/ - struct PlcBus plc_demo_bus; struct PlcDriver plc_demo_drv; struct PlcDevice plc_demo_dev; char plc_demo_ip[] = {192, 168, 250, 5}; -/******************************************************************************* - * Code - ******************************************************************************/ - +/******************************************************************************/ void PlcTestInit(void) { @@ -63,10 +47,13 @@ void PlcTestInit(void) PlcBusInit(&plc_demo_bus, PLC_BUS_NAME); PlcDriverInit(&plc_demo_drv, PLC_DRV_NAME); + PlcDriverAttachToBus(PLC_DRV_NAME, PLC_BUS_NAME); // register plc device plc_demo_dev.state = DEV_INIT; - PlcDevRegister(&plc_demo_dev, NULL, "plc test device"); + PlcDevRegister(&plc_demo_dev, NULL, PLC_DEV_NAME); + + PlcDeviceAttachToBus(PLC_DEV_NAME, PLC_BUS_NAME); } void PlcReadUATask(void *arg) diff --git a/APP_Framework/Framework/control/plc/shared/Makefile b/APP_Framework/Framework/control/plc/shared/Makefile index 1f0543f9a..1dd297b03 100755 --- a/APP_Framework/Framework/control/plc/shared/Makefile +++ b/APP_Framework/Framework/control/plc/shared/Makefile @@ -1,4 +1,4 @@ -SRC_FILES := plc.c plc_dev.c plc_bus.c plc_drv.c +SRC_FILES := plc_dev.c plc_bus.c plc_drv.c include $(KERNEL_ROOT)/compiler.mk diff --git a/APP_Framework/Framework/control/plc/shared/plc.c b/APP_Framework/Framework/control/plc/shared/plc.c deleted file mode 100755 index e1af3da34..000000000 --- a/APP_Framework/Framework/control/plc/shared/plc.c +++ /dev/null @@ -1,185 +0,0 @@ -/* -* Copyright (c) 2021 AIIT XUOS Lab -* XiUOS is licensed under Mulan PSL v2. -* You can use this software according to the terms and conditions of the Mulan PSL v2. -* You may obtain a copy of Mulan PSL v2 at: -* http://license.coscl.org.cn/MulanPSL2 -* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, -* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, -* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. -* See the Mulan PSL v2 for more details. -*/ - -/** - * @file plc.c - * @brief plc relative activities - * @version 1.0 - * @author AIIT XUOS Lab - * @date 2021.12.15 - */ - - -#include "open62541.h" -#include "ua_api.h" -#include "plc.h" -#include "plc_bus.h" -#include "plc_dev.h" - - -static DoubleLinklistType plcdev_list; - -int PlcDevConfigure(struct HardwareDev *dev, uint8 plc_chip_select, uint8 plc_cs_release); - - -/*Create the plc device linklist*/ -static void PlcDeviceLinkInit() -{ - InitDoubleLinkList(&plcdev_list); -} - -static int PlcDeviceOpen(void *dev) -{ - NULL_PARAM_CHECK(dev); - - struct PlcDevice *plc_dev = (struct PlcDevice *)dev; - - if(plc_dev->net == PLC_IND_ENET_OPCUA) - { - return ua_open(plc_dev->priv_data); - } - - return EOK; -} - -static void PlcDeviceClose(void *dev) -{ - NULL_PARAM_CHECK(dev); - - struct PlcDevice *plc_dev = (struct PlcDevice *)dev; - - if(plc_dev->net == PLC_IND_ENET_OPCUA) - { - ua_close(plc_dev->priv_data); - } -} - -static int PlcDeviceWrite(void *dev, const void *buf, size_t len) -{ - NULL_PARAM_CHECK(dev); - NULL_PARAM_CHECK(buf); - - int ret; - struct PlcDevice *plc_dev = (struct PlcDevice *)dev; - - if(plc_dev->net == PLC_IND_ENET_OPCUA) - { - ret = ua_write(plc_dev->priv_data, buf, len); - } - - return ret; -} - -static int PlcDeviceRead(void *dev, void *buf, size_t len) -{ - NULL_PARAM_CHECK(dev); - NULL_PARAM_CHECK(buf); - - int ret; - struct PlcDevice *plc_dev = (struct PlcDevice *)dev; - - if(plc_dev->net == PLC_IND_ENET_OPCUA) - { - ret = ua_read(plc_dev->priv_data, buf, len); - } - - return ret; -} - -static struct PlcOps plc_done = -{ - .open = PlcDeviceOpen, - .close = PlcDeviceClose, - .write = PlcDeviceWrite, - .read = PlcDeviceRead, -}; - -struct PlcDevice *PlcDevFind(const char *dev_name, enum DevType dev_type) -{ - NULL_PARAM_CHECK(dev_name); - - struct PlcDevice *device = NONE; - - DoubleLinklistType *node = NONE; - DoubleLinklistType *head = &plcdev_list; - - for (node = head->node_next; node != head; node = node->node_next) { - device = SYS_DOUBLE_LINKLIST_ENTRY(node, struct PlcDevice, link); - if ((!strcmp(device->name, dev_name)) && (dev_type == device->type)) { - return device; - } - } - - KPrintf("PlcDevFind cannot find the %s device.return NULL\n", dev_name); - return NONE; -} - -int PlcDevRegister(struct PlcDevice *plc_device, void *plc_param, const char *device_name) -{ - NULL_PARAM_CHECK(plc_device); - NULL_PARAM_CHECK(device_name); - - x_err_t ret = EOK; - static x_bool dev_link_flag = RET_FALSE; - - if (!dev_link_flag) { - PlcDeviceLinkInit(); - dev_link_flag = RET_TRUE; - } - - if (DEV_INSTALL != plc_device->state) { - strncpy(plc_device->name, device_name, strlen(device_name)); - plc_device->ops = &plc_done; - DoubleLinkListInsertNodeAfter(&plcdev_list, &(plc_device->link)); - plc_device->state = DEV_INSTALL; - } else { - KPrintf("PlcDevRegister device has been register state%u\n", plc_device->type); - } - - return ret; -} - -int PlcDeviceAttachToBus(const char *dev_name, const char *bus_name) -{ - NULL_PARAM_CHECK(dev_name); - NULL_PARAM_CHECK(bus_name); - - x_err_t ret = EOK; - - struct Bus *bus; - struct HardwareDev *device; - - bus = BusFind(bus_name); - if (NONE == bus) { - KPrintf("PlcDeviceAttachToBus find plc bus error!name %s\n", bus_name); - return ERROR; - } - - if (TYPE_PLC_BUS == bus->bus_type) { - device = PlcHardwareDevFind(dev_name, TYPE_PLC_DEV); - if (NONE == device) { - KPrintf("PlcDeviceAttachToBus find plc device error!name %s\n", dev_name); - return ERROR; - } - - if (TYPE_PLC_DEV == device->dev_type) { - ret = DeviceRegisterToBus(bus, device); - if (EOK != ret) { - KPrintf("PlcDeviceAttachToBus DeviceRegisterToBus error %u\n", ret); - return ERROR; - } - } - } - - return EOK; -} - diff --git a/APP_Framework/Framework/control/plc/shared/plc.h b/APP_Framework/Framework/control/plc/shared/plc.h deleted file mode 100755 index 4cd097d1d..000000000 --- a/APP_Framework/Framework/control/plc/shared/plc.h +++ /dev/null @@ -1,140 +0,0 @@ -/* -* Copyright (c) 2021 AIIT XUOS Lab -* XiUOS is licensed under Mulan PSL v2. -* You can use this software according to the terms and conditions of the Mulan PSL v2. -* You may obtain a copy of Mulan PSL v2 at: -* http://license.coscl.org.cn/MulanPSL2 -* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, -* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, -* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. -* See the Mulan PSL v2 for more details. -*/ - -/** - * @file plc.h - * @brief plc relative definition and structure - * @version 1.0 - * @author AIIT XUOS Lab - * @date 2022-01-24 - */ - -#ifndef __PLC_H_ -#define __PLC_H_ - -#include "bus.h" -#include "xs_klist.h" - -#define IP_ADDR_SIZE 32 -#define PLC_NAME_SIZE 32 - -// PLC device information -struct PlcInfo { - uint32_t ability; // PLC ability - uint32_t id; // PLC Device ID - uint32_t soft_version; // software version - uint32_t hard_version; // hardware version - uint32_t date; // manufact date - const char *vendor; // vendor - const char *model; // product model -}; - -enum PlcSerialType { - PLC_SERIAL_232, - PLC_SERIAL_485, - PLC_SERIAL_CAN -}; - -union PlcCfg { - struct { - char ip_addr[IP_ADDR_SIZE]; - uint32_t ip_port; - } PlcIpCfg; - struct { - enum PlcSerialType serial_type; - char station_id; - char serial_port; - } PlcSerialCfg; -}; - -struct PlcDevice; - -#undef open -#undef close -#undef read -#undef write - -// operation API -struct PlcOps { - int (*open)(void *dev); // open and connect PLC device - void (*close)(void* dev); // close and disconnect PLC device - int (*read)(void* dev, void *buf, size_t len); // read data from PLC - int (*write)(void* dev, const void *buf, size_t len); // write data from PLC - int (*ioctl)(void* dev, int cmd, void *arg); // send control command to PLC -}; - -enum PlcCtlType { - PLC_CTRL_TYPE_HSC, - PLC_CTRL_TYPE_PID, - PLC_CTRL_TYPE_PHASING -}; - - -#define PLC_ABILITY_HSC ((uint32_t)(1 << PLC_CTRL_TYPE_HSC)) -#define PLC_ABILITY_PID ((uint32_t)(1 << PLC_CTRL_TYPE_PID)) -#define PLC_ABILITY_PHASING ((uint32_t)(1 << PLC_CTRL_TYPE_PHASING)) - - -enum PlcIndHybridNet -{ - // PLC Field Bus - PLC_IND_FIELD_MODBUS_485, - PLC_IND_FIELD_PROFIBUS, - PLC_IND_FIELD_CANOPEN, - PLC_IND_FIELD_DEVICENET, - PLC_IND_FIELD_CONTROLNET, - - // PLC ETHERNET - PLC_IND_ENET_ETHERNET_IP, - PLC_IND_ENET_PROFINET, - PLC_IND_ENET_ETHERCAT, - PLC_IND_ENET_SERCOS, - PLC_IND_ENET_OPCUA, - - // PLC wireless net - PLC_IND_WIRELESS -}; - -enum PlcTransType -{ - PLC_TRANS_TCP, - PLC_TRANS_UDP, - PLC_TRANS_SERIAL -}; - -//communication interface -struct PlcInterface -{ - char ip_addr[IP_ADDR_SIZE]; - char attrib; -}; - -// identify PLC device -struct PlcDevice { - char name[PLC_NAME_SIZE]; /* name of the device */ - enum PlcCtlType type; /* PLC Control Type */ - enum DevState state; - enum PlcIndHybridNet net; - enum PlcTransType trans; - - struct PlcInfo info;/* Plc info, such as vendor name and model name */ - union PlcCfg cfg; - struct PlcOps *ops; /* filesystem-like APIs for data transferring */ - struct PlcInterface interface; /* protocols used for transferring data from program to plc */ - - void *priv_data; - DoubleLinklistType link;/* link list node */ -}; - -int PlcDevRegister(struct PlcDevice *plc_device, void *plc_param, const char *device_name); - -#endif diff --git a/APP_Framework/Framework/control/plc/shared/plc_bus.c b/APP_Framework/Framework/control/plc/shared/plc_bus.c index 436c68ec4..4eb5a3613 100755 --- a/APP_Framework/Framework/control/plc/shared/plc_bus.c +++ b/APP_Framework/Framework/control/plc/shared/plc_bus.c @@ -21,6 +21,8 @@ #include "plc_bus.h" #include "plc_dev.h" +/******************************************************************************/ + int PlcBusInit(struct PlcBus *plc_bus, const char *bus_name) { NULL_PARAM_CHECK(plc_bus); diff --git a/APP_Framework/Framework/control/plc/shared/plc_dev.c b/APP_Framework/Framework/control/plc/shared/plc_dev.c index 1aea23da7..6950e79b6 100755 --- a/APP_Framework/Framework/control/plc/shared/plc_dev.c +++ b/APP_Framework/Framework/control/plc/shared/plc_dev.c @@ -1,5 +1,5 @@ /* -* Copyright (c) 2020 AIIT XUOS Lab +* Copyright (c) 2021 AIIT XUOS Lab * XiUOS is licensed under Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: @@ -11,136 +11,117 @@ */ /** -* @file plc_dev.c -* @brief register plc dev function using bus driver framework -* @version 1.0 -* @author AIIT XUOS Lab -* @date 2022-01-24 -*/ + * @file plc.c + * @brief plc relative activities + * @version 1.0 + * @author AIIT XUOS Lab + * @date 2021.12.15 + */ +#include "ua_api.h" #include "plc_bus.h" #include "plc_dev.h" -static DoubleLinklistType plcdev_linklist; +static DoubleLinklistType plcdev_list; + +/******************************************************************************/ /*Create the plc device linklist*/ -static void PlcHardwareDevLinkInit() +static void PlcDeviceLinkInit() { - InitDoubleLinkList(&plcdev_linklist); + InitDoubleLinkList(&plcdev_list); } -static uint32 PlcHardwareDevOpen(void *dev) +static int PlcDeviceOpen(void *dev) { NULL_PARAM_CHECK(dev); - PlcHardwareDevConfigureCs(dev, 1, 0); + struct PlcDevice *plc_dev = (struct PlcDevice *)dev; + + if(plc_dev->net == PLC_IND_ENET_OPCUA) + { + return ua_open(plc_dev->priv_data); + } return EOK; } - -static uint32 PlcHardwareDevClose(void *dev) +static void PlcDeviceClose(void *dev) { NULL_PARAM_CHECK(dev); - PlcHardwareDevConfigureCs(dev, 0, 1); + struct PlcDevice *plc_dev = (struct PlcDevice *)dev; - return EOK; + if(plc_dev->net == PLC_IND_ENET_OPCUA) + { + ua_close(plc_dev->priv_data); + } } - -static uint32 PlcHardwareDevWrite(void *dev, struct BusBlockWriteParam *write_param) +static int PlcDeviceWrite(void *dev, const void *buf, size_t len) { NULL_PARAM_CHECK(dev); - NULL_PARAM_CHECK(write_param); + NULL_PARAM_CHECK(buf); int ret; - struct PlcHardwareDevice *plc_dev = (struct PlcHardwareDevice *)dev; - struct PlcDataStandard *plc_msg; + struct PlcDevice *plc_dev = (struct PlcDevice *)dev; - plc_msg = (struct PlcDataStandard *)x_malloc(sizeof(struct PlcDataStandard)); - if (NONE == plc_msg) { - KPrintf("PlcHardwareDevWrite x_malloc msg error\n"); - x_free(plc_msg); - return ERROR; + if(plc_dev->net == PLC_IND_ENET_OPCUA) + { + ret = ua_write(plc_dev->priv_data, buf, len); } - //memset(plc_msg, 0, sizeof(struct PlcDataStandard)); - - plc_msg->tx_buff = (uint8 *)write_param->buffer; - plc_msg->rx_buff = NONE; - plc_msg->length = write_param->size; - plc_msg->plc_chip_select = 0; - plc_msg->plc_cs_release = 0; - plc_msg->next = NONE; - - ret = plc_dev->plc_dev_done->dev_write(plc_dev, plc_msg); - x_free(plc_msg); - return ret; } -static uint32 PlcHardwareDevRead(void *dev, struct BusBlockReadParam *read_param) +static int PlcDeviceRead(void *dev, void *buf, size_t len) { NULL_PARAM_CHECK(dev); - NULL_PARAM_CHECK(read_param); + NULL_PARAM_CHECK(buf); int ret; + struct PlcDevice *plc_dev = (struct PlcDevice *)dev; - struct PlcHardwareDevice *plc_dev = (struct PlcHardwareDevice *)dev; - - struct PlcDataStandard *plc_msg; - - plc_msg = (struct PlcDataStandard *)x_malloc(sizeof(struct PlcDataStandard)); - if (NONE == plc_msg) { - x_free(plc_msg); - return ERROR; + if(plc_dev->net == PLC_IND_ENET_OPCUA) + { + ret = ua_read(plc_dev->priv_data, buf, len); } - //memset(plc_msg, 0, sizeof(struct PlcDataStandard)); - - plc_msg->tx_buff = NONE; - plc_msg->rx_buff = (uint8 *)read_param->buffer; - plc_msg->length = read_param->size; - plc_msg->plc_chip_select = 0; - plc_msg->plc_cs_release = 0; - plc_msg->next = NONE; - - ret = plc_dev->plc_dev_done->dev_read(plc_dev, plc_msg); - x_free(plc_msg); - return ret; } -static const struct HalDevDone dev_done = +static struct PlcOps plc_done = { - .open = PlcHardwareDevOpen, - .close = PlcHardwareDevClose, - .write = PlcHardwareDevWrite, - .read = PlcHardwareDevRead, + .open = PlcDeviceOpen, + .close = PlcDeviceClose, + .write = PlcDeviceWrite, + .read = PlcDeviceRead, }; -HardwareDevType PlcHardwareDevFind(const char *dev_name, enum DevType dev_type) +/* find PLC device with device name */ +struct HardwareDev *PlcDevFind(const char *dev_name) { NULL_PARAM_CHECK(dev_name); - struct HardwareDev *device = NONE; + struct PlcDevice *device = NONE; + struct HardwareDev *haldev = NONE; DoubleLinklistType *node = NONE; - DoubleLinklistType *head = &plcdev_linklist; + DoubleLinklistType *head = &plcdev_list; for (node = head->node_next; node != head; node = node->node_next) { - device = SYS_DOUBLE_LINKLIST_ENTRY(node, struct HardwareDev, dev_link); - if ((!strcmp(device->dev_name, dev_name)) && (dev_type == device->dev_type)) { - return device; + device = SYS_DOUBLE_LINKLIST_ENTRY(node, struct PlcDevice, link); + if (!strcmp(device->name, dev_name)) { + haldev = &device->haldev; + return haldev; } } - KPrintf("PlcHardwareDevFind cannot find the %s device.return NULL\n", dev_name); + plc_print("plc: [%s] cannot find the %s device\n", __func__, dev_name); return NONE; } -int PlcHardwareDevRegister(struct PlcHardwareDevice *plc_device, void *plc_param, const char *device_name) +int PlcDevRegister(struct PlcDevice *plc_device, void *plc_param, const char *device_name) { NULL_PARAM_CHECK(plc_device); NULL_PARAM_CHECK(device_name); @@ -149,31 +130,28 @@ int PlcHardwareDevRegister(struct PlcHardwareDevice *plc_device, void *plc_param static x_bool dev_link_flag = RET_FALSE; if (!dev_link_flag) { - PlcHardwareDevLinkInit(); + PlcDeviceLinkInit(); dev_link_flag = RET_TRUE; } - if (DEV_INSTALL != plc_device->haldev.dev_state) { + if (DEV_INSTALL != plc_device->state) { strncpy(plc_device->haldev.dev_name, device_name, NAME_NUM_MAX); plc_device->haldev.dev_type = TYPE_PLC_DEV; plc_device->haldev.dev_state = DEV_INSTALL; - //only plc bus dev need to register dev_done - if (RET_TRUE != plc_device->plc_dev_flag) { - plc_device->haldev.dev_done = &dev_done; - } + strncpy(plc_device->name, device_name, strlen(device_name)); + plc_device->ops = &plc_done; - plc_device->haldev.private_data = plc_param; - - DoubleLinkListInsertNodeAfter(&plcdev_linklist, &(plc_device->haldev.dev_link)); + DoubleLinkListInsertNodeAfter(&plcdev_list, &(plc_device->link)); + plc_device->state = DEV_INSTALL; } else { - KPrintf("PlcHardwareDevRegister device has been register state%u\n", plc_device->haldev.dev_state); + KPrintf("PlcDevRegister device has been register state%u\n", plc_device->type); } return ret; } -int PlcHardwareDevAttachToBus(const char *dev_name, const char *bus_name) +int PlcDeviceAttachToBus(const char *dev_name, const char *bus_name) { NULL_PARAM_CHECK(dev_name); NULL_PARAM_CHECK(bus_name); @@ -185,21 +163,21 @@ int PlcHardwareDevAttachToBus(const char *dev_name, const char *bus_name) bus = BusFind(bus_name); if (NONE == bus) { - KPrintf("PlcHardwareDevAttachToBus find plc bus error!name %s\n", bus_name); + KPrintf("PlcDeviceAttachToBus find plc bus error!name %s\n", bus_name); return ERROR; } if (TYPE_PLC_BUS == bus->bus_type) { - device = PlcHardwareDevFind(dev_name, TYPE_PLC_DEV); + device = PlcDevFind(dev_name); if (NONE == device) { - KPrintf("PlcHardwareDevAttachToBus find plc device error!name %s\n", dev_name); + KPrintf("PlcDeviceAttachToBus find plc device error!name %s\n", dev_name); return ERROR; } if (TYPE_PLC_DEV == device->dev_type) { ret = DeviceRegisterToBus(bus, device); if (EOK != ret) { - KPrintf("PlcHardwareDevAttachToBus DeviceRegisterToBus error %u\n", ret); + KPrintf("PlcDeviceAttachToBus DeviceRegisterToBus error %u\n", ret); return ERROR; } } @@ -208,32 +186,3 @@ int PlcHardwareDevAttachToBus(const char *dev_name, const char *bus_name) return EOK; } -int PlcHardwareDevConfigureCs(struct HardwareDev *dev, uint8 plc_chip_select, uint8 plc_cs_release) -{ - NULL_PARAM_CHECK(dev); - - int ret; - struct PlcHardwareDevice *plc_dev = (struct PlcHardwareDevice *)dev; - struct PlcDataStandard *msg; - - msg = (struct PlcDataStandard *)x_malloc(sizeof(struct PlcDataStandard)); - if (NONE == msg){ - KPrintf("PlcHardwareDevConfigureCs x_malloc msg error\n"); - x_free(msg); - return ERROR; - } - - //memset(msg, 0, sizeof(struct PlcDataStandard)); - msg->length = 0; - msg->rx_buff = NONE; - msg->tx_buff = NONE; - msg->next = NONE; - msg->plc_chip_select = plc_chip_select; - msg->plc_cs_release = plc_cs_release; - - ret = plc_dev->plc_dev_done->dev_write(plc_dev, msg); - - x_free(msg); - return ret; -} - diff --git a/APP_Framework/Framework/control/plc/shared/plc_dev.h b/APP_Framework/Framework/control/plc/shared/plc_dev.h index b892d5e5d..b0d767805 100755 --- a/APP_Framework/Framework/control/plc/shared/plc_dev.h +++ b/APP_Framework/Framework/control/plc/shared/plc_dev.h @@ -1,5 +1,5 @@ /* -* Copyright (c) 2020 AIIT XUOS Lab +* Copyright (c) 2021 AIIT XUOS Lab * XiUOS is licensed under Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: @@ -11,129 +11,139 @@ */ /** -* @file dev_plc.h -* @brief define plc dev function using bus driver framework -* @version 1.0 -* @author AIIT XUOS Lab -* @date 2022-01-24 -*/ + * @file plc_dev.h + * @brief plc relative definition and structure + * @version 1.0 + * @author AIIT XUOS Lab + * @date 2022-01-24 + */ -#ifndef DEV_PLC_H -#define DEV_PLC_H +#ifndef __PLC_DEV_H_ +#define __PLC_DEV_H_ -#include +#include "bus.h" +#include "xs_klist.h" -#ifdef __cpluspluss -extern "C" { -#endif +#undef open +#undef close +#undef read +#undef write -#define PLC_MAX_CLOCK 40000000 -#define plc_device_max_num 4 +#define IP_ADDR_SIZE 32 +#define PLC_NAME_SIZE 32 -#define PLC_LINE_CPHA (1 << 0) -#define PLC_LINE_CPOL (1 << 1) - -#define PLC_LSB (0 << 2) -#define PLC_MSB (1 << 2) - -#define PLC_MASTER (0 << 3) -#define DEV_PLC_SLAVE (1 << 3) - -#define PLC_MODE_0 (0 | 0) -#define PLC_MODE_1 (0 | PLC_LINE_CPHA) -#define PLC_MODE_2 (PLC_LINE_CPOL | 0) -#define PLC_MODE_3 (PLC_LINE_CPOL | PLC_LINE_CPHA) -#define PLC_MODE_MASK (PLC_LINE_CPHA | PLC_LINE_CPOL | PLC_MSB) - -#define PLC_CS_HIGH (1 << 4) -#define PLC_NO_CS (1 << 5) -#define PLC_3WIRE (1 << 6) -#define PLC_READY (1 << 7) - -struct PlcDataStandard -{ - uint8 plc_chip_select; - uint8 plc_cs_release; - - const uint8 *tx_buff; - uint32 tx_len; - - uint8 *rx_buff; - uint32 rx_len; - - uint32 length; - struct PlcDataStandard *next; +// PLC device information +struct PlcInfo { + uint32_t ability; // PLC ability + uint32_t id; // PLC Device ID + uint32_t soft_version; // software version + uint32_t hard_version; // hardware version + uint32_t date; // manufact date + const char *vendor; // vendor + const char *model; // product model }; -struct PlcMasterParam -{ - uint8 plc_work_mode;//CPOL CPHA - uint8 plc_frame_format;//frame format - uint8 plc_data_bit_width;//bit width - uint8 plc_data_endian;//little endian 0 : big endian 1 - uint32 plc_maxfrequency;//work frequency +enum PlcSerialType { + PLC_SERIAL_232, + PLC_SERIAL_485, + PLC_SERIAL_CAN }; -struct PlcDmaParam -{ - uint8 plc_master_id; - uint8 plc_dmac_txchannel; - uint8 plc_dmac_rxchannel; +union PlcCfg { + struct { + char ip_addr[IP_ADDR_SIZE]; + uint32_t ip_port; + } PlcIpCfg; + struct { + enum PlcSerialType serial_type; + char station_id; + char serial_port; + } PlcSerialCfg; }; -struct PlcSlaveParam -{ - uint8 plc_slave_id; - uint8 plc_cs_gpio_pin; - uint8 plc_cs_select_id; +struct PlcDevice; + +// operation API +struct PlcOps { + int (*open)(void *dev); // open and connect PLC device + void (*close)(void* dev); // close and disconnect PLC device + int (*read)(void* dev, void *buf, size_t len); // read data from PLC + int (*write)(void* dev, const void *buf, size_t len); // write data from PLC + int (*ioctl)(void* dev, int cmd, void *arg); // send control command to PLC }; -typedef struct -{ - struct PlcDmaParam *plc_dma_param; - - struct PlcSlaveParam *plc_slave_param; - - struct PlcMasterParam *plc_master_param; - -}PlcDeviceParam; - -struct PlcHardwareDevice; - -struct PlcDevDone -{ - uint32 (*dev_open) (struct PlcHardwareDevice *dev); - uint32 (*dev_close) (struct PlcHardwareDevice *dev); - uint32 (*dev_write) (struct PlcHardwareDevice *dev, struct PlcDataStandard *msg); - uint32 (*dev_read) (struct PlcHardwareDevice *dev, struct PlcDataStandard *msg); +enum PlcCtlType { + PLC_CTRL_TYPE_HSC, + PLC_CTRL_TYPE_PID, + PLC_CTRL_TYPE_PHASING }; -struct PlcHardwareDevice + +#define PLC_ABILITY_HSC ((uint32_t)(1 << PLC_CTRL_TYPE_HSC)) +#define PLC_ABILITY_PID ((uint32_t)(1 << PLC_CTRL_TYPE_PID)) +#define PLC_ABILITY_PHASING ((uint32_t)(1 << PLC_CTRL_TYPE_PHASING)) + + +enum PlcIndHybridNet { - struct HardwareDev haldev; - PlcDeviceParam plc_param; + // PLC Field Bus + PLC_IND_FIELD_MODBUS_485, + PLC_IND_FIELD_PROFIBUS, + PLC_IND_FIELD_CANOPEN, + PLC_IND_FIELD_DEVICENET, + PLC_IND_FIELD_CONTROLNET, - x_bool plc_dev_flag; + // PLC ETHERNET + PLC_IND_ENET_ETHERNET_IP, + PLC_IND_ENET_PROFINET, + PLC_IND_ENET_ETHERCAT, + PLC_IND_ENET_SERCOS, + PLC_IND_ENET_OPCUA, - const struct PlcDevDone *plc_dev_done; - - void *private_data; + // PLC wireless net + PLC_IND_WIRELESS }; -/*Register the plc device*/ -int PlcHardwareDevRegister(struct PlcHardwareDevice *plc_device, void *plc_param, const char *device_name); +enum PlcTransType +{ + PLC_TRANS_TCP, + PLC_TRANS_UDP, + PLC_TRANS_SERIAL +}; -/*Register the plc device to the plc bus*/ -int PlcHardwareDeviceAttachToBus(const char *dev_name, const char *bus_name); +//communication interface +struct PlcInterface +{ + char ip_addr[IP_ADDR_SIZE]; + char attrib; +}; -/*Find the register plc device*/ -HardwareDevType PlcHardwareDevFind(const char *dev_name, enum DevType dev_type); +// identify PLC device +struct PlcDevice { + struct HardwareDev haldev; /* hardware device driver for bus */ + char name[PLC_NAME_SIZE]; /* name of the device */ + enum PlcCtlType type; /* PLC Control Type */ + enum DevState state; + enum PlcIndHybridNet net; + enum PlcTransType trans; -/*Configure the cs pin of plc dev*/ -int PlcHardwareDevConfigureCs(struct HardwareDev *dev, uint8 plc_chip_select, uint8 plc_cs_release); + struct PlcInfo info;/* Plc info, such as vendor name and model name */ + union PlcCfg cfg; + struct PlcOps *ops; /* filesystem-like APIs for data transferring */ + struct PlcInterface interface; /* protocols used for transferring data from program to plc */ -#ifdef __cplusplus -} -#endif + void *priv_data; /* private data for different PLC*/ + DoubleLinklistType link;/* link list node */ +}; + + +#define plc_print KPrintf + +/******************************************************************************/ + +int PlcDevRegister(struct PlcDevice *plc_device, void *plc_param, const char *device_name); +int PlcDeviceAttachToBus(const char *dev_name, const char *bus_name); + +/******************************************************************************/ #endif diff --git a/APP_Framework/Framework/control/plc/shared/plc_drv.c b/APP_Framework/Framework/control/plc/shared/plc_drv.c index 3dee75a2f..129ba1b61 100755 --- a/APP_Framework/Framework/control/plc/shared/plc_drv.c +++ b/APP_Framework/Framework/control/plc/shared/plc_drv.c @@ -24,6 +24,8 @@ static DoubleLinklistType plcdrv_linklist; +/******************************************************************************/ + /*Create the driver linklist*/ static void PlcDrvLinkInit() { diff --git a/Ubiquitous/XiUOS/resources/ethernet/cmd_lwip/lwip_config_demo.c b/Ubiquitous/XiUOS/resources/ethernet/cmd_lwip/lwip_config_demo.c index 682288efc..87e79fd8e 100755 --- a/Ubiquitous/XiUOS/resources/ethernet/cmd_lwip/lwip_config_demo.c +++ b/Ubiquitous/XiUOS/resources/ethernet/cmd_lwip/lwip_config_demo.c @@ -18,11 +18,6 @@ * @date 2021.12.15 */ - -/******************************************************************************* - * Includes - ******************************************************************************/ - #include "lwip/opt.h" #if LWIP_IPV4 && LWIP_RAW @@ -41,21 +36,7 @@ #include #include "connect_ethernet.h" -/******************************************************************************* - * Definitions - ******************************************************************************/ - -/******************************************************************************* - * Prototypes - ******************************************************************************/ - -/******************************************************************************* - * Variables - ******************************************************************************/ - -/******************************************************************************* - * Code - ******************************************************************************/ +/******************************************************************************/ static void *LwipSetIPTask(void *param) { diff --git a/Ubiquitous/XiUOS/resources/ethernet/cmd_lwip/lwip_dhcp_demo.c b/Ubiquitous/XiUOS/resources/ethernet/cmd_lwip/lwip_dhcp_demo.c index 108a20841..cfeff2499 100755 --- a/Ubiquitous/XiUOS/resources/ethernet/cmd_lwip/lwip_dhcp_demo.c +++ b/Ubiquitous/XiUOS/resources/ethernet/cmd_lwip/lwip_dhcp_demo.c @@ -39,23 +39,9 @@ #include "fsl_iomuxc.h" #include "sys_arch.h" -/******************************************************************************* - * Definitions - ******************************************************************************/ - #define LWIP_DHCP_TIME 10000 // 10s -/******************************************************************************* - * Prototypes - ******************************************************************************/ - -/******************************************************************************* - * Variables - ******************************************************************************/ - -/******************************************************************************* - * Code - ******************************************************************************/ +/******************************************************************************/ /*! * @brief Prints DHCP status of the interface when it has changed from last status. diff --git a/Ubiquitous/XiUOS/resources/ethernet/cmd_lwip/lwip_ping_demo.c b/Ubiquitous/XiUOS/resources/ethernet/cmd_lwip/lwip_ping_demo.c index 13480bdfe..f99e8b380 100755 --- a/Ubiquitous/XiUOS/resources/ethernet/cmd_lwip/lwip_ping_demo.c +++ b/Ubiquitous/XiUOS/resources/ethernet/cmd_lwip/lwip_ping_demo.c @@ -18,10 +18,6 @@ * @date 2021.12.15 */ -/******************************************************************************* - * Includes - ******************************************************************************/ - #include "lwip/opt.h" #if LWIP_IPV4 && LWIP_RAW @@ -39,26 +35,12 @@ #include #include "connect_ethernet.h" -/******************************************************************************* - * Definitions - ******************************************************************************/ - -/******************************************************************************* - * Prototypes - ******************************************************************************/ - -/******************************************************************************* - * Variables - ******************************************************************************/ - char test_ip_addr[] = {192, 168, 250, 253}; char test_net_mask[] = {255, 255, 255, 0}; char test_gw_addr[] = {192, 168, 250, 252}; ip4_addr_t ping_addr; -/******************************************************************************* - * Code - ******************************************************************************/ +/******************************************************************************/ void LwipPingTest(int argc, char *argv[]) { diff --git a/Ubiquitous/XiUOS/resources/ethernet/cmd_lwip/lwip_tcp_demo.c b/Ubiquitous/XiUOS/resources/ethernet/cmd_lwip/lwip_tcp_demo.c index 3fa2763e6..1e882f253 100755 --- a/Ubiquitous/XiUOS/resources/ethernet/cmd_lwip/lwip_tcp_demo.c +++ b/Ubiquitous/XiUOS/resources/ethernet/cmd_lwip/lwip_tcp_demo.c @@ -24,27 +24,13 @@ #include "lwip/sys.h" #include "tcpecho_raw.h" -/******************************************************************************* - * Definitions - ******************************************************************************/ - #define MSG_SIZE 128 -/******************************************************************************* - * Prototypes - ******************************************************************************/ - -/******************************************************************************* - * Variables - ******************************************************************************/ - // this is for test in shell, in fact, shell restrict the length of input string, which is less then 128 char tcp_send_msg[MSG_SIZE] = {0}; char tcp_target[] = {192, 168, 250, 252}; -/******************************************************************************* - * Code - ******************************************************************************/ +/******************************************************************************/ static void LwipTcpSendTask(void *arg) { diff --git a/Ubiquitous/XiUOS/resources/ethernet/cmd_lwip/lwip_udp_demo.c b/Ubiquitous/XiUOS/resources/ethernet/cmd_lwip/lwip_udp_demo.c index ba9cf501b..5b2d4e3bc 100755 --- a/Ubiquitous/XiUOS/resources/ethernet/cmd_lwip/lwip_udp_demo.c +++ b/Ubiquitous/XiUOS/resources/ethernet/cmd_lwip/lwip_udp_demo.c @@ -24,31 +24,16 @@ #include #include "lwip/sys.h" - -/******************************************************************************* - * Definitions - ******************************************************************************/ - #define UDP_TASK_STACK_SIZE 4096 #define UDP_TASK_PRIO 15 #define PBUF_SIZE 27 -/******************************************************************************* - * Prototypes - ******************************************************************************/ - -/******************************************************************************* - * Variables - ******************************************************************************/ - static struct udp_pcb *udpecho_raw_pcb; char udp_target[] = {192, 168, 250, 252}; char hello_str[] = {"hello world\r\n"}; char udp_send_msg[] = "\n\nThis one is UDP pkg. Congratulations on you.\n\n"; -/******************************************************************************* - * Code - ******************************************************************************/ +/******************************************************************************/ static void LwipUDPSendTask(void *arg) { diff --git a/Ubiquitous/XiUOS/tool/shell/letter-shell/cmd.c b/Ubiquitous/XiUOS/tool/shell/letter-shell/cmd.c index 008236ded..6acf8ed01 100644 --- a/Ubiquitous/XiUOS/tool/shell/letter-shell/cmd.c +++ b/Ubiquitous/XiUOS/tool/shell/letter-shell/cmd.c @@ -384,6 +384,7 @@ static char *const bus_type_str[] = "SERIAL_BUS", "ADC_BUS", "DAC_BUS", + "PLC_BUS", "Unknown" };