flash.cc
Go to the documentation of this file.
1 
37 #include "details/channel.hh"
38 #include "details/query.hh"
39 
43 
44 namespace crl {
45 namespace multisense {
46 namespace details {
47 
48 //
49 // Erase a flash region
50 
51 void impl::eraseFlashRegion(uint32_t region)
52 {
53  wire::SysFlashResponse response;
54 
55  //
56  // Start the erase operation
57 
59  response);
60  if (Status_Ok != status)
61  CRL_EXCEPTION("OP_ERASE failed: %d", status);
62 
63  //
64  // Check for success, or flash in progress
65 
66  switch(response.status) {
69  break; // ok, erase is happening
70  default:
71  CRL_EXCEPTION("OP_ERASE ack'd, but failed: %d\n", response.status);
72  }
73 
74  //
75  // Wait for the erase to complete
76 
77  const double ERASE_TIMEOUT = 210.0; // seconds
78 
80 
81  int prevProgress = -1;
82 
83  while((utility::TimeStamp::getCurrentTime() - start) < ERASE_TIMEOUT) {
84 
85  //
86  // Request current progress
87 
88  status = waitData(wire::SysFlashOp(), response);
89  if (Status_Ok != status)
90  CRL_EXCEPTION_RAW("failed to request flash erase status");
91 
92  //
93  // IDLE means the flash has been erased
94 
96  return; // success
97 
98  //
99  // Prompt and delay a bit
100 
101  if (response.erase_progress != prevProgress &&
102  0 == (response.erase_progress % 5))
103  CRL_DEBUG("erasing... %3d%%\n", response.erase_progress);
104  usleep(100000);
105 
106  prevProgress = response.erase_progress;
107  }
108 
109  CRL_EXCEPTION("erase op timed out after %.0f seconds", ERASE_TIMEOUT);
110 }
111 
112 //
113 // Program or verify a flash region from a file
114 
115 void impl::programOrVerifyFlashRegion(std::ifstream& file,
116  uint32_t operation,
117  uint32_t region)
118 {
119  //
120  // Get file size
121 
122  file.seekg(0, file.end);
123  std::streamoff fileLength = file.tellg();
124  file.seekg(0, file.beg);
125 
126  wire::SysFlashOp op(operation, region, 0,
128 
129  int prevProgress = -1;
130 
131  const char *opNameP;
132 
133  switch(operation) {
134  case wire::SysFlashOp::OP_PROGRAM: opNameP = "programming"; break;
135  case wire::SysFlashOp::OP_VERIFY: opNameP = "verifying"; break;
136  default:
137  CRL_EXCEPTION("unknown operation type: %d", operation);
138  }
139 
140  do {
141 
142  //
143  // Initialize data and read next chunk
144 
145  memset(op.data, 0xFF, op.length);
146  file.read((char *) op.data, op.length);
147 
148  //
149  // Send command, await response
150 
152 
153  Status status = waitData(op, rsp, 0.5, 4);
154  if (Status_Ok != status)
155  CRL_EXCEPTION("SysFlashOp (%s) failed: %d", opNameP, status);
157  unsigned int fileSize = (unsigned int)file.tellg();
158  CRL_EXCEPTION("%s failed @ %d/%d bytes", opNameP, fileSize, fileLength);
159  }
160 
161  //
162  // Print out progress
163 
164  int progress = static_cast<int> ((100 * op.start_address) / fileLength);
165  if (progress != prevProgress && 0 == (progress % 5))
166  CRL_DEBUG("%s... %3d%%\n", opNameP, progress);
167 
168  //
169  // Update state
170 
171  prevProgress = progress;
172  op.start_address += op.length;
173 
174  } while (!file.eof());
175 
176  if ((int) op.start_address < fileLength)
177  CRL_EXCEPTION("unexpected EOF while %s", opNameP);
178 
179  CRL_DEBUG("%s complete\n", opNameP);
180 }
181 
182 //
183 // Wrapper for all flash operations
184 
185 Status impl::doFlashOp(const std::string& filename,
186  uint32_t operation,
187  uint32_t region)
188 {
189  try {
190  std::ifstream file(filename.c_str(),
191  std::ios::in | std::ios::binary);
192 
193  if (!file.good())
194  CRL_EXCEPTION("unable to open file: \"%s\"",
195  filename.c_str());
196 
197  if (wire::SysFlashOp::OP_PROGRAM == operation)
198  eraseFlashRegion(region);
199 
200  programOrVerifyFlashRegion(file, operation, region);
201 
202  } catch (const std::exception& e) {
203  CRL_DEBUG("exception: %s\n", e.what());
204  return Status_Exception;
205  }
206 
207  return Status_Ok;
208 }
209 
210 }}} // namespaces
static CRL_CONSTEXPR uint32_t OP_PROGRAM
#define CRL_EXCEPTION(fmt,...)
Definition: Exception.hh:71
static CRL_CONSTEXPR uint32_t OP_ERASE
static CRL_CONSTEXPR uint32_t MAX_LENGTH
void eraseFlashRegion(uint32_t region)
Definition: flash.cc:51
#define CRL_EXCEPTION_RAW(fmt)
Definition: Exception.hh:77
static CRL_CONSTEXPR uint32_t OP_VERIFY
Definition: channel.cc:56
static CRL_CONSTEXPR Status Status_Ok
#define CRL_DEBUG(fmt,...)
Definition: Exception.hh:83
static CRL_CONSTEXPR Status Status_Exception
Status waitData(const T &command, U &data, const double &timeout=DEFAULT_ACK_TIMEOUT(), int32_t attempts=DEFAULT_ACK_ATTEMPTS)
Definition: query.hh:120
void programOrVerifyFlashRegion(std::ifstream &file, uint32_t operation, uint32_t region)
Definition: flash.cc:115
Status doFlashOp(const std::string &filename, uint32_t operation, uint32_t region)
Definition: flash.cc:185


multisense_lib
Author(s):
autogenerated on Sun Mar 14 2021 02:34:50