Stream: show and tell

Topic: Roc on a microcontroller


view this post on Zulip Brendan Hansknecht (Feb 12 2022 at 02:49):

So not that it is doing more than calling the fib function, but I got Roc running on a microcontroller. Specifically a microbitv2 running a cortex-m4.

18:47:49.263 Acording to Roc, the fib of 39 is...
18:47:49.263 63245986
18:47:49.263 Acording to Roc, the fib of 40 is...
18:47:49.263 102334155
18:47:49.263 Acording to Roc, the fib of 41 is...
18:47:49.263 165580141
18:47:49.294 Acording to Roc, the fib of 42 is...
18:47:49.294 267914296
18:47:49.294 Acording to Roc, the fib of 43 is...
18:47:49.294 433494437
18:47:49.294 Acording to Roc, the fib of 44 is...
18:47:49.294 701408733
18:47:49.325 Acording to Roc, the fib of 45 is...
18:47:49.325 1134903170
18:47:49.325 Acording to Roc, the fib of 46 is...
18:47:49.325 panicked at 'Roc panicked: 0x7be1 0', src/05-led-roulette/src/main.rs:20:5

view this post on Zulip Brendan Hansknecht (Feb 12 2022 at 02:50):

Really easy to get Roc to panic. Not fully sure why. Not specifically related to the value passed in. Like if I add more debug printing, Roc will panic more easier. It is a very strange side effect I guess.

view this post on Zulip Notification Bot (Feb 12 2022 at 03:56):

This topic was moved here from #compiler development > casual conversation by Richard Feldman

view this post on Zulip Brendan Hansknecht (Feb 12 2022 at 04:57):

Also, it looks to be running at the same speed as the while loop rust version. Both taking about 260ms to calculate the fib 10000 times.
Though linking in roc adds about 3000B while the rust fib function is like 100B.

20:36:02.026 Calculating fib(92) 10000 times in roc
20:36:02.278 Result: 7540113804746346429
20:36:02.278 Calculating fib(92) 10000 times in rust linear
20:36:02.542 Result: 7540113804746346429

This is the max fib that fits into a u64. Any higher and roc would panic.

view this post on Zulip Brendan Hansknecht (Feb 15 2022 at 09:28):

Roc calculating prime numbers and then displaying them in binary on an led display. The host passes a model (u64) to roc. Roc returns the next model and a 2d array to set the display to. Each iteration roc calculates the highest prime under the models value. microbit-primes.mp4

view this post on Zulip Folkert de Vries (Feb 15 2022 at 09:37):

very cool!

view this post on Zulip Lucas Rosa (Feb 15 2022 at 14:23):

woah that's cool!

view this post on Zulip Jared (Jun 09 2022 at 20:01):

@Brendan Hansknecht is this code up anywhere? I'm interested in trying to get Roc going on an esp8266

view this post on Zulip Brendan Hansknecht (Jun 09 2022 at 20:20):

https://github.com/bhansconnect/roc-microbit

view this post on Zulip Brendan Hansknecht (Jun 09 2022 at 20:20):

I haven't touched in a long while though, so it probably won't work if you update to the newest version of the compiler...but not fully sure

view this post on Zulip Brendan Hansknecht (Jun 09 2022 at 20:20):

Can probably fix it up if you need help. (probably even have an esp8266 sitting around to test it on)

view this post on Zulip Brendan Hansknecht (Jun 09 2022 at 20:23):

Also, you may want to try and use something based on the embassy branch. Embassy is a nicer/newer framework, but may not support as many chips especially with limited support for hardware/more manual setup if trying a different chip.

view this post on Zulip Brendan Hansknecht (Jun 09 2022 at 20:27):

Might be more of a reference for how you would do things on an esp8266. It looks like esp8266 may need to use a different rust based framework or c++ framework.

view this post on Zulip Jared (Jun 09 2022 at 21:02):

Yeah I was thinking I would write the platform in c++, as that's what I'm used to using w/ esp8266. no idea how hard it will be to get everything playing nice though :sweat_smile:

view this post on Zulip Jared (Jun 09 2022 at 21:03):

any special reason it's got roc as a git submodule? Is it just to pin to a specific version?

view this post on Zulip Brendan Hansknecht (Jun 09 2022 at 21:04):

Yeah, keeps it separate from my roc that I am working on, pins to specific version, and makes the repo more self contained.

view this post on Zulip Brendan Hansknecht (Jun 09 2022 at 21:04):

And yeah, C++ should be totally fine.

view this post on Zulip Brendan Hansknecht (Jun 09 2022 at 21:05):

Probably easier

view this post on Zulip Jared (Jun 09 2022 at 21:05):

oh I see you needed to make changes to roc to get it to target microbit

