mrfioc2  2.3.0
flash.h
Go to the documentation of this file.
1 /*************************************************************************\
2 * Copyright (c) 2017 Michael Davidsaver
3 * mrfioc2 is distributed subject to a Software License Agreement found
4 * in file LICENSE that is included with this distribution.
5 \*************************************************************************/
6 #ifndef FLASH_H
7 #define FLASH_H
8 
9 #include <vector>
10 #include <string>
11 #include <istream>
12 
13 #include <epicsTypes.h>
14 #include <shareLib.h>
15 
16 #include <mrfCommon.h>
17 
18 namespace mrf {
19 
20 struct SPIDevice;
21 
23 class epicsShareClass CFIFlash
24 {
25 public:
26  explicit CFIFlash(const SPIDevice& dev);
27  ~CFIFlash();
28 
29  struct ID {
30  /* vendors
31  * 0x20 - Micron
32  */
33  epicsUInt8 vendor,
34  dev_type,
35  dev_id;
36 
37  const char *vendorName;
38  epicsUInt32 capacity,
39  sectorSize,
40  pageSize;
41 
42  std::vector<epicsUInt8> SN;
43  };
44 
46  void readID(ID *id);
47 
48  inline bool writable() { check(); return info.pageSize>0 && info.sectorSize>0; }
49  // worst case alignment for start address
50  inline epicsUInt32 alignement() { check(); return (info.pageSize-1)|(info.sectorSize-1); }
51  // optimal block size of write operation
52  inline epicsUInt32 blockSize() { return alignement()+1u; }
53  inline epicsUInt32 pageSize() { check(); return info.pageSize; }
54  inline epicsUInt32 sectorSize() { check(); return info.sectorSize; }
55 
56  void read(epicsUInt32 start, epicsUInt32 count, epicsUInt8 *in);
57  inline void read(epicsUInt32 start, std::vector<epicsUInt8>& in)
58  { read(start, in.size(), &in[0]); }
59 
60  void write(epicsUInt32 start, epicsUInt32 count, const epicsUInt8 *out, bool strict = true);
61  void write(epicsUInt32 start, const std::vector<epicsUInt8>& out, bool strict = true)
62  { write(start, out.size(), &out[0], strict); }
63 
64  void erase(epicsUInt32 start, epicsUInt32 count, bool strict = true);
65 private:
66  SPIDevice dev;
67  bool haveinfo;
68  ID info;
69 
70  void check();
71  unsigned status();
72 
73  void writeEnable(bool e);
74  void busyWait(double timeout, unsigned n=10);
75 
76  struct WriteEnabler
77  {
78  CFIFlash& dev;
79  explicit WriteEnabler(CFIFlash& dev) : dev(dev)
80  {}
81  void enable()
82  { dev.writeEnable(true); }
83  ~WriteEnabler()
84  { dev.writeEnable(false); }
85  };
86 };
87 
89 class epicsShareClass CFIStreamBuf : public std::streambuf
90 {
91  CFIFlash& flash;
92  epicsUInt32 pos;
93  std::vector<char> buf;
94 public:
95  CFIStreamBuf(CFIFlash& flash);
96 
97  virtual int_type underflow() OVERRIDE FINAL;
98 
99  virtual pos_type seekoff(off_type off, std::ios_base::seekdir dir, std::ios_base::openmode mode) OVERRIDE FINAL;
100  virtual pos_type seekpos(pos_type pos, std::ios_base::openmode mode) OVERRIDE FINAL;
101 };
102 
104 struct epicsShareClass XilinxBitInfo
105 {
107 
108  bool read(std::istream& strm);
109 
110  std::string project, part, date;
111 };
112 
113 } // namespace mrf
114 
115 #endif // FLASH_H
void read(epicsUInt32 start, std::vector< epicsUInt8 > &in)
Definition: flash.h:57
epicsUInt32 blockSize()
Definition: flash.h:52
const char * vendorName
Definition: flash.h:37
epicsUInt32 sectorSize()
Definition: flash.h:54
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
void write(epicsUInt32 start, const std::vector< epicsUInt8 > &out, bool strict=true)
Definition: flash.h:61
std::vector< epicsUInt8 > SN
Definition: flash.h:42
std::string project
Definition: flash.h:110
epicsUInt8 vendor
Definition: flash.h:33
bool writable()
Definition: flash.h:48
Adapt CFIFlash for use with std::istream.
Definition: flash.h:89
Definition: flash.cpp:23
epicsUInt32 alignement()
Definition: flash.h:50
epicsUInt32 pageSize()
Definition: flash.h:53