Is there a good way to pipe two serial devices?

Discussions and support about ChibiOS/RT, the free embedded RTOS.
carldong
Posts: 62
Joined: Wed Oct 15, 2014 6:08 pm
Has thanked: 1 time
Been thanked: 1 time

Is there a good way to pipe two serial devices?

Postby carldong » Mon Feb 20, 2017 8:13 am

I am looking for a good way to "link" two serial devices. Say, SDU1 and SD1, so that a board acts like a USB-TTL translator. I am stuck looking for a way to implement this elegantly. Currently I can only think about polling byte by byte and transfer them in an infinite loop, but that doesn't feel very elegant...

Searching the API, I didn't find an existing way to create data pipes between devices(maybe I missed something). MemoryStream looks something like what I want, but I am not quite sure(I will be testing it). It seems that streams can only be read into memory buffers, but not another stream.

What are some good ways to implement this?

User avatar
Giovanni
Site Admin
Posts: 14455
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: Is there a good way to pipe two serial devices?

Postby Giovanni » Mon Feb 20, 2017 8:56 am

Hi,

Both SDs and SDUs implement their own buffering mechanism, you could have a single loop waiting for the RX/TX events from the ports and doing transfers. The loop would sleep inside chEvtWaitxxx() when there is no data to transfer.

Giovanni

carldong
Posts: 62
Joined: Wed Oct 15, 2014 6:08 pm
Has thanked: 1 time
Been thanked: 1 time

Re: Is there a good way to pipe two serial devices?

Postby carldong » Mon Feb 20, 2017 6:43 pm

Do you mean that I need two threads? I have not used Events before, but there doesn't seem like RX/TX are sending any of these. Does that mean I need to add events into the LLD?

And which section of the document should I look at?

carldong
Posts: 62
Joined: Wed Oct 15, 2014 6:08 pm
Has thanked: 1 time
Been thanked: 1 time

Re: Is there a good way to pipe two serial devices?

Postby carldong » Mon Feb 20, 2017 8:14 pm

After digging some docs, I found that it seems to require 2 threads for the event method. I will try this first, but is it possible to do the same in one thread, while not hard blocking others?

User avatar
Giovanni
Site Admin
Posts: 14455
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: Is there a good way to pipe two serial devices?

Postby Giovanni » Mon Feb 20, 2017 9:57 pm

All time spent waiting for events will be available to other threads. I think you can use a single thread waiting for four events (RX and TX for both sides).

Giovanni

carldong
Posts: 62
Joined: Wed Oct 15, 2014 6:08 pm
Has thanked: 1 time
Been thanked: 1 time

Re: Is there a good way to pipe two serial devices?

Postby carldong » Mon Feb 20, 2017 11:09 pm

Can I wait for multiple events at the same time? I don't quite understand what those eventid_t parameters are assigned. Do I assign an arbitrary number to the ID, and use something like chEvtWaitAny? Does this mean I can have at most 32 events registered on a specific source?

Currently I am using a dumb solution:

Code: Select all

static uint8_t pipeByte(BaseChannel *chp1, BaseChannel *chp2) {
  int8_t b;
  b = chnGetTimeout(chp1, TIME_IMMEDIATE);
  if (b != STM_RESET && b != STM_TIMEOUT) {
    chnPutTimeout(chp2, b, TIME_IMMEDIATE);
  }
  /* ETX.*/
  if (b == 0x03) {
    return 1;
  }
  return 0;
}


Of course, it blocks every other threads. For my application it is fine, but I don't like it...

User avatar
Giovanni
Site Admin
Posts: 14455
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: Is there a good way to pipe two serial devices?

Postby Giovanni » Mon Feb 20, 2017 11:11 pm

Correct, you have to use chEvtWaitAny(), you can register on up to 32 distinct sources and wait for any of those to be signaled.

Events are meant to be used when you need to wait for multiple sources.

Giovanni

carldong
Posts: 62
Joined: Wed Oct 15, 2014 6:08 pm
Has thanked: 1 time
Been thanked: 1 time

Re: Is there a good way to pipe two serial devices?

Postby carldong » Mon Feb 20, 2017 11:33 pm

Ah, I see. So it looks like I don't need to spawn new threads. I will try that, too.

carldong
Posts: 62
Joined: Wed Oct 15, 2014 6:08 pm
Has thanked: 1 time
Been thanked: 1 time

Re: Is there a good way to pipe two serial devices?

Postby carldong » Fri Mar 03, 2017 12:07 am

Actually, I found the document always says something like this:

mask -- the event flags set to be ORed.

Do you mean ANDed? OR doesn't make sense to me, since ORing 0 is not modifying the flag set at all.

User avatar
Giovanni
Site Admin
Posts: 14455
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: Is there a good way to pipe two serial devices?

Postby Giovanni » Fri Mar 03, 2017 8:48 am

It is correct, generating an event means setting a bit in the mask to 1.

Giovanni


Return to “ChibiOS/RT”

Who is online

Users browsing this forum: No registered users and 11 guests