Created
July 20, 2014 16:36
-
-
Save apparentlymart/19f221e191bd591dd704 to your computer and use it in GitHub Desktop.
mbed LPC1768 pyLLVM GPIO
This file contains 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
/* Boot stub. Must be linked first to be at the beginning of the program ROM. */ | |
/* Main program must provide an entry point function called "main". */ | |
.cpu cortex-m3 | |
.thumb | |
.word 0x10008000 /* stack top address */ | |
.word _start /* 1 Reset */ | |
.word hang /* 2 NMI */ | |
.word hang /* 3 HardFault */ | |
.word hang /* 4 MemManage */ | |
.word hang /* 5 BusFault */ | |
.word hang /* 6 UsageFault */ | |
.word hang /* 7 RESERVED */ | |
.word hang /* 8 RESERVED */ | |
.word hang /* 9 RESERVED*/ | |
.word hang /* 10 RESERVED */ | |
.word hang /* 11 SVCall */ | |
.word hang /* 12 Debug Monitor */ | |
.word hang /* 13 RESERVED */ | |
.word hang /* 14 PendSV */ | |
.word hang /* 15 SysTick */ | |
.word hang /* 16 External Interrupt(0) */ | |
.word hang /* 17 External Interrupt(1) */ | |
.word hang /* 18 External Interrupt(2) */ | |
.word hang /* 19 ... */ | |
.thumb_func | |
hang: b . | |
.thumb_func | |
.globl _start | |
_start: | |
bl main | |
bl hang | |
.end |
This file contains 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
#!/usr/bin/env python | |
from llvm import * | |
from llvm.core import * | |
module = Module.new('blink') | |
ty_void = Type.void() | |
ty_int32 = Type.int(32) | |
ty_int32_ptr = Type.pointer(ty_int32) | |
ty_f_main = Type.function(ty_void, []) | |
f_main = module.add_function(ty_f_main, "main") | |
bb = f_main.append_basic_block("entry") | |
builder = Builder.new(bb) | |
# OK, now for the instructions themselves. We'll create an add | |
# instruction that returns the sum as a value, which we'll use | |
# a ret instruction to return. | |
#tmp = builder.add(f_sum.args[0], f_sum.args[1], "tmp") | |
#builder.ret(tmp) | |
reg_direction = builder.inttoptr( | |
Constant.int(ty_int32, 537509922), | |
ty_int32_ptr, | |
) | |
# LPC1768 GPIOs have both a basic value register, allowing the | |
# port to be read and written as a single integer, and special | |
# set/clear registers that will set/clear (respectively) any | |
# bit set to 1 when written, leaving any 0 bits unchanged. | |
# This allows individual bits to be modified without having to | |
# read and preserve the other bits. | |
reg_clear = builder.inttoptr( | |
Constant.int(ty_int32, 537509950), | |
ty_int32_ptr, | |
) | |
reg_set = builder.inttoptr( | |
Constant.int(ty_int32, 537509946), | |
ty_int32_ptr, | |
) | |
# Set the LED pin to be an output. | |
builder.store( | |
Constant.int(ty_int32, 128), | |
reg_direction, | |
volatile=True, | |
) | |
# Clear LED pin. | |
builder.store( | |
Constant.int(ty_int32, 128), | |
reg_clear, | |
volatile=True, | |
) | |
# Set LED pin. | |
builder.store( | |
Constant.int(ty_int32, 128), | |
reg_set, | |
volatile=True, | |
) | |
builder.ret_void() | |
# We've completed the definition now! Let's see the LLVM assembly | |
# language representation of what we've created: | |
print module |
This file contains 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
/* GCC Linker Script */ | |
MEMORY | |
{ | |
rom(RX) : ORIGIN = 0x00000000, LENGTH = 0x40000 | |
ram(WAIL) : ORIGIN = 0x10000000, LENGTH = 30K | |
} | |
SECTIONS | |
{ | |
.text : { *(.text*) } > rom | |
.bss : { *(.bss*) } > ram | |
} |
This file contains 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
all: blink.bin | |
blink.ll: genblink.py | |
python genblink.py >blink.ll | |
blink.bc: blink.ll | |
opt-3.4 blink.ll -o blink.bc | |
blink.s: blink.bc | |
llc-3.4 -march=thumb blink.bc -o blink.s | |
%.o: %.s | |
arm-none-eabi-as $< -o $@ | |
blink.elf: boot.o blink.o | |
arm-none-eabi-ld -T linker boot.o blink.o -o blink.elf | |
blink.bin: blink.elf | |
arm-none-eabi-objcopy blink.elf blink.bin -O binary | |
clean: | |
rm -f blink.ll blink.s blink.o boot.o blink.elf blink.bin blink.bc | |
upload: blink.bin | |
cp blink.bin /media/MBED/blink.bin |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment