diff --git a/APP_Framework/Applications/framework_init.c b/APP_Framework/Applications/framework_init.c index 0f7505558..339c7a71b 100644 --- a/APP_Framework/Applications/framework_init.c +++ b/APP_Framework/Applications/framework_init.c @@ -21,6 +21,7 @@ extern int AdapterBlueToothInit(void); extern int AdapterWifiInit(void); extern int AdapterEthernetInit(void); extern int AdapterZigbeeInit(void); +extern int AdapterLoraInit(void); extern int D124VoiceInit(void); extern int Hs300xTemperatureInit(void); @@ -106,6 +107,9 @@ static struct InitDesc connection_desc[] = #endif #ifdef CONNECTION_ADAPTER_ETHERNET { "ethernet adapter", AdapterEthernetInit}, +#endif +#ifdef CONNECTION_ADAPTER_LORA + { "lora adapter", AdapterLoraInit}, #endif { "NULL", NULL }, }; diff --git a/APP_Framework/Framework/connection/4g/adapter_4g.c b/APP_Framework/Framework/connection/4g/adapter_4g.c index 00bf150db..766c620bd 100644 --- a/APP_Framework/Framework/connection/4g/adapter_4g.c +++ b/APP_Framework/Framework/connection/4g/adapter_4g.c @@ -51,7 +51,7 @@ int Adapter4GInit(void) struct Adapter *adapter = PrivMalloc(sizeof(struct Adapter)); if (!adapter) { - free(adapter); + PrivFree(adapter); return -1; } @@ -60,7 +60,7 @@ int Adapter4GInit(void) ret = Adapter4GRegister(adapter); if (ret < 0) { printf("Adapter4GInit register 4G adapter error\n"); - free(adapter); + PrivFree(adapter); return -1; } @@ -68,7 +68,7 @@ int Adapter4GInit(void) AdapterProductInfoType product_info = Ec200tAttach(adapter); if (!product_info) { printf("Adapter4GInit ec200t attach error\n"); - free(adapter); + PrivFree(adapter); return -1; } diff --git a/APP_Framework/Framework/connection/adapter.c b/APP_Framework/Framework/connection/adapter.c index f44421a1b..1707955a7 100644 --- a/APP_Framework/Framework/connection/adapter.c +++ b/APP_Framework/Framework/connection/adapter.c @@ -395,7 +395,7 @@ int AdapterDeviceConnect(struct Adapter *adapter, enum NetRoleType net_role, con * @param priv_net_group - private net group * @return success: 0 , failure: other */ -int AdapterDeviceJoin(struct Adapter *adapter, const char *priv_net_group) +int AdapterDeviceJoin(struct Adapter *adapter, unsigned char *priv_net_group) { if (!adapter) return -1; diff --git a/APP_Framework/Framework/connection/adapter.h b/APP_Framework/Framework/connection/adapter.h index eb492301c..15f6f578b 100644 --- a/APP_Framework/Framework/connection/adapter.h +++ b/APP_Framework/Framework/connection/adapter.h @@ -90,6 +90,7 @@ enum NetRoleType COORDINATOR, ROUTER, END_DEVICE, + GATEWAY, ROLE_NONE, }; @@ -147,7 +148,7 @@ struct PrivProtocolDone int (*setdhcp)(struct Adapter *adapter, int enable); int (*ping)(struct Adapter *adapter, const char *destination); int (*netstat)(struct Adapter *adapter); - int (*join)(struct Adapter *adapter, const char *priv_net_group); + int (*join)(struct Adapter *adapter, unsigned char *priv_net_group); int (*send)(struct Adapter *adapter, const void *buf, size_t len); int (*recv)(struct Adapter *adapter, void *buf, size_t len); int (*quit)(struct Adapter *adapter); @@ -164,6 +165,7 @@ struct Adapter struct Socket socket; + int net_role_id; enum NetProtocolType net_protocol; enum NetRoleType net_role; enum AdapterStatus adapter_status; @@ -171,6 +173,7 @@ struct Adapter char buffer[ADAPTER_BUFFSIZE]; void *done; + void *adapter_param; struct DoublelistNode link; }; @@ -206,7 +209,7 @@ int AdapterDeviceControl(struct Adapter *adapter, int cmd, void *args); int AdapterDeviceConnect(struct Adapter *adapter, enum NetRoleType net_role, const char *ip, const char *port, enum IpType ip_type); /*Join to a certain private net, only support PRIVATE_PROTOCOL*/ -int AdapterDeviceJoin(struct Adapter *adapter, const char *priv_net_group); +int AdapterDeviceJoin(struct Adapter *adapter, unsigned char *priv_net_group); /*Adapter disconnect from ip net or private net group*/ int AdapterDeviceDisconnect(struct Adapter *adapter); @@ -220,10 +223,10 @@ int AdapterDeviceSetDown(struct Adapter *adapter); /*Set ip/gateway/netmask address*/ int AdapterDeviceSetAddr(struct Adapter *adapter, const char *ip, const char *gateway, const char *netmask); -/**/ +/*Set DNS function*/ int AdapterDeviceSetDns(struct Adapter *adapter, const char *dns_addr, uint8 dns_count); -/**/ +/*Set DHCP function*/ int AdapterDeviceSetDhcp(struct Adapter *adapter, int enable); /*ping function*/ diff --git a/APP_Framework/Framework/connection/bluetooth/adapter_bluetooth.c b/APP_Framework/Framework/connection/bluetooth/adapter_bluetooth.c index 0f855a4ae..ace054b49 100644 --- a/APP_Framework/Framework/connection/bluetooth/adapter_bluetooth.c +++ b/APP_Framework/Framework/connection/bluetooth/adapter_bluetooth.c @@ -24,7 +24,7 @@ extern AdapterProductInfoType Hc08Attach(struct Adapter *adapter); #endif -#define ADAPTER_BLUETOOTH_NAME "BlueTooth" +#define ADAPTER_BLUETOOTH_NAME "bluetooth" static int AdapterBlueToothRegister(struct Adapter *adapter) { @@ -50,7 +50,7 @@ int AdapterBlueToothInit(void) struct Adapter *adapter = PrivMalloc(sizeof(struct Adapter)); if (!adapter) { - free(adapter); + PrivFree(adapter); return -1; } @@ -59,7 +59,7 @@ int AdapterBlueToothInit(void) ret = AdapterBlueToothRegister(adapter); if (ret < 0) { printf("AdapterBlueToothInit register BT adapter error\n"); - free(adapter); + PrivFree(adapter); return -1; } @@ -67,7 +67,7 @@ int AdapterBlueToothInit(void) AdapterProductInfoType product_info = Hc08Attach(adapter); if (!product_info) { printf("AdapterBlueToothInit hc08 attach error\n"); - free(adapter); + PrivFree(adapter); return -1; } diff --git a/APP_Framework/Framework/connection/ethernet/adapter_ethernet.c b/APP_Framework/Framework/connection/ethernet/adapter_ethernet.c index 601ca8400..00a8dbf9b 100644 --- a/APP_Framework/Framework/connection/ethernet/adapter_ethernet.c +++ b/APP_Framework/Framework/connection/ethernet/adapter_ethernet.c @@ -51,7 +51,7 @@ int AdapterEthernetInit(void) struct Adapter *adapter = PrivMalloc(sizeof(struct Adapter)); if (!adapter) { printf("AdapterEthernetInit malloc error\n"); - free(adapter); + PrivFree(adapter); return -1; } @@ -60,7 +60,7 @@ int AdapterEthernetInit(void) ret = AdapterEthernetRegister(adapter); if (ret < 0) { printf("AdapterEthernetInit register ethernet adapter error\n"); - free(adapter); + PrivFree(adapter); return -1; } @@ -68,7 +68,7 @@ int AdapterEthernetInit(void) AdapterProductInfoType product_info = Hfa21EthernetAttach(adapter); if (!product_info) { printf("AdapterEthernetInit hfa21 attach error\n"); - free(adapter); + PrivFree(adapter); return -1; } diff --git a/APP_Framework/Framework/connection/lora/Kconfig b/APP_Framework/Framework/connection/lora/Kconfig index e69de29bb..fd8c8fda9 100644 --- a/APP_Framework/Framework/connection/lora/Kconfig +++ b/APP_Framework/Framework/connection/lora/Kconfig @@ -0,0 +1,30 @@ +config ADAPTER_SX1278 + bool "Using lora adapter device SX1278" + default y + +choice + prompt "Lora device adapter select net role type " + default AS_LORA_CLIENT_ROLE + + config AS_LORA_GATEWAY_ROLE + bool "config as a gateway" + + config AS_LORA_CLIENT_ROLE + bool "config as a client" +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" +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" +endif + +if ADAPTER_SX1278 + source "$APP_DIR/Framework/connection/lora/sx1278/Kconfig" +endif diff --git a/APP_Framework/Framework/connection/lora/Makefile b/APP_Framework/Framework/connection/lora/Makefile index 91779a061..1a9114b6f 100644 --- a/APP_Framework/Framework/connection/lora/Makefile +++ b/APP_Framework/Framework/connection/lora/Makefile @@ -1,3 +1,7 @@ SRC_FILES := adapter_lora.c +ifeq ($(CONFIG_ADAPTER_SX1278),y) + SRC_DIR += sx1278 +endif + include $(KERNEL_ROOT)/compiler.mk diff --git a/APP_Framework/Framework/connection/lora/adapter_lora.c b/APP_Framework/Framework/connection/lora/adapter_lora.c index 7a2af4450..ab3c08007 100644 --- a/APP_Framework/Framework/connection/lora/adapter_lora.c +++ b/APP_Framework/Framework/connection/lora/adapter_lora.c @@ -15,5 +15,625 @@ * @brief Implement the connection lora adapter function * @version 1.1 * @author AIIT XUOS Lab - * @date 2021.06.25 + * @date 2021.10.20 */ + +#include + +#ifdef ADAPTER_SX1278 +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_DATA_HEAD 0x3C +#define ADAPTER_LORA_NET_PANID 0x0102 +#define ADAPTER_LORA_DATA_TYPE_JOIN 0x0A +#define ADAPTER_LORA_DATA_TYPE_QUIT 0x0B +#define ADAPTER_LORA_DATA_TYPE_JOIN_REPLY 0x0C +#define ADAPTER_LORA_DATA_TYPE_QUIT_REPLY 0x0D +#define ADAPTER_LORA_DATA_TYPE_USERDATA 0x0E +#define ADAPTER_LORA_DATA_TYPE_CMD 0x0F + +enum ClientState +{ + CLIENT_DISCONNECT = 0, + CLIENT_CONNECT, + CLIENT_DEFAULT, +}; + +enum DataType +{ + LORA_CLIENT_ACCESS = 0, + LORA_GATEWAY_REPLY, + LORA_USER_DATA, +}; + +struct LoraGatewayParam +{ + uint8 gateway_id; + uint16 panid; + uint8 client_id[ADAPTER_LORA_CLIENT_NUM]; + int client_num; +}; + +struct LoraClientParam +{ + uint8 client_id; + uint16 panid; + enum ClientState client_state; + + uint8 gateway_id; +}; + +/*LoraDataFormat: +**flame_head : 0x3C +**length : sizeof(struct LoraDataFormat) +**panid : 0x0102 +**client_id : 0x01、0x02、0x03... +**gateway_id : 0x11、0x12、0x13... +**data_type : 0x0A(join net, client->gateway)、0x0B(join net reply, gateway->client)、 + 0x0C(user data, client->gateway)、0x0D(user data cmd, gateway->client) +**data : user data +**crc16 : CRC 16 check data +*/ +struct LoraDataFormat +{ + uint8 flame_head; + uint32 length; + uint16 panid; + uint8 client_id; + uint8 gateway_id; + + uint8 data_type; + uint8 data[ADAPTER_LORA_DATA_LENGTH]; + + uint16 crc16; +}; + +/*******************LORA MESH FUNCTION***********************/ + +/** + * @description: create CRC16 data + * @param data input data buffer + * @param length input data buffer minus check code + */ +static uint16 LoraCrc16(uint8 *data, uint16 length) +{ + int j; + uint16 crc_data = 0xFFFF; + + while (length--) { + crc_data ^= *data++; + for( j = 0 ; j < 8 ; j ++) { + if(crc_data & 0x01) /* LSB(b0)=1 */ + crc_data = crc_data >> 1 ^ 0xA001; + else + crc_data = crc_data >> 1; + } + } + + return crc_data; +} + +/** + * @description: CRC16 check + * @param data input data buffer + * @param length input data buffer minus check code + */ +static int LoraCrc16Check(uint8 *data, uint16 length) +{ + uint16 crc_data; + uint16 input_data = (((uint16)data[length - 1] << 8) & 0xFF00) | ((uint16)data[length - 2] & 0x00FF); + + crc_data = LoraCrc16(data, length - 2); + + if (crc_data == input_data) + return 0; + else + return -1; +} + +/** + * @description: Lora Gateway reply connect request to Client + * @param adapter Lora adapter pointer + * @param gateway_recv_data Lora Client connect data + */ +static int LoraGatewayReply(struct Adapter *adapter, struct LoraDataFormat *gateway_recv_data) +{ + int i; + struct LoraGatewayParam *gateway = (struct LoraGatewayParam *)adapter->adapter_param; + struct LoraDataFormat gateway_reply_data; + + memset(&gateway_reply_data, 0, sizeof(struct LoraDataFormat)); + + if (ADAPTER_LORA_DATA_TYPE_JOIN == gateway_recv_data->data_type) { + if (gateway->client_num > 6) { + printf("Lora gateway only support 6(max) client\n"); + gateway->client_num = 0; + } + gateway->client_id[gateway->client_num] = gateway_recv_data->client_id; + gateway->client_num ++; + + gateway_reply_data.flame_head = ADAPTER_LORA_DATA_HEAD; + gateway_reply_data.length = sizeof(struct LoraDataFormat); + gateway_reply_data.panid = gateway->panid; + gateway_reply_data.data_type = ADAPTER_LORA_DATA_TYPE_JOIN_REPLY; + gateway_reply_data.client_id = gateway_recv_data->client_id; + gateway_reply_data.gateway_id = gateway->gateway_id; + gateway_reply_data.crc16 = LoraCrc16((uint8 *)&gateway_reply_data, sizeof(struct LoraDataFormat) - 2); + + if (AdapterDeviceSend(adapter, (uint8 *)&gateway_reply_data, gateway_reply_data.length) < 0) { + return -1; + } + + printf("Lora gateway 0x%x accept client 0x%x \n", gateway->gateway_id, gateway_recv_data->client_id); + } else if (ADAPTER_LORA_DATA_TYPE_QUIT == gateway_recv_data->data_type) { + for (i = 0; i < gateway->client_num; i ++) { + if (gateway->client_id[i] == gateway_recv_data->client_id) { + gateway->client_id[i] = 0; + gateway->client_num --; + break; + } + } + + gateway_reply_data.flame_head = ADAPTER_LORA_DATA_HEAD; + gateway_reply_data.length = sizeof(struct LoraDataFormat); + gateway_reply_data.panid = gateway->panid; + gateway_reply_data.data_type = ADAPTER_LORA_DATA_TYPE_QUIT_REPLY; + gateway_reply_data.client_id = gateway_recv_data->client_id; + gateway_reply_data.gateway_id = gateway->gateway_id; + gateway_reply_data.crc16 = LoraCrc16((uint8 *)&gateway_reply_data, sizeof(struct LoraDataFormat) - 2); + + if (AdapterDeviceSend(adapter, (uint8 *)&gateway_reply_data, gateway_reply_data.length) < 0) { + return -1; + } + + printf("Lora gateway 0x%x accept client 0x%x \n", gateway->gateway_id, gateway_recv_data->client_id); + } + + return 0; +} + +/** + * @description: Lora Gateway send cmd to Client + * @param adapter Lora adapter pointer + * @param client_id Lora Client id + * @param cmd Lora cmd + */ +static int LoraGatewaySendCmd(struct Adapter *adapter, unsigned char client_id, int cmd) +{ + struct LoraGatewayParam *gateway = (struct LoraGatewayParam *)adapter->adapter_param; + struct LoraDataFormat gateway_cmd_data; + + memset(&gateway_cmd_data, 0, sizeof(struct LoraDataFormat)); + + gateway_cmd_data.flame_head = ADAPTER_LORA_DATA_HEAD; + gateway_cmd_data.length = sizeof(struct LoraDataFormat); + gateway_cmd_data.panid = gateway->panid; + gateway_cmd_data.data_type = cmd; + gateway_cmd_data.client_id = client_id; + gateway_cmd_data.gateway_id = gateway->gateway_id; + gateway_cmd_data.crc16 = LoraCrc16((uint8 *)&gateway_cmd_data, sizeof(struct LoraDataFormat) - 2); + + if (AdapterDeviceSend(adapter, (uint8 *)&gateway_cmd_data, gateway_cmd_data.length) < 0) { + return -1; + } + + return 0; +} + +/** + * @description: Lora Gateway handle the user data from the client + * @param adapter Lora adapter pointer + * @param gateway_recv_data Lora Client user data + */ +static int LoraGatewayHandleData(struct Adapter *adapter, struct LoraDataFormat *gateway_recv_data) +{ + /*User need to handle client data depends on the requirement*/ + printf("Lora Gateway receive Client %d data:\n", gateway_recv_data->client_id); + printf("%s\n", gateway_recv_data->data); + return 0; +} + +/** + * @description: Lora Client update status after join the net + * @param adapter Lora adapter pointer + * @param client_recv_data Lora Client recv data from Lora Gateway + */ +static int LoraClientUpdate(struct Adapter *adapter, struct LoraDataFormat *client_recv_data) +{ + struct LoraClientParam *client = (struct LoraClientParam *)adapter->adapter_param; + + if (ADAPTER_LORA_DATA_TYPE_JOIN_REPLY == client_recv_data->data_type) { + client->gateway_id = client_recv_data->gateway_id; + client->panid = client_recv_data->panid; + client->client_state = CLIENT_CONNECT; + printf("LoraClientUpdate client join panid 0x%x successfully\n", client->panid); + } else if (ADAPTER_LORA_DATA_TYPE_QUIT_REPLY == client_recv_data->data_type) { + client->gateway_id = 0; + client->panid = 0; + client->client_state = CLIENT_DISCONNECT; + printf("LoraClientUpdate client quit panid 0x%x successfully\n", client->panid); + } + + return 0; +} + +/** + * @description: Lora Client send the user data to the gateway + * @param adapter Lora adapter pointer + * @param send_buf Lora Client send user data buf + * @param length user data length (max size is ADAPTER_LORA_DATA_LENGTH) + */ +static int LoraClientSendData(struct Adapter *adapter, void *send_buf, int length) +{ + struct LoraClientParam *client = (struct LoraClientParam *)adapter->adapter_param; + + if (client->client_state != CLIENT_CONNECT) { + printf("Lora client %d do not connect to Lora gateway\n", client->client_id); + return 0; + } + + if (length > ADAPTER_LORA_DATA_LENGTH) { + printf("Lora client %d send data %d larger than max %d \n", client->client_id, length, ADAPTER_LORA_DATA_LENGTH); + return 0; + } + + struct LoraDataFormat client_user_data; + + memset(&client_user_data, 0, sizeof(struct LoraDataFormat)); + + client_user_data.flame_head = ADAPTER_LORA_DATA_HEAD; + client_user_data.length = sizeof(struct LoraDataFormat); + client_user_data.panid = client->panid; + client_user_data.data_type = ADAPTER_LORA_DATA_TYPE_USERDATA; + client_user_data.client_id = client->client_id; + client_user_data.gateway_id = client->gateway_id; + + memcpy(client_user_data.data, (uint8 *)send_buf, length); + + client_user_data.crc16 = LoraCrc16((uint8 *)&client_user_data, sizeof(struct LoraDataFormat) - 2); + + if (AdapterDeviceSend(adapter, (uint8 *)&client_user_data, client_user_data.length) < 0) { + return -1; + } + + return 0; +} + +/** + * @description: Lora Gateway receive data analyzing + * @param adapter Lora adapter pointer + */ +static int LoraGateWayDataAnalyze(struct Adapter *adapter) +{ + int ret = 0; + + struct LoraDataFormat gateway_recv_data; + + memset(&gateway_recv_data, 0, sizeof(struct LoraDataFormat)); + + AdapterDeviceRecv(adapter, &gateway_recv_data, sizeof(struct LoraDataFormat)); + + // printf("gateway_recv_data\n"); + // 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", + // gateway_recv_data->flame_head, gateway_recv_data->length, gateway_recv_data->panid, gateway_recv_data->data_type, + // gateway_recv_data->client_id, gateway_recv_data->gateway_id, gateway_recv_data->crc16); + + if (LoraCrc16Check((uint8 *)&gateway_recv_data, sizeof(struct LoraDataFormat)) < 0) { + printf("LoraGateWayDataAnalyze CRC check error\n"); + return -1; + } + + if ((ADAPTER_LORA_DATA_HEAD == gateway_recv_data.flame_head) && + (ADAPTER_LORA_NET_PANID == gateway_recv_data.panid)) { + switch (gateway_recv_data.data_type) + { + case ADAPTER_LORA_DATA_TYPE_JOIN : + case ADAPTER_LORA_DATA_TYPE_QUIT : + ret = LoraGatewayReply(adapter, &gateway_recv_data); + break; + case ADAPTER_LORA_DATA_TYPE_USERDATA : + ret = LoraGatewayHandleData(adapter, &gateway_recv_data); + break; + default: + break; + } + } + + return ret; +} + +/** + * @description: Lora Client receive data analyzing + * @param adapter Lora adapter pointer + * @param send_buf Lora Client send user data buf + * @param length user data length (max size is ADAPTER_LORA_DATA_LENGTH) + */ +static int LoraClientDataAnalyze(struct Adapter *adapter, void *send_buf, int length) +{ + int ret = 0; + + struct LoraDataFormat client_recv_data; + + memset(&client_recv_data, 0, sizeof(struct LoraDataFormat)); + + AdapterDeviceRecv(adapter, &client_recv_data, sizeof(struct LoraDataFormat)); + + // printf("client_recv_data\n"); + // 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); + + if (LoraCrc16Check((uint8 *)&client_recv_data, sizeof(struct LoraDataFormat)) < 0) { + printf("LoraClientDataAnalyze CRC check error\n"); + return -1; + } + + if ((ADAPTER_LORA_DATA_HEAD == client_recv_data.flame_head) && + (ADAPTER_LORA_NET_PANID == client_recv_data.panid)) { + + if (client_recv_data.client_id == adapter->net_role_id) { + switch (client_recv_data.data_type) + { + case ADAPTER_LORA_DATA_TYPE_JOIN_REPLY : + case ADAPTER_LORA_DATA_TYPE_QUIT_REPLY : + ret = LoraClientUpdate(adapter, &client_recv_data); + break; + case ADAPTER_LORA_DATA_TYPE_CMD : + if (send_buf) { + ret = LoraClientSendData(adapter, send_buf, length); + } + break; + default: + break; + } + } + } + + return ret; +} + +/** + * @description: Lora Client join net function + * @param adapter Lora adapter pointer + * @param panid Lora net panid + */ +static int LoraClientJoinNet(struct Adapter *adapter, unsigned short panid) +{ + struct LoraDataFormat client_join_data; + + memset(&client_join_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_JOIN; + client_join_data.client_id = adapter->net_role_id; + client_join_data.crc16 = LoraCrc16((uint8 *)&client_join_data, sizeof(struct LoraDataFormat) - 2); + + if (AdapterDeviceJoin(adapter, (uint8 *)&client_join_data) < 0) { + return -1; + } + + return 0; +} + +/** + * @description: Lora Gateway task + * @param parameter - Lora adapter pointer + */ +static void *LoraGatewayTask(void *parameter) +{ + int i; + int ret = 0; + struct Adapter *lora_adapter = (struct Adapter *)parameter; + struct LoraGatewayParam *gateway = (struct LoraGatewayParam *)lora_adapter->adapter_param; + + while (1) { + + ret = LoraGateWayDataAnalyze(lora_adapter); + if (ret < 0) { + printf("LoraGateWayDataAnalyze error\n"); + PrivTaskDelay(500); + continue; + } + + PrivTaskDelay(2000); + + if (gateway->client_num > 0) { + for (i = 0; i < ADAPTER_LORA_CLIENT_NUM; i ++) { + if (gateway->client_id[i]) { + ret = LoraGatewaySendCmd(lora_adapter, gateway->client_id[i], ADAPTER_LORA_DATA_TYPE_CMD); + if (ret < 0) { + printf("LoraGatewaySendCmd client %d ID %d error\n", i, gateway->client_id[i]); + PrivTaskDelay(500); + continue; + } + } + } + } + } + + return 0; +} + +/** + * @description: Lora Client data upload task + * @param parameter - Lora adapter pointer + */ +static void *LoraClientDataTask(void *parameter) +{ + int ret = 0; + struct Adapter *lora_adapter = (struct Adapter *)parameter; + struct LoraClientParam *client = (struct LoraClientParam *)lora_adapter->adapter_param; + + uint8 lora_send_buf[ADAPTER_LORA_DATA_LENGTH]; + memset(lora_send_buf, 0, ADAPTER_LORA_DATA_LENGTH); + sprintf(lora_send_buf, "Lora adapter test\n"); + + while (1) { + + PrivTaskDelay(100); + + if (CLIENT_DISCONNECT == client->client_state) { + ret = LoraClientJoinNet(lora_adapter, client->panid); + if (ret < 0) { + printf("LoraClientJoinNet error panid 0x%x\n", client->panid); + continue; + } + + ret = LoraClientDataAnalyze(lora_adapter, NULL, 0); + if (ret < 0) { + printf("LoraClientDataAnalyze error\n"); + PrivTaskDelay(500); + continue; + } + } + + if (CLIENT_CONNECT == client->client_state) { + ret = LoraClientDataAnalyze(lora_adapter, (void *)lora_send_buf, strlen(lora_send_buf)); + if (ret < 0) { + printf("LoraClientDataAnalyze error\n"); + PrivTaskDelay(500); + continue; + } + } + } +} + +/*******************LORA ADAPTER FUNCTION********************/ + +static int AdapterLoraRegister(struct Adapter *adapter) +{ + int ret = 0; + struct LoraGatewayParam *lora_gateway; + struct LoraClientParam *lora_client; + + strncpy(adapter->name, ADAPTER_LORA_NAME, NAME_NUM_MAX); + + adapter->net_protocol = PRIVATE_PROTOCOL; + +#ifdef AS_LORA_GATEWAY_ROLE + lora_gateway = PrivMalloc(sizeof(struct LoraGatewayParam)); + if (!lora_gateway) { + PrivFree(lora_gateway); + return -1; + } + + memset(lora_gateway, 0, sizeof(struct LoraGatewayParam)); + + lora_gateway->gateway_id = ADAPTER_LORA_NET_ROLE_ID; + lora_gateway->panid = ADAPTER_LORA_NET_PANID; + + adapter->net_role = GATEWAY; + adapter->adapter_param = (void *)lora_gateway; +#else //AS_LORA_CLIENT_ROLE + lora_client = PrivMalloc(sizeof(struct LoraClientParam)); + if (!lora_client) { + PrivFree(lora_client); + return -1; + } + + memset(lora_client, 0, sizeof(struct LoraClientParam)); + + lora_client->client_id = ADAPTER_LORA_NET_ROLE_ID; + lora_client->client_state = CLIENT_DISCONNECT; + lora_client->panid = ADAPTER_LORA_NET_PANID; + + adapter->net_role = CLIENT; + adapter->adapter_param = (void *)lora_client; +#endif + + adapter->net_role_id = ADAPTER_LORA_NET_ROLE_ID; + + adapter->adapter_status = UNREGISTERED; + + ret = AdapterDeviceRegister(adapter); + if (ret < 0) { + printf("Adapter4G register error\n"); + if (lora_gateway) + PrivFree(lora_gateway); + if (lora_client) + PrivFree(lora_client); + + return -1; + } + + return ret; +} + +int AdapterLoraInit(void) +{ + int ret = 0; + + struct Adapter *adapter = PrivMalloc(sizeof(struct Adapter)); + if (!adapter) { + PrivFree(adapter); + return -1; + } + + memset(adapter, 0, sizeof(struct Adapter)); + + ret = AdapterLoraRegister(adapter); + if (ret < 0) { + printf("AdapterLoraInit register lora adapter error\n"); + PrivFree(adapter); + return -1; + } + +#ifdef ADAPTER_SX1278 + AdapterProductInfoType product_info = Sx1278Attach(adapter); + if (!product_info) { + printf("AdapterLoraInit sx1278 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; +} + +/******************Lora TEST*********************/ +#ifdef AS_LORA_GATEWAY_ROLE +static pthread_t lora_gateway_task; +#else //AS_LORA_CLIENT_ROLE +static pthread_t lora_client_data_task; +#endif + +int AdapterLoraTest(void) +{ + struct Adapter *adapter = AdapterDeviceFindByName(ADAPTER_LORA_NAME); + + AdapterDeviceOpen(adapter); + + //create lora gateway task +#ifdef AS_LORA_GATEWAY_ROLE + pthread_attr_t lora_gateway_attr; + lora_gateway_attr.schedparam.sched_priority = 20; + lora_gateway_attr.stacksize = 4096; + + PrivTaskCreate(&lora_gateway_task, &lora_gateway_attr, &LoraGatewayTask, (void *)adapter); + PrivTaskStartup(&lora_gateway_task); + +#else //AS_LORA_CLIENT_ROLE + //create lora client task + pthread_attr_t lora_client_attr; + lora_client_attr.schedparam.sched_priority = 20; + lora_client_attr.stacksize = 4096; + + PrivTaskCreate(&lora_client_data_task, &lora_client_attr, &LoraClientDataTask, (void *)adapter); + PrivTaskStartup(&lora_client_data_task); +#endif + + return 0; +} +SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC)|SHELL_CMD_PARAM_NUM(0)|SHELL_CMD_DISABLE_RETURN, AdapterLoraTest, AdapterLoraTest, show adapter lora information); diff --git a/APP_Framework/Framework/connection/lora/sx1278/Kconfig b/APP_Framework/Framework/connection/lora/sx1278/Kconfig index e69de29bb..971cc6fcc 100644 --- a/APP_Framework/Framework/connection/lora/sx1278/Kconfig +++ b/APP_Framework/Framework/connection/lora/sx1278/Kconfig @@ -0,0 +1,17 @@ +config ADAPTER_LORA_SX1278 + string "SX1278 adapter name" + default "sx1278" + +if ADD_XIUOS_FETURES + config ADAPTER_SX1278_DRIVER + string "SX1278 device spi driver path" + default "/dev/spi2_lora" +endif + +if ADD_NUTTX_FETURES + +endif + +if ADD_RTTHREAD_FETURES + +endif diff --git a/APP_Framework/Framework/connection/lora/sx1278/Makefile b/APP_Framework/Framework/connection/lora/sx1278/Makefile index e69de29bb..70390c409 100644 --- a/APP_Framework/Framework/connection/lora/sx1278/Makefile +++ b/APP_Framework/Framework/connection/lora/sx1278/Makefile @@ -0,0 +1,3 @@ +SRC_FILES := sx1278.c + +include $(KERNEL_ROOT)/compiler.mk diff --git a/APP_Framework/Framework/connection/lora/sx1278/sx1278.c b/APP_Framework/Framework/connection/lora/sx1278/sx1278.c index e69de29bb..3b32d6e18 100644 --- a/APP_Framework/Framework/connection/lora/sx1278/sx1278.c +++ b/APP_Framework/Framework/connection/lora/sx1278/sx1278.c @@ -0,0 +1,151 @@ +/* +* 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 sx1278.c + * @brief Implement the connection lora adapter function + * @version 1.1 + * @author AIIT XUOS Lab + * @date 2021.10.20 + */ + +#include + +/** + * @description: Open SX1278 spi function + * @param adapter - Lora device pointer + * @return success: 0, failure: -1 + */ +static int Sx1278Open(struct Adapter *adapter) +{ + /*step1: open sx1278 spi port*/ + adapter->fd = PrivOpen(ADAPTER_SX1278_DRIVER, O_RDWR); + if (adapter->fd < 0) { + printf("Sx1278Open get spi %s fd error\n", ADAPTER_SX1278_DRIVER); + return -1; + } + + printf("Sx1278Open done\n"); + + return 0; +} + +/** + * @description: Close SX1278 spi function + * @param adapter - Lora device pointer + * @return success: 0, failure: -1 + */ +static int Sx1278Close(struct Adapter *adapter) +{ + /*step1: close sx1278 spi port*/ + PrivClose(adapter->fd); + + ADAPTER_DEBUG("Sx1278Close done\n"); + + return 0; +} + +/** + * @description: SX1278 ioctl function + * @param adapter - Lora device pointer + * @param cmd - ioctl cmd + * @param args - iotl params + * @return success: 0, failure: -1 + */ +static int Sx1278Ioctl(struct Adapter *adapter, int cmd, void *args) +{ + /*to do*/ + return 0; +} + +/** + * @description: SX1278 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 Sx1278Join(struct Adapter *adapter, unsigned char *priv_net_group) +{ + + PrivWrite(adapter->fd, (void *)priv_net_group, 144); + + return 0; +} + +/** + * @description: SX1278 send data function + * @param adapter - Lora device pointer + * @param buf - data buffers + * @param len - data len + * @return success: 0, failure: -1 + */ +static int Sx1278Send(struct Adapter *adapter, const void *buf, size_t len) +{ + PrivWrite(adapter->fd, buf, len); + return 0; +} + +/** + * @description: SX1278 receive data function + * @param adapter - Lora device pointer + * @param buf - data buffers + * @param len - data len + * @return success: 0, failure: -1 + */ +static int Sx1278Recv(struct Adapter *adapter, void *buf, size_t len) +{ + return PrivRead(adapter->fd, buf, len);; +} + +/** + * @description: SX1278 quit lora net group function + * @param adapter - Lora device pointer + * @return success: 0, failure: -1 + */ +static int Sx1278Quit(struct Adapter *adapter) +{ + /*to do*/ + + return 0; +} + +static const struct PrivProtocolDone sx1278_done = +{ + .open = Sx1278Open, + .close = Sx1278Close, + .ioctl = Sx1278Ioctl, + .setup = NULL, + .setdown = NULL, + .setaddr = NULL, + .setdns = NULL, + .setdhcp = NULL, + .ping = NULL, + .netstat = NULL, + .join = Sx1278Join, + .send = Sx1278Send, + .recv = Sx1278Recv, + .quit = Sx1278Quit, +}; + +AdapterProductInfoType Sx1278Attach(struct Adapter *adapter) +{ + struct AdapterProductInfo *product_info = malloc(sizeof(struct AdapterProductInfo)); + if (!product_info) { + printf("Sx1278Attach malloc product_info error\n"); + return NULL; + } + + strncpy(product_info->model_name, ADAPTER_LORA_SX1278,sizeof(product_info->model_name)); + product_info->model_done = (void *)&sx1278_done; + + return product_info; +} diff --git a/APP_Framework/Framework/connection/wifi/adapter_wifi.c b/APP_Framework/Framework/connection/wifi/adapter_wifi.c index 701d30508..ab6ea8ca6 100644 --- a/APP_Framework/Framework/connection/wifi/adapter_wifi.c +++ b/APP_Framework/Framework/connection/wifi/adapter_wifi.c @@ -52,7 +52,7 @@ int AdapterWifiInit(void) struct Adapter *adapter = PrivMalloc(sizeof(struct Adapter)); if (!adapter) { printf("AdapterWifiInit malloc error\n"); - free(adapter); + PrivFree(adapter); return -1; } @@ -61,7 +61,7 @@ int AdapterWifiInit(void) ret = AdapterWifiRegister(adapter); if (ret < 0) { printf("AdapterWifiInit register wifi adapter error\n"); - free(adapter); + PrivFree(adapter); return -1; } @@ -69,7 +69,7 @@ int AdapterWifiInit(void) AdapterProductInfoType product_info = Hfa21WifiAttach(adapter); if (!product_info) { printf("AdapterWifiInit hfa21 attach error\n"); - free(adapter); + PrivFree(adapter); return -1; } diff --git a/APP_Framework/Framework/connection/zigbee/adapter_zigbee.c b/APP_Framework/Framework/connection/zigbee/adapter_zigbee.c index 482ef7566..258946501 100644 --- a/APP_Framework/Framework/connection/zigbee/adapter_zigbee.c +++ b/APP_Framework/Framework/connection/zigbee/adapter_zigbee.c @@ -72,14 +72,14 @@ int AdapterZigbeeInit(void) ret = AdapterZigbeeRegister(adapter); if (ret < 0) { printf("AdapterZigbeeRegister register zigbee adapter error\n"); - free(adapter); + PrivFree(adapter); return -1; } #ifdef ADAPTER_E18 AdapterProductInfoType product_info = E18Attach(adapter); if (!product_info) { printf("AdapterZigbeeRegister e18 attach error\n"); - free(adapter); + PrivFree(adapter); return -1; } diff --git a/APP_Framework/Framework/connection/zigbee/e18/e18.c b/APP_Framework/Framework/connection/zigbee/e18/e18.c index 1342ad63d..491811ed4 100644 --- a/APP_Framework/Framework/connection/zigbee/e18/e18.c +++ b/APP_Framework/Framework/connection/zigbee/e18/e18.c @@ -255,7 +255,7 @@ static int E18Ioctl(struct Adapter *adapter, int cmd, void *args) return ret; } -static int E18Join(struct Adapter *adapter, const char *priv_net_group) +static int E18Join(struct Adapter *adapter, unsigned char *priv_net_group) { int ret = 0; diff --git a/Ubiquitous/XiUOS/board/aiit-arm32-board/third_party_driver/spi/Kconfig b/Ubiquitous/XiUOS/board/aiit-arm32-board/third_party_driver/spi/Kconfig index 2e6e1df44..4d35f147a 100644 --- a/Ubiquitous/XiUOS/board/aiit-arm32-board/third_party_driver/spi/Kconfig +++ b/Ubiquitous/XiUOS/board/aiit-arm32-board/third_party_driver/spi/Kconfig @@ -22,42 +22,42 @@ if BSP_USING_SPI2 config SPI_2_DRV_NAME string "spi bus 2 driver name" default "spi2_drv" - menuconfig CONNECTION_COMMUNICATION_LORA_USING_SX1278 - bool "Using spi lora SX1278" + menuconfig RESOURCES_SPI_LORA + bool "Using spi lora function" default n - if CONNECTION_COMMUNICATION_LORA_USING_SX1278 + if RESOURCES_SPI_LORA config SX12XX_SPI_DEVICE_NAME - string "SX1278 device spi name" + string "SX1278 lora device spi name" default "spi2_dev0" config SX12XX_DEVICE_NAME - string "SX1278 device name" + string "SX1278 lora device name" default "spi2_lora" - config CONNECTION_COMMUNICATION_LORA_SX12XX_RST_PIN + config SX12XX_DEVICE_RST_PIN int default 10 - config CONNECTION_COMMUNICATION_LORA_SX12XX_DO0_PIN + config SX12XX_DEVICE_DO0_PIN int default 10 - config CONNECTION_COMMUNICATION_LORA_SX12XX_DO1_PIN + config SX12XX_DEVICE_DO1_PIN int default 10 - config CONNECTION_COMMUNICATION_LORA_SX12XX_DO2_PIN + config SX12XX_DEVICE_DO2_PIN int default 10 - config CONNECTION_COMMUNICATION_LORA_SX12XX_DO3_PIN + config SX12XX_DEVICE_DO3_PIN int default 10 - config CONNECTION_COMMUNICATION_LORA_SX12XX_DO4_PIN + config SX12XX_DEVICE_DO4_PIN int default 10 - config CONNECTION_COMMUNICATION_LORA_SX12XX_DO5_PIN + config SX12XX_DEVICE_DO5_PIN int default 10 endif diff --git a/Ubiquitous/XiUOS/board/aiit-arm32-board/third_party_driver/spi/Makefile b/Ubiquitous/XiUOS/board/aiit-arm32-board/third_party_driver/spi/Makefile index 3f71678e8..e87afde1a 100644 --- a/Ubiquitous/XiUOS/board/aiit-arm32-board/third_party_driver/spi/Makefile +++ b/Ubiquitous/XiUOS/board/aiit-arm32-board/third_party_driver/spi/Makefile @@ -4,7 +4,7 @@ ifeq ($(CONFIG_RESOURCES_SPI_SFUD),y) SRC_FILES += connect_flash_spi.c endif -ifeq ($(CONFIG_CONNECTION_COMMUNICATION_LORA_USING_SX1278),y) +ifeq ($(CONFIG_RESOURCES_SPI_LORA),y) SRC_DIR += third_party_spi_lora SRC_FILES += connect_lora_spi.c endif diff --git a/Ubiquitous/XiUOS/board/aiit-arm32-board/third_party_driver/spi/connect_lora_spi.c b/Ubiquitous/XiUOS/board/aiit-arm32-board/third_party_driver/spi/connect_lora_spi.c index 7688b59e2..b60d074c3 100644 --- a/Ubiquitous/XiUOS/board/aiit-arm32-board/third_party_driver/spi/connect_lora_spi.c +++ b/Ubiquitous/XiUOS/board/aiit-arm32-board/third_party_driver/spi/connect_lora_spi.c @@ -38,17 +38,17 @@ void SX1276InitIo(void) buspin = PinBusInitGet(); PinCfg.cmd = GPIO_CONFIG_MODE; - PinCfg.pin = CONNECTION_COMMUNICATION_LORA_SX12XX_DO0_PIN; + PinCfg.pin = SX12XX_DEVICE_DO0_PIN; PinCfg.mode = GPIO_CFG_INPUT; BusDrvConfigure(buspin->owner_driver, &configure_info); PinCfg.cmd = GPIO_CONFIG_MODE; - PinCfg.pin = CONNECTION_COMMUNICATION_LORA_SX12XX_DO1_PIN; + PinCfg.pin = SX12XX_DEVICE_DO1_PIN; PinCfg.mode = GPIO_CFG_INPUT; BusDrvConfigure(buspin->owner_driver, &configure_info); PinCfg.cmd = GPIO_CONFIG_MODE; - PinCfg.pin = CONNECTION_COMMUNICATION_LORA_SX12XX_DO2_PIN; + PinCfg.pin = SX12XX_DEVICE_DO2_PIN; PinCfg.mode = GPIO_CFG_INPUT; BusDrvConfigure(buspin->owner_driver, &configure_info); } @@ -60,7 +60,7 @@ inline uint8_t SX1276ReadDio0(void) struct BusBlockReadParam read_param; read_param.buffer = (void *)&PinStat; - PinStat.pin = CONNECTION_COMMUNICATION_LORA_SX12XX_DO0_PIN; + PinStat.pin = SX12XX_DEVICE_DO0_PIN; return BusDevReadData(buspin->owner_haldev, &read_param); } @@ -72,7 +72,7 @@ inline uint8_t SX1276ReadDio1(void) struct BusBlockReadParam read_param; read_param.buffer = (void *)&PinStat; - PinStat.pin = CONNECTION_COMMUNICATION_LORA_SX12XX_DO1_PIN; + PinStat.pin = SX12XX_DEVICE_DO1_PIN; return BusDevReadData(buspin->owner_haldev, &read_param); } @@ -84,7 +84,7 @@ inline uint8_t SX1276ReadDio2(void) struct BusBlockReadParam read_param; read_param.buffer = (void *)&PinStat; - PinStat.pin = CONNECTION_COMMUNICATION_LORA_SX12XX_DO2_PIN; + PinStat.pin = SX12XX_DEVICE_DO2_PIN; return BusDevReadData(buspin->owner_haldev, &read_param); } @@ -96,7 +96,7 @@ inline uint8_t SX1276ReadDio3(void) struct BusBlockReadParam read_param; read_param.buffer = (void *)&PinStat; - PinStat.pin = CONNECTION_COMMUNICATION_LORA_SX12XX_DO3_PIN; + PinStat.pin = SX12XX_DEVICE_DO3_PIN; return BusDevReadData(buspin->owner_haldev, &read_param); } @@ -108,7 +108,7 @@ inline uint8_t SX1276ReadDio4(void) struct BusBlockReadParam read_param; read_param.buffer = (void *)&PinStat; - PinStat.pin = CONNECTION_COMMUNICATION_LORA_SX12XX_DO4_PIN; + PinStat.pin = SX12XX_DEVICE_DO4_PIN; return BusDevReadData(buspin->owner_haldev, &read_param); } @@ -120,7 +120,7 @@ inline uint8_t SX1276ReadDio5(void) struct BusBlockReadParam read_param; read_param.buffer = (void *)&PinStat; - PinStat.pin = CONNECTION_COMMUNICATION_LORA_SX12XX_DO5_PIN; + PinStat.pin = SX12XX_DEVICE_DO5_PIN; return BusDevReadData(buspin->owner_haldev, &read_param); } @@ -129,11 +129,11 @@ inline void SX1276WriteRxTx(uint8_t txEnable) { if (txEnable != 0) { - + /*to do*/ } else { - + /*to do*/ } } @@ -152,18 +152,18 @@ void SX1276SetReset(uint8_t state) if (state == RADIO_RESET_ON) { PinCfg.cmd = GPIO_CONFIG_MODE; - PinCfg.pin = CONNECTION_COMMUNICATION_LORA_SX12XX_RST_PIN; + PinCfg.pin = SX12XX_DEVICE_RST_PIN; PinCfg.mode = GPIO_CFG_OUTPUT; BusDrvConfigure(buspin->owner_driver, &configure_info); PinStat.val = GPIO_LOW; - PinStat.pin = CONNECTION_COMMUNICATION_LORA_SX12XX_RST_PIN; + PinStat.pin = SX12XX_DEVICE_RST_PIN; BusDevWriteData(buspin->owner_haldev, &write_param); } else { PinCfg.cmd = GPIO_CONFIG_MODE; - PinCfg.pin = CONNECTION_COMMUNICATION_LORA_SX12XX_RST_PIN; + PinCfg.pin = SX12XX_DEVICE_RST_PIN; PinCfg.mode = GPIO_CFG_INPUT; BusDrvConfigure(buspin->owner_driver, &configure_info); } @@ -265,7 +265,7 @@ static uint32 SpiLoraWrite(void *dev, struct BusBlockWriteParam *write_param) uint8 i; char Msg[SPI_LORA_BUFFER_SIZE] = {0}; - if (write_param->size > 120) { + if (write_param->size > 256) { KPrintf("SpiLoraWrite ERROR:The message is too long!\n"); return ERROR; } else { @@ -297,10 +297,9 @@ static uint32 SpiLoraRead(void *dev, struct BusBlockReadParam *read_param) //while(Radio->Process() != RF_RX_DONE); //Radio->GetRxPacket(read_param->buffer, (uint16 *)&read_param->read_length); while(SX1276Process() != RF_RX_DONE); - SX1276GetRxPacket(read_param->buffer, (uint16 *)&read_param->read_length); - KPrintf("SpiLoraRead : %s\n", read_param->buffer); + SX1276GetRxPacket(read_param->buffer, (uint16 *)&read_param->read_length); - return EOK; + return read_param->read_length; } static uint32 SpiLoraOpen(void *dev) @@ -354,7 +353,7 @@ static uint32 SpiLoraOpen(void *dev) KPrintf("LoRa check failed!\n!"); } else { Radio = RadioDriverInit(); - KPrintf("LoRa check ok!\nNote: The length of the message that can be sent in a single time is 120 characters\n"); + KPrintf("LoRa check ok!\nNote: The length of the message that can be sent in a single time is 256 characters\n"); } lora_init_status = RET_TRUE; @@ -480,6 +479,7 @@ int LoraSx12xxSpiDeviceInit(void) return EOK; } +#define LORA_TEST #ifdef LORA_TEST /*Just for lora test*/ static struct Bus *bus; diff --git a/Ubiquitous/XiUOS/board/aiit-arm32-board/third_party_driver/spi/connect_spi.c b/Ubiquitous/XiUOS/board/aiit-arm32-board/third_party_driver/spi/connect_spi.c index c971521c6..5b8d68750 100644 --- a/Ubiquitous/XiUOS/board/aiit-arm32-board/third_party_driver/spi/connect_spi.c +++ b/Ubiquitous/XiUOS/board/aiit-arm32-board/third_party_driver/spi/connect_spi.c @@ -1221,7 +1221,7 @@ static uint32 Stm32SpiReadData(struct SpiHardwareDevice *spi_dev, struct SpiData { int state; x_size_t message_length, already_send_length; - uint16 send_length; + uint16 read_length; const uint8 *ReadBuf; NULL_PARAM_CHECK(spi_dev); @@ -1241,24 +1241,24 @@ static uint32 Stm32SpiReadData(struct SpiHardwareDevice *spi_dev, struct SpiData ReadBuf = spi_datacfg->rx_buff; while (message_length) { if (message_length > 65535){ - send_length = 65535; + read_length = 65535; message_length = message_length - 65535; } else { - send_length = message_length; + read_length = message_length; message_length = 0; } /* calculate the start address */ - already_send_length = spi_datacfg->length - send_length - message_length; + already_send_length = spi_datacfg->length - read_length - message_length; ReadBuf = (uint8 *)spi_datacfg->rx_buff + already_send_length; /* start once data exchange in DMA mode */ if (spi_datacfg->rx_buff) { - memset((uint8_t *)ReadBuf, 0xff, send_length); + memset((uint8_t *)ReadBuf, 0xff, read_length); if (StmSpi->spi_dma_flag & SPI_USING_RX_DMA_FLAG) { - state = SpiReceiveDma(*SpiInit, SpiInstance, StmSpi->dma.dma_rx.init, StmSpi->dma.dma_rx.instance, StmSpi->dma.dma_tx.init, StmSpi->dma.dma_tx.instance, (uint8_t *)ReadBuf, send_length); + state = SpiReceiveDma(*SpiInit, SpiInstance, StmSpi->dma.dma_rx.init, StmSpi->dma.dma_rx.instance, StmSpi->dma.dma_tx.init, StmSpi->dma.dma_tx.instance, (uint8_t *)ReadBuf, read_length); } else { - state = SpiReceive(*SpiInit, SpiInstance, (uint8_t *)ReadBuf, send_length, 1000); + state = SpiReceive(*SpiInit, SpiInstance, (uint8_t *)ReadBuf, read_length, 1000); } } diff --git a/Ubiquitous/XiUOS/board/aiit-riscv64-board/third_party_driver/spi/Kconfig b/Ubiquitous/XiUOS/board/aiit-riscv64-board/third_party_driver/spi/Kconfig index 077e7e5eb..de61c1ef5 100644 --- a/Ubiquitous/XiUOS/board/aiit-riscv64-board/third_party_driver/spi/Kconfig +++ b/Ubiquitous/XiUOS/board/aiit-riscv64-board/third_party_driver/spi/Kconfig @@ -29,38 +29,38 @@ if BSP_USING_SPI1 config BSP_SPI1_SS0_PIN int "spi1 ss0 pin number" default 9 - menuconfig CONNECTION_COMMUNICATION_LORA_USING_SX1278 - bool "Using spi lora SX1278" + menuconfig RESOURCES_SPI_LORA + bool "Using spi lora function" default n - if CONNECTION_COMMUNICATION_LORA_USING_SX1278 + if RESOURCES_SPI_LORA config SX12XX_DEVICE_NAME - string "SX1278 device name" + string "SX1278 lora device name" default "spi1_lora" - config CONNECTION_COMMUNICATION_LORA_SX12XX_RST_PIN + config SX12XX_DEVICE_RST_PIN int default 10 - config CONNECTION_COMMUNICATION_LORA_SX12XX_DO0_PIN + config SX12XX_DEVICE_DO0_PIN int default 10 - config CONNECTION_COMMUNICATION_LORA_SX12XX_DO1_PIN + config SX12XX_DEVICE_DO1_PIN int default 10 - config CONNECTION_COMMUNICATION_LORA_SX12XX_DO2_PIN + config SX12XX_DEVICE_DO2_PIN int default 10 - config CONNECTION_COMMUNICATION_LORA_SX12XX_DO3_PIN + config SX12XX_DEVICE_DO3_PIN int default 10 - config CONNECTION_COMMUNICATION_LORA_SX12XX_DO4_PIN + config SX12XX_DEVICE_DO4_PIN int default 10 - config CONNECTION_COMMUNICATION_LORA_SX12XX_DO5_PIN + config SX12XX_DEVICE_DO5_PIN int default 10 endif diff --git a/Ubiquitous/XiUOS/board/aiit-riscv64-board/third_party_driver/spi/Makefile b/Ubiquitous/XiUOS/board/aiit-riscv64-board/third_party_driver/spi/Makefile index 724218ab7..f64c853be 100644 --- a/Ubiquitous/XiUOS/board/aiit-riscv64-board/third_party_driver/spi/Makefile +++ b/Ubiquitous/XiUOS/board/aiit-riscv64-board/third_party_driver/spi/Makefile @@ -1,7 +1,7 @@ SRC_FILES := connect_spi.c hardware_spi.c -ifeq ($(CONFIG_CONNECTION_COMMUNICATION_LORA_USING_SX1278),y) +ifeq ($(CONFIG_RESOURCES_SPI_LORA),y) SRC_DIR += third_party_spi_lora SRC_FILES += connect_lora_spi.c endif diff --git a/Ubiquitous/XiUOS/board/aiit-riscv64-board/third_party_driver/spi/connect_lora_spi.c b/Ubiquitous/XiUOS/board/aiit-riscv64-board/third_party_driver/spi/connect_lora_spi.c index b27871d77..598d156aa 100644 --- a/Ubiquitous/XiUOS/board/aiit-riscv64-board/third_party_driver/spi/connect_lora_spi.c +++ b/Ubiquitous/XiUOS/board/aiit-riscv64-board/third_party_driver/spi/connect_lora_spi.c @@ -38,17 +38,17 @@ void SX1276InitIo(void) buspin = PinBusInitGet(); PinCfg.cmd = GPIO_CONFIG_MODE; - PinCfg.pin = CONNECTION_COMMUNICATION_LORA_SX12XX_DO0_PIN; + PinCfg.pin = SX12XX_DEVICE_DO0_PIN; PinCfg.mode = GPIO_CFG_INPUT; BusDrvConfigure(buspin->owner_driver, &configure_info); PinCfg.cmd = GPIO_CONFIG_MODE; - PinCfg.pin = CONNECTION_COMMUNICATION_LORA_SX12XX_DO1_PIN; + PinCfg.pin = SX12XX_DEVICE_DO1_PIN; PinCfg.mode = GPIO_CFG_INPUT; BusDrvConfigure(buspin->owner_driver, &configure_info); PinCfg.cmd = GPIO_CONFIG_MODE; - PinCfg.pin = CONNECTION_COMMUNICATION_LORA_SX12XX_DO2_PIN; + PinCfg.pin = SX12XX_DEVICE_DO2_PIN; PinCfg.mode = GPIO_CFG_INPUT; BusDrvConfigure(buspin->owner_driver, &configure_info); } @@ -60,7 +60,7 @@ inline uint8_t SX1276ReadDio0(void) struct BusBlockReadParam read_param; read_param.buffer = (void *)&PinStat; - PinStat.pin = CONNECTION_COMMUNICATION_LORA_SX12XX_DO0_PIN; + PinStat.pin = SX12XX_DEVICE_DO0_PIN; return BusDevReadData(buspin->owner_haldev, &read_param); } @@ -72,7 +72,7 @@ inline uint8_t SX1276ReadDio1(void) struct BusBlockReadParam read_param; read_param.buffer = (void *)&PinStat; - PinStat.pin = CONNECTION_COMMUNICATION_LORA_SX12XX_DO1_PIN; + PinStat.pin = SX12XX_DEVICE_DO1_PIN; return BusDevReadData(buspin->owner_haldev, &read_param); } @@ -84,7 +84,7 @@ inline uint8_t SX1276ReadDio2(void) struct BusBlockReadParam read_param; read_param.buffer = (void *)&PinStat; - PinStat.pin = CONNECTION_COMMUNICATION_LORA_SX12XX_DO2_PIN; + PinStat.pin = SX12XX_DEVICE_DO2_PIN; return BusDevReadData(buspin->owner_haldev, &read_param); } @@ -96,7 +96,7 @@ inline uint8_t SX1276ReadDio3(void) struct BusBlockReadParam read_param; read_param.buffer = (void *)&PinStat; - PinStat.pin = CONNECTION_COMMUNICATION_LORA_SX12XX_DO3_PIN; + PinStat.pin = SX12XX_DEVICE_DO3_PIN; return BusDevReadData(buspin->owner_haldev, &read_param); } @@ -108,7 +108,7 @@ inline uint8_t SX1276ReadDio4(void) struct BusBlockReadParam read_param; read_param.buffer = (void *)&PinStat; - PinStat.pin = CONNECTION_COMMUNICATION_LORA_SX12XX_DO4_PIN; + PinStat.pin = SX12XX_DEVICE_DO4_PIN; return BusDevReadData(buspin->owner_haldev, &read_param); } @@ -120,7 +120,7 @@ inline uint8_t SX1276ReadDio5(void) struct BusBlockReadParam read_param; read_param.buffer = (void *)&PinStat; - PinStat.pin = CONNECTION_COMMUNICATION_LORA_SX12XX_DO5_PIN; + PinStat.pin = SX12XX_DEVICE_DO5_PIN; return BusDevReadData(buspin->owner_haldev, &read_param); } @@ -145,18 +145,18 @@ void SX1276SetReset(uint8_t state) if (state == RADIO_RESET_ON) { PinCfg.cmd = GPIO_CONFIG_MODE; - PinCfg.pin = CONNECTION_COMMUNICATION_LORA_SX12XX_RST_PIN; + PinCfg.pin = SX12XX_DEVICE_RST_PIN; PinCfg.mode = GPIO_CFG_OUTPUT; BusDrvConfigure(buspin->owner_driver, &configure_info); PinStat.val = GPIO_LOW; - PinStat.pin = CONNECTION_COMMUNICATION_LORA_SX12XX_RST_PIN; + PinStat.pin = SX12XX_DEVICE_RST_PIN; BusDevWriteData(buspin->owner_haldev, &write_param); } else { PinCfg.cmd = GPIO_CONFIG_MODE; - PinCfg.pin = CONNECTION_COMMUNICATION_LORA_SX12XX_RST_PIN; + PinCfg.pin = SX12XX_DEVICE_RST_PIN; PinCfg.mode = GPIO_CFG_INPUT; BusDrvConfigure(buspin->owner_driver, &configure_info); } @@ -255,10 +255,7 @@ static uint32 SpiLoraWrite(void *dev, struct BusBlockWriteParam *write_param) NULL_PARAM_CHECK(dev); NULL_PARAM_CHECK(write_param); - uint8 i; - char Msg[SPI_LORA_BUFFER_SIZE] = {0}; - - if (write_param->size > 120) { + if (write_param->size > 256) { KPrintf("SpiLoraWrite ERROR:The message is too long!\n"); return ERROR; } else { @@ -290,10 +287,9 @@ static uint32 SpiLoraRead(void *dev, struct BusBlockReadParam *read_param) //while(Radio->Process() != RF_RX_DONE); //Radio->GetRxPacket(read_param->buffer, (uint16 *)&read_param->read_length); while(SX1276Process() != RF_RX_DONE); - SX1276GetRxPacket(read_param->buffer, (uint16 *)&read_param->read_length); - KPrintf("SpiLoraRead : %s\n", read_param->buffer); + SX1276GetRxPacket(read_param->buffer, (uint16 *)&read_param->read_length); - return EOK; + return read_param->read_length; } static uint32 SpiLoraOpen(void *dev) @@ -347,7 +343,7 @@ static uint32 SpiLoraOpen(void *dev) KPrintf("LoRa check failed!\n!"); } else { Radio = RadioDriverInit(); - KPrintf("LoRa check ok!\nNote: The length of the message that can be sent in a single time is 120 characters\n"); + KPrintf("LoRa check ok!\nNote: The length of the message that can be sent in a single time is 256 characters\n"); } lora_init_status = RET_TRUE; @@ -465,6 +461,7 @@ int LoraSx12xxSpiDeviceInit(void) return EOK; } +#define LORA_TEST #ifdef LORA_TEST /*Just for lora test*/ static struct Bus *bus; diff --git a/Ubiquitous/XiUOS/board/stm32f407-st-discovery/third_party_driver/spi/connect_spi.c b/Ubiquitous/XiUOS/board/stm32f407-st-discovery/third_party_driver/spi/connect_spi.c index 9e68aac22..a78ae70e2 100644 --- a/Ubiquitous/XiUOS/board/stm32f407-st-discovery/third_party_driver/spi/connect_spi.c +++ b/Ubiquitous/XiUOS/board/stm32f407-st-discovery/third_party_driver/spi/connect_spi.c @@ -1125,7 +1125,7 @@ static uint32 Stm32SpiReadData(struct SpiHardwareDevice *spi_dev, struct SpiData { int state; x_size_t message_length, already_send_length; - uint16 send_length; + uint16 read_length; const uint8 *ReadBuf; NULL_PARAM_CHECK(spi_dev); @@ -1145,24 +1145,24 @@ static uint32 Stm32SpiReadData(struct SpiHardwareDevice *spi_dev, struct SpiData ReadBuf = spi_datacfg->rx_buff; while (message_length) { if (message_length > 65535) { - send_length = 65535; + read_length = 65535; message_length = message_length - 65535; } else { - send_length = message_length; + read_length = message_length; message_length = 0; } /* calculate the start address */ - already_send_length = spi_datacfg->length - send_length - message_length; + already_send_length = spi_datacfg->length - read_length - message_length; ReadBuf = (uint8 *)spi_datacfg->rx_buff + already_send_length; /* start once data exchange in DMA mode */ if (spi_datacfg->rx_buff) { - memset((uint8_t *)ReadBuf, 0xff, send_length); + memset((uint8_t *)ReadBuf, 0xff, read_length); if (StmSpi->spi_dma_flag & SPI_USING_RX_DMA_FLAG) { - state = SpiReceiveDma(*spi_init, spi_instance, StmSpi->dma.dma_rx.init, StmSpi->dma.dma_rx.instance, StmSpi->dma.dma_tx.init, StmSpi->dma.dma_tx.instance, (uint8_t *)ReadBuf, send_length); + state = SpiReceiveDma(*spi_init, spi_instance, StmSpi->dma.dma_rx.init, StmSpi->dma.dma_rx.instance, StmSpi->dma.dma_tx.init, StmSpi->dma.dma_tx.instance, (uint8_t *)ReadBuf, read_length); } else { - state = SpiReceive(*spi_init, spi_instance, (uint8_t *)ReadBuf, send_length, 1000); + state = SpiReceive(*spi_init, spi_instance, (uint8_t *)ReadBuf, read_length, 1000); } } diff --git a/Ubiquitous/XiUOS/kernel/include/xs_init.h b/Ubiquitous/XiUOS/kernel/include/xs_init.h index 11b2ac6b3..dbf0f3659 100644 --- a/Ubiquitous/XiUOS/kernel/include/xs_init.h +++ b/Ubiquitous/XiUOS/kernel/include/xs_init.h @@ -37,6 +37,7 @@ void InitCmpts(void); extern int VfsInit(void); extern int WorkSysWorkQueueInit(void); extern int FlashW25qxxSpiDeviceInit(void); +extern int LoraSx12xxSpiDeviceInit(void); extern int FatfsInit(void); extern int Ch376fsInit(void); extern int LibcSystemInit(void); diff --git a/Ubiquitous/XiUOS/kernel/thread/init.c b/Ubiquitous/XiUOS/kernel/thread/init.c index 6369e3529..68d9a3bce 100644 --- a/Ubiquitous/XiUOS/kernel/thread/init.c +++ b/Ubiquitous/XiUOS/kernel/thread/init.c @@ -81,6 +81,10 @@ struct InitSequenceDesc device_init[] = #ifdef RESOURCES_SPI_SFUD { "W25Qxx_spi", FlashW25qxxSpiDeviceInit}, #endif +#endif + +#ifdef RESOURCES_SPI_LORA + {"LORA_spi", LoraSx12xxSpiDeviceInit}, #endif { " NONE ", NONE }, };