-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathports.c
33 lines (30 loc) · 921 Bytes
/
ports.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include "ports.h"
#include "ccu.h"
void gpio_init() {
BUS_CLK_GATING2 |= (1<<5);
APB0_CLK_GATING |= (1<<0);
}
void set_pin_mode(uint32_t port_addr, uint32_t pin, uint32_t mode) {
struct port_registers * port = (struct port_registers *)port_addr;
if(pin < 8) {
port->cfg0 &= ~(7 << ((pin - 0) * 4));
port->cfg0 |= (mode << ((pin - 0) * 4));
} else if(pin < 16) {
port->cfg1 &= ~(7 << ((pin - 8) * 4));
port->cfg1 |= (mode << ((pin - 8) * 4));
} else if(pin < 24) {
port->cfg2 &= ~(7 << ((pin - 16) * 4));
port->cfg2 |= (mode << ((pin - 16) * 4));
} else {
port->cfg3 &= ~(7 << ((pin - 24) * 4));
port->cfg3 |= (mode << ((pin - 24) * 4));
}
}
void set_pin_data(uint32_t port_addr, uint32_t pin, uint32_t data) {
struct port_registers * port = (struct port_registers *)port_addr;
if(data) {
port->data |= (1 << pin);
} else {
port->data &= ~(1 << pin);
}
}