18 #include <epicsThread.h> 19 #include <epicsStdio.h> 23 #include <epicsStdio.h> 29 #include <epicsExport.h> 41 printf(
"No such device");
51 printf(
"Vendor: %02x (%s)\nDevice: %02x\nID: %02x\n",
54 printf(
"Capacity: 0x%x\n", (
unsigned)info.
capacity);
56 printf(
"Sector: 0x%x\n", (
unsigned)info.
sectorSize);
58 printf(
"Page: 0x%x\n", (
unsigned)info.
pageSize);
59 if(!info.
SN.empty()) {
61 for(
size_t i=0; i<info.
SN.size(); i++)
62 printf(
" %02x",
unsigned(info.
SN[i]));
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");
75 std::istream strm(&sbuf);
77 if(image.
read(strm)) {
78 printf(
"Bit Stream found\n Project: %s\n Part: %s\n Build: %s\n",
83 }
catch(std::exception& e){
84 printf(
"Error: %s\n", e.what());
90 void flashread(
const char *name,
int addrraw,
int countraw,
const char *outfile)
95 printf(
"No such device");
98 epicsUInt32
addr = addrraw, count = countraw;
102 std::ostream *strm = &std::cout;
105 fstrm.open(outfile, std::ios_base::out|std::ios_base::binary);
107 throw std::runtime_error(
"Unable to open output file");
112 const epicsUInt32
N = std::min(count, mem.
blockSize());
113 std::vector<epicsUInt8> buf(N);
118 (*strm).write((
const char*)&buf[0], buf.size());
121 for(
size_t i=0; i<buf.size(); i++) {
122 printf(
"%02x",
int(buf[i]));
133 printf(
"| %u\n",
unsigned(count));
138 }
catch(std::exception& e){
139 printf(
"Error: %s\n", e.what());
145 void flashwrite(
const char *name,
int addrraw,
const char *infile)
147 if(!infile || infile[0]==
'\0') {
148 printf(
"Usage: flashwrite <name> <start_address> <filename>\n");
155 printf(
"No such device");
158 epicsUInt32
addr = addrraw;
162 printf(
"Device not writable\n");
166 std::ifstream strm(infile, std::ios_base::in|std::ios_base::binary);
168 throw std::runtime_error(
"Unable to open output file");
170 strm.seekg(0, std::ios_base::end);
171 const long fsize = strm.tellg();
181 std::istream mstrm(&sbuf);
187 match &= infile.
part.empty() || infile.
part==inmem.
part;
189 fprintf(stderr,
"Bitstream header mis-match.\nFile: \"%s\", \"%s\"\nROM: \"%s\", \"%s\"\n",
194 fprintf(stderr,
"To override, re-run after setting: var(\"flashAcknowledgeMismatch\", 1)\n");
202 std::vector<epicsUInt8> buf;
206 printf(
"| %u/%u\n", (
unsigned)pos, (
unsigned)fsize);
209 buf.resize(strm.read((
char*)&buf[0], buf.size()).gcount());
211 throw std::runtime_error(
"I/O Error");
216 mem.
write(addr, buf,
false);
222 }
catch(std::exception& e){
223 printf(
"Error: %s\n", e.what());
234 printf(
"No such device");
237 epicsUInt32
addr = addrraw, count = countraw;
241 mem.
erase(addr, count);
245 }
catch(std::exception& e){
246 printf(
"Error: %s\n", e.what());
251 static const iocshArg flashinfoArg0 = {
"device",iocshArgString};
252 static const iocshArg *
const flashinfoArgs[1] =
254 static const iocshFuncDef flashinfoFuncDef =
255 {
"flashinfo",1,flashinfoArgs};
257 static void flashinfoCall(
const iocshArgBuf *
args)
262 static const iocshArg flashreadArg0 = {
"device",iocshArgString};
263 static const iocshArg flashreadArg1 = {
"address",iocshArgInt};
264 static const iocshArg flashreadArg2 = {
"count",iocshArgInt};
265 static const iocshArg flashreadArg3 = {
"file",iocshArgString};
266 static const iocshArg *
const flashreadArgs[4] =
267 {&flashreadArg0,&flashreadArg1,&flashreadArg2,&flashreadArg3};
268 static const iocshFuncDef flashreadFuncDef =
269 {
"flashread",4,flashreadArgs};
271 static void flashreadCall(
const iocshArgBuf *
args)
273 flashread(args[0].sval, args[1].ival, args[2].ival, args[3].sval);
276 static const iocshArg flashwriteArg0 = {
"device",iocshArgString};
277 static const iocshArg flashwriteArg1 = {
"address",iocshArgInt};
278 static const iocshArg flashwriteArg2 = {
"file",iocshArgString};
279 static const iocshArg *
const flashwriteArgs[3] =
280 {&flashwriteArg0,&flashwriteArg1,&flashwriteArg2};
281 static const iocshFuncDef flashwriteFuncDef =
282 {
"flashwrite",3,flashwriteArgs};
284 static void flashwriteCall(
const iocshArgBuf *
args)
286 flashwrite(args[0].sval, args[1].ival, args[2].sval);
289 static const iocshArg flasheraseArg0 = {
"device",iocshArgString};
290 static const iocshArg flasheraseArg1 = {
"address",iocshArgInt};
291 static const iocshArg flasheraseArg2 = {
"count",iocshArgInt};
292 static const iocshArg *
const flasheraseArgs[3] =
293 {&flasheraseArg0,&flasheraseArg1,&flasheraseArg2};
294 static const iocshFuncDef flasheraseFuncDef =
295 {
"flasherase",3,flasheraseArgs};
297 static void flasheraseCall(
const iocshArgBuf *
args)
299 flasherase(args[0].sval, args[1].ival, args[2].ival);
302 static void registrarFlashOps()
304 iocshRegister(&flashinfoFuncDef, &flashinfoCall);
305 iocshRegister(&flashreadFuncDef, &flashreadCall);
306 iocshRegister(&flashwriteFuncDef, &flashwriteCall);
307 iocshRegister(&flasheraseFuncDef, &flasheraseCall);
int flashAcknowledgeMismatch
epicsUInt32 pageSize
PAGE PROGRAM (0x02) size in bytes. Always a power of 2.
void flashread(const char *name, int addrraw, int countraw, const char *outfile)
void flashinfo(const char *name)
void readID(ID *id)
execute command 0x9f JEDEC-ID read
epicsUInt32 capacity
total capacity in bytes
Attempt to read out the header of a Xilinx bitstream file.
Handling for Common Flash Interfafce compliant chips.
epicsUInt32 sectorSize
SECTOR ERASE (0xd8) size in bytes. Always a power of 2.
epicsExportAddress(int, flashAcknowledgeMismatch)
epicsExportRegistrar(registrarFlashOps)
std::vector< epicsUInt8 > SN
void flashwrite(const char *name, int addrraw, const char *infile)
bool read(std::istream &strm)
void write(epicsUInt32 start, epicsUInt32 count, const epicsUInt8 *out, bool strict=true)
static bool lookupDev(const std::string &name, SPIDevice *)
Adapt CFIFlash for use with std::istream.
void flasherase(const char *name, int addrraw, int countraw)
void erase(epicsUInt32 start, epicsUInt32 count, bool strict=true)
void read(epicsUInt32 start, epicsUInt32 count, epicsUInt8 *in)