Espruino interrupts

Posted on
  • Hello! I haven't programmed in JavaScript for at least a dozen years and I'm sure it has changed a bit over the years. I am also fairly new to the Arduiono/electronics/microcontroller/IOT­/maker scene, but I am learning and enjoying it! I am an Espruino Pico backer and am really looking forward to playing with it and re-learning JS.

    I noticed somewhere that Espruino JS doesn't support hardware interrupts. Is this true? If so, what would be the explanation? It seems like that could be a severe limitation. Please understand that I'm not at all trying to be negative against Espruino, I am just wondering what the reasons and ramifications are.

    Thanks so much!

  • @Manxome in a similar way you can attach an Interrupt to a pin on, say, an Arduino, you can set a watch on a pin in Espruino. When the pin changes (rises, falls, or both) it will call your function:­bal_setWatch

  • @Manxome, it does... and very cool, jus as @thomc explains. Did JS a while a go for a lot... in 97, but got laughed at a lot by the .jsp 'fans' - even by my own kin - ... guess what: it's back bit time, for a while now and is THE client platform (with SimglePageApps instead of .jsp Web pages etc.). The mid 80' through mid 90' regarding client-server enjoying renaissance riding the easy to use Web http(s) protocol wave.

    Espruino uses interrupts for the setTimeot() and setIntervall(),

    Espruino is THE thing for exactly just one (1) battery for a life time due to the interrupt drivenness of the whole infrastructure.

    Put setBusyIndicator(LED1) and *setSleepIndicator(LED2) into your code and then you see how Espruino goes from activity to no activity to absolute silent Zzzzzzzzzz... until the next event wakes it up... totally different to the do loop in arduino's main.

    Arduino uses its speed to make you believe it is 'on interrupt'... and burns just (m)Amps... doing nothing but checking if it should do something or doing what you unconditionally tell to do. Yes, you can used interrupts too. Espruino does use interrupts for all it does. Espruino even provides commands to go to sleep and deep sleep... almost deep freeze... and comes back pretty quickly without frost bites... ;-)

    Your app knows when to enter what state and can use these commands. See examples used in reference.

    What programming languages did you use in the past?

  • OK, I found it in the FAQ page:


    Yes! We have added peek32 and poke32 instructions which allow you to directly access anything >in the ARM's address space.

    You won't be able to use interrupts, but other hardware (including DMA) can be set up and used this way.

    Thanks for the replies @thomc and @AllObjects, but I'm afraid I don't understand enough yet to reconcile your comments with the FAQ. Pretty sure I read that the Arduino can sleep and wake up on an interrupt and I was hoping to have that functionality on my Espruino Picos. From what you are saying, I am guessing it can, but I'm wondering what the limitations are.

    @AllObjects, I used to write a lot of ASP web pages with JavaScript back in the late 90s. Also wrote a lot of C/C++/C#, as well as a bunch of APL a long time ago :)

  • I'm sure you might miss some of APL's terseness in todays again verbose, COBOL like languages, such as Java. I miss some of Smalltalk's concise and consistent application of oo thinking combined with minimal syntax noise. From the constructs I like JavaScript a lot - just miss classes being 1st class objects... (which practically no other commonly used languages support either).

  • Allow me to clear some things up with some examples.
    Espruino's version of interrupts are watches, watches can be set to happen when a pin rises (from low to high), falls (High to low) or both. Watches are set like this:

    function toggleLED(){
    digitalWrite(LED1, !digitalRead(LED1));
    setWatch(toggleLED, BTN1, {repeat:true,edge:"rising", debounce:1/ms/});

    This watch executes the ToggleLED function every time button 1 is pressed, this watch also induces a debounce for the button.
    Because of Espruino's event driven nature the chip automatically sleeps while nothing is happening to reduce power usage, in Arduino you have to tell the chip to sleep in your program.
    Watches can be removed with the function clearWatch(), this function without any arguments deletes all watches currently set. However if when you create your watch you assign it a variable, e.g. Watch1 = setWatch(... , you can pass clearWatch that variable and only remove specific watches.

    Some more advanced information:
    High is at least 1.546v
    Low is below 1.166v

  • Thanks @Ducky! It sounds like Espruino is handling interrupts internally, but I am still confused by the FAQ:

    You won't be able to use interrupts,...

  • I think the intent of that is to indicate that you can't manipulate hardware interrupt setup via peek/poke, based on it's location in the FAQ? I think that might be in reference to interrupts generated by the on-chip peripherals (i think digitalPulse uses one of the timers to tigger an interrupt), which aren't exposed via setWatch()

    Re sleep: You can also use setDeepSleep() to get power usage down even further.

  • Thanks @DrAzzy! That makes sense :)

  • ...may be you could... and if poking does not get you there, poked assembler/machine code get's you there... but you should not... otherwise why Espruino/JavaScript in the first place (with which you then would mess). I like Espruino's implementation very much: it translates / maps the internal/low level things into the JavaScript application language layer. It even has the support for managing bursts of interrupts: they are queued in order to not get lost... and it convey as much 'realtime' feeling as possible: with the interrupt, additional context information - most importantly the time of the interrupt *including the time of the previous one - is provided and makes it available to the application. When the JavaScript engine finally catches up - the (next) interrupt is picked up from the queue and handled on the JavaScript level. So you could say: hardware events are mapped to software event, like you are used to Web-program on a button onclick="someJavaScript...;". Clicking with the mouse is a very hardware event, but it is finally handled in the application layer through a software event.

  • Hi,

    Sorry I'm a bit late replying here - was away all weekend. I think it's all been explained, but hopefully this is more of a summary:

    Espruino uses interrupts internally, for quite a lot of things - setWatch, digitalPulse, serial, timers, waveforms, and so on. However that's only native code - JavaScript itself doesn't execute in an interrupt. If an interrupt needs JavaScript to be run then it puts a message in a queue (often with an accurate timestamp), and the JavaScript is run when the interpreter is next ready to do it.

    If Espruino is idle it'll basically have exactly the same effect as if you had an interrupt - but if it's busy then you'll have to wait a small time until the code is executed.

    The decision not to allow JS in interrupts is actually for a few reasons:

    • it makes the interpreter smaller and faster
    • it makes it much easier to be sure that there aren't any bugs in the interpreter itself
    • it's really easy to shoot yourself in the foot with interrupts - even in JS :)
    • The JavaScript code isn't executed anywhere near as quickly as C - so if you're trying to do something super-fast you probably need to find another way to do it anyway.

    Having said all that, you can use peek and poke to do basically anything - including setting up your own interrupts (which would have to run native code, not JavaScript). It's just not easy. I am planning on adding something that'll let you execute native code (created via the inline assembler) on interrupts though, so that should improve the situation.

    In the mean time, it's actually pretty easy to compile Espruino yourself, and you can then add your own bits of C code (which can run on interrupts).

  • Thanks @Gordon and everyone! What an awesome, helpful, responsive and informative forum! My questions have been thoroughly answered :)

  • Great overview! Thanks, @Gordon. #interrupts

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview

Espruino interrupts

Posted by Avatar for Manxome @Manxome