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

Subversion Repositories s6soc

[/] [s6soc/] [trunk/] [sw/] [host/] [deppi.cpp] - Blame information for rev 45

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

Line No. Rev Author Line
1 11 dgisselq
////////////////////////////////////////////////////////////////////////////////
2
//
3
// Filename:    deppi.cpp
4
//
5
// Project:     CMod S6 System on a Chip, ZipCPU demonstration project
6
//
7
// Purpose:     Creates a DEPP interface pseudo-character device, similar to
8
//              what you might get from a serial port or other character device,
9
//      from the DEPP interface to a CMOD S6 board.
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 <stdio.h>
41
#include <stdlib.h>
42
#include <unistd.h>
43
#include <string.h>
44
#include <ctype.h>
45
#include <time.h>
46
// From Digilent's Adept Library
47
#include "dpcdecl.h"
48
#include "dmgr.h"
49
#include "depp.h"
50
 
51
// From my own library
52
#include "llcomms.h"
53
#include "deppi.h"
54
 
55 45 dgisselq
FILE    *dbgfp = stderr;
56
 
57
DEPPI::DEPPI(const char *szSel) {
58
        if ((!szSel)||(szSel[0] == '\0')) {
59
                // Number of digilent devcies on a system
60
                int     pcdvc;
61
 
62
                // Go fish and try to find the device
63
                DmgrEnumDevices(&pcdvc);
64
 
65
                if (pcdvc < 0) {
66
                        depperr();
67
                        exit(EXIT_FAILURE);
68
                }
69
 
70
                //
71
                int     found = 0; // Number of devices found mtg our criteria
72
                DVC     dvcinfo; // A structure to receive device info
73
                int     foundid=-1; // The id number of the device we found
74
 
75
                //
76
                for(int devid=0; devid < pcdvc; devid++) {
77
                        DmgrGetDvc(devid, &dvcinfo);
78
                        // fprintf(dbgfp, "DEVICE NAME: %s\n", dvcinfo.szName);
79
                        if (strcmp(dvcinfo.szName, "CmodS6")==0) {
80
                                found++;
81
                                // fprintf(dbgfp, "Found a CMOD!\n");
82
                                foundid = devid;
83
                        }
84
                }
85
 
86
                if (found == 0) {
87
                        fprintf(stderr, "No CModS6 devices found\n");
88
                        exit(EXIT_FAILURE);
89
                } else if (found > 1) {
90
                        fprintf(stderr, "More than one CModS6 device found.  Please consider opening your\n");
91
                        fprintf(stderr, "device with a valid serial number instead.\n");
92
                        exit(EXIT_FAILURE);
93
                }
94
 
95
                DmgrGetDvc(foundid, &dvcinfo);
96
                if (!DmgrOpen(&m_dev, dvcinfo.szConn)) {
97
                        fprintf(stderr, "Could not open device!\n");
98
                        depperr();
99
                        exit(EXIT_FAILURE);
100
                }
101
 
102
                //
103
                DmgrFreeDvcEnum();
104
        } else if (!DmgrOpen(&m_dev, (char *)szSel)) {
105
                // We know the device serial number, so go open that particular
106
                // device
107
                fprintf(stderr, "Named device open (DmgrOpen) failed!\n");
108
                depperr();
109 11 dgisselq
                exit(EXIT_FAILURE);
110
        }
111
 
112
        if (!DeppEnable(m_dev)) {
113 45 dgisselq
                fprintf(stderr, "Could not enable DEPP interface to (opened) device\n");
114
 
115
                depperr();
116 11 dgisselq
                exit(EXIT_FAILURE);
117
        }
118
 
119
        m_int = false, m_err = false;
120
 
121
        // fprintf(stdout, "Flushing **************\n");
122
        flush_read();
123
        // fprintf(stdout, "Flushed! **************\n");
124
}
125
 
126
DEPPI::~DEPPI(void) {
127
        close();
128
}
129
 
130
void    DEPPI::close(void) {
131
        if (m_dev)
132
                DmgrClose(m_dev);
133
        m_dev = 0;
134
}
135
 
136
void    DEPPI::depperr(void) {
137
        ERC     erc = DmgrGetLastError();
138
        if(erc != ercNoErc) {
139 45 dgisselq
                char scode[cchErcMax], smsg[cchErcMsgMax];
140
                DmgrSzFromErc(erc, scode, smsg);
141
                fprintf(stderr, "ErrCode(%d): %s\n", erc, scode);
142
                fprintf(stderr, "ErrMessage: %s\n", smsg);
143
 
144
                if (erc == ercCapabilityConflict) {
145
                        fprintf(stderr, "Do you have the hardware manager in Vivado open?\n");
146
                        fprintf(stderr, "That could cause this conflict.\n");
147
                }
148 11 dgisselq
                close();
149
                exit(EXIT_FAILURE);
150
        }
151
}
152
 
