JavaScript running directly on a microcontroller with RISC-V architecture! That’s it! I am happy to introduce my port of Espruino to SiFive HiFive1 dev Board. Link of the GitHub source code and firmware binary in the conclusions of this article.

Espruino

Espruino is a JavaScript interpreter for microcontrollers. It was created by Gordon Williams in 2012, written in C and initially called by him TinyJS. Espruino goal is to make the learning and development of embedded systems easier using JavaScript. You can check the motivations of the creation of the Espruino in an article writen by the Gordon: https://makezine.com/2017/06/01/espruino-open-for-business/

HiFive1

HiFive1 is the first Arduino compatible development board with a RISC-V microcontroller. The Freedom E310  is the first mass-produced RISC-V chip too. The manufacturer is the SiFive, a pioneer in the industry that provides the core project archives in its Github: https://github.com/sifive/freedom . You can also learn more about the board here: https://www.sifive.com/boards/hifive1

 

Espruino on HiFive1

In last weeks I have been working on Espruino for HiFive1. Gordon did a great job in the scope division of the project, which greatly helped the development, special thanks to Gordon for the wonderful work.

The project is well divided into these blocks:

Development

I had to work first on the Toolchain, where I added the settings and rules for Makefiles to compile and build the binary. I have based on the examples from the SiFive SDK and the builds generated in PlatformIO for HiFive1. With a working Toolchain I can work in driver development and hardware abstraction.

In the drivers I worked on Serial UART connection, machine interruptions, access to GPIO: pin mux, configuration and levels reads.

In the Hardware abstraction are the calls that the JavaScript interpreter knows. Here I implemented some functions by binding the functionality from the drivers. I also added the timers functionality, which the interpreter uses for generating dates, intervals, time outs, and so on.

Usually in embedded systems the first peripheral we work on is the UART Serial. I would like to thanks to David Grubb who wrote a cool library for UART of the FE310, which I used as a base and added RX interrupt functions.

State of Art

At the moment we have an Espruino running on HiFive1 with the basic features, JavaScript runtime and GPIO access. We can for example use the onboard RGB LEDs to write an assorted blink:

var who = 0;

setInterval(() => {
  
  if (who == 0) {
    // Red led on
    LED1.reset();
    LED2.set();
    LED3.set();
  } else if (who == 1) {
    // Green led on
    LED2.reset();
    LED1.set();
    LED3.set();
  } else if (who == 2) {
    // Blue led on
    LED3.reset();
    LED1.set();
    LED2.set();
  }
  
  who++;
  
  if (who > 2)
    who = 0;
  
}, 500);

 

We can use the Espruino IDE Chrome extension to “upload” to the board:

 

Or program JavaScript directly over the serial connection:

JavaScript for microcontrollers in your HiFive1, so simple!

Conclusion

Once again I have to thanks here to the Gordon that did a great job writing the Espruino, the API and the interpreter really make the accesses and use of embedded systems much easier.

There is still a lot of work to be done in this port, write the driver to implement “save ()” to store program in the SPI flash is the next step, but it’s a start.

I hope that the community that is using FE310 in their projects, or start using them, will benefit from this work

Sources: https://github.com/microhobby/Espruino

Firmware: espruino_2v00.142_hifive.elf

Any questions, comments or suggestions you can contact me. Thanks to read!

Categories: RISC-VSiFive

Leave a Reply

Your email address will not be published. Required fields are marked *