Strings to mailboxes

Discussions and support about ChibiOS/RT, the free embedded RTOS.
steved
Posts: 693
Joined: Fri Nov 09, 2012 2:22 pm
Has thanked: 9 times
Been thanked: 99 times

Re: Strings to mailboxes

Postby steved » Wed May 13, 2020 2:50 pm

My thinking was that the printf() logic is going to take exactly the same time to generate a character stream regardless of where it goes. It's the time to save that stream somewhere that varies with method (and even with your 2-thread solution you can reach a point where there is no more temporary output buffer space). I simply sized my serial port output buffer such that writing should never block. (But I did find in stress testing that I had far too much to say, and writing to the serial port was slowing things down. Solution: be more to the point in messages).

I've initialised mailbox queues from all over the place; usually I do it as part of setting up one of the tasks that uses it (which is often off the main thread), but as long as you set things up in the right order and don't try to use an uninitialised mailbox (and use permanently allocated memory for buffer and scratchpad, of course), I don't see why you would have a problem.

JSStabl
Posts: 53
Joined: Tue Feb 25, 2020 4:06 pm
Been thanked: 1 time

Re: Strings to mailboxes

Postby JSStabl » Wed May 13, 2020 3:31 pm

Isnt the chprintf function blocking? If not that could be a solution as well. Need to take care of syslock like you did though.

Hmmm, weird. If I do this it works:

Code: Select all

THD_WORKING_AREA(serial_output_thread_wa, 256);
THD_FUNCTION(serial_output_thread, arg) {
    (void)arg;
    chRegSetThreadName("Uart Comm Thread");
    msg_t msg = 0;
    chMBObjectInit(&serial_mb, serial_mb_buffer, 10);
    BaseSequentialStream *stream = (BaseSequentialStream *)&SD3;
    while (true) {
        chMBFetchTimeout(&serial_mb, &msg, TIME_INFINITE);
        const char *s_msg = (const char *)msg;
        chprintf(stream, "%010d: %s \r\n",chVTGetSystemTime(), s_msg);
    }

}


If I move the Object init to my main function (after ch init) it stops working for some reason.

steved
Posts: 693
Joined: Fri Nov 09, 2012 2:22 pm
Has thanked: 9 times
Been thanked: 99 times

Re: Strings to mailboxes

Postby steved » Wed May 13, 2020 3:42 pm

chprintf() isn't of itself blocking; it just passes characters on to a character stream, which is what determines blocking behaviour. If you use Chibi's SERIAL driver (think I said 'UART' earlier, which might have confused things), that has a circular output buffer of configurable size. So as long as the buffer doesn't get full, chprintf() won't block.

JSStabl
Posts: 53
Joined: Tue Feb 25, 2020 4:06 pm
Been thanked: 1 time

Re: Strings to mailboxes

Postby JSStabl » Wed May 13, 2020 3:48 pm

Neat, thanks!

I see to have a general issue with using mailboxes defined in other c files though.

When I define:

Code: Select all

static mailbox_t state_mb;
static msg_t state_mb_buffer[10];


In my header file and then initialize the Mailbox from the same c file, I can post messages to it from within that same c file. However when I include the .h file in another c file and try to post a message there I receive a timeout. Not sure whats happening there....

steved
Posts: 693
Joined: Fri Nov 09, 2012 2:22 pm
Has thanked: 9 times
Been thanked: 99 times

Re: Strings to mailboxes

Postby steved » Wed May 13, 2020 4:00 pm

Its the 'static' qualifier - means the variable is local to the source file in which it appears.

So you will have a copy of the mailbox structure in each source file.

I tend to create my own API which hides the Chibi-specific bits.

JSStabl
Posts: 53
Joined: Tue Feb 25, 2020 4:06 pm
Been thanked: 1 time

Re: Strings to mailboxes

Postby JSStabl » Thu May 14, 2020 8:40 am

Yeah, that was it, thanks :)


Return to “ChibiOS/RT”

Who is online

Users browsing this forum: FXCoder and 1 guest