Boot reaction in Blech

35 views
Skip to first unread message

Matthias

unread,
Mar 7, 2020, 8:52:47 AM3/7/20
to blech-lang
Hi Blech Team,

consider the following Belch example

//@file control.blc
@[EntryPoint]
activity
Main (btn: bool)(led: bool)
  led
= true
  repeat
    await btn
    led
= false
    await btn
    led
= true
 
end
end

as well as the following C code that runs the synchronous code:

//@file main.c
#include blech/control.h

int main (…) {
  u8 led
= 0;
  blc_blech_control_init
();
 
while(1) {
   
if(sysTick) {
      blc_blech_control_tick
(
        btnIsPressed
(), &led);
      setLedState
(led);
   
}
 
}
 
return 0;
}

My question is related to the boot reaction in Blech which sets led = true before the repeat loop is entered and the first button press is awaited. When exactly is led = true executed in the C code? Is it done
  1. when executing blc_blech_control_init()
  2. or on the first execution of blc_blech_control_tick()?
If (1) is true, how does the Blech program pass the updated value of led back to the C env? The init function is called without parameters.

If (2) is true, this means that the boot reaction is postponed until the first event occurs. Since we cannot await the first event at that point (because the boot reaction has not been executed yet) we cannot perform a reaction with respect to that event occurence. So basically we will always ignore the first event. Is that intended?

Thanks in advance!

Regards,
Matthias

Gretz Friedrich (CR/AEE1)

unread,
Mar 8, 2020, 5:46:21 AM3/8/20
to blech-lang

Hello Matthias,

the quick answer is: your main loop is wrong.

 

If you take a look at the automatically generated main program, you’ll see that the tick function is called at step 0 and then the state is printed for state 0 (that is the initial moment).

This means you need to ensure that both, the init and the tick function, run when booting the system. The init function sets the program counters while the first call to tick will make sure that the program runs until the first await and makes all initialization on the way.

Only after that your reactive loop should start.

 

You could argue that we could have incorporated this in the init function instead of merely setting the pc’s. But at the moment it is like this.

 

Cheers

Friedrich

--
You received this message because you are subscribed to the Google Groups "blech-lang" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blech-lang+...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/blech-lang/0810b944-1e1e-4584-9844-f99ba04ed6c4%40googlegroups.com.

Matthias

unread,
Mar 8, 2020, 2:48:27 PM3/8/20
to blech-lang
Hello Friedrich,

okay, got it. So init() and tick() have to be called pair-wise before actually entering the event loop:

//@file main.c
#include blech/control.h

int main (…) {
  u8 led
= 0;

  blc_blech_control_init
(); // init of PCs
  blc_blech_control_tick
(<...>); // perform boot reaction
 
while(1) {

   
if(sysTick) {
      blc_blech_control_tick
(
        btnIsPressed
(), &led);
      setLedState
(led);
   
}
 
}
 
return 0;
}

This brings me to the next question:
Which state of the Blech inputs / outputs should I pass to tick() on its very first invocation before the event loop? Logically, the boot reaction itself is triggered before any event has been occurred in the system. To be more precise: Actually, there are no "events" – so to speak – but inputs that get processed on every tick. So shall these inputs already contain a valid value (a value that actually describes the current state of the input) when tick is called for the first time or is it just up to the programmer on how to deal with that? What would be the best approach from your point of view?

Thanks again!

Regards,
Matthias

To unsubscribe from this group and stop receiving emails from it, send an email to blech-lang+unsubscribe@googlegroups.com.

Gretz Friedrich (CR/AEE1)

unread,
Jul 1, 2020, 9:59:50 AM7/1/20
to blech-lang

Hey,

we’ve probably finished this discussion elsewhere in the meantime but for documentation sake I’ll supply the missing answer.

 

So basically the problem is that you have an event triggered reaction loop.

 

For main-loop and time triggered loops the matter is simpler: the while(1) will always perform the “boot reaction” upon starting the program in the same fashion as all the following reactions.

 

Now, here you have a main loop that triggers a reaction only in those iterations where a Boolean flag is set (i.e., an event is present). This means the very first iteration of the loop has no effect and does not trigger a “boot reaction”.

As you’ve suggested you need to “pull up” the very first call to the tick function and make it before the loop starts.

 

Regarding the arguments note that none of the await statements will be triggered in this very first reaction. That is because the program only runs up to the first await statements and pauses. So if your input only has an effect for await statements, then it does not matter how you set it in this very first reaction.

For example, instead of calling btnIsPressed(), you could simply set “false” to be the input.

 

If however the inputs are values that influence the control or data flow of your program (including the initialisation reaction), then you’d need to come up with default values that fit the initialisation logics your program.

 

The outputs are always locations, like &led, which exist (and hopefully are initialised in your C code) anyway and can be given as an argument also for the boot reaction.

Again it is up to you whether you want to propagate the updated output values to the environment. So if you want to show the effect that the LED is turned on during the boot process, you need to also add the call

 

setLedState(led);

 

right after this boot reaction. Otherwise the boot process is “hidden” from the environment.

 

Best regards

Friedrich

To unsubscribe from this group and stop receiving emails from it, send an email to blech-lang+...@googlegroups.com.

--

You received this message because you are subscribed to the Google Groups "blech-lang" group.

To unsubscribe from this group and stop receiving emails from it, send an email to blech-lang+...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/blech-lang/13bbe9d4-e73f-4259-bee1-2c6c395a0be4%40googlegroups.com.

Reply all
Reply to author
Forward
0 new messages