view this post on Zulip Jared (Jun 09 2022 at 21:05):

I assume I'll need similar changes?

view this post on Zulip Brendan Hansknecht (Jun 09 2022 at 21:06):

Yeah

view this post on Zulip Jared (Jun 09 2022 at 21:06):

espessif has their own llvm fork that's used https://github.com/espressif/llvm-project

view this post on Zulip Brendan Hansknecht (Jun 09 2022 at 21:06):

Oh...that may be problematic if it is need...or well, it would probably just require you telling roc to use that fork, so probably not terrible if it is up to date

view this post on Zulip Jared (Jun 09 2022 at 21:07):

yeah they have a llvm13 release

view this post on Zulip Brendan Hansknecht (Jun 09 2022 at 21:07):

You might be able to just target the same architecture with roc

view this post on Zulip Brendan Hansknecht (Jun 09 2022 at 21:07):

Yeah, then probably not to bad. Just build roc with that as your llvm path

view this post on Zulip Brendan Hansknecht (Jun 09 2022 at 21:07):

forget the exact correct term here

view this post on Zulip Brendan Hansknecht (Jun 09 2022 at 21:07):

And then add in the target like I did for the microbit

view this post on Zulip Jared (Jun 09 2022 at 21:08):

so, I have almost no understanding of how llvm works, but I know there's some sort of pseudo-assembly. Is there a way to have roc just output llvm-asm, and then externally bundle that up & link & such?

view this post on Zulip Jared (Jun 09 2022 at 21:08):

that way I wouldn't need roc to know anything about the custom llvm fork, or the architecture as much (?)

view this post on Zulip Brendan Hansknecht (Jun 09 2022 at 21:09):

Technically yes, but it is probably easier to have roc just spit out an object file

view this post on Zulip Brendan Hansknecht (Jun 09 2022 at 21:10):

As long as you get the architecture right, it should still run fine. I assume the custom version of LLVM is mostly for the hardware mapping more so than the assembly instruction type.

view this post on Zulip Brendan Hansknecht (Jun 09 2022 at 21:11):

Also, compiling roc with a different llvm that you already have on your machine should be easy. Should just be setting an environment variable to point to the correct version of llvm

view this post on Zulip Brendan Hansknecht (Jun 09 2022 at 21:11):

Otherwise, if you add --debug, it will dump the llvm ir which you can theoretically use as input.

view this post on Zulip Jared (Jun 09 2022 at 21:43):

ok cool. So by "getting the architecture right", I'm guessing the 8266 is RISC-V? the wikipedia page just says "RISC" so I'm not 100% sure.

view this post on Zulip Jared (Jun 09 2022 at 21:47):

oh wait maybe it's called xtensa

view this post on Zulip Brendan Hansknecht (Jun 09 2022 at 21:58):

It would be whatever the llvm target flag would get set to. Also, forget the esp8266 was the risc-v one. Fancy.

view this post on Zulip Jared (Jun 09 2022 at 22:15):

oh I guess it's just risc-based, not quite risc-v. any ideas on what the Environment would be?

view this post on Zulip Jared (Jun 10 2022 at 02:55):

:thinking: ok anyway I've got the xtensa llvm set up as my LLVM_SYS_130_PREFIX, but now cargo can't build roc at all :grimacing: because the cc doesn't know how to build for x86_64. Am I supposed to set the LLVM_SYS_130_PREFIX only when running the cli, and not during cargo build?

view this post on Zulip Jared (Jun 10 2022 at 03:14):

oof zig doesn't have xtensa support in core. that might be one blow too many

view this post on Zulip Jared (Jun 10 2022 at 03:22):

hmmmm but someone has gotten zig to work with xtensa apparently :thinking: https://github.com/inetbowser/zig-xtensa

view this post on Zulip Brendan Hansknecht (Jun 10 2022 at 04:49):

Oh. That might be more complicated than I expected. I would have guessed it still supported x86 building

view this post on Zulip Brendan Hansknecht (Jun 10 2022 at 04:50):

But yeah, needs to be set during build

view this post on Zulip Jared (Jun 10 2022 at 12:25):

it's possible it would know how to do x86 if I build from source; I'm just working with the release bundle they put out.

view this post on Zulip Brendan Hansknecht (Jun 19 2022 at 00:32):

Line following in Roc

view this post on Zulip Brendan Hansknecht (Jun 19 2022 at 00:34):

Technically not really more complicated on the roc side than the prime example. Mostly was pipelining on the rust side to control the robot and exposing that to roc.

view this post on Zulip Richard Feldman (Jun 19 2022 at 13:00):

coooooooooool :heart_eyes: :heart_eyes: :heart_eyes:


Last updated: Jul 05 2025 at 12:14 UTC