Hey Giovanni,
now I had some minutes to rework that UART listener, my experience is pretty fine now most of the time.
I get all messages processed that come in.
Code: Select all
// UART event listener
static WORKING_AREA(waThread_4, 1024);
static msg_t Thread4(void *arg)
{
(void)arg;
chRegSetThreadName("BLEData");
static unsigned char data[256];
static uint8_t rxBuffer[256];
static uint8_t* pRxBuffer = rxBuffer;
static struct ble_header hdr;
EventListener elBLEData;
flagsmask_t flags;
chEvtRegisterMask((EventSource *)chnGetEventSource(&SD1), &elBLEData, EVENT_MASK(1));
while (TRUE)
{
chEvtWaitOneTimeout(EVENT_MASK(1), MS2ST(10));
chSysLock();
flags = chEvtGetAndClearFlags(&elBLEData);
chSysUnlock();
if(flags & CHN_INPUT_AVAILABLE)
{
ledFlash(); //show: something has been received
msg_t charbuf;
do{ //read all there is into buffer
charbuf = chnGetTimeout(&SD1, TIME_IMMEDIATE);
if ( charbuf != Q_TIMEOUT )
*pRxBuffer++ = (uint8_t)charbuf;
}
while(charbuf != Q_TIMEOUT);
if(pRxBuffer-rxBuffer >= sizeof(hdr)){ //Do I have at least the header?
memmove(&hdr, rxBuffer, sizeof(hdr));
if(hdr.lolen){ //payload bytes to come?
if(pRxBuffer-rxBuffer >= sizeof(hdr)+hdr.lolen){
memmove(data, &rxBuffer[4], hdr.lolen);
pRxBuffer = rxBuffer; //reset moving pointer
struct ble_msg *msg = ble_get_msg_hdr(hdr); //process message
msg->handler(data);
}
}
else{
pRxBuffer = rxBuffer; //reset moving pointer
struct ble_msg *msg = ble_get_msg_hdr(hdr); //process message
msg->handler(data);
}
}
}
}
}
One thing that bothers me though is the thing I read in the forum when sending stuff, one should do a one byte read?
Code: Select all
/**
* @brief Sends a BLE message.
* @param[in] len1 length [bytes] of header to send
* @param[in] data1 pointer to header array
* @param[in] len2 length [bytes] of data send
* @param[in] data2 pointer to data array
*/
void uartBLESendMessage(uint8 len1,uint8* data1,uint16 len2,uint8* data2){
chSequentialStreamPut(&SD1, len1+len2);
chSequentialStreamWrite(&SD1, data1, len1);
if(len2)
chSequentialStreamWrite(&SD1, data2, len2);
sdGet(&SD1);
}
The thing is, I see in an init sequence only the first command goes out to the BLE112 (out of three), but the answer message gets received and processed nicely.
If I leave that sdGet() out in the sending routine the whole processing gets slowed done by the factor of 10 (at least to my eyes) but everything gets sent out and processed.