Crystal lang for microcontrollers

Hello Everyone, I recently bought a arduino, and I would like to use crystal to program for it, I known that is not officially supported but, I still want to give it a shot for learning purposes, so I write the following blink program for arduino mega2560 using C bindings for arduino.h :

@[Link("Arduino.h")]
lib Arduino
    fun pinMode(pin : UInt8, mode : UInt8) 
    fun digitalWrite(pin : UInt8, val : UInt8)
    fun delay(ms : UInt32)

    fun setup()
    fun loop()
end


def setup
    Arduino.pinMode(22, 0x1)
end

def loop
    Arduino.digitalWrite(22, 0x1)

    Arduino.delay(1000)

    Arduino.digitalWrite(22, 0x0)

    Arduino.delay(1000)
end

setup()

while true
  loop()
end

using this to compile and returning a .o object and a linker command:

crystal build trigger.cr --cross-compile --target "avr-unknown-unknown" --mcpu=atmega2560 --error-trace --prelude=empty
cc trigger.o -o trigger  --target=avr-unknown-unknown -mmcu=atmega2560 -Wl,--gc-sections -L/snap/crystal/2186/bin/../lib/crystal -lArduino.h

however when I try to link it gives me a error:

cc: error: unrecognized command-line option ‘--target=avr-unknown-unknown’
cc: error: unrecognized command-line option ‘-mmcu=atmega2560’

anyone know how proper make a avr executable, I know it probably require a little tricks and the code may be unsafe and ugly but I still wanna try

thank you all in advance :slight_smile:

3 Likes

You can find some basic instructions on how to build for arduino in the PR that enabled initial support:

1 Like

We only added support for the AVR architecture.

The Arduino library is something else entirely, and they didn’t make it easy to integrate: there’s no libarduino.a or similar (:sob:) and it’s a mix of C and C++ (:sob:). I have a minimal experimental integration… somewhere.

I think the Arduino IDE builds each required source file as needed for the exact CPU as it builds and links the project.

2 Likes

Hello again thank you for the good references and sorry for the late reply but I didn’t have the time to test and reply.

I achieved to generate some .hex files but without the binds for Arduino will be terrible write anything a little more complex, maybe I should try to write some libs in my free time.

Thank you all again for your help, and I really look forward for the crystal evolution in the micro-controllers world

Digging into the arduino sdk is indeed gnarly! Nothing is documented, dozens of boards have arbitrary pinouts and pin-abilities. The IDE performs hijinks to manipulate source code pre-compilation, and scripting avr-dude itself is rather brutal.

But I think this is one of the ways that Crystal could really shine. It’s low level enough to run on a dedicated processor (ruby can’t do that), and it’s high level enough to be used and built-on by humans (where C struggles). There have been attempts at embedded python, etc, but that requires new hardware.

Embedded programming is a terribly managed ecosystem of software. Part of what Arduino managed to do so many years ago was hide most of that brutalist approach to software behind something beginner friendly macros and functions, and a toolchain that supported development.

About 15 years ago I had the good fortune to teach middle schoolers (and even much younger) to program Arduino boards and it was amazing to see them adapt and execute within the clumsy Arduino IDE all because the SDK (and broader ecosystem) provided a meaningful set of tools they could understand.

What was really disappointing to see was when a student wanted to do more but that required jumping into “real code” – deep C crap that’s difficult to read even for a seasoned software professional. The student had a vision for what they wanted to accomplish, but when shown what was required to make it happen they would just surrender: “can you do it for me?” or worse they would give up entirely.

Raspberry Pi comes along and offers python as a native tool, and it’s fine but still suffers from a complete lack of developer ergonomics – partly because it’s “python” and partly because it’s a thin layer on top of the brutalist C code which powers it. And now, instead of running a dedicated processor, it’s running a whole operating system, with all the pitfalls and pain that accompanies it.

Anyway, I have high hopes for Embedded Crystal.

4 Likes

I would break out in tears of joy if I could do microcontroler stuff in Crystal.

I forgot to push my highly experimental shard :scientist:

This is a mere dump of my local shard, that’s working with the deb packages on Ubuntu 20.04. I can’t vouch that it actually works, especially the manual C++ bindings :sweat_smile:

And there are zero support for interrupts or PROGMEN (to put things in address space 1), and the bindings only care about atmega328p (the classic Arduino Uno). You’ll have to fix or add support for other boards & tweaks.

The libarduino-core.a must be built for each and every CPU. The Makefile in the shard can be used to do that. It would be great to have some more generic.

2 Likes

What about binding to Zephyr? That’s the approach embedded Swift is taking, and could provide a cleaner interface than Arduino

Zephyr is an OS. One that targets microcontrollers, but with enough memory and CPU power such as the RP2040 or ATMEL SAM (both ARM based) which includes the SAM based Arduino boards, but not the ones based on ATMEL AVR (The Uno R2 is 16Mhz, 32KB flash and 2KB RAM).

I’m surprised that it supports some ESP32 though, but they seem to have more CPU power, flash and ram than the usual AVR.

That doesn’t mean it wouldn’t be nice (quite the opposite), but it won’t cover all the embedded spectrum —but still cover quite a lot!

Crystal team drawing too many pies … but, it never became real, even see no hope.

When we can remove the “preview” from within “-D preview_mt”?
I think it would have been reasonable to discuss AVR support at that time.

1 Like