OpenCores
URL https://opencores.org/ocsvn/openarty/openarty/trunk

Subversion Repositories openarty

[/] [openarty/] [trunk/] [sw/] [board/] [exmulti.c] - Blame information for rev 48

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 36 dgisselq
////////////////////////////////////////////////////////////////////////////////
2
//
3
// Filename:    exmulti.c
4
//
5
// Project:     OpenArty, an entirely open SoC based upon the Arty platform
6
//
7
// Purpose:     Very similar to exstartup.c, the purpose of this program is to
8
//              demonstrate several working peripherals.  To the exstartup
9
//      peripherals, we'll add the GPS and the GPS PPS tracking.
10
//
11
// Creator:     Dan Gisselquist, Ph.D.
12
//              Gisselquist Technology, LLC
13
//
14
////////////////////////////////////////////////////////////////////////////////
15
//
16
// Copyright (C) 2015-2016, Gisselquist Technology, LLC
17
//
18
// This program is free software (firmware): you can redistribute it and/or
19
// modify it under the terms of  the GNU General Public License as published
20
// by the Free Software Foundation, either version 3 of the License, or (at
21
// your option) any later version.
22
//
23
// This program is distributed in the hope that it will be useful, but WITHOUT
24
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
25
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
26
// for more details.
27
//
28
// You should have received a copy of the GNU General Public License along
29
// with this program.  (It's in the $(ROOT)/doc directory, run make with no
30
// target there if the PDF file isn't present.)  If not, see
31
// <http://www.gnu.org/licenses/> for a copy.
32
//
33
// License:     GPL, v3, as defined and found on www.gnu.org,
34
//              http://www.gnu.org/licenses/gpl.html
35
//
36
//
37
////////////////////////////////////////////////////////////////////////////////
38
//
39
//
40
#include "artyboard.h"
41
#include "zipsys.h"
42
 
43
void    idle_task(void) {
44
        while(1)
45
                zip_idle();
46
}
47
 
48
void    wait_on_interrupt(int mask) {
49
        zip->pic = DALLPIC|mask;
50
        zip->pic = EINT(mask);
51
        zip_rtu();
52
}
53
 
54
int     user_stack[256];
55
void    user_task(void) {
56
        const unsigned white = 0x070707, black = 0;
57
        while(1) {
58
                unsigned        btn, subnow, sw;
59
 
60
                subnow = (sys->io_gps_sub >> 28)&0x0f;
61
 
62
                // If the button is pressed, toggle the LED
63
                // Otherwise, turn the LED off.
64
                //
65
 
66
                // First, get all the pressed buttons
67
                btn = (sys->io_btnsw) & 0x0f0;
68
                // Now, acknowledge the button presses that we just read
69
                sys->io_btnsw = btn;
70
                btn >>= 4;
71
 
72
                // Now, use the time as the toggle function.
73
                btn = (subnow ^ btn)&btn & 0x07;
74
 
75
                sys->io_ledctrl = btn | 0x070;
76
 
77
                sw = sys->io_btnsw & 0x0f;
78
                for(int i=0; i<4; i++)
79
                        sys->io_clrled[i] = (sw & (1<<i)) ? white : black;
80
 
81
        }
82
}
83
 
84
int     mpyuhi(int a, int b) {
85
        // err_in_ns = mpyuhi(err_in_ns, err);
86
        // __asm__("MPYUHI %1,%0" :"+r"(a):"r"(b));
87
        unsigned alo, blo, ahi, bhi, f, o, i, l, rhi;
88
 
89
        alo = (a & 0x0ffff);
90
        ahi = (a >> 16)&0x0ffff;
91
        blo = (b & 0x0ffff);
92
        bhi = (b >> 16)&0x0ffff;
93
 
94
        l = alo * blo;
95
        o = ahi * blo;
96
        i = alo * bhi;
97
        f = ahi * bhi;
98
 
99
        rhi = o + i + (l >> 16);
100
        return (rhi >> 16) + f;
101
        // return f;
102
        // return ahi;
103
}
104
 
105
int     hexdigit(int v) {
106
        if (v < 10)
107
                return v + '0';
108
        else
109
                return v + 'A' - 10;
110
}
111
 
112
int     errstring[128];
113
 
