-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
72 lines (60 loc) · 1.6 KB
/
main.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
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
#include "gpio.h"
#include "uart.h"
#define CHECK_ERROR(expr) \
{ \
int res = expr; \
if (res != 0) { \
return res; \
} \
}
// Sets up a uart connection over the pins
// 2 (rx) and 3 (tx).
inline int setup_uart(struct uart* uart) {
struct input_pin pin_2;
pin_2.port = 0;
pin_2.pin = 2;
gpio_input_init(&pin_2);
struct output_pin pin_3;
pin_3.port = 0;
pin_3.pin = 3;
gpio_output_init(&pin_3);
uart->rx = pin_2;
uart->tx = pin_3;
return init_uart(uart);
}
// the main function that is exported by the module.
int start() {
// setup pin 10 as input
struct input_pin pin_10;
pin_10.port = 0;
pin_10.pin = 10;
CHECK_ERROR(gpio_input_init(&pin_10));
// setup pin 8 (LED) as output
struct output_pin pin_8;
pin_8.port = 0;
pin_8.pin = 8;
CHECK_ERROR(gpio_output_init(&pin_8));
// setup a uart connection
struct uart uart;
CHECK_ERROR(setup_uart(&uart));
while (1) {
// turn the LED on
CHECK_ERROR(set_high(&pin_8));
// read the value of pin 10 and
// write it to the console and over uart
bool val;
CHECK_ERROR(is_high(&pin_10, &val));
if (val) {
print("val_10 is hi", 13);
} else {
print("val_10 is lo", 13);
}
write(&uart, (unsigned char)val);
// wait for a second
delay_ms(1000);
// turn the led off
CHECK_ERROR(set_low(&pin_8));
// wait for another second
delay_ms(1000);
}
}