diff --git a/ARM/Nordic/include/ble_intrf.h b/ARM/Nordic/include/ble_intrf.h index ca7e1159..359d538a 100755 --- a/ARM/Nordic/include/ble_intrf.h +++ b/ARM/Nordic/include/ble_intrf.h @@ -41,6 +41,26 @@ Modified by Date Description #include "device_intrf.h" #include "cfifo.h" +/** + * Calculate require mem + */ +#define BLEINTRF_CFIFO_TOTAL_MEMSIZE(NbBlk, BlkSize) CFIFO_TOTAL_MEMSIZE(NbBlk, BlkSize + 2) + +/** + * This structure define the CFIFO data packet + * It is a variable length structure defined by user + * + * NOTE : Data element can be more than 1 byte. DO NOT use sizeof() to calculate + * the size of this structure. It should be mapped to appropriate buffer memory + */ +#pragma pack(push, 1) +typedef struct __BleDeviceInterfPacket { + uint16_t Len; // Valid data length + uint8_t Data[1];// Data container array +} BLEINTRF_PKT; +#pragma pack(pop) + +#pragma pack(push, 4) typedef struct __BleDeviceInterfConfig { BLESRVC *pBleSrv; // BLE Service int RxCharIdx; // Write characteristic index (From BLE) @@ -63,6 +83,7 @@ typedef struct __BleDeviceInterf { HCFIFO hRxFifo; HCFIFO hTxFifo; } BLEINTRF; +#pragma pack(pop) #ifdef __cplusplus diff --git a/ARM/Nordic/include/ble_service.h b/ARM/Nordic/include/ble_service.h index bfb8e2b7..f090ce79 100755 --- a/ARM/Nordic/include/ble_service.h +++ b/ARM/Nordic/include/ble_service.h @@ -69,6 +69,14 @@ typedef void (*BLESRVC_WRCB) (BLESRVC *pBlueIOSvc, uint8_t *pData, int Offset, i */ typedef void (*BLESRVC_SETNOTCB)(BLESRVC *pBlueIOSvc, bool bEnable); +/** + * Callback when transmission is completed + * + * @param pBlueIOSvc + * @param CharIdx + */ +typedef void (*BLESRVC_TXCOMPLETE)(BLESRVC *pBlueIOSvc, int CharIdx); + // Service connection security types typedef enum { BLESRVC_SECTYPE_NONE, // open, no security @@ -86,12 +94,13 @@ typedef struct __BLE_Service_Char_Data { int MaxDataLen; // char max data length uint32_t Property; // char properties define by BLUEIOSVC_CHAR_PROP_... const char *pDesc; // char UTF-8 description string - BLESRVC_WRCB WrCB; // Callback for write char, set to NULL for read char + BLESRVC_WRCB WrCB; // Callback for write char, set to NULL for read char bool bNotify; // Notify flag for read characteristic BLESRVC_SETNOTCB SetNotifCB; // Callback on set notification uint8_t *pDefValue; // pointer to char default values uint16_t ValueLen; // Default value length in bytes ble_gatts_char_handles_t Hdl; // char handle + BLESRVC_TXCOMPLETE TxCompleteCB; // Callback when TX is completed } BLESRVC_CHAR; /* diff --git a/ARM/Nordic/nRF52/src/gcc_nrf52_xxaa.ld b/ARM/Nordic/nRF52/src/gcc_nrf52_xxaa.ld index c05f4a17..93dfaf6b 100755 --- a/ARM/Nordic/nRF52/src/gcc_nrf52_xxaa.ld +++ b/ARM/Nordic/nRF52/src/gcc_nrf52_xxaa.ld @@ -7,11 +7,11 @@ SEARCH_DIR("../../../../EHAL/ARM/src") SEARCH_DIR("../../../../../EHAL/ARM/src") SEARCH_DIR("../../../../../../EHAL/ARM/src") GROUP(-lgcc -lc -lnosys -lCMSIS) - -MEMORY -{ - FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 0x80000 - RAM (rwx) : ORIGIN = 0x20000000, LENGTH = 0x8000 + +MEMORY +{ + FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 0x80000 + RAM (rwx) : ORIGIN = 0x20000000, LENGTH = 0x10000 } diff --git a/ARM/Nordic/nRF52/src/gcc_nrf52_xxab.ld b/ARM/Nordic/nRF52/src/gcc_nrf52_xxab.ld new file mode 100755 index 00000000..040cf679 --- /dev/null +++ b/ARM/Nordic/nRF52/src/gcc_nrf52_xxab.ld @@ -0,0 +1,18 @@ +OUTPUT_FORMAT ("elf32-littlearm", "elf32-bigarm", "elf32-littlearm") +SEARCH_DIR(.) +SEARCH_DIR("../../../../src") +SEARCH_DIR("../../../../../src") +SEARCH_DIR("../../../../../../src") +SEARCH_DIR("../../../../EHAL/ARM/src") +SEARCH_DIR("../../../../../EHAL/ARM/src") +SEARCH_DIR("../../../../../../EHAL/ARM/src") +GROUP(-lgcc -lc -lnosys -lCMSIS) + +MEMORY +{ + FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 0x40000 + RAM (rwx) : ORIGIN = 0x20000000, LENGTH = 0x8000 +} + + +INCLUDE "gcc_arm_flash.ld" diff --git a/ARM/Nordic/src/ble_intrf.cpp b/ARM/Nordic/src/ble_intrf.cpp index 84428821..541143a2 100755 --- a/ARM/Nordic/src/ble_intrf.cpp +++ b/ARM/Nordic/src/ble_intrf.cpp @@ -40,7 +40,7 @@ Modified by Date Description #include "ble_app.h" #define NRFBLEINTRF_PACKET_SIZE NRF_BLE_MAX_MTU_SIZE -#define NRFBLEINTRF_CFIFO_SIZE CFIFO_TOTAL_MEMSIZE(NRFBLEINTRF_PACKET_SIZE << 1, 1) +#define NRFBLEINTRF_CFIFO_SIZE CFIFO_TOTAL_MEMSIZE(2, NRFBLEINTRF_PACKET_SIZE + sizeof(BLEINTRF_PKT) - 1) static uint8_t s_nRFBleRxFifoMem[NRFBLEINTRF_CFIFO_SIZE]; static uint8_t s_nRFBleTxFifoMem[NRFBLEINTRF_CFIFO_SIZE]; @@ -132,32 +132,31 @@ bool BleIntrfStartRx(DEVINTRF *pDevIntrf, int DevAddr) } /** - * @brief - RxData + * @brief - RxData : retrieve 1 packet of received data * Receive data into pBuff passed in parameter. Assuming StartRx was - * called prior calling this function to get the actual data + * called prior calling this function to get the actual data. BufferLen + * to receive data must be at least 1 packet in size. Otherwise remaining + * bytes are dropped. * * @param * pDevIntrf : Pointer to an instance of the Device Interface * pBuff : Pointer to memory area to receive data. - * BuffLen : Length of buffer memory in bytes + * BuffLen : Length of buffer memory in bytes. Must be at least 1 packet + * in size. Otherwise remaining bytes are dropped. * * @return Number of bytes read */ int BleIntrfRxData(DEVINTRF *pDevIntrf, uint8_t *pBuff, int BuffLen) { BLEINTRF *intrf = (BLEINTRF*)pDevIntrf->pDevData; + BLEINTRF_PKT *pkt; int cnt = 0; - while (BuffLen > 0) + pkt = (BLEINTRF_PKT *)CFifoGet(intrf->hRxFifo); + if (pkt != NULL) { - int l = BuffLen; - uint8_t *p = CFifoGetMultiple(intrf->hRxFifo, &l); - if (p == NULL) - break; - memcpy(pBuff, p, l); - BuffLen -= l; - pBuff += l; - cnt += l; + cnt = min(BuffLen, pkt->Len); + memcpy(pBuff, pkt->Data, cnt); } return cnt; @@ -213,15 +212,18 @@ bool BleIntrfStartTx(DEVINTRF *pDevIntrf, int DevAddr) int BleIntrfTxData(DEVINTRF *pDevIntrf, uint8_t *pData, int DataLen) { BLEINTRF *intrf = (BLEINTRF*)pDevIntrf->pDevData; + BLEINTRF_PKT *pkt; + int maxlen = intrf->hTxFifo->BlkSize - sizeof(pkt->Len); int cnt = 0; while (DataLen > 0) { - int l = DataLen; - uint8_t *p = CFifoPutMultiple(intrf->hTxFifo, &l); - if (p == NULL) + pkt = (BLEINTRF_PKT *)CFifoPut(intrf->hTxFifo); + if (pkt == NULL) break; - memcpy(p, pData, l); + int l = min(DataLen, maxlen); + memcpy(pkt->Data, pData, l); + pkt->Len = l; DataLen -= l; pData += l; cnt += l; @@ -229,12 +231,11 @@ int BleIntrfTxData(DEVINTRF *pDevIntrf, uint8_t *pData, int DataLen) if (s_bBleIntrfTxReady == true) { - s_bBleIntrfTxReady = false; - int l = NRFBLEINTRF_PACKET_SIZE; - uint8_t *p = CFifoGetMultiple(intrf->hTxFifo, &l); - if (p != NULL) + pkt = (BLEINTRF_PKT *)CFifoGet(intrf->hTxFifo); + if (pkt != NULL) { - uint32_t res = BleSrvcCharNotify(intrf->pBleSrv, intrf->TxCharIdx, p, l); + s_bBleIntrfTxReady = false; + uint32_t res = BleSrvcCharNotify(intrf->pBleSrv, intrf->TxCharIdx, pkt->Data, pkt->Len); #if (NRF_SD_BLE_API_VERSION <= 3) if (res != BLE_ERROR_NO_TX_PACKETS) #else @@ -280,20 +281,48 @@ void BleIntrfReset(DEVINTRF *pDevIntrf) } +/** + * + * + */ +void BleIntrfTxComplete(BLESRVC *pBleSvc, int CharIdx) +{ + BLEINTRF *intrf = (BLEINTRF*)pBleSvc->pContext; + BLEINTRF_PKT *pkt; + + s_bBleIntrfTxReady = true; + pkt = (BLEINTRF_PKT *)CFifoGet(intrf->hTxFifo); + if (pkt != NULL) + { + s_bBleIntrfTxReady = false; + uint32_t res = BleSrvcCharNotify(intrf->pBleSrv, intrf->TxCharIdx, pkt->Data, pkt->Len); +#if (NRF_SD_BLE_API_VERSION <= 3) + if (res != BLE_ERROR_NO_TX_PACKETS) +#else + if (res != NRF_ERROR_RESOURCES) +#endif + { + s_bBleIntrfTxReady = true; + } + } +} + void BleIntrfRxWrCB(BLESRVC *pBleSvc, uint8_t *pData, int Offset, int Len) { BLEINTRF *intrf = (BLEINTRF*)pBleSvc->pContext; + BLEINTRF_PKT *pkt; + int maxlen = intrf->hTxFifo->BlkSize - sizeof(pkt->Len); - do { - int l = Len; - uint8_t *p = CFifoPutMultiple(intrf->hRxFifo, &l); - if (p == NULL) + while (Len > 0) { + pkt = (BLEINTRF_PKT *)CFifoPut(intrf->hRxFifo); + if (pkt == NULL) break; - //int l = min(intrf->PacketSize, Len); - memcpy(p, pData, l); + int l = min(intrf->PacketSize, Len); + memcpy(pkt->Data, pData, l); + pkt->Len = l; Len -= l; pData += l; - } while (Len > 0); + } if (CFifoUsed(intrf->hRxFifo) > 0 && intrf->DevIntrf.EvtCB != NULL) { diff --git a/ARM/Nordic/src/ble_service.cpp b/ARM/Nordic/src/ble_service.cpp index 755b7ab9..f8f1fc2c 100755 --- a/ARM/Nordic/src/ble_service.cpp +++ b/ARM/Nordic/src/ble_service.cpp @@ -102,7 +102,8 @@ void BleSrvcEvtHandler(BLESRVC *pSrvc, ble_evt_t *pBleEvt) for (int i = 0; i < pSrvc->NbChar; i++) { - if (p_evt_write->handle == 0) + if (p_evt_write->op == BLE_GATTS_OP_EXEC_WRITE_REQ_NOW) + //if (p_evt_write->handle == 0) { printf("Long Write\r\n"); GATLWRHDR *hdr = (GATLWRHDR *)pSrvc->pLongWrBuff; @@ -162,6 +163,25 @@ void BleSrvcEvtHandler(BLESRVC *pSrvc, ble_evt_t *pBleEvt) } break; +#if (NRF_SD_BLE_API_VERSION > 3) + case BLE_GATTS_EVT_HVN_TX_COMPLETE: + if (pSrvc->ConnHdl == pBleEvt->evt.gatts_evt.conn_handle) + { + for (int i = 0; i < pSrvc->NbChar; i++) + { + if (pSrvc->pCharArray[i].TxCompleteCB) + { + pSrvc->pCharArray[i].TxCompleteCB(pSrvc, i); + } + } + } + break; +#else + case BLE_EVT_TX_COMPLETE: + break; + +#endif + default: break; }