mrfioc2  2.3.0
Functions | Variables
flashiocsh.cpp File Reference
#include <stdexcept>
#include <vector>
#include <fstream>
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <iocsh.h>
#include <epicsThread.h>
#include <epicsStdio.h>
#include "mrfCommon.h"
#include "mrf/spi.h"
#include "mrf/flash.h"
#include <epicsExport.h>
Include dependency graph for flashiocsh.cpp:

Go to the source code of this file.

Functions

void flashinfo (const char *name)
 
void flashread (const char *name, int addrraw, int countraw, const char *outfile)
 
void flashwrite (const char *name, int addrraw, const char *infile)
 
void flasherase (const char *name, int addrraw, int countraw)
 
 epicsExportRegistrar (registrarFlashOps)
 
 epicsExportAddress (int, flashAcknowledgeMismatch)
 

Variables

int flashAcknowledgeMismatch
 

Function Documentation

◆ epicsExportAddress()

epicsExportAddress ( int  ,
flashAcknowledgeMismatch   
)

◆ epicsExportRegistrar()

epicsExportRegistrar ( registrarFlashOps  )

◆ flasherase()

void flasherase ( const char *  name,
int  addrraw,
int  countraw 
)

Definition at line 229 of file flashiocsh.cpp.

230 {
231  try {
232  mrf::SPIDevice dev;
233  if(!mrf::SPIDevice::lookupDev(name, &dev)) {
234  printf("No such device");
235  return;
236  }
237  epicsUInt32 addr = addrraw, count = countraw;
238 
239  mrf::CFIFlash mem(dev);
240 
241  mem.erase(addr, count);
242 
243  printf("\nDone\n");
244 
245  }catch(std::exception& e){
246  printf("Error: %s\n", e.what());
247  }
248 }
Handling for Common Flash Interfafce compliant chips.
Definition: flash.h:23
Definition: devObj.h:97
static bool lookupDev(const std::string &name, SPIDevice *)
Definition: spi.cpp:74

◆ flashinfo()

void flashinfo ( const char *  name)

Definition at line 36 of file flashiocsh.cpp.

37 {
38  try {
39  mrf::SPIDevice dev;
40  if(!mrf::SPIDevice::lookupDev(name, &dev)) {
41  printf("No such device");
42  return;
43  }
44 
45  mrf::CFIFlash mem(dev);
46 
47  mrf::CFIFlash::ID info;
48 
49  mem.readID(&info);
50 
51  printf("Vendor: %02x (%s)\nDevice: %02x\nID: %02x\n",
52  info.vendor, info.vendorName, info.dev_type, info.dev_id);
53  if(info.capacity)
54  printf("Capacity: 0x%x\n", (unsigned)info.capacity);
55  if(info.sectorSize)
56  printf("Sector: 0x%x\n", (unsigned)info.sectorSize);
57  if(info.pageSize)
58  printf("Page: 0x%x\n", (unsigned)info.pageSize);
59  if(!info.SN.empty()) {
60  printf("S/N: ");
61  for(size_t i=0; i<info.SN.size(); i++)
62  printf(" %02x", unsigned(info.SN[i]));
63  printf("\n");
64  }
65 
66  if(!info.capacity || !info.sectorSize || !info.pageSize) {
67  printf("Warning: Some information about this flash chip is not known.\n"
68  " Please open an issue and include this output of flashinfo()\n"
69  " and if known the flash chip vendor and part number.\n"
70  " https://github.com/epics-modules/mrfioc2/issues\n");
71  }
72 
73  {
74  mrf::CFIStreamBuf sbuf(mem);
75  std::istream strm(&sbuf);
76  mrf::XilinxBitInfo image;
77  if(image.read(strm)) {
78  printf("Bit Stream found\n Project: %s\n Part: %s\n Build: %s\n",
79  image.project.c_str(), image.part.c_str(), image.date.c_str());
80  }
81  }
82 
83  }catch(std::exception& e){
84  printf("Error: %s\n", e.what());
85  }
86 }
epicsUInt32 pageSize
PAGE PROGRAM (0x02) size in bytes. Always a power of 2.
Definition: flash.h:38
const char * vendorName
Definition: flash.h:37
std::string date
Definition: flash.h:110
epicsUInt32 capacity
total capacity in bytes
Definition: flash.h:38
Attempt to read out the header of a Xilinx bitstream file.
Definition: flash.h:104
Handling for Common Flash Interfafce compliant chips.
Definition: flash.h:23
epicsUInt32 sectorSize
SECTOR ERASE (0xd8) size in bytes. Always a power of 2.
Definition: flash.h:38
std::string part
Definition: flash.h:110
std::vector< epicsUInt8 > SN
Definition: flash.h:42
epicsUInt8 dev_type
Definition: flash.h:33
std::string project
Definition: flash.h:110
epicsUInt8 vendor
Definition: flash.h:33
bool read(std::istream &strm)
Definition: flash.cpp:405
static bool lookupDev(const std::string &name, SPIDevice *)
Definition: spi.cpp:74
Adapt CFIFlash for use with std::istream.
Definition: flash.h:89
epicsUInt8 dev_id
Definition: flash.h:33

◆ flashread()

void flashread ( const char *  name,
int  addrraw,
int  countraw,
const char *  outfile 
)

