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

Subversion Repositories openarty

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

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 52 dgisselq
#include "artyboard.h"
41 49 dgisselq
#include "zipcpu.h"
42
#include "zipsys.h"
43 52 dgisselq
#include <stdio.h>
44
#include <string.h>
45 36 dgisselq
 
46 52 dgisselq
#define sys     _sys
47
 
48 36 dgisselq
void    idle_task(void) {
49
        while(1)
50
                zip_idle();
51
}
52
 
53
void    wait_on_interrupt(int mask) {
54 52 dgisselq
        if (mask & SYSINT_AUX) {
55
                zip->z_apic = INT_ENABLE;
56
        }
57 49 dgisselq
        zip->z_pic = DALLPIC|mask;
58
        zip->z_pic = EINT(mask);
59 36 dgisselq
        zip_rtu();
60
}
61
 
62
int     user_stack[256];
63
void    user_task(void) {
64
        const unsigned white = 0x070707, black = 0;
65
        while(1) {
66
                unsigned        btn, subnow, sw;
67
 
68 52 dgisselq
                subnow = (sys->io_b.i_tim.sub >> 28)&0x0f;
69 36 dgisselq
 
70
                // If the button is pressed, toggle the LED
71
                // Otherwise, turn the LED off.
72
                //
73
 
74
                // First, get all the pressed buttons
75 52 dgisselq
                btn = (sys->io_b.i_btnsw) & 0x0f0;
76 36 dgisselq
                // Now, acknowledge the button presses that we just read
77 52 dgisselq
                sys->io_b.i_btnsw = btn;
78 36 dgisselq
                btn >>= 4;
79
 
80
                // Now, use the time as the toggle function.
81
                btn = (subnow ^ btn)&btn & 0x07;
82
 
83 52 dgisselq
                sys->io_b.i_leds = btn | 0x070;
84 36 dgisselq
 
85 52 dgisselq
                sw = sys->io_b.i_btnsw & 0x0f;
86 36 dgisselq
                for(int i=0; i<4; i++)
87 52 dgisselq
                        sys->io_b.i_clrled[i] = (sw & (1<<i)) ? white : black;
88 36 dgisselq
 
89
        }
90
}
91
 
92
int     mpyuhi(int a, int b) {
93
        // err_in_ns = mpyuhi(err_in_ns, err);
94
        // __asm__("MPYUHI %1,%0" :"+r"(a):"r"(b));
95
        unsigned alo, blo, ahi, bhi, f, o, i, l, rhi;
96
 
97
        alo = (a & 0x0ffff);
98
        ahi = (a >> 16)&0x0ffff;
99
        blo = (b & 0x0ffff);
100
        bhi = (b >> 16)&0x0ffff;
101
 
102
        l = alo * blo;
103
        o = ahi * blo;
104
        i = alo * bhi;
105
        f = ahi * bhi;
106
 
107
        rhi = o + i + (l >> 16);
108
        return (rhi >> 16) + f;
109
        // return f;
110
        // return ahi;
111
}
112
 
113 52 dgisselq
 
114
int     gps_lock = 0;
115
void    gps_process_line(const char *line) {
116
        if ((line[0] != '$')||(line[1] != 'G')
117
                        ||(line[2] != 'P'))
118
                return;
119
 
120
        // GGA, GSV(x3), RMC, VTG
121
        if (line[3] == 'G') {
122
                /*
123
                if ((line[4] == 'G')&&(line[5] == 'A')) {
124
                } // else if ((line[5] == 'S')&&(line[6] == 'V')) {
125
                }
126
                */
127
                // printf("GPS Line: %s\r\n", line);
128
        } else if (line[3] == 'R') {
129
                // if ((line[4] == 'M')&&(line[5] == 'C')&&(line[6] == ','))
130
                {
131
                        // char outbuf[256], *outptr = outbuf;
132
                        // const char *here = &line[8], *there;
133
                        // there = strchr(here, ',');
134
                        fputs("RMC-Line\r\n", stdout);
135
#ifdef  PROCLINE
136
                        if ((there)&&(there - here > 6)) {
137
                                outptr += sprintf(outptr,
138
                                        "TIME: %c%c:%c%c:%c%c\r\n",
139
                                                here[0], here[1],
140
                                                here[2], here[3],
141
                                                here[4], here[5]);
142
                                here = there + 1;
143
                                there = strchr(here, ',');
144
                        } if (there) {
145
                                if (*here == 'A')
146
                                        gps_lock = 1;
147
                                else
148
                                        gps_lock = 0;
149
                                here = there + 1;
150
                                there = strchr(there+1, ',');
151
                        } if (there) {
152
                                there = strchr(there+1, ',');
153
                        } if (there) {
154
                                char    tmp[32];
155
                                strncpy(tmp, here, there-here);
156
                                outptr += sprintf(outptr, "LATITUDE: %s\r\n");
157
                                here = there + 1;
158
                                there = strchr(there+1, ',');
159
                        } if (there) {
160
                                there = strchr(there+1, ',');
161
                        } if (there) {
162
                                char    tmp[32];
163
                                strncpy(tmp, here, there-here);
164
                                outptr += sprintf(outptr, "LONGITUDE: %s\r\n");
165
                        } if (gps_lock)
166
                                fputs(outbuf, stdout);
167
                        else    puts("No GPS lock\r\n");
168
#endif
169
                }
170
                printf("GPS RMC Line: %s\r\n", line);
171
        } else if (line[3] == 'V') {
172
                /*
173
                if ((line[4] == 'T')&&(line[5] == 'G')) {
174
                }
175
                */
176
                // printf("GPS Line: %s\r\n", line);
177
        } else printf("Other GPS Line: %s\r\n", line);
178 36 dgisselq
}
179
 
