Cat
SpecsSlave.cpp
Go to the documentation of this file.
1 //$Id: SpecsSlave.cpp,v 1.10 2006/07/03 09:02:07 fmachefe Exp $
2 //------------------------------------------------------------------------------
3 //
4 // Package : SpecsSlave
5 //
6 // Description:
7 //
8 // Author(s) : F. Machefert
9 // Date : 12 February 2004
10 //
11 //------------------------------------------------------------------------------
12 
13 // CATCore include files
14 #include "Tools.h"
15 #include "SpecsI2c.h"
16 #include "SpecsParallelBus.h"
17 
18 // Specslib include files
19 #ifndef _NODEVICE_
20 #include "Specs.h"
21 #endif
22 
23 // CATCore include files
24 #include "SpecsMaster.h"
25 
26 // local include file
27 #include "SpecsSlave.h"
28 
29 //=============================================================================
30 //
31 //=============================================================================
33  debug("SpecsSlave built.","SpecsSlave::SpecsSlave");
34 
35  setAddress(0x0);
36  setType("SpecsSlave");
37  setId(0);
39 }
40 
41 //=============================================================================
42 //
43 //=============================================================================
45 }
46 
47 //=============================================================================
48 //
49 //=============================================================================
50 void SpecsSlave::setAddress(unsigned char add){
51  m_address=add;
52 #ifndef _NODEVICE_
53  specsSlave.SpecsslaveAdd=add;
54 #endif
55 }
56 
57 //=============================================================================
58 //
59 //=============================================================================
60 #ifndef _NODEVICE_
62  return &specsSlave;
63 }
64 #endif
65 
66 //=============================================================================
67 //
68 //=============================================================================
70 #ifndef _NODEVICE_
71  SPECSMASTER* master=specsMasterDevice();
72  if (0!=master){
73  SpecsError status = specs_slave_open ( master, address() , &specsSlave );
74  if (0!=status){
75  warning("SpecsSlave " + name() + "could not be initialized correctly [code:" +
76  itos(status) + "].","SpecsSlave::initSpecs");
77  return StatusCode::FAILURE;
78  }
79  else{
80  int nAttempts = 10;
81  for (int i=0 ; i<nAttempts ; ++i){
82  U16 rd;
83  U16 wr = (U16)(rand()&&0xFFFC);
84  // specsWriteRegister(3,wr);
85  // specsReadRegister (3,rd);
86  rd=wr;
87  if (wr!=rd){
88  warning("Specs Slave "+name()+" [Add="+itos (address())+"] : wrong connection [attempt "+
89  itos(i)+"/"+itos (nAttempts)+"]. Further use of this slave is hazardeous.",
90  "SpecsSlave::init");
91  return StatusCode::FAILURE;
92  }
93  }
94  info("SpecsSlave "+name()+" initialized and connected to master.","SpecsSlave::init");
95  }
96  }
97  else{
98  fatal("Could not initialize SpecsSlave "+name()+".","SpecsSlave::init");
99  return StatusCode::FAILURE;
100  }
101 #else
102  debug("Compilation in the _NODEVICE_ mode.","SpecsSlave::init");
103 #endif
104  resetInternal();
105  reset();
106  return StatusCode::SUCCESS;
107 }
108 
116 //=============================================================================
117 //
118 //=============================================================================
120 #ifndef _NODEVICE_
121  SPECSMASTER* master=specsMasterDevice();
122  if (0!=master){
123  SpecsError status = specs_slave_open ( master, address() , &specsSlave );
124  if (0!=status){
125  info("SpecsSlave "+name()+"could not be initialized correctly [code:"+
126  itos(status) + "].","initSpecs");
127  return false;
128  }
129  else{
130  SPECSSLAVE *specsSlv=specsSlaveDevice();
131  if ( 0==specsSlv ){
132  warning("Could not reach proper Specs Master and/or Slave.",
133  "SpecsElement::specsWriteI2c");
134  return false;
135  }
136 
137  U16 value ;
138  U8 reg = 8 ;
139  status = specs_register_read ( specsSlv , reg , &value );
140 
141  if (0!=status){
142  debug("No SpecsSlave detected at address " +
143  itos (address()) + "[code=" + itos(status) + "]",
144  "specsSlave::detect");
145  specsMaster()->reset();
146  return false;
147  }
148  else {
149  info("SpecsSlave detected at address " +
150  itos (address()) + "[date=" + itos(value)+"]",
151  "specsSlave::detect");
152  return true;
153  }
154  }
155  }
156 #endif
157  return false;
158 }
159 
160 //=============================================================================
161 //
162 //=============================================================================
164  init();
165  std::vector<Hierarchy*> list = this->children();
166  std::vector<Hierarchy*>::iterator iter;
167  for (iter=list.begin() ; iter!=list.end() ; ++iter){
168  dynamic_cast<Element*>((*iter))->recursiveInitCommunications();
169  }
170 }
171 
172 //=============================================================================
173 //
174 //=============================================================================
176  element->setParent(i2c);
177  i2c->addChild(element);
178  debug(element->name()+" added to the i2c child tree.","SpecsSlave::addI2c");
179 }
180 
181 //=============================================================================
182 //
183 //=============================================================================
185 #ifndef _NODEVICE_
186  SPECSSLAVE *specsSlv=specsSlaveDevice();
187  SpecsError status = specs_slave_internal_reset ( specsSlv );
188  if (0!=status){
189  warning("SpecsSlave " + name() +
190  ": failure in performing the internal reset [code:" +
191  itos(status) + "].",
192  "SpecsSlave::reset" ); }
193  else{
194  info("internal reset of the specs slave "+
195  name()+" "+" [address="+itos(address())+"].",
196  "SpecsSlave::resetInternal");
197  }
198 #endif
199 }
200 
201 //=============================================================================
202 //
203 //=============================================================================
205 #ifndef _NODEVICE_
206  SPECSSLAVE *specsSlv=specsSlaveDevice();
207  SpecsError status = specs_slave_external_reset ( specsSlv );
208  if (0!=status){
209  warning("SpecsSlave " + name() +
210  ": failure in performing the SpecsSlave reset [code:" +
211  itos(status) + "].",
212  "SpecsSlave::reset" ); }
213  else{
214  debug("reset of the specs slave "+
215  name()+" "+" [address="+itos(address())+"]." ,
216  "SpecsSlave::reset");
217  }
218 #endif
219 }
void info(std::string mymsg)
Definition: Object.h:38
std::string itos(int)
Definition: Tools.cpp:46
void reset()
Definition: SpecsSlave.cpp:204
SpecsI2c * i2c
Definition: SpecsSlave.h:28
void setParent(Hierarchy *parent)
Definition: Hierarchy.cpp:67
virtual SpecsMaster * specsMaster()
void add(int attribut)
Definition: Attrib.h:67
void recursiveInitCommunications()
Definition: SpecsSlave.cpp:163
std::vector< Hierarchy * > children()
Definition: Hierarchy.h:33
void fatal(std::string mymsg)
Definition: Object.h:41
unsigned char address()
void resetInternal()
Definition: SpecsSlave.cpp:184
void setType(std::string type)
Definition: Object.h:52
unsigned char U8
Definition: ICECALv3.h:55
void setId(unsigned char id)
Definition: Object.h:53
void debug(std::string mymsg)
Definition: Object.h:37
unsigned char m_address
SPECSSLAVE * specsSlaveDevice()
Definition: SpecsSlave.cpp:61
virtual SPECSMASTER * specsMasterDevice()
virtual SpecsSlave * specsSlave()
void setAddress(unsigned char)
Definition: SpecsSlave.cpp:50
std::string name() const
Definition: Object.h:28
StatusCode init()
Definition: SpecsSlave.cpp:69
virtual void addChild(Hierarchy *element)
Definition: Hierarchy.cpp:83
unsigned short U16
Definition: ICECALv3.h:54
void warning(std::string mymsg)
Definition: Object.h:39
void addI2c(Hierarchy *)
Definition: SpecsSlave.cpp:175
bool detect()
Definition: SpecsSlave.cpp:119