Skip to content

Arduino support? #3637

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
clankill3r opened this issue Nov 9, 2019 · 24 comments
Open

Arduino support? #3637

clankill3r opened this issue Nov 9, 2019 · 24 comments
Labels
arch-avr 8-bit AVR
Milestone

Comments

@clankill3r
Copy link

Is it possible to have support for Arduino and other micro controllers?
And if so, what needs to happen?

@pixelherodev
Copy link
Contributor

Arduino is AVR-based, so really it's just a matter of waiting for LLVM's AVR support to stabilize, and adding support to the standard library. In theory, basic language support should already be in place, as long as you don't use the standard library.

The real question then is how to handle stuff like the GPIO on an Arduino - e.g. digitalRead in the Arduino AVR SDK. Similar functionality could probably be added to the standard library under std.os for Arduino.

@daurnimator daurnimator added the arch-avr 8-bit AVR label Nov 10, 2019
@clankill3r
Copy link
Author

clankill3r commented Nov 12, 2019 via email

@andrewrk
Copy link
Member

Just to be clear, you can in fact use most of the standard library. But the cross platform OS abstractions lack Arduino support, so it would be a compile error currently to, for example, open a file.

@clankill3r
Copy link
Author

Interesting. I might look into this more later (like 6 months from now).

@prazzb
Copy link
Contributor

prazzb commented Nov 13, 2019

auduino's digitalRead is mmio so that's pretty trivial. But as I mentioned in #3645, avr-lld support is pretty bad. Thus, for simple code I have done

zig build-obj --release-small --target avr-freestanding-eabi main.zig
avr-ld -o main.elf main.o # This is the gnu linker
llvm-objcopy -j .text -j .data -O binary main.elf main.bin

The need for llvm-objcopy should be removed after #2826

Because of #3645 (and probably other issues) the below example doesn't run with master (as of this writing) but does work with 0.5.0

const SFR=packed struct {
 pin03:u4,
 pin47:u4,
};
const PORTB = 0x38;

export fn main () void{
    var portb=@intToPtr(*volatile SFR,PORTB);
    portb.pin47=15;
    while(true){}
}

This example reads portb, changes upper nibble to 0xF and then writes data back to portb.
You can verify the output by checking the elf file or the final binary file with any disassembler

I should probably write a github wiki page for avr

@andrewrk andrewrk added this to the 1.1.0 milestone Nov 27, 2019
@frett27
Copy link

frett27 commented Jan 3, 2020

i had a small time to work on it,
work with :

const PORTBDATA = 0x25;
const PORTBDIR = 0x24;

export fn main () void{
    var pb = @intToPtr(*volatile u8, PORTBDATA);
    var pbd = @intToPtr(*volatile u8, PORTBDIR);
    pbd.* = 0xff;
    while(true){
        pb.* = (1 << 5);
        var i : u32 = 0;
        while (i<5000) {
            // nop
            i +=1;
            var j = pb.*;
        }
        pb.* = 0;
        i = 0;
        while (i<5000) {
            // nop
            i +=1;
            var j = pb.*;
        }
    }
}

and compile with

export PATH=/c/zig-windows-x86_64-0.5.0+51cbd9682/:$PATH
zig build-obj --release-small -target avr-freestanding-eabi test-blink.zig

export PATH=/c/projets/2019-Compteur-Facebook/arduino-1.8.9/hardware/tools/avr/bin:$PATH
avr-ld -o main.elf test-blink.o
avr-objcopy -j .text -j .data -O ihex main.elf main.hex

avrdude.exe  -C/c/projets/2019-Compteur-Facebook/arduino-1.8.9/hardware/tools/avr/etc/avrdude.conf -patmega328p -carduino -PCOM5 -b115200 -D -Uflash:w:main.hex:i

@daurnimator
Copy link
Contributor

const PORTBDATA = 0x25;

This could be: const PORTBDATA = @intToPtr(*volatile u8, 0x25); and then you don't need it in your main function.

@frett27
Copy link

frett27 commented Jan 5, 2020

sure,
sorry, i was so excited to make it work on Uno :-)

@pixelherodev
Copy link
Contributor

pixelherodev commented Jan 9, 2020

So, should standard functionality be implemented in os.arduino? I can definitely do that at some point in the next month if that's desired.

