Skip to content

Commit d1a6197

Browse files
MaksymShutiakcpq
authored andcommitted
feat: add new board blinky example, nRF52840
1 parent 00c96aa commit d1a6197

File tree

5 files changed

+104
-0
lines changed

5 files changed

+104
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/**/build/

templates/blinky/nRF52840dk/Makefile

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
CFLAGS ?= -W -Wall -Wextra -Werror -Wundef -Wshadow -Wdouble-promotion -Wformat-truncation -fno-common
2+
CFLAGS += -g3 -Os -ffunction-sections -fdata-sections -Wno-shadow
3+
CFLAGS += -mcpu=cortex-m4
4+
SOURCES = main.c
5+
6+
build:
7+
mkdir build
8+
arm-none-eabi-gcc $(SOURCES) -c $(CFLAGS) -o ./build/main.o
9+
arm-none-eabi-gcc -T link.ld -nostdlib ./build/main.o -o ./build/firmware.elf
10+
arm-none-eabi-objcopy -Oihex ./build/firmware.elf ./build/firmware.hex
11+
12+
flash:
13+
nrfjprog -f NRF52 --program ./build/firmware.hex --recover --verify
14+
nrfjprog -f NRF52 --reset
15+
16+
clean:
17+
rm -rf ./build

templates/blinky/nRF52840dk/hal.h

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#define P0 ((struct gpio *) 0x50000000)
2+
#define P1 ((struct gpio *) 0x50000300)
3+
#define BUILD_IN_LED_1 13
4+
5+
enum { GPIO_MODE_INPUT, GPIO_MODE_OUTPUT };
6+
enum { LOW, HIGH };
7+
8+
struct gpio {
9+
volatile uint32_t RESERVED[321], OUT, OUTSET, OUTCLR, IN, DIR, DIRSET, DIRCLR, LATCH, DETECTMODE, PIN_CNF[32];
10+
};
11+
12+
void set_gpio_mode(struct gpio * port, int pin, int mode) {
13+
port->DIR |= mode << pin;
14+
}
15+
16+
void gpio_write(struct gpio * port, int pin, int value) {
17+
if (value == 1)
18+
port->OUTSET |= 1 << pin;
19+
else
20+
port->OUTCLR |= 1 << pin;
21+
}
22+
23+
static inline void spin(volatile uint32_t count) {
24+
while (count--) (void) 0;
25+
}

templates/blinky/nRF52840dk/link.ld

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
ENTRY(_reset);
2+
3+
MEMORY {
4+
flash(rx) : ORIGIN = 0x00000000, LENGTH = 1M
5+
sram(rwx) : ORIGIN = 0x20000000, LENGTH = 256k
6+
}
7+
_estack = ORIGIN(sram) + LENGTH(sram);
8+
9+
SECTIONS {
10+
.vectors : { KEEP(*(.vectors)) } > flash
11+
.text : { *(.text*) } > flash
12+
.rodata : { *(.rodata*) } > flash
13+
14+
.data : {
15+
_sdata = .; /* .data section start */
16+
*(.first_data)
17+
*(.data SORT(.data.*))
18+
_edata = .; /* .data section end */
19+
} > sram AT > flash
20+
_sidata = LOADADDR(.data);
21+
22+
.bss : {
23+
_sbss = .; /* .bss section start */
24+
*(.bss SORT(.bss.*) COMMON)
25+
_ebss = .; /* .bss section end */
26+
} > sram
27+
28+
. = ALIGN(8);
29+
_end = .; /* for cmsis_gcc.h */
30+
}

templates/blinky/nRF52840dk/main.c

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <stdint.h>
2+
#include "hal.h"
3+
4+
int main(void) {
5+
set_gpio_mode(P0, BUILD_IN_LED_1, GPIO_MODE_OUTPUT);
6+
7+
while (1) {
8+
gpio_write(P0, BUILD_IN_LED_1, HIGH);
9+
spin(9999999);
10+
gpio_write(P0, BUILD_IN_LED_1, LOW);
11+
spin(9999999);
12+
}
13+
}
14+
15+
// Startup code
16+
__attribute__((naked, noreturn)) void _reset(void) {
17+
// memset .bss to zero, and copy .data section to RAM region
18+
extern long _sbss, _ebss, _sdata, _edata, _sidata;
19+
for (long *dst = &_sbss; dst < &_ebss; dst++) *dst = 0;
20+
for (long *dst = &_sdata, *src = &_sidata; dst < &_edata;) *dst++ = *src++;
21+
22+
main(); // Call main()
23+
for (;;) (void) 0; // Infinite loop in the case if main() returns
24+
}
25+
26+
extern void _estack(void); // Defined in link.ld
27+
28+
// 16 standard and 42 nRF52-specific handlers
29+
__attribute__((section(".vectors"))) void (*const tab[16 + 42])(void) = {
30+
_estack, _reset};
31+

0 commit comments

Comments
 (0)