Skip to content

Instantly share code, notes, and snippets.

@denizzzka
Last active January 23, 2020 12:04
Show Gist options
  • Select an option

  • Save denizzzka/1e31d3cf275403ae50768eca613fb1c6 to your computer and use it in GitHub Desktop.

Select an option

Save denizzzka/1e31d3cf275403ae50768eca613fb1c6 to your computer and use it in GitHub Desktop.
Bare metal Dlang ARM
// https://github.com/ldc-developers/ldc/issues/3290
import ldc.attributes;
@llvmAttr("nounwind"):
import ldc.llvmasm;
#include <libopencm3/stm32/rcc.h>
#include <libopencm3/stm32/gpio.h>
static void gpio_setup()
{
rcc_periph_clock_enable(RCC_GPIOB);
gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO1);
}
import ldc.attributes;
extern(C) void main()
{
int i;
gpio_setup();
while (1) {
gpio_toggle(GPIOB, GPIO1); /* LED on/off */
for (i = 0; i < 800000; i++) /* Wait a bit. */
__asm("nop", "");
}
}
@denizzzka
Copy link
Author

denizzzka commented Jan 18, 2020

How to build:

Build libopencm3

Build dpp

Save previous snippet as test.dpp

Then:

~/Dev/dpp/bin/d++ --define=STM32F1 --include-path=../../../libopencm3/include/ --preprocess-only test.dpp 

ldc2 -g -march=thumb -mcpu=cortex-m3 --betterC -c test.d

arm-none-eabi-ld test.o --gc-sections -L ../../../libopencm3/lib/ -lopencm3_stm32f1 -T ../../../libopencm3/lib/stm32/f1/stm32f103x8.ld -o test.elf

@denizzzka
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment