-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_gpio_timer.c
137 lines (109 loc) · 4.48 KB
/
test_gpio_timer.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include "gpio.h"
#include "timer.h"
// You call assert on an expression that you expect to be true. If expr
// actually evaluates to false, then assert calls abort, which stops
// your program and flashes the red light of doom.
#define assert(expr) if(!(expr)) abort()
// infinite loop that flashes the red power LED (GPIO 35)
void abort(void) {
volatile unsigned int *FSEL3 = (void *)0x2020000c;
volatile unsigned int *SET1 = (void *)0x20200020;
volatile unsigned int *CLR1 = (void *)0x2020002c;
volatile int delay; // volatile counter to prevent optimize out of delay loop
int bit35 = 1 << 3;
// Configure GPIO 35 function to be output.
// This wipes functions for other gpios in this register (30-39)
// but that's okay, because this is a dead-end routine.
*FSEL3 = 1 << 15;
while (1) { // infinite loop
*SET1 = bit35; // on
for (delay = 0x100000; delay > 0; delay--) ;
*CLR1 = bit35; // off
for (delay = 0x100000; delay > 0; delay--) ;
}
}
void test_gpio_set_get_function(void) {
gpio_init();
// Test get pin function (pin 2 defaults to input)
assert( gpio_get_function(GPIO_PIN2) == GPIO_FUNC_INPUT );
// Set pin 2 to output, confirm get returns what was set
gpio_set_output(GPIO_PIN2);
assert( gpio_get_function(GPIO_PIN2) == GPIO_FUNC_OUTPUT );
// Set pin 2 back to input, confirm get returns what was set
gpio_set_input(GPIO_PIN2);
assert( gpio_get_function(GPIO_PIN2) == GPIO_FUNC_INPUT );
// Set pin 34 back to input, confirm get returns what was set
gpio_set_input(GPIO_PIN34);
assert( gpio_get_function(GPIO_PIN34) == GPIO_FUNC_INPUT );
// Set pin 45 back to output, confirm get returns what was set
gpio_set_output(GPIO_PIN45);
assert( gpio_get_function(GPIO_PIN45) == GPIO_FUNC_OUTPUT);
}
void test_gpio_read_write(void) {
gpio_init();
// set pin 20 to output before gpio_write
gpio_set_function(GPIO_PIN20, GPIO_FUNC_OUTPUT);
// gpio_write low, confirm gpio_read reads what was written
gpio_write(GPIO_PIN20, 0);
assert( gpio_read(GPIO_PIN20) == 0 );
// gpio_write high, confirm gpio_read reads what was written
gpio_write(GPIO_PIN20, 1);
assert( gpio_read(GPIO_PIN20) == 1 );
// gpio_write low, confirm gpio_read reads what was written
gpio_write(GPIO_PIN20, 0);
assert( gpio_read(GPIO_PIN20) == 0 );
// gpio_write low, confirm gpio_read reads what was written (using a GPIO greater than 32)
gpio_write(GPIO_PIN34, 0);
assert( gpio_read(GPIO_PIN34) == 0 );
// gpio_write high, confirm gpio_read reads what was written (using a GPIO greater than 32 again)
gpio_write(GPIO_PIN39, 1);
assert( gpio_read(GPIO_PIN39) == 1);
}
void test_timer(void) {
timer_init();
// Test timer tick count incrementing
unsigned int start = timer_get_ticks();
for( int i=0; i<10; i++ ) { /* Spin */ }
unsigned int finish = timer_get_ticks();
assert( finish > start );
// Test timer delay
int usecs = 100;
start = timer_get_ticks();
timer_delay_us(usecs);
finish = timer_get_ticks();
assert( finish >= start + usecs );
}
void test_breadboard(void) {
unsigned int segment[8] = {GPIO_PIN26, GPIO_PIN19, GPIO_PIN13, GPIO_PIN6,
GPIO_PIN5, GPIO_PIN11, GPIO_PIN9, GPIO_PIN10};
unsigned int digit[4] = {GPIO_PIN21, GPIO_PIN20, GPIO_PIN16, GPIO_PIN12};
unsigned int button = GPIO_PIN2;
gpio_init();
for (int i = 0; i < 8; i++) { // configure segments
gpio_set_output(segment[i]);
}
for (int i = 0; i < 4; i++) { // configure digits
gpio_set_output(digit[i]);
}
gpio_set_input(button); // configure button
while (1) { // loop forever (finish via button press, see below)
for (int i = 0; i < 4; i++) { // iterate over digits
gpio_write(digit[i], 1); // turn on digit
for (int j = 0; j < 8; j++) { // iterate over segments
if (gpio_read(button) == 0) return; // read button, exit if button pressed
gpio_write(segment[j], 1); // turn on segment
timer_delay_ms(200);
gpio_write(segment[j], 0); // turn off segment
}
gpio_write(digit[i], 0); // turn off digit
}
}
}
// Uncomment each call below when you have implemented the functions
// and are ready to test them
void main(void) {
test_gpio_set_get_function();
test_gpio_read_write();
test_timer();
test_breadboard();
}