Skip to content

Commit 4e81d44

Browse files
MaksymShutiakcpq
authored andcommitted
bugfix: gpio struct wrong mapping, refactor of hal methods
1 parent d44bdaf commit 4e81d44

File tree

2 files changed

+44
-11
lines changed

2 files changed

+44
-11
lines changed

templates/blinky/nRF52840dk/hal.h

+32-8
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,47 @@
11
#define P0 ((struct gpio *) 0x50000000)
22
#define P1 ((struct gpio *) 0x50000300)
3-
#define BUILD_IN_LED_1 13
3+
4+
#define BUILD_IN_BUTTON_4_PIN 25
5+
#define BUILD_IN_BUTTON_3_PIN 24
6+
#define BUILD_IN_BUTTON_2_PIN 12
7+
#define BUILD_IN_BUTTON_1_PIN 11
8+
#define BUILD_IN_LED_1_PIN 13
9+
#define BUILD_IN_LED_2_PIN 14
10+
#define BUILD_IN_LED_3_PIN 15
11+
#define BUILD_IN_LED_4_PIN 16
412

513
enum { GPIO_MODE_INPUT, GPIO_MODE_OUTPUT };
614
enum { LOW, HIGH };
715

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

12-
void set_gpio_mode(struct gpio * port, int pin, int mode) {
13-
port->DIR |= mode << pin;
20+
void set_gpio_mode(struct gpio * port, int pin, int mode, int pull) {
21+
if (pull == 0) {
22+
port->PIN_CNF[pin] = mode << 0;
23+
}
24+
else {
25+
port->PIN_CNF[pin] = mode << 0 | pull << 2;
26+
}
1427
}
1528

29+
int gpio_read(struct gpio * port, int pin) {
30+
uint32_t button = ~(port->IN);
31+
if (button & (1 << pin)) {
32+
return 1;
33+
}
34+
else {
35+
return 0;
36+
}
37+
}
1638
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;
39+
if (value == 0) {
40+
port->OUTSET = 1 << pin;
41+
}
42+
else {
43+
port->OUTCLR = 1 << pin;
44+
}
2145
}
2246

2347
static inline void spin(volatile uint32_t count) {

templates/blinky/nRF52840dk/main.c

+12-3
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,21 @@
22
#include "hal.h"
33

44
int main(void) {
5-
set_gpio_mode(P0, BUILD_IN_LED_1, GPIO_MODE_OUTPUT);
5+
set_gpio_mode(P0, BUILD_IN_LED_1_PIN, GPIO_MODE_OUTPUT, 0);
6+
set_gpio_mode(P0, BUILD_IN_LED_2_PIN, GPIO_MODE_OUTPUT, 0);
7+
set_gpio_mode(P0, BUILD_IN_LED_3_PIN, GPIO_MODE_OUTPUT, 0);
8+
set_gpio_mode(P0, BUILD_IN_LED_4_PIN, GPIO_MODE_OUTPUT, 0);
69

710
while (1) {
8-
gpio_write(P0, BUILD_IN_LED_1, HIGH);
11+
gpio_write(P0, BUILD_IN_LED_1_PIN, HIGH);
12+
gpio_write(P0, BUILD_IN_LED_2_PIN, HIGH);
13+
gpio_write(P0, BUILD_IN_LED_3_PIN, HIGH);
14+
gpio_write(P0, BUILD_IN_LED_4_PIN, HIGH);
915
spin(9999999);
10-
gpio_write(P0, BUILD_IN_LED_1, LOW);
16+
gpio_write(P0, BUILD_IN_LED_4_PIN, LOW);
17+
gpio_write(P0, BUILD_IN_LED_3_PIN, LOW);
18+
gpio_write(P0, BUILD_IN_LED_2_PIN, LOW);
19+
gpio_write(P0, BUILD_IN_LED_1_PIN, LOW);
1120
spin(9999999);
1221
}
1322
}

0 commit comments

Comments
 (0)