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("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  CRL_EXCEPTION("%s failed @ %d/%d bytes", opNameP,
158  file.tellg(), fileLength);
159 
160  //
161  // Print out progress
162 
163  int progress = static_cast<int> ((100 * op.start_address) / fileLength);
164  if (progress != prevProgress && 0 == (progress % 5))
165  CRL_DEBUG("%s... %3d%%\n", opNameP, progress);
166 
167  //
168  // Update state
169 
170  prevProgress = progress;
171  op.start_address += op.length;
172 
173  } while (!file.eof());
174 
175  if ((int) op.start_address < fileLength)
176  CRL_EXCEPTION("unexpected EOF while %s", opNameP);
177 
178  CRL_DEBUG("%s complete\n", opNameP);
179 }
180 
181 //
182 // Wrapper for all flash operations
183 
184 Status impl::doFlashOp(const std::string& filename,
185  uint32_t operation,
186  uint32_t region)
187 {
188  try {
189  std::ifstream file(filename.c_str(),
190  std::ios::in | std::ios::binary);
191 
192  if (!file.good())
193  CRL_EXCEPTION("unable to open file: \"%s\"",
194  filename.c_str());
195 
196  if (wire::SysFlashOp::OP_PROGRAM == operation)
197  eraseFlashRegion(region);
198 
199  programOrVerifyFlashRegion(file, operation, region);
200 
201  } catch (const std::exception& e) {
202  CRL_DEBUG("exception: %s\n", e.what());
203  return Status_Exception;
204  }
205 
206  return Status_Ok;
207 }
208 
209 }}}; // 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
static CRL_CONSTEXPR uint32_t OP_VERIFY
Definition: channel.cc:56
static CRL_CONSTEXPR Status Status_Ok
#define CRL_DEBUG(fmt,...)
Definition: Exception.hh:77
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:184


multisense_lib
Author(s):
autogenerated on Sat Apr 6 2019 02:16:46