forked from palmleon/EFESLabs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lab3.c
223 lines (211 loc) · 8.11 KB
/
Lab3.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
* "Hello World" example.
*
* This example prints 'Hello from Nios II' to the STDOUT stream. It runs on
* the Nios II 'standard', 'full_featured', 'fast', and 'low_cost' example
* designs. It runs with or without the MicroC/OS-II RTOS and requires a STDOUT
* device in your system's hardware.
* The memory footprint of this hosted application is ~69 kbytes by default
* using the standard reference design.
*
* For a reduced footprint version of this template, and an explanation of how
* to reduce the memory footprint for a given application, see the
* "small_hello_world" template.
*
*/
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "system.h"
#include "sys/alt_timestamp.h"
#include "altera_avalon_uart_regs.h"
#include "altera_avalon_pio_regs.h"
#define RXDATA_OFFSET 0
#define TXDATA_OFFSET 1
#define STATUS_OFFSET 2
#define CONTROL_OFFSET 3
#define DIVISOR_OFFSET 4
#define PROJECT4
#ifdef PROJECT1
int main()
{
int *base = UART_0_BASE;
int RXDATA, TXDATA, STATUS, CONTROL, DIVISOR;
RXDATA = *(base + RXDATA_OFFSET);
TXDATA = *(base + TXDATA_OFFSET);
STATUS = *(base + STATUS_OFFSET);
CONTROL = *(base + CONTROL_OFFSET);
DIVISOR = *(base + DIVISOR_OFFSET);
//RXDATA = IORD_ALTERA_AVALON_UART_RXDATA(UART_0_BASE);
//TXDATA = IORD_ALTERA_AVALON_UART_TXDATA(UART_0_BASE);
//STATUS = IORD_ALTERA_AVALON_UART_STATUS(UART_0_BASE);
//CONTROL = IORD_ALTERA_AVALON_UART_CONTROL(UART_0_BASE);
//DIVISOR = IORD_ALTERA_AVALON_UART_DIVISOR(UART_0_BASE);
printf("Rxdata = %X\n", RXDATA);
printf("Txdata = %X\n", TXDATA);
printf("Status = %X\n", STATUS);
printf("Control = %X\n", CONTROL);
printf("Divisor = %X\n", DIVISOR);
return 0;
}
#endif
#ifdef PROJECT2
int main(){
int *base = UART_0_BASE;
int new_divisor = 50000000/2400 - 1;
int read_divisor;
//IOWR_ALTERA_AVALON_UART_DIVISOR(UART_0_BASE, new_divisor);
*(base + DIVISOR_OFFSET) = new_divisor;
//read_divisor = IORD_ALTERA_AVALON_UART_DIVISOR(UART_0_BASE);
read_divisor = *(base + DIVISOR_OFFSET);
printf("Divisor after writing = %d\n", read_divisor);
printf("Expected divisor = %d\n", new_divisor);
return 0;
}
#endif
#ifdef PROJECT31
int main(){
int *base = UART_0_BASE;
int old_status, new_status;
int new_divisor = 50000000/2400 - 1;
*(base + DIVISOR_OFFSET) = new_divisor;
//IOWR_ALTERA_AVALON_UART_DIVISOR(UART_0_BASE, new_divisor);
old_status = *(base + STATUS_OFFSET);
//old_status = IORD_ALTERA_AVALON_UART_STATUS(UART_0_BASE);
printf("Status Reg before reading: 0x%X\n", old_status); //Status Reg = 0x60
//IOWR_ALTERA_AVALON_UART_TXDATA(UART_0_BASE, 'A');
*(base + TXDATA_OFFSET) = 'A';
//new_status = IORD_ALTERA_AVALON_UART_STATUS(UART_0_BASE);
new_status = *(base + STATUS_OFFSET);
printf("Status Reg after reading: 0x%X", new_status); //Status Reg = 0x40
return 0;
}
#endif
#ifdef PROJECT32
int main(){
int *base = UART_0_BASE;
int old_status, new_status;
int new_divisor = 50000000/2400 - 1;
//IOWR_ALTERA_AVALON_UART_DIVISOR(UART_0_BASE, new_divisor);
*(base + DIVISOR_OFFSET) = new_divisor;
//old_status = IORD_ALTERA_AVALON_UART_STATUS(UART_0_BASE);
old_status = *(base + STATUS_OFFSET);
printf("Status Reg before reading: 0x%X\n", old_status); //Status Reg = 0x60
//IOWR_ALTERA_AVALON_UART_TXDATA(UART_0_BASE, 'A');
*(base + TXDATA_OFFSET) = 'A';
//IOWR_ALTERA_AVALON_UART_TXDATA(UART_0_BASE, 'B');
*(base + TXDATA_OFFSET) = 'B';
//new_status = IORD_ALTERA_AVALON_UART_STATUS(UART_0_BASE); //Status Reg = 0x0
new_status = *(base + STATUS_OFFSET);
printf("Status Reg after reading: 0x%X\n", new_status);
return 0;
}
#endif
#ifdef PROJECT33
int main(){
int *base = UART_0_BASE;
int old_status, new_status;
int new_divisor = 50000000/2400 - 1;
//IOWR_ALTERA_AVALON_UART_DIVISOR(UART_0_BASE, new_divisor);
*(base + DIVISOR_OFFSET) = new_divisor;
//old_status = IORD_ALTERA_AVALON_UART_STATUS(UART_0_BASE);
old_status = *(base + STATUS_OFFSET);
printf("Status Reg before reading: 0x%x\n", old_status); //Status Reg = 0x60
//IOWR_ALTERA_AVALON_UART_TXDATA(UART_0_BASE, 'A');
*(base + TXDATA_OFFSET) = 'A';
//IOWR_ALTERA_AVALON_UART_TXDATA(UART_0_BASE, 'B');
*(base + TXDATA_OFFSET) = 'B';
//IOWR_ALTERA_AVALON_UART_TXDATA(UART_0_BASE, 'A');
*(base + TXDATA_OFFSET) = 'C';
//new_status = IORD_ALTERA_AVALON_UART_STATUS(UART_0_BASE); //Status Reg = 0x0
new_status = *(base + STATUS_OFFSET);
printf("Status Reg after reading: 0x%x", new_status);
return 0;
}
#endif
#ifdef PROJECT4
int main(){
int *base = UART_0_BASE;
char char_to_print, *string = "My name is Leonardo\r\n";
int status_content, status_before, status_after;
int new_divisor = 50000000/2400 - 1;
//status_content = IORD_ALTERA_AVALON_UART_STATUS(UART_0_BASE); //manual clearing of the status reg
status_content = *(base + STATUS_OFFSET);
//IOWR_ALTERA_AVALON_UART_STATUS(UART_0_BASE, status_content & 0x11F);
*(base + STATUS_OFFSET) = status_content & 0x11F;
//IOWR_ALTERA_AVALON_UART_DIVISOR(UART_0_BASE, new_divisor); //set the divisor for the baud rate
*(base + DIVISOR_OFFSET) = new_divisor;
//IOWR_ALTERA_AVALON_UART_CONTROL(UART_0_BASE, 0);
*(base + CONTROL_OFFSET) = 0;
for(int i=0; i<strlen(string); i++){
do {
//status_content = IORD_ALTERA_AVALON_UART_STATUS(UART_0_BASE);
status_content = *(base + STATUS_OFFSET);
status_content &= 0x0040; //mask the RRDY bit
} while(status_content == 0);
//IOWR_ALTERA_AVALON_UART_TXDATA(UART_0_BASE, string[i]);
*(base + TXDATA_OFFSET) = string[i];
}
}
#endif
#ifdef PROJECT51
int main(){
int *base = UART_0_BASE;
int status_content, status_before, status_after;
int new_divisor = 50000000/2400 - 1;
char char_to_print;
//status_content = IORD_ALTERA_AVALON_UART_STATUS(UART_0_BASE); //manual clearing of the status reg
status_content = *(base + STATUS_OFFSET);
//IOWR_ALTERA_AVALON_UART_STATUS(UART_0_BASE, status_content & 0x11F);
*(base + STATUS_OFFSET) = status_content & 0x11F;
//IOWR_ALTERA_AVALON_UART_DIVISOR(UART_0_BASE, new_divisor);
*(base + DIVISOR_OFFSET) = new_divisor;
//IOWR_ALTERA_AVALON_UART_CONTROL(UART_0_BASE, 0);
*(base + CONTROL_OFFSET) = 0;
while(1){
//status_content = IORD_ALTERA_AVALON_UART_STATUS(UART_0_BASE);
status_content = *(base + STATUS_OFFSET);
status_content &= 0x0080; //mask the RRDY bit
if(status_content > 0) { //RRDY = 1
//status_before = IORD_ALTERA_AVALON_UART_STATUS(UART_0_BASE);
status_before = *(base + STATUS_OFFSET);
printf("Status Reg before reading: 0x%X\n", status_before); //Status Reg=0x16C without previous clearing, 0xE0 with clearing
//char_to_print = IORD_ALTERA_AVALON_UART_RXDATA(UART_0_BASE);
char_to_print = *(base + RXDATA_OFFSET);
printf("Found char = %c\n", (char) char_to_print);
//status_after = IORD_ALTERA_AVALON_UART_STATUS(UART_0_BASE);
status_after = *(base + STATUS_OFFSET);
printf("Status Reg before reading: 0x%X\n", status_after); //Status Reg=0x1EC without previous clearing, 0x60 with clearing
}
}
}
#endif
#ifdef PROJECT52
int main(){
int *base = UART_0_BASE;
int status_content, status_before, status_after;
int new_divisor = 50000000/2400 - 1;
char char_to_print;
//status_content = IORD_ALTERA_AVALON_UART_STATUS(UART_0_BASE); //manual clearing of the status reg
status_content = *(base + STATUS_OFFSET);
//IOWR_ALTERA_AVALON_UART_STATUS(UART_0_BASE, status_content & 0x11F);
*(base + STATUS_OFFSET) = status_content & 0x11F;
//IOWR_ALTERA_AVALON_UART_DIVISOR(UART_0_BASE, new_divisor);
*(base + DIVISOR_OFFSET) = new_divisor;
//IOWR_ALTERA_AVALON_UART_CONTROL(UART_0_BASE, 0);
*(base + CONTROL_OFFSET) = 0;
while(1){
//status_before = IORD_ALTERA_AVALON_UART_STATUS(UART_0_BASE);
status_before = *(base + STATUS_OFFSET);
printf("Status Reg before reading: 0x%X\n", status_before); //if we write more than 1 char at a time: status reg=0x168
//char_to_print = IORD_ALTERA_AVALON_UART_RXDATA(UART_0_BASE);
char_to_print = *(base + RXDATA_OFFSET);
printf("Found char = %c\n", (char) char_to_print);
//status_after = IORD_ALTERA_AVALON_UART_STATUS(UART_0_BASE);
status_after = *(base + STATUS_OFFSET);
printf("Status Reg before reading: 0x%X\n", status_after); //if we write more than 1 char at a time: status reg=0x168
usleep(1000000);
}
}
#endif