180 52 dgisselq
char    errstring[128];
181 36 dgisselq
 
182 52 dgisselq
 
183 49 dgisselq
void    main(int argc, char **argv) {
184 36 dgisselq
        const unsigned red = 0x0ff0000, green = 0x0ff00, blue = 0x0ff,
185
                white = 0x070707, black = 0, dimgreen = 0x1f00,
186 49 dgisselq
                second = CLOCKFREQHZ;
187 36 dgisselq
        int     i, sw;
188
 
189
        // Start the GPS converging ...
190
        sys->io_gps.g_alpha = 2;
191
        sys->io_gps.g_beta  = 0x14bda12f;
192
        sys->io_gps.g_gamma = 0x1f533ae8;
193
 
194 52 dgisselq
        // 
195 36 dgisselq
        int     user_context[16];
196
        for(i=0; i<15; i++)
197
                user_context[i] = 0;
198
        user_context[15] = (unsigned)idle_task;
199
        zip_restore_context(user_context);
200
 
201
        for(i=0; i<4; i++)
202 52 dgisselq
                sys->io_b.i_clrled[i] = red;
203
        sys->io_b.i_leds = 0x0ff;
204 36 dgisselq
 
205
        // Clear the PIC
206
        //
207
        //      Acknowledge all interrupts, turn off all interrupts
208
        //
209 49 dgisselq
        zip->z_pic = CLEARPIC;
210 52 dgisselq
        while(sys->io_b.i_pwrcount < (second >> 4))
211 36 dgisselq
                ;
212
 
213
        // Repeating timer, every 250ms
214 49 dgisselq
        zip->z_tma = TMR_INTERVAL | (second/4);
215 36 dgisselq
        wait_on_interrupt(SYSINT_TMA);
216
 
217 52 dgisselq
        sys->io_b.i_clrled[0] = green;
218
        sys->io_b.i_leds = 0x010;
219 36 dgisselq
 
220
        wait_on_interrupt(SYSINT_TMA);
221
 
222 52 dgisselq
        sys->io_b.i_clrled[0] = dimgreen;
223
        sys->io_b.i_clrled[1] = green;
224
        sys->io_scope[0].s_ctrl = WBSCOPE_NO_RESET | 32;
225
        sys->io_b.i_leds = 0x020;
226 36 dgisselq
 
227
        wait_on_interrupt(SYSINT_TMA);
228
 
229 52 dgisselq
        sys->io_b.i_clrled[1] = dimgreen;
230
        sys->io_b.i_clrled[2] = green;
231
        sys->io_b.i_leds = 0x040;
232 36 dgisselq
 
233
        wait_on_interrupt(SYSINT_TMA);
234
 
235 52 dgisselq
        sys->io_b.i_clrled[2] = dimgreen;
236
        sys->io_b.i_clrled[3] = green;
237
        sys->io_b.i_leds = 0x080;
238 36 dgisselq
 
239
        wait_on_interrupt(SYSINT_TMA);
240
 
241 52 dgisselq
        sys->io_b.i_clrled[3] = dimgreen;
242 36 dgisselq
 
243
        wait_on_interrupt(SYSINT_TMA);
244
 
245
        for(i=0; i<4; i++)
246 52 dgisselq
                sys->io_b.i_clrled[i] = black;
247 36 dgisselq
 
248
        // Wait one second ...
249
        for(i=0; i<4; i++)
250
                wait_on_interrupt(SYSINT_TMA);
251
 
252
        // Blink all the LEDs
253
        //      First turn them on
254 52 dgisselq
        sys->io_b.i_leds = 0x0ff;
255 36 dgisselq
        // Then wait a quarter second
256
        wait_on_interrupt(SYSINT_TMA);
257
        // Then turn the back off
258 52 dgisselq
        sys->io_b.i_leds = 0x0f0;
259 36 dgisselq
        // and wait another quarter second
260
        wait_on_interrupt(SYSINT_TMA);
261
 
262
        // Now, read buttons, and flash an LED on any button being held
263
        // down ... ? neat?
264
 
265
        // Now, let's synchronize ourselves to the PPS
266
        user_context[13] = (int)&user_stack[256];
267
        user_context[15] = (int)&user_task;
268
        zip_restore_context(user_context);
269
 
270
        do {
271
                wait_on_interrupt(SYSINT_PPS|SYSINT_TMA);
272 49 dgisselq
        } while((zip->z_pic & SYSINT_PPS)==0);
273 52 dgisselq
 
274
        printf("GPS RECORD START\r\n");
275
 
276
        zip->z_tma = TMR_INTERVAL | (second/1000);
277
        wait_on_interrupt(SYSINT_TMA);
278
        sys->io_gpsu.u_rx = 0x01000;
279 36 dgisselq
        while(1) {
280 52 dgisselq
                char    *s = errstring;
281 36 dgisselq
 
282 49 dgisselq
                zip->z_wdt = CLOCKFREQ_HZ*4;
283 52 dgisselq
                sys->io_b.i_leds = 0x088;
284 36 dgisselq
 
285
                // 1. Read and report the GPS tracking err
286
 
287
                // Get the upper 32-bits of the error;
288
                int     err = *(int *)(&sys->io_gpstb.tb_err);
289 52 dgisselq
                int     err_in_ns, err_in_us;
290
                int     err_sgn = (err < 0)?1:0, err_in_ns_rem;
291
 
292 36 dgisselq
                err_in_ns = (err<0)?-err:err;
293
                err_in_ns = mpyuhi(err_in_ns, 1000000000);
294
 
295 52 dgisselq
                err_in_us = err_in_ns / 1000;
296
                err_in_ns_rem = err_in_ns - err_in_us * 1000;
297
                if (err_sgn)
298
                        err_in_us = - err_in_us;
299 36 dgisselq
 
300 52 dgisselq
                printf("\r\nGPS PPS Err: 0x%08x => 0x%08x => %+5d.%03d us\r\n",
301
                        err, err_in_ns, err_in_us, err_in_ns_rem);
302 36 dgisselq
 
303
 
304
 
305 52 dgisselq
                sys->io_b.i_leds = 0x080;
306 36 dgisselq
 
307 52 dgisselq
                zip->z_pic  = SYSINT_GPSRXF | SYSINT_PPS | SYSINT_TMA;
308
                {
309
                        const int       LINEBUFSZ = 80;
310
                        char    line[LINEBUFSZ], *linep = line;
311
                        do {
312
                                int     v;
313
                                wait_on_interrupt(SYSINT_PPS|SYSINT_GPSRXF|SYSINT_TMA);
314 36 dgisselq
 
315 52 dgisselq
                                while(((v = sys->io_gpsu.u_rx)&0x100)==0) {
316
                                        v &= 0x0ff;
317
                                        // putchar(v);
318
                                        // sys->io_uart.u_tx = v;
319
                                        *linep++ = v;
320
                                        if(linep-line > LINEBUFSZ)
321
                                                linep = line;
322
                                        if ((v == '\r')||(v == '\n')) {
323
                                                *linep = '\0';
324
                                                if (line[0] == '$')
325
                                                        gps_process_line(line);
326
                                                linep = line;
327
                                        }
328
                                }
329
                        } while((zip->z_pic & SYSINT_PPS)==0);
330 36 dgisselq
                }
331
        }
332
 
333
        zip_halt();
334
}
335
 

powered by: WebSVN 2.1.0

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