Edit: the other question then is how it should be implemented. os.arduino.digitalRead could be done, but a pin type might also work - e.g.

const led_pin = os.arduino.digital_pin(3);

export fn main() void {
    led_pin.mode(os.arduino.digital_pin.output);
    led_pin.write(1);
    // do other stuff
}

@frett27
Copy link

frett27 commented Jan 9, 2020

Arduino is a microcontroller, and pin management permit to handle the hardware directly.

Seems there are different strategies in adding arduino support :

  • create a "bare bone" harware library (arduino.*, for uno, micro ... etc)
  • create a HAL (hardware abstraction layer), to have the same names, for all elements, (generic for common properties like pins .. ), and b able to add specific things also, and ease the port to other microcontrollers
  • integrate a small RT OS (chibi or FreeRT OS) -> much smart, we gain the threading, semaphores .. etc ..

none the less, i like the "pin" object description,
this also can be started simply and refactor as needed, the "bare bone" is the first step to other ones.

i would probably not put it in os. module

make sense ?
any ideas ?

i saw rust has worked also to create a dedicated project for embedded

@frett27
Copy link

frett27 commented Jan 9, 2020

Creating a "pin" object, leads to create a wrapper, or modify all existing libraries we could reuse.This has to be put into balance.

@freiguy1
Copy link

freiguy1 commented Dec 3, 2020

I don't think the std lib should have things like digtalWrite(1, true) because at the ATmega328 chip level, pin 1 means nothing. The Arduino company has designated that itself. In the future, perhaps there could be an "ArduinoUno" board-specific library which has these things.

Have I overlooked something?

@clankill3r
Copy link
Author

clankill3r commented Dec 4, 2020 via email

@midirhee12
Copy link
Contributor

As far as possible goes, is it possible to just have a cImport for these kinds of things instead of rewriting everything into std lib?

@s-celles
Copy link

s-celles commented Dec 30, 2021

@clankill3r https://github.com/FireFox317/avr-arduino-zig from @FireFox317 may help
FYI I currently haven't try it myself!

Some forks can be interesting also https://github.com/FireFox317/avr-arduino-zig/network
https://github.com/Guigui220D/avr-arduino-zig
https://github.com/SebastianZaha/avr-arduino-zig

@clankill3r
Copy link
Author

@krishnaTORQUE for your future github adventures, there is a subscribe button in the right pannel 😃

@Tiedye
Copy link

Tiedye commented Jun 6, 2023

As far as possible goes, is it possible to just have a cImport for these kinds of things instead of rewriting everything into std lib?

Is this possible today?

@silversquirl
Copy link
Contributor

For info on getting Zig working on AVR and other microcontrollers, I'd recommend the microzig project and the Zig Embedded Group Discord

@nekodjin
Copy link

Any update on the progress/feasibility of this?

@AnErrupTion
Copy link

AnErrupTion commented Jul 26, 2023

Any update on the progress/feasibility of this?

It is, in theory, possible to use Zig's C backend and use either avr-gcc to build, or use something like PlatformIO. But, since the Arduino SDK and libraries are made in C++, you can't directly import them via @cImport(), so you'd need to either write C wrappers, or rewrite those in Zig.

@Gamer-Kold
Copy link

so you'd need to either write C wrappers, or rewrite those in Zig

That doesn't sound too bad; I have some free time, does anyone know how big the SDK is and/or where I can get my hands on it. I'm having trouble finding it.

@AnErrupTion
Copy link

I'm having trouble finding it.

You can find it here: https://github.com/arduino/ArduinoCore-avr

@mutexstream
Copy link

Has there been progress on this?

@vlappa
Copy link

vlappa commented May 18, 2025

For info on getting Zig working on AVR and other microcontrollers, I'd recommend the microzig project and the Zig Embedded Group Discord

FYI: ZigEmbeddedGroup/microzig#534 (comment)

Milestone is set after Zig 1.0? Fair enough, but like mentioned in the issue above, it might be a nice way to learn programming (with Zig) if one could use educational resources for Arduino with Zig as well (with some adjustments). Could be a nice way to introduce the next generation programmers to Zig and to programming in general.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arch-avr 8-bit AVR
Projects
None yet
Development

No branches or pull requests