Skip to content

Commit ea476de

Browse files
committed
refactor nrf code
1 parent 35c8cc5 commit ea476de

File tree

4 files changed

+35
-52
lines changed

4 files changed

+35
-52
lines changed
Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
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
2+
CFLAGS += -g3 -O0 -ffunction-sections -fdata-sections -Wno-shadow
3+
CFLAGS += -mcpu=cortex-m4 -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16
4+
LDFLAGS ?= -Tlink.ld -nostartfiles -nostdlib --specs nano.specs -lc -lgcc -Wl,--gc-sections
45
SOURCES = main.c
56

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
7+
8+
firmware.elf: hal.h link.ld Makefile $(SOURCES)
9+
arm-none-eabi-gcc $(SOURCES) $(CFLAGS) $(CFLAGS_EXTRA) $(LDFLAGS) -o $@
10+
11+
firwmare.hex:
12+
arm-none-eabi-objcopy -Oihex firmware.elf firmware.hex
1113

1214
flash:
13-
nrfjprog -f NRF52 --program ./build/firmware.hex --recover --verify
15+
nrfjprog -f NRF52 --program firmware.hex --recover --verify
1416
nrfjprog -f NRF52 --reset
1517

1618
clean:
17-
rm -rf ./build
19+
rm -rf firmware.*

templates/blinky/nRF52840dk/hal.h

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,34 @@
11
#define P0 ((struct gpio *) 0x50000000)
22
#define P1 ((struct gpio *) 0x50000300)
33

4-
#define BUILT_IN_BUTTON_4_PIN 25
5-
#define BUILT_IN_BUTTON_3_PIN 24
6-
#define BUILT_IN_BUTTON_2_PIN 12
7-
#define BUILT_IN_BUTTON_1_PIN 11
8-
#define BUILT_IN_LED_1_PIN 13
9-
#define BUILT_IN_LED_2_PIN 14
10-
#define BUILT_IN_LED_3_PIN 15
11-
#define BUILT_IN_LED_4_PIN 16
12-
134
enum { GPIO_MODE_INPUT, GPIO_MODE_OUTPUT };
145
enum { LOW, HIGH };
156

167
struct gpio {
17-
volatile uint32_t RESERVED[321], OUT, OUTSET, OUTCLR, IN, DIR, DIRSET, DIRCLR, LATCH, DETECTMODE, RESERVED_SECOND[118], PIN_CNF[32];
8+
volatile uint32_t RESERVED[321], OUT, OUTSET, OUTCLR, IN, DIR, DIRSET, DIRCLR,
9+
LATCH, DETECTMODE, RESERVED_SECOND[118], PIN_CNF[32];
1810
};
1911

20-
void set_gpio_mode(struct gpio * port, int pin, int mode, int pull) {
12+
void set_gpio_mode(struct gpio *port, int pin, int mode, int pull) {
2113
if (pull == 0) {
2214
port->PIN_CNF[pin] = mode << 0;
23-
}
24-
else {
15+
} else {
2516
port->PIN_CNF[pin] = mode << 0 | pull << 2;
2617
}
2718
}
2819

29-
int gpio_read(struct gpio * port, int pin) {
20+
int gpio_read(struct gpio *port, int pin) {
3021
uint32_t button = ~(port->IN);
3122
if (button & (1 << pin)) {
3223
return 1;
33-
}
34-
else {
24+
} else {
3525
return 0;
3626
}
3727
}
38-
void gpio_write(struct gpio * port, int pin, int value) {
28+
void gpio_write(struct gpio *port, int pin, int value) {
3929
if (value == 0) {
4030
port->OUTSET = 1 << pin;
41-
}
42-
else {
31+
} else {
4332
port->OUTCLR = 1 << pin;
4433
}
4534
}
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
ENTRY(_reset);
2-
1+
ENTRY(Reset_Handler);
32
MEMORY {
4-
flash(rx) : ORIGIN = 0x00000000, LENGTH = 1M
3+
flash(rx) : ORIGIN = 0x00000000, LENGTH = 1M
54
sram(rwx) : ORIGIN = 0x20000000, LENGTH = 256k
65
}
76
_estack = ORIGIN(sram) + LENGTH(sram);
@@ -12,19 +11,20 @@ SECTIONS {
1211
.rodata : { *(.rodata*) } > flash
1312

1413
.data : {
15-
_sdata = .; /* .data section start */
14+
_sdata = .;
1615
*(.first_data)
1716
*(.data SORT(.data.*))
18-
_edata = .; /* .data section end */
17+
*(.iram .iram* .iram.*)
18+
_edata = .;
1919
} > sram AT > flash
2020
_sidata = LOADADDR(.data);
2121

2222
.bss : {
23-
_sbss = .; /* .bss section start */
23+
_sbss = .;
2424
*(.bss SORT(.bss.*) COMMON)
25-
_ebss = .; /* .bss section end */
25+
_ebss = .;
2626
} > sram
2727

2828
. = ALIGN(8);
29-
_end = .; /* for cmsis_gcc.h */
29+
_end = .;
3030
}

templates/blinky/nRF52840dk/main.c

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,21 @@
11
#include <stdint.h>
22
#include "hal.h"
33

4+
#define LED1 19
5+
46
int main(void) {
5-
set_gpio_mode(P0, BUILT_IN_LED_1_PIN, GPIO_MODE_OUTPUT, 0);
6-
set_gpio_mode(P0, BUILT_IN_LED_2_PIN, GPIO_MODE_OUTPUT, 0);
7-
set_gpio_mode(P0, BUILT_IN_LED_3_PIN, GPIO_MODE_OUTPUT, 0);
8-
set_gpio_mode(P0, BUILT_IN_LED_4_PIN, GPIO_MODE_OUTPUT, 0);
7+
set_gpio_mode(P0, LED1, GPIO_MODE_OUTPUT, 0);
98

9+
int level = 0;
1010
while (1) {
11-
gpio_write(P0, BUILT_IN_LED_1_PIN, HIGH);
12-
gpio_write(P0, BUILT_IN_LED_2_PIN, HIGH);
13-
gpio_write(P0, BUILT_IN_LED_3_PIN, HIGH);
14-
gpio_write(P0, BUILT_IN_LED_4_PIN, HIGH);
15-
spin(9999999);
16-
gpio_write(P0, BUILT_IN_LED_4_PIN, LOW);
17-
gpio_write(P0, BUILT_IN_LED_3_PIN, LOW);
18-
gpio_write(P0, BUILT_IN_LED_2_PIN, LOW);
19-
gpio_write(P0, BUILT_IN_LED_1_PIN, LOW);
11+
gpio_write(P0, LED1, level);
2012
spin(9999999);
13+
level = !level;
2114
}
2215
}
2316

2417
// Startup code
25-
__attribute__((naked, noreturn)) void _reset(void) {
18+
__attribute__((naked, noreturn)) void Reset_Handler(void) {
2619
// memset .bss to zero, and copy .data section to RAM region
2720
extern long _sbss, _ebss, _sdata, _edata, _sidata;
2821
for (long *dst = &_sbss; dst < &_ebss; dst++) *dst = 0;
@@ -36,5 +29,4 @@ extern void _estack(void); // Defined in link.ld
3629

3730
// 16 standard and 42 nRF-specific handlers
3831
__attribute__((section(".vectors"))) void (*const tab[16 + 42])(void) = {
39-
_estack, _reset};
40-
32+
_estack, Reset_Handler};

0 commit comments

Comments
 (0)