Page 2 of 2
Re: Serial driver on 2.5.x is not working properly
Posted: Tue Oct 01, 2013 8:13 pm
by goeck
OK, you're right. I thought that function waits for those sizeof(hdr) [actually 4] bytes to be available and then returns. That was what I understood when I read that this function is blocking. So I can't use this function for that purpose but must to do a approach with the following code?
Code: Select all
chnGetTimeout(&SD1, TIME_IMMEDIATE);
I was so happy to have a clean and crisp solution with only calling sdRead...

Well I see, as you say I get the number of read bytes returned so I could still use this crisp approach and retry until my desired count is reached...

Re: Serial driver on 2.5.x is not working properly
Posted: Tue Oct 01, 2013 8:20 pm
by Giovanni
You can read exactly 4 bytes by specifying TIME_INFINITE but you would not be sure those 4 bytes are the ones you were expecting, if some bytes are lost in the stream because communication errors the program would go out of synch with the transmitter.
You may try reading 4 bytes with a reasonable timeout, if a byte is lost then the function would timeout. You should design the state machine of your protocol considering all the possible outcomes THEN write the code.
Giovanni
Re: Serial driver on 2.5.x is not working properly
Posted: Tue Oct 01, 2013 8:26 pm
by goeck
The protocol is given by the BLE112 module, actually the BGAPI, which is not nicely done if one asks me. But.. I have to deal with it now.
It's done the following flavour. One gets a header with excactly 4 bytes which holds how many payload bytes are to come. I have to read those 4 bytes in any case and read the payload count then read the N bytes, no CRC, no endsign...
If an comm-error occurs the message is lost and I have to resend the command.
Re: Serial driver on 2.5.x is not working properly
Posted: Tue Oct 01, 2013 8:42 pm
by Giovanni
Is the packet started with a special symbol? if so you may synchronize on that.
If there are no special symbols then use timeouts and assume that the whole sequence takes at most N milliseconds, timing out would reset your state machine and you start waiting for an header again AFTER resetting the RX queue. This should be sufficient to resynchronize with the data flow.
Giovanni
Re: Serial driver on 2.5.x is not working properly
Posted: Wed Oct 02, 2013 10:17 am
by goeck
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.
Re: Serial driver on 2.5.x is not working properly
Posted: Wed Oct 09, 2013 10:15 am
by goeck
Hey everyone,
now I have found the point, I guess. The very first byte in buffer after UART init is a dummy byte having the content 0xFF. This breaks my message handler.
I have to read that befor the UART listener tzhread gets set up.
basically can't I just flush the buffers during the UART init sequence?
I see now high level command for this, donÄt want to do it platform dependent.
Here's the latest event listener code for reference.
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 struct ble_header hdr;
static uint32_t payload_length = 0, cnt_received = 0;
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_INPU)
if(flags & CHN_INPUT_AVAILABLE)
{
ledFlash(); //Indicate: something has been received
msg_t charbuf;
do{ //Read all there is into buffer
charbuf = chnGetTimeout(&SD1, TIME_IMMEDIATE);
if(charbuf != Q_TIMEOUT){
if(cnt_received < sizeof(hdr)){
if(cnt_received == 0)
hdr.type_hilen = (uint8_t)charbuf;
if(cnt_received == 1)
hdr.lolen = (uint8_t)charbuf;
if(cnt_received == 2)
hdr.cls= (uint8_t)charbuf;
if(cnt_received == 3)
hdr.command= (uint8_t)charbuf;
}
else
data[cnt_received - sizeof(hdr)] = (uint8_t)charbuf;
cnt_received++;
}
}
while(charbuf != Q_TIMEOUT);
if(cnt_received >= sizeof(hdr)){ //Do we have at least the header bytes?
const struct ble_msg *msg = ble_get_msg_hdr(hdr); //Process message header
if(!msg){ //Was message decoded successfully?
cnt_received = 0; //Reset receiver counter
gwinPrintf(GW1, "[BLE] unknown message\n");
gdisp_lld_display();
}
else{
payload_length = ((uint32_t)(hdr.type_hilen & 0x07))<<8 | (uint32_t)hdr.lolen;
if(cnt_received >= sizeof(hdr)+payload_length){ //Have we gathered all payload bytes?
cnt_received = 0; //Reset receiver counter
msg->handler(data);
}
}
}
}
}
}
I really dislike the way I fill the header, but can't find a more elegant solution which reliably works. I would like to use the receive counter as an index to stuff data into hdr... anyhow
Advices are appreciated.
Code: Select all
void initUART(){
sdStart(&SD1, NULL);
palSetPadMode(GPIOA, 9, PAL_MODE_ALTERNATE(1)); // USART1 TX.
palSetPadMode(GPIOA, 10, PAL_MODE_ALTERNATE(1)); // USART1 RX.
sdGet(&SD1); //Do the dummy-one-byte-read to kinda flush the rx buffer
}
Keep it up
Greetings
Re: Serial driver on 2.5.x is not working properly
Posted: Wed Oct 09, 2013 10:32 am
by Giovanni
Hi,
I remember a spurious byte being transmitted, this received 0xFF is new for me.
The TX dummy byte is a glitch caused by the USART in the transition from clock OFF to clock ON but I don't know if this will fix also your problem.
The workaround:
1) In board.h program RX as input with pull up, TX as output push pull with latched value 1.
2) In the main.c start the serial driver using sdStart().
3) AFTER starting the serial driver change RX and TX to alternate.
If this does not work then reset the receive queue after sdStart(), this would make your parser start from a clean state.
Giovanni
Re: Serial driver on 2.5.x is not working properly
Posted: Thu Oct 10, 2013 10:19 am
by goeck
Hey Giovanni,
thanks vor the quick infos. I just tried to do a queue reset but my handler won't recognize anything afterwards.
Is this the right way to do it?
I kind of found another solution, maybe. I firstly set the pad modes and afterwards started the Serial Driver. This gives me clear results. See here how I did it previously (UART2 driver part, commented out) and how I actually do it now.
Code: Select all
/**
* Activates the serial driver 1 using the driver default configuration.
* PA9 and PA10 are routed to USART1.
*/
void initUART(){
palSetPadMode(GPIOA, 9, PAL_MODE_ALTERNATE(1)); // USART1 TX.
palSetPadMode(GPIOA, 10, PAL_MODE_ALTERNATE(1)); // USART1 RX.
sdStart(&SD1, NULL);
// sdStart(&SD2, NULL);
// palSetPadMode(GPIOA, 2, PAL_MODE_ALTERNATE(1)); // USART2 TX.
// palSetPadMode(GPIOA, 3, PAL_MODE_ALTERNATE(1)); // USART2 RX.
}
Thanks for all the support!
I really appreciate it.
Cheers