forked from enjoy-digital/litex
/
main.c
270 lines (234 loc) · 5.52 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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// This file is Copyright (c) 2020 Florent Kermarrec <florent@enjoy-digital.fr>
// License: BSD
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <irq.h>
#include <uart.h>
#include <console.h>
#include <generated/csr.h>
/*-----------------------------------------------------------------------*/
/* Uart */
/*-----------------------------------------------------------------------*/
static char *readstr(void)
{
char c[2];
static char s[64];
static int ptr = 0;
if(readchar_nonblock()) {
c[0] = readchar();
c[1] = 0;
switch(c[0]) {
case 0x7f:
case 0x08:
if(ptr > 0) {
ptr--;
putsnonl("\x08 \x08");
}
break;
case 0x07:
break;
case '\r':
case '\n':
s[ptr] = 0x00;
putsnonl("\n");
ptr = 0;
return s;
default:
if(ptr >= (sizeof(s) - 1))
break;
putsnonl(c);
s[ptr] = c[0];
ptr++;
break;
}
}
return NULL;
}
static char *get_token(char **str)
{
char *c, *d;
c = (char *)strchr(*str, ' ');
if(c == NULL) {
d = *str;
*str = *str+strlen(*str);
return d;
}
*c = 0;
d = *str;
*str = c+1;
return d;
}
static void prompt(void)
{
printf("\e[92;1mlitex-demo-app\e[0m> ");
}
/*-----------------------------------------------------------------------*/
/* Help */
/*-----------------------------------------------------------------------*/
static void help(void)
{
puts("\nLiteX minimal demo app built "__DATE__" "__TIME__"\n");
puts("Available commands:");
puts("help - Show this command");
puts("reboot - Reboot CPU");
#ifdef CSR_LEDS_BASE
puts("led - Led demo");
#endif
puts("donut - Spinning Donut demo");
puts("ident - Identifier of the system");
puts("dwa - DAC write a (+1)");
puts("dwb - DAC write b (x2)");
puts("dwcw - DAC write cw (toggles)");
puts("dr - DAC read all");
puts("c - Counter thing");
}
/*-----------------------------------------------------------------------*/
/* Commands */
/*-----------------------------------------------------------------------*/
static void reboot_cmd(void)
{
ctrl_reset_write(1);
}
#ifdef CSR_LEDS_BASE
static void led_cmd(void)
{
int i;
printf("Led demo...\n");
printf("Counter mode...\n");
for(i=0; i<32; i++) {
leds_out_write(i);
busy_wait(100);
}
printf("Shift mode...\n");
for(i=0; i<4; i++) {
leds_out_write(1<<i);
busy_wait(200);
}
for(i=0; i<4; i++) {
leds_out_write(1<<(3-i));
busy_wait(200);
}
printf("Dance mode...\n");
for(i=0; i<4; i++) {
leds_out_write(0x55);
busy_wait(200);
leds_out_write(0xaa);
busy_wait(200);
}
}
#endif
extern void donut(void);
static void donut_cmd(void)
{
printf("Donut demo...\n");
donut();
}
/**
* Command "ident"
*
* Identifier of the system
*
*/
#define IDENT_SIZE 256
static void ident_cmd()
{
char buffer[IDENT_SIZE];
get_ident(buffer);
printf("Ident: %s\n", *buffer ? buffer : "-");
printf("NICK ER SEJ\n");
}
static void dac_read_all()
{
//uint32_t dac_data_reg = dac_data_read();
uint32_t dac_data_a_reg = dac_data_a_storage_read();
uint32_t dac_data_b_reg = dac_data_b_storage_read();
uint32_t dac_cw_reg = dac_cw_read();
//printf("Data:\t\t0x%08x (%d)\n", dac_data_reg, dac_data_reg);
printf("Data A:\t\t0x%08x (%d)\n", dac_data_a_reg, dac_data_a_reg);
printf("Data B:\t\t0x%08x (%d)\n", dac_data_b_reg, dac_data_b_reg);
printf("Data CW:\t0x%08x (%d)\n", dac_cw_reg, dac_cw_reg);
}
static void dac_write_a()
{
uint32_t a = dac_data_b_storage_read();
dac_data_a_storage_write(a+1);
dac_read_all();
}
static void dac_write_b()
{
uint32_t a = dac_data_b_storage_read();
dac_data_b_storage_write(a<<1);
dac_read_all();
}
static void dac_write_cw()
{
uint32_t a = dac_cw_read();
dac_cw_write(~a);
dac_read_all();
}
static void count()
{
uint32_t a = 0;
uint32_t i = 0;
uint32_t counter = 0;
for (i; i < 2; i++) {
printf("Round %d\n", i);
for (counter = 0; counter < (1024); counter++ ) {
dac_data_a_storage_write(counter);
dac_data_b_storage_write(counter);
//a = dac_cw_read();
//dac_cw_write(a ^ 1);
dac_read_all();
}
}
printf("Done counting!\n");
}
/*-----------------------------------------------------------------------*/
/* Console service / Main */
/*-----------------------------------------------------------------------*/
static void console_service(void)
{
char *str;
char *token;
str = readstr();
if(str == NULL) return;
token = get_token(&str);
if(strcmp(token, "help") == 0)
help();
else if(strcmp(token, "reboot") == 0)
reboot_cmd();
#ifdef CSR_LEDS_BASE
else if(strcmp(token, "led") == 0)
led_cmd();
#endif
else if(strcmp(token, "donut") == 0)
donut_cmd();
else if(strcmp(token, "ident") == 0)
ident_cmd();
else if(strcmp(token, "dwa") == 0)
dac_write_a();
else if(strcmp(token, "dwb") == 0)
dac_write_b();
else if(strcmp(token, "dwcw") == 0)
dac_write_cw();
else if(strcmp(token, "dr") == 0)
dac_read_all();
else if(strcmp(token, "c") == 0)
count();
prompt();
}
int main(void)
{
#ifdef CONFIG_CPU_HAS_INTERRUPT
irq_setmask(0);
irq_setie(1);
#endif
uart_init();
help();
prompt();
while(1) {
console_service();
}
return 0;
}