Skip to content

Commit

Permalink
Dev (#20)
Browse files Browse the repository at this point in the history
* Add retry

* missing UART_RETRY_MAX

* move device name setting

* Add MS8607 enviromental sensor

* Add MS8607 sensor

* Add MS8607 sensor

* Isn’t that obvious.

* Refactor BLE intern FIFO handling

* Fixed nRF52 linker script RAM length

* nRF52 xxAB variant 256KB FLASH, 32KB RAM
  • Loading branch information
hnhoan authored Jun 7, 2017
1 parent 08141da commit fddad7e
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 36 deletions.
21 changes: 21 additions & 0 deletions ARM/Nordic/include/ble_intrf.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -63,6 +83,7 @@ typedef struct __BleDeviceInterf {
HCFIFO hRxFifo;
HCFIFO hTxFifo;
} BLEINTRF;
#pragma pack(pop)

#ifdef __cplusplus

Expand Down
11 changes: 10 additions & 1 deletion ARM/Nordic/include/ble_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;

/*
Expand Down
10 changes: 5 additions & 5 deletions ARM/Nordic/nRF52/src/gcc_nrf52_xxaa.ld
Original file line number Diff line number Diff line change
Expand Up @@ -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
}


Expand Down
18 changes: 18 additions & 0 deletions ARM/Nordic/nRF52/src/gcc_nrf52_xxab.ld
Original file line number Diff line number Diff line change
@@ -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"
87 changes: 58 additions & 29 deletions ARM/Nordic/src/ble_intrf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -213,28 +212,30 @@ 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;
}

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
Expand Down Expand Up @@ -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)
{
Expand Down
22 changes: 21 additions & 1 deletion ARM/Nordic/src/ble_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit fddad7e

Please sign in to comment.