-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
executable file
·84 lines (67 loc) · 2.89 KB
/
main.cpp
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include "device.h"
#include <cstdint>
enum gpioPullMode : uint8_t { GPIO_PULL_OFF, GPIO_PULL_UP, GPIO_PULL_DOWN };
enum gpioDirection : uint8_t { GPIO_DIRECTION_OFF, GPIO_DIRECTION_IN, GPIO_DIRECTION_OUT };
enum gpioPort : uint8_t { GPIO_PORTA = 0, GPIO_PORTB = 1, GPIO_PORTC = 2, GPIO_PORTD = 3, GPIO_PORTE = 4 };
enum gpioFunc : uint8_t { GPIO_FUNC_A, GPIO_FUNC_B, GPIO_FUNC_C, GPIO_FUNC_D, GPIO_FUNC_E, GPIO_FUNC_F, GPIO_FUNC_G, GPIO_FUNC_H, GPIO_FUNC_I };
constexpr uint32_t GPIO_PIN_FUNCTION_OFF = 0xffffffffu;
constexpr uint8_t definePin(const enum gpioPort port, const uint8_t pin) noexcept {
return ((static_cast<uint8_t>(port) & 0x07U) << 5U) + (pin & 0x1FU); // binary compatable with atmel HAL
}
constexpr gpioPort getPort(const uint8_t pin) noexcept {
return static_cast<gpioPort>(pin >> 5u);
}
constexpr uint8_t getPin(const uint8_t pin) noexcept {
return pin & 0x1Fu;
}
constexpr uint8_t LED_BLUE = definePin(GPIO_PORTA, 11);
constexpr uint8_t LED_RED = definePin(GPIO_PORTB, 8);
constexpr uint8_t LED_GREEN = definePin(GPIO_PORTB, 9);
void gpio_set_pin_direction(const uint8_t pin, const gpioDirection direction) {
const gpioPort port = getPort(pin);
const uint32_t mask = 1U << getPin(pin);
switch (direction) {
case GPIO_DIRECTION_OFF:
PORT->Group[port].DIRCLR.reg = mask;
PORT->Group[port].WRCONFIG.reg = PORT_WRCONFIG_WRPINCFG | (mask & 0xffff);
PORT->Group[port].WRCONFIG.reg = PORT_WRCONFIG_HWSEL | PORT_WRCONFIG_WRPINCFG | ((mask & 0xffff0000) >> 16);
break;
case GPIO_DIRECTION_IN:
PORT->Group[port].DIRCLR.reg = mask;
PORT->Group[port].WRCONFIG.reg = PORT_WRCONFIG_WRPINCFG | PORT_WRCONFIG_INEN | (mask & 0xffff);
PORT->Group[port].WRCONFIG.reg = PORT_WRCONFIG_HWSEL | PORT_WRCONFIG_WRPINCFG | PORT_WRCONFIG_INEN | ((mask & 0xffff0000) >> 16);
break;
case GPIO_DIRECTION_OUT:
PORT->Group[port].DIRSET.reg = mask;
PORT->Group[port].WRCONFIG.reg = PORT_WRCONFIG_WRPINCFG | (mask & 0xffff);
PORT->Group[port].WRCONFIG.reg = PORT_WRCONFIG_HWSEL | PORT_WRCONFIG_WRPINCFG | ((mask & 0xffff0000) >> 16);
break;
}
}
void gpio_toggle_pin_level(const uint8_t pin) {
const gpioPort port = getPort(pin);
const uint32_t mask = 1U << getPin(pin);
PORT->Group[port].OUTTGL.reg = mask;
}
void delayMS(uint32_t t) {
t *= 1000; // hardcoded to handle the default CPU frequency
while(t-- > 0) {
// NOOP for delay
asm volatile ("");
}
}
int main() {
SystemCoreClockUpdate();
gpio_set_pin_direction(LED_BLUE, GPIO_DIRECTION_OUT);
gpio_set_pin_direction(LED_GREEN, GPIO_DIRECTION_OUT);
gpio_set_pin_direction(LED_RED, GPIO_DIRECTION_OUT);
int delay = 250;
while(1) {
gpio_toggle_pin_level(LED_BLUE);
delayMS(delay);
gpio_toggle_pin_level(LED_GREEN);
delayMS(delay);
gpio_toggle_pin_level(LED_RED);
delayMS(delay);
}
}