diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs index cb3d62f..8591f04 100644 --- a/.settings/org.eclipse.core.resources.prefs +++ b/.settings/org.eclipse.core.resources.prefs @@ -14,3 +14,4 @@ encoding//StdPeriphDriver/inc/CH58x_gpio.h=GBK encoding//StdPeriphDriver/inc/CH58x_sys.h=GBK encoding//StdPeriphDriver/inc/ISP585.h=GBK encoding//bsp/inc/bsp_uart.h=UTF-8 +encoding//bsp/src/bsp_master.c=GBK diff --git a/APP/multiCentral.c b/APP/multiCentral.c index ce378f8..60e2ecb 100644 --- a/APP/multiCentral.c +++ b/APP/multiCentral.c @@ -353,7 +353,6 @@ static uint16_t connect0_ProcessEvent(uint8_t task_id, uint16_t events) centralConnIistStartDiscovery_0(); return (events ^ START_SVC_DISCOVERY_EVT); } - if (events & START_READ_OR_WRITE_EVT) { if (centralConnList[CONNECT0_ITEM].procedureInProgress == FALSE) @@ -399,25 +398,41 @@ static uint16_t connect0_ProcessEvent(uint8_t task_id, uint16_t events) } return (events ^ START_READ_OR_WRITE_EVT); } - - if (events & CH3_WRITE_EVT) + if (events & CH2_READ_EVT) { - PRINT("CH3_WRITE_EVT\r\n"); + logDebug("CH2_READ_EVT\r\n"); if (centralConnList[CONNECT0_ITEM].procedureInProgress == FALSE) { - PRINT("centralConnList CH3_WRITE_EVT\n"); + // read + attReadReq_t req; + + req.handle = centralConnList[CONNECT0_ITEM].charHd2; + if (GATT_ReadCharValue(centralConnList[CONNECT0_ITEM].connHandle, &req, centralTaskId) == SUCCESS) + { + centralConnList[CONNECT0_ITEM].procedureInProgress = TRUE; + tmos_start_task(centralConnList[CONNECT0_ITEM].taskID, CH2_READ_EVT, 160000); + } + } + return (events ^ CH2_READ_EVT); + } + if (events & CH3_WRITE_EVT) + { + logDebug("CH3_WRITE_EVT\r\n"); + if (centralConnList[CONNECT0_ITEM].procedureInProgress == FALSE) + { + logDebug("centralConnList CH3_WRITE_EVT\n"); static uint8_t sendcnt = 0; attWriteReq_t req; req.cmd = FALSE; req.sig = FALSE; req.handle = centralConnList[CONNECT0_ITEM].charHd3; - PRINT("handle:%x\r\n", req.handle); + logDebug("handle:%x\r\n", req.handle); req.len = 3; req.pValue = GATT_bm_alloc(centralConnList[CONNECT0_ITEM].connHandle, ATT_WRITE_REQ, req.len, NULL, 0); if (req.pValue != NULL) { - PRINT("req.pValue != NULL\r\n"); + logDebug("req.pValue != NULL\r\n"); req.pValue[0] = 0x11; req.pValue[1] = 0x22; req.pValue[2] = sendcnt++; @@ -425,8 +440,8 @@ static uint16_t connect0_ProcessEvent(uint8_t task_id, uint16_t events) if (GATT_WriteCharValue(centralConnList[CONNECT0_ITEM].connHandle, &req, centralTaskId) == SUCCESS) { centralConnList[CONNECT0_ITEM].procedureInProgress = TRUE; - tmos_start_task(centralConnList[CONNECT0_ITEM].taskID, CH3_WRITE_EVT, DEFAULT_READ_OR_WRITE_DELAY); - PRINT("GATT_WriteCharValue\r\n"); + tmos_start_task(centralConnList[CONNECT0_ITEM].taskID, CH3_WRITE_EVT, 3200); + logDebug("GATT_WriteCharValue\r\n"); } else { @@ -436,7 +451,73 @@ static uint16_t connect0_ProcessEvent(uint8_t task_id, uint16_t events) } return (events ^ CH3_WRITE_EVT); } + if (events & CH4_NOTI_EVT) + { + logDebug("CH4_NOTI_EVT\r\n"); + if (centralConnList[CONNECT0_ITEM].procedureInProgress == FALSE) + { + // Do a write + attWriteReq_t req; + logDebug("CH4_NOTI_EVT handle:%x\r\n", req.handle); + req.cmd = FALSE; + req.sig = FALSE; + req.handle = centralConnList[CONNECT0_ITEM].charHd4; + req.len = 2; + req.pValue = GATT_bm_alloc(centralConnList[CONNECT0_ITEM].connHandle, ATT_WRITE_REQ, req.len, NULL, 0); // 内存申请 + if (req.pValue != NULL) + { + logDebug("CH4_NOTI_EVT req.pValue != NULL\r\n"); + req.pValue[0] = 1; + req.pValue[1] = 0; + if (GATT_WriteCharValue(centralConnList[CONNECT0_ITEM].connHandle, &req, centralTaskId) == SUCCESS) // 发送 + { + logDebug("GATT_WriteCharValue CH4_NOTI_EVT\r\n"); + centralConnList[CONNECT0_ITEM].procedureInProgress = TRUE; + } + else + { + GATT_bm_free((gattMsg_t *)&req, ATT_WRITE_REQ); // 释放内存 + } + } + } + return (events ^ CH4_NOTI_EVT); + } + // if (events & START_WRITE_CCCD_EVT) + // { + // if (centralConnList[CONNECT0_ITEM].procedureInProgress == FALSE) + // { + // // Do a write + // attWriteReq_t req; + + // req.cmd = FALSE; + // req.sig = FALSE; + // req.handle = centralConnList[CONNECT0_ITEM].cccHdl; + // req.len = 2; + // req.pValue = GATT_bm_alloc(centralConnList[CONNECT0_ITEM].connHandle, ATT_WRITE_REQ, req.len, NULL, 0); + // if (req.pValue != NULL) + // { + // req.pValue[0] = 1; + // req.pValue[1] = 0; + + // if (GATT_WriteCharValue(centralConnList[CONNECT0_ITEM].connHandle, &req, centralTaskId) == SUCCESS) + // { + // centralConnList[CONNECT0_ITEM].procedureInProgress = TRUE; + // } + // else + // { + // GATT_bm_free((gattMsg_t *)&req, ATT_WRITE_REQ); + // } + // } + // } + // return (events ^ START_WRITE_CCCD_EVT); + // } + if (events & START_READ_RSSI_EVT) + { + GAPRole_ReadRssiCmd(centralConnList[CONNECT0_ITEM].connHandle); + tmos_start_task(centralConnList[CONNECT0_ITEM].taskID, START_READ_RSSI_EVT, DEFAULT_RSSI_PERIOD); + return (events ^ START_READ_RSSI_EVT); + } if (events & START_PARAM_UPDATE_EVT) { // start connect parameter update @@ -448,43 +529,6 @@ static uint16_t connect0_ProcessEvent(uint8_t task_id, uint16_t events) return (events ^ START_PARAM_UPDATE_EVT); } - - if (events & START_WRITE_CCCD_EVT) - { - if (centralConnList[CONNECT0_ITEM].procedureInProgress == FALSE) - { - // Do a write - attWriteReq_t req; - - req.cmd = FALSE; - req.sig = FALSE; - req.handle = centralConnList[CONNECT0_ITEM].cccHdl; - req.len = 2; - req.pValue = GATT_bm_alloc(centralConnList[CONNECT0_ITEM].connHandle, ATT_WRITE_REQ, req.len, NULL, 0); - if (req.pValue != NULL) - { - req.pValue[0] = 1; - req.pValue[1] = 0; - - if (GATT_WriteCharValue(centralConnList[CONNECT0_ITEM].connHandle, &req, centralTaskId) == SUCCESS) - { - centralConnList[CONNECT0_ITEM].procedureInProgress = TRUE; - } - else - { - GATT_bm_free((gattMsg_t *)&req, ATT_WRITE_REQ); - } - } - } - return (events ^ START_WRITE_CCCD_EVT); - } - - if (events & START_READ_RSSI_EVT) - { - GAPRole_ReadRssiCmd(centralConnList[CONNECT0_ITEM].connHandle); - tmos_start_task(centralConnList[CONNECT0_ITEM].taskID, START_READ_RSSI_EVT, DEFAULT_RSSI_PERIOD); - return (events ^ START_READ_RSSI_EVT); - } // Discard unknown events return 0; } @@ -544,14 +588,14 @@ static void centralProcessGATTMsg(gattMsgEvent_t *pMsg) { uint8_t status = pMsg->msg.errorRsp.errCode; - PRINT("Exchange MTU Error: %x\r\n", status); + logDebug("Exchange MTU Error: %x\r\n", status); } centralConnList[connItem].procedureInProgress = FALSE; } if (pMsg->method == ATT_MTU_UPDATED_EVENT) { - PRINT("MTU: %x\r\n", pMsg->msg.mtuEvt.MTU); + logDebug("MTU: %x\r\n", pMsg->msg.mtuEvt.MTU); } if ((pMsg->method == ATT_READ_RSP) || @@ -562,12 +606,13 @@ static void centralProcessGATTMsg(gattMsgEvent_t *pMsg) { uint8_t status = pMsg->msg.errorRsp.errCode; - PRINT("Read Error: %x\r\n", status); + logDebug("Read Error: %x\r\n", status); } else { // After a successful read, display the read value - PRINT("Read rsp: %x\r\n", *pMsg->msg.readRsp.pValue); + logDebug("Read rsp: "); + logHexDumpAll(pMsg->msg.readRsp.pValue, pMsg->msg.readRsp.len); } centralConnList[connItem].procedureInProgress = FALSE; } @@ -579,19 +624,19 @@ static void centralProcessGATTMsg(gattMsgEvent_t *pMsg) { uint8_t status = pMsg->msg.errorRsp.errCode; - PRINT("Write Error: %x\r\n", status); + logDebug("Write Error: %x\r\n", status); } else { // After a succesful write, display the value that was written and increment value - PRINT("Write sent: %x\r\n", centralCharVal); + logDebug("Write sent: %x\r\n", centralCharVal); } centralConnList[connItem].procedureInProgress = FALSE; } else if (pMsg->method == ATT_HANDLE_VALUE_NOTI) { - // PRINT("Receive noti: %x\r\n", *pMsg->msg.handleValueNoti.pValue); + logDebug("Noti: "); logHexDumpAll(pMsg->msg.handleValueNoti.pValue, pMsg->msg.handleValueNoti.len); } else if (centralConnList[connItem].discState != BLE_DISC_STATE_IDLE) @@ -613,7 +658,7 @@ static void centralProcessGATTMsg(gattMsgEvent_t *pMsg) */ static void centralRssiCB(uint16_t connHandle, int8_t rssi) { - PRINT("RSSI -%d dB Conn - %x \r\n", -rssi, connHandle); + logDebug("RSSI -%d dB Conn - %x \r\n", -rssi, connHandle); } /********************************************************************* @@ -645,7 +690,7 @@ static void centralEventCB(gapRoleEvent_t *pEvent) { case GAP_DEVICE_INIT_DONE_EVENT: { - PRINT("Discovering...\r\n"); + logDebug("Discovering...\r\n"); GAPRole_CentralStartDiscovery(DEFAULT_DISCOVERY_MODE, DEFAULT_DISCOVERY_ACTIVE_SCAN, DEFAULT_DISCOVERY_WHITE_LIST); @@ -673,18 +718,18 @@ static void centralEventCB(gapRoleEvent_t *pEvent) // Peer device not found if (i == centralScanRes) { - PRINT("Device not found...\r\n"); + logDebug("Device not found...\r\n"); centralScanRes = 0; GAPRole_CentralStartDiscovery(DEFAULT_DISCOVERY_MODE, DEFAULT_DISCOVERY_ACTIVE_SCAN, DEFAULT_DISCOVERY_WHITE_LIST); - PRINT("Discovering...\r\n"); + logDebug("Discovering...\r\n"); } // Peer device found else { - PRINT("Device found...\r\n"); + logDebug("Device found...\r\n"); GAPRole_CentralEstablishLink(DEFAULT_LINK_HIGH_DUTY_CYCLE, DEFAULT_LINK_WHITE_LIST, centralDevList[i].addrType, @@ -692,7 +737,7 @@ static void centralEventCB(gapRoleEvent_t *pEvent) // Start establish link timeout event tmos_start_task(centralTaskId, ESTABLISH_LINK_TIMEOUT_EVT, ESTABLISH_LINK_TIMEOUT); - PRINT("Connecting...\r\n"); + logDebug("Connecting...\r\n"); } } break; @@ -712,14 +757,14 @@ static void centralEventCB(gapRoleEvent_t *pEvent) if (connItem == CENTRAL_MAX_CONNECTION) { GAPRole_TerminateLink(pEvent->linkCmpl.connectionHandle); - PRINT("Connection max...\r\n"); + logDebug("Connection max...\r\n"); } else { centralConnList[connItem].state = BLE_STATE_CONNECTED; centralConnList[connItem].connHandle = pEvent->linkCmpl.connectionHandle; - PRINT("Conn %x - Int %x \r\n", pEvent->linkCmpl.connectionHandle, pEvent->linkCmpl.connInterval); + logDebug("Conn %x - Int %x \r\n", pEvent->linkCmpl.connectionHandle, pEvent->linkCmpl.connInterval); // 连接0 if (connItem == CONNECT0_ITEM) @@ -746,7 +791,7 @@ static void centralEventCB(gapRoleEvent_t *pEvent) // { // } - PRINT("Connected...\r\n"); + logDebug("Connected...\r\n"); // See if need discover again for (connItem = 0; connItem < CENTRAL_MAX_CONNECTION; connItem++) @@ -756,7 +801,7 @@ static void centralEventCB(gapRoleEvent_t *pEvent) } if (connItem < CENTRAL_MAX_CONNECTION) { - PRINT("Discovering...\r\n"); + logDebug("Discovering...\r\n"); centralScanRes = 0; GAPRole_CentralStartDiscovery(DEFAULT_DISCOVERY_MODE, DEFAULT_DISCOVERY_ACTIVE_SCAN, @@ -766,8 +811,8 @@ static void centralEventCB(gapRoleEvent_t *pEvent) } else { - PRINT("Connect Failed...Reason:%X\r\n", pEvent->gap.hdr.status); - PRINT("Discovering...\r\n"); + logDebug("Connect Failed...Reason:%X\r\n", pEvent->gap.hdr.status); + logDebug("Discovering...\r\n"); centralScanRes = 0; GAPRole_CentralStartDiscovery(DEFAULT_DISCOVERY_MODE, DEFAULT_DISCOVERY_ACTIVE_SCAN, @@ -788,7 +833,7 @@ static void centralEventCB(gapRoleEvent_t *pEvent) { // Should not go there } - PRINT(" %x Disconnected...Reason:%x\r\n", centralConnList[connItem].connHandle, pEvent->linkTerminate.reason); + logDebug(" %x Disconnected...Reason:%x\r\n", centralConnList[connItem].connHandle, pEvent->linkTerminate.reason); centralConnList[connItem].state = BLE_STATE_IDLE; centralConnList[connItem].connHandle = GAP_CONNHANDLE_INIT; centralConnList[connItem].discState = BLE_DISC_STATE_IDLE; @@ -798,7 +843,7 @@ static void centralEventCB(gapRoleEvent_t *pEvent) tmos_stop_task(centralConnList[connItem].taskID, START_READ_RSSI_EVT); - PRINT("Discovering...\r\n"); + logDebug("Discovering...\r\n"); GAPRole_CentralStartDiscovery(DEFAULT_DISCOVERY_MODE, DEFAULT_DISCOVERY_ACTIVE_SCAN, DEFAULT_DISCOVERY_WHITE_LIST); @@ -807,7 +852,7 @@ static void centralEventCB(gapRoleEvent_t *pEvent) case GAP_LINK_PARAM_UPDATE_EVENT: { - PRINT("Update %x - Int %x \r\n", pEvent->linkUpdate.connectionHandle, pEvent->linkUpdate.connInterval); + logDebug("Update %x - Int %x \r\n", pEvent->linkUpdate.connectionHandle, pEvent->linkUpdate.connInterval); } break; @@ -827,35 +872,35 @@ static void centralPairStateCB(uint16_t connHandle, uint8_t state, uint8_t statu { if (state == GAPBOND_PAIRING_STATE_STARTED) { - PRINT("Connection %04x - Pairing started:%d\r\n", connHandle, status); + logDebug("Connection %04x - Pairing started:%d\r\n", connHandle, status); } else if (state == GAPBOND_PAIRING_STATE_COMPLETE) { if (status == SUCCESS) { - PRINT("Connection %04x - Pairing success\r\n", connHandle); + logDebug("Connection %04x - Pairing success\r\n", connHandle); } else { - PRINT("Connection %04x - Pairing fail\r\n", connHandle); + logDebug("Connection %04x - Pairing fail\r\n", connHandle); } } else if (state == GAPBOND_PAIRING_STATE_BONDED) { if (status == SUCCESS) { - PRINT("Connection %04x - Bonding success\r\n", connHandle); + logDebug("Connection %04x - Bonding success\r\n", connHandle); } } else if (state == GAPBOND_PAIRING_STATE_BOND_SAVED) { if (status == SUCCESS) { - PRINT("Connection %04x - Bond save success\r\n", connHandle); + logDebug("Connection %04x - Bond save success\r\n", connHandle); } else { - PRINT("Connection %04x - Bond save failed: %d\r\n", connHandle, status); + logDebug("Connection %04x - Bond save failed: %d\r\n", connHandle, status); } } } @@ -878,7 +923,7 @@ static void centralPasscodeCB(uint8_t *deviceAddr, uint16_t connectionHandle, // Display passcode to user if (uiOutputs != 0) { - PRINT("Passcode:%06d\r\n", (int)passcode); + logDebug("Passcode:%06d\r\n", (int)passcode); } // Send passcode response GAPBondMgr_PasscodeRsp(connectionHandle, SUCCESS, passcode); @@ -931,8 +976,8 @@ static void centralGATTDiscoveryEvent(uint8_t connItem, gattMsgEvent_t *pMsg) centralConnList[connItem].svcEndHdl = ATT_GRP_END_HANDLE(pMsg->msg.findByTypeValueRsp.pHandlesInfo, 0); // Display Profile Service handle range - PRINT("Found Profile Service handle : %x ~ %x \r\n", centralConnList[connItem].svcStartHdl, centralConnList[connItem].svcEndHdl); -#if 1 + logDebug("Found Profile Service handle : %x ~ %x \r\n", centralConnList[connItem].svcStartHdl, centralConnList[connItem].svcEndHdl); +#if 0 for (uint16_t i = 0; i < pMsg->msg.readByGrpTypeRsp.numGrps; i++) { // uuid @@ -959,159 +1004,96 @@ static void centralGATTDiscoveryEvent(uint8_t connItem, gattMsgEvent_t *pMsg) pMsg->hdr.status == bleProcedureComplete) || (pMsg->method == ATT_ERROR_RSP)) { -#if 0 - if(centralConnList[connItem].svcStartHdl != 0) - { - // Discover characteristic - centralConnList[connItem].discState = BLE_DISC_STATE_CHAR; - req.startHandle = centralConnList[connItem].svcStartHdl; - req.endHandle = centralConnList[connItem].svcEndHdl; - req.type.len = ATT_BT_UUID_SIZE; - req.type.uuid[0] = LO_UINT16(SIMPLEPROFILE_CHAR1_UUID); - req.type.uuid[1] = HI_UINT16(SIMPLEPROFILE_CHAR1_UUID); - - GATT_ReadUsingCharUUID(centralConnList[connItem].connHandle, &req, centralTaskId); - } -#endif // Discover characteristic centralConnList[connItem].discState = BLE_DISC_STATE_CHAR; - uint8_t ret = GATT_DiscAllChars(centralConnList[connItem].connHandle, 0x01,0xFFFF, centralTaskId); - PRINT("GATT_DiscAllChars:%02x\r\n", ret); + uint8_t ret = GATT_DiscAllChars(centralConnList[connItem].connHandle, 0x01, 0xFFFF, centralTaskId); + logDebug("GATT_DiscAllChars:%02x\r\n", ret); } } else if (centralConnList[connItem].discState == BLE_DISC_STATE_CHAR) { -#if 0 // Characteristic found, store handle - if(pMsg->method == ATT_READ_BY_TYPE_RSP && - pMsg->msg.readByTypeRsp.numPairs > 0) - { - centralConnList[connItem].charHdl = BUILD_UINT16(pMsg->msg.readByTypeRsp.pDataList[0], - pMsg->msg.readByTypeRsp.pDataList[1]); - centralConnList[connItem].procedureInProgress = FALSE; - - // Start do read or write - tmos_start_task(centralConnList[connItem].taskID, START_READ_OR_WRITE_EVT, DEFAULT_READ_OR_WRITE_DELAY); - - // Display Characteristic 1 handle - PRINT("Found Characteristic 1 handle : %x \r\n", centralConnList[0].charHdl); - } - - if((pMsg->method == ATT_READ_BY_TYPE_RSP && - pMsg->hdr.status == bleProcedureComplete) || - (pMsg->method == ATT_ERROR_RSP)) - { - // Discover characteristic - centralConnList[connItem].discState = BLE_DISC_STATE_CCCD; - req.startHandle = centralConnList[connItem].svcStartHdl; - req.endHandle = centralConnList[connItem].svcEndHdl; - req.type.len = ATT_BT_UUID_SIZE; - req.type.uuid[0] = LO_UINT16(GATT_CLIENT_CHAR_CFG_UUID); - req.type.uuid[1] = HI_UINT16(GATT_CLIENT_CHAR_CFG_UUID); - - GATT_ReadUsingCharUUID(centralConnList[connItem].connHandle, &req, centralTaskId); - } -#endif - // Characteristic found, store handle - if(pMsg->method == ATT_READ_BY_TYPE_RSP && - pMsg->msg.readByTypeRsp.numPairs > 0) + if (pMsg->method == ATT_READ_BY_TYPE_RSP && + pMsg->msg.readByTypeRsp.numPairs > 0) { for (unsigned char i = 0; i < pMsg->msg.readByTypeRsp.numPairs; i++) { // characteristic properties uint8_t char_properties = pMsg->msg.readByTypeRsp.pDataList[pMsg->msg.readByTypeRsp.len * i + 2]; uint16_t char_value_handle = BUILD_UINT16(pMsg->msg.readByTypeRsp.pDataList[pMsg->msg.readByTypeRsp.len * i + 3], - pMsg->msg.readByTypeRsp.pDataList[pMsg->msg.readByTypeRsp.len * i + 4]); + pMsg->msg.readByTypeRsp.pDataList[pMsg->msg.readByTypeRsp.len * i + 4]); // characteristic uuid length uint8_t char_uuid_length = pMsg->msg.readByGrpTypeRsp.len - 5; // uuid uint8_t *char_uuid = &(pMsg->msg.readByGrpTypeRsp.pDataList[pMsg->msg.readByGrpTypeRsp.len * i + 5]); - PRINT("______________________________\n"); - PRINT("char_uuid :"); + logDebug("|-------------------------------------|"); + logDebug("char_uuid\t:"); for (uint8_t i = 0; i < char_uuid_length; i++) { - printf("%02x ", char_uuid[i]); + logDebug("%02x ", char_uuid[i]); } - printf("\n"); - // PRINT("char_properties :%02x,%s\r\n",char_properties,(char_properties&(GATT_PROP_WRITE|GATT_PROP_WRITE_NO_RSP))?"wite handle":""); - PRINT("char_value_handle:%04x\r\n", char_value_handle); - PRINT("char_uuid_length :%02d bit\r\n", char_uuid_length); + logDebug("char_value_handle\t:%04x\r\n", char_value_handle); + logDebug("char_uuid_length\t:%02d bit\r\n", char_uuid_length); uint16_t char1uuid = SIMPLEPROFILE_CHAR1_UUID; uint16_t char2uuid = SIMPLEPROFILE_CHAR2_UUID; uint16_t char3uuid = SIMPLEPROFILE_CHAR3_UUID; - // req.type.uuid[0] = LO_UINT16(GATT_CLIENT_CHAR_CFG_UUID); - // req.type.uuid[1] = HI_UINT16(GATT_CLIENT_CHAR_CFG_UUID); - if (tmos_memcmp(char_uuid, &char3uuid, 2)) + uint16_t char4uuid = SIMPLEPROFILE_CHAR4_UUID; + if (tmos_memcmp(char_uuid, &char1uuid, 2)) { if (char_properties & (GATT_PROP_WRITE | GATT_PROP_WRITE_NO_RSP)) { - centralConnList[connItem].charHd3 = char_value_handle; - PRINT("char3 Write handle:%04x\r\n", char_value_handle); + centralConnList[connItem].charHdl = char_value_handle; + logDebug("char1 Write handle\t:%04x\r\n", char_value_handle); centralConnList[connItem].procedureInProgress = FALSE; - tmos_start_task(centralConnList[CONNECT0_ITEM].taskID, CH3_WRITE_EVT, 1600); + // tmos_start_task(centralConnList[CONNECT0_ITEM].taskID, START_READ_OR_WRITE_EVT, 1600); } } else if (tmos_memcmp(char_uuid, &char2uuid, 2)) { if (char_properties & (GATT_PROP_READ)) { - centralConnList[connItem].charHd2 = char_value_handle; + centralConnList[connItem].charHd2 = char_value_handle; centralConnList[connItem].procedureInProgress = FALSE; - PRINT("char2 Write handle:%04x\r\n", char_value_handle); - // tmos_start_task(centralConnList[CONNECT0_ITEM].taskID, CH2_READ_EVT, 1600); + logDebug("char2 Write handle\t:%04x\r\n", char_value_handle); + // tmos_start_task(centralConnList[CONNECT0_ITEM].taskID, CH2_READ_EVT, 2400); } } - // if (char_properties & (GATT_PROP_WRITE | GATT_PROP_WRITE_NO_RSP)) - // { - // centralConnList[connItem].charHdl = char_value_handle; - // PRINT("Write handle:%04x\r\n", char_value_handle); - // tmos_start_task(centralTaskId, START_READ_OR_WRITE_EVT, 1600); - // } - if (char_properties & GATT_PROP_NOTIFY) + else if (tmos_memcmp(char_uuid, &char3uuid, 2)) { - centralConnList[connItem].cccHdl = char_value_handle + 1; // 通过GATT_DiscAllChars或者handle,noti/indi的handle值需要+1 - PRINT("Notify handle:%04x\r\n", char_value_handle); - tmos_start_task(centralConnList[CONNECT0_ITEM].taskID, START_WRITE_CCCD_EVT, 1600); + if (char_properties & (GATT_PROP_WRITE | GATT_PROP_WRITE_NO_RSP)) + { + centralConnList[connItem].charHd3 = char_value_handle; + logDebug("char3 Write handle\t:%04x\r\n", char_value_handle); + centralConnList[connItem].procedureInProgress = FALSE; + tmos_start_task(centralConnList[CONNECT0_ITEM].taskID, CH3_WRITE_EVT, 3200); + } } - if (char_properties & GATT_PROP_INDICATE) + else if (tmos_memcmp(char_uuid, &char4uuid, 2)) { - centralConnList[connItem].cccHdl = char_value_handle + 1; // 通过GATT_DiscAllChars或者handle,noti/indi的handle值需要+1 - PRINT("Indicate handle:%04x\r\n", char_value_handle); - tmos_start_task(centralConnList[CONNECT0_ITEM].taskID, START_WRITE_CCCD_EVT, 800); + if (char_properties & (GATT_PROP_NOTIFY)) + { + centralConnList[connItem].discState = BLE_DISC_STATE_IDLE; + centralConnList[connItem].charHd4 = char_value_handle + 1; + centralConnList[connItem].procedureInProgress = FALSE; + logDebug("char4 Write handle\t:%04x\r\n", char_value_handle); + tmos_start_task(centralConnList[CONNECT0_ITEM].taskID, CH4_NOTI_EVT, 1600); + + } } } } } - else if (centralConnList[connItem].discState == BLE_DISC_STATE_CCCD) - { - // Characteristic found, store handle - if (pMsg->method == ATT_READ_BY_TYPE_RSP && - pMsg->msg.readByTypeRsp.numPairs > 0) - { - centralConnList[connItem].cccHdl = BUILD_UINT16(pMsg->msg.readByTypeRsp.pDataList[0], - pMsg->msg.readByTypeRsp.pDataList[1]); - - centralConnList[connItem].procedureInProgress = FALSE; - - // Start do write CCCD - tmos_start_task(centralConnList[connItem].taskID, START_WRITE_CCCD_EVT, DEFAULT_WRITE_CCCD_DELAY); - - // Display Characteristic 1 handle - PRINT("Found client characteristic configuration handle : %x \r\n", centralConnList[connItem].cccHdl); - } - centralConnList[connItem].discState = BLE_DISC_STATE_IDLE; - } + // 连接1的枚举 + // else if (connItem == CONNECT1_ITEM) + // { + + // } + // 连接2的枚举 + // else if(connItem == CONNECT2_ITEM) + // { + // } } - // 连接1的枚举 - else if (connItem == CONNECT1_ITEM) - { - } - // 连接2的枚举 - // else if(connItem == CONNECT2_ITEM) - // { - // } } - /********************************************************************* * @fn centralAddDeviceInfo * @@ -1140,13 +1122,13 @@ static void centralAddDeviceInfo(uint8_t *pAddr, uint8_t addrType) // Increment scan result count centralScanRes++; // Display device addr - PRINT("Device %d - Addr %x %x %x %x %x %x \r\n", centralScanRes, - centralDevList[centralScanRes - 1].addr[0], - centralDevList[centralScanRes - 1].addr[1], - centralDevList[centralScanRes - 1].addr[2], - centralDevList[centralScanRes - 1].addr[3], - centralDevList[centralScanRes - 1].addr[4], - centralDevList[centralScanRes - 1].addr[5]); + logDebug("Device %d - Addr %x %x %x %x %x %x \r\n", centralScanRes, + centralDevList[centralScanRes - 1].addr[0], + centralDevList[centralScanRes - 1].addr[1], + centralDevList[centralScanRes - 1].addr[2], + centralDevList[centralScanRes - 1].addr[3], + centralDevList[centralScanRes - 1].addr[4], + centralDevList[centralScanRes - 1].addr[5]); } } @@ -1176,3 +1158,99 @@ static uint8_t centralAddrCmp(peerAddrDefItem_t *PeerAddrDef, uint8_t *addr) } /************************ endfile @ central **************************/ + +/*用户自定义文件*/ +void BSP_Master_Send(centralConnItem_t *centralConnList, uint8_t id, uint8_t *Data, uint8_t Len) +{ + attWriteReq_t req; + uint8_t res; + + if (centralConnList[id].state == BLE_STATE_CONNECTED) // 检查连接状态是否为连接状态 + { + req.cmd = FALSE; + req.sig = FALSE; + req.handle = centralConnList[id].charHd3; + req.len = Len; + req.pValue = GATT_bm_alloc(centralConnList[id].connHandle, ATT_WRITE_REQ, req.len, NULL, 0); + if (req.pValue != NULL) + { + tmos_memcpy(req.pValue, Data, Len); + res = GATT_WriteCharValue(centralConnList[id].connHandle, &req, centralTaskId); + if (res == SUCCESS) + { + logDebug("\n Master Write ok"); + } + else + { + logDebug("\n Master Write faild= %x", res); + GATT_bm_free((gattMsg_t *)&req, ATT_WRITE_REQ); + } + } + else + { + logDebug("\n alloc Faild"); + } + } +} + +int BSP_Master_Receive_Data(uint8_t *data, uint16_t len) +{ + int ret = 0; + uint16_t index = 0; + uint8_t data_buf[64] = {0}; + // 解析接收到的数据帧,先寻找AA开头,然后再找AA下一个字节,其代表了数据长度,然后找到代表长度的值的长度的下一位,其为校验码,校验码后为结束码0x55, + // 如果数据正确,则提取数据,不正确,则不处理 + if (len < 4) + { // 至少需要 4 个字节:起始码、长度、校验码、结束码 + logError("BT 数据帧长度不足"); + logHexDumpAll(data, len); + return 1; + } + while (index < len && data[index] != 0xAA) // 寻找起始码 0xAA + { + index++; + } + if (index >= len - 3) // 不够空间容纳长度、校验码和结束码 + { + logError("BT 数据帧起始码错误"); + logHexDumpAll(data, len); + return 2; + } + + uint16_t datalength = data[index + 1]; // 读取数据长度 + if (index + 2 + datalength + 1 >= len) // 检查数据长度是否合理 数据长度 + 校验码 + 结束码 + { + + logError("BT 数据帧长度错误"); + logHexDumpAll(data, len); + return 3; // + } + + uint8_t rx_sum = data[index + 2 + datalength]; // 读取校验码 + uint8_t calculated_sum = XOR_CheckSum(&data[0], datalength + 2); // 计算校验码 + if (rx_sum != calculated_sum) + { + + logError("BT 数据帧校验码错误 rx_sum = %02X, calculated_sum = %02X", rx_sum, calculated_sum); + logHexDumpAll(data, len); + return 4; + } + + if (data[index + 2 + datalength + 1] != 0x55) // 检查结束码 + { + logError("BT 数据帧结束码错误"); + logHexDumpAll(data, len); + return 5; + } + // 到这一步说明数据没问题,将接收到的数据通过中心任务发送出去 + // uint8_t *p_data; + // p_data = tmos_msg_allocate(length); + // if (p_data) + // { + // tmos_memcpy(p_data, data, length); + // tmos_msg_send(task_id, p_data); + // } + // + // tmos_msg_send(task_id, data); + return ret; +} diff --git a/Ld/Link.ld b/Ld/Link.ld index f6d6e18..06eaa32 100644 --- a/Ld/Link.ld +++ b/Ld/Link.ld @@ -1,9 +1,13 @@ ENTRY( _start ) +__stack_size = 1024; + +PROVIDE( __stack_size = __stack_size); + MEMORY { FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 448K - RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 128K + RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 96K } SECTIONS diff --git a/bsp/src/bsp_master.c b/bsp/src/bsp_master.c index 5ad0b5b..ef548f9 100644 --- a/bsp/src/bsp_master.c +++ b/bsp/src/bsp_master.c @@ -1,119 +1,23 @@ -// #include "bsp_master.h" -// #include "bsp_uart.h" -// #include "log.h" -// #include "multiCentral.h" -// #include "bsp_valve.h" +/* + * @Author: mbw + * @Date: 2024-12-11 13:52:38 + * @LastEditors: mbw && 1600520629@qq.com + * @LastEditTime: 2024-12-12 09:11:54 + * @FilePath: \ble_-tyq_-bjq_-ch584-m\bsp\src\bsp_Master.c + * @Description: + * + * Copyright (c) 2024 by ${git_name_email}, All Rights Reserved. + */ +#include "bsp_master.h" +#include "bsp_uart.h" +#include "log.h" +#include "multiCentral.h" +#include "bsp_valve.h" -// #undef LOG_ENABLE -// #define LOG_ENABLE 1 +#undef LOG_ENABLE +#define LOG_ENABLE 1 -// void BSP_Bt_Init(void) -// { -// } +void BSP_Bt_Init(void) +{ +} -// void BSP_Master_Send(centralConnItem_t *centralConnList, uint8_t id, uint8_t *Data, uint8_t Len) -// { -// attWriteReq_t req; -// uint8_t res; - -// if (centralConnList[id].state == BLE_STATE_CONNECTED) // 检查连接状态是否为连接状态 -// { -// if (centralConnList[id].procedureInProgress == TRUE) -// { -// logWarning("\n Write buy"); -// } -// else -// { -// req.cmd = FALSE; -// req.sig = FALSE; -// req.handle = centralConnList[id].connHandle; -// req.len = Len; -// req.pValue = GATT_bm_alloc(centralConnList[id].connHandle, ATT_WRITE_REQ, req.len, NULL, 0); -// if (req.pValue != NULL) -// { -// tmos_memcpy(req.pValue, Data, Len); -// res = GATT_WriteCharValue(centralConnList[id].connHandle, &req, centralTaskId); -// if (res == SUCCESS) -// { -// centralConnList[id].procedureInProgress = TRUE; // 翻转读写状态 -// logDebug("\n Master Write ok"); -// } -// else -// { -// logDebug("\n Master Write faild= %x", res); -// GATT_bm_free((gattMsg_t *)&req, ATT_WRITE_REQ); -// } -// } -// else -// { -// logDebug("\n alloc Faild"); -// } -// } -// } -// else -// { -// } -// } - -// int BSP_Master_Receive_Data(uint8_t *data, uint16_t len) -// { -// int ret = 0; -// uint16_t index = 0; -// uint8_t data_buf[64] = {0}; -// // 解析接收到的数据帧,先寻找AA开头,然后再找AA下一个字节,其代表了数据长度,然后找到代表长度的值的长度的下一位,其为校验码,校验码后为结束码0x55, -// // 如果数据正确,则提取数据,不正确,则不处理 -// if (len < 4) -// { // 至少需要 4 个字节:起始码、长度、校验码、结束码 -// logError("BT 数据帧长度不足"); -// logHexDumpAll(data, len); -// return 1; -// } - -// while (index < len && data[index] != 0xAA) // 寻找起始码 0xAA -// { -// index++; -// } -// if (index >= len - 3) // 不够空间容纳长度、校验码和结束码 -// { -// logError("BT 数据帧起始码错误"); -// logHexDumpAll(data, len); -// return 2; -// } - -// uint16_t datalength = data[index + 1]; // 读取数据长度 -// if (index + 2 + datalength + 1 >= len) // 检查数据长度是否合理 数据长度 + 校验码 + 结束码 -// { - -// logError("BT 数据帧长度错误"); -// logHexDumpAll(data, len); -// return 3; // -// } - -// uint8_t rx_sum = data[index + 2 + datalength]; // 读取校验码 -// uint8_t calculated_sum = XOR_CheckSum(&data[0], datalength + 2); // 计算校验码 -// if (rx_sum != calculated_sum) -// { - -// logError("BT 数据帧校验码错误 rx_sum = %02X, calculated_sum = %02X", rx_sum, calculated_sum); -// logHexDumpAll(data, len); -// return 4; -// } - -// if (data[index + 2 + datalength + 1] != 0x55) // 检查结束码 -// { -// logError("BT 数据帧结束码错误"); -// logHexDumpAll(data, len); -// return 5; -// } -// // 到这一步说明数据没问题,将接收到的数据通过中心任务发送出去 -// uint8_t *p_data; -// p_data = tmos_msg_allocate(length); -// if (p_data) -// { -// tmos_memcpy(p_data, data, length); -// tmos_msg_send(task_id, p_data); -// } - -// tmos_msg_send(task_id, data); -// return ret; -// } \ No newline at end of file diff --git a/bsp/src/bsp_uart.c b/bsp/src/bsp_uart.c index e92c972..b62f163 100644 --- a/bsp/src/bsp_uart.c +++ b/bsp/src/bsp_uart.c @@ -2,7 +2,7 @@ * @Author: mbw * @Date: 2024-12-06 16:52:30 * @LastEditors: mbw && 1600520629@qq.com - * @LastEditTime: 2024-12-11 16:35:52 + * @LastEditTime: 2024-12-12 11:12:18 * @FilePath: \ble_-tyq_-bjq_-ch584-m\bsp\src\bsp_uart.c * @Description: * @@ -268,7 +268,7 @@ uint16_t BT_ProcessEvent(uint8_t task_id, uint16_t events) } else if (events & BT_SEND_EVT) { - BSP_Uart1_Send_String("\r\n");//报警器端接收是以回车换行结尾,所以这里也加上换行 + // BSP_Uart1_Send_String("\r\n");//报警器端接收是以回车换行结尾,所以这里也加上换行 tmos_start_task(task_id, BT_SEND_EVT, 1000); return (events ^ BT_SEND_EVT); } diff --git a/common/letter-shell-master/src/shell_cfg.h b/common/letter-shell-master/src/shell_cfg.h index c1ddd76..845f8de 100644 --- a/common/letter-shell-master/src/shell_cfg.h +++ b/common/letter-shell-master/src/shell_cfg.h @@ -14,6 +14,8 @@ #include "bsp_tim.h" #include "stdlib.h" +#include "CONFIG.h" + #ifdef SHELL_CFG_USER #include SHELL_CFG_USER @@ -200,7 +202,7 @@ * @brief shell内存分配 * shell本身不需要此接口,若使用shell伴生对象,需要进行定义 */ -#define SHELL_MALLOC(size) malloc(size) +#define SHELL_MALLOC(size) tmos_msg_allocate(size) #endif /** SHELL_MALLOC */ #ifndef SHELL_FREE @@ -208,7 +210,7 @@ * @brief shell内存释放 * shell本身不需要此接口,若使用shell伴生对象,需要进行定义 */ -#define SHELL_FREE(obj) free(obj) +#define SHELL_FREE(obj) tmos_msg_deallocate(obj) #endif /** SHELL_FREE */ #ifndef SHELL_SHOW_INFO diff --git a/common/letter-shell-master/src/shell_companion.c b/common/letter-shell-master/src/shell_companion.c index eb494c7..4886da1 100644 --- a/common/letter-shell-master/src/shell_companion.c +++ b/common/letter-shell-master/src/shell_companion.c @@ -22,7 +22,7 @@ signed char shellCompanionAdd(Shell *shell, int id, void *object) { ShellCompanionObj *companions = shell->info.companions; - ShellCompanionObj *node = SHELL_MALLOC(sizeof(ShellCompanionObj)); + ShellCompanionObj *node = (ShellCompanionObj*)SHELL_MALLOC(sizeof(ShellCompanionObj)); SHELL_ASSERT(node, return -1); node->id = id; node->obj = object; @@ -54,7 +54,7 @@ signed char shellCompanionDel(Shell *shell, int id) { front->next = companions->next; } - SHELL_FREE(companions); + SHELL_FREE((uint8_t *)companions); return 0; } front = companions;