diff --git a/APP_Framework/Framework/connection/4g/adapter_4g.c b/APP_Framework/Framework/connection/4g/adapter_4g.c index ab288a654..60272c666 100644 --- a/APP_Framework/Framework/connection/4g/adapter_4g.c +++ b/APP_Framework/Framework/connection/4g/adapter_4g.c @@ -24,8 +24,6 @@ extern AdapterProductInfoType Ec200tAttach(struct Adapter *adapter); #endif -#define ADAPTER_4G_NAME "4G" - static int Adapter4GRegister(struct Adapter *adapter) { int ret = 0; diff --git a/APP_Framework/Framework/connection/adapter.h b/APP_Framework/Framework/connection/adapter.h index a70c1e6e9..aefb0df1f 100644 --- a/APP_Framework/Framework/connection/adapter.h +++ b/APP_Framework/Framework/connection/adapter.h @@ -45,6 +45,15 @@ extern "C" { #define ADAPTER_DEBUG #endif +#define ADAPTER_4G_NAME "4G" +#define ADAPTER_BLUETOOTH_NAME "bluetooth" +#define ADAPTER_ETHERNET_NAME "ethernet" +#define ADAPTER_ETHERCAT_NAME "ethercat" +#define ADAPTER_LORA_NAME "lora" +#define ADAPTER_NBIOT_NAME "nbiot" +#define ADAPTER_WIFI_NAME "wifi" +#define ADAPTER_ZIGBEE_NAME "zigbee" + struct Adapter; struct AdapterProductInfo; typedef struct Adapter *AdapterType; @@ -108,6 +117,12 @@ enum IpType IPV6, }; +struct AdapterData +{ + uint32 len; + uint8 *buffer; +}; + struct AdapterProductInfo { uint32_t functions; diff --git a/APP_Framework/Framework/connection/bluetooth/adapter_bluetooth.c b/APP_Framework/Framework/connection/bluetooth/adapter_bluetooth.c index ace054b49..b8cc0694e 100644 --- a/APP_Framework/Framework/connection/bluetooth/adapter_bluetooth.c +++ b/APP_Framework/Framework/connection/bluetooth/adapter_bluetooth.c @@ -24,8 +24,6 @@ extern AdapterProductInfoType Hc08Attach(struct Adapter *adapter); #endif -#define ADAPTER_BLUETOOTH_NAME "bluetooth" - static int AdapterBlueToothRegister(struct Adapter *adapter) { int ret = 0; diff --git a/APP_Framework/Framework/connection/ethernet/adapter_ethernet.c b/APP_Framework/Framework/connection/ethernet/adapter_ethernet.c index 654166ac7..239472e38 100644 --- a/APP_Framework/Framework/connection/ethernet/adapter_ethernet.c +++ b/APP_Framework/Framework/connection/ethernet/adapter_ethernet.c @@ -24,8 +24,6 @@ extern AdapterProductInfoType Hfa21EthernetAttach(struct Adapter *adapter); #endif -#define ADAPTER_ETHERNET_NAME "ethernet" - static int AdapterEthernetRegister(struct Adapter *adapter) { int ret = 0; diff --git a/APP_Framework/Framework/connection/industrial_ethernet/ethercat/adapter_ethercat.c b/APP_Framework/Framework/connection/industrial_ethernet/ethercat/adapter_ethercat.c index 640151505..05c4b3c40 100644 --- a/APP_Framework/Framework/connection/industrial_ethernet/ethercat/adapter_ethercat.c +++ b/APP_Framework/Framework/connection/industrial_ethernet/ethercat/adapter_ethercat.c @@ -25,8 +25,6 @@ extern AdapterProductInfoType Hfa21EthercatAttach(struct Adapter *adapter); #endif -#define ADAPTER_ETHERCAT_NAME "ethercat" - /** * @description: clear the datagram in ethercat frame, do not free the frame itself, since it may be a memory space on stack * @param frame - ethercat frame pointer diff --git a/APP_Framework/Framework/connection/industrial_ethernet/ethercat/hfa21_ethercat/hfa21_ethercat.c b/APP_Framework/Framework/connection/industrial_ethernet/ethercat/hfa21_ethercat/hfa21_ethercat.c index 820f391c2..4e6cf11e2 100644 --- a/APP_Framework/Framework/connection/industrial_ethernet/ethercat/hfa21_ethercat/hfa21_ethercat.c +++ b/APP_Framework/Framework/connection/industrial_ethernet/ethercat/hfa21_ethercat/hfa21_ethercat.c @@ -26,7 +26,7 @@ // therefore, only the TCP/UDP datagrams are considered, // here EtherCAT is in fact an application layer protocol. -#define ADAPTER_ETHERNET_NAME "ethernet" //"wifi" +// #define ADAPTER_ETHERNET_NAME "ethernet" //"wifi" EcatFrame ecat_data; static struct Adapter *ethernet; uint32_t self_address; diff --git a/APP_Framework/Framework/connection/lora/Kconfig b/APP_Framework/Framework/connection/lora/Kconfig index fd8c8fda9..7dd6b6c43 100644 --- a/APP_Framework/Framework/connection/lora/Kconfig +++ b/APP_Framework/Framework/connection/lora/Kconfig @@ -1,6 +1,10 @@ config ADAPTER_SX1278 bool "Using lora adapter device SX1278" - default y + default n + +config ADAPTER_E220 + bool "Using lora adapter device E220-400T22S" + default n choice prompt "Lora device adapter select net role type " @@ -15,16 +19,20 @@ endchoice if AS_LORA_GATEWAY_ROLE config ADAPTER_LORA_NET_ROLE_ID - hex "if Lora device config as a gateway, set gateway net id" - default "0x10" + hex "if Lora device config as a gateway, set gateway net id" + default "0xFF" endif if AS_LORA_CLIENT_ROLE config ADAPTER_LORA_NET_ROLE_ID - hex "if Lora device config as a client, set client net id" - default "0x01" + hex "if Lora device config as a client, set client net id" + default "0x01" endif if ADAPTER_SX1278 source "$APP_DIR/Framework/connection/lora/sx1278/Kconfig" endif + +if ADAPTER_E220 + source "$APP_DIR/Framework/connection/lora/e220/Kconfig" +endif diff --git a/APP_Framework/Framework/connection/lora/Makefile b/APP_Framework/Framework/connection/lora/Makefile index 9fb8f1a2f..e1b055274 100644 --- a/APP_Framework/Framework/connection/lora/Makefile +++ b/APP_Framework/Framework/connection/lora/Makefile @@ -8,9 +8,14 @@ endif ifeq ($(CONFIG_ADD_XIZI_FETURES),y) SRC_FILES := adapter_lora.c + ifeq ($(CONFIG_ADAPTER_SX1278),y) SRC_DIR += sx1278 endif + + ifeq ($(CONFIG_ADAPTER_E220),y) + SRC_DIR += e220 + endif include $(KERNEL_ROOT)/compiler.mk endif diff --git a/APP_Framework/Framework/connection/lora/adapter_lora.c b/APP_Framework/Framework/connection/lora/adapter_lora.c index a91e0fdec..397e89e26 100644 --- a/APP_Framework/Framework/connection/lora/adapter_lora.c +++ b/APP_Framework/Framework/connection/lora/adapter_lora.c @@ -24,10 +24,13 @@ extern AdapterProductInfoType Sx1278Attach(struct Adapter *adapter); #endif -#define ADAPTER_LORA_NAME "lora" -#define ADAPTER_LORA_CLIENT_NUM 6 -#define ADAPTER_LORA_DATA_LENGTH 128 -#define ADAPTER_LORA_RECV_DATA_LENGTH 256 +#ifdef ADAPTER_E220 +extern AdapterProductInfoType E220Attach(struct Adapter *adapter); +#endif + +#define ADAPTER_LORA_CLIENT_NUM 255 +#define ADAPTER_LORA_DATA_LENGTH 256 +#define ADAPTER_LORA_RECV_DATA_LENGTH ADAPTER_LORA_DATA_LENGTH + 16 #define ADAPTER_LORA_DATA_HEAD 0x3C #define ADAPTER_LORA_NET_PANID 0x0102 @@ -164,13 +167,7 @@ static int LoraReceiveDataCheck(uint8 *data, uint16 length, struct LoraDataForma uint32 recv_data_length = 0; for ( i = 0; i < length; i ++) { if (ADAPTER_LORA_DATA_HEAD == data[i]) { -#ifdef ADD_NUTTX_FETURES - /*Big-Endian*/ recv_data_length = (data[i + 4] & 0xFF) | ((data[i + 5] & 0xFF) << 8) | ((data[i + 6] & 0xFF) << 16) | ((data[i + 7] & 0xFF) << 24); -#else - /*Little-Endian*/ - recv_data_length = ((data[i + 4] & 0xFF) << 24) | ((data[i + 5] & 0xFF) << 16) | ((data[i + 6] & 0xFF) << 8) | (data[i + 7] & 0xFF); -#endif if (sizeof(struct LoraDataFormat) == recv_data_length) { memcpy(recv_data, (uint8 *)(data + i), sizeof(struct LoraDataFormat)); return 0; @@ -422,7 +419,7 @@ static int LoraClientDataAnalyze(struct Adapter *adapter, void *send_buf, int le LoraReceiveDataCheck(lora_recv_data, ADAPTER_LORA_RECV_DATA_LENGTH, client_recv_data); - printf("%s:client_recv_data\n",__func__); + printf("%s:client_recv_data\n", __func__); printf("head 0x%x length %d panid 0x%x data_type 0x%x client_id 0x%x gateway_id 0x%x crc 0x%x\n", client_recv_data->flame_head, client_recv_data->length, client_recv_data->panid, client_recv_data->data_type, client_recv_data->client_id, client_recv_data->gateway_id, client_recv_data->crc16); @@ -466,6 +463,7 @@ static int LoraClientDataAnalyze(struct Adapter *adapter, void *send_buf, int le static int LoraClientJoinNet(struct Adapter *adapter, unsigned short panid) { struct LoraDataFormat client_join_data; + struct AdapterData priv_lora_net; memset(&client_join_data, 0, sizeof(struct LoraDataFormat)); @@ -476,12 +474,15 @@ static int LoraClientJoinNet(struct Adapter *adapter, unsigned short panid) client_join_data.client_id = adapter->net_role_id; client_join_data.crc16 = LoraCrc16((uint8 *)&client_join_data, sizeof(struct LoraDataFormat) - 2); - printf("%s:client_join_data\n",__func__); + printf("%s:client_join_data\n", __func__); printf("head 0x%x length %d panid 0x%x data_type 0x%x client_id 0x%x gateway_id 0x%x crc 0x%x\n", client_join_data.flame_head, client_join_data.length, client_join_data.panid, client_join_data.data_type, client_join_data.client_id, client_join_data.gateway_id, client_join_data.crc16); - if (AdapterDeviceJoin(adapter, (uint8 *)&client_join_data) < 0) { + priv_lora_net.len = sizeof(struct LoraDataFormat); + priv_lora_net.buffer = (uint8 *)&client_join_data; + + if (AdapterDeviceJoin(adapter, (uint8 *)&priv_lora_net) < 0) { return -1; } @@ -495,18 +496,22 @@ static int LoraClientJoinNet(struct Adapter *adapter, unsigned short panid) */ static int LoraClientQuitNet(struct Adapter *adapter, unsigned short panid) { - struct LoraDataFormat client_join_data; + struct LoraDataFormat client_quit_data; + struct AdapterData priv_lora_net; - memset(&client_join_data, 0, sizeof(struct LoraDataFormat)); + memset(&client_quit_data, 0, sizeof(struct LoraDataFormat)); - client_join_data.flame_head = ADAPTER_LORA_DATA_HEAD; - client_join_data.length = sizeof(struct LoraDataFormat); - client_join_data.panid = panid; - client_join_data.data_type = ADAPTER_LORA_DATA_TYPE_QUIT; - client_join_data.client_id = adapter->net_role_id; - client_join_data.crc16 = LoraCrc16((uint8 *)&client_join_data, sizeof(struct LoraDataFormat) - 2); + client_quit_data.flame_head = ADAPTER_LORA_DATA_HEAD; + client_quit_data.length = sizeof(struct LoraDataFormat); + client_quit_data.panid = panid; + client_quit_data.data_type = ADAPTER_LORA_DATA_TYPE_QUIT; + client_quit_data.client_id = adapter->net_role_id; + client_quit_data.crc16 = LoraCrc16((uint8 *)&client_quit_data, sizeof(struct LoraDataFormat) - 2); - if (AdapterDeviceDisconnect(adapter, (uint8 *)&client_join_data) < 0) { + priv_lora_net.len = sizeof(struct LoraDataFormat); + priv_lora_net.buffer = (uint8 *)&client_quit_data; + + if (AdapterDeviceDisconnect(adapter, (uint8 *)&priv_lora_net) < 0) { return -1; } @@ -620,7 +625,6 @@ static void *LoraGatewayTask(void *parameter) static void *LoraClientDataTask(void *parameter) { int i, ret = 0; - int join_times = 10; struct Adapter *lora_adapter = (struct Adapter *)parameter; struct LoraClientParam *client = (struct LoraClientParam *)lora_adapter->adapter_param; @@ -786,6 +790,19 @@ int AdapterLoraInit(void) #endif +#ifdef ADAPTER_E220 + AdapterProductInfoType product_info = E220Attach(adapter); + if (!product_info) { + printf("AdapterLoraInit e220 attach error\n"); + PrivFree(adapter); + return -1; + } + + adapter->product_info_flag = 1; + adapter->info = product_info; + adapter->done = product_info->model_done; +#endif + return ret; } diff --git a/APP_Framework/Framework/connection/lora/e220/Kconfig b/APP_Framework/Framework/connection/lora/e220/Kconfig new file mode 100644 index 000000000..050a8ea9b --- /dev/null +++ b/APP_Framework/Framework/connection/lora/e220/Kconfig @@ -0,0 +1,44 @@ +config ADAPTER_LORA_E220 + string "E220-400T22S adapter name" + default "e220" + +if ADD_XIZI_FETURES + config ADAPTER_E220_M0 + int "E220 M0 pin number" + default "11" + + config ADAPTER_E220_M1 + int "E220 M1 pin number" + default "9" + + config ADAPTER_E220_PIN_DRIVER + string "E220 device pin driver path" + default "/dev/pin_dev" + + config ADAPTER_E220_DRIVER_EXTUART + bool "Using extra uart to support lora" + default y + + config ADAPTER_E220_DRIVER + string "E220 device uart driver path" + default "/dev/uart1_dev1" + depends on !ADAPTER_E220_DRIVER_EXTUART + + if ADAPTER_E220_DRIVER_EXTUART + config ADAPTER_E220_DRIVER + string "E220 device extra uart driver path" + default "/dev/extuart_dev3" + + config ADAPTER_E220_DRIVER_EXT_PORT + int "if E220 device using extuart, choose port" + default "3" + endif +endif + +if ADD_NUTTX_FETURES + +endif + +if ADD_RTTHREAD_FETURES + +endif diff --git a/APP_Framework/Framework/connection/lora/e220/Makefile b/APP_Framework/Framework/connection/lora/e220/Makefile new file mode 100644 index 000000000..734805688 --- /dev/null +++ b/APP_Framework/Framework/connection/lora/e220/Makefile @@ -0,0 +1,3 @@ +SRC_FILES := e220.c + +include $(KERNEL_ROOT)/compiler.mk diff --git a/APP_Framework/Framework/connection/lora/e220/e220.c b/APP_Framework/Framework/connection/lora/e220/e220.c new file mode 100644 index 000000000..3a028817e --- /dev/null +++ b/APP_Framework/Framework/connection/lora/e220/e220.c @@ -0,0 +1,536 @@ +/* +* Copyright (c) 2020 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 e220.c + * @brief Implement the connection E220-400T22S lora adapter function + * @version 2.0 + * @author AIIT XUOS Lab + * @date 2022.4.20 + */ + +#include + +#define E220_GATEWAY_ADDRESS 0xFFFF +#define E220_CHANNEL 0x04 + +#ifdef AS_LORA_GATEWAY_ROLE +#define E220_ADDRESS E220_GATEWAY_ADDRESS +#endif + +#ifdef AS_LORA_CLIENT_ROLE +#define E220_ADDRESS 0xFFFF +#endif + +#define E220_UART_BAUD_RATE 9600 + +enum E220LoraMode +{ + DATA_TRANSFER_MODE = 0, //M1 : M0 = 0 : 0 + WOR_SEND_MODE, //M1 : M0 = 0 : 1 + WOR_RECEIVE_MODE, //M1 : M0 = 1 : 0 + CONFIGURE_MODE_MODE, //M1 : M0 = 1 : 1 +}; + +/** + * @description: Config E220 work mode by set M1/M0 pin + * @param mode Lora working mode + * @return NULL + */ +static void E220LoraModeConfig(enum E220LoraMode mode) +{ + //delay 1s , wait AUX ready + PrivTaskDelay(1000); + + int pin_fd; + pin_fd = PrivOpen(ADAPTER_E220_PIN_DRIVER, O_RDWR); + if (pin_fd < 0) { + printf("open %s error\n", ADAPTER_E220_PIN_DRIVER); + return; + } + + //Step1: config M0 and M1 GPIO + struct PinParam pin_param; + pin_param.cmd = GPIO_CONFIG_MODE; + pin_param.mode = GPIO_CFG_OUTPUT; + pin_param.pin = ADAPTER_E220_M0; + + struct PrivIoctlCfg ioctl_cfg; + ioctl_cfg.ioctl_driver_type = PIN_TYPE; + ioctl_cfg.args = &pin_param; + PrivIoctl(pin_fd, OPE_CFG, &ioctl_cfg); + + pin_param.pin = ADAPTER_E220_M1; + ioctl_cfg.args = &pin_param; + PrivIoctl(pin_fd, OPE_CFG, &ioctl_cfg); + + //Step2 : set M0 and M1 high or low + struct PinStat pin_stat; + + switch (mode) + { + case DATA_TRANSFER_MODE: + pin_stat.pin = ADAPTER_E220_M1; + pin_stat.val = GPIO_LOW; + PrivWrite(pin_fd, &pin_stat, 1); + + pin_stat.pin = ADAPTER_E220_M0; + pin_stat.val = GPIO_LOW; + PrivWrite(pin_fd, &pin_stat, 1); + break; + + case WOR_SEND_MODE: + pin_stat.pin = ADAPTER_E220_M1; + pin_stat.val = GPIO_LOW; + PrivWrite(pin_fd, &pin_stat, 1); + + pin_stat.pin = ADAPTER_E220_M0; + pin_stat.val = GPIO_HIGH; + PrivWrite(pin_fd, &pin_stat, 1); + break; + + case WOR_RECEIVE_MODE: + pin_stat.pin = ADAPTER_E220_M1; + pin_stat.val = GPIO_HIGH; + PrivWrite(pin_fd, &pin_stat, 1); + + pin_stat.pin = ADAPTER_E220_M0; + pin_stat.val = GPIO_LOW; + PrivWrite(pin_fd, &pin_stat, 1); + break; + + case CONFIGURE_MODE_MODE: + pin_stat.pin = ADAPTER_E220_M1; + pin_stat.val = GPIO_HIGH; + PrivWrite(pin_fd, &pin_stat, 1); + + pin_stat.pin = ADAPTER_E220_M0; + pin_stat.val = GPIO_HIGH; + PrivWrite(pin_fd, &pin_stat, 1); + break; + default: + break; + } + + PrivClose(pin_fd); + + //delay 20ms , wait mode switch done + PrivTaskDelay(20); +} + +/** + * @description: Switch baud rate to register bit + * @param baud_rate - baud_rate + * @return baud_rate_bit + */ +static uint8 E220BaudRateSwitch(uint32 baud_rate) +{ + uint8 baud_rate_bit; + + switch (baud_rate) + { + case 1200: + baud_rate_bit = 0x0; + break; + + case 2400: + baud_rate_bit = 0x1; + break; + + case 4800: + baud_rate_bit = 0x2; + break; + + case 9600: + baud_rate_bit = 0x3; + break; + + case 19200: + baud_rate_bit = 0x4; + break; + + case 38400: + baud_rate_bit = 0x5; + break; + + case 57600: + baud_rate_bit = 0x6; + break; + + case 115200: + baud_rate_bit = 0x7; + break; + + default: + break; + } + + return baud_rate_bit; +} + +/** + * @description: Set E220 register, such as address、channel、baud rate... + * @param adapter - lora adapter + * @param address - address + * @param channel - channel + * @param baud_rate - baud_rate + * @return success: 0, failure: -1 + */ +static int E220SetRegisterParam(struct Adapter *adapter, uint16 address, uint8 channel, uint32 baud_rate) +{ + int ret; + uint8 buffer[50] = {0}; + uint8 baud_rate_bit = E220BaudRateSwitch(baud_rate); + + E220LoraModeConfig(CONFIGURE_MODE_MODE); + PrivTaskDelay(30); + + buffer[0] = 0xC0; //write register order + buffer[1] = 0x00; //register start-address + buffer[2] = 0x08; //register length + + buffer[3] = (address >> 8) & 0xFF; //high address + buffer[4] = address & 0xFF; //low adderss + + buffer[5] = ((baud_rate_bit << 5) & 0xE0) | 0x04; + + buffer[6] = 0x00; + buffer[7] = channel; //channel + buffer[8] = 0x03; + buffer[9] = 0; //high-cipher + buffer[10] = 0; //low-cipher + + ret = PrivWrite(adapter->fd, (void *)buffer, 11); + if(ret < 0){ + printf("E220SetRegisterParam send failed %d!\n", ret); + } + + PrivRead(adapter->fd, buffer, 11); + + E220LoraModeConfig(DATA_TRANSFER_MODE); + PrivTaskDelay(1000); + + return 0; +} + +/** + * @description: Get E220 register, such as address、channel、baud rate... + * @param buf - data buf + * @return success: 0, failure: -1 + */ +static int E220GetRegisterParam(uint8 *buf) +{ + int ret; + uint8 buffer[3] = {0}; + + struct Adapter *adapter = AdapterDeviceFindByName(ADAPTER_LORA_NAME); + if (NULL == adapter) { + printf("E220GetRegisterParam find lora adapter error\n"); + return -1; + } + + E220LoraModeConfig(CONFIGURE_MODE_MODE); + PrivTaskDelay(30); + + buffer[0] = 0xC1; //read register order + buffer[1] = 0x00; //register start-address + buffer[2] = 0x08; //register length + + ret = PrivWrite(adapter->fd, (void *)buffer, 3); + if(ret < 0){ + printf("E220GetRegisterParam send failed %d!\n", ret); + } + + PrivRead(adapter->fd, buf, 11); + + E220LoraModeConfig(DATA_TRANSFER_MODE); + PrivTaskDelay(30); + + return 0; +} + +/** + * @description: Open E220 uart function + * @param adapter - Lora device pointer + * @return success: 0, failure: -1 + */ +static int E220Open(struct Adapter *adapter) +{ + /*step1: open e220 uart port*/ + adapter->fd = PrivOpen(ADAPTER_E220_DRIVER, O_RDWR); + if (adapter->fd < 0) { + printf("E220Open get uart %s fd error\n", ADAPTER_E220_DRIVER); + return -1; + } + + struct SerialDataCfg cfg; + memset(&cfg, 0 ,sizeof(struct SerialDataCfg)); + + cfg.serial_baud_rate = BAUD_RATE_9600; + cfg.serial_data_bits = DATA_BITS_8; + cfg.serial_stop_bits = STOP_BITS_1; + cfg.serial_parity_mode = PARITY_NONE; + cfg.serial_bit_order = BIT_ORDER_LSB; + cfg.serial_invert_mode = NRZ_NORMAL; + cfg.serial_buffer_size = SERIAL_RB_BUFSZ; + + /*aiit board use ch438, so it needs more serial configuration*/ +#ifdef ADAPTER_E220_DRIVER_EXTUART + cfg.ext_uart_no = ADAPTER_E220_DRIVER_EXT_PORT; + cfg.port_configure = PORT_CFG_INIT; +#endif + + struct PrivIoctlCfg ioctl_cfg; + ioctl_cfg.ioctl_driver_type = SERIAL_TYPE; + ioctl_cfg.args = &cfg; + + PrivIoctl(adapter->fd, OPE_INT, &ioctl_cfg); + + E220SetRegisterParam(adapter, E220_ADDRESS, E220_CHANNEL, E220_UART_BAUD_RATE); + + ADAPTER_DEBUG("E220Open done\n"); + + return 0; +} + +/** + * @description: Close E220 uart function + * @param adapter - Lora device pointer + * @return success: 0, failure: -1 + */ +static int E220Close(struct Adapter *adapter) +{ + /*step1: close e220 uart port*/ + int ret; + ret = PrivClose(adapter->fd); + if(ret < 0) { + printf("E220 close failed: %d!\n", ret); + return -1; + } + + ADAPTER_DEBUG("E220 Close done\n"); + + return 0; +} + +/** + * @description: E220 ioctl function + * @param adapter - Lora device pointer + * @param cmd - ioctl cmd + * @param args - iotl params + * @return success: 0, failure: -1 + */ +static int E220Ioctl(struct Adapter *adapter, int cmd, void *args) +{ + /*to do*/ + return 0; +} + +/** + * @description: E220 join lora net group function + * @param adapter - Lora device pointer + * @param priv_net_group - priv_net_group params + * @return success: 0, failure: -1 + */ +static int E220Join(struct Adapter *adapter, unsigned char *priv_net_group) +{ + int ret; + struct AdapterData *priv_net_group_data = (struct AdapterData *)priv_net_group; + + ret = PrivWrite(adapter->fd, (void *)priv_net_group_data->buffer, priv_net_group_data->len); + if(ret < 0) { + printf("E220 Join net group failed: %d!\n", ret); + } + + return ret; +} + +/** + * @description: E220 send data function + * @param adapter - Lora device pointer + * @param buf - data buffers + * @param len - data len + * @return success: 0, failure: -1 + */ +static int E220Send(struct Adapter *adapter, const void *buf, size_t len) +{ + int ret; + + ret = PrivWrite(adapter->fd, (void *)buf, len); + if(ret < 0){ + printf("send failed %d!\n", ret); + } + + return ret; +} + +/** + * @description: E220 receive data function + * @param adapter - Lora device pointer + * @param buf - data buffers + * @param len - data len + * @return success: 0, failure: -1 + */ +static int E220Recv(struct Adapter *adapter, void *buf, size_t len) +{ + int recv_len, recv_len_continue; + + uint8 *recv_buf = PrivMalloc(len); + + recv_len = PrivRead(adapter->fd, recv_buf, len); + while (recv_len < len) { + recv_len_continue = PrivRead(adapter->fd, recv_buf + recv_len, len - recv_len); + + recv_len += recv_len_continue; + } + + memcpy(buf, recv_buf, recv_len); + + PrivFree(recv_buf); + + return recv_len; +} + +/** + * @description: E220 quit lora net group function + * @param adapter - Lora device pointer + * @param priv_net_group - priv_net_group params + * @return success: 0, failure: -1 + */ +static int E220Quit(struct Adapter *adapter, unsigned char *priv_net_group) +{ + int ret; + + struct AdapterData *priv_net_group_data = (struct AdapterData *)priv_net_group; + + ret = PrivWrite(adapter->fd, (void *)priv_net_group_data->buffer, priv_net_group_data->len); + if(ret < 0){ + printf("E220 quit net group failed %d!\n", ret); + } + + return ret; +} + +static const struct PrivProtocolDone e220_done = +{ + .open = E220Open, + .close = E220Close, + .ioctl = E220Ioctl, + .setup = NULL, + .setdown = NULL, + .setaddr = NULL, + .setdns = NULL, + .setdhcp = NULL, + .ping = NULL, + .netstat = NULL, + .join = E220Join, + .send = E220Send, + .recv = E220Recv, + .quit = E220Quit, +}; + +AdapterProductInfoType E220Attach(struct Adapter *adapter) +{ + struct AdapterProductInfo *product_info = malloc(sizeof(struct AdapterProductInfo)); + if (!product_info) { + printf("E220Attach malloc product_info error\n"); + return NULL; + } + + strncpy(product_info->model_name, ADAPTER_LORA_E220,sizeof(product_info->model_name)); + product_info->model_done = (void *)&e220_done; + + return product_info; +} + +//###################TEST#################### +static void LoraOpen(void) +{ + struct Adapter *adapter = AdapterDeviceFindByName(ADAPTER_LORA_NAME); + if (NULL == adapter) { + printf("LoraReceive find lora adapter error\n"); + return; + } + + E220Open(adapter); +} + +static void LoraRead(void *parameter) +{ + int RevLen; + uint8 i, cnt = 0; + + uint8 buffer[256]; + + memset(buffer, 0, 256); + + struct Adapter *adapter = AdapterDeviceFindByName(ADAPTER_LORA_NAME); + if (NULL == adapter) { + printf("LoraRead find lora adapter error\n"); + return; + } + + while (1) + { + KPrintf("ready to read lora data\n"); + + RevLen = E220Recv(adapter, buffer, 256); + if (RevLen) { + KPrintf("lora get data %u\n", RevLen); + for (i = 0; i < RevLen; i ++) { + printf("i %u data 0x%x\n", i, buffer[i]); + } + + memset(buffer, 0, 256); + + PrivTaskDelay(1000); + + cnt ++; + E220Send(adapter, &cnt, 1); + } + } +} + +static void LoraTest(void) +{ + int ret; + + LoraOpen(); + + int task_lora_read = KTaskCreate("task_lora_read", LoraRead, NONE, 2048, 10); + ret = StartupKTask(task_lora_read); + if (ret != EOK) { + KPrintf("StartupKTask task_lora_read failed .\n"); + return; + } +} +SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN), +LoraTest, LoraTest, lora send and receive message); + +static void LoraSend(int argc, char *argv[]) +{ + struct Adapter *adapter = AdapterDeviceFindByName(ADAPTER_LORA_NAME); + if (NULL == adapter) { + printf("LoraRead find lora adapter error\n"); + return; + } + + char Msg[256] = {0}; + + if (argc == 2) { + strncpy(Msg, argv[1], 256); + + E220Send(adapter, Msg, strlen(Msg)); + } +} +SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN), +LoraSend, LoraSend, lora send message); diff --git a/APP_Framework/Framework/connection/lora/sx1278/sx1278.c b/APP_Framework/Framework/connection/lora/sx1278/sx1278.c index 914db8998..dda8b54e0 100644 --- a/APP_Framework/Framework/connection/lora/sx1278/sx1278.c +++ b/APP_Framework/Framework/connection/lora/sx1278/sx1278.c @@ -108,7 +108,9 @@ static int Sx1278Ioctl(struct Adapter *adapter, int cmd, void *args) static int Sx1278Join(struct Adapter *adapter, unsigned char *priv_net_group) { int ret; - ret = Sx127x_Nuttx_Write(adapter->fd, (void *)priv_net_group, 144); + struct AdapterData *priv_net_group_data = (struct AdapterData *)priv_net_group; + + ret = Sx127x_Nuttx_Write(adapter->fd, (void *)priv_net_group_data->buffer, priv_net_group_data->len); if(ret < 0){ printf("Sx1278 Join net group failed: %d!\n", ret); } @@ -119,7 +121,9 @@ static int Sx1278Join(struct Adapter *adapter, unsigned char *priv_net_group) static int Sx1278Join(struct Adapter *adapter, unsigned char *priv_net_group) { int ret; - ret = PrivWrite(adapter->fd, (void *)priv_net_group, 144); + struct AdapterData *priv_net_group_data = (struct AdapterData *)priv_net_group; + + ret = PrivWrite(adapter->fd, (void *)priv_net_group_data->buffer, priv_net_group_data->len); if(ret < 0){ printf("Sx1278 Join net group failed: %d!\n", ret); } @@ -196,7 +200,9 @@ static int Sx1278Recv(struct Adapter *adapter, void *buf, size_t len) static int Sx1278Quit(struct Adapter *adapter, unsigned char *priv_net_group) { int ret; - ret = Sx127x_Nuttx_Write(adapter->fd, (void *)priv_net_group, 144); + struct AdapterData *priv_net_group_data = (struct AdapterData *)priv_net_group; + + ret = Sx127x_Nuttx_Write(adapter->fd, (void *)priv_net_group_data->buffer, priv_net_group_data->len); if(ret < 0){ printf("Sx1278 quit net group failed %d!\n", ret); } @@ -207,7 +213,9 @@ static int Sx1278Quit(struct Adapter *adapter, unsigned char *priv_net_group) static int Sx1278Quit(struct Adapter *adapter, unsigned char *priv_net_group) { int ret; - ret = PrivWrite(adapter->fd, (void *)priv_net_group, 144); + struct AdapterData *priv_net_group_data = (struct AdapterData *)priv_net_group; + + ret = PrivWrite(adapter->fd, (void *)priv_net_group_data->buffer, priv_net_group_data->len); if(ret < 0){ printf("Sx1278 quit net group failed %d!\n", ret); } diff --git a/APP_Framework/Framework/connection/nbiot/adapter_nbiot.c b/APP_Framework/Framework/connection/nbiot/adapter_nbiot.c index 6126a723a..873ff6d1f 100644 --- a/APP_Framework/Framework/connection/nbiot/adapter_nbiot.c +++ b/APP_Framework/Framework/connection/nbiot/adapter_nbiot.c @@ -25,8 +25,6 @@ extern AdapterProductInfoType BC28Attach(struct Adapter *adapter); #endif -#define ADAPTER_NBIOT_NAME "nbiot" - static int AdapterNbiotRegister(struct Adapter *adapter) { int ret = 0; diff --git a/APP_Framework/Framework/connection/wifi/adapter_wifi.c b/APP_Framework/Framework/connection/wifi/adapter_wifi.c index cf1ed7a96..2b9dddc76 100644 --- a/APP_Framework/Framework/connection/wifi/adapter_wifi.c +++ b/APP_Framework/Framework/connection/wifi/adapter_wifi.c @@ -29,8 +29,6 @@ extern AdapterProductInfoType Hfa21WifiAttach(struct Adapter *adapter); extern AdapterProductInfoType Esp07sWifiAttach(struct Adapter *adapter); #endif -#define ADAPTER_WIFI_NAME "wifi" - static int AdapterWifiRegister(struct Adapter *adapter) { int ret = 0; diff --git a/APP_Framework/Framework/connection/zigbee/adapter_zigbee.c b/APP_Framework/Framework/connection/zigbee/adapter_zigbee.c index adfe8dedc..e3577cf22 100644 --- a/APP_Framework/Framework/connection/zigbee/adapter_zigbee.c +++ b/APP_Framework/Framework/connection/zigbee/adapter_zigbee.c @@ -26,8 +26,6 @@ extern AdapterProductInfoType E18Attach(struct Adapter *adapter); #endif -#define ADAPTER_ZIGBEE_NAME "zigbee" - static int AdapterZigbeeRegister(struct Adapter *adapter) { int ret = 0; diff --git a/Ubiquitous/XiZi/board/xidatong/third_party_driver/ch438/connect_ch438.c b/Ubiquitous/XiZi/board/xidatong/third_party_driver/ch438/connect_ch438.c index 500a54fa7..a022c32b3 100644 --- a/Ubiquitous/XiZi/board/xidatong/third_party_driver/ch438/connect_ch438.c +++ b/Ubiquitous/XiZi/board/xidatong/third_party_driver/ch438/connect_ch438.c @@ -629,7 +629,7 @@ void WriteCH438Block(uint8 maddr, uint8 mlen, uint8 *mbuf) ** date: **------------------------------------------------------------------------------------------------------- ********************************************************************************************************/ -void Ch438UartSend( uint8 ext_uart_no,uint8 *data, uint8 Num ) +void Ch438UartSend(uint8 ext_uart_no, uint8 *data, uint16 Num) { uint8 REG_LSR_ADDR,REG_THR_ADDR; @@ -958,10 +958,35 @@ static uint32 ImxrtCh438WriteData(void *dev, struct BusBlockWriteParam *write_pa NULL_PARAM_CHECK(dev); NULL_PARAM_CHECK(write_param); + int write_len, write_len_continue; + int i, write_index; + uint8 *write_buffer; + struct SerialHardwareDevice *serial_dev = (struct SerialHardwareDevice *)dev; struct SerialDevParam *dev_param = (struct SerialDevParam *)serial_dev->haldev.private_data; - Ch438UartSend(dev_param->ext_uart_no, (uint8 *)write_param->buffer, write_param->size); + write_len = write_param->size; + write_len_continue = write_param->size; + write_buffer = (uint8 *)write_param->buffer; + + if (write_len > 256) { + if (0 == write_len % 256) { + write_index = write_len / 256; + for (i = 0; i < write_index; i ++) { + Ch438UartSend(dev_param->ext_uart_no, write_buffer + i * 256, 256); + } + } else { + write_index = 0; + while (write_len_continue > 256) { + Ch438UartSend(dev_param->ext_uart_no, write_buffer + write_index * 256, 256); + write_index++; + write_len_continue = write_len - write_index * 256; + } + Ch438UartSend(dev_param->ext_uart_no, write_buffer + write_index * 256, write_len_continue); + } + } else { + Ch438UartSend(dev_param->ext_uart_no, write_buffer, write_len); + } return EOK; } @@ -974,6 +999,7 @@ static uint32 ImxrtCh438ReadData(void *dev, struct BusBlockReadParam *read_param x_err_t result; uint8 rcv_num = 0; uint8 gInterruptStatus; + uint8 interrupt_done = 0; uint8 InterruptStatus; static uint8 dat; uint8 REG_LCR_ADDR; @@ -991,54 +1017,49 @@ static uint32 ImxrtCh438ReadData(void *dev, struct BusBlockReadParam *read_param struct SerialHardwareDevice *serial_dev = (struct SerialHardwareDevice *)dev; struct SerialDevParam *dev_param = (struct SerialDevParam *)serial_dev->haldev.private_data; - result = KSemaphoreObtain(ch438_sem, WAITING_FOREVER); - if (EOK == result) { - gInterruptStatus = ReadCH438Data(REG_SSR_ADDR); - if (!gInterruptStatus) { - dat = ReadCH438Data(REG_LCR0_ADDR); - dat = ReadCH438Data(REG_IER0_ADDR); - dat = ReadCH438Data(REG_MCR0_ADDR); - dat = ReadCH438Data(REG_LSR0_ADDR); - dat = ReadCH438Data(REG_MSR0_ADDR); - dat = ReadCH438Data(REG_RBR0_ADDR); - dat = ReadCH438Data(REG_THR0_ADDR); - dat = ReadCH438Data(REG_IIR0_ADDR); - dat = dat ; - } else { - if (gInterruptStatus & interrupt_num[dev_param->ext_uart_no]) { /* check which uart port triggers interrupt*/ - REG_LCR_ADDR = offset_addr[dev_param->ext_uart_no] | REG_LCR0_ADDR; - REG_DLL_ADDR = offset_addr[dev_param->ext_uart_no] | REG_DLL0_ADDR; - REG_DLM_ADDR = offset_addr[dev_param->ext_uart_no] | REG_DLM0_ADDR; - REG_IER_ADDR = offset_addr[dev_param->ext_uart_no] | REG_IER0_ADDR; - REG_MCR_ADDR = offset_addr[dev_param->ext_uart_no] | REG_MCR0_ADDR; - REG_FCR_ADDR = offset_addr[dev_param->ext_uart_no] | REG_FCR0_ADDR; - REG_RBR_ADDR = offset_addr[dev_param->ext_uart_no] | REG_RBR0_ADDR; - REG_THR_ADDR = offset_addr[dev_param->ext_uart_no] | REG_THR0_ADDR; - REG_IIR_ADDR = offset_addr[dev_param->ext_uart_no] | REG_IIR0_ADDR; - REG_LSR_ADDR = offset_addr[dev_param->ext_uart_no] | REG_LSR0_ADDR; - REG_MSR_ADDR = offset_addr[dev_param->ext_uart_no] | REG_MSR0_ADDR; - - InterruptStatus = ReadCH438Data( REG_IIR_ADDR ) & 0x0f; /* read the status of the uart port*/ + while (!interrupt_done) { + result = KSemaphoreObtain(ch438_sem, WAITING_FOREVER); + if (EOK == result) { + gInterruptStatus = ReadCH438Data(REG_SSR_ADDR); + if (!gInterruptStatus) { + // dat = ReadCH438Data(REG_LCR0_ADDR); + // dat = ReadCH438Data(REG_IER0_ADDR); + // dat = ReadCH438Data(REG_MCR0_ADDR); + // dat = ReadCH438Data(REG_LSR0_ADDR); + // dat = ReadCH438Data(REG_MSR0_ADDR); + // dat = ReadCH438Data(REG_RBR0_ADDR); + // dat = ReadCH438Data(REG_THR0_ADDR); + // dat = ReadCH438Data(REG_IIR0_ADDR); + // dat = dat; + interrupt_done = 0; + } else { + if (gInterruptStatus & interrupt_num[dev_param->ext_uart_no]) { /* check which uart port triggers interrupt*/ + REG_LCR_ADDR = offset_addr[dev_param->ext_uart_no] | REG_LCR0_ADDR; + REG_DLL_ADDR = offset_addr[dev_param->ext_uart_no] | REG_DLL0_ADDR; + REG_DLM_ADDR = offset_addr[dev_param->ext_uart_no] | REG_DLM0_ADDR; + REG_IER_ADDR = offset_addr[dev_param->ext_uart_no] | REG_IER0_ADDR; + REG_MCR_ADDR = offset_addr[dev_param->ext_uart_no] | REG_MCR0_ADDR; + REG_FCR_ADDR = offset_addr[dev_param->ext_uart_no] | REG_FCR0_ADDR; + REG_RBR_ADDR = offset_addr[dev_param->ext_uart_no] | REG_RBR0_ADDR; + REG_THR_ADDR = offset_addr[dev_param->ext_uart_no] | REG_THR0_ADDR; + REG_IIR_ADDR = offset_addr[dev_param->ext_uart_no] | REG_IIR0_ADDR; + REG_LSR_ADDR = offset_addr[dev_param->ext_uart_no] | REG_LSR0_ADDR; + REG_MSR_ADDR = offset_addr[dev_param->ext_uart_no] | REG_MSR0_ADDR; + + InterruptStatus = ReadCH438Data( REG_IIR_ADDR ) & 0x0f; /* read the status of the uart port*/ - switch( InterruptStatus ) - { - case INT_NOINT: /* NO INTERRUPT */ - break; - case INT_THR_EMPTY: /* THR EMPTY INTERRUPT*/ - break; - case INT_RCV_OVERTIME: /* RECV OVERTIME INTERRUPT*/ - case INT_RCV_SUCCESS: /* RECV INTERRUPT SUCCESSFULLY*/ + if ((INT_RCV_OVERTIME == InterruptStatus) || (INT_RCV_SUCCESS == InterruptStatus)) { rcv_num = Ch438UartRcv(dev_param->ext_uart_no, (uint8 *)read_param->buffer, read_param->size); read_param->read_length = rcv_num; - break; - case INT_RCV_LINES: /* RECV LINES INTERRUPT */ - ReadCH438Data( REG_LSR_ADDR ); - break; - case INT_MODEM_CHANGE: /* MODEM CHANGE INTERRUPT */ - ReadCH438Data( REG_MSR_ADDR ); - break; - default: - break; + + interrupt_done = 1; + + // int i; + // uint8 *buffer = (uint8 *)read_param->buffer; + // for (i = 0; i < rcv_num; i ++) { + // KPrintf("Ch438UartRcv i %u data 0x%x\n", i, buffer[i]); + // } + } } } } diff --git a/Ubiquitous/XiZi/resources/bus.c b/Ubiquitous/XiZi/resources/bus.c index fc1df7002..31f87ebd8 100644 --- a/Ubiquitous/XiZi/resources/bus.c +++ b/Ubiquitous/XiZi/resources/bus.c @@ -52,7 +52,7 @@ static int BusMatchDrvDev(struct Driver *driver, struct HardwareDev *device) NULL_PARAM_CHECK(device); if(!strncmp(driver->owner_bus->bus_name, device->owner_bus->bus_name, NAME_NUM_MAX)) { - KPrintf("BusMatchDrvDev match successfully, bus name %s\n", driver->owner_bus->bus_name); + //KPrintf("BusMatchDrvDev match successfully, bus name %s\n", driver->owner_bus->bus_name); driver->private_data = device->private_data;//driver get the device param device->owner_bus->owner_driver = driver;