diff options
author | Josh Rahm <joshuarahm@gmail.com> | 2023-01-18 02:06:09 -0700 |
---|---|---|
committer | Josh Rahm <joshuarahm@gmail.com> | 2023-01-18 02:06:09 -0700 |
commit | 2ead5ea02814bcae8f4b08206aa8017051357b0f (patch) | |
tree | c8d3be91672c139429930cf4672d3815a4aab26b | |
download | ch573-2ead5ea02814bcae8f4b08206aa8017051357b0f.tar.gz ch573-2ead5ea02814bcae8f4b08206aa8017051357b0f.tar.bz2 ch573-2ead5ea02814bcae8f4b08206aa8017051357b0f.zip |
Initial blinky commit!
-rw-r--r-- | .clang-format | 11 | ||||
-rw-r--r-- | .gitignore | 3 | ||||
-rw-r--r-- | Makefile | 20 | ||||
-rw-r--r-- | blinky.c | 90 | ||||
-rw-r--r-- | ls.ld | 18 |
5 files changed, 142 insertions, 0 deletions
diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..567ff11 --- /dev/null +++ b/.clang-format @@ -0,0 +1,11 @@ +BasedOnStyle: Google +IndentWidth: 2 +BreakBeforeBraces: Custom +BraceWrapping: + AfterFunction: true +AllowShortFunctionsOnASingleLine: InlineOnly +AlignAfterOpenBracket: AlwaysBreak +BinPackArguments: false +BinPackParameters: false +ExperimentalAutoDetectBinPacking: false +AllowAllParametersOfDeclarationOnNextLine: true diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7cf238f --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.o +*.elf +*.bin diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..ce65816 --- /dev/null +++ b/Makefile @@ -0,0 +1,20 @@ +CC=riscv32-unknown-elf-gcc +LD=riscv32-unknown-elf-ld +CPY=riscv32-unknown-elf-objcopy + +all: blinky.bin + +blinky.bin: blinky.elf + $(CPY) -O binary blinky.elf blinky.bin + +blinky.o: blinky.c + $(CC) -Os -lgcc -static -nostartfiles -o blinky.o -c blinky.c + +blinky.elf: blinky.o ls.ld + $(LD) --cref -static -T ls.ld -o blinky.elf blinky.o + +flash: all + sudo ~/Projects/isp55e0/isp55e0 -f blinky.bin + +clean: + rm -rf *.o *.elf *.bin diff --git a/blinky.c b/blinky.c new file mode 100644 index 0000000..001e185 --- /dev/null +++ b/blinky.c @@ -0,0 +1,90 @@ +#include <stdint.h> + +/* + * Function (or really pointer to code) for the reset interrupt handler. + */ +void on_reset(void); + +/* + * Function which delays for a bit. + */ +void delay(void); + +/* Type def to a void function to make things mor ereadable. */ +typedef void (*isr_routine)(void); + +/* Gpio configuration structure. This is exactly how it is laid out in + * memory. In these registers bit X referes to pin X. */ +typedef struct { + /* The direction of the gpio pin. 1 = output, 0 = input. */ + uint32_t dir; + + /* The value of the gpio pin (for input). */ + uint32_t in; + + /* Write to set the output value for the pin. */ + uint32_t out; + + /* The clear value. Resets the pin to what it is at reset. */ + uint32_t clr; + + /* Sets the pin to be pull-up on logical 1 in input mode. */ + uint32_t pu; + + /* Sets whether the pin should be pull-down (open-drain) for logical 0 or + * drive for logical 1. 1 = pull-down, 0 = drive. */ + uint32_t pd_drv; +} gpio_t; + +/** The ISR Vector structure. This is linked to starting at address 0. */ +__attribute((__section__(".isr_vector"))) volatile struct { + // What NULL points to. nothing useful. + uint32_t reserved__; + // Called when the device boots or reset is pressed. + isr_routine reset_cb; + isr_routine nmi_cb; + isr_routine exc_cb; +} isr_vectors = {.reset_cb = on_reset}; + +// GPIO configuration registers. These are defined in the linker script. +extern volatile gpio_t gpio_a; +extern volatile gpio_t gpio_b; + +/* Main routine. This is called on_reset once everything else has been set up. + */ +static void start(void) +{ + uint32_t bit = 1 << 8; + gpio_a.dir |= bit; // Set to "out" + gpio_a.pd_drv |= bit; // Set to "open drain" + + for (;;) { + gpio_a.out &= ~bit; // Pin low (turns on LED) + delay(); + gpio_a.out |= bit; // Pin high (turns off LED) + delay(); + } +} + +/* + * The reset callback.This has to be a naked function because the stack pointer + * may not be initialized!!. + */ +__attribute((naked)) void on_reset(void) +{ + // Set up the stack pointer to point to the end of SRAM. + asm volatile( + "li sp,0x20008000\n" + "addi sp,sp,-4\n" + "jalr %0\n" + "spin:\n" + "j spin\n" + : + : "r"(start)); +} + +void delay(void) +{ + for (volatile uint32_t i = 0; i < 20000; ++i) + ; +} @@ -0,0 +1,18 @@ +MEMORY +{ + flash : org = 0x00000000, len = 512k + sram : org = 0x20003800, len = 18k +} + +SECTIONS +{ + gpio_a = ABSOLUTE(0x400010A0); + gpio_b = ABSOLUTE(0x400010C0); + + . = ORIGIN(flash); + .text : ALIGN(0x04) { + *(.isr_vector); + . = ALIGN(0x100); + *(.text); + } >flash AT>flash +} |