Skip to content

Commit

Permalink
feat: add new board blinky example, nRF52840
Browse files Browse the repository at this point in the history
  • Loading branch information
MaksymShutiak authored and cpq committed Aug 28, 2024
1 parent 00c96aa commit d1a6197
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/**/build/
17 changes: 17 additions & 0 deletions templates/blinky/nRF52840dk/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
CFLAGS ?= -W -Wall -Wextra -Werror -Wundef -Wshadow -Wdouble-promotion -Wformat-truncation -fno-common
CFLAGS += -g3 -Os -ffunction-sections -fdata-sections -Wno-shadow
CFLAGS += -mcpu=cortex-m4
SOURCES = main.c

build:
mkdir build
arm-none-eabi-gcc $(SOURCES) -c $(CFLAGS) -o ./build/main.o
arm-none-eabi-gcc -T link.ld -nostdlib ./build/main.o -o ./build/firmware.elf
arm-none-eabi-objcopy -Oihex ./build/firmware.elf ./build/firmware.hex

flash:
nrfjprog -f NRF52 --program ./build/firmware.hex --recover --verify
nrfjprog -f NRF52 --reset

clean:
rm -rf ./build
25 changes: 25 additions & 0 deletions templates/blinky/nRF52840dk/hal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#define P0 ((struct gpio *) 0x50000000)
#define P1 ((struct gpio *) 0x50000300)
#define BUILD_IN_LED_1 13

enum { GPIO_MODE_INPUT, GPIO_MODE_OUTPUT };
enum { LOW, HIGH };

struct gpio {
volatile uint32_t RESERVED[321], OUT, OUTSET, OUTCLR, IN, DIR, DIRSET, DIRCLR, LATCH, DETECTMODE, PIN_CNF[32];
};

void set_gpio_mode(struct gpio * port, int pin, int mode) {
port->DIR |= mode << pin;
}

void gpio_write(struct gpio * port, int pin, int value) {
if (value == 1)
port->OUTSET |= 1 << pin;
else
port->OUTCLR |= 1 << pin;
}

static inline void spin(volatile uint32_t count) {
while (count--) (void) 0;
}
30 changes: 30 additions & 0 deletions templates/blinky/nRF52840dk/link.ld
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
ENTRY(_reset);

MEMORY {
flash(rx) : ORIGIN = 0x00000000, LENGTH = 1M
sram(rwx) : ORIGIN = 0x20000000, LENGTH = 256k
}
_estack = ORIGIN(sram) + LENGTH(sram);

SECTIONS {
.vectors : { KEEP(*(.vectors)) } > flash
.text : { *(.text*) } > flash
.rodata : { *(.rodata*) } > flash

.data : {
_sdata = .; /* .data section start */
*(.first_data)
*(.data SORT(.data.*))
_edata = .; /* .data section end */
} > sram AT > flash
_sidata = LOADADDR(.data);

.bss : {
_sbss = .; /* .bss section start */
*(.bss SORT(.bss.*) COMMON)
_ebss = .; /* .bss section end */
} > sram

. = ALIGN(8);
_end = .; /* for cmsis_gcc.h */
}
31 changes: 31 additions & 0 deletions templates/blinky/nRF52840dk/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <stdint.h>
#include "hal.h"

int main(void) {
set_gpio_mode(P0, BUILD_IN_LED_1, GPIO_MODE_OUTPUT);

while (1) {
gpio_write(P0, BUILD_IN_LED_1, HIGH);
spin(9999999);
gpio_write(P0, BUILD_IN_LED_1, LOW);
spin(9999999);
}
}

// Startup code
__attribute__((naked, noreturn)) void _reset(void) {
// memset .bss to zero, and copy .data section to RAM region
extern long _sbss, _ebss, _sdata, _edata, _sidata;
for (long *dst = &_sbss; dst < &_ebss; dst++) *dst = 0;
for (long *dst = &_sdata, *src = &_sidata; dst < &_edata;) *dst++ = *src++;

main(); // Call main()
for (;;) (void) 0; // Infinite loop in the case if main() returns
}

extern void _estack(void); // Defined in link.ld

// 16 standard and 42 nRF52-specific handlers
__attribute__((section(".vectors"))) void (*const tab[16 + 42])(void) = {
_estack, _reset};

0 comments on commit d1a6197

Please sign in to comment.