Definition at line 90 of file flashiocsh.cpp.

91 {
92  try {
93  mrf::SPIDevice dev;
94  if(!mrf::SPIDevice::lookupDev(name, &dev)) {
95  printf("No such device");
96  return;
97  }
98  epicsUInt32 addr = addrraw, count = countraw;
99 
100  mrf::CFIFlash mem(dev);
101 
102  std::ostream *strm = &std::cout;
103  std::ofstream fstrm;
104  if(outfile) {
105  fstrm.open(outfile, std::ios_base::out|std::ios_base::binary);
106  if(fstrm.fail())
107  throw std::runtime_error("Unable to open output file");
108  strm = &fstrm;
109  }
110 
111  while(count) {
112  const epicsUInt32 N = std::min(count, mem.blockSize());
113  std::vector<epicsUInt8> buf(N);
114 
115  mem.read(addr, buf);
116 
117  if(outfile) {
118  (*strm).write((const char*)&buf[0], buf.size());
119 
120  } else {
121  for(size_t i=0; i<buf.size(); i++) {
122  printf("%02x", int(buf[i]));
123  if(i%16==15)
124  printf("\n");
125  else if(i%4==3)
126  printf(" ");
127  }
128  }
129 
130  addr += N;
131  count -= N;
132  if(outfile)
133  printf("| %u\n", unsigned(count));
134  }
135 
136  printf("\nDone\n");
137 
138  }catch(std::exception& e){
139  printf("Error: %s\n", e.what());
140  }
141 }
Handling for Common Flash Interfafce compliant chips.
Definition: flash.h:23
Definition: devObj.h:97
static bool lookupDev(const std::string &name, SPIDevice *)
Definition: spi.cpp:74

◆ flashwrite()

void flashwrite ( const char *  name,
int  addrraw,
const char *  infile 
)

Definition at line 145 of file flashiocsh.cpp.

146 {
147  if(!infile || infile[0]=='\0') {
148  printf("Usage: flashwrite <name> <start_address> <filename>\n");
149  return;
150  }
151 
152  try {
153  mrf::SPIDevice dev;
154  if(!mrf::SPIDevice::lookupDev(name, &dev)) {
155  printf("No such device");
156  return;
157  }
158  epicsUInt32 addr = addrraw;
159 
160  mrf::CFIFlash mem(dev);
161  if(!mem.writable()) {
162  printf("Device not writable\n");
163  return;
164  }
165 
166  std::ifstream strm(infile, std::ios_base::in|std::ios_base::binary);
167  if(strm.fail())
168  throw std::runtime_error("Unable to open output file");
169 
170  strm.seekg(0, std::ios_base::end);
171  const long fsize = strm.tellg();
172  strm.seekg(0);
173 
174  if(flashAcknowledgeMismatch!=2) {
175  mrf::XilinxBitInfo infile, inmem;
176 
177  infile.read(strm);
178  strm.seekg(0);
179 
180  mrf::CFIStreamBuf sbuf(mem);
181  std::istream mstrm(&sbuf);
182  mstrm.seekg(addr);
183  inmem.read(mstrm);
184 
185  bool match = true;
186  match &= infile.project.empty() || infile.project==inmem.project;
187  match &= infile.part.empty() || infile.part==inmem.part;
188  if(!match) {
189  fprintf(stderr, "Bitstream header mis-match.\nFile: \"%s\", \"%s\"\nROM: \"%s\", \"%s\"\n",
190  infile.part.c_str(), infile.project.c_str(),
191  inmem.part.c_str(), inmem.project.c_str());
192 
194  fprintf(stderr, "To override, re-run after setting: var(\"flashAcknowledgeMismatch\", 1)\n");
195  return;
196  }
197  }
198  }
199  // user must re-ack for each operation
201 
202  std::vector<epicsUInt8> buf;
203 
204  long pos=0;
205  while(strm.good()) {
206  printf("| %u/%u\n", (unsigned)pos, (unsigned)fsize);
207 
208  buf.resize(mem.blockSize());
209  buf.resize(strm.read((char*)&buf[0], buf.size()).gcount());
210  if(strm.bad())
211  throw std::runtime_error("I/O Error");
212  if(buf.empty())
213  break;
214  pos += buf.size();
215 
216  mem.write(addr, buf, false);
217  addr += buf.size();
218  }
219 
220  printf("\nDone\n");
221 
222  }catch(std::exception& e){
223  printf("Error: %s\n", e.what());
224  }
225 }
int flashAcknowledgeMismatch
Definition: flashiocsh.cpp:32
Attempt to read out the header of a Xilinx bitstream file.
Definition: flash.h:104
Handling for Common Flash Interfafce compliant chips.
Definition: flash.h:23
std::string part
Definition: flash.h:110
Definition: devObj.h:97
std::string project
Definition: flash.h:110
bool read(std::istream &strm)
Definition: flash.cpp:405
static bool lookupDev(const std::string &name, SPIDevice *)
Definition: spi.cpp:74
Adapt CFIFlash for use with std::istream.
Definition: flash.h:89

Variable Documentation

◆ flashAcknowledgeMismatch

int flashAcknowledgeMismatch

Definition at line 32 of file flashiocsh.cpp.