153
void    DEPPI::write(char *buf, int len) {
154
        bool    good = true;
155 45 dgisselq
        const bool      dbg = false;
156 11 dgisselq
 
157 45 dgisselq
        if (dbg) {
158 11 dgisselq
                // Debug code--write one at a time
159
                fputs("WR: ", stdout);
160
                for(int i=0; i<len; i++) {
161
                        good = good && DeppPutReg(m_dev, 0, (unsigned char)buf[i], false);
162
                        fputc(buf[i], stdout);
163
                } fputc('\n', stdout);
164
        } else
165
                good = DeppPutRegRepeat(m_dev, 0, (unsigned char *)buf, len, false);
166
        if (!good)
167
                depperr();
168
}
169
 
170
int     DEPPI::read(char *buf, int len) {
171
        return read(buf, len, 4);
172
}
173
 
174
int     DEPPI::read(char *buf, int len, int timeout_ms) {
175
        int     left = len, nr=0;
176
        struct  timespec        now, later;
177 45 dgisselq
        const   bool    dbg = false;
178 11 dgisselq
 
179
        clock_gettime(CLOCK_MONOTONIC, &now);
180
 
181 45 dgisselq
        if (dbg) fprintf(dbgfp, "USBI::read(%d) (FIFO is %d-%d)\n", len, m_rend, m_rbeg);
182 11 dgisselq
        nr = pop_fifo(buf, left);
183
        left -= nr;
184
 
185
        while(left > 0) {
186
                raw_read(left, timeout_ms);
187
                nr = pop_fifo(&buf[len-left], left);
188
                left -= nr;
189
 
190 45 dgisselq
                if (dbg) fprintf(dbgfp, "\tWHILE (nr = %d, LEFT = %d, len=%d)\n", nr, left, len);
191 11 dgisselq
                if (nr == 0)
192
                        break;
193
#define TIMEOUT
194
#ifdef  TIMEOUT
195
                if (timeout_ms == 0)
196
                        break;
197
                else if (timeout_ms > 0) {
198
                        clock_gettime(CLOCK_MONOTONIC, &later);
199
 
200
                        long    num_ns = later.tv_nsec - now.tv_nsec, num_ms;
201
                        if (num_ns < 0) {
202
                                num_ns += 1000000000;
203
                                later.tv_sec--;
204
                        } num_ms = num_ns / 1000000;
205
                        if (later.tv_sec > now.tv_sec)
206
                                num_ms += (later.tv_sec - now.tv_sec)*1000;
207
 
208
                        if (num_ms > timeout_ms)
209
                                break;
210
                }
211
#endif
212
        }
213
 
214 45 dgisselq
        if(dbg) fprintf(dbgfp, "READ %d characters (%d req, %d left)\n", len-left, len, left);
215 11 dgisselq
        return len-left;
216
}
217
 
218
void    DEPPI::raw_read(const int clen, int timeout_ms) {
219
        int     empty = RCV_BUFMASK - ((m_rbeg - m_rend)&(RCV_BUFMASK));
220
        int     len = clen;
221
        bool    good = true;
222 45 dgisselq
        const   bool    dbg = false;
223 11 dgisselq
 
224 45 dgisselq
 
225
        if (dbg) fprintf(dbgfp, "DEPPI::raw_read(len=%d)\n", clen);
226 11 dgisselq
        if (len > empty)
227
                len = empty;
228
        if (len > 0) {
229
                // Fill the tail of our buffer
230
                int ln = len;
231
                if (ln > PKTLEN)
232
                        ln = PKTLEN;
233
 
234
                // fprintf(stdout, "RAW-READ(%d)\n", ln);
235
                if (false) {
236
                        // Debug code--read one word at a time
237
                        for(int i=0; i<ln; i++) {
238
                                good = good && DeppGetReg(m_dev, 0, (unsigned char *)&m_rxbuf[i], false);
239
                                usleep(1);
240
                        }
241
                } else
242
                        good = good && DeppGetRegRepeat(m_dev, 0, (unsigned char *)m_rxbuf, ln, false);
243 45 dgisselq
                if(dbg) fprintf(dbgfp, "DEPP: Pushing to FIFO\n");
244 11 dgisselq
                push_fifo(m_rxbuf, ln);
245
                len -= ln;
246
        }
247
 
248
        if (!good)
249
                depperr();
250
}
251
 
252
void    DEPPI::flush_read(void) {
253 45 dgisselq
        const   bool    dbg = false;
254
 
255
        if (dbg)        fprintf(dbgfp, "DEPPI::FLUSH-READ()\n");
256
 
257
        do {
258 11 dgisselq
                m_rbeg = m_rend = 0;
259 45 dgisselq
        } while(poll(4));
260
 
261
        if (dbg)        fprintf(dbgfp, "DEPPI::FLUSH-READ() -- COMPLETE\n");
262 11 dgisselq
}
263
 
