From 7a172fd1366df6157123609bbd92151568511ecd Mon Sep 17 00:00:00 2001 From: Liu_Weichao Date: Wed, 17 Nov 2021 17:37:04 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81support=20adapter=5Flora=20gateway=20?= =?UTF-8?q?and=20client=20state-machine-model;2=E3=80=81fix=20aiit-arm32-b?= =?UTF-8?q?oard=20usb=20compile=20error.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- APP_Framework/Framework/connection/adapter.c | 5 +- APP_Framework/Framework/connection/adapter.h | 4 +- .../Framework/connection/lora/adapter_lora.c | 284 +++++-- .../Framework/connection/lora/sx1278/sx1278.c | 7 +- .../third_party_driver/spi/connect_lora_spi.c | 26 +- .../third_party_driver/spi/connect_spi.c | 801 ++++++++---------- .../src/radio/sx1276-LoRa.c | 5 +- .../usb/STM32_USB_OTG_Driver/inc/usb_conf.h | 4 +- .../third_party_driver/spi/connect_lora_spi.c | 23 +- .../third_party_driver/spi/connect_spi.c | 6 +- .../src/radio/sx1276-LoRa.c | 5 +- .../third_party_driver/spi/connect_spi.c | 6 +- .../third_party_driver/spi/connect_spi.c | 13 +- Ubiquitous/XiUOS/resources/include/dev_spi.h | 2 +- Ubiquitous/XiUOS/resources/spi/dev_spi.c | 83 +- .../usb/third_party_usb/usbhost/core/core.c | 14 +- .../usb/third_party_usb/usbhost/core/hub.c | 8 +- 17 files changed, 721 insertions(+), 575 deletions(-) diff --git a/APP_Framework/Framework/connection/adapter.c b/APP_Framework/Framework/connection/adapter.c index 1707955a7..6de8a2aa6 100644 --- a/APP_Framework/Framework/connection/adapter.c +++ b/APP_Framework/Framework/connection/adapter.c @@ -419,9 +419,10 @@ int AdapterDeviceJoin(struct Adapter *adapter, unsigned char *priv_net_group) /** * @description: Adapter disconnect from ip net or private net group * @param adapter - adapter device pointer + * @param priv_net_group - private net group for PRIVATE_PROTOCOL quit function * @return success: 0 , failure: other */ -int AdapterDeviceDisconnect(struct Adapter *adapter) +int AdapterDeviceDisconnect(struct Adapter *adapter, unsigned char *priv_net_group) { if (!adapter) return -1; @@ -432,7 +433,7 @@ int AdapterDeviceDisconnect(struct Adapter *adapter) if (NULL == priv_done->quit) return -1; - return priv_done->quit(adapter); + return priv_done->quit(adapter, priv_net_group); } else if (IP_PROTOCOL == adapter->net_protocol) { struct IpProtocolDone *ip_done = (struct IpProtocolDone *)adapter->done; diff --git a/APP_Framework/Framework/connection/adapter.h b/APP_Framework/Framework/connection/adapter.h index 15f6f578b..a70c1e6e9 100644 --- a/APP_Framework/Framework/connection/adapter.h +++ b/APP_Framework/Framework/connection/adapter.h @@ -151,7 +151,7 @@ struct PrivProtocolDone 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); + int (*quit)(struct Adapter *adapter, unsigned char *priv_net_group); }; struct Adapter @@ -212,7 +212,7 @@ int AdapterDeviceConnect(struct Adapter *adapter, enum NetRoleType net_role, con int AdapterDeviceJoin(struct Adapter *adapter, unsigned char *priv_net_group); /*Adapter disconnect from ip net or private net group*/ -int AdapterDeviceDisconnect(struct Adapter *adapter); +int AdapterDeviceDisconnect(struct Adapter *adapter, unsigned char *priv_net_group); /*Set up to net*/ int AdapterDeviceSetUp(struct Adapter *adapter); diff --git a/APP_Framework/Framework/connection/lora/adapter_lora.c b/APP_Framework/Framework/connection/lora/adapter_lora.c index ab3c08007..c62e6a590 100644 --- a/APP_Framework/Framework/connection/lora/adapter_lora.c +++ b/APP_Framework/Framework/connection/lora/adapter_lora.c @@ -37,6 +37,10 @@ extern AdapterProductInfoType Sx1278Attach(struct Adapter *adapter); #define ADAPTER_LORA_DATA_TYPE_USERDATA 0x0E #define ADAPTER_LORA_DATA_TYPE_CMD 0x0F +//need to change status if the lora client wants to quit the net when timeout or a certain event +//eg.can also use sem to trigger quit function +static int g_adapter_lora_quit_flag = 0; + enum ClientState { CLIENT_DISCONNECT = 0, @@ -51,12 +55,22 @@ enum DataType LORA_USER_DATA, }; +enum LoraGatewayState { + LORA_STATE_IDLE = 0, + LORA_JOIN_NET, + LORA_QUIT_NET, + LORA_RECV_DATA, +}; + +static uint8 LoraGatewayState = LORA_STATE_IDLE; + struct LoraGatewayParam { uint8 gateway_id; uint16 panid; uint8 client_id[ADAPTER_LORA_CLIENT_NUM]; int client_num; + int receive_data_sem; }; struct LoraClientParam @@ -144,18 +158,30 @@ static int LoraCrc16Check(uint8 *data, uint16 length) static int LoraGatewayReply(struct Adapter *adapter, struct LoraDataFormat *gateway_recv_data) { int i; + int client_join_flag = 0; 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; + + for (i = 0; i < gateway->client_num; i ++) { + if (gateway_recv_data->client_id == gateway->client_id[i]) { + printf("Lora client_%d 0x%x has already join net 0x%x\n", i, gateway_recv_data->client_id, gateway->gateway_id); + client_join_flag = 1; + break; + } + } + + if (!client_join_flag) { + 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->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); @@ -231,8 +257,8 @@ static int LoraGatewaySendCmd(struct Adapter *adapter, unsigned char client_id, * @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*/ +{ + /*User needs 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; @@ -307,37 +333,27 @@ static int LoraClientSendData(struct Adapter *adapter, void *send_buf, int lengt /** * @description: Lora Gateway receive data analyzing * @param adapter Lora adapter pointer + * @param gateway_recv_data Lora gateway receive data pointer */ -static int LoraGateWayDataAnalyze(struct Adapter *adapter) +static int LoraGateWayDataAnalyze(struct Adapter *adapter, struct LoraDataFormat *gateway_recv_data) { 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) { + 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) + 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); + ret = LoraGatewayReply(adapter, gateway_recv_data); break; case ADAPTER_LORA_DATA_TYPE_USERDATA : - ret = LoraGatewayHandleData(adapter, &gateway_recv_data); + ret = LoraGatewayHandleData(adapter, gateway_recv_data); break; default: break; @@ -357,31 +373,37 @@ static int LoraClientDataAnalyze(struct Adapter *adapter, void *send_buf, int le { int ret = 0; - struct LoraDataFormat client_recv_data; + struct LoraDataFormat *client_recv_data = PrivMalloc(sizeof(struct LoraDataFormat)); - memset(&client_recv_data, 0, sizeof(struct LoraDataFormat)); + 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"); + ret = AdapterDeviceRecv(adapter, client_recv_data, sizeof(struct LoraDataFormat)); + if (0 == ret) { + printf("LoraClientDataAnalyze recv error.Just return\n"); + PrivFree(client_recv_data); return -1; } - if ((ADAPTER_LORA_DATA_HEAD == client_recv_data.flame_head) && - (ADAPTER_LORA_NET_PANID == client_recv_data.panid)) { + 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 (client_recv_data.client_id == adapter->net_role_id) { - switch (client_recv_data.data_type) + if ((ADAPTER_LORA_DATA_HEAD == client_recv_data->flame_head) && + (ADAPTER_LORA_NET_PANID == client_recv_data->panid)) { + if (LoraCrc16Check((uint8 *)client_recv_data, sizeof(struct LoraDataFormat)) < 0) { + printf("LoraClientDataAnalyze CRC check error\n"); + PrivFree(client_recv_data); + return -1; + } + + //only handle this client_id information from gateway + 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); + ret = LoraClientUpdate(adapter, client_recv_data); break; case ADAPTER_LORA_DATA_TYPE_CMD : if (send_buf) { @@ -394,6 +416,7 @@ static int LoraClientDataAnalyze(struct Adapter *adapter, void *send_buf, int le } } + PrivFree(client_recv_data); return ret; } @@ -422,6 +445,104 @@ static int LoraClientJoinNet(struct Adapter *adapter, unsigned short panid) return 0; } +/** + * @description: Lora Client quit net function + * @param adapter Lora adapter pointer + * @param panid Lora net panid + */ +static int LoraClientQuitNet(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_QUIT; + client_join_data.client_id = adapter->net_role_id; + client_join_data.crc16 = LoraCrc16((uint8 *)&client_join_data, sizeof(struct LoraDataFormat) - 2); + + if (AdapterDeviceDisconnect(adapter, (uint8 *)&client_join_data) < 0) { + return -1; + } + + return 0; +} + +/** + * @description: Lora Gateway Process function + * @param lora_adapter Lora adapter pointer + * @param gateway Lora gateway pointer + * @param gateway_recv_data Lora gateway receive data pointer + */ +int LoraGatewayProcess(struct Adapter *lora_adapter, struct LoraGatewayParam *gateway, struct LoraDataFormat *gateway_recv_data) +{ + int i, ret = 0; + switch (LoraGatewayState) + { + case LORA_STATE_IDLE: + ret = AdapterDeviceRecv(lora_adapter, gateway_recv_data, sizeof(struct LoraDataFormat)); + if (0 == ret) { + printf("LoraGatewayProcess IDLE recv error.Just return\n"); + break; + } + + if (ADAPTER_LORA_DATA_TYPE_JOIN == gateway_recv_data->data_type) { + LoraGatewayState = LORA_JOIN_NET; + } else if (ADAPTER_LORA_DATA_TYPE_QUIT == gateway_recv_data->data_type) { + LoraGatewayState = LORA_QUIT_NET; + } else { + LoraGatewayState = LORA_STATE_IDLE; + } + break; + case LORA_JOIN_NET: + case LORA_QUIT_NET: + ret = LoraGateWayDataAnalyze(lora_adapter, gateway_recv_data); + if (ret < 0) { + printf("LoraGateWayDataAnalyze state %d error, re-send data cmd to client\n", LoraGatewayState); + PrivTaskDelay(500); + } + LoraGatewayState = LORA_RECV_DATA; + break; + case LORA_RECV_DATA: + for (i = 0; i < gateway->client_num; i ++) { + if (gateway->client_id[i]) { + printf("LoraGatewayProcess send to client %d for data\n", gateway->client_id[i]); + ret = LoraGatewaySendCmd(lora_adapter, gateway->client_id[i], ADAPTER_LORA_DATA_TYPE_CMD); + if (ret < 0) { + printf("LoraGatewaySendCmd client ID %d error\n", gateway->client_id[i]); + PrivTaskDelay(500); + continue; + } + + ret = AdapterDeviceRecv(lora_adapter, gateway_recv_data, sizeof(struct LoraDataFormat)); + if (0 == ret) { + printf("LoraGatewayProcess recv error.Just return\n"); + continue; + } + + if (ADAPTER_LORA_DATA_TYPE_JOIN == gateway_recv_data->data_type) { + LoraGatewayState = LORA_JOIN_NET; + } else if (ADAPTER_LORA_DATA_TYPE_QUIT == gateway_recv_data->data_type) { + LoraGatewayState = LORA_QUIT_NET; + } else { + ret = LoraGateWayDataAnalyze(lora_adapter, gateway_recv_data); + if (ret < 0) { + printf("LoraGateWayDataAnalyze error, re-send data cmd to client\n"); + PrivTaskDelay(500); + } + } + } + } + break; + default: + break; + } + + return 0; +} + /** * @description: Lora Gateway task * @param parameter - Lora adapter pointer @@ -432,30 +553,10 @@ static void *LoraGatewayTask(void *parameter) int ret = 0; struct Adapter *lora_adapter = (struct Adapter *)parameter; struct LoraGatewayParam *gateway = (struct LoraGatewayParam *)lora_adapter->adapter_param; + struct LoraDataFormat gateway_recv_data; 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; - } - } - } - } + LoraGatewayProcess(lora_adapter, gateway, &gateway_recv_data); } return 0; @@ -467,28 +568,29 @@ static void *LoraGatewayTask(void *parameter) */ static void *LoraClientDataTask(void *parameter) { - int ret = 0; + int i, ret = 0; + int join_times = 10; struct Adapter *lora_adapter = (struct Adapter *)parameter; struct LoraClientParam *client = (struct LoraClientParam *)lora_adapter->adapter_param; + //set lora_send_buf for test 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"); + sprintf(lora_send_buf, "Lora client %d adapter test\n", client->client_id); while (1) { PrivTaskDelay(100); - if (CLIENT_DISCONNECT == client->client_state) { + if ((CLIENT_DISCONNECT == client->client_state) && (!g_adapter_lora_quit_flag)) { 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"); + printf("LoraClientDataAnalyze error, reconnect to gateway\n"); PrivTaskDelay(500); continue; } @@ -497,12 +599,46 @@ static void *LoraClientDataTask(void *parameter) 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"); + printf("LoraClientDataAnalyze error, wait for next data cmd\n"); PrivTaskDelay(500); continue; } } } + + return 0; +} + +/** + * @description: Lora Client quit task + * @param parameter - Lora adapter pointer + */ +static void *LoraClientQuitTask(void *parameter) +{ + int ret = 0; + struct Adapter *lora_adapter = (struct Adapter *)parameter; + struct LoraClientParam *client = (struct LoraClientParam *)lora_adapter->adapter_param; + + while (1) { + PrivTaskDelay(100); + + if ((CLIENT_CONNECT == client->client_state) && (g_adapter_lora_quit_flag)) { + ret = LoraClientQuitNet(lora_adapter, client->panid); + if (ret < 0) { + printf("LoraClientQuitNet error panid 0x%x, re-quit net\n", client->panid); + continue; + } + + ret = LoraClientDataAnalyze(lora_adapter, NULL, 0); + if (ret < 0) { + printf("LoraClientQuitTask LoraClientDataAnalyze error\n"); + PrivTaskDelay(500); + continue; + } + } + } + + return 0; } /*******************LORA ADAPTER FUNCTION********************/ @@ -607,6 +743,7 @@ int AdapterLoraInit(void) static pthread_t lora_gateway_task; #else //AS_LORA_CLIENT_ROLE static pthread_t lora_client_data_task; +static pthread_t lora_client_quit_task; #endif int AdapterLoraTest(void) @@ -619,7 +756,7 @@ int AdapterLoraTest(void) #ifdef AS_LORA_GATEWAY_ROLE pthread_attr_t lora_gateway_attr; lora_gateway_attr.schedparam.sched_priority = 20; - lora_gateway_attr.stacksize = 4096; + lora_gateway_attr.stacksize = 2048; PrivTaskCreate(&lora_gateway_task, &lora_gateway_attr, &LoraGatewayTask, (void *)adapter); PrivTaskStartup(&lora_gateway_task); @@ -628,10 +765,15 @@ int AdapterLoraTest(void) //create lora client task pthread_attr_t lora_client_attr; lora_client_attr.schedparam.sched_priority = 20; - lora_client_attr.stacksize = 4096; + lora_client_attr.stacksize = 2048; PrivTaskCreate(&lora_client_data_task, &lora_client_attr, &LoraClientDataTask, (void *)adapter); PrivTaskStartup(&lora_client_data_task); + + lora_client_attr.stacksize = 1024; + + PrivTaskCreate(&lora_client_quit_task, &lora_client_attr, &LoraClientQuitTask, (void *)adapter); + PrivTaskStartup(&lora_client_quit_task); #endif return 0; diff --git a/APP_Framework/Framework/connection/lora/sx1278/sx1278.c b/APP_Framework/Framework/connection/lora/sx1278/sx1278.c index 3b32d6e18..6c97e9aa0 100644 --- a/APP_Framework/Framework/connection/lora/sx1278/sx1278.c +++ b/APP_Framework/Framework/connection/lora/sx1278/sx1278.c @@ -103,17 +103,18 @@ static int Sx1278Send(struct Adapter *adapter, const void *buf, size_t len) */ static int Sx1278Recv(struct Adapter *adapter, void *buf, size_t len) { - return PrivRead(adapter->fd, buf, len);; + return PrivRead(adapter->fd, buf, len); } /** * @description: SX1278 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 Sx1278Quit(struct Adapter *adapter) +static int Sx1278Quit(struct Adapter *adapter, unsigned char *priv_net_group) { - /*to do*/ + PrivWrite(adapter->fd, (void *)priv_net_group, 144); return 0; } 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 b60d074c3..ec61b30b4 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 @@ -262,9 +262,6 @@ 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 > 256) { KPrintf("SpiLoraWrite ERROR:The message is too long!\n"); return ERROR; @@ -289,15 +286,32 @@ static uint32 SpiLoraRead(void *dev, struct BusBlockReadParam *read_param) { NULL_PARAM_CHECK(dev); NULL_PARAM_CHECK(read_param); + + int read_times = 100; //Radio->StartRx(); SX1276StartRx(); KPrintf("SpiLoraRead Ready!\n"); + + while (read_times) { + if (SX1276Process() != RF_RX_DONE) { + read_times --; + MdelayKTask(500); + } else { + break; + } + } + + if (read_times > 0) { + SX1276GetRxPacket(read_param->buffer, (uint16 *)&read_param->read_length); + } else { + read_param->read_length = 0; + } //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); + // while(SX1276Process() != RF_RX_DONE); + // SX1276GetRxPacket(read_param->buffer, (uint16 *)&read_param->read_length); return read_param->read_length; } @@ -479,7 +493,7 @@ int LoraSx12xxSpiDeviceInit(void) return EOK; } -#define LORA_TEST +//#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 5b8d68750..813a46a64 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 @@ -91,15 +91,11 @@ Modification: #endif /** - * This function SPI device initialization - * - * @param SpiDrv SPI device structure pointer - * - * @param cfg SPI device operating mode configuration structure pointer - * - * @return if successful return EOK + * This function SPI device initialization + * @param SpiDrv SPI device structure pointer + * @param cfg SPI device operating mode configuration structure pointer + * @return if successful return EOK */ - static x_err_t Stm32SpiInit(struct Stm32Spi *SpiDrv, struct SpiMasterParam *cfg) { NULL_PARAM_CHECK(SpiDrv); @@ -248,20 +244,15 @@ static x_err_t Stm32SpiInit(struct Stm32Spi *SpiDrv, struct SpiMasterParam *cfg) return EOK; } -/** - * This function Use DMA during spi transfer - * - * @param spi_bus SPI bus handle - * - * @param SettingLen Set data length - * - * @param RxBaseAddr Base address for receiving data - * - * @param TxBaseAddr Base address for sending data - * - * @return none - */ +/** + * This function Use DMA during spi transfer + * @param spi_bus SPI bus handle + * @param SettingLen Set data length + * @param RxBaseAddr Base address for receiving data + * @param TxBaseAddr Base address for sending data + * @return none + */ static void DmaSpiConfig(struct SpiBus *spi_bus, uint32_t setting_len, void *rx_base_addr, void *tx_base_addr) { struct Stm32Spi *spi = (struct Stm32Spi *)spi_bus->private_data; @@ -349,11 +340,9 @@ static void DmaSpiConfig(struct SpiBus *spi_bus, uint32_t setting_len, void *rx_ } /** - * This function DMA receiving completion interrupt - * + * This function DMA receiving completion interrupt * @param spi_bus SPI bus pointer - * - * @return none + * @return none */ static void DmaRxDoneIsr(struct SpiBus *spi_bus) { @@ -373,11 +362,9 @@ static void DmaRxDoneIsr(struct SpiBus *spi_bus) } /** - * This function DMA sending completion interrupt - * + * This function DMA sending completion interrupt * @param spi_bus SPI bus pointer - * - * @return none + * @return none */ static void DmaTxDoneIsr(struct SpiBus *spi_bus) { @@ -395,22 +382,16 @@ static void DmaTxDoneIsr(struct SpiBus *spi_bus) DMA_ClearFlag(spi->dma.dma_tx.instance, spi->spi_dma_flag); } } + /** - * This function SPI wait timeout function - * - * @param SpiInit SPI Init structure definition - * - * @param SpiInstance SPI control handle - * - * @param Flag SPI Status flag - * - * @param State SPI status - * - * @param Timeout Overflow time - * - * @param Tickstart Starting time - * - * @return if successful return EOK + * This function SPI wait timeout function + * @param SpiInit SPI Init structure definition + * @param SpiInstance SPI control handle + * @param Flag SPI Status flag + * @param State SPI status + * @param Timeout Overflow time + * @param Tickstart Starting time + * @return if successful return EOK */ static int SpiWaitUntilTimeout(SPI_InitTypeDef SpiInit, SPI_TypeDef *SpiInstance, uint32_t Flag, uint32_t State, uint32_t Timeout, uint32_t Tickstart) { @@ -434,22 +415,16 @@ static int SpiWaitUntilTimeout(SPI_InitTypeDef SpiInit, SPI_TypeDef *SpiInstance } return 0; } + /** - * This function SPI sends data through DMA - * - * @param SpiInit SPI Init structure - * - * @param SpiInstance SPI control handle - * - * @param dma_init DMA Init structure - * - * @param dma_instance DMA Controller - * - * @param pData Send data buffer address - * - * @param Size Amount of data sent - * - * @return if successful return EOK + * This function SPI sends data through DMA + * @param SpiInit SPI Init structure + * @param SpiInstance SPI control handle + * @param dma_init DMA Init structure + * @param dma_instance DMA Controller + * @param pData Send data buffer address + * @param Size Amount of data sent + * @return if successful return EOK */ int SpiTransmitDma(SPI_InitTypeDef SpiInit, SPI_TypeDef *SpiInstance, DMA_InitTypeDef dma_init, DMA_Stream_TypeDef *dma_instance, uint8_t *pData, uint16_t Size) { @@ -520,28 +495,19 @@ int SpiTransmitDma(SPI_InitTypeDef SpiInit, SPI_TypeDef *SpiInstance, DMA_InitTy error : return errorcode; } + /** - * This function SPI carries out duplex communication through DMA - * - * @param SpiInit SPI Init structure - * - * @param SpiInstance SPI control handle - * - * @param dmarx_init DMA Init structure---Rx - * + * This function SPI carries out duplex communication through DMA + * @param SpiInit SPI Init structure + * @param SpiInstance SPI control handle + * @param dmarx_init DMA Init structure---Rx * @param dmarx_instance DMA Controller - * - * @param dmatx_init DMA Init structure---Tx - * + * @param dmatx_init DMA Init structure---Tx * @param dmatx_instance DMA Controller - * - * @param pTxData Send data buffer address - * - * @param pRxData Receive data buffer address - * - * @param Size Amount of data - * - * @return if successful return EOK + * @param pTxData Send data buffer address + * @param pRxData Receive data buffer address + * @param Size Amount of data + * @return if successful return EOK */ int SpiTransmitreceiveDma(SPI_InitTypeDef SpiInit, SPI_TypeDef *SpiInstance, DMA_InitTypeDef dmarx_init, DMA_Stream_TypeDef *dmarx_instance, DMA_InitTypeDef dmatx_init, DMA_Stream_TypeDef *dmatx_instance, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size) { @@ -653,26 +619,18 @@ int SpiTransmitreceiveDma(SPI_InitTypeDef SpiInit, SPI_TypeDef *SpiInstance, DMA error : return errorcode; } + /** - * This function SPI receives data through DMA - * - * @param SpiInit SPI Init structure - * - * @param SpiInstance SPI control handle - * - * @param dmarx_init DMA Init structure---Rx - * + * This function SPI receives data through DMA + * @param SpiInit SPI Init structure + * @param SpiInstance SPI control handle + * @param dmarx_init DMA Init structure---Rx * @param dmarx_instance DMA Controller - * - * @param dmatx_init DMA Init structure---Tx - * + * @param dmatx_init DMA Init structure---Tx * @param dmatx_instance DMA Controller - * - * @param pData Receive data buffer address - * - * @param Size Amount of data - * - * @return if successful return EOK + * @param pData Receive data buffer address + * @param Size Amount of data + * @return if successful return EOK */ int SpiReceiveDma(SPI_InitTypeDef SpiInit, SPI_TypeDef *SpiInstance, DMA_InitTypeDef dmarx_init, DMA_Stream_TypeDef *dmarx_instance, DMA_InitTypeDef dmatx_init, DMA_Stream_TypeDef *dmatx_instance, uint8_t *pData, uint16_t Size) { @@ -748,20 +706,15 @@ int SpiReceiveDma(SPI_InitTypeDef SpiInit, SPI_TypeDef *SpiInstance, DMA_InitTyp error: return errorcode; } + /** - * This function SPI receives data - * - * @param SpiInit SPI Init structure - * - * @param SpiInstance SPI control handle - * - * @param pData Transmit data buffer address - * - * @param Size Amount of data - * - * @param Timeout waiting time - * - * @return if successful return EOK + * This function SPI receives data + * @param SpiInit SPI Init structure + * @param SpiInstance SPI control handle + * @param pData Transmit data buffer address + * @param Size Amount of data + * @param Timeout waiting time + * @return if successful return EOK */ int SpiTransmit(SPI_InitTypeDef SpiInit, SPI_TypeDef *SpiInstance, uint8_t *pData, uint16_t Size, uint32_t Timeout) { @@ -876,22 +829,16 @@ int SpiTransmit(SPI_InitTypeDef SpiInit, SPI_TypeDef *SpiInstance, uint8_t *pDat error: return errorcode; } + /** - * This function SPI Transmit and receive - * - * @param SpiInit SPI Init structure - * - * @param SpiInstance SPI control handle - * - * @param pTxData Transmit data buffer address - * - * @param pRxData receive data buffer address - * - * @param Size Amount of data - * - * @param Timeout waiting time - * - * @return if successful return EOK + * This function SPI Transmit and receive + * @param SpiInit SPI Init structure + * @param SpiInstance SPI control handle + * @param pTxData Transmit data buffer address + * @param pRxData receive data buffer address + * @param Size Amount of data + * @param Timeout waiting time + * @return if successful return EOK */ int SpiTransmitreceive(SPI_InitTypeDef SpiInit, SPI_TypeDef *SpiInstance, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size, uint32_t Timeout) { @@ -1029,20 +976,15 @@ int SpiTransmitreceive(SPI_InitTypeDef SpiInit, SPI_TypeDef *SpiInstance, uint8_ error : return errorcode; } + /** - * This function SPI receive data - * - * @param SpiInit SPI Init structure - * - * @param SpiInstance SPI control handle - * - * @param pData data buffer address - * - * @param Size Amount of data - * - * @param Timeout waiting time - * - * @return if successful return EOK + * This function SPI receive data + * @param SpiInit SPI Init structure + * @param SpiInstance SPI control handle + * @param pData data buffer address + * @param Size Amount of data + * @param Timeout waiting time + * @return if successful return EOK */ int SpiReceive(SPI_InitTypeDef SpiInit, SPI_TypeDef *SpiInstance, uint8_t *pData, uint16_t Size, uint32_t Timeout) { @@ -1140,208 +1082,197 @@ error : } /** - * This function SPI write data - * - * @param spi_dev SPI device structure handle - * - * @param spi_datacfg SPI device information structure handle - * - * @return datacfg length + * This function SPI write data + * @param spi_dev SPI device structure handle + * @param spi_datacfg SPI device information structure handle + * @return datacfg length */ static uint32 Stm32SpiWriteData(struct SpiHardwareDevice *spi_dev, struct SpiDataStandard *spi_datacfg) { - int state; - x_size_t message_length, already_send_length; - uint16 send_length; - const uint8 *WriteBuf; + int state = 0; + x_size_t message_length, already_send_length; + uint16 send_length; + const uint8 *WriteBuf; - NULL_PARAM_CHECK(spi_dev); - NULL_PARAM_CHECK(spi_datacfg); + NULL_PARAM_CHECK(spi_dev); + NULL_PARAM_CHECK(spi_datacfg); - struct Stm32Spi *StmSpi = CONTAINER_OF(spi_dev->haldev.owner_bus, struct Stm32Spi, spi_bus); - SPI_TypeDef *SpiInstance = StmSpi->instance; - SPI_InitTypeDef *SpiInit = &StmSpi->init; - struct Stm32HwSpiCs *cs = (struct Stm32HwSpiCs *)spi_dev->private_data; + struct Stm32Spi *StmSpi = CONTAINER_OF(spi_dev->haldev.owner_bus, struct Stm32Spi, spi_bus); + SPI_TypeDef *SpiInstance = StmSpi->instance; + SPI_InitTypeDef *SpiInit = &StmSpi->init; + struct Stm32HwSpiCs *cs = (struct Stm32HwSpiCs *)spi_dev->private_data; - while(NONE != spi_datacfg) { + while (NONE != spi_datacfg) { if (spi_datacfg->spi_chip_select) { - GPIO_WriteBit(cs->GPIOx, cs->GPIO_Pin, Bit_RESET); + GPIO_WriteBit(cs->GPIOx, cs->GPIO_Pin, Bit_RESET); } message_length = spi_datacfg->length; WriteBuf = spi_datacfg->tx_buff; while (message_length) { - if (message_length > 65535) { - send_length = 65535; - message_length = message_length - 65535; - } else { - send_length = message_length; - message_length = 0; - } + if (message_length > 65535) { + send_length = 65535; + message_length = message_length - 65535; + } else { + send_length = message_length; + message_length = 0; + } - /* calculate the start address */ - already_send_length = spi_datacfg->length - send_length - message_length; - WriteBuf = (uint8 *)spi_datacfg->tx_buff + already_send_length; - - /* start once data exchange in DMA mode */ - if (spi_datacfg->tx_buff) { - if (StmSpi->spi_dma_flag & SPI_USING_TX_DMA_FLAG) { - state = SpiTransmitDma(*SpiInit, SpiInstance, StmSpi->dma.dma_tx.init, StmSpi->dma.dma_tx.instance, (uint8_t *)WriteBuf, send_length); - } else { - state = SpiTransmit(*SpiInit, SpiInstance, (uint8_t *)WriteBuf, send_length, 1000); - } - } + /* calculate the start address */ + already_send_length = spi_datacfg->length - send_length - message_length; + WriteBuf = (uint8 *)spi_datacfg->tx_buff + already_send_length; + + /* start once data exchange in DMA mode */ + if (spi_datacfg->tx_buff) { + if (StmSpi->spi_dma_flag & SPI_USING_TX_DMA_FLAG) { + state = SpiTransmitDma(*SpiInit, SpiInstance, StmSpi->dma.dma_tx.init, StmSpi->dma.dma_tx.instance, (uint8_t *)WriteBuf, send_length); + } else { + state = SpiTransmit(*SpiInit, SpiInstance, (uint8_t *)WriteBuf, send_length, 1000); + } + } - if (state != 0) { - KPrintf("spi transfer error : %d\n", state); - spi_datacfg->length = 0; - } + if (state != 0) { + KPrintf("spi write error : %d\n", state); + spi_datacfg->length = 0; + } } if (spi_datacfg->spi_cs_release) { - GPIO_WriteBit(cs->GPIOx, cs->GPIO_Pin, Bit_SET); + GPIO_WriteBit(cs->GPIOx, cs->GPIO_Pin, Bit_SET); } spi_datacfg = spi_datacfg->next; } - return spi_datacfg->length; + return EOK; } /** - * This function SPI read data - * - * @param spi_dev SPI device structure handle - * - * @param spi_datacfg SPI device information structure handle - * - * @return datacfg length + * This function SPI read data + * @param spi_dev SPI device structure handle + * @param spi_datacfg SPI device information structure handle + * @return datacfg length */ static uint32 Stm32SpiReadData(struct SpiHardwareDevice *spi_dev, struct SpiDataStandard *spi_datacfg) { - int state; - x_size_t message_length, already_send_length; - uint16 read_length; - const uint8 *ReadBuf; + NULL_PARAM_CHECK(spi_dev); + NULL_PARAM_CHECK(spi_datacfg); - NULL_PARAM_CHECK(spi_dev); - NULL_PARAM_CHECK(spi_datacfg); + int state; + x_size_t message_length, already_send_length; + uint16 read_length, spi_read_length = 0; + uint8 *ReadBuf; - struct Stm32Spi *StmSpi = CONTAINER_OF(spi_dev->haldev.owner_bus, struct Stm32Spi, spi_bus); - SPI_TypeDef *SpiInstance = StmSpi->instance; - SPI_InitTypeDef *SpiInit = &StmSpi->init; - struct Stm32HwSpiCs *cs = (struct Stm32HwSpiCs *)spi_dev->private_data; + struct Stm32Spi *StmSpi = CONTAINER_OF(spi_dev->haldev.owner_bus, struct Stm32Spi, spi_bus); + SPI_TypeDef *SpiInstance = StmSpi->instance; + SPI_InitTypeDef *SpiInit = &StmSpi->init; + struct Stm32HwSpiCs *cs = (struct Stm32HwSpiCs *)spi_dev->private_data; while(NONE != spi_datacfg) { - if(spi_datacfg->spi_chip_select) { - GPIO_WriteBit(cs->GPIOx, cs->GPIO_Pin, Bit_RESET); + if (spi_datacfg->spi_chip_select) { + GPIO_WriteBit(cs->GPIOx, cs->GPIO_Pin, Bit_RESET); } message_length = spi_datacfg->length; ReadBuf = spi_datacfg->rx_buff; while (message_length) { - if (message_length > 65535){ - read_length = 65535; - message_length = message_length - 65535; + if (message_length > 65535){ + read_length = 65535; + message_length = message_length - 65535; + } else { + read_length = message_length; + message_length = 0; + } + + /* calculate the start address */ + already_send_length = spi_datacfg->length - read_length - message_length; + ReadBuf = spi_datacfg->rx_buff + already_send_length; + /* start once data exchange in DMA mode */ + if ((spi_datacfg->rx_buff) && (ReadBuf)) { + //memset(ReadBuf, 0, 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, read_length); } else { - read_length = message_length; - message_length = 0; + state = SpiReceive(*SpiInit, SpiInstance, ReadBuf, read_length, 1000); } + } - /* calculate the start address */ - 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, 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, read_length); - } else { - state = SpiReceive(*SpiInit, SpiInstance, (uint8_t *)ReadBuf, read_length, 1000); - } - } - - if (state != 0) { - KPrintf("spi transfer error : %d\n", state); - spi_datacfg->length = 0; - } + if (state != 0) { + KPrintf("spi read error : %d\n", state); + spi_datacfg->length = 0; + } } if (spi_datacfg->spi_cs_release) { - GPIO_WriteBit(cs->GPIOx, cs->GPIO_Pin, Bit_SET); + GPIO_WriteBit(cs->GPIOx, cs->GPIO_Pin, Bit_SET); } + spi_read_length += spi_datacfg->length; spi_datacfg = spi_datacfg->next; } - return spi_datacfg->length; + return spi_read_length; } /** - * This function SPI driver initialization function - * + * This function SPI driver initialization function * @param spi_drv SPI driver structure handle - * - * @return if successful return EOK + * @return if successful return EOK */ static uint32 SpiDrvInit(struct SpiDriver *spi_drv) { - NULL_PARAM_CHECK(spi_drv); + NULL_PARAM_CHECK(spi_drv); - SpiDeviceParam *dev_param = (SpiDeviceParam *)(spi_drv->driver.private_data); + SpiDeviceParam *dev_param = (SpiDeviceParam *)(spi_drv->driver.private_data); - struct Stm32Spi *StmSpi = CONTAINER_OF(spi_drv->driver.owner_bus, struct Stm32Spi, spi_bus); + struct Stm32Spi *StmSpi = CONTAINER_OF(spi_drv->driver.owner_bus, struct Stm32Spi, spi_bus); - return Stm32SpiInit(StmSpi, dev_param->spi_master_param); + return Stm32SpiInit(StmSpi, dev_param->spi_master_param); } /** - * This function SPI driver configuration param - * + * This function SPI driver configuration param * @param spi_drv SPI driver structure handle - * - * @param spi_param SPI master param structure handle - * - * @return if successful return EOK + * @param spi_param SPI master param structure handle + * @return if successful return EOK */ static uint32 SpiDrvConfigure(struct SpiDriver *spi_drv, struct SpiMasterParam *spi_param) { - NULL_PARAM_CHECK(spi_drv); - NULL_PARAM_CHECK(spi_param); + NULL_PARAM_CHECK(spi_drv); + NULL_PARAM_CHECK(spi_param); - SpiDeviceParam *dev_param = (SpiDeviceParam *)(spi_drv->driver.private_data); + SpiDeviceParam *dev_param = (SpiDeviceParam *)(spi_drv->driver.private_data); - dev_param->spi_master_param = spi_param; - dev_param->spi_master_param->spi_work_mode = dev_param->spi_master_param->spi_work_mode & SPI_MODE_MASK; + dev_param->spi_master_param = spi_param; + dev_param->spi_master_param->spi_work_mode = dev_param->spi_master_param->spi_work_mode & SPI_MODE_MASK; - return EOK; + return EOK; } /*Configure the spi device param, make sure struct (configure_info->private_data) = (SpiMasterParam)*/ static uint32 Stm32SpiDrvConfigure(void *drv, struct BusConfigureInfo *configure_info) { - NULL_PARAM_CHECK(drv); - NULL_PARAM_CHECK(configure_info); + NULL_PARAM_CHECK(drv); + NULL_PARAM_CHECK(configure_info); - x_err_t ret = EOK; - struct SpiDriver *spi_drv = (struct SpiDriver *)drv; - struct SpiMasterParam *spi_param; + x_err_t ret = EOK; + struct SpiDriver *spi_drv = (struct SpiDriver *)drv; + struct SpiMasterParam *spi_param; - switch (configure_info->configure_cmd) - { - case OPE_INT: - ret = SpiDrvInit(spi_drv); - break; - case OPE_CFG: - spi_param = (struct SpiMasterParam *)configure_info->private_data; - ret = SpiDrvConfigure(spi_drv, spi_param); - break; - default: - break; - } + switch (configure_info->configure_cmd) + { + case OPE_INT: + ret = SpiDrvInit(spi_drv); + break; + case OPE_CFG: + spi_param = (struct SpiMasterParam *)configure_info->private_data; + ret = SpiDrvConfigure(spi_drv, spi_param); + break; + default: + break; + } - return ret; + return ret; } /*manage the spi device operations*/ @@ -1354,11 +1285,11 @@ static const struct SpiDevDone spi_dev_done = }; #if defined(BSP_USING_SPI1) -struct Stm32Spi spi1; +static struct Stm32Spi spi1; #if defined(BSP_SPI1_TX_USING_DMA) void DMA2_Stream3_IRQHandler(int irq_num, void *arg) { - DmaTxDoneIsr(&spi1.spi_bus); + DmaTxDoneIsr(&spi1.spi_bus); } DECLARE_HW_IRQ(DMA2_Stream3_IRQn, DMA2_Stream3_IRQHandler, NONE); #endif @@ -1366,18 +1297,18 @@ DECLARE_HW_IRQ(DMA2_Stream3_IRQn, DMA2_Stream3_IRQHandler, NONE); #if defined(BSP_SPI1_RX_USING_DMA) void DMA2_Stream0_IRQHandler(int irq_num, void *arg) { - DmaRxDoneIsr(&spi1.spi_bus); + DmaRxDoneIsr(&spi1.spi_bus); } DECLARE_HW_IRQ(DMA2_Stream0_IRQn, DMA2_Stream0_IRQHandler, NONE); #endif #endif #if defined(BSP_USING_SPI2) -struct Stm32Spi spi2; +static struct Stm32Spi spi2; #if defined(BSP_SPI2_TX_USING_DMA) void DMA1_Stream4_IRQHandler(int irq_num, void *arg) { - DmaTxDoneIsr(&spi2.spi_bus); + DmaTxDoneIsr(&spi2.spi_bus); } DECLARE_HW_IRQ(DMA1_Stream4_IRQn, DMA1_Stream4_IRQHandler, NONE); #endif @@ -1385,18 +1316,18 @@ DECLARE_HW_IRQ(DMA1_Stream4_IRQn, DMA1_Stream4_IRQHandler, NONE); #if defined(BSP_SPI2_RX_USING_DMA) void DMA1_Stream3_IRQHandler(int irq_num, void *arg) { - DmaTxDoneIsr(&spi2.spi_bus); + DmaTxDoneIsr(&spi2.spi_bus); } DECLARE_HW_IRQ(DMA1_Stream3_IRQn, DMA1_Stream3_IRQHandler, NONE); #endif #endif #if defined(BSP_USING_SPI3) -struct Stm32Spi spi3; +static struct Stm32Spi spi3; #if defined(BSP_SPI3_TX_USING_DMA) void DMA1_Stream7_IRQHandler(int irq_num, void *arg) { - DmaTxDoneIsr(&spi3.spi_bus); + DmaTxDoneIsr(&spi3.spi_bus); } DECLARE_HW_IRQ(DMA1_Stream7_IRQn, DMA1_Stream7_IRQHandler, NONE); #endif @@ -1409,7 +1340,7 @@ DECLARE_HW_IRQ(DMA1_Stream7_IRQn, DMA1_Stream7_IRQHandler, NONE); */ void DMA1_Stream2_IRQHandler(int irq_num, void *arg) { - DmaRxDoneIsr(&spi3.spi_bus); + DmaRxDoneIsr(&spi3.spi_bus); } DECLARE_HW_IRQ(DMA1_Stream2_IRQn, DMA1_Stream2_IRQHandler, NONE); #endif @@ -1417,33 +1348,31 @@ DECLARE_HW_IRQ(DMA1_Stream2_IRQn, DMA1_Stream2_IRQHandler, NONE); /** * This function RCC clock configuration function - * - * @return none + * @return none */ static void RCCConfiguration(void) { #ifdef BSP_USING_SPI1 - RCC_AHB1PeriphClockCmd(SPI1_GPIO_RCC, ENABLE); - - RCC_APB2PeriphClockCmd(RCC_APBPeriph_SPI1, ENABLE); + RCC_AHB1PeriphClockCmd(SPI1_GPIO_RCC, ENABLE); + + RCC_APB2PeriphClockCmd(RCC_APBPeriph_SPI1, ENABLE); #endif #ifdef BSP_USING_SPI2 - RCC_AHB1PeriphClockCmd(SPI2_GPIO_RCC | SPI2_GPIO_RCC_SCK, ENABLE); - - RCC_APB1PeriphClockCmd(RCC_APBPeriph_SPI2, ENABLE); + RCC_AHB1PeriphClockCmd(SPI2_GPIO_RCC | SPI2_GPIO_RCC_SCK, ENABLE); + + RCC_APB1PeriphClockCmd(RCC_APBPeriph_SPI2, ENABLE); #endif #ifdef BSP_USING_SPI3 - RCC_AHB1PeriphClockCmd(SPI3_GPIO_RCC | SPI3_GPIO_RCC_NSS, ENABLE); - - RCC_APB2PeriphClockCmd(RCC_APBPeriph_SPI3, ENABLE); + RCC_AHB1PeriphClockCmd(SPI3_GPIO_RCC | SPI3_GPIO_RCC_NSS, ENABLE); + + RCC_APB2PeriphClockCmd(RCC_APBPeriph_SPI3, ENABLE); #endif } /** - * This function GPIO Configuration function - * - * @return none + * This function GPIO Configuration function + * @return none */ static void GPIOConfiguration(void) { @@ -1498,236 +1427,228 @@ static void GPIOConfiguration(void) } /** - * This function Init the spi bus 、spi driver and attach to the bus - * + * This function Init the spi bus 、spi driver and attach to the bus * @param spi_bus Spi bus info pointer - * - * @param spi_driver Spi driver info pointer - * - * @return EOK + * @param spi_driver Spi driver info pointer + * @return EOK */ static int BoardSpiBusInit(struct Stm32Spi *stm32spi_bus, struct SpiDriver *spi_driver, char* drv_name) { - x_err_t ret = EOK; + x_err_t ret = EOK; - /*Init the spi bus */ - ret = SpiBusInit(&stm32spi_bus->spi_bus, stm32spi_bus->bus_name); - if (EOK != ret) { - KPrintf("Board_Spi_init SpiBusInit error %d\n", ret); - return ERROR; - } + /*Init the spi bus */ + ret = SpiBusInit(&stm32spi_bus->spi_bus, stm32spi_bus->bus_name); + if (EOK != ret) { + KPrintf("Board_Spi_init SpiBusInit error %d\n", ret); + return ERROR; + } - /*Init the spi driver*/ - ret = SpiDriverInit(spi_driver, drv_name); - if (EOK != ret) { - KPrintf("Board_Spi_init SpiDriverInit error %d\n", ret); - return ERROR; - } + /*Init the spi driver*/ + ret = SpiDriverInit(spi_driver, drv_name); + if (EOK != ret) { + KPrintf("Board_Spi_init SpiDriverInit error %d\n", ret); + return ERROR; + } - /*Attach the spi driver to the spi bus*/ - ret = SpiDriverAttachToBus(drv_name, stm32spi_bus->bus_name); - if (EOK != ret) { - KPrintf("Board_Spi_init SpiDriverAttachToBus error %d\n", ret); - return ERROR; - } + /*Attach the spi driver to the spi bus*/ + ret = SpiDriverAttachToBus(drv_name, stm32spi_bus->bus_name); + if (EOK != ret) { + KPrintf("Board_Spi_init SpiDriverAttachToBus error %d\n", ret); + return ERROR; + } - return ret; + return ret; } /** * This function SPI bus initialization - * - * @return EOK + * @return EOK */ static int Stm32HwSpiBusInit(void) { - x_err_t ret = EOK; - struct Stm32Spi *StmSpiBus; + x_err_t ret = EOK; + struct Stm32Spi *StmSpiBus; - RCCConfiguration(); - GPIOConfiguration(); + RCCConfiguration(); + GPIOConfiguration(); #ifdef BSP_USING_SPI1 - StmSpiBus = &spi1; - StmSpiBus->instance = SPI1; - StmSpiBus->bus_name = SPI_BUS_NAME_1; - StmSpiBus->spi_bus.private_data = &spi1; - DmaSpiConfig(&StmSpiBus->spi_bus, 0, NONE, NONE); + memset(&spi1, 0, sizeof(struct Stm32Spi)); + StmSpiBus = &spi1; + StmSpiBus->instance = SPI1; + StmSpiBus->bus_name = SPI_BUS_NAME_1; + StmSpiBus->spi_bus.private_data = &spi1; + DmaSpiConfig(&StmSpiBus->spi_bus, 0, NONE, NONE); - static struct SpiDriver spi_driver_1; - memset(&spi_driver_1, 0, sizeof(struct SpiDriver)); + static struct SpiDriver spi_driver_1; + memset(&spi_driver_1, 0, sizeof(struct SpiDriver)); - spi_driver_1.configure = &(Stm32SpiDrvConfigure); + spi_driver_1.configure = &(Stm32SpiDrvConfigure); - ret = BoardSpiBusInit(StmSpiBus, &spi_driver_1, SPI_1_DRV_NAME); - if (EOK != ret) { - KPrintf("Board_Spi_Init spi_bus_init %s error ret %u\n", StmSpiBus->bus_name, ret); - return ERROR; - } + ret = BoardSpiBusInit(StmSpiBus, &spi_driver_1, SPI_1_DRV_NAME); + if (EOK != ret) { + KPrintf("Board_Spi_Init spi_bus_init %s error ret %u\n", StmSpiBus->bus_name, ret); + return ERROR; + } #endif #ifdef BSP_USING_SPI2 - StmSpiBus = &spi2; - StmSpiBus->instance = SPI2; - StmSpiBus->bus_name = SPI_BUS_NAME_2; - StmSpiBus->spi_bus.private_data = &spi2; - DmaSpiConfig(&StmSpiBus->spi_bus, 0, NONE, NONE); + memset(&spi2, 0, sizeof(struct Stm32Spi)); + StmSpiBus = &spi2; + StmSpiBus->instance = SPI2; + StmSpiBus->bus_name = SPI_BUS_NAME_2; + StmSpiBus->spi_bus.private_data = &spi2; + DmaSpiConfig(&StmSpiBus->spi_bus, 0, NONE, NONE); - static struct SpiDriver spi_driver_2; - memset(&spi_driver_2, 0, sizeof(struct SpiDriver)); + static struct SpiDriver spi_driver_2; + memset(&spi_driver_2, 0, sizeof(struct SpiDriver)); - spi_driver_2.configure = &(Stm32SpiDrvConfigure); + spi_driver_2.configure = &(Stm32SpiDrvConfigure); - ret = BoardSpiBusInit(StmSpiBus, &spi_driver_2, SPI_2_DRV_NAME); - if (EOK != ret) { - return ERROR; - } + ret = BoardSpiBusInit(StmSpiBus, &spi_driver_2, SPI_2_DRV_NAME); + if (EOK != ret) { + return ERROR; + } #endif #ifdef BSP_USING_SPI3 - StmSpiBus = &spi3; - StmSpiBus->instance = SPI3; - StmSpiBus->bus_name = SPI_BUS_NAME_3; - StmSpiBus->spi_bus.private_data = &spi3; - DmaSpiConfig(&StmSpiBus->spi_bus, 0, NONE, NONE); + memset(&spi3, 0, sizeof(struct Stm32Spi)); + StmSpiBus = &spi3; + StmSpiBus->instance = SPI3; + StmSpiBus->bus_name = SPI_BUS_NAME_3; + StmSpiBus->spi_bus.private_data = &spi3; + DmaSpiConfig(&StmSpiBus->spi_bus, 0, NONE, NONE); - static struct SpiDriver spi_driver_3; - memset(&spi_driver_3, 0, sizeof(struct SpiDriver)); + static struct SpiDriver spi_driver_3; + memset(&spi_driver_3, 0, sizeof(struct SpiDriver)); - spi_driver_3.configure = &(Stm32SpiDrvConfigure); + spi_driver_3.configure = &(Stm32SpiDrvConfigure); - ret = BoardSpiBusInit(StmSpiBus, &spi_driver_3, SPI_3_DRV_NAME); - if (EOK != ret) { - KPrintf("Board_Spi_Init spi_bus_init %s error ret %u\n", StmSpiBus->bus_name, ret); - return ERROR; - } + ret = BoardSpiBusInit(StmSpiBus, &spi_driver_3, SPI_3_DRV_NAME); + if (EOK != ret) { + KPrintf("Board_Spi_Init spi_bus_init %s error ret %u\n", StmSpiBus->bus_name, ret); + return ERROR; + } #endif - return EOK; + return EOK; } /** - * This function Mount the spi device to the bus - * - * @param bus_name Bus Name - * - * @param device_name spi device name - * - * @param cs_gpiox GPIO pin configuration handle - * - * @param cs_gpio_pin GPIO number - * - * @return EOK + * This function Mount the spi device to the bus + * @param bus_name Bus Name + * @param device_name spi device name + * @param cs_gpiox GPIO pin configuration handle + * @param cs_gpio_pin GPIO number + * @return EOK */ x_err_t HwSpiDeviceAttach(const char *bus_name, const char *device_name, GPIO_TypeDef *cs_gpiox, uint16_t cs_gpio_pin) { - NULL_PARAM_CHECK(bus_name); - NULL_PARAM_CHECK(device_name); + NULL_PARAM_CHECK(bus_name); + NULL_PARAM_CHECK(device_name); - x_err_t result; - struct SpiHardwareDevice *spi_device; - struct Stm32HwSpiCs *cs_pin_param; - static SpiDeviceParam spi_dev_param; - memset(&spi_dev_param, 0, sizeof(SpiDeviceParam)); + x_err_t result; + struct SpiHardwareDevice *spi_device; + struct Stm32HwSpiCs *cs_pin_param; + static SpiDeviceParam spi_dev_param; + memset(&spi_dev_param, 0, sizeof(SpiDeviceParam)); - /* initialize the cs pin && select the slave*/ - GPIO_InitTypeDef GPIO_Initure; - GPIO_Initure.GPIO_Pin = cs_gpio_pin; - GPIO_Initure.GPIO_Mode = GPIO_Mode_OUT; - GPIO_Initure.GPIO_PuPd = GPIO_PuPd_UP; - GPIO_Initure.GPIO_Speed = GPIO_Speed_50MHz; - GPIO_Init(cs_gpiox, &GPIO_Initure); - GPIO_WriteBit(cs_gpiox, cs_gpio_pin, Bit_SET); + /* initialize the cs pin && select the slave*/ + GPIO_InitTypeDef GPIO_Initure; + GPIO_Initure.GPIO_Pin = cs_gpio_pin; + GPIO_Initure.GPIO_Mode = GPIO_Mode_OUT; + GPIO_Initure.GPIO_PuPd = GPIO_PuPd_UP; + GPIO_Initure.GPIO_Speed = GPIO_Speed_50MHz; + GPIO_Init(cs_gpiox, &GPIO_Initure); + GPIO_WriteBit(cs_gpiox, cs_gpio_pin, Bit_SET); - /* attach the device to spi bus*/ - spi_device = (struct SpiHardwareDevice *)x_malloc(sizeof(struct SpiHardwareDevice)); - CHECK(spi_device); - memset(spi_device, 0, sizeof(struct SpiHardwareDevice)); - cs_pin_param = (struct Stm32HwSpiCs *)x_malloc(sizeof(struct Stm32HwSpiCs)); - CHECK(cs_pin_param); - memset(cs_pin_param, 0, sizeof(struct Stm32HwSpiCs)); - cs_pin_param->GPIOx = cs_gpiox; - cs_pin_param->GPIO_Pin = cs_gpio_pin; + /* attach the device to spi bus*/ + spi_device = (struct SpiHardwareDevice *)x_malloc(sizeof(struct SpiHardwareDevice)); + CHECK(spi_device); + memset(spi_device, 0, sizeof(struct SpiHardwareDevice)); + cs_pin_param = (struct Stm32HwSpiCs *)x_malloc(sizeof(struct Stm32HwSpiCs)); + CHECK(cs_pin_param); + memset(cs_pin_param, 0, sizeof(struct Stm32HwSpiCs)); + cs_pin_param->GPIOx = cs_gpiox; + cs_pin_param->GPIO_Pin = cs_gpio_pin; - spi_device->spi_dev_done = &spi_dev_done; - spi_device->private_data = (void *)cs_pin_param; + spi_device->spi_dev_done = &spi_dev_done; + spi_device->private_data = (void *)cs_pin_param; - result = SpiDeviceRegister(spi_device, (void *)&spi_dev_param, device_name); - if (result != EOK) { - SYS_ERR("%s device %p register faild, %d\n", device_name, spi_device, result); - } + result = SpiDeviceRegister(spi_device, (void *)&spi_dev_param, device_name); + if (result != EOK) { + SYS_ERR("%s device %p register faild, %d\n", device_name, spi_device, result); + } - result = SpiDeviceAttachToBus(device_name, bus_name); - if (result != EOK) { - SYS_ERR("%s attach to %s faild, %d\n", device_name, bus_name, result); - } + result = SpiDeviceAttachToBus(device_name, bus_name); + if (result != EOK) { + SYS_ERR("%s attach to %s faild, %d\n", device_name, bus_name, result); + } - CHECK(result == EOK); + CHECK(result == EOK); - KPrintf("%s attach to %s done\n", device_name, bus_name); + KPrintf("%s attach to %s done\n", device_name, bus_name); - return result; + return result; } /** * This function Get DMA information - * - * @return none + * @return none */ static void Stm32GetDmaInfo(void) { #ifdef BSP_SPI1_RX_USING_DMA /*SPI1 uses DMA receive enable*/ - spi1.spi_dma_flag |= SPI_USING_RX_DMA_FLAG; - spi1.dma.dma_rx.instance = DMA2_Stream0; - spi1.dma.dma_rx.dma_rcc = RCC_AHB1ENR_DMA2EN; - spi1.dma.dma_rx.channel = DMA_Channel_3; - spi1.dma.dma_rx.dma_irq = DMA2_Stream0_IRQn; + spi1.spi_dma_flag |= SPI_USING_RX_DMA_FLAG; + spi1.dma.dma_rx.instance = DMA2_Stream0; + spi1.dma.dma_rx.dma_rcc = RCC_AHB1ENR_DMA2EN; + spi1.dma.dma_rx.channel = DMA_Channel_3; + spi1.dma.dma_rx.dma_irq = DMA2_Stream0_IRQn; #endif #ifdef BSP_SPI1_TX_USING_DMA /*SPI1 uses DMA send enable*/ - spi1.spi_dma_flag |= SPI_USING_TX_DMA_FLAG; - spi1.dma.dma_tx.instance = DMA2_Stream3; - spi1.dma.dma_tx.dma_rcc = RCC_AHB1ENR_DMA2EN; - spi1.dma.dma_tx.channel = DMA_Channel_3; - spi1.dma.dma_tx.dma_irq = DMA2_Stream3_IRQn; + spi1.spi_dma_flag |= SPI_USING_TX_DMA_FLAG; + spi1.dma.dma_tx.instance = DMA2_Stream3; + spi1.dma.dma_tx.dma_rcc = RCC_AHB1ENR_DMA2EN; + spi1.dma.dma_tx.channel = DMA_Channel_3; + spi1.dma.dma_tx.dma_irq = DMA2_Stream3_IRQn; #endif #ifdef BSP_SPI2_RX_USING_DMA /*SPI2 uses DMA receive enable*/ - spi2.spi_dma_flag |= SPI_USING_RX_DMA_FLAG; - spi2.dma.dma_rx.instance = DMA1_Stream3; /* DMA1 Data stream 3*/ - spi2.dma.dma_rx.dma_rcc = RCC_AHB1ENR_DMA1EN; - spi2.dma.dma_rx.channel = DMA_Channel_0; - spi2.dma.dma_rx.dma_irq = DMA1_Stream3_IRQn; + spi2.spi_dma_flag |= SPI_USING_RX_DMA_FLAG; + spi2.dma.dma_rx.instance = DMA1_Stream3; /* DMA1 Data stream 3*/ + spi2.dma.dma_rx.dma_rcc = RCC_AHB1ENR_DMA1EN; + spi2.dma.dma_rx.channel = DMA_Channel_0; + spi2.dma.dma_rx.dma_irq = DMA1_Stream3_IRQn; #endif #ifdef BSP_SPI2_TX_USING_DMA /*SPI2 uses DMA send enable*/ - spi2.spi_dma_flag |= SPI_USING_TX_DMA_FLAG; - spi2.dma.dma_tx.instance = DMA1_Stream4; /* DMA1 Data stream 4*/ - spi2.dma.dma_tx.dma_rcc = RCC_AHB1ENR_DMA1EN; - spi2.dma.dma_tx.channel = DMA_Channel_0; - spi2.dma.dma_tx.dma_irq = DMA1_Stream4_IRQn; /*Enable DMA interrupt line*/ + spi2.spi_dma_flag |= SPI_USING_TX_DMA_FLAG; + spi2.dma.dma_tx.instance = DMA1_Stream4; /* DMA1 Data stream 4*/ + spi2.dma.dma_tx.dma_rcc = RCC_AHB1ENR_DMA1EN; + spi2.dma.dma_tx.channel = DMA_Channel_0; + spi2.dma.dma_tx.dma_irq = DMA1_Stream4_IRQn; /*Enable DMA interrupt line*/ #endif #ifdef BSP_SPI3_RX_USING_DMA /*SPI3 uses DMA receive enable*/ - spi3.spi_dma_flag |= SPI_USING_RX_DMA_FLAG; - spi3.dma.dma_rx.instance = DMA1_Stream2; /* DMA1 Data stream 2*/ - spi3.dma.dma_rx.dma_rcc = RCC_AHB1ENR_DMA1EN; - spi3.dma.dma_rx.channel = DMA_Channel_0; - spi3.dma.dma_rx.dma_irq = DMA1_Stream2_IRQn; /*Enable DMA interrupt line*/ + spi3.spi_dma_flag |= SPI_USING_RX_DMA_FLAG; + spi3.dma.dma_rx.instance = DMA1_Stream2; /* DMA1 Data stream 2*/ + spi3.dma.dma_rx.dma_rcc = RCC_AHB1ENR_DMA1EN; + spi3.dma.dma_rx.channel = DMA_Channel_0; + spi3.dma.dma_rx.dma_irq = DMA1_Stream2_IRQn; /*Enable DMA interrupt line*/ #endif #ifdef BSP_SPI3_TX_USING_DMA /*SPI3 uses DMA send enable*/ - spi3.spi_dma_flag |= SPI_USING_TX_DMA_FLAG; - spi3.dma.dma_tx.instance = DMA1_Stream7; /* DMA1 Data stream 7*/ - spi3.dma.dma_tx.dma_rcc = RCC_AHB1ENR_DMA1EN; - spi3.dma.dma_tx.channel = DMA_Channel_0; - spi3.dma.dma_tx.dma_irq = DMA1_Stream7_IRQn; /*Enable DMA interrupt line*/ + spi3.spi_dma_flag |= SPI_USING_TX_DMA_FLAG; + spi3.dma.dma_tx.instance = DMA1_Stream7; /* DMA1 Data stream 7*/ + spi3.dma.dma_tx.dma_rcc = RCC_AHB1ENR_DMA1EN; + spi3.dma.dma_tx.channel = DMA_Channel_0; + spi3.dma.dma_tx.dma_irq = DMA1_Stream7_IRQn; /*Enable DMA interrupt line*/ #endif } /** * This function hardware spi initialization - * - * @return EOK + * @return EOK */ int Stm32HwSpiInit(void) { - Stm32GetDmaInfo(); - return Stm32HwSpiBusInit(); + Stm32GetDmaInfo(); + return Stm32HwSpiBusInit(); } diff --git a/Ubiquitous/XiUOS/board/aiit-arm32-board/third_party_driver/spi/third_party_spi_lora/src/radio/sx1276-LoRa.c b/Ubiquitous/XiUOS/board/aiit-arm32-board/third_party_driver/spi/third_party_spi_lora/src/radio/sx1276-LoRa.c index 297d6e8af..f8645b09f 100644 --- a/Ubiquitous/XiUOS/board/aiit-arm32-board/third_party_driver/spi/third_party_spi_lora/src/radio/sx1276-LoRa.c +++ b/Ubiquitous/XiUOS/board/aiit-arm32-board/third_party_driver/spi/third_party_spi_lora/src/radio/sx1276-LoRa.c @@ -183,6 +183,7 @@ tSX1276LR* SX1276LR; * Local RF buffer for communication support */ static uint8_t RFBuffer[RF_BUFFER_SIZE]; +static uint8_t TFBuffer[RF_BUFFER_SIZE]; /*! * RF state machine variable @@ -401,7 +402,7 @@ void SX1276LoRaSetTxPacket( const void *buffer, uint16_t size ) { TxPacketSize = 255; } - memcpy( ( void * )RFBuffer, buffer, ( size_t )TxPacketSize ); + memcpy( ( void * )TFBuffer, buffer, ( size_t )TxPacketSize ); RFLRState = RFLR_STATE_TX_INIT; } @@ -678,7 +679,7 @@ uint32_t SX1276LoRaProcess( void ) SX1276LR->RegFifoAddrPtr = SX1276LR->RegFifoTxBaseAddr; SX1276Write( REG_LR_FIFOADDRPTR, SX1276LR->RegFifoAddrPtr ); // Write payload buffer to LORA modem - SX1276WriteFifo( RFBuffer, SX1276LR->RegPayloadLength ); + SX1276WriteFifo( TFBuffer, SX1276LR->RegPayloadLength ); // TxDone RxTimeout FhssChangeChannel ValidHeader SX1276LR->RegDioMapping1 = RFLR_DIOMAPPING1_DIO0_01 | RFLR_DIOMAPPING1_DIO1_00 | RFLR_DIOMAPPING1_DIO2_00 | RFLR_DIOMAPPING1_DIO3_01; // PllLock Mode Ready diff --git a/Ubiquitous/XiUOS/board/aiit-arm32-board/third_party_driver/usb/STM32_USB_OTG_Driver/inc/usb_conf.h b/Ubiquitous/XiUOS/board/aiit-arm32-board/third_party_driver/usb/STM32_USB_OTG_Driver/inc/usb_conf.h index ec7e39df1..f9f9acf10 100644 --- a/Ubiquitous/XiUOS/board/aiit-arm32-board/third_party_driver/usb/STM32_USB_OTG_Driver/inc/usb_conf.h +++ b/Ubiquitous/XiUOS/board/aiit-arm32-board/third_party_driver/usb/STM32_USB_OTG_Driver/inc/usb_conf.h @@ -240,8 +240,10 @@ #endif /* USB_OTG_HS_INTERNAL_DMA_ENABLED */ -#if defined ( __GNUC__ ) /* GNU Compiler */ +#if defined ( __GNUC__ ) /* GNU Compiler */ +#ifndef __packed #define __packed __attribute__ ((__packed__)) +#endif #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 598d156aa..12f7b3555 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 @@ -279,15 +279,32 @@ static uint32 SpiLoraRead(void *dev, struct BusBlockReadParam *read_param) { NULL_PARAM_CHECK(dev); NULL_PARAM_CHECK(read_param); + + int read_times = 1000; //Radio->StartRx(); SX1276StartRx(); KPrintf("SpiLoraRead Ready!\n"); + + while (read_times) { + if (SX1276Process() != RF_RX_DONE) { + read_times --; + MdelayKTask(500); + } else { + break; + } + } + + if (read_times > 0) { + SX1276GetRxPacket(read_param->buffer, (uint16 *)&read_param->read_length); + } else { + read_param->read_length = 0; + } //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); + // while(SX1276Process() != RF_RX_DONE); + // SX1276GetRxPacket(read_param->buffer, (uint16 *)&read_param->read_length); return read_param->read_length; } @@ -461,7 +478,7 @@ int LoraSx12xxSpiDeviceInit(void) return EOK; } -#define LORA_TEST +//#define LORA_TEST #ifdef LORA_TEST /*Just for lora test*/ static struct Bus *bus; diff --git a/Ubiquitous/XiUOS/board/aiit-riscv64-board/third_party_driver/spi/connect_spi.c b/Ubiquitous/XiUOS/board/aiit-riscv64-board/third_party_driver/spi/connect_spi.c index 7445b0660..c2a7ee5fb 100644 --- a/Ubiquitous/XiUOS/board/aiit-riscv64-board/third_party_driver/spi/connect_spi.c +++ b/Ubiquitous/XiUOS/board/aiit-riscv64-board/third_party_driver/spi/connect_spi.c @@ -214,13 +214,14 @@ static uint32 SpiWriteData(struct SpiHardwareDevice *spi_dev, struct SpiDataStan spi_datacfg = spi_datacfg->next; } - return spi_datacfg->length; + return EOK; } static uint32 SpiReadData(struct SpiHardwareDevice *spi_dev, struct SpiDataStandard *spi_datacfg) { SpiDeviceParam *dev_param = (SpiDeviceParam *)(spi_dev->haldev.private_data); + uint32 spi_read_length = 0;; uint8 device_id = dev_param->spi_slave_param->spi_slave_id; uint8 device_master_id = dev_param->spi_dma_param->spi_master_id; uint8 cs_gpio_pin = dev_param->spi_slave_param->spi_cs_gpio_pin; @@ -282,10 +283,11 @@ static uint32 SpiReadData(struct SpiHardwareDevice *spi_dev, struct SpiDataStand gpiohs_set_pin(cs_gpio_pin, GPIO_PV_HIGH); } + spi_read_length += spi_datacfg->length; spi_datacfg = spi_datacfg->next; } - return spi_datacfg->length; + return spi_read_length; } /*manage the spi device operations*/ diff --git a/Ubiquitous/XiUOS/board/aiit-riscv64-board/third_party_driver/spi/third_party_spi_lora/src/radio/sx1276-LoRa.c b/Ubiquitous/XiUOS/board/aiit-riscv64-board/third_party_driver/spi/third_party_spi_lora/src/radio/sx1276-LoRa.c index 297d6e8af..f8645b09f 100644 --- a/Ubiquitous/XiUOS/board/aiit-riscv64-board/third_party_driver/spi/third_party_spi_lora/src/radio/sx1276-LoRa.c +++ b/Ubiquitous/XiUOS/board/aiit-riscv64-board/third_party_driver/spi/third_party_spi_lora/src/radio/sx1276-LoRa.c @@ -183,6 +183,7 @@ tSX1276LR* SX1276LR; * Local RF buffer for communication support */ static uint8_t RFBuffer[RF_BUFFER_SIZE]; +static uint8_t TFBuffer[RF_BUFFER_SIZE]; /*! * RF state machine variable @@ -401,7 +402,7 @@ void SX1276LoRaSetTxPacket( const void *buffer, uint16_t size ) { TxPacketSize = 255; } - memcpy( ( void * )RFBuffer, buffer, ( size_t )TxPacketSize ); + memcpy( ( void * )TFBuffer, buffer, ( size_t )TxPacketSize ); RFLRState = RFLR_STATE_TX_INIT; } @@ -678,7 +679,7 @@ uint32_t SX1276LoRaProcess( void ) SX1276LR->RegFifoAddrPtr = SX1276LR->RegFifoTxBaseAddr; SX1276Write( REG_LR_FIFOADDRPTR, SX1276LR->RegFifoAddrPtr ); // Write payload buffer to LORA modem - SX1276WriteFifo( RFBuffer, SX1276LR->RegPayloadLength ); + SX1276WriteFifo( TFBuffer, SX1276LR->RegPayloadLength ); // TxDone RxTimeout FhssChangeChannel ValidHeader SX1276LR->RegDioMapping1 = RFLR_DIOMAPPING1_DIO0_01 | RFLR_DIOMAPPING1_DIO1_00 | RFLR_DIOMAPPING1_DIO2_00 | RFLR_DIOMAPPING1_DIO3_01; // PllLock Mode Ready diff --git a/Ubiquitous/XiUOS/board/kd233/third_party_driver/spi/connect_spi.c b/Ubiquitous/XiUOS/board/kd233/third_party_driver/spi/connect_spi.c index 1ddbda670..2e74711eb 100644 --- a/Ubiquitous/XiUOS/board/kd233/third_party_driver/spi/connect_spi.c +++ b/Ubiquitous/XiUOS/board/kd233/third_party_driver/spi/connect_spi.c @@ -215,13 +215,14 @@ static uint32 SpiWriteData(struct SpiHardwareDevice *spi_dev, struct SpiDataStan spi_datacfg = spi_datacfg->next; } - return spi_datacfg->length; + return EOK; } static uint32 SpiReadData(struct SpiHardwareDevice *spi_dev, struct SpiDataStandard *spi_datacfg) { SpiDeviceParam *dev_param = (SpiDeviceParam *)(spi_dev->haldev.private_data); + uint32 spi_read_length = 0; uint8 device_id = dev_param->spi_slave_param->spi_slave_id; uint8 device_master_id = dev_param->spi_dma_param->spi_master_id; uint8 cs_gpio_pin = dev_param->spi_slave_param->spi_cs_gpio_pin; @@ -281,10 +282,11 @@ static uint32 SpiReadData(struct SpiHardwareDevice *spi_dev, struct SpiDataStand gpiohs_set_pin(cs_gpio_pin, GPIO_PV_HIGH); } + spi_read_length += spi_datacfg->length; spi_datacfg = spi_datacfg->next; } - return spi_datacfg->length; + return spi_read_length; } /*manage the spi device operations*/ 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 a78ae70e2..5366f58e8 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 @@ -1097,7 +1097,7 @@ static uint32 Stm32SpiWriteData(struct SpiHardwareDevice *spi_dev, struct SpiDat } if (state != 0) { - KPrintf("spi transfer error : %d\n", state); + KPrintf("spi write error : %d\n", state); spi_datacfg->length = 0; } } @@ -1109,7 +1109,7 @@ static uint32 Stm32SpiWriteData(struct SpiHardwareDevice *spi_dev, struct SpiDat spi_datacfg = spi_datacfg->next; } - return spi_datacfg->length; + return EOK; } /** @@ -1125,7 +1125,7 @@ static uint32 Stm32SpiReadData(struct SpiHardwareDevice *spi_dev, struct SpiData { int state; x_size_t message_length, already_send_length; - uint16 read_length; + uint16 read_length, spi_read_length = 0; const uint8 *ReadBuf; NULL_PARAM_CHECK(spi_dev); @@ -1158,7 +1158,7 @@ static uint32 Stm32SpiReadData(struct SpiHardwareDevice *spi_dev, struct SpiData /* start once data exchange in DMA mode */ if (spi_datacfg->rx_buff) { - memset((uint8_t *)ReadBuf, 0xff, read_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, read_length); } else { @@ -1167,7 +1167,7 @@ static uint32 Stm32SpiReadData(struct SpiHardwareDevice *spi_dev, struct SpiData } if (state != 0) { - KPrintf("spi transfer error : %d\n", state); + KPrintf("spi read error : %d\n", state); spi_datacfg->length = 0; } } @@ -1176,10 +1176,11 @@ static uint32 Stm32SpiReadData(struct SpiHardwareDevice *spi_dev, struct SpiData GPIO_WriteBit(cs->GPIOx, cs->GPIO_Pin, Bit_SET); } + spi_read_length += spi_datacfg->length; spi_datacfg = spi_datacfg->next; } - return spi_datacfg->length; + return spi_read_length; } /** diff --git a/Ubiquitous/XiUOS/resources/include/dev_spi.h b/Ubiquitous/XiUOS/resources/include/dev_spi.h index 9984427f9..f0bb6fd7c 100644 --- a/Ubiquitous/XiUOS/resources/include/dev_spi.h +++ b/Ubiquitous/XiUOS/resources/include/dev_spi.h @@ -58,7 +58,7 @@ struct SpiDataStandard const uint8 *tx_buff; uint32 tx_len; - const uint8 *rx_buff; + uint8 *rx_buff; uint32 rx_len; uint32 length; diff --git a/Ubiquitous/XiUOS/resources/spi/dev_spi.c b/Ubiquitous/XiUOS/resources/spi/dev_spi.c index 22798e509..b2cc0f0ae 100644 --- a/Ubiquitous/XiUOS/resources/spi/dev_spi.c +++ b/Ubiquitous/XiUOS/resources/spi/dev_spi.c @@ -52,17 +52,30 @@ static uint32 SpiDeviceWrite(void *dev, struct BusBlockWriteParam *write_param) NULL_PARAM_CHECK(dev); NULL_PARAM_CHECK(write_param); + int ret; struct SpiHardwareDevice *spi_dev = (struct SpiHardwareDevice *)dev; - struct SpiDataStandard spi_msg; + struct SpiDataStandard *spi_msg; - spi_msg.tx_buff = (uint8 *)write_param->buffer; - spi_msg.rx_buff = NONE; - spi_msg.length = write_param->size; - spi_msg.spi_chip_select = 0; - spi_msg.spi_cs_release = 0; - spi_msg.next = NONE; + spi_msg = (struct SpiDataStandard *)x_malloc(sizeof(struct SpiDataStandard)); + if (NONE == spi_msg) { + KPrintf("SpiDeviceWrite x_malloc msg error\n"); + x_free(spi_msg); + return ERROR; + } - return spi_dev->spi_dev_done->dev_write(spi_dev, &spi_msg); + //memset(spi_msg, 0, sizeof(struct SpiDataStandard)); + + spi_msg->tx_buff = (uint8 *)write_param->buffer; + spi_msg->rx_buff = NONE; + spi_msg->length = write_param->size; + spi_msg->spi_chip_select = 0; + spi_msg->spi_cs_release = 0; + spi_msg->next = NONE; + + ret = spi_dev->spi_dev_done->dev_write(spi_dev, spi_msg); + x_free(spi_msg); + + return ret; } static uint32 SpiDeviceRead(void *dev, struct BusBlockReadParam *read_param) @@ -70,17 +83,30 @@ static uint32 SpiDeviceRead(void *dev, struct BusBlockReadParam *read_param) NULL_PARAM_CHECK(dev); NULL_PARAM_CHECK(read_param); + int ret; struct SpiHardwareDevice *spi_dev = (struct SpiHardwareDevice *)dev; - struct SpiDataStandard spi_msg; + struct SpiDataStandard *spi_msg; - spi_msg.tx_buff = NONE; - spi_msg.rx_buff = (uint8 *)read_param->buffer; - spi_msg.length = read_param->size; - spi_msg.spi_chip_select = 0; - spi_msg.spi_cs_release = 0; - spi_msg.next = NONE; + spi_msg = (struct SpiDataStandard *)x_malloc(sizeof(struct SpiDataStandard)); + if (NONE == spi_msg) { + KPrintf("SpiDeviceRead x_malloc msg error\n"); + x_free(spi_msg); + return ERROR; + } - return spi_dev->spi_dev_done->dev_read(spi_dev, &spi_msg); + //memset(spi_msg, 0, sizeof(struct SpiDataStandard)); + + spi_msg->tx_buff = NONE; + spi_msg->rx_buff = (uint8 *)read_param->buffer; + spi_msg->length = read_param->size; + spi_msg->spi_chip_select = 0; + spi_msg->spi_cs_release = 0; + spi_msg->next = NONE; + + ret = spi_dev->spi_dev_done->dev_read(spi_dev, spi_msg); + x_free(spi_msg); + + return ret; } static const struct HalDevDone dev_done = @@ -183,12 +209,27 @@ int SpiDevConfigureCs(struct HardwareDev *dev, uint8 spi_chip_select, uint8 spi_ { NULL_PARAM_CHECK(dev); + int ret; struct SpiHardwareDevice *spi_dev = (struct SpiHardwareDevice *)dev; - struct SpiDataStandard msg; + struct SpiDataStandard *msg; - memset(&msg, 0, sizeof(struct SpiDataStandard)); - msg.spi_chip_select = spi_chip_select; - msg.spi_cs_release = spi_cs_release; + msg = (struct SpiDataStandard *)x_malloc(sizeof(struct SpiDataStandard)); + if (NONE == msg) { + KPrintf("SpiDevConfigureCs x_malloc msg error\n"); + x_free(msg); + return ERROR; + } - return spi_dev->spi_dev_done->dev_write(spi_dev, &msg); + //memset(msg, 0, sizeof(struct SpiDataStandard)); + msg->length = 0; + msg->rx_buff = NONE; + msg->tx_buff = NONE; + msg->next = NONE; + msg->spi_chip_select = spi_chip_select; + msg->spi_cs_release = spi_cs_release; + + ret = spi_dev->spi_dev_done->dev_write(spi_dev, msg); + + x_free(msg); + return ret; } diff --git a/Ubiquitous/XiUOS/resources/usb/third_party_usb/usbhost/core/core.c b/Ubiquitous/XiUOS/resources/usb/third_party_usb/usbhost/core/core.c index e2f587551..783a57e6f 100644 --- a/Ubiquitous/XiUOS/resources/usb/third_party_usb/usbhost/core/core.c +++ b/Ubiquitous/XiUOS/resources/usb/third_party_usb/usbhost/core/core.c @@ -491,14 +491,14 @@ x_err_t UsbhClearFeature(UinstPointer device, int endpoint, int feature) x_err_t UsbhGetInterfaceDescriptor(UcfgDescPointer CfgDesc, int num, UintfDescPointer* IntfDesc) { - uint64 ptr, depth = 0; + uint32 ptr, depth = 0; UdescPointer desc; NULL_PARAM_CHECK(CfgDesc); - ptr = (uint64)CfgDesc + CfgDesc->bLength; - while(ptr < (uint64)CfgDesc + CfgDesc->wTotalLength) + ptr = (uint32)CfgDesc + CfgDesc->bLength; + while(ptr < (uint32)CfgDesc + CfgDesc->wTotalLength) { if(depth++ > 0x20) { @@ -517,7 +517,7 @@ x_err_t UsbhGetInterfaceDescriptor(UcfgDescPointer CfgDesc, int num, return EOK; } } - ptr = (uint64)desc + desc->bLength; + ptr = (uint32)desc + desc->bLength; } KPrintf("usb_get_interface_descriptor %d failed\n", num); @@ -538,7 +538,7 @@ x_err_t UsbhGetEndpointDescriptor(UintfDescPointer IntfDesc, int num, UepDescPointer* EpDesc) { int count = 0, depth = 0; - uint64 ptr; + uint32 ptr; UdescPointer desc; @@ -546,7 +546,7 @@ x_err_t UsbhGetEndpointDescriptor(UintfDescPointer IntfDesc, int num, CHECK(num < IntfDesc->bNumEndpoints); *EpDesc = NONE; - ptr = (uint64)IntfDesc + IntfDesc->bLength; + ptr = (uint32)IntfDesc + IntfDesc->bLength; while(count < IntfDesc->bNumEndpoints) { if(depth++ > 0x20) @@ -567,7 +567,7 @@ x_err_t UsbhGetEndpointDescriptor(UintfDescPointer IntfDesc, int num, } else count++; } - ptr = (uint64)desc + desc->bLength; + ptr = (uint32)desc + desc->bLength; } KPrintf("usb_get_endpoint_descriptor %d failed\n", num); diff --git a/Ubiquitous/XiUOS/resources/usb/third_party_usb/usbhost/core/hub.c b/Ubiquitous/XiUOS/resources/usb/third_party_usb/usbhost/core/hub.c index 059404274..e2b75cc45 100644 --- a/Ubiquitous/XiUOS/resources/usb/third_party_usb/usbhost/core/hub.c +++ b/Ubiquitous/XiUOS/resources/usb/third_party_usb/usbhost/core/hub.c @@ -49,7 +49,7 @@ static x_err_t RootHubCtrl(struct uhcd *hcd, uint16 port, uint8 cmd, void *args) hcd->roothub->PortStatus[port-1] = (*(uint32 *)args); break; case RH_CLEAR_PORT_FEATURE: - switch(((uint64)args)) + switch(((uint32)args)) { case PORT_FEAT_C_CONNECTION: hcd->roothub->PortStatus[port-1] &= ~PORT_CCSC; @@ -69,7 +69,7 @@ static x_err_t RootHubCtrl(struct uhcd *hcd, uint16 port, uint8 cmd, void *args) } break; case RH_SET_PORT_FEATURE: - switch((uint64)args) + switch((uint32)args) { case PORT_FEAT_CONNECTION: hcd->roothub->PortStatus[port-1] |= PORT_CCSC; @@ -222,7 +222,7 @@ x_err_t UsbhHubClearPortFeature(UhubPointer hub, uint16 port, uint16 feature) if(hub->IsRoothub) { RootHubCtrl(hub->hcd, port, RH_CLEAR_PORT_FEATURE, - (void*)(uint64)feature); + (void*)(uint32)feature); return EOK; } @@ -254,7 +254,7 @@ x_err_t UsbhHubSetPortFeature(UhubPointer hub, uint16 port, if(hub->IsRoothub) { RootHubCtrl(hub->hcd, port, RH_SET_PORT_FEATURE, - (void*)(uint64)feature); + (void*)(uint32)feature); return EOK; }