114
void    entry(void) {
115
        const unsigned red = 0x0ff0000, green = 0x0ff00, blue = 0x0ff,
116
                white = 0x070707, black = 0, dimgreen = 0x1f00,
117
                second = 81250000;
118
        int     i, sw;
119
 
120
        // Start the GPS converging ...
121
        sys->io_gps.g_alpha = 2;
122
        sys->io_gps.g_beta  = 0x14bda12f;
123
        sys->io_gps.g_gamma = 0x1f533ae8;
124
 
125
        int     user_context[16];
126
        for(i=0; i<15; i++)
127
                user_context[i] = 0;
128
        user_context[15] = (unsigned)idle_task;
129
        zip_restore_context(user_context);
130
 
131
        for(i=0; i<4; i++)
132
                sys->io_clrled[i] = red;
133
        sys->io_ledctrl = 0x0ff;
134
 
135
        // Clear the PIC
136
        //
137
        //      Acknowledge all interrupts, turn off all interrupts
138
        //
139
        zip->pic = 0x7fff7fff;
140
        while(sys->io_pwrcount < (second >> 4))
141
                ;
142
 
143
        // Repeating timer, every 250ms
144
        zip->tma = (second/4) | 0x80000000;
145
        wait_on_interrupt(SYSINT_TMA);
146
 
147
        sys->io_clrled[0] = green;
148
        sys->io_ledctrl = 0x010;
149
 
150
        wait_on_interrupt(SYSINT_TMA);
151
 
152
        sys->io_clrled[0] = dimgreen;
153
        sys->io_clrled[1] = green;
154
        sys->io_scope[0].s_ctrl = 32 | 0x80000000; // SCOPE_TRIGGER;
155
        sys->io_ledctrl = 0x020;
156
 
157
        wait_on_interrupt(SYSINT_TMA);
158
 
159
        sys->io_clrled[1] = dimgreen;
160
        sys->io_clrled[2] = green;
161
        sys->io_ledctrl = 0x040;
162
 
163
        wait_on_interrupt(SYSINT_TMA);
164
 
165
        sys->io_clrled[2] = dimgreen;
166
        sys->io_clrled[3] = green;
167
        sys->io_ledctrl = 0x080;
168
 
169
        wait_on_interrupt(SYSINT_TMA);
170
 
171
        sys->io_clrled[3] = dimgreen;
172
 
173
        wait_on_interrupt(SYSINT_TMA);
174
 
175
        for(i=0; i<4; i++)
176
                sys->io_clrled[i] = black;
177
 
178
        // Wait one second ...
179
        for(i=0; i<4; i++)
180
                wait_on_interrupt(SYSINT_TMA);
181
 
182
        // Blink all the LEDs
183
        //      First turn them on
184
        sys->io_ledctrl = 0x0ff;
185
        // Then wait a quarter second
186
        wait_on_interrupt(SYSINT_TMA);
187
        // Then turn the back off
188
        sys->io_ledctrl = 0x0f0;
189
        // and wait another quarter second
190
        wait_on_interrupt(SYSINT_TMA);
191
 
192
        // Now, read buttons, and flash an LED on any button being held
193
        // down ... ? neat?
194
 
195
        // Now, let's synchronize ourselves to the PPS
196
        user_context[13] = (int)&user_stack[256];
197
        user_context[15] = (int)&user_task;
198
        zip_restore_context(user_context);
199
 
200
        do {
201
                wait_on_interrupt(SYSINT_PPS|SYSINT_TMA);
202
        } while((zip->pic & SYSINT_PPS)==0);
203
 
204
        while(1) {
205
                int     *s = errstring;
206
 
207
                zip->wdt = CLOCKFREQ_HZ*4;
208
                sys->io_ledctrl = 0x088;
209
 
210
                // 1. Read and report the GPS tracking err
211
 
212
                // Get the upper 32-bits of the error;
213
                int     err = *(int *)(&sys->io_gpstb.tb_err);
214
                int     err_in_ns;
215
                /*
216
                long    err_in_ns_long = err;
217
                err_in_ns_long *= 1000000000l;
218
                err_in_ns_long >>= 32;
219
                int     err_in_ns = (int)(err_in_ns_long);
220
                */
221
                int     err_sgn = (err < 0)?1:0;
222
                err_in_ns = (err<0)?-err:err;
223
                err_in_ns = mpyuhi(err_in_ns, 1000000000);
224
                int     digit;
225
 
226
 
227
                *s++ = '\r';
228
                *s++ = '\n';
229
                *s++ = 'G';
230
                *s++ = 'P';
231
                *s++ = 'S';
232
                *s++ = ' ';
233
                *s++ = 'P';
234
                *s++ = 'P';
235
                *s++ = 'S';
236
                *s++ = ' ';
237
                *s++ = 'E';
238
                *s++ = 'r';
239
                *s++ = 'r';
240
                *s++ = ':';
241
                *s++ = ' ';
242
 
243
 
244
                *s++ = '0';
245
                *s++ = 'x';
246
                *s++ = hexdigit((err>>28)&0x0f);
247
                *s++ = hexdigit((err>>24)&0x0f);
248
                *s++ = hexdigit((err>>20)&0x0f);
249
                *s++ = hexdigit((err>>16)&0x0f);
250
                *s++ = hexdigit((err>>12)&0x0f);
251
                *s++ = hexdigit((err>> 8)&0x0f);
252
                *s++ = hexdigit((err>> 4)&0x0f);
253
                *s++ = hexdigit((err    )&0x0f);
254
 
255
                *s++ = ' ';
256
                *s++ = '=';
257
                *s++ = '>';
258
                *s++ = ' ';
259
 
260
                *s++ = '0';
261
                *s++ = 'x';
262
                *s++ = hexdigit((err_in_ns>>28)&0x0f);
263
                *s++ = hexdigit((err_in_ns>>24)&0x0f);
264
                *s++ = hexdigit((err_in_ns>>20)&0x0f);
265
                *s++ = hexdigit((err_in_ns>>16)&0x0f);
266
                *s++ = hexdigit((err_in_ns>>12)&0x0f);
267
                *s++ = hexdigit((err_in_ns>> 8)&0x0f);
268
                *s++ = hexdigit((err_in_ns>> 4)&0x0f);
269
                *s++ = hexdigit((err_in_ns    )&0x0f);
270
 
271
                *s++ = ' ';
272
                *s++ = '=';
273
                *s++ = '>';
274
                *s++ = ' ';
275
 
276
                *s++ = (err_sgn)?'-':' ';
277
                // Milliseconds
278
                digit = err_in_ns / 100000000;
279
                *s++ = digit+'0';
280
                err_in_ns -= digit * 100000000;
281
                //
282
                digit = err_in_ns / 10000000;
283
                *s++ = digit+'0';
284
                err_in_ns -= digit * 10000000;
285
                //
286
                digit = err_in_ns / 1000000;
287
                *s++ = digit+'0';
288
                err_in_ns -= digit * 1000000;
289
                // Micro seconds
290
                digit = err_in_ns / 100000;
291
                *s++ = digit+'0';
292
                err_in_ns -= digit * 100000;
293
                //
294
                digit = err_in_ns / 10000;
295
                *s++ = digit+'0';
296
                err_in_ns -= digit * 10000;
297
                //
298
                digit = err_in_ns / 1000;
299
                *s++ = digit+'0';
300
                err_in_ns -= digit * 1000;
301
                // Nano seconds
302
                *s++ = '.';
303
                digit = err_in_ns / 100;
304
                *s++ = digit+'0';
305
                err_in_ns -= digit * 100;
306
                //
307
                digit = err_in_ns / 10;
308
                *s++ = digit+'0';
309
                err_in_ns -= digit * 10;
310
                //
311
                digit = err_in_ns;
312
                *s++ = digit+'0';
313
                //
314
                *s++ = ' ';
315
                *s++ = 'u';
316
                *s++ = 'S';
317
                *s++ = '\r';
318
                *s++ = '\n';
319
                *s++ = '\r';
320
                *s++ = '\n';
321
                *s++ = '\0';
322
 
323
                /*
324
                zip->dma.ctrl = DMACLEAR;
325
                zip->dma.rd = errstring;
326
                zip->dma.wr = &sys->io_uart_tx;
327
                zip->dma.len = s - errstring-1;
328
                zip->dma.ctrl = (DMAONEATATIME|DMA_CONSTDST|DMA_GPSRX);
329
                wait_on_interrupt(SYSINT_DMAC);
330
                */
331
 
332
                for(int i=0; errstring[i]; i++) {
333
                        wait_on_interrupt(SYSINT_UARTTX);
334
                        sys->io_uart_tx = errstring[i];
335
                        zip->pic = SYSINT_UARTTX;
336
                }
337
 
338
                sys->io_ledctrl = 0x080;
339
 
340
                /*
341
                zip->dma.rd = &sys->io_gps_rx;
342
                zip->dma.wr = &sys->io_uart_tx;
343
                zip->dma.len = 0x01000000;
344
                zip->dma.ctrl = (DMAONEATATIME|DMA_CONSTDST|DMA_CONSTSRC|DMA_GPSRX);
345
                wait_on_interrupt(SYSINT_PPS);
346
                */
347
 
348
                /*
349
                if (zip_ucc() & CC_FAULT) {
350
                        zip_save_context(user_context);
351
                        user_context[14] = CC_GIE;
352
                        user_context[15] = (int)&idle_task;
353
                        zip_restore_context(user_context);
354
                }
355
                */
356
 
357
                zip->pic = SYSINT_GPSRX | SYSINT_PPS;
358
                do {
359
                        wait_on_interrupt(SYSINT_PPS|SYSINT_GPSRX);
360
                        if (zip->pic & SYSINT_GPSRX) {
361
                                sys->io_uart_tx = sys->io_gps_rx;
362
                                zip->pic = SYSINT_GPSRX;
363
                        }
364
                } while((zip->pic & SYSINT_PPS)==0);
365
 
366
                // wait_on_interrupt(SYSINT_PPS);
367
                // zip->dma.ctrl= DMACLEAR;
368
        }
369
 
370
        zip_halt();
371
}
372
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.