ok, although hard to explain the problem with my poor English, I try it nevertheless:
1st, my threads are already running by safecall or by mutexes. Nevertheless, the BT messages get mixed up if I'm sending, e.g., 3 or 4 strings of length >40 shortly one after the other to the 1st, then to the 2nd, then to 3rd slave. In that case the program often hangs up.
So because you asked or maybe didn't understand so far when reading through my program source code, this is roughly my program design (but it's whole lot of stuff).
I have 1 master and 3 slaves (currently 2, the 3rd is waiting for progress).
for each slave the master has 1 outbox and 2 inboxes => 3x3=9 mailboxes over all.
each slave has 1 corresponding inbox and 2 corresponding outboxes (3 mailboxes on each slave).
Code: Select all
#define BT_CONN_1 1 // Slave 1
#define OUTBOX_1 1 // out string, event-based, on demand
#define INBOX_11 2 // sensors+values string (continuously)
#define INBOX_12 3 // values (ack, requested)
#define BT_CONN_2 2 // Slave 2
#define OUTBOX_2 4 // out string, event-based, on demand
#define INBOX_21 5 // sensors+values string (continuously)
#define INBOX_22 6 // values (ack, requested)
#define BT_CONN_3 3 // Slave 3
#define OUTBOX_3 7 // out string, event-based, on demand
#define INBOX_31 8 // sensors+values string (continuously)
#define INBOX_32 9 // values (ack, requested)
The master has to know each and every of all 4 sensor states of every single slave at any time (which may change very quickly),
and additionally 6-10 different calculated values of char, int, or long which are changing their values rather slowly from time to time (not as quick as the sensors).
so each slave uptdates his series of values of interest rather quickly in an endless loop and packs it to a out string (strlen ~56-58). He provides his out string by putting it into a mailbox.
this string must be received by the master continuously from every slave within ~ 50 ms each
(or even if possible <20-30 ms, that means 100-200 ms at most for 1 receive loop to all slaves, better if <100 ms should be possible).
This variable polling is running in a single task, lets call it the VariableReadingTask.
This VariableReadingTask uses inboxes _11, _21, _31 (for Receive Slave String)
Now from time to time there are events happening at the master (sensor events, function calculation events, BT message events, or user interface/interrupt events).
The master now has to send a special command or requests or even rather long arrays (<= 140 chars) to a specfic slave, or maybe 2, or sometimes even to all 3 of them one after the other,
then for a long while (several minutes) maybe there is no such a event and nothing has to be done at all. But sometimes there might also be very frequent commands to a specific slave (e.g., quickly stopping a motor prematurely when a remote sensor signalizes a critical value). There is no way to plan these events and/or to daisy-chain them into the VariableReadingTask, because in these cases it's unpredictable when the master needs to send a motor command or not, or if he needs a special remote value which is not continuously sent by the VariableReadingTask.
In other cases, several commands are queued-up, with waiting periods in between (waiting for buttun-pressed, waiting for caclculating result ready, waiting for motor encoder reached) and so it keeps waiting most of the time and does nothing in between. Chaining this into the VariableReadingTask would cause that during this waiting periods also no other variables would be received as periodically requested. Nevertheless, it can be prematurely interrupted by "stop task" from other tasks in urgent cases (behaviour- or event-based).
Anyway, all requests and commands that the master may have to send on demand (within ~50 ms) are centralized in a, lets call it, MasterEventTask.
This MasterEventTask uses outbox _1, _2, _3 (for send BT msg) and inboxes _12, _22, _32 (for receive ack number).
So these 2 BT tasks have to run independently, 1 continuously, and 1 on demand, and that's exactly what I'm doing currently in my program.