ngc 1.4.0
 
Loading...
Searching...
No Matches
ngcbMODIO.hpp
Go to the documentation of this file.
1
7#ifndef ngcbMODIO_H
8#define ngcbMODIO_H
9
10
11#ifndef __cplusplus
12#error This is a C++ include file and cannot be used from plain C
13#endif
14
15#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18#include <unistd.h>
19#include <fcntl.h>
20#include <errno.h>
21
22#include <ngc/core/ngcbDrv.h>
23
24#ifdef _PTHREAD_H
25#define ngcbMODIO_THR 1
26#endif
27
36class ngcbMODIO {
37public:
40 erms_[0] = '\0'; path_[0] = '\0'; ngcbModInit(&mod_);
41 }
42
50 explicit ngcbMODIO(const char *path) {
51 erms_[0] = '\0'; path_[0] = '\0'; ngcbModInit(&mod_);
52 if (path != NULL) strcpy(path_, path);
53 }
54
56 virtual ~ngcbMODIO() {
57 if (mod_.fd >= 0) Close();
58 }
59
61 const char *ErrMsg() {return (erms_);}
62
64 void Sim(int stat) {
65 if (stat) mod_.flags |= 0x1;
66 else mod_.flags &= ~0x1;
67 }
68
70 void Verbose(int stat) {
71 if (stat) mod_.flags |= 0x2;
72 else mod_.flags &= ~0x2;
73 }
74
76 void Period(unsigned int p) {mod_.period = p;}
77
79 int Open() {return (Open(path_));}
80
82 int Open(const char *path) {
83 if (mod_.fd >= 0)
84 {
85 strcpy(erms_, "device is already open");
86 return (-1);
87 }
88 else if (ngcbModOpen(path, &mod_, erms_) < 0)
89 {
90 return (-1);
91 }
92 else
93 {
94 char s[256];
95 strcpy(s, path);
96 strcpy(path_, s);
97 return (0);
98 }
99 }
100
102 int Close() {return (ngcbModClose(&mod_, erms_));}
103
105 int Timeout(int seconds) {return (ngcbModTimeout(&mod_, seconds, erms_));}
106
114 int Fifo(int size) {return (ngcbModFifo(&mod_, size, erms_));}
115
117 int Read(int addr, int *buffer, int size) {
118 return (ngcbModRead(&mod_, addr, buffer, size, erms_));
119 }
120
122 int Write(int addr, const int *buffer, int size) {
123 return (ngcbModWrite(&mod_, addr, buffer, size, erms_));
124 }
125
130 int Wait(int addr, int mask, void *lock = NULL) {
131 return (Wait(addr, mask, -1, lock));
132 }
133
141 int Wait(int addr, int mask, int timeout, void *lock = NULL) {
142#ifdef ngcbMODIO_THR
143 unsigned long int p = 1000 * mod_.period; /* polling period */
144 long int n = (timeout * 1000000) / p; /* polling cycles */
145 int reg = 0x0; /* register value */
146 pthread_mutex_t *m = (pthread_mutex_t *)lock; /* mutex (locked) */
147
148 /*
149 * Read register once
150 */
151 if (Read(addr, &reg, 1) < 0)
152 {
153 return (-1);
154 }
155
156 while (((reg & mask) != mask) && (n != 0))
157 {
158 if (m != NULL)
159 {
160 /*
161 * Unlock the mutex while sleeping
162 */
163 if (pthread_mutex_unlock(m) != 0)
164 {
165 sprintf(erms_, "mutex unlock failed - %s", strerror(errno));
166 return (-1);
167 }
168 }
169
170 /*
171 * Sleep for polling interval...
172 */
173 MicroSleep_(p);
174
175 if (m != NULL)
176 {
177 /*
178 * Lock the mutex before reading
179 */
180 if (pthread_mutex_lock(m) != 0)
181 {
182 sprintf(erms_, "mutex lock failed - %s", strerror(errno));
183 return (-1);
184 }
185 }
186
187 /*
188 * ...and read again
189 */
190 if (Read(addr, &reg, 1) < 0)
191 {
192 return (-1);
193 }
194
195 if (n > 0) n--;
196 }
197
198 if ((reg & mask) != mask)
199 {
200 /*
201 * If the bit-mask is not yet set then our timer has expired
202 */
203 strcpy(erms_, "timeout");
204 return (-1);
205 }
206
207 return (0);
208#else
209 USE(lock);
210 return (ngcbModWait(&mod_, addr, mask, timeout, erms_));
211#endif
212 }
213
214protected:
215
216private:
218 char erms_[256];
219
221 char path_[256];
222
224 ngcb_mod_t mod_;
225
226#ifdef ngcbMODIO_THR
228 void MicroSleep_(unsigned long int ms) {
229 if (ms > 0)
230 {
231 struct timeval t;
232 t.tv_sec = ms / 1000000;
233 ms -= t.tv_sec * 1000000;
234 t.tv_usec = ms;
235 select(0, NULL, NULL, NULL, &t);
236 }
237 }
238#endif
239};
240
241#endif
int Fifo(int size)
Definition ngcbMODIO.hpp:114
void Verbose(int stat)
Set verbose mode (0 or 1)
Definition ngcbMODIO.hpp:70
int Wait(int addr, int mask, void *lock=NULL)
Definition ngcbMODIO.hpp:130
int Open()
Open module using the saved path.
Definition ngcbMODIO.hpp:79
int Wait(int addr, int mask, int timeout, void *lock=NULL)
Definition ngcbMODIO.hpp:141
int Write(int addr, const int *buffer, int size)
Write buffer to address - size is given in words (32-bit)
Definition ngcbMODIO.hpp:122
void Period(unsigned int p)
Set polling period (milliseconds)
Definition ngcbMODIO.hpp:76
ngcbMODIO()
Trivial constructor.
Definition ngcbMODIO.hpp:39
void Sim(int stat)
Set simulation mode (0 or 1)
Definition ngcbMODIO.hpp:64
virtual ~ngcbMODIO()
Destructor.
Definition ngcbMODIO.hpp:56
int Timeout(int seconds)
Set communication timeout.
Definition ngcbMODIO.hpp:105
int Open(const char *path)
Open module path.
Definition ngcbMODIO.hpp:82
int Close()
Shutdown module interface.
Definition ngcbMODIO.hpp:102
const char * ErrMsg()
Return error message.
Definition ngcbMODIO.hpp:61
ngcbMODIO(const char *path)
Definition ngcbMODIO.hpp:50
int Read(int addr, int *buffer, int size)
Read buffer from address - size is given in words (32-bit)
Definition ngcbMODIO.hpp:117
int ngcbModWrite(ngcb_mod_t *m, int addr, const int *buffer, int size, char *erms)
Definition ngcbDrvMod.c:454
int ngcbModWait(ngcb_mod_t *m, int addr, int mask, int timeout, char *erms)
Definition ngcbDrvMod.c:601
int ngcbModFifo(ngcb_mod_t *m, int size, char *erms)
Definition ngcbDrvMod.c:307
int ngcbModRead(ngcb_mod_t *m, int addr, int *buffer, int size, char *erms)
Definition ngcbDrvMod.c:326
int ngcbModOpen(const char *path, ngcb_mod_t *m, char *erms)
Definition ngcbDrvMod.c:55
void ngcbModInit(ngcb_mod_t *m)
Definition ngcbDrvMod.c:43
int ngcbModTimeout(ngcb_mod_t *m, int seconds, char *erms)
Definition ngcbDrvMod.c:285
int ngcbModClose(ngcb_mod_t *m, char *erms)
Definition ngcbDrvMod.c:248
#define USE(x)
Definition ngcb.h:28
Definition ngcbDrv.h:221