Skip to content

Commit 76e0195

Browse files
committed
nrf nits
1 parent ea476de commit 76e0195

File tree

4 files changed

+59
-46
lines changed

4 files changed

+59
-46
lines changed

templates/blinky/nRF52840dk/Makefile

+13-5
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
11
CFLAGS ?= -W -Wall -Wextra -Werror -Wundef -Wshadow -Wdouble-promotion -Wformat-truncation -fno-common
2-
CFLAGS += -g3 -O0 -ffunction-sections -fdata-sections -Wno-shadow
2+
CFLAGS += -g3 -Os -ffunction-sections -fdata-sections -Wno-shadow
33
CFLAGS += -mcpu=cortex-m4 -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16
44
LDFLAGS ?= -Tlink.ld -nostartfiles -nostdlib --specs nano.specs -lc -lgcc -Wl,--gc-sections
5-
SOURCES = main.c
65

6+
SOURCES = main.c
77

8-
firmware.elf: hal.h link.ld Makefile $(SOURCES)
8+
firmware.elf: hal.h link.ld Makefile $(SOURCES) #cmsis_core cmsis_nrf
99
arm-none-eabi-gcc $(SOURCES) $(CFLAGS) $(CFLAGS_EXTRA) $(LDFLAGS) -o $@
10+
arm-none-eabi-size $@
1011

11-
firwmare.hex:
12-
arm-none-eabi-objcopy -Oihex firmware.elf firmware.hex
12+
firmware.bin firmware.hex: firmware.elf
13+
arm-none-eabi-objcopy -O ihex firmware.elf firmware.hex
14+
arm-none-eabi-objcopy -O binary firmware.elf firmware.bin
1315

1416
flash:
1517
nrfjprog -f NRF52 --program firmware.hex --recover --verify
1618
nrfjprog -f NRF52 --reset
1719

20+
cmsis_core:
21+
git clone -c advice.detachedHead=false -q --depth 1 -b 5.9.0 https://github.com/ARM-software/CMSIS_5 $@
22+
23+
cmsis_nrf:
24+
curl -sL https://developer.nordicsemi.com/nRF5_SDK/pieces/nRF_DeviceFamilyPack/NordicSemiconductor.nRF_DeviceFamilyPack.8.44.1.pack -o x.zip && mkdir -p $@ && (cd $@ && unzip -q ../x.zip && rm -f ../x.zip)
25+
1826
clean:
1927
rm -rf firmware.*

templates/blinky/nRF52840dk/hal.h

+35-29
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,44 @@
1-
#define P0 ((struct gpio *) 0x50000000)
2-
#define P1 ((struct gpio *) 0x50000300)
1+
#pragma once
32

4-
enum { GPIO_MODE_INPUT, GPIO_MODE_OUTPUT };
5-
enum { LOW, HIGH };
3+
#include <stdbool.h>
4+
#include <stdint.h>
65

7-
struct gpio {
8-
volatile uint32_t RESERVED[321], OUT, OUTSET, OUTCLR, IN, DIR, DIRSET, DIRCLR,
9-
LATCH, DETECTMODE, RESERVED_SECOND[118], PIN_CNF[32];
10-
};
6+
#include "nrf52.h"
117