264
void    DEPPI::push_fifo(char *buf, int len) {
265
        char    last = 0;
266
        char    *sptr = buf;
267 45 dgisselq
        const   bool    dbg = false;
268 11 dgisselq
 
269 45 dgisselq
        if (dbg)  fprintf(dbgfp, "DEPP::PUSH(%d)\n", len);
270 11 dgisselq
 
271
        if (m_rbeg != m_rend)
272
                last = m_rbuf[(m_rbeg-1)&RCV_BUFMASK];
273 45 dgisselq
        if (dbg)        fprintf(dbgfp, "DEPPI::PUSH() last=%d, rbeg=%d, rend=%d\n", last, m_rbeg, m_rend);
274 11 dgisselq
        for(int i=0; i<len; i++) {
275
                char v = *sptr++;
276
                if (((v & 0x80)||((unsigned char)v < 0x10))&&(v == last)) {
277
                        // Skipp any stuff bytes
278 45 dgisselq
                        if (dbg)  fprintf(dbgfp, "SKIPPING-1: %02x\n", v & 0x0ff);
279 11 dgisselq
                } else if ((unsigned char)v == 0x0ff) {
280
                        // Skipp any not-yet-ready bytes
281 45 dgisselq
                        if (dbg)  fprintf(dbgfp, "SKIPPING-2: %02x\n", 0x0ff);
282 11 dgisselq
                } else {
283
                        m_rbuf[m_rbeg] = v;
284 45 dgisselq
                        if (dbg) fprintf(dbgfp, "PUSHING: 0x%02x \'%c\'\n",
285
                                v&0x0ff, isprint(v)?v:'.');
286 11 dgisselq
                        m_rbeg = (m_rbeg+1)&(RCV_BUFMASK);
287
                } last = v;
288
        }
289
}
290
 
291
int     DEPPI::pop_fifo(char *buf, int len) {
292
        int     avail = (m_rbeg - m_rend)&(RCV_BUFMASK);
293
        int     left = len;
294
        int     nr = 0;
295 45 dgisselq
        const   bool    dbg = false;
296 11 dgisselq
 
297 45 dgisselq
        if (dbg) fprintf(dbgfp, "Attempting to pop %d items from FIFO (%d - %d)\n",
298
                        len, m_rend, m_rbeg);
299 11 dgisselq
        while((avail > 0)&&(left > 0)) {
300
                int ln = RCV_BUFLEN-m_rend;
301
                if (ln > left)
302
                        ln = left;
303
                if (ln > avail)
304
                        ln = avail;
305
                memcpy(&buf[len-left], &m_rbuf[m_rend], ln);
306
                left   -= ln;
307
                avail  -= ln;
308
                m_rend  = (m_rend + ln)&(RCV_BUFMASK);
309
                nr     += ln;
310
 
311
        }
312
 
313
        return nr;
314
}
315
 
316
bool    DEPPI::poll(unsigned ms) {
317
        int     avail = (m_rbeg-m_rend)&(RCV_BUFMASK);
318
        bool    r = true;
319 45 dgisselq
        const   bool    dbg = false;
320 11 dgisselq
 
321 45 dgisselq
        if (dbg) fprintf(dbgfp, "POLL\n");
322 11 dgisselq
        if ((avail < 2)&&((avail<1)||(m_rbuf[m_rend]&0x80)||(m_rbuf[m_rend]<0x10))) {
323 45 dgisselq
                if (dbg) fprintf(dbgfp, "POLL -- CALLS RAW READ\n");
324 11 dgisselq
                raw_read(4,ms);
325
                avail = (m_rbeg-m_rend)&(RCV_BUFMASK);
326
 
327
                if (avail != 0) {
328
                        // Read 'til there's nothing more to be read
329
                        char    v = (m_rbuf[(m_rbeg-1)&(RCV_BUFMASK)]);
330
                        while(((v&0x80)==0)&&((unsigned)v>=0x10)&&(avail < RCV_BUFMASK-32)) {
331
                                raw_read(26,ms);
332
                                if (avail == ((m_rbeg-m_rend)&(RCV_BUFMASK)))
333
                                        break; // We didn't read anything more
334
                                avail = (m_rbeg-m_rend)&(RCV_BUFMASK);
335 45 dgisselq
                                if (dbg) fprintf(dbgfp, "POLL/LOOP -- %d available\n", avail);
336 11 dgisselq
                        }
337
                        if (avail < 1)
338
                                r = false;
339
                        else if ((avail==1)&&((m_rbuf[m_rend]&0x80)||(m_rbuf[m_rend]<0x10)))
340
                                r = false;
341
                } else r = false;
342
        }
343 45 dgisselq
        if (dbg) fprintf(dbgfp, "POLL -- is %s\n", (r)?"true":"false");
344 11 dgisselq
 
345
        return r;
346
}

powered by: WebSVN 2.1.0

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