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