12-
void set_gpio_mode(struct gpio *port, int pin, int mode, int pull) {
13-
if (pull == 0) {
14-
port->PIN_CNF[pin] = mode << 0;
15-
} else {
16-
port->PIN_CNF[pin] = mode << 0 | pull << 2;
17-
}
8+
#define BIT(x) (1UL << (x))
9+
#define CLRSET(R, CLEARMASK, SETMASK) (R) = ((R) & ~(CLEARMASK)) | (SETMASK)
10+
#define PIN(bank, num) ((((bank) - 'A') << 8) | (num))
11+
#define PINNO(pin) (pin & 255)
12+
#define PINBANK(pin) (pin >> 8)
13+
14+
static inline void spin(volatile uint32_t count) {
15+
while (count--) (void) 0;
1816
}
1917

20-
int gpio_read(struct gpio *port, int pin) {
21-
uint32_t button = ~(port->IN);
22-
if (button & (1 << pin)) {
23-
return 1;
24-
} else {
25-
return 0;
26-
}
18+
#define GPIO(N) ((NRF_GPIO_Type *) ((NRF_P0_BASE) + 0x300 * (N)))
19+
20+
static inline NRF_GPIO_Type *gpio_bank(uint16_t pin) {
21+
return GPIO(PINBANK(pin));
2722
}
28-
void gpio_write(struct gpio *port, int pin, int value) {
29-
if (value == 0) {
30-
port->OUTSET = 1 << pin;
31-
} else {
32-
port->OUTCLR = 1 << pin;
33-
}
23+
24+
enum { GPIO_MODE_INPUT = 0, GPIO_MODE_OUTPUT = 1 };
25+
enum { GPIO_PULL_NONE = 0, GPIO_PULL_DOWN = 1, GPIO_PULL_UP = 3U };
26+
27+
void gpio_init(uint16_t pin, uint8_t mode, uint8_t pull) {
28+
NRF_GPIO_Type *gpio = gpio_bank(pin);
29+
gpio->PIN_CNF[pin] = (mode & 1U) << 0 | (pull & 3U) << 2U;
3430
}
3531

36-
static inline void spin(volatile uint32_t count) {
37-
while (count--) (void) 0;
32+
static inline bool gpio_read(uint16_t pin) {
33+
NRF_GPIO_Type *gpio = gpio_bank(pin);
34+
return ~(gpio->IN) & BIT(PINNO(pin));
35+
}
36+
37+
static inline void gpio_write(uint16_t pin, bool value) {
38+
NRF_GPIO_Type *gpio = gpio_bank(pin);
39+
if (value) {
40+
gpio->OUTSET = BIT(PINNO(pin));
41+
} else {
42+
gpio->OUTCLR = BIT(PINNO(pin));
43+
}
3844
}

templates/blinky/nRF52840dk/link.ld

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
ENTRY(Reset_Handler);
22
MEMORY {
3-
flash(rx) : ORIGIN = 0x00000000, LENGTH = 1M
4-
sram(rwx) : ORIGIN = 0x20000000, LENGTH = 256k
3+
flash(rx) : ORIGIN = 0x00000000, LENGTH = 512k
4+
sram(rwx) : ORIGIN = 0x20000000, LENGTH = 64k
55
}
66
_estack = ORIGIN(sram) + LENGTH(sram);
77

templates/blinky/nRF52840dk/main.c

+9-10
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
1-
#include <stdint.h>
21
#include "hal.h"
32

4-
#define LED1 19
3+
#define LED1 PIN('A', 17)
54

65
int main(void) {
7-
set_gpio_mode(P0, LED1, GPIO_MODE_OUTPUT, 0);
6+
gpio_init(LED1, GPIO_MODE_OUTPUT, 0);
87

9-
int level = 0;
10-
while (1) {
11-
gpio_write(P0, LED1, level);
8+
for (;;) {
9+
gpio_write(LED1, true);
10+
spin(9999999);
11+
gpio_write(LED1, false);
1212
spin(9999999);
13-
level = !level;
1413
}
1514
}
1615

1716
// Startup code
1817
__attribute__((naked, noreturn)) void Reset_Handler(void) {
1918
// memset .bss to zero, and copy .data section to RAM region
20-
extern long _sbss, _ebss, _sdata, _edata, _sidata;
21-
for (long *dst = &_sbss; dst < &_ebss; dst++) *dst = 0;
22-
for (long *dst = &_sdata, *src = &_sidata; dst < &_edata;) *dst++ = *src++;
19+
extern uint32_t _sbss, _ebss, _sdata, _edata, _sidata;
20+
for (uint32_t *dst = &_sbss; dst < &_ebss; dst++) *dst = 0;
21+
for (uint32_t *dst = &_sdata, *src = &_sidata; dst < &_edata;) *dst++ = *src++;
2322

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

0 commit comments

Comments
 (0)