Created
November 20, 2024 15:17
-
-
Save Alexander-Barth/9ef488531fd353518cdcfa2d4c8557c7 to your computer and use it in GitHub Desktop.
Julia on Arduino with MCUCompiler.jl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module blink_led | |
| # https://docs.arduino.cc/retired/hacking/software/PortManipulation/ | |
| # /usr/lib/avr/include/avr/iom328p.h | |
| # from Debian package avr-libc | |
| const __SFR_OFFSET = 0x20 | |
| @inline _SFR_IO8(io_addr) = Ptr{UInt8}(Int(io_addr + __SFR_OFFSET)) | |
| # maps to Arduino digital pins 8 to 13 | |
| const PINB = _SFR_IO8(0x03) | |
| const DDRB = _SFR_IO8(0x04) | |
| const PORTB = _SFR_IO8(0x05) | |
| # maps to Arduino analog pins 0 to 5. | |
| const PINC = _SFR_IO8(0x06) | |
| const DDRC = _SFR_IO8(0x07) | |
| const PORTC = _SFR_IO8(0x08) | |
| # maps to Arduino digital pins 0 to 7 | |
| const PIND = _SFR_IO8(0x09) | |
| const DDRD = _SFR_IO8(0x0A) | |
| const PORTD = _SFR_IO8(0x0B) | |
| function load(x::Ptr{UInt8}) | |
| return Base.llvmcall( | |
| """ | |
| %ptr = inttoptr i64 %0 to i8* | |
| %data = load i8, i8* %ptr | |
| ret i8 %data | |
| """, | |
| UInt8, | |
| Tuple{Ptr{UInt8}}, | |
| x | |
| ) | |
| end | |
| function volatile_store!(x::Ptr{UInt8}, v::UInt8) | |
| return Base.llvmcall( | |
| """ | |
| %ptr = inttoptr i64 %0 to i8* | |
| store volatile i8 %1, i8* %ptr, align 1 | |
| ret void | |
| """, | |
| Cvoid, | |
| Tuple{Ptr{UInt8},UInt8}, | |
| x, | |
| v | |
| ) | |
| end | |
| function keep(x::T) where T <: Union{Int8,UInt8} | |
| return Base.llvmcall( | |
| """ | |
| call void asm sideeffect "", "X,~{memory}"(i8 %0) | |
| ret void | |
| """, | |
| Cvoid, | |
| Tuple{T}, | |
| x | |
| ) | |
| end | |
| function keep(x::T) where T <: Union{Int16,UInt16} | |
| return Base.llvmcall( | |
| """ | |
| call void asm sideeffect "", "X,~{memory}"(i16 %0) | |
| ret void | |
| """, | |
| Cvoid, | |
| Tuple{T}, | |
| x | |
| ) | |
| end | |
| function keep(x::T) where T <: Union{Int32,UInt32} | |
| return Base.llvmcall( | |
| """ | |
| call void asm sideeffect "", "X,~{memory}"(i32 %0) | |
| ret void | |
| """, | |
| Cvoid, | |
| Tuple{T}, | |
| x | |
| ) | |
| end | |
| const INPUT = false; | |
| const OUTPUT = true; | |
| const LOW = false; | |
| const HIGH = true; | |
| const LED_BUILTIN = 13; | |
| @inline function getbit(ptr,bit) | |
| v = load(ptr) | |
| state = (v & UInt8(1 << bit)) != UInt8(0) | |
| end | |
| @inline function setbit(ptr,bit,state) | |
| v = load(ptr) | |
| if state | |
| v |= UInt8(state << bit) | |
| else | |
| v &= ~UInt8(~state << bit) | |
| end | |
| volatile_store!(ptr, v) | |
| end | |
| @inline function pinMode(pin, state) | |
| (ddr,shift) = | |
| if pin < 8 | |
| (DDRD,0x00) | |
| else | |
| (DDRB,0x08) | |
| end | |
| setbit(ddr,pin-shift,state) | |
| return nothing | |
| end | |
| @inline function digitalRead(pin) | |
| (port,shift) = | |
| if pin < 8 | |
| (PORTD,0x00) | |
| else | |
| (PORTB,0x08) | |
| end | |
| return getbit(port,pin-shift) | |
| end | |
| @inline function digitalWrite(pin, state) | |
| (port,shift) = | |
| if pin < 8 | |
| (PORTD,0x00) | |
| else | |
| (PORTB,0x08) | |
| end | |
| setbit(port,pin-shift,state) | |
| return nothing | |
| end | |
| @inline function delay(ms) | |
| # TODO: do not hard-code delay | |
| for a in UInt8(0):UInt8(14) | |
| for z in UInt8(0):UInt8(255) | |
| for y in UInt8(0):UInt8(255) | |
| keep(a) | |
| keep(z) | |
| keep(y) | |
| end | |
| end | |
| end | |
| end | |
| function main() | |
| # pinMode(4, OUTPUT) | |
| # pinMode(5, OUTPUT) | |
| # pinMode(LED_BUILTIN, OUTPUT) | |
| pinMode.((3,4,LED_BUILTIN), OUTPUT) | |
| pinMode(2, INPUT) | |
| digitalWrite(2, HIGH) | |
| while true | |
| #s = digitalRead(2) | |
| s = getbit(PIND,2) | |
| digitalWrite(3, s) | |
| digitalWrite(4, HIGH) | |
| digitalWrite(LED_BUILTIN, LOW) | |
| delay(300) | |
| digitalWrite(4, LOW) | |
| digitalWrite(LED_BUILTIN, HIGH) | |
| delay(300) | |
| end | |
| end | |
| end # module | |
| using MCUCompiler | |
| using GPUCompiler | |
| struct ArduinoParams1 <: GPUCompiler.AbstractCompilerParams | |
| name:: String | |
| end | |
| target = MCUCompiler.Arduino.ArduinoTarget(ArduinoParams1("blink_led")) | |
| MCUCompiler.build(blink_led,"./",target) | |
| #run(`avr-objdump -d /home/abarth/Test/Arduino/latest/blink_led.elf`) | |
| cd("/home/abarth/Test/Arduino/latest") do | |
| run(`avrdude -V -c arduino -p ATMEGA328P -P /dev/ttyACM3 -U flash:w:blink_led.